]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat.c
EmpathyChat: Fix pending messages not being displayed
[empathy.git] / libempathy-gtk / empathy-chat.c
index 18b08d480dc28eb621f8a45b379b593377aff36a..fc35bec887771656d12ce69e1bb53c99dac447ed 100644 (file)
@@ -2,6 +2,7 @@
 /*
  * Copyright (C) 2002-2007 Imendio AB
  * Copyright (C) 2007-2010 Collabora Ltd.
+ * Copyright (C) 2012 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  *          Xavier Claessens <xclaesse@gmail.com>
  */
 
-#include <config.h>
 /* for GCompletion */
 #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
 
-#include <string.h>
-#include <stdlib.h>
+#include "config.h"
+#include "empathy-chat.h"
 
-#include <gdk/gdkkeysyms.h>
 #include <glib/gi18n-lib.h>
-#include <gtk/gtk.h>
-
-#include <telepathy-glib/account-manager.h>
-#include <telepathy-glib/util.h>
-#include <telepathy-logger/telepathy-logger.h>
-#include <libempathy/empathy-gsettings.h>
-#include <libempathy/empathy-keyring.h>
-#include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-request-util.h>
-#include <libempathy/empathy-chatroom-manager.h>
-#include <libempathy/empathy-client-factory.h>
-
-#include "empathy-chat.h"
-#include "empathy-spell.h"
-#include "empathy-contact-dialogs.h"
+#include <tp-account-widgets/tpaw-keyring.h>
+#include <tp-account-widgets/tpaw-builder.h>
+#include <tp-account-widgets/tpaw-utils.h>
+#include <telepathy-glib/telepathy-glib-dbus.h>
+
+#include "empathy-client-factory.h"
+#include "empathy-gsettings.h"
+#include "empathy-individual-information-dialog.h"
 #include "empathy-individual-store-channel.h"
 #include "empathy-individual-view.h"
 #include "empathy-input-text-view.h"
+#include "empathy-request-util.h"
 #include "empathy-search-bar.h"
+#include "empathy-spell.h"
+#include "empathy-string-parser.h"
 #include "empathy-theme-manager.h"
-#include "empathy-theme-adium.h"
-#include "empathy-smiley-manager.h"
 #include "empathy-ui-utils.h"
-#include "empathy-string-parser.h"
-#include "extensions/extensions.h"
+#include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
 #define COMPOSING_STOP_TIMEOUT 5
