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