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