[polari/wip/fmuellner/misc-cleanups: 26/33] accountsMonitor: Provide lookup function



commit 7dc1eaf378e2a1f39f5ba9e053dba6bf9b330e7a
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Jul 30 17:02:47 2016 +0200

    accountsMonitor: Provide lookup function
    
    We are currently using TpSimpleClientFactory.ensure_account()
    when we need to look up accounts, which is slightly dodgy as it
    bypasses the account filtering in AccountsMonitor. To address
    this, add a small wrapper around Map.get().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769582

 src/accountsMonitor.js |    4 ++++
 src/application.js     |   34 ++++++++++++++--------------------
 src/chatroomManager.js |   24 ++++++++----------------
 3 files changed, 26 insertions(+), 36 deletions(-)
---
diff --git a/src/accountsMonitor.js b/src/accountsMonitor.js
index 6e466d5..30dc870 100644
--- a/src/accountsMonitor.js
+++ b/src/accountsMonitor.js
@@ -45,6 +45,10 @@ const AccountsMonitor = new Lang.Class({
         return this._accountManager;
     },
 
+    lookupAccount: function(accountPath) {
+        return this._accounts.get(accountPath);
+    },
+
     _onPrepared: function(am, res) {
         try {
             am.prepare_finish(res);
diff --git a/src/application.js b/src/application.js
index 2968589..f8d6ce3 100644
--- a/src/application.js
+++ b/src/application.js
@@ -51,7 +51,7 @@ const Application = new Lang.Class({
 
         this._accountsMonitor.connect('account-removed', Lang.bind(this,
             function(am, account) {
-                this._removeSavedChannelsForAccount(account);
+                this._removeSavedChannelsForAccount(account.object_path);
             }));
 
         this._settings = new Gio.Settings({ schema_id: 'org.gnome.Polari' });
@@ -323,9 +323,8 @@ const Application = new Lang.Class({
                                  GLib.Variant.new('aa{sv}', savedChannels));
     },
 
-    _removeSavedChannelsForAccount: function(account) {
+    _removeSavedChannelsForAccount: function(accountPath) {
         let savedChannels = this._settings.get_value('saved-channel-list').deep_unpack();
-        let accountPath = GLib.Variant.new('s', account.get_object_path());
 
         let savedChannels = savedChannels.filter(function(a) {
             return !a.account.equal(accountPath);
@@ -350,16 +349,12 @@ const Application = new Lang.Class({
     },
 
     _requestChannel: function(accountPath, targetType, targetId, time, callback) {
-        // have this in AccountMonitor?
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
-
-        if (!account.enabled) {
-            // if we are requesting a channel for a disabled account, we
-            // are restoring saved channels; if the account has also never
-            // been online, it was removed since the channel was saved
-            if (!account.has_been_online)
-                this._removeSavedChannelsForAccount(account);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
+
+        if (!account || !account.enabled) {
+            // the account was removed since the channel was saved
+            if (!account)
+                this._removeSavedChannelsForAccount(accountPath);
             return;
         }
 
@@ -482,11 +477,12 @@ const Application = new Lang.Class({
 
     _onJoinRoom: function(action, parameter) {
         let [accountPath, channelName, time] = parameter.deep_unpack();
+        let account = this._accountsMonitor.lookupAccount(accountPath);
+        if (!account)
+            return;
+
         this._requestChannel(accountPath, Tp.HandleType.ROOM,
                              channelName, time);
-
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
         this._addSavedChannel(account, channelName);
     },
 
@@ -551,8 +547,7 @@ const Application = new Lang.Class({
 
     _onRemoveConnection: function(action, parameter){
         let accountPath = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
         account.set_enabled_async(false, Lang.bind(this,
             function() {
                 let label = _("%s removed.").format(account.display_name);
@@ -574,8 +569,7 @@ const Application = new Lang.Class({
 
     _onEditConnection: function(action, parameter) {
         let accountPath = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
         let dialog = new Connections.ConnectionProperties(account);
         dialog.transient_for = this._window;
         dialog.connect('response', Lang.bind(this,
diff --git a/src/chatroomManager.js b/src/chatroomManager.js
index 3fe7726..8f9479f 100644
--- a/src/chatroomManager.js
+++ b/src/chatroomManager.js
@@ -283,8 +283,7 @@ const _ChatroomManager = new Lang.Class({
 
     _onConnectAccountActivated: function(action, parameter) {
         let accountPath = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
         account.request_presence_async(Tp.ConnectionPresenceType.AVAILABLE,
                                        'available',
                                        account.requested_status_message,
@@ -293,18 +292,13 @@ const _ChatroomManager = new Lang.Class({
 
     _onReconnectAccountActivated: function(action, parameter) {
         let accountPath = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
-        account.reconnect_async(Lang.bind(this,
-            function (a, res){
-                a.reconnect_finish(res);
-            }));
+        let account = this._accountsMonitor.lookupAccount(accountPath);
+        account.reconnect_async((a, res) => { a.reconnect_finish(res); });
     },
 
     _onAuthenticateAccountActivated: function(action, parameter) {
         let [accountPath, password] = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
 
         let prompt = new GLib.Variant('b', password.length > 0);
         let params = GLib.Variant.new('a{sv}', { 'password-prompt': prompt });
@@ -320,10 +314,9 @@ const _ChatroomManager = new Lang.Class({
 
     _onJoinActivated: function(action, parameter) {
         let [accountPath, channelName, time] = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
 
-        if (!account.enabled)
+        if (!account || !account.enabled)
             return;
 
         let room = this._ensureRoom(account, channelName, Tp.HandleType.ROOM);
@@ -334,10 +327,9 @@ const _ChatroomManager = new Lang.Class({
 
     _onQueryActivated: function(action, parameter) {
         let [accountPath, channelName, message, time] = parameter.deep_unpack();
-        let factory = Tp.AccountManager.dup().get_factory();
-        let account = factory.ensure_account(accountPath, []);
+        let account = this._accountsMonitor.lookupAccount(accountPath);
 
-        if (!account.enabled)
+        if (!account || !account.enabled)
             return;
 
         let room = this._ensureRoom(account, channelName, Tp.HandleType.CONTACT);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]