]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
Merge branch 'people-nearby-fake-group-613558'
[empathy.git] / libempathy-gtk / empathy-chat.c
index dec4757dbb2f0bec5d001dd8849f06a658dd4853..36b4136ee40154d667c9c80c54c455b97c82fb79 100644 (file)
 
 #include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
+#ifdef ENABLE_TPL
+#include <telepathy-logger/log-manager.h>
+#else
 
 #include <libempathy/empathy-log-manager.h>
+#endif /* ENABLE_TPL */
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-dispatcher.h>
 #include "empathy-contact-list-store.h"
 #include "empathy-contact-list-view.h"
 #include "empathy-contact-menu.h"
+#include "empathy-search-bar.h"
 #include "empathy-theme-manager.h"
 #include "empathy-smiley-manager.h"
 #include "empathy-ui-utils.h"
+#include "empathy-string-parser.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
 #include <libempathy/empathy-debug.h>
@@ -71,7 +77,11 @@ typedef struct {
        EmpathyContact    *remote_contact;
        gboolean           show_contacts;
 
+#ifdef ENABLE_TPL
+       TplLogManager     *log_manager;
+#else
        EmpathyLogManager *log_manager;
+#endif /* ENABLE_TPL */
        TpAccountManager  *account_manager;
        GList             *input_history;
        GList             *input_history_current;
@@ -93,11 +103,35 @@ typedef struct {
        GtkWidget         *label_topic;
        GtkWidget         *contact_list_view;
        GtkWidget         *info_bar_vbox;
+       GtkWidget         *search_bar;
 
        guint              unread_messages;
        /* TRUE if the pending messages can be displayed. This is to avoid to show
         * pending messages *before* messages from logs. (#603980) */
        gboolean           can_show_pending;
+
+       /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
+        * be differently handled since it introduces another race condition, which
+        * is really hard to occur, but still possible.
+        *
+        * With the current workaround (which has the race above), we need to be
+        * sure to ACK any pending messages only when the retrieval of backlogs is
+        * finished, that's why using retrieving_backlogs flag.
+        * empathy_chat_messages_read () will check this variable and not ACK
+        * anything when TRUE. It will be set TRUE at chat_constructed () and set
+        * back to FALSE when the backlog has been retrieved and the pending
+        * messages actually showed to the user.
+        *
+        * Race condition introduced with this workaround:
+        * Scenario: a message is pending, the user is notified and selects the tab.
+        * the tab with a pending message is focused before the messages are properly
+        * shown (since the preparation of the window is slower AND async WRT the
+        * tab showing), which means the user won't see any new messages (rare but
+        * possible), if he/she will change tab focus before the messages are
+        * properly shown, the tab will be set as 'seen' and the user won't be
+        * notified again about the already notified pending messages when the
+        * messages in tab will be properly shown */
+       gboolean           retrieving_backlogs;
 } EmpathyChatPriv;
 
 typedef struct {
@@ -618,7 +652,7 @@ chat_command_join_cb (EmpathyDispatchOperation *dispatch,
        if (error != NULL) {
                DEBUG ("Error: %s", error->message);
                empathy_chat_view_append_event (chat->view,
-                       _("Failed to join chatroom"));
+                       _("Failed to join chat room"));
        }
 }
 
@@ -695,13 +729,24 @@ static void
 chat_command_join (EmpathyChat *chat,
                   GStrv        strv)
 {
+       guint i = 0;
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       TpConnection *connection;
 
-       connection = empathy_tp_chat_get_connection (priv->tp_chat);
-       empathy_dispatcher_join_muc (connection, strv[1],
-                                    chat_command_join_cb,
-                                    chat);
+       GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
+
+       while (rooms[i] != NULL) {
+               /* ignore empty strings */
+               if (!EMP_STR_EMPTY (rooms[i])) {
+                       TpConnection *connection;
+
+                       connection = empathy_tp_chat_get_connection (priv->tp_chat);
+                       empathy_dispatcher_join_muc (connection, rooms[i],
+                                                    chat_command_join_cb,
+                                                    chat);
+               }
+               i++;
+       }
+       g_strfreev (rooms);
 }
 
 static void
