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