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