]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-log-window.c
Merge commit 'staz/dnd'
[empathy.git] / libempathy-gtk / empathy-log-window.c
index b50ab9faf9207db74ec2729e6a23d46d6264b914..3345f5b150253db0d8141322925f84b3653f1105 100644 (file)
@@ -1,7 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2006-2007 Imendio AB
- * Copyright (C) 2007 Collabora Ltd.
+ * Copyright (C) 2007-2008 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -15,9 +15,9 @@
  *
  * You should have received a copy of the GNU General Public
  * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- * 
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
  * Authors: Martyn Russell <martyn@imendio.com>
  *          Xavier Claessens <xclaesse@gmail.com>
  */
 #include <string.h>
 #include <stdlib.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
-#include <glade/glade.h>
 
+#include <telepathy-glib/account-manager.h>
+#ifdef ENABLE_TPL
+#include <telepathy-logger/log-manager.h>
+#endif /* ENABLE_TPL */
+
+#ifndef ENABLE_TPL
 #include <libempathy/empathy-log-manager.h>
+#endif /* ENABLE_TPL */
 #include <libempathy/empathy-chatroom-manager.h>
 #include <libempathy/empathy-chatroom.h>
 #include <libempathy/empathy-message.h>
-#include <libempathy/empathy-debug.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-time.h>
 
 #include "empathy-log-window.h"
 #include "empathy-account-chooser.h"
 #include "empathy-chat-view.h"
+#include "empathy-theme-manager.h"
 #include "empathy-ui-utils.h"
 
