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