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 #include <libempathy/empathy-utils.h>
13 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
15 /* Flashing delay for icons (milliseconds). */
16 #define FLASH_TIMEOUT 500
29 SIG_INDIVIDUAL_ACTIVATED,
30 SIG_POPUP_INDIVIDUAL_MENU,
32 SIG_INDIVIDUAL_TOOLTIP,
36 static guint signals[LAST_SIGNAL];
38 #define NO_GROUP "X-no-group"
40 struct _EmpathyRosterViewPriv
42 /* FolksIndividual (borrowed) -> GHashTable (
43 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
45 * When not using groups, this hash just have one element mapped
46 * from the special NO_GROUP key. We could use it as a set but
47 * I prefer to stay coherent in the way this hash is managed.
49 GHashTable *roster_contacts;
50 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
51 GHashTable *roster_groups;
52 /* Hash of the EmpathyRosterContact currently displayed */
53 GHashTable *displayed_contacts;
56 /* queue of (Event *). The most recent events are in the head of the queue
57 * so we always display the icon of the oldest one. */
60 gboolean display_flash_event;
62 gboolean show_offline;
66 EmpathyLiveSearch *search;
68 EmpathyRosterModel *model;
74 FolksIndividual *individual;
81 FolksIndividual *individual,
85 Event *event = g_slice_new (Event);
88 event->individual = g_object_ref (individual);
89 event->icon = g_strdup (icon);
90 event->user_data = user_data;
95 event_free (gpointer data)
98 g_object_unref (event->individual);
101 g_slice_free (Event, event);
105 empathy_roster_view_get_property (GObject *object,
110 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
115 g_value_set_object (value, self->priv->model);
117 case PROP_SHOW_OFFLINE:
118 g_value_set_boolean (value, self->priv->show_offline);
120 case PROP_SHOW_GROUPS:
121 g_value_set_boolean (value, self->priv->show_groups);
124 g_value_set_boolean (value, self->priv->empty);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 empathy_roster_view_set_property (GObject *object,
138 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
143 g_assert (self->priv->model == NULL);
144 self->priv->model = g_value_dup_object (value);
146 case PROP_SHOW_OFFLINE:
147 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
149 case PROP_SHOW_GROUPS:
150 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 roster_contact_changed_cb (GtkWidget *child,
161 EmpathyRosterView *self)
163 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
167 add_roster_contact (EmpathyRosterView *self,
168 FolksIndividual *individual,
173 contact = empathy_roster_contact_new (individual, group);
175 /* Need to refilter if online is changed */
176 g_signal_connect (contact, "notify::online",
177 G_CALLBACK (roster_contact_changed_cb), self);
179 /* Need to resort if alias is changed */
180 g_signal_connect (contact, "notify::alias",
181 G_CALLBACK (roster_contact_changed_cb), self);
183 gtk_widget_show (contact);
184 gtk_container_add (GTK_CONTAINER (self), contact);
190 group_expanded_cb (EmpathyRosterGroup *group,
192 EmpathyRosterView *self)
196 widgets = empathy_roster_group_get_widgets (group);
197 for (l = widgets; l != NULL; l = g_list_next (l))
199 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
202 g_list_free (widgets);
205 static EmpathyRosterGroup *
206 lookup_roster_group (EmpathyRosterView *self,
209 return g_hash_table_lookup (self->priv->roster_groups, group);
212 static EmpathyRosterGroup *
213 ensure_roster_group (EmpathyRosterView *self,
216 GtkWidget *roster_group;
218 roster_group = (GtkWidget *) lookup_roster_group (self, group);
219 if (roster_group != NULL)
220 return EMPATHY_ROSTER_GROUP (roster_group);
222 if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
223 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
224 else if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY))
225 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
227 roster_group = empathy_roster_group_new (group, NULL);
229 g_signal_connect (roster_group, "notify::expanded",
230 G_CALLBACK (group_expanded_cb), self);
232 gtk_widget_show (roster_group);
233 gtk_container_add (GTK_CONTAINER (self), roster_group);
235 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
238 return EMPATHY_ROSTER_GROUP (roster_group);
242 update_group_widgets (EmpathyRosterView *self,
243 EmpathyRosterGroup *group,
244 EmpathyRosterContact *contact,
247 guint old_count, count;
249 old_count = empathy_roster_group_get_widgets_count (group);
252 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
254 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
256 if (count != old_count)
257 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
261 add_to_group (EmpathyRosterView *self,
262 FolksIndividual *individual,
266 GHashTable *contacts;
267 EmpathyRosterGroup *roster_group = NULL;
269 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
270 if (contacts == NULL)
273 if (tp_strdiff (group, NO_GROUP))
274 roster_group = ensure_roster_group (self, group);
276 contact = add_roster_contact (self, individual, group);
277 g_hash_table_insert (contacts, g_strdup (group), contact);
279 if (roster_group != NULL)
281 update_group_widgets (self, roster_group,
282 EMPATHY_ROSTER_CONTACT (contact), TRUE);
287 individual_added (EmpathyRosterView *self,
288 FolksIndividual *individual)
290 GHashTable *contacts;
292 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
293 if (contacts != NULL)
296 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
298 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
300 if (!self->priv->show_groups)
302 add_to_group (self, individual, NO_GROUP);
309 tops = empathy_roster_model_get_top_individuals (self->priv->model);
311 if (folks_favourite_details_get_is_favourite (
312 FOLKS_FAVOURITE_DETAILS (individual)) ||
313 g_list_index (tops, individual) != -1)
315 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
318 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
321 if (g_list_length (groups) > 0)
323 for (l = groups; l != NULL; l = g_list_next (l))
325 add_to_group (self, individual, l->data);
330 /* No group, adds to Ungrouped */
331 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
334 g_list_free (groups);
339 set_event_icon_on_individual (EmpathyRosterView *self,
340 FolksIndividual *individual,
343 GHashTable *contacts;
347 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
348 if (contacts == NULL)
351 g_hash_table_iter_init (&iter, contacts);
352 while (g_hash_table_iter_next (&iter, NULL, &v))
354 EmpathyRosterContact *contact =v;
356 empathy_roster_contact_set_event_icon (contact, icon);
361 flash_event (Event *event,
362 EmpathyRosterView *self)
364 set_event_icon_on_individual (self, event->individual, event->icon);
368 unflash_event (Event *event,
369 EmpathyRosterView *self)
371 set_event_icon_on_individual (self, event->individual, NULL);
375 flash_cb (gpointer data)
377 EmpathyRosterView *self = data;
379 if (self->priv->display_flash_event)
381 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
382 self->priv->display_flash_event = FALSE;
386 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
387 self->priv->display_flash_event = TRUE;
394 start_flashing (EmpathyRosterView *self)
396 if (self->priv->flash_id != 0)
399 self->priv->display_flash_event = TRUE;
401 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
406 stop_flashing (EmpathyRosterView *self)
408 if (self->priv->flash_id == 0)
411 g_source_remove (self->priv->flash_id);
412 self->priv->flash_id = 0;
416 remove_event (EmpathyRosterView *self,
419 unflash_event (event, self);
420 g_queue_remove (self->priv->events, event);
422 if (g_queue_get_length (self->priv->events) == 0)
424 stop_flashing (self);
429 remove_all_individual_event (EmpathyRosterView *self,
430 FolksIndividual *individual)
434 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
437 Event *event = l->data;
439 if (event->individual == individual)
441 remove_event (self, event);
448 individual_removed (EmpathyRosterView *self,
449 FolksIndividual *individual)
451 GHashTable *contacts;
455 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
456 if (contacts == NULL)
459 remove_all_individual_event (self, individual);
461 g_hash_table_iter_init (&iter, contacts);
462 while (g_hash_table_iter_next (&iter, &key, &value))
464 const gchar *group_name = key;
465 GtkWidget *contact = value;
466 EmpathyRosterGroup *group;
468 group = lookup_roster_group (self, group_name);
471 update_group_widgets (self, group,
472 EMPATHY_ROSTER_CONTACT (contact), FALSE);
475 gtk_container_remove (GTK_CONTAINER (self), contact);
478 g_hash_table_remove (self->priv->roster_contacts, individual);
482 individual_added_cb (EmpathyRosterModel *model,
483 FolksIndividual *individual,
484 EmpathyRosterView *self)
486 individual_added (self, individual);
490 individual_removed_cb (EmpathyRosterModel *model,
491 FolksIndividual *individual,
492 EmpathyRosterView *self)
494 individual_removed (self, individual);
498 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
499 EmpathyRosterContact *b)
501 FolksIndividual *ind_a, *ind_b;
502 const gchar *alias_a, *alias_b;
504 ind_a = empathy_roster_contact_get_individual (a);
505 ind_b = empathy_roster_contact_get_individual (b);
507 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
508 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
510 return g_ascii_strcasecmp (alias_a, alias_b);
514 contact_is_favourite (EmpathyRosterContact *contact)
516 FolksIndividual *individual;
518 individual = empathy_roster_contact_get_individual (contact);
520 return folks_favourite_details_get_is_favourite (
521 FOLKS_FAVOURITE_DETAILS (individual));
525 contact_in_top (EmpathyRosterView *self,
526 EmpathyRosterContact *contact)
528 FolksIndividual *individual;
531 if (contact_is_favourite (contact))
534 individual = empathy_roster_contact_get_individual (contact);
536 tops = empathy_roster_model_get_top_individuals (self->priv->model);
538 if (g_list_index (tops, individual) != -1)
545 compare_roster_contacts_no_group (EmpathyRosterView *self,
546 EmpathyRosterContact *a,
547 EmpathyRosterContact *b)
549 gboolean top_a, top_b;
551 top_a = contact_in_top (self, a);
552 top_b = contact_in_top (self, b);
555 /* Both contacts are in the top of the roster (or not). Sort them
557 return compare_roster_contacts_by_alias (a, b);
565 compare_group_names (const gchar *group_a,
566 const gchar *group_b)
568 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
571 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
574 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
576 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
579 return g_ascii_strcasecmp (group_a, group_b);
583 compare_roster_contacts_with_groups (EmpathyRosterView *self,
584 EmpathyRosterContact *a,
585 EmpathyRosterContact *b)
587 const gchar *group_a, *group_b;
589 group_a = empathy_roster_contact_get_group (a);
590 group_b = empathy_roster_contact_get_group (b);
592 if (!tp_strdiff (group_a, group_b))
593 /* Same group, compare the contacts */
594 return compare_roster_contacts_by_alias (a, b);
597 return compare_group_names (group_a, group_b);
601 compare_roster_contacts (EmpathyRosterView *self,
602 EmpathyRosterContact *a,
603 EmpathyRosterContact *b)
605 if (!self->priv->show_groups)
606 return compare_roster_contacts_no_group (self, a, b);
608 return compare_roster_contacts_with_groups (self, a, b);
612 compare_roster_groups (EmpathyRosterGroup *a,
613 EmpathyRosterGroup *b)
615 const gchar *name_a, *name_b;
617 name_a = empathy_roster_group_get_name (a);
618 name_b = empathy_roster_group_get_name (b);
620 return compare_group_names (name_a, name_b);
624 compare_contact_group (EmpathyRosterContact *contact,
625 EmpathyRosterGroup *group)
627 const char *contact_group, *group_name;
629 contact_group = empathy_roster_contact_get_group (contact);
630 group_name = empathy_roster_group_get_name (group);
632 if (!tp_strdiff (contact_group, group_name))
633 /* @contact is in @group, @group has to be displayed first */
636 /* @contact is in a different group, sort by group name */
637 return compare_group_names (contact_group, group_name);
641 roster_view_sort (gconstpointer a,
645 EmpathyRosterView *self = user_data;
647 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
648 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
649 EMPATHY_ROSTER_CONTACT (b));
650 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
651 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
652 EMPATHY_ROSTER_GROUP (b));
653 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
654 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
655 EMPATHY_ROSTER_GROUP (b));
656 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
657 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
658 EMPATHY_ROSTER_GROUP (a));
660 g_return_val_if_reached (0);
664 update_separator (GtkWidget **separator,
671 /* No separator before the first row */
672 g_clear_object (separator);
676 if (*separator != NULL)
679 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
680 g_object_ref_sink (*separator);
684 is_searching (EmpathyRosterView *self)
686 if (self->priv->search == NULL)
689 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
693 update_empty (EmpathyRosterView *self,
696 if (self->priv->empty == empty)
699 self->priv->empty = empty;
700 g_object_notify (G_OBJECT (self), "empty");
704 add_to_displayed (EmpathyRosterView *self,
705 EmpathyRosterContact *contact)
707 FolksIndividual *individual;
708 GHashTable *contacts;
712 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
715 g_hash_table_add (self->priv->displayed_contacts, contact);
716 update_empty (self, FALSE);
718 /* Groups of this contact may now be displayed if we just displays the first
719 * child in this group. */
721 if (!self->priv->show_groups)
724 individual = empathy_roster_contact_get_individual (contact);
725 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
726 if (contacts == NULL)
729 g_hash_table_iter_init (&iter, contacts);
730 while (g_hash_table_iter_next (&iter, &k, NULL))
732 const gchar *group_name = k;
735 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
739 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
744 remove_from_displayed (EmpathyRosterView *self,
745 EmpathyRosterContact *contact)
747 g_hash_table_remove (self->priv->displayed_contacts, contact);
749 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
750 update_empty (self, TRUE);
754 * check if @contact should be displayed according to @self's current status
755 * and without consideration for the state of @contact's groups.
758 contact_should_be_displayed (EmpathyRosterView *self,
759 EmpathyRosterContact *contact)
761 if (is_searching (self))
763 FolksIndividual *individual;
765 individual = empathy_roster_contact_get_individual (contact);
767 return empathy_individual_match_string (individual,
768 empathy_live_search_get_text (self->priv->search),
769 empathy_live_search_get_words (self->priv->search));
772 if (self->priv->show_offline)
775 if (contact_is_favourite (contact))
777 const gchar *group_name;
779 if (!self->priv->show_groups)
780 /* Always display favourite contacts in non-group mode. */
783 group_name = empathy_roster_contact_get_group (contact);
785 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
786 /* Always display favourite contact in group mode only in the
791 return empathy_roster_contact_is_online (contact);
796 filter_contact (EmpathyRosterView *self,
797 EmpathyRosterContact *contact)
801 displayed = contact_should_be_displayed (self, contact);
803 if (self->priv->show_groups)
805 const gchar *group_name;
806 EmpathyRosterGroup *group;
808 group_name = empathy_roster_contact_get_group (contact);
809 group = lookup_roster_group (self, group_name);
813 /* When searching, always display even if the group is closed */
814 if (!is_searching (self) &&
815 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
822 add_to_displayed (self, contact);
826 remove_from_displayed (self, contact);
833 filter_group (EmpathyRosterView *self,
834 EmpathyRosterGroup *group)
838 /* Display the group if it contains at least one displayed contact */
839 widgets = empathy_roster_group_get_widgets (group);
840 for (l = widgets; l != NULL; l = g_list_next (l))
842 EmpathyRosterContact *contact = l->data;
844 if (contact_should_be_displayed (self, contact))
852 filter_list (GtkWidget *child,
855 EmpathyRosterView *self = user_data;
857 if (EMPATHY_IS_ROSTER_CONTACT (child))
858 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
860 else if (EMPATHY_IS_ROSTER_GROUP (child))
861 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
863 g_return_val_if_reached (FALSE);
866 /* @list: GList of EmpathyRosterContact
868 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
871 individual_in_list (FolksIndividual *individual,
876 for (l = list; l != NULL; l = g_list_next (l))
878 EmpathyRosterContact *contact = l->data;
880 if (empathy_roster_contact_get_individual (contact) == individual)
888 populate_view (EmpathyRosterView *self)
890 GList *individuals, *l;
892 individuals = empathy_roster_model_get_individuals (self->priv->model);
893 for (l = individuals; l != NULL; l = g_list_next (l))
895 FolksIndividual *individual = l->data;
897 individual_added (self, individual);
900 g_list_free (individuals);
904 remove_from_group (EmpathyRosterView *self,
905 FolksIndividual *individual,
908 GHashTable *contacts;
910 EmpathyRosterGroup *roster_group;
912 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
913 if (contacts == NULL)
916 contact = g_hash_table_lookup (contacts, group);
920 g_hash_table_remove (contacts, group);
922 if (g_hash_table_size (contacts) == 0)
924 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
927 roster_group = lookup_roster_group (self, group);
929 if (roster_group != NULL)
931 update_group_widgets (self, roster_group,
932 EMPATHY_ROSTER_CONTACT (contact), FALSE);
935 gtk_container_remove (GTK_CONTAINER (self), contact);
939 update_top_contacts (EmpathyRosterView *self)
942 GList *to_add = NULL, *to_remove = NULL;
943 EmpathyRosterGroup *group;
945 if (!self->priv->show_groups)
947 egg_list_box_resort (EGG_LIST_BOX (self));
951 tops = empathy_roster_model_get_top_individuals (self->priv->model);
953 group = g_hash_table_lookup (self->priv->roster_groups,
954 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
957 to_add = g_list_copy (tops);
963 contacts = empathy_roster_group_get_widgets (group);
965 /* Check which EmpathyRosterContact have to be removed */
966 for (l = contacts; l != NULL; l = g_list_next (l))
968 EmpathyRosterContact *contact = l->data;
969 FolksIndividual *individual;
971 if (contact_is_favourite (contact))
974 individual = empathy_roster_contact_get_individual (contact);
976 if (g_list_find (tops, individual) == NULL)
977 to_remove = g_list_prepend (to_remove, individual);
980 /* Check which EmpathyRosterContact have to be added */
981 for (l = tops; l != NULL; l = g_list_next (l))
983 FolksIndividual *individual = l->data;
985 if (!individual_in_list (individual, contacts))
986 to_add = g_list_prepend (to_add, individual);
990 for (l = to_add; l != NULL; l = g_list_next (l))
991 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
993 for (l = to_remove; l != NULL; l = g_list_next (l))
994 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
996 g_list_free (to_add);
997 g_list_free (to_remove);
1001 groups_changed_cb (EmpathyRosterModel *model,
1002 FolksIndividual *individual,
1005 EmpathyRosterView *self)
1007 if (!self->priv->show_groups)
1012 add_to_group (self, individual, group);
1016 remove_from_group (self, individual, group);
1021 top_individuals_changed_cb (EmpathyRosterModel *model,
1022 EmpathyRosterView *self)
1024 update_top_contacts (self);
1028 favourites_changed_cb (EmpathyRosterModel *model,
1029 FolksIndividual *individual,
1031 EmpathyRosterView *self)
1033 GHashTable *contacts;
1035 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1036 if (contacts == NULL)
1039 if (self->priv->show_groups)
1042 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1044 remove_from_group (self, individual,
1045 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1051 contact = g_hash_table_lookup (contacts, NO_GROUP);
1052 if (contact == NULL)
1055 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1060 empathy_roster_view_constructed (GObject *object)
1062 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1063 void (*chain_up) (GObject *) =
1064 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1066 if (chain_up != NULL)
1069 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1071 populate_view (self);
1073 tp_g_signal_connect_object (self->priv->model, "individual-added",
1074 G_CALLBACK (individual_added_cb), self, 0);
1075 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1076 G_CALLBACK (individual_removed_cb), self, 0);
1077 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1078 G_CALLBACK (groups_changed_cb), self, 0);
1079 tp_g_signal_connect_object (self->priv->model, "top-individuals-changed",
1080 G_CALLBACK (top_individuals_changed_cb), self, 0);
1081 tp_g_signal_connect_object (self->priv->model, "favourites-changed",
1082 G_CALLBACK (favourites_changed_cb), self, 0);
1084 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1085 roster_view_sort, self, NULL);
1087 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1090 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1092 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1096 empathy_roster_view_dispose (GObject *object)
1098 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1099 void (*chain_up) (GObject *) =
1100 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1102 stop_flashing (self);
1104 empathy_roster_view_set_live_search (self, NULL);
1105 g_clear_object (&self->priv->model);
1107 if (chain_up != NULL)
1112 empathy_roster_view_finalize (GObject *object)
1114 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1115 void (*chain_up) (GObject *) =
1116 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1118 g_hash_table_unref (self->priv->roster_contacts);
1119 g_hash_table_unref (self->priv->roster_groups);
1120 g_hash_table_unref (self->priv->displayed_contacts);
1121 g_queue_free_full (self->priv->events, event_free);
1123 if (chain_up != NULL)
1128 empathy_roster_view_child_activated (EggListBox *box,
1131 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1132 EmpathyRosterContact *contact;
1133 FolksIndividual *individual;
1136 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1139 contact = EMPATHY_ROSTER_CONTACT (child);
1140 individual = empathy_roster_contact_get_individual (contact);
1142 /* Activate the oldest event associated with this contact, if any */
1143 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1144 l = g_list_previous (l))
1146 Event *event = l->data;
1148 if (event->individual == individual)
1150 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1156 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1160 fire_popup_individual_menu (EmpathyRosterView *self,
1165 EmpathyRosterContact *contact;
1166 FolksIndividual *individual;
1168 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1171 contact = EMPATHY_ROSTER_CONTACT (child);
1172 individual = empathy_roster_contact_get_individual (contact);
1174 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1175 individual, button, time);
1179 empathy_roster_view_button_press_event (GtkWidget *widget,
1180 GdkEventButton *event)
1182 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1183 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1184 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1186 if (event->button == 3)
1190 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1194 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1196 fire_popup_individual_menu (self, child, event->button, event->time);
1200 return chain_up (widget, event);
1204 empathy_roster_view_key_press_event (GtkWidget *widget,
1207 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1208 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1209 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1211 if (event->keyval == GDK_KEY_Menu)
1215 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1218 fire_popup_individual_menu (self, child, 0, event->time);
1221 return chain_up (widget, event);
1225 * @out_child: (out) (allow-none)
1228 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1230 GtkWidget **out_child)
1234 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1236 if (out_child != NULL)
1239 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1242 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1246 * @out_child: (out) (allow-none)
1249 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1254 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1256 if (EMPATHY_IS_ROSTER_CONTACT (child))
1257 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1258 else if (EMPATHY_IS_ROSTER_GROUP (child))
1259 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1265 empathy_roster_view_query_tooltip (GtkWidget *widget,
1268 gboolean keyboard_mode,
1269 GtkTooltip *tooltip)
1271 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1272 FolksIndividual *individual;
1276 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1277 if (individual == NULL)
1280 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1281 individual, keyboard_mode, tooltip, &result);
1285 GtkAllocation allocation;
1287 gtk_widget_get_allocation (child, &allocation);
1288 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1295 empathy_roster_view_remove (GtkContainer *container,
1298 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1299 void (*chain_up) (GtkContainer *, GtkWidget *) =
1300 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1302 chain_up (container, widget);
1304 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1305 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1309 empathy_roster_view_class_init (
1310 EmpathyRosterViewClass *klass)
1312 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1313 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1314 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1315 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1318 oclass->get_property = empathy_roster_view_get_property;
1319 oclass->set_property = empathy_roster_view_set_property;
1320 oclass->constructed = empathy_roster_view_constructed;
1321 oclass->dispose = empathy_roster_view_dispose;
1322 oclass->finalize = empathy_roster_view_finalize;
1324 widget_class->button_press_event = empathy_roster_view_button_press_event;
1325 widget_class->key_press_event = empathy_roster_view_key_press_event;
1326 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1328 container_class->remove = empathy_roster_view_remove;
1330 box_class->child_activated = empathy_roster_view_child_activated;
1332 spec = g_param_spec_object ("model", "Model",
1333 "EmpathyRosterModel",
1334 EMPATHY_TYPE_ROSTER_MODEL,
1335 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1336 g_object_class_install_property (oclass, PROP_MODEL, spec);
1338 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1339 "Show offline contacts",
1341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1342 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1344 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1348 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1350 spec = g_param_spec_boolean ("empty", "Empty",
1351 "Is the view currently empty?",
1353 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1354 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1356 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1357 G_OBJECT_CLASS_TYPE (klass),
1359 0, NULL, NULL, NULL,
1361 1, FOLKS_TYPE_INDIVIDUAL);
1363 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1364 G_OBJECT_CLASS_TYPE (klass),
1366 0, NULL, NULL, NULL,
1368 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1370 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1371 G_OBJECT_CLASS_TYPE (klass),
1373 0, NULL, NULL, NULL,
1375 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1377 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1378 G_OBJECT_CLASS_TYPE (klass),
1380 0, g_signal_accumulator_true_handled, NULL, NULL,
1382 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1384 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1388 empathy_roster_view_init (EmpathyRosterView *self)
1390 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1391 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1393 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1394 NULL, (GDestroyNotify) g_hash_table_unref);
1395 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1397 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1399 self->priv->events = g_queue_new ();
1401 self->priv->empty = TRUE;
1405 empathy_roster_view_new (EmpathyRosterModel *model)
1407 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1409 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1415 empathy_roster_view_show_offline (EmpathyRosterView *self,
1418 if (self->priv->show_offline == show)
1421 self->priv->show_offline = show;
1422 egg_list_box_refilter (EGG_LIST_BOX (self));
1424 g_object_notify (G_OBJECT (self), "show-offline");
1428 clear_view (EmpathyRosterView *self)
1430 gtk_container_foreach (GTK_CONTAINER (self),
1431 (GtkCallback) gtk_widget_destroy, NULL);
1433 g_hash_table_remove_all (self->priv->roster_contacts);
1434 g_hash_table_remove_all (self->priv->roster_groups);
1435 g_hash_table_remove_all (self->priv->displayed_contacts);
1439 empathy_roster_view_show_groups (EmpathyRosterView *self,
1442 if (self->priv->show_groups == show)
1445 self->priv->show_groups = show;
1447 /* TODO: block sort/filter? */
1449 populate_view (self);
1451 g_object_notify (G_OBJECT (self), "show-groups");
1455 select_first_contact (EmpathyRosterView *self)
1457 GList *children, *l;
1459 children = gtk_container_get_children (GTK_CONTAINER (self));
1460 for (l = children; l != NULL; l = g_list_next (l))
1462 GtkWidget *child = l->data;
1464 if (!gtk_widget_get_child_visible (child))
1467 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1470 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1474 g_list_free (children);
1478 search_text_notify_cb (EmpathyLiveSearch *search,
1480 EmpathyRosterView *self)
1482 egg_list_box_refilter (EGG_LIST_BOX (self));
1484 select_first_contact (self);
1488 search_activate_cb (GtkWidget *search,
1489 EmpathyRosterView *self)
1491 EggListBox *box = EGG_LIST_BOX (self);
1494 child = egg_list_box_get_selected_child (box);
1498 empathy_roster_view_child_activated (box, child);
1502 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1503 EmpathyLiveSearch *search)
1505 if (self->priv->search != NULL)
1507 g_signal_handlers_disconnect_by_func (self->priv->search,
1508 search_text_notify_cb, self);
1509 g_signal_handlers_disconnect_by_func (self->priv->search,
1510 search_activate_cb, self);
1512 g_clear_object (&self->priv->search);
1518 self->priv->search = g_object_ref (search);
1520 g_signal_connect (self->priv->search, "notify::text",
1521 G_CALLBACK (search_text_notify_cb), self);
1522 g_signal_connect (self->priv->search, "activate",
1523 G_CALLBACK (search_activate_cb), self);
1527 empathy_roster_view_is_empty (EmpathyRosterView *self)
1529 return self->priv->empty;
1533 empathy_roster_view_is_searching (EmpathyRosterView *self)
1535 return (self->priv->search != NULL &&
1536 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1539 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1540 * Empathy's internals. */
1542 empathy_roster_view_add_event (EmpathyRosterView *self,
1543 FolksIndividual *individual,
1547 GHashTable *contacts;
1549 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1550 if (contacts == NULL)
1553 self->priv->last_event_id++;
1555 g_queue_push_head (self->priv->events,
1556 event_new (self->priv->last_event_id, individual, icon, user_data));
1558 start_flashing (self);
1560 return self->priv->last_event_id;
1564 empathy_roster_view_remove_event (EmpathyRosterView *self,
1569 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1570 l = g_list_next (l))
1572 Event *event = l->data;
1574 if (event->id == event_id)
1576 remove_event (self, event);
1583 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1587 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1589 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1592 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));