-#define DEBUG_DOMAIN "LogWindow"
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include <libempathy/empathy-debug.h>
 
 typedef struct {
        GtkWidget         *window;
@@ -69,71 +76,57 @@ typedef struct {
 
        gchar             *last_find;
 
+#ifndef ENABLE_TPL
        EmpathyLogManager *log_manager;
+#else
+       TplLogManager     *log_manager;
+#endif /* ENABLE_TPL */
+
+       /* Those are only used while waiting for the account chooser to be ready */
+       TpAccount         *selected_account;
+       gchar             *selected_chat_id;
+       gboolean          selected_is_chatroom;
 } EmpathyLogWindow;
 
-static void
-log_window_destroy_cb (GtkWidget       *widget,
-                      EmpathyLogWindow *window);
-static void
-log_window_entry_find_changed_cb (GtkWidget       *entry,
-                                 EmpathyLogWindow *window);
-static void
-log_window_find_changed_cb (GtkTreeSelection *selection,
-                           EmpathyLogWindow  *window);
-static void
-log_window_find_populate (EmpathyLogWindow *window,
-                         const gchar     *search_criteria);
-static void
-log_window_find_setup (EmpathyLogWindow *window);
-static void
-log_window_button_find_clicked_cb (GtkWidget       *widget,
-                                  EmpathyLogWindow *window);
-static void
-log_window_button_next_clicked_cb (GtkWidget       *widget,
-                                  EmpathyLogWindow *window);
-static void
-log_window_button_previous_clicked_cb (GtkWidget       *widget,
-                                      EmpathyLogWindow *window);
-static void
-log_window_chats_changed_cb (GtkTreeSelection *selection,
-                            EmpathyLogWindow  *window);
-static void
-log_window_chats_populate (EmpathyLogWindow *window);
-static void
-log_window_chats_setup (EmpathyLogWindow *window);
-static void
-log_window_chats_accounts_changed_cb (GtkWidget       *combobox,
-                                     EmpathyLogWindow *window);
-static void
-log_window_chats_new_message_cb (EmpathyContact   *own_contact,
-                                EmpathyMessage   *message,
-                                EmpathyLogWindow *window);
-static void
-log_window_chats_set_selected  (EmpathyLogWindow *window,
-                               McAccount       *account,
-                               const gchar     *chat_id,
-                               gboolean         is_chatroom);
-static gboolean
-log_window_chats_get_selected (EmpathyLogWindow  *window,
-                              McAccount       **account,
-                              gchar           **chat_id,
-                              gboolean         *is_chatroom);
-static void
-log_window_chats_get_messages (EmpathyLogWindow *window,
-                              const gchar     *date_to_show);
-static void
-log_window_calendar_chats_day_selected_cb (GtkWidget       *calendar,
-                                          EmpathyLogWindow *window);
-static void
-log_window_calendar_chats_month_changed_cb (GtkWidget       *calendar,
-                                           EmpathyLogWindow *window);
-static void
-log_window_entry_chats_changed_cb (GtkWidget       *entry,
-                                  EmpathyLogWindow *window);
-static void
-log_window_entry_chats_activate_cb (GtkWidget       *entry,
-                                   EmpathyLogWindow *window);
+static void     log_window_destroy_cb                      (GtkWidget        *widget,
+                                                           EmpathyLogWindow *window);
+static void     log_window_entry_find_changed_cb           (GtkWidget        *entry,
+                                                           EmpathyLogWindow *window);
+static void     log_window_find_changed_cb                 (GtkTreeSelection *selection,
+                                                           EmpathyLogWindow *window);
+static void     log_window_find_populate                   (EmpathyLogWindow *window,
+                                                           const gchar      *search_criteria);
+static void     log_window_find_setup                      (EmpathyLogWindow *window);
+static void     log_window_button_find_clicked_cb          (GtkWidget        *widget,
+                                                           EmpathyLogWindow *window);
+static void     log_window_button_next_clicked_cb          (GtkWidget        *widget,
+                                                           EmpathyLogWindow *window);
+static void     log_window_button_previous_clicked_cb      (GtkWidget        *widget,
+                                                           EmpathyLogWindow *window);
+static void     log_window_chats_changed_cb                (GtkTreeSelection *selection,
+                                                           EmpathyLogWindow *window);
+static void     log_window_chats_populate                  (EmpathyLogWindow *window);
+static void     log_window_chats_setup                     (EmpathyLogWindow *window);
+static void     log_window_chats_accounts_changed_cb       (GtkWidget        *combobox,
+                                                           EmpathyLogWindow *window);
+static void     log_window_chats_set_selected              (EmpathyLogWindow *window,
+                                                           TpAccount        *account,
+                                                           const gchar      *chat_id,
+                                                           gboolean          is_chatroom);
+static gboolean log_window_chats_get_selected              (EmpathyLogWindow *window,
+                                                           TpAccount       **account,
+                                                           gchar           **chat_id,
+                                                           gboolean         *is_chatroom);
+static void     log_window_chats_get_messages              (EmpathyLogWindow *window,
+                                                           const gchar      *date_to_show);
+static void     log_window_calendar_chats_day_selected_cb  (GtkWidget        *calendar,
+                                                           EmpathyLogWindow *window);
+static void     log_window_calendar_chats_month_changed_cb (GtkWidget        *calendar,
+                                                           EmpathyLogWindow *window);
+static void     log_window_entry_chats_changed_cb          (GtkWidget        *entry,
+                                                           EmpathyLogWindow *window);
+static void     log_window_entry_chats_activate_cb         (GtkWidget        *entry,
+                                                           EmpathyLogWindow *window);
 
 enum {
        COL_FIND_ACCOUNT_ICON,
@@ -156,17 +149,56 @@ enum {
        COL_CHAT_COUNT
 };
 
-void
-empathy_log_window_show (McAccount   *account,
+static void
+account_manager_prepared_cb (GObject *source_object,
+                            GAsyncResult *result,
+                            gpointer user_data)
+{
+       TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
+       EmpathyLogWindow *window = user_data;
+       guint account_num;
+       GList *accounts;
+       GError *error = NULL;
+
+       if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
+               DEBUG ("Failed to prepare account manager: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       accounts = tp_account_manager_get_valid_accounts (account_manager);
+       account_num = g_list_length (accounts);
+       g_list_free (accounts);
+
+       if (account_num > 1) {
+               gtk_widget_show (window->vbox_chats);
+               gtk_widget_show (window->account_chooser_chats);
+       } else {
+               gtk_widget_hide (window->vbox_chats);
+               gtk_widget_hide (window->account_chooser_chats);
+       }
+}
+
+static void
+account_chooser_ready_cb (EmpathyAccountChooser *chooser,
+                       EmpathyLogWindow *window)
+{
+       gtk_notebook_set_current_page (GTK_NOTEBOOK (window->notebook), 1);
+       log_window_chats_set_selected (window, window->selected_account,
+                                      window->selected_chat_id, window->selected_is_chatroom);
+}
+
+GtkWidget *
+empathy_log_window_show (TpAccount  *account,
                        const gchar *chat_id,
                        gboolean     is_chatroom,
                        GtkWindow   *parent)
 {
        static EmpathyLogWindow *window = NULL;
        EmpathyAccountChooser   *account_chooser;
-       GList                  *accounts;
-       gint                    account_num;
-       GladeXML               *glade;
+       TpAccountManager        *account_manager;
+       GtkBuilder             *gui;
+       gchar                  *filename;
 
        if (window) {
                gtk_window_present (GTK_WINDOW (window->window));
@@ -177,15 +209,19 @@ empathy_log_window_show (McAccount   *account,
                                                       chat_id, is_chatroom);
                }
 
-               return;
+               return window->window;
        }
 
        window = g_new0 (EmpathyLogWindow, 1);
-       window->log_manager = empathy_log_manager_new ();
-
-       glade = empathy_glade_get_file ("empathy-log-window.glade",
-                                      "log_window",
-                                      NULL,
+#ifndef ENABLE_TPL
+       window->log_manager = empathy_log_manager_dup_singleton ();
+#else
+       window->log_manager = tpl_log_manager_dup_singleton ();
+#endif /* ENABLE_TPL */
+
+       filename = empathy_file_lookup ("empathy-log-window.ui",
+                                       "libempathy-gtk");
+       gui = empathy_builder_get_file (filename,
                                       "log_window", &window->window,
                                       "notebook", &window->notebook,
                                       "entry_find", &window->entry_find,
@@ -200,8 +236,9 @@ empathy_log_window_show (McAccount   *account,
                                       "treeview_chats", &window->treeview_chats,
                                       "scrolledwindow_chats", &window->scrolledwindow_chats,
                                       NULL);
-       empathy_glade_connect (glade,
-                             window,
+       g_free (filename);
+
+       empathy_builder_connect (gui, window,
                              "log_window", "destroy", log_window_destroy_cb,
                              "entry_find", "changed", log_window_entry_find_changed_cb,
                              "button_previous", "clicked", log_window_button_previous_clicked_cb,
@@ -211,7 +248,7 @@ empathy_log_window_show (McAccount   *account,
                              "entry_chats", "activate", log_window_entry_chats_activate_cb,
                              NULL);
 
-       g_object_unref (glade);
+       g_object_unref (gui);
 
        g_object_add_weak_pointer (G_OBJECT (window->window),
                                   (gpointer) &window);
@@ -225,13 +262,13 @@ empathy_log_window_show (McAccount   *account,
                          window);
 
        /* Configure Search EmpathyChatView */
-       window->chatview_find = empathy_chat_view_new ();
+       window->chatview_find = empathy_theme_manager_create_view (empathy_theme_manager_get ());
        gtk_container_add (GTK_CONTAINER (window->scrolledwindow_find),
                           GTK_WIDGET (window->chatview_find));
        gtk_widget_show (GTK_WIDGET (window->chatview_find));
 
        /* Configure Contacts EmpathyChatView */
-       window->chatview_chats = empathy_chat_view_new ();
+       window->chatview_chats = empathy_theme_manager_create_view (empathy_theme_manager_get ());
        gtk_container_add (GTK_CONTAINER (window->scrolledwindow_chats),
                           GTK_WIDGET (window->chatview_chats));
        gtk_widget_show (GTK_WIDGET (window->chatview_chats));
@@ -249,17 +286,10 @@ empathy_log_window_show (McAccount   *account,
                          window);
 
        /* Populate */
-       accounts = mc_accounts_list ();
-       account_num = g_list_length (accounts);
-       mc_accounts_list_free (accounts);
-
-       if (account_num > 1) {
-               gtk_widget_show (window->vbox_chats);
-               gtk_widget_show (window->account_chooser_chats);
-       } else {
-               gtk_widget_hide (window->vbox_chats);
-               gtk_widget_hide (window->account_chooser_chats);
-       }
+       account_manager = tp_account_manager_dup ();
+       tp_account_manager_prepare_async (account_manager, NULL,
+                                         account_manager_prepared_cb, window);
+       g_object_unref (account_manager);
 
        /* Search List */
        log_window_find_setup (window);
@@ -268,11 +298,17 @@ empathy_log_window_show (McAccount   *account,
        log_window_chats_setup (window);
        log_window_chats_populate (window);
 
-       /* Select chat */
        if (account && chat_id) {
-               gtk_notebook_set_current_page (GTK_NOTEBOOK (window->notebook), 1);
-               log_window_chats_set_selected (window, account,
-                                              chat_id, is_chatroom);
+               window->selected_account = account;
+               window->selected_chat_id = g_strdup (chat_id);
+               window->selected_is_chatroom = is_chatroom;
+
+               if (empathy_account_chooser_is_ready (account_chooser))
+                       account_chooser_ready_cb (account_chooser, window);
+               else
+                       /* Chat will be selected once the account chooser is ready */
+                       g_signal_connect (account_chooser, "ready",
+                                         G_CALLBACK (account_chooser_ready_cb), window);
        }
 
        if (parent) {
@@ -281,18 +317,17 @@ empathy_log_window_show (McAccount   *account,
        }
 
        gtk_widget_show (window->window);
+
+       return window->window;
 }
 
 static void
 log_window_destroy_cb (GtkWidget       *widget,
                       EmpathyLogWindow *window)
 {
-       g_signal_handlers_disconnect_by_func (window->log_manager,
-                                             log_window_chats_new_message_cb,
-                                             window);
-
        g_free (window->last_find);
        g_object_unref (window->log_manager);
+       g_free (window->selected_chat_id);
 
        g_free (window);
 }
@@ -309,14 +344,72 @@ log_window_entry_find_changed_cb (GtkWidget       *entry,
 
        str = gtk_entry_get_text (GTK_ENTRY (window->entry_find));
 
-       is_sensitive &= !G_STR_EMPTY (str);
-       is_sensitive &= 
-               !window->last_find || 
+       is_sensitive &= !EMP_STR_EMPTY (str);
+       is_sensitive &=
+               !window->last_find ||
                (window->last_find && strcmp (window->last_find, str) != 0);
 
        gtk_widget_set_sensitive (window->button_find, is_sensitive);
 }
 
+#ifdef ENABLE_TPL
+static void
+got_messages_for_date_cb (GObject *manager,
+                       GAsyncResult *result,
+                       gpointer user_data)
+{
+       EmpathyLogWindow *window = user_data;
+       GList         *messages;
+       GList         *l;
+       gboolean       can_do_previous;
+       gboolean       can_do_next;
+       GError        *error = NULL;
+
+       messages = tpl_log_manager_get_messages_for_date_async_finish (result, &error);
+
+       if (error != NULL) {
+                       DEBUG ("Unable to retrieve messages for the selected date: %s. Aborting",
+                                       error->message);
+                       empathy_chat_view_append_event (window->chatview_find,
+                                       "Unable to retrieve messages for the selected date");
+                       g_error_free (error);
+                       return;
+       }
+
+       for (l = messages; l; l = l->next) {
+                       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 (window->chatview_find, message);
+                       g_object_unref (message);
+       }
+       g_list_free (messages);
+
+       /* Scroll to the most recent messages */
+       empathy_chat_view_scroll (window->chatview_find, TRUE);
+
+       /* Highlight and find messages */
+       empathy_chat_view_highlight (window->chatview_find,
+                       window->last_find,
+                       FALSE);
+       empathy_chat_view_find_next (window->chatview_find,
+                       window->last_find,
+                       TRUE,
+                       FALSE);
+       empathy_chat_view_find_abilities (window->chatview_find,
+                       window->last_find,
+                       FALSE,
+                       &can_do_previous,
+                       &can_do_next);
+       gtk_widget_set_sensitive (window->button_previous, can_do_previous);
+       gtk_widget_set_sensitive (window->button_next, can_do_next);
+       gtk_widget_set_sensitive (window->button_find, FALSE);
+}
+#endif /* ENABLE_TPL */
+
 static void
 log_window_find_changed_cb (GtkTreeSelection *selection,
                            EmpathyLogWindow  *window)
@@ -324,15 +417,17 @@ log_window_find_changed_cb (GtkTreeSelection *selection,
        GtkTreeView   *view;
        GtkTreeModel  *model;
        GtkTreeIter    iter;
-       McAccount     *account;
+       TpAccount     *account;
        gchar         *chat_id;
        gboolean       is_chatroom;
        gchar         *date;
+#ifndef ENABLE_TPL
        EmpathyMessage *message;
        GList         *messages;
        GList         *l;
        gboolean       can_do_previous;
        gboolean       can_do_next;
+#endif /* ENABLE_TPL */
 
        /* Get selected information */
        view = GTK_TREE_VIEW (window->treeview_find);
@@ -343,7 +438,7 @@ log_window_find_changed_cb (GtkTreeSelection *selection,
                gtk_widget_set_sensitive (window->button_next, FALSE);
 
                empathy_chat_view_clear (window->chatview_find);
-       
+
                return;
        }
 
@@ -364,15 +459,26 @@ log_window_find_changed_cb (GtkTreeSelection *selection,
        empathy_chat_view_scroll (window->chatview_find, FALSE);
 
        /* Get messages */
+#ifndef ENABLE_TPL
        messages = empathy_log_manager_get_messages_for_date (window->log_manager,
                                                              account,
                                                              chat_id,
                                                              is_chatroom,
                                                              date);
+#else
+       tpl_log_manager_get_messages_for_date_async (window->log_manager,
+                                                             account,
+                                                             chat_id,
+                                                             is_chatroom,
+                                                             date,
+                                                             got_messages_for_date_cb,
+                                                             window);
+#endif /* ENABLE_TPL */
        g_object_unref (account);
        g_free (date);
        g_free (chat_id);
 
+#ifndef ENABLE_TPL
        for (l = messages; l; l = l->next) {
                message = l->data;
                empathy_chat_view_append_message (window->chatview_find, message);
@@ -385,30 +491,102 @@ log_window_find_changed_cb (GtkTreeSelection *selection,
 
        /* Highlight and find messages */
        empathy_chat_view_highlight (window->chatview_find,
-                                   window->last_find);
+                                   window->last_find, FALSE);
        empathy_chat_view_find_next (window->chatview_find,
                                    window->last_find,
-                                   TRUE);
+                                   TRUE,
+                                   FALSE);
        empathy_chat_view_find_abilities (window->chatview_find,
                                         window->last_find,
+                                        FALSE,
                                         &can_do_previous,
                                         &can_do_next);
        gtk_widget_set_sensitive (window->button_previous, can_do_previous);
        gtk_widget_set_sensitive (window->button_next, can_do_next);
        gtk_widget_set_sensitive (window->button_find, FALSE);
+#endif /* ENABLE_TPL */
 }
 
+
+#ifdef ENABLE_TPL
+static void
+log_manager_searched_new_cb (GObject *manager,
+                             GAsyncResult *result,
+                             gpointer user_data)
+{
+       GList               *hits;
+       GList               *l;
+       GtkTreeIter          iter;
+       GtkListStore        *store = user_data;
+       GError              *error = NULL;
+
+       hits = tpl_log_manager_search_new_async_finish (result, &error);
+
+       if (error != NULL) {
+                       DEBUG ("%s. Aborting", error->message);
+                       g_error_free (error);
+                       return;
+       }
+
+       for (l = hits; l; l = l->next) {
+                       TplLogSearchHit *hit;
+                       const gchar         *account_name;
+                       const gchar         *account_icon;
+                       gchar               *date_readable;
+
+                       hit = l->data;
+
+                       /* Protect against invalid data (corrupt or old log files. */
+                       if (!hit->account || !hit->chat_id) {
+                                       continue;
+                       }
+
+                       date_readable = tpl_log_manager_get_date_readable (hit->date);
+                       account_name = tp_account_get_display_name (hit->account);
+                       account_icon = tp_account_get_icon_name (hit->account);
+
+                       gtk_list_store_append (store, &iter);
+                       gtk_list_store_set (store, &iter,
+                                       COL_FIND_ACCOUNT_ICON, account_icon,
+                                       COL_FIND_ACCOUNT_NAME, account_name,
+                                       COL_FIND_ACCOUNT, hit->account,
+                                       COL_FIND_CHAT_NAME, hit->chat_id, /* FIXME */
+                                       COL_FIND_CHAT_ID, hit->chat_id,
+                                       COL_FIND_IS_CHATROOM, hit->is_chatroom,
+                                       COL_FIND_DATE, hit->date,
+                                       COL_FIND_DATE_READABLE, date_readable,
+                                       -1);
+
+                       g_free (date_readable);
+
+                       /* FIXME: Update COL_FIND_CHAT_NAME */
+                       if (hit->is_chatroom) {
+                       } else {
+                       }
+       }
+
+       if (hits) {
+                       tpl_log_manager_search_free (hits);
+       }
+}
+#endif /* ENABLE_TPL */
+
+
 static void
 log_window_find_populate (EmpathyLogWindow *window,
                          const gchar     *search_criteria)
 {
+#ifndef ENABLE_TPL
        GList              *hits, *l;
 
+#endif /* ENABLE_TPL */
        GtkTreeView        *view;
        GtkTreeModel       *model;
        GtkTreeSelection   *selection;
        GtkListStore       *store;
+#ifndef ENABLE_TPL
        GtkTreeIter         iter;
+#endif /* ENABLE_TPL */
 
        view = GTK_TREE_VIEW (window->treeview_find);
        model = gtk_tree_view_get_model (view);
@@ -419,11 +597,15 @@ log_window_find_populate (EmpathyLogWindow *window,
 
        gtk_list_store_clear (store);
 
-       if (G_STR_EMPTY (search_criteria)) {
+       if (EMP_STR_EMPTY (search_criteria)) {
                /* Just clear the search. */
                return;
        }
 
+#ifdef ENABLE_TPL
+       tpl_log_manager_search_new_async (window->log_manager, search_criteria,
+                       log_manager_searched_new_cb, (gpointer) store);
+#else
        hits = empathy_log_manager_search_new (window->log_manager, search_criteria);
 
        for (l = hits; l; l = l->next) {
@@ -440,8 +622,8 @@ log_window_find_populate (EmpathyLogWindow *window,
                }
 
                date_readable = empathy_log_manager_get_date_readable (hit->date);
-               account_name = mc_account_get_display_name (hit->account);
-               account_icon = empathy_icon_name_from_account (hit->account);
+               account_name = tp_account_get_display_name (hit->account);
+               account_icon = tp_account_get_icon_name (hit->account);
 
                gtk_list_store_append (store, &iter);
                gtk_list_store_set (store, &iter,
@@ -466,6 +648,7 @@ log_window_find_populate (EmpathyLogWindow *window,
        if (hits) {
                empathy_log_manager_search_free (hits);
        }
+#endif /* ENABLE_TPL */
 }
 
 static void
@@ -487,7 +670,7 @@ log_window_find_setup (EmpathyLogWindow *window)
        store = gtk_list_store_new (COL_FIND_COUNT,
                                    G_TYPE_STRING,          /* account icon name */
                                    G_TYPE_STRING,          /* account name */
-                                   MC_TYPE_ACCOUNT,        /* account */
+                                   TP_TYPE_ACCOUNT,        /* account */
                                    G_TYPE_STRING,          /* chat name */
                                    G_TYPE_STRING,          /* chat id */
                                    G_TYPE_BOOLEAN,         /* is chatroom */
@@ -583,9 +766,11 @@ log_window_button_next_clicked_cb (GtkWidget       *widget,
 
                empathy_chat_view_find_next (window->chatview_find,
                                            window->last_find,
+                                           FALSE,
                                            FALSE);
                empathy_chat_view_find_abilities (window->chatview_find,
                                                 window->last_find,
+                                                FALSE,
                                                 &can_do_previous,
                                                 &can_do_next);
                gtk_widget_set_sensitive (window->button_previous, can_do_previous);
@@ -603,9 +788,11 @@ log_window_button_previous_clicked_cb (GtkWidget       *widget,
 
                empathy_chat_view_find_previous (window->chatview_find,
                                                window->last_find,
+                                               FALSE,
                                                FALSE);
                empathy_chat_view_find_abilities (window->chatview_find,
                                                 window->last_find,
+                                                FALSE,
                                                 &can_do_previous,
                                                 &can_do_next);
                gtk_widget_set_sensitive (window->button_previous, can_do_previous);
@@ -627,27 +814,101 @@ log_window_chats_changed_cb (GtkTreeSelection *selection,
        log_window_chats_get_messages (window, NULL);
 }
 
+#ifdef ENABLE_TPL
+static void
+log_manager_got_chats_cb (GObject *manager,
+                       GAsyncResult *result,
+                       gpointer user_data)
+{
+       EmpathyLogWindow      *window = user_data;
+       GList                 *chats;
+       GList                 *l;
+       EmpathyAccountChooser *account_chooser;
+       TpAccount             *account;
+       GtkTreeView           *view;
+       GtkTreeModel          *model;
+       GtkTreeSelection      *selection;
+       GtkListStore          *store;
+       GtkTreeIter            iter;
+       GError                *error = NULL;
+
+       chats = tpl_log_manager_get_chats_async_finish (result, &error);
+
+       if (error != NULL) {
+                       DEBUG ("%s. Aborting", error->message);
+                       g_error_free (error);
+                       return;
+       }
+
+       account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser_chats);
+       account = empathy_account_chooser_dup_account (account_chooser);
+
+       view = GTK_TREE_VIEW (window->treeview_chats);
+       model = gtk_tree_view_get_model (view);
+       selection = gtk_tree_view_get_selection (view);
+       store = GTK_LIST_STORE (model);
+
+       for (l = chats; l; l = l->next) {
+                       TplLogSearchHit *hit;
+
+                       hit = l->data;
+
+                       gtk_list_store_append (store, &iter);
+                       gtk_list_store_set (store, &iter,
+                                       COL_CHAT_ICON, "empathy-available", /* FIXME */
+                                       COL_CHAT_NAME, hit->chat_id,
+                                       COL_CHAT_ACCOUNT, account,
+                                       COL_CHAT_ID, hit->chat_id,
+                                       COL_CHAT_IS_CHATROOM, hit->is_chatroom,
+                                       -1);
+
+                       /* FIXME: Update COL_CHAT_ICON/NAME */
+                       if (hit->is_chatroom) {
+                       } else {
+                       }
+       }
+       tpl_log_manager_search_free (chats);
+
+       /* Unblock signals */
+       g_signal_handlers_unblock_by_func (selection,
+                       log_window_chats_changed_cb,
+                       window);
+
+       g_object_unref (account);
+}
+#endif /* ENABLE_TPL */
+
+
 static void
 log_window_chats_populate (EmpathyLogWindow *window)
 {
        EmpathyAccountChooser *account_chooser;
-       McAccount            *account;
+       TpAccount             *account;
+#ifndef ENABLE_TPL
        GList                *chats, *l;
+#endif /* ENABLE_TPL */
 
        GtkTreeView          *view;
        GtkTreeModel         *model;
        GtkTreeSelection     *selection;
        GtkListStore         *store;
+#ifndef ENABLE_TPL
        GtkTreeIter           iter;
+#endif /* ENABLE_TPL */
 
        account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser_chats);
-       account = empathy_account_chooser_get_account (account_chooser);
+       account = empathy_account_chooser_dup_account (account_chooser);
 
        view = GTK_TREE_VIEW (window->treeview_chats);
        model = gtk_tree_view_get_model (view);
        selection = gtk_tree_view_get_selection (view);
        store = GTK_LIST_STORE (model);
 
+       if (account == NULL) {
+               gtk_list_store_clear (store);
+               return;
+       }
+
        /* Block signals to stop the logs being retrieved prematurely */
        g_signal_handlers_block_by_func (selection,
                                         log_window_chats_changed_cb,
@@ -655,6 +916,10 @@ log_window_chats_populate (EmpathyLogWindow *window)
 
        gtk_list_store_clear (store);
 
+#ifdef ENABLE_TPL
+       tpl_log_manager_get_chats_async (window->log_manager, account,
+                       log_manager_got_chats_cb, (gpointer) window);
+#else
        chats = empathy_log_manager_get_chats (window->log_manager, account);
        for (l = chats; l; l = l->next) {
                EmpathyLogSearchHit *hit;
@@ -664,7 +929,7 @@ log_window_chats_populate (EmpathyLogWindow *window)
                gtk_list_store_append (store, &iter);
                gtk_list_store_set (store, &iter,
                                    COL_CHAT_ICON, "empathy-available", /* FIXME */
-                                   COL_CHAT_NAME, hit->chat_id,                                
+                                   COL_CHAT_NAME, hit->chat_id,
                                    COL_CHAT_ACCOUNT, account,
                                    COL_CHAT_ID, hit->chat_id,
                                    COL_CHAT_IS_CHATROOM, hit->is_chatroom,
@@ -684,6 +949,7 @@ log_window_chats_populate (EmpathyLogWindow *window)
 
 
        g_object_unref (account);
+#endif /* ENABLE_TPL */
 }
 
 static void
@@ -702,11 +968,11 @@ log_window_chats_setup (EmpathyLogWindow *window)
 
        /* new store */
        store = gtk_list_store_new (COL_CHAT_COUNT,
-                                   G_TYPE_STRING,    /* icon */
-                                   G_TYPE_STRING,    /* name */
-                                   MC_TYPE_ACCOUNT,  /* account */
-                                   G_TYPE_STRING,    /* id */
-                                   G_TYPE_BOOLEAN);  /* is chatroom */
+                                   G_TYPE_STRING,        /* icon */
+                                   G_TYPE_STRING,        /* name */
+                                   TP_TYPE_ACCOUNT,      /* account */
+                                   G_TYPE_STRING,        /* id */
+                                   G_TYPE_BOOLEAN);      /* is chatroom */
 
        model = GTK_TREE_MODEL (store);
        sortable = GTK_TREE_SORTABLE (store);
@@ -755,20 +1021,9 @@ log_window_chats_accounts_changed_cb (GtkWidget       *combobox,
        log_window_chats_populate (window);
 }
 
-static void
-log_window_chats_new_message_cb (EmpathyContact   *own_contact,
-                                EmpathyMessage   *message,
-                                EmpathyLogWindow *window)
-{
-       empathy_chat_view_append_message (window->chatview_chats, message);
-
-       /* Scroll to the most recent messages */
-       empathy_chat_view_scroll_down (window->chatview_chats);
-}
-
 static void
 log_window_chats_set_selected  (EmpathyLogWindow *window,
-                               McAccount       *account,
+                               TpAccount        *account,
                                const gchar     *chat_id,
                                gboolean         is_chatroom)
 {
@@ -792,7 +1047,7 @@ log_window_chats_set_selected  (EmpathyLogWindow *window,
        }
 
        for (ok = TRUE; ok; ok = gtk_tree_model_iter_next (model, &iter)) {
-               McAccount *this_account;
+               TpAccount *this_account;
                gchar     *this_chat_id;
                gboolean   this_is_chatroom;
 
@@ -802,7 +1057,7 @@ log_window_chats_set_selected  (EmpathyLogWindow *window,
                                    COL_CHAT_IS_CHATROOM, &this_is_chatroom,
                                    -1);
 
-               if (empathy_account_equal (this_account, account) &&
+               if (this_account == account &&
                    strcmp (this_chat_id, chat_id) == 0 &&
                    this_is_chatroom == is_chatroom) {
                        gtk_tree_selection_select_iter (selection, &iter);
@@ -821,7 +1076,7 @@ log_window_chats_set_selected  (EmpathyLogWindow *window,
 
 static gboolean
 log_window_chats_get_selected (EmpathyLogWindow  *window,
-                              McAccount       **account,
+                              TpAccount       **account,
                               gchar           **chat_id,
                               gboolean         *is_chatroom)
 {
@@ -830,7 +1085,7 @@ log_window_chats_get_selected (EmpathyLogWindow  *window,
        GtkTreeSelection *selection;
        GtkTreeIter       iter;
        gchar            *id = NULL;
-       McAccount        *acc = NULL;
+       TpAccount        *acc = NULL;
        gboolean          room = FALSE;
 
        view = GTK_TREE_VIEW (window->treeview_chats);
@@ -864,11 +1119,224 @@ log_window_chats_get_selected (EmpathyLogWindow  *window,
        return TRUE;
 }
 
+#ifdef ENABLE_TPL
+static void
+log_window_got_messages_for_date_cb (GObject *manager,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  EmpathyLogWindow *window = user_data;
+  GList *messages;
+  GList *l;
+  GError *error = NULL;
+
+  messages = tpl_log_manager_get_messages_for_date_async_finish (result, &error);
+
+  if (error != NULL) {
+      DEBUG ("Unable to retrieve messages for the selected date: %s. Aborting",
+          error->message);
+      empathy_chat_view_append_event (window->chatview_find,
+          "Unable to retrieve messages for the selected date");
+      g_error_free (error);
+      return;
+  }
+
+  for (l = messages; l; l = l->next) {
+      EmpathyMessage *message = empathy_message_from_tpl_log_entry (l->data);
+      g_object_unref (l->data);
+      empathy_chat_view_append_message (window->chatview_chats,
+          message);
+      g_object_unref (message);
+  }
+  g_list_free (messages);
+
+  /* Turn back on scrolling */
+  empathy_chat_view_scroll (window->chatview_find, TRUE);
+
+  /* Give the search entry main focus */
+  gtk_widget_grab_focus (window->entry_chats);
+}
+
+
+static void
+log_window_get_messages_for_date (EmpathyLogWindow *window,
+                                 const gchar *date)
+{
+  TpAccount *account;
+  gchar *chat_id;
+  gboolean is_chatroom;
+
+  gtk_calendar_clear_marks (GTK_CALENDAR (window->calendar_chats));
+
+  if (!log_window_chats_get_selected (window, &account,
+        &chat_id, &is_chatroom)) {
+      return;
+  }
+
+  /* Clear all current messages shown in the textview */
+  empathy_chat_view_clear (window->chatview_chats);
+
+  /* Turn off scrolling temporarily */
+  empathy_chat_view_scroll (window->chatview_find, FALSE);
+
+  /* Get messages */
+  tpl_log_manager_get_messages_for_date_async (window->log_manager,
+      account, chat_id,
+      is_chatroom,
+      date,
+      log_window_got_messages_for_date_cb,
+      (gpointer) window);
+}
+
+static void
+log_manager_got_dates_cb (GObject *manager,
+                          GAsyncResult *result,
+                          gpointer user_data)
+{
+  EmpathyLogWindow *window = user_data;
+  GList         *dates;
+  GList         *l;
+  guint          year_selected;
+  guint          year;
+  guint          month;
+  guint          month_selected;
+  guint          day;
+  gboolean       day_selected = FALSE;
+  const gchar   *date = NULL;
+  GError        *error = NULL;
+
+  dates = tpl_log_manager_get_dates_async_finish (result, &error);
+
+  if (error != NULL) {
+    DEBUG ("Unable to retrieve messages' dates: %s. Aborting",
+        error->message);
+    empathy_chat_view_append_event (window->chatview_find,
+        "Unable to retrieve messages' dates");
+      return;
+  }
+
+  for (l = dates; l; l = l->next) {
+      const gchar *str;
+
+      str = l->data;
+      if (!str) {
+          continue;
+      }
+
+      sscanf (str, "%4d%2d%2d", &year, &month, &day);
+      gtk_calendar_get_date (GTK_CALENDAR (window->calendar_chats),
+          &year_selected,
+          &month_selected,
+          NULL);
+
+      month_selected++;
+
+      if (!l->next) {
+          date = str;
+      }
+
+      if (year != year_selected || month != month_selected) {
+          continue;
+      }
+
+      DEBUG ("Marking date:'%s'", str);
+      gtk_calendar_mark_day (GTK_CALENDAR (window->calendar_chats), day);
+
+      if (l->next) {
+          continue;
+      }
+
+      day_selected = TRUE;
+
+      gtk_calendar_select_day (GTK_CALENDAR (window->calendar_chats), day);
+  }
+
+  if (!day_selected) {
+      /* Unselect the day in the calendar */
+      gtk_calendar_select_day (GTK_CALENDAR (window->calendar_chats), 0);
+  }
+
+  g_signal_handlers_unblock_by_func (window->calendar_chats,
+      log_window_calendar_chats_day_selected_cb,
+      window);
+
+  if (date) {
+      log_window_get_messages_for_date (window, date);
+  }
+
+  g_list_foreach (dates, (GFunc) g_free, NULL);
+  g_list_free (dates);
+}
+
+
 static void
 log_window_chats_get_messages (EmpathyLogWindow *window,
                               const gchar     *date_to_show)
 {
-       McAccount     *account;
+       TpAccount     *account;
+       gchar         *chat_id;
+       gboolean       is_chatroom;
+       const gchar   *date;
+       guint          year_selected;
+       guint          year;
+       guint          month;
+       guint          month_selected;
+       guint          day;
+
+
+       if (!log_window_chats_get_selected (window, &account,
+                                           &chat_id, &is_chatroom)) {
+               return;
+       }
+
+       g_signal_handlers_block_by_func (window->calendar_chats,
+                                        log_window_calendar_chats_day_selected_cb,
+                                        window);
+
+       /* Either use the supplied date or get the last */
+       date = date_to_show;
+       if (!date) {
+               /* Get a list of dates and show them on the calendar */
+               tpl_log_manager_get_dates_async (window->log_manager,
+                                                      account, chat_id,
+                                                      is_chatroom,
+                                                      log_manager_got_dates_cb, (gpointer) window);
+    /* signal unblocked at the end of the CB flow */
+       } else {
+               sscanf (date, "%4d%2d%2d", &year, &month, &day);
+               gtk_calendar_get_date (GTK_CALENDAR (window->calendar_chats),
+                               &year_selected,
+                               &month_selected,
+                               NULL);
+
+               month_selected++;
+
+               if (year != year_selected && month != month_selected) {
+                       day = 0;
+               }
+
+               gtk_calendar_select_day (GTK_CALENDAR (window->calendar_chats), day);
+
+    g_signal_handlers_unblock_by_func (window->calendar_chats,
+        log_window_calendar_chats_day_selected_cb,
+        window);
+       }
+
+       if (date) {
+      log_window_get_messages_for_date (window, date);
+       }
+
+       g_object_unref (account);
+       g_free (chat_id);
+}
+
+#else
+
+static void
+log_window_chats_get_messages (EmpathyLogWindow *window,
+                              const gchar     *date_to_show)
+{
+       TpAccount     *account;
        gchar         *chat_id;
        gboolean       is_chatroom;
        EmpathyMessage *message;
@@ -926,7 +1394,7 @@ log_window_chats_get_messages (EmpathyLogWindow *window,
                        }
 
 
-                       empathy_debug (DEBUG_DOMAIN, "Marking date:'%s'", str);
+                       DEBUG ("Marking date:'%s'", str);
                        gtk_calendar_mark_day (GTK_CALENDAR (window->calendar_chats), day);
 
                        if (l->next) {
@@ -957,10 +1425,15 @@ log_window_chats_get_messages (EmpathyLogWindow *window,
 
                gtk_calendar_select_day (GTK_CALENDAR (window->calendar_chats), day);
        }
+
        g_signal_handlers_unblock_by_func (window->calendar_chats,
                                           log_window_calendar_chats_day_selected_cb,
                                           window);
 
+       if (!date) {
+               goto OUT;
+       }
+
        /* Clear all current messages shown in the textview */
        empathy_chat_view_clear (window->chatview_chats);
 
@@ -982,22 +1455,21 @@ log_window_chats_get_messages (EmpathyLogWindow *window,
        }
        g_list_free (messages);
 
-       g_list_foreach (dates, (GFunc) g_free, NULL);
-       g_list_free (dates);
-
-       g_object_unref (account);
-       g_free (chat_id);
-
        /* Turn back on scrolling */
        empathy_chat_view_scroll (window->chatview_find, TRUE);
 
-       /* Scroll to the most recent messages */
-       empathy_chat_view_scroll_down (window->chatview_chats);
-
        /* Give the search entry main focus */
        gtk_widget_grab_focus (window->entry_chats);
+
+OUT:
+       g_list_foreach (dates, (GFunc) g_free, NULL);
+       g_list_free (dates);
+       g_object_unref (account);
+       g_free (chat_id);
 }
 
+#endif /* ENABLE_TPL */
+
 static void
 log_window_calendar_chats_day_selected_cb (GtkWidget       *calendar,
                                           EmpathyLogWindow *window)
@@ -1015,48 +1487,115 @@ log_window_calendar_chats_day_selected_cb (GtkWidget       *calendar,
 
        date = g_strdup_printf ("%4.4d%2.2d%2.2d", year, month, day);
 
-       empathy_debug (DEBUG_DOMAIN, "Currently selected date is:'%s'", date);
+       DEBUG ("Currently selected date is:'%s'", date);
 
        log_window_chats_get_messages (window, date);
 
        g_free (date);
 }
 
+
+#ifdef ENABLE_TPL
+static void
+log_window_updating_calendar_month_cb (GObject *manager,
+               GAsyncResult *result, gpointer user_data)
+{
+       EmpathyLogWindow *window = user_data;
+       GList                                   *dates;
+       GList                                   *l;
+       guint                                    year_selected;
+       guint                                    month_selected;
+       GError                          *error = NULL;
+
+       dates = tpl_log_manager_get_dates_async_finish (result, &error);
+
+       if (error != NULL) {
+                       DEBUG ("Unable to retrieve messages' dates: %s. Aborting",
+                                       error->message);
+                       empathy_chat_view_append_event (window->chatview_find,
+                                       "Unable to retrieve messages' dates");
+                       g_error_free (error);
+                       return;
+       }
+
+       gtk_calendar_clear_marks (GTK_CALENDAR (window->calendar_chats));
+       g_object_get (window->calendar_chats,
+                       "month", &month_selected,
+                       "year", &year_selected,
+                       NULL);
+
+       /* We need this here because it appears that the months start from 0 */
+       month_selected++;
+
+       for (l = dates; l; l = l->next) {
+                       const gchar *str;
+                       guint        year;
+                       guint        month;
+                       guint        day;
+
+                       str = l->data;
+                       if (!str) {
+                                       continue;
+                       }
+
+                       sscanf (str, "%4d%2d%2d", &year, &month, &day);
+
+                       if (year == year_selected && month == month_selected) {
+                                       DEBUG ("Marking date:'%s'", str);
+                                       gtk_calendar_mark_day (GTK_CALENDAR (window->calendar_chats), day);
+                       }
+       }
+
+       g_list_foreach (dates, (GFunc) g_free, NULL);
+       g_list_free (dates);
+
+       DEBUG ("Currently showing month %d and year %d", month_selected,
+                       year_selected);
+}
+#endif /* ENABLE_TPL */
+
+
 static void
 log_window_calendar_chats_month_changed_cb (GtkWidget       *calendar,
                                            EmpathyLogWindow *window)
 {
-       McAccount     *account;
+       TpAccount     *account;
        gchar         *chat_id;
        gboolean       is_chatroom;
+#ifndef ENABLE_TPL
        guint          year_selected;
        guint          month_selected;
 
        GList         *dates;
        GList         *l;
+#endif /* ENABLE_TPL */
 
        gtk_calendar_clear_marks (GTK_CALENDAR (calendar));
 
        if (!log_window_chats_get_selected (window, &account,
                                            &chat_id, &is_chatroom)) {
-               empathy_debug (DEBUG_DOMAIN, "No chat selected to get dates for...");
+               DEBUG ("No chat selected to get dates for...");
                return;
        }
 
+       /* Get the log object for this contact */
+#ifdef ENABLE_TPL
+       tpl_log_manager_get_dates_async (window->log_manager, account,
+                                              chat_id, is_chatroom,
+                                              log_window_updating_calendar_month_cb,
+                                              (gpointer) window);
+#else
+       dates = empathy_log_manager_get_dates (window->log_manager, account,
+                                              chat_id, is_chatroom);
+
        g_object_get (calendar,
                      "month", &month_selected,
                      "year", &year_selected,
                      NULL);
 
-       /* We need this hear because it appears that the months start from 0 */
+       /* We need this here because it appears that the months start from 0 */
        month_selected++;
 
-       /* Get the log object for this contact */
-       dates = empathy_log_manager_get_dates (window->log_manager, account,
-                                              chat_id, is_chatroom);
-       g_object_unref (account);
-       g_free (chat_id);
-
        for (l = dates; l; l = l->next) {
                const gchar *str;
                guint        year;
@@ -1071,7 +1610,7 @@ log_window_calendar_chats_month_changed_cb (GtkWidget       *calendar,
                sscanf (str, "%4d%2d%2d", &year, &month, &day);
 
                if (year == year_selected && month == month_selected) {
-                       empathy_debug (DEBUG_DOMAIN, "Marking date:'%s'", str);
+                       DEBUG ("Marking date:'%s'", str);
                        gtk_calendar_mark_day (GTK_CALENDAR (window->calendar_chats), day);
                }
        }
@@ -1079,9 +1618,12 @@ log_window_calendar_chats_month_changed_cb (GtkWidget       *calendar,
        g_list_foreach (dates, (GFunc) g_free, NULL);
        g_list_free (dates);
 
-       empathy_debug (DEBUG_DOMAIN,
-                     "Currently showing month %d and year %d",
-                     month_selected, year_selected);
+       DEBUG ("Currently showing month %d and year %d", month_selected,
+               year_selected);
+#endif /* ENABLE_TPL */
+
+       g_object_unref (account);
+       g_free (chat_id);
 }
 
 static void
@@ -1091,12 +1633,13 @@ log_window_entry_chats_changed_cb (GtkWidget       *entry,
        const gchar *str;
 
        str = gtk_entry_get_text (GTK_ENTRY (window->entry_chats));
-       empathy_chat_view_highlight (window->chatview_chats, str);
+       empathy_chat_view_highlight (window->chatview_chats, str, FALSE);
 
        if (str) {
                empathy_chat_view_find_next (window->chatview_chats,
                                            str,
-                                           TRUE);
+                                           TRUE,
+                                           FALSE);
        }
 }
 
@@ -1111,6 +1654,7 @@ log_window_entry_chats_activate_cb (GtkWidget       *entry,
        if (str) {
                empathy_chat_view_find_next (window->chatview_chats,
                                            str,
+                                           FALSE,
                                            FALSE);
        }
 }