]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-roster-view.c
Removed the manager from the view, relying only on the 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_VIEW_GROUP_TOP_GROUP))
223     roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
224   else if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_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_VIEW_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_VIEW_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 gboolean
514 contact_is_favourite (EmpathyRosterContact *contact)
515 {
516   FolksIndividual *individual;
517
518   individual = empathy_roster_contact_get_individual (contact);
519
520   return folks_favourite_details_get_is_favourite (
521       FOLKS_FAVOURITE_DETAILS (individual));
522 }
523
524 static gboolean
525 contact_in_top (EmpathyRosterView *self,
526     EmpathyRosterContact *contact)
527 {
528   FolksIndividual *individual;
529   GList *tops;
530
531   if (contact_is_favourite (contact))
532     return TRUE;
533
534   individual = empathy_roster_contact_get_individual (contact);
535
536   tops = empathy_roster_model_get_top_individuals (self->priv->model);
537
538   if (g_list_index (tops, individual) != -1)
539     return TRUE;
540
541   return FALSE;
542 }
543
544 static gint
545 compare_roster_contacts_no_group (EmpathyRosterView *self,
546     EmpathyRosterContact *a,
547     EmpathyRosterContact *b)
548 {
549   gboolean top_a, top_b;
550
551   top_a = contact_in_top (self, a);
552   top_b = contact_in_top (self, b);
553
554   if (top_a == top_b)
555     /* Both contacts are in the top of the roster (or not). Sort them
556      * alphabetically */
557     return compare_roster_contacts_by_alias (a, b);
558   else if (top_a)
559     return -1;
560   else
561     return 1;
562 }
563
564 static gint
565 compare_group_names (const gchar *group_a,
566     const gchar *group_b)
567 {
568   if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
569     return -1;
570
571   if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
572     return 1;
573
574   if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
575     return 1;
576   else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
577     return -1;
578
579   return g_ascii_strcasecmp (group_a, group_b);
580 }
581
582 static gint
583 compare_roster_contacts_with_groups (EmpathyRosterView *self,
584     EmpathyRosterContact *a,
585     EmpathyRosterContact *b)
586 {
587   const gchar *group_a, *group_b;
588
589   group_a = empathy_roster_contact_get_group (a);
590   group_b = empathy_roster_contact_get_group (b);
591
592   if (!tp_strdiff (group_a, group_b))
593     /* Same group, compare the contacts */
594     return compare_roster_contacts_by_alias (a, b);
595
596   /* Sort by group */
597   return compare_group_names (group_a, group_b);
598 }
599
600 static gint
601 compare_roster_contacts (EmpathyRosterView *self,
602     EmpathyRosterContact *a,
603     EmpathyRosterContact *b)
604 {
605   if (!self->priv->show_groups)
606     return compare_roster_contacts_no_group (self, a, b);
607   else
608     return compare_roster_contacts_with_groups (self, a, b);
609 }
610
611 static gint
612 compare_roster_groups (EmpathyRosterGroup *a,
613     EmpathyRosterGroup *b)
614 {
615   const gchar *name_a, *name_b;
616
617   name_a = empathy_roster_group_get_name (a);
618   name_b = empathy_roster_group_get_name (b);
619
620   return compare_group_names (name_a, name_b);
621 }
622
623 static gint
624 compare_contact_group (EmpathyRosterContact *contact,
625     EmpathyRosterGroup *group)
626 {
627   const char *contact_group, *group_name;
628
629   contact_group = empathy_roster_contact_get_group (contact);
630   group_name = empathy_roster_group_get_name (group);
631
632   if (!tp_strdiff (contact_group, group_name))
633     /* @contact is in @group, @group has to be displayed first */
634     return 1;
635
636   /* @contact is in a different group, sort by group name */
637   return compare_group_names (contact_group, group_name);
638 }
639
640 static gint
641 roster_view_sort (gconstpointer a,
642     gconstpointer b,
643     gpointer user_data)
644 {
645   EmpathyRosterView *self = user_data;
646
647   if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
648     return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
649         EMPATHY_ROSTER_CONTACT (b));
650   else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
651     return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
652         EMPATHY_ROSTER_GROUP (b));
653   else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
654     return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
655         EMPATHY_ROSTER_GROUP (b));
656   else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
657     return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
658         EMPATHY_ROSTER_GROUP (a));
659
660   g_return_val_if_reached (0);
661 }
662
663 static void
664 update_separator (GtkWidget **separator,
665     GtkWidget *child,
666     GtkWidget *before,
667     gpointer user_data)
668 {
669   if (before == NULL)
670     {
671       /* No separator before the first row */
672       g_clear_object (separator);
673       return;
674     }
675
676   if (*separator != NULL)
677     return;
678
679   *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
680   g_object_ref_sink (*separator);
681 }
682
683 static gboolean
684 is_searching (EmpathyRosterView *self)
685 {
686   if (self->priv->search == NULL)
687     return FALSE;
688
689   return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
690 }
691
692 static void
693 update_empty (EmpathyRosterView *self,
694     gboolean empty)
695 {
696   if (self->priv->empty == empty)
697     return;
698
699   self->priv->empty = empty;
700   g_object_notify (G_OBJECT (self), "empty");
701 }
702
703 static void
704 add_to_displayed (EmpathyRosterView *self,
705     EmpathyRosterContact *contact)
706 {
707   FolksIndividual *individual;
708   GHashTable *contacts;
709   GHashTableIter iter;
710   gpointer k;
711
712   if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
713     return;
714
715   g_hash_table_add (self->priv->displayed_contacts, contact);
716   update_empty (self, FALSE);
717
718   /* Groups of this contact may now be displayed if we just displays the first
719    * child in this group. */
720
721   if (!self->priv->show_groups)
722     return;
723
724   individual = empathy_roster_contact_get_individual (contact);
725   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
726   if (contacts == NULL)
727     return;
728
729   g_hash_table_iter_init (&iter, contacts);
730   while (g_hash_table_iter_next (&iter, &k, NULL))
731     {
732       const gchar *group_name = k;
733       GtkWidget *group;
734
735       group = g_hash_table_lookup (self->priv->roster_groups, group_name);
736       if (group == NULL)
737         continue;
738
739       egg_list_box_child_changed (EGG_LIST_BOX (self), group);
740     }
741 }
742
743 static void
744 remove_from_displayed (EmpathyRosterView *self,
745     EmpathyRosterContact *contact)
746 {
747   g_hash_table_remove (self->priv->displayed_contacts, contact);
748
749   if (g_hash_table_size (self->priv->displayed_contacts) == 0)
750     update_empty (self, TRUE);
751 }
752
753 /**
754  * check if @contact should be displayed according to @self's current status
755  * and without consideration for the state of @contact's groups.
756  */
757 static gboolean
758 contact_should_be_displayed (EmpathyRosterView *self,
759     EmpathyRosterContact *contact)
760 {
761   if (is_searching (self))
762     {
763       FolksIndividual *individual;
764
765       individual = empathy_roster_contact_get_individual (contact);
766
767       return empathy_individual_match_string (individual,
768           empathy_live_search_get_text (self->priv->search),
769           empathy_live_search_get_words (self->priv->search));
770     }
771
772   if (self->priv->show_offline)
773       return TRUE;
774
775   if (contact_is_favourite (contact))
776     {
777       const gchar *group_name;
778
779       if (!self->priv->show_groups)
780         /* Always display favourite contacts in non-group mode. */
781         return TRUE;
782
783       group_name = empathy_roster_contact_get_group (contact);
784
785       if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
786         /* Always display favourite contact in group mode only in the
787          * 'top group'*/
788         return TRUE;
789     }
790
791   return empathy_roster_contact_is_online (contact);
792 }
793
794
795 static gboolean
796 filter_contact (EmpathyRosterView *self,
797     EmpathyRosterContact *contact)
798 {
799   gboolean displayed;
800
801   displayed = contact_should_be_displayed (self, contact);
802
803   if (self->priv->show_groups)
804     {
805       const gchar *group_name;
806       EmpathyRosterGroup *group;
807
808       group_name = empathy_roster_contact_get_group (contact);
809       group = lookup_roster_group (self, group_name);
810
811       if (group != NULL)
812         {
813           /* When searching, always display even if the group is closed */
814           if (!is_searching (self) &&
815               !gtk_expander_get_expanded (GTK_EXPANDER (group)))
816             displayed = FALSE;
817         }
818     }
819
820   if (displayed)
821     {
822       add_to_displayed (self, contact);
823     }
824   else
825     {
826       remove_from_displayed (self, contact);
827     }
828
829   return displayed;
830 }
831
832 static gboolean
833 filter_group (EmpathyRosterView *self,
834     EmpathyRosterGroup *group)
835 {
836   GList *widgets, *l;
837
838   /* Display the group if it contains at least one displayed contact */
839   widgets = empathy_roster_group_get_widgets (group);
840   for (l = widgets; l != NULL; l = g_list_next (l))
841     {
842       EmpathyRosterContact *contact = l->data;
843
844       if (contact_should_be_displayed (self, contact))
845         return TRUE;
846     }
847
848   return FALSE;
849 }
850
851 static gboolean
852 filter_list (GtkWidget *child,
853     gpointer user_data)
854 {
855   EmpathyRosterView *self = user_data;
856
857   if (EMPATHY_IS_ROSTER_CONTACT (child))
858     return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
859
860   else if (EMPATHY_IS_ROSTER_GROUP (child))
861     return filter_group (self, EMPATHY_ROSTER_GROUP (child));
862
863   g_return_val_if_reached (FALSE);
864 }
865
866 /* @list: GList of EmpathyRosterContact
867  *
868  * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
869  * @individual */
870 static gboolean
871 individual_in_list (FolksIndividual *individual,
872     GList *list)
873 {
874   GList *l;
875
876   for (l = list; l != NULL; l = g_list_next (l))
877     {
878       EmpathyRosterContact *contact = l->data;
879
880       if (empathy_roster_contact_get_individual (contact) == individual)
881         return TRUE;
882     }
883
884   return FALSE;
885 }
886
887 static void
888 populate_view (EmpathyRosterView *self)
889 {
890   GList *individuals, *l;
891
892   individuals = empathy_roster_model_get_individuals (self->priv->model);
893   for (l = individuals; l != NULL; l = g_list_next (l))
894     {
895       FolksIndividual *individual = l->data;
896
897       individual_added (self, individual);
898     }
899
900   g_list_free (individuals);
901 }
902
903 static void
904 remove_from_group (EmpathyRosterView *self,
905     FolksIndividual *individual,
906     const gchar *group)
907 {
908   GHashTable *contacts;
909   GtkWidget *contact;
910   EmpathyRosterGroup *roster_group;
911
912   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
913   if (contacts == NULL)
914     return;
915
916   contact = g_hash_table_lookup (contacts, group);
917   if (contact == NULL)
918     return;
919
920   g_hash_table_remove (contacts, group);
921
922   if (g_hash_table_size (contacts) == 0)
923     {
924       add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
925     }
926
927   roster_group = lookup_roster_group (self, group);
928
929   if (roster_group != NULL)
930     {
931       update_group_widgets (self, roster_group,
932           EMPATHY_ROSTER_CONTACT (contact), FALSE);
933     }
934
935   gtk_container_remove (GTK_CONTAINER (self), contact);
936 }
937
938 static void
939 update_top_contacts (EmpathyRosterView *self)
940 {
941   GList *tops, *l;
942   GList *to_add = NULL, *to_remove = NULL;
943   EmpathyRosterGroup *group;
944
945   if (!self->priv->show_groups)
946     {
947       egg_list_box_resort (EGG_LIST_BOX (self));
948       return;
949     }
950
951   tops = empathy_roster_model_get_top_individuals (self->priv->model);
952
953   group = g_hash_table_lookup (self->priv->roster_groups,
954       EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
955   if (group == NULL)
956     {
957       to_add = g_list_copy (tops);
958     }
959   else
960     {
961       GList *contacts;
962
963       contacts = empathy_roster_group_get_widgets (group);
964
965       /* Check which EmpathyRosterContact have to be removed */
966       for (l = contacts; l != NULL; l = g_list_next (l))
967         {
968           EmpathyRosterContact *contact = l->data;
969           FolksIndividual *individual;
970
971           if (contact_is_favourite (contact))
972             continue;
973
974           individual = empathy_roster_contact_get_individual (contact);
975
976           if (g_list_find (tops, individual) == NULL)
977             to_remove = g_list_prepend (to_remove, individual);
978         }
979
980       /* Check which EmpathyRosterContact have to be added */
981       for (l = tops; l != NULL; l = g_list_next (l))
982         {
983           FolksIndividual *individual = l->data;
984
985           if (!individual_in_list (individual, contacts))
986             to_add = g_list_prepend (to_add, individual);
987         }
988     }
989
990   for (l = to_add; l != NULL; l = g_list_next (l))
991     add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
992
993   for (l = to_remove; l != NULL; l = g_list_next (l))
994     remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
995
996   g_list_free (to_add);
997   g_list_free (to_remove);
998 }
999
1000 static void
1001 groups_changed_cb (EmpathyRosterModel *model,
1002     FolksIndividual *individual,
1003     const gchar *group,
1004     gboolean is_member,
1005     EmpathyRosterView *self)
1006 {
1007   if (!self->priv->show_groups)
1008     return;
1009
1010   if (is_member)
1011     {
1012       add_to_group (self, individual, group);
1013     }
1014   else
1015     {
1016       remove_from_group (self, individual, group);
1017     }
1018 }
1019
1020 static void
1021 top_individuals_changed_cb (EmpathyRosterModel *model,
1022     EmpathyRosterView *self)
1023 {
1024   update_top_contacts (self);
1025 }
1026
1027 static void
1028 favourites_changed_cb (EmpathyRosterModel *model,
1029     FolksIndividual *individual,
1030     gboolean favourite,
1031     EmpathyRosterView *self)
1032 {
1033   GHashTable *contacts;
1034
1035   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1036   if (contacts == NULL)
1037     return;
1038
1039   if (self->priv->show_groups)
1040     {
1041       if (favourite)
1042         add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1043       else
1044         remove_from_group (self, individual,
1045             EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1046     }
1047   else
1048     {
1049       GtkWidget *contact;
1050
1051       contact = g_hash_table_lookup (contacts, NO_GROUP);
1052       if (contact == NULL)
1053         return;
1054
1055       egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1056     }
1057 }
1058
1059 static void
1060 empathy_roster_view_constructed (GObject *object)
1061 {
1062   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1063   void (*chain_up) (GObject *) =
1064       ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1065
1066   if (chain_up != NULL)
1067     chain_up (object);
1068
1069   g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1070
1071   populate_view (self);
1072
1073   tp_g_signal_connect_object (self->priv->model, "individual-added",
1074       G_CALLBACK (individual_added_cb), self, 0);
1075   tp_g_signal_connect_object (self->priv->model, "individual-removed",
1076       G_CALLBACK (individual_removed_cb), self, 0);
1077   tp_g_signal_connect_object (self->priv->model, "groups-changed",
1078       G_CALLBACK (groups_changed_cb), self, 0);
1079   tp_g_signal_connect_object (self->priv->model, "top-individuals-changed",
1080       G_CALLBACK (top_individuals_changed_cb), self, 0);
1081   tp_g_signal_connect_object (self->priv->model, "favourites-changed",
1082       G_CALLBACK (favourites_changed_cb), self, 0);
1083
1084   egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1085       roster_view_sort, self, NULL);
1086
1087   egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1088       self, NULL);
1089
1090   egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1091
1092   egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1093 }
1094
1095 static void
1096 empathy_roster_view_dispose (GObject *object)
1097 {
1098   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1099   void (*chain_up) (GObject *) =
1100       ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1101
1102   stop_flashing (self);
1103
1104   empathy_roster_view_set_live_search (self, NULL);
1105   g_clear_object (&self->priv->model);
1106
1107   if (chain_up != NULL)
1108     chain_up (object);
1109 }
1110
1111 static void
1112 empathy_roster_view_finalize (GObject *object)
1113 {
1114   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1115   void (*chain_up) (GObject *) =
1116       ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1117
1118   g_hash_table_unref (self->priv->roster_contacts);
1119   g_hash_table_unref (self->priv->roster_groups);
1120   g_hash_table_unref (self->priv->displayed_contacts);
1121   g_queue_free_full (self->priv->events, event_free);
1122
1123   if (chain_up != NULL)
1124     chain_up (object);
1125 }
1126
1127 static void
1128 empathy_roster_view_child_activated (EggListBox *box,
1129     GtkWidget *child)
1130 {
1131   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1132   EmpathyRosterContact *contact;
1133   FolksIndividual *individual;
1134   GList *l;
1135
1136   if (!EMPATHY_IS_ROSTER_CONTACT (child))
1137     return;
1138
1139   contact = EMPATHY_ROSTER_CONTACT (child);
1140   individual = empathy_roster_contact_get_individual (contact);
1141
1142   /* Activate the oldest event associated with this contact, if any */
1143   for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1144       l = g_list_previous (l))
1145     {
1146       Event *event = l->data;
1147
1148       if (event->individual == individual)
1149         {
1150           g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1151               event->user_data);
1152           return;
1153         }
1154     }
1155
1156   g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1157 }
1158
1159 static void
1160 fire_popup_individual_menu (EmpathyRosterView *self,
1161     GtkWidget *child,
1162     guint button,
1163     guint time)
1164 {
1165   EmpathyRosterContact *contact;
1166   FolksIndividual *individual;
1167
1168   if (!EMPATHY_IS_ROSTER_CONTACT (child))
1169     return;
1170
1171   contact = EMPATHY_ROSTER_CONTACT (child);
1172   individual = empathy_roster_contact_get_individual (contact);
1173
1174   g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1175       individual, button, time);
1176 }
1177
1178 static gboolean
1179 empathy_roster_view_button_press_event (GtkWidget *widget,
1180     GdkEventButton *event)
1181 {
1182   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1183   gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1184       ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1185
1186   if (event->button == 3)
1187     {
1188       GtkWidget *child;
1189
1190       child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1191
1192       if (child != NULL)
1193         {
1194           egg_list_box_select_child (EGG_LIST_BOX (self), child);
1195
1196           fire_popup_individual_menu (self, child, event->button, event->time);
1197         }
1198     }
1199
1200   return chain_up (widget, event);
1201 }
1202
1203 static gboolean
1204 empathy_roster_view_key_press_event (GtkWidget *widget,
1205     GdkEventKey *event)
1206 {
1207   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1208   gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1209       ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1210
1211   if (event->keyval == GDK_KEY_Menu)
1212     {
1213       GtkWidget *child;
1214
1215       child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1216
1217       if (child != NULL)
1218         fire_popup_individual_menu (self, child, 0, event->time);
1219     }
1220
1221   return chain_up (widget, event);
1222 }
1223
1224 /**
1225  * @out_child: (out) (allow-none)
1226  */
1227 FolksIndividual *
1228 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1229     gint y,
1230     GtkWidget **out_child)
1231 {
1232   GtkWidget *child;
1233
1234   child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1235
1236   if (out_child != NULL)
1237     *out_child = child;
1238
1239   if (!EMPATHY_IS_ROSTER_CONTACT (child))
1240     return NULL;
1241
1242   return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1243 }
1244
1245 /**
1246  * @out_child: (out) (allow-none)
1247  */
1248 const gchar *
1249 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1250     gint y)
1251 {
1252   GtkWidget *child;
1253
1254   child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1255
1256   if (EMPATHY_IS_ROSTER_CONTACT (child))
1257     return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1258   else if (EMPATHY_IS_ROSTER_GROUP (child))
1259     return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1260
1261   return NULL;
1262 }
1263
1264 static gboolean
1265 empathy_roster_view_query_tooltip (GtkWidget *widget,
1266     gint x,
1267     gint y,
1268     gboolean keyboard_mode,
1269     GtkTooltip *tooltip)
1270 {
1271   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1272   FolksIndividual *individual;
1273   gboolean result;
1274   GtkWidget *child;
1275
1276   individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1277   if (individual == NULL)
1278     return FALSE;
1279
1280   g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1281       individual, keyboard_mode, tooltip, &result);
1282
1283   if (result)
1284     {
1285       GtkAllocation allocation;
1286
1287       gtk_widget_get_allocation (child, &allocation);
1288       gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1289     }
1290
1291   return result;
1292 }
1293
1294 static void
1295 empathy_roster_view_remove (GtkContainer *container,
1296     GtkWidget *widget)
1297 {
1298   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1299   void (*chain_up) (GtkContainer *, GtkWidget *) =
1300       ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1301
1302   chain_up (container, widget);
1303
1304   if (EMPATHY_IS_ROSTER_CONTACT (widget))
1305     remove_from_displayed (self, (EmpathyRosterContact *) widget);
1306 }
1307
1308 static void
1309 empathy_roster_view_class_init (
1310     EmpathyRosterViewClass *klass)
1311 {
1312   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1313   EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1314   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1315   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1316   GParamSpec *spec;
1317
1318   oclass->get_property = empathy_roster_view_get_property;
1319   oclass->set_property = empathy_roster_view_set_property;
1320   oclass->constructed = empathy_roster_view_constructed;
1321   oclass->dispose = empathy_roster_view_dispose;
1322   oclass->finalize = empathy_roster_view_finalize;
1323
1324   widget_class->button_press_event = empathy_roster_view_button_press_event;
1325   widget_class->key_press_event = empathy_roster_view_key_press_event;
1326   widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1327
1328   container_class->remove = empathy_roster_view_remove;
1329
1330   box_class->child_activated = empathy_roster_view_child_activated;
1331
1332   spec = g_param_spec_object ("model", "Model",
1333       "EmpathyRosterModel",
1334       EMPATHY_TYPE_ROSTER_MODEL,
1335       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1336   g_object_class_install_property (oclass, PROP_MODEL, spec);
1337
1338   spec = g_param_spec_boolean ("show-offline", "Show Offline",
1339       "Show offline contacts",
1340       FALSE,
1341       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1342   g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1343
1344   spec = g_param_spec_boolean ("show-groups", "Show Groups",
1345       "Show groups",
1346       FALSE,
1347       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1348   g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1349
1350   spec = g_param_spec_boolean ("empty", "Empty",
1351       "Is the view currently empty?",
1352       FALSE,
1353       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1354   g_object_class_install_property (oclass, PROP_EMPTY, spec);
1355
1356   signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1357       G_OBJECT_CLASS_TYPE (klass),
1358       G_SIGNAL_RUN_LAST,
1359       0, NULL, NULL, NULL,
1360       G_TYPE_NONE,
1361       1, FOLKS_TYPE_INDIVIDUAL);
1362
1363   signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1364       G_OBJECT_CLASS_TYPE (klass),
1365       G_SIGNAL_RUN_LAST,
1366       0, NULL, NULL, NULL,
1367       G_TYPE_NONE,
1368       3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1369
1370   signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1371       G_OBJECT_CLASS_TYPE (klass),
1372       G_SIGNAL_RUN_LAST,
1373       0, NULL, NULL, NULL,
1374       G_TYPE_NONE,
1375       2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1376
1377   signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1378       G_OBJECT_CLASS_TYPE (klass),
1379       G_SIGNAL_RUN_LAST,
1380       0, g_signal_accumulator_true_handled, NULL, NULL,
1381       G_TYPE_BOOLEAN,
1382       3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1383
1384   g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1385 }
1386
1387 static void
1388 empathy_roster_view_init (EmpathyRosterView *self)
1389 {
1390   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1391       EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1392
1393   self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1394       NULL, (GDestroyNotify) g_hash_table_unref);
1395   self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1396       g_free, NULL);
1397   self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1398
1399   self->priv->events = g_queue_new ();
1400
1401   self->priv->empty = TRUE;
1402 }
1403
1404 GtkWidget *
1405 empathy_roster_view_new (EmpathyRosterModel *model)
1406 {
1407   g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1408
1409   return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1410       "model", model,
1411       NULL);
1412 }
1413
1414 void
1415 empathy_roster_view_show_offline (EmpathyRosterView *self,
1416     gboolean show)
1417 {
1418   if (self->priv->show_offline == show)
1419     return;
1420
1421   self->priv->show_offline = show;
1422   egg_list_box_refilter (EGG_LIST_BOX (self));
1423
1424   g_object_notify (G_OBJECT (self), "show-offline");
1425 }
1426
1427 static void
1428 clear_view (EmpathyRosterView *self)
1429 {
1430   gtk_container_foreach (GTK_CONTAINER (self),
1431       (GtkCallback) gtk_widget_destroy, NULL);
1432
1433   g_hash_table_remove_all (self->priv->roster_contacts);
1434   g_hash_table_remove_all (self->priv->roster_groups);
1435   g_hash_table_remove_all (self->priv->displayed_contacts);
1436 }
1437
1438 void
1439 empathy_roster_view_show_groups (EmpathyRosterView *self,
1440     gboolean show)
1441 {
1442   if (self->priv->show_groups == show)
1443     return;
1444
1445   self->priv->show_groups = show;
1446
1447   /* TODO: block sort/filter? */
1448   clear_view (self);
1449   populate_view (self);
1450
1451   g_object_notify (G_OBJECT (self), "show-groups");
1452 }
1453
1454 static void
1455 select_first_contact (EmpathyRosterView *self)
1456 {
1457   GList *children, *l;
1458
1459   children = gtk_container_get_children (GTK_CONTAINER (self));
1460   for (l = children; l != NULL; l = g_list_next (l))
1461     {
1462       GtkWidget *child = l->data;
1463
1464       if (!gtk_widget_get_child_visible (child))
1465         continue;
1466
1467       if (!EMPATHY_IS_ROSTER_CONTACT (child))
1468         continue;
1469
1470       egg_list_box_select_child (EGG_LIST_BOX (self), child);
1471       break;
1472     }
1473
1474   g_list_free (children);
1475 }
1476
1477 static void
1478 search_text_notify_cb (EmpathyLiveSearch *search,
1479     GParamSpec *pspec,
1480     EmpathyRosterView *self)
1481 {
1482   egg_list_box_refilter (EGG_LIST_BOX (self));
1483
1484   select_first_contact (self);
1485 }
1486
1487 static void
1488 search_activate_cb (GtkWidget *search,
1489   EmpathyRosterView *self)
1490 {
1491   EggListBox *box = EGG_LIST_BOX (self);
1492   GtkWidget *child;
1493
1494   child = egg_list_box_get_selected_child (box);
1495   if (child == NULL)
1496     return;
1497
1498   empathy_roster_view_child_activated (box, child);
1499 }
1500
1501 void
1502 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1503     EmpathyLiveSearch *search)
1504 {
1505   if (self->priv->search != NULL)
1506     {
1507       g_signal_handlers_disconnect_by_func (self->priv->search,
1508           search_text_notify_cb, self);
1509       g_signal_handlers_disconnect_by_func (self->priv->search,
1510           search_activate_cb, self);
1511
1512       g_clear_object (&self->priv->search);
1513     }
1514
1515   if (search == NULL)
1516     return;
1517
1518   self->priv->search = g_object_ref (search);
1519
1520   g_signal_connect (self->priv->search, "notify::text",
1521       G_CALLBACK (search_text_notify_cb), self);
1522   g_signal_connect (self->priv->search, "activate",
1523       G_CALLBACK (search_activate_cb), self);
1524 }
1525
1526 gboolean
1527 empathy_roster_view_is_empty (EmpathyRosterView *self)
1528 {
1529   return self->priv->empty;
1530 }
1531
1532 gboolean
1533 empathy_roster_view_is_searching (EmpathyRosterView *self)
1534 {
1535   return (self->priv->search != NULL &&
1536       gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1537 }
1538
1539 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1540  * Empathy's internals. */
1541 guint
1542 empathy_roster_view_add_event (EmpathyRosterView *self,
1543     FolksIndividual *individual,
1544     const gchar *icon,
1545     gpointer user_data)
1546 {
1547   GHashTable *contacts;
1548
1549   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1550   if (contacts == NULL)
1551     return 0;
1552
1553   self->priv->last_event_id++;
1554
1555   g_queue_push_head (self->priv->events,
1556       event_new (self->priv->last_event_id, individual, icon, user_data));
1557
1558   start_flashing (self);
1559
1560   return self->priv->last_event_id;
1561 }
1562
1563 void
1564 empathy_roster_view_remove_event (EmpathyRosterView *self,
1565     guint event_id)
1566 {
1567   GList *l;
1568
1569   for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1570       l = g_list_next (l))
1571     {
1572       Event *event = l->data;
1573
1574       if (event->id == event_id)
1575         {
1576           remove_event (self, event);
1577           return;
1578         }
1579     }
1580 }
1581
1582 FolksIndividual *
1583 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1584 {
1585   GtkWidget *child;
1586
1587   child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1588
1589   if (!EMPATHY_IS_ROSTER_CONTACT (child))
1590     return NULL;
1591
1592   return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1593 }