]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-roster-view.c
sort contacts by most recent event
[empathy.git] / libempathy-gtk / empathy-roster-view.c
1 #include "config.h"
2 #include "empathy-roster-view.h"
3
4 #include <glib/gi18n-lib.h>
5
6 #include "empathy-contact-groups.h"
7 #include "empathy-roster-contact.h"
8 #include "empathy-roster-group.h"
9 #include "empathy-ui-utils.h"
10
11 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, GTK_TYPE_LIST_BOX)
12
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
15
16 /* Delay in milliseconds between the last stroke on the keyboard and the start
17  * of the live search. */
18 #define SEARCH_TIMEOUT 500
19
20 enum
21 {
22   PROP_MODEL = 1,
23   PROP_SHOW_OFFLINE,
24   PROP_SHOW_GROUPS,
25   PROP_EMPTY,
26   N_PROPS
27 };
28
29 enum
30 {
31   SIG_INDIVIDUAL_ACTIVATED,
32   SIG_POPUP_INDIVIDUAL_MENU,
33   SIG_EVENT_ACTIVATED,
34   SIG_INDIVIDUAL_TOOLTIP,
35   LAST_SIGNAL
36 };
37
38 static guint signals[LAST_SIGNAL];
39
40 #define NO_GROUP "X-no-group"
41
42 struct _EmpathyRosterViewPriv
43 {
44   /* FolksIndividual (borrowed) -> GHashTable (
45    * (gchar * group_name) -> EmpathyRosterContact (borrowed))
46    *
47    * When not using groups, this hash just have one element mapped
48    * from the special NO_GROUP key. We could use it as a set but
49    * I prefer to stay coherent in the way this hash is managed.
50    */
51   GHashTable *roster_contacts;
52   /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
53   GHashTable *roster_groups;
54   /* Hash of the EmpathyRosterContact currently displayed */
55   GHashTable *displayed_contacts;
56
57   guint last_event_id;
58   /* queue of (Event *). The most recent events are in the head of the queue
59    * so we always display the icon of the oldest one. */
60   GQueue *events;
61   guint flash_id;
62   gboolean display_flash_event;
63
64   guint search_id;
65
66   gboolean show_offline;
67   gboolean show_groups;
68   gboolean empty;
69
70   TpawLiveSearch *search;
71
72   EmpathyRosterModel *model;
73 };
74
75 /* Prototypes to break cycles */
76 static void remove_from_group (EmpathyRosterView *self,
77     FolksIndividual *individual,
78     const gchar *group);
79
80 typedef struct
81 {
82   guint id;
83   FolksIndividual *individual;
84   gchar *icon;
85   gpointer user_data;
86 } Event;
87
88 static Event *
89 event_new (guint id,
90     FolksIndividual *individual,
91     const gchar *icon,
92     gpointer user_data)
93 {
94   Event *event = g_slice_new (Event);
95
96   event->id = id;
97   event->individual = g_object_ref (individual);
98   event->icon = g_strdup (icon);
99   event->user_data = user_data;
100   return event;
101 }
102
103 static void
104 event_free (gpointer data)
105 {
106   Event *event = data;
107   g_object_unref (event->individual);
108   g_free (event->icon);
109
110   g_slice_free (Event, event);
111 }
112
113 static void
114 empathy_roster_view_get_property (GObject *object,
115     guint property_id,
116     GValue *value,
117     GParamSpec *pspec)
118 {
119   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
120
121   switch (property_id)
122     {
123       case PROP_MODEL:
124         g_value_set_object (value, self->priv->model);
125         break;
126       case PROP_SHOW_OFFLINE:
127         g_value_set_boolean (value, self->priv->show_offline);
128         break;
129       case PROP_SHOW_GROUPS:
130         g_value_set_boolean (value, self->priv->show_groups);
131         break;
132       case PROP_EMPTY:
133         g_value_set_boolean (value, self->priv->empty);
134         break;
135       default:
136         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
137         break;
138     }
139 }
140
141 static void
142 empathy_roster_view_set_property (GObject *object,
143     guint property_id,
144     const GValue *value,
145     GParamSpec *pspec)
146 {
147   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
148
149   switch (property_id)
150     {
151       case PROP_MODEL:
152         g_assert (self->priv->model == NULL);
153         self->priv->model = g_value_dup_object (value);
154         break;
155       case PROP_SHOW_OFFLINE:
156         empathy_roster_view_show_offline (self, g_value_get_boolean (value));
157         break;
158       case PROP_SHOW_GROUPS:
159         empathy_roster_view_show_groups (self, g_value_get_boolean (value));
160         break;
161       default:
162         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163         break;
164     }
165 }
166
167 static void
168 roster_contact_changed_cb (GtkListBoxRow *child,
169     GParamSpec *spec,
170     EmpathyRosterView *self)
171 {
172   gtk_list_box_row_changed (child);
173 }
174
175 static GtkWidget *
176 add_roster_contact (EmpathyRosterView *self,
177     FolksIndividual *individual,
178     const gchar *group)
179 {
180   GtkWidget *contact;
181
182   contact = empathy_roster_contact_new (individual, group);
183
184   /* Need to refilter if online is changed */
185   g_signal_connect (contact, "notify::online",
186       G_CALLBACK (roster_contact_changed_cb), self);
187
188   /* Need to resort if alias is changed */
189   g_signal_connect (contact, "notify::alias",
190       G_CALLBACK (roster_contact_changed_cb), self);
191
192   /* Need to resort if most recent event changed */
193   g_signal_connect (contact, "notify::most-recent-event",
194       G_CALLBACK (roster_contact_changed_cb), self);
195
196   gtk_widget_show (contact);
197   gtk_container_add (GTK_CONTAINER (self), contact);
198
199   return contact;
200 }
201
202 static void
203 group_expanded_cb (GtkWidget *expander,
204     GParamSpec *spec,
205     EmpathyRosterGroup *group)
206 {
207   GList *widgets, *l;
208
209   widgets = empathy_roster_group_get_widgets (group);
210   for (l = widgets; l != NULL; l = g_list_next (l))
211     {
212       gtk_list_box_row_changed (l->data);
213     }
214
215   g_list_free (widgets);
216
217   empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
218       gtk_expander_get_expanded (group->expander));
219 }
220
221 static EmpathyRosterGroup *
222 lookup_roster_group (EmpathyRosterView *self,
223     const gchar *group)
224 {
225   return g_hash_table_lookup (self->priv->roster_groups, group);
226 }
227
228 static EmpathyRosterGroup *
229 ensure_roster_group (EmpathyRosterView *self,
230     const gchar *group)
231 {
232   GtkWidget *roster_group;
233
234   roster_group = (GtkWidget *) lookup_roster_group (self, group);
235   if (roster_group != NULL)
236     return EMPATHY_ROSTER_GROUP (roster_group);
237
238   if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
239     roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
240   else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
241     roster_group = empathy_roster_group_new (group, "im-local-xmpp");
242   else
243     roster_group = empathy_roster_group_new (group, NULL);
244
245   gtk_expander_set_expanded (EMPATHY_ROSTER_GROUP (roster_group)->expander,
246       empathy_contact_group_get_expanded (group));
247
248   g_signal_connect (EMPATHY_ROSTER_GROUP (roster_group)->expander,
249       "notify::expanded", G_CALLBACK (group_expanded_cb), roster_group);
250
251   gtk_widget_show (roster_group);
252   gtk_container_add (GTK_CONTAINER (self), roster_group);
253
254   g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
255       roster_group);
256
257   return EMPATHY_ROSTER_GROUP (roster_group);
258 }
259
260 static void
261 update_empty (EmpathyRosterView *self,
262     gboolean empty)
263 {
264   if (self->priv->empty == empty)
265     return;
266
267   self->priv->empty = empty;
268   g_object_notify (G_OBJECT (self), "empty");
269 }
270
271 static gboolean filter_group (EmpathyRosterView *self,
272     EmpathyRosterGroup *group);
273
274 static gboolean
275 at_least_one_group_displayed (EmpathyRosterView *self)
276 {
277   GHashTableIter iter;
278   gpointer v;
279
280   g_hash_table_iter_init (&iter, self->priv->roster_groups);
281   while (g_hash_table_iter_next (&iter, NULL, &v))
282     {
283       EmpathyRosterGroup *group = EMPATHY_ROSTER_GROUP (v);
284
285       if (filter_group (self, group))
286         return TRUE;
287     }
288
289   return FALSE;
290 }
291
292 static void
293 check_if_empty (EmpathyRosterView *self)
294 {
295   /* Roster is considered as empty if there is no contact *and* no group
296    * currently displayed. */
297   if (g_hash_table_size (self->priv->displayed_contacts) != 0 ||
298       at_least_one_group_displayed (self))
299     {
300       update_empty (self, FALSE);
301       return;
302     }
303
304   update_empty (self, TRUE);
305 }
306
307 static void
308 update_group_widgets (EmpathyRosterView *self,
309     EmpathyRosterGroup *group,
310     EmpathyRosterContact *contact,
311     gboolean add)
312 {
313   guint old_count, count;
314
315   old_count = empathy_roster_group_get_widgets_count (group);
316
317   if (add)
318     count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
319   else
320     count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
321
322   if (count != old_count)
323     {
324       gtk_list_box_row_changed (GTK_LIST_BOX_ROW (group));
325
326       check_if_empty (self);
327     }
328 }
329
330 static void
331 add_to_group (EmpathyRosterView *self,
332     FolksIndividual *individual,
333     const gchar *group)
334 {
335   GtkWidget *contact;
336   GHashTable *contacts;
337   EmpathyRosterGroup *roster_group = NULL;
338
339   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
340   if (contacts == NULL)
341     return;
342
343   if (g_hash_table_lookup (contacts, group) != NULL)
344     return;
345
346   if (tp_strdiff (group, NO_GROUP))
347     roster_group = ensure_roster_group (self, group);
348
349   contact = add_roster_contact (self, individual, group);
350   g_hash_table_insert (contacts, g_strdup (group), contact);
351
352   if (roster_group != NULL)
353     {
354       update_group_widgets (self, roster_group,
355           EMPATHY_ROSTER_CONTACT (contact), TRUE);
356     }
357
358   if (tp_strdiff (group, NO_GROUP) &&
359       tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED) &&
360       g_hash_table_size (contacts) == 2 /* 1:Ungrouped and 2:first group */)
361     {
362       remove_from_group (self, individual,
363           EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
364     }
365 }
366
367 static void
368 individual_favourite_change_cb (FolksIndividual *individual,
369     GParamSpec *spec,
370     EmpathyRosterView *self)
371 {
372   /* We may have to refilter the contact as only favorite contacts are always
373    * displayed regardless of their presence. */
374   GHashTable *contacts;
375   GtkWidget *contact;
376
377   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
378   if (contacts == NULL)
379     return;
380
381   if (self->priv->show_groups)
382     contact = g_hash_table_lookup (contacts,
383         EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
384   else
385     contact = g_hash_table_lookup (contacts, NO_GROUP);
386
387   if (contact == NULL)
388     return;
389
390   gtk_list_box_row_changed (GTK_LIST_BOX_ROW (contact));
391 }
392
393 static void
394 individual_added (EmpathyRosterView *self,
395     FolksIndividual *individual)
396 {
397   GHashTable *contacts;
398
399   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
400   if (contacts != NULL)
401     return;
402
403   contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
404
405   g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
406
407   if (!self->priv->show_groups)
408     {
409       add_to_group (self, individual, NO_GROUP);
410     }
411   else
412     {
413       GList *groups, *l;
414
415       groups = empathy_roster_model_dup_groups_for_individual (self->priv->model,
416           individual);
417
418       if (g_list_length (groups) > 0)
419         {
420           for (l = groups; l != NULL; l = g_list_next (l))
421             {
422               add_to_group (self, individual, l->data);
423             }
424         }
425       else
426         {
427           /* No group, adds to Ungrouped */
428           add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
429         }
430
431       g_list_free_full (groups, g_free);
432     }
433
434   tp_g_signal_connect_object (individual, "notify::is-favourite",
435       G_CALLBACK (individual_favourite_change_cb), self, 0);
436 }
437
438 static void
439 set_event_icon_on_individual (EmpathyRosterView *self,
440     FolksIndividual *individual,
441     const gchar *icon)
442 {
443   GHashTable *contacts;
444   GHashTableIter iter;
445   gpointer v;
446
447   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
448   if (contacts == NULL)
449     return;
450
451   g_hash_table_iter_init (&iter, contacts);
452   while (g_hash_table_iter_next (&iter, NULL, &v))
453     {
454       EmpathyRosterContact *contact =v;
455
456       empathy_roster_contact_set_event_icon (contact, icon);
457     }
458 }
459
460 static void
461 flash_event (Event *event,
462     EmpathyRosterView *self)
463 {
464   set_event_icon_on_individual (self, event->individual, event->icon);
465 }
466
467 static void
468 unflash_event (Event *event,
469     EmpathyRosterView *self)
470 {
471   set_event_icon_on_individual (self, event->individual, NULL);
472 }
473
474 static gboolean
475 flash_cb (gpointer data)
476 {
477   EmpathyRosterView *self = data;
478
479   if (self->priv->display_flash_event)
480     {
481       g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
482       self->priv->display_flash_event = FALSE;
483     }
484   else
485     {
486       g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
487       self->priv->display_flash_event = TRUE;
488     }
489
490   return TRUE;
491 }
492
493 static void
494 start_flashing (EmpathyRosterView *self)
495 {
496   if (self->priv->flash_id != 0)
497     return;
498
499   self->priv->display_flash_event = TRUE;
500
501   self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
502       flash_cb, self);
503 }
504
505 static void
506 stop_flashing (EmpathyRosterView *self)
507 {
508   if (self->priv->flash_id == 0)
509     return;
510
511   g_source_remove (self->priv->flash_id);
512   self->priv->flash_id = 0;
513 }
514
515 static void
516 remove_event (EmpathyRosterView *self,
517     Event *event)
518 {
519   unflash_event (event, self);
520   g_queue_remove (self->priv->events, event);
521
522   if (g_queue_get_length (self->priv->events) == 0)
523     {
524       stop_flashing (self);
525     }
526 }
527
528 static void
529 remove_all_individual_event (EmpathyRosterView *self,
530     FolksIndividual *individual)
531 {
532   GList *l;
533
534   for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
535       l = g_list_next (l))
536     {
537       Event *event = l->data;
538
539       if (event->individual == individual)
540         {
541           remove_event (self, event);
542           return;
543         }
544     }
545 }
546
547 static void
548 individual_removed (EmpathyRosterView *self,
549     FolksIndividual *individual)
550 {
551   GHashTable *contacts;
552   GHashTableIter iter;
553   gpointer key, value;
554
555   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
556   if (contacts == NULL)
557     return;
558
559   remove_all_individual_event (self, individual);
560
561   g_hash_table_iter_init (&iter, contacts);
562   while (g_hash_table_iter_next (&iter, &key, &value))
563     {
564       const gchar *group_name = key;
565       GtkWidget *contact = value;
566       EmpathyRosterGroup *group;
567
568       group = lookup_roster_group (self, group_name);
569       if (group != NULL)
570         {
571           update_group_widgets (self, group,
572               EMPATHY_ROSTER_CONTACT (contact), FALSE);
573         }
574
575       gtk_container_remove (GTK_CONTAINER (self), contact);
576     }
577
578   g_hash_table_remove (self->priv->roster_contacts, individual);
579 }
580
581 static void
582 individual_added_cb (EmpathyRosterModel *model,
583     FolksIndividual *individual,
584     EmpathyRosterView *self)
585 {
586   individual_added (self, individual);
587 }
588
589 static void
590 individual_removed_cb (EmpathyRosterModel *model,
591     FolksIndividual *individual,
592     EmpathyRosterView *self)
593 {
594   individual_removed (self, individual);
595 }
596
597 static gboolean
598 contact_in_top (EmpathyRosterView *self,
599     EmpathyRosterContact *contact)
600 {
601   if (!self->priv->show_groups)
602     {
603       /* Always display top contacts in non-group mode. */
604       GList *groups;
605       FolksIndividual *individual;
606       gboolean result = FALSE;
607
608       individual = empathy_roster_contact_get_individual (contact);
609
610       groups = empathy_roster_model_dup_groups_for_individual (
611           self->priv->model, individual);
612
613       if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
614             (GCompareFunc) g_strcmp0) != NULL)
615         result = TRUE;
616
617       g_list_free_full (groups, g_free);
618
619       return result;
620     }
621
622   if (!tp_strdiff (empathy_roster_contact_get_group (contact),
623           EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
624     /* If we are displaying contacts, we only want to *always* display the
625      * RosterContact which is displayed at the top; not the ones displayed in
626      * the 'normal' group sections */
627     return TRUE;
628
629   return FALSE;
630 }
631
632 static gint
633 compare_roster_contacts_by_conversation_time (EmpathyRosterContact *a,
634     EmpathyRosterContact *b)
635 {
636   gint64 ts_a, ts_b;
637
638   ts_a = empathy_roster_contact_get_most_recent_timestamp (a);
639   ts_b = empathy_roster_contact_get_most_recent_timestamp (b);
640
641   if (ts_a == ts_b) return 0;
642   if (ts_a > ts_b) return -1;
643   return 1;
644 }
645
646 static gint
647 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
648     EmpathyRosterContact *b)
649 {
650   FolksIndividual *ind_a, *ind_b;
651   const gchar *alias_a, *alias_b;
652
653   ind_a = empathy_roster_contact_get_individual (a);
654   ind_b = empathy_roster_contact_get_individual (b);
655
656   alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
657   alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
658
659   return g_utf8_collate (alias_a, alias_b);
660 }
661
662 static gint
663 compare_roster_contacts_no_group (EmpathyRosterView *self,
664     EmpathyRosterContact *a,
665     EmpathyRosterContact *b)
666 {
667   gboolean top_a, top_b;
668
669   top_a = contact_in_top (self, a);
670   top_b = contact_in_top (self, b);
671
672   if (top_a == top_b)
673     /* Both contacts are in the top of the roster (or not). Sort them
674      * alphabetically */
675     return compare_roster_contacts_by_conversation_time (a, b);
676   else if (top_a)
677     return -1;
678   else
679     return 1;
680 }
681
682 static gint
683 compare_group_names (const gchar *group_a,
684     const gchar *group_b)
685 {
686   if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
687     return -1;
688
689   if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
690     return 1;
691
692   if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
693     return 1;
694   else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
695     return -1;
696
697   return g_utf8_collate (group_a, group_b);
698 }
699
700 static gint
701 compare_roster_contacts_with_groups (EmpathyRosterView *self,
702     EmpathyRosterContact *a,
703     EmpathyRosterContact *b)
704 {
705   const gchar *group_a, *group_b;
706
707   group_a = empathy_roster_contact_get_group (a);
708   group_b = empathy_roster_contact_get_group (b);
709
710   if (!tp_strdiff (group_a, group_b))
711     /* Same group, compare the contacts */
712     return compare_roster_contacts_by_conversation_time (a, b);
713
714   /* Sort by group */
715   return compare_group_names (group_a, group_b);
716 }
717
718 static gint
719 compare_roster_contacts (EmpathyRosterView *self,
720     EmpathyRosterContact *a,
721     EmpathyRosterContact *b)
722 {
723   if (!self->priv->show_groups)
724     return compare_roster_contacts_no_group (self, a, b);
725   else
726     return compare_roster_contacts_with_groups (self, a, b);
727 }
728
729 static gint
730 compare_roster_groups (EmpathyRosterGroup *a,
731     EmpathyRosterGroup *b)
732 {
733   const gchar *name_a, *name_b;
734
735   name_a = empathy_roster_group_get_name (a);
736   name_b = empathy_roster_group_get_name (b);
737
738   return compare_group_names (name_a, name_b);
739 }
740
741 static gint
742 compare_contact_group (EmpathyRosterContact *contact,
743     EmpathyRosterGroup *group)
744 {
745   const char *contact_group, *group_name;
746
747   contact_group = empathy_roster_contact_get_group (contact);
748   group_name = empathy_roster_group_get_name (group);
749
750   if (!tp_strdiff (contact_group, group_name))
751     /* @contact is in @group, @group has to be displayed first */
752     return 1;
753
754   /* @contact is in a different group, sort by group name */
755   return compare_group_names (contact_group, group_name);
756 }
757
758 static gint
759 roster_view_sort (GtkListBoxRow *a,
760     GtkListBoxRow *b,
761     gpointer user_data)
762 {
763   EmpathyRosterView *self = user_data;
764
765   if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
766     return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
767         EMPATHY_ROSTER_CONTACT (b));
768   else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
769     return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
770         EMPATHY_ROSTER_GROUP (b));
771   else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
772     return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
773         EMPATHY_ROSTER_GROUP (b));
774   else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
775     return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
776         EMPATHY_ROSTER_GROUP (a));
777
778   g_return_val_if_reached (0);
779 }
780
781 static void
782 update_header (GtkListBoxRow *row,
783     GtkListBoxRow *before,
784     gpointer user_data)
785 {
786   if (before == NULL)
787     {
788       /* No separator before the first row */
789       gtk_list_box_row_set_header (row, NULL);
790       return;
791     }
792
793   if (gtk_list_box_row_get_header (row) != NULL)
794     return;
795
796   gtk_list_box_row_set_header (row,
797       gtk_separator_new (GTK_ORIENTATION_HORIZONTAL));
798 }
799
800 static gboolean
801 is_searching (EmpathyRosterView *self)
802 {
803   if (self->priv->search == NULL)
804     return FALSE;
805
806   return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
807 }
808
809 static void
810 add_to_displayed (EmpathyRosterView *self,
811     EmpathyRosterContact *contact)
812 {
813   FolksIndividual *individual;
814   GHashTable *contacts;
815   GHashTableIter iter;
816   gpointer k;
817
818   if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
819     return;
820
821   g_hash_table_add (self->priv->displayed_contacts, contact);
822   update_empty (self, FALSE);
823
824   /* Groups of this contact may now be displayed if we just displays the first
825    * child in this group. */
826
827   if (!self->priv->show_groups)
828     return;
829
830   individual = empathy_roster_contact_get_individual (contact);
831   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
832   if (contacts == NULL)
833     return;
834
835   g_hash_table_iter_init (&iter, contacts);
836   while (g_hash_table_iter_next (&iter, &k, NULL))
837     {
838       const gchar *group_name = k;
839       GtkListBoxRow *group;
840
841       group = g_hash_table_lookup (self->priv->roster_groups, group_name);
842       if (group == NULL)
843         continue;
844
845       gtk_list_box_row_changed (group);
846     }
847 }
848
849 static void
850 remove_from_displayed (EmpathyRosterView *self,
851     EmpathyRosterContact *contact)
852 {
853   g_hash_table_remove (self->priv->displayed_contacts, contact);
854
855   check_if_empty (self);
856 }
857
858 static gboolean
859 contact_is_favorite (EmpathyRosterContact *contact)
860 {
861   FolksIndividual *individual;
862
863   individual = empathy_roster_contact_get_individual (contact);
864
865   return folks_favourite_details_get_is_favourite (
866       FOLKS_FAVOURITE_DETAILS (individual));
867 }
868
869 /**
870  * check if @contact should be displayed according to @self's current status
871  * and without consideration for the state of @contact's groups.
872  */
873 static gboolean
874 contact_should_be_displayed (EmpathyRosterView *self,
875     EmpathyRosterContact *contact)
876 {
877   if (is_searching (self))
878     {
879       FolksIndividual *individual;
880
881       individual = empathy_roster_contact_get_individual (contact);
882
883       return empathy_individual_match_string (individual,
884           tpaw_live_search_get_text (self->priv->search),
885           tpaw_live_search_get_words (self->priv->search));
886     }
887
888   if (self->priv->show_offline)
889       return TRUE;
890
891   if (contact_in_top (self, contact) &&
892       contact_is_favorite (contact))
893     /* Favorite top contacts are always displayed */
894     return TRUE;
895
896   return empathy_roster_contact_is_online (contact);
897 }
898
899
900 static gboolean
901 filter_contact (EmpathyRosterView *self,
902     EmpathyRosterContact *contact)
903 {
904   gboolean displayed;
905
906   displayed = contact_should_be_displayed (self, contact);
907
908   if (self->priv->show_groups)
909     {
910       const gchar *group_name;
911       EmpathyRosterGroup *group;
912
913       group_name = empathy_roster_contact_get_group (contact);
914       group = lookup_roster_group (self, group_name);
915
916       if (group != NULL)
917         {
918           /* When searching, always display even if the group is closed */
919           if (!is_searching (self) &&
920               !gtk_expander_get_expanded (group->expander))
921             displayed = FALSE;
922         }
923     }
924
925   if (displayed)
926     {
927       add_to_displayed (self, contact);
928     }
929   else
930     {
931       remove_from_displayed (self, contact);
932     }
933
934   return displayed;
935 }
936
937 static gboolean
938 filter_group (EmpathyRosterView *self,
939     EmpathyRosterGroup *group)
940 {
941   GList *widgets, *l;
942   gboolean result = FALSE;
943
944   /* Display the group if it contains at least one displayed contact */
945   widgets = empathy_roster_group_get_widgets (group);
946   for (l = widgets; l != NULL; l = g_list_next (l))
947     {
948       EmpathyRosterContact *contact = l->data;
949
950       if (contact_should_be_displayed (self, contact))
951         {
952           result = TRUE;
953           break;
954         }
955     }
956
957   g_list_free (widgets);
958
959   return result;
960 }
961
962 static gboolean
963 filter_list (GtkListBoxRow *child,
964     gpointer user_data)
965 {
966   EmpathyRosterView *self = user_data;
967
968   if (EMPATHY_IS_ROSTER_CONTACT (child))
969     return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
970
971   else if (EMPATHY_IS_ROSTER_GROUP (child))
972     return filter_group (self, EMPATHY_ROSTER_GROUP (child));
973
974   g_return_val_if_reached (FALSE);
975 }
976
977 static void
978 populate_view (EmpathyRosterView *self)
979 {
980   GList *individuals, *l;
981
982   individuals = empathy_roster_model_get_individuals (self->priv->model);
983   for (l = individuals; l != NULL; l = g_list_next (l))
984     {
985       FolksIndividual *individual = l->data;
986
987       individual_added (self, individual);
988     }
989
990   g_list_free (individuals);
991 }
992
993 static void
994 remove_from_group (EmpathyRosterView *self,
995     FolksIndividual *individual,
996     const gchar *group)
997 {
998   GHashTable *contacts;
999   GtkWidget *contact;
1000   EmpathyRosterGroup *roster_group;
1001
1002   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1003   if (contacts == NULL)
1004     return;
1005
1006   contact = g_hash_table_lookup (contacts, group);
1007   if (contact == NULL)
1008     return;
1009
1010   g_hash_table_remove (contacts, group);
1011
1012   if (g_hash_table_size (contacts) == 0)
1013     {
1014       add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
1015     }
1016
1017   roster_group = lookup_roster_group (self, group);
1018
1019   if (roster_group != NULL)
1020     {
1021       update_group_widgets (self, roster_group,
1022           EMPATHY_ROSTER_CONTACT (contact), FALSE);
1023     }
1024
1025   gtk_container_remove (GTK_CONTAINER (self), contact);
1026 }
1027
1028 static void
1029 groups_changed_cb (EmpathyRosterModel *model,
1030     FolksIndividual *individual,
1031     const gchar *group,
1032     gboolean is_member,
1033     EmpathyRosterView *self)
1034 {
1035   if (!self->priv->show_groups)
1036     {
1037       gtk_list_box_invalidate_sort (GTK_LIST_BOX (self));
1038       return;
1039     }
1040
1041   if (is_member)
1042     {
1043       add_to_group (self, individual, group);
1044     }
1045   else
1046     {
1047       remove_from_group (self, individual, group);
1048     }
1049 }
1050
1051 static void
1052 empathy_roster_view_constructed (GObject *object)
1053 {
1054   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1055   void (*chain_up) (GObject *) =
1056       ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1057
1058   if (chain_up != NULL)
1059     chain_up (object);
1060
1061   g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1062
1063   /* Get saved group states. */
1064   empathy_contact_groups_get_all ();
1065
1066   populate_view (self);
1067
1068   tp_g_signal_connect_object (self->priv->model, "individual-added",
1069       G_CALLBACK (individual_added_cb), self, 0);
1070   tp_g_signal_connect_object (self->priv->model, "individual-removed",
1071       G_CALLBACK (individual_removed_cb), self, 0);
1072   tp_g_signal_connect_object (self->priv->model, "groups-changed",
1073       G_CALLBACK (groups_changed_cb), self, 0);
1074
1075   gtk_list_box_set_sort_func (GTK_LIST_BOX (self),
1076       roster_view_sort, self, NULL);
1077
1078   gtk_list_box_set_header_func (GTK_LIST_BOX (self), update_header, self, NULL);
1079
1080   gtk_list_box_set_filter_func (GTK_LIST_BOX (self), filter_list, self, NULL);
1081
1082   gtk_list_box_set_activate_on_single_click (GTK_LIST_BOX (self), FALSE);
1083 }
1084
1085 static void
1086 clear_view (EmpathyRosterView *self)
1087 {
1088   g_hash_table_remove_all (self->priv->roster_contacts);
1089   g_hash_table_remove_all (self->priv->roster_groups);
1090   g_hash_table_remove_all (self->priv->displayed_contacts);
1091
1092   gtk_container_foreach (GTK_CONTAINER (self),
1093       (GtkCallback) gtk_widget_destroy, NULL);
1094 }
1095
1096 static void
1097 empathy_roster_view_dispose (GObject *object)
1098 {
1099   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1100   void (*chain_up) (GObject *) =
1101       ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1102
1103   /* Start by clearing the view so our internal hash tables are cleared from
1104    * objects being destroyed. */
1105   clear_view (self);
1106
1107   stop_flashing (self);
1108
1109   empathy_roster_view_set_live_search (self, NULL);
1110   g_clear_object (&self->priv->model);
1111
1112   if (self->priv->search_id != 0)
1113     {
1114       g_source_remove (self->priv->search_id);
1115       self->priv->search_id = 0;
1116     }
1117
1118   if (chain_up != NULL)
1119     chain_up (object);
1120 }
1121
1122 static void
1123 empathy_roster_view_finalize (GObject *object)
1124 {
1125   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1126   void (*chain_up) (GObject *) =
1127       ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1128
1129   g_hash_table_unref (self->priv->roster_contacts);
1130   g_hash_table_unref (self->priv->roster_groups);
1131   g_hash_table_unref (self->priv->displayed_contacts);
1132   g_queue_free_full (self->priv->events, event_free);
1133
1134   if (chain_up != NULL)
1135     chain_up (object);
1136 }
1137
1138 static void
1139 empathy_roster_view_row_activated (GtkListBox *box,
1140     GtkListBoxRow *row)
1141 {
1142   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1143   EmpathyRosterContact *contact;
1144   FolksIndividual *individual;
1145   GList *l;
1146
1147   if (!EMPATHY_IS_ROSTER_CONTACT (row))
1148     return;
1149
1150   contact = EMPATHY_ROSTER_CONTACT (row);
1151   individual = empathy_roster_contact_get_individual (contact);
1152
1153   /* Activate the oldest event associated with this contact, if any */
1154   for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1155       l = g_list_previous (l))
1156     {
1157       Event *event = l->data;
1158
1159       if (event->individual == individual)
1160         {
1161           g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1162               event->user_data);
1163           return;
1164         }
1165     }
1166
1167   g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1168 }
1169
1170 static void
1171 fire_popup_individual_menu (EmpathyRosterView *self,
1172     GtkListBoxRow *row,
1173     guint button,
1174     guint time)
1175 {
1176   EmpathyRosterContact *contact;
1177   FolksIndividual *individual;
1178   const gchar *active_group;
1179
1180   if (!EMPATHY_IS_ROSTER_CONTACT (row))
1181     return;
1182
1183   contact = EMPATHY_ROSTER_CONTACT (row);
1184   individual = empathy_roster_contact_get_individual (contact);
1185
1186   active_group = empathy_roster_contact_get_group (contact);
1187   g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1188       active_group, individual, button, time);
1189 }
1190
1191 static gboolean
1192 empathy_roster_view_button_press_event (GtkWidget *widget,
1193     GdkEventButton *event)
1194 {
1195   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1196   gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1197       ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1198
1199   if (event->button == 3)
1200     {
1201       GtkListBoxRow *row;
1202
1203       row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), event->y);
1204
1205       if (row != NULL)
1206         {
1207           gtk_list_box_select_row (GTK_LIST_BOX (self), row);
1208
1209           fire_popup_individual_menu (self, row, event->button, event->time);
1210         }
1211     }
1212
1213   return chain_up (widget, event);
1214 }
1215
1216 static gboolean
1217 empathy_roster_view_key_press_event (GtkWidget *widget,
1218     GdkEventKey *event)
1219 {
1220   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1221   gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1222       ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1223
1224   if (event->keyval == GDK_KEY_Menu)
1225     {
1226       GtkListBoxRow *row;
1227
1228       row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
1229
1230       if (row != NULL)
1231         fire_popup_individual_menu (self, row, 0, event->time);
1232     }
1233
1234   return chain_up (widget, event);
1235 }
1236
1237 /**
1238  * @out_row: (out) (allow-none)
1239  */
1240 FolksIndividual *
1241 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1242     gint y,
1243     GtkListBoxRow **out_row)
1244 {
1245   GtkListBoxRow *row;
1246
1247   row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
1248
1249   if (out_row != NULL)
1250     *out_row = row;
1251
1252   if (!EMPATHY_IS_ROSTER_CONTACT (row))
1253     return NULL;
1254
1255   return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));
1256 }
1257
1258 const gchar *
1259 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1260     gint y)
1261 {
1262   GtkListBoxRow *row;
1263
1264   row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
1265
1266   if (EMPATHY_IS_ROSTER_CONTACT (row))
1267     return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (row));
1268   else if (EMPATHY_IS_ROSTER_GROUP (row))
1269     return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (row));
1270
1271   return NULL;
1272 }
1273
1274 static gboolean
1275 empathy_roster_view_query_tooltip (GtkWidget *widget,
1276     gint x,
1277     gint y,
1278     gboolean keyboard_mode,
1279     GtkTooltip *tooltip)
1280 {
1281   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1282   FolksIndividual *individual;
1283   gboolean result;
1284   GtkListBoxRow *row;
1285
1286   individual = empathy_roster_view_get_individual_at_y (self, y, &row);
1287   if (individual == NULL)
1288     return FALSE;
1289
1290   g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1291       individual, keyboard_mode, tooltip, &result);
1292
1293   if (result)
1294     {
1295       GtkAllocation allocation;
1296
1297       gtk_widget_get_allocation (GTK_WIDGET (row), &allocation);
1298       gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1299     }
1300
1301   return result;
1302 }
1303
1304 static void
1305 empathy_roster_view_remove (GtkContainer *container,
1306     GtkWidget *widget)
1307 {
1308   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1309   void (*chain_up) (GtkContainer *, GtkWidget *) =
1310       ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1311
1312   chain_up (container, widget);
1313
1314   if (EMPATHY_IS_ROSTER_CONTACT (widget))
1315     remove_from_displayed (self, (EmpathyRosterContact *) widget);
1316 }
1317
1318 static void
1319 empathy_roster_view_class_init (
1320     EmpathyRosterViewClass *klass)
1321 {
1322   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1323   GtkListBoxClass *box_class = GTK_LIST_BOX_CLASS (klass);
1324   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1325   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1326   GParamSpec *spec;
1327
1328   oclass->get_property = empathy_roster_view_get_property;
1329   oclass->set_property = empathy_roster_view_set_property;
1330   oclass->constructed = empathy_roster_view_constructed;
1331   oclass->dispose = empathy_roster_view_dispose;
1332   oclass->finalize = empathy_roster_view_finalize;
1333
1334   widget_class->button_press_event = empathy_roster_view_button_press_event;
1335   widget_class->key_press_event = empathy_roster_view_key_press_event;
1336   widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1337
1338   container_class->remove = empathy_roster_view_remove;
1339
1340   box_class->row_activated = empathy_roster_view_row_activated;
1341
1342   spec = g_param_spec_object ("model", "Model",
1343       "EmpathyRosterModel",
1344       EMPATHY_TYPE_ROSTER_MODEL,
1345       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1346   g_object_class_install_property (oclass, PROP_MODEL, spec);
1347
1348   spec = g_param_spec_boolean ("show-offline", "Show Offline",
1349       "Show offline contacts",
1350       FALSE,
1351       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1352   g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1353
1354   spec = g_param_spec_boolean ("show-groups", "Show Groups",
1355       "Show groups",
1356       FALSE,
1357       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1358   g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1359
1360   spec = g_param_spec_boolean ("empty", "Empty",
1361       "Is the view currently empty?",
1362       FALSE,
1363       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1364   g_object_class_install_property (oclass, PROP_EMPTY, spec);
1365
1366   signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1367       G_OBJECT_CLASS_TYPE (klass),
1368       G_SIGNAL_RUN_LAST,
1369       0, NULL, NULL, NULL,
1370       G_TYPE_NONE,
1371       1, FOLKS_TYPE_INDIVIDUAL);
1372
1373   signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1374       G_OBJECT_CLASS_TYPE (klass),
1375       G_SIGNAL_RUN_LAST,
1376       0, NULL, NULL, NULL,
1377       G_TYPE_NONE,
1378       4, G_TYPE_STRING, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT,
1379           G_TYPE_UINT);
1380
1381   signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1382       G_OBJECT_CLASS_TYPE (klass),
1383       G_SIGNAL_RUN_LAST,
1384       0, NULL, NULL, NULL,
1385       G_TYPE_NONE,
1386       2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1387
1388   signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1389       G_OBJECT_CLASS_TYPE (klass),
1390       G_SIGNAL_RUN_LAST,
1391       0, g_signal_accumulator_true_handled, NULL, NULL,
1392       G_TYPE_BOOLEAN,
1393       3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1394
1395   g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1396 }
1397
1398 static void
1399 empathy_roster_view_init (EmpathyRosterView *self)
1400 {
1401   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1402       EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1403
1404   self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1405       NULL, (GDestroyNotify) g_hash_table_unref);
1406   self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1407       g_free, NULL);
1408   self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1409
1410   self->priv->events = g_queue_new ();
1411
1412   self->priv->empty = TRUE;
1413 }
1414
1415 GtkWidget *
1416 empathy_roster_view_new (EmpathyRosterModel *model)
1417 {
1418   g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1419
1420   return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1421       "model", model,
1422       NULL);
1423 }
1424
1425 void
1426 empathy_roster_view_show_offline (EmpathyRosterView *self,
1427     gboolean show)
1428 {
1429   if (self->priv->show_offline == show)
1430     return;
1431
1432   self->priv->show_offline = show;
1433   gtk_list_box_invalidate_filter (GTK_LIST_BOX (self));
1434
1435   g_object_notify (G_OBJECT (self), "show-offline");
1436 }
1437
1438 void
1439 empathy_roster_view_show_groups (EmpathyRosterView *self,
1440     gboolean show)
1441 {
1442   if (self->priv->show_groups == show)
1443     return;
1444
1445   self->priv->show_groups = show;
1446
1447   /* TODO: block sort/filter? */
1448   clear_view (self);
1449   populate_view (self);
1450
1451   g_object_notify (G_OBJECT (self), "show-groups");
1452 }
1453
1454 static void
1455 select_first_contact (EmpathyRosterView *self)
1456 {
1457   GList *children, *l;
1458
1459   children = gtk_container_get_children (GTK_CONTAINER (self));
1460   for (l = children; l != NULL; l = g_list_next (l))
1461     {
1462       GtkWidget *child = l->data;
1463
1464       if (!gtk_widget_get_child_visible (child))
1465         continue;
1466
1467       if (!EMPATHY_IS_ROSTER_CONTACT (child))
1468         continue;
1469
1470       gtk_list_box_select_row (GTK_LIST_BOX (self), GTK_LIST_BOX_ROW (child));
1471       break;
1472     }
1473
1474   g_list_free (children);
1475 }
1476
1477 static gboolean
1478 search_timeout_cb (EmpathyRosterView *self)
1479 {
1480   gtk_list_box_invalidate_filter (GTK_LIST_BOX (self));
1481
1482   select_first_contact (self);
1483
1484   self->priv->search_id = 0;
1485   return G_SOURCE_REMOVE;
1486 }
1487
1488 static void
1489 search_text_notify_cb (TpawLiveSearch *search,
1490     GParamSpec *pspec,
1491     EmpathyRosterView *self)
1492 {
1493   if (self->priv->search_id != 0)
1494     g_source_remove (self->priv->search_id);
1495
1496   self->priv->search_id = g_timeout_add (SEARCH_TIMEOUT,
1497       (GSourceFunc) search_timeout_cb, self);
1498 }
1499
1500 static void
1501 search_activate_cb (GtkWidget *search,
1502   EmpathyRosterView *self)
1503 {
1504   GtkListBox *box = GTK_LIST_BOX (self);
1505   GtkListBoxRow *row;
1506
1507   row = gtk_list_box_get_selected_row (box);
1508   if (row == NULL)
1509     return;
1510
1511   empathy_roster_view_row_activated (box, row);
1512 }
1513
1514 void
1515 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1516     TpawLiveSearch *search)
1517 {
1518   if (self->priv->search != NULL)
1519     {
1520       g_signal_handlers_disconnect_by_func (self->priv->search,
1521           search_text_notify_cb, self);
1522       g_signal_handlers_disconnect_by_func (self->priv->search,
1523           search_activate_cb, self);
1524
1525       g_clear_object (&self->priv->search);
1526     }
1527
1528   if (search == NULL)
1529     return;
1530
1531   self->priv->search = g_object_ref (search);
1532
1533   g_signal_connect (self->priv->search, "notify::text",
1534       G_CALLBACK (search_text_notify_cb), self);
1535   g_signal_connect (self->priv->search, "activate",
1536       G_CALLBACK (search_activate_cb), self);
1537 }
1538
1539 gboolean
1540 empathy_roster_view_is_empty (EmpathyRosterView *self)
1541 {
1542   return self->priv->empty;
1543 }
1544
1545 gboolean
1546 empathy_roster_view_is_searching (EmpathyRosterView *self)
1547 {
1548   return (self->priv->search != NULL &&
1549       gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1550 }
1551
1552 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1553  * Empathy's internals. */
1554 guint
1555 empathy_roster_view_add_event (EmpathyRosterView *self,
1556     FolksIndividual *individual,
1557     const gchar *icon,
1558     gpointer user_data)
1559 {
1560   GHashTable *contacts;
1561
1562   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1563   if (contacts == NULL)
1564     return 0;
1565
1566   self->priv->last_event_id++;
1567
1568   g_queue_push_head (self->priv->events,
1569       event_new (self->priv->last_event_id, individual, icon, user_data));
1570
1571   start_flashing (self);
1572
1573   return self->priv->last_event_id;
1574 }
1575
1576 void
1577 empathy_roster_view_remove_event (EmpathyRosterView *self,
1578     guint event_id)
1579 {
1580   GList *l;
1581
1582   for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1583       l = g_list_next (l))
1584     {
1585       Event *event = l->data;
1586
1587       if (event->id == event_id)
1588         {
1589           remove_event (self, event);
1590           return;
1591         }
1592     }
1593 }
1594
1595 FolksIndividual *
1596 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1597 {
1598   GtkListBoxRow *row;
1599
1600   row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
1601
1602   if (!EMPATHY_IS_ROSTER_CONTACT (row))
1603     return NULL;
1604
1605   return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));
1606 }