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,
33 static guint signals[LAST_SIGNAL];
35 #define NO_GROUP "X-no-group"
36 #define UNGROUPED _("Ungrouped")
37 #define TOP_GROUP _("Top Contacts")
39 struct _EmpathyRosterViewPriv
41 EmpathyIndividualManager *manager;
43 /* FolksIndividual (borrowed) -> GHashTable (
44 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
46 * When not using groups, this hash just have one element mapped
47 * from the special NO_GROUP key. We could use it as a set but
48 * I prefer to stay coherent in the way this hash is managed.
50 GHashTable *roster_contacts;
51 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
52 GHashTable *roster_groups;
53 /* Hash of the EmpathyRosterContact currently displayed */
54 GHashTable *displayed_contacts;
57 /* queue of (Event *). The most recent events are in the head of the queue
58 * so we always display the icon of the oldest one. */
61 gboolean display_flash_event;
63 gboolean show_offline;
67 EmpathyLiveSearch *search;
69 EmpathyRosterViewIndividualTooltipCb individual_tooltip_cb;
70 gpointer individual_tooltip_data;
76 FolksIndividual *individual;
83 FolksIndividual *individual,
87 Event *event = g_slice_new (Event);
90 event->individual = g_object_ref (individual);
91 event->icon = g_strdup (icon);
92 event->user_data = user_data;
97 event_free (gpointer data)
100 g_object_unref (event->individual);
101 g_free (event->icon);
103 g_slice_free (Event, event);
107 empathy_roster_view_get_property (GObject *object,
112 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
117 g_value_set_object (value, self->priv->manager);
119 case PROP_SHOW_OFFLINE:
120 g_value_set_boolean (value, self->priv->show_offline);
122 case PROP_SHOW_GROUPS:
123 g_value_set_boolean (value, self->priv->show_groups);
126 g_value_set_boolean (value, self->priv->empty);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
135 empathy_roster_view_set_property (GObject *object,
140 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
145 g_assert (self->priv->manager == NULL); /* construct only */
146 self->priv->manager = g_value_dup_object (value);
148 case PROP_SHOW_OFFLINE:
149 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
151 case PROP_SHOW_GROUPS:
152 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
161 roster_contact_changed_cb (GtkWidget *child,
163 EmpathyRosterView *self)
165 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
169 add_roster_contact (EmpathyRosterView *self,
170 FolksIndividual *individual,
175 contact = empathy_roster_contact_new (individual, group);
177 /* Need to refilter if online is changed */
178 g_signal_connect (contact, "notify::online",
179 G_CALLBACK (roster_contact_changed_cb), self);
181 /* Need to resort if alias is changed */
182 g_signal_connect (contact, "notify::alias",
183 G_CALLBACK (roster_contact_changed_cb), self);
185 gtk_widget_show (contact);
186 gtk_container_add (GTK_CONTAINER (self), contact);
192 group_expanded_cb (EmpathyRosterGroup *group,
194 EmpathyRosterView *self)
198 widgets = empathy_roster_group_get_widgets (group);
199 for (l = widgets; l != NULL; l = g_list_next (l))
201 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
204 g_list_free (widgets);
207 static EmpathyRosterGroup *
208 lookup_roster_group (EmpathyRosterView *self,
211 return g_hash_table_lookup (self->priv->roster_groups, group);
214 static EmpathyRosterGroup *
215 ensure_roster_group (EmpathyRosterView *self,
218 GtkWidget *roster_group;
220 roster_group = (GtkWidget *) lookup_roster_group (self, group);
221 if (roster_group != NULL)
222 return EMPATHY_ROSTER_GROUP (roster_group);
224 if (!tp_strdiff (group, TOP_GROUP))
225 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
227 roster_group = empathy_roster_group_new (group, NULL);
229 g_signal_connect (roster_group, "notify::expanded",
230 G_CALLBACK (group_expanded_cb), self);
232 gtk_widget_show (roster_group);
233 gtk_container_add (GTK_CONTAINER (self), roster_group);
235 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
238 return EMPATHY_ROSTER_GROUP (roster_group);
242 update_group_widgets (EmpathyRosterView *self,
243 EmpathyRosterGroup *group,
244 EmpathyRosterContact *contact,
247 guint old_count, count;
249 old_count = empathy_roster_group_get_widgets_count (group);
252 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
254 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
256 if (count != old_count)
257 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
261 add_to_group (EmpathyRosterView *self,
262 FolksIndividual *individual,
266 GHashTable *contacts;
267 EmpathyRosterGroup *roster_group = NULL;
269 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
270 if (contacts == NULL)
273 if (tp_strdiff (group, NO_GROUP))
274 roster_group = ensure_roster_group (self, group);
276 contact = add_roster_contact (self, individual, group);
277 g_hash_table_insert (contacts, g_strdup (group), contact);
279 if (roster_group != NULL)
281 update_group_widgets (self, roster_group,
282 EMPATHY_ROSTER_CONTACT (contact), TRUE);
287 individual_added (EmpathyRosterView *self,
288 FolksIndividual *individual)
290 GHashTable *contacts;
292 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
293 if (contacts != NULL)
296 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
298 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
300 if (!self->priv->show_groups)
302 add_to_group (self, individual, NO_GROUP);
309 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
311 if (folks_favourite_details_get_is_favourite (
312 FOLKS_FAVOURITE_DETAILS (individual)) ||
313 g_list_index (tops, individual) != -1)
315 add_to_group (self, individual, TOP_GROUP);
318 groups = folks_group_details_get_groups (
319 FOLKS_GROUP_DETAILS (individual));
321 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
323 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
325 while (iter != NULL && gee_iterator_next (iter))
327 gchar *group = gee_iterator_get (iter);
329 add_to_group (self, individual, group);
334 g_clear_object (&iter);
338 /* No group, adds to Ungrouped */
339 add_to_group (self, individual, UNGROUPED);
345 set_event_icon_on_individual (EmpathyRosterView *self,
346 FolksIndividual *individual,
349 GHashTable *contacts;
353 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
354 if (contacts == NULL)
357 g_hash_table_iter_init (&iter, contacts);
358 while (g_hash_table_iter_next (&iter, NULL, &v))
360 EmpathyRosterContact *contact =v;
362 empathy_roster_contact_set_event_icon (contact, icon);
367 flash_event (Event *event,
368 EmpathyRosterView *self)
370 set_event_icon_on_individual (self, event->individual, event->icon);
374 unflash_event (Event *event,
375 EmpathyRosterView *self)
377 set_event_icon_on_individual (self, event->individual, NULL);
381 flash_cb (gpointer data)
383 EmpathyRosterView *self = data;
385 if (self->priv->display_flash_event)
387 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
388 self->priv->display_flash_event = FALSE;
392 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
393 self->priv->display_flash_event = TRUE;
400 start_flashing (EmpathyRosterView *self)
402 if (self->priv->flash_id != 0)
405 self->priv->display_flash_event = TRUE;
407 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
412 stop_flashing (EmpathyRosterView *self)
414 if (self->priv->flash_id == 0)
417 g_source_remove (self->priv->flash_id);
418 self->priv->flash_id = 0;
422 remove_event (EmpathyRosterView *self,
425 unflash_event (event, self);
426 g_queue_remove (self->priv->events, event);
428 if (g_queue_get_length (self->priv->events) == 0)
430 stop_flashing (self);
435 remove_all_individual_event (EmpathyRosterView *self,
436 FolksIndividual *individual)
440 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
443 Event *event = l->data;
445 if (event->individual == individual)
447 remove_event (self, event);
454 individual_removed (EmpathyRosterView *self,
455 FolksIndividual *individual)
457 GHashTable *contacts;
461 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
462 if (contacts == NULL)
465 remove_all_individual_event (self, individual);
467 g_hash_table_iter_init (&iter, contacts);
468 while (g_hash_table_iter_next (&iter, &key, &value))
470 const gchar *group_name = key;
471 GtkWidget *contact = value;
472 EmpathyRosterGroup *group;
474 group = lookup_roster_group (self, group_name);
477 update_group_widgets (self, group,
478 EMPATHY_ROSTER_CONTACT (contact), FALSE);
481 gtk_container_remove (GTK_CONTAINER (self), contact);
484 g_hash_table_remove (self->priv->roster_contacts, individual);
488 members_changed_cb (EmpathyIndividualManager *manager,
489 const gchar *message,
492 TpChannelGroupChangeReason reason,
493 EmpathyRosterView *self)
497 for (l = added; l != NULL; l = g_list_next (l))
499 FolksIndividual *individual = l->data;
501 individual_added (self, individual);
504 for (l = removed; l != NULL; l = g_list_next (l))
506 FolksIndividual *individual = l->data;
508 individual_removed (self, individual);
513 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
514 EmpathyRosterContact *b)
516 FolksIndividual *ind_a, *ind_b;
517 const gchar *alias_a, *alias_b;
519 ind_a = empathy_roster_contact_get_individual (a);
520 ind_b = empathy_roster_contact_get_individual (b);
522 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
523 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
525 return g_ascii_strcasecmp (alias_a, alias_b);
529 contact_is_favourite (EmpathyRosterContact *contact)
531 FolksIndividual *individual;
533 individual = empathy_roster_contact_get_individual (contact);
535 return folks_favourite_details_get_is_favourite (
536 FOLKS_FAVOURITE_DETAILS (individual));
540 contact_in_top (EmpathyRosterView *self,
541 EmpathyRosterContact *contact)
543 FolksIndividual *individual;
546 if (contact_is_favourite (contact))
549 individual = empathy_roster_contact_get_individual (contact);
551 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
553 if (g_list_index (tops, individual) != -1)
560 compare_roster_contacts_no_group (EmpathyRosterView *self,
561 EmpathyRosterContact *a,
562 EmpathyRosterContact *b)
564 gboolean top_a, top_b;
566 top_a = contact_in_top (self, a);
567 top_b = contact_in_top (self, b);
570 /* Both contacts are in the top of the roster (or not). Sort them
572 return compare_roster_contacts_by_alias (a, b);
580 compare_group_names (const gchar *group_a,
581 const gchar *group_b)
583 if (!tp_strdiff (group_a, TOP_GROUP))
586 if (!tp_strdiff (group_b, TOP_GROUP))
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. */
730 individual = empathy_roster_contact_get_individual (contact);
731 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
732 if (contacts == NULL)
735 g_hash_table_iter_init (&iter, contacts);
736 while (g_hash_table_iter_next (&iter, NULL, &v))
738 GtkWidget *group = GTK_WIDGET (v);
740 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
745 remove_from_displayed (EmpathyRosterView *self,
746 EmpathyRosterContact *contact)
748 g_hash_table_remove (self->priv->displayed_contacts, contact);
750 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
751 update_empty (self, TRUE);
755 * check if @contact should be displayed according to @self's current status
756 * and without consideration for the state of @contact's groups.
759 contact_should_be_displayed (EmpathyRosterView *self,
760 EmpathyRosterContact *contact)
762 if (is_searching (self))
764 FolksIndividual *individual;
766 individual = empathy_roster_contact_get_individual (contact);
768 return empathy_individual_match_string (individual,
769 empathy_live_search_get_text (self->priv->search),
770 empathy_live_search_get_words (self->priv->search));
774 if (self->priv->show_offline)
778 else if (!self->priv->show_groups &&
779 contact_is_favourite (contact))
781 /* Always display favourite contacts in non-group mode. In the group
782 * mode we'll display only the one in the 'top' section. */
787 return empathy_roster_contact_is_online (contact);
793 filter_contact (EmpathyRosterView *self,
794 EmpathyRosterContact *contact)
798 displayed = contact_should_be_displayed (self, contact);
800 if (self->priv->show_groups)
802 const gchar *group_name;
803 EmpathyRosterGroup *group;
805 group_name = empathy_roster_contact_get_group (contact);
806 group = lookup_roster_group (self, group_name);
808 if (!tp_strdiff (group_name, TOP_GROUP) &&
809 contact_is_favourite (contact))
814 /* When searching, always display even if the group is closed */
815 if (!is_searching (self) &&
816 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
823 add_to_displayed (self, contact);
827 remove_from_displayed (self, contact);
834 filter_group (EmpathyRosterView *self,
835 EmpathyRosterGroup *group)
839 /* Display the group if it contains at least one displayed contact */
840 widgets = empathy_roster_group_get_widgets (group);
841 for (l = widgets; l != NULL; l = g_list_next (l))
843 EmpathyRosterContact *contact = l->data;
845 if (contact_should_be_displayed (self, contact))
853 filter_list (GtkWidget *child,
856 EmpathyRosterView *self = user_data;
858 if (EMPATHY_IS_ROSTER_CONTACT (child))
859 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
861 else if (EMPATHY_IS_ROSTER_GROUP (child))
862 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
864 g_return_val_if_reached (FALSE);
867 /* @list: GList of EmpathyRosterContact
869 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
872 individual_in_list (FolksIndividual *individual,
877 for (l = list; l != NULL; l = g_list_next (l))
879 EmpathyRosterContact *contact = l->data;
881 if (empathy_roster_contact_get_individual (contact) == individual)
889 populate_view (EmpathyRosterView *self)
891 GList *individuals, *l;
893 individuals = empathy_individual_manager_get_members (self->priv->manager);
894 for (l = individuals; l != NULL; l = g_list_next (l))
896 FolksIndividual *individual = l->data;
898 individual_added (self, individual);
901 g_list_free (individuals);
905 remove_from_group (EmpathyRosterView *self,
906 FolksIndividual *individual,
909 GHashTable *contacts;
911 EmpathyRosterGroup *roster_group;
913 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
914 if (contacts == NULL)
917 contact = g_hash_table_lookup (contacts, group);
921 g_hash_table_remove (contacts, group);
923 if (g_hash_table_size (contacts) == 0)
925 add_to_group (self, individual, UNGROUPED);
928 roster_group = lookup_roster_group (self, group);
930 if (roster_group != NULL)
932 update_group_widgets (self, roster_group,
933 EMPATHY_ROSTER_CONTACT (contact), FALSE);
936 gtk_container_remove (GTK_CONTAINER (self), contact);
940 update_top_contacts (EmpathyRosterView *self)
943 GList *to_add = NULL, *to_remove = NULL;
944 EmpathyRosterGroup *group;
946 if (!self->priv->show_groups)
948 egg_list_box_resort (EGG_LIST_BOX (self));
952 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
954 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
957 to_add = g_list_copy (tops);
963 contacts = empathy_roster_group_get_widgets (group);
965 /* Check which EmpathyRosterContact have to be removed */
966 for (l = contacts; l != NULL; l = g_list_next (l))
968 EmpathyRosterContact *contact = l->data;
969 FolksIndividual *individual;
971 if (contact_is_favourite (contact))
974 individual = empathy_roster_contact_get_individual (contact);
976 if (g_list_find (tops, individual) == NULL)
977 to_remove = g_list_prepend (to_remove, individual);
980 /* Check which EmpathyRosterContact have to be added */
981 for (l = tops; l != NULL; l = g_list_next (l))
983 FolksIndividual *individual = l->data;
985 if (!individual_in_list (individual, contacts))
986 to_add = g_list_prepend (to_add, individual);
990 for (l = to_add; l != NULL; l = g_list_next (l))
991 add_to_group (self, l->data, TOP_GROUP);
993 for (l = to_remove; l != NULL; l = g_list_next (l))
994 remove_from_group (self, l->data, TOP_GROUP);
996 g_list_free (to_add);
997 g_list_free (to_remove);
1001 groups_changed_cb (EmpathyIndividualManager *manager,
1002 FolksIndividual *individual,
1005 EmpathyRosterView *self)
1007 if (!self->priv->show_groups)
1012 add_to_group (self, individual, group);
1016 remove_from_group (self, individual, group);
1021 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1023 EmpathyRosterView *self)
1025 update_top_contacts (self);
1029 favourites_changed_cb (EmpathyIndividualManager *manager,
1030 FolksIndividual *individual,
1032 EmpathyRosterView *self)
1034 GHashTable *contacts;
1036 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1037 if (contacts == NULL)
1040 if (self->priv->show_groups)
1043 add_to_group (self, individual, TOP_GROUP);
1045 remove_from_group (self, individual, TOP_GROUP);
1051 contact = g_hash_table_lookup (contacts, NO_GROUP);
1052 if (contact == NULL)
1055 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1060 empathy_roster_view_constructed (GObject *object)
1062 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1063 void (*chain_up) (GObject *) =
1064 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1066 if (chain_up != NULL)
1069 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1071 populate_view (self);
1073 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1074 G_CALLBACK (members_changed_cb), self, 0);
1075 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1076 G_CALLBACK (groups_changed_cb), self, 0);
1077 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1078 G_CALLBACK (top_individuals_changed_cb), self, 0);
1079 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1080 G_CALLBACK (favourites_changed_cb), self, 0);
1082 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1083 roster_view_sort, self, NULL);
1085 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1088 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1090 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1094 empathy_roster_view_dispose (GObject *object)
1096 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1097 void (*chain_up) (GObject *) =
1098 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1100 stop_flashing (self);
1102 empathy_roster_view_set_live_search (self, NULL);
1103 g_clear_object (&self->priv->manager);
1105 if (chain_up != NULL)
1110 empathy_roster_view_finalize (GObject *object)
1112 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1113 void (*chain_up) (GObject *) =
1114 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1116 g_hash_table_unref (self->priv->roster_contacts);
1117 g_hash_table_unref (self->priv->roster_groups);
1118 g_hash_table_unref (self->priv->displayed_contacts);
1119 g_queue_free_full (self->priv->events, event_free);
1121 if (chain_up != NULL)
1126 empathy_roster_view_child_activated (EggListBox *box,
1129 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1130 EmpathyRosterContact *contact;
1131 FolksIndividual *individual;
1134 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1137 contact = EMPATHY_ROSTER_CONTACT (child);
1138 individual = empathy_roster_contact_get_individual (contact);
1140 /* Activate the oldest event associated with this contact, if any */
1141 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1142 l = g_list_previous (l))
1144 Event *event = l->data;
1146 if (event->individual == individual)
1148 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1154 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1158 fire_popup_individual_menu (EmpathyRosterView *self,
1163 EmpathyRosterContact *contact;
1164 FolksIndividual *individual;
1166 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1169 contact = EMPATHY_ROSTER_CONTACT (child);
1170 individual = empathy_roster_contact_get_individual (contact);
1172 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1173 individual, button, time);
1177 empathy_roster_view_button_press_event (GtkWidget *widget,
1178 GdkEventButton *event)
1180 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1181 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1182 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1184 if (event->button == 3)
1188 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1192 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1194 fire_popup_individual_menu (self, child, event->button, event->time);
1198 return chain_up (widget, event);
1202 empathy_roster_view_key_press_event (GtkWidget *widget,
1205 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1206 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1207 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1209 if (event->keyval == GDK_KEY_Menu)
1213 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1216 fire_popup_individual_menu (self, child, 0, event->time);
1219 return chain_up (widget, event);
1223 empathy_roster_view_query_tooltip (GtkWidget *widget,
1226 gboolean keyboard_mode,
1227 GtkTooltip *tooltip)
1229 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1231 EmpathyRosterContact *contact;
1232 FolksIndividual *individual;
1235 if (self->priv->individual_tooltip_cb == NULL)
1238 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1239 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1242 contact = EMPATHY_ROSTER_CONTACT (child);
1243 individual = empathy_roster_contact_get_individual (contact);
1245 result = self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
1246 tooltip, self->priv->individual_tooltip_data);
1250 GtkAllocation allocation;
1252 gtk_widget_get_allocation (child, &allocation);
1253 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1260 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
1261 EmpathyRosterViewIndividualTooltipCb callback,
1264 self->priv->individual_tooltip_cb = callback;
1265 self->priv->individual_tooltip_data = user_data;
1267 gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
1271 empathy_roster_view_remove (GtkContainer *container,
1274 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1275 void (*chain_up) (GtkContainer *, GtkWidget *) =
1276 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1278 chain_up (container, widget);
1280 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1281 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1285 empathy_roster_view_class_init (
1286 EmpathyRosterViewClass *klass)
1288 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1289 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1290 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1291 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1294 oclass->get_property = empathy_roster_view_get_property;
1295 oclass->set_property = empathy_roster_view_set_property;
1296 oclass->constructed = empathy_roster_view_constructed;
1297 oclass->dispose = empathy_roster_view_dispose;
1298 oclass->finalize = empathy_roster_view_finalize;
1300 widget_class->button_press_event = empathy_roster_view_button_press_event;
1301 widget_class->key_press_event = empathy_roster_view_key_press_event;
1302 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1304 container_class->remove = empathy_roster_view_remove;
1306 box_class->child_activated = empathy_roster_view_child_activated;
1308 spec = g_param_spec_object ("manager", "Manager",
1309 "EmpathyIndividualManager",
1310 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1311 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1312 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1314 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1315 "Show offline contacts",
1317 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1318 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1320 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1323 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1324 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1326 spec = g_param_spec_boolean ("empty", "Empty",
1327 "Is the view currently empty?",
1329 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1330 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1332 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1333 G_OBJECT_CLASS_TYPE (klass),
1335 0, NULL, NULL, NULL,
1337 1, FOLKS_TYPE_INDIVIDUAL);
1339 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1340 G_OBJECT_CLASS_TYPE (klass),
1342 0, NULL, NULL, NULL,
1344 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1346 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1347 G_OBJECT_CLASS_TYPE (klass),
1349 0, NULL, NULL, NULL,
1351 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
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);