]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-log-window.c
Set the edited icon for edited messages in the log viewer
[empathy.git] / libempathy-gtk / empathy-log-window.c
1 /*
2  * Copyright (C) 2006-2007 Imendio AB
3  * Copyright (C) 2007-2011 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  *          Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <glib/gi18n-lib.h>
31 #include <gtk/gtk.h>
32
33 #include <telepathy-glib/telepathy-glib.h>
34 #include <telepathy-glib/proxy-subclass.h>
35
36 #include <telepathy-logger/telepathy-logger.h>
37 #ifdef HAVE_CALL_LOGS
38 # include <telepathy-logger/call-event.h>
39 #endif
40
41 #include <extensions/extensions.h>
42
43 #include <libempathy/action-chain-internal.h>
44 #include <libempathy/empathy-chatroom-manager.h>
45 #include <libempathy/empathy-chatroom.h>
46 #include <libempathy/empathy-message.h>
47 #include <libempathy/empathy-request-util.h>
48 #include <libempathy/empathy-utils.h>
49 #include <libempathy/empathy-time.h>
50
51 #include "empathy-log-window.h"
52 #include "empathy-account-chooser.h"
53 #include "empathy-call-utils.h"
54 #include "empathy-chat-view.h"
55 #include "empathy-contact-dialogs.h"
56 #include "empathy-images.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-ui-utils.h"
59
60 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
61 #include <libempathy/empathy-debug.h>
62
63 typedef struct
64 {
65   GtkWidget *window;
66
67   GtkWidget *button_profile;
68   GtkWidget *button_chat;
69   GtkWidget *button_call;
70   GtkWidget *button_video;
71
72   GtkWidget *search_entry;
73
74   GtkWidget *notebook;
75   GtkWidget *spinner;
76
77   GtkWidget *treeview_who;
78   GtkWidget *treeview_what;
79   GtkWidget *treeview_when;
80   GtkWidget *treeview_events;
81
82   GtkTreeStore *store_events;
83
84   GtkWidget *account_chooser;
85
86   gchar *last_find;
87
88   TplActionChain *chain;
89   TplLogManager *log_manager;
90
91   /* Used to cancel logger calls when no longer needed */
92   guint count;
93
94   /* List of owned TplLogSearchHits, free with tpl_log_search_hit_free */
95   GList *hits;
96   guint source;
97
98   /* Only used while waiting for the account chooser to be ready */
99   TpAccount *selected_account;
100   gchar *selected_chat_id;
101   gboolean selected_is_chatroom;
102 } EmpathyLogWindow;
103
104 static void log_window_destroy_cb                (GtkWidget        *widget,
105                                                   EmpathyLogWindow *window);
106 static void log_window_search_entry_changed_cb   (GtkWidget        *entry,
107                                                   EmpathyLogWindow *window);
108 static void log_window_search_entry_activate_cb  (GtkWidget        *widget,
109                                                   EmpathyLogWindow *window);
110 static void log_window_search_entry_icon_pressed_cb (GtkEntry      *entry,
111                                                   GtkEntryIconPosition icon_pos,
112                                                   GdkEvent *event,
113                                                   gpointer user_data);
114 static void log_window_who_populate              (EmpathyLogWindow *window);
115 static void log_window_who_setup                 (EmpathyLogWindow *window);
116 static void log_window_when_setup                (EmpathyLogWindow *window);
117 static void log_window_what_setup                (EmpathyLogWindow *window);
118 static void log_window_events_setup              (EmpathyLogWindow *window);
119 static void log_window_chats_accounts_changed_cb (GtkWidget        *combobox,
120                                                   EmpathyLogWindow *window);
121 static void log_window_chats_set_selected        (EmpathyLogWindow *window);
122 static void log_window_chats_get_messages        (EmpathyLogWindow *window,
123                                                   gboolean force_get_dates);
124 static void log_window_when_changed_cb           (GtkTreeSelection *selection,
125                                                   EmpathyLogWindow *window);
126 static void log_window_delete_menu_clicked_cb    (GtkMenuItem      *menuitem,
127                                                   EmpathyLogWindow *window);
128
129 static void
130 empathy_account_chooser_filter_has_logs (TpAccount *account,
131     EmpathyAccountChooserFilterResultCallback callback,
132     gpointer callback_data,
133     gpointer user_data);
134
135 enum
136 {
137   PAGE_EVENTS,
138   PAGE_SPINNER,
139   PAGE_EMPTY
140 };
141
142 enum
143 {
144   COL_TYPE_ANY,
145   COL_TYPE_SEPARATOR,
146   COL_TYPE_NORMAL
147 };
148
149 enum
150 {
151   COL_WHO_TYPE,
152   COL_WHO_ICON,
153   COL_WHO_NAME,
154   COL_WHO_ACCOUNT,
155   COL_WHO_TARGET,
156   COL_WHO_COUNT
157 };
158
159 enum
160 {
161   COL_WHAT_TYPE,
162   COL_WHAT_SUBTYPE,
163   COL_WHAT_TEXT,
164   COL_WHAT_ICON,
165   COL_WHAT_COUNT
166 };
167
168 enum
169 {
170   COL_WHEN_DATE,
171   COL_WHEN_TEXT,
172   COL_WHEN_ICON,
173   COL_WHEN_COUNT
174 };
175
176 enum
177 {
178   COL_EVENTS_TYPE,
179   COL_EVENTS_TS,
180   COL_EVENTS_PRETTY_DATE,
181   COL_EVENTS_ICON,
182   COL_EVENTS_TEXT,
183   COL_EVENTS_ACCOUNT,
184   COL_EVENTS_TARGET,
185   COL_EVENTS_EVENT,
186   COL_EVENTS_COUNT
187 };
188
189 #define CALENDAR_ICON "stock_calendar"
190
191 /* Seconds between two messages to be considered one conversation */
192 #define MAX_GAP 30*60
193
194 #define WHAT_TYPE_SEPARATOR -1
195
196 typedef enum
197 {
198   EVENT_CALL_INCOMING = 1 << 0,
199   EVENT_CALL_OUTGOING = 1 << 1,
200   EVENT_CALL_MISSED   = 1 << 2,
201   EVENT_CALL_ALL      = 1 << 3,
202 } EventSubtype;
203
204 static EmpathyLogWindow *log_window = NULL;
205
206 static gboolean has_element;
207
208 #ifndef _date_copy
209 #define _date_copy(d) g_date_new_julian (g_date_get_julian (d))
210 #endif
211
212 typedef struct
213 {
214   EmpathyLogWindow *window;
215   TpAccount *account;
216   TplEntity *entity;
217   GDate *date;
218   TplEventTypeMask event_mask;
219   EventSubtype subtype;
220   guint count;
221 } Ctx;
222
223 static Ctx *
224 ctx_new (EmpathyLogWindow *window,
225     TpAccount *account,
226     TplEntity *entity,
227     GDate *date,
228     TplEventTypeMask event_mask,
229     EventSubtype subtype,
230     guint count)
231 {
232   Ctx *ctx = g_slice_new0 (Ctx);
233
234   ctx->window = window;
235   if (account != NULL)
236     ctx->account = g_object_ref (account);
237   if (entity != NULL)
238     ctx->entity = g_object_ref (entity);
239   if (date != NULL)
240     ctx->date = _date_copy (date);
241   ctx->event_mask = event_mask;
242   ctx->subtype = subtype;
243   ctx->count = count;
244
245   return ctx;
246 }
247
248 static void
249 ctx_free (Ctx *ctx)
250 {
251   tp_clear_object (&ctx->account);
252   tp_clear_object (&ctx->entity);
253   tp_clear_pointer (&ctx->date, g_date_free);
254
255   g_slice_free (Ctx, ctx);
256 }
257
258 static void
259 account_chooser_ready_cb (EmpathyAccountChooser *chooser,
260     EmpathyLogWindow *window)
261 {
262   /* We'll display the account once the model has been populate with the chats
263    * of this account. */
264   empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (
265       window->account_chooser), window->selected_account);
266 }
267
268 static void
269 select_account_once_ready (EmpathyLogWindow *self,
270     TpAccount *account,
271     const gchar *chat_id,
272     gboolean is_chatroom)
273 {
274   EmpathyAccountChooser *account_chooser;
275
276   account_chooser = EMPATHY_ACCOUNT_CHOOSER (self->account_chooser);
277
278   tp_clear_object (&self->selected_account);
279   self->selected_account = g_object_ref (account);
280
281   g_free (self->selected_chat_id);
282   self->selected_chat_id = g_strdup (chat_id);
283
284   self->selected_is_chatroom = is_chatroom;
285
286   if (empathy_account_chooser_is_ready (account_chooser))
287     account_chooser_ready_cb (account_chooser, self);
288   else
289     /* Chat will be selected once the account chooser is ready */
290     g_signal_connect (account_chooser, "ready",
291         G_CALLBACK (account_chooser_ready_cb), self);
292 }
293
294 static void
295 toolbutton_profile_clicked (GtkToolButton *toolbutton,
296     EmpathyLogWindow *window)
297 {
298   GtkTreeView *view;
299   GtkTreeSelection *selection;
300   GtkTreeModel *model;
301   GtkTreeIter iter;
302   GtkTreePath *path;
303   GList *paths;
304   TpAccount *account;
305   TplEntity *target;
306   EmpathyContact *contact;
307   gint type;
308
309   g_return_if_fail (window != NULL);
310
311   view = GTK_TREE_VIEW (log_window->treeview_who);
312   selection = gtk_tree_view_get_selection (view);
313
314   paths = gtk_tree_selection_get_selected_rows (selection, &model);
315   g_return_if_fail (paths != NULL);
316
317   path = paths->data;
318   gtk_tree_model_get_iter (model, &iter, path);
319   gtk_tree_model_get (model, &iter,
320       COL_WHO_ACCOUNT, &account,
321       COL_WHO_TARGET, &target,
322       COL_WHO_TYPE, &type,
323       -1);
324
325   g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
326
327   g_return_if_fail (type == COL_TYPE_NORMAL);
328
329   contact = empathy_contact_from_tpl_contact (account, target);
330   empathy_contact_information_dialog_show (contact,
331       GTK_WINDOW (window->window));
332   g_object_unref (contact);
333
334   g_object_unref (account);
335   g_object_unref (target);
336 }
337
338 static void
339 toolbutton_chat_clicked (GtkToolButton *toolbutton,
340     EmpathyLogWindow *window)
341 {
342   GtkTreeView *view;
343   GtkTreeSelection *selection;
344   GtkTreeModel *model;
345   GtkTreeIter iter;
346   GtkTreePath *path;
347   GList *paths;
348   TpAccount *account;
349   TplEntity *target;
350   EmpathyContact *contact;
351   gint type;
352
353   g_return_if_fail (window != NULL);
354
355   view = GTK_TREE_VIEW (log_window->treeview_who);
356   selection = gtk_tree_view_get_selection (view);
357
358   paths = gtk_tree_selection_get_selected_rows (selection, &model);
359   g_return_if_fail (paths != NULL);
360
361   path = paths->data;
362   gtk_tree_model_get_iter (model, &iter, path);
363   gtk_tree_model_get (model, &iter,
364       COL_WHO_ACCOUNT, &account,
365       COL_WHO_TARGET, &target,
366       COL_WHO_TYPE, &type,
367       -1);
368
369   g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
370
371   g_return_if_fail (type == COL_TYPE_NORMAL);
372
373   contact = empathy_contact_from_tpl_contact (account, target);
374   empathy_chat_with_contact (contact,
375       gtk_get_current_event_time ());
376
377   g_object_unref (contact);
378   g_object_unref (account);
379   g_object_unref (target);
380 }
381
382 static void
383 toolbutton_av_clicked (GtkToolButton *toolbutton,
384     EmpathyLogWindow *window)
385 {
386   GtkTreeView *view;
387   GtkTreeSelection *selection;
388   GtkTreeModel *model;
389   GtkTreeIter iter;
390   GtkTreePath *path;
391   GList *paths;
392   TpAccount *account;
393   gchar *contact;
394   gint type;
395   gboolean video;
396
397   g_return_if_fail (window != NULL);
398
399   view = GTK_TREE_VIEW (log_window->treeview_who);
400   selection = gtk_tree_view_get_selection (view);
401
402   paths = gtk_tree_selection_get_selected_rows (selection, &model);
403   g_return_if_fail (paths != NULL);
404
405   path = paths->data;
406   gtk_tree_model_get_iter (model, &iter, path);
407   gtk_tree_model_get (model, &iter,
408       COL_WHO_ACCOUNT, &account,
409       COL_WHO_NAME, &contact,
410       COL_WHO_TYPE, &type,
411       -1);
412
413   g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
414
415   g_return_if_fail (type == COL_TYPE_NORMAL);
416
417   video = (GTK_WIDGET (toolbutton) == window->button_video);
418
419   empathy_call_new_with_streams (contact, account,
420       TRUE, video, gtk_get_current_event_time ());
421
422   g_free (contact);
423   g_object_unref (account);
424 }
425
426 GtkWidget *
427 empathy_log_window_show (TpAccount  *account,
428     const gchar *chat_id,
429     gboolean     is_chatroom,
430     GtkWindow   *parent)
431 {
432   EmpathyAccountChooser   *account_chooser;
433   GtkBuilder             *gui;
434   gchar                  *filename;
435   EmpathyLogWindow       *window;
436   GtkWidget *vbox, *accounts, *search, *label, *quit;
437
438   if (log_window != NULL)
439     {
440       gtk_window_present (GTK_WINDOW (log_window->window));
441
442       if (account != NULL && chat_id != NULL)
443         select_account_once_ready (log_window, account, chat_id, is_chatroom);
444
445       return log_window->window;
446     }
447
448   log_window = g_new0 (EmpathyLogWindow, 1);
449   log_window->chain = _tpl_action_chain_new_async (NULL, NULL, NULL);
450
451   log_window->log_manager = tpl_log_manager_dup_singleton ();
452
453   window = log_window;
454
455   filename = empathy_file_lookup ("empathy-log-window.ui", "libempathy-gtk");
456   gui = empathy_builder_get_file (filename,
457       "log_window", &window->window,
458       "toolbutton_profile", &window->button_profile,
459       "toolbutton_chat", &window->button_chat,
460       "toolbutton_call", &window->button_call,
461       "toolbutton_video", &window->button_video,
462       "toolbutton_accounts", &accounts,
463       "toolbutton_search", &search,
464       "imagemenuitem_quit", &quit,
465       "treeview_who", &window->treeview_who,
466       "treeview_what", &window->treeview_what,
467       "treeview_when", &window->treeview_when,
468       "treeview_events", &window->treeview_events,
469       "notebook", &window->notebook,
470       "spinner", &window->spinner,
471       NULL);
472   g_free (filename);
473
474   empathy_builder_connect (gui, window,
475       "log_window", "destroy", log_window_destroy_cb,
476       "toolbutton_profile", "clicked", toolbutton_profile_clicked,
477       "toolbutton_chat", "clicked", toolbutton_chat_clicked,
478       "toolbutton_call", "clicked", toolbutton_av_clicked,
479       "toolbutton_video", "clicked", toolbutton_av_clicked,
480       "imagemenuitem_delete", "activate", log_window_delete_menu_clicked_cb,
481       NULL);
482
483   g_object_unref (gui);
484
485   g_object_add_weak_pointer (G_OBJECT (window->window),
486       (gpointer) &log_window);
487
488   g_signal_connect_swapped (quit, "activate",
489       G_CALLBACK (gtk_widget_destroy), window->window);
490
491   /* Account chooser for chats */
492   vbox = gtk_vbox_new (FALSE, 3);
493
494   window->account_chooser = empathy_account_chooser_new ();
495   account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
496   empathy_account_chooser_set_has_all_option (account_chooser, TRUE);
497   empathy_account_chooser_set_filter (account_chooser,
498       empathy_account_chooser_filter_has_logs, NULL);
499   empathy_account_chooser_set_all (account_chooser);
500
501   g_signal_connect (window->account_chooser, "changed",
502       G_CALLBACK (log_window_chats_accounts_changed_cb),
503       window);
504
505   label = gtk_label_new (_("Show"));
506
507   gtk_box_pack_start (GTK_BOX (vbox),
508       window->account_chooser,
509       FALSE, FALSE, 0);
510
511   gtk_box_pack_start (GTK_BOX (vbox),
512       label,
513       FALSE, FALSE, 0);
514
515   gtk_widget_show_all (vbox);
516   gtk_container_add (GTK_CONTAINER (accounts), vbox);
517
518   /* Search entry */
519   vbox = gtk_vbox_new (FALSE, 3);
520
521   window->search_entry = gtk_entry_new ();
522   gtk_entry_set_icon_from_stock (GTK_ENTRY (window->search_entry),
523       GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
524   gtk_entry_set_icon_from_stock (GTK_ENTRY (window->search_entry),
525       GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
526
527   label = gtk_label_new (_("Search"));
528
529   gtk_box_pack_start (GTK_BOX (vbox),
530       window->search_entry,
531       FALSE, FALSE, 0);
532
533   gtk_box_pack_start (GTK_BOX (vbox),
534       label,
535       FALSE, FALSE, 0);
536
537   gtk_widget_show_all (vbox);
538   gtk_container_add (GTK_CONTAINER (search), vbox);
539
540   g_signal_connect (window->search_entry, "changed",
541       G_CALLBACK (log_window_search_entry_changed_cb),
542       window);
543
544   g_signal_connect (window->search_entry, "activate",
545       G_CALLBACK (log_window_search_entry_activate_cb),
546       window);
547
548   g_signal_connect (window->search_entry, "icon-press",
549       G_CALLBACK (log_window_search_entry_icon_pressed_cb),
550       window);
551
552   /* Contacts */
553   log_window_events_setup (window);
554   log_window_who_setup (window);
555   log_window_what_setup (window);
556   log_window_when_setup (window);
557
558   log_window_who_populate (window);
559
560   if (account != NULL && chat_id != NULL)
561     select_account_once_ready (window, account, chat_id, is_chatroom);
562
563   if (parent != NULL)
564     gtk_window_set_transient_for (GTK_WINDOW (window->window),
565         GTK_WINDOW (parent));
566
567   gtk_widget_show (window->window);
568
569   return window->window;
570 }
571
572 static void
573 log_window_destroy_cb (GtkWidget *widget,
574     EmpathyLogWindow *window)
575 {
576   if (window->source != 0)
577     g_source_remove (window->source);
578
579   g_free (window->last_find);
580   _tpl_action_chain_free (window->chain);
581   g_object_unref (window->log_manager);
582   tp_clear_object (&window->selected_account);
583   g_free (window->selected_chat_id);
584
585   g_free (window);
586 }
587
588 static gboolean
589 account_equal (TpAccount *a,
590     TpAccount *b)
591 {
592   return g_str_equal (tp_proxy_get_object_path (a),
593       tp_proxy_get_object_path (b));
594 }
595
596 static gboolean
597 entity_equal (TplEntity *a,
598     TplEntity *b)
599 {
600   return g_str_equal (tpl_entity_get_identifier (a),
601       tpl_entity_get_identifier (b));
602 }
603
604 static gboolean
605 is_same_confroom (TplEvent *e1,
606     TplEvent *e2)
607 {
608   TplEntity *sender1 = tpl_event_get_sender (e1);
609   TplEntity *receiver1 = tpl_event_get_receiver (e1);
610   TplEntity *sender2 = tpl_event_get_sender (e2);
611   TplEntity *receiver2 = tpl_event_get_receiver (e2);
612   TplEntity *room1, *room2;
613
614   if (receiver1 == NULL || receiver2 == NULL)
615     return FALSE;
616
617   if (tpl_entity_get_entity_type (sender1) == TPL_ENTITY_ROOM)
618     room1 = sender1;
619   else if (tpl_entity_get_entity_type (receiver1) == TPL_ENTITY_ROOM)
620     room1 = receiver1;
621   else
622     return FALSE;
623
624   if (tpl_entity_get_entity_type (sender2) == TPL_ENTITY_ROOM)
625     room2 = sender2;
626   else if (tpl_entity_get_entity_type (receiver2) == TPL_ENTITY_ROOM)
627     room2 = receiver2;
628   else
629     return FALSE;
630
631   return g_str_equal (tpl_entity_get_identifier (room1),
632       tpl_entity_get_identifier (room2));
633 }
634
635 static TplEntity *
636 event_get_target (TplEvent *event)
637 {
638   TplEntity *sender = tpl_event_get_sender (event);
639   TplEntity *receiver = tpl_event_get_receiver (event);
640
641   if (tpl_entity_get_entity_type (sender) == TPL_ENTITY_SELF)
642     return receiver;
643
644   return sender;
645 }
646
647 static gboolean
648 model_is_parent (GtkTreeModel *model,
649     GtkTreeIter *iter,
650     TplEvent *event)
651 {
652   TplEvent *stored_event;
653   TplEntity *target;
654   TpAccount *account;
655   gboolean found = FALSE;
656   GtkTreeIter parent;
657
658   if (gtk_tree_model_iter_parent (model, &parent, iter))
659     return FALSE;
660
661   gtk_tree_model_get (model, iter,
662       COL_EVENTS_ACCOUNT, &account,
663       COL_EVENTS_TARGET, &target,
664       COL_EVENTS_EVENT, &stored_event,
665       -1);
666
667   if (G_OBJECT_TYPE (event) == G_OBJECT_TYPE (stored_event) &&
668       account_equal (account, tpl_event_get_account (event)) &&
669       (entity_equal (target, event_get_target (event)) ||
670       is_same_confroom (event, stored_event)))
671     {
672       GtkTreeIter child;
673       gint64 timestamp;
674
675       gtk_tree_model_iter_nth_child (model, &child, iter,
676           gtk_tree_model_iter_n_children (model, iter) - 1);
677
678       gtk_tree_model_get (model, &child,
679           COL_EVENTS_TS, &timestamp,
680           -1);
681
682       if (ABS (tpl_event_get_timestamp (event) - timestamp) < MAX_GAP)
683         {
684           /* The gap is smaller than 30 min */
685           found = TRUE;
686         }
687     }
688
689   g_object_unref (stored_event);
690   g_object_unref (account);
691   g_object_unref (target);
692
693   return found;
694 }
695
696 static const gchar *
697 get_contact_alias_for_message (EmpathyMessage *message)
698 {
699   EmpathyContact *sender, *receiver;
700
701   sender = empathy_message_get_sender (message);
702   receiver = empathy_message_get_receiver (message);
703
704   if (empathy_contact_is_user (sender))
705     return empathy_contact_get_alias (receiver);
706
707   return empathy_contact_get_alias (sender);
708 }
709
710 static void
711 get_parent_iter_for_message (TplEvent *event,
712     EmpathyMessage *message,
713     GtkTreeIter *parent)
714 {
715   GtkTreeStore *store;
716   GtkTreeModel *model;
717   GtkTreeIter iter;
718   gboolean parent_found = FALSE;
719   gboolean next;
720
721   store = log_window->store_events;
722   model = GTK_TREE_MODEL (store);
723
724   for (next = gtk_tree_model_get_iter_first (model, &iter);
725        next;
726        next = gtk_tree_model_iter_next (model, &iter))
727     {
728       if ((parent_found = model_is_parent (model, &iter, event)))
729         break;
730     }
731
732   if (parent_found)
733     {
734       *parent = iter;
735     }
736   else
737     {
738       GDateTime *date;
739       gchar *body, *pretty_date;
740
741       date = g_date_time_new_from_unix_utc (
742           tpl_event_get_timestamp (event));
743
744       pretty_date = g_date_time_format (date,
745           C_("A date with the time", "%A, %e %B %Y %X"));
746
747       body = g_markup_printf_escaped (_("Chat with %s"),
748           get_contact_alias_for_message (message));
749
750       gtk_tree_store_append (store, &iter, NULL);
751       gtk_tree_store_set (store, &iter,
752           COL_EVENTS_TS, tpl_event_get_timestamp (event),
753           COL_EVENTS_PRETTY_DATE, pretty_date,
754           COL_EVENTS_TEXT, body,
755           COL_EVENTS_ICON, "stock_text_justify",
756           COL_EVENTS_ACCOUNT, tpl_event_get_account (event),
757           COL_EVENTS_TARGET, event_get_target (event),
758           COL_EVENTS_EVENT, event,
759           -1);
760
761       *parent = iter;
762
763       g_free (body);
764       g_free (pretty_date);
765       g_date_time_unref (date);
766     }
767 }
768
769 static const gchar *
770 get_icon_for_event (TplEvent *event)
771 {
772   const gchar *icon = NULL;
773
774   if (TPL_IS_TEXT_EVENT (event))
775     {
776       TplTextEvent *text = TPL_TEXT_EVENT (event);
777
778       if (!tp_str_empty (tpl_text_event_get_supersedes_token (text)))
779         icon = EMPATHY_IMAGE_EDIT_MESSAGE;
780     }
781 #ifdef HAVE_CALL_LOGS
782   else if (TPL_IS_CALL_EVENT (event))
783     {
784       TplCallEvent *call = TPL_CALL_EVENT (event);
785       TplCallEndReason reason = tpl_call_event_get_end_reason (call);
786       TplEntity *sender = tpl_event_get_sender (event);
787       TplEntity *receiver = tpl_event_get_receiver (event);
788
789       if (reason == TPL_CALL_END_REASON_NO_ANSWER)
790         icon = EMPATHY_IMAGE_CALL_MISSED;
791       else if (tpl_entity_get_entity_type (sender) == TPL_ENTITY_SELF)
792         icon = EMPATHY_IMAGE_CALL_OUTGOING;
793       else if (tpl_entity_get_entity_type (receiver) == TPL_ENTITY_SELF)
794         icon = EMPATHY_IMAGE_CALL_INCOMING;
795     }
796 #endif
797
798   return icon;
799 }
800
801 static void
802 log_window_append_chat_message (TplEvent *event,
803     EmpathyMessage *message)
804 {
805   GtkTreeStore *store = log_window->store_events;
806   GtkTreeIter iter, parent;
807   gchar *pretty_date, *body;
808   GDateTime *date;
809
810   date = g_date_time_new_from_unix_utc (
811       tpl_event_get_timestamp (event));
812
813   pretty_date = g_date_time_format (date, "%X");
814
815   get_parent_iter_for_message (event, message, &parent);
816
817   if (tpl_text_event_get_message_type (TPL_TEXT_EVENT (event))
818       == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)
819     {
820       /* Translators: this is an emote: '* Danielle waves' */
821       body = g_markup_printf_escaped (_("<i>* %s %s</i>"),
822           tpl_entity_get_alias (tpl_event_get_sender (event)),
823           empathy_message_get_body (message));
824     }
825   else
826     {
827       /* Translators: this is a message: 'Danielle: hello'
828        * The string in bold is the sender's name */
829       body = g_markup_printf_escaped (_("<b>%s:</b> %s"),
830           tpl_entity_get_alias (tpl_event_get_sender (event)),
831           empathy_message_get_body (message));
832     }
833
834   gtk_tree_store_append (store, &iter, &parent);
835   gtk_tree_store_set (store, &iter,
836       COL_EVENTS_TS, tpl_event_get_timestamp (event),
837       COL_EVENTS_PRETTY_DATE, pretty_date,
838       COL_EVENTS_TEXT, body,
839       COL_EVENTS_ICON, get_icon_for_event (event),
840       COL_EVENTS_ACCOUNT, tpl_event_get_account (event),
841       COL_EVENTS_TARGET, event_get_target (event),
842       COL_EVENTS_EVENT, event,
843       -1);
844
845   g_free (body);
846   g_free (pretty_date);
847   g_date_time_unref (date);
848 }
849
850 #ifdef HAVE_CALL_LOGS
851 static void
852 log_window_append_call (TplEvent *event,
853     EmpathyMessage *message)
854 {
855   TplCallEvent *call = TPL_CALL_EVENT (event);
856   GtkTreeStore *store = log_window->store_events;
857   GtkTreeIter iter, child;
858   gchar *pretty_date, *duration, *finished;
859   GDateTime *started_date, *finished_date;
860   GTimeSpan span;
861
862   started_date = g_date_time_new_from_unix_utc (
863       tpl_event_get_timestamp (event));
864
865   pretty_date = g_date_time_format (started_date,
866       C_("A date with the time", "%A, %e %B %Y %X"));
867
868   gtk_tree_store_append (store, &iter, NULL);
869   gtk_tree_store_set (store, &iter,
870       COL_EVENTS_TS, tpl_event_get_timestamp (event),
871       COL_EVENTS_PRETTY_DATE, pretty_date,
872       COL_EVENTS_TEXT, empathy_message_get_body (message),
873       COL_EVENTS_ICON, get_icon_for_event (event),
874       COL_EVENTS_ACCOUNT, tpl_event_get_account (event),
875       COL_EVENTS_TARGET, event_get_target (event),
876       COL_EVENTS_EVENT, event,
877       -1);
878
879   if (tpl_call_event_get_end_reason (call) != TPL_CALL_END_REASON_NO_ANSWER)
880     {
881       gchar *body;
882
883       span = tpl_call_event_get_duration (TPL_CALL_EVENT (event));
884       if (span < 60)
885         duration = g_strdup_printf (_("%" G_GINT64_FORMAT " seconds"), span);
886       else
887         duration = g_strdup_printf (_("%" G_GINT64_FORMAT " minutes"),
888             span / 60);
889
890       finished_date = g_date_time_add (started_date, -span);
891       finished = g_date_time_format (finished_date, "%X");
892       g_date_time_unref (finished_date);
893
894       body = g_strdup_printf (_("Call took %s, ended at %s"),
895           duration, finished);
896
897       g_free (duration);
898       g_free (finished);
899
900       gtk_tree_store_append (store, &child, &iter);
901       gtk_tree_store_set (store, &child,
902           COL_EVENTS_TS, tpl_event_get_timestamp (event),
903           COL_EVENTS_TEXT, body,
904           COL_EVENTS_ACCOUNT, tpl_event_get_account (event),
905           COL_EVENTS_TARGET, event_get_target (event),
906           COL_EVENTS_EVENT, event,
907           -1);
908
909       g_free (body);
910     }
911
912   g_free (pretty_date);
913   g_date_time_unref (started_date);
914 }
915 #endif
916
917 static void
918 log_window_append_message (TplEvent *event,
919     EmpathyMessage *message)
920 {
921   if (TPL_IS_TEXT_EVENT (event))
922     log_window_append_chat_message (event, message);
923 #ifdef HAVE_CALL_LOGS
924   else if (TPL_IS_CALL_EVENT (event))
925     log_window_append_call (event, message);
926 #endif
927   else
928     DEBUG ("Message type not handled");
929 }
930
931 static void
932 add_all_accounts_and_entities (GList **accounts,
933     GList **entities)
934 {
935   GtkTreeView      *view;
936   GtkTreeModel     *model;
937   GtkTreeIter       iter;
938
939   view = GTK_TREE_VIEW (log_window->treeview_who);
940   model = gtk_tree_view_get_model (view);
941
942   if (!gtk_tree_model_get_iter_first (model, &iter))
943     return;
944
945   do
946     {
947       TpAccount *account;
948       TplEntity *entity;
949       gint type;
950
951       gtk_tree_model_get (model, &iter,
952           COL_WHO_ACCOUNT, &account,
953           COL_WHO_TARGET, &entity,
954           COL_WHO_TYPE, &type,
955           -1);
956
957       if (type != COL_TYPE_NORMAL)
958         continue;
959
960       if (accounts != NULL)
961         *accounts = g_list_append (*accounts, account);
962
963       if (entities != NULL)
964         *entities = g_list_append (*entities, entity);
965     }
966   while (gtk_tree_model_iter_next (model, &iter));
967 }
968
969 static gboolean
970 log_window_get_selected (EmpathyLogWindow *window,
971     GList **accounts,
972     GList **entities,
973     GList **dates,
974     TplEventTypeMask *event_mask,
975     EventSubtype *subtype)
976 {
977   GtkTreeView      *view;
978   GtkTreeModel     *model;
979   GtkTreeSelection *selection;
980   GtkTreeIter       iter;
981   TplEventTypeMask  ev = 0;
982   EventSubtype      st = 0;
983   GList            *paths, *l;
984   gint              type;
985
986   view = GTK_TREE_VIEW (window->treeview_who);
987   model = gtk_tree_view_get_model (view);
988   selection = gtk_tree_view_get_selection (view);
989
990   paths = gtk_tree_selection_get_selected_rows (selection, NULL);
991   if (paths == NULL)
992     return FALSE;
993
994   if (accounts != NULL)
995     *accounts = NULL;
996   if (entities != NULL)
997     *entities = NULL;
998
999   for (l = paths; l != NULL; l = l->next)
1000     {
1001       GtkTreePath *path = l->data;
1002       TpAccount *account;
1003       TplEntity *entity;
1004
1005       gtk_tree_model_get_iter (model, &iter, path);
1006       gtk_tree_model_get (model, &iter,
1007           COL_WHO_ACCOUNT, &account,
1008           COL_WHO_TARGET, &entity,
1009           COL_WHO_TYPE, &type,
1010           -1);
1011
1012       if (type == COL_TYPE_ANY)
1013         {
1014           if (accounts != NULL || entities != NULL)
1015             add_all_accounts_and_entities (accounts, entities);
1016           break;
1017         }
1018
1019       if (accounts != NULL)
1020         *accounts = g_list_append (*accounts, g_object_ref (account));
1021
1022       if (entities != NULL)
1023         *entities = g_list_append (*entities, g_object_ref (entity));
1024
1025       g_object_unref (account);
1026       g_object_unref (entity);
1027     }
1028   g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
1029
1030   view = GTK_TREE_VIEW (window->treeview_what);
1031   model = gtk_tree_view_get_model (view);
1032   selection = gtk_tree_view_get_selection (view);
1033
1034   paths = gtk_tree_selection_get_selected_rows (selection, NULL);
1035   for (l = paths; l != NULL; l = l->next)
1036     {
1037       GtkTreePath *path = l->data;
1038       TplEventTypeMask mask;
1039       EventSubtype submask;
1040
1041       gtk_tree_model_get_iter (model, &iter, path);
1042       gtk_tree_model_get (model, &iter,
1043           COL_WHAT_TYPE, &mask,
1044           COL_WHAT_SUBTYPE, &submask,
1045           -1);
1046
1047       ev |= mask;
1048       st |= submask;
1049     }
1050   g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
1051
1052   view = GTK_TREE_VIEW (window->treeview_when);
1053   model = gtk_tree_view_get_model (view);
1054   selection = gtk_tree_view_get_selection (view);
1055
1056   if (dates != NULL)
1057     {
1058       *dates = NULL;
1059
1060       paths = gtk_tree_selection_get_selected_rows (selection, NULL);
1061       for (l = paths; l != NULL; l = l->next)
1062         {
1063           GtkTreePath *path = l->data;
1064           GDate *date;
1065
1066           gtk_tree_model_get_iter (model, &iter, path);
1067           gtk_tree_model_get (model, &iter,
1068               COL_WHEN_DATE, &date,
1069               -1);
1070
1071           *dates = g_list_append (*dates, date);
1072         }
1073       g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
1074     }
1075
1076   if (event_mask != NULL)
1077     *event_mask = ev;
1078
1079   if (subtype != NULL)
1080     *subtype = st;
1081
1082   return TRUE;
1083 }
1084
1085 static gboolean
1086 model_has_entity (GtkTreeModel *model,
1087     GtkTreePath *path,
1088     GtkTreeIter *iter,
1089     gpointer data)
1090 {
1091   TplLogSearchHit *hit = data;
1092   TplEntity *e;
1093   TpAccount *a;
1094   gboolean ret = FALSE;
1095
1096   gtk_tree_model_get (model, iter,
1097       COL_WHO_TARGET, &e,
1098       COL_WHO_ACCOUNT, &a,
1099       -1);
1100
1101   if (e != NULL && entity_equal (hit->target, e) &&
1102       a != NULL && account_equal (hit->account, a))
1103     {
1104       ret = has_element = TRUE;
1105     }
1106
1107   tp_clear_object (&e);
1108   tp_clear_object (&a);
1109
1110   return ret;
1111 }
1112
1113 static gboolean
1114 model_has_date (GtkTreeModel *model,
1115     GtkTreePath *path,
1116     GtkTreeIter *iter,
1117     gpointer data)
1118 {
1119   GDate *date = data;
1120   GDate *d;
1121
1122   gtk_tree_model_get (model, iter,
1123       COL_WHEN_DATE, &d,
1124       -1);
1125
1126   if (!g_date_compare (date, d))
1127     {
1128       has_element = TRUE;
1129       return TRUE;
1130     }
1131
1132   return FALSE;
1133 }
1134
1135 static void
1136 get_events_for_date (TplActionChain *chain, gpointer user_data);
1137
1138 static void
1139 populate_events_from_search_hits (GList *accounts,
1140     GList *targets,
1141     GList *dates)
1142 {
1143   TplEventTypeMask event_mask;
1144   EventSubtype subtype;
1145   GDate *anytime;
1146   GList *l;
1147   gboolean is_anytime = FALSE;
1148
1149   if (!log_window_get_selected (log_window,
1150       NULL, NULL, NULL, &event_mask, &subtype))
1151     return;
1152
1153   anytime = g_date_new_dmy (2, 1, -1);
1154   if (g_list_find_custom (dates, anytime, (GCompareFunc) g_date_compare))
1155     is_anytime = TRUE;
1156
1157   for (l = log_window->hits; l != NULL; l = l->next)
1158     {
1159       TplLogSearchHit *hit = l->data;
1160       GList *acc, *targ;
1161       gboolean found = FALSE;
1162
1163       /* Protect against invalid data (corrupt or old log files). */
1164       if (hit->account == NULL || hit->target == NULL)
1165         continue;
1166
1167       for (acc = accounts, targ = targets;
1168            acc != NULL && targ != NULL && !found;
1169            acc = acc->next, targ = targ->next)
1170         {
1171           TpAccount *account = acc->data;
1172           TplEntity *target = targ->data;
1173
1174           if (account_equal (hit->account, account) &&
1175               entity_equal (hit->target, target))
1176             found = TRUE;
1177         }
1178
1179         if (!found)
1180           continue;
1181
1182       if (is_anytime ||
1183           g_list_find_custom (dates, hit->date, (GCompareFunc) g_date_compare)
1184               != NULL)
1185         {
1186           Ctx *ctx;
1187
1188           ctx = ctx_new (log_window, hit->account, hit->target, hit->date,
1189               event_mask, subtype, log_window->count);
1190           _tpl_action_chain_append (log_window->chain,
1191               get_events_for_date, ctx);
1192         }
1193     }
1194
1195   _tpl_action_chain_start (log_window->chain);
1196
1197   g_date_free (anytime);
1198 }
1199
1200 static gchar *
1201 format_date_for_display (GDate *date)
1202 {
1203   gchar *text;
1204   GDate *now = NULL;
1205   gint days_elapsed;
1206
1207   /* g_date_strftime sucks */
1208
1209   now = g_date_new ();
1210   g_date_set_time_t (now, time (NULL));
1211
1212   days_elapsed = g_date_days_between (date, now);
1213
1214   if (days_elapsed < 0)
1215     {
1216       text = NULL;
1217     }
1218   else if (days_elapsed == 0)
1219     {
1220       text = g_strdup (_("Today"));
1221     }
1222   else if (days_elapsed == 1)
1223     {
1224       text = g_strdup (_("Yesterday"));
1225     }
1226   else
1227     {
1228       GDateTime *dt;
1229
1230       dt = g_date_time_new_utc (g_date_get_year (date),
1231           g_date_get_month (date), g_date_get_day (date),
1232           0, 0, 0);
1233
1234       if (days_elapsed <= 7)
1235         text = g_date_time_format (dt, "%A");
1236       else
1237         text = g_date_time_format (dt,
1238             C_("A date such as '23 May 2010', "
1239                "%e is the day, %B the month and %Y the year",
1240                "%e %B %Y"));
1241
1242       g_date_time_unref (dt);
1243     }
1244
1245   g_date_free (now);
1246
1247   return text;
1248 }
1249
1250 static void
1251 populate_dates_from_search_hits (GList *accounts,
1252     GList *targets)
1253 {
1254   GList *l;
1255   GtkTreeView *view;
1256   GtkTreeModel *model;
1257   GtkListStore *store;
1258   GtkTreeSelection *selection;
1259   GtkTreeIter iter;
1260
1261   if (log_window == NULL)
1262     return;
1263
1264   view = GTK_TREE_VIEW (log_window->treeview_when);
1265   model = gtk_tree_view_get_model (view);
1266   store = GTK_LIST_STORE (model);
1267   selection = gtk_tree_view_get_selection (view);
1268
1269   for (l = log_window->hits; l != NULL; l = l->next)
1270     {
1271       TplLogSearchHit *hit = l->data;
1272       GList *acc, *targ;
1273       gboolean found = FALSE;
1274
1275       /* Protect against invalid data (corrupt or old log files). */
1276       if (hit->account == NULL || hit->target == NULL)
1277         continue;
1278
1279       for (acc = accounts, targ = targets;
1280            acc != NULL && targ != NULL && !found;
1281            acc = acc->next, targ = targ->next)
1282         {
1283           TpAccount *account = acc->data;
1284           TplEntity *target = targ->data;
1285
1286           if (account_equal (hit->account, account) &&
1287               entity_equal (hit->target, target))
1288             found = TRUE;
1289         }
1290
1291         if (!found)
1292           continue;
1293
1294       /* Add the date if it's not already there */
1295       has_element = FALSE;
1296       gtk_tree_model_foreach (model, model_has_date, hit->date);
1297       if (!has_element)
1298         {
1299           gchar *text = format_date_for_display (hit->date);
1300
1301           gtk_list_store_append (store, &iter);
1302           gtk_list_store_set (store, &iter,
1303               COL_WHEN_DATE, hit->date,
1304               COL_WHEN_TEXT, text,
1305               COL_WHEN_ICON, CALENDAR_ICON,
1306               -1);
1307         }
1308     }
1309
1310   if (gtk_tree_model_get_iter_first (model, &iter))
1311     {
1312       gtk_list_store_prepend (store, &iter);
1313       gtk_list_store_set (store, &iter,
1314           COL_WHEN_DATE, g_date_new_dmy (1, 1, -1),
1315           COL_WHEN_TEXT, "separator",
1316           -1);
1317
1318       gtk_list_store_prepend (store, &iter);
1319       gtk_list_store_set (store, &iter,
1320           COL_WHEN_DATE, g_date_new_dmy (2, 1, -1),
1321           COL_WHEN_TEXT, _("Anytime"),
1322           -1);
1323
1324       if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 2))
1325         gtk_tree_selection_select_iter (selection, &iter);
1326     }
1327 }
1328
1329 static void
1330 populate_entities_from_search_hits (void)
1331 {
1332   EmpathyAccountChooser *account_chooser;
1333   TpAccount *account;
1334   GtkTreeView *view;
1335   GtkTreeModel *model;
1336   GtkTreeIter iter;
1337   GtkListStore *store;
1338   GList *l;
1339
1340   view = GTK_TREE_VIEW (log_window->treeview_who);
1341   model = gtk_tree_view_get_model (view);
1342   store = GTK_LIST_STORE (model);
1343
1344   gtk_list_store_clear (store);
1345
1346   account_chooser = EMPATHY_ACCOUNT_CHOOSER (log_window->account_chooser);
1347   account = empathy_account_chooser_get_account (account_chooser);
1348
1349   for (l = log_window->hits; l; l = l->next)
1350     {
1351       TplLogSearchHit *hit = l->data;
1352
1353       /* Protect against invalid data (corrupt or old log files). */
1354       if (hit->account == NULL || hit->target == NULL)
1355         continue;
1356
1357       /* Filter based on the selected account */
1358       if (account != NULL && !account_equal (account, hit->account))
1359         continue;
1360
1361       /* Add the entity if it's not already there */
1362       has_element = FALSE;
1363       gtk_tree_model_foreach (model, model_has_entity, hit);
1364       if (!has_element)
1365         {
1366           TplEntityType type = tpl_entity_get_entity_type (hit->target);
1367           gboolean room = type == TPL_ENTITY_ROOM;
1368
1369           gtk_list_store_append (store, &iter);
1370           gtk_list_store_set (store, &iter,
1371               COL_WHO_TYPE, COL_TYPE_NORMAL,
1372               COL_WHO_ICON, room ? EMPATHY_IMAGE_GROUP_MESSAGE
1373                                  : EMPATHY_IMAGE_AVATAR_DEFAULT,
1374               COL_WHO_NAME, tpl_entity_get_alias (hit->target),
1375               COL_WHO_ACCOUNT, hit->account,
1376               COL_WHO_TARGET, hit->target,
1377               -1);
1378         }
1379     }
1380
1381   if (gtk_tree_model_get_iter_first (model, &iter))
1382     {
1383       gtk_list_store_prepend (store, &iter);
1384       gtk_list_store_set (store, &iter,
1385           COL_WHO_TYPE, COL_TYPE_SEPARATOR,
1386           COL_WHO_NAME, "separator",
1387           -1);
1388
1389       gtk_list_store_prepend (store, &iter);
1390       gtk_list_store_set (store, &iter,
1391           COL_WHO_TYPE, COL_TYPE_ANY,
1392           COL_WHO_NAME, _("Anyone"),
1393           -1);
1394     }
1395
1396   /* FIXME: select old entity if still available */
1397 }
1398
1399 static void
1400 log_manager_searched_new_cb (GObject *manager,
1401     GAsyncResult *result,
1402     gpointer user_data)
1403 {
1404   GList *hits;
1405   GtkTreeView *view;
1406   GtkTreeSelection *selection;
1407   GError *error = NULL;
1408
1409   if (log_window == NULL)
1410     return;
1411
1412   if (!tpl_log_manager_search_finish (TPL_LOG_MANAGER (manager),
1413       result, &hits, &error))
1414     {
1415       DEBUG ("%s. Aborting", error->message);
1416       g_error_free (error);
1417       return;
1418     }
1419
1420   tp_clear_pointer (&log_window->hits, tpl_log_manager_search_free);
1421   log_window->hits = hits;
1422
1423   populate_entities_from_search_hits ();
1424
1425   view = GTK_TREE_VIEW (log_window->treeview_when);
1426   selection = gtk_tree_view_get_selection (view);
1427
1428   g_signal_handlers_unblock_by_func (selection,
1429       log_window_when_changed_cb,
1430       log_window);
1431 }
1432
1433 static void
1434 log_window_find_populate (EmpathyLogWindow *window,
1435     const gchar *search_criteria)
1436 {
1437   GtkTreeView *view;
1438   GtkTreeModel *model;
1439   GtkTreeSelection *selection;
1440   GtkListStore *store;
1441
1442   gtk_tree_store_clear (window->store_events);
1443
1444   view = GTK_TREE_VIEW (window->treeview_who);
1445   model = gtk_tree_view_get_model (view);
1446   store = GTK_LIST_STORE (model);
1447
1448   gtk_list_store_clear (store);
1449
1450   view = GTK_TREE_VIEW (window->treeview_when);
1451   model = gtk_tree_view_get_model (view);
1452   store = GTK_LIST_STORE (model);
1453   selection = gtk_tree_view_get_selection (view);
1454
1455   gtk_list_store_clear (store);
1456
1457   if (EMP_STR_EMPTY (search_criteria))
1458     {
1459       tp_clear_pointer (&window->hits, tpl_log_manager_search_free);
1460       log_window_who_populate (window);
1461       return;
1462     }
1463
1464   g_signal_handlers_block_by_func (selection,
1465       log_window_when_changed_cb,
1466       window);
1467
1468   tpl_log_manager_search_async (window->log_manager,
1469       search_criteria, TPL_EVENT_MASK_ANY,
1470       log_manager_searched_new_cb, NULL);
1471 }
1472
1473 static gboolean
1474 start_find_search (EmpathyLogWindow *window)
1475 {
1476   const gchar *str;
1477
1478   str = gtk_entry_get_text (GTK_ENTRY (window->search_entry));
1479
1480   /* Don't find the same crap again */
1481   if (window->last_find && !tp_strdiff (window->last_find, str))
1482     return FALSE;
1483
1484   g_free (window->last_find);
1485   window->last_find = g_strdup (str);
1486
1487   log_window_find_populate (window, str);
1488
1489   return FALSE;
1490 }
1491
1492 static void
1493 log_window_search_entry_changed_cb (GtkWidget *entry,
1494     EmpathyLogWindow *window)
1495 {
1496   if (window->source != 0)
1497     g_source_remove (window->source);
1498   window->source = g_timeout_add (500, (GSourceFunc) start_find_search,
1499       window);
1500 }
1501
1502 static void
1503 log_window_search_entry_activate_cb (GtkWidget *entry,
1504     EmpathyLogWindow *self)
1505 {
1506   start_find_search (self);
1507 }
1508
1509 static void
1510 log_window_search_entry_icon_pressed_cb (GtkEntry *entry,
1511     GtkEntryIconPosition icon_pos,
1512     GdkEvent *event,
1513     gpointer user_data)
1514 {
1515   if (icon_pos != GTK_ENTRY_ICON_SECONDARY)
1516     return;
1517
1518   gtk_entry_buffer_set_text (gtk_entry_get_buffer (entry),
1519     "", -1);
1520 }
1521
1522 static void
1523 log_window_update_buttons_sensitivity (EmpathyLogWindow *window,
1524     GtkTreeModel *model,
1525     GtkTreeSelection *selection)
1526 {
1527   EmpathyContact *contact;
1528   EmpathyCapabilities capabilities;
1529   TpAccount *account;
1530   TplEntity *target;
1531   GtkTreeIter iter;
1532   GList *paths;
1533   GtkTreePath *path;
1534   gboolean profile, chat, call, video;
1535
1536   profile = chat = call = video = FALSE;
1537
1538   if (!gtk_tree_model_get_iter_first (model, &iter))
1539     goto out;
1540
1541   if (gtk_tree_selection_count_selected_rows (selection) != 1)
1542     goto out;
1543
1544   if (gtk_tree_selection_iter_is_selected (selection, &iter))
1545     goto out;
1546
1547   paths = gtk_tree_selection_get_selected_rows (selection, &model);
1548   g_return_if_fail (paths != NULL);
1549
1550   path = paths->data;
1551   gtk_tree_model_get_iter (model, &iter, path);
1552   gtk_tree_model_get (model, &iter,
1553       COL_WHO_ACCOUNT, &account,
1554       COL_WHO_TARGET, &target,
1555       -1);
1556
1557   g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free);
1558
1559   contact = empathy_contact_from_tpl_contact (account, target);
1560
1561   g_object_unref (account);
1562   g_object_unref (target);
1563
1564   capabilities = empathy_contact_get_capabilities (contact);
1565
1566   profile = chat = TRUE;
1567   call = capabilities & EMPATHY_CAPABILITIES_AUDIO;
1568   video = capabilities & EMPATHY_CAPABILITIES_VIDEO;
1569
1570  out:
1571   gtk_widget_set_sensitive (window->button_profile, profile);
1572   gtk_widget_set_sensitive (window->button_chat, chat);
1573   gtk_widget_set_sensitive (window->button_call, call);
1574   gtk_widget_set_sensitive (window->button_video, video);
1575 }
1576
1577 static void
1578 log_window_who_changed_cb (GtkTreeSelection *selection,
1579     EmpathyLogWindow  *window)
1580 {
1581   GtkTreeView *view;
1582   GtkTreeModel *model;
1583   GtkTreeIter iter;
1584
1585   DEBUG ("log_window_who_changed_cb");
1586
1587   view = gtk_tree_selection_get_tree_view (selection);
1588   model = gtk_tree_view_get_model (view);
1589
1590   if (gtk_tree_model_get_iter_first (model, &iter))
1591     {
1592       /* If 'Anyone' is selected, everything else should be deselected */
1593       if (gtk_tree_selection_iter_is_selected (selection, &iter))
1594         {
1595           g_signal_handlers_block_by_func (selection,
1596               log_window_who_changed_cb,
1597               window);
1598
1599           gtk_tree_selection_unselect_all (selection);
1600           gtk_tree_selection_select_iter (selection, &iter);
1601
1602           g_signal_handlers_unblock_by_func (selection,
1603               log_window_who_changed_cb,
1604               window);
1605         }
1606     }
1607
1608   log_window_update_buttons_sensitivity (window, model, selection);
1609
1610   /* The contact changed, so the dates need to be updated */
1611   log_window_chats_get_messages (window, TRUE);
1612 }
1613
1614 static void
1615 log_manager_got_entities_cb (GObject *manager,
1616     GAsyncResult *result,
1617     gpointer user_data)
1618 {
1619   Ctx                   *ctx = user_data;
1620   GList                 *entities;
1621   GList                 *l;
1622   GtkTreeView           *view;
1623   GtkTreeModel          *model;
1624   GtkTreeSelection      *selection;
1625   GtkListStore          *store;
1626   GtkTreeIter            iter;
1627   GError                *error = NULL;
1628   gboolean               select_account = FALSE;
1629
1630   if (log_window == NULL)
1631     goto out;
1632
1633   if (log_window->count != ctx->count)
1634     goto out;
1635
1636   if (!tpl_log_manager_get_entities_finish (TPL_LOG_MANAGER (manager),
1637       result, &entities, &error))
1638     {
1639       DEBUG ("%s. Aborting", error->message);
1640       g_error_free (error);
1641       goto out;
1642     }
1643
1644   view = GTK_TREE_VIEW (ctx->window->treeview_who);
1645   model = gtk_tree_view_get_model (view);
1646   selection = gtk_tree_view_get_selection (view);
1647   store = GTK_LIST_STORE (model);
1648
1649   /* Block signals to stop the logs being retrieved prematurely  */
1650   g_signal_handlers_block_by_func (selection,
1651       log_window_who_changed_cb, ctx->window);
1652
1653   for (l = entities; l; l = l->next)
1654     {
1655       TplEntity *entity = TPL_ENTITY (l->data);
1656       TplEntityType type = tpl_entity_get_entity_type (entity);
1657       gboolean room = type == TPL_ENTITY_ROOM;
1658
1659       gtk_list_store_append (store, &iter);
1660       gtk_list_store_set (store, &iter,
1661           COL_WHO_TYPE, COL_TYPE_NORMAL,
1662           COL_WHO_ICON, room ? EMPATHY_IMAGE_GROUP_MESSAGE
1663                              : EMPATHY_IMAGE_AVATAR_DEFAULT,
1664           COL_WHO_NAME, tpl_entity_get_alias (entity),
1665           COL_WHO_ACCOUNT, ctx->account,
1666           COL_WHO_TARGET, entity,
1667           -1);
1668
1669       if (ctx->window->selected_account != NULL &&
1670           !tp_strdiff (tp_proxy_get_object_path (ctx->account),
1671           tp_proxy_get_object_path (ctx->window->selected_account)))
1672         select_account = TRUE;
1673     }
1674   g_list_free_full (entities, g_object_unref);
1675
1676   if (gtk_tree_model_get_iter_first (model, &iter))
1677     {
1678       gint type;
1679
1680       gtk_tree_model_get (model, &iter,
1681           COL_WHO_TYPE, &type,
1682           -1);
1683
1684       if (type != COL_TYPE_ANY)
1685         {
1686           gtk_list_store_prepend (store, &iter);
1687           gtk_list_store_set (store, &iter,
1688               COL_WHO_TYPE, COL_TYPE_SEPARATOR,
1689               COL_WHO_NAME, "separator",
1690               -1);
1691
1692           gtk_list_store_prepend (store, &iter);
1693           gtk_list_store_set (store, &iter,
1694               COL_WHO_TYPE, COL_TYPE_ANY,
1695               COL_WHO_NAME, _("Anyone"),
1696               -1);
1697         }
1698     }
1699
1700   /* Unblock signals */
1701   g_signal_handlers_unblock_by_func (selection,
1702       log_window_who_changed_cb,
1703       ctx->window);
1704
1705   /* We display the selected account if we populate the model with chats from
1706    * this account. */
1707   if (select_account)
1708     log_window_chats_set_selected (ctx->window);
1709
1710 out:
1711   _tpl_action_chain_continue (log_window->chain);
1712   ctx_free (ctx);
1713 }
1714
1715 static void
1716 get_entities_for_account (TplActionChain *chain, gpointer user_data)
1717 {
1718   Ctx *ctx = user_data;
1719
1720   tpl_log_manager_get_entities_async (ctx->window->log_manager, ctx->account,
1721       log_manager_got_entities_cb, ctx);
1722 }
1723
1724 static void
1725 select_first_entity (TplActionChain *chain, gpointer user_data)
1726 {
1727   GtkTreeView *view;
1728   GtkTreeModel *model;
1729   GtkTreeSelection *selection;
1730   GtkTreeIter iter;
1731
1732   view = GTK_TREE_VIEW (log_window->treeview_who);
1733   model = gtk_tree_view_get_model (view);
1734   selection = gtk_tree_view_get_selection (view);
1735
1736   if (gtk_tree_model_get_iter_first (model, &iter))
1737     gtk_tree_selection_select_iter (selection, &iter);
1738
1739   _tpl_action_chain_continue (log_window->chain);
1740 }
1741
1742 static void
1743 log_window_who_populate (EmpathyLogWindow *window)
1744 {
1745   EmpathyAccountChooser *account_chooser;
1746   TpAccount *account;
1747   gboolean all_accounts;
1748   GtkTreeView *view;
1749   GtkTreeModel *model;
1750   GtkTreeSelection *selection;
1751   GtkListStore *store;
1752   Ctx *ctx;
1753
1754   if (window->hits != NULL)
1755     {
1756       populate_entities_from_search_hits ();
1757       return;
1758     }
1759
1760   account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
1761   account = empathy_account_chooser_dup_account (account_chooser);
1762   all_accounts = empathy_account_chooser_has_all_selected (account_chooser);
1763
1764   view = GTK_TREE_VIEW (window->treeview_who);
1765   model = gtk_tree_view_get_model (view);
1766   selection = gtk_tree_view_get_selection (view);
1767   store = GTK_LIST_STORE (model);
1768
1769   /* Block signals to stop the logs being retrieved prematurely  */
1770   g_signal_handlers_block_by_func (selection,
1771       log_window_who_changed_cb,
1772       window);
1773
1774   gtk_list_store_clear (store);
1775
1776   /* Unblock signals */
1777   g_signal_handlers_unblock_by_func (selection,
1778       log_window_who_changed_cb,
1779       window);
1780
1781   _tpl_action_chain_clear (window->chain);
1782   window->count++;
1783
1784   if (!all_accounts && account == NULL)
1785     {
1786       return;
1787     }
1788   else if (!all_accounts)
1789     {
1790       ctx = ctx_new (window, account, NULL, NULL, 0, 0, window->count);
1791       _tpl_action_chain_append (window->chain, get_entities_for_account, ctx);
1792     }
1793   else
1794     {
1795       TpAccountManager *manager;
1796       GList *accounts, *l;
1797
1798       manager = empathy_account_chooser_get_account_manager (account_chooser);
1799       accounts = tp_account_manager_get_valid_accounts (manager);
1800
1801       for (l = accounts; l != NULL; l = l->next)
1802         {
1803           account = l->data;
1804
1805           ctx = ctx_new (window, account, NULL, NULL, 0, 0, window->count);
1806           _tpl_action_chain_append (window->chain,
1807               get_entities_for_account, ctx);
1808         }
1809
1810       g_list_free (accounts);
1811     }
1812   _tpl_action_chain_append (window->chain, select_first_entity, NULL);
1813   _tpl_action_chain_start (window->chain);
1814 }
1815
1816 static gint
1817 sort_by_name (GtkTreeModel *model,
1818     GtkTreeIter *a,
1819     GtkTreeIter *b,
1820     gpointer user_data)
1821 {
1822   gchar *name1, *name2;
1823   gint type1, type2;
1824   gint ret;
1825
1826   gtk_tree_model_get (model, a,
1827       COL_WHO_TYPE, &type1,
1828       COL_WHO_NAME, &name1,
1829       -1);
1830
1831   gtk_tree_model_get (model, b,
1832       COL_WHO_TYPE, &type2,
1833       COL_WHO_NAME, &name2,
1834       -1);
1835
1836   if (type1 == COL_TYPE_ANY)
1837     ret = -1;
1838   else if (type2 == COL_TYPE_ANY)
1839     ret = 1;
1840   else if (type1 == COL_TYPE_SEPARATOR)
1841     ret = -1;
1842   else if (type2 == COL_TYPE_SEPARATOR)
1843     ret = 1;
1844   else
1845     ret = g_strcmp0 (name1, name2);
1846
1847   g_free (name1);
1848   g_free (name2);
1849
1850   return ret;
1851 }
1852
1853 static gboolean
1854 who_row_is_separator (GtkTreeModel *model,
1855     GtkTreeIter *iter,
1856     gpointer data)
1857 {
1858   gint type;
1859
1860   gtk_tree_model_get (model, iter,
1861       COL_WHO_TYPE, &type,
1862       -1);
1863
1864   return (type == COL_TYPE_SEPARATOR);
1865 }
1866
1867 static void
1868 log_window_events_setup (EmpathyLogWindow *window)
1869 {
1870   GtkTreeView       *view;
1871   GtkTreeModel      *model;
1872   GtkTreeSelection  *selection;
1873   GtkTreeSortable   *sortable;
1874   GtkTreeViewColumn *column;
1875   GtkTreeStore      *store;
1876   GtkCellRenderer   *cell;
1877
1878   view = GTK_TREE_VIEW (window->treeview_events);
1879   selection = gtk_tree_view_get_selection (view);
1880
1881   /* new store */
1882   window->store_events = store = gtk_tree_store_new (COL_EVENTS_COUNT,
1883       G_TYPE_INT,           /* type */
1884       G_TYPE_INT64,         /* timestamp */
1885       G_TYPE_STRING,        /* stringified date */
1886       G_TYPE_STRING,        /* icon */
1887       G_TYPE_STRING,        /* name */
1888       TP_TYPE_ACCOUNT,      /* account */
1889       TPL_TYPE_ENTITY,      /* target */
1890       TPL_TYPE_EVENT);      /* event */
1891
1892   model = GTK_TREE_MODEL (store);
1893   sortable = GTK_TREE_SORTABLE (store);
1894
1895   gtk_tree_view_set_model (view, model);
1896
1897   /* new column */
1898   column = gtk_tree_view_column_new ();
1899
1900   cell = gtk_cell_renderer_pixbuf_new ();
1901   gtk_tree_view_column_pack_start (column, cell, FALSE);
1902   gtk_tree_view_column_add_attribute (column, cell,
1903       "icon-name", COL_EVENTS_ICON);
1904
1905   cell = gtk_cell_renderer_text_new ();
1906   gtk_tree_view_column_pack_start (column, cell, TRUE);
1907   gtk_tree_view_column_add_attribute (column, cell,
1908       "markup", COL_EVENTS_TEXT);
1909
1910   cell = gtk_cell_renderer_text_new ();
1911   g_object_set (cell, "xalign", 1.0, NULL);
1912   gtk_tree_view_column_pack_end (column, cell, FALSE);
1913   gtk_tree_view_column_add_attribute (column, cell,
1914       "text", COL_EVENTS_PRETTY_DATE);
1915
1916   gtk_tree_view_append_column (view, column);
1917
1918   /* set up treeview properties */
1919   gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
1920   gtk_tree_view_set_headers_visible (view, FALSE);
1921
1922   gtk_tree_sortable_set_sort_column_id (sortable,
1923       COL_EVENTS_TS,
1924       GTK_SORT_ASCENDING);
1925
1926   gtk_tree_view_set_enable_search (view, FALSE);
1927
1928   g_object_unref (store);
1929 }
1930
1931 static void
1932 log_window_who_setup (EmpathyLogWindow *window)
1933 {
1934   GtkTreeView       *view;
1935   GtkTreeModel      *model;
1936   GtkTreeSelection  *selection;
1937   GtkTreeSortable   *sortable;
1938   GtkTreeViewColumn *column;
1939   GtkListStore      *store;
1940   GtkCellRenderer   *cell;
1941
1942   view = GTK_TREE_VIEW (window->treeview_who);
1943   selection = gtk_tree_view_get_selection (view);
1944
1945   /* new store */
1946   store = gtk_list_store_new (COL_WHO_COUNT,
1947       G_TYPE_INT,           /* type */
1948       G_TYPE_STRING,        /* icon */
1949       G_TYPE_STRING,        /* name */
1950       TP_TYPE_ACCOUNT,      /* account */
1951       TPL_TYPE_ENTITY);     /* target */
1952
1953   model = GTK_TREE_MODEL (store);
1954   sortable = GTK_TREE_SORTABLE (store);
1955
1956   gtk_tree_view_set_model (view, model);
1957
1958   /* new column */
1959   column = gtk_tree_view_column_new ();
1960   gtk_tree_view_column_set_title (column, _("Who"));
1961
1962   cell = gtk_cell_renderer_pixbuf_new ();
1963   gtk_tree_view_column_pack_start (column, cell, FALSE);
1964   gtk_tree_view_column_add_attribute (column, cell,
1965       "icon-name",
1966       COL_WHO_ICON);
1967
1968   cell = gtk_cell_renderer_text_new ();
1969   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1970   gtk_tree_view_column_pack_start (column, cell, TRUE);
1971   gtk_tree_view_column_add_attribute (column, cell,
1972       "text",
1973       COL_WHO_NAME);
1974
1975   gtk_tree_view_append_column (view, column);
1976
1977   /* set up treeview properties */
1978   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
1979   gtk_tree_view_set_row_separator_func (view, who_row_is_separator,
1980       NULL, NULL);
1981
1982   gtk_tree_sortable_set_sort_column_id (sortable,
1983       COL_WHO_NAME,
1984       GTK_SORT_ASCENDING);
1985   gtk_tree_sortable_set_sort_func (sortable,
1986       COL_WHO_NAME, sort_by_name,
1987       NULL, NULL);
1988
1989   gtk_tree_view_set_search_column (view, COL_WHO_NAME);
1990
1991   /* set up signals */
1992   g_signal_connect (selection, "changed",
1993       G_CALLBACK (log_window_who_changed_cb), window);
1994
1995   g_object_unref (store);
1996 }
1997
1998 static void
1999 log_window_chats_accounts_changed_cb (GtkWidget       *combobox,
2000     EmpathyLogWindow *window)
2001 {
2002   /* Clear all current messages shown in the textview */
2003   gtk_tree_store_clear (window->store_events);
2004
2005   log_window_who_populate (window);
2006 }
2007
2008 static void
2009 log_window_chats_set_selected (EmpathyLogWindow *window)
2010 {
2011   GtkTreeView          *view;
2012   GtkTreeModel         *model;
2013   GtkTreeSelection     *selection;
2014   GtkTreeIter           iter;
2015   GtkTreePath          *path;
2016   gboolean              next;
2017
2018   view = GTK_TREE_VIEW (window->treeview_who);
2019   model = gtk_tree_view_get_model (view);
2020   selection = gtk_tree_view_get_selection (view);
2021
2022   for (next = gtk_tree_model_get_iter_first (model, &iter);
2023        next;
2024        next = gtk_tree_model_iter_next (model, &iter))
2025     {
2026       TpAccount   *this_account;
2027       TplEntity   *this_target;
2028       const gchar *this_chat_id;
2029       gboolean     this_is_chatroom;
2030       gint         this_type;
2031
2032       gtk_tree_model_get (model, &iter,
2033           COL_WHO_TYPE, &this_type,
2034           COL_WHO_ACCOUNT, &this_account,
2035           COL_WHO_TARGET, &this_target,
2036           -1);
2037
2038       if (this_type != COL_TYPE_NORMAL)
2039         continue;
2040
2041       this_chat_id = tpl_entity_get_identifier (this_target);
2042       this_is_chatroom = tpl_entity_get_entity_type (this_target)
2043           == TPL_ENTITY_ROOM;
2044
2045       if (this_account == window->selected_account &&
2046           !tp_strdiff (this_chat_id, window->selected_chat_id) &&
2047           this_is_chatroom == window->selected_is_chatroom)
2048         {
2049           gtk_tree_selection_select_iter (selection, &iter);
2050           path = gtk_tree_model_get_path (model, &iter);
2051           gtk_tree_view_scroll_to_cell (view, path, NULL, TRUE, 0.5, 0.0);
2052           gtk_tree_path_free (path);
2053           g_object_unref (this_account);
2054           g_object_unref (this_target);
2055           break;
2056         }
2057
2058       g_object_unref (this_account);
2059       g_object_unref (this_target);
2060     }
2061
2062   tp_clear_object (&window->selected_account);
2063   tp_clear_pointer (&window->selected_chat_id, g_free);
2064 }
2065
2066 static gint
2067 sort_by_date (GtkTreeModel *model,
2068     GtkTreeIter *a,
2069     GtkTreeIter *b,
2070     gpointer user_data)
2071 {
2072   GDate *date1, *date2;
2073
2074   gtk_tree_model_get (model, a,
2075       COL_WHEN_DATE, &date1,
2076       -1);
2077
2078   gtk_tree_model_get (model, b,
2079       COL_WHEN_DATE, &date2,
2080       -1);
2081
2082   return g_date_compare (date1, date2);
2083 }
2084
2085 static gboolean
2086 when_row_is_separator (GtkTreeModel *model,
2087     GtkTreeIter *iter,
2088     gpointer data)
2089 {
2090   gchar *when;
2091   gboolean ret;
2092
2093   gtk_tree_model_get (model, iter,
2094       COL_WHEN_TEXT, &when,
2095       -1);
2096
2097   ret = g_str_equal (when, "separator");
2098   g_free (when);
2099   return ret;
2100 }
2101
2102 static void
2103 log_window_when_changed_cb (GtkTreeSelection *selection,
2104     EmpathyLogWindow *window)
2105 {
2106   GtkTreeView *view;
2107   GtkTreeModel *model;
2108   GtkTreeIter iter;
2109
2110   DEBUG ("log_window_when_changed_cb");
2111
2112   view = gtk_tree_selection_get_tree_view (selection);
2113   model = gtk_tree_view_get_model (view);
2114
2115   /* If 'Anytime' is selected, everything else should be deselected */
2116   if (gtk_tree_model_get_iter_first (model, &iter))
2117     {
2118       if (gtk_tree_selection_iter_is_selected (selection, &iter))
2119         {
2120           g_signal_handlers_block_by_func (selection,
2121               log_window_when_changed_cb,
2122               window);
2123
2124           gtk_tree_selection_unselect_all (selection);
2125           gtk_tree_selection_select_iter (selection, &iter);
2126
2127           g_signal_handlers_unblock_by_func (selection,
2128               log_window_when_changed_cb,
2129               window);
2130         }
2131     }
2132
2133   log_window_chats_get_messages (window, FALSE);
2134 }
2135
2136 static void
2137 log_window_when_setup (EmpathyLogWindow *window)
2138 {
2139   GtkTreeView       *view;
2140   GtkTreeModel      *model;
2141   GtkTreeSelection  *selection;
2142   GtkTreeSortable   *sortable;
2143   GtkTreeViewColumn *column;
2144   GtkListStore      *store;
2145   GtkCellRenderer   *cell;
2146
2147   view = GTK_TREE_VIEW (window->treeview_when);
2148   selection = gtk_tree_view_get_selection (view);
2149
2150   /* new store */
2151   store = gtk_list_store_new (COL_WHEN_COUNT,
2152       G_TYPE_DATE,        /* date */
2153       G_TYPE_STRING,      /* stringified date */
2154       G_TYPE_STRING);     /* icon */
2155
2156   model = GTK_TREE_MODEL (store);
2157   sortable = GTK_TREE_SORTABLE (store);
2158
2159   gtk_tree_view_set_model (view, model);
2160
2161   /* new column */
2162   column = gtk_tree_view_column_new ();
2163   gtk_tree_view_column_set_title (column, _("When"));
2164
2165   cell = gtk_cell_renderer_pixbuf_new ();
2166   gtk_tree_view_column_pack_start (column, cell, FALSE);
2167   gtk_tree_view_column_add_attribute (column, cell,
2168       "icon-name", COL_WHEN_ICON);
2169
2170   cell = gtk_cell_renderer_text_new ();
2171   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2172   gtk_tree_view_column_pack_start (column, cell, TRUE);
2173   gtk_tree_view_column_add_attribute (column, cell,
2174       "text",
2175       COL_WHEN_TEXT);
2176
2177   gtk_tree_view_append_column (view, column);
2178
2179   /* set up treeview properties */
2180   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
2181   gtk_tree_view_set_row_separator_func (view, when_row_is_separator,
2182       NULL, NULL);
2183   gtk_tree_sortable_set_sort_column_id (sortable,
2184       COL_WHEN_DATE,
2185       GTK_SORT_DESCENDING);
2186   gtk_tree_sortable_set_sort_func (sortable,
2187       COL_WHEN_DATE, sort_by_date,
2188       NULL, NULL);
2189
2190   gtk_tree_view_set_search_column (view, COL_WHEN_TEXT);
2191
2192   /* set up signals */
2193   g_signal_connect (selection, "changed",
2194       G_CALLBACK (log_window_when_changed_cb),
2195       window);
2196
2197   g_object_unref (store);
2198 }
2199
2200 static gboolean
2201 what_row_is_separator (GtkTreeModel *model,
2202     GtkTreeIter *iter,
2203     gpointer data)
2204 {
2205   gint type;
2206
2207   gtk_tree_model_get (model, iter,
2208       COL_WHAT_TYPE, &type,
2209       -1);
2210
2211   return (type == WHAT_TYPE_SEPARATOR);
2212 }
2213
2214 static void
2215 log_window_what_changed_cb (GtkTreeSelection *selection,
2216     EmpathyLogWindow *window)
2217 {
2218   GtkTreeView *view;
2219   GtkTreeModel *model;
2220   GtkTreeIter iter;
2221
2222   DEBUG ("log_window_what_changed_cb");
2223
2224   view = gtk_tree_selection_get_tree_view (selection);
2225   model = gtk_tree_view_get_model (view);
2226
2227   /* If 'Anything' is selected, everything else should be deselected */
2228   if (gtk_tree_model_get_iter_first (model, &iter))
2229     {
2230       if (gtk_tree_selection_iter_is_selected (selection, &iter))
2231         {
2232           g_signal_handlers_block_by_func (selection,
2233               log_window_what_changed_cb,
2234               window);
2235
2236           gtk_tree_selection_unselect_all (selection);
2237           gtk_tree_selection_select_iter (selection, &iter);
2238
2239           g_signal_handlers_unblock_by_func (selection,
2240               log_window_what_changed_cb,
2241               window);
2242         }
2243     }
2244
2245   /* The dates need to be updated if we're not searching */
2246   log_window_chats_get_messages (window, window->hits == NULL);
2247 }
2248
2249 static gboolean
2250 log_window_what_collapse_row_cb (GtkTreeView *tree_view,
2251     GtkTreeIter *iter,
2252     GtkTreePath *path,
2253     gpointer user_data)
2254 {
2255   /* Reject collapsing */
2256   return TRUE;
2257 }
2258
2259 struct event
2260 {
2261   gint type;
2262   EventSubtype subtype;
2263   const gchar *icon;
2264   const gchar *text;
2265 };
2266
2267 static void
2268 log_window_what_setup (EmpathyLogWindow *window)
2269 {
2270   GtkTreeView       *view;
2271   GtkTreeModel      *model;
2272   GtkTreeSelection  *selection;
2273   GtkTreeViewColumn *column;
2274   GtkTreeIter        iter;
2275   GtkTreeStore      *store;
2276   GtkCellRenderer   *cell;
2277   guint i;
2278   struct event events [] = {
2279     { TPL_EVENT_MASK_ANY, 0, NULL, _("Anything") },
2280     { WHAT_TYPE_SEPARATOR, 0, NULL, "separator" },
2281     { TPL_EVENT_MASK_TEXT, 0, "stock_text_justify", _("Text chats") },
2282 #ifdef HAVE_CALL_LOGS
2283     { TPL_EVENT_MASK_CALL, EVENT_CALL_ALL, EMPATHY_IMAGE_CALL, _("Calls") },
2284 #endif
2285   };
2286 #ifdef HAVE_CALL_LOGS
2287   struct event call_events [] = {
2288     { TPL_EVENT_MASK_CALL, EVENT_CALL_INCOMING, EMPATHY_IMAGE_CALL_INCOMING, _("Incoming calls") },
2289     { TPL_EVENT_MASK_CALL, EVENT_CALL_OUTGOING, EMPATHY_IMAGE_CALL_OUTGOING, _("Outgoing calls") },
2290     { TPL_EVENT_MASK_CALL, EVENT_CALL_MISSED, EMPATHY_IMAGE_CALL_MISSED, _("Missed calls") }
2291   };
2292   GtkTreeIter parent;
2293 #endif
2294
2295   view = GTK_TREE_VIEW (window->treeview_what);
2296   selection = gtk_tree_view_get_selection (view);
2297
2298   /* new store */
2299   store = gtk_tree_store_new (COL_WHAT_COUNT,
2300       G_TYPE_INT,         /* history type */
2301       G_TYPE_INT,         /* history subtype */
2302       G_TYPE_STRING,      /* stringified history type */
2303       G_TYPE_STRING);     /* icon */
2304
2305   model = GTK_TREE_MODEL (store);
2306
2307   gtk_tree_view_set_model (view, model);
2308
2309   /* new column */
2310   column = gtk_tree_view_column_new ();
2311   gtk_tree_view_column_set_title (column, _("What"));
2312
2313   cell = gtk_cell_renderer_pixbuf_new ();
2314   gtk_tree_view_column_pack_start (column, cell, FALSE);
2315   gtk_tree_view_column_add_attribute (column, cell,
2316       "icon-name", COL_WHAT_ICON);
2317
2318   cell = gtk_cell_renderer_text_new ();
2319   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2320   gtk_tree_view_column_pack_start (column, cell, TRUE);
2321   gtk_tree_view_column_add_attribute (column, cell,
2322       "text", COL_WHAT_TEXT);
2323
2324   gtk_tree_view_append_column (view, column);
2325   gtk_tree_view_set_search_column (view, COL_WHAT_TEXT);
2326
2327   /* set up treeview properties */
2328   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
2329   gtk_tree_view_set_show_expanders (view, FALSE);
2330   gtk_tree_view_set_level_indentation (view, 12);
2331   gtk_tree_view_expand_all (view);
2332   gtk_tree_view_set_row_separator_func (view, what_row_is_separator,
2333       NULL, NULL);
2334
2335   /* populate */
2336   for (i = 0; i < G_N_ELEMENTS (events); i++)
2337     {
2338       gtk_tree_store_append (store, &iter, NULL);
2339       gtk_tree_store_set (store, &iter,
2340           COL_WHAT_TYPE, events[i].type,
2341           COL_WHAT_SUBTYPE, events[i].subtype,
2342           COL_WHAT_TEXT, events[i].text,
2343           COL_WHAT_ICON, events[i].icon,
2344           -1);
2345     }
2346
2347 #ifdef HAVE_CALL_LOGS
2348   gtk_tree_model_iter_nth_child (model, &parent, NULL, 3);
2349   for (i = 0; i < G_N_ELEMENTS (call_events); i++)
2350     {
2351       gtk_tree_store_append (store, &iter, &parent);
2352       gtk_tree_store_set (store, &iter,
2353           COL_WHAT_TYPE, call_events[i].type,
2354           COL_WHAT_SUBTYPE, call_events[i].subtype,
2355           COL_WHAT_TEXT, call_events[i].text,
2356           COL_WHAT_ICON, call_events[i].icon,
2357           -1);
2358     }
2359 #endif
2360
2361   gtk_tree_view_expand_all (view);
2362
2363   /* select 'Anything' */
2364   if (gtk_tree_model_get_iter_first (model, &iter))
2365     gtk_tree_selection_select_iter (selection, &iter);
2366
2367   /* set up signals */
2368   g_signal_connect (view, "test-collapse-row",
2369       G_CALLBACK (log_window_what_collapse_row_cb),
2370       NULL);
2371   g_signal_connect (selection, "changed",
2372       G_CALLBACK (log_window_what_changed_cb),
2373       window);
2374
2375   g_object_unref (store);
2376 }
2377
2378 static void
2379 start_spinner (void)
2380 {
2381   gtk_spinner_start (GTK_SPINNER (log_window->spinner));
2382   gtk_notebook_set_current_page (GTK_NOTEBOOK (log_window->notebook),
2383       PAGE_EMPTY);
2384 }
2385
2386 static gboolean
2387 show_spinner (gpointer data)
2388 {
2389   gboolean active;
2390
2391   if (log_window == NULL)
2392     return FALSE;
2393
2394   g_object_get (log_window->spinner, "active", &active, NULL);
2395
2396   if (active)
2397     gtk_notebook_set_current_page (GTK_NOTEBOOK (log_window->notebook),
2398         PAGE_SPINNER);
2399
2400   return FALSE;
2401 }
2402
2403 static void
2404 show_events (TplActionChain *chain,
2405     gpointer user_data)
2406 {
2407   gtk_spinner_stop (GTK_SPINNER (log_window->spinner));
2408   gtk_notebook_set_current_page (GTK_NOTEBOOK (log_window->notebook),
2409       PAGE_EVENTS);
2410
2411   _tpl_action_chain_continue (chain);
2412 }
2413
2414 static void
2415 log_window_got_messages_for_date_cb (GObject *manager,
2416     GAsyncResult *result,
2417     gpointer user_data)
2418 {
2419   Ctx *ctx = user_data;
2420   GtkTreeView *view;
2421   GtkTreeModel *model;
2422   GtkTreeIter iter;
2423   GList *events;
2424   GList *l;
2425   GError *error = NULL;
2426   gint n;
2427
2428   if (log_window == NULL)
2429     {
2430       ctx_free (ctx);
2431       return;
2432     }
2433
2434   if (log_window->count != ctx->count)
2435     goto out;
2436
2437   if (!tpl_log_manager_get_events_for_date_finish (TPL_LOG_MANAGER (manager),
2438       result, &events, &error))
2439     {
2440       DEBUG ("Unable to retrieve messages for the selected date: %s. Aborting",
2441           error->message);
2442       g_error_free (error);
2443       goto out;
2444     }
2445
2446   for (l = events; l; l = l->next)
2447     {
2448       TplEvent *event = l->data;
2449       gboolean append = TRUE;
2450
2451 #ifdef HAVE_CALL_LOGS
2452       if (TPL_IS_CALL_EVENT (l->data)
2453           && ctx->event_mask & TPL_EVENT_MASK_CALL
2454           && ctx->event_mask != TPL_EVENT_MASK_ANY)
2455         {
2456           TplCallEvent *call = l->data;
2457
2458           append = FALSE;
2459
2460           if (ctx->subtype & EVENT_CALL_ALL)
2461             {
2462               append = TRUE;
2463             }
2464           else
2465             {
2466               TplCallEndReason reason = tpl_call_event_get_end_reason (call);
2467               TplEntity *sender = tpl_event_get_sender (event);
2468               TplEntity *receiver = tpl_event_get_receiver (event);
2469
2470               if (reason == TPL_CALL_END_REASON_NO_ANSWER)
2471                 {
2472                   if (ctx->subtype & EVENT_CALL_MISSED)
2473                     append = TRUE;
2474                 }
2475               else if (ctx->subtype & EVENT_CALL_OUTGOING
2476                   && tpl_entity_get_entity_type (sender) == TPL_ENTITY_SELF)
2477                 {
2478                   append = TRUE;
2479                 }
2480               else if (ctx->subtype & EVENT_CALL_INCOMING
2481                   && tpl_entity_get_entity_type (receiver) == TPL_ENTITY_SELF)
2482                 {
2483                   append = TRUE;
2484                 }
2485             }
2486         }
2487 #endif
2488
2489       if (append)
2490         {
2491           EmpathyMessage *msg = empathy_message_from_tpl_log_event (event);
2492           log_window_append_message (event, msg);
2493           tp_clear_object (&msg);
2494         }
2495
2496       g_object_unref (event);
2497     }
2498   g_list_free (events);
2499
2500   view = GTK_TREE_VIEW (log_window->treeview_events);
2501   model = gtk_tree_view_get_model (view);
2502   n = gtk_tree_model_iter_n_children (model, NULL) - 1;
2503
2504   if (n >= 0 && gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
2505     {
2506       GtkTreePath *path;
2507
2508       path = gtk_tree_model_get_path (model, &iter);
2509       gtk_tree_view_scroll_to_cell (view, path, NULL, FALSE, 0, 0);
2510       gtk_tree_path_free (path);
2511     }
2512
2513  out:
2514   ctx_free (ctx);
2515
2516   _tpl_action_chain_continue (log_window->chain);
2517 }
2518
2519 static void
2520 get_events_for_date (TplActionChain *chain, gpointer user_data)
2521 {
2522   Ctx *ctx = user_data;
2523
2524   tpl_log_manager_get_events_for_date_async (ctx->window->log_manager,
2525       ctx->account, ctx->entity, ctx->event_mask,
2526       ctx->date,
2527       log_window_got_messages_for_date_cb,
2528       ctx);
2529 }
2530
2531 static void
2532 log_window_get_messages_for_dates (EmpathyLogWindow *window,
2533     GList *dates)
2534 {
2535   GList *accounts, *targets, *acc, *targ, *l;
2536   TplEventTypeMask event_mask;
2537   EventSubtype subtype;
2538   GDate *date, *anytime, *separator;
2539
2540   if (!log_window_get_selected (window,
2541       &accounts, &targets, NULL, &event_mask, &subtype))
2542     return;
2543
2544   anytime = g_date_new_dmy (2, 1, -1);
2545   separator = g_date_new_dmy (1, 1, -1);
2546
2547   _tpl_action_chain_clear (window->chain);
2548   window->count++;
2549
2550   for (acc = accounts, targ = targets;
2551        acc != NULL && targ != NULL;
2552        acc = acc->next, targ = targ->next)
2553     {
2554       TpAccount *account = acc->data;
2555       TplEntity *target = targ->data;
2556
2557       for (l = dates; l != NULL; l = l->next)
2558         {
2559           date = l->data;
2560
2561           /* Get events */
2562           if (g_date_compare (date, anytime) != 0)
2563             {
2564               Ctx *ctx;
2565
2566               ctx = ctx_new (window, account, target, date, event_mask, subtype,
2567                   window->count);
2568               _tpl_action_chain_append (window->chain, get_events_for_date, ctx);
2569             }
2570           else
2571             {
2572               GtkTreeView *view = GTK_TREE_VIEW (window->treeview_when);
2573               GtkTreeModel *model = gtk_tree_view_get_model (view);
2574               GtkTreeIter iter;
2575               gboolean next;
2576               GDate *d;
2577
2578               for (next = gtk_tree_model_get_iter_first (model, &iter);
2579                    next;
2580                    next = gtk_tree_model_iter_next (model, &iter))
2581                 {
2582                   Ctx *ctx;
2583
2584                   gtk_tree_model_get (model, &iter,
2585                       COL_WHEN_DATE, &d,
2586                       -1);
2587
2588                   if (g_date_compare (d, anytime) != 0 &&
2589                       g_date_compare (d, separator) != 0)
2590                     {
2591                       ctx = ctx_new (window, account, target, d,
2592                           event_mask, subtype, window->count);
2593                       _tpl_action_chain_append (window->chain, get_events_for_date, ctx);
2594                     }
2595                 }
2596             }
2597         }
2598     }
2599
2600   start_spinner ();
2601   g_timeout_add (1000, show_spinner, NULL);
2602   _tpl_action_chain_append (window->chain, show_events, NULL);
2603   _tpl_action_chain_start (window->chain);
2604
2605   g_list_free_full (accounts, g_object_unref);
2606   g_list_free_full (targets, g_object_unref);
2607   g_date_free (separator);
2608   g_date_free (anytime);
2609 }
2610
2611 static void
2612 log_manager_got_dates_cb (GObject *manager,
2613     GAsyncResult *result,
2614     gpointer user_data)
2615 {
2616   Ctx *ctx = user_data;
2617   GtkTreeView *view;
2618   GtkTreeModel *model;
2619   GtkListStore *store;
2620   GtkTreeIter iter;
2621   GList *dates;
2622   GList *l;
2623   GError *error = NULL;
2624
2625   if (log_window == NULL)
2626     goto out;
2627
2628   if (log_window->count != ctx->count)
2629     goto out;
2630
2631   if (!tpl_log_manager_get_dates_finish (TPL_LOG_MANAGER (manager),
2632        result, &dates, &error))
2633     {
2634       DEBUG ("Unable to retrieve messages' dates: %s. Aborting",
2635           error->message);
2636       goto out;
2637     }
2638
2639   view = GTK_TREE_VIEW (log_window->treeview_when);
2640   model = gtk_tree_view_get_model (view);
2641   store = GTK_LIST_STORE (model);
2642
2643   for (l = dates; l != NULL; l = l->next)
2644     {
2645       GDate *date = l->data;
2646
2647       /* Add the date if it's not already there */
2648       has_element = FALSE;
2649       gtk_tree_model_foreach (model, model_has_date, date);
2650       if (!has_element)
2651         {
2652           gchar *text = format_date_for_display (date);
2653
2654           gtk_list_store_append (store, &iter);
2655           gtk_list_store_set (store, &iter,
2656               COL_WHEN_DATE, date,
2657               COL_WHEN_TEXT, text,
2658               COL_WHEN_ICON, CALENDAR_ICON,
2659               -1);
2660
2661           g_free (text);
2662         }
2663     }
2664
2665   if (gtk_tree_model_get_iter_first (model, &iter))
2666     {
2667       gchar *separator = NULL;
2668
2669       if (gtk_tree_model_iter_next (model, &iter))
2670         {
2671           gtk_tree_model_get (model, &iter,
2672               COL_WHEN_TEXT, &separator,
2673               -1);
2674         }
2675
2676       if (g_strcmp0 (separator, "separator") != 0)
2677         {
2678           gtk_list_store_prepend (store, &iter);
2679           gtk_list_store_set (store, &iter,
2680               COL_WHEN_DATE, g_date_new_dmy (1, 1, -1),
2681               COL_WHEN_TEXT, "separator",
2682               -1);
2683
2684           gtk_list_store_prepend (store, &iter);
2685           gtk_list_store_set (store, &iter,
2686               COL_WHEN_DATE, g_date_new_dmy (2, 1, -1),
2687               COL_WHEN_TEXT, _("Anytime"),
2688               -1);
2689         }
2690     }
2691
2692   g_list_free_full (dates, g_free);
2693  out:
2694   ctx_free (ctx);
2695   _tpl_action_chain_continue (log_window->chain);
2696 }
2697
2698 static void
2699 select_first_date (TplActionChain *chain, gpointer user_data)
2700 {
2701   GtkTreeView *view;
2702   GtkTreeModel *model;
2703   GtkTreeSelection *selection;
2704   GtkTreeIter iter;
2705
2706   view = GTK_TREE_VIEW (log_window->treeview_when);
2707   model = gtk_tree_view_get_model (view);
2708   selection = gtk_tree_view_get_selection (view);
2709
2710   /* Show messages of the most recent date */
2711   if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 2))
2712     gtk_tree_selection_select_iter (selection, &iter);
2713
2714   _tpl_action_chain_continue (log_window->chain);
2715 }
2716
2717 static void
2718 get_dates_for_entity (TplActionChain *chain, gpointer user_data)
2719 {
2720   Ctx *ctx = user_data;
2721
2722   tpl_log_manager_get_dates_async (ctx->window->log_manager,
2723       ctx->account, ctx->entity, ctx->event_mask,
2724       log_manager_got_dates_cb, ctx);
2725 }
2726
2727 static void
2728 log_window_chats_get_messages (EmpathyLogWindow *window,
2729     gboolean force_get_dates)
2730 {
2731   GList *accounts, *targets, *dates;
2732   TplEventTypeMask event_mask;
2733   GtkTreeView *view;
2734   GtkTreeModel *model;
2735   GtkListStore *store;
2736   GtkTreeSelection *selection;
2737
2738   if (!log_window_get_selected (window, &accounts, &targets,
2739       &dates, &event_mask, NULL))
2740     return;
2741
2742   view = GTK_TREE_VIEW (window->treeview_when);
2743   selection = gtk_tree_view_get_selection (view);
2744   model = gtk_tree_view_get_model (view);
2745   store = GTK_LIST_STORE (model);
2746
2747   /* Clear all current messages shown in the textview */
2748   gtk_tree_store_clear (window->store_events);
2749
2750   _tpl_action_chain_clear (window->chain);
2751   window->count++;
2752
2753   /* If there's a search use the returned hits */
2754   if (window->hits != NULL)
2755     {
2756       if (force_get_dates)
2757         {
2758           g_signal_handlers_block_by_func (selection,
2759               log_window_when_changed_cb,
2760               window);
2761
2762           gtk_list_store_clear (store);
2763
2764           g_signal_handlers_unblock_by_func (selection,
2765               log_window_when_changed_cb,
2766               window);
2767
2768           populate_dates_from_search_hits (accounts, targets);
2769         }
2770       else
2771         {
2772           populate_events_from_search_hits (accounts, targets, dates);
2773         }
2774     }
2775   /* Either use the supplied date or get the last */
2776   else if (force_get_dates || dates == NULL)
2777     {
2778       GList *acc, *targ;
2779
2780       g_signal_handlers_block_by_func (selection,
2781           log_window_when_changed_cb,
2782           window);
2783
2784       gtk_list_store_clear (store);
2785
2786       g_signal_handlers_unblock_by_func (selection,
2787           log_window_when_changed_cb,
2788           window);
2789
2790       /* Get a list of dates and show them on the treeview */
2791       for (targ = targets, acc = accounts;
2792            targ != NULL && acc != NULL;
2793            targ = targ->next, acc = acc->next)
2794         {
2795           TpAccount *account = acc->data;
2796           TplEntity *target = targ->data;
2797           Ctx *ctx = ctx_new (window, account, target, NULL, event_mask, 0,
2798               window->count);
2799
2800           _tpl_action_chain_append (window->chain, get_dates_for_entity, ctx);
2801         }
2802       _tpl_action_chain_append (window->chain, select_first_date, NULL);
2803       _tpl_action_chain_start (window->chain);
2804     }
2805   else
2806     {
2807       /* Show messages of the selected date */
2808       log_window_get_messages_for_dates (window, dates);
2809     }
2810
2811   g_list_free_full (accounts, g_object_unref);
2812   g_list_free_full (targets, g_object_unref);
2813   g_list_free_full (dates, (GFreeFunc) g_date_free);
2814 }
2815
2816 typedef struct {
2817   EmpathyAccountChooserFilterResultCallback callback;
2818   gpointer user_data;
2819 } FilterCallbackData;
2820
2821 static void
2822 got_entities (GObject *manager,
2823     GAsyncResult *result,
2824     gpointer user_data)
2825 {
2826   FilterCallbackData *data = user_data;
2827   GList *entities;
2828   GError *error = NULL;
2829
2830   if (!tpl_log_manager_get_entities_finish (TPL_LOG_MANAGER (manager),
2831       result, &entities, &error))
2832     {
2833       DEBUG ("Could not get entities: %s", error->message);
2834       g_error_free (error);
2835       data->callback (FALSE, data->user_data);
2836     }
2837   else
2838     {
2839       data->callback (entities != NULL, data->user_data);
2840
2841       g_list_free_full (entities, g_object_unref);
2842     }
2843
2844   g_slice_free (FilterCallbackData, data);
2845 }
2846
2847 static void
2848 empathy_account_chooser_filter_has_logs (TpAccount *account,
2849     EmpathyAccountChooserFilterResultCallback callback,
2850     gpointer callback_data,
2851     gpointer user_data)
2852 {
2853   TplLogManager *manager = tpl_log_manager_dup_singleton ();
2854   FilterCallbackData *cb_data = g_slice_new0 (FilterCallbackData);
2855
2856   cb_data->callback = callback;
2857   cb_data->user_data = callback_data;
2858
2859   tpl_log_manager_get_entities_async (manager, account, got_entities, cb_data);
2860
2861   g_object_unref (manager);
2862 }
2863
2864 static void
2865 log_window_logger_clear_account_cb (TpProxy *proxy,
2866     const GError *error,
2867     gpointer user_data,
2868     GObject *weak_object)
2869 {
2870   EmpathyLogWindow *window = user_data;
2871
2872   if (error != NULL)
2873     g_warning ("Error when clearing logs: %s", error->message);
2874
2875   /* Refresh the log viewer so the logs are cleared if the account
2876    * has been deleted */
2877   gtk_tree_store_clear (window->store_events);
2878   log_window_who_populate (window);
2879
2880   /* Re-filter the account chooser so the accounts without logs get greyed out */
2881   empathy_account_chooser_set_filter (
2882       EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
2883       empathy_account_chooser_filter_has_logs, NULL);
2884 }
2885
2886 static void
2887 log_window_clear_logs_chooser_select_account (EmpathyAccountChooser *chooser,
2888     EmpathyLogWindow *window)
2889 {
2890   EmpathyAccountChooser *account_chooser;
2891
2892   account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
2893
2894   empathy_account_chooser_set_account (chooser,
2895       empathy_account_chooser_get_account (account_chooser));
2896 }
2897
2898 static void
2899 log_window_delete_menu_clicked_cb (GtkMenuItem *menuitem,
2900     EmpathyLogWindow *window)
2901 {
2902   GtkWidget *dialog, *content_area, *hbox, *label;
2903   EmpathyAccountChooser *account_chooser;
2904   gint response_id;
2905   TpDBusDaemon *bus;
2906   TpProxy *logger;
2907   GError *error = NULL;
2908
2909   account_chooser = (EmpathyAccountChooser *) empathy_account_chooser_new ();
2910   empathy_account_chooser_set_has_all_option (account_chooser, TRUE);
2911   empathy_account_chooser_set_filter (account_chooser,
2912       empathy_account_chooser_filter_has_logs, NULL);
2913
2914   /* Select the same account as in the history window */
2915   if (empathy_account_chooser_is_ready (account_chooser))
2916     log_window_clear_logs_chooser_select_account (account_chooser, window);
2917   else
2918     g_signal_connect (account_chooser, "ready",
2919         G_CALLBACK (log_window_clear_logs_chooser_select_account), window);
2920
2921   dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (window->window),
2922       GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING,
2923       GTK_BUTTONS_NONE,
2924       _("Are you sure you want to delete all logs of previous conversations?"));
2925
2926   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2927       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2928       _("Clear All"), GTK_RESPONSE_APPLY,
2929       NULL);
2930
2931   content_area = gtk_message_dialog_get_message_area (
2932       GTK_MESSAGE_DIALOG (dialog));
2933
2934   hbox = gtk_hbox_new (FALSE, 6);
2935   label = gtk_label_new (_("Delete from:"));
2936   gtk_box_pack_start (GTK_BOX (hbox), label,
2937       FALSE, FALSE, 0);
2938   gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (account_chooser),
2939       FALSE, FALSE, 0);
2940   gtk_box_pack_start (GTK_BOX (content_area), hbox,
2941       FALSE, FALSE, 0);
2942
2943   gtk_widget_show_all (hbox);
2944
2945   response_id = gtk_dialog_run (GTK_DIALOG (dialog));
2946
2947   if (response_id != GTK_RESPONSE_APPLY)
2948     goto out;
2949
2950   bus = tp_dbus_daemon_dup (&error);
2951   if (error != NULL)
2952     {
2953       g_warning ("Could not delete logs: %s", error->message);
2954       g_error_free (error);
2955       goto out;
2956     }
2957
2958   logger = g_object_new (TP_TYPE_PROXY,
2959       "bus-name", "org.freedesktop.Telepathy.Logger",
2960       "object-path", "/org/freedesktop/Telepathy/Logger",
2961       "dbus-daemon", bus,
2962       NULL);
2963   g_object_unref (bus);
2964
2965   tp_proxy_add_interface_by_id (logger, EMP_IFACE_QUARK_LOGGER);
2966
2967   if (empathy_account_chooser_has_all_selected (account_chooser))
2968     {
2969       DEBUG ("Deleting logs for all the accounts");
2970
2971       emp_cli_logger_call_clear (logger, -1,
2972           log_window_logger_clear_account_cb,
2973           window, NULL, G_OBJECT (window->window));
2974     }
2975   else
2976     {
2977       TpAccount *account;
2978
2979       account = empathy_account_chooser_get_account (account_chooser);
2980
2981       DEBUG ("Deleting logs for %s", tp_proxy_get_object_path (account));
2982
2983       emp_cli_logger_call_clear_account (logger, -1,
2984           tp_proxy_get_object_path (account),
2985           log_window_logger_clear_account_cb,
2986           window, NULL, G_OBJECT (window->window));
2987     }
2988
2989   g_object_unref (logger);
2990  out:
2991   gtk_widget_destroy (dialog);
2992 }