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