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 = empathy_roster_model_get_groups_for_individual (self->priv->model,
357 if (g_list_length (groups) > 0)
359 for (l = groups; l != NULL; l = g_list_next (l))
361 add_to_group (self, individual, l->data);
366 /* No group, adds to Ungrouped */
367 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
370 g_list_free (groups);
375 set_event_icon_on_individual (EmpathyRosterView *self,
376 FolksIndividual *individual,
379 GHashTable *contacts;
383 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
384 if (contacts == NULL)
387 g_hash_table_iter_init (&iter, contacts);
388 while (g_hash_table_iter_next (&iter, NULL, &v))
390 EmpathyRosterContact *contact =v;
392 empathy_roster_contact_set_event_icon (contact, icon);
397 flash_event (Event *event,
398 EmpathyRosterView *self)
400 set_event_icon_on_individual (self, event->individual, event->icon);
404 unflash_event (Event *event,
405 EmpathyRosterView *self)
407 set_event_icon_on_individual (self, event->individual, NULL);
411 flash_cb (gpointer data)
413 EmpathyRosterView *self = data;
415 if (self->priv->display_flash_event)
417 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
418 self->priv->display_flash_event = FALSE;
422 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
423 self->priv->display_flash_event = TRUE;
430 start_flashing (EmpathyRosterView *self)
432 if (self->priv->flash_id != 0)
435 self->priv->display_flash_event = TRUE;
437 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
442 stop_flashing (EmpathyRosterView *self)
444 if (self->priv->flash_id == 0)
447 g_source_remove (self->priv->flash_id);
448 self->priv->flash_id = 0;
452 remove_event (EmpathyRosterView *self,
455 unflash_event (event, self);
456 g_queue_remove (self->priv->events, event);
458 if (g_queue_get_length (self->priv->events) == 0)
460 stop_flashing (self);
465 remove_all_individual_event (EmpathyRosterView *self,
466 FolksIndividual *individual)
470 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
473 Event *event = l->data;
475 if (event->individual == individual)
477 remove_event (self, event);
484 individual_removed (EmpathyRosterView *self,
485 FolksIndividual *individual)
487 GHashTable *contacts;
491 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
492 if (contacts == NULL)
495 remove_all_individual_event (self, individual);
497 g_hash_table_iter_init (&iter, contacts);
498 while (g_hash_table_iter_next (&iter, &key, &value))
500 const gchar *group_name = key;
501 GtkWidget *contact = value;
502 EmpathyRosterGroup *group;
504 group = lookup_roster_group (self, group_name);
507 update_group_widgets (self, group,
508 EMPATHY_ROSTER_CONTACT (contact), FALSE);
511 gtk_container_remove (GTK_CONTAINER (self), contact);
514 g_hash_table_remove (self->priv->roster_contacts, individual);
518 individual_added_cb (EmpathyRosterModel *model,
519 FolksIndividual *individual,
520 EmpathyRosterView *self)
522 individual_added (self, individual);
526 individual_removed_cb (EmpathyRosterModel *model,
527 FolksIndividual *individual,
528 EmpathyRosterView *self)
530 individual_removed (self, individual);
534 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
535 EmpathyRosterContact *b)
537 FolksIndividual *ind_a, *ind_b;
538 const gchar *alias_a, *alias_b;
540 ind_a = empathy_roster_contact_get_individual (a);
541 ind_b = empathy_roster_contact_get_individual (b);
543 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
544 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
546 return g_ascii_strcasecmp (alias_a, alias_b);
550 contact_is_favourite (EmpathyRosterContact *contact)
552 FolksIndividual *individual;
554 individual = empathy_roster_contact_get_individual (contact);
556 return folks_favourite_details_get_is_favourite (
557 FOLKS_FAVOURITE_DETAILS (individual));
561 contact_in_top (EmpathyRosterView *self,
562 EmpathyRosterContact *contact)
564 FolksIndividual *individual;
567 if (contact_is_favourite (contact))
570 individual = empathy_roster_contact_get_individual (contact);
572 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
574 if (g_list_index (tops, individual) != -1)
581 compare_roster_contacts_no_group (EmpathyRosterView *self,
582 EmpathyRosterContact *a,
583 EmpathyRosterContact *b)
585 gboolean top_a, top_b;
587 top_a = contact_in_top (self, a);
588 top_b = contact_in_top (self, b);
591 /* Both contacts are in the top of the roster (or not). Sort them
593 return compare_roster_contacts_by_alias (a, b);
601 compare_group_names (const gchar *group_a,
602 const gchar *group_b)
604 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
607 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
610 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
612 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
615 return g_ascii_strcasecmp (group_a, group_b);
619 compare_roster_contacts_with_groups (EmpathyRosterView *self,
620 EmpathyRosterContact *a,
621 EmpathyRosterContact *b)
623 const gchar *group_a, *group_b;
625 group_a = empathy_roster_contact_get_group (a);
626 group_b = empathy_roster_contact_get_group (b);
628 if (!tp_strdiff (group_a, group_b))
629 /* Same group, compare the contacts */
630 return compare_roster_contacts_by_alias (a, b);
633 return compare_group_names (group_a, group_b);
637 compare_roster_contacts (EmpathyRosterView *self,
638 EmpathyRosterContact *a,
639 EmpathyRosterContact *b)
641 if (!self->priv->show_groups)
642 return compare_roster_contacts_no_group (self, a, b);
644 return compare_roster_contacts_with_groups (self, a, b);
648 compare_roster_groups (EmpathyRosterGroup *a,
649 EmpathyRosterGroup *b)
651 const gchar *name_a, *name_b;
653 name_a = empathy_roster_group_get_name (a);
654 name_b = empathy_roster_group_get_name (b);
656 return compare_group_names (name_a, name_b);
660 compare_contact_group (EmpathyRosterContact *contact,
661 EmpathyRosterGroup *group)
663 const char *contact_group, *group_name;
665 contact_group = empathy_roster_contact_get_group (contact);
666 group_name = empathy_roster_group_get_name (group);
668 if (!tp_strdiff (contact_group, group_name))
669 /* @contact is in @group, @group has to be displayed first */
672 /* @contact is in a different group, sort by group name */
673 return compare_group_names (contact_group, group_name);
677 roster_view_sort (gconstpointer a,
681 EmpathyRosterView *self = user_data;
683 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
684 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
685 EMPATHY_ROSTER_CONTACT (b));
686 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
687 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
688 EMPATHY_ROSTER_GROUP (b));
689 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
690 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
691 EMPATHY_ROSTER_GROUP (b));
692 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
693 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
694 EMPATHY_ROSTER_GROUP (a));
696 g_return_val_if_reached (0);
700 update_separator (GtkWidget **separator,
707 /* No separator before the first row */
708 g_clear_object (separator);
712 if (*separator != NULL)
715 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
716 g_object_ref_sink (*separator);
720 is_searching (EmpathyRosterView *self)
722 if (self->priv->search == NULL)
725 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
729 update_empty (EmpathyRosterView *self,
732 if (self->priv->empty == empty)
735 self->priv->empty = empty;
736 g_object_notify (G_OBJECT (self), "empty");
740 add_to_displayed (EmpathyRosterView *self,
741 EmpathyRosterContact *contact)
743 FolksIndividual *individual;
744 GHashTable *contacts;
748 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
751 g_hash_table_add (self->priv->displayed_contacts, contact);
752 update_empty (self, FALSE);
754 /* Groups of this contact may now be displayed if we just displays the first
755 * child in this group. */
757 if (!self->priv->show_groups)
760 individual = empathy_roster_contact_get_individual (contact);
761 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
762 if (contacts == NULL)
765 g_hash_table_iter_init (&iter, contacts);
766 while (g_hash_table_iter_next (&iter, &k, NULL))
768 const gchar *group_name = k;
771 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
775 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
780 remove_from_displayed (EmpathyRosterView *self,
781 EmpathyRosterContact *contact)
783 g_hash_table_remove (self->priv->displayed_contacts, contact);
785 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
786 update_empty (self, TRUE);
790 * check if @contact should be displayed according to @self's current status
791 * and without consideration for the state of @contact's groups.
794 contact_should_be_displayed (EmpathyRosterView *self,
795 EmpathyRosterContact *contact)
797 if (is_searching (self))
799 FolksIndividual *individual;
801 individual = empathy_roster_contact_get_individual (contact);
803 return empathy_individual_match_string (individual,
804 empathy_live_search_get_text (self->priv->search),
805 empathy_live_search_get_words (self->priv->search));
808 if (self->priv->show_offline)
811 if (contact_is_favourite (contact))
813 const gchar *group_name;
815 if (!self->priv->show_groups)
816 /* Always display favourite contacts in non-group mode. */
819 group_name = empathy_roster_contact_get_group (contact);
821 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
822 /* Always display favourite contact in group mode only in the
827 return empathy_roster_contact_is_online (contact);
832 filter_contact (EmpathyRosterView *self,
833 EmpathyRosterContact *contact)
837 displayed = contact_should_be_displayed (self, contact);
839 if (self->priv->show_groups)
841 const gchar *group_name;
842 EmpathyRosterGroup *group;
844 group_name = empathy_roster_contact_get_group (contact);
845 group = lookup_roster_group (self, group_name);
849 /* When searching, always display even if the group is closed */
850 if (!is_searching (self) &&
851 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
858 add_to_displayed (self, contact);
862 remove_from_displayed (self, contact);
869 filter_group (EmpathyRosterView *self,
870 EmpathyRosterGroup *group)
874 /* Display the group if it contains at least one displayed contact */
875 widgets = empathy_roster_group_get_widgets (group);
876 for (l = widgets; l != NULL; l = g_list_next (l))
878 EmpathyRosterContact *contact = l->data;
880 if (contact_should_be_displayed (self, contact))
888 filter_list (GtkWidget *child,
891 EmpathyRosterView *self = user_data;
893 if (EMPATHY_IS_ROSTER_CONTACT (child))
894 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
896 else if (EMPATHY_IS_ROSTER_GROUP (child))
897 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
899 g_return_val_if_reached (FALSE);
902 /* @list: GList of EmpathyRosterContact
904 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
907 individual_in_list (FolksIndividual *individual,
912 for (l = list; l != NULL; l = g_list_next (l))
914 EmpathyRosterContact *contact = l->data;
916 if (empathy_roster_contact_get_individual (contact) == individual)
924 populate_view (EmpathyRosterView *self)
926 GList *individuals, *l;
928 individuals = empathy_roster_model_get_individuals (self->priv->model);
929 for (l = individuals; l != NULL; l = g_list_next (l))
931 FolksIndividual *individual = l->data;
933 individual_added (self, individual);
936 g_list_free (individuals);
940 remove_from_group (EmpathyRosterView *self,
941 FolksIndividual *individual,
944 GHashTable *contacts;
946 EmpathyRosterGroup *roster_group;
948 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
949 if (contacts == NULL)
952 contact = g_hash_table_lookup (contacts, group);
956 g_hash_table_remove (contacts, group);
958 if (g_hash_table_size (contacts) == 0)
960 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
963 roster_group = lookup_roster_group (self, group);
965 if (roster_group != NULL)
967 update_group_widgets (self, roster_group,
968 EMPATHY_ROSTER_CONTACT (contact), FALSE);
971 gtk_container_remove (GTK_CONTAINER (self), contact);
975 update_top_contacts (EmpathyRosterView *self)
978 GList *to_add = NULL, *to_remove = NULL;
979 EmpathyRosterGroup *group;
981 if (!self->priv->show_groups)
983 egg_list_box_resort (EGG_LIST_BOX (self));
987 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
989 group = g_hash_table_lookup (self->priv->roster_groups,
990 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
993 to_add = g_list_copy (tops);
999 contacts = empathy_roster_group_get_widgets (group);
1001 /* Check which EmpathyRosterContact have to be removed */
1002 for (l = contacts; l != NULL; l = g_list_next (l))
1004 EmpathyRosterContact *contact = l->data;
1005 FolksIndividual *individual;
1007 if (contact_is_favourite (contact))
1010 individual = empathy_roster_contact_get_individual (contact);
1012 if (g_list_find (tops, individual) == NULL)
1013 to_remove = g_list_prepend (to_remove, individual);
1016 /* Check which EmpathyRosterContact have to be added */
1017 for (l = tops; l != NULL; l = g_list_next (l))
1019 FolksIndividual *individual = l->data;
1021 if (!individual_in_list (individual, contacts))
1022 to_add = g_list_prepend (to_add, individual);
1026 for (l = to_add; l != NULL; l = g_list_next (l))
1027 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1029 for (l = to_remove; l != NULL; l = g_list_next (l))
1030 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1032 g_list_free (to_add);
1033 g_list_free (to_remove);
1037 groups_changed_cb (EmpathyRosterModel *model,
1038 FolksIndividual *individual,
1041 EmpathyRosterView *self)
1043 if (!self->priv->show_groups)
1048 add_to_group (self, individual, group);
1052 remove_from_group (self, individual, group);
1057 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1059 EmpathyRosterView *self)
1061 update_top_contacts (self);
1065 favourites_changed_cb (EmpathyIndividualManager *manager,
1066 FolksIndividual *individual,
1068 EmpathyRosterView *self)
1070 GHashTable *contacts;
1072 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1073 if (contacts == NULL)
1076 if (self->priv->show_groups)
1079 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1081 remove_from_group (self, individual,
1082 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1088 contact = g_hash_table_lookup (contacts, NO_GROUP);
1089 if (contact == NULL)
1092 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1097 empathy_roster_view_constructed (GObject *object)
1099 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1100 void (*chain_up) (GObject *) =
1101 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1103 if (chain_up != NULL)
1106 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1107 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1109 populate_view (self);
1111 tp_g_signal_connect_object (self->priv->model, "individual-added",
1112 G_CALLBACK (individual_added_cb), self, 0);
1113 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1114 G_CALLBACK (individual_removed_cb), self, 0);
1115 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1116 G_CALLBACK (groups_changed_cb), self, 0);
1117 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1118 G_CALLBACK (top_individuals_changed_cb), self, 0);
1119 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1120 G_CALLBACK (favourites_changed_cb), self, 0);
1122 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1123 roster_view_sort, self, NULL);
1125 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1128 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1130 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1134 empathy_roster_view_dispose (GObject *object)
1136 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1137 void (*chain_up) (GObject *) =
1138 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1140 stop_flashing (self);
1142 empathy_roster_view_set_live_search (self, NULL);
1143 g_clear_object (&self->priv->manager);
1144 g_clear_object (&self->priv->model);
1146 if (chain_up != NULL)
1151 empathy_roster_view_finalize (GObject *object)
1153 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1154 void (*chain_up) (GObject *) =
1155 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1157 g_hash_table_unref (self->priv->roster_contacts);
1158 g_hash_table_unref (self->priv->roster_groups);
1159 g_hash_table_unref (self->priv->displayed_contacts);
1160 g_queue_free_full (self->priv->events, event_free);
1162 if (chain_up != NULL)
1167 empathy_roster_view_child_activated (EggListBox *box,
1170 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1171 EmpathyRosterContact *contact;
1172 FolksIndividual *individual;
1175 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1178 contact = EMPATHY_ROSTER_CONTACT (child);
1179 individual = empathy_roster_contact_get_individual (contact);
1181 /* Activate the oldest event associated with this contact, if any */
1182 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1183 l = g_list_previous (l))
1185 Event *event = l->data;
1187 if (event->individual == individual)
1189 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1195 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1199 fire_popup_individual_menu (EmpathyRosterView *self,
1204 EmpathyRosterContact *contact;
1205 FolksIndividual *individual;
1207 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1210 contact = EMPATHY_ROSTER_CONTACT (child);
1211 individual = empathy_roster_contact_get_individual (contact);
1213 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1214 individual, button, time);
1218 empathy_roster_view_button_press_event (GtkWidget *widget,
1219 GdkEventButton *event)
1221 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1222 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1223 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1225 if (event->button == 3)
1229 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1233 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1235 fire_popup_individual_menu (self, child, event->button, event->time);
1239 return chain_up (widget, event);
1243 empathy_roster_view_key_press_event (GtkWidget *widget,
1246 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1247 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1248 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1250 if (event->keyval == GDK_KEY_Menu)
1254 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1257 fire_popup_individual_menu (self, child, 0, event->time);
1260 return chain_up (widget, event);
1264 * @out_child: (out) (allow-none)
1267 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1269 GtkWidget **out_child)
1273 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1275 if (out_child != NULL)
1278 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1281 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1285 * @out_child: (out) (allow-none)
1288 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1293 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1295 if (EMPATHY_IS_ROSTER_CONTACT (child))
1296 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1297 else if (EMPATHY_IS_ROSTER_GROUP (child))
1298 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1304 empathy_roster_view_query_tooltip (GtkWidget *widget,
1307 gboolean keyboard_mode,
1308 GtkTooltip *tooltip)
1310 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1311 FolksIndividual *individual;
1315 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1316 if (individual == NULL)
1319 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1320 individual, keyboard_mode, tooltip, &result);
1324 GtkAllocation allocation;
1326 gtk_widget_get_allocation (child, &allocation);
1327 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1334 empathy_roster_view_remove (GtkContainer *container,
1337 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1338 void (*chain_up) (GtkContainer *, GtkWidget *) =
1339 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1341 chain_up (container, widget);
1343 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1344 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1348 empathy_roster_view_class_init (
1349 EmpathyRosterViewClass *klass)
1351 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1352 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1353 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1354 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1357 oclass->get_property = empathy_roster_view_get_property;
1358 oclass->set_property = empathy_roster_view_set_property;
1359 oclass->constructed = empathy_roster_view_constructed;
1360 oclass->dispose = empathy_roster_view_dispose;
1361 oclass->finalize = empathy_roster_view_finalize;
1363 widget_class->button_press_event = empathy_roster_view_button_press_event;
1364 widget_class->key_press_event = empathy_roster_view_key_press_event;
1365 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1367 container_class->remove = empathy_roster_view_remove;
1369 box_class->child_activated = empathy_roster_view_child_activated;
1371 spec = g_param_spec_object ("manager", "Manager",
1372 "EmpathyIndividualManager",
1373 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1374 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1375 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1377 spec = g_param_spec_object ("model", "Model",
1378 "EmpathyRosterModel",
1379 EMPATHY_TYPE_ROSTER_MODEL,
1380 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1381 g_object_class_install_property (oclass, PROP_MODEL, spec);
1383 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1384 "Show offline contacts",
1386 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1387 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1389 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1392 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1393 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1395 spec = g_param_spec_boolean ("empty", "Empty",
1396 "Is the view currently empty?",
1398 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1399 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1401 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1402 G_OBJECT_CLASS_TYPE (klass),
1404 0, NULL, NULL, NULL,
1406 1, FOLKS_TYPE_INDIVIDUAL);
1408 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1409 G_OBJECT_CLASS_TYPE (klass),
1411 0, NULL, NULL, NULL,
1413 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1415 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1416 G_OBJECT_CLASS_TYPE (klass),
1418 0, NULL, NULL, NULL,
1420 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1422 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1423 G_OBJECT_CLASS_TYPE (klass),
1425 0, g_signal_accumulator_true_handled, NULL, NULL,
1427 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1429 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1433 empathy_roster_view_init (EmpathyRosterView *self)
1435 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1436 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1438 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1439 NULL, (GDestroyNotify) g_hash_table_unref);
1440 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1442 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1444 self->priv->events = g_queue_new ();
1446 self->priv->empty = TRUE;
1450 empathy_roster_view_new (EmpathyIndividualManager *manager,
1451 EmpathyRosterModel *model)
1453 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1454 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1456 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1462 EmpathyIndividualManager *
1463 empathy_roster_view_get_manager (EmpathyRosterView *self)
1465 return self->priv->manager;
1469 empathy_roster_view_show_offline (EmpathyRosterView *self,
1472 if (self->priv->show_offline == show)
1475 self->priv->show_offline = show;
1476 egg_list_box_refilter (EGG_LIST_BOX (self));
1478 g_object_notify (G_OBJECT (self), "show-offline");
1482 clear_view (EmpathyRosterView *self)
1484 gtk_container_foreach (GTK_CONTAINER (self),
1485 (GtkCallback) gtk_widget_destroy, NULL);
1487 g_hash_table_remove_all (self->priv->roster_contacts);
1488 g_hash_table_remove_all (self->priv->roster_groups);
1489 g_hash_table_remove_all (self->priv->displayed_contacts);
1493 empathy_roster_view_show_groups (EmpathyRosterView *self,
1496 if (self->priv->show_groups == show)
1499 self->priv->show_groups = show;
1501 /* TODO: block sort/filter? */
1503 populate_view (self);
1505 g_object_notify (G_OBJECT (self), "show-groups");
1509 select_first_contact (EmpathyRosterView *self)
1511 GList *children, *l;
1513 children = gtk_container_get_children (GTK_CONTAINER (self));
1514 for (l = children; l != NULL; l = g_list_next (l))
1516 GtkWidget *child = l->data;
1518 if (!gtk_widget_get_child_visible (child))
1521 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1524 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1528 g_list_free (children);
1532 search_text_notify_cb (EmpathyLiveSearch *search,
1534 EmpathyRosterView *self)
1536 egg_list_box_refilter (EGG_LIST_BOX (self));
1538 select_first_contact (self);
1542 search_activate_cb (GtkWidget *search,
1543 EmpathyRosterView *self)
1545 EggListBox *box = EGG_LIST_BOX (self);
1548 child = egg_list_box_get_selected_child (box);
1552 empathy_roster_view_child_activated (box, child);
1556 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1557 EmpathyLiveSearch *search)
1559 if (self->priv->search != NULL)
1561 g_signal_handlers_disconnect_by_func (self->priv->search,
1562 search_text_notify_cb, self);
1563 g_signal_handlers_disconnect_by_func (self->priv->search,
1564 search_activate_cb, self);
1566 g_clear_object (&self->priv->search);
1572 self->priv->search = g_object_ref (search);
1574 g_signal_connect (self->priv->search, "notify::text",
1575 G_CALLBACK (search_text_notify_cb), self);
1576 g_signal_connect (self->priv->search, "activate",
1577 G_CALLBACK (search_activate_cb), self);
1581 empathy_roster_view_is_empty (EmpathyRosterView *self)
1583 return self->priv->empty;
1587 empathy_roster_view_is_searching (EmpathyRosterView *self)
1589 return (self->priv->search != NULL &&
1590 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1593 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1594 * Empathy's internals. */
1596 empathy_roster_view_add_event (EmpathyRosterView *self,
1597 FolksIndividual *individual,
1601 GHashTable *contacts;
1603 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1604 if (contacts == NULL)
1607 self->priv->last_event_id++;
1609 g_queue_push_head (self->priv->events,
1610 event_new (self->priv->last_event_id, individual, icon, user_data));
1612 start_flashing (self);
1614 return self->priv->last_event_id;
1618 empathy_roster_view_remove_event (EmpathyRosterView *self,
1623 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1624 l = g_list_next (l))
1626 Event *event = l->data;
1628 if (event->id == event_id)
1630 remove_event (self, event);
1637 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1641 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1643 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1646 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));