]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
Disconnect all signals from tp_chat when the UI is destroyed
[empathy.git] / libempathy-gtk / empathy-chat.c
index 5fac903a3ec7d7ed1c71cdee9a6585fe1bfec787..985eaa4cf6f3dc57a8e8682a276d55e0a734974f 100644 (file)
@@ -65,7 +65,6 @@
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
 typedef struct {
        EmpathyTpChat     *tp_chat;
-       gulong            tp_chat_destroy_handler;
        McAccount         *account;
        gchar             *id;
        gchar             *name;
@@ -168,28 +167,54 @@ chat_set_property (GObject      *object,
 }
 
 static void
-chat_connection_changed_cb (EmpathyAccountManager *manager,
-                           McAccount *account,
-                           TpConnectionStatusReason reason,
-                           TpConnectionStatus current,
-                           TpConnectionStatus previous,
-                           EmpathyChat *chat)
+chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
+                                 const GError             *error,
+                                 gpointer                  user_data)
+{
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       EmpathyTpChat *tpchat;
+
+       if (error != NULL) {
+               empathy_chat_view_append_event (chat->view,
+                       _("Failed to reconnect this chat"));
+               return;
+       }
+
+       tpchat = EMPATHY_TP_CHAT (
+               empathy_dispatch_operation_get_channel_wrapper (dispatch));
+
+       if (empathy_dispatch_operation_claim (dispatch)) {
+               empathy_chat_set_tp_chat (chat, tpchat);
+       }
+}
+
+static void
+chat_new_connection_cb (EmpathyAccountManager *manager,
+                       TpConnection *connection,
+                       EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
+       McAccount *account;
 
-       if (current == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
-           empathy_account_equal (account, priv->account) &&
-           priv->handle_type != TP_HANDLE_TYPE_NONE) {
+       account = empathy_account_manager_get_account (manager, connection);
+       if (!priv->tp_chat && empathy_account_equal (account, priv->account) &&
+           priv->handle_type != TP_HANDLE_TYPE_NONE &&
+           !EMP_STR_EMPTY (priv->id)) {
                
                DEBUG ("Account reconnected, request a new Text channel");
 
                switch (priv->handle_type) {
                        case TP_HANDLE_TYPE_CONTACT:
-                               empathy_dispatcher_chat_with_contact_id (account, priv->id,
-                                       NULL, NULL);
+                               empathy_dispatcher_chat_with_contact_id (
+                                       connection, priv->id,
+                                       chat_connect_channel_reconnected,
+                                       chat);
                                break;
                        case TP_HANDLE_TYPE_ROOM:
-                               empathy_dispatcher_join_muc (account, priv->id, NULL, NULL);
+                               empathy_dispatcher_join_muc (connection,
+                                       priv->id,
+                                       chat_connect_channel_reconnected,
+                                       chat);
                                break;
                        default:
                                g_assert_not_reached ();
@@ -350,12 +375,12 @@ chat_send (EmpathyChat  *chat,
        EmpathyChatPriv *priv;
        EmpathyMessage  *message;
 
-       priv = GET_PRIV (chat);
-
        if (EMP_STR_EMPTY (msg)) {
                return;
        }
 
+       priv = GET_PRIV (chat);
+
        chat_sent_message_add (chat, msg);
 
        if (g_str_has_prefix (msg, "/clear")) {
@@ -363,10 +388,19 @@ chat_send (EmpathyChat  *chat,
                return;
        }
 
-       message = empathy_message_new (msg);
+       /* Blacklist messages begining by '/', except for "/me" and "/say"
+        * because they are handled in EmpathyMessage */
+       if (msg[0] == '/' &&
+           !g_str_has_prefix (msg, "/me") &&
+           !g_str_has_prefix (msg, "/say")) {
+               empathy_chat_view_append_event (chat->view,
+                       _("Unsupported command"));
+               return;
+       }
 
+       /* We can send the message */
+       message = empathy_message_new (msg);
        empathy_tp_chat_send (priv->tp_chat, message);
-
        g_object_unref (message);
 }
 
@@ -559,28 +593,6 @@ chat_property_changed_cb (EmpathyTpChat *tp_chat,
        }
 }
 
-static gboolean
-chat_get_is_command (const gchar *str)
-{
-       g_return_val_if_fail (str != NULL, FALSE);
-
-       if (str[0] != '/') {
-               return FALSE;
-       }
-
-       if (g_str_has_prefix (str, "/me")) {
-               return TRUE;
-       }
-       else if (g_str_has_prefix (str, "/nick")) {
-               return TRUE;
-       }
-       else if (g_str_has_prefix (str, "/topic")) {
-               return TRUE;
-       }
-
-       return FALSE;
-}
-
 static void
 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
                                   EmpathyChat    *chat)
@@ -640,8 +652,8 @@ chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
 
                str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
 
-               /* spell check string */
-               if (!chat_get_is_command (str)) {
+               /* spell check string if not a command */
+               if (str[0] != '/') {
                        correct = empathy_spell_check (str);
                } else {
                        correct = TRUE;
@@ -1026,14 +1038,31 @@ chat_input_populate_popup_cb (GtkTextView *view,
        }
 }
 
+static gboolean
+chat_log_filter (EmpathyMessage *message,
+                gpointer user_data)
+{
+       EmpathyChat *chat = (EmpathyChat *) user_data;
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       const GList *pending;
+
+       pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+
+       for (; pending; pending = g_list_next (pending)) {
+               if (empathy_message_equal (message, pending->data)) {
+                       return FALSE;
+               }
+       }
+
+       return TRUE;
+}
+
 static void
 chat_add_logs (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
        gboolean         is_chatroom;
        GList           *messages, *l;
-       guint            i = 0;
-       const GList     *pending_messages, *m;
 
        if (!priv->id) {
                return;
@@ -1044,28 +1073,17 @@ chat_add_logs (EmpathyChat *chat)
 
        /* Add messages from last conversation */
        is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
-       messages = empathy_log_manager_get_last_messages (priv->log_manager,
-                                                         priv->account,
-                                                         priv->id,
-                                                         is_chatroom);
 
-       pending_messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+       messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
+                                                             priv->account,
+                                                             priv->id,
+                                                             is_chatroom,
+                                                             5,
+                                                             chat_log_filter,
+                                                             chat);
 
-       for (l = g_list_last (messages); l; l = g_list_previous (l)) {
-               if (i < 10) {
-                       gboolean found = FALSE;
-
-                       for (m = pending_messages; m; m = g_list_next (m)) {
-                               if (empathy_message_equal (l->data, m->data)) {
-                                       found = TRUE;
-                               }
-                       }
-
-                       if (!found) {
-                               empathy_chat_view_append_message (chat->view, l->data);
-                               i++;
-                       }
-               }
+       for (l = messages; l; l = g_list_next (l)) {
+               empathy_chat_view_append_message (chat->view, l->data);
                g_object_unref (l->data);
        }
 
@@ -1120,10 +1138,6 @@ chat_members_changed_cb (EmpathyTpChat  *tp_chat,
        if (priv->block_events_timeout_id == 0) {
                gchar *str;
 
-               empathy_contact_run_until_ready (contact,
-                                                EMPATHY_CONTACT_READY_NAME,
-                                                NULL);
-
                if (is_member) {
                        str = g_strdup_printf (_("%s has joined the room"),
                                               empathy_contact_get_name (contact));
@@ -1238,6 +1252,7 @@ chat_destroy_cb (EmpathyTpChat *tp_chat,
                return;
        }
 
+       chat_composing_remove_timeout (chat);
        g_object_unref (priv->tp_chat);
        priv->tp_chat = NULL;
        g_object_notify (G_OBJECT (chat), "tp-chat");
@@ -1268,16 +1283,14 @@ static void
 chat_create_ui (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       GladeXML        *glade;
+       GtkBuilder      *gui;
        GList           *list = NULL; 
        gchar           *filename;
        GtkTextBuffer   *buffer;
 
-       filename = empathy_file_lookup ("empathy-chat.glade",
+       filename = empathy_file_lookup ("empathy-chat.ui",
                                        "libempathy-gtk");
-       glade = empathy_glade_get_file (filename,
-                                       "chat_widget",
-                                       NULL,
+       gui = empathy_builder_get_file (filename,
                                        "chat_widget", &priv->widget,
                                        "hpaned", &priv->hpaned,
                                        "vbox_left", &priv->vbox_left,
@@ -1288,7 +1301,6 @@ chat_create_ui (EmpathyChat *chat)
                                        "scrolled_window_contacts", &priv->scrolled_window_contacts,
                                        NULL);
        g_free (filename);
-       g_object_unref (glade);
 
        /* Add message view. */
        chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
@@ -1354,6 +1366,7 @@ chat_create_ui (EmpathyChat *chat)
 
        /* Add the main widget in the chat widget */
        gtk_container_add (GTK_CONTAINER (chat), priv->widget);
+       g_object_unref (gui);
 }
 
 static void
@@ -1416,13 +1429,26 @@ chat_finalize (GObject *object)
        chat_composing_remove_timeout (chat);
 
        g_signal_handlers_disconnect_by_func (priv->account_manager,
-                                             chat_connection_changed_cb, object);
+                                             chat_new_connection_cb, object);
 
        g_object_unref (priv->account_manager);
        g_object_unref (priv->log_manager);
 
        if (priv->tp_chat) {
-               g_signal_handler_disconnect (priv->tp_chat, priv->tp_chat_destroy_handler);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_destroy_cb, chat);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_message_received_cb, chat);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_send_error_cb, chat);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_state_changed_cb, chat);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_property_changed_cb, chat);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_members_changed_cb, chat);
+               g_signal_handlers_disconnect_by_func (priv->tp_chat,
+                       chat_remote_contact_changed_cb, chat);
                empathy_tp_chat_close (priv->tp_chat);
                g_object_unref (priv->tp_chat);
        }
@@ -1440,6 +1466,7 @@ chat_finalize (GObject *object)
        g_free (priv->id);
        g_free (priv->name);
        g_free (priv->subject);
+       g_completion_free (priv->completion);
 
        G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
 }
@@ -1559,8 +1586,8 @@ empathy_chat_init (EmpathyChat *chat)
        priv->account_manager = empathy_account_manager_dup_singleton ();
 
        g_signal_connect (priv->account_manager,
-                         "account-connection-changed",
-                         G_CALLBACK (chat_connection_changed_cb),
+                         "new-connection",
+                         G_CALLBACK (chat_new_connection_cb),
                          chat);
 
        /* Block events for some time to avoid having "has come online" or
@@ -1594,6 +1621,7 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
                          EmpathyTpChat *tp_chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
+       TpConnection    *connection;
 
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
        g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
@@ -1608,8 +1636,14 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
        }
 
        priv->tp_chat = g_object_ref (tp_chat);
-       priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
+       connection = empathy_tp_chat_get_connection (priv->tp_chat);
+       priv->account = empathy_account_manager_get_account (priv->account_manager,
+                                                            connection);
+       g_object_ref (priv->account);
 
+       g_signal_connect (tp_chat, "destroy",
+                         G_CALLBACK (chat_destroy_cb),
+                         chat);
        g_signal_connect (tp_chat, "message-received",
                          G_CALLBACK (chat_message_received_cb),
                          chat);
@@ -1628,10 +1662,6 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
        g_signal_connect_swapped (tp_chat, "notify::remote-contact",
                                  G_CALLBACK (chat_remote_contact_changed_cb),
                                  chat);
-       priv->tp_chat_destroy_handler =
-               g_signal_connect (tp_chat, "destroy",
-                         G_CALLBACK (chat_destroy_cb),
-                         chat);
 
        chat_remote_contact_changed_cb (chat);
 
@@ -1711,20 +1741,6 @@ empathy_chat_get_remote_contact (EmpathyChat *chat)
        return priv->remote_contact;
 }
 
-guint
-empathy_chat_get_members_count (EmpathyChat *chat)
-{
-       EmpathyChatPriv *priv = GET_PRIV (chat);
-
-       g_return_val_if_fail (EMPATHY_IS_CHAT (chat), 0);
-
-       if (priv->tp_chat) {
-               return empathy_tp_chat_get_members_count (priv->tp_chat);
-       }
-
-       return 0;
-}
-
 GtkWidget *
 empathy_chat_get_contact_menu (EmpathyChat *chat)
 {