]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
Don't send unknown commands. Fixes bug #577026
[empathy.git] / libempathy-gtk / empathy-chat.c
index ce5f69880519d3b4cdbee8009e4d2744fe7cc66b..d11f331610670887e24a875e44bc85733acd60f5 100644 (file)
@@ -40,6 +40,7 @@
 #include <libempathy/empathy-log-manager.h>
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-utils.h>
+#include <libempathy/empathy-dispatcher.h>
 
 #include "empathy-chat.h"
 #include "empathy-conf.h"
@@ -64,6 +65,7 @@
 #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;
@@ -177,22 +179,23 @@ chat_connection_changed_cb (EmpathyAccountManager *manager,
 
        if (current == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
            empathy_account_equal (account, priv->account) &&
-           priv->handle_type != TP_HANDLE_TYPE_NONE) {
-               TpConnection *connection;
-               MissionControl *mc;
-
+           priv->handle_type != TP_HANDLE_TYPE_NONE &&
+           !EMP_STR_EMPTY (priv->id)) {
+               
                DEBUG ("Account reconnected, request a new Text channel");
 
-               mc = empathy_mission_control_new ();
-               connection = mission_control_get_tpconnection (mc, account, NULL);
-               tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
-               empathy_connection_request_channel (connection, -1,
-                                                   TP_IFACE_CHANNEL_TYPE_TEXT,
-                                                   priv->handle_type,
-                                                   priv->id, TRUE,
-                                                   NULL, NULL, NULL, NULL);
-               g_object_unref (connection);
-               g_object_unref (mc);
+               switch (priv->handle_type) {
+                       case TP_HANDLE_TYPE_CONTACT:
+                               empathy_dispatcher_chat_with_contact_id (account, priv->id,
+                                       NULL, NULL);
+                               break;
+                       case TP_HANDLE_TYPE_ROOM:
+                               empathy_dispatcher_join_muc (account, priv->id, NULL, NULL);
+                               break;
+                       default:
+                               g_assert_not_reached ();
+                               break;
+               }
        }
 }
 
