When a new text channel arrives, check if there is no existing GossipChat
authorXavier Claessens <xclaesse@gmail.com>
Sun, 13 May 2007 20:29:46 +0000 (20:29 +0000)
committerXavier Claessens <xclaesse@src.gnome.org>
Sun, 13 May 2007 20:29:46 +0000 (20:29 +0000)
2006-05-13  Xavier Claessens  <xclaesse@gmail.com>

* libempathy-gtk/gossip-chat-window.c:
* libempathy-gtk/gossip-chat-window.h:
* src/empathy-chat-main.c:
* libempathy/empathy-tp-chat.c:
* libempathy/empathy-tp-chat.h: When a new text channel arrives,
check
if there is no existing GossipChat object for it.

svn path=/trunk/; revision=56

ChangeLog
libempathy-gtk/gossip-chat-window.c
libempathy-gtk/gossip-chat-window.h
libempathy/empathy-tp-chat.c
libempathy/empathy-tp-chat.h
src/empathy-chat-main.c

index 8beeea2..9830207 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2006-05-13  Xavier Claessens  <xclaesse@gmail.com>
+
+       * libempathy-gtk/gossip-chat-window.c:
+       * libempathy-gtk/gossip-chat-window.h:
+       * src/empathy-chat-main.c:
+       * libempathy/empathy-tp-chat.c:
+       * libempathy/empathy-tp-chat.h: When a new text channel arrives, check
+       if there is no existing GossipChat object for it.
+
 2006-05-13  Xavier Claessens  <xclaesse@gmail.com>
 
        * src/Makefile.am:
index 62ace37..fb11237 100644 (file)
@@ -1874,3 +1874,31 @@ gossip_chat_window_has_focus (GossipChatWindow *window)
 
        return has_focus;
 }
+
+GossipChat *
+gossip_chat_window_find_chat_by_id (const gchar *id)
+{
+       GList *l;
+
+       for (l = chat_windows; l; l = l->next) {
+               GossipChatWindowPriv *priv;
+               GossipChatWindow     *window;
+               GList                *ll;
+
+               window = l->data;
+               priv = GET_PRIV (window);
+
+               for (ll = priv->chats; ll; ll = ll->next) {
+                       GossipChat *chat;
+
+                       chat = ll->data;
+
+                       if (strcmp (id, gossip_chat_get_id (chat)) == 0) {
+                               return chat;
+                       }
+               }
+       }
+
+       return NULL;
+}
+
index 783dc5b..18b06ad 100644 (file)
@@ -70,6 +70,7 @@ void              gossip_chat_window_move_chat       (GossipChatWindow *old_wind
 void              gossip_chat_window_switch_to_chat  (GossipChatWindow *window,
                                                      GossipChat       *chat);
 gboolean          gossip_chat_window_has_focus       (GossipChatWindow *window);
+GossipChat *      gossip_chat_window_find_chat_by_id (const gchar      *id);
 
 G_END_DECLS
 
index 2e5444f..58950f6 100644 (file)
@@ -386,11 +386,7 @@ empathy_tp_chat_set_state (EmpathyTpChat             *chat,
 const gchar *
 empathy_tp_chat_get_id (EmpathyTpChat *chat)
 {
-       EmpathyTpChatPriv  *priv;
-       TpConn             *tp_conn;
-       GArray             *handles;
-       gchar             **names;
-       GError             *error = NULL;
+       EmpathyTpChatPriv *priv;
 
        g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
 
@@ -400,12 +396,33 @@ empathy_tp_chat_get_id (EmpathyTpChat *chat)
                return priv->id;
        }
 
-       tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (handles, priv->tp_chan->handle);
+       priv->id = empathy_tp_chat_build_id (priv->account, priv->tp_chan);
+
+       return priv->id;
+}
 
+gchar *
+empathy_tp_chat_build_id (McAccount *account,
+                         TpChan    *tp_chan)
+{
+       MissionControl *mc;
+       TpConn         *tp_conn;
+       GArray         *handles;
+       gchar         **names;
+       gchar          *id;
+       GError         *error = NULL;
+       
+       g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+       g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
+
+       mc = gossip_mission_control_new ();
+       tp_conn = mission_control_get_connection (mc, account, NULL);
+       g_object_unref (mc);
+
+       handles = g_array_new (FALSE, FALSE, sizeof (guint));
+       g_array_append_val (handles, tp_chan->handle);
        if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
-                                     priv->tp_chan->handle_type,
+                                     tp_chan->handle_type,
                                      handles,
                                      &names,
                                      &error)) {
@@ -420,14 +437,14 @@ empathy_tp_chat_get_id (EmpathyTpChat *chat)
        }
 
        /* A handle name is unique only for a specific account */
