2 #include "empathy-roster-view.h"
4 #include <glib/gi18n-lib.h>
6 #include "empathy-contact-groups.h"
7 #include "empathy-roster-contact.h"
8 #include "empathy-roster-group.h"
9 #include "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
16 /* Delay in milliseconds between the last stroke on the keyboard and the start
17 * of the live search. */
18 #define SEARCH_TIMEOUT 500
31 SIG_INDIVIDUAL_ACTIVATED,
32 SIG_POPUP_INDIVIDUAL_MENU,
34 SIG_INDIVIDUAL_TOOLTIP,
38 static guint signals[LAST_SIGNAL];
40 #define NO_GROUP "X-no-group"
42 struct _EmpathyRosterViewPriv
44 /* FolksIndividual (borrowed) -> GHashTable (
45 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
47 * When not using groups, this hash just have one element mapped
48 * from the special NO_GROUP key. We could use it as a set but
49 * I prefer to stay coherent in the way this hash is managed.
51 GHashTable *roster_contacts;
52 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
53 GHashTable *roster_groups;
54 /* Hash of the EmpathyRosterContact currently displayed */
55 GHashTable *displayed_contacts;
58 /* queue of (Event *). The most recent events are in the head of the queue
59 * so we always display the icon of the oldest one. */
62 gboolean display_flash_event;
66 gboolean show_offline;
70 EmpathyLiveSearch *search;
72 EmpathyRosterModel *model;
78 FolksIndividual *individual;
85 FolksIndividual *individual,
89 Event *event = g_slice_new (Event);
92 event->individual = g_object_ref (individual);
93 event->icon = g_strdup (icon);
94 event->user_data = user_data;
99 event_free (gpointer data)
102 g_object_unref (event->individual);
103 g_free (event->icon);
105 g_slice_free (Event, event);
109 empathy_roster_view_get_property (GObject *object,
114 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
119 g_value_set_object (value, self->priv->model);
121 case PROP_SHOW_OFFLINE:
122 g_value_set_boolean (value, self->priv->show_offline);
124 case PROP_SHOW_GROUPS:
125 g_value_set_boolean (value, self->priv->show_groups);
128 g_value_set_boolean (value, self->priv->empty);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
137 empathy_roster_view_set_property (GObject *object,
142 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
147 g_assert (self->priv->model == NULL);
148 self->priv->model = g_value_dup_object (value);
150 case PROP_SHOW_OFFLINE:
151 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
153 case PROP_SHOW_GROUPS:
154 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 roster_contact_changed_cb (GtkWidget *child,
165 EmpathyRosterView *self)
167 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
171 add_roster_contact (EmpathyRosterView *self,
172 FolksIndividual *individual,
177 contact = empathy_roster_contact_new (individual, group);
179 /* Need to refilter if online is changed */
180 g_signal_connect (contact, "notify::online",
181 G_CALLBACK (roster_contact_changed_cb), self);
183 /* Need to resort if alias is changed */
184 g_signal_connect (contact, "notify::alias",
185 G_CALLBACK (roster_contact_changed_cb), self);
187 gtk_widget_show (contact);
188 gtk_container_add (GTK_CONTAINER (self), contact);
194 group_expanded_cb (EmpathyRosterGroup *group,
196 EmpathyRosterView *self)
200 widgets = empathy_roster_group_get_widgets (group);
201 for (l = widgets; l != NULL; l = g_list_next (l))
203 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
206 g_list_free (widgets);
208 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
209 gtk_expander_get_expanded (GTK_EXPANDER (group)));
212 static EmpathyRosterGroup *
213 lookup_roster_group (EmpathyRosterView *self,
216 return g_hash_table_lookup (self->priv->roster_groups, group);
219 static EmpathyRosterGroup *
220 ensure_roster_group (EmpathyRosterView *self,
223 GtkWidget *roster_group;
225 roster_group = (GtkWidget *) lookup_roster_group (self, group);
226 if (roster_group != NULL)
227 return EMPATHY_ROSTER_GROUP (roster_group);
229 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
230 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
231 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
232 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
234 roster_group = empathy_roster_group_new (group, NULL);
236 gtk_expander_set_expanded (GTK_EXPANDER (roster_group),
237 empathy_contact_group_get_expanded (group));
239 g_signal_connect (roster_group, "notify::expanded",
240 G_CALLBACK (group_expanded_cb), self);
242 gtk_widget_show (roster_group);
243 gtk_container_add (GTK_CONTAINER (self), roster_group);
245 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
248 return EMPATHY_ROSTER_GROUP (roster_group);
252 update_empty (EmpathyRosterView *self,
255 if (self->priv->empty == empty)
258 self->priv->empty = empty;
259 g_object_notify (G_OBJECT (self), "empty");
262 static gboolean filter_group (EmpathyRosterView *self,
263 EmpathyRosterGroup *group);
266 at_least_one_group_displayed (EmpathyRosterView *self)
271 g_hash_table_iter_init (&iter, self->priv->roster_groups);
272 while (g_hash_table_iter_next (&iter, NULL, &v))
274 EmpathyRosterGroup *group = EMPATHY_ROSTER_GROUP (v);
276 if (filter_group (self, group))
284 check_if_empty (EmpathyRosterView *self)
286 /* Roster is considered as empty if there is no contact *and* no group
287 * currently displayed. */
288 if (g_hash_table_size (self->priv->displayed_contacts) != 0 ||
289 at_least_one_group_displayed (self))
291 update_empty (self, FALSE);
295 update_empty (self, TRUE);
299 update_group_widgets (EmpathyRosterView *self,
300 EmpathyRosterGroup *group,
301 EmpathyRosterContact *contact,
304 guint old_count, count;
306 old_count = empathy_roster_group_get_widgets_count (group);
309 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
311 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
313 if (count != old_count)
315 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
317 check_if_empty (self);
322 add_to_group (EmpathyRosterView *self,
323 FolksIndividual *individual,
327 GHashTable *contacts;
328 EmpathyRosterGroup *roster_group = NULL;
330 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
331 if (contacts == NULL)
334 if (g_hash_table_lookup (contacts, group) != NULL)
337 if (tp_strdiff (group, NO_GROUP))
338 roster_group = ensure_roster_group (self, group);
340 contact = add_roster_contact (self, individual, group);
341 g_hash_table_insert (contacts, g_strdup (group), contact);
343 if (roster_group != NULL)
345 update_group_widgets (self, roster_group,
346 EMPATHY_ROSTER_CONTACT (contact), TRUE);
351 individual_favourite_change_cb (FolksIndividual *individual,
353 EmpathyRosterView *self)
355 /* We may have to refilter the contact as only favorite contacts are always
356 * displayed regardless of their presence. */
357 GHashTable *contacts;
360 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
361 if (contacts == NULL)
364 if (self->priv->show_groups)
365 contact = g_hash_table_lookup (contacts,
366 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
368 contact = g_hash_table_lookup (contacts, NO_GROUP);
373 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
377 individual_added (EmpathyRosterView *self,
378 FolksIndividual *individual)
380 GHashTable *contacts;
382 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
383 if (contacts != NULL)
386 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
388 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
390 if (!self->priv->show_groups)
392 add_to_group (self, individual, NO_GROUP);
398 groups = empathy_roster_model_dup_groups_for_individual (self->priv->model,
401 if (g_list_length (groups) > 0)
403 for (l = groups; l != NULL; l = g_list_next (l))
405 add_to_group (self, individual, l->data);
410 /* No group, adds to Ungrouped */
411 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
414 g_list_free_full (groups, g_free);
417 tp_g_signal_connect_object (individual, "notify::is-favourite",
418 G_CALLBACK (individual_favourite_change_cb), self, 0);
422 set_event_icon_on_individual (EmpathyRosterView *self,
423 FolksIndividual *individual,
426 GHashTable *contacts;
430 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
431 if (contacts == NULL)
434 g_hash_table_iter_init (&iter, contacts);
435 while (g_hash_table_iter_next (&iter, NULL, &v))
437 EmpathyRosterContact *contact =v;
439 empathy_roster_contact_set_event_icon (contact, icon);
444 flash_event (Event *event,
445 EmpathyRosterView *self)
447 set_event_icon_on_individual (self, event->individual, event->icon);
451 unflash_event (Event *event,
452 EmpathyRosterView *self)
454 set_event_icon_on_individual (self, event->individual, NULL);
458 flash_cb (gpointer data)
460 EmpathyRosterView *self = data;
462 if (self->priv->display_flash_event)
464 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
465 self->priv->display_flash_event = FALSE;
469 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
470 self->priv->display_flash_event = TRUE;
477 start_flashing (EmpathyRosterView *self)
479 if (self->priv->flash_id != 0)
482 self->priv->display_flash_event = TRUE;
484 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
489 stop_flashing (EmpathyRosterView *self)
491 if (self->priv->flash_id == 0)
494 g_source_remove (self->priv->flash_id);
495 self->priv->flash_id = 0;
499 remove_event (EmpathyRosterView *self,
502 unflash_event (event, self);
503 g_queue_remove (self->priv->events, event);
505 if (g_queue_get_length (self->priv->events) == 0)
507 stop_flashing (self);
512 remove_all_individual_event (EmpathyRosterView *self,
513 FolksIndividual *individual)
517 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
520 Event *event = l->data;
522 if (event->individual == individual)
524 remove_event (self, event);
531 individual_removed (EmpathyRosterView *self,
532 FolksIndividual *individual)
534 GHashTable *contacts;
538 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
539 if (contacts == NULL)
542 remove_all_individual_event (self, individual);
544 g_hash_table_iter_init (&iter, contacts);
545 while (g_hash_table_iter_next (&iter, &key, &value))
547 const gchar *group_name = key;
548 GtkWidget *contact = value;
549 EmpathyRosterGroup *group;
551 group = lookup_roster_group (self, group_name);
554 update_group_widgets (self, group,
555 EMPATHY_ROSTER_CONTACT (contact), FALSE);
558 gtk_container_remove (GTK_CONTAINER (self), contact);
561 g_hash_table_remove (self->priv->roster_contacts, individual);
565 individual_added_cb (EmpathyRosterModel *model,
566 FolksIndividual *individual,
567 EmpathyRosterView *self)
569 individual_added (self, individual);
573 individual_removed_cb (EmpathyRosterModel *model,
574 FolksIndividual *individual,
575 EmpathyRosterView *self)
577 individual_removed (self, individual);
581 contact_in_top (EmpathyRosterView *self,
582 EmpathyRosterContact *contact)
584 if (!self->priv->show_groups)
586 /* Always display top contacts in non-group mode. */
588 FolksIndividual *individual;
589 gboolean result = FALSE;
591 individual = empathy_roster_contact_get_individual (contact);
593 groups = empathy_roster_model_dup_groups_for_individual (
594 self->priv->model, individual);
596 if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
597 (GCompareFunc) g_strcmp0) != NULL)
600 g_list_free_full (groups, g_free);
605 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
606 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
607 /* If we are displaying contacts, we only want to *always* display the
608 * RosterContact which is displayed at the top; not the ones displayed in
609 * the 'normal' group sections */
616 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
617 EmpathyRosterContact *b)
619 FolksIndividual *ind_a, *ind_b;
620 const gchar *alias_a, *alias_b;
622 ind_a = empathy_roster_contact_get_individual (a);
623 ind_b = empathy_roster_contact_get_individual (b);
625 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
626 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
628 return g_ascii_strcasecmp (alias_a, alias_b);
632 compare_roster_contacts_no_group (EmpathyRosterView *self,
633 EmpathyRosterContact *a,
634 EmpathyRosterContact *b)
636 gboolean top_a, top_b;
638 top_a = contact_in_top (self, a);
639 top_b = contact_in_top (self, b);
642 /* Both contacts are in the top of the roster (or not). Sort them
644 return compare_roster_contacts_by_alias (a, b);
652 compare_group_names (const gchar *group_a,
653 const gchar *group_b)
655 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
658 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
661 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
663 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
666 return g_ascii_strcasecmp (group_a, group_b);
670 compare_roster_contacts_with_groups (EmpathyRosterView *self,
671 EmpathyRosterContact *a,
672 EmpathyRosterContact *b)
674 const gchar *group_a, *group_b;
676 group_a = empathy_roster_contact_get_group (a);
677 group_b = empathy_roster_contact_get_group (b);
679 if (!tp_strdiff (group_a, group_b))
680 /* Same group, compare the contacts */
681 return compare_roster_contacts_by_alias (a, b);
684 return compare_group_names (group_a, group_b);
688 compare_roster_contacts (EmpathyRosterView *self,
689 EmpathyRosterContact *a,
690 EmpathyRosterContact *b)
692 if (!self->priv->show_groups)
693 return compare_roster_contacts_no_group (self, a, b);
695 return compare_roster_contacts_with_groups (self, a, b);
699 compare_roster_groups (EmpathyRosterGroup *a,
700 EmpathyRosterGroup *b)
702 const gchar *name_a, *name_b;
704 name_a = empathy_roster_group_get_name (a);
705 name_b = empathy_roster_group_get_name (b);
707 return compare_group_names (name_a, name_b);
711 compare_contact_group (EmpathyRosterContact *contact,
712 EmpathyRosterGroup *group)
714 const char *contact_group, *group_name;
716 contact_group = empathy_roster_contact_get_group (contact);
717 group_name = empathy_roster_group_get_name (group);
719 if (!tp_strdiff (contact_group, group_name))
720 /* @contact is in @group, @group has to be displayed first */
723 /* @contact is in a different group, sort by group name */
724 return compare_group_names (contact_group, group_name);
728 roster_view_sort (gconstpointer a,
732 EmpathyRosterView *self = user_data;
734 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
735 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
736 EMPATHY_ROSTER_CONTACT (b));
737 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
738 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
739 EMPATHY_ROSTER_GROUP (b));
740 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
741 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
742 EMPATHY_ROSTER_GROUP (b));
743 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
744 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
745 EMPATHY_ROSTER_GROUP (a));
747 g_return_val_if_reached (0);
751 update_separator (GtkWidget **separator,
758 /* No separator before the first row */
759 g_clear_object (separator);
763 if (*separator != NULL)
766 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
767 g_object_ref_sink (*separator);
771 is_searching (EmpathyRosterView *self)
773 if (self->priv->search == NULL)
776 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
780 add_to_displayed (EmpathyRosterView *self,
781 EmpathyRosterContact *contact)
783 FolksIndividual *individual;
784 GHashTable *contacts;
788 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
791 g_hash_table_add (self->priv->displayed_contacts, contact);
792 update_empty (self, FALSE);
794 /* Groups of this contact may now be displayed if we just displays the first
795 * child in this group. */
797 if (!self->priv->show_groups)
800 individual = empathy_roster_contact_get_individual (contact);
801 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
802 if (contacts == NULL)
805 g_hash_table_iter_init (&iter, contacts);
806 while (g_hash_table_iter_next (&iter, &k, NULL))
808 const gchar *group_name = k;
811 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
815 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
820 remove_from_displayed (EmpathyRosterView *self,
821 EmpathyRosterContact *contact)
823 g_hash_table_remove (self->priv->displayed_contacts, contact);
825 check_if_empty (self);
829 contact_is_favorite (EmpathyRosterContact *contact)
831 FolksIndividual *individual;
833 individual = empathy_roster_contact_get_individual (contact);
835 return folks_favourite_details_get_is_favourite (
836 FOLKS_FAVOURITE_DETAILS (individual));
840 * check if @contact should be displayed according to @self's current status
841 * and without consideration for the state of @contact's groups.
844 contact_should_be_displayed (EmpathyRosterView *self,
845 EmpathyRosterContact *contact)
847 if (is_searching (self))
849 FolksIndividual *individual;
851 individual = empathy_roster_contact_get_individual (contact);
853 return empathy_individual_match_string (individual,
854 empathy_live_search_get_text (self->priv->search),
855 empathy_live_search_get_words (self->priv->search));
858 if (self->priv->show_offline)
861 if (contact_in_top (self, contact) &&
862 contact_is_favorite (contact))
863 /* Favorite top contacts are always displayed */
866 return empathy_roster_contact_is_online (contact);
871 filter_contact (EmpathyRosterView *self,
872 EmpathyRosterContact *contact)
876 displayed = contact_should_be_displayed (self, contact);
878 if (self->priv->show_groups)
880 const gchar *group_name;
881 EmpathyRosterGroup *group;
883 group_name = empathy_roster_contact_get_group (contact);
884 group = lookup_roster_group (self, group_name);
888 /* When searching, always display even if the group is closed */
889 if (!is_searching (self) &&
890 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
897 add_to_displayed (self, contact);
901 remove_from_displayed (self, contact);
908 filter_group (EmpathyRosterView *self,
909 EmpathyRosterGroup *group)
912 gboolean result = FALSE;
914 /* Display the group if it contains at least one displayed contact */
915 widgets = empathy_roster_group_get_widgets (group);
916 for (l = widgets; l != NULL; l = g_list_next (l))
918 EmpathyRosterContact *contact = l->data;
920 if (contact_should_be_displayed (self, contact))
927 g_list_free (widgets);
933 filter_list (GtkWidget *child,
936 EmpathyRosterView *self = user_data;
938 if (EMPATHY_IS_ROSTER_CONTACT (child))
939 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
941 else if (EMPATHY_IS_ROSTER_GROUP (child))
942 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
944 g_return_val_if_reached (FALSE);
948 populate_view (EmpathyRosterView *self)
950 GList *individuals, *l;
952 individuals = empathy_roster_model_get_individuals (self->priv->model);
953 for (l = individuals; l != NULL; l = g_list_next (l))
955 FolksIndividual *individual = l->data;
957 individual_added (self, individual);
960 g_list_free (individuals);
964 remove_from_group (EmpathyRosterView *self,
965 FolksIndividual *individual,
968 GHashTable *contacts;
970 EmpathyRosterGroup *roster_group;
972 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
973 if (contacts == NULL)
976 contact = g_hash_table_lookup (contacts, group);
980 g_hash_table_remove (contacts, group);
982 if (g_hash_table_size (contacts) == 0)
984 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
987 roster_group = lookup_roster_group (self, group);
989 if (roster_group != NULL)
991 update_group_widgets (self, roster_group,
992 EMPATHY_ROSTER_CONTACT (contact), FALSE);
995 gtk_container_remove (GTK_CONTAINER (self), contact);
999 groups_changed_cb (EmpathyRosterModel *model,
1000 FolksIndividual *individual,
1003 EmpathyRosterView *self)
1005 if (!self->priv->show_groups)
1007 egg_list_box_resort (EGG_LIST_BOX (self));
1013 add_to_group (self, individual, group);
1017 remove_from_group (self, individual, group);
1022 empathy_roster_view_constructed (GObject *object)
1024 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1025 void (*chain_up) (GObject *) =
1026 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1028 if (chain_up != NULL)
1031 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1033 /* Get saved group states. */
1034 empathy_contact_groups_get_all ();
1036 populate_view (self);
1038 tp_g_signal_connect_object (self->priv->model, "individual-added",
1039 G_CALLBACK (individual_added_cb), self, 0);
1040 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1041 G_CALLBACK (individual_removed_cb), self, 0);
1042 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1043 G_CALLBACK (groups_changed_cb), self, 0);
1045 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1046 roster_view_sort, self, NULL);
1048 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1051 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1053 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1057 clear_view (EmpathyRosterView *self)
1059 g_hash_table_remove_all (self->priv->roster_contacts);
1060 g_hash_table_remove_all (self->priv->roster_groups);
1061 g_hash_table_remove_all (self->priv->displayed_contacts);
1063 gtk_container_foreach (GTK_CONTAINER (self),
1064 (GtkCallback) gtk_widget_destroy, NULL);
1068 empathy_roster_view_dispose (GObject *object)
1070 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1071 void (*chain_up) (GObject *) =
1072 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1074 /* Start by clearing the view so our internal hash tables are cleared from
1075 * objects being destroyed. */
1078 stop_flashing (self);
1080 empathy_roster_view_set_live_search (self, NULL);
1081 g_clear_object (&self->priv->model);
1083 if (self->priv->search_id != 0)
1085 g_source_remove (self->priv->search_id);
1086 self->priv->search_id = 0;
1089 if (chain_up != NULL)
1094 empathy_roster_view_finalize (GObject *object)
1096 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1097 void (*chain_up) (GObject *) =
1098 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1100 g_hash_table_unref (self->priv->roster_contacts);
1101 g_hash_table_unref (self->priv->roster_groups);
1102 g_hash_table_unref (self->priv->displayed_contacts);
1103 g_queue_free_full (self->priv->events, event_free);
1105 if (chain_up != NULL)
1110 empathy_roster_view_child_activated (EggListBox *box,
1113 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1114 EmpathyRosterContact *contact;
1115 FolksIndividual *individual;
1118 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1121 contact = EMPATHY_ROSTER_CONTACT (child);
1122 individual = empathy_roster_contact_get_individual (contact);
1124 /* Activate the oldest event associated with this contact, if any */
1125 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1126 l = g_list_previous (l))
1128 Event *event = l->data;
1130 if (event->individual == individual)
1132 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1138 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1142 fire_popup_individual_menu (EmpathyRosterView *self,
1147 EmpathyRosterContact *contact;
1148 FolksIndividual *individual;
1150 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1153 contact = EMPATHY_ROSTER_CONTACT (child);
1154 individual = empathy_roster_contact_get_individual (contact);
1156 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1157 individual, button, time);
1161 empathy_roster_view_button_press_event (GtkWidget *widget,
1162 GdkEventButton *event)
1164 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1165 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1166 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1168 if (event->button == 3)
1172 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1176 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1178 fire_popup_individual_menu (self, child, event->button, event->time);
1182 return chain_up (widget, event);
1186 empathy_roster_view_key_press_event (GtkWidget *widget,
1189 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1190 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1191 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1193 if (event->keyval == GDK_KEY_Menu)
1197 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1200 fire_popup_individual_menu (self, child, 0, event->time);
1203 return chain_up (widget, event);
1207 * @out_child: (out) (allow-none)
1210 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1212 GtkWidget **out_child)
1216 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1218 if (out_child != NULL)
1221 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1224 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1228 * @out_child: (out) (allow-none)
1231 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1236 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1238 if (EMPATHY_IS_ROSTER_CONTACT (child))
1239 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1240 else if (EMPATHY_IS_ROSTER_GROUP (child))
1241 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1247 empathy_roster_view_query_tooltip (GtkWidget *widget,
1250 gboolean keyboard_mode,
1251 GtkTooltip *tooltip)
1253 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1254 FolksIndividual *individual;
1258 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1259 if (individual == NULL)
1262 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1263 individual, keyboard_mode, tooltip, &result);
1267 GtkAllocation allocation;
1269 gtk_widget_get_allocation (child, &allocation);
1270 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1277 empathy_roster_view_remove (GtkContainer *container,
1280 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1281 void (*chain_up) (GtkContainer *, GtkWidget *) =
1282 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1284 chain_up (container, widget);
1286 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1287 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1291 empathy_roster_view_class_init (
1292 EmpathyRosterViewClass *klass)
1294 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1295 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1296 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1297 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1300 oclass->get_property = empathy_roster_view_get_property;
1301 oclass->set_property = empathy_roster_view_set_property;
1302 oclass->constructed = empathy_roster_view_constructed;
1303 oclass->dispose = empathy_roster_view_dispose;
1304 oclass->finalize = empathy_roster_view_finalize;
1306 widget_class->button_press_event = empathy_roster_view_button_press_event;
1307 widget_class->key_press_event = empathy_roster_view_key_press_event;
1308 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1310 container_class->remove = empathy_roster_view_remove;
1312 box_class->child_activated = empathy_roster_view_child_activated;
1314 spec = g_param_spec_object ("model", "Model",
1315 "EmpathyRosterModel",
1316 EMPATHY_TYPE_ROSTER_MODEL,
1317 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1318 g_object_class_install_property (oclass, PROP_MODEL, spec);
1320 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1321 "Show offline contacts",
1323 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1324 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1326 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1330 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1332 spec = g_param_spec_boolean ("empty", "Empty",
1333 "Is the view currently empty?",
1335 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1336 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1338 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1339 G_OBJECT_CLASS_TYPE (klass),
1341 0, NULL, NULL, NULL,
1343 1, FOLKS_TYPE_INDIVIDUAL);
1345 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1346 G_OBJECT_CLASS_TYPE (klass),
1348 0, NULL, NULL, NULL,
1350 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1352 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1353 G_OBJECT_CLASS_TYPE (klass),
1355 0, NULL, NULL, NULL,
1357 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1359 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1360 G_OBJECT_CLASS_TYPE (klass),
1362 0, g_signal_accumulator_true_handled, NULL, NULL,
1364 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1366 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1370 empathy_roster_view_init (EmpathyRosterView *self)
1372 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1373 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1375 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1376 NULL, (GDestroyNotify) g_hash_table_unref);
1377 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1379 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1381 self->priv->events = g_queue_new ();
1383 self->priv->empty = TRUE;
1387 empathy_roster_view_new (EmpathyRosterModel *model)
1389 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1391 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1397 empathy_roster_view_show_offline (EmpathyRosterView *self,
1400 if (self->priv->show_offline == show)
1403 self->priv->show_offline = show;
1404 egg_list_box_refilter (EGG_LIST_BOX (self));
1406 g_object_notify (G_OBJECT (self), "show-offline");
1410 empathy_roster_view_show_groups (EmpathyRosterView *self,
1413 if (self->priv->show_groups == show)
1416 self->priv->show_groups = show;
1418 /* TODO: block sort/filter? */
1420 populate_view (self);
1422 g_object_notify (G_OBJECT (self), "show-groups");
1426 select_first_contact (EmpathyRosterView *self)
1428 GList *children, *l;
1430 children = gtk_container_get_children (GTK_CONTAINER (self));
1431 for (l = children; l != NULL; l = g_list_next (l))
1433 GtkWidget *child = l->data;
1435 if (!gtk_widget_get_child_visible (child))
1438 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1441 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1445 g_list_free (children);
1449 search_timeout_cb (EmpathyRosterView *self)
1451 egg_list_box_refilter (EGG_LIST_BOX (self));
1453 select_first_contact (self);
1455 self->priv->search_id = 0;
1456 return G_SOURCE_REMOVE;
1460 search_text_notify_cb (EmpathyLiveSearch *search,
1462 EmpathyRosterView *self)
1464 if (self->priv->search_id != 0)
1465 g_source_remove (self->priv->search_id);
1467 self->priv->search_id = g_timeout_add (SEARCH_TIMEOUT,
1468 (GSourceFunc) search_timeout_cb, self);
1472 search_activate_cb (GtkWidget *search,
1473 EmpathyRosterView *self)
1475 EggListBox *box = EGG_LIST_BOX (self);
1478 child = egg_list_box_get_selected_child (box);
1482 empathy_roster_view_child_activated (box, child);
1486 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1487 EmpathyLiveSearch *search)
1489 if (self->priv->search != NULL)
1491 g_signal_handlers_disconnect_by_func (self->priv->search,
1492 search_text_notify_cb, self);
1493 g_signal_handlers_disconnect_by_func (self->priv->search,
1494 search_activate_cb, self);
1496 g_clear_object (&self->priv->search);
1502 self->priv->search = g_object_ref (search);
1504 g_signal_connect (self->priv->search, "notify::text",
1505 G_CALLBACK (search_text_notify_cb), self);
1506 g_signal_connect (self->priv->search, "activate",
1507 G_CALLBACK (search_activate_cb), self);
1511 empathy_roster_view_is_empty (EmpathyRosterView *self)
1513 return self->priv->empty;
1517 empathy_roster_view_is_searching (EmpathyRosterView *self)
1519 return (self->priv->search != NULL &&
1520 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1523 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1524 * Empathy's internals. */
1526 empathy_roster_view_add_event (EmpathyRosterView *self,
1527 FolksIndividual *individual,
1531 GHashTable *contacts;
1533 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1534 if (contacts == NULL)
1537 self->priv->last_event_id++;
1539 g_queue_push_head (self->priv->events,
1540 event_new (self->priv->last_event_id, individual, icon, user_data));
1542 start_flashing (self);
1544 return self->priv->last_event_id;
1548 empathy_roster_view_remove_event (EmpathyRosterView *self,
1553 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1554 l = g_list_next (l))
1556 Event *event = l->data;
1558 if (event->id == event_id)
1560 remove_event (self, event);
1567 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1571 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1573 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1576 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));