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