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