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