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 roster_group = empathy_roster_group_new (group);
226 g_signal_connect (roster_group, "notify::expanded",
227 G_CALLBACK (group_expanded_cb), self);
229 gtk_widget_show (roster_group);
230 gtk_container_add (GTK_CONTAINER (self), roster_group);
232 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
235 return EMPATHY_ROSTER_GROUP (roster_group);
239 update_group_widgets (EmpathyRosterView *self,
240 EmpathyRosterGroup *group,
241 EmpathyRosterContact *contact,
244 guint old_count, count;
246 old_count = empathy_roster_group_get_widgets_count (group);
249 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
251 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
253 if (count != old_count)
254 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
258 add_to_group (EmpathyRosterView *self,
259 FolksIndividual *individual,
263 GHashTable *contacts;
264 EmpathyRosterGroup *roster_group = NULL;
266 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
267 if (contacts == NULL)
270 if (tp_strdiff (group, NO_GROUP))
271 roster_group = ensure_roster_group (self, group);
273 contact = add_roster_contact (self, individual, group);
274 g_hash_table_insert (contacts, g_strdup (group), contact);
276 if (roster_group != NULL)
278 update_group_widgets (self, roster_group,
279 EMPATHY_ROSTER_CONTACT (contact), TRUE);
284 individual_added (EmpathyRosterView *self,
285 FolksIndividual *individual)
287 GHashTable *contacts;
289 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
290 if (contacts != NULL)
293 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
295 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
297 if (!self->priv->show_groups)
299 add_to_group (self, individual, NO_GROUP);
305 if (folks_favourite_details_get_is_favourite (
306 FOLKS_FAVOURITE_DETAILS (individual)))
308 add_to_group (self, individual, TOP_GROUP);
311 groups = folks_group_details_get_groups (
312 FOLKS_GROUP_DETAILS (individual));
314 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
316 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
318 while (iter != NULL && gee_iterator_next (iter))
320 gchar *group = gee_iterator_get (iter);
322 add_to_group (self, individual, group);
327 g_clear_object (&iter);
331 /* No group, adds to Ungrouped */
332 add_to_group (self, individual, UNGROUPED);
338 set_event_icon_on_individual (EmpathyRosterView *self,
339 FolksIndividual *individual,
342 GHashTable *contacts;
346 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
347 if (contacts == NULL)
350 g_hash_table_iter_init (&iter, contacts);
351 while (g_hash_table_iter_next (&iter, NULL, &v))
353 EmpathyRosterContact *contact =v;
355 empathy_roster_contact_set_event_icon (contact, icon);
360 flash_event (Event *event,
361 EmpathyRosterView *self)
363 set_event_icon_on_individual (self, event->individual, event->icon);
367 unflash_event (Event *event,
368 EmpathyRosterView *self)
370 set_event_icon_on_individual (self, event->individual, NULL);
374 flash_cb (gpointer data)
376 EmpathyRosterView *self = data;
378 if (self->priv->display_flash_event)
380 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
381 self->priv->display_flash_event = FALSE;
385 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
386 self->priv->display_flash_event = TRUE;
393 start_flashing (EmpathyRosterView *self)
395 if (self->priv->flash_id != 0)
398 self->priv->display_flash_event = TRUE;
400 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
405 stop_flashing (EmpathyRosterView *self)
407 if (self->priv->flash_id == 0)
410 g_source_remove (self->priv->flash_id);
411 self->priv->flash_id = 0;
415 remove_event (EmpathyRosterView *self,
418 unflash_event (event, self);
419 g_queue_remove (self->priv->events, event);
421 if (g_queue_get_length (self->priv->events) == 0)
423 stop_flashing (self);
428 remove_all_individual_event (EmpathyRosterView *self,
429 FolksIndividual *individual)
433 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
436 Event *event = l->data;
438 if (event->individual == individual)
440 remove_event (self, event);
447 individual_removed (EmpathyRosterView *self,
448 FolksIndividual *individual)
450 GHashTable *contacts;
454 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
455 if (contacts == NULL)
458 remove_all_individual_event (self, individual);
460 g_hash_table_iter_init (&iter, contacts);
461 while (g_hash_table_iter_next (&iter, &key, &value))
463 const gchar *group_name = key;
464 GtkWidget *contact = value;
465 EmpathyRosterGroup *group;
467 group = lookup_roster_group (self, group_name);
470 update_group_widgets (self, group,
471 EMPATHY_ROSTER_CONTACT (contact), FALSE);
474 gtk_container_remove (GTK_CONTAINER (self), contact);
477 g_hash_table_remove (self->priv->roster_contacts, individual);
481 members_changed_cb (EmpathyIndividualManager *manager,
482 const gchar *message,
485 TpChannelGroupChangeReason reason,
486 EmpathyRosterView *self)
490 for (l = added; l != NULL; l = g_list_next (l))
492 FolksIndividual *individual = l->data;
494 individual_added (self, individual);
497 for (l = removed; l != NULL; l = g_list_next (l))
499 FolksIndividual *individual = l->data;
501 individual_removed (self, individual);
506 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
507 EmpathyRosterContact *b)
509 FolksIndividual *ind_a, *ind_b;
510 const gchar *alias_a, *alias_b;
512 ind_a = empathy_roster_contact_get_individual (a);
513 ind_b = empathy_roster_contact_get_individual (b);
515 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
516 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
518 return g_ascii_strcasecmp (alias_a, alias_b);
522 contact_is_favourite (EmpathyRosterContact *contact)
524 FolksIndividual *individual;
526 individual = empathy_roster_contact_get_individual (contact);
528 return folks_favourite_details_get_is_favourite (
529 FOLKS_FAVOURITE_DETAILS (individual));
533 contact_in_top (EmpathyRosterView *self,
534 EmpathyRosterContact *contact)
536 FolksIndividual *individual;
539 if (contact_is_favourite (contact))
542 individual = empathy_roster_contact_get_individual (contact);
544 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
546 if (g_list_index (tops, individual) != -1)
553 compare_roster_contacts_no_group (EmpathyRosterView *self,
554 EmpathyRosterContact *a,
555 EmpathyRosterContact *b)
557 gboolean top_a, top_b;
559 top_a = contact_in_top (self, a);
560 top_b = contact_in_top (self, b);
563 /* Both contacts are in the top of the roster (or not). Sort them
565 return compare_roster_contacts_by_alias (a, b);
573 compare_group_names (const gchar *group_a,
574 const gchar *group_b)
576 if (!tp_strdiff (group_a, TOP_GROUP))
579 if (!tp_strdiff (group_b, TOP_GROUP))
582 return g_ascii_strcasecmp (group_a, group_b);
586 compare_roster_contacts_with_groups (EmpathyRosterView *self,
587 EmpathyRosterContact *a,
588 EmpathyRosterContact *b)
590 const gchar *group_a, *group_b;
592 group_a = empathy_roster_contact_get_group (a);
593 group_b = empathy_roster_contact_get_group (b);
595 if (!tp_strdiff (group_a, group_b))
596 /* Same group, compare the contacts */
597 return compare_roster_contacts_by_alias (a, b);
600 return compare_group_names (group_a, group_b);
604 compare_roster_contacts (EmpathyRosterView *self,
605 EmpathyRosterContact *a,
606 EmpathyRosterContact *b)
608 if (!self->priv->show_groups)
609 return compare_roster_contacts_no_group (self, a, b);
611 return compare_roster_contacts_with_groups (self, a, b);
615 compare_roster_groups (EmpathyRosterGroup *a,
616 EmpathyRosterGroup *b)
618 const gchar *name_a, *name_b;
620 name_a = empathy_roster_group_get_name (a);
621 name_b = empathy_roster_group_get_name (b);
623 return compare_group_names (name_a, name_b);
627 compare_contact_group (EmpathyRosterContact *contact,
628 EmpathyRosterGroup *group)
630 const char *contact_group, *group_name;
632 contact_group = empathy_roster_contact_get_group (contact);
633 group_name = empathy_roster_group_get_name (group);
635 if (!tp_strdiff (contact_group, group_name))
636 /* @contact is in @group, @group has to be displayed first */
639 /* @contact is in a different group, sort by group name */
640 return compare_group_names (contact_group, group_name);
644 roster_view_sort (gconstpointer a,
648 EmpathyRosterView *self = user_data;
650 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
651 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
652 EMPATHY_ROSTER_CONTACT (b));
653 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
654 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
655 EMPATHY_ROSTER_GROUP (b));
656 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
657 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
658 EMPATHY_ROSTER_GROUP (b));
659 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
660 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
661 EMPATHY_ROSTER_GROUP (a));
663 g_return_val_if_reached (0);
667 update_separator (GtkWidget **separator,
674 /* No separator before the first row */
675 g_clear_object (separator);
679 if (*separator != NULL)
682 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
683 g_object_ref_sink (*separator);
687 is_searching (EmpathyRosterView *self)
689 if (self->priv->search == NULL)
692 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
696 update_empty (EmpathyRosterView *self,
699 if (self->priv->empty == empty)
702 self->priv->empty = empty;
703 g_object_notify (G_OBJECT (self), "empty");
707 add_to_displayed (EmpathyRosterView *self,
708 EmpathyRosterContact *contact)
710 FolksIndividual *individual;
711 GHashTable *contacts;
715 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
718 g_hash_table_add (self->priv->displayed_contacts, contact);
719 update_empty (self, FALSE);
721 /* Groups of this contact may now be displayed if we just displays the first
722 * child in this group. */
723 individual = empathy_roster_contact_get_individual (contact);
724 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
725 if (contacts == NULL)
728 g_hash_table_iter_init (&iter, contacts);
729 while (g_hash_table_iter_next (&iter, NULL, &v))
731 GtkWidget *group = GTK_WIDGET (v);
733 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
738 remove_from_displayed (EmpathyRosterView *self,
739 EmpathyRosterContact *contact)
741 g_hash_table_remove (self->priv->displayed_contacts, contact);
743 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
744 update_empty (self, TRUE);
748 * check if @contact should be displayed according to @self's current status
749 * and without consideration for the state of @contact's groups.
752 contact_should_be_displayed (EmpathyRosterView *self,
753 EmpathyRosterContact *contact)
755 if (is_searching (self))
757 FolksIndividual *individual;
759 individual = empathy_roster_contact_get_individual (contact);
761 return empathy_individual_match_string (individual,
762 empathy_live_search_get_text (self->priv->search),
763 empathy_live_search_get_words (self->priv->search));
767 if (self->priv->show_offline)
771 else if (!self->priv->show_groups &&
772 contact_is_favourite (contact))
774 /* Always display favourite contacts in non-group mode. In the group
775 * mode we'll display only the one in the 'top' section. */
780 return empathy_roster_contact_is_online (contact);
786 filter_contact (EmpathyRosterView *self,
787 EmpathyRosterContact *contact)
791 displayed = contact_should_be_displayed (self, contact);
793 if (self->priv->show_groups)
795 const gchar *group_name;
796 EmpathyRosterGroup *group;
798 group_name = empathy_roster_contact_get_group (contact);
799 group = lookup_roster_group (self, group_name);
801 if (!tp_strdiff (group_name, TOP_GROUP) &&
802 contact_is_favourite (contact))
807 /* When searching, always display even if the group is closed */
808 if (!is_searching (self) &&
809 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
816 add_to_displayed (self, contact);
820 remove_from_displayed (self, contact);
827 filter_group (EmpathyRosterView *self,
828 EmpathyRosterGroup *group)
832 /* Display the group if it contains at least one displayed contact */
833 widgets = empathy_roster_group_get_widgets (group);
834 for (l = widgets; l != NULL; l = g_list_next (l))
836 EmpathyRosterContact *contact = l->data;
838 if (contact_should_be_displayed (self, contact))
846 filter_list (GtkWidget *child,
849 EmpathyRosterView *self = user_data;
851 if (EMPATHY_IS_ROSTER_CONTACT (child))
852 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
854 else if (EMPATHY_IS_ROSTER_GROUP (child))
855 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
857 g_return_val_if_reached (FALSE);
860 /* @list: GList of EmpathyRosterContact
862 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
865 individual_in_list (FolksIndividual *individual,
870 for (l = list; l != NULL; l = g_list_next (l))
872 EmpathyRosterContact *contact = l->data;
874 if (empathy_roster_contact_get_individual (contact) == individual)
882 populate_view (EmpathyRosterView *self)
884 GList *individuals, *l;
886 individuals = empathy_individual_manager_get_members (self->priv->manager);
887 for (l = individuals; l != NULL; l = g_list_next (l))
889 FolksIndividual *individual = l->data;
891 individual_added (self, individual);
894 g_list_free (individuals);
898 remove_from_group (EmpathyRosterView *self,
899 FolksIndividual *individual,
902 GHashTable *contacts;
904 EmpathyRosterGroup *roster_group;
906 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
907 if (contacts == NULL)
910 contact = g_hash_table_lookup (contacts, group);
914 g_hash_table_remove (contacts, group);
916 if (g_hash_table_size (contacts) == 0)
918 add_to_group (self, individual, UNGROUPED);
921 roster_group = lookup_roster_group (self, group);
923 if (roster_group != NULL)
925 update_group_widgets (self, roster_group,
926 EMPATHY_ROSTER_CONTACT (contact), FALSE);
929 gtk_container_remove (GTK_CONTAINER (self), contact);
933 update_top_contacts (EmpathyRosterView *self)
936 GList *to_add = NULL, *to_remove = NULL;
937 EmpathyRosterGroup *group;
939 if (!self->priv->show_groups)
941 egg_list_box_resort (EGG_LIST_BOX (self));
945 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
947 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
950 to_add = g_list_copy (tops);
956 contacts = empathy_roster_group_get_widgets (group);
958 /* Check which EmpathyRosterContact have to be removed */
959 for (l = contacts; l != NULL; l = g_list_next (l))
961 EmpathyRosterContact *contact = l->data;
962 FolksIndividual *individual;
964 if (contact_is_favourite (contact))
967 individual = empathy_roster_contact_get_individual (contact);
969 if (g_list_find (tops, individual) == NULL)
970 to_remove = g_list_prepend (to_remove, individual);
973 /* Check which EmpathyRosterContact have to be added */
974 for (l = tops; l != NULL; l = g_list_next (l))
976 FolksIndividual *individual = l->data;
978 if (!individual_in_list (individual, contacts))
979 to_add = g_list_prepend (to_add, individual);
983 for (l = to_add; l != NULL; l = g_list_next (l))
984 add_to_group (self, l->data, TOP_GROUP);
986 for (l = to_remove; l != NULL; l = g_list_next (l))
987 remove_from_group (self, l->data, TOP_GROUP);
989 g_list_free (to_add);
990 g_list_free (to_remove);
994 groups_changed_cb (EmpathyIndividualManager *manager,
995 FolksIndividual *individual,
998 EmpathyRosterView *self)
1000 if (!self->priv->show_groups)
1005 add_to_group (self, individual, group);
1009 remove_from_group (self, individual, group);
1014 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1016 EmpathyRosterView *self)
1018 update_top_contacts (self);
1022 favourites_changed_cb (EmpathyIndividualManager *manager,
1023 FolksIndividual *individual,
1025 EmpathyRosterView *self)
1027 GHashTable *contacts;
1029 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1030 if (contacts == NULL)
1033 if (self->priv->show_groups)
1036 add_to_group (self, individual, TOP_GROUP);
1038 remove_from_group (self, individual, TOP_GROUP);
1044 contact = g_hash_table_lookup (contacts, NO_GROUP);
1045 if (contact == NULL)
1048 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1053 empathy_roster_view_constructed (GObject *object)
1055 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1056 void (*chain_up) (GObject *) =
1057 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1059 if (chain_up != NULL)
1062 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1064 populate_view (self);
1066 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1067 G_CALLBACK (members_changed_cb), self, 0);
1068 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1069 G_CALLBACK (groups_changed_cb), self, 0);
1070 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1071 G_CALLBACK (top_individuals_changed_cb), self, 0);
1072 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1073 G_CALLBACK (favourites_changed_cb), self, 0);
1075 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1076 roster_view_sort, self, NULL);
1078 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1081 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1083 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1087 empathy_roster_view_dispose (GObject *object)
1089 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1090 void (*chain_up) (GObject *) =
1091 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1093 stop_flashing (self);
1095 empathy_roster_view_set_live_search (self, NULL);
1096 g_clear_object (&self->priv->manager);
1098 if (chain_up != NULL)
1103 empathy_roster_view_finalize (GObject *object)
1105 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1106 void (*chain_up) (GObject *) =
1107 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1109 g_hash_table_unref (self->priv->roster_contacts);
1110 g_hash_table_unref (self->priv->roster_groups);
1111 g_hash_table_unref (self->priv->displayed_contacts);
1112 g_queue_free_full (self->priv->events, event_free);
1114 if (chain_up != NULL)
1119 empathy_roster_view_child_activated (EggListBox *box,
1122 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1123 EmpathyRosterContact *contact;
1124 FolksIndividual *individual;
1127 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1130 contact = EMPATHY_ROSTER_CONTACT (child);
1131 individual = empathy_roster_contact_get_individual (contact);
1133 /* Activate the oldest event associated with this contact, if any */
1134 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1135 l = g_list_previous (l))
1137 Event *event = l->data;
1139 if (event->individual == individual)
1141 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1147 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1151 fire_popup_individual_menu (EmpathyRosterView *self,
1156 EmpathyRosterContact *contact;
1157 FolksIndividual *individual;
1159 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1162 contact = EMPATHY_ROSTER_CONTACT (child);
1163 individual = empathy_roster_contact_get_individual (contact);
1165 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1166 individual, button, time);
1170 empathy_roster_view_button_press_event (GtkWidget *widget,
1171 GdkEventButton *event)
1173 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1174 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1175 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1177 if (event->button == 3)
1181 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1184 fire_popup_individual_menu (self, child, event->button, event->time);
1187 return chain_up (widget, event);
1191 empathy_roster_view_key_press_event (GtkWidget *widget,
1194 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1195 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1196 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1198 if (event->keyval == GDK_KEY_Menu)
1202 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1205 fire_popup_individual_menu (self, child, 0, event->time);
1208 return chain_up (widget, event);
1212 empathy_roster_view_query_tooltip (GtkWidget *widget,
1215 gboolean keyboard_mode,
1216 GtkTooltip *tooltip)
1218 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1220 EmpathyRosterContact *contact;
1221 FolksIndividual *individual;
1223 if (self->priv->individual_tooltip_cb == NULL)
1226 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1227 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1230 contact = EMPATHY_ROSTER_CONTACT (child);
1231 individual = empathy_roster_contact_get_individual (contact);
1233 return self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
1234 tooltip, self->priv->individual_tooltip_data);
1238 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
1239 EmpathyRosterViewIndividualTooltipCb callback,
1242 self->priv->individual_tooltip_cb = callback;
1243 self->priv->individual_tooltip_data = user_data;
1245 gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
1249 empathy_roster_view_remove (GtkContainer *container,
1252 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1253 void (*chain_up) (GtkContainer *, GtkWidget *) =
1254 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1256 chain_up (container, widget);
1258 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1259 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1263 empathy_roster_view_class_init (
1264 EmpathyRosterViewClass *klass)
1266 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1267 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1268 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1269 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1272 oclass->get_property = empathy_roster_view_get_property;
1273 oclass->set_property = empathy_roster_view_set_property;
1274 oclass->constructed = empathy_roster_view_constructed;
1275 oclass->dispose = empathy_roster_view_dispose;
1276 oclass->finalize = empathy_roster_view_finalize;
1278 widget_class->button_press_event = empathy_roster_view_button_press_event;
1279 widget_class->key_press_event = empathy_roster_view_key_press_event;
1280 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1282 container_class->remove = empathy_roster_view_remove;
1284 box_class->child_activated = empathy_roster_view_child_activated;
1286 spec = g_param_spec_object ("manager", "Manager",
1287 "EmpathyIndividualManager",
1288 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1289 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1290 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1292 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1293 "Show offline contacts",
1295 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1296 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1298 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1301 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1302 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1304 spec = g_param_spec_boolean ("empty", "Empty",
1305 "Is the view currently empty?",
1307 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1308 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1310 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1311 G_OBJECT_CLASS_TYPE (klass),
1313 0, NULL, NULL, NULL,
1315 1, FOLKS_TYPE_INDIVIDUAL);
1317 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1318 G_OBJECT_CLASS_TYPE (klass),
1320 0, NULL, NULL, NULL,
1322 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1324 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1325 G_OBJECT_CLASS_TYPE (klass),
1327 0, NULL, NULL, NULL,
1329 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1331 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1335 empathy_roster_view_init (EmpathyRosterView *self)
1337 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1338 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1340 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1341 NULL, (GDestroyNotify) g_hash_table_unref);
1342 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1344 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1346 self->priv->events = g_queue_new ();
1348 self->priv->empty = TRUE;
1352 empathy_roster_view_new (EmpathyIndividualManager *manager)
1354 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1356 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1361 EmpathyIndividualManager *
1362 empathy_roster_view_get_manager (EmpathyRosterView *self)
1364 return self->priv->manager;
1368 empathy_roster_view_show_offline (EmpathyRosterView *self,
1371 if (self->priv->show_offline == show)
1374 self->priv->show_offline = show;
1375 egg_list_box_refilter (EGG_LIST_BOX (self));
1377 g_object_notify (G_OBJECT (self), "show-offline");
1381 clear_view (EmpathyRosterView *self)
1383 gtk_container_foreach (GTK_CONTAINER (self),
1384 (GtkCallback) gtk_widget_destroy, NULL);
1386 g_hash_table_remove_all (self->priv->roster_contacts);
1387 g_hash_table_remove_all (self->priv->roster_groups);
1388 g_hash_table_remove_all (self->priv->displayed_contacts);
1392 empathy_roster_view_show_groups (EmpathyRosterView *self,
1395 if (self->priv->show_groups == show)
1398 self->priv->show_groups = show;
1400 /* TODO: block sort/filter? */
1402 populate_view (self);
1404 g_object_notify (G_OBJECT (self), "show-groups");
1408 select_first_contact (EmpathyRosterView *self)
1410 GList *children, *l;
1412 children = gtk_container_get_children (GTK_CONTAINER (self));
1413 for (l = children; l != NULL; l = g_list_next (l))
1415 GtkWidget *child = l->data;
1417 if (!gtk_widget_get_child_visible (child))
1420 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1423 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1427 g_list_free (children);
1431 search_text_notify_cb (EmpathyLiveSearch *search,
1433 EmpathyRosterView *self)
1435 egg_list_box_refilter (EGG_LIST_BOX (self));
1437 select_first_contact (self);
1441 search_activate_cb (GtkWidget *search,
1442 EmpathyRosterView *self)
1444 EggListBox *box = EGG_LIST_BOX (self);
1447 child = egg_list_box_get_selected_child (box);
1451 empathy_roster_view_child_activated (box, child);
1455 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1456 EmpathyLiveSearch *search)
1458 if (self->priv->search != NULL)
1460 g_signal_handlers_disconnect_by_func (self->priv->search,
1461 search_text_notify_cb, self);
1462 g_signal_handlers_disconnect_by_func (self->priv->search,
1463 search_activate_cb, self);
1465 g_clear_object (&self->priv->search);
1471 self->priv->search = g_object_ref (search);
1473 g_signal_connect (self->priv->search, "notify::text",
1474 G_CALLBACK (search_text_notify_cb), self);
1475 g_signal_connect (self->priv->search, "activate",
1476 G_CALLBACK (search_activate_cb), self);
1480 empathy_roster_view_is_empty (EmpathyRosterView *self)
1482 return self->priv->empty;
1486 empathy_roster_view_is_searching (EmpathyRosterView *self)
1488 return (self->priv->search != NULL &&
1489 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1492 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1493 * Empathy's internals. */
1495 empathy_roster_view_add_event (EmpathyRosterView *self,
1496 FolksIndividual *individual,
1500 GHashTable *contacts;
1502 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1503 if (contacts == NULL)
1506 self->priv->last_event_id++;
1508 g_queue_push_head (self->priv->events,
1509 event_new (self->priv->last_event_id, individual, icon, user_data));
1511 start_flashing (self);
1513 return self->priv->last_event_id;
1517 empathy_roster_view_remove_event (EmpathyRosterView *self,
1522 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1523 l = g_list_next (l))
1525 Event *event = l->data;
1527 if (event->id == event_id)
1529 remove_event (self, event);