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