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