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
30 SIG_INDIVIDUAL_ACTIVATED,
31 SIG_POPUP_INDIVIDUAL_MENU,
33 SIG_INDIVIDUAL_TOOLTIP,
37 static guint signals[LAST_SIGNAL];
39 #define NO_GROUP "X-no-group"
41 struct _EmpathyRosterViewPriv
43 EmpathyIndividualManager *manager;
45 /* FolksIndividual (borrowed) -> GHashTable (
46 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
48 * When not using groups, this hash just have one element mapped
49 * from the special NO_GROUP key. We could use it as a set but
50 * I prefer to stay coherent in the way this hash is managed.
52 GHashTable *roster_contacts;
53 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
54 GHashTable *roster_groups;
55 /* Hash of the EmpathyRosterContact currently displayed */
56 GHashTable *displayed_contacts;
59 /* queue of (Event *). The most recent events are in the head of the queue
60 * so we always display the icon of the oldest one. */
63 gboolean display_flash_event;
65 gboolean show_offline;
69 EmpathyLiveSearch *search;
71 EmpathyRosterModel *model;
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);
121 g_value_set_object (value, self->priv->model);
123 case PROP_SHOW_OFFLINE:
124 g_value_set_boolean (value, self->priv->show_offline);
126 case PROP_SHOW_GROUPS:
127 g_value_set_boolean (value, self->priv->show_groups);
130 g_value_set_boolean (value, self->priv->empty);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139 empathy_roster_view_set_property (GObject *object,
144 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
149 g_assert (self->priv->manager == NULL); /* construct only */
150 self->priv->manager = g_value_dup_object (value);
153 g_assert (self->priv->model == NULL);
154 self->priv->model = g_value_dup_object (value);
156 case PROP_SHOW_OFFLINE:
157 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
159 case PROP_SHOW_GROUPS:
160 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
169 roster_contact_changed_cb (GtkWidget *child,
171 EmpathyRosterView *self)
173 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
177 is_xmpp_local_contact (FolksIndividual *individual)
179 EmpathyContact *contact;
180 TpConnection *connection;
181 const gchar *protocol_name = NULL;
184 contact = empathy_contact_dup_from_folks_individual (individual);
189 connection = empathy_contact_get_connection (contact);
190 protocol_name = tp_connection_get_protocol_name (connection);
191 result = !tp_strdiff (protocol_name, "local-xmpp");
192 g_object_unref (contact);
198 add_roster_contact (EmpathyRosterView *self,
199 FolksIndividual *individual,
204 contact = empathy_roster_contact_new (individual, group);
206 /* Need to refilter if online is changed */
207 g_signal_connect (contact, "notify::online",
208 G_CALLBACK (roster_contact_changed_cb), self);
210 /* Need to resort if alias is changed */
211 g_signal_connect (contact, "notify::alias",
212 G_CALLBACK (roster_contact_changed_cb), self);
214 gtk_widget_show (contact);
215 gtk_container_add (GTK_CONTAINER (self), contact);
221 group_expanded_cb (EmpathyRosterGroup *group,
223 EmpathyRosterView *self)
227 widgets = empathy_roster_group_get_widgets (group);
228 for (l = widgets; l != NULL; l = g_list_next (l))
230 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
233 g_list_free (widgets);
236 static EmpathyRosterGroup *
237 lookup_roster_group (EmpathyRosterView *self,
240 return g_hash_table_lookup (self->priv->roster_groups, group);
243 static EmpathyRosterGroup *
244 ensure_roster_group (EmpathyRosterView *self,
247 GtkWidget *roster_group;
249 roster_group = (GtkWidget *) lookup_roster_group (self, group);
250 if (roster_group != NULL)
251 return EMPATHY_ROSTER_GROUP (roster_group);
253 if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
254 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
255 else if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY))
256 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
258 roster_group = empathy_roster_group_new (group, NULL);
260 g_signal_connect (roster_group, "notify::expanded",
261 G_CALLBACK (group_expanded_cb), self);
263 gtk_widget_show (roster_group);
264 gtk_container_add (GTK_CONTAINER (self), roster_group);
266 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
269 return EMPATHY_ROSTER_GROUP (roster_group);
273 update_group_widgets (EmpathyRosterView *self,
274 EmpathyRosterGroup *group,
275 EmpathyRosterContact *contact,
278 guint old_count, count;
280 old_count = empathy_roster_group_get_widgets_count (group);
283 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
285 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
287 if (count != old_count)
288 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
292 add_to_group (EmpathyRosterView *self,
293 FolksIndividual *individual,
297 GHashTable *contacts;
298 EmpathyRosterGroup *roster_group = NULL;
300 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
301 if (contacts == NULL)
304 if (tp_strdiff (group, NO_GROUP))
305 roster_group = ensure_roster_group (self, group);
307 contact = add_roster_contact (self, individual, group);
308 g_hash_table_insert (contacts, g_strdup (group), contact);
310 if (roster_group != NULL)
312 update_group_widgets (self, roster_group,
313 EMPATHY_ROSTER_CONTACT (contact), TRUE);
318 individual_added (EmpathyRosterView *self,
319 FolksIndividual *individual)
321 GHashTable *contacts;
323 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
324 if (contacts != NULL)
327 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
329 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
331 if (!self->priv->show_groups)
333 add_to_group (self, individual, NO_GROUP);
335 else if (is_xmpp_local_contact (individual))
337 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY);
344 tops = empathy_individual_manager_get_top_individuals (
345 self->priv->manager);
347 if (folks_favourite_details_get_is_favourite (
348 FOLKS_FAVOURITE_DETAILS (individual)) ||
349 g_list_index (tops, individual) != -1)
351 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
354 groups = folks_group_details_get_groups (
355 FOLKS_GROUP_DETAILS (individual));
357 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
359 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
361 while (iter != NULL && gee_iterator_next (iter))
363 gchar *group = gee_iterator_get (iter);
365 add_to_group (self, individual, group);
370 g_clear_object (&iter);
374 /* No group, adds to Ungrouped */
375 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
381 set_event_icon_on_individual (EmpathyRosterView *self,
382 FolksIndividual *individual,
385 GHashTable *contacts;
389 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
390 if (contacts == NULL)
393 g_hash_table_iter_init (&iter, contacts);
394 while (g_hash_table_iter_next (&iter, NULL, &v))
396 EmpathyRosterContact *contact =v;
398 empathy_roster_contact_set_event_icon (contact, icon);
403 flash_event (Event *event,
404 EmpathyRosterView *self)
406 set_event_icon_on_individual (self, event->individual, event->icon);
410 unflash_event (Event *event,
411 EmpathyRosterView *self)
413 set_event_icon_on_individual (self, event->individual, NULL);
417 flash_cb (gpointer data)
419 EmpathyRosterView *self = data;
421 if (self->priv->display_flash_event)
423 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
424 self->priv->display_flash_event = FALSE;
428 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
429 self->priv->display_flash_event = TRUE;
436 start_flashing (EmpathyRosterView *self)
438 if (self->priv->flash_id != 0)
441 self->priv->display_flash_event = TRUE;
443 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
448 stop_flashing (EmpathyRosterView *self)
450 if (self->priv->flash_id == 0)
453 g_source_remove (self->priv->flash_id);
454 self->priv->flash_id = 0;
458 remove_event (EmpathyRosterView *self,
461 unflash_event (event, self);
462 g_queue_remove (self->priv->events, event);
464 if (g_queue_get_length (self->priv->events) == 0)
466 stop_flashing (self);
471 remove_all_individual_event (EmpathyRosterView *self,
472 FolksIndividual *individual)
476 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
479 Event *event = l->data;
481 if (event->individual == individual)
483 remove_event (self, event);
490 individual_removed (EmpathyRosterView *self,
491 FolksIndividual *individual)
493 GHashTable *contacts;
497 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
498 if (contacts == NULL)
501 remove_all_individual_event (self, individual);
503 g_hash_table_iter_init (&iter, contacts);
504 while (g_hash_table_iter_next (&iter, &key, &value))
506 const gchar *group_name = key;
507 GtkWidget *contact = value;
508 EmpathyRosterGroup *group;
510 group = lookup_roster_group (self, group_name);
513 update_group_widgets (self, group,
514 EMPATHY_ROSTER_CONTACT (contact), FALSE);
517 gtk_container_remove (GTK_CONTAINER (self), contact);
520 g_hash_table_remove (self->priv->roster_contacts, individual);
524 members_changed_cb (EmpathyIndividualManager *manager,
525 const gchar *message,
528 TpChannelGroupChangeReason reason,
529 EmpathyRosterView *self)
533 for (l = added; l != NULL; l = g_list_next (l))
535 FolksIndividual *individual = l->data;
537 individual_added (self, individual);
540 for (l = removed; l != NULL; l = g_list_next (l))
542 FolksIndividual *individual = l->data;
544 individual_removed (self, individual);
549 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
550 EmpathyRosterContact *b)
552 FolksIndividual *ind_a, *ind_b;
553 const gchar *alias_a, *alias_b;
555 ind_a = empathy_roster_contact_get_individual (a);
556 ind_b = empathy_roster_contact_get_individual (b);
558 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
559 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
561 return g_ascii_strcasecmp (alias_a, alias_b);
565 contact_is_favourite (EmpathyRosterContact *contact)
567 FolksIndividual *individual;
569 individual = empathy_roster_contact_get_individual (contact);
571 return folks_favourite_details_get_is_favourite (
572 FOLKS_FAVOURITE_DETAILS (individual));
576 contact_in_top (EmpathyRosterView *self,
577 EmpathyRosterContact *contact)
579 FolksIndividual *individual;
582 if (contact_is_favourite (contact))
585 individual = empathy_roster_contact_get_individual (contact);
587 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
589 if (g_list_index (tops, individual) != -1)
596 compare_roster_contacts_no_group (EmpathyRosterView *self,
597 EmpathyRosterContact *a,
598 EmpathyRosterContact *b)
600 gboolean top_a, top_b;
602 top_a = contact_in_top (self, a);
603 top_b = contact_in_top (self, b);
606 /* Both contacts are in the top of the roster (or not). Sort them
608 return compare_roster_contacts_by_alias (a, b);
616 compare_group_names (const gchar *group_a,
617 const gchar *group_b)
619 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
622 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
625 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
627 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
630 return g_ascii_strcasecmp (group_a, group_b);
634 compare_roster_contacts_with_groups (EmpathyRosterView *self,
635 EmpathyRosterContact *a,
636 EmpathyRosterContact *b)
638 const gchar *group_a, *group_b;
640 group_a = empathy_roster_contact_get_group (a);
641 group_b = empathy_roster_contact_get_group (b);
643 if (!tp_strdiff (group_a, group_b))
644 /* Same group, compare the contacts */
645 return compare_roster_contacts_by_alias (a, b);
648 return compare_group_names (group_a, group_b);
652 compare_roster_contacts (EmpathyRosterView *self,
653 EmpathyRosterContact *a,
654 EmpathyRosterContact *b)
656 if (!self->priv->show_groups)
657 return compare_roster_contacts_no_group (self, a, b);
659 return compare_roster_contacts_with_groups (self, a, b);
663 compare_roster_groups (EmpathyRosterGroup *a,
664 EmpathyRosterGroup *b)
666 const gchar *name_a, *name_b;
668 name_a = empathy_roster_group_get_name (a);
669 name_b = empathy_roster_group_get_name (b);
671 return compare_group_names (name_a, name_b);
675 compare_contact_group (EmpathyRosterContact *contact,
676 EmpathyRosterGroup *group)
678 const char *contact_group, *group_name;
680 contact_group = empathy_roster_contact_get_group (contact);
681 group_name = empathy_roster_group_get_name (group);
683 if (!tp_strdiff (contact_group, group_name))
684 /* @contact is in @group, @group has to be displayed first */
687 /* @contact is in a different group, sort by group name */
688 return compare_group_names (contact_group, group_name);
692 roster_view_sort (gconstpointer a,
696 EmpathyRosterView *self = user_data;
698 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
699 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
700 EMPATHY_ROSTER_CONTACT (b));
701 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
702 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
703 EMPATHY_ROSTER_GROUP (b));
704 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
705 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
706 EMPATHY_ROSTER_GROUP (b));
707 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
708 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
709 EMPATHY_ROSTER_GROUP (a));
711 g_return_val_if_reached (0);
715 update_separator (GtkWidget **separator,
722 /* No separator before the first row */
723 g_clear_object (separator);
727 if (*separator != NULL)
730 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
731 g_object_ref_sink (*separator);
735 is_searching (EmpathyRosterView *self)
737 if (self->priv->search == NULL)
740 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
744 update_empty (EmpathyRosterView *self,
747 if (self->priv->empty == empty)
750 self->priv->empty = empty;
751 g_object_notify (G_OBJECT (self), "empty");
755 add_to_displayed (EmpathyRosterView *self,
756 EmpathyRosterContact *contact)
758 FolksIndividual *individual;
759 GHashTable *contacts;
763 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
766 g_hash_table_add (self->priv->displayed_contacts, contact);
767 update_empty (self, FALSE);
769 /* Groups of this contact may now be displayed if we just displays the first
770 * child in this group. */
772 if (!self->priv->show_groups)
775 individual = empathy_roster_contact_get_individual (contact);
776 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
777 if (contacts == NULL)
780 g_hash_table_iter_init (&iter, contacts);
781 while (g_hash_table_iter_next (&iter, &k, NULL))
783 const gchar *group_name = k;
786 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
790 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
795 remove_from_displayed (EmpathyRosterView *self,
796 EmpathyRosterContact *contact)
798 g_hash_table_remove (self->priv->displayed_contacts, contact);
800 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
801 update_empty (self, TRUE);
805 * check if @contact should be displayed according to @self's current status
806 * and without consideration for the state of @contact's groups.
809 contact_should_be_displayed (EmpathyRosterView *self,
810 EmpathyRosterContact *contact)
812 if (is_searching (self))
814 FolksIndividual *individual;
816 individual = empathy_roster_contact_get_individual (contact);
818 return empathy_individual_match_string (individual,
819 empathy_live_search_get_text (self->priv->search),
820 empathy_live_search_get_words (self->priv->search));
823 if (self->priv->show_offline)
826 if (contact_is_favourite (contact))
828 const gchar *group_name;
830 if (!self->priv->show_groups)
831 /* Always display favourite contacts in non-group mode. */
834 group_name = empathy_roster_contact_get_group (contact);
836 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
837 /* Always display favourite contact in group mode only in the
842 return empathy_roster_contact_is_online (contact);
847 filter_contact (EmpathyRosterView *self,
848 EmpathyRosterContact *contact)
852 displayed = contact_should_be_displayed (self, contact);
854 if (self->priv->show_groups)
856 const gchar *group_name;
857 EmpathyRosterGroup *group;
859 group_name = empathy_roster_contact_get_group (contact);
860 group = lookup_roster_group (self, group_name);
864 /* When searching, always display even if the group is closed */
865 if (!is_searching (self) &&
866 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
873 add_to_displayed (self, contact);
877 remove_from_displayed (self, contact);
884 filter_group (EmpathyRosterView *self,
885 EmpathyRosterGroup *group)
889 /* Display the group if it contains at least one displayed contact */
890 widgets = empathy_roster_group_get_widgets (group);
891 for (l = widgets; l != NULL; l = g_list_next (l))
893 EmpathyRosterContact *contact = l->data;
895 if (contact_should_be_displayed (self, contact))
903 filter_list (GtkWidget *child,
906 EmpathyRosterView *self = user_data;
908 if (EMPATHY_IS_ROSTER_CONTACT (child))
909 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
911 else if (EMPATHY_IS_ROSTER_GROUP (child))
912 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
914 g_return_val_if_reached (FALSE);
917 /* @list: GList of EmpathyRosterContact
919 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
922 individual_in_list (FolksIndividual *individual,
927 for (l = list; l != NULL; l = g_list_next (l))
929 EmpathyRosterContact *contact = l->data;
931 if (empathy_roster_contact_get_individual (contact) == individual)
939 populate_view (EmpathyRosterView *self)
941 GList *individuals, *l;
943 individuals = empathy_roster_model_get_individuals (self->priv->model);
944 for (l = individuals; l != NULL; l = g_list_next (l))
946 FolksIndividual *individual = l->data;
948 individual_added (self, individual);
951 g_list_free (individuals);
955 remove_from_group (EmpathyRosterView *self,
956 FolksIndividual *individual,
959 GHashTable *contacts;
961 EmpathyRosterGroup *roster_group;
963 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
964 if (contacts == NULL)
967 contact = g_hash_table_lookup (contacts, group);
971 g_hash_table_remove (contacts, group);
973 if (g_hash_table_size (contacts) == 0)
975 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
978 roster_group = lookup_roster_group (self, group);
980 if (roster_group != NULL)
982 update_group_widgets (self, roster_group,
983 EMPATHY_ROSTER_CONTACT (contact), FALSE);
986 gtk_container_remove (GTK_CONTAINER (self), contact);
990 update_top_contacts (EmpathyRosterView *self)
993 GList *to_add = NULL, *to_remove = NULL;
994 EmpathyRosterGroup *group;
996 if (!self->priv->show_groups)
998 egg_list_box_resort (EGG_LIST_BOX (self));
1002 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
1004 group = g_hash_table_lookup (self->priv->roster_groups,
1005 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1008 to_add = g_list_copy (tops);
1014 contacts = empathy_roster_group_get_widgets (group);
1016 /* Check which EmpathyRosterContact have to be removed */
1017 for (l = contacts; l != NULL; l = g_list_next (l))
1019 EmpathyRosterContact *contact = l->data;
1020 FolksIndividual *individual;
1022 if (contact_is_favourite (contact))
1025 individual = empathy_roster_contact_get_individual (contact);
1027 if (g_list_find (tops, individual) == NULL)
1028 to_remove = g_list_prepend (to_remove, individual);
1031 /* Check which EmpathyRosterContact have to be added */
1032 for (l = tops; l != NULL; l = g_list_next (l))
1034 FolksIndividual *individual = l->data;
1036 if (!individual_in_list (individual, contacts))
1037 to_add = g_list_prepend (to_add, individual);
1041 for (l = to_add; l != NULL; l = g_list_next (l))
1042 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1044 for (l = to_remove; l != NULL; l = g_list_next (l))
1045 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1047 g_list_free (to_add);
1048 g_list_free (to_remove);
1052 groups_changed_cb (EmpathyIndividualManager *manager,
1053 FolksIndividual *individual,
1056 EmpathyRosterView *self)
1058 if (!self->priv->show_groups)
1063 add_to_group (self, individual, group);
1067 remove_from_group (self, individual, group);
1072 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1074 EmpathyRosterView *self)
1076 update_top_contacts (self);
1080 favourites_changed_cb (EmpathyIndividualManager *manager,
1081 FolksIndividual *individual,
1083 EmpathyRosterView *self)
1085 GHashTable *contacts;
1087 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1088 if (contacts == NULL)
1091 if (self->priv->show_groups)
1094 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1096 remove_from_group (self, individual,
1097 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1103 contact = g_hash_table_lookup (contacts, NO_GROUP);
1104 if (contact == NULL)
1107 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1112 empathy_roster_view_constructed (GObject *object)
1114 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1115 void (*chain_up) (GObject *) =
1116 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1118 if (chain_up != NULL)
1121 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1122 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1124 populate_view (self);
1126 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1127 G_CALLBACK (members_changed_cb), self, 0);
1128 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1129 G_CALLBACK (groups_changed_cb), self, 0);
1130 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1131 G_CALLBACK (top_individuals_changed_cb), self, 0);
1132 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1133 G_CALLBACK (favourites_changed_cb), self, 0);
1135 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1136 roster_view_sort, self, NULL);
1138 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1141 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1143 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1147 empathy_roster_view_dispose (GObject *object)
1149 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1150 void (*chain_up) (GObject *) =
1151 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1153 stop_flashing (self);
1155 empathy_roster_view_set_live_search (self, NULL);
1156 g_clear_object (&self->priv->manager);
1157 g_clear_object (&self->priv->model);
1159 if (chain_up != NULL)
1164 empathy_roster_view_finalize (GObject *object)
1166 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1167 void (*chain_up) (GObject *) =
1168 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1170 g_hash_table_unref (self->priv->roster_contacts);
1171 g_hash_table_unref (self->priv->roster_groups);
1172 g_hash_table_unref (self->priv->displayed_contacts);
1173 g_queue_free_full (self->priv->events, event_free);
1175 if (chain_up != NULL)
1180 empathy_roster_view_child_activated (EggListBox *box,
1183 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1184 EmpathyRosterContact *contact;
1185 FolksIndividual *individual;
1188 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1191 contact = EMPATHY_ROSTER_CONTACT (child);
1192 individual = empathy_roster_contact_get_individual (contact);
1194 /* Activate the oldest event associated with this contact, if any */
1195 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1196 l = g_list_previous (l))
1198 Event *event = l->data;
1200 if (event->individual == individual)
1202 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1208 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1212 fire_popup_individual_menu (EmpathyRosterView *self,
1217 EmpathyRosterContact *contact;
1218 FolksIndividual *individual;
1220 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1223 contact = EMPATHY_ROSTER_CONTACT (child);
1224 individual = empathy_roster_contact_get_individual (contact);
1226 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1227 individual, button, time);
1231 empathy_roster_view_button_press_event (GtkWidget *widget,
1232 GdkEventButton *event)
1234 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1235 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1236 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1238 if (event->button == 3)
1242 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1246 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1248 fire_popup_individual_menu (self, child, event->button, event->time);
1252 return chain_up (widget, event);
1256 empathy_roster_view_key_press_event (GtkWidget *widget,
1259 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1260 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1261 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1263 if (event->keyval == GDK_KEY_Menu)
1267 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1270 fire_popup_individual_menu (self, child, 0, event->time);
1273 return chain_up (widget, event);
1277 * @out_child: (out) (allow-none)
1280 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1282 GtkWidget **out_child)
1286 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1288 if (out_child != NULL)
1291 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1294 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1298 * @out_child: (out) (allow-none)
1301 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1306 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1308 if (EMPATHY_IS_ROSTER_CONTACT (child))
1309 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1310 else if (EMPATHY_IS_ROSTER_GROUP (child))
1311 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1317 empathy_roster_view_query_tooltip (GtkWidget *widget,
1320 gboolean keyboard_mode,
1321 GtkTooltip *tooltip)
1323 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1324 FolksIndividual *individual;
1328 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1329 if (individual == NULL)
1332 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1333 individual, keyboard_mode, tooltip, &result);
1337 GtkAllocation allocation;
1339 gtk_widget_get_allocation (child, &allocation);
1340 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1347 empathy_roster_view_remove (GtkContainer *container,
1350 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1351 void (*chain_up) (GtkContainer *, GtkWidget *) =
1352 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1354 chain_up (container, widget);
1356 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1357 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1361 empathy_roster_view_class_init (
1362 EmpathyRosterViewClass *klass)
1364 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1365 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1366 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1367 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1370 oclass->get_property = empathy_roster_view_get_property;
1371 oclass->set_property = empathy_roster_view_set_property;
1372 oclass->constructed = empathy_roster_view_constructed;
1373 oclass->dispose = empathy_roster_view_dispose;
1374 oclass->finalize = empathy_roster_view_finalize;
1376 widget_class->button_press_event = empathy_roster_view_button_press_event;
1377 widget_class->key_press_event = empathy_roster_view_key_press_event;
1378 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1380 container_class->remove = empathy_roster_view_remove;
1382 box_class->child_activated = empathy_roster_view_child_activated;
1384 spec = g_param_spec_object ("manager", "Manager",
1385 "EmpathyIndividualManager",
1386 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1387 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1388 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1390 spec = g_param_spec_object ("model", "Model",
1391 "EmpathyRosterModel",
1392 EMPATHY_TYPE_ROSTER_MODEL,
1393 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1394 g_object_class_install_property (oclass, PROP_MODEL, spec);
1396 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1397 "Show offline contacts",
1399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1400 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1402 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1406 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1408 spec = g_param_spec_boolean ("empty", "Empty",
1409 "Is the view currently empty?",
1411 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1412 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1414 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1415 G_OBJECT_CLASS_TYPE (klass),
1417 0, NULL, NULL, NULL,
1419 1, FOLKS_TYPE_INDIVIDUAL);
1421 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1422 G_OBJECT_CLASS_TYPE (klass),
1424 0, NULL, NULL, NULL,
1426 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1428 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1429 G_OBJECT_CLASS_TYPE (klass),
1431 0, NULL, NULL, NULL,
1433 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1435 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1436 G_OBJECT_CLASS_TYPE (klass),
1438 0, g_signal_accumulator_true_handled, NULL, NULL,
1440 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1442 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1446 empathy_roster_view_init (EmpathyRosterView *self)
1448 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1449 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1451 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1452 NULL, (GDestroyNotify) g_hash_table_unref);
1453 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1455 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1457 self->priv->events = g_queue_new ();
1459 self->priv->empty = TRUE;
1463 empathy_roster_view_new (EmpathyIndividualManager *manager,
1464 EmpathyRosterModel *model)
1466 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1467 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1469 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1475 EmpathyIndividualManager *
1476 empathy_roster_view_get_manager (EmpathyRosterView *self)
1478 return self->priv->manager;
1482 empathy_roster_view_show_offline (EmpathyRosterView *self,
1485 if (self->priv->show_offline == show)
1488 self->priv->show_offline = show;
1489 egg_list_box_refilter (EGG_LIST_BOX (self));
1491 g_object_notify (G_OBJECT (self), "show-offline");
1495 clear_view (EmpathyRosterView *self)
1497 gtk_container_foreach (GTK_CONTAINER (self),
1498 (GtkCallback) gtk_widget_destroy, NULL);
1500 g_hash_table_remove_all (self->priv->roster_contacts);
1501 g_hash_table_remove_all (self->priv->roster_groups);
1502 g_hash_table_remove_all (self->priv->displayed_contacts);
1506 empathy_roster_view_show_groups (EmpathyRosterView *self,
1509 if (self->priv->show_groups == show)
1512 self->priv->show_groups = show;
1514 /* TODO: block sort/filter? */
1516 populate_view (self);
1518 g_object_notify (G_OBJECT (self), "show-groups");
1522 select_first_contact (EmpathyRosterView *self)
1524 GList *children, *l;
1526 children = gtk_container_get_children (GTK_CONTAINER (self));
1527 for (l = children; l != NULL; l = g_list_next (l))
1529 GtkWidget *child = l->data;
1531 if (!gtk_widget_get_child_visible (child))
1534 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1537 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1541 g_list_free (children);
1545 search_text_notify_cb (EmpathyLiveSearch *search,
1547 EmpathyRosterView *self)
1549 egg_list_box_refilter (EGG_LIST_BOX (self));
1551 select_first_contact (self);
1555 search_activate_cb (GtkWidget *search,
1556 EmpathyRosterView *self)
1558 EggListBox *box = EGG_LIST_BOX (self);
1561 child = egg_list_box_get_selected_child (box);
1565 empathy_roster_view_child_activated (box, child);
1569 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1570 EmpathyLiveSearch *search)
1572 if (self->priv->search != NULL)
1574 g_signal_handlers_disconnect_by_func (self->priv->search,
1575 search_text_notify_cb, self);
1576 g_signal_handlers_disconnect_by_func (self->priv->search,
1577 search_activate_cb, self);
1579 g_clear_object (&self->priv->search);
1585 self->priv->search = g_object_ref (search);
1587 g_signal_connect (self->priv->search, "notify::text",
1588 G_CALLBACK (search_text_notify_cb), self);
1589 g_signal_connect (self->priv->search, "activate",
1590 G_CALLBACK (search_activate_cb), self);
1594 empathy_roster_view_is_empty (EmpathyRosterView *self)
1596 return self->priv->empty;
1600 empathy_roster_view_is_searching (EmpathyRosterView *self)
1602 return (self->priv->search != NULL &&
1603 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1606 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1607 * Empathy's internals. */
1609 empathy_roster_view_add_event (EmpathyRosterView *self,
1610 FolksIndividual *individual,
1614 GHashTable *contacts;
1616 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1617 if (contacts == NULL)
1620 self->priv->last_event_id++;
1622 g_queue_push_head (self->priv->events,
1623 event_new (self->priv->last_event_id, individual, icon, user_data));
1625 start_flashing (self);
1627 return self->priv->last_event_id;
1631 empathy_roster_view_remove_event (EmpathyRosterView *self,
1636 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1637 l = g_list_next (l))
1639 Event *event = l->data;
1641 if (event->id == event_id)
1643 remove_event (self, event);
1650 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1654 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1656 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1659 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));