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