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"
40 struct _EmpathyRosterViewPriv
42 EmpathyIndividualManager *manager;
44 /* FolksIndividual (borrowed) -> GHashTable (
45 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
47 * When not using groups, this hash just have one element mapped
48 * from the special NO_GROUP key. We could use it as a set but
49 * I prefer to stay coherent in the way this hash is managed.
51 GHashTable *roster_contacts;
52 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
53 GHashTable *roster_groups;
54 /* Hash of the EmpathyRosterContact currently displayed */
55 GHashTable *displayed_contacts;
58 /* queue of (Event *). The most recent events are in the head of the queue
59 * so we always display the icon of the oldest one. */
62 gboolean display_flash_event;
64 gboolean show_offline;
68 EmpathyLiveSearch *search;
74 FolksIndividual *individual;
81 FolksIndividual *individual,
85 Event *event = g_slice_new (Event);
88 event->individual = g_object_ref (individual);
89 event->icon = g_strdup (icon);
90 event->user_data = user_data;
95 event_free (gpointer data)
98 g_object_unref (event->individual);
101 g_slice_free (Event, event);
105 empathy_roster_view_get_property (GObject *object,
110 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
115 g_value_set_object (value, self->priv->manager);
117 case PROP_SHOW_OFFLINE:
118 g_value_set_boolean (value, self->priv->show_offline);
120 case PROP_SHOW_GROUPS:
121 g_value_set_boolean (value, self->priv->show_groups);
124 g_value_set_boolean (value, self->priv->empty);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 empathy_roster_view_set_property (GObject *object,
138 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
143 g_assert (self->priv->manager == NULL); /* construct only */
144 self->priv->manager = g_value_dup_object (value);
146 case PROP_SHOW_OFFLINE:
147 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
149 case PROP_SHOW_GROUPS:
150 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 roster_contact_changed_cb (GtkWidget *child,
161 EmpathyRosterView *self)
163 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
167 is_xmpp_local_contact (FolksIndividual *individual)
169 EmpathyContact *contact;
170 TpConnection *connection;
171 const gchar *protocol_name = NULL;
174 contact = empathy_contact_dup_from_folks_individual (individual);
179 connection = empathy_contact_get_connection (contact);
180 protocol_name = tp_connection_get_protocol_name (connection);
181 result = !tp_strdiff (protocol_name, "local-xmpp");
182 g_object_unref (contact);
188 add_roster_contact (EmpathyRosterView *self,
189 FolksIndividual *individual,
194 contact = empathy_roster_contact_new (individual, group);
196 /* Need to refilter if online is changed */
197 g_signal_connect (contact, "notify::online",
198 G_CALLBACK (roster_contact_changed_cb), self);
200 /* Need to resort if alias is changed */
201 g_signal_connect (contact, "notify::alias",
202 G_CALLBACK (roster_contact_changed_cb), self);
204 gtk_widget_show (contact);
205 gtk_container_add (GTK_CONTAINER (self), contact);
211 group_expanded_cb (EmpathyRosterGroup *group,
213 EmpathyRosterView *self)
217 widgets = empathy_roster_group_get_widgets (group);
218 for (l = widgets; l != NULL; l = g_list_next (l))
220 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
223 g_list_free (widgets);
226 static EmpathyRosterGroup *
227 lookup_roster_group (EmpathyRosterView *self,
230 return g_hash_table_lookup (self->priv->roster_groups, group);
233 static EmpathyRosterGroup *
234 ensure_roster_group (EmpathyRosterView *self,
237 GtkWidget *roster_group;
239 roster_group = (GtkWidget *) lookup_roster_group (self, group);
240 if (roster_group != NULL)
241 return EMPATHY_ROSTER_GROUP (roster_group);
243 if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
244 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
245 else if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY))
246 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
248 roster_group = empathy_roster_group_new (group, NULL);
250 g_signal_connect (roster_group, "notify::expanded",
251 G_CALLBACK (group_expanded_cb), self);
253 gtk_widget_show (roster_group);
254 gtk_container_add (GTK_CONTAINER (self), roster_group);
256 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
259 return EMPATHY_ROSTER_GROUP (roster_group);
263 update_group_widgets (EmpathyRosterView *self,
264 EmpathyRosterGroup *group,
265 EmpathyRosterContact *contact,
268 guint old_count, count;
270 old_count = empathy_roster_group_get_widgets_count (group);
273 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
275 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
277 if (count != old_count)
278 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
282 add_to_group (EmpathyRosterView *self,
283 FolksIndividual *individual,
287 GHashTable *contacts;
288 EmpathyRosterGroup *roster_group = NULL;
290 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
291 if (contacts == NULL)
294 if (tp_strdiff (group, NO_GROUP))
295 roster_group = ensure_roster_group (self, group);
297 contact = add_roster_contact (self, individual, group);
298 g_hash_table_insert (contacts, g_strdup (group), contact);
300 if (roster_group != NULL)
302 update_group_widgets (self, roster_group,
303 EMPATHY_ROSTER_CONTACT (contact), TRUE);
308 individual_added (EmpathyRosterView *self,
309 FolksIndividual *individual)
311 GHashTable *contacts;
313 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
314 if (contacts != NULL)
317 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
319 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
321 if (!self->priv->show_groups)
323 add_to_group (self, individual, NO_GROUP);
325 else if (is_xmpp_local_contact (individual))
327 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY);
334 tops = empathy_individual_manager_get_top_individuals (
335 self->priv->manager);
337 if (folks_favourite_details_get_is_favourite (
338 FOLKS_FAVOURITE_DETAILS (individual)) ||
339 g_list_index (tops, individual) != -1)
341 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
344 groups = folks_group_details_get_groups (
345 FOLKS_GROUP_DETAILS (individual));
347 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
349 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
351 while (iter != NULL && gee_iterator_next (iter))
353 gchar *group = gee_iterator_get (iter);
355 add_to_group (self, individual, group);
360 g_clear_object (&iter);
364 /* No group, adds to Ungrouped */
365 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
371 set_event_icon_on_individual (EmpathyRosterView *self,
372 FolksIndividual *individual,
375 GHashTable *contacts;
379 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
380 if (contacts == NULL)
383 g_hash_table_iter_init (&iter, contacts);
384 while (g_hash_table_iter_next (&iter, NULL, &v))
386 EmpathyRosterContact *contact =v;
388 empathy_roster_contact_set_event_icon (contact, icon);
393 flash_event (Event *event,
394 EmpathyRosterView *self)
396 set_event_icon_on_individual (self, event->individual, event->icon);
400 unflash_event (Event *event,
401 EmpathyRosterView *self)
403 set_event_icon_on_individual (self, event->individual, NULL);
407 flash_cb (gpointer data)
409 EmpathyRosterView *self = data;
411 if (self->priv->display_flash_event)
413 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
414 self->priv->display_flash_event = FALSE;
418 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
419 self->priv->display_flash_event = TRUE;
426 start_flashing (EmpathyRosterView *self)
428 if (self->priv->flash_id != 0)
431 self->priv->display_flash_event = TRUE;
433 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
438 stop_flashing (EmpathyRosterView *self)
440 if (self->priv->flash_id == 0)
443 g_source_remove (self->priv->flash_id);
444 self->priv->flash_id = 0;
448 remove_event (EmpathyRosterView *self,
451 unflash_event (event, self);
452 g_queue_remove (self->priv->events, event);
454 if (g_queue_get_length (self->priv->events) == 0)
456 stop_flashing (self);
461 remove_all_individual_event (EmpathyRosterView *self,
462 FolksIndividual *individual)
466 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
469 Event *event = l->data;
471 if (event->individual == individual)
473 remove_event (self, event);
480 individual_removed (EmpathyRosterView *self,
481 FolksIndividual *individual)
483 GHashTable *contacts;
487 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
488 if (contacts == NULL)
491 remove_all_individual_event (self, individual);
493 g_hash_table_iter_init (&iter, contacts);
494 while (g_hash_table_iter_next (&iter, &key, &value))
496 const gchar *group_name = key;
497 GtkWidget *contact = value;
498 EmpathyRosterGroup *group;
500 group = lookup_roster_group (self, group_name);
503 update_group_widgets (self, group,
504 EMPATHY_ROSTER_CONTACT (contact), FALSE);
507 gtk_container_remove (GTK_CONTAINER (self), contact);
510 g_hash_table_remove (self->priv->roster_contacts, individual);
514 members_changed_cb (EmpathyIndividualManager *manager,
515 const gchar *message,
518 TpChannelGroupChangeReason reason,
519 EmpathyRosterView *self)
523 for (l = added; l != NULL; l = g_list_next (l))
525 FolksIndividual *individual = l->data;
527 individual_added (self, individual);
530 for (l = removed; l != NULL; l = g_list_next (l))
532 FolksIndividual *individual = l->data;
534 individual_removed (self, individual);
539 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
540 EmpathyRosterContact *b)
542 FolksIndividual *ind_a, *ind_b;
543 const gchar *alias_a, *alias_b;
545 ind_a = empathy_roster_contact_get_individual (a);
546 ind_b = empathy_roster_contact_get_individual (b);
548 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
549 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
551 return g_ascii_strcasecmp (alias_a, alias_b);
555 contact_is_favourite (EmpathyRosterContact *contact)
557 FolksIndividual *individual;
559 individual = empathy_roster_contact_get_individual (contact);
561 return folks_favourite_details_get_is_favourite (
562 FOLKS_FAVOURITE_DETAILS (individual));
566 contact_in_top (EmpathyRosterView *self,
567 EmpathyRosterContact *contact)
569 FolksIndividual *individual;
572 if (contact_is_favourite (contact))
575 individual = empathy_roster_contact_get_individual (contact);
577 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
579 if (g_list_index (tops, individual) != -1)
586 compare_roster_contacts_no_group (EmpathyRosterView *self,
587 EmpathyRosterContact *a,
588 EmpathyRosterContact *b)
590 gboolean top_a, top_b;
592 top_a = contact_in_top (self, a);
593 top_b = contact_in_top (self, b);
596 /* Both contacts are in the top of the roster (or not). Sort them
598 return compare_roster_contacts_by_alias (a, b);
606 compare_group_names (const gchar *group_a,
607 const gchar *group_b)
609 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
612 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
615 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
617 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
620 return g_ascii_strcasecmp (group_a, group_b);
624 compare_roster_contacts_with_groups (EmpathyRosterView *self,
625 EmpathyRosterContact *a,
626 EmpathyRosterContact *b)
628 const gchar *group_a, *group_b;
630 group_a = empathy_roster_contact_get_group (a);
631 group_b = empathy_roster_contact_get_group (b);
633 if (!tp_strdiff (group_a, group_b))
634 /* Same group, compare the contacts */
635 return compare_roster_contacts_by_alias (a, b);
638 return compare_group_names (group_a, group_b);
642 compare_roster_contacts (EmpathyRosterView *self,
643 EmpathyRosterContact *a,
644 EmpathyRosterContact *b)
646 if (!self->priv->show_groups)
647 return compare_roster_contacts_no_group (self, a, b);
649 return compare_roster_contacts_with_groups (self, a, b);
653 compare_roster_groups (EmpathyRosterGroup *a,
654 EmpathyRosterGroup *b)
656 const gchar *name_a, *name_b;
658 name_a = empathy_roster_group_get_name (a);
659 name_b = empathy_roster_group_get_name (b);
661 return compare_group_names (name_a, name_b);
665 compare_contact_group (EmpathyRosterContact *contact,
666 EmpathyRosterGroup *group)
668 const char *contact_group, *group_name;
670 contact_group = empathy_roster_contact_get_group (contact);
671 group_name = empathy_roster_group_get_name (group);
673 if (!tp_strdiff (contact_group, group_name))
674 /* @contact is in @group, @group has to be displayed first */
677 /* @contact is in a different group, sort by group name */
678 return compare_group_names (contact_group, group_name);
682 roster_view_sort (gconstpointer a,
686 EmpathyRosterView *self = user_data;
688 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
689 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
690 EMPATHY_ROSTER_CONTACT (b));
691 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
692 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
693 EMPATHY_ROSTER_GROUP (b));
694 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
695 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
696 EMPATHY_ROSTER_GROUP (b));
697 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
698 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
699 EMPATHY_ROSTER_GROUP (a));
701 g_return_val_if_reached (0);
705 update_separator (GtkWidget **separator,
712 /* No separator before the first row */
713 g_clear_object (separator);
717 if (*separator != NULL)
720 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
721 g_object_ref_sink (*separator);
725 is_searching (EmpathyRosterView *self)
727 if (self->priv->search == NULL)
730 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
734 update_empty (EmpathyRosterView *self,
737 if (self->priv->empty == empty)
740 self->priv->empty = empty;
741 g_object_notify (G_OBJECT (self), "empty");
745 add_to_displayed (EmpathyRosterView *self,
746 EmpathyRosterContact *contact)
748 FolksIndividual *individual;
749 GHashTable *contacts;
753 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
756 g_hash_table_add (self->priv->displayed_contacts, contact);
757 update_empty (self, FALSE);
759 /* Groups of this contact may now be displayed if we just displays the first
760 * child in this group. */
762 if (!self->priv->show_groups)
765 individual = empathy_roster_contact_get_individual (contact);
766 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
767 if (contacts == NULL)
770 g_hash_table_iter_init (&iter, contacts);
771 while (g_hash_table_iter_next (&iter, &k, NULL))
773 const gchar *group_name = k;
776 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
780 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
785 remove_from_displayed (EmpathyRosterView *self,
786 EmpathyRosterContact *contact)
788 g_hash_table_remove (self->priv->displayed_contacts, contact);
790 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
791 update_empty (self, TRUE);
795 * check if @contact should be displayed according to @self's current status
796 * and without consideration for the state of @contact's groups.
799 contact_should_be_displayed (EmpathyRosterView *self,
800 EmpathyRosterContact *contact)
802 if (is_searching (self))
804 FolksIndividual *individual;
806 individual = empathy_roster_contact_get_individual (contact);
808 return empathy_individual_match_string (individual,
809 empathy_live_search_get_text (self->priv->search),
810 empathy_live_search_get_words (self->priv->search));
813 if (self->priv->show_offline)
816 if (contact_is_favourite (contact))
818 const gchar *group_name;
820 if (!self->priv->show_groups)
821 /* Always display favourite contacts in non-group mode. */
824 group_name = empathy_roster_contact_get_group (contact);
826 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
827 /* Always display favourite contact in group mode only in the
832 return empathy_roster_contact_is_online (contact);
837 filter_contact (EmpathyRosterView *self,
838 EmpathyRosterContact *contact)
842 displayed = contact_should_be_displayed (self, contact);
844 if (self->priv->show_groups)
846 const gchar *group_name;
847 EmpathyRosterGroup *group;
849 group_name = empathy_roster_contact_get_group (contact);
850 group = lookup_roster_group (self, group_name);
854 /* When searching, always display even if the group is closed */
855 if (!is_searching (self) &&
856 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
863 add_to_displayed (self, contact);
867 remove_from_displayed (self, contact);
874 filter_group (EmpathyRosterView *self,
875 EmpathyRosterGroup *group)
879 /* Display the group if it contains at least one displayed contact */
880 widgets = empathy_roster_group_get_widgets (group);
881 for (l = widgets; l != NULL; l = g_list_next (l))
883 EmpathyRosterContact *contact = l->data;
885 if (contact_should_be_displayed (self, contact))
893 filter_list (GtkWidget *child,
896 EmpathyRosterView *self = user_data;
898 if (EMPATHY_IS_ROSTER_CONTACT (child))
899 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
901 else if (EMPATHY_IS_ROSTER_GROUP (child))
902 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
904 g_return_val_if_reached (FALSE);
907 /* @list: GList of EmpathyRosterContact
909 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
912 individual_in_list (FolksIndividual *individual,
917 for (l = list; l != NULL; l = g_list_next (l))
919 EmpathyRosterContact *contact = l->data;
921 if (empathy_roster_contact_get_individual (contact) == individual)
929 populate_view (EmpathyRosterView *self)
931 GList *individuals, *l;
933 individuals = empathy_individual_manager_get_members (self->priv->manager);
934 for (l = individuals; l != NULL; l = g_list_next (l))
936 FolksIndividual *individual = l->data;
938 individual_added (self, individual);
941 g_list_free (individuals);
945 remove_from_group (EmpathyRosterView *self,
946 FolksIndividual *individual,
949 GHashTable *contacts;
951 EmpathyRosterGroup *roster_group;
953 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
954 if (contacts == NULL)
957 contact = g_hash_table_lookup (contacts, group);
961 g_hash_table_remove (contacts, group);
963 if (g_hash_table_size (contacts) == 0)
965 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
968 roster_group = lookup_roster_group (self, group);
970 if (roster_group != NULL)
972 update_group_widgets (self, roster_group,
973 EMPATHY_ROSTER_CONTACT (contact), FALSE);
976 gtk_container_remove (GTK_CONTAINER (self), contact);
980 update_top_contacts (EmpathyRosterView *self)
983 GList *to_add = NULL, *to_remove = NULL;
984 EmpathyRosterGroup *group;
986 if (!self->priv->show_groups)
988 egg_list_box_resort (EGG_LIST_BOX (self));
992 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
994 group = g_hash_table_lookup (self->priv->roster_groups,
995 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
998 to_add = g_list_copy (tops);
1004 contacts = empathy_roster_group_get_widgets (group);
1006 /* Check which EmpathyRosterContact have to be removed */
1007 for (l = contacts; l != NULL; l = g_list_next (l))
1009 EmpathyRosterContact *contact = l->data;
1010 FolksIndividual *individual;
1012 if (contact_is_favourite (contact))
1015 individual = empathy_roster_contact_get_individual (contact);
1017 if (g_list_find (tops, individual) == NULL)
1018 to_remove = g_list_prepend (to_remove, individual);
1021 /* Check which EmpathyRosterContact have to be added */
1022 for (l = tops; l != NULL; l = g_list_next (l))
1024 FolksIndividual *individual = l->data;
1026 if (!individual_in_list (individual, contacts))
1027 to_add = g_list_prepend (to_add, individual);
1031 for (l = to_add; l != NULL; l = g_list_next (l))
1032 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1034 for (l = to_remove; l != NULL; l = g_list_next (l))
1035 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1037 g_list_free (to_add);
1038 g_list_free (to_remove);
1042 groups_changed_cb (EmpathyIndividualManager *manager,
1043 FolksIndividual *individual,
1046 EmpathyRosterView *self)
1048 if (!self->priv->show_groups)
1053 add_to_group (self, individual, group);
1057 remove_from_group (self, individual, group);
1062 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1064 EmpathyRosterView *self)
1066 update_top_contacts (self);
1070 favourites_changed_cb (EmpathyIndividualManager *manager,
1071 FolksIndividual *individual,
1073 EmpathyRosterView *self)
1075 GHashTable *contacts;
1077 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1078 if (contacts == NULL)
1081 if (self->priv->show_groups)
1084 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1086 remove_from_group (self, individual,
1087 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1093 contact = g_hash_table_lookup (contacts, NO_GROUP);
1094 if (contact == NULL)
1097 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1102 empathy_roster_view_constructed (GObject *object)
1104 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1105 void (*chain_up) (GObject *) =
1106 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1108 if (chain_up != NULL)
1111 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1113 populate_view (self);
1115 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1116 G_CALLBACK (members_changed_cb), self, 0);
1117 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1118 G_CALLBACK (groups_changed_cb), self, 0);
1119 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1120 G_CALLBACK (top_individuals_changed_cb), self, 0);
1121 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1122 G_CALLBACK (favourites_changed_cb), self, 0);
1124 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1125 roster_view_sort, self, NULL);
1127 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1130 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1132 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1136 empathy_roster_view_dispose (GObject *object)
1138 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1139 void (*chain_up) (GObject *) =
1140 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1142 stop_flashing (self);
1144 empathy_roster_view_set_live_search (self, NULL);
1145 g_clear_object (&self->priv->manager);
1147 if (chain_up != NULL)
1152 empathy_roster_view_finalize (GObject *object)
1154 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1155 void (*chain_up) (GObject *) =
1156 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1158 g_hash_table_unref (self->priv->roster_contacts);
1159 g_hash_table_unref (self->priv->roster_groups);
1160 g_hash_table_unref (self->priv->displayed_contacts);
1161 g_queue_free_full (self->priv->events, event_free);
1163 if (chain_up != NULL)
1168 empathy_roster_view_child_activated (EggListBox *box,
1171 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1172 EmpathyRosterContact *contact;
1173 FolksIndividual *individual;
1176 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1179 contact = EMPATHY_ROSTER_CONTACT (child);
1180 individual = empathy_roster_contact_get_individual (contact);
1182 /* Activate the oldest event associated with this contact, if any */
1183 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1184 l = g_list_previous (l))
1186 Event *event = l->data;
1188 if (event->individual == individual)
1190 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1196 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1200 fire_popup_individual_menu (EmpathyRosterView *self,
1205 EmpathyRosterContact *contact;
1206 FolksIndividual *individual;
1208 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1211 contact = EMPATHY_ROSTER_CONTACT (child);
1212 individual = empathy_roster_contact_get_individual (contact);
1214 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1215 individual, button, time);
1219 empathy_roster_view_button_press_event (GtkWidget *widget,
1220 GdkEventButton *event)
1222 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1223 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1224 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1226 if (event->button == 3)
1230 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1234 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1236 fire_popup_individual_menu (self, child, event->button, event->time);
1240 return chain_up (widget, event);
1244 empathy_roster_view_key_press_event (GtkWidget *widget,
1247 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1248 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1249 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1251 if (event->keyval == GDK_KEY_Menu)
1255 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1258 fire_popup_individual_menu (self, child, 0, event->time);
1261 return chain_up (widget, event);
1265 * @out_child: (out) (allow-none)
1268 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1270 GtkWidget **out_child)
1274 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1276 if (out_child != NULL)
1279 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1282 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1286 * @out_child: (out) (allow-none)
1289 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1294 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1296 if (EMPATHY_IS_ROSTER_CONTACT (child))
1297 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1298 else if (EMPATHY_IS_ROSTER_GROUP (child))
1299 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1305 empathy_roster_view_query_tooltip (GtkWidget *widget,
1308 gboolean keyboard_mode,
1309 GtkTooltip *tooltip)
1311 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1312 FolksIndividual *individual;
1316 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1317 if (individual == NULL)
1320 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1321 individual, keyboard_mode, tooltip, &result);
1325 GtkAllocation allocation;
1327 gtk_widget_get_allocation (child, &allocation);
1328 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1335 empathy_roster_view_remove (GtkContainer *container,
1338 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1339 void (*chain_up) (GtkContainer *, GtkWidget *) =
1340 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1342 chain_up (container, widget);
1344 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1345 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1349 empathy_roster_view_class_init (
1350 EmpathyRosterViewClass *klass)
1352 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1353 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1354 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1355 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1358 oclass->get_property = empathy_roster_view_get_property;
1359 oclass->set_property = empathy_roster_view_set_property;
1360 oclass->constructed = empathy_roster_view_constructed;
1361 oclass->dispose = empathy_roster_view_dispose;
1362 oclass->finalize = empathy_roster_view_finalize;
1364 widget_class->button_press_event = empathy_roster_view_button_press_event;
1365 widget_class->key_press_event = empathy_roster_view_key_press_event;
1366 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1368 container_class->remove = empathy_roster_view_remove;
1370 box_class->child_activated = empathy_roster_view_child_activated;
1372 spec = g_param_spec_object ("manager", "Manager",
1373 "EmpathyIndividualManager",
1374 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1375 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1376 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1378 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1379 "Show offline contacts",
1381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1382 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1384 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1388 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1390 spec = g_param_spec_boolean ("empty", "Empty",
1391 "Is the view currently empty?",
1393 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1394 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1396 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1397 G_OBJECT_CLASS_TYPE (klass),
1399 0, NULL, NULL, NULL,
1401 1, FOLKS_TYPE_INDIVIDUAL);
1403 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1404 G_OBJECT_CLASS_TYPE (klass),
1406 0, NULL, NULL, NULL,
1408 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1410 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1411 G_OBJECT_CLASS_TYPE (klass),
1413 0, NULL, NULL, NULL,
1415 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1417 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1418 G_OBJECT_CLASS_TYPE (klass),
1420 0, g_signal_accumulator_true_handled, NULL, NULL,
1422 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1424 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1428 empathy_roster_view_init (EmpathyRosterView *self)
1430 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1431 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1433 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1434 NULL, (GDestroyNotify) g_hash_table_unref);
1435 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1437 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1439 self->priv->events = g_queue_new ();
1441 self->priv->empty = TRUE;
1445 empathy_roster_view_new (EmpathyIndividualManager *manager)
1447 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1449 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1454 EmpathyIndividualManager *
1455 empathy_roster_view_get_manager (EmpathyRosterView *self)
1457 return self->priv->manager;
1461 empathy_roster_view_show_offline (EmpathyRosterView *self,
1464 if (self->priv->show_offline == show)
1467 self->priv->show_offline = show;
1468 egg_list_box_refilter (EGG_LIST_BOX (self));
1470 g_object_notify (G_OBJECT (self), "show-offline");
1474 clear_view (EmpathyRosterView *self)
1476 gtk_container_foreach (GTK_CONTAINER (self),
1477 (GtkCallback) gtk_widget_destroy, NULL);
1479 g_hash_table_remove_all (self->priv->roster_contacts);
1480 g_hash_table_remove_all (self->priv->roster_groups);
1481 g_hash_table_remove_all (self->priv->displayed_contacts);
1485 empathy_roster_view_show_groups (EmpathyRosterView *self,
1488 if (self->priv->show_groups == show)
1491 self->priv->show_groups = show;
1493 /* TODO: block sort/filter? */
1495 populate_view (self);
1497 g_object_notify (G_OBJECT (self), "show-groups");
1501 select_first_contact (EmpathyRosterView *self)
1503 GList *children, *l;
1505 children = gtk_container_get_children (GTK_CONTAINER (self));
1506 for (l = children; l != NULL; l = g_list_next (l))
1508 GtkWidget *child = l->data;
1510 if (!gtk_widget_get_child_visible (child))
1513 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1516 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1520 g_list_free (children);
1524 search_text_notify_cb (EmpathyLiveSearch *search,
1526 EmpathyRosterView *self)
1528 egg_list_box_refilter (EGG_LIST_BOX (self));
1530 select_first_contact (self);
1534 search_activate_cb (GtkWidget *search,
1535 EmpathyRosterView *self)
1537 EggListBox *box = EGG_LIST_BOX (self);
1540 child = egg_list_box_get_selected_child (box);
1544 empathy_roster_view_child_activated (box, child);
1548 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1549 EmpathyLiveSearch *search)
1551 if (self->priv->search != NULL)
1553 g_signal_handlers_disconnect_by_func (self->priv->search,
1554 search_text_notify_cb, self);
1555 g_signal_handlers_disconnect_by_func (self->priv->search,
1556 search_activate_cb, self);
1558 g_clear_object (&self->priv->search);
1564 self->priv->search = g_object_ref (search);
1566 g_signal_connect (self->priv->search, "notify::text",
1567 G_CALLBACK (search_text_notify_cb), self);
1568 g_signal_connect (self->priv->search, "activate",
1569 G_CALLBACK (search_activate_cb), self);
1573 empathy_roster_view_is_empty (EmpathyRosterView *self)
1575 return self->priv->empty;
1579 empathy_roster_view_is_searching (EmpathyRosterView *self)
1581 return (self->priv->search != NULL &&
1582 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1585 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1586 * Empathy's internals. */
1588 empathy_roster_view_add_event (EmpathyRosterView *self,
1589 FolksIndividual *individual,
1593 GHashTable *contacts;
1595 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1596 if (contacts == NULL)
1599 self->priv->last_event_id++;
1601 g_queue_push_head (self->priv->events,
1602 event_new (self->priv->last_event_id, individual, icon, user_data));
1604 start_flashing (self);
1606 return self->priv->last_event_id;
1610 empathy_roster_view_remove_event (EmpathyRosterView *self,
1615 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1616 l = g_list_next (l))
1618 Event *event = l->data;
1620 if (event->id == event_id)
1622 remove_event (self, event);
1629 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1633 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1635 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1638 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));