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 _("Most Used")
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 groups = folks_group_details_get_groups (
306 FOLKS_GROUP_DETAILS (individual));
308 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
310 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
312 while (iter != NULL && gee_iterator_next (iter))
314 gchar *group = gee_iterator_get (iter);
316 add_to_group (self, individual, group);
321 g_clear_object (&iter);
325 /* No group, adds to Ungrouped */
326 add_to_group (self, individual, UNGROUPED);
332 set_event_icon_on_individual (EmpathyRosterView *self,
333 FolksIndividual *individual,
336 GHashTable *contacts;
340 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
341 if (contacts == NULL)
344 g_hash_table_iter_init (&iter, contacts);
345 while (g_hash_table_iter_next (&iter, NULL, &v))
347 EmpathyRosterContact *contact =v;
349 empathy_roster_contact_set_event_icon (contact, icon);
354 flash_event (Event *event,
355 EmpathyRosterView *self)
357 set_event_icon_on_individual (self, event->individual, event->icon);
361 unflash_event (Event *event,
362 EmpathyRosterView *self)
364 set_event_icon_on_individual (self, event->individual, NULL);
368 flash_cb (gpointer data)
370 EmpathyRosterView *self = data;
372 if (self->priv->display_flash_event)
374 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
375 self->priv->display_flash_event = FALSE;
379 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
380 self->priv->display_flash_event = TRUE;
387 start_flashing (EmpathyRosterView *self)
389 if (self->priv->flash_id != 0)
392 self->priv->display_flash_event = TRUE;
394 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
399 stop_flashing (EmpathyRosterView *self)
401 if (self->priv->flash_id == 0)
404 g_source_remove (self->priv->flash_id);
405 self->priv->flash_id = 0;
409 remove_event (EmpathyRosterView *self,
412 unflash_event (event, self);
413 g_queue_remove (self->priv->events, event);
415 if (g_queue_get_length (self->priv->events) == 0)
417 stop_flashing (self);
422 remove_all_individual_event (EmpathyRosterView *self,
423 FolksIndividual *individual)
427 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
430 Event *event = l->data;
432 if (event->individual == individual)
434 remove_event (self, event);
441 individual_removed (EmpathyRosterView *self,
442 FolksIndividual *individual)
444 GHashTable *contacts;
448 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
449 if (contacts == NULL)
452 remove_all_individual_event (self, individual);
454 g_hash_table_iter_init (&iter, contacts);
455 while (g_hash_table_iter_next (&iter, &key, &value))
457 const gchar *group_name = key;
458 GtkWidget *contact = value;
459 EmpathyRosterGroup *group;
461 group = lookup_roster_group (self, group_name);
464 update_group_widgets (self, group,
465 EMPATHY_ROSTER_CONTACT (contact), FALSE);
468 gtk_container_remove (GTK_CONTAINER (self), contact);
471 g_hash_table_remove (self->priv->roster_contacts, individual);
475 members_changed_cb (EmpathyIndividualManager *manager,
476 const gchar *message,
479 TpChannelGroupChangeReason reason,
480 EmpathyRosterView *self)
484 for (l = added; l != NULL; l = g_list_next (l))
486 FolksIndividual *individual = l->data;
488 individual_added (self, individual);
491 for (l = removed; l != NULL; l = g_list_next (l))
493 FolksIndividual *individual = l->data;
495 individual_removed (self, individual);
500 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
501 EmpathyRosterContact *b)
503 FolksIndividual *ind_a, *ind_b;
504 const gchar *alias_a, *alias_b;
506 ind_a = empathy_roster_contact_get_individual (a);
507 ind_b = empathy_roster_contact_get_individual (b);
509 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
510 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
512 return g_ascii_strcasecmp (alias_a, alias_b);
516 compare_individual_top_position (EmpathyRosterView *self,
517 EmpathyRosterContact *a,
518 EmpathyRosterContact *b)
520 FolksIndividual *ind_a, *ind_b;
522 gint index_a, index_b;
524 ind_a = empathy_roster_contact_get_individual (a);
525 ind_b = empathy_roster_contact_get_individual (b);
527 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
529 index_a = g_list_index (tops, ind_a);
530 index_b = g_list_index (tops, ind_b);
532 if (index_a == index_b)
541 return index_a - index_b;
545 compare_roster_contacts_no_group (EmpathyRosterView *self,
546 EmpathyRosterContact *a,
547 EmpathyRosterContact *b)
551 top = compare_individual_top_position (self, a, b);
555 return compare_roster_contacts_by_alias (a, b);
559 compare_group_names (const gchar *group_a,
560 const gchar *group_b)
562 if (!tp_strdiff (group_a, TOP_GROUP))
565 if (!tp_strdiff (group_b, TOP_GROUP))
568 return g_ascii_strcasecmp (group_a, group_b);
572 compare_roster_contacts_with_groups (EmpathyRosterView *self,
573 EmpathyRosterContact *a,
574 EmpathyRosterContact *b)
576 const gchar *group_a, *group_b;
578 group_a = empathy_roster_contact_get_group (a);
579 group_b = empathy_roster_contact_get_group (b);
581 if (!tp_strdiff (group_a, group_b))
582 /* Same group, compare the contacts */
583 return compare_roster_contacts_by_alias (a, b);
586 return compare_group_names (group_a, group_b);
590 compare_roster_contacts (EmpathyRosterView *self,
591 EmpathyRosterContact *a,
592 EmpathyRosterContact *b)
594 if (!self->priv->show_groups)
595 return compare_roster_contacts_no_group (self, a, b);
597 return compare_roster_contacts_with_groups (self, a, b);
601 compare_roster_groups (EmpathyRosterGroup *a,
602 EmpathyRosterGroup *b)
604 const gchar *name_a, *name_b;
606 name_a = empathy_roster_group_get_name (a);
607 name_b = empathy_roster_group_get_name (b);
609 return compare_group_names (name_a, name_b);
613 compare_contact_group (EmpathyRosterContact *contact,
614 EmpathyRosterGroup *group)
616 const char *contact_group, *group_name;
618 contact_group = empathy_roster_contact_get_group (contact);
619 group_name = empathy_roster_group_get_name (group);
621 if (!tp_strdiff (contact_group, group_name))
622 /* @contact is in @group, @group has to be displayed first */
625 /* @contact is in a different group, sort by group name */
626 return compare_group_names (contact_group, group_name);
630 roster_view_sort (gconstpointer a,
634 EmpathyRosterView *self = user_data;
636 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
637 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
638 EMPATHY_ROSTER_CONTACT (b));
639 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
640 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
641 EMPATHY_ROSTER_GROUP (b));
642 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
643 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
644 EMPATHY_ROSTER_GROUP (b));
645 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
646 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
647 EMPATHY_ROSTER_GROUP (a));
649 g_return_val_if_reached (0);
653 update_separator (GtkWidget **separator,
660 /* No separator before the first row */
661 g_clear_object (separator);
665 if (*separator != NULL)
668 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
669 g_object_ref_sink (*separator);
673 is_searching (EmpathyRosterView *self)
675 if (self->priv->search == NULL)
678 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
682 update_empty (EmpathyRosterView *self,
685 if (self->priv->empty == empty)
688 self->priv->empty = empty;
689 g_object_notify (G_OBJECT (self), "empty");
693 add_to_displayed (EmpathyRosterView *self,
694 EmpathyRosterContact *contact)
696 FolksIndividual *individual;
697 GHashTable *contacts;
701 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
704 g_hash_table_add (self->priv->displayed_contacts, contact);
705 update_empty (self, FALSE);
707 /* Groups of this contact may now be displayed if we just displays the first
708 * child in this group. */
709 individual = empathy_roster_contact_get_individual (contact);
710 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
711 if (contacts == NULL)
714 g_hash_table_iter_init (&iter, contacts);
715 while (g_hash_table_iter_next (&iter, NULL, &v))
717 GtkWidget *group = GTK_WIDGET (v);
719 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
724 remove_from_displayed (EmpathyRosterView *self,
725 EmpathyRosterContact *contact)
727 g_hash_table_remove (self->priv->displayed_contacts, contact);
729 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
730 update_empty (self, TRUE);
734 * check if @contact should be displayed according to @self's current status
735 * and without consideration for the state of @contact's groups.
738 contact_should_be_displayed (EmpathyRosterView *self,
739 EmpathyRosterContact *contact)
741 if (is_searching (self))
743 FolksIndividual *individual;
745 individual = empathy_roster_contact_get_individual (contact);
747 return empathy_individual_match_string (individual,
748 empathy_live_search_get_text (self->priv->search),
749 empathy_live_search_get_words (self->priv->search));
753 if (self->priv->show_offline)
759 return empathy_roster_contact_is_online (contact);
765 filter_contact (EmpathyRosterView *self,
766 EmpathyRosterContact *contact)
770 displayed = contact_should_be_displayed (self, contact);
772 if (self->priv->show_groups)
774 const gchar *group_name;
775 EmpathyRosterGroup *group;
777 group_name = empathy_roster_contact_get_group (contact);
778 group = lookup_roster_group (self, group_name);
782 /* When searching, always display even if the group is closed */
783 if (!is_searching (self) &&
784 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
791 add_to_displayed (self, contact);
795 remove_from_displayed (self, contact);
802 filter_group (EmpathyRosterView *self,
803 EmpathyRosterGroup *group)
807 /* Display the group if it contains at least one displayed contact */
808 widgets = empathy_roster_group_get_widgets (group);
809 for (l = widgets; l != NULL; l = g_list_next (l))
811 EmpathyRosterContact *contact = l->data;
813 if (contact_should_be_displayed (self, contact))
821 filter_list (GtkWidget *child,
824 EmpathyRosterView *self = user_data;
826 if (EMPATHY_IS_ROSTER_CONTACT (child))
827 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
829 else if (EMPATHY_IS_ROSTER_GROUP (child))
830 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
832 g_return_val_if_reached (FALSE);
835 /* @list: GList of EmpathyRosterContact
837 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
840 individual_in_list (FolksIndividual *individual,
845 for (l = list; l != NULL; l = g_list_next (l))
847 EmpathyRosterContact *contact = l->data;
849 if (empathy_roster_contact_get_individual (contact) == individual)
857 populate_view (EmpathyRosterView *self)
859 GList *individuals, *l;
861 individuals = empathy_individual_manager_get_members (self->priv->manager);
862 for (l = individuals; l != NULL; l = g_list_next (l))
864 FolksIndividual *individual = l->data;
866 individual_added (self, individual);
869 g_list_free (individuals);
873 remove_from_group (EmpathyRosterView *self,
874 FolksIndividual *individual,
877 GHashTable *contacts;
879 EmpathyRosterGroup *roster_group;
881 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
882 if (contacts == NULL)
885 contact = g_hash_table_lookup (contacts, group);
889 g_hash_table_remove (contacts, group);
891 if (g_hash_table_size (contacts) == 0)
893 add_to_group (self, individual, UNGROUPED);
896 roster_group = lookup_roster_group (self, group);
898 if (roster_group != NULL)
900 update_group_widgets (self, roster_group,
901 EMPATHY_ROSTER_CONTACT (contact), FALSE);
904 gtk_container_remove (GTK_CONTAINER (self), contact);
908 update_top_contacts (EmpathyRosterView *self)
911 GList *to_add = NULL, *to_remove = NULL;
912 EmpathyRosterGroup *group;
914 if (!self->priv->show_groups)
916 egg_list_box_resort (EGG_LIST_BOX (self));
920 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
922 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
925 to_add = g_list_copy (tops);
931 contacts = empathy_roster_group_get_widgets (group);
933 /* Check which EmpathyRosterContact have to be removed */
934 for (l = contacts; l != NULL; l = g_list_next (l))
936 EmpathyRosterContact *contact = l->data;
937 FolksIndividual *individual;
939 individual = empathy_roster_contact_get_individual (contact);
941 if (g_list_find (tops, individual) == NULL)
942 to_remove = g_list_prepend (to_remove, individual);
945 /* Check which EmpathyRosterContact have to be added */
946 for (l = tops; l != NULL; l = g_list_next (l))
948 FolksIndividual *individual = l->data;
950 if (!individual_in_list (individual, contacts))
951 to_add = g_list_prepend (to_add, individual);
955 for (l = to_add; l != NULL; l = g_list_next (l))
956 add_to_group (self, l->data, TOP_GROUP);
958 for (l = to_remove; l != NULL; l = g_list_next (l))
959 remove_from_group (self, l->data, TOP_GROUP);
961 g_list_free (to_add);
962 g_list_free (to_remove);
966 groups_changed_cb (EmpathyIndividualManager *manager,
967 FolksIndividual *individual,
970 EmpathyRosterView *self)
972 if (!self->priv->show_groups)
977 add_to_group (self, individual, group);
981 remove_from_group (self, individual, group);
986 top_individuals_changed_cb (EmpathyIndividualManager *manager,
988 EmpathyRosterView *self)
990 update_top_contacts (self);
994 empathy_roster_view_constructed (GObject *object)
996 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
997 void (*chain_up) (GObject *) =
998 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1000 if (chain_up != NULL)
1003 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1005 populate_view (self);
1007 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1008 G_CALLBACK (members_changed_cb), self, 0);
1009 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1010 G_CALLBACK (groups_changed_cb), self, 0);
1011 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1012 G_CALLBACK (top_individuals_changed_cb), self, 0);
1014 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1015 roster_view_sort, self, NULL);
1017 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1020 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1022 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1026 empathy_roster_view_dispose (GObject *object)
1028 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1029 void (*chain_up) (GObject *) =
1030 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1032 stop_flashing (self);
1034 empathy_roster_view_set_live_search (self, NULL);
1035 g_clear_object (&self->priv->manager);
1037 if (chain_up != NULL)
1042 empathy_roster_view_finalize (GObject *object)
1044 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1045 void (*chain_up) (GObject *) =
1046 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1048 g_hash_table_unref (self->priv->roster_contacts);
1049 g_hash_table_unref (self->priv->roster_groups);
1050 g_hash_table_unref (self->priv->displayed_contacts);
1051 g_queue_free_full (self->priv->events, event_free);
1053 if (chain_up != NULL)
1058 empathy_roster_view_child_activated (EggListBox *box,
1061 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1062 EmpathyRosterContact *contact;
1063 FolksIndividual *individual;
1066 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1069 contact = EMPATHY_ROSTER_CONTACT (child);
1070 individual = empathy_roster_contact_get_individual (contact);
1072 /* Activate the oldest event associated with this contact, if any */
1073 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1074 l = g_list_previous (l))
1076 Event *event = l->data;
1078 if (event->individual == individual)
1080 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1086 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1090 fire_popup_individual_menu (EmpathyRosterView *self,
1095 EmpathyRosterContact *contact;
1096 FolksIndividual *individual;
1098 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1101 contact = EMPATHY_ROSTER_CONTACT (child);
1102 individual = empathy_roster_contact_get_individual (contact);
1104 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1105 individual, button, time);
1109 empathy_roster_view_button_press_event (GtkWidget *widget,
1110 GdkEventButton *event)
1112 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1113 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1114 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1116 if (event->button == 3)
1120 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1123 fire_popup_individual_menu (self, child, event->button, event->time);
1126 return chain_up (widget, event);
1130 empathy_roster_view_key_press_event (GtkWidget *widget,
1133 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1134 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1135 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1137 if (event->keyval == GDK_KEY_Menu)
1141 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1144 fire_popup_individual_menu (self, child, 0, event->time);
1147 return chain_up (widget, event);
1151 empathy_roster_view_query_tooltip (GtkWidget *widget,
1154 gboolean keyboard_mode,
1155 GtkTooltip *tooltip)
1157 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1159 EmpathyRosterContact *contact;
1160 FolksIndividual *individual;
1162 if (self->priv->individual_tooltip_cb == NULL)
1165 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1166 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1169 contact = EMPATHY_ROSTER_CONTACT (child);
1170 individual = empathy_roster_contact_get_individual (contact);
1172 return self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
1173 tooltip, self->priv->individual_tooltip_data);
1177 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
1178 EmpathyRosterViewIndividualTooltipCb callback,
1181 self->priv->individual_tooltip_cb = callback;
1182 self->priv->individual_tooltip_data = user_data;
1184 gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
1188 empathy_roster_view_remove (GtkContainer *container,
1191 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1192 void (*chain_up) (GtkContainer *, GtkWidget *) =
1193 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1195 chain_up (container, widget);
1197 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1198 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1202 empathy_roster_view_class_init (
1203 EmpathyRosterViewClass *klass)
1205 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1206 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1207 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1208 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1211 oclass->get_property = empathy_roster_view_get_property;
1212 oclass->set_property = empathy_roster_view_set_property;
1213 oclass->constructed = empathy_roster_view_constructed;
1214 oclass->dispose = empathy_roster_view_dispose;
1215 oclass->finalize = empathy_roster_view_finalize;
1217 widget_class->button_press_event = empathy_roster_view_button_press_event;
1218 widget_class->key_press_event = empathy_roster_view_key_press_event;
1219 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1221 container_class->remove = empathy_roster_view_remove;
1223 box_class->child_activated = empathy_roster_view_child_activated;
1225 spec = g_param_spec_object ("manager", "Manager",
1226 "EmpathyIndividualManager",
1227 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1228 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1229 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1231 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1232 "Show offline contacts",
1234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1235 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1237 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1240 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1241 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1243 spec = g_param_spec_boolean ("empty", "Empty",
1244 "Is the view currently empty?",
1246 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1247 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1249 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1250 G_OBJECT_CLASS_TYPE (klass),
1252 0, NULL, NULL, NULL,
1254 1, FOLKS_TYPE_INDIVIDUAL);
1256 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1257 G_OBJECT_CLASS_TYPE (klass),
1259 0, NULL, NULL, NULL,
1261 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1263 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1264 G_OBJECT_CLASS_TYPE (klass),
1266 0, NULL, NULL, NULL,
1268 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1270 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1274 empathy_roster_view_init (EmpathyRosterView *self)
1276 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1277 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1279 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1280 NULL, (GDestroyNotify) g_hash_table_unref);
1281 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1283 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1285 self->priv->events = g_queue_new ();
1287 self->priv->empty = TRUE;
1291 empathy_roster_view_new (EmpathyIndividualManager *manager)
1293 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1295 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1300 EmpathyIndividualManager *
1301 empathy_roster_view_get_manager (EmpathyRosterView *self)
1303 return self->priv->manager;
1307 empathy_roster_view_show_offline (EmpathyRosterView *self,
1310 if (self->priv->show_offline == show)
1313 self->priv->show_offline = show;
1314 egg_list_box_refilter (EGG_LIST_BOX (self));
1316 g_object_notify (G_OBJECT (self), "show-offline");
1320 clear_view (EmpathyRosterView *self)
1322 gtk_container_foreach (GTK_CONTAINER (self),
1323 (GtkCallback) gtk_widget_destroy, NULL);
1325 g_hash_table_remove_all (self->priv->roster_contacts);
1326 g_hash_table_remove_all (self->priv->roster_groups);
1327 g_hash_table_remove_all (self->priv->displayed_contacts);
1331 empathy_roster_view_show_groups (EmpathyRosterView *self,
1334 if (self->priv->show_groups == show)
1337 self->priv->show_groups = show;
1339 /* TODO: block sort/filter? */
1341 populate_view (self);
1343 g_object_notify (G_OBJECT (self), "show-groups");
1347 select_first_contact (EmpathyRosterView *self)
1349 GList *children, *l;
1351 children = gtk_container_get_children (GTK_CONTAINER (self));
1352 for (l = children; l != NULL; l = g_list_next (l))
1354 GtkWidget *child = l->data;
1356 if (!gtk_widget_get_child_visible (child))
1359 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1362 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1366 g_list_free (children);
1370 search_text_notify_cb (EmpathyLiveSearch *search,
1372 EmpathyRosterView *self)
1374 egg_list_box_refilter (EGG_LIST_BOX (self));
1376 select_first_contact (self);
1380 search_activate_cb (GtkWidget *search,
1381 EmpathyRosterView *self)
1383 EggListBox *box = EGG_LIST_BOX (self);
1386 child = egg_list_box_get_selected_child (box);
1390 empathy_roster_view_child_activated (box, child);
1394 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1395 EmpathyLiveSearch *search)
1397 if (self->priv->search != NULL)
1399 g_signal_handlers_disconnect_by_func (self->priv->search,
1400 search_text_notify_cb, self);
1401 g_signal_handlers_disconnect_by_func (self->priv->search,
1402 search_activate_cb, self);
1404 g_clear_object (&self->priv->search);
1410 self->priv->search = g_object_ref (search);
1412 g_signal_connect (self->priv->search, "notify::text",
1413 G_CALLBACK (search_text_notify_cb), self);
1414 g_signal_connect (self->priv->search, "activate",
1415 G_CALLBACK (search_activate_cb), self);
1419 empathy_roster_view_is_empty (EmpathyRosterView *self)
1421 return self->priv->empty;
1425 empathy_roster_view_is_searching (EmpathyRosterView *self)
1427 return (self->priv->search != NULL &&
1428 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1431 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1432 * Empathy's internals. */
1434 empathy_roster_view_add_event (EmpathyRosterView *self,
1435 FolksIndividual *individual,
1439 GHashTable *contacts;
1441 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1442 if (contacts == NULL)
1445 self->priv->last_event_id++;
1447 g_queue_push_head (self->priv->events,
1448 event_new (self->priv->last_event_id, individual, icon, user_data));
1450 start_flashing (self);
1452 return self->priv->last_event_id;
1456 empathy_roster_view_remove_event (EmpathyRosterView *self,
1461 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1462 l = g_list_next (l))
1464 Event *event = l->data;
1466 if (event->id == event_id)
1468 remove_event (self, event);