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 add_roster_contact (EmpathyRosterView *self,
178 FolksIndividual *individual,
183 contact = empathy_roster_contact_new (individual, group);
185 /* Need to refilter if online is changed */
186 g_signal_connect (contact, "notify::online",
187 G_CALLBACK (roster_contact_changed_cb), self);
189 /* Need to resort if alias is changed */
190 g_signal_connect (contact, "notify::alias",
191 G_CALLBACK (roster_contact_changed_cb), self);
193 gtk_widget_show (contact);
194 gtk_container_add (GTK_CONTAINER (self), contact);
200 group_expanded_cb (EmpathyRosterGroup *group,
202 EmpathyRosterView *self)
206 widgets = empathy_roster_group_get_widgets (group);
207 for (l = widgets; l != NULL; l = g_list_next (l))
209 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
212 g_list_free (widgets);
215 static EmpathyRosterGroup *
216 lookup_roster_group (EmpathyRosterView *self,
219 return g_hash_table_lookup (self->priv->roster_groups, group);
222 static EmpathyRosterGroup *
223 ensure_roster_group (EmpathyRosterView *self,
226 GtkWidget *roster_group;
228 roster_group = (GtkWidget *) lookup_roster_group (self, group);
229 if (roster_group != NULL)
230 return EMPATHY_ROSTER_GROUP (roster_group);
232 if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
233 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
234 else if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY))
235 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
237 roster_group = empathy_roster_group_new (group, NULL);
239 g_signal_connect (roster_group, "notify::expanded",
240 G_CALLBACK (group_expanded_cb), self);
242 gtk_widget_show (roster_group);
243 gtk_container_add (GTK_CONTAINER (self), roster_group);
245 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
248 return EMPATHY_ROSTER_GROUP (roster_group);
252 update_group_widgets (EmpathyRosterView *self,
253 EmpathyRosterGroup *group,
254 EmpathyRosterContact *contact,
257 guint old_count, count;
259 old_count = empathy_roster_group_get_widgets_count (group);
262 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
264 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
266 if (count != old_count)
267 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
271 add_to_group (EmpathyRosterView *self,
272 FolksIndividual *individual,
276 GHashTable *contacts;
277 EmpathyRosterGroup *roster_group = NULL;
279 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
280 if (contacts == NULL)
283 if (tp_strdiff (group, NO_GROUP))
284 roster_group = ensure_roster_group (self, group);
286 contact = add_roster_contact (self, individual, group);
287 g_hash_table_insert (contacts, g_strdup (group), contact);
289 if (roster_group != NULL)
291 update_group_widgets (self, roster_group,
292 EMPATHY_ROSTER_CONTACT (contact), TRUE);
297 individual_added (EmpathyRosterView *self,
298 FolksIndividual *individual)
300 GHashTable *contacts;
302 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
303 if (contacts != NULL)
306 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
308 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
310 if (!self->priv->show_groups)
312 add_to_group (self, individual, NO_GROUP);
319 tops = empathy_roster_model_get_top_individuals (self->priv->model);
321 if (folks_favourite_details_get_is_favourite (
322 FOLKS_FAVOURITE_DETAILS (individual)) ||
323 g_list_index (tops, individual) != -1)
325 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
328 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
331 if (g_list_length (groups) > 0)
333 for (l = groups; l != NULL; l = g_list_next (l))
335 add_to_group (self, individual, l->data);
340 /* No group, adds to Ungrouped */
341 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
344 g_list_free (groups);
349 set_event_icon_on_individual (EmpathyRosterView *self,
350 FolksIndividual *individual,
353 GHashTable *contacts;
357 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
358 if (contacts == NULL)
361 g_hash_table_iter_init (&iter, contacts);
362 while (g_hash_table_iter_next (&iter, NULL, &v))
364 EmpathyRosterContact *contact =v;
366 empathy_roster_contact_set_event_icon (contact, icon);
371 flash_event (Event *event,
372 EmpathyRosterView *self)
374 set_event_icon_on_individual (self, event->individual, event->icon);
378 unflash_event (Event *event,
379 EmpathyRosterView *self)
381 set_event_icon_on_individual (self, event->individual, NULL);
385 flash_cb (gpointer data)
387 EmpathyRosterView *self = data;
389 if (self->priv->display_flash_event)
391 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
392 self->priv->display_flash_event = FALSE;
396 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
397 self->priv->display_flash_event = TRUE;
404 start_flashing (EmpathyRosterView *self)
406 if (self->priv->flash_id != 0)
409 self->priv->display_flash_event = TRUE;
411 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
416 stop_flashing (EmpathyRosterView *self)
418 if (self->priv->flash_id == 0)
421 g_source_remove (self->priv->flash_id);
422 self->priv->flash_id = 0;
426 remove_event (EmpathyRosterView *self,
429 unflash_event (event, self);
430 g_queue_remove (self->priv->events, event);
432 if (g_queue_get_length (self->priv->events) == 0)
434 stop_flashing (self);
439 remove_all_individual_event (EmpathyRosterView *self,
440 FolksIndividual *individual)
444 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
447 Event *event = l->data;
449 if (event->individual == individual)
451 remove_event (self, event);
458 individual_removed (EmpathyRosterView *self,
459 FolksIndividual *individual)
461 GHashTable *contacts;
465 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
466 if (contacts == NULL)
469 remove_all_individual_event (self, individual);
471 g_hash_table_iter_init (&iter, contacts);
472 while (g_hash_table_iter_next (&iter, &key, &value))
474 const gchar *group_name = key;
475 GtkWidget *contact = value;
476 EmpathyRosterGroup *group;
478 group = lookup_roster_group (self, group_name);
481 update_group_widgets (self, group,
482 EMPATHY_ROSTER_CONTACT (contact), FALSE);
485 gtk_container_remove (GTK_CONTAINER (self), contact);
488 g_hash_table_remove (self->priv->roster_contacts, individual);
492 individual_added_cb (EmpathyRosterModel *model,
493 FolksIndividual *individual,
494 EmpathyRosterView *self)
496 individual_added (self, individual);
500 individual_removed_cb (EmpathyRosterModel *model,
501 FolksIndividual *individual,
502 EmpathyRosterView *self)
504 individual_removed (self, individual);
508 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
509 EmpathyRosterContact *b)
511 FolksIndividual *ind_a, *ind_b;
512 const gchar *alias_a, *alias_b;
514 ind_a = empathy_roster_contact_get_individual (a);
515 ind_b = empathy_roster_contact_get_individual (b);
517 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
518 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
520 return g_ascii_strcasecmp (alias_a, alias_b);
524 contact_is_favourite (EmpathyRosterContact *contact)
526 FolksIndividual *individual;
528 individual = empathy_roster_contact_get_individual (contact);
530 return folks_favourite_details_get_is_favourite (
531 FOLKS_FAVOURITE_DETAILS (individual));
535 contact_in_top (EmpathyRosterView *self,
536 EmpathyRosterContact *contact)
538 FolksIndividual *individual;
541 if (contact_is_favourite (contact))
544 individual = empathy_roster_contact_get_individual (contact);
546 tops = empathy_roster_model_get_top_individuals (self->priv->model);
548 if (g_list_index (tops, individual) != -1)
555 compare_roster_contacts_no_group (EmpathyRosterView *self,
556 EmpathyRosterContact *a,
557 EmpathyRosterContact *b)
559 gboolean top_a, top_b;
561 top_a = contact_in_top (self, a);
562 top_b = contact_in_top (self, b);
565 /* Both contacts are in the top of the roster (or not). Sort them
567 return compare_roster_contacts_by_alias (a, b);
575 compare_group_names (const gchar *group_a,
576 const gchar *group_b)
578 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
581 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
584 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
586 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
589 return g_ascii_strcasecmp (group_a, group_b);
593 compare_roster_contacts_with_groups (EmpathyRosterView *self,
594 EmpathyRosterContact *a,
595 EmpathyRosterContact *b)
597 const gchar *group_a, *group_b;
599 group_a = empathy_roster_contact_get_group (a);
600 group_b = empathy_roster_contact_get_group (b);
602 if (!tp_strdiff (group_a, group_b))
603 /* Same group, compare the contacts */
604 return compare_roster_contacts_by_alias (a, b);
607 return compare_group_names (group_a, group_b);
611 compare_roster_contacts (EmpathyRosterView *self,
612 EmpathyRosterContact *a,
613 EmpathyRosterContact *b)
615 if (!self->priv->show_groups)
616 return compare_roster_contacts_no_group (self, a, b);
618 return compare_roster_contacts_with_groups (self, a, b);
622 compare_roster_groups (EmpathyRosterGroup *a,
623 EmpathyRosterGroup *b)
625 const gchar *name_a, *name_b;
627 name_a = empathy_roster_group_get_name (a);
628 name_b = empathy_roster_group_get_name (b);
630 return compare_group_names (name_a, name_b);
634 compare_contact_group (EmpathyRosterContact *contact,
635 EmpathyRosterGroup *group)
637 const char *contact_group, *group_name;
639 contact_group = empathy_roster_contact_get_group (contact);
640 group_name = empathy_roster_group_get_name (group);
642 if (!tp_strdiff (contact_group, group_name))
643 /* @contact is in @group, @group has to be displayed first */
646 /* @contact is in a different group, sort by group name */
647 return compare_group_names (contact_group, group_name);
651 roster_view_sort (gconstpointer a,
655 EmpathyRosterView *self = user_data;
657 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
658 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
659 EMPATHY_ROSTER_CONTACT (b));
660 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
661 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
662 EMPATHY_ROSTER_GROUP (b));
663 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
664 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
665 EMPATHY_ROSTER_GROUP (b));
666 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
667 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
668 EMPATHY_ROSTER_GROUP (a));
670 g_return_val_if_reached (0);
674 update_separator (GtkWidget **separator,
681 /* No separator before the first row */
682 g_clear_object (separator);
686 if (*separator != NULL)
689 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
690 g_object_ref_sink (*separator);
694 is_searching (EmpathyRosterView *self)
696 if (self->priv->search == NULL)
699 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
703 update_empty (EmpathyRosterView *self,
706 if (self->priv->empty == empty)
709 self->priv->empty = empty;
710 g_object_notify (G_OBJECT (self), "empty");
714 add_to_displayed (EmpathyRosterView *self,
715 EmpathyRosterContact *contact)
717 FolksIndividual *individual;
718 GHashTable *contacts;
722 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
725 g_hash_table_add (self->priv->displayed_contacts, contact);
726 update_empty (self, FALSE);
728 /* Groups of this contact may now be displayed if we just displays the first
729 * child in this group. */
731 if (!self->priv->show_groups)
734 individual = empathy_roster_contact_get_individual (contact);
735 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
736 if (contacts == NULL)
739 g_hash_table_iter_init (&iter, contacts);
740 while (g_hash_table_iter_next (&iter, &k, NULL))
742 const gchar *group_name = k;
745 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
749 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
754 remove_from_displayed (EmpathyRosterView *self,
755 EmpathyRosterContact *contact)
757 g_hash_table_remove (self->priv->displayed_contacts, contact);
759 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
760 update_empty (self, TRUE);
764 * check if @contact should be displayed according to @self's current status
765 * and without consideration for the state of @contact's groups.
768 contact_should_be_displayed (EmpathyRosterView *self,
769 EmpathyRosterContact *contact)
771 if (is_searching (self))
773 FolksIndividual *individual;
775 individual = empathy_roster_contact_get_individual (contact);
777 return empathy_individual_match_string (individual,
778 empathy_live_search_get_text (self->priv->search),
779 empathy_live_search_get_words (self->priv->search));
782 if (self->priv->show_offline)
785 if (contact_is_favourite (contact))
787 const gchar *group_name;
789 if (!self->priv->show_groups)
790 /* Always display favourite contacts in non-group mode. */
793 group_name = empathy_roster_contact_get_group (contact);
795 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
796 /* Always display favourite contact in group mode only in the
801 return empathy_roster_contact_is_online (contact);
806 filter_contact (EmpathyRosterView *self,
807 EmpathyRosterContact *contact)
811 displayed = contact_should_be_displayed (self, contact);
813 if (self->priv->show_groups)
815 const gchar *group_name;
816 EmpathyRosterGroup *group;
818 group_name = empathy_roster_contact_get_group (contact);
819 group = lookup_roster_group (self, group_name);
823 /* When searching, always display even if the group is closed */
824 if (!is_searching (self) &&
825 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
832 add_to_displayed (self, contact);
836 remove_from_displayed (self, contact);
843 filter_group (EmpathyRosterView *self,
844 EmpathyRosterGroup *group)
848 /* Display the group if it contains at least one displayed contact */
849 widgets = empathy_roster_group_get_widgets (group);
850 for (l = widgets; l != NULL; l = g_list_next (l))
852 EmpathyRosterContact *contact = l->data;
854 if (contact_should_be_displayed (self, contact))
862 filter_list (GtkWidget *child,
865 EmpathyRosterView *self = user_data;
867 if (EMPATHY_IS_ROSTER_CONTACT (child))
868 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
870 else if (EMPATHY_IS_ROSTER_GROUP (child))
871 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
873 g_return_val_if_reached (FALSE);
876 /* @list: GList of EmpathyRosterContact
878 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
881 individual_in_list (FolksIndividual *individual,
886 for (l = list; l != NULL; l = g_list_next (l))
888 EmpathyRosterContact *contact = l->data;
890 if (empathy_roster_contact_get_individual (contact) == individual)
898 populate_view (EmpathyRosterView *self)
900 GList *individuals, *l;
902 individuals = empathy_roster_model_get_individuals (self->priv->model);
903 for (l = individuals; l != NULL; l = g_list_next (l))
905 FolksIndividual *individual = l->data;
907 individual_added (self, individual);
910 g_list_free (individuals);
914 remove_from_group (EmpathyRosterView *self,
915 FolksIndividual *individual,
918 GHashTable *contacts;
920 EmpathyRosterGroup *roster_group;
922 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
923 if (contacts == NULL)
926 contact = g_hash_table_lookup (contacts, group);
930 g_hash_table_remove (contacts, group);
932 if (g_hash_table_size (contacts) == 0)
934 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
937 roster_group = lookup_roster_group (self, group);
939 if (roster_group != NULL)
941 update_group_widgets (self, roster_group,
942 EMPATHY_ROSTER_CONTACT (contact), FALSE);
945 gtk_container_remove (GTK_CONTAINER (self), contact);
949 update_top_contacts (EmpathyRosterView *self)
952 GList *to_add = NULL, *to_remove = NULL;
953 EmpathyRosterGroup *group;
955 if (!self->priv->show_groups)
957 egg_list_box_resort (EGG_LIST_BOX (self));
961 tops = empathy_roster_model_get_top_individuals (self->priv->model);
963 group = g_hash_table_lookup (self->priv->roster_groups,
964 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
967 to_add = g_list_copy (tops);
973 contacts = empathy_roster_group_get_widgets (group);
975 /* Check which EmpathyRosterContact have to be removed */
976 for (l = contacts; l != NULL; l = g_list_next (l))
978 EmpathyRosterContact *contact = l->data;
979 FolksIndividual *individual;
981 if (contact_is_favourite (contact))
984 individual = empathy_roster_contact_get_individual (contact);
986 if (g_list_find (tops, individual) == NULL)
987 to_remove = g_list_prepend (to_remove, individual);
990 /* Check which EmpathyRosterContact have to be added */
991 for (l = tops; l != NULL; l = g_list_next (l))
993 FolksIndividual *individual = l->data;
995 if (!individual_in_list (individual, contacts))
996 to_add = g_list_prepend (to_add, individual);
1000 for (l = to_add; l != NULL; l = g_list_next (l))
1001 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1003 for (l = to_remove; l != NULL; l = g_list_next (l))
1004 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1006 g_list_free (to_add);
1007 g_list_free (to_remove);
1011 groups_changed_cb (EmpathyRosterModel *model,
1012 FolksIndividual *individual,
1015 EmpathyRosterView *self)
1017 if (!self->priv->show_groups)
1022 add_to_group (self, individual, group);
1026 remove_from_group (self, individual, group);
1031 top_individuals_changed_cb (EmpathyRosterModel *model,
1032 EmpathyRosterView *self)
1034 update_top_contacts (self);
1038 favourites_changed_cb (EmpathyIndividualManager *manager,
1039 FolksIndividual *individual,
1041 EmpathyRosterView *self)
1043 GHashTable *contacts;
1045 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1046 if (contacts == NULL)
1049 if (self->priv->show_groups)
1052 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1054 remove_from_group (self, individual,
1055 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1061 contact = g_hash_table_lookup (contacts, NO_GROUP);
1062 if (contact == NULL)
1065 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1070 empathy_roster_view_constructed (GObject *object)
1072 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1073 void (*chain_up) (GObject *) =
1074 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1076 if (chain_up != NULL)
1079 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1080 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1082 populate_view (self);
1084 tp_g_signal_connect_object (self->priv->model, "individual-added",
1085 G_CALLBACK (individual_added_cb), self, 0);
1086 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1087 G_CALLBACK (individual_removed_cb), self, 0);
1088 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1089 G_CALLBACK (groups_changed_cb), self, 0);
1090 tp_g_signal_connect_object (self->priv->model, "top-individuals-changed",
1091 G_CALLBACK (top_individuals_changed_cb), self, 0);
1092 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1093 G_CALLBACK (favourites_changed_cb), self, 0);
1095 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1096 roster_view_sort, self, NULL);
1098 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1101 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1103 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1107 empathy_roster_view_dispose (GObject *object)
1109 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1110 void (*chain_up) (GObject *) =
1111 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1113 stop_flashing (self);
1115 empathy_roster_view_set_live_search (self, NULL);
1116 g_clear_object (&self->priv->manager);
1117 g_clear_object (&self->priv->model);
1119 if (chain_up != NULL)
1124 empathy_roster_view_finalize (GObject *object)
1126 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1127 void (*chain_up) (GObject *) =
1128 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1130 g_hash_table_unref (self->priv->roster_contacts);
1131 g_hash_table_unref (self->priv->roster_groups);
1132 g_hash_table_unref (self->priv->displayed_contacts);
1133 g_queue_free_full (self->priv->events, event_free);
1135 if (chain_up != NULL)
1140 empathy_roster_view_child_activated (EggListBox *box,
1143 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1144 EmpathyRosterContact *contact;
1145 FolksIndividual *individual;
1148 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1151 contact = EMPATHY_ROSTER_CONTACT (child);
1152 individual = empathy_roster_contact_get_individual (contact);
1154 /* Activate the oldest event associated with this contact, if any */
1155 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1156 l = g_list_previous (l))
1158 Event *event = l->data;
1160 if (event->individual == individual)
1162 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1168 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1172 fire_popup_individual_menu (EmpathyRosterView *self,
1177 EmpathyRosterContact *contact;
1178 FolksIndividual *individual;
1180 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1183 contact = EMPATHY_ROSTER_CONTACT (child);
1184 individual = empathy_roster_contact_get_individual (contact);
1186 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1187 individual, button, time);
1191 empathy_roster_view_button_press_event (GtkWidget *widget,
1192 GdkEventButton *event)
1194 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1195 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1196 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1198 if (event->button == 3)
1202 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1206 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1208 fire_popup_individual_menu (self, child, event->button, event->time);
1212 return chain_up (widget, event);
1216 empathy_roster_view_key_press_event (GtkWidget *widget,
1219 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1220 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1221 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1223 if (event->keyval == GDK_KEY_Menu)
1227 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1230 fire_popup_individual_menu (self, child, 0, event->time);
1233 return chain_up (widget, event);
1237 * @out_child: (out) (allow-none)
1240 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1242 GtkWidget **out_child)
1246 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1248 if (out_child != NULL)
1251 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1254 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1258 * @out_child: (out) (allow-none)
1261 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1266 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1268 if (EMPATHY_IS_ROSTER_CONTACT (child))
1269 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1270 else if (EMPATHY_IS_ROSTER_GROUP (child))
1271 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1277 empathy_roster_view_query_tooltip (GtkWidget *widget,
1280 gboolean keyboard_mode,
1281 GtkTooltip *tooltip)
1283 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1284 FolksIndividual *individual;
1288 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1289 if (individual == NULL)
1292 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1293 individual, keyboard_mode, tooltip, &result);
1297 GtkAllocation allocation;
1299 gtk_widget_get_allocation (child, &allocation);
1300 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1307 empathy_roster_view_remove (GtkContainer *container,
1310 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1311 void (*chain_up) (GtkContainer *, GtkWidget *) =
1312 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1314 chain_up (container, widget);
1316 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1317 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1321 empathy_roster_view_class_init (
1322 EmpathyRosterViewClass *klass)
1324 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1325 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1326 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1327 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1330 oclass->get_property = empathy_roster_view_get_property;
1331 oclass->set_property = empathy_roster_view_set_property;
1332 oclass->constructed = empathy_roster_view_constructed;
1333 oclass->dispose = empathy_roster_view_dispose;
1334 oclass->finalize = empathy_roster_view_finalize;
1336 widget_class->button_press_event = empathy_roster_view_button_press_event;
1337 widget_class->key_press_event = empathy_roster_view_key_press_event;
1338 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1340 container_class->remove = empathy_roster_view_remove;
1342 box_class->child_activated = empathy_roster_view_child_activated;
1344 spec = g_param_spec_object ("manager", "Manager",
1345 "EmpathyIndividualManager",
1346 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1347 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1348 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1350 spec = g_param_spec_object ("model", "Model",
1351 "EmpathyRosterModel",
1352 EMPATHY_TYPE_ROSTER_MODEL,
1353 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1354 g_object_class_install_property (oclass, PROP_MODEL, spec);
1356 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1357 "Show offline contacts",
1359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1360 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1362 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1366 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1368 spec = g_param_spec_boolean ("empty", "Empty",
1369 "Is the view currently empty?",
1371 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1372 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1374 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1375 G_OBJECT_CLASS_TYPE (klass),
1377 0, NULL, NULL, NULL,
1379 1, FOLKS_TYPE_INDIVIDUAL);
1381 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1382 G_OBJECT_CLASS_TYPE (klass),
1384 0, NULL, NULL, NULL,
1386 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1388 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1389 G_OBJECT_CLASS_TYPE (klass),
1391 0, NULL, NULL, NULL,
1393 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1395 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1396 G_OBJECT_CLASS_TYPE (klass),
1398 0, g_signal_accumulator_true_handled, NULL, NULL,
1400 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1402 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1406 empathy_roster_view_init (EmpathyRosterView *self)
1408 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1409 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1411 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1412 NULL, (GDestroyNotify) g_hash_table_unref);
1413 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1415 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1417 self->priv->events = g_queue_new ();
1419 self->priv->empty = TRUE;
1423 empathy_roster_view_new (EmpathyIndividualManager *manager,
1424 EmpathyRosterModel *model)
1426 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1427 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1429 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1435 EmpathyIndividualManager *
1436 empathy_roster_view_get_manager (EmpathyRosterView *self)
1438 return self->priv->manager;
1442 empathy_roster_view_show_offline (EmpathyRosterView *self,
1445 if (self->priv->show_offline == show)
1448 self->priv->show_offline = show;
1449 egg_list_box_refilter (EGG_LIST_BOX (self));
1451 g_object_notify (G_OBJECT (self), "show-offline");
1455 clear_view (EmpathyRosterView *self)
1457 gtk_container_foreach (GTK_CONTAINER (self),
1458 (GtkCallback) gtk_widget_destroy, NULL);
1460 g_hash_table_remove_all (self->priv->roster_contacts);
1461 g_hash_table_remove_all (self->priv->roster_groups);
1462 g_hash_table_remove_all (self->priv->displayed_contacts);
1466 empathy_roster_view_show_groups (EmpathyRosterView *self,
1469 if (self->priv->show_groups == show)
1472 self->priv->show_groups = show;
1474 /* TODO: block sort/filter? */
1476 populate_view (self);
1478 g_object_notify (G_OBJECT (self), "show-groups");
1482 select_first_contact (EmpathyRosterView *self)
1484 GList *children, *l;
1486 children = gtk_container_get_children (GTK_CONTAINER (self));
1487 for (l = children; l != NULL; l = g_list_next (l))
1489 GtkWidget *child = l->data;
1491 if (!gtk_widget_get_child_visible (child))
1494 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1497 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1501 g_list_free (children);
1505 search_text_notify_cb (EmpathyLiveSearch *search,
1507 EmpathyRosterView *self)
1509 egg_list_box_refilter (EGG_LIST_BOX (self));
1511 select_first_contact (self);
1515 search_activate_cb (GtkWidget *search,
1516 EmpathyRosterView *self)
1518 EggListBox *box = EGG_LIST_BOX (self);
1521 child = egg_list_box_get_selected_child (box);
1525 empathy_roster_view_child_activated (box, child);
1529 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1530 EmpathyLiveSearch *search)
1532 if (self->priv->search != NULL)
1534 g_signal_handlers_disconnect_by_func (self->priv->search,
1535 search_text_notify_cb, self);
1536 g_signal_handlers_disconnect_by_func (self->priv->search,
1537 search_activate_cb, self);
1539 g_clear_object (&self->priv->search);
1545 self->priv->search = g_object_ref (search);
1547 g_signal_connect (self->priv->search, "notify::text",
1548 G_CALLBACK (search_text_notify_cb), self);
1549 g_signal_connect (self->priv->search, "activate",
1550 G_CALLBACK (search_activate_cb), self);
1554 empathy_roster_view_is_empty (EmpathyRosterView *self)
1556 return self->priv->empty;
1560 empathy_roster_view_is_searching (EmpathyRosterView *self)
1562 return (self->priv->search != NULL &&
1563 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1566 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1567 * Empathy's internals. */
1569 empathy_roster_view_add_event (EmpathyRosterView *self,
1570 FolksIndividual *individual,
1574 GHashTable *contacts;
1576 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1577 if (contacts == NULL)
1580 self->priv->last_event_id++;
1582 g_queue_push_head (self->priv->events,
1583 event_new (self->priv->last_event_id, individual, icon, user_data));
1585 start_flashing (self);
1587 return self->priv->last_event_id;
1591 empathy_roster_view_remove_event (EmpathyRosterView *self,
1596 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1597 l = g_list_next (l))
1599 Event *event = l->data;
1601 if (event->id == event_id)
1603 remove_event (self, event);
1610 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1614 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1616 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1619 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));