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 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
27 SIG_INDIVIDUAL_ACTIVATED,
28 SIG_POPUP_INDIVIDUAL_MENU,
30 SIG_INDIVIDUAL_TOOLTIP,
34 static guint signals[LAST_SIGNAL];
36 #define NO_GROUP "X-no-group"
37 #define UNGROUPED _("Ungrouped")
38 #define TOP_GROUP _("Top Contacts")
40 struct _EmpathyRosterViewPriv
42 EmpathyIndividualManager *manager;
44 /* FolksIndividual (borrowed) -> GHashTable (
45 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
47 * When not using groups, this hash just have one element mapped
48 * from the special NO_GROUP key. We could use it as a set but
49 * I prefer to stay coherent in the way this hash is managed.
51 GHashTable *roster_contacts;
52 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
53 GHashTable *roster_groups;
54 /* Hash of the EmpathyRosterContact currently displayed */
55 GHashTable *displayed_contacts;
58 /* queue of (Event *). The most recent events are in the head of the queue
59 * so we always display the icon of the oldest one. */
62 gboolean display_flash_event;
64 gboolean show_offline;
68 EmpathyLiveSearch *search;
74 FolksIndividual *individual;
81 FolksIndividual *individual,
85 Event *event = g_slice_new (Event);
88 event->individual = g_object_ref (individual);
89 event->icon = g_strdup (icon);
90 event->user_data = user_data;
95 event_free (gpointer data)
98 g_object_unref (event->individual);
101 g_slice_free (Event, event);
105 empathy_roster_view_get_property (GObject *object,
110 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
115 g_value_set_object (value, self->priv->manager);
117 case PROP_SHOW_OFFLINE:
118 g_value_set_boolean (value, self->priv->show_offline);
120 case PROP_SHOW_GROUPS:
121 g_value_set_boolean (value, self->priv->show_groups);
124 g_value_set_boolean (value, self->priv->empty);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 empathy_roster_view_set_property (GObject *object,
138 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
143 g_assert (self->priv->manager == NULL); /* construct only */
144 self->priv->manager = g_value_dup_object (value);
146 case PROP_SHOW_OFFLINE:
147 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
149 case PROP_SHOW_GROUPS:
150 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 roster_contact_changed_cb (GtkWidget *child,
161 EmpathyRosterView *self)
163 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
167 add_roster_contact (EmpathyRosterView *self,
168 FolksIndividual *individual,
173 contact = empathy_roster_contact_new (individual, group);
175 /* Need to refilter if online is changed */
176 g_signal_connect (contact, "notify::online",
177 G_CALLBACK (roster_contact_changed_cb), self);
179 /* Need to resort if alias is changed */
180 g_signal_connect (contact, "notify::alias",
181 G_CALLBACK (roster_contact_changed_cb), self);
183 gtk_widget_show (contact);
184 gtk_container_add (GTK_CONTAINER (self), contact);
190 group_expanded_cb (EmpathyRosterGroup *group,
192 EmpathyRosterView *self)
196 widgets = empathy_roster_group_get_widgets (group);
197 for (l = widgets; l != NULL; l = g_list_next (l))
199 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
202 g_list_free (widgets);
205 static EmpathyRosterGroup *
206 lookup_roster_group (EmpathyRosterView *self,
209 return g_hash_table_lookup (self->priv->roster_groups, group);
212 static EmpathyRosterGroup *
213 ensure_roster_group (EmpathyRosterView *self,
216 GtkWidget *roster_group;
218 roster_group = (GtkWidget *) lookup_roster_group (self, group);
219 if (roster_group != NULL)
220 return EMPATHY_ROSTER_GROUP (roster_group);
222 if (!tp_strdiff (group, TOP_GROUP))
223 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
225 roster_group = empathy_roster_group_new (group, NULL);
227 g_signal_connect (roster_group, "notify::expanded",
228 G_CALLBACK (group_expanded_cb), self);
230 gtk_widget_show (roster_group);
231 gtk_container_add (GTK_CONTAINER (self), roster_group);
233 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
236 return EMPATHY_ROSTER_GROUP (roster_group);
240 update_group_widgets (EmpathyRosterView *self,
241 EmpathyRosterGroup *group,
242 EmpathyRosterContact *contact,
245 guint old_count, count;
247 old_count = empathy_roster_group_get_widgets_count (group);
250 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
252 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
254 if (count != old_count)
255 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
259 add_to_group (EmpathyRosterView *self,
260 FolksIndividual *individual,
264 GHashTable *contacts;
265 EmpathyRosterGroup *roster_group = NULL;
267 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
268 if (contacts == NULL)
271 if (tp_strdiff (group, NO_GROUP))
272 roster_group = ensure_roster_group (self, group);
274 contact = add_roster_contact (self, individual, group);
275 g_hash_table_insert (contacts, g_strdup (group), contact);
277 if (roster_group != NULL)
279 update_group_widgets (self, roster_group,
280 EMPATHY_ROSTER_CONTACT (contact), TRUE);
285 individual_added (EmpathyRosterView *self,
286 FolksIndividual *individual)
288 GHashTable *contacts;
290 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
291 if (contacts != NULL)
294 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
296 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
298 if (!self->priv->show_groups)
300 add_to_group (self, individual, NO_GROUP);
307 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
309 if (folks_favourite_details_get_is_favourite (
310 FOLKS_FAVOURITE_DETAILS (individual)) ||
311 g_list_index (tops, individual) != -1)
313 add_to_group (self, individual, TOP_GROUP);
316 groups = folks_group_details_get_groups (
317 FOLKS_GROUP_DETAILS (individual));
319 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
321 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
323 while (iter != NULL && gee_iterator_next (iter))
325 gchar *group = gee_iterator_get (iter);
327 add_to_group (self, individual, group);
332 g_clear_object (&iter);
336 /* No group, adds to Ungrouped */
337 add_to_group (self, individual, UNGROUPED);
343 set_event_icon_on_individual (EmpathyRosterView *self,
344 FolksIndividual *individual,
347 GHashTable *contacts;
351 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
352 if (contacts == NULL)
355 g_hash_table_iter_init (&iter, contacts);
356 while (g_hash_table_iter_next (&iter, NULL, &v))
358 EmpathyRosterContact *contact =v;
360 empathy_roster_contact_set_event_icon (contact, icon);
365 flash_event (Event *event,
366 EmpathyRosterView *self)
368 set_event_icon_on_individual (self, event->individual, event->icon);
372 unflash_event (Event *event,
373 EmpathyRosterView *self)
375 set_event_icon_on_individual (self, event->individual, NULL);
379 flash_cb (gpointer data)
381 EmpathyRosterView *self = data;
383 if (self->priv->display_flash_event)
385 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
386 self->priv->display_flash_event = FALSE;
390 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
391 self->priv->display_flash_event = TRUE;
398 start_flashing (EmpathyRosterView *self)
400 if (self->priv->flash_id != 0)
403 self->priv->display_flash_event = TRUE;
405 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
410 stop_flashing (EmpathyRosterView *self)
412 if (self->priv->flash_id == 0)
415 g_source_remove (self->priv->flash_id);
416 self->priv->flash_id = 0;
420 remove_event (EmpathyRosterView *self,
423 unflash_event (event, self);
424 g_queue_remove (self->priv->events, event);
426 if (g_queue_get_length (self->priv->events) == 0)
428 stop_flashing (self);
433 remove_all_individual_event (EmpathyRosterView *self,
434 FolksIndividual *individual)
438 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
441 Event *event = l->data;
443 if (event->individual == individual)
445 remove_event (self, event);
452 individual_removed (EmpathyRosterView *self,
453 FolksIndividual *individual)
455 GHashTable *contacts;
459 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
460 if (contacts == NULL)
463 remove_all_individual_event (self, individual);
465 g_hash_table_iter_init (&iter, contacts);
466 while (g_hash_table_iter_next (&iter, &key, &value))
468 const gchar *group_name = key;
469 GtkWidget *contact = value;
470 EmpathyRosterGroup *group;
472 group = lookup_roster_group (self, group_name);
475 update_group_widgets (self, group,
476 EMPATHY_ROSTER_CONTACT (contact), FALSE);
479 gtk_container_remove (GTK_CONTAINER (self), contact);
482 g_hash_table_remove (self->priv->roster_contacts, individual);
486 members_changed_cb (EmpathyIndividualManager *manager,
487 const gchar *message,
490 TpChannelGroupChangeReason reason,
491 EmpathyRosterView *self)
495 for (l = added; l != NULL; l = g_list_next (l))
497 FolksIndividual *individual = l->data;
499 individual_added (self, individual);
502 for (l = removed; l != NULL; l = g_list_next (l))
504 FolksIndividual *individual = l->data;
506 individual_removed (self, individual);
511 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
512 EmpathyRosterContact *b)
514 FolksIndividual *ind_a, *ind_b;
515 const gchar *alias_a, *alias_b;
517 ind_a = empathy_roster_contact_get_individual (a);
518 ind_b = empathy_roster_contact_get_individual (b);
520 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
521 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
523 return g_ascii_strcasecmp (alias_a, alias_b);
527 contact_is_favourite (EmpathyRosterContact *contact)
529 FolksIndividual *individual;
531 individual = empathy_roster_contact_get_individual (contact);
533 return folks_favourite_details_get_is_favourite (
534 FOLKS_FAVOURITE_DETAILS (individual));
538 contact_in_top (EmpathyRosterView *self,
539 EmpathyRosterContact *contact)
541 FolksIndividual *individual;
544 if (contact_is_favourite (contact))
547 individual = empathy_roster_contact_get_individual (contact);
549 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
551 if (g_list_index (tops, individual) != -1)
558 compare_roster_contacts_no_group (EmpathyRosterView *self,
559 EmpathyRosterContact *a,
560 EmpathyRosterContact *b)
562 gboolean top_a, top_b;
564 top_a = contact_in_top (self, a);
565 top_b = contact_in_top (self, b);
568 /* Both contacts are in the top of the roster (or not). Sort them
570 return compare_roster_contacts_by_alias (a, b);
578 compare_group_names (const gchar *group_a,
579 const gchar *group_b)
581 if (!tp_strdiff (group_a, TOP_GROUP))
584 if (!tp_strdiff (group_b, TOP_GROUP))
587 return g_ascii_strcasecmp (group_a, group_b);
591 compare_roster_contacts_with_groups (EmpathyRosterView *self,
592 EmpathyRosterContact *a,
593 EmpathyRosterContact *b)
595 const gchar *group_a, *group_b;
597 group_a = empathy_roster_contact_get_group (a);
598 group_b = empathy_roster_contact_get_group (b);
600 if (!tp_strdiff (group_a, group_b))
601 /* Same group, compare the contacts */
602 return compare_roster_contacts_by_alias (a, b);
605 return compare_group_names (group_a, group_b);
609 compare_roster_contacts (EmpathyRosterView *self,
610 EmpathyRosterContact *a,
611 EmpathyRosterContact *b)
613 if (!self->priv->show_groups)
614 return compare_roster_contacts_no_group (self, a, b);
616 return compare_roster_contacts_with_groups (self, a, b);
620 compare_roster_groups (EmpathyRosterGroup *a,
621 EmpathyRosterGroup *b)
623 const gchar *name_a, *name_b;
625 name_a = empathy_roster_group_get_name (a);
626 name_b = empathy_roster_group_get_name (b);
628 return compare_group_names (name_a, name_b);
632 compare_contact_group (EmpathyRosterContact *contact,
633 EmpathyRosterGroup *group)
635 const char *contact_group, *group_name;
637 contact_group = empathy_roster_contact_get_group (contact);
638 group_name = empathy_roster_group_get_name (group);
640 if (!tp_strdiff (contact_group, group_name))
641 /* @contact is in @group, @group has to be displayed first */
644 /* @contact is in a different group, sort by group name */
645 return compare_group_names (contact_group, group_name);
649 roster_view_sort (gconstpointer a,
653 EmpathyRosterView *self = user_data;
655 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
656 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
657 EMPATHY_ROSTER_CONTACT (b));
658 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
659 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
660 EMPATHY_ROSTER_GROUP (b));
661 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
662 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
663 EMPATHY_ROSTER_GROUP (b));
664 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
665 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
666 EMPATHY_ROSTER_GROUP (a));
668 g_return_val_if_reached (0);
672 update_separator (GtkWidget **separator,
679 /* No separator before the first row */
680 g_clear_object (separator);
684 if (*separator != NULL)
687 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
688 g_object_ref_sink (*separator);
692 is_searching (EmpathyRosterView *self)
694 if (self->priv->search == NULL)
697 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
701 update_empty (EmpathyRosterView *self,
704 if (self->priv->empty == empty)
707 self->priv->empty = empty;
708 g_object_notify (G_OBJECT (self), "empty");
712 add_to_displayed (EmpathyRosterView *self,
713 EmpathyRosterContact *contact)
715 FolksIndividual *individual;
716 GHashTable *contacts;
720 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
723 g_hash_table_add (self->priv->displayed_contacts, contact);
724 update_empty (self, FALSE);
726 /* Groups of this contact may now be displayed if we just displays the first
727 * child in this group. */
729 if (!self->priv->show_groups)
732 individual = empathy_roster_contact_get_individual (contact);
733 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
734 if (contacts == NULL)
737 g_hash_table_iter_init (&iter, contacts);
738 while (g_hash_table_iter_next (&iter, &k, NULL))
740 const gchar *group_name = k;
743 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
747 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
752 remove_from_displayed (EmpathyRosterView *self,
753 EmpathyRosterContact *contact)
755 g_hash_table_remove (self->priv->displayed_contacts, contact);
757 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
758 update_empty (self, TRUE);
762 * check if @contact should be displayed according to @self's current status
763 * and without consideration for the state of @contact's groups.
766 contact_should_be_displayed (EmpathyRosterView *self,
767 EmpathyRosterContact *contact)
769 if (is_searching (self))
771 FolksIndividual *individual;
773 individual = empathy_roster_contact_get_individual (contact);
775 return empathy_individual_match_string (individual,
776 empathy_live_search_get_text (self->priv->search),
777 empathy_live_search_get_words (self->priv->search));
781 if (self->priv->show_offline)
785 else if (!self->priv->show_groups &&
786 contact_is_favourite (contact))
788 /* Always display favourite contacts in non-group mode. In the group
789 * mode we'll display only the one in the 'top' section. */
794 return empathy_roster_contact_is_online (contact);
800 filter_contact (EmpathyRosterView *self,
801 EmpathyRosterContact *contact)
805 displayed = contact_should_be_displayed (self, contact);
807 if (self->priv->show_groups)
809 const gchar *group_name;
810 EmpathyRosterGroup *group;
812 group_name = empathy_roster_contact_get_group (contact);
813 group = lookup_roster_group (self, group_name);
815 if (!tp_strdiff (group_name, TOP_GROUP) &&
816 contact_is_favourite (contact))
821 /* When searching, always display even if the group is closed */
822 if (!is_searching (self) &&
823 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
830 add_to_displayed (self, contact);
834 remove_from_displayed (self, contact);
841 filter_group (EmpathyRosterView *self,
842 EmpathyRosterGroup *group)
846 /* Display the group if it contains at least one displayed contact */
847 widgets = empathy_roster_group_get_widgets (group);
848 for (l = widgets; l != NULL; l = g_list_next (l))
850 EmpathyRosterContact *contact = l->data;
852 if (contact_should_be_displayed (self, contact))
860 filter_list (GtkWidget *child,
863 EmpathyRosterView *self = user_data;
865 if (EMPATHY_IS_ROSTER_CONTACT (child))
866 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
868 else if (EMPATHY_IS_ROSTER_GROUP (child))
869 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
871 g_return_val_if_reached (FALSE);
874 /* @list: GList of EmpathyRosterContact
876 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
879 individual_in_list (FolksIndividual *individual,
884 for (l = list; l != NULL; l = g_list_next (l))
886 EmpathyRosterContact *contact = l->data;
888 if (empathy_roster_contact_get_individual (contact) == individual)
896 populate_view (EmpathyRosterView *self)
898 GList *individuals, *l;
900 individuals = empathy_individual_manager_get_members (self->priv->manager);
901 for (l = individuals; l != NULL; l = g_list_next (l))
903 FolksIndividual *individual = l->data;
905 individual_added (self, individual);
908 g_list_free (individuals);
912 remove_from_group (EmpathyRosterView *self,
913 FolksIndividual *individual,
916 GHashTable *contacts;
918 EmpathyRosterGroup *roster_group;
920 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
921 if (contacts == NULL)
924 contact = g_hash_table_lookup (contacts, group);
928 g_hash_table_remove (contacts, group);
930 if (g_hash_table_size (contacts) == 0)
932 add_to_group (self, individual, UNGROUPED);
935 roster_group = lookup_roster_group (self, group);
937 if (roster_group != NULL)
939 update_group_widgets (self, roster_group,
940 EMPATHY_ROSTER_CONTACT (contact), FALSE);
943 gtk_container_remove (GTK_CONTAINER (self), contact);
947 update_top_contacts (EmpathyRosterView *self)
950 GList *to_add = NULL, *to_remove = NULL;
951 EmpathyRosterGroup *group;
953 if (!self->priv->show_groups)
955 egg_list_box_resort (EGG_LIST_BOX (self));
959 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
961 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
964 to_add = g_list_copy (tops);
970 contacts = empathy_roster_group_get_widgets (group);
972 /* Check which EmpathyRosterContact have to be removed */
973 for (l = contacts; l != NULL; l = g_list_next (l))
975 EmpathyRosterContact *contact = l->data;
976 FolksIndividual *individual;
978 if (contact_is_favourite (contact))
981 individual = empathy_roster_contact_get_individual (contact);
983 if (g_list_find (tops, individual) == NULL)
984 to_remove = g_list_prepend (to_remove, individual);
987 /* Check which EmpathyRosterContact have to be added */
988 for (l = tops; l != NULL; l = g_list_next (l))
990 FolksIndividual *individual = l->data;
992 if (!individual_in_list (individual, contacts))
993 to_add = g_list_prepend (to_add, individual);
997 for (l = to_add; l != NULL; l = g_list_next (l))
998 add_to_group (self, l->data, TOP_GROUP);
1000 for (l = to_remove; l != NULL; l = g_list_next (l))
1001 remove_from_group (self, l->data, TOP_GROUP);
1003 g_list_free (to_add);
1004 g_list_free (to_remove);
1008 groups_changed_cb (EmpathyIndividualManager *manager,
1009 FolksIndividual *individual,
1012 EmpathyRosterView *self)
1014 if (!self->priv->show_groups)
1019 add_to_group (self, individual, group);
1023 remove_from_group (self, individual, group);
1028 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1030 EmpathyRosterView *self)
1032 update_top_contacts (self);
1036 favourites_changed_cb (EmpathyIndividualManager *manager,
1037 FolksIndividual *individual,
1039 EmpathyRosterView *self)
1041 GHashTable *contacts;
1043 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1044 if (contacts == NULL)
1047 if (self->priv->show_groups)
1050 add_to_group (self, individual, TOP_GROUP);
1052 remove_from_group (self, individual, TOP_GROUP);
1058 contact = g_hash_table_lookup (contacts, NO_GROUP);
1059 if (contact == NULL)
1062 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1067 empathy_roster_view_constructed (GObject *object)
1069 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1070 void (*chain_up) (GObject *) =
1071 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1073 if (chain_up != NULL)
1076 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1078 populate_view (self);
1080 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1081 G_CALLBACK (members_changed_cb), self, 0);
1082 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1083 G_CALLBACK (groups_changed_cb), self, 0);
1084 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1085 G_CALLBACK (top_individuals_changed_cb), self, 0);
1086 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1087 G_CALLBACK (favourites_changed_cb), self, 0);
1089 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1090 roster_view_sort, self, NULL);
1092 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1095 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1097 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1101 empathy_roster_view_dispose (GObject *object)
1103 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1104 void (*chain_up) (GObject *) =
1105 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1107 stop_flashing (self);
1109 empathy_roster_view_set_live_search (self, NULL);
1110 g_clear_object (&self->priv->manager);
1112 if (chain_up != NULL)
1117 empathy_roster_view_finalize (GObject *object)
1119 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1120 void (*chain_up) (GObject *) =
1121 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1123 g_hash_table_unref (self->priv->roster_contacts);
1124 g_hash_table_unref (self->priv->roster_groups);
1125 g_hash_table_unref (self->priv->displayed_contacts);
1126 g_queue_free_full (self->priv->events, event_free);
1128 if (chain_up != NULL)
1133 empathy_roster_view_child_activated (EggListBox *box,
1136 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1137 EmpathyRosterContact *contact;
1138 FolksIndividual *individual;
1141 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1144 contact = EMPATHY_ROSTER_CONTACT (child);
1145 individual = empathy_roster_contact_get_individual (contact);
1147 /* Activate the oldest event associated with this contact, if any */
1148 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1149 l = g_list_previous (l))
1151 Event *event = l->data;
1153 if (event->individual == individual)
1155 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1161 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1165 fire_popup_individual_menu (EmpathyRosterView *self,
1170 EmpathyRosterContact *contact;
1171 FolksIndividual *individual;
1173 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1176 contact = EMPATHY_ROSTER_CONTACT (child);
1177 individual = empathy_roster_contact_get_individual (contact);
1179 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1180 individual, button, time);
1184 empathy_roster_view_button_press_event (GtkWidget *widget,
1185 GdkEventButton *event)
1187 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1188 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1189 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1191 if (event->button == 3)
1195 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1199 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1201 fire_popup_individual_menu (self, child, event->button, event->time);
1205 return chain_up (widget, event);
1209 empathy_roster_view_key_press_event (GtkWidget *widget,
1212 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1213 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1214 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1216 if (event->keyval == GDK_KEY_Menu)
1220 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1223 fire_popup_individual_menu (self, child, 0, event->time);
1226 return chain_up (widget, event);
1230 empathy_roster_view_query_tooltip (GtkWidget *widget,
1233 gboolean keyboard_mode,
1234 GtkTooltip *tooltip)
1236 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1238 EmpathyRosterContact *contact;
1239 FolksIndividual *individual;
1242 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1243 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1246 contact = EMPATHY_ROSTER_CONTACT (child);
1247 individual = empathy_roster_contact_get_individual (contact);
1249 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1250 individual, keyboard_mode, tooltip, &result);
1254 GtkAllocation allocation;
1256 gtk_widget_get_allocation (child, &allocation);
1257 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1264 empathy_roster_view_remove (GtkContainer *container,
1267 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1268 void (*chain_up) (GtkContainer *, GtkWidget *) =
1269 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1271 chain_up (container, widget);
1273 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1274 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1278 empathy_roster_view_class_init (
1279 EmpathyRosterViewClass *klass)
1281 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1282 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1283 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1284 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1287 oclass->get_property = empathy_roster_view_get_property;
1288 oclass->set_property = empathy_roster_view_set_property;
1289 oclass->constructed = empathy_roster_view_constructed;
1290 oclass->dispose = empathy_roster_view_dispose;
1291 oclass->finalize = empathy_roster_view_finalize;
1293 widget_class->button_press_event = empathy_roster_view_button_press_event;
1294 widget_class->key_press_event = empathy_roster_view_key_press_event;
1295 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1297 container_class->remove = empathy_roster_view_remove;
1299 box_class->child_activated = empathy_roster_view_child_activated;
1301 spec = g_param_spec_object ("manager", "Manager",
1302 "EmpathyIndividualManager",
1303 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1304 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1305 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1307 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1308 "Show offline contacts",
1310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1311 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1313 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1316 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1317 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1319 spec = g_param_spec_boolean ("empty", "Empty",
1320 "Is the view currently empty?",
1322 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1323 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1325 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1326 G_OBJECT_CLASS_TYPE (klass),
1328 0, NULL, NULL, NULL,
1330 1, FOLKS_TYPE_INDIVIDUAL);
1332 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1333 G_OBJECT_CLASS_TYPE (klass),
1335 0, NULL, NULL, NULL,
1337 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1339 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1340 G_OBJECT_CLASS_TYPE (klass),
1342 0, NULL, NULL, NULL,
1344 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1346 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1347 G_OBJECT_CLASS_TYPE (klass),
1349 0, g_signal_accumulator_true_handled, NULL, NULL,
1351 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1353 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1357 empathy_roster_view_init (EmpathyRosterView *self)
1359 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1360 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1362 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1363 NULL, (GDestroyNotify) g_hash_table_unref);
1364 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1366 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1368 self->priv->events = g_queue_new ();
1370 self->priv->empty = TRUE;
1374 empathy_roster_view_new (EmpathyIndividualManager *manager)
1376 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1378 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1383 EmpathyIndividualManager *
1384 empathy_roster_view_get_manager (EmpathyRosterView *self)
1386 return self->priv->manager;
1390 empathy_roster_view_show_offline (EmpathyRosterView *self,
1393 if (self->priv->show_offline == show)
1396 self->priv->show_offline = show;
1397 egg_list_box_refilter (EGG_LIST_BOX (self));
1399 g_object_notify (G_OBJECT (self), "show-offline");
1403 clear_view (EmpathyRosterView *self)
1405 gtk_container_foreach (GTK_CONTAINER (self),
1406 (GtkCallback) gtk_widget_destroy, NULL);
1408 g_hash_table_remove_all (self->priv->roster_contacts);
1409 g_hash_table_remove_all (self->priv->roster_groups);
1410 g_hash_table_remove_all (self->priv->displayed_contacts);
1414 empathy_roster_view_show_groups (EmpathyRosterView *self,
1417 if (self->priv->show_groups == show)
1420 self->priv->show_groups = show;
1422 /* TODO: block sort/filter? */
1424 populate_view (self);
1426 g_object_notify (G_OBJECT (self), "show-groups");
1430 select_first_contact (EmpathyRosterView *self)
1432 GList *children, *l;
1434 children = gtk_container_get_children (GTK_CONTAINER (self));
1435 for (l = children; l != NULL; l = g_list_next (l))
1437 GtkWidget *child = l->data;
1439 if (!gtk_widget_get_child_visible (child))
1442 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1445 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1449 g_list_free (children);
1453 search_text_notify_cb (EmpathyLiveSearch *search,
1455 EmpathyRosterView *self)
1457 egg_list_box_refilter (EGG_LIST_BOX (self));
1459 select_first_contact (self);
1463 search_activate_cb (GtkWidget *search,
1464 EmpathyRosterView *self)
1466 EggListBox *box = EGG_LIST_BOX (self);
1469 child = egg_list_box_get_selected_child (box);
1473 empathy_roster_view_child_activated (box, child);
1477 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1478 EmpathyLiveSearch *search)
1480 if (self->priv->search != NULL)
1482 g_signal_handlers_disconnect_by_func (self->priv->search,
1483 search_text_notify_cb, self);
1484 g_signal_handlers_disconnect_by_func (self->priv->search,
1485 search_activate_cb, self);
1487 g_clear_object (&self->priv->search);
1493 self->priv->search = g_object_ref (search);
1495 g_signal_connect (self->priv->search, "notify::text",
1496 G_CALLBACK (search_text_notify_cb), self);
1497 g_signal_connect (self->priv->search, "activate",
1498 G_CALLBACK (search_activate_cb), self);
1502 empathy_roster_view_is_empty (EmpathyRosterView *self)
1504 return self->priv->empty;
1508 empathy_roster_view_is_searching (EmpathyRosterView *self)
1510 return (self->priv->search != NULL &&
1511 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1514 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1515 * Empathy's internals. */
1517 empathy_roster_view_add_event (EmpathyRosterView *self,
1518 FolksIndividual *individual,
1522 GHashTable *contacts;
1524 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1525 if (contacts == NULL)
1528 self->priv->last_event_id++;
1530 g_queue_push_head (self->priv->events,
1531 event_new (self->priv->last_event_id, individual, icon, user_data));
1533 start_flashing (self);
1535 return self->priv->last_event_id;
1539 empathy_roster_view_remove_event (EmpathyRosterView *self,
1544 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1545 l = g_list_next (l))
1547 Event *event = l->data;
1549 if (event->id == event_id)
1551 remove_event (self, event);