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 individual_added_cb (EmpathyRosterModel *model,
525 FolksIndividual *individual,
526 EmpathyRosterView *self)
528 individual_added (self, individual);
532 individual_removed_cb (EmpathyRosterModel *model,
533 FolksIndividual *individual,
534 EmpathyRosterView *self)
536 individual_removed (self, individual);
540 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
541 EmpathyRosterContact *b)
543 FolksIndividual *ind_a, *ind_b;
544 const gchar *alias_a, *alias_b;
546 ind_a = empathy_roster_contact_get_individual (a);
547 ind_b = empathy_roster_contact_get_individual (b);
549 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
550 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
552 return g_ascii_strcasecmp (alias_a, alias_b);
556 contact_is_favourite (EmpathyRosterContact *contact)
558 FolksIndividual *individual;
560 individual = empathy_roster_contact_get_individual (contact);
562 return folks_favourite_details_get_is_favourite (
563 FOLKS_FAVOURITE_DETAILS (individual));
567 contact_in_top (EmpathyRosterView *self,
568 EmpathyRosterContact *contact)
570 FolksIndividual *individual;
573 if (contact_is_favourite (contact))
576 individual = empathy_roster_contact_get_individual (contact);
578 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
580 if (g_list_index (tops, individual) != -1)
587 compare_roster_contacts_no_group (EmpathyRosterView *self,
588 EmpathyRosterContact *a,
589 EmpathyRosterContact *b)
591 gboolean top_a, top_b;
593 top_a = contact_in_top (self, a);
594 top_b = contact_in_top (self, b);
597 /* Both contacts are in the top of the roster (or not). Sort them
599 return compare_roster_contacts_by_alias (a, b);
607 compare_group_names (const gchar *group_a,
608 const gchar *group_b)
610 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
613 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
616 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
618 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
621 return g_ascii_strcasecmp (group_a, group_b);
625 compare_roster_contacts_with_groups (EmpathyRosterView *self,
626 EmpathyRosterContact *a,
627 EmpathyRosterContact *b)
629 const gchar *group_a, *group_b;
631 group_a = empathy_roster_contact_get_group (a);
632 group_b = empathy_roster_contact_get_group (b);
634 if (!tp_strdiff (group_a, group_b))
635 /* Same group, compare the contacts */
636 return compare_roster_contacts_by_alias (a, b);
639 return compare_group_names (group_a, group_b);
643 compare_roster_contacts (EmpathyRosterView *self,
644 EmpathyRosterContact *a,
645 EmpathyRosterContact *b)
647 if (!self->priv->show_groups)
648 return compare_roster_contacts_no_group (self, a, b);
650 return compare_roster_contacts_with_groups (self, a, b);
654 compare_roster_groups (EmpathyRosterGroup *a,
655 EmpathyRosterGroup *b)
657 const gchar *name_a, *name_b;
659 name_a = empathy_roster_group_get_name (a);
660 name_b = empathy_roster_group_get_name (b);
662 return compare_group_names (name_a, name_b);
666 compare_contact_group (EmpathyRosterContact *contact,
667 EmpathyRosterGroup *group)
669 const char *contact_group, *group_name;
671 contact_group = empathy_roster_contact_get_group (contact);
672 group_name = empathy_roster_group_get_name (group);
674 if (!tp_strdiff (contact_group, group_name))
675 /* @contact is in @group, @group has to be displayed first */
678 /* @contact is in a different group, sort by group name */
679 return compare_group_names (contact_group, group_name);
683 roster_view_sort (gconstpointer a,
687 EmpathyRosterView *self = user_data;
689 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
690 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
691 EMPATHY_ROSTER_CONTACT (b));
692 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
693 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
694 EMPATHY_ROSTER_GROUP (b));
695 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
696 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
697 EMPATHY_ROSTER_GROUP (b));
698 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
699 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
700 EMPATHY_ROSTER_GROUP (a));
702 g_return_val_if_reached (0);
706 update_separator (GtkWidget **separator,
713 /* No separator before the first row */
714 g_clear_object (separator);
718 if (*separator != NULL)
721 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
722 g_object_ref_sink (*separator);
726 is_searching (EmpathyRosterView *self)
728 if (self->priv->search == NULL)
731 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
735 update_empty (EmpathyRosterView *self,
738 if (self->priv->empty == empty)
741 self->priv->empty = empty;
742 g_object_notify (G_OBJECT (self), "empty");
746 add_to_displayed (EmpathyRosterView *self,
747 EmpathyRosterContact *contact)
749 FolksIndividual *individual;
750 GHashTable *contacts;
754 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
757 g_hash_table_add (self->priv->displayed_contacts, contact);
758 update_empty (self, FALSE);
760 /* Groups of this contact may now be displayed if we just displays the first
761 * child in this group. */
763 if (!self->priv->show_groups)
766 individual = empathy_roster_contact_get_individual (contact);
767 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
768 if (contacts == NULL)
771 g_hash_table_iter_init (&iter, contacts);
772 while (g_hash_table_iter_next (&iter, &k, NULL))
774 const gchar *group_name = k;
777 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
781 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
786 remove_from_displayed (EmpathyRosterView *self,
787 EmpathyRosterContact *contact)
789 g_hash_table_remove (self->priv->displayed_contacts, contact);
791 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
792 update_empty (self, TRUE);
796 * check if @contact should be displayed according to @self's current status
797 * and without consideration for the state of @contact's groups.
800 contact_should_be_displayed (EmpathyRosterView *self,
801 EmpathyRosterContact *contact)
803 if (is_searching (self))
805 FolksIndividual *individual;
807 individual = empathy_roster_contact_get_individual (contact);
809 return empathy_individual_match_string (individual,
810 empathy_live_search_get_text (self->priv->search),
811 empathy_live_search_get_words (self->priv->search));
814 if (self->priv->show_offline)
817 if (contact_is_favourite (contact))
819 const gchar *group_name;
821 if (!self->priv->show_groups)
822 /* Always display favourite contacts in non-group mode. */
825 group_name = empathy_roster_contact_get_group (contact);
827 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
828 /* Always display favourite contact in group mode only in the
833 return empathy_roster_contact_is_online (contact);
838 filter_contact (EmpathyRosterView *self,
839 EmpathyRosterContact *contact)
843 displayed = contact_should_be_displayed (self, contact);
845 if (self->priv->show_groups)
847 const gchar *group_name;
848 EmpathyRosterGroup *group;
850 group_name = empathy_roster_contact_get_group (contact);
851 group = lookup_roster_group (self, group_name);
855 /* When searching, always display even if the group is closed */
856 if (!is_searching (self) &&
857 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
864 add_to_displayed (self, contact);
868 remove_from_displayed (self, contact);
875 filter_group (EmpathyRosterView *self,
876 EmpathyRosterGroup *group)
880 /* Display the group if it contains at least one displayed contact */
881 widgets = empathy_roster_group_get_widgets (group);
882 for (l = widgets; l != NULL; l = g_list_next (l))
884 EmpathyRosterContact *contact = l->data;
886 if (contact_should_be_displayed (self, contact))
894 filter_list (GtkWidget *child,
897 EmpathyRosterView *self = user_data;
899 if (EMPATHY_IS_ROSTER_CONTACT (child))
900 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
902 else if (EMPATHY_IS_ROSTER_GROUP (child))
903 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
905 g_return_val_if_reached (FALSE);
908 /* @list: GList of EmpathyRosterContact
910 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
913 individual_in_list (FolksIndividual *individual,
918 for (l = list; l != NULL; l = g_list_next (l))
920 EmpathyRosterContact *contact = l->data;
922 if (empathy_roster_contact_get_individual (contact) == individual)
930 populate_view (EmpathyRosterView *self)
932 GList *individuals, *l;
934 individuals = empathy_roster_model_get_individuals (self->priv->model);
935 for (l = individuals; l != NULL; l = g_list_next (l))
937 FolksIndividual *individual = l->data;
939 individual_added (self, individual);
942 g_list_free (individuals);
946 remove_from_group (EmpathyRosterView *self,
947 FolksIndividual *individual,
950 GHashTable *contacts;
952 EmpathyRosterGroup *roster_group;
954 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
955 if (contacts == NULL)
958 contact = g_hash_table_lookup (contacts, group);
962 g_hash_table_remove (contacts, group);
964 if (g_hash_table_size (contacts) == 0)
966 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
969 roster_group = lookup_roster_group (self, group);
971 if (roster_group != NULL)
973 update_group_widgets (self, roster_group,
974 EMPATHY_ROSTER_CONTACT (contact), FALSE);
977 gtk_container_remove (GTK_CONTAINER (self), contact);
981 update_top_contacts (EmpathyRosterView *self)
984 GList *to_add = NULL, *to_remove = NULL;
985 EmpathyRosterGroup *group;
987 if (!self->priv->show_groups)
989 egg_list_box_resort (EGG_LIST_BOX (self));
993 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
995 group = g_hash_table_lookup (self->priv->roster_groups,
996 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
999 to_add = g_list_copy (tops);
1005 contacts = empathy_roster_group_get_widgets (group);
1007 /* Check which EmpathyRosterContact have to be removed */
1008 for (l = contacts; l != NULL; l = g_list_next (l))
1010 EmpathyRosterContact *contact = l->data;
1011 FolksIndividual *individual;
1013 if (contact_is_favourite (contact))
1016 individual = empathy_roster_contact_get_individual (contact);
1018 if (g_list_find (tops, individual) == NULL)
1019 to_remove = g_list_prepend (to_remove, individual);
1022 /* Check which EmpathyRosterContact have to be added */
1023 for (l = tops; l != NULL; l = g_list_next (l))
1025 FolksIndividual *individual = l->data;
1027 if (!individual_in_list (individual, contacts))
1028 to_add = g_list_prepend (to_add, individual);
1032 for (l = to_add; l != NULL; l = g_list_next (l))
1033 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1035 for (l = to_remove; l != NULL; l = g_list_next (l))
1036 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1038 g_list_free (to_add);
1039 g_list_free (to_remove);
1043 groups_changed_cb (EmpathyIndividualManager *manager,
1044 FolksIndividual *individual,
1047 EmpathyRosterView *self)
1049 if (!self->priv->show_groups)
1054 add_to_group (self, individual, group);
1058 remove_from_group (self, individual, group);
1063 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1065 EmpathyRosterView *self)
1067 update_top_contacts (self);
1071 favourites_changed_cb (EmpathyIndividualManager *manager,
1072 FolksIndividual *individual,
1074 EmpathyRosterView *self)
1076 GHashTable *contacts;
1078 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1079 if (contacts == NULL)
1082 if (self->priv->show_groups)
1085 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1087 remove_from_group (self, individual,
1088 EMPATHY_ROSTER_VIEW_GROUP_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));
1113 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1115 populate_view (self);
1117 tp_g_signal_connect_object (self->priv->model, "individual-added",
1118 G_CALLBACK (individual_added_cb), self, 0);
1119 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1120 G_CALLBACK (individual_removed_cb), self, 0);
1121 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1122 G_CALLBACK (groups_changed_cb), self, 0);
1123 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1124 G_CALLBACK (top_individuals_changed_cb), self, 0);
1125 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1126 G_CALLBACK (favourites_changed_cb), self, 0);
1128 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1129 roster_view_sort, self, NULL);
1131 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1134 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1136 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1140 empathy_roster_view_dispose (GObject *object)
1142 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1143 void (*chain_up) (GObject *) =
1144 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1146 stop_flashing (self);
1148 empathy_roster_view_set_live_search (self, NULL);
1149 g_clear_object (&self->priv->manager);
1150 g_clear_object (&self->priv->model);
1152 if (chain_up != NULL)
1157 empathy_roster_view_finalize (GObject *object)
1159 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1160 void (*chain_up) (GObject *) =
1161 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1163 g_hash_table_unref (self->priv->roster_contacts);
1164 g_hash_table_unref (self->priv->roster_groups);
1165 g_hash_table_unref (self->priv->displayed_contacts);
1166 g_queue_free_full (self->priv->events, event_free);
1168 if (chain_up != NULL)
1173 empathy_roster_view_child_activated (EggListBox *box,
1176 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1177 EmpathyRosterContact *contact;
1178 FolksIndividual *individual;
1181 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1184 contact = EMPATHY_ROSTER_CONTACT (child);
1185 individual = empathy_roster_contact_get_individual (contact);
1187 /* Activate the oldest event associated with this contact, if any */
1188 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1189 l = g_list_previous (l))
1191 Event *event = l->data;
1193 if (event->individual == individual)
1195 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1201 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1205 fire_popup_individual_menu (EmpathyRosterView *self,
1210 EmpathyRosterContact *contact;
1211 FolksIndividual *individual;
1213 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1216 contact = EMPATHY_ROSTER_CONTACT (child);
1217 individual = empathy_roster_contact_get_individual (contact);
1219 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1220 individual, button, time);
1224 empathy_roster_view_button_press_event (GtkWidget *widget,
1225 GdkEventButton *event)
1227 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1228 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1229 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1231 if (event->button == 3)
1235 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1239 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1241 fire_popup_individual_menu (self, child, event->button, event->time);
1245 return chain_up (widget, event);
1249 empathy_roster_view_key_press_event (GtkWidget *widget,
1252 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1253 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1254 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1256 if (event->keyval == GDK_KEY_Menu)
1260 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1263 fire_popup_individual_menu (self, child, 0, event->time);
1266 return chain_up (widget, event);
1270 * @out_child: (out) (allow-none)
1273 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1275 GtkWidget **out_child)
1279 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1281 if (out_child != NULL)
1284 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1287 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1291 * @out_child: (out) (allow-none)
1294 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1299 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1301 if (EMPATHY_IS_ROSTER_CONTACT (child))
1302 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1303 else if (EMPATHY_IS_ROSTER_GROUP (child))
1304 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1310 empathy_roster_view_query_tooltip (GtkWidget *widget,
1313 gboolean keyboard_mode,
1314 GtkTooltip *tooltip)
1316 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1317 FolksIndividual *individual;
1321 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1322 if (individual == NULL)
1325 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1326 individual, keyboard_mode, tooltip, &result);
1330 GtkAllocation allocation;
1332 gtk_widget_get_allocation (child, &allocation);
1333 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1340 empathy_roster_view_remove (GtkContainer *container,
1343 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1344 void (*chain_up) (GtkContainer *, GtkWidget *) =
1345 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1347 chain_up (container, widget);
1349 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1350 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1354 empathy_roster_view_class_init (
1355 EmpathyRosterViewClass *klass)
1357 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1358 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1359 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1360 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1363 oclass->get_property = empathy_roster_view_get_property;
1364 oclass->set_property = empathy_roster_view_set_property;
1365 oclass->constructed = empathy_roster_view_constructed;
1366 oclass->dispose = empathy_roster_view_dispose;
1367 oclass->finalize = empathy_roster_view_finalize;
1369 widget_class->button_press_event = empathy_roster_view_button_press_event;
1370 widget_class->key_press_event = empathy_roster_view_key_press_event;
1371 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1373 container_class->remove = empathy_roster_view_remove;
1375 box_class->child_activated = empathy_roster_view_child_activated;
1377 spec = g_param_spec_object ("manager", "Manager",
1378 "EmpathyIndividualManager",
1379 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1380 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1381 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1383 spec = g_param_spec_object ("model", "Model",
1384 "EmpathyRosterModel",
1385 EMPATHY_TYPE_ROSTER_MODEL,
1386 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1387 g_object_class_install_property (oclass, PROP_MODEL, spec);
1389 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1390 "Show offline contacts",
1392 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1393 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1395 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1399 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1401 spec = g_param_spec_boolean ("empty", "Empty",
1402 "Is the view currently empty?",
1404 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1405 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1407 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1408 G_OBJECT_CLASS_TYPE (klass),
1410 0, NULL, NULL, NULL,
1412 1, FOLKS_TYPE_INDIVIDUAL);
1414 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1415 G_OBJECT_CLASS_TYPE (klass),
1417 0, NULL, NULL, NULL,
1419 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1421 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1422 G_OBJECT_CLASS_TYPE (klass),
1424 0, NULL, NULL, NULL,
1426 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1428 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1429 G_OBJECT_CLASS_TYPE (klass),
1431 0, g_signal_accumulator_true_handled, NULL, NULL,
1433 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1435 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1439 empathy_roster_view_init (EmpathyRosterView *self)
1441 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1442 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1444 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1445 NULL, (GDestroyNotify) g_hash_table_unref);
1446 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1448 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1450 self->priv->events = g_queue_new ();
1452 self->priv->empty = TRUE;
1456 empathy_roster_view_new (EmpathyIndividualManager *manager,
1457 EmpathyRosterModel *model)
1459 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1460 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1462 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1468 EmpathyIndividualManager *
1469 empathy_roster_view_get_manager (EmpathyRosterView *self)
1471 return self->priv->manager;
1475 empathy_roster_view_show_offline (EmpathyRosterView *self,
1478 if (self->priv->show_offline == show)
1481 self->priv->show_offline = show;
1482 egg_list_box_refilter (EGG_LIST_BOX (self));
1484 g_object_notify (G_OBJECT (self), "show-offline");
1488 clear_view (EmpathyRosterView *self)
1490 gtk_container_foreach (GTK_CONTAINER (self),
1491 (GtkCallback) gtk_widget_destroy, NULL);
1493 g_hash_table_remove_all (self->priv->roster_contacts);
1494 g_hash_table_remove_all (self->priv->roster_groups);
1495 g_hash_table_remove_all (self->priv->displayed_contacts);
1499 empathy_roster_view_show_groups (EmpathyRosterView *self,
1502 if (self->priv->show_groups == show)
1505 self->priv->show_groups = show;
1507 /* TODO: block sort/filter? */
1509 populate_view (self);
1511 g_object_notify (G_OBJECT (self), "show-groups");
1515 select_first_contact (EmpathyRosterView *self)
1517 GList *children, *l;
1519 children = gtk_container_get_children (GTK_CONTAINER (self));
1520 for (l = children; l != NULL; l = g_list_next (l))
1522 GtkWidget *child = l->data;
1524 if (!gtk_widget_get_child_visible (child))
1527 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1530 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1534 g_list_free (children);
1538 search_text_notify_cb (EmpathyLiveSearch *search,
1540 EmpathyRosterView *self)
1542 egg_list_box_refilter (EGG_LIST_BOX (self));
1544 select_first_contact (self);
1548 search_activate_cb (GtkWidget *search,
1549 EmpathyRosterView *self)
1551 EggListBox *box = EGG_LIST_BOX (self);
1554 child = egg_list_box_get_selected_child (box);
1558 empathy_roster_view_child_activated (box, child);
1562 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1563 EmpathyLiveSearch *search)
1565 if (self->priv->search != NULL)
1567 g_signal_handlers_disconnect_by_func (self->priv->search,
1568 search_text_notify_cb, self);
1569 g_signal_handlers_disconnect_by_func (self->priv->search,
1570 search_activate_cb, self);
1572 g_clear_object (&self->priv->search);
1578 self->priv->search = g_object_ref (search);
1580 g_signal_connect (self->priv->search, "notify::text",
1581 G_CALLBACK (search_text_notify_cb), self);
1582 g_signal_connect (self->priv->search, "activate",
1583 G_CALLBACK (search_activate_cb), self);
1587 empathy_roster_view_is_empty (EmpathyRosterView *self)
1589 return self->priv->empty;
1593 empathy_roster_view_is_searching (EmpathyRosterView *self)
1595 return (self->priv->search != NULL &&
1596 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1599 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1600 * Empathy's internals. */
1602 empathy_roster_view_add_event (EmpathyRosterView *self,
1603 FolksIndividual *individual,
1607 GHashTable *contacts;
1609 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1610 if (contacts == NULL)
1613 self->priv->last_event_id++;
1615 g_queue_push_head (self->priv->events,
1616 event_new (self->priv->last_event_id, individual, icon, user_data));
1618 start_flashing (self);
1620 return self->priv->last_event_id;
1624 empathy_roster_view_remove_event (EmpathyRosterView *self,
1629 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1630 l = g_list_next (l))
1632 Event *event = l->data;
1634 if (event->id == event_id)
1636 remove_event (self, event);
1643 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1647 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1649 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1652 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));