[polari] chatroomManager: Stop tying rooms' lifecycle to their channels



commit f6b73e8e57a064af10279826ecb701c67f710dbd
Author: Florian Müllner <fmuellner gnome org>
Date:   Tue Dec 10 01:07:50 2013 +0100

    chatroomManager: Stop tying rooms' lifecycle to their channels
    
    Currently rooms are created and destroyed as the corresponding
    channel becomes available or goes away. However this behavior
    can result in the application appearing sluggish when the time
    between user request and result takes too long, and it is plain
    wrong when no user interaction at all is involved (for instance
    when the network goes away). So instead, create rooms immediately
    when requested by the user, and keep them around until removed
    explicitly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=710271

 src/chatroomManager.js |   62 ++++++++++++++++++++++++++++++++++++++++-------
 src/lib/polari-room.c  |   35 ++++++++++-----------------
 src/lib/polari-room.h  |    4 ++-
 3 files changed, 68 insertions(+), 33 deletions(-)
---
diff --git a/src/chatroomManager.js b/src/chatroomManager.js
index 4b38098..9a9f420 100644
--- a/src/chatroomManager.js
+++ b/src/chatroomManager.js
@@ -42,6 +42,15 @@ const _ChatroomManager = new Lang.Class({
             this._app.release(); // no point in carrying on
         }
 
+        let joinAction = this._app.lookup_action('join-room');
+        joinAction.connect('activate', Lang.bind(this, this._onJoinActivated));
+
+        let queryAction = this._app.lookup_action('message-user');
+        queryAction.connect('activate', Lang.bind(this, this._onQueryActivated));
+
+        let leaveAction = this._app.lookup_action('leave-room');
+        leaveAction.connect('activate', Lang.bind(this, this._onLeaveActivated));
+
         this._observer = Tp.SimpleObserver.new_with_am(am, true,
             'Polari', true, Lang.bind(this, this._observeChannels));
 
@@ -100,25 +109,55 @@ const _ChatroomManager = new Lang.Class({
         action.activate(parameter);
     },
 
-    _ensureRoomForChannel: function(channel) {
-        let room = this._rooms[Polari.create_room_id_from_channel(channel)];
+    _onJoinActivated: function(action, parameter) {
+        let [accountPath, channelName, ] = parameter.deep_unpack();
+        let factory = Tp.AccountManager.dup().get_factory();
+        let account = factory.ensure_account(accountPath, []);
+
+        if (!account.enabled)
+            return;
+
+        this._ensureRoom(account, channelName, Tp.HandleType.ROOM);
+    },
+
+    _onQueryActivated: function(action, parameter) {
+        let [accountPath, channelName, ] = parameter.deep_unpack();
+        let factory = Tp.AccountManager.dup().get_factory();
+        let account = factory.ensure_account(accountPath, []);
+
+        if (!account.enabled)
+            return;
+
+        this._ensureRoom(account, channelName, Tp.HandleType.CONTACT);
+    },
+
+    _onLeaveActivated: function(action, parameter) {
+        let [id, ] = parameter.deep_unpack();
+        let room = this._rooms[id];
+        this._removeRoom(room);
+    },
+
+    _ensureRoom: function(account, channelName, type) {
+        let room = this._rooms[Polari.create_room_id(account, channelName, type)];
         if (room)
             return room;
 
-        let account = channel.get_connection().get_account();
         let room = new Polari.Room({ account: account,
-                                     channel_name: channel.get_identifier(),
-                                     type: channel.handle_type });
-        room.channel = channel;
-        room.channel.connect('invalidated', Lang.bind(this,
-            function() {
-                this._removeRoom(room);
-            }));
+                                     channel_name: channelName,
+                                     type: type });
         this._addRoom(room);
 
         return room;
     },
 
+    _ensureRoomForChannel: function(channel) {
+        let account = channel.connection.get_account();
+        let channelName = channel.identifier;
+        let room = this._ensureRoom(account, channelName, channel.handle_type);
+        room.channel = channel;
+        return room;
+    },
+
     _processRequest: function(context, connection, channels, processChannel) {
         if (connection.protocol_name != 'irc') {
             let message = 'Not implementing non-IRC protocols';
@@ -178,6 +217,9 @@ const _ChatroomManager = new Lang.Class({
             return;
         this._rooms[room.id] = room;
         this.emit('room-added', room);
+
+        if (this.roomCount == 1)
+            this.setActiveRoom(room);
     },
 
     _removeRoom: function(room) {
diff --git a/src/lib/polari-room.c b/src/lib/polari-room.c
index 9f19eab..ed5c545 100644
--- a/src/lib/polari-room.c
+++ b/src/lib/polari-room.c
@@ -86,35 +86,26 @@ G_DEFINE_TYPE_WITH_PRIVATE (PolariRoom, polari_room, G_TYPE_OBJECT)
 static void polari_room_set_channel (PolariRoom *room, TpChannel *channel);
 
 
-static char *
-polari_create_room_id (TpAccount    *account,
-                       const char   *name,
-                       TpHandleType  type)
-{
-  return g_strdup_printf ("%s/%d/%s",
-                          tp_proxy_get_object_path (TP_PROXY (account)),
-                          type, name);
-}
-
 /**
  * polari_room_id_from_channel:
- * @channel: a TpChannel
+ * @account: a TpAccount
+ * @name: the room name
+ * @type: the room type
  *
- * Returns: (transfer full): a room ID corresponding to @channel
+ * Returns: (transfer full): a room ID corresponding to @account, @name
+ *                           and @type
  */
 char *
-polari_create_room_id_from_channel (TpChannel *channel)
+polari_create_room_id (TpAccount    *account,
+                       const char   *name,
+                       TpHandleType  type)
 {
-  TpAccount *account;
-  TpHandleType type;
-  const char *name;
+  g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (name != NULL, NULL);
 
-  g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
-
-  account = tp_connection_get_account (tp_channel_get_connection (channel));
-  name = tp_channel_get_identifier (channel);
-  tp_channel_get_handle (channel, &type);
-  return polari_create_room_id (account, name, type);
+  return g_strdup_printf ("%s/%d/%s",
+                          tp_proxy_get_object_path (TP_PROXY (account)),
+                          type, name);
 }
 
 gboolean
diff --git a/src/lib/polari-room.h b/src/lib/polari-room.h
index 92338ac..9906ad2 100644
--- a/src/lib/polari-room.h
+++ b/src/lib/polari-room.h
@@ -58,7 +58,9 @@ void  polari_room_remove_member (PolariRoom *room, TpContact *member);
 
 int   polari_room_compare (PolariRoom *room, PolariRoom *other);
 
-char *polari_create_room_id_from_channel (TpChannel *channel);
+char *polari_create_room_id (TpAccount    *account,
+                             const char   *name,
+                             TpHandleType  type);
 
 G_END_DECLS
 


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