Merge branch 'gnome-3-6'
[empathy-mirror.git] / libempathy-gtk / empathy-roster-view.c
blobc3ae66e25ba05fe62197a6b364b9ada0a5b32936
1 #include "config.h"
3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy/empathy-contact-groups.h>
9 #include <libempathy-gtk/empathy-roster-contact.h>
10 #include <libempathy-gtk/empathy-roster-group.h>
11 #include <libempathy-gtk/empathy-ui-utils.h>
13 #include <libempathy/empathy-utils.h>
15 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
17 /* Flashing delay for icons (milliseconds). */
18 #define FLASH_TIMEOUT 500
20 /* Delay in milliseconds between the last stroke on the keyboard and the start
21 * of the live search. */
22 #define SEARCH_TIMEOUT 500
24 enum
26 PROP_MODEL = 1,
27 PROP_SHOW_OFFLINE,
28 PROP_SHOW_GROUPS,
29 PROP_EMPTY,
30 N_PROPS
33 enum
35 SIG_INDIVIDUAL_ACTIVATED,
36 SIG_POPUP_INDIVIDUAL_MENU,
37 SIG_EVENT_ACTIVATED,
38 SIG_INDIVIDUAL_TOOLTIP,
39 LAST_SIGNAL
42 static guint signals[LAST_SIGNAL];
44 #define NO_GROUP "X-no-group"
46 struct _EmpathyRosterViewPriv
48 /* FolksIndividual (borrowed) -> GHashTable (
49 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
51 * When not using groups, this hash just have one element mapped
52 * from the special NO_GROUP key. We could use it as a set but
53 * I prefer to stay coherent in the way this hash is managed.
55 GHashTable *roster_contacts;
56 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
57 GHashTable *roster_groups;
58 /* Hash of the EmpathyRosterContact currently displayed */
59 GHashTable *displayed_contacts;
61 guint last_event_id;
62 /* queue of (Event *). The most recent events are in the head of the queue
63 * so we always display the icon of the oldest one. */
64 GQueue *events;
65 guint flash_id;
66 gboolean display_flash_event;
68 guint search_id;
70 gboolean show_offline;
71 gboolean show_groups;
72 gboolean empty;
74 EmpathyLiveSearch *search;
76 EmpathyRosterModel *model;
79 typedef struct
81 guint id;
82 FolksIndividual *individual;
83 gchar *icon;
84 gpointer user_data;
85 } Event;
87 static Event *
88 event_new (guint id,
89 FolksIndividual *individual,
90 const gchar *icon,
91 gpointer user_data)
93 Event *event = g_slice_new (Event);
95 event->id = id;
96 event->individual = g_object_ref (individual);
97 event->icon = g_strdup (icon);
98 event->user_data = user_data;
99 return event;
102 static void
103 event_free (gpointer data)
105 Event *event = data;
106 g_object_unref (event->individual);
107 g_free (event->icon);
109 g_slice_free (Event, event);
112 static void
113 empathy_roster_view_get_property (GObject *object,
114 guint property_id,
115 GValue *value,
116 GParamSpec *pspec)
118 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
120 switch (property_id)
122 case PROP_MODEL:
123 g_value_set_object (value, self->priv->model);
124 break;
125 case PROP_SHOW_OFFLINE:
126 g_value_set_boolean (value, self->priv->show_offline);
127 break;
128 case PROP_SHOW_GROUPS:
129 g_value_set_boolean (value, self->priv->show_groups);
130 break;
131 case PROP_EMPTY:
132 g_value_set_boolean (value, self->priv->empty);
133 break;
134 default:
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
136 break;
140 static void
141 empathy_roster_view_set_property (GObject *object,
142 guint property_id,
143 const GValue *value,
144 GParamSpec *pspec)
146 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
148 switch (property_id)
150 case PROP_MODEL:
151 g_assert (self->priv->model == NULL);
152 self->priv->model = g_value_dup_object (value);
153 break;
154 case PROP_SHOW_OFFLINE:
155 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
156 break;
157 case PROP_SHOW_GROUPS:
158 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
159 break;
160 default:
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
162 break;
166 static void
167 roster_contact_changed_cb (GtkWidget *child,
168 GParamSpec *spec,
169 EmpathyRosterView *self)
171 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
174 static GtkWidget *
175 add_roster_contact (EmpathyRosterView *self,
176 FolksIndividual *individual,
177 const gchar *group)
179 GtkWidget *contact;
181 contact = empathy_roster_contact_new (individual, group);
183 /* Need to refilter if online is changed */
184 g_signal_connect (contact, "notify::online",
185 G_CALLBACK (roster_contact_changed_cb), self);
187 /* Need to resort if alias is changed */
188 g_signal_connect (contact, "notify::alias",
189 G_CALLBACK (roster_contact_changed_cb), self);
191 gtk_widget_show (contact);
192 gtk_container_add (GTK_CONTAINER (self), contact);
194 return contact;
197 static void
198 group_expanded_cb (EmpathyRosterGroup *group,
199 GParamSpec *spec,
200 EmpathyRosterView *self)
202 GList *widgets, *l;
204 widgets = empathy_roster_group_get_widgets (group);
205 for (l = widgets; l != NULL; l = g_list_next (l))
207 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
210 g_list_free (widgets);
212 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
213 gtk_expander_get_expanded (GTK_EXPANDER (group)));
216 static EmpathyRosterGroup *
217 lookup_roster_group (EmpathyRosterView *self,
218 const gchar *group)
220 return g_hash_table_lookup (self->priv->roster_groups, group);
223 static EmpathyRosterGroup *
224 ensure_roster_group (EmpathyRosterView *self,
225 const gchar *group)
227 GtkWidget *roster_group;
229 roster_group = (GtkWidget *) lookup_roster_group (self, group);
230 if (roster_group != NULL)
231 return EMPATHY_ROSTER_GROUP (roster_group);
233 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
234 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
235 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
236 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
237 else
238 roster_group = empathy_roster_group_new (group, NULL);
240 gtk_expander_set_expanded (GTK_EXPANDER (roster_group),
241 empathy_contact_group_get_expanded (group));
243 g_signal_connect (roster_group, "notify::expanded",
244 G_CALLBACK (group_expanded_cb), self);
246 gtk_widget_show (roster_group);
247 gtk_container_add (GTK_CONTAINER (self), roster_group);
249 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
250 roster_group);
252 return EMPATHY_ROSTER_GROUP (roster_group);
255 static void
256 update_empty (EmpathyRosterView *self,
257 gboolean empty)
259 if (self->priv->empty == empty)
260 return;
262 self->priv->empty = empty;
263 g_object_notify (G_OBJECT (self), "empty");
266 static gboolean filter_group (EmpathyRosterView *self,
267 EmpathyRosterGroup *group);
269 static gboolean
270 at_least_one_group_displayed (EmpathyRosterView *self)
272 GHashTableIter iter;
273 gpointer v;
275 g_hash_table_iter_init (&iter, self->priv->roster_groups);
276 while (g_hash_table_iter_next (&iter, NULL, &v))
278 EmpathyRosterGroup *group = EMPATHY_ROSTER_GROUP (v);
280 if (filter_group (self, group))
281 return TRUE;
284 return FALSE;
287 static void
288 check_if_empty (EmpathyRosterView *self)
290 /* Roster is considered as empty if there is no contact *and* no group
291 * currently displayed. */
292 if (g_hash_table_size (self->priv->displayed_contacts) != 0 ||
293 at_least_one_group_displayed (self))
295 update_empty (self, FALSE);
296 return;
299 update_empty (self, TRUE);
302 static void
303 update_group_widgets (EmpathyRosterView *self,
304 EmpathyRosterGroup *group,
305 EmpathyRosterContact *contact,
306 gboolean add)
308 guint old_count, count;
310 old_count = empathy_roster_group_get_widgets_count (group);
312 if (add)
313 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
314 else
315 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
317 if (count != old_count)
319 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
321 check_if_empty (self);
325 static void
326 add_to_group (EmpathyRosterView *self,
327 FolksIndividual *individual,
328 const gchar *group)
330 GtkWidget *contact;
331 GHashTable *contacts;
332 EmpathyRosterGroup *roster_group = NULL;
334 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
335 if (contacts == NULL)
336 return;
338 if (g_hash_table_lookup (contacts, group) != NULL)
339 return;
341 if (tp_strdiff (group, NO_GROUP))
342 roster_group = ensure_roster_group (self, group);
344 contact = add_roster_contact (self, individual, group);
345 g_hash_table_insert (contacts, g_strdup (group), contact);
347 if (roster_group != NULL)
349 update_group_widgets (self, roster_group,
350 EMPATHY_ROSTER_CONTACT (contact), TRUE);
354 static void
355 individual_favourite_change_cb (FolksIndividual *individual,
356 GParamSpec *spec,
357 EmpathyRosterView *self)
359 /* We may have to refilter the contact as only favorite contacts are always
360 * displayed regardless of their presence. */
361 GHashTable *contacts;
362 GtkWidget *contact;
364 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
365 if (contacts == NULL)
366 return;
368 if (self->priv->show_groups)
369 contact = g_hash_table_lookup (contacts,
370 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
371 else
372 contact = g_hash_table_lookup (contacts, NO_GROUP);
374 if (contact == NULL)
375 return;
377 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
380 static void
381 individual_added (EmpathyRosterView *self,
382 FolksIndividual *individual)
384 GHashTable *contacts;
386 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
387 if (contacts != NULL)
388 return;
390 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
392 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
394 if (!self->priv->show_groups)
396 add_to_group (self, individual, NO_GROUP);
398 else
400 GList *groups, *l;
402 groups = empathy_roster_model_dup_groups_for_individual (self->priv->model,
403 individual);
405 if (g_list_length (groups) > 0)
407 for (l = groups; l != NULL; l = g_list_next (l))
409 add_to_group (self, individual, l->data);
412 else
414 /* No group, adds to Ungrouped */
415 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
418 g_list_free_full (groups, g_free);
421 tp_g_signal_connect_object (individual, "notify::is-favourite",
422 G_CALLBACK (individual_favourite_change_cb), self, 0);
425 static void
426 set_event_icon_on_individual (EmpathyRosterView *self,
427 FolksIndividual *individual,
428 const gchar *icon)
430 GHashTable *contacts;
431 GHashTableIter iter;
432 gpointer v;
434 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
435 if (contacts == NULL)
436 return;
438 g_hash_table_iter_init (&iter, contacts);
439 while (g_hash_table_iter_next (&iter, NULL, &v))
441 EmpathyRosterContact *contact =v;
443 empathy_roster_contact_set_event_icon (contact, icon);
447 static void
448 flash_event (Event *event,
449 EmpathyRosterView *self)
451 set_event_icon_on_individual (self, event->individual, event->icon);
454 static void
455 unflash_event (Event *event,
456 EmpathyRosterView *self)
458 set_event_icon_on_individual (self, event->individual, NULL);
461 static gboolean
462 flash_cb (gpointer data)
464 EmpathyRosterView *self = data;
466 if (self->priv->display_flash_event)
468 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
469 self->priv->display_flash_event = FALSE;
471 else
473 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
474 self->priv->display_flash_event = TRUE;
477 return TRUE;
480 static void
481 start_flashing (EmpathyRosterView *self)
483 if (self->priv->flash_id != 0)
484 return;
486 self->priv->display_flash_event = TRUE;
488 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
489 flash_cb, self);
492 static void
493 stop_flashing (EmpathyRosterView *self)
495 if (self->priv->flash_id == 0)
496 return;
498 g_source_remove (self->priv->flash_id);
499 self->priv->flash_id = 0;
502 static void
503 remove_event (EmpathyRosterView *self,
504 Event *event)
506 unflash_event (event, self);
507 g_queue_remove (self->priv->events, event);
509 if (g_queue_get_length (self->priv->events) == 0)
511 stop_flashing (self);
515 static void
516 remove_all_individual_event (EmpathyRosterView *self,
517 FolksIndividual *individual)
519 GList *l;
521 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
522 l = g_list_next (l))
524 Event *event = l->data;
526 if (event->individual == individual)
528 remove_event (self, event);
529 return;
534 static void
535 individual_removed (EmpathyRosterView *self,
536 FolksIndividual *individual)
538 GHashTable *contacts;
539 GHashTableIter iter;
540 gpointer key, value;
542 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
543 if (contacts == NULL)
544 return;
546 remove_all_individual_event (self, individual);
548 g_hash_table_iter_init (&iter, contacts);
549 while (g_hash_table_iter_next (&iter, &key, &value))
551 const gchar *group_name = key;
552 GtkWidget *contact = value;
553 EmpathyRosterGroup *group;
555 group = lookup_roster_group (self, group_name);
556 if (group != NULL)
558 update_group_widgets (self, group,
559 EMPATHY_ROSTER_CONTACT (contact), FALSE);
562 gtk_container_remove (GTK_CONTAINER (self), contact);
565 g_hash_table_remove (self->priv->roster_contacts, individual);
568 static void
569 individual_added_cb (EmpathyRosterModel *model,
570 FolksIndividual *individual,
571 EmpathyRosterView *self)
573 individual_added (self, individual);
576 static void
577 individual_removed_cb (EmpathyRosterModel *model,
578 FolksIndividual *individual,
579 EmpathyRosterView *self)
581 individual_removed (self, individual);
584 static gboolean
585 contact_in_top (EmpathyRosterView *self,
586 EmpathyRosterContact *contact)
588 if (!self->priv->show_groups)
590 /* Always display top contacts in non-group mode. */
591 GList *groups;
592 FolksIndividual *individual;
593 gboolean result = FALSE;
595 individual = empathy_roster_contact_get_individual (contact);
597 groups = empathy_roster_model_dup_groups_for_individual (
598 self->priv->model, individual);
600 if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
601 (GCompareFunc) g_strcmp0) != NULL)
602 result = TRUE;
604 g_list_free_full (groups, g_free);
606 return result;
609 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
610 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
611 /* If we are displaying contacts, we only want to *always* display the
612 * RosterContact which is displayed at the top; not the ones displayed in
613 * the 'normal' group sections */
614 return TRUE;
616 return FALSE;
619 static gint
620 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
621 EmpathyRosterContact *b)
623 FolksIndividual *ind_a, *ind_b;
624 const gchar *alias_a, *alias_b;
626 ind_a = empathy_roster_contact_get_individual (a);
627 ind_b = empathy_roster_contact_get_individual (b);
629 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
630 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
632 return g_ascii_strcasecmp (alias_a, alias_b);
635 static gint
636 compare_roster_contacts_no_group (EmpathyRosterView *self,
637 EmpathyRosterContact *a,
638 EmpathyRosterContact *b)
640 gboolean top_a, top_b;
642 top_a = contact_in_top (self, a);
643 top_b = contact_in_top (self, b);
645 if (top_a == top_b)
646 /* Both contacts are in the top of the roster (or not). Sort them
647 * alphabetically */
648 return compare_roster_contacts_by_alias (a, b);
649 else if (top_a)
650 return -1;
651 else
652 return 1;
655 static gint
656 compare_group_names (const gchar *group_a,
657 const gchar *group_b)
659 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
660 return -1;
662 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
663 return 1;
665 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
666 return 1;
667 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
668 return -1;
670 return g_ascii_strcasecmp (group_a, group_b);
673 static gint
674 compare_roster_contacts_with_groups (EmpathyRosterView *self,
675 EmpathyRosterContact *a,
676 EmpathyRosterContact *b)
678 const gchar *group_a, *group_b;
680 group_a = empathy_roster_contact_get_group (a);
681 group_b = empathy_roster_contact_get_group (b);
683 if (!tp_strdiff (group_a, group_b))
684 /* Same group, compare the contacts */
685 return compare_roster_contacts_by_alias (a, b);
687 /* Sort by group */
688 return compare_group_names (group_a, group_b);
691 static gint
692 compare_roster_contacts (EmpathyRosterView *self,
693 EmpathyRosterContact *a,
694 EmpathyRosterContact *b)
696 if (!self->priv->show_groups)
697 return compare_roster_contacts_no_group (self, a, b);
698 else
699 return compare_roster_contacts_with_groups (self, a, b);
702 static gint
703 compare_roster_groups (EmpathyRosterGroup *a,
704 EmpathyRosterGroup *b)
706 const gchar *name_a, *name_b;
708 name_a = empathy_roster_group_get_name (a);
709 name_b = empathy_roster_group_get_name (b);
711 return compare_group_names (name_a, name_b);
714 static gint
715 compare_contact_group (EmpathyRosterContact *contact,
716 EmpathyRosterGroup *group)
718 const char *contact_group, *group_name;
720 contact_group = empathy_roster_contact_get_group (contact);
721 group_name = empathy_roster_group_get_name (group);
723 if (!tp_strdiff (contact_group, group_name))
724 /* @contact is in @group, @group has to be displayed first */
725 return 1;
727 /* @contact is in a different group, sort by group name */
728 return compare_group_names (contact_group, group_name);
731 static gint
732 roster_view_sort (gconstpointer a,
733 gconstpointer b,
734 gpointer user_data)
736 EmpathyRosterView *self = user_data;
738 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
739 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
740 EMPATHY_ROSTER_CONTACT (b));
741 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
742 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
743 EMPATHY_ROSTER_GROUP (b));
744 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
745 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
746 EMPATHY_ROSTER_GROUP (b));
747 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
748 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
749 EMPATHY_ROSTER_GROUP (a));
751 g_return_val_if_reached (0);
754 static void
755 update_separator (GtkWidget **separator,
756 GtkWidget *child,
757 GtkWidget *before,
758 gpointer user_data)
760 if (before == NULL)
762 /* No separator before the first row */
763 g_clear_object (separator);
764 return;
767 if (*separator != NULL)
768 return;
770 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
771 g_object_ref_sink (*separator);
774 static gboolean
775 is_searching (EmpathyRosterView *self)
777 if (self->priv->search == NULL)
778 return FALSE;
780 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
783 static void
784 add_to_displayed (EmpathyRosterView *self,
785 EmpathyRosterContact *contact)
787 FolksIndividual *individual;
788 GHashTable *contacts;
789 GHashTableIter iter;
790 gpointer k;
792 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
793 return;
795 g_hash_table_add (self->priv->displayed_contacts, contact);
796 update_empty (self, FALSE);
798 /* Groups of this contact may now be displayed if we just displays the first
799 * child in this group. */
801 if (!self->priv->show_groups)
802 return;
804 individual = empathy_roster_contact_get_individual (contact);
805 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
806 if (contacts == NULL)
807 return;
809 g_hash_table_iter_init (&iter, contacts);
810 while (g_hash_table_iter_next (&iter, &k, NULL))
812 const gchar *group_name = k;
813 GtkWidget *group;
815 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
816 if (group == NULL)
817 continue;
819 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
823 static void
824 remove_from_displayed (EmpathyRosterView *self,
825 EmpathyRosterContact *contact)
827 g_hash_table_remove (self->priv->displayed_contacts, contact);
829 check_if_empty (self);
832 static gboolean
833 contact_is_favorite (EmpathyRosterContact *contact)
835 FolksIndividual *individual;
837 individual = empathy_roster_contact_get_individual (contact);
839 return folks_favourite_details_get_is_favourite (
840 FOLKS_FAVOURITE_DETAILS (individual));
844 * check if @contact should be displayed according to @self's current status
845 * and without consideration for the state of @contact's groups.
847 static gboolean
848 contact_should_be_displayed (EmpathyRosterView *self,
849 EmpathyRosterContact *contact)
851 if (is_searching (self))
853 FolksIndividual *individual;
855 individual = empathy_roster_contact_get_individual (contact);
857 return empathy_individual_match_string (individual,
858 empathy_live_search_get_text (self->priv->search),
859 empathy_live_search_get_words (self->priv->search));
862 if (self->priv->show_offline)
863 return TRUE;
865 if (contact_in_top (self, contact) &&
866 contact_is_favorite (contact))
867 /* Favorite top contacts are always displayed */
868 return TRUE;
870 return empathy_roster_contact_is_online (contact);
874 static gboolean
875 filter_contact (EmpathyRosterView *self,
876 EmpathyRosterContact *contact)
878 gboolean displayed;
880 displayed = contact_should_be_displayed (self, contact);
882 if (self->priv->show_groups)
884 const gchar *group_name;
885 EmpathyRosterGroup *group;
887 group_name = empathy_roster_contact_get_group (contact);
888 group = lookup_roster_group (self, group_name);
890 if (group != NULL)
892 /* When searching, always display even if the group is closed */
893 if (!is_searching (self) &&
894 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
895 displayed = FALSE;
899 if (displayed)
901 add_to_displayed (self, contact);
903 else
905 remove_from_displayed (self, contact);
908 return displayed;
911 static gboolean
912 filter_group (EmpathyRosterView *self,
913 EmpathyRosterGroup *group)
915 GList *widgets, *l;
916 gboolean result = FALSE;
918 /* Display the group if it contains at least one displayed contact */
919 widgets = empathy_roster_group_get_widgets (group);
920 for (l = widgets; l != NULL; l = g_list_next (l))
922 EmpathyRosterContact *contact = l->data;
924 if (contact_should_be_displayed (self, contact))
926 result = TRUE;
927 break;
931 g_list_free (widgets);
933 return result;
936 static gboolean
937 filter_list (GtkWidget *child,
938 gpointer user_data)
940 EmpathyRosterView *self = user_data;
942 if (EMPATHY_IS_ROSTER_CONTACT (child))
943 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
945 else if (EMPATHY_IS_ROSTER_GROUP (child))
946 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
948 g_return_val_if_reached (FALSE);
951 static void
952 populate_view (EmpathyRosterView *self)
954 GList *individuals, *l;
956 individuals = empathy_roster_model_get_individuals (self->priv->model);
957 for (l = individuals; l != NULL; l = g_list_next (l))
959 FolksIndividual *individual = l->data;
961 individual_added (self, individual);
964 g_list_free (individuals);
967 static void
968 remove_from_group (EmpathyRosterView *self,
969 FolksIndividual *individual,
970 const gchar *group)
972 GHashTable *contacts;
973 GtkWidget *contact;
974 EmpathyRosterGroup *roster_group;
976 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
977 if (contacts == NULL)
978 return;
980 contact = g_hash_table_lookup (contacts, group);
981 if (contact == NULL)
982 return;
984 g_hash_table_remove (contacts, group);
986 if (g_hash_table_size (contacts) == 0)
988 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
991 roster_group = lookup_roster_group (self, group);
993 if (roster_group != NULL)
995 update_group_widgets (self, roster_group,
996 EMPATHY_ROSTER_CONTACT (contact), FALSE);
999 gtk_container_remove (GTK_CONTAINER (self), contact);
1002 static void
1003 groups_changed_cb (EmpathyRosterModel *model,
1004 FolksIndividual *individual,
1005 const gchar *group,
1006 gboolean is_member,
1007 EmpathyRosterView *self)
1009 if (!self->priv->show_groups)
1011 egg_list_box_resort (EGG_LIST_BOX (self));
1012 return;
1015 if (is_member)
1017 add_to_group (self, individual, group);
1019 else
1021 remove_from_group (self, individual, group);
1025 static void
1026 empathy_roster_view_constructed (GObject *object)
1028 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1029 void (*chain_up) (GObject *) =
1030 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1032 if (chain_up != NULL)
1033 chain_up (object);
1035 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1037 /* Get saved group states. */
1038 empathy_contact_groups_get_all ();
1040 populate_view (self);
1042 tp_g_signal_connect_object (self->priv->model, "individual-added",
1043 G_CALLBACK (individual_added_cb), self, 0);
1044 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1045 G_CALLBACK (individual_removed_cb), self, 0);
1046 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1047 G_CALLBACK (groups_changed_cb), self, 0);
1049 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1050 roster_view_sort, self, NULL);
1052 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1053 self, NULL);
1055 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1057 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1060 static void
1061 clear_view (EmpathyRosterView *self)
1063 g_hash_table_remove_all (self->priv->roster_contacts);
1064 g_hash_table_remove_all (self->priv->roster_groups);
1065 g_hash_table_remove_all (self->priv->displayed_contacts);
1067 gtk_container_foreach (GTK_CONTAINER (self),
1068 (GtkCallback) gtk_widget_destroy, NULL);
1071 static void
1072 empathy_roster_view_dispose (GObject *object)
1074 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1075 void (*chain_up) (GObject *) =
1076 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1078 /* Start by clearing the view so our internal hash tables are cleared from
1079 * objects being destroyed. */
1080 clear_view (self);
1082 stop_flashing (self);
1084 empathy_roster_view_set_live_search (self, NULL);
1085 g_clear_object (&self->priv->model);
1087 if (self->priv->search_id != 0)
1089 g_source_remove (self->priv->search_id);
1090 self->priv->search_id = 0;
1093 if (chain_up != NULL)
1094 chain_up (object);
1097 static void
1098 empathy_roster_view_finalize (GObject *object)
1100 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1101 void (*chain_up) (GObject *) =
1102 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1104 g_hash_table_unref (self->priv->roster_contacts);
1105 g_hash_table_unref (self->priv->roster_groups);
1106 g_hash_table_unref (self->priv->displayed_contacts);
1107 g_queue_free_full (self->priv->events, event_free);
1109 if (chain_up != NULL)
1110 chain_up (object);
1113 static void
1114 empathy_roster_view_child_activated (EggListBox *box,
1115 GtkWidget *child)
1117 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1118 EmpathyRosterContact *contact;
1119 FolksIndividual *individual;
1120 GList *l;
1122 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1123 return;
1125 contact = EMPATHY_ROSTER_CONTACT (child);
1126 individual = empathy_roster_contact_get_individual (contact);
1128 /* Activate the oldest event associated with this contact, if any */
1129 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1130 l = g_list_previous (l))
1132 Event *event = l->data;
1134 if (event->individual == individual)
1136 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1137 event->user_data);
1138 return;
1142 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1145 static void
1146 fire_popup_individual_menu (EmpathyRosterView *self,
1147 GtkWidget *child,
1148 guint button,
1149 guint time)
1151 EmpathyRosterContact *contact;
1152 FolksIndividual *individual;
1154 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1155 return;
1157 contact = EMPATHY_ROSTER_CONTACT (child);
1158 individual = empathy_roster_contact_get_individual (contact);
1160 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1161 individual, button, time);
1164 static gboolean
1165 empathy_roster_view_button_press_event (GtkWidget *widget,
1166 GdkEventButton *event)
1168 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1169 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1170 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1172 if (event->button == 3)
1174 GtkWidget *child;
1176 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1178 if (child != NULL)
1180 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1182 fire_popup_individual_menu (self, child, event->button, event->time);
1186 return chain_up (widget, event);
1189 static gboolean
1190 empathy_roster_view_key_press_event (GtkWidget *widget,
1191 GdkEventKey *event)
1193 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1194 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1195 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1197 if (event->keyval == GDK_KEY_Menu)
1199 GtkWidget *child;
1201 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1203 if (child != NULL)
1204 fire_popup_individual_menu (self, child, 0, event->time);
1207 return chain_up (widget, event);
1211 * @out_child: (out) (allow-none)
1213 FolksIndividual *
1214 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1215 gint y,
1216 GtkWidget **out_child)
1218 GtkWidget *child;
1220 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1222 if (out_child != NULL)
1223 *out_child = child;
1225 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1226 return NULL;
1228 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1232 * @out_child: (out) (allow-none)
1234 const gchar *
1235 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1236 gint y)
1238 GtkWidget *child;
1240 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1242 if (EMPATHY_IS_ROSTER_CONTACT (child))
1243 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1244 else if (EMPATHY_IS_ROSTER_GROUP (child))
1245 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1247 return NULL;
1250 static gboolean
1251 empathy_roster_view_query_tooltip (GtkWidget *widget,
1252 gint x,
1253 gint y,
1254 gboolean keyboard_mode,
1255 GtkTooltip *tooltip)
1257 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1258 FolksIndividual *individual;
1259 gboolean result;
1260 GtkWidget *child;
1262 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1263 if (individual == NULL)
1264 return FALSE;
1266 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1267 individual, keyboard_mode, tooltip, &result);
1269 if (result)
1271 GtkAllocation allocation;
1273 gtk_widget_get_allocation (child, &allocation);
1274 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1277 return result;
1280 static void
1281 empathy_roster_view_remove (GtkContainer *container,
1282 GtkWidget *widget)
1284 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1285 void (*chain_up) (GtkContainer *, GtkWidget *) =
1286 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1288 chain_up (container, widget);
1290 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1291 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1294 static void
1295 empathy_roster_view_class_init (
1296 EmpathyRosterViewClass *klass)
1298 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1299 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1300 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1301 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1302 GParamSpec *spec;
1304 oclass->get_property = empathy_roster_view_get_property;
1305 oclass->set_property = empathy_roster_view_set_property;
1306 oclass->constructed = empathy_roster_view_constructed;
1307 oclass->dispose = empathy_roster_view_dispose;
1308 oclass->finalize = empathy_roster_view_finalize;
1310 widget_class->button_press_event = empathy_roster_view_button_press_event;
1311 widget_class->key_press_event = empathy_roster_view_key_press_event;
1312 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1314 container_class->remove = empathy_roster_view_remove;
1316 box_class->child_activated = empathy_roster_view_child_activated;
1318 spec = g_param_spec_object ("model", "Model",
1319 "EmpathyRosterModel",
1320 EMPATHY_TYPE_ROSTER_MODEL,
1321 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1322 g_object_class_install_property (oclass, PROP_MODEL, spec);
1324 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1325 "Show offline contacts",
1326 FALSE,
1327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1328 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1330 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1331 "Show groups",
1332 FALSE,
1333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1334 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1336 spec = g_param_spec_boolean ("empty", "Empty",
1337 "Is the view currently empty?",
1338 FALSE,
1339 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1340 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1342 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1343 G_OBJECT_CLASS_TYPE (klass),
1344 G_SIGNAL_RUN_LAST,
1345 0, NULL, NULL, NULL,
1346 G_TYPE_NONE,
1347 1, FOLKS_TYPE_INDIVIDUAL);
1349 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1350 G_OBJECT_CLASS_TYPE (klass),
1351 G_SIGNAL_RUN_LAST,
1352 0, NULL, NULL, NULL,
1353 G_TYPE_NONE,
1354 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1356 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1357 G_OBJECT_CLASS_TYPE (klass),
1358 G_SIGNAL_RUN_LAST,
1359 0, NULL, NULL, NULL,
1360 G_TYPE_NONE,
1361 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1363 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1364 G_OBJECT_CLASS_TYPE (klass),
1365 G_SIGNAL_RUN_LAST,
1366 0, g_signal_accumulator_true_handled, NULL, NULL,
1367 G_TYPE_BOOLEAN,
1368 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1370 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1373 static void
1374 empathy_roster_view_init (EmpathyRosterView *self)
1376 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1377 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1379 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1380 NULL, (GDestroyNotify) g_hash_table_unref);
1381 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1382 g_free, NULL);
1383 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1385 self->priv->events = g_queue_new ();
1387 self->priv->empty = TRUE;
1390 GtkWidget *
1391 empathy_roster_view_new (EmpathyRosterModel *model)
1393 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1395 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1396 "model", model,
1397 NULL);
1400 void
1401 empathy_roster_view_show_offline (EmpathyRosterView *self,
1402 gboolean show)
1404 if (self->priv->show_offline == show)
1405 return;
1407 self->priv->show_offline = show;
1408 egg_list_box_refilter (EGG_LIST_BOX (self));
1410 g_object_notify (G_OBJECT (self), "show-offline");
1413 void
1414 empathy_roster_view_show_groups (EmpathyRosterView *self,
1415 gboolean show)
1417 if (self->priv->show_groups == show)
1418 return;
1420 self->priv->show_groups = show;
1422 /* TODO: block sort/filter? */
1423 clear_view (self);
1424 populate_view (self);
1426 g_object_notify (G_OBJECT (self), "show-groups");
1429 static void
1430 select_first_contact (EmpathyRosterView *self)
1432 GList *children, *l;
1434 children = gtk_container_get_children (GTK_CONTAINER (self));
1435 for (l = children; l != NULL; l = g_list_next (l))
1437 GtkWidget *child = l->data;
1439 if (!gtk_widget_get_child_visible (child))
1440 continue;
1442 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1443 continue;
1445 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1446 break;
1449 g_list_free (children);
1452 static gboolean
1453 search_timeout_cb (EmpathyRosterView *self)
1455 egg_list_box_refilter (EGG_LIST_BOX (self));
1457 select_first_contact (self);
1459 self->priv->search_id = 0;
1460 return G_SOURCE_REMOVE;
1463 static void
1464 search_text_notify_cb (EmpathyLiveSearch *search,
1465 GParamSpec *pspec,
1466 EmpathyRosterView *self)
1468 if (self->priv->search_id != 0)
1469 g_source_remove (self->priv->search_id);
1471 self->priv->search_id = g_timeout_add (SEARCH_TIMEOUT,
1472 (GSourceFunc) search_timeout_cb, self);
1475 static void
1476 search_activate_cb (GtkWidget *search,
1477 EmpathyRosterView *self)
1479 EggListBox *box = EGG_LIST_BOX (self);
1480 GtkWidget *child;
1482 child = egg_list_box_get_selected_child (box);
1483 if (child == NULL)
1484 return;
1486 empathy_roster_view_child_activated (box, child);
1489 void
1490 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1491 EmpathyLiveSearch *search)
1493 if (self->priv->search != NULL)
1495 g_signal_handlers_disconnect_by_func (self->priv->search,
1496 search_text_notify_cb, self);
1497 g_signal_handlers_disconnect_by_func (self->priv->search,
1498 search_activate_cb, self);
1500 g_clear_object (&self->priv->search);
1503 if (search == NULL)
1504 return;
1506 self->priv->search = g_object_ref (search);
1508 g_signal_connect (self->priv->search, "notify::text",
1509 G_CALLBACK (search_text_notify_cb), self);
1510 g_signal_connect (self->priv->search, "activate",
1511 G_CALLBACK (search_activate_cb), self);
1514 gboolean
1515 empathy_roster_view_is_empty (EmpathyRosterView *self)
1517 return self->priv->empty;
1520 gboolean
1521 empathy_roster_view_is_searching (EmpathyRosterView *self)
1523 return (self->priv->search != NULL &&
1524 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1527 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1528 * Empathy's internals. */
1529 guint
1530 empathy_roster_view_add_event (EmpathyRosterView *self,
1531 FolksIndividual *individual,
1532 const gchar *icon,
1533 gpointer user_data)
1535 GHashTable *contacts;
1537 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1538 if (contacts == NULL)
1539 return 0;
1541 self->priv->last_event_id++;
1543 g_queue_push_head (self->priv->events,
1544 event_new (self->priv->last_event_id, individual, icon, user_data));
1546 start_flashing (self);
1548 return self->priv->last_event_id;
1551 void
1552 empathy_roster_view_remove_event (EmpathyRosterView *self,
1553 guint event_id)
1555 GList *l;
1557 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1558 l = g_list_next (l))
1560 Event *event = l->data;
1562 if (event->id == event_id)
1564 remove_event (self, event);
1565 return;
1570 FolksIndividual *
1571 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1573 GtkWidget *child;
1575 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1577 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1578 return NULL;
1580 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));