-       priv->id = g_strdup_printf ("%s/%s",
-                                   mc_account_get_unique_name (priv->account),
-                                   *names);
+       id = g_strdup_printf ("%s/%s",
+                             mc_account_get_unique_name (account),
+                             *names);
 
        g_strfreev (names);
        g_object_unref (tp_conn);
 
-       return priv->id;
+       return id;
 }
 
 static void
index 28b7804..0bd3395 100644 (file)
@@ -65,6 +65,8 @@ void           empathy_tp_chat_send             (EmpathyTpChat             *chat
 void           empathy_tp_chat_set_state        (EmpathyTpChat             *chat,
                                                 TelepathyChannelChatState  state);
 const gchar *  empathy_tp_chat_get_id           (EmpathyTpChat             *chat);
+gchar *        empathy_tp_chat_build_id         (McAccount                 *account,
+                                                TpChan                    *tp_chan);
 
 G_END_DECLS
 
index 2826e4b..5c34784 100644 (file)
@@ -37,6 +37,7 @@
 #include <libempathy/empathy-chandler.h>
 #include <libempathy/empathy-contact-manager.h>
 #include <libempathy/empathy-contact-list.h>
+#include <libempathy/empathy-tp-chat.h>
 #include <libempathy-gtk/gossip-private-chat.h>
 
 #define DEBUG_DOMAIN "ChatMain"
@@ -101,17 +102,38 @@ new_channel_cb (EmpathyChandler *chandler,
                TpChan          *tp_chan,
                gpointer         user_data)
 {
+       MissionControl *mc;
+       McAccount      *account;
+       GossipChat     *chat;
+       gchar          *id;
+
+       mc = gossip_mission_control_new ();
+       account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
+       id = empathy_tp_chat_build_id (account, tp_chan);
+
+       chat = gossip_chat_window_find_chat_by_id (id);
+       if (chat) {
+               /* The chat already exists */
+               if (!gossip_chat_is_connected (chat)) {
+                       EmpathyTpChat *tp_chat;
+
+                       /* The chat died, give him the new text channel */
+                       tp_chat = empathy_tp_chat_new (account, tp_chan);
+                       gossip_chat_set_tp_chat (chat, tp_chat);
+                       g_object_unref (tp_chat);
+               }
+               gossip_chat_present (chat);
+
+               goto OUT;
+       }
+
        if (tp_chan->handle_type == TP_HANDLE_TYPE_CONTACT) {
-               MissionControl        *mc;
-               McAccount             *account;
                EmpathyContactManager *manager;
                EmpathyContactList    *list;
                GossipContact         *contact;
                GossipPrivateChat     *chat;
 
-               /* We have a private chat channel */
-               mc = gossip_mission_control_new ();
-               account = mission_control_get_account_for_connection (mc, tp_conn, NULL);
+               /* We have a new private chat channel */
                manager = empathy_contact_manager_new ();
                list = empathy_contact_manager_get_list (manager, account);
                contact = empathy_contact_list_get_from_handle (list, tp_chan->handle);
@@ -126,12 +148,15 @@ new_channel_cb (EmpathyChandler *chandler,
 
                gossip_chat_present (GOSSIP_CHAT (chat));
 
-               g_object_unref (mc);
-               g_object_unref (account);
                g_object_unref (contact);
                g_object_unref (chat);
                g_object_unref (manager);
        }
+
+OUT:
+       g_free (id);
+       g_object_unref (account);
+       g_object_unref (mc);
 }
 
 int