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