]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-roster-view.c
add API for individual tooltips
[empathy.git] / libempathy-gtk / empathy-roster-view.c
1 #include "config.h"
2
3 #include "empathy-roster-view.h"
4
5 #include <glib/gi18n-lib.h>
6
7 #include <libempathy-gtk/empathy-roster-contact.h>
8 #include <libempathy-gtk/empathy-roster-group.h>
9 #include <libempathy-gtk/empathy-ui-utils.h>
10
11 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
12
13 enum
14 {
15   PROP_MANAGER = 1,
16   PROP_SHOW_OFFLINE,
17   PROP_SHOW_GROUPS,
18   N_PROPS
19 };
20
21 enum
22 {
23   SIG_INDIVIDUAL_ACTIVATED,
24   SIG_POPUP_INDIVIDUAL_MENU,
25   LAST_SIGNAL
26 };
27
28 static guint signals[LAST_SIGNAL];
29
30 #define NO_GROUP "X-no-group"
31 #define UNGROUPPED _("Ungroupped")
32 #define TOP_GROUP _("Most Used")
33
34 struct _EmpathyRosterViewPriv
35 {
36   EmpathyIndividualManager *manager;
37
38   /* FolksIndividual (borrowed) -> GHashTable (
39    * (gchar * group_name) -> EmpathyRosterContact (borrowed))
40    *
41    * When not using groups, this hash just have one element mapped
42    * from the special NO_GROUP key. We could use it as a set but
43    * I prefer to stay coherent in the way this hash is managed.
44    */
45   GHashTable *roster_contacts;
46   /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
47   GHashTable *roster_groups;
48
49   gboolean show_offline;
50   gboolean show_groups;
51
52   EmpathyLiveSearch *search;
53
54   EmpathyRosterViewIndividualTooltipCb individual_tooltip_cb;
55   gpointer individual_tooltip_data;
56 };
57
58 static void
59 empathy_roster_view_get_property (GObject *object,
60     guint property_id,
61     GValue *value,
62     GParamSpec *pspec)
63 {
64   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
65
66   switch (property_id)
67     {
68       case PROP_MANAGER:
69         g_value_set_object (value, self->priv->manager);
70         break;
71       case PROP_SHOW_OFFLINE:
72         g_value_set_boolean (value, self->priv->show_offline);
73         break;
74       case PROP_SHOW_GROUPS:
75         g_value_set_boolean (value, self->priv->show_groups);
76         break;
77       default:
78         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
79         break;
80     }
81 }
82
83 static void
84 empathy_roster_view_set_property (GObject *object,
85     guint property_id,
86     const GValue *value,
87     GParamSpec *pspec)
88 {
89   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
90
91   switch (property_id)
92     {
93       case PROP_MANAGER:
94         g_assert (self->priv->manager == NULL); /* construct only */
95         self->priv->manager = g_value_dup_object (value);
96         break;
97       case PROP_SHOW_OFFLINE:
98         empathy_roster_view_show_offline (self, g_value_get_boolean (value));
99         break;
100       case PROP_SHOW_GROUPS:
101         empathy_roster_view_show_groups (self, g_value_get_boolean (value));
102         break;
103       default:
104         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105         break;
106     }
107 }
108
109 static void
110 roster_contact_changed_cb (GtkWidget *child,
111     GParamSpec *spec,
112     EmpathyRosterView *self)
113 {
114   egg_list_box_child_changed (EGG_LIST_BOX (self), child);
115 }
116
117 static GtkWidget *
118 add_roster_contact (EmpathyRosterView *self,
119     FolksIndividual *individual,
120     const gchar *group)
121 {
122   GtkWidget *contact;
123
124   contact = empathy_roster_contact_new (individual, group);
125
126   /* Need to refilter if online is changed */
127   g_signal_connect (contact, "notify::online",
128       G_CALLBACK (roster_contact_changed_cb), self);
129
130   /* Need to resort if alias is changed */
131   g_signal_connect (contact, "notify::alias",
132       G_CALLBACK (roster_contact_changed_cb), self);
133
134   gtk_widget_show (contact);
135   gtk_container_add (GTK_CONTAINER (self), contact);
136
137   return contact;
138 }
139
140 static void
141 group_expanded_cb (EmpathyRosterGroup *group,
142     GParamSpec *spec,
143     EmpathyRosterView *self)
144 {
145   GList *widgets, *l;
146
147   widgets = empathy_roster_group_get_widgets (group);
148   for (l = widgets; l != NULL; l = g_list_next (l))
149     {
150       egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
151     }
152
153   g_list_free (widgets);
154 }
155
156 static EmpathyRosterGroup *
157 lookup_roster_group (EmpathyRosterView *self,
158     const gchar *group)
159 {
160   return g_hash_table_lookup (self->priv->roster_groups, group);
161 }
162
163 static void
164 ensure_roster_group (EmpathyRosterView *self,
165     const gchar *group)
166 {
167   GtkWidget *roster_group;
168
169   roster_group = (GtkWidget *) lookup_roster_group (self, group);
170   if (roster_group != NULL)
171     return;
172
173   roster_group = empathy_roster_group_new (group);
174
175   g_signal_connect (roster_group, "notify::expanded",
176       G_CALLBACK (group_expanded_cb), self);
177
178   gtk_widget_show (roster_group);
179   gtk_container_add (GTK_CONTAINER (self), roster_group);
180
181   g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
182       roster_group);
183 }
184
185 static void
186 add_to_group (EmpathyRosterView *self,
187     FolksIndividual *individual,
188     const gchar *group)
189 {
190   GtkWidget *contact;
191   GHashTable *contacts;
192
193   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
194   if (contacts == NULL)
195     return;
196
197   if (tp_strdiff (group, NO_GROUP))
198     ensure_roster_group (self, group);
199
200   contact = add_roster_contact (self, individual, group);
201   g_hash_table_insert (contacts, g_strdup (group), contact);
202 }
203
204 static void
205 individual_added (EmpathyRosterView *self,
206     FolksIndividual *individual)
207 {
208   GHashTable *contacts;
209
210   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
211   if (contacts != NULL)
212     return;
213
214   contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
215
216   g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
217
218   if (!self->priv->show_groups)
219     {
220       add_to_group (self, individual, NO_GROUP);
221     }
222   else
223     {
224       GeeSet *groups;
225
226       groups = folks_group_details_get_groups (
227           FOLKS_GROUP_DETAILS (individual));
228
229       if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
230         {
231           GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
232
233           while (iter != NULL && gee_iterator_next (iter))
234             {
235               gchar *group = gee_iterator_get (iter);
236
237               add_to_group (self, individual, group);
238
239               g_free (group);
240             }
241
242           g_clear_object (&iter);
243         }
244       else
245         {
246           /* No group, adds to Ungroupped */
247           add_to_group (self, individual, UNGROUPPED);
248         }
249     }
250 }
251
252 static void
253 update_group_widgets_count (EmpathyRosterView *self,
254     EmpathyRosterGroup *group,
255     EmpathyRosterContact *contact,
256     gboolean displayed)
257 {
258   if (displayed)
259     {
260       if (empathy_roster_group_add_widget (group, GTK_WIDGET (contact)) == 1)
261         {
262           egg_list_box_child_changed (EGG_LIST_BOX (self),
263               GTK_WIDGET (group));
264         }
265     }
266   else
267     {
268       if (empathy_roster_group_remove_widget (group, GTK_WIDGET (contact)) == 0)
269         {
270           egg_list_box_child_changed (EGG_LIST_BOX (self),
271               GTK_WIDGET (group));
272         }
273     }
274 }
275
276 static void
277 individual_removed (EmpathyRosterView *self,
278     FolksIndividual *individual)
279 {
280   GHashTable *contacts;
281   GHashTableIter iter;
282   gpointer key, value;
283
284   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
285   if (contacts == NULL)
286     return;
287
288   g_hash_table_iter_init (&iter, contacts);
289   while (g_hash_table_iter_next (&iter, &key, &value))
290     {
291       const gchar *group_name = key;
292       GtkWidget *contact = value;
293       EmpathyRosterGroup *group;
294
295       group = lookup_roster_group (self, group_name);
296       if (group != NULL)
297         {
298           update_group_widgets_count (self, group,
299               EMPATHY_ROSTER_CONTACT (contact), FALSE);
300         }
301
302       gtk_container_remove (GTK_CONTAINER (self), contact);
303     }
304
305   g_hash_table_remove (self->priv->roster_contacts, individual);
306 }
307
308 static void
309 members_changed_cb (EmpathyIndividualManager *manager,
310     const gchar *message,
311     GList *added,
312     GList *removed,
313     TpChannelGroupChangeReason reason,
314     EmpathyRosterView *self)
315 {
316   GList *l;
317
318   for (l = added; l != NULL; l = g_list_next (l))
319     {
320       FolksIndividual *individual = l->data;
321
322       individual_added (self, individual);
323     }
324
325   for (l = removed; l != NULL; l = g_list_next (l))
326     {
327       FolksIndividual *individual = l->data;
328
329       individual_removed (self, individual);
330     }
331 }
332
333 static gint
334 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
335     EmpathyRosterContact *b)
336 {
337   FolksIndividual *ind_a, *ind_b;
338   const gchar *alias_a, *alias_b;
339
340   ind_a = empathy_roster_contact_get_individual (a);
341   ind_b = empathy_roster_contact_get_individual (b);
342
343   alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
344   alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
345
346   return g_ascii_strcasecmp (alias_a, alias_b);
347 }
348
349 static gint
350 compare_individual_top_position (EmpathyRosterView *self,
351     EmpathyRosterContact *a,
352     EmpathyRosterContact *b)
353 {
354   FolksIndividual *ind_a, *ind_b;
355   GList *tops;
356   gint index_a, index_b;
357
358   ind_a = empathy_roster_contact_get_individual (a);
359   ind_b = empathy_roster_contact_get_individual (b);
360
361   tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
362
363   index_a = g_list_index (tops, ind_a);
364   index_b = g_list_index (tops, ind_b);
365
366   if (index_a == index_b)
367     return 0;
368
369   if (index_a == -1)
370     return 1;
371
372   if (index_b == -1)
373     return -1;
374
375   return index_a - index_b;
376 }
377
378 static gint
379 compare_roster_contacts_no_group (EmpathyRosterView *self,
380     EmpathyRosterContact *a,
381     EmpathyRosterContact *b)
382 {
383   gint top;
384
385   top = compare_individual_top_position (self, a, b);
386   if (top != 0)
387     return top;
388
389   return compare_roster_contacts_by_alias (a, b);
390 }
391
392 static gint
393 compare_group_names (const gchar *group_a,
394     const gchar *group_b)
395 {
396   if (!tp_strdiff (group_a, TOP_GROUP))
397     return -1;
398
399   if (!tp_strdiff (group_b, TOP_GROUP))
400     return 1;
401
402   return g_ascii_strcasecmp (group_a, group_b);
403 }
404
405 static gint
406 compare_roster_contacts_with_groups (EmpathyRosterView *self,
407     EmpathyRosterContact *a,
408     EmpathyRosterContact *b)
409 {
410   const gchar *group_a, *group_b;
411
412   group_a = empathy_roster_contact_get_group (a);
413   group_b = empathy_roster_contact_get_group (b);
414
415   if (!tp_strdiff (group_a, group_b))
416     /* Same group, compare the contacts */
417     return compare_roster_contacts_by_alias (a, b);
418
419   /* Sort by group */
420   return compare_group_names (group_a, group_b);
421 }
422
423 static gint
424 compare_roster_contacts (EmpathyRosterView *self,
425     EmpathyRosterContact *a,
426     EmpathyRosterContact *b)
427 {
428   if (!self->priv->show_groups)
429     return compare_roster_contacts_no_group (self, a, b);
430   else
431     return compare_roster_contacts_with_groups (self, a, b);
432 }
433
434 static gint
435 compare_roster_groups (EmpathyRosterGroup *a,
436     EmpathyRosterGroup *b)
437 {
438   const gchar *name_a, *name_b;
439
440   name_a = empathy_roster_group_get_name (a);
441   name_b = empathy_roster_group_get_name (b);
442
443   return compare_group_names (name_a, name_b);
444 }
445
446 static gint
447 compare_contact_group (EmpathyRosterContact *contact,
448     EmpathyRosterGroup *group)
449 {
450   const char *contact_group, *group_name;
451
452   contact_group = empathy_roster_contact_get_group (contact);
453   group_name = empathy_roster_group_get_name (group);
454
455   if (!tp_strdiff (contact_group, group_name))
456     /* @contact is in @group, @group has to be displayed first */
457     return 1;
458
459   /* @contact is in a different group, sort by group name */
460   return compare_group_names (contact_group, group_name);
461 }
462
463 static gint
464 roster_view_sort (gconstpointer a,
465     gconstpointer b,
466     gpointer user_data)
467 {
468   EmpathyRosterView *self = user_data;
469
470   if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
471     return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
472         EMPATHY_ROSTER_CONTACT (b));
473   else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
474     return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
475         EMPATHY_ROSTER_GROUP (b));
476   else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
477     return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
478         EMPATHY_ROSTER_GROUP (b));
479   else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
480     return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
481         EMPATHY_ROSTER_GROUP (a));
482
483   g_return_val_if_reached (0);
484 }
485
486 static void
487 update_separator (GtkWidget **separator,
488     GtkWidget *child,
489     GtkWidget *before,
490     gpointer user_data)
491 {
492   if (before == NULL)
493     {
494       /* No separator before the first row */
495       g_clear_object (separator);
496       return;
497     }
498
499   if (*separator != NULL)
500     return;
501
502   *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
503   g_object_ref_sink (*separator);
504 }
505
506 static gboolean
507 is_searching (EmpathyRosterView *self)
508 {
509   if (self->priv->search == NULL)
510     return FALSE;
511
512   return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
513 }
514
515 static gboolean
516 filter_contact (EmpathyRosterView *self,
517     EmpathyRosterContact *contact)
518 {
519   gboolean displayed;
520
521   if (is_searching (self))
522     {
523       FolksIndividual *individual;
524
525       individual = empathy_roster_contact_get_individual (contact);
526
527       displayed = empathy_individual_match_string (individual,
528           empathy_live_search_get_text (self->priv->search),
529           empathy_live_search_get_words (self->priv->search));
530     }
531   else
532     {
533       if (self->priv->show_offline)
534         {
535           displayed = TRUE;
536         }
537       else
538         {
539           displayed = empathy_roster_contact_is_online (contact);
540         }
541     }
542
543   if (self->priv->show_groups)
544     {
545       const gchar *group_name;
546       EmpathyRosterGroup *group;
547
548       group_name = empathy_roster_contact_get_group (contact);
549       group = lookup_roster_group (self, group_name);
550
551       if (group != NULL)
552         {
553           update_group_widgets_count (self, group, contact, displayed);
554
555           /* When searching, always display even if the group is closed */
556           if (!is_searching (self) &&
557               !gtk_expander_get_expanded (GTK_EXPANDER (group)))
558             return FALSE;
559         }
560     }
561
562   return displayed;
563 }
564
565 static gboolean
566 filter_group (EmpathyRosterView *self,
567     EmpathyRosterGroup *group)
568 {
569   return empathy_roster_group_get_widgets_count (group);
570 }
571
572 static gboolean
573 filter_list (GtkWidget *child,
574     gpointer user_data)
575 {
576   EmpathyRosterView *self = user_data;
577
578   if (EMPATHY_IS_ROSTER_CONTACT (child))
579     return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
580
581   else if (EMPATHY_IS_ROSTER_GROUP (child))
582     return filter_group (self, EMPATHY_ROSTER_GROUP (child));
583
584   g_return_val_if_reached (FALSE);
585 }
586
587 /* @list: GList of EmpathyRosterContact
588  *
589  * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
590  * @individual */
591 static gboolean
592 individual_in_list (FolksIndividual *individual,
593     GList *list)
594 {
595   GList *l;
596
597   for (l = list; l != NULL; l = g_list_next (l))
598     {
599       EmpathyRosterContact *contact = l->data;
600
601       if (empathy_roster_contact_get_individual (contact) == individual)
602         return TRUE;
603     }
604
605   return FALSE;
606 }
607
608 static void
609 populate_view (EmpathyRosterView *self)
610 {
611   GList *individuals, *l;
612
613   individuals = empathy_individual_manager_get_members (self->priv->manager);
614   for (l = individuals; l != NULL; l = g_list_next (l))
615     {
616       FolksIndividual *individual = l->data;
617
618       individual_added (self, individual);
619     }
620
621   g_list_free (individuals);
622 }
623
624 static void
625 remove_from_group (EmpathyRosterView *self,
626     FolksIndividual *individual,
627     const gchar *group)
628 {
629   GHashTable *contacts;
630   GtkWidget *contact;
631   EmpathyRosterGroup *roster_group;
632
633   contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
634   if (contacts == NULL)
635     return;
636
637   contact = g_hash_table_lookup (contacts, group);
638   if (contact == NULL)
639     return;
640
641   g_hash_table_remove (contacts, group);
642
643   if (g_hash_table_size (contacts) == 0)
644     {
645       add_to_group (self, individual, UNGROUPPED);
646     }
647
648   roster_group = lookup_roster_group (self, group);
649
650   if (roster_group != NULL)
651     {
652       update_group_widgets_count (self, roster_group,
653           EMPATHY_ROSTER_CONTACT (contact), FALSE);
654     }
655
656   gtk_container_remove (GTK_CONTAINER (self), contact);
657 }
658
659 static void
660 update_top_contacts (EmpathyRosterView *self)
661 {
662   GList *tops, *l;
663   GList *to_add = NULL, *to_remove = NULL;
664   EmpathyRosterGroup *group;
665
666   if (!self->priv->show_groups)
667     {
668       egg_list_box_resort (EGG_LIST_BOX (self));
669       return;
670     }
671
672   tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
673
674   group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
675   if (group == NULL)
676     {
677       to_add = g_list_copy (tops);
678     }
679   else
680     {
681       GList *contacts;
682
683       contacts = empathy_roster_group_get_widgets (group);
684
685       /* Check which EmpathyRosterContact have to be removed */
686       for (l = contacts; l != NULL; l = g_list_next (l))
687         {
688           EmpathyRosterContact *contact = l->data;
689           FolksIndividual *individual;
690
691           individual = empathy_roster_contact_get_individual (contact);
692
693           if (g_list_find (tops, individual) == NULL)
694             to_remove = g_list_prepend (to_remove, individual);
695         }
696
697       /* Check which EmpathyRosterContact have to be added */
698       for (l = tops; l != NULL; l = g_list_next (l))
699         {
700           FolksIndividual *individual = l->data;
701
702           if (!individual_in_list (individual, contacts))
703             to_add = g_list_prepend (to_add, individual);
704         }
705     }
706
707   for (l = to_add; l != NULL; l = g_list_next (l))
708     add_to_group (self, l->data, TOP_GROUP);
709
710   for (l = to_remove; l != NULL; l = g_list_next (l))
711     remove_from_group (self, l->data, TOP_GROUP);
712
713   g_list_free (to_add);
714   g_list_free (to_remove);
715 }
716
717 static void
718 groups_changed_cb (EmpathyIndividualManager *manager,
719     FolksIndividual *individual,
720     gchar *group,
721     gboolean is_member,
722     EmpathyRosterView *self)
723 {
724   if (!self->priv->show_groups)
725     return;
726
727   if (is_member)
728     {
729       add_to_group (self, individual, group);
730     }
731   else
732     {
733       remove_from_group (self, individual, group);
734     }
735 }
736
737 static void
738 top_individuals_changed_cb (EmpathyIndividualManager *manager,
739     GParamSpec *spec,
740     EmpathyRosterView *self)
741 {
742   update_top_contacts (self);
743 }
744
745 static void
746 empathy_roster_view_constructed (GObject *object)
747 {
748   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
749   void (*chain_up) (GObject *) =
750       ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
751
752   if (chain_up != NULL)
753     chain_up (object);
754
755   g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
756
757   populate_view (self);
758
759   tp_g_signal_connect_object (self->priv->manager, "members-changed",
760       G_CALLBACK (members_changed_cb), self, 0);
761   tp_g_signal_connect_object (self->priv->manager, "groups-changed",
762       G_CALLBACK (groups_changed_cb), self, 0);
763   tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
764       G_CALLBACK (top_individuals_changed_cb), self, 0);
765
766   egg_list_box_set_sort_func (EGG_LIST_BOX (self),
767       roster_view_sort, self, NULL);
768
769   egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
770       self, NULL);
771
772   egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
773
774   egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
775 }
776
777 static void
778 empathy_roster_view_dispose (GObject *object)
779 {
780   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
781   void (*chain_up) (GObject *) =
782       ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
783
784   empathy_roster_view_set_live_search (self, NULL);
785   g_clear_object (&self->priv->manager);
786
787   if (chain_up != NULL)
788     chain_up (object);
789 }
790
791 static void
792 empathy_roster_view_finalize (GObject *object)
793 {
794   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
795   void (*chain_up) (GObject *) =
796       ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
797
798   g_hash_table_unref (self->priv->roster_contacts);
799   g_hash_table_unref (self->priv->roster_groups);
800
801   if (chain_up != NULL)
802     chain_up (object);
803 }
804
805 static void
806 empathy_roster_view_child_activated (EggListBox *box,
807     GtkWidget *child)
808 {
809   EmpathyRosterContact *contact;
810   FolksIndividual *individual;
811
812   if (!EMPATHY_IS_ROSTER_CONTACT (child))
813     return;
814
815   contact = EMPATHY_ROSTER_CONTACT (child);
816   individual = empathy_roster_contact_get_individual (contact);
817
818   g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
819 }
820
821 static void
822 fire_popup_individual_menu (EmpathyRosterView *self,
823     GtkWidget *child,
824     guint button,
825     guint time)
826 {
827   EmpathyRosterContact *contact;
828   FolksIndividual *individual;
829
830   if (!EMPATHY_IS_ROSTER_CONTACT (child))
831     return;
832
833   contact = EMPATHY_ROSTER_CONTACT (child);
834   individual = empathy_roster_contact_get_individual (contact);
835
836   g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
837       individual, button, time);
838 }
839
840 static gboolean
841 empathy_roster_view_button_press_event (GtkWidget *widget,
842     GdkEventButton *event)
843 {
844   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
845   gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
846       ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
847
848   if (event->button == 3)
849     {
850       GtkWidget *child;
851
852       child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
853
854       if (child != NULL)
855         fire_popup_individual_menu (self, child, event->button, event->time);
856     }
857
858   return chain_up (widget, event);
859 }
860
861 static gboolean
862 empathy_roster_view_key_press_event (GtkWidget *widget,
863     GdkEventKey *event)
864 {
865   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
866   gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
867       ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
868
869   if (event->keyval == GDK_KEY_Menu)
870     {
871       GtkWidget *child;
872
873       child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
874
875       if (child != NULL)
876         fire_popup_individual_menu (self, child, 0, event->time);
877     }
878
879   return chain_up (widget, event);
880 }
881
882 static gboolean
883 empathy_roster_view_query_tooltip (GtkWidget *widget,
884     gint x,
885     gint y,
886     gboolean keyboard_mode,
887     GtkTooltip *tooltip)
888 {
889   EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
890   GtkWidget *child;
891   EmpathyRosterContact *contact;
892   FolksIndividual *individual;
893
894   if (self->priv->individual_tooltip_cb == NULL)
895     return FALSE;
896
897   child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
898   if (!EMPATHY_IS_ROSTER_CONTACT (child))
899     return FALSE;
900
901   contact = EMPATHY_ROSTER_CONTACT (child);
902   individual = empathy_roster_contact_get_individual (contact);
903
904   return self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
905       tooltip, self->priv->individual_tooltip_data);
906 }
907
908 void
909 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
910     EmpathyRosterViewIndividualTooltipCb callback,
911     gpointer user_data)
912 {
913   self->priv->individual_tooltip_cb = callback;
914   self->priv->individual_tooltip_data = user_data;
915
916   gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
917 }
918
919 static void
920 empathy_roster_view_class_init (
921     EmpathyRosterViewClass *klass)
922 {
923   GObjectClass *oclass = G_OBJECT_CLASS (klass);
924   EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
925   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
926   GParamSpec *spec;
927
928   oclass->get_property = empathy_roster_view_get_property;
929   oclass->set_property = empathy_roster_view_set_property;
930   oclass->constructed = empathy_roster_view_constructed;
931   oclass->dispose = empathy_roster_view_dispose;
932   oclass->finalize = empathy_roster_view_finalize;
933
934   widget_class->button_press_event = empathy_roster_view_button_press_event;
935   widget_class->key_press_event = empathy_roster_view_key_press_event;
936   widget_class->query_tooltip = empathy_roster_view_query_tooltip;
937
938   box_class->child_activated = empathy_roster_view_child_activated;
939
940   spec = g_param_spec_object ("manager", "Manager",
941       "EmpathyIndividualManager",
942       EMPATHY_TYPE_INDIVIDUAL_MANAGER,
943       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
944   g_object_class_install_property (oclass, PROP_MANAGER, spec);
945
946   spec = g_param_spec_boolean ("show-offline", "Show Offline",
947       "Show offline contacts",
948       FALSE,
949       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
950   g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
951
952   spec = g_param_spec_boolean ("show-groups", "Show Groups",
953       "Show groups",
954       FALSE,
955       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
956   g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
957
958   signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
959       G_OBJECT_CLASS_TYPE (klass),
960       G_SIGNAL_RUN_LAST,
961       0, NULL, NULL, NULL,
962       G_TYPE_NONE,
963       1, FOLKS_TYPE_INDIVIDUAL);
964
965   signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
966       G_OBJECT_CLASS_TYPE (klass),
967       G_SIGNAL_RUN_LAST,
968       0, NULL, NULL, NULL,
969       G_TYPE_NONE,
970       3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
971
972   g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
973 }
974
975 static void
976 empathy_roster_view_init (EmpathyRosterView *self)
977 {
978   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
979       EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
980
981   self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
982       NULL, (GDestroyNotify) g_hash_table_unref);
983   self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
984       g_free, NULL);
985 }
986
987 GtkWidget *
988 empathy_roster_view_new (EmpathyIndividualManager *manager)
989 {
990   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
991
992   return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
993       "manager", manager,
994       NULL);
995 }
996
997 EmpathyIndividualManager *
998 empathy_roster_view_get_manager (EmpathyRosterView *self)
999 {
1000   return self->priv->manager;
1001 }
1002
1003 void
1004 empathy_roster_view_show_offline (EmpathyRosterView *self,
1005     gboolean show)
1006 {
1007   if (self->priv->show_offline == show)
1008     return;
1009
1010   self->priv->show_offline = show;
1011   egg_list_box_refilter (EGG_LIST_BOX (self));
1012
1013   g_object_notify (G_OBJECT (self), "show-offline");
1014 }
1015
1016 static void
1017 clear_view (EmpathyRosterView *self)
1018 {
1019   gtk_container_foreach (GTK_CONTAINER (self),
1020       (GtkCallback) gtk_widget_destroy, NULL);
1021
1022   g_hash_table_remove_all (self->priv->roster_contacts);
1023 }
1024
1025 void
1026 empathy_roster_view_show_groups (EmpathyRosterView *self,
1027     gboolean show)
1028 {
1029   if (self->priv->show_groups == show)
1030     return;
1031
1032   self->priv->show_groups = show;
1033
1034   /* TODO: block sort/filter? */
1035   clear_view (self);
1036   populate_view (self);
1037
1038   g_object_notify (G_OBJECT (self), "show-groups");
1039 }
1040
1041 static void
1042 select_first_contact (EmpathyRosterView *self)
1043 {
1044   GList *children, *l;
1045
1046   children = gtk_container_get_children (GTK_CONTAINER (self));
1047   for (l = children; l != NULL; l = g_list_next (l))
1048     {
1049       GtkWidget *child = l->data;
1050
1051       if (!gtk_widget_get_child_visible (child))
1052         continue;
1053
1054       if (!EMPATHY_IS_ROSTER_CONTACT (child))
1055         continue;
1056
1057       egg_list_box_select_child (EGG_LIST_BOX (self), child);
1058       break;
1059     }
1060
1061   g_list_free (children);
1062 }
1063
1064 static void
1065 search_text_notify_cb (EmpathyLiveSearch *search,
1066     GParamSpec *pspec,
1067     EmpathyRosterView *self)
1068 {
1069   egg_list_box_refilter (EGG_LIST_BOX (self));
1070
1071   select_first_contact (self);
1072 }
1073
1074 static void
1075 search_activate_cb (GtkWidget *search,
1076   EmpathyRosterView *self)
1077 {
1078   EggListBox *box = EGG_LIST_BOX (self);
1079   GtkWidget *child;
1080
1081   child = egg_list_box_get_selected_child (box);
1082   if (child == NULL)
1083     return;
1084
1085   empathy_roster_view_child_activated (box, child);
1086 }
1087
1088 void
1089 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1090     EmpathyLiveSearch *search)
1091 {
1092   if (self->priv->search != NULL)
1093     {
1094       g_signal_handlers_disconnect_by_func (self->priv->search,
1095           search_text_notify_cb, self);
1096       g_signal_handlers_disconnect_by_func (self->priv->search,
1097           search_activate_cb, self);
1098
1099       g_clear_object (&self->priv->search);
1100     }
1101
1102   if (search == NULL)
1103     return;
1104
1105   self->priv->search = g_object_ref (search);
1106
1107   g_signal_connect (self->priv->search, "notify::text",
1108       G_CALLBACK (search_text_notify_cb), self);
1109   g_signal_connect (self->priv->search, "activate",
1110       G_CALLBACK (search_activate_cb), self);
1111 }