@@ -799,36 +844,36 @@ typedef struct {
 
 static ChatCommandItem commands[] = {
        {"clear", 1, 1, chat_command_clear,
-        N_("/clear, clear all messages from the current conversation")},
+        N_("/clear: clear all messages from the current conversation")},
 
        {"topic", 2, 2, chat_command_topic,
-        N_("/topic <topic>, set the topic of the current conversation")},
+        N_("/topic <topic>: set the topic of the current conversation")},
 
        {"join", 2, 2, chat_command_join,
-        N_("/join <chatroom id>, join a new chatroom")},
+        N_("/join <chat room ID>: join a new chat room")},
 
        {"j", 2, 2, chat_command_join,
-        N_("/j <chatroom id>, join a new chatroom")},
+        N_("/j <chat room ID>: join a new chat room")},
 
        {"query", 2, 3, chat_command_query,
-        N_("/query <contact id> [<message>], open a private chat")},
+        N_("/query <contact ID> [<message>]: open a private chat")},
 
        {"msg", 3, 3, chat_command_msg,
-        N_("/msg <contact id> <message>, open a private chat")},
+        N_("/msg <contact ID> <message>: open a private chat")},
 
        {"nick", 2, 2, chat_command_nick,
-        N_("/nick <nickname>, change your nickname on current server")},
+        N_("/nick <nickname>: change your nickname on the current server")},
 
        {"me", 2, 2, chat_command_me,
-        N_("/me <message>, send an ACTION message to the current conversation")},
+        N_("/me <message>: send an ACTION message to the current conversation")},
 
        {"say", 2, 2, chat_command_say,
-        N_("/say <message>, send <message> to the current conversation. "
+        N_("/say <message>: send <message> to the current conversation. "
            "This is used to send a message starting with a '/'. For example: "
-           "\"/say /join is used to join a new chatroom\"")},
+           "\"/say /join is used to join a new chat room\"")},
 
        {"help", 1, 2, chat_command_help,
-        N_("/help [<command>], show all supported commands. "
+        N_("/help [<command>]: show all supported commands. "
            "If <command> is defined, show its usage.")},
 };
 
@@ -988,7 +1033,7 @@ chat_send (EmpathyChat  *chat,
 
                if (!second_slash) {
                        empathy_chat_view_append_event (chat->view,
-                               _("Unknown command, see /help for the available"
+                               _("Unknown command; see /help for the available"
                                  " commands"));
                        return;
                }
@@ -1113,7 +1158,6 @@ chat_message_received_cb (EmpathyTpChat  *tp_chat,
                          EmpathyChat    *chat)
 {
        chat_message_received (chat, message);
-       empathy_tp_chat_acknowledge_message (tp_chat, message);
 }
 
 static void
@@ -1169,7 +1213,12 @@ chat_property_changed_cb (EmpathyTpChat *tp_chat,
                if (EMP_STR_EMPTY (priv->subject)) {
                        gtk_widget_hide (priv->hbox_topic);
                } else {
-                       gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
+                       gchar *markup_text;
+
+                       markup_text = empathy_add_link_markup (priv->subject);
+                       gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
+                       g_free (markup_text);
+
                        gtk_widget_show (priv->hbox_topic);
                }
                if (priv->block_events_timeout_id == 0) {
@@ -1195,13 +1244,10 @@ static void
 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
                                    EmpathyChat    *chat)
 {
-       EmpathyChatPriv *priv;
        GtkTextIter     start, end;
        gchar          *str;
        gboolean        spell_checker = FALSE;
 
-       priv = GET_PRIV (chat);
-
        if (gtk_text_buffer_get_char_count (buffer) == 0) {
                chat_composing_stop (chat);
        } else {
@@ -1270,6 +1316,13 @@ chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
        }
 }
 
+static gboolean
+empathy_isspace_cb (gunichar c,
+                gpointer data)
+{
+       return g_unichar_isspace (c);
+}
+
 static gboolean
 chat_input_key_press_event_cb (GtkWidget   *widget,
                               GdkEventKey *event,
@@ -1351,6 +1404,9 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                gtk_adjustment_set_value (adj, val);
                return TRUE;
        }
+       if (event->keyval == GDK_Escape) {
+               empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
+       }
        if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
            event->keyval == GDK_Tab) {
                GtkTextBuffer *buffer;
@@ -1364,7 +1420,9 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
 
                /* Get the start of the nick to complete. */
                gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
-               gtk_text_iter_backward_word_start (&start);
+               if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
+                       gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
+               }
                is_start_of_buffer = gtk_text_iter_is_start (&start);
 
                list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
@@ -1381,6 +1439,8 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                        guint        len;
                        const gchar *text;
                        gchar       *complete_char = NULL;
+                       GString     *message = NULL;
+                       GList       *l;
 
                        gtk_text_buffer_delete (buffer, &start, &current);
 
@@ -1396,6 +1456,18 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                                text = empathy_contact_get_name (completed_list->data);
                        } else {
                                text = completed;
+
+                               /* Print all hits to the scrollback view, so the
+                                * user knows what possibilities he has.
+                                * Fixes #599779
+                                * */
+                                message = g_string_new ("");
+                                for (l = completed_list; l != NULL; l = l->next) {
+                                       g_string_append (message, empathy_contact_get_name (l->data));
+                                       g_string_append (message, " - ");
+                                }
+                                empathy_chat_view_append_event (chat->view, message->str);
+                                g_string_free (message, TRUE);
                        }
 
                        gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
@@ -1670,31 +1742,128 @@ chat_input_populate_popup_cb (GtkTextView *view,
        }
 }
 
