[polari/wip/fmuellner/room-list: 2/11] serverRoomManager:



commit daa5765bce818b6170664bd873bd73f1354f818e
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Nov 10 22:14:53 2016 +0100

    serverRoomManager:

 src/application.js                     |    2 +
 src/org.gnome.Polari.src.gresource.xml |    1 +
 src/serverRoomManager.js               |   90 ++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 0 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 7818776..56a3474 100644
--- a/src/application.js
+++ b/src/application.js
@@ -12,6 +12,7 @@ const Lang = imports.lang;
 const MainWindow = imports.mainWindow;
 const PasteManager = imports.pasteManager;
 const RoomManager = imports.roomManager;
+const ServerRoomManager = imports.serverRoomManager;
 const TelepathyClient = imports.telepathyClient;
 const UserTracker = imports.userTracker;
 const Utils = imports.utils;
@@ -169,6 +170,7 @@ const Application = new Lang.Class({
         this._accountsMonitor = AccountsMonitor.getDefault();
         this._userStatusMonitor = UserTracker.getUserStatusMonitor();
         this._networksManager = NetworksManager.getDefault();
+        this._serverRoomManager = ServerRoomManager.getDefault();
 
         this._accountsMonitor.connect('account-status-changed',
                                       Lang.bind(this, this._onAccountStatusChanged));
diff --git a/src/org.gnome.Polari.src.gresource.xml b/src/org.gnome.Polari.src.gresource.xml
index 17dc187..a526f25 100644
--- a/src/org.gnome.Polari.src.gresource.xml
+++ b/src/org.gnome.Polari.src.gresource.xml
@@ -16,6 +16,7 @@
     <file>roomList.js</file>
     <file>roomManager.js</file>
     <file>roomStack.js</file>
+    <file>serverRoomManager.js</file>
     <file>tabCompletion.js</file>
     <file>telepathyClient.js</file>
     <file>userList.js</file>
diff --git a/src/serverRoomManager.js b/src/serverRoomManager.js
new file mode 100644
index 0000000..78a4c30
--- /dev/null
+++ b/src/serverRoomManager.js
@@ -0,0 +1,90 @@
+const GLib = imports.gi.GLib;
+const Tp = imports.gi.TelepathyGLib;
+
+const AccountsMonitor = imports.accountsMonitor;
+const Lang = imports.lang;
+const Signals = imports.signals;
+
+let _singleton = null;
+
+function getDefault() {
+    if (_singleton == null)
+        _singleton = new _ServerRoomManager();
+    return _singleton;
+}
+
+const _ServerRoomManager = new Lang.Class({
+    Name: '_ServerRoomManager',
+
+    _init: function() {
+        this._roomLists = new Map();
+
+        this._accountsMonitor = AccountsMonitor.getDefault();
+        this._accountsMonitor.connect('account-status-changed',
+                                      Lang.bind(this, this._onAccountStatusChanged));
+        this._accountsMonitor.connect('account-removed',
+                                      Lang.bind(this, this._onAccountRemoved));
+        this._accountsMonitor.prepare(() => {
+            this._accountsMonitor.enabledAccounts.forEach(a => {
+                this._onAccountStatusChanged(this._accountsMonitor, a);
+            });
+        });
+    },
+
+    getRoomInfos: function(account) {
+        let roomList = this._roomLists.get(account);
+        if (!roomList || roomList.list.listing)
+            return [];
+        return roomList.rooms.slice();
+    },
+
+    isLoading: function(account) {
+        let roomList = this._roomLists.get(account);
+        if (!roomList)
+            return false;
+        return roomList.list.listing;
+    },
+
+    _onAccountStatusChanged: function(mon, account) {
+        if (account.connection_status != Tp.ConnectionStatus.CONNECTED)
+            return;
+
+        if (this._roomLists.has(account))
+            return;
+
+        let roomList = new Tp.RoomList({ account: account });
+        roomList.init_async(GLib.PRIORITY_DEFAULT, null, (o, res) => {
+            roomList.init_finish(res);
+            roomList.start();
+        });
+        roomList.connect('got-room', Lang.bind(this, this._onGotRoom));
+        roomList.connect('notify::listing',
+                         Lang.bind(this, this._onListingChanged));
+        this._roomLists.set(account, { list: roomList, rooms: [] });
+    },
+
+    _onAccountRemoved: function(mon, account) {
+        let roomList = this._roomLists.get(account);
+        if (!roomList)
+            return;
+
+        roomList.list.run_dispose();
+        this._roomLists.delete(account);
+    },
+
+    _onGotRoom: function(list, roomInfo) {
+        let roomList = this._roomLists.get(list.account);
+        if (!roomList)
+            return;
+
+        debug('Got room %s for account %s'.format(roomInfo.get_name(),
+                                                  list.account.display_name));
+        roomList.rooms.push(roomInfo);
+    },
+
+    _onListingChanged: function(list) {
+        if (!list.listing)
+            this.emit('loaded', list.account);
+    }
+});
+Signals.addSignalMethods(_ServerRoomManager.prototype);


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