3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy-gtk/empathy-roster-contact.h>
8 #include <libempathy-gtk/empathy-roster-group.h>
9 #include <libempathy-gtk/empathy-ui-utils.h>
11 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
27 SIG_INDIVIDUAL_ACTIVATED,
28 SIG_POPUP_INDIVIDUAL_MENU,
33 static guint signals[LAST_SIGNAL];
35 #define NO_GROUP "X-no-group"
36 #define UNGROUPED _("Ungrouped")
37 #define TOP_GROUP _("Top Contacts")
39 struct _EmpathyRosterViewPriv
41 EmpathyIndividualManager *manager;
43 /* FolksIndividual (borrowed) -> GHashTable (
44 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
46 * When not using groups, this hash just have one element mapped
47 * from the special NO_GROUP key. We could use it as a set but
48 * I prefer to stay coherent in the way this hash is managed.
50 GHashTable *roster_contacts;
51 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
52 GHashTable *roster_groups;
53 /* Hash of the EmpathyRosterContact currently displayed */
54 GHashTable *displayed_contacts;
57 /* queue of (Event *). The most recent events are in the head of the queue
58 * so we always display the icon of the oldest one. */
61 gboolean display_flash_event;
63 gboolean show_offline;
67 EmpathyLiveSearch *search;
69 EmpathyRosterViewIndividualTooltipCb individual_tooltip_cb;
70 gpointer individual_tooltip_data;
76 FolksIndividual *individual;
83 FolksIndividual *individual,
87 Event *event = g_slice_new (Event);
90 event->individual = g_object_ref (individual);
91 event->icon = g_strdup (icon);
92 event->user_data = user_data;
97 event_free (gpointer data)
100 g_object_unref (event->individual);
101 g_free (event->icon);
103 g_slice_free (Event, event);
107 empathy_roster_view_get_property (GObject *object,
112 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
117 g_value_set_object (value, self->priv->manager);
119 case PROP_SHOW_OFFLINE:
120 g_value_set_boolean (value, self->priv->show_offline);
122 case PROP_SHOW_GROUPS:
123 g_value_set_boolean (value, self->priv->show_groups);
126 g_value_set_boolean (value, self->priv->empty);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
135 empathy_roster_view_set_property (GObject *object,
140 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
145 g_assert (self->priv->manager == NULL); /* construct only */
146 self->priv->manager = g_value_dup_object (value);
148 case PROP_SHOW_OFFLINE:
149 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
151 case PROP_SHOW_GROUPS:
152 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
161 roster_contact_changed_cb (GtkWidget *child,
163 EmpathyRosterView *self)
165 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
169 add_roster_contact (EmpathyRosterView *self,
170 FolksIndividual *individual,
175 contact = empathy_roster_contact_new (individual, group);
177 /* Need to refilter if online is changed */
178 g_signal_connect (contact, "notify::online",
179 G_CALLBACK (roster_contact_changed_cb), self);
181 /* Need to resort if alias is changed */
182 g_signal_connect (contact, "notify::alias",
183 G_CALLBACK (roster_contact_changed_cb), self);
185 gtk_widget_show (contact);
186 gtk_container_add (GTK_CONTAINER (self), contact);
192 group_expanded_cb (EmpathyRosterGroup *group,
194 EmpathyRosterView *self)
198 widgets = empathy_roster_group_get_widgets (group);
199 for (l = widgets; l != NULL; l = g_list_next (l))
201 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
204 g_list_free (widgets);
207 static EmpathyRosterGroup *
208 lookup_roster_group (EmpathyRosterView *self,
211 return g_hash_table_lookup (self->priv->roster_groups, group);
214 static EmpathyRosterGroup *
215 ensure_roster_group (EmpathyRosterView *self,
218 GtkWidget *roster_group;
220 roster_group = (GtkWidget *) lookup_roster_group (self, group);
221 if (roster_group != NULL)
222 return EMPATHY_ROSTER_GROUP (roster_group);
224 if (!tp_strdiff (group, TOP_GROUP))
225 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
227 roster_group = empathy_roster_group_new (group, NULL);
229 g_signal_connect (roster_group, "notify::expanded",
230 G_CALLBACK (group_expanded_cb), self);
232 gtk_widget_show (roster_group);
233 gtk_container_add (GTK_CONTAINER (self), roster_group);
235 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
238 return EMPATHY_ROSTER_GROUP (roster_group);
242 update_group_widgets (EmpathyRosterView *self,
243 EmpathyRosterGroup *group,
244 EmpathyRosterContact *contact,
247 guint old_count, count;
249 old_count = empathy_roster_group_get_widgets_count (group);
252 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
254 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
256 if (count != old_count)
257 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
261 add_to_group (EmpathyRosterView *self,
262 FolksIndividual *individual,
266 GHashTable *contacts;
267 EmpathyRosterGroup *roster_group = NULL;
269 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
270 if (contacts == NULL)
273 if (tp_strdiff (group, NO_GROUP))
274 roster_group = ensure_roster_group (self, group);
276 contact = add_roster_contact (self, individual, group);
277 g_hash_table_insert (contacts, g_strdup (group), contact);
279 if (roster_group != NULL)
281 update_group_widgets (self, roster_group,
282 EMPATHY_ROSTER_CONTACT (contact), TRUE);
287 individual_added (EmpathyRosterView *self,
288 FolksIndividual *individual)
290 GHashTable *contacts;
292 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
293 if (contacts != NULL)
296 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
298 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
300 if (!self->priv->show_groups)
302 add_to_group (self, individual, NO_GROUP);
308 if (folks_favourite_details_get_is_favourite (
309 FOLKS_FAVOURITE_DETAILS (individual)))
311 add_to_group (self, individual, TOP_GROUP);
314 groups = folks_group_details_get_groups (
315 FOLKS_GROUP_DETAILS (individual));
317 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
319 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
321 while (iter != NULL && gee_iterator_next (iter))
323 gchar *group = gee_iterator_get (iter);
325 add_to_group (self, individual, group);
330 g_clear_object (&iter);
334 /* No group, adds to Ungrouped */
335 add_to_group (self, individual, UNGROUPED);
341 set_event_icon_on_individual (EmpathyRosterView *self,
342 FolksIndividual *individual,
345 GHashTable *contacts;
349 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
350 if (contacts == NULL)
353 g_hash_table_iter_init (&iter, contacts);
354 while (g_hash_table_iter_next (&iter, NULL, &v))
356 EmpathyRosterContact *contact =v;
358 empathy_roster_contact_set_event_icon (contact, icon);
363 flash_event (Event *event,
364 EmpathyRosterView *self)
366 set_event_icon_on_individual (self, event->individual, event->icon);
370 unflash_event (Event *event,
371 EmpathyRosterView *self)
373 set_event_icon_on_individual (self, event->individual, NULL);
377 flash_cb (gpointer data)
379 EmpathyRosterView *self = data;
381 if (self->priv->display_flash_event)
383 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
384 self->priv->display_flash_event = FALSE;
388 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
389 self->priv->display_flash_event = TRUE;
396 start_flashing (EmpathyRosterView *self)
398 if (self->priv->flash_id != 0)
401 self->priv->display_flash_event = TRUE;
403 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
408 stop_flashing (EmpathyRosterView *self)
410 if (self->priv->flash_id == 0)
413 g_source_remove (self->priv->flash_id);
414 self->priv->flash_id = 0;
418 remove_event (EmpathyRosterView *self,
421 unflash_event (event, self);
422 g_queue_remove (self->priv->events, event);
424 if (g_queue_get_length (self->priv->events) == 0)
426 stop_flashing (self);
431 remove_all_individual_event (EmpathyRosterView *self,
432 FolksIndividual *individual)
436 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
439 Event *event = l->data;
441 if (event->individual == individual)
443 remove_event (self, event);
450 individual_removed (EmpathyRosterView *self,
451 FolksIndividual *individual)
453 GHashTable *contacts;
457 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
458 if (contacts == NULL)
461 remove_all_individual_event (self, individual);
463 g_hash_table_iter_init (&iter, contacts);
464 while (g_hash_table_iter_next (&iter, &key, &value))
466 const gchar *group_name = key;
467 GtkWidget *contact = value;
468 EmpathyRosterGroup *group;
470 group = lookup_roster_group (self, group_name);
473 update_group_widgets (self, group,
474 EMPATHY_ROSTER_CONTACT (contact), FALSE);
477 gtk_container_remove (GTK_CONTAINER (self), contact);
480 g_hash_table_remove (self->priv->roster_contacts, individual);
484 members_changed_cb (EmpathyIndividualManager *manager,
485 const gchar *message,
488 TpChannelGroupChangeReason reason,
489 EmpathyRosterView *self)
493 for (l = added; l != NULL; l = g_list_next (l))
495 FolksIndividual *individual = l->data;
497 individual_added (self, individual);
500 for (l = removed; l != NULL; l = g_list_next (l))
502 FolksIndividual *individual = l->data;
504 individual_removed (self, individual);
509 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
510 EmpathyRosterContact *b)
512 FolksIndividual *ind_a, *ind_b;
513 const gchar *alias_a, *alias_b;
515 ind_a = empathy_roster_contact_get_individual (a);
516 ind_b = empathy_roster_contact_get_individual (b);
518 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
519 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
521 return g_ascii_strcasecmp (alias_a, alias_b);
525 contact_is_favourite (EmpathyRosterContact *contact)
527 FolksIndividual *individual;
529 individual = empathy_roster_contact_get_individual (contact);
531 return folks_favourite_details_get_is_favourite (
532 FOLKS_FAVOURITE_DETAILS (individual));
536 contact_in_top (EmpathyRosterView *self,
537 EmpathyRosterContact *contact)
539 FolksIndividual *individual;
542 if (contact_is_favourite (contact))
545 individual = empathy_roster_contact_get_individual (contact);
547 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
549 if (g_list_index (tops, individual) != -1)
556 compare_roster_contacts_no_group (EmpathyRosterView *self,
557 EmpathyRosterContact *a,
558 EmpathyRosterContact *b)
560 gboolean top_a, top_b;
562 top_a = contact_in_top (self, a);
563 top_b = contact_in_top (self, b);
566 /* Both contacts are in the top of the roster (or not). Sort them
568 return compare_roster_contacts_by_alias (a, b);
576 compare_group_names (const gchar *group_a,
577 const gchar *group_b)
579 if (!tp_strdiff (group_a, TOP_GROUP))
582 if (!tp_strdiff (group_b, TOP_GROUP))
585 return g_ascii_strcasecmp (group_a, group_b);
589 compare_roster_contacts_with_groups (EmpathyRosterView *self,
590 EmpathyRosterContact *a,
591 EmpathyRosterContact *b)
593 const gchar *group_a, *group_b;
595 group_a = empathy_roster_contact_get_group (a);
596 group_b = empathy_roster_contact_get_group (b);
598 if (!tp_strdiff (group_a, group_b))
599 /* Same group, compare the contacts */
600 return compare_roster_contacts_by_alias (a, b);
603 return compare_group_names (group_a, group_b);
607 compare_roster_contacts (EmpathyRosterView *self,
608 EmpathyRosterContact *a,
609 EmpathyRosterContact *b)
611 if (!self->priv->show_groups)
612 return compare_roster_contacts_no_group (self, a, b);
614 return compare_roster_contacts_with_groups (self, a, b);
618 compare_roster_groups (EmpathyRosterGroup *a,
619 EmpathyRosterGroup *b)
621 const gchar *name_a, *name_b;
623 name_a = empathy_roster_group_get_name (a);
624 name_b = empathy_roster_group_get_name (b);
626 return compare_group_names (name_a, name_b);
630 compare_contact_group (EmpathyRosterContact *contact,
631 EmpathyRosterGroup *group)
633 const char *contact_group, *group_name;
635 contact_group = empathy_roster_contact_get_group (contact);
636 group_name = empathy_roster_group_get_name (group);
638 if (!tp_strdiff (contact_group, group_name))
639 /* @contact is in @group, @group has to be displayed first */
642 /* @contact is in a different group, sort by group name */
643 return compare_group_names (contact_group, group_name);
647 roster_view_sort (gconstpointer a,
651 EmpathyRosterView *self = user_data;
653 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
654 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
655 EMPATHY_ROSTER_CONTACT (b));
656 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
657 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
658 EMPATHY_ROSTER_GROUP (b));
659 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
660 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
661 EMPATHY_ROSTER_GROUP (b));
662 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
663 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
664 EMPATHY_ROSTER_GROUP (a));
666 g_return_val_if_reached (0);
670 update_separator (GtkWidget **separator,
677 /* No separator before the first row */
678 g_clear_object (separator);
682 if (*separator != NULL)
685 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
686 g_object_ref_sink (*separator);
690 is_searching (EmpathyRosterView *self)
692 if (self->priv->search == NULL)
695 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
699 update_empty (EmpathyRosterView *self,
702 if (self->priv->empty == empty)
705 self->priv->empty = empty;
706 g_object_notify (G_OBJECT (self), "empty");
710 add_to_displayed (EmpathyRosterView *self,
711 EmpathyRosterContact *contact)
713 FolksIndividual *individual;
714 GHashTable *contacts;
718 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
721 g_hash_table_add (self->priv->displayed_contacts, contact);
722 update_empty (self, FALSE);
724 /* Groups of this contact may now be displayed if we just displays the first
725 * child in this group. */
726 individual = empathy_roster_contact_get_individual (contact);
727 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
728 if (contacts == NULL)
731 g_hash_table_iter_init (&iter, contacts);
732 while (g_hash_table_iter_next (&iter, NULL, &v))
734 GtkWidget *group = GTK_WIDGET (v);
736 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
741 remove_from_displayed (EmpathyRosterView *self,
742 EmpathyRosterContact *contact)
744 g_hash_table_remove (self->priv->displayed_contacts, contact);
746 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
747 update_empty (self, TRUE);
751 * check if @contact should be displayed according to @self's current status
752 * and without consideration for the state of @contact's groups.
755 contact_should_be_displayed (EmpathyRosterView *self,
756 EmpathyRosterContact *contact)
758 if (is_searching (self))
760 FolksIndividual *individual;
762 individual = empathy_roster_contact_get_individual (contact);
764 return empathy_individual_match_string (individual,
765 empathy_live_search_get_text (self->priv->search),
766 empathy_live_search_get_words (self->priv->search));
770 if (self->priv->show_offline)
774 else if (!self->priv->show_groups &&
775 contact_is_favourite (contact))
777 /* Always display favourite contacts in non-group mode. In the group
778 * mode we'll display only the one in the 'top' section. */
783 return empathy_roster_contact_is_online (contact);
789 filter_contact (EmpathyRosterView *self,
790 EmpathyRosterContact *contact)
794 displayed = contact_should_be_displayed (self, contact);
796 if (self->priv->show_groups)
798 const gchar *group_name;
799 EmpathyRosterGroup *group;
801 group_name = empathy_roster_contact_get_group (contact);
802 group = lookup_roster_group (self, group_name);
804 if (!tp_strdiff (group_name, TOP_GROUP) &&
805 contact_is_favourite (contact))
810 /* When searching, always display even if the group is closed */
811 if (!is_searching (self) &&
812 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
819 add_to_displayed (self, contact);
823 remove_from_displayed (self, contact);
830 filter_group (EmpathyRosterView *self,
831 EmpathyRosterGroup *group)
835 /* Display the group if it contains at least one displayed contact */
836 widgets = empathy_roster_group_get_widgets (group);
837 for (l = widgets; l != NULL; l = g_list_next (l))
839 EmpathyRosterContact *contact = l->data;
841 if (contact_should_be_displayed (self, contact))
849 filter_list (GtkWidget *child,
852 EmpathyRosterView *self = user_data;
854 if (EMPATHY_IS_ROSTER_CONTACT (child))
855 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
857 else if (EMPATHY_IS_ROSTER_GROUP (child))
858 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
860 g_return_val_if_reached (FALSE);
863 /* @list: GList of EmpathyRosterContact
865 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
868 individual_in_list (FolksIndividual *individual,
873 for (l = list; l != NULL; l = g_list_next (l))
875 EmpathyRosterContact *contact = l->data;
877 if (empathy_roster_contact_get_individual (contact) == individual)
885 populate_view (EmpathyRosterView *self)
887 GList *individuals, *l;
889 individuals = empathy_individual_manager_get_members (self->priv->manager);
890 for (l = individuals; l != NULL; l = g_list_next (l))
892 FolksIndividual *individual = l->data;
894 individual_added (self, individual);
897 g_list_free (individuals);
901 remove_from_group (EmpathyRosterView *self,
902 FolksIndividual *individual,
905 GHashTable *contacts;
907 EmpathyRosterGroup *roster_group;
909 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
910 if (contacts == NULL)
913 contact = g_hash_table_lookup (contacts, group);
917 g_hash_table_remove (contacts, group);
919 if (g_hash_table_size (contacts) == 0)
921 add_to_group (self, individual, UNGROUPED);
924 roster_group = lookup_roster_group (self, group);
926 if (roster_group != NULL)
928 update_group_widgets (self, roster_group,
929 EMPATHY_ROSTER_CONTACT (contact), FALSE);
932 gtk_container_remove (GTK_CONTAINER (self), contact);
936 update_top_contacts (EmpathyRosterView *self)
939 GList *to_add = NULL, *to_remove = NULL;
940 EmpathyRosterGroup *group;
942 if (!self->priv->show_groups)
944 egg_list_box_resort (EGG_LIST_BOX (self));
948 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
950 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
953 to_add = g_list_copy (tops);
959 contacts = empathy_roster_group_get_widgets (group);
961 /* Check which EmpathyRosterContact have to be removed */
962 for (l = contacts; l != NULL; l = g_list_next (l))
964 EmpathyRosterContact *contact = l->data;
965 FolksIndividual *individual;
967 if (contact_is_favourite (contact))
970 individual = empathy_roster_contact_get_individual (contact);
972 if (g_list_find (tops, individual) == NULL)
973 to_remove = g_list_prepend (to_remove, individual);
976 /* Check which EmpathyRosterContact have to be added */
977 for (l = tops; l != NULL; l = g_list_next (l))
979 FolksIndividual *individual = l->data;
981 if (!individual_in_list (individual, contacts))
982 to_add = g_list_prepend (to_add, individual);
986 for (l = to_add; l != NULL; l = g_list_next (l))
987 add_to_group (self, l->data, TOP_GROUP);
989 for (l = to_remove; l != NULL; l = g_list_next (l))
990 remove_from_group (self, l->data, TOP_GROUP);
992 g_list_free (to_add);
993 g_list_free (to_remove);
997 groups_changed_cb (EmpathyIndividualManager *manager,
998 FolksIndividual *individual,
1001 EmpathyRosterView *self)
1003 if (!self->priv->show_groups)
1008 add_to_group (self, individual, group);
1012 remove_from_group (self, individual, group);
1017 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1019 EmpathyRosterView *self)
1021 update_top_contacts (self);
1025 favourites_changed_cb (EmpathyIndividualManager *manager,
1026 FolksIndividual *individual,
1028 EmpathyRosterView *self)
1030 GHashTable *contacts;
1032 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1033 if (contacts == NULL)
1036 if (self->priv->show_groups)
1039 add_to_group (self, individual, TOP_GROUP);
1041 remove_from_group (self, individual, TOP_GROUP);
1047 contact = g_hash_table_lookup (contacts, NO_GROUP);
1048 if (contact == NULL)
1051 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1056 empathy_roster_view_constructed (GObject *object)
1058 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1059 void (*chain_up) (GObject *) =
1060 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1062 if (chain_up != NULL)
1065 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1067 populate_view (self);
1069 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1070 G_CALLBACK (members_changed_cb), self, 0);
1071 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1072 G_CALLBACK (groups_changed_cb), self, 0);
1073 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1074 G_CALLBACK (top_individuals_changed_cb), self, 0);
1075 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1076 G_CALLBACK (favourites_changed_cb), self, 0);
1078 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1079 roster_view_sort, self, NULL);
1081 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1084 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1086 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1090 empathy_roster_view_dispose (GObject *object)
1092 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1093 void (*chain_up) (GObject *) =
1094 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1096 stop_flashing (self);
1098 empathy_roster_view_set_live_search (self, NULL);
1099 g_clear_object (&self->priv->manager);
1101 if (chain_up != NULL)
1106 empathy_roster_view_finalize (GObject *object)
1108 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1109 void (*chain_up) (GObject *) =
1110 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1112 g_hash_table_unref (self->priv->roster_contacts);
1113 g_hash_table_unref (self->priv->roster_groups);
1114 g_hash_table_unref (self->priv->displayed_contacts);
1115 g_queue_free_full (self->priv->events, event_free);
1117 if (chain_up != NULL)
1122 empathy_roster_view_child_activated (EggListBox *box,
1125 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1126 EmpathyRosterContact *contact;
1127 FolksIndividual *individual;
1130 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1133 contact = EMPATHY_ROSTER_CONTACT (child);
1134 individual = empathy_roster_contact_get_individual (contact);
1136 /* Activate the oldest event associated with this contact, if any */
1137 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1138 l = g_list_previous (l))
1140 Event *event = l->data;
1142 if (event->individual == individual)
1144 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1150 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1154 fire_popup_individual_menu (EmpathyRosterView *self,
1159 EmpathyRosterContact *contact;
1160 FolksIndividual *individual;
1162 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1165 contact = EMPATHY_ROSTER_CONTACT (child);
1166 individual = empathy_roster_contact_get_individual (contact);
1168 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1169 individual, button, time);
1173 empathy_roster_view_button_press_event (GtkWidget *widget,
1174 GdkEventButton *event)
1176 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1177 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1178 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1180 if (event->button == 3)
1184 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1187 fire_popup_individual_menu (self, child, event->button, event->time);
1190 return chain_up (widget, event);
1194 empathy_roster_view_key_press_event (GtkWidget *widget,
1197 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1198 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1199 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1201 if (event->keyval == GDK_KEY_Menu)
1205 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1208 fire_popup_individual_menu (self, child, 0, event->time);
1211 return chain_up (widget, event);
1215 empathy_roster_view_query_tooltip (GtkWidget *widget,
1218 gboolean keyboard_mode,
1219 GtkTooltip *tooltip)
1221 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1223 EmpathyRosterContact *contact;
1224 FolksIndividual *individual;
1226 if (self->priv->individual_tooltip_cb == NULL)
1229 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1230 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1233 contact = EMPATHY_ROSTER_CONTACT (child);
1234 individual = empathy_roster_contact_get_individual (contact);
1236 return self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
1237 tooltip, self->priv->individual_tooltip_data);
1241 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
1242 EmpathyRosterViewIndividualTooltipCb callback,
1245 self->priv->individual_tooltip_cb = callback;
1246 self->priv->individual_tooltip_data = user_data;
1248 gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
1252 empathy_roster_view_remove (GtkContainer *container,
1255 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1256 void (*chain_up) (GtkContainer *, GtkWidget *) =
1257 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1259 chain_up (container, widget);
1261 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1262 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1266 empathy_roster_view_class_init (
1267 EmpathyRosterViewClass *klass)
1269 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1270 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1271 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1272 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1275 oclass->get_property = empathy_roster_view_get_property;
1276 oclass->set_property = empathy_roster_view_set_property;
1277 oclass->constructed = empathy_roster_view_constructed;
1278 oclass->dispose = empathy_roster_view_dispose;
1279 oclass->finalize = empathy_roster_view_finalize;
1281 widget_class->button_press_event = empathy_roster_view_button_press_event;
1282 widget_class->key_press_event = empathy_roster_view_key_press_event;
1283 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1285 container_class->remove = empathy_roster_view_remove;
1287 box_class->child_activated = empathy_roster_view_child_activated;
1289 spec = g_param_spec_object ("manager", "Manager",
1290 "EmpathyIndividualManager",
1291 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1292 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1293 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1295 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1296 "Show offline contacts",
1298 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1299 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1301 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1305 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1307 spec = g_param_spec_boolean ("empty", "Empty",
1308 "Is the view currently empty?",
1310 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1311 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1313 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1314 G_OBJECT_CLASS_TYPE (klass),
1316 0, NULL, NULL, NULL,
1318 1, FOLKS_TYPE_INDIVIDUAL);
1320 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1321 G_OBJECT_CLASS_TYPE (klass),
1323 0, NULL, NULL, NULL,
1325 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1327 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1328 G_OBJECT_CLASS_TYPE (klass),
1330 0, NULL, NULL, NULL,
1332 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1334 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1338 empathy_roster_view_init (EmpathyRosterView *self)
1340 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1341 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1343 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1344 NULL, (GDestroyNotify) g_hash_table_unref);
1345 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1347 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1349 self->priv->events = g_queue_new ();
1351 self->priv->empty = TRUE;
1355 empathy_roster_view_new (EmpathyIndividualManager *manager)
1357 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1359 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1364 EmpathyIndividualManager *
1365 empathy_roster_view_get_manager (EmpathyRosterView *self)
1367 return self->priv->manager;
1371 empathy_roster_view_show_offline (EmpathyRosterView *self,
1374 if (self->priv->show_offline == show)
1377 self->priv->show_offline = show;
1378 egg_list_box_refilter (EGG_LIST_BOX (self));
1380 g_object_notify (G_OBJECT (self), "show-offline");
1384 clear_view (EmpathyRosterView *self)
1386 gtk_container_foreach (GTK_CONTAINER (self),
1387 (GtkCallback) gtk_widget_destroy, NULL);
1389 g_hash_table_remove_all (self->priv->roster_contacts);
1390 g_hash_table_remove_all (self->priv->roster_groups);
1391 g_hash_table_remove_all (self->priv->displayed_contacts);
1395 empathy_roster_view_show_groups (EmpathyRosterView *self,
1398 if (self->priv->show_groups == show)
1401 self->priv->show_groups = show;
1403 /* TODO: block sort/filter? */
1405 populate_view (self);
1407 g_object_notify (G_OBJECT (self), "show-groups");
1411 select_first_contact (EmpathyRosterView *self)
1413 GList *children, *l;
1415 children = gtk_container_get_children (GTK_CONTAINER (self));
1416 for (l = children; l != NULL; l = g_list_next (l))
1418 GtkWidget *child = l->data;
1420 if (!gtk_widget_get_child_visible (child))
1423 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1426 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1430 g_list_free (children);
1434 search_text_notify_cb (EmpathyLiveSearch *search,
1436 EmpathyRosterView *self)
1438 egg_list_box_refilter (EGG_LIST_BOX (self));
1440 select_first_contact (self);
1444 search_activate_cb (GtkWidget *search,
1445 EmpathyRosterView *self)
1447 EggListBox *box = EGG_LIST_BOX (self);
1450 child = egg_list_box_get_selected_child (box);
1454 empathy_roster_view_child_activated (box, child);
1458 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1459 EmpathyLiveSearch *search)
1461 if (self->priv->search != NULL)
1463 g_signal_handlers_disconnect_by_func (self->priv->search,
1464 search_text_notify_cb, self);
1465 g_signal_handlers_disconnect_by_func (self->priv->search,
1466 search_activate_cb, self);
1468 g_clear_object (&self->priv->search);
1474 self->priv->search = g_object_ref (search);
1476 g_signal_connect (self->priv->search, "notify::text",
1477 G_CALLBACK (search_text_notify_cb), self);
1478 g_signal_connect (self->priv->search, "activate",
1479 G_CALLBACK (search_activate_cb), self);
1483 empathy_roster_view_is_empty (EmpathyRosterView *self)
1485 return self->priv->empty;
1489 empathy_roster_view_is_searching (EmpathyRosterView *self)
1491 return (self->priv->search != NULL &&
1492 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1495 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1496 * Empathy's internals. */
1498 empathy_roster_view_add_event (EmpathyRosterView *self,
1499 FolksIndividual *individual,
1503 GHashTable *contacts;
1505 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1506 if (contacts == NULL)
1509 self->priv->last_event_id++;
1511 g_queue_push_head (self->priv->events,
1512 event_new (self->priv->last_event_id, individual, icon, user_data));
1514 start_flashing (self);
1516 return self->priv->last_event_id;
1520 empathy_roster_view_remove_event (EmpathyRosterView *self,
1525 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1526 l = g_list_next (l))
1528 Event *event = l->data;
1530 if (event->id == event_id)
1532 remove_event (self, event);