@@ -81,6 +73,17 @@ struct _EmpathyChatPriv {
        GSettings         *gsettings_ui;
 
        TplLogManager     *log_manager;
+       TplLogWalker      *log_walker;
+       /* Are we watching for scrolling movements? */
+       gboolean           watch_scroll;
+       /* Maximum page size of the chat->view. */
+       guint              max_page_size;
+       /* The offset from the lower edge of the chat->view before it
+        * expanded to fit in the newly fetched logs. This is to
+        * restore the chat->view to the page it was on before the
+        * latest batch of logs were inserted. */
+       guint              scroll_offset;
+
        TpAccountManager  *account_manager;
        GList             *input_history;
        GList             *input_history_current;
@@ -124,9 +127,6 @@ struct _EmpathyChatPriv {
 
        guint              unread_messages;
        guint              unread_messages_when_offline;
-       /* 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
@@ -198,6 +198,7 @@ static guint signals[LAST_SIGNAL] = { 0 };
 
 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
 
+static gboolean chat_scrollable_connect (gpointer user_data);
 static gboolean update_misspelled_words (gpointer data);
 
 static void
@@ -324,7 +325,7 @@ chat_new_connection_cb (TpAccount   *account,
 
        if (priv->tp_chat != NULL || account != priv->account ||
            priv->handle_type == TP_HANDLE_TYPE_NONE ||
-           EMP_STR_EMPTY (priv->id))
+           TPAW_STR_EMPTY (priv->id))
                return;
 
        g_object_ref (chat);
@@ -724,12 +725,12 @@ chat_command_msg_cb (GObject *source,
                DEBUG ("Failed to get channel: %s", error->message);
                g_error_free (error);
 
-               empathy_chat_view_append_event (data->chat->view,
+               empathy_theme_adium_append_event (data->chat->view,
                        _("Failed to open private chat"));
                goto OUT;
        }
 
-       if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
+       if (!TPAW_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
                TpTextChannel *text = (TpTextChannel *) channel;
                TpMessage *msg;
 
@@ -754,9 +755,9 @@ nick_command_supported (EmpathyChat *chat)
        EmpathyChatPriv * priv = GET_PRIV (chat);
        TpConnection    *connection;
 
-       connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
+       connection = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
        return tp_proxy_has_interface_by_id (connection,
-               EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
+               TP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
 }
 
 static gboolean
@@ -772,7 +773,7 @@ static void
 chat_command_clear (EmpathyChat *chat,
                    GStrv        strv)
 {
-       empathy_chat_view_clear (chat->view);
+       empathy_theme_adium_clear (chat->view);
 }
 
 static void
@@ -782,13 +783,13 @@ chat_command_topic (EmpathyChat *chat,
        EmpathyChatPriv *priv = GET_PRIV (chat);
 
        if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
-               empathy_chat_view_append_event (chat->view,
+               empathy_theme_adium_append_event (chat->view,
                        _("Topic not supported on this conversation"));
                return;
        }
 
        if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
-               empathy_chat_view_append_event (chat->view,
+               empathy_theme_adium_append_event (chat->view,
                        _("You are not allowed to change the topic"));
                return;
        }
@@ -819,7 +820,7 @@ chat_command_join (EmpathyChat *chat,
        * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
        while (rooms[i] != NULL) {
                /* ignore empty strings */
-               if (!EMP_STR_EMPTY (rooms[i])) {
+               if (!TPAW_STR_EMPTY (rooms[i])) {
                        empathy_chat_join_muc (chat, rooms[i]);
                }
                i++;
@@ -842,17 +843,13 @@ chat_command_msg_internal (EmpathyChat *chat,
        EmpathyChatPriv *priv = GET_PRIV (chat);
        ChatCommandMsgData *data;
        TpAccountChannelRequest *req;
-       GHashTable *request;
-
-       request = tp_asv_new (
-               TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
-               TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
-               TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
-               NULL);
 
-       req = tp_account_channel_request_new (priv->account, request,
+       req = tp_account_channel_request_new_text (priv->account,
                empathy_get_current_action_time ());
 
+       tp_account_channel_request_set_target_id (req, TP_HANDLE_TYPE_CONTACT,
+               contact_id);
+
        /* FIXME: We should probably search in members alias. But this
         * is enough for IRC */
        data = g_slice_new (ChatCommandMsgData);
@@ -860,10 +857,9 @@ chat_command_msg_internal (EmpathyChat *chat,
        data->message = g_strdup (message);
 
        tp_account_channel_request_ensure_and_observe_channel_async (req,
-               EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
+               EMPATHY_CHAT_TP_BUS_NAME, NULL, chat_command_msg_cb, data);
 
        g_object_unref (req);
-       g_hash_table_unref (request);
 }
 
 static void
@@ -883,7 +879,7 @@ chat_command_msg (EmpathyChat *chat,
 }
 
 static void
-callback_for_request_rename (TpProxy *proxy,
+callback_for_request_rename (TpConnection *conn,
                  const GError *error,
                  gpointer user_data,
                  GObject *weak_object)
@@ -898,11 +894,11 @@ chat_command_nick (EmpathyChat *chat,
                   GStrv        strv)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       TpProxy *proxy;
+       TpConnection *conn;
 
-       proxy = TP_PROXY (tp_account_get_connection (priv->account));
+       conn = tp_account_get_connection (priv->account);
 
-       emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
+       tp_cli_connection_interface_renaming_call_request_rename (conn, -1,
                strv[1], callback_for_request_rename, NULL, NULL, NULL);
 }
 
@@ -959,22 +955,21 @@ whois_got_contact_cb (GObject *source,
 {
        EmpathyChat *chat = user_data;
        EmpathyContact *contact;
-       GtkWidget *window;
+       FolksIndividual *individual;
 
        contact = empathy_client_factory_dup_contact_by_id_finish (
                EMPATHY_CLIENT_FACTORY (source), result, NULL);
 
        if (contact == NULL) {
-               empathy_chat_view_append_event (chat->view, _("Invalid contact ID"));
+               empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
                goto out;
        }
 
-       window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
-       g_return_if_fail (window != NULL);
-       g_return_if_fail (gtk_widget_is_toplevel (window));
-       empathy_contact_information_dialog_show (contact,
-               GTK_WINDOW (window));
+       individual = empathy_ensure_individual_from_tp_contact (
+               empathy_contact_get_tp_contact (contact));
+       empathy_display_individual_info (individual);
 
+       g_object_unref (individual);
        g_object_unref (contact);
 
 out:
@@ -989,7 +984,7 @@ chat_command_whois (EmpathyChat *chat,
        TpConnection *conn;
        EmpathyClientFactory *factory;
 
-       conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
+       conn = tp_channel_get_connection ((TpChannel *) priv->tp_chat);
        factory = empathy_client_factory_dup ();
 
        /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
@@ -1110,7 +1105,7 @@ chat_command_show_help (EmpathyChat     *chat,
        }
 
        str = g_strdup_printf (_("Usage: %s"), _(item->help));
-       empathy_chat_view_append_event (chat->view, str);
+       empathy_theme_adium_append_event (chat->view, str);
        g_free (str);
 }
 
@@ -1132,7 +1127,7 @@ chat_command_help (EmpathyChat *chat,
                        if (commands[i].help == NULL) {
                                continue;
                        }
-                       empathy_chat_view_append_event (chat->view,
+                       empathy_theme_adium_append_event (chat->view,
                                _(commands[i].help));
                }
                return;
@@ -1153,7 +1148,7 @@ chat_command_help (EmpathyChat *chat,
                }
        }
 
-       empathy_chat_view_append_event (chat->view,
+       empathy_theme_adium_append_event (chat->view,
                _("Unknown command"));
 }
 
@@ -1191,7 +1186,7 @@ chat_command_parse (const gchar *text, guint max_parts)
 
        /* Append last part if not empty */
        item = g_strstrip (g_strdup (text));
-       if (!EMP_STR_EMPTY (item)) {
+       if (!TPAW_STR_EMPTY (item)) {
                g_ptr_array_add (array, item);
                DEBUG ("\tITEM: \"%s\"", item);
        } else {
@@ -1219,7 +1214,7 @@ chat_send (EmpathyChat  *chat,
        TpMessage  *message;
        guint            i;
 
-       if (EMP_STR_EMPTY (msg)) {
+       if (TPAW_STR_EMPTY (msg)) {
                return;
        }
 
@@ -1279,7 +1274,7 @@ chat_send (EmpathyChat  *chat,
                }
 
                if (!second_slash) {
-                       empathy_chat_view_append_event (chat->view,
+                       empathy_theme_adium_append_event (chat->view,
                                _("Unknown command; see /help for the available"
                                  " commands"));
                        return;
@@ -1429,7 +1424,6 @@ chat_should_highlight (EmpathyChat *chat,
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
        const gchar   *msg;
-       TpChannelTextMessageFlags flags;
 
        g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
 
@@ -1446,8 +1440,7 @@ chat_should_highlight (EmpathyChat *chat,
                return FALSE;
        }
 
-       flags = empathy_message_get_flags (message);
-       if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
+       if (empathy_message_is_backlog (message)) {
                /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
                 * have already been received by the user before (and so are in the logs) */
                return FALSE;
@@ -1475,7 +1468,7 @@ chat_message_received (EmpathyChat *chat,
                        empathy_message_get_supersedes (message),
                        empathy_message_get_body (message));
 
-               empathy_chat_view_edit_message (chat->view, message);
+               empathy_theme_adium_edit_message (chat->view, message);
        } else {
                gboolean should_highlight = chat_should_highlight (chat, message);
 
@@ -1488,7 +1481,7 @@ chat_message_received (EmpathyChat *chat,
                        empathy_contact_get_alias (sender),
                        empathy_contact_get_handle (sender));
 
-               empathy_chat_view_append_message (chat->view, message, should_highlight);
+               empathy_theme_adium_append_message (chat->view, message, should_highlight);
 
                if (empathy_message_is_incoming (message)) {
                        priv->unread_messages++;
@@ -1521,7 +1514,7 @@ chat_message_acknowledged_cb (EmpathyTpChat  *tp_chat,
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
 
-       empathy_chat_view_message_acknowledged (chat->view,
+       empathy_theme_adium_message_acknowledged (chat->view,
            message);
 
        if (!empathy_message_is_edit (message)) {
@@ -1535,7 +1528,7 @@ append_balance_error (EmpathyChat *chat,
                      const gchar *message_body)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
+       TpConnection *conn = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
        const gchar *uri = tp_connection_get_balance_uri (conn);
        const gchar *error = _("insufficient balance to send message");
        gchar *str, *str_markup = NULL;
@@ -1567,9 +1560,9 @@ append_balance_error (EmpathyChat *chat,
        }
 
        if (str_markup != NULL)
-               empathy_chat_view_append_event_markup (chat->view, str_markup, str);
+               empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
        else
-               empathy_chat_view_append_event (chat->view, str);
+               empathy_theme_adium_append_event (chat->view, str);
 
        g_free (str);
        g_free (str_markup);
@@ -1625,7 +1618,7 @@ chat_send_error_cb (EmpathyTpChat          *tp_chat,
                        str = g_strdup_printf (_("Error sending message: %s"), error);
        }
 
-       empathy_chat_view_append_event (chat->view, str);
+       empathy_theme_adium_append_event (chat->view, str);
        g_free (str);
 }
 
@@ -1671,13 +1664,13 @@ chat_subject_changed_cb (EmpathyChat *chat)
                priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
                g_object_notify (G_OBJECT (chat), "subject");
 
-               if (EMP_STR_EMPTY (priv->subject)) {
+               if (TPAW_STR_EMPTY (priv->subject)) {
                        gtk_widget_hide (priv->hbox_topic);
                } else {
                        gchar *markup_topic;
                        gchar *markup_text;
 
-                       markup_topic = empathy_add_link_markup (priv->subject);
+                       markup_topic = tpaw_add_link_markup (priv->subject);
                        markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
                                _("Topic:"), markup_topic);
 
@@ -1690,7 +1683,7 @@ chat_subject_changed_cb (EmpathyChat *chat)
                if (priv->block_events_timeout_id == 0) {
                        gchar *str = NULL;
 
-                       if (!EMP_STR_EMPTY (priv->subject)) {
+                       if (!TPAW_STR_EMPTY (priv->subject)) {
                                const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
 
                                if (tp_str_empty (actor)) {
@@ -1705,7 +1698,7 @@ chat_subject_changed_cb (EmpathyChat *chat)
                        }
 
                        if (str != NULL) {
-                               empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
+                               empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
                                g_free (str);
                        }
                }
@@ -2028,7 +2021,7 @@ chat_input_key_press_event_cb (GtkWidget   *widget,
                                        g_string_append (message, empathy_contact_get_alias (l->data));
                                        g_string_append (message, " - ");
                                 }
-                                empathy_chat_view_append_event (chat->view, message->str);
+                                empathy_theme_adium_append_event (chat->view, message->str);
                                 g_string_free (message, TRUE);
                        }
 
@@ -2089,7 +2082,14 @@ chat_input_has_focus_notify_cb (GtkWidget   *widget,
                                GParamSpec  *pspec,
                                EmpathyChat *chat)
 {
-       empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
+       empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
+}
+
+void
+empathy_chat_insert_smiley (GtkTextBuffer *buffer,
+                               EmpathySmiley        *smiley)
+{
+       gtk_text_buffer_insert_at_cursor (buffer, smiley->str, -1);
 }
 
 static void
@@ -2099,15 +2099,10 @@ chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
 {
        EmpathyChat   *chat = EMPATHY_CHAT (user_data);
        GtkTextBuffer *buffer;
-       GtkTextIter    iter;
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
 
-       gtk_text_buffer_get_end_iter (buffer, &iter);
-       gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
-
-       gtk_text_buffer_get_end_iter (buffer, &iter);
-       gtk_text_buffer_insert (buffer, &iter, " ", -1);
+       empathy_chat_insert_smiley (buffer, smiley);
 }
 
 typedef struct {
@@ -2409,7 +2404,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 (!EMP_STR_EMPTY (str)) {
+       if (!TPAW_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);
@@ -2456,7 +2451,7 @@ chat_input_populate_popup_cb (GtkTextView *view,
                str = gtk_text_buffer_get_text (buffer,
                                                &start, &end, FALSE);
        }
-       if (!EMP_STR_EMPTY (str)) {
+       if (!TPAW_STR_EMPTY (str)) {
                chat_spell = chat_spell_new (chat, str, start, end);
                g_object_set_data_full (G_OBJECT (menu),
                                        "chat-spell", chat_spell,
@@ -2496,10 +2491,11 @@ static gboolean
 chat_log_filter (TplEvent *event,
                 gpointer user_data)
 {
-       EmpathyChat *chat = user_data;
-       EmpathyMessage *message;
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
        EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyMessage *message;
        const GList *pending;
+       bool retval = FALSE;
 
        g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
        g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
@@ -2508,29 +2504,25 @@ chat_log_filter (TplEvent *event,
        message = empathy_message_from_tpl_log_event (event);
 
        for (; pending; pending = g_list_next (pending)) {
-               if (empathy_message_equal (message, pending->data)) {
-                       g_object_unref (message);
-                       return FALSE;
-               }
+               if (empathy_message_equal (message, pending->data))
+                       goto out;
        }
 
+       retval = TRUE;
+
+out:
        g_object_unref (message);
-       return TRUE;
+       return retval;
 }
 
-
 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;
+       g_return_if_fail (chat->view != NULL);
+       g_return_if_fail (priv->tp_chat != NULL);
 
        messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
 
@@ -2540,9 +2532,28 @@ show_pending_messages (EmpathyChat *chat) {
        }
 }
 
+static gboolean
+chat_scrollable_set_value (gpointer user_data)
+{
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       GtkAdjustment *adjustment;
+       guint upper;
+
+       adjustment = gtk_scrollable_get_vadjustment (
+           GTK_SCROLLABLE (chat->view));
+
+       /* Set the chat->view's adjustment back to the value it had
+        * before it grew as a result of new logs being inserted.
+        */
+       upper  = (guint) gtk_adjustment_get_upper (adjustment);
+       gtk_adjustment_set_value (adjustment, upper - priv->scroll_offset);
+
+       return G_SOURCE_REMOVE;
+}
 
 static void
-got_filtered_messages_cb (GObject *manager,
+got_filtered_messages_cb (GObject *walker,
                GAsyncResult *result,
                gpointer user_data)
 {
@@ -2552,16 +2563,16 @@ got_filtered_messages_cb (GObject *manager,
        EmpathyChatPriv *priv = GET_PRIV (chat);
        GError *error = NULL;
 
-       if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
+       if (!tpl_log_walker_get_events_finish (TPL_LOG_WALKER (walker),
                result, &messages, &error)) {
                DEBUG ("%s. Aborting.", error->message);
-               empathy_chat_view_append_event (chat->view,
+               empathy_theme_adium_append_event (chat->view,
                        _("Failed to retrieve recent logs"));
                g_error_free (error);
                goto out;
        }
 
-       for (l = messages; l; l = g_list_next (l)) {
+       for (l = g_list_last (messages); l; l = g_list_previous (l)) {
                EmpathyMessage *message;
 
                g_assert (TPL_IS_EVENT (l->data));
@@ -2586,14 +2597,14 @@ got_filtered_messages_cb (GObject *manager,
                                "sender", empathy_message_get_sender (message),
                                NULL);
 
-                       empathy_chat_view_append_message (chat->view, syn_msg,
+                       empathy_theme_adium_prepend_message (chat->view, syn_msg,
                                                          chat_should_highlight (chat, syn_msg));
-                       empathy_chat_view_edit_message (chat->view, message);
+                       empathy_theme_adium_edit_message (chat->view, message);
 
                        g_object_unref (syn_msg);
                } else {
                        /* append the latest message */
-                       empathy_chat_view_append_message (chat->view, message,
+                       empathy_theme_adium_prepend_message (chat->view, message,
                                                          chat_should_highlight (chat, message));
                }
 
@@ -2602,50 +2613,149 @@ got_filtered_messages_cb (GObject *manager,
        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);
+       empathy_theme_adium_scroll (chat->view, TRUE);
+
+       /* We start watching the scrolling movements only after the first
+        * batch of logs have been fetched. Otherwise, if the
+        * chat->view's page size is too small the scrollbar might hit
+        * the upper edge and trigger another batch of logs to be
+        * fetched.
+        */
+       if (G_UNLIKELY (!priv->watch_scroll &&
+                       !tpl_log_walker_is_end (priv->log_walker))) {
+               priv->watch_scroll = TRUE;
+               g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_connect,
+                   g_object_ref (chat), g_object_unref);
+       }
+       else {
+               GtkAdjustment *adjustment;
+               guint upper;
+               guint value;
+
+               /* The chat->view's adjustment won't change unless we
+                * return to the main loop. Save the current offset
+                * from the lower edge (or the upper value of the
+                * adjustment) so that we can restore it later once the
+                * adjustment grows.
+                */
+               adjustment = gtk_scrollable_get_vadjustment (
+                   GTK_SCROLLABLE (chat->view));
+               upper = (guint) gtk_adjustment_get_upper (adjustment);
+               value = (guint) gtk_adjustment_get_value (adjustment);
+               priv->scroll_offset = upper - value;
+
+               g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_set_value,
+                   g_object_ref (chat), g_object_unref);
+       }
+
+       g_object_unref (chat);
 }
 
-static void
+static gboolean
 chat_add_logs (EmpathyChat *chat)
 {
        EmpathyChatPriv *priv = GET_PRIV (chat);
-       TplEntity       *target;
 
        if (!priv->id) {
-               return;
+               return G_SOURCE_REMOVE;
        }
 
        /* Turn off scrolling temporarily */
-       empathy_chat_view_scroll (chat->view, FALSE);
+       empathy_theme_adium_scroll (chat->view, FALSE);
 
-       /* Add messages from last conversation */
-       if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
-         target = tpl_entity_new_from_room_id (priv->id);
-       else
-         target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
+       tpl_log_walker_get_events_async (priv->log_walker, 5,
+           got_filtered_messages_cb, g_object_ref (chat));
+
+       return G_SOURCE_REMOVE;
+}
+
+static void
+chat_schedule_logs (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+
+       if (priv->retrieving_backlogs)
+               return;
 
        priv->retrieving_backlogs = TRUE;
-       tpl_log_manager_get_filtered_events_async (priv->log_manager,
-                                                  priv->account,
-                                                  target,
-                                                  TPL_EVENT_MASK_TEXT,
-                                                  5,
-                                                  chat_log_filter,
-                                                  chat,
-                                                  got_filtered_messages_cb,
-                                                  (gpointer) chat);
+       g_timeout_add_full (G_PRIORITY_LOW, 500, /* ms */
+           (GSourceFunc) chat_add_logs, g_object_ref (chat), g_object_unref);
+}
 
-       g_object_unref (target);
+static void
+chat_view_adjustment_changed_cb (GtkAdjustment *adjustment,
+                                gpointer user_data)
+{
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       guint page_size;
+
+       if (tpl_log_walker_is_end (priv->log_walker)) {
+               g_signal_handlers_disconnect_by_func (adjustment,
+                   chat_view_adjustment_changed_cb, user_data);
+               return;
+       }
+
+       page_size = (guint) gtk_adjustment_get_page_size (adjustment);
+       if (page_size <= priv->max_page_size)
+               return;
+
+       /* We need to fetch more logs if the page size of the view
+        * increases, so that there is no empty space at the top.
+        */
+       if (G_LIKELY (priv->max_page_size != 0))
+               chat_schedule_logs (chat);
+
+       priv->max_page_size = page_size;
+}
+
+static void
+chat_view_adjustment_value_changed_cb (GtkAdjustment *adjustment,
+                                      gpointer user_data)
+{
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       guint lower;
+       guint value;
+
+       if (tpl_log_walker_is_end (priv->log_walker)) {
+               g_signal_handlers_disconnect_by_func (adjustment,
+                   chat_view_adjustment_value_changed_cb, user_data);
+               return;
+       }
+
+       lower = (guint) gtk_adjustment_get_lower (adjustment);
+       value = (guint) gtk_adjustment_get_value (adjustment);
+       if (value != lower)
+               return;
+
+       /* Request for more logs to be fetched if the user hit the
+        * upper edge of the chat->view.
+        */
+       chat_schedule_logs (chat);
+}
+
+static gboolean
+chat_scrollable_connect (gpointer user_data)
+{
+       EmpathyChat *chat = EMPATHY_CHAT (user_data);
+       GtkAdjustment *adjustment;
+
+       adjustment = gtk_scrollable_get_vadjustment (
+           GTK_SCROLLABLE (chat->view));
+
+       g_signal_connect (adjustment, "changed",
+           G_CALLBACK (chat_view_adjustment_changed_cb), chat);
+       g_signal_connect (adjustment, "value-changed",
+           G_CALLBACK (chat_view_adjustment_value_changed_cb), chat);
+
+       return G_SOURCE_REMOVE;
 }
 
 static gint
@@ -2723,7 +2833,7 @@ build_part_message (guint           reason,
                g_string_append_printf (s, _("%s has left the room"), name);
        }
 
-       if (!EMP_STR_EMPTY (message)) {
+       if (!TPAW_STR_EMPTY (message)) {
                /* Note to translators: this string is appended to
                 * notifications like "foo has left the room", with the message
                 * given by the user living the room. If this poses a problem,
@@ -2760,7 +2870,7 @@ chat_members_changed_cb (EmpathyTpChat  *tp_chat,
                str = build_part_message (reason, name, actor, message);
        }
 
-       empathy_chat_view_append_event (chat->view, str);
+       empathy_theme_adium_append_event (chat->view, str);
        g_free (str);
 }
 
@@ -2782,7 +2892,7 @@ chat_member_renamed_cb (EmpathyTpChat  *tp_chat,
                str = g_strdup_printf (_("%s is now known as %s"),
                                       empathy_contact_get_alias (old_contact),
                                       empathy_contact_get_alias (new_contact));
-               empathy_chat_view_append_event (chat->view, str);
+               empathy_theme_adium_append_event (chat->view, str);
                g_free (str);
        }
 
@@ -2966,7 +3076,7 @@ chat_invalidated_cb (EmpathyTpChat *tp_chat,
        priv->tp_chat = NULL;
        g_object_notify (G_OBJECT (chat), "tp-chat");
 
-       empathy_chat_view_append_event (chat->view, _("Disconnected"));
+       empathy_theme_adium_append_event (chat->view, _("Disconnected"));
        gtk_widget_set_sensitive (chat->input_text_view, FALSE);
 
        chat_update_contacts_visibility (chat, FALSE);
@@ -3123,7 +3233,7 @@ chat_create_ui (EmpathyChat *chat)
 
        filename = empathy_file_lookup ("empathy-chat.ui",
                                        "libempathy-gtk");
-       gui = empathy_builder_get_file (filename,
+       gui = tpaw_builder_get_file (filename,
                                        "chat_widget", &priv->widget,
                                        "hpaned", &priv->hpaned,
                                        "vbox_left", &priv->vbox_left,
@@ -3136,7 +3246,7 @@ chat_create_ui (EmpathyChat *chat)
                                        "info_bar_vbox", &priv->info_bar_vbox,
                                        NULL);
 
-       empathy_builder_connect (gui, chat,
+       tpaw_builder_connect (gui, chat,
                "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
                "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
                NULL);
@@ -3258,6 +3368,7 @@ chat_finalize (GObject *object)
 
        g_object_unref (priv->account_manager);
        g_object_unref (priv->log_manager);
+       g_object_unref (priv->log_walker);
 
        if (priv->tp_chat) {
                g_signal_handlers_disconnect_by_func (priv->tp_chat,
@@ -3315,16 +3426,37 @@ chat_constructed (GObject *object)
 {
        EmpathyChat *chat = EMPATHY_CHAT (object);
        EmpathyChatPriv *priv = GET_PRIV (chat);
+       TplEntity *target;
+
+       if (priv->tp_chat != NULL) {
+               TpChannel *channel = TP_CHANNEL (priv->tp_chat);
+               TpConnection *conn = tp_channel_get_connection (channel);
+               gboolean supports_avatars =
+                       tp_proxy_has_interface_by_id (conn,
+                                                     TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS);
+
+               empathy_theme_adium_set_show_avatars (chat->view,
+                                                   supports_avatars);
+       }
+
+       /* Add messages from last conversations. Backlog messages are always
+        * prepended and pending messages are appended, so we can do both
+        * independently. Hacks like we previously had for bug #603980 are no
+        * longer needed. Pending messages are handled within
+        * empathy_chat_set_tp_chat() so we don't have to care about them here.
+        */
+       if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
+               target = tpl_entity_new_from_room_id (priv->id);
+       else
+               target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
+
+       priv->log_walker = tpl_log_manager_walk_filtered_events (priv->log_manager, priv->account, target,
+                                                                TPL_EVENT_MASK_TEXT, chat_log_filter, chat);
+       g_object_unref (target);
 
        if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
-               /* First display logs from the logger and then display pending messages */
                chat_add_logs (chat);
        }
-        else {
-               /* Just display pending messages for rooms */
-               priv->can_show_pending = TRUE;
-               show_pending_messages (chat);
-       }
 }
 
 static void
@@ -3486,7 +3618,7 @@ account_manager_prepared_cb (GObject *source_object,
                return;
        }
 
-       accounts = tp_account_manager_get_valid_accounts (account_manager);
+       accounts = tp_account_manager_dup_valid_accounts (account_manager);
 
        for (l = accounts; l != NULL; l = l->next) {
                TpAccount *account = l->data;
@@ -3495,7 +3627,7 @@ account_manager_prepared_cb (GObject *source_object,
                                             chat, 0);
        }
 
-       g_list_free (accounts);
+       g_list_free_full (accounts, g_object_unref);
 }
 
 static void
@@ -3530,6 +3662,9 @@ empathy_chat_init (EmpathyChat *chat)
        priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
        g_completion_set_compare (priv->completion, chat_contacts_completion_func);
 
+       /* Create UI early so by the time empathy_chat_set_tp_chat() is called
+        * (construct property) the view will already exists to receive pending
+        * messages. */
        chat_create_ui (chat);
 }
 
@@ -3584,7 +3719,7 @@ remember_password_infobar_response_cb (GtkWidget *info_bar,
 
        if (response_id == GTK_RESPONSE_OK) {
                DEBUG ("Saving room password");
-               empathy_keyring_set_room_password_async (priv->account,
+               tpaw_keyring_set_room_password_async (priv->account,
                                                         empathy_tp_chat_get_id (priv->tp_chat),
                                                         data->password,
                                                         NULL, NULL);
@@ -3704,14 +3839,8 @@ provide_password_cb (GObject *tp_chat,
                return;
        }
 
-       if (empathy_keyring_is_available ()) {
-               /* ask whether they want to save the password */
-               chat_prompt_to_save_password (self, data);
-       } else {
-               /* Get rid of the password info bar finally */
-               gtk_widget_destroy (data->info_bar);
-               g_slice_free (PasswordData, data);
-       }
+    /* ask whether they want to save the password */
+    chat_prompt_to_save_password (self, data);
 
        /* Room joined */
        gtk_widget_set_sensitive (priv->hpaned, TRUE);
@@ -3777,7 +3906,7 @@ password_entry_changed_cb (GtkEditable *entry,
        str = gtk_entry_get_text (GTK_ENTRY (entry));
 
        gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
-           GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
+           GTK_ENTRY_ICON_SECONDARY, !TPAW_STR_EMPTY (str));
 }
 
 static void
@@ -3926,7 +4055,7 @@ chat_room_got_password_cb (GObject *source,
        const gchar *password;
        GError *error = NULL;
 
-       password = empathy_keyring_get_room_password_finish (priv->account,
+       password = tpaw_keyring_get_room_password_finish (priv->account,
            result, &error);
 
        if (error != NULL) {
@@ -3948,7 +4077,7 @@ chat_password_needed_changed_cb (EmpathyChat *self)
        EmpathyChatPriv *priv = GET_PRIV (self);
 
        if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
-               empathy_keyring_get_room_password_async (priv->account,
+               tpaw_keyring_get_room_password_async (priv->account,
                                                         empathy_tp_chat_get_id (priv->tp_chat),
                                                         chat_room_got_password_cb, self);
        }
@@ -4043,7 +4172,7 @@ empathy_chat_set_tp_chat (EmpathyChat   *chat,
        if (chat->input_text_view) {
                gtk_widget_set_sensitive (chat->input_text_view, TRUE);
                if (priv->block_events_timeout_id == 0) {
-                       empathy_chat_view_append_event (chat->view, _("Connected"));
+                       empathy_theme_adium_append_event (chat->view, _("Connected"));
                }
        }
 
@@ -4051,9 +4180,6 @@ empathy_chat_set_tp_chat (EmpathyChat   *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);
 
        /* check if a password is needed */
@@ -4150,7 +4276,7 @@ empathy_chat_get_contact_menu (EmpathyChat *chat)
        if (individual == NULL)
                return NULL;
 
-       menu = empathy_individual_menu_new (individual,
+       menu = empathy_individual_menu_new (individual, NULL,
                                         EMPATHY_INDIVIDUAL_FEATURE_CALL |
                                         EMPATHY_INDIVIDUAL_FEATURE_LOG |
                                         EMPATHY_INDIVIDUAL_FEATURE_INFO |
@@ -4166,7 +4292,7 @@ empathy_chat_clear (EmpathyChat *chat)
 {
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
 
-       empathy_chat_view_clear (chat->view);
+       empathy_theme_adium_clear (chat->view);
 }
 
 void
@@ -4174,7 +4300,7 @@ empathy_chat_scroll_down (EmpathyChat *chat)
 {
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
 
-       empathy_chat_view_scroll_down (chat->view);
+       empathy_theme_adium_scroll_down (chat->view);
 }
 
 void
@@ -4194,51 +4320,72 @@ empathy_chat_cut (EmpathyChat *chat)
        }
 }
 
-void
-empathy_chat_copy (EmpathyChat *chat)
+static gboolean
+copy_from_chat_view (EmpathyChat *chat)
+{
+       if (!empathy_theme_adium_get_has_selection (chat->view))
+               return FALSE;
+
+       empathy_theme_adium_copy_clipboard (chat->view);
+       return TRUE;
+}
+
+static gboolean
+copy_from_input (EmpathyChat *chat)
 {
        GtkTextBuffer *buffer;
+       GtkClipboard *clipboard;
+
+       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
+       if (!gtk_text_buffer_get_has_selection (buffer))
+               return FALSE;
+
+       clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+       gtk_text_buffer_copy_clipboard (buffer, clipboard);
+       return TRUE;
+}
+
+static gboolean
+copy_from_topic (EmpathyChat *chat)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       gint start_offset;
+       gint end_offset;
+       gchar *start;
+       gchar *end;
+       gchar *selection;
+       const gchar *topic;
+       GtkClipboard *clipboard;
+
+       if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
+                                                      &start_offset,
+                                                      &end_offset))
+               return FALSE;
+
+       topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
+       start = g_utf8_offset_to_pointer (topic, start_offset);
+       end = g_utf8_offset_to_pointer (topic, end_offset);
+       selection = g_strndup (start, end - start);
 
+       clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+       gtk_clipboard_set_text (clipboard, selection, -1);
+
+       g_free (selection);
+       return TRUE;
+}
+
+void
+empathy_chat_copy (EmpathyChat *chat)
+{
        g_return_if_fail (EMPATHY_IS_CHAT (chat));
 
-       if (empathy_chat_view_get_has_selection (chat->view)) {
-               empathy_chat_view_copy_clipboard (chat->view);
+       if (copy_from_chat_view (chat))
                return;
-       }
 
-       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
-       if (gtk_text_buffer_get_has_selection (buffer)) {
-               GtkClipboard *clipboard;
-
-               clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+       if (copy_from_input (chat))
+               return;
 
-               gtk_text_buffer_copy_clipboard (buffer, clipboard);
-       }
-       else {
-               gint start_offset;
-               gint end_offset;
-               EmpathyChatPriv *priv = GET_PRIV (chat);
-
-               if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
-                                                              &start_offset,
-                                                              &end_offset)) {
-                       gchar *start;
-                       gchar *end;
-                       gchar *selection;
-                       const gchar *topic;
-                       GtkClipboard *clipboard;
-
-                       topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
-                       start = g_utf8_offset_to_pointer (topic, start_offset);
-                       end = g_utf8_offset_to_pointer (topic, end_offset);
-                       selection = g_strndup (start, end - start);
-
-                       clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
-                       gtk_clipboard_set_text (clipboard, selection, -1);
-
-                       g_free (selection);
-               }
-       }
+       copy_from_topic (chat);
 }
 
 void