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