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 #include <libempathy/empathy-utils.h>
13 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
15 /* Flashing delay for icons (milliseconds). */
16 #define FLASH_TIMEOUT 500
29 SIG_INDIVIDUAL_ACTIVATED,
30 SIG_POPUP_INDIVIDUAL_MENU,
32 SIG_INDIVIDUAL_TOOLTIP,
36 static guint signals[LAST_SIGNAL];
38 #define NO_GROUP "X-no-group"
39 #define UNGROUPED _("Ungrouped")
40 #define TOP_GROUP _("Top Contacts")
41 #define PEOPLE_NEARBY _("People Nearby")
43 struct _EmpathyRosterViewPriv
45 EmpathyIndividualManager *manager;
47 /* FolksIndividual (borrowed) -> GHashTable (
48 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
50 * When not using groups, this hash just have one element mapped
51 * from the special NO_GROUP key. We could use it as a set but
52 * I prefer to stay coherent in the way this hash is managed.
54 GHashTable *roster_contacts;
55 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
56 GHashTable *roster_groups;
57 /* Hash of the EmpathyRosterContact currently displayed */
58 GHashTable *displayed_contacts;
61 /* queue of (Event *). The most recent events are in the head of the queue
62 * so we always display the icon of the oldest one. */
65 gboolean display_flash_event;
67 gboolean show_offline;
71 EmpathyLiveSearch *search;
77 FolksIndividual *individual;
84 FolksIndividual *individual,
88 Event *event = g_slice_new (Event);
91 event->individual = g_object_ref (individual);
92 event->icon = g_strdup (icon);
93 event->user_data = user_data;
98 event_free (gpointer data)
101 g_object_unref (event->individual);
102 g_free (event->icon);
104 g_slice_free (Event, event);
108 empathy_roster_view_get_property (GObject *object,
113 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
118 g_value_set_object (value, self->priv->manager);
120 case PROP_SHOW_OFFLINE:
121 g_value_set_boolean (value, self->priv->show_offline);
123 case PROP_SHOW_GROUPS:
124 g_value_set_boolean (value, self->priv->show_groups);
127 g_value_set_boolean (value, self->priv->empty);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
136 empathy_roster_view_set_property (GObject *object,
141 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
146 g_assert (self->priv->manager == NULL); /* construct only */
147 self->priv->manager = g_value_dup_object (value);
149 case PROP_SHOW_OFFLINE:
150 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
152 case PROP_SHOW_GROUPS:
153 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
162 roster_contact_changed_cb (GtkWidget *child,
164 EmpathyRosterView *self)
166 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
170 is_xmpp_local_contact (FolksIndividual *individual)
172 EmpathyContact *contact;
173 TpConnection *connection;
174 const gchar *protocol_name = NULL;
177 contact = empathy_contact_dup_from_folks_individual (individual);
182 connection = empathy_contact_get_connection (contact);
183 protocol_name = tp_connection_get_protocol_name (connection);
184 result = !tp_strdiff (protocol_name, "local-xmpp");
185 g_object_unref (contact);
191 add_roster_contact (EmpathyRosterView *self,
192 FolksIndividual *individual,
197 contact = empathy_roster_contact_new (individual, group);
199 /* Need to refilter if online is changed */
200 g_signal_connect (contact, "notify::online",
201 G_CALLBACK (roster_contact_changed_cb), self);
203 /* Need to resort if alias is changed */
204 g_signal_connect (contact, "notify::alias",
205 G_CALLBACK (roster_contact_changed_cb), self);
207 gtk_widget_show (contact);
208 gtk_container_add (GTK_CONTAINER (self), contact);
214 group_expanded_cb (EmpathyRosterGroup *group,
216 EmpathyRosterView *self)
220 widgets = empathy_roster_group_get_widgets (group);
221 for (l = widgets; l != NULL; l = g_list_next (l))
223 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
226 g_list_free (widgets);
229 static EmpathyRosterGroup *
230 lookup_roster_group (EmpathyRosterView *self,
233 return g_hash_table_lookup (self->priv->roster_groups, group);
236 static EmpathyRosterGroup *
237 ensure_roster_group (EmpathyRosterView *self,
240 GtkWidget *roster_group;
242 roster_group = (GtkWidget *) lookup_roster_group (self, group);
243 if (roster_group != NULL)
244 return EMPATHY_ROSTER_GROUP (roster_group);
246 if (!tp_strdiff (group, TOP_GROUP))
247 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
248 else if (!tp_strdiff (group, PEOPLE_NEARBY))
249 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
251 roster_group = empathy_roster_group_new (group, NULL);
253 g_signal_connect (roster_group, "notify::expanded",
254 G_CALLBACK (group_expanded_cb), self);
256 gtk_widget_show (roster_group);
257 gtk_container_add (GTK_CONTAINER (self), roster_group);
259 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
262 return EMPATHY_ROSTER_GROUP (roster_group);
266 update_group_widgets (EmpathyRosterView *self,
267 EmpathyRosterGroup *group,
268 EmpathyRosterContact *contact,
271 guint old_count, count;
273 old_count = empathy_roster_group_get_widgets_count (group);
276 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
278 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
280 if (count != old_count)
281 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
285 add_to_group (EmpathyRosterView *self,
286 FolksIndividual *individual,
290 GHashTable *contacts;
291 EmpathyRosterGroup *roster_group = NULL;
293 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
294 if (contacts == NULL)
297 if (tp_strdiff (group, NO_GROUP))
298 roster_group = ensure_roster_group (self, group);
300 contact = add_roster_contact (self, individual, group);
301 g_hash_table_insert (contacts, g_strdup (group), contact);
303 if (roster_group != NULL)
305 update_group_widgets (self, roster_group,
306 EMPATHY_ROSTER_CONTACT (contact), TRUE);
311 individual_added (EmpathyRosterView *self,
312 FolksIndividual *individual)
314 GHashTable *contacts;
316 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
317 if (contacts != NULL)
320 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
322 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
324 if (!self->priv->show_groups)
326 add_to_group (self, individual, NO_GROUP);
328 else if (is_xmpp_local_contact (individual))
330 add_to_group (self, individual, PEOPLE_NEARBY);
337 tops = empathy_individual_manager_get_top_individuals (
338 self->priv->manager);
340 if (folks_favourite_details_get_is_favourite (
341 FOLKS_FAVOURITE_DETAILS (individual)) ||
342 g_list_index (tops, individual) != -1)
344 add_to_group (self, individual, TOP_GROUP);
347 groups = folks_group_details_get_groups (
348 FOLKS_GROUP_DETAILS (individual));
350 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
352 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
354 while (iter != NULL && gee_iterator_next (iter))
356 gchar *group = gee_iterator_get (iter);
358 add_to_group (self, individual, group);
363 g_clear_object (&iter);
367 /* No group, adds to Ungrouped */
368 add_to_group (self, individual, UNGROUPED);
374 set_event_icon_on_individual (EmpathyRosterView *self,
375 FolksIndividual *individual,
378 GHashTable *contacts;
382 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
383 if (contacts == NULL)
386 g_hash_table_iter_init (&iter, contacts);
387 while (g_hash_table_iter_next (&iter, NULL, &v))
389 EmpathyRosterContact *contact =v;
391 empathy_roster_contact_set_event_icon (contact, icon);
396 flash_event (Event *event,
397 EmpathyRosterView *self)
399 set_event_icon_on_individual (self, event->individual, event->icon);
403 unflash_event (Event *event,
404 EmpathyRosterView *self)
406 set_event_icon_on_individual (self, event->individual, NULL);
410 flash_cb (gpointer data)
412 EmpathyRosterView *self = data;
414 if (self->priv->display_flash_event)
416 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
417 self->priv->display_flash_event = FALSE;
421 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
422 self->priv->display_flash_event = TRUE;
429 start_flashing (EmpathyRosterView *self)
431 if (self->priv->flash_id != 0)
434 self->priv->display_flash_event = TRUE;
436 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
441 stop_flashing (EmpathyRosterView *self)
443 if (self->priv->flash_id == 0)
446 g_source_remove (self->priv->flash_id);
447 self->priv->flash_id = 0;
451 remove_event (EmpathyRosterView *self,
454 unflash_event (event, self);
455 g_queue_remove (self->priv->events, event);
457 if (g_queue_get_length (self->priv->events) == 0)
459 stop_flashing (self);
464 remove_all_individual_event (EmpathyRosterView *self,
465 FolksIndividual *individual)
469 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
472 Event *event = l->data;
474 if (event->individual == individual)
476 remove_event (self, event);
483 individual_removed (EmpathyRosterView *self,
484 FolksIndividual *individual)
486 GHashTable *contacts;
490 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
491 if (contacts == NULL)
494 remove_all_individual_event (self, individual);
496 g_hash_table_iter_init (&iter, contacts);
497 while (g_hash_table_iter_next (&iter, &key, &value))
499 const gchar *group_name = key;
500 GtkWidget *contact = value;
501 EmpathyRosterGroup *group;
503 group = lookup_roster_group (self, group_name);
506 update_group_widgets (self, group,
507 EMPATHY_ROSTER_CONTACT (contact), FALSE);
510 gtk_container_remove (GTK_CONTAINER (self), contact);
513 g_hash_table_remove (self->priv->roster_contacts, individual);
517 members_changed_cb (EmpathyIndividualManager *manager,
518 const gchar *message,
521 TpChannelGroupChangeReason reason,
522 EmpathyRosterView *self)
526 for (l = added; l != NULL; l = g_list_next (l))
528 FolksIndividual *individual = l->data;
530 individual_added (self, individual);
533 for (l = removed; l != NULL; l = g_list_next (l))
535 FolksIndividual *individual = l->data;
537 individual_removed (self, individual);
542 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
543 EmpathyRosterContact *b)
545 FolksIndividual *ind_a, *ind_b;
546 const gchar *alias_a, *alias_b;
548 ind_a = empathy_roster_contact_get_individual (a);
549 ind_b = empathy_roster_contact_get_individual (b);
551 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
552 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
554 return g_ascii_strcasecmp (alias_a, alias_b);
558 contact_is_favourite (EmpathyRosterContact *contact)
560 FolksIndividual *individual;
562 individual = empathy_roster_contact_get_individual (contact);
564 return folks_favourite_details_get_is_favourite (
565 FOLKS_FAVOURITE_DETAILS (individual));
569 contact_in_top (EmpathyRosterView *self,
570 EmpathyRosterContact *contact)
572 FolksIndividual *individual;
575 if (contact_is_favourite (contact))
578 individual = empathy_roster_contact_get_individual (contact);
580 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
582 if (g_list_index (tops, individual) != -1)
589 compare_roster_contacts_no_group (EmpathyRosterView *self,
590 EmpathyRosterContact *a,
591 EmpathyRosterContact *b)
593 gboolean top_a, top_b;
595 top_a = contact_in_top (self, a);
596 top_b = contact_in_top (self, b);
599 /* Both contacts are in the top of the roster (or not). Sort them
601 return compare_roster_contacts_by_alias (a, b);
609 compare_group_names (const gchar *group_a,
610 const gchar *group_b)
612 if (!tp_strdiff (group_a, TOP_GROUP))
615 if (!tp_strdiff (group_b, TOP_GROUP))
618 if (!tp_strdiff (group_a, UNGROUPED))
620 else if (!tp_strdiff (group_b, UNGROUPED))
623 return g_ascii_strcasecmp (group_a, group_b);
627 compare_roster_contacts_with_groups (EmpathyRosterView *self,
628 EmpathyRosterContact *a,
629 EmpathyRosterContact *b)
631 const gchar *group_a, *group_b;
633 group_a = empathy_roster_contact_get_group (a);
634 group_b = empathy_roster_contact_get_group (b);
636 if (!tp_strdiff (group_a, group_b))
637 /* Same group, compare the contacts */
638 return compare_roster_contacts_by_alias (a, b);
641 return compare_group_names (group_a, group_b);
645 compare_roster_contacts (EmpathyRosterView *self,
646 EmpathyRosterContact *a,
647 EmpathyRosterContact *b)
649 if (!self->priv->show_groups)
650 return compare_roster_contacts_no_group (self, a, b);
652 return compare_roster_contacts_with_groups (self, a, b);
656 compare_roster_groups (EmpathyRosterGroup *a,
657 EmpathyRosterGroup *b)
659 const gchar *name_a, *name_b;
661 name_a = empathy_roster_group_get_name (a);
662 name_b = empathy_roster_group_get_name (b);
664 return compare_group_names (name_a, name_b);
668 compare_contact_group (EmpathyRosterContact *contact,
669 EmpathyRosterGroup *group)
671 const char *contact_group, *group_name;
673 contact_group = empathy_roster_contact_get_group (contact);
674 group_name = empathy_roster_group_get_name (group);
676 if (!tp_strdiff (contact_group, group_name))
677 /* @contact is in @group, @group has to be displayed first */
680 /* @contact is in a different group, sort by group name */
681 return compare_group_names (contact_group, group_name);
685 roster_view_sort (gconstpointer a,
689 EmpathyRosterView *self = user_data;
691 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
692 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
693 EMPATHY_ROSTER_CONTACT (b));
694 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
695 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
696 EMPATHY_ROSTER_GROUP (b));
697 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
698 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
699 EMPATHY_ROSTER_GROUP (b));
700 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
701 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
702 EMPATHY_ROSTER_GROUP (a));
704 g_return_val_if_reached (0);
708 update_separator (GtkWidget **separator,
715 /* No separator before the first row */
716 g_clear_object (separator);
720 if (*separator != NULL)
723 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
724 g_object_ref_sink (*separator);
728 is_searching (EmpathyRosterView *self)
730 if (self->priv->search == NULL)
733 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
737 update_empty (EmpathyRosterView *self,
740 if (self->priv->empty == empty)
743 self->priv->empty = empty;
744 g_object_notify (G_OBJECT (self), "empty");
748 add_to_displayed (EmpathyRosterView *self,
749 EmpathyRosterContact *contact)
751 FolksIndividual *individual;
752 GHashTable *contacts;
756 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
759 g_hash_table_add (self->priv->displayed_contacts, contact);
760 update_empty (self, FALSE);
762 /* Groups of this contact may now be displayed if we just displays the first
763 * child in this group. */
765 if (!self->priv->show_groups)
768 individual = empathy_roster_contact_get_individual (contact);
769 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
770 if (contacts == NULL)
773 g_hash_table_iter_init (&iter, contacts);
774 while (g_hash_table_iter_next (&iter, &k, NULL))
776 const gchar *group_name = k;
779 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
783 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
788 remove_from_displayed (EmpathyRosterView *self,
789 EmpathyRosterContact *contact)
791 g_hash_table_remove (self->priv->displayed_contacts, contact);
793 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
794 update_empty (self, TRUE);
798 * check if @contact should be displayed according to @self's current status
799 * and without consideration for the state of @contact's groups.
802 contact_should_be_displayed (EmpathyRosterView *self,
803 EmpathyRosterContact *contact)
805 if (is_searching (self))
807 FolksIndividual *individual;
809 individual = empathy_roster_contact_get_individual (contact);
811 return empathy_individual_match_string (individual,
812 empathy_live_search_get_text (self->priv->search),
813 empathy_live_search_get_words (self->priv->search));
817 if (self->priv->show_offline)
821 else if (!self->priv->show_groups &&
822 contact_is_favourite (contact))
824 /* Always display favourite contacts in non-group mode. In the group
825 * mode we'll display only the one in the 'top' section. */
830 return empathy_roster_contact_is_online (contact);
836 filter_contact (EmpathyRosterView *self,
837 EmpathyRosterContact *contact)
841 displayed = contact_should_be_displayed (self, contact);
843 if (self->priv->show_groups)
845 const gchar *group_name;
846 EmpathyRosterGroup *group;
848 group_name = empathy_roster_contact_get_group (contact);
849 group = lookup_roster_group (self, group_name);
851 if (!tp_strdiff (group_name, TOP_GROUP) &&
852 contact_is_favourite (contact))
857 /* When searching, always display even if the group is closed */
858 if (!is_searching (self) &&
859 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
866 add_to_displayed (self, contact);
870 remove_from_displayed (self, contact);
877 filter_group (EmpathyRosterView *self,
878 EmpathyRosterGroup *group)
882 /* Display the group if it contains at least one displayed contact */
883 widgets = empathy_roster_group_get_widgets (group);
884 for (l = widgets; l != NULL; l = g_list_next (l))
886 EmpathyRosterContact *contact = l->data;
888 if (contact_should_be_displayed (self, contact))
896 filter_list (GtkWidget *child,
899 EmpathyRosterView *self = user_data;
901 if (EMPATHY_IS_ROSTER_CONTACT (child))
902 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
904 else if (EMPATHY_IS_ROSTER_GROUP (child))
905 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
907 g_return_val_if_reached (FALSE);
910 /* @list: GList of EmpathyRosterContact
912 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
915 individual_in_list (FolksIndividual *individual,
920 for (l = list; l != NULL; l = g_list_next (l))
922 EmpathyRosterContact *contact = l->data;
924 if (empathy_roster_contact_get_individual (contact) == individual)
932 populate_view (EmpathyRosterView *self)
934 GList *individuals, *l;
936 individuals = empathy_individual_manager_get_members (self->priv->manager);
937 for (l = individuals; l != NULL; l = g_list_next (l))
939 FolksIndividual *individual = l->data;
941 individual_added (self, individual);
944 g_list_free (individuals);
948 remove_from_group (EmpathyRosterView *self,
949 FolksIndividual *individual,
952 GHashTable *contacts;
954 EmpathyRosterGroup *roster_group;
956 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
957 if (contacts == NULL)
960 contact = g_hash_table_lookup (contacts, group);
964 g_hash_table_remove (contacts, group);
966 if (g_hash_table_size (contacts) == 0)
968 add_to_group (self, individual, UNGROUPED);
971 roster_group = lookup_roster_group (self, group);
973 if (roster_group != NULL)
975 update_group_widgets (self, roster_group,
976 EMPATHY_ROSTER_CONTACT (contact), FALSE);
979 gtk_container_remove (GTK_CONTAINER (self), contact);
983 update_top_contacts (EmpathyRosterView *self)
986 GList *to_add = NULL, *to_remove = NULL;
987 EmpathyRosterGroup *group;
989 if (!self->priv->show_groups)
991 egg_list_box_resort (EGG_LIST_BOX (self));
995 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
997 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
1000 to_add = g_list_copy (tops);
1006 contacts = empathy_roster_group_get_widgets (group);
1008 /* Check which EmpathyRosterContact have to be removed */
1009 for (l = contacts; l != NULL; l = g_list_next (l))
1011 EmpathyRosterContact *contact = l->data;
1012 FolksIndividual *individual;
1014 if (contact_is_favourite (contact))
1017 individual = empathy_roster_contact_get_individual (contact);
1019 if (g_list_find (tops, individual) == NULL)
1020 to_remove = g_list_prepend (to_remove, individual);
1023 /* Check which EmpathyRosterContact have to be added */
1024 for (l = tops; l != NULL; l = g_list_next (l))
1026 FolksIndividual *individual = l->data;
1028 if (!individual_in_list (individual, contacts))
1029 to_add = g_list_prepend (to_add, individual);
1033 for (l = to_add; l != NULL; l = g_list_next (l))
1034 add_to_group (self, l->data, TOP_GROUP);
1036 for (l = to_remove; l != NULL; l = g_list_next (l))
1037 remove_from_group (self, l->data, TOP_GROUP);
1039 g_list_free (to_add);
1040 g_list_free (to_remove);
1044 groups_changed_cb (EmpathyIndividualManager *manager,
1045 FolksIndividual *individual,
1048 EmpathyRosterView *self)
1050 if (!self->priv->show_groups)
1055 add_to_group (self, individual, group);
1059 remove_from_group (self, individual, group);
1064 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1066 EmpathyRosterView *self)
1068 update_top_contacts (self);
1072 favourites_changed_cb (EmpathyIndividualManager *manager,
1073 FolksIndividual *individual,
1075 EmpathyRosterView *self)
1077 GHashTable *contacts;
1079 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1080 if (contacts == NULL)
1083 if (self->priv->show_groups)
1086 add_to_group (self, individual, TOP_GROUP);
1088 remove_from_group (self, individual, TOP_GROUP);
1094 contact = g_hash_table_lookup (contacts, NO_GROUP);
1095 if (contact == NULL)
1098 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1103 empathy_roster_view_constructed (GObject *object)
1105 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1106 void (*chain_up) (GObject *) =
1107 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1109 if (chain_up != NULL)
1112 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1114 populate_view (self);
1116 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1117 G_CALLBACK (members_changed_cb), self, 0);
1118 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1119 G_CALLBACK (groups_changed_cb), self, 0);
1120 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1121 G_CALLBACK (top_individuals_changed_cb), self, 0);
1122 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1123 G_CALLBACK (favourites_changed_cb), self, 0);
1125 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1126 roster_view_sort, self, NULL);
1128 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1131 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1133 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1137 empathy_roster_view_dispose (GObject *object)
1139 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1140 void (*chain_up) (GObject *) =
1141 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1143 stop_flashing (self);
1145 empathy_roster_view_set_live_search (self, NULL);
1146 g_clear_object (&self->priv->manager);
1148 if (chain_up != NULL)
1153 empathy_roster_view_finalize (GObject *object)
1155 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1156 void (*chain_up) (GObject *) =
1157 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1159 g_hash_table_unref (self->priv->roster_contacts);
1160 g_hash_table_unref (self->priv->roster_groups);
1161 g_hash_table_unref (self->priv->displayed_contacts);
1162 g_queue_free_full (self->priv->events, event_free);
1164 if (chain_up != NULL)
1169 empathy_roster_view_child_activated (EggListBox *box,
1172 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1173 EmpathyRosterContact *contact;
1174 FolksIndividual *individual;
1177 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1180 contact = EMPATHY_ROSTER_CONTACT (child);
1181 individual = empathy_roster_contact_get_individual (contact);
1183 /* Activate the oldest event associated with this contact, if any */
1184 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1185 l = g_list_previous (l))
1187 Event *event = l->data;
1189 if (event->individual == individual)
1191 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1197 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1201 fire_popup_individual_menu (EmpathyRosterView *self,
1206 EmpathyRosterContact *contact;
1207 FolksIndividual *individual;
1209 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1212 contact = EMPATHY_ROSTER_CONTACT (child);
1213 individual = empathy_roster_contact_get_individual (contact);
1215 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1216 individual, button, time);
1220 empathy_roster_view_button_press_event (GtkWidget *widget,
1221 GdkEventButton *event)
1223 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1224 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1225 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1227 if (event->button == 3)
1231 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1235 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1237 fire_popup_individual_menu (self, child, event->button, event->time);
1241 return chain_up (widget, event);
1245 empathy_roster_view_key_press_event (GtkWidget *widget,
1248 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1249 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1250 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1252 if (event->keyval == GDK_KEY_Menu)
1256 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1259 fire_popup_individual_menu (self, child, 0, event->time);
1262 return chain_up (widget, event);
1266 * @out_child: (out) (allow-none)
1269 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1271 GtkWidget **out_child)
1275 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1277 if (out_child != NULL)
1280 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1283 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1287 empathy_roster_view_query_tooltip (GtkWidget *widget,
1290 gboolean keyboard_mode,
1291 GtkTooltip *tooltip)
1293 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1294 FolksIndividual *individual;
1298 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1299 if (individual == NULL)
1302 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1303 individual, keyboard_mode, tooltip, &result);
1307 GtkAllocation allocation;
1309 gtk_widget_get_allocation (child, &allocation);
1310 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1317 empathy_roster_view_remove (GtkContainer *container,
1320 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1321 void (*chain_up) (GtkContainer *, GtkWidget *) =
1322 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1324 chain_up (container, widget);
1326 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1327 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1331 empathy_roster_view_class_init (
1332 EmpathyRosterViewClass *klass)
1334 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1335 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1336 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1337 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1340 oclass->get_property = empathy_roster_view_get_property;
1341 oclass->set_property = empathy_roster_view_set_property;
1342 oclass->constructed = empathy_roster_view_constructed;
1343 oclass->dispose = empathy_roster_view_dispose;
1344 oclass->finalize = empathy_roster_view_finalize;
1346 widget_class->button_press_event = empathy_roster_view_button_press_event;
1347 widget_class->key_press_event = empathy_roster_view_key_press_event;
1348 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1350 container_class->remove = empathy_roster_view_remove;
1352 box_class->child_activated = empathy_roster_view_child_activated;
1354 spec = g_param_spec_object ("manager", "Manager",
1355 "EmpathyIndividualManager",
1356 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1357 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1358 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1360 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1361 "Show offline contacts",
1363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1364 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1366 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1370 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1372 spec = g_param_spec_boolean ("empty", "Empty",
1373 "Is the view currently empty?",
1375 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1376 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1378 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1379 G_OBJECT_CLASS_TYPE (klass),
1381 0, NULL, NULL, NULL,
1383 1, FOLKS_TYPE_INDIVIDUAL);
1385 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1386 G_OBJECT_CLASS_TYPE (klass),
1388 0, NULL, NULL, NULL,
1390 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1392 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1393 G_OBJECT_CLASS_TYPE (klass),
1395 0, NULL, NULL, NULL,
1397 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1399 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1400 G_OBJECT_CLASS_TYPE (klass),
1402 0, g_signal_accumulator_true_handled, NULL, NULL,
1404 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1406 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1410 empathy_roster_view_init (EmpathyRosterView *self)
1412 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1413 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1415 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1416 NULL, (GDestroyNotify) g_hash_table_unref);
1417 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1419 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1421 self->priv->events = g_queue_new ();
1423 self->priv->empty = TRUE;
1427 empathy_roster_view_new (EmpathyIndividualManager *manager)
1429 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1431 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1436 EmpathyIndividualManager *
1437 empathy_roster_view_get_manager (EmpathyRosterView *self)
1439 return self->priv->manager;
1443 empathy_roster_view_show_offline (EmpathyRosterView *self,
1446 if (self->priv->show_offline == show)
1449 self->priv->show_offline = show;
1450 egg_list_box_refilter (EGG_LIST_BOX (self));
1452 g_object_notify (G_OBJECT (self), "show-offline");
1456 clear_view (EmpathyRosterView *self)
1458 gtk_container_foreach (GTK_CONTAINER (self),
1459 (GtkCallback) gtk_widget_destroy, NULL);
1461 g_hash_table_remove_all (self->priv->roster_contacts);
1462 g_hash_table_remove_all (self->priv->roster_groups);
1463 g_hash_table_remove_all (self->priv->displayed_contacts);
1467 empathy_roster_view_show_groups (EmpathyRosterView *self,
1470 if (self->priv->show_groups == show)
1473 self->priv->show_groups = show;
1475 /* TODO: block sort/filter? */
1477 populate_view (self);
1479 g_object_notify (G_OBJECT (self), "show-groups");
1483 select_first_contact (EmpathyRosterView *self)
1485 GList *children, *l;
1487 children = gtk_container_get_children (GTK_CONTAINER (self));
1488 for (l = children; l != NULL; l = g_list_next (l))
1490 GtkWidget *child = l->data;
1492 if (!gtk_widget_get_child_visible (child))
1495 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1498 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1502 g_list_free (children);
1506 search_text_notify_cb (EmpathyLiveSearch *search,
1508 EmpathyRosterView *self)
1510 egg_list_box_refilter (EGG_LIST_BOX (self));
1512 select_first_contact (self);
1516 search_activate_cb (GtkWidget *search,
1517 EmpathyRosterView *self)
1519 EggListBox *box = EGG_LIST_BOX (self);
1522 child = egg_list_box_get_selected_child (box);
1526 empathy_roster_view_child_activated (box, child);
1530 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1531 EmpathyLiveSearch *search)
1533 if (self->priv->search != NULL)
1535 g_signal_handlers_disconnect_by_func (self->priv->search,
1536 search_text_notify_cb, self);
1537 g_signal_handlers_disconnect_by_func (self->priv->search,
1538 search_activate_cb, self);
1540 g_clear_object (&self->priv->search);
1546 self->priv->search = g_object_ref (search);
1548 g_signal_connect (self->priv->search, "notify::text",
1549 G_CALLBACK (search_text_notify_cb), self);
1550 g_signal_connect (self->priv->search, "activate",
1551 G_CALLBACK (search_activate_cb), self);
1555 empathy_roster_view_is_empty (EmpathyRosterView *self)
1557 return self->priv->empty;
1561 empathy_roster_view_is_searching (EmpathyRosterView *self)
1563 return (self->priv->search != NULL &&
1564 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1567 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1568 * Empathy's internals. */
1570 empathy_roster_view_add_event (EmpathyRosterView *self,
1571 FolksIndividual *individual,
1575 GHashTable *contacts;
1577 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1578 if (contacts == NULL)
1581 self->priv->last_event_id++;
1583 g_queue_push_head (self->priv->events,
1584 event_new (self->priv->last_event_id, individual, icon, user_data));
1586 start_flashing (self);
1588 return self->priv->last_event_id;
1592 empathy_roster_view_remove_event (EmpathyRosterView *self,
1597 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1598 l = g_list_next (l))
1600 Event *event = l->data;
1602 if (event->id == event_id)
1604 remove_event (self, event);
1611 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1615 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1617 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1620 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));