+
+#ifdef ENABLE_TPL
+static gboolean
+chat_log_filter (TplLogEntry *log,
+                gpointer user_data)
+#else
 static gboolean
 chat_log_filter (EmpathyMessage *message,
                 gpointer user_data)
+#endif /* ENABLE_TPL */
 {
-       EmpathyChat *chat = (EmpathyChat *) user_data;
+       EmpathyChat *chat = user_data;
+#ifdef ENABLE_TPL
+       EmpathyMessage *message;
+#endif /* ENABLE_TPL */
        EmpathyChatPriv *priv = GET_PRIV (chat);
        const GList *pending;
 
+#ifdef ENABLE_TPL
+       g_return_val_if_fail (TPL_IS_LOG_ENTRY (log), FALSE);
+#else
+       g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
+#endif /* ENABLE_TPL */
+       g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
+
        pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
+#ifdef ENABLE_TPL
+       message = empathy_message_from_tpl_log_entry (log);
+#endif /* ENABLE_TPL */
 
        for (; pending; pending = g_list_next (pending)) {
                if (empathy_message_equal (message, pending->data)) {
                        return FALSE;
                }
        }
-
+#ifdef ENABLE_TPL
+       g_object_unref (message);
+#endif
        return TRUE;
 }
 
+
+static void
+show_pending_messages (EmpathyChat *chat) {
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       const GList *messages, *l;
+
+       g_return_if_fail (EMPATHY_IS_CHAT (chat));
+
+       if (chat->view == NULL || priv->tp_chat == NULL)
+               return;
+
+       if (!priv->can_show_pending)
+               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);
+       }
+}
+
+
+#ifdef ENABLE_TPL
+static void
+got_filtered_messages_cb (GObject *manager,
+               GAsyncResult *result,
+               gpointer user_data)
+{
+       GList *l;
+       GList *messages;
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       GError *error = NULL;
+
+       messages = tpl_log_manager_get_filtered_messages_async_finish (result, &error);
+
+       if (error != NULL) {
+               DEBUG ("%s. Aborting.", error->message);
+               empathy_chat_view_append_event (chat->view,
+                       _("Failed to retrieve recent logs"));
+               g_error_free (error);
+               goto out;
+       }
+
+       for (l = messages; l; l = g_list_next (l)) {
+               EmpathyMessage *message;
+               g_assert (TPL_IS_LOG_ENTRY (l->data));
+
+               message = empathy_message_from_tpl_log_entry (l->data);
+               g_object_unref (l->data);
+
+               empathy_chat_view_append_message (chat->view, message);
+               g_object_unref (message);
+       }
+       g_list_free (messages);
+
+out:
+       /* in case of TPL error, skip backlog and show pending messages */
+       priv->can_show_pending = TRUE;
+       show_pending_messages (chat);
+
+       /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
+        * about it in EmpathyChatPriv definition */
+       priv->retrieving_backlogs = FALSE;
+       empathy_chat_messages_read (chat);
+
+       /* Turn back on scrolling */
+       empathy_chat_view_scroll (chat->view, TRUE);
+}
+#endif /* ENABLE_TPL */
+
+
 static void
 chat_add_logs (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
        gboolean         is_chatroom;
+#ifndef ENABLE_TPL
        GList           *messages, *l;
+#endif /* ENABLE_TPL */
 
        if (!priv->id) {
                return;
@@ -1706,6 +1875,7 @@ chat_add_logs (EmpathyChat *chat)
        /* Add messages from last conversation */
        is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
 
+#ifndef ENABLE_TPL
        messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
                                                              priv->account,
                                                              priv->id,
@@ -1723,6 +1893,18 @@ chat_add_logs (EmpathyChat *chat)
 
        /* Turn back on scrolling */
        empathy_chat_view_scroll (chat->view, TRUE);
+#else
+       priv->retrieving_backlogs = TRUE;
+       tpl_log_manager_get_filtered_messages_async (priv->log_manager,
+                                                             priv->account,
+                                                             priv->id,
+                                                             is_chatroom,
+                                                             5,
+                                                             chat_log_filter,
+                                                             chat,
+                                                             got_filtered_messages_cb,
+                                                             (gpointer) chat);
+#endif /* ENABLE_TPL */
 }
 
 static gint
@@ -2008,27 +2190,6 @@ chat_hpaned_pos_changed_cb (GtkWidget* hpaned, gpointer user_data)
        return TRUE;
 }
 