@@ -346,26 +349,33 @@ chat_send (EmpathyChat  *chat,
           const gchar *msg)
 {
        EmpathyChatPriv *priv;
-       EmpathyMessage  *message;
-
-       priv = GET_PRIV (chat);
 
-       if (G_STR_EMPTY (msg)) {
+       if (EMP_STR_EMPTY (msg)) {
                return;
        }
 
+       priv = GET_PRIV (chat);
+
        chat_sent_message_add (chat, msg);
 
+       /* If this is not a command, send the message */
+       if (msg[0] != '/') {
+               EmpathyMessage  *message;
+
+               message = empathy_message_new (msg);
+               empathy_tp_chat_send (priv->tp_chat, message);
+               g_object_unref (message);
+               return;
+       }
+
+       /* Check for all supported commands */
        if (g_str_has_prefix (msg, "/clear")) {
                empathy_chat_view_clear (chat->view);
                return;
        }
 
-       message = empathy_message_new (msg);
-
-       empathy_tp_chat_send (priv->tp_chat, message);
-
-       g_object_unref (message);
+       /* This is an unknown command, display a message to the user */
+       empathy_chat_view_append_event (chat->view, _("Unsupported command"));
 }
 
 static void
@@ -452,9 +462,7 @@ chat_state_changed_cb (EmpathyTpChat      *tp_chat,
 }
 
 static void
-chat_message_received_cb (EmpathyTpChat  *tp_chat,
-                         EmpathyMessage *message,
-                         EmpathyChat    *chat)
+chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
        EmpathyContact  *sender;
@@ -465,25 +473,25 @@ chat_message_received_cb (EmpathyTpChat  *tp_chat,
                empathy_contact_get_name (sender),
                empathy_contact_get_handle (sender));
 
-       if (priv->id) {
-               gboolean is_chatroom;
-
-               is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
-               empathy_log_manager_add_message (priv->log_manager,
-                                                priv->id, is_chatroom,
-                                                message);
-       }
-
        empathy_chat_view_append_message (chat->view, message);
 
-       /* We received a message so the contact is no more composing */
-       chat_state_changed_cb (tp_chat, sender,
+       /* We received a message so the contact is no longer composing */
+       chat_state_changed_cb (priv->tp_chat, sender,
                               TP_CHANNEL_CHAT_STATE_ACTIVE,
                               chat);
 
        g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
 }
 
+static void
+chat_message_received_cb (EmpathyTpChat  *tp_chat,
+                         EmpathyMessage *message,
+                         EmpathyChat    *chat)
+{
+       chat_message_received (chat, message);
+       empathy_tp_chat_acknowledge_message (tp_chat, message);
+}
+
 static void
 chat_send_error_cb (EmpathyTpChat          *tp_chat,
                    EmpathyMessage         *message,
@@ -534,7 +542,7 @@ chat_property_changed_cb (EmpathyTpChat *tp_chat,
                priv->subject = g_value_dup_string (value);
                g_object_notify (G_OBJECT (chat), "subject");
 
-               if (G_STR_EMPTY (priv->subject)) {
+               if (EMP_STR_EMPTY (priv->subject)) {
                        gtk_widget_hide (priv->hbox_topic);
                } else {
                        gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
@@ -543,7 +551,7 @@ chat_property_changed_cb (EmpathyTpChat *tp_chat,
                if (priv->block_events_timeout_id == 0) {
                        gchar *str;
 
-                       if (!G_STR_EMPTY (priv->subject)) {
+                       if (!EMP_STR_EMPTY (priv->subject)) {
                                str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
                        } else {
                                str = g_strdup (_("No topic defined"));
@@ -978,7 +986,7 @@ chat_input_populate_popup_cb (GtkTextView *view,
        /* Add the Send menu item. */
        gtk_text_buffer_get_bounds (buffer, &start, &end);
        str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
-       if (!G_STR_EMPTY (str)) {
+       if (!EMP_STR_EMPTY (str)) {
                item = gtk_menu_item_new_with_mnemonic (_("_Send"));
                g_signal_connect (G_OBJECT (item), "activate",
                                  G_CALLBACK (chat_text_send_cb), chat);
@@ -1003,7 +1011,7 @@ chat_input_populate_popup_cb (GtkTextView *view,
                str = gtk_text_buffer_get_text (buffer,
                                                &start, &end, FALSE);
        }
-       if (!G_STR_EMPTY (str)) {
+       if (!EMP_STR_EMPTY (str)) {
                chat_spell = chat_spell_new (chat, str, start, end);
                g_object_set_data_full (G_OBJECT (menu),
                                        "chat_spell", chat_spell,
@@ -1026,14 +1034,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            num_messages;
-       guint            i;
 
        if (!priv->id) {
                return;
@@ -1044,25 +1069,20 @@ 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);
-       num_messages  = g_list_length (messages);
-
-       /* Only keep the 10 last messages */
-       for (i = 0; num_messages - i > 10; i++) {
-               EmpathyMessage *message;
-
-               message = messages->data;
-               messages = g_list_remove (messages, message);
-               g_object_unref (message);
-       }
 
-       for (l = messages; l; l = l->next) {
+       messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
+                                                             priv->account,
+                                                             priv->id,
+                                                             is_chatroom,
+                                                             5,
+                                                             chat_log_filter,
+                                                             chat);
+
+       for (l = messages; l; l = g_list_next (l)) {
                empathy_chat_view_append_message (chat->view, l->data);
                g_object_unref (l->data);
        }
+
        g_list_free (messages);
 
        /* Turn back on scrolling */
@@ -1232,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");
@@ -1241,6 +1262,23 @@ chat_destroy_cb (EmpathyTpChat *tp_chat,
        chat_set_show_contacts (chat, FALSE);
 }
 
+static void
+show_pending_messages (EmpathyChat *chat) {
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       const GList *messages, *l;
+
+       if (chat->view == NULL || priv->tp_chat == NULL)
+               return;
+
+       messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+
+       for (l = messages; l != NULL ; l = g_list_next (l)) {
+               EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
+               chat_message_received (chat, message);
+       }
+       empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
+}
+
 static void
 chat_create_ui (EmpathyChat *chat)
 {
@@ -1399,6 +1437,8 @@ chat_finalize (GObject *object)
        g_object_unref (priv->log_manager);
 
        if (priv->tp_chat) {
+               g_signal_handler_disconnect (priv->tp_chat, priv->tp_chat_destroy_handler);
+               empathy_tp_chat_close (priv->tp_chat);
                g_object_unref (priv->tp_chat);
        }
        if (priv->account) {
@@ -1415,6 +1455,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);
 }
@@ -1426,6 +1467,7 @@ chat_constructed (GObject *object)
 
        chat_create_ui (chat);
        chat_add_logs (chat);
+       show_pending_messages (chat);
 }
 
 static void
@@ -1602,7 +1644,8 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
        g_signal_connect_swapped (tp_chat, "notify::remote-contact",
                                  G_CALLBACK (chat_remote_contact_changed_cb),
                                  chat);
-       g_signal_connect (tp_chat, "destroy",
+       priv->tp_chat_destroy_handler =
+               g_signal_connect (tp_chat, "destroy",
                          G_CALLBACK (chat_destroy_cb),
                          chat);
 
@@ -1615,12 +1658,14 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
                }
        }
 
-       empathy_tp_chat_set_acknowledge (priv->tp_chat, TRUE);
-       empathy_tp_chat_emit_pendings (priv->tp_chat);
-
        g_object_notify (G_OBJECT (chat), "tp-chat");
        g_object_notify (G_OBJECT (chat), "id");
        g_object_notify (G_OBJECT (chat), "account");
+
+       /* This is a noop when tp-chat is set at object construction time and causes
+        * the pending messages to be show when it's set on the object after it has
+        * been created */
+       show_pending_messages (chat);
 }
 
 McAccount *