]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
Updated Polish translation
[empathy.git] / libempathy-gtk / empathy-chat.c
index 624b1a03fc0b7cc166bbbc5cc24fcaf237cf8f8b..4db9e455c4a31a98c4b7e3c38a658588021ba124 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 {
@@ -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
@@ -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) {
@@ -1358,6 +1407,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;
@@ -1693,31 +1745,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;
@@ -1729,6 +1878,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,
@@ -1746,6 +1896,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
@@ -2031,27 +2193,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)
 {
@@ -2124,6 +2265,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);
 
@@ -2139,7 +2287,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);
 
@@ -2241,7 +2390,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) {
@@ -2271,8 +2420,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
@@ -2415,7 +2568,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;
@@ -2864,6 +3021,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,
@@ -2898,7 +3067,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;
 }
@@ -2911,5 +3080,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;
 }