-
-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;
-
-       if (!priv->can_show_pending)
-               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)
 {
@@ -2091,9 +2252,9 @@ chat_create_ui (EmpathyChat *chat)
                          G_CALLBACK (chat_input_populate_popup_cb),
                          chat);
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
-       g_signal_connect (buffer, "changed",
+       empathy_signal_connect_weak  (buffer, "changed",
                          G_CALLBACK (chat_input_text_buffer_changed_cb),
-                         chat);
+                         G_OBJECT (chat));
        gtk_text_buffer_create_tag (buffer, "misspelled",
                                    "underline", PANGO_UNDERLINE_ERROR,
                                    NULL);
@@ -2101,6 +2262,13 @@ chat_create_ui (EmpathyChat *chat)
                           chat->input_text_view);
        gtk_widget_show (chat->input_text_view);
 
+       /* Add the (invisible) search bar */
+       priv->search_bar = empathy_search_bar_new (chat->view);
+       gtk_box_pack_start (GTK_BOX(priv->vbox_left),
+                           priv->search_bar,
+                           FALSE, FALSE, 0);
+       gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
+
        /* Initialy hide the topic, will be shown if not empty */
        gtk_widget_hide (priv->hbox_topic);
 
@@ -2116,7 +2284,8 @@ chat_create_ui (EmpathyChat *chat)
                gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
 
        /* Set widget focus order */
-       list = g_list_append (NULL, priv->scrolled_window_input);
+       list = g_list_append (NULL, priv->search_bar);
+       list = g_list_append (list, priv->scrolled_window_input);
        gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
        g_list_free (list);
 
@@ -2218,7 +2387,7 @@ chat_finalize (GObject *object)
                        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);
+               empathy_tp_chat_leave (priv->tp_chat);
                g_object_unref (priv->tp_chat);
        }
        if (priv->account) {
@@ -2248,8 +2417,12 @@ chat_constructed (GObject *object)
 
        if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
                chat_add_logs (chat);
+#ifndef ENABLE_TPL
+       /* When async API are involved, pending message are shown at the end of the
+        * callbacks' chain fired by chat_add_logs */
        priv->can_show_pending = TRUE;
        show_pending_messages (chat);
+#endif /* ENABLE_TPL */
 }
 
 static void
@@ -2392,7 +2565,11 @@ empathy_chat_init (EmpathyChat *chat)
                EMPATHY_TYPE_CHAT, EmpathyChatPriv);
 
        chat->priv = priv;
+#ifndef ENABLE_TPL
        priv->log_manager = empathy_log_manager_dup_singleton ();
+#else
+       priv->log_manager = tpl_log_manager_dup_singleton ();
+#endif /* ENABLE_TPL */
        priv->contacts_width = -1;
        priv->input_history = NULL;
        priv->input_history_current = NULL;
@@ -2841,6 +3018,18 @@ empathy_chat_paste (EmpathyChat *chat)
        gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
 }
 
+void
+empathy_chat_find (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv;
+
+       g_return_if_fail (EMPATHY_IS_CHAT (chat));
+
+       priv = GET_PRIV (chat);
+
+       empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
+}
+
 void
 empathy_chat_correct_word (EmpathyChat  *chat,
                          GtkTextIter *start,
@@ -2875,7 +3064,7 @@ empathy_chat_get_nb_unread_messages (EmpathyChat *self)
 {
        EmpathyChatPriv *priv = GET_PRIV (self);
 
-       g_return_val_if_fail (EMPATHY_IS_CHAT (self), FALSE);
+       g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
 
        return priv->unread_messages;
 }
@@ -2888,5 +3077,13 @@ empathy_chat_messages_read (EmpathyChat *self)
 
        g_return_if_fail (EMPATHY_IS_CHAT (self));
 
+       /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
+        * definition. If we are still retrieving the backlogs, do not ACK */
+       if (priv->retrieving_backlogs)
+               return;
+
+       if (priv->tp_chat != NULL ) {
+                       empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
+       }
        priv->unread_messages = 0;
 }