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
30 SIG_INDIVIDUAL_ACTIVATED,
31 SIG_POPUP_INDIVIDUAL_MENU,
33 SIG_INDIVIDUAL_TOOLTIP,
37 static guint signals[LAST_SIGNAL];
39 #define NO_GROUP "X-no-group"
41 struct _EmpathyRosterViewPriv
43 EmpathyIndividualManager *manager;
45 /* FolksIndividual (borrowed) -> GHashTable (
46 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
48 * When not using groups, this hash just have one element mapped
49 * from the special NO_GROUP key. We could use it as a set but
50 * I prefer to stay coherent in the way this hash is managed.
52 GHashTable *roster_contacts;
53 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
54 GHashTable *roster_groups;
55 /* Hash of the EmpathyRosterContact currently displayed */
56 GHashTable *displayed_contacts;
59 /* queue of (Event *). The most recent events are in the head of the queue
60 * so we always display the icon of the oldest one. */
63 gboolean display_flash_event;
65 gboolean show_offline;
69 EmpathyLiveSearch *search;
71 EmpathyRosterModel *model;
77 FolksIndividual *individual;
84 FolksIndividual *individual,
88 Event *event = g_slice_new (Event);
91 event->individual = g_object_ref (individual);
92 event->icon = g_strdup (icon);
93 event->user_data = user_data;
98 event_free (gpointer data)
101 g_object_unref (event->individual);
102 g_free (event->icon);
104 g_slice_free (Event, event);
108 empathy_roster_view_get_property (GObject *object,
113 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
118 g_value_set_object (value, self->priv->manager);
121 g_value_set_object (value, self->priv->model);
123 case PROP_SHOW_OFFLINE:
124 g_value_set_boolean (value, self->priv->show_offline);
126 case PROP_SHOW_GROUPS:
127 g_value_set_boolean (value, self->priv->show_groups);
130 g_value_set_boolean (value, self->priv->empty);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139 empathy_roster_view_set_property (GObject *object,
144 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
149 g_assert (self->priv->manager == NULL); /* construct only */
150 self->priv->manager = g_value_dup_object (value);
153 g_assert (self->priv->model == NULL);
154 self->priv->model = g_value_dup_object (value);
156 case PROP_SHOW_OFFLINE:
157 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
159 case PROP_SHOW_GROUPS:
160 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
169 roster_contact_changed_cb (GtkWidget *child,
171 EmpathyRosterView *self)
173 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
177 add_roster_contact (EmpathyRosterView *self,
178 FolksIndividual *individual,
183 contact = empathy_roster_contact_new (individual, group);
185 /* Need to refilter if online is changed */
186 g_signal_connect (contact, "notify::online",
187 G_CALLBACK (roster_contact_changed_cb), self);
189 /* Need to resort if alias is changed */
190 g_signal_connect (contact, "notify::alias",
191 G_CALLBACK (roster_contact_changed_cb), self);
193 gtk_widget_show (contact);
194 gtk_container_add (GTK_CONTAINER (self), contact);
200 group_expanded_cb (EmpathyRosterGroup *group,
202 EmpathyRosterView *self)
206 widgets = empathy_roster_group_get_widgets (group);
207 for (l = widgets; l != NULL; l = g_list_next (l))
209 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
212 g_list_free (widgets);
215 static EmpathyRosterGroup *
216 lookup_roster_group (EmpathyRosterView *self,
219 return g_hash_table_lookup (self->priv->roster_groups, group);
222 static EmpathyRosterGroup *
223 ensure_roster_group (EmpathyRosterView *self,
226 GtkWidget *roster_group;
228 roster_group = (GtkWidget *) lookup_roster_group (self, group);
229 if (roster_group != NULL)
230 return EMPATHY_ROSTER_GROUP (roster_group);
232 if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
233 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
234 else if (!tp_strdiff (group, EMPATHY_ROSTER_VIEW_GROUP_PEOPLE_NEARBY))
235 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
237 roster_group = empathy_roster_group_new (group, NULL);
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_group_widgets (EmpathyRosterView *self,
253 EmpathyRosterGroup *group,
254 EmpathyRosterContact *contact,
257 guint old_count, count;
259 old_count = empathy_roster_group_get_widgets_count (group);
262 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
264 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
266 if (count != old_count)
267 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
271 add_to_group (EmpathyRosterView *self,
272 FolksIndividual *individual,
276 GHashTable *contacts;
277 EmpathyRosterGroup *roster_group = NULL;
279 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
280 if (contacts == NULL)
283 if (tp_strdiff (group, NO_GROUP))
284 roster_group = ensure_roster_group (self, group);
286 contact = add_roster_contact (self, individual, group);
287 g_hash_table_insert (contacts, g_strdup (group), contact);
289 if (roster_group != NULL)
291 update_group_widgets (self, roster_group,
292 EMPATHY_ROSTER_CONTACT (contact), TRUE);
297 individual_added (EmpathyRosterView *self,
298 FolksIndividual *individual)
300 GHashTable *contacts;
302 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
303 if (contacts != NULL)
306 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
308 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
310 if (!self->priv->show_groups)
312 add_to_group (self, individual, NO_GROUP);
319 tops = empathy_individual_manager_get_top_individuals (
320 self->priv->manager);
322 if (folks_favourite_details_get_is_favourite (
323 FOLKS_FAVOURITE_DETAILS (individual)) ||
324 g_list_index (tops, individual) != -1)
326 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
329 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
332 if (g_list_length (groups) > 0)
334 for (l = groups; l != NULL; l = g_list_next (l))
336 add_to_group (self, individual, l->data);
341 /* No group, adds to Ungrouped */
342 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
345 g_list_free (groups);
350 set_event_icon_on_individual (EmpathyRosterView *self,
351 FolksIndividual *individual,
354 GHashTable *contacts;
358 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
359 if (contacts == NULL)
362 g_hash_table_iter_init (&iter, contacts);
363 while (g_hash_table_iter_next (&iter, NULL, &v))
365 EmpathyRosterContact *contact =v;
367 empathy_roster_contact_set_event_icon (contact, icon);
372 flash_event (Event *event,
373 EmpathyRosterView *self)
375 set_event_icon_on_individual (self, event->individual, event->icon);
379 unflash_event (Event *event,
380 EmpathyRosterView *self)
382 set_event_icon_on_individual (self, event->individual, NULL);
386 flash_cb (gpointer data)
388 EmpathyRosterView *self = data;
390 if (self->priv->display_flash_event)
392 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
393 self->priv->display_flash_event = FALSE;
397 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
398 self->priv->display_flash_event = TRUE;
405 start_flashing (EmpathyRosterView *self)
407 if (self->priv->flash_id != 0)
410 self->priv->display_flash_event = TRUE;
412 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
417 stop_flashing (EmpathyRosterView *self)
419 if (self->priv->flash_id == 0)
422 g_source_remove (self->priv->flash_id);
423 self->priv->flash_id = 0;
427 remove_event (EmpathyRosterView *self,
430 unflash_event (event, self);
431 g_queue_remove (self->priv->events, event);
433 if (g_queue_get_length (self->priv->events) == 0)
435 stop_flashing (self);
440 remove_all_individual_event (EmpathyRosterView *self,
441 FolksIndividual *individual)
445 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
448 Event *event = l->data;
450 if (event->individual == individual)
452 remove_event (self, event);
459 individual_removed (EmpathyRosterView *self,
460 FolksIndividual *individual)
462 GHashTable *contacts;
466 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
467 if (contacts == NULL)
470 remove_all_individual_event (self, individual);
472 g_hash_table_iter_init (&iter, contacts);
473 while (g_hash_table_iter_next (&iter, &key, &value))
475 const gchar *group_name = key;
476 GtkWidget *contact = value;
477 EmpathyRosterGroup *group;
479 group = lookup_roster_group (self, group_name);
482 update_group_widgets (self, group,
483 EMPATHY_ROSTER_CONTACT (contact), FALSE);
486 gtk_container_remove (GTK_CONTAINER (self), contact);
489 g_hash_table_remove (self->priv->roster_contacts, individual);
493 individual_added_cb (EmpathyRosterModel *model,
494 FolksIndividual *individual,
495 EmpathyRosterView *self)
497 individual_added (self, individual);
501 individual_removed_cb (EmpathyRosterModel *model,
502 FolksIndividual *individual,
503 EmpathyRosterView *self)
505 individual_removed (self, individual);
509 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
510 EmpathyRosterContact *b)
512 FolksIndividual *ind_a, *ind_b;
513 const gchar *alias_a, *alias_b;
515 ind_a = empathy_roster_contact_get_individual (a);
516 ind_b = empathy_roster_contact_get_individual (b);
518 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
519 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
521 return g_ascii_strcasecmp (alias_a, alias_b);
525 contact_is_favourite (EmpathyRosterContact *contact)
527 FolksIndividual *individual;
529 individual = empathy_roster_contact_get_individual (contact);
531 return folks_favourite_details_get_is_favourite (
532 FOLKS_FAVOURITE_DETAILS (individual));
536 contact_in_top (EmpathyRosterView *self,
537 EmpathyRosterContact *contact)
539 FolksIndividual *individual;
542 if (contact_is_favourite (contact))
545 individual = empathy_roster_contact_get_individual (contact);
547 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
549 if (g_list_index (tops, individual) != -1)
556 compare_roster_contacts_no_group (EmpathyRosterView *self,
557 EmpathyRosterContact *a,
558 EmpathyRosterContact *b)
560 gboolean top_a, top_b;
562 top_a = contact_in_top (self, a);
563 top_b = contact_in_top (self, b);
566 /* Both contacts are in the top of the roster (or not). Sort them
568 return compare_roster_contacts_by_alias (a, b);
576 compare_group_names (const gchar *group_a,
577 const gchar *group_b)
579 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
582 if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
585 if (!tp_strdiff (group_a, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
587 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED))
590 return g_ascii_strcasecmp (group_a, group_b);
594 compare_roster_contacts_with_groups (EmpathyRosterView *self,
595 EmpathyRosterContact *a,
596 EmpathyRosterContact *b)
598 const gchar *group_a, *group_b;
600 group_a = empathy_roster_contact_get_group (a);
601 group_b = empathy_roster_contact_get_group (b);
603 if (!tp_strdiff (group_a, group_b))
604 /* Same group, compare the contacts */
605 return compare_roster_contacts_by_alias (a, b);
608 return compare_group_names (group_a, group_b);
612 compare_roster_contacts (EmpathyRosterView *self,
613 EmpathyRosterContact *a,
614 EmpathyRosterContact *b)
616 if (!self->priv->show_groups)
617 return compare_roster_contacts_no_group (self, a, b);
619 return compare_roster_contacts_with_groups (self, a, b);
623 compare_roster_groups (EmpathyRosterGroup *a,
624 EmpathyRosterGroup *b)
626 const gchar *name_a, *name_b;
628 name_a = empathy_roster_group_get_name (a);
629 name_b = empathy_roster_group_get_name (b);
631 return compare_group_names (name_a, name_b);
635 compare_contact_group (EmpathyRosterContact *contact,
636 EmpathyRosterGroup *group)
638 const char *contact_group, *group_name;
640 contact_group = empathy_roster_contact_get_group (contact);
641 group_name = empathy_roster_group_get_name (group);
643 if (!tp_strdiff (contact_group, group_name))
644 /* @contact is in @group, @group has to be displayed first */
647 /* @contact is in a different group, sort by group name */
648 return compare_group_names (contact_group, group_name);
652 roster_view_sort (gconstpointer a,
656 EmpathyRosterView *self = user_data;
658 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
659 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
660 EMPATHY_ROSTER_CONTACT (b));
661 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
662 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
663 EMPATHY_ROSTER_GROUP (b));
664 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
665 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
666 EMPATHY_ROSTER_GROUP (b));
667 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
668 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
669 EMPATHY_ROSTER_GROUP (a));
671 g_return_val_if_reached (0);
675 update_separator (GtkWidget **separator,
682 /* No separator before the first row */
683 g_clear_object (separator);
687 if (*separator != NULL)
690 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
691 g_object_ref_sink (*separator);
695 is_searching (EmpathyRosterView *self)
697 if (self->priv->search == NULL)
700 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
704 update_empty (EmpathyRosterView *self,
707 if (self->priv->empty == empty)
710 self->priv->empty = empty;
711 g_object_notify (G_OBJECT (self), "empty");
715 add_to_displayed (EmpathyRosterView *self,
716 EmpathyRosterContact *contact)
718 FolksIndividual *individual;
719 GHashTable *contacts;
723 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
726 g_hash_table_add (self->priv->displayed_contacts, contact);
727 update_empty (self, FALSE);
729 /* Groups of this contact may now be displayed if we just displays the first
730 * child in this group. */
732 if (!self->priv->show_groups)
735 individual = empathy_roster_contact_get_individual (contact);
736 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
737 if (contacts == NULL)
740 g_hash_table_iter_init (&iter, contacts);
741 while (g_hash_table_iter_next (&iter, &k, NULL))
743 const gchar *group_name = k;
746 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
750 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
755 remove_from_displayed (EmpathyRosterView *self,
756 EmpathyRosterContact *contact)
758 g_hash_table_remove (self->priv->displayed_contacts, contact);
760 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
761 update_empty (self, TRUE);
765 * check if @contact should be displayed according to @self's current status
766 * and without consideration for the state of @contact's groups.
769 contact_should_be_displayed (EmpathyRosterView *self,
770 EmpathyRosterContact *contact)
772 if (is_searching (self))
774 FolksIndividual *individual;
776 individual = empathy_roster_contact_get_individual (contact);
778 return empathy_individual_match_string (individual,
779 empathy_live_search_get_text (self->priv->search),
780 empathy_live_search_get_words (self->priv->search));
783 if (self->priv->show_offline)
786 if (contact_is_favourite (contact))
788 const gchar *group_name;
790 if (!self->priv->show_groups)
791 /* Always display favourite contacts in non-group mode. */
794 group_name = empathy_roster_contact_get_group (contact);
796 if (!tp_strdiff (group_name, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP))
797 /* Always display favourite contact in group mode only in the
802 return empathy_roster_contact_is_online (contact);
807 filter_contact (EmpathyRosterView *self,
808 EmpathyRosterContact *contact)
812 displayed = contact_should_be_displayed (self, contact);
814 if (self->priv->show_groups)
816 const gchar *group_name;
817 EmpathyRosterGroup *group;
819 group_name = empathy_roster_contact_get_group (contact);
820 group = lookup_roster_group (self, group_name);
824 /* When searching, always display even if the group is closed */
825 if (!is_searching (self) &&
826 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
833 add_to_displayed (self, contact);
837 remove_from_displayed (self, contact);
844 filter_group (EmpathyRosterView *self,
845 EmpathyRosterGroup *group)
849 /* Display the group if it contains at least one displayed contact */
850 widgets = empathy_roster_group_get_widgets (group);
851 for (l = widgets; l != NULL; l = g_list_next (l))
853 EmpathyRosterContact *contact = l->data;
855 if (contact_should_be_displayed (self, contact))
863 filter_list (GtkWidget *child,
866 EmpathyRosterView *self = user_data;
868 if (EMPATHY_IS_ROSTER_CONTACT (child))
869 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
871 else if (EMPATHY_IS_ROSTER_GROUP (child))
872 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
874 g_return_val_if_reached (FALSE);
877 /* @list: GList of EmpathyRosterContact
879 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
882 individual_in_list (FolksIndividual *individual,
887 for (l = list; l != NULL; l = g_list_next (l))
889 EmpathyRosterContact *contact = l->data;
891 if (empathy_roster_contact_get_individual (contact) == individual)
899 populate_view (EmpathyRosterView *self)
901 GList *individuals, *l;
903 individuals = empathy_roster_model_get_individuals (self->priv->model);
904 for (l = individuals; l != NULL; l = g_list_next (l))
906 FolksIndividual *individual = l->data;
908 individual_added (self, individual);
911 g_list_free (individuals);
915 remove_from_group (EmpathyRosterView *self,
916 FolksIndividual *individual,
919 GHashTable *contacts;
921 EmpathyRosterGroup *roster_group;
923 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
924 if (contacts == NULL)
927 contact = g_hash_table_lookup (contacts, group);
931 g_hash_table_remove (contacts, group);
933 if (g_hash_table_size (contacts) == 0)
935 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_UNGROUPED);
938 roster_group = lookup_roster_group (self, group);
940 if (roster_group != NULL)
942 update_group_widgets (self, roster_group,
943 EMPATHY_ROSTER_CONTACT (contact), FALSE);
946 gtk_container_remove (GTK_CONTAINER (self), contact);
950 update_top_contacts (EmpathyRosterView *self)
953 GList *to_add = NULL, *to_remove = NULL;
954 EmpathyRosterGroup *group;
956 if (!self->priv->show_groups)
958 egg_list_box_resort (EGG_LIST_BOX (self));
962 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
964 group = g_hash_table_lookup (self->priv->roster_groups,
965 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
968 to_add = g_list_copy (tops);
974 contacts = empathy_roster_group_get_widgets (group);
976 /* Check which EmpathyRosterContact have to be removed */
977 for (l = contacts; l != NULL; l = g_list_next (l))
979 EmpathyRosterContact *contact = l->data;
980 FolksIndividual *individual;
982 if (contact_is_favourite (contact))
985 individual = empathy_roster_contact_get_individual (contact);
987 if (g_list_find (tops, individual) == NULL)
988 to_remove = g_list_prepend (to_remove, individual);
991 /* Check which EmpathyRosterContact have to be added */
992 for (l = tops; l != NULL; l = g_list_next (l))
994 FolksIndividual *individual = l->data;
996 if (!individual_in_list (individual, contacts))
997 to_add = g_list_prepend (to_add, individual);
1001 for (l = to_add; l != NULL; l = g_list_next (l))
1002 add_to_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1004 for (l = to_remove; l != NULL; l = g_list_next (l))
1005 remove_from_group (self, l->data, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1007 g_list_free (to_add);
1008 g_list_free (to_remove);
1012 groups_changed_cb (EmpathyRosterModel *model,
1013 FolksIndividual *individual,
1016 EmpathyRosterView *self)
1018 if (!self->priv->show_groups)
1023 add_to_group (self, individual, group);
1027 remove_from_group (self, individual, group);
1032 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1034 EmpathyRosterView *self)
1036 update_top_contacts (self);
1040 favourites_changed_cb (EmpathyIndividualManager *manager,
1041 FolksIndividual *individual,
1043 EmpathyRosterView *self)
1045 GHashTable *contacts;
1047 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1048 if (contacts == NULL)
1051 if (self->priv->show_groups)
1054 add_to_group (self, individual, EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1056 remove_from_group (self, individual,
1057 EMPATHY_ROSTER_VIEW_GROUP_TOP_GROUP);
1063 contact = g_hash_table_lookup (contacts, NO_GROUP);
1064 if (contact == NULL)
1067 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1072 empathy_roster_view_constructed (GObject *object)
1074 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1075 void (*chain_up) (GObject *) =
1076 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1078 if (chain_up != NULL)
1081 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1082 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1084 populate_view (self);
1086 tp_g_signal_connect_object (self->priv->model, "individual-added",
1087 G_CALLBACK (individual_added_cb), self, 0);
1088 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1089 G_CALLBACK (individual_removed_cb), self, 0);
1090 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1091 G_CALLBACK (groups_changed_cb), self, 0);
1092 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1093 G_CALLBACK (top_individuals_changed_cb), self, 0);
1094 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1095 G_CALLBACK (favourites_changed_cb), self, 0);
1097 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1098 roster_view_sort, self, NULL);
1100 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1103 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1105 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1109 empathy_roster_view_dispose (GObject *object)
1111 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1112 void (*chain_up) (GObject *) =
1113 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1115 stop_flashing (self);
1117 empathy_roster_view_set_live_search (self, NULL);
1118 g_clear_object (&self->priv->manager);
1119 g_clear_object (&self->priv->model);
1121 if (chain_up != NULL)
1126 empathy_roster_view_finalize (GObject *object)
1128 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1129 void (*chain_up) (GObject *) =
1130 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1132 g_hash_table_unref (self->priv->roster_contacts);
1133 g_hash_table_unref (self->priv->roster_groups);
1134 g_hash_table_unref (self->priv->displayed_contacts);
1135 g_queue_free_full (self->priv->events, event_free);
1137 if (chain_up != NULL)
1142 empathy_roster_view_child_activated (EggListBox *box,
1145 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1146 EmpathyRosterContact *contact;
1147 FolksIndividual *individual;
1150 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1153 contact = EMPATHY_ROSTER_CONTACT (child);
1154 individual = empathy_roster_contact_get_individual (contact);
1156 /* Activate the oldest event associated with this contact, if any */
1157 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1158 l = g_list_previous (l))
1160 Event *event = l->data;
1162 if (event->individual == individual)
1164 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1170 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1174 fire_popup_individual_menu (EmpathyRosterView *self,
1179 EmpathyRosterContact *contact;
1180 FolksIndividual *individual;
1182 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1185 contact = EMPATHY_ROSTER_CONTACT (child);
1186 individual = empathy_roster_contact_get_individual (contact);
1188 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1189 individual, button, time);
1193 empathy_roster_view_button_press_event (GtkWidget *widget,
1194 GdkEventButton *event)
1196 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1197 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1198 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1200 if (event->button == 3)
1204 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1208 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1210 fire_popup_individual_menu (self, child, event->button, event->time);
1214 return chain_up (widget, event);
1218 empathy_roster_view_key_press_event (GtkWidget *widget,
1221 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1222 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1223 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1225 if (event->keyval == GDK_KEY_Menu)
1229 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1232 fire_popup_individual_menu (self, child, 0, event->time);
1235 return chain_up (widget, event);
1239 * @out_child: (out) (allow-none)
1242 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1244 GtkWidget **out_child)
1248 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1250 if (out_child != NULL)
1253 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1256 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1260 * @out_child: (out) (allow-none)
1263 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1268 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1270 if (EMPATHY_IS_ROSTER_CONTACT (child))
1271 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1272 else if (EMPATHY_IS_ROSTER_GROUP (child))
1273 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1279 empathy_roster_view_query_tooltip (GtkWidget *widget,
1282 gboolean keyboard_mode,
1283 GtkTooltip *tooltip)
1285 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1286 FolksIndividual *individual;
1290 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1291 if (individual == NULL)
1294 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1295 individual, keyboard_mode, tooltip, &result);
1299 GtkAllocation allocation;
1301 gtk_widget_get_allocation (child, &allocation);
1302 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1309 empathy_roster_view_remove (GtkContainer *container,
1312 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1313 void (*chain_up) (GtkContainer *, GtkWidget *) =
1314 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1316 chain_up (container, widget);
1318 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1319 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1323 empathy_roster_view_class_init (
1324 EmpathyRosterViewClass *klass)
1326 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1327 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1328 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1329 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1332 oclass->get_property = empathy_roster_view_get_property;
1333 oclass->set_property = empathy_roster_view_set_property;
1334 oclass->constructed = empathy_roster_view_constructed;
1335 oclass->dispose = empathy_roster_view_dispose;
1336 oclass->finalize = empathy_roster_view_finalize;
1338 widget_class->button_press_event = empathy_roster_view_button_press_event;
1339 widget_class->key_press_event = empathy_roster_view_key_press_event;
1340 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1342 container_class->remove = empathy_roster_view_remove;
1344 box_class->child_activated = empathy_roster_view_child_activated;
1346 spec = g_param_spec_object ("manager", "Manager",
1347 "EmpathyIndividualManager",
1348 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1349 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1350 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1352 spec = g_param_spec_object ("model", "Model",
1353 "EmpathyRosterModel",
1354 EMPATHY_TYPE_ROSTER_MODEL,
1355 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1356 g_object_class_install_property (oclass, PROP_MODEL, spec);
1358 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1359 "Show offline contacts",
1361 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1362 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1364 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1368 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1370 spec = g_param_spec_boolean ("empty", "Empty",
1371 "Is the view currently empty?",
1373 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1374 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1376 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1377 G_OBJECT_CLASS_TYPE (klass),
1379 0, NULL, NULL, NULL,
1381 1, FOLKS_TYPE_INDIVIDUAL);
1383 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1384 G_OBJECT_CLASS_TYPE (klass),
1386 0, NULL, NULL, NULL,
1388 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1390 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1391 G_OBJECT_CLASS_TYPE (klass),
1393 0, NULL, NULL, NULL,
1395 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1397 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1398 G_OBJECT_CLASS_TYPE (klass),
1400 0, g_signal_accumulator_true_handled, NULL, NULL,
1402 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1404 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1408 empathy_roster_view_init (EmpathyRosterView *self)
1410 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1411 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1413 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1414 NULL, (GDestroyNotify) g_hash_table_unref);
1415 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1417 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1419 self->priv->events = g_queue_new ();
1421 self->priv->empty = TRUE;
1425 empathy_roster_view_new (EmpathyIndividualManager *manager,
1426 EmpathyRosterModel *model)
1428 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1429 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1431 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1437 EmpathyIndividualManager *
1438 empathy_roster_view_get_manager (EmpathyRosterView *self)
1440 return self->priv->manager;
1444 empathy_roster_view_show_offline (EmpathyRosterView *self,
1447 if (self->priv->show_offline == show)
1450 self->priv->show_offline = show;
1451 egg_list_box_refilter (EGG_LIST_BOX (self));
1453 g_object_notify (G_OBJECT (self), "show-offline");
1457 clear_view (EmpathyRosterView *self)
1459 gtk_container_foreach (GTK_CONTAINER (self),
1460 (GtkCallback) gtk_widget_destroy, NULL);
1462 g_hash_table_remove_all (self->priv->roster_contacts);
1463 g_hash_table_remove_all (self->priv->roster_groups);
1464 g_hash_table_remove_all (self->priv->displayed_contacts);
1468 empathy_roster_view_show_groups (EmpathyRosterView *self,
1471 if (self->priv->show_groups == show)
1474 self->priv->show_groups = show;
1476 /* TODO: block sort/filter? */
1478 populate_view (self);
1480 g_object_notify (G_OBJECT (self), "show-groups");
1484 select_first_contact (EmpathyRosterView *self)
1486 GList *children, *l;
1488 children = gtk_container_get_children (GTK_CONTAINER (self));
1489 for (l = children; l != NULL; l = g_list_next (l))
1491 GtkWidget *child = l->data;
1493 if (!gtk_widget_get_child_visible (child))
1496 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1499 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1503 g_list_free (children);
1507 search_text_notify_cb (EmpathyLiveSearch *search,
1509 EmpathyRosterView *self)
1511 egg_list_box_refilter (EGG_LIST_BOX (self));
1513 select_first_contact (self);
1517 search_activate_cb (GtkWidget *search,
1518 EmpathyRosterView *self)
1520 EggListBox *box = EGG_LIST_BOX (self);
1523 child = egg_list_box_get_selected_child (box);
1527 empathy_roster_view_child_activated (box, child);
1531 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1532 EmpathyLiveSearch *search)
1534 if (self->priv->search != NULL)
1536 g_signal_handlers_disconnect_by_func (self->priv->search,
1537 search_text_notify_cb, self);
1538 g_signal_handlers_disconnect_by_func (self->priv->search,
1539 search_activate_cb, self);
1541 g_clear_object (&self->priv->search);
1547 self->priv->search = g_object_ref (search);
1549 g_signal_connect (self->priv->search, "notify::text",
1550 G_CALLBACK (search_text_notify_cb), self);
1551 g_signal_connect (self->priv->search, "activate",
1552 G_CALLBACK (search_activate_cb), self);
1556 empathy_roster_view_is_empty (EmpathyRosterView *self)
1558 return self->priv->empty;
1562 empathy_roster_view_is_searching (EmpathyRosterView *self)
1564 return (self->priv->search != NULL &&
1565 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1568 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1569 * Empathy's internals. */
1571 empathy_roster_view_add_event (EmpathyRosterView *self,
1572 FolksIndividual *individual,
1576 GHashTable *contacts;
1578 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1579 if (contacts == NULL)
1582 self->priv->last_event_id++;
1584 g_queue_push_head (self->priv->events,
1585 event_new (self->priv->last_event_id, individual, icon, user_data));
1587 start_flashing (self);
1589 return self->priv->last_event_id;
1593 empathy_roster_view_remove_event (EmpathyRosterView *self,
1598 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1599 l = g_list_next (l))
1601 Event *event = l->data;
1603 if (event->id == event_id)
1605 remove_event (self, event);
1612 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1616 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1618 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1621 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));