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_MODEL_GROUP_TOP_GROUP))
223 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
224 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_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_MODEL_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_MODEL_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 compare_roster_contacts_no_group (EmpathyRosterView *self,
515 EmpathyRosterContact *a,
516 EmpathyRosterContact *b)
518 gboolean top_a, top_b;
520 top_a = empathy_roster_model_contact_in_top (self->priv->model, a);
521 top_b = empathy_roster_model_contact_in_top (self->priv->model, b);
524 /* Both contacts are in the top of the roster (or not). Sort them
526 return compare_roster_contacts_by_alias (a, b);
534 compare_group_names (const gchar *group_a,
535 const gchar *group_b)
537 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
540 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
543 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
545 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
548 return g_ascii_strcasecmp (group_a, group_b);
552 compare_roster_contacts_with_groups (EmpathyRosterView *self,
553 EmpathyRosterContact *a,
554 EmpathyRosterContact *b)
556 const gchar *group_a, *group_b;
558 group_a = empathy_roster_contact_get_group (a);
559 group_b = empathy_roster_contact_get_group (b);
561 if (!tp_strdiff (group_a, group_b))
562 /* Same group, compare the contacts */
563 return compare_roster_contacts_by_alias (a, b);
566 return compare_group_names (group_a, group_b);
570 compare_roster_contacts (EmpathyRosterView *self,
571 EmpathyRosterContact *a,
572 EmpathyRosterContact *b)
574 if (!self->priv->show_groups)
575 return compare_roster_contacts_no_group (self, a, b);
577 return compare_roster_contacts_with_groups (self, a, b);
581 compare_roster_groups (EmpathyRosterGroup *a,
582 EmpathyRosterGroup *b)
584 const gchar *name_a, *name_b;
586 name_a = empathy_roster_group_get_name (a);
587 name_b = empathy_roster_group_get_name (b);
589 return compare_group_names (name_a, name_b);
593 compare_contact_group (EmpathyRosterContact *contact,
594 EmpathyRosterGroup *group)
596 const char *contact_group, *group_name;
598 contact_group = empathy_roster_contact_get_group (contact);
599 group_name = empathy_roster_group_get_name (group);
601 if (!tp_strdiff (contact_group, group_name))
602 /* @contact is in @group, @group has to be displayed first */
605 /* @contact is in a different group, sort by group name */
606 return compare_group_names (contact_group, group_name);
610 roster_view_sort (gconstpointer a,
614 EmpathyRosterView *self = user_data;
616 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
617 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
618 EMPATHY_ROSTER_CONTACT (b));
619 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
620 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
621 EMPATHY_ROSTER_GROUP (b));
622 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
623 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
624 EMPATHY_ROSTER_GROUP (b));
625 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
626 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
627 EMPATHY_ROSTER_GROUP (a));
629 g_return_val_if_reached (0);
633 update_separator (GtkWidget **separator,
640 /* No separator before the first row */
641 g_clear_object (separator);
645 if (*separator != NULL)
648 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
649 g_object_ref_sink (*separator);
653 is_searching (EmpathyRosterView *self)
655 if (self->priv->search == NULL)
658 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
662 update_empty (EmpathyRosterView *self,
665 if (self->priv->empty == empty)
668 self->priv->empty = empty;
669 g_object_notify (G_OBJECT (self), "empty");
673 add_to_displayed (EmpathyRosterView *self,
674 EmpathyRosterContact *contact)
676 FolksIndividual *individual;
677 GHashTable *contacts;
681 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
684 g_hash_table_add (self->priv->displayed_contacts, contact);
685 update_empty (self, FALSE);
687 /* Groups of this contact may now be displayed if we just displays the first
688 * child in this group. */
690 if (!self->priv->show_groups)
693 individual = empathy_roster_contact_get_individual (contact);
694 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
695 if (contacts == NULL)
698 g_hash_table_iter_init (&iter, contacts);
699 while (g_hash_table_iter_next (&iter, &k, NULL))
701 const gchar *group_name = k;
704 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
708 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
713 remove_from_displayed (EmpathyRosterView *self,
714 EmpathyRosterContact *contact)
716 g_hash_table_remove (self->priv->displayed_contacts, contact);
718 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
719 update_empty (self, TRUE);
723 * check if @contact should be displayed according to @self's current status
724 * and without consideration for the state of @contact's groups.
727 contact_should_be_displayed (EmpathyRosterView *self,
728 EmpathyRosterContact *contact)
730 if (is_searching (self))
732 FolksIndividual *individual;
734 individual = empathy_roster_contact_get_individual (contact);
736 return empathy_individual_match_string (individual,
737 empathy_live_search_get_text (self->priv->search),
738 empathy_live_search_get_words (self->priv->search));
741 if (self->priv->show_offline)
744 if (empathy_roster_model_contact_in_top (self->priv->model,
747 const gchar *group_name;
749 if (!self->priv->show_groups)
750 /* Always display favourite contacts in non-group mode. */
753 group_name = empathy_roster_contact_get_group (contact);
755 if (!tp_strdiff (group_name, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
756 /* Always display favourite contact in group mode only in the
761 return empathy_roster_contact_is_online (contact);
766 filter_contact (EmpathyRosterView *self,
767 EmpathyRosterContact *contact)
771 displayed = contact_should_be_displayed (self, contact);
773 if (self->priv->show_groups)
775 const gchar *group_name;
776 EmpathyRosterGroup *group;
778 group_name = empathy_roster_contact_get_group (contact);
779 group = lookup_roster_group (self, group_name);
783 /* When searching, always display even if the group is closed */
784 if (!is_searching (self) &&
785 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
792 add_to_displayed (self, contact);
796 remove_from_displayed (self, contact);
803 filter_group (EmpathyRosterView *self,
804 EmpathyRosterGroup *group)
808 /* Display the group if it contains at least one displayed contact */
809 widgets = empathy_roster_group_get_widgets (group);
810 for (l = widgets; l != NULL; l = g_list_next (l))
812 EmpathyRosterContact *contact = l->data;
814 if (contact_should_be_displayed (self, contact))
822 filter_list (GtkWidget *child,
825 EmpathyRosterView *self = user_data;
827 if (EMPATHY_IS_ROSTER_CONTACT (child))
828 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
830 else if (EMPATHY_IS_ROSTER_GROUP (child))
831 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
833 g_return_val_if_reached (FALSE);
836 /* @list: GList of EmpathyRosterContact
838 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
841 individual_in_list (FolksIndividual *individual,
846 for (l = list; l != NULL; l = g_list_next (l))
848 EmpathyRosterContact *contact = l->data;
850 if (empathy_roster_contact_get_individual (contact) == individual)
858 populate_view (EmpathyRosterView *self)
860 GList *individuals, *l;
862 individuals = empathy_roster_model_get_individuals (self->priv->model);
863 for (l = individuals; l != NULL; l = g_list_next (l))
865 FolksIndividual *individual = l->data;
867 individual_added (self, individual);
870 g_list_free (individuals);
874 remove_from_group (EmpathyRosterView *self,
875 FolksIndividual *individual,
878 GHashTable *contacts;
880 EmpathyRosterGroup *roster_group;
882 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
883 if (contacts == NULL)
886 contact = g_hash_table_lookup (contacts, group);
890 g_hash_table_remove (contacts, group);
892 if (g_hash_table_size (contacts) == 0)
894 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
897 roster_group = lookup_roster_group (self, group);
899 if (roster_group != NULL)
901 update_group_widgets (self, roster_group,
902 EMPATHY_ROSTER_CONTACT (contact), FALSE);
905 gtk_container_remove (GTK_CONTAINER (self), contact);
909 update_top_contacts (EmpathyRosterView *self)
912 GList *to_add = NULL, *to_remove = NULL;
913 EmpathyRosterGroup *group;
915 if (!self->priv->show_groups)
917 egg_list_box_resort (EGG_LIST_BOX (self));
921 tops = empathy_roster_model_get_top_individuals (self->priv->model);
923 group = g_hash_table_lookup (self->priv->roster_groups,
924 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
927 to_add = g_list_copy (tops);
933 contacts = empathy_roster_group_get_widgets (group);
935 /* Check which EmpathyRosterContact have to be removed */
936 for (l = contacts; l != NULL; l = g_list_next (l))
938 EmpathyRosterContact *contact = l->data;
939 FolksIndividual *individual;
941 if (empathy_roster_model_contact_in_top (self->priv->model,
945 individual = empathy_roster_contact_get_individual (contact);
947 if (g_list_find (tops, individual) == NULL)
948 to_remove = g_list_prepend (to_remove, individual);
951 /* Check which EmpathyRosterContact have to be added */
952 for (l = tops; l != NULL; l = g_list_next (l))
954 FolksIndividual *individual = l->data;
956 if (!individual_in_list (individual, contacts))
957 to_add = g_list_prepend (to_add, individual);
961 for (l = to_add; l != NULL; l = g_list_next (l))
962 add_to_group (self, l->data, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
964 for (l = to_remove; l != NULL; l = g_list_next (l))
965 remove_from_group (self, l->data, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
967 g_list_free (to_add);
968 g_list_free (to_remove);
972 groups_changed_cb (EmpathyRosterModel *model,
973 FolksIndividual *individual,
976 EmpathyRosterView *self)
978 if (!self->priv->show_groups)
983 add_to_group (self, individual, group);
987 remove_from_group (self, individual, group);
992 top_individuals_changed_cb (EmpathyRosterModel *model,
993 EmpathyRosterView *self)
995 update_top_contacts (self);
999 favourites_changed_cb (EmpathyRosterModel *model,
1000 FolksIndividual *individual,
1002 EmpathyRosterView *self)
1004 GHashTable *contacts;
1006 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1007 if (contacts == NULL)
1010 if (self->priv->show_groups)
1013 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
1015 remove_from_group (self, individual,
1016 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
1022 contact = g_hash_table_lookup (contacts, NO_GROUP);
1023 if (contact == NULL)
1026 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1031 empathy_roster_view_constructed (GObject *object)
1033 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1034 void (*chain_up) (GObject *) =
1035 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1037 if (chain_up != NULL)
1040 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1042 populate_view (self);
1044 tp_g_signal_connect_object (self->priv->model, "individual-added",
1045 G_CALLBACK (individual_added_cb), self, 0);
1046 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1047 G_CALLBACK (individual_removed_cb), self, 0);
1048 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1049 G_CALLBACK (groups_changed_cb), self, 0);
1050 tp_g_signal_connect_object (self->priv->model, "top-individuals-changed",
1051 G_CALLBACK (top_individuals_changed_cb), self, 0);
1052 tp_g_signal_connect_object (self->priv->model, "favourites-changed",
1053 G_CALLBACK (favourites_changed_cb), self, 0);
1055 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1056 roster_view_sort, self, NULL);
1058 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1061 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1063 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1067 empathy_roster_view_dispose (GObject *object)
1069 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1070 void (*chain_up) (GObject *) =
1071 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1073 stop_flashing (self);
1075 empathy_roster_view_set_live_search (self, NULL);
1076 g_clear_object (&self->priv->model);
1078 if (chain_up != NULL)
1083 empathy_roster_view_finalize (GObject *object)
1085 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1086 void (*chain_up) (GObject *) =
1087 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1089 g_hash_table_unref (self->priv->roster_contacts);
1090 g_hash_table_unref (self->priv->roster_groups);
1091 g_hash_table_unref (self->priv->displayed_contacts);
1092 g_queue_free_full (self->priv->events, event_free);
1094 if (chain_up != NULL)
1099 empathy_roster_view_child_activated (EggListBox *box,
1102 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1103 EmpathyRosterContact *contact;
1104 FolksIndividual *individual;
1107 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1110 contact = EMPATHY_ROSTER_CONTACT (child);
1111 individual = empathy_roster_contact_get_individual (contact);
1113 /* Activate the oldest event associated with this contact, if any */
1114 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1115 l = g_list_previous (l))
1117 Event *event = l->data;
1119 if (event->individual == individual)
1121 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1127 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1131 fire_popup_individual_menu (EmpathyRosterView *self,
1136 EmpathyRosterContact *contact;
1137 FolksIndividual *individual;
1139 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1142 contact = EMPATHY_ROSTER_CONTACT (child);
1143 individual = empathy_roster_contact_get_individual (contact);
1145 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1146 individual, button, time);
1150 empathy_roster_view_button_press_event (GtkWidget *widget,
1151 GdkEventButton *event)
1153 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1154 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1155 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1157 if (event->button == 3)
1161 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1165 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1167 fire_popup_individual_menu (self, child, event->button, event->time);
1171 return chain_up (widget, event);
1175 empathy_roster_view_key_press_event (GtkWidget *widget,
1178 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1179 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1180 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1182 if (event->keyval == GDK_KEY_Menu)
1186 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1189 fire_popup_individual_menu (self, child, 0, event->time);
1192 return chain_up (widget, event);
1196 * @out_child: (out) (allow-none)
1199 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1201 GtkWidget **out_child)
1205 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1207 if (out_child != NULL)
1210 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1213 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1217 * @out_child: (out) (allow-none)
1220 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1225 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1227 if (EMPATHY_IS_ROSTER_CONTACT (child))
1228 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1229 else if (EMPATHY_IS_ROSTER_GROUP (child))
1230 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1236 empathy_roster_view_query_tooltip (GtkWidget *widget,
1239 gboolean keyboard_mode,
1240 GtkTooltip *tooltip)
1242 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1243 FolksIndividual *individual;
1247 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1248 if (individual == NULL)
1251 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1252 individual, keyboard_mode, tooltip, &result);
1256 GtkAllocation allocation;
1258 gtk_widget_get_allocation (child, &allocation);
1259 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1266 empathy_roster_view_remove (GtkContainer *container,
1269 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1270 void (*chain_up) (GtkContainer *, GtkWidget *) =
1271 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1273 chain_up (container, widget);
1275 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1276 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1280 empathy_roster_view_class_init (
1281 EmpathyRosterViewClass *klass)
1283 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1284 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1285 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1286 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1289 oclass->get_property = empathy_roster_view_get_property;
1290 oclass->set_property = empathy_roster_view_set_property;
1291 oclass->constructed = empathy_roster_view_constructed;
1292 oclass->dispose = empathy_roster_view_dispose;
1293 oclass->finalize = empathy_roster_view_finalize;
1295 widget_class->button_press_event = empathy_roster_view_button_press_event;
1296 widget_class->key_press_event = empathy_roster_view_key_press_event;
1297 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1299 container_class->remove = empathy_roster_view_remove;
1301 box_class->child_activated = empathy_roster_view_child_activated;
1303 spec = g_param_spec_object ("model", "Model",
1304 "EmpathyRosterModel",
1305 EMPATHY_TYPE_ROSTER_MODEL,
1306 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1307 g_object_class_install_property (oclass, PROP_MODEL, spec);
1309 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1310 "Show offline contacts",
1312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1313 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1315 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1318 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1319 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1321 spec = g_param_spec_boolean ("empty", "Empty",
1322 "Is the view currently empty?",
1324 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1325 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1327 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1328 G_OBJECT_CLASS_TYPE (klass),
1330 0, NULL, NULL, NULL,
1332 1, FOLKS_TYPE_INDIVIDUAL);
1334 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1335 G_OBJECT_CLASS_TYPE (klass),
1337 0, NULL, NULL, NULL,
1339 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1341 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1342 G_OBJECT_CLASS_TYPE (klass),
1344 0, NULL, NULL, NULL,
1346 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1348 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1349 G_OBJECT_CLASS_TYPE (klass),
1351 0, g_signal_accumulator_true_handled, NULL, NULL,
1353 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1355 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1359 empathy_roster_view_init (EmpathyRosterView *self)
1361 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1362 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1364 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1365 NULL, (GDestroyNotify) g_hash_table_unref);
1366 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1368 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1370 self->priv->events = g_queue_new ();
1372 self->priv->empty = TRUE;
1376 empathy_roster_view_new (EmpathyRosterModel *model)
1378 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1380 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1386 empathy_roster_view_show_offline (EmpathyRosterView *self,
1389 if (self->priv->show_offline == show)
1392 self->priv->show_offline = show;
1393 egg_list_box_refilter (EGG_LIST_BOX (self));
1395 g_object_notify (G_OBJECT (self), "show-offline");
1399 clear_view (EmpathyRosterView *self)
1401 gtk_container_foreach (GTK_CONTAINER (self),
1402 (GtkCallback) gtk_widget_destroy, NULL);
1404 g_hash_table_remove_all (self->priv->roster_contacts);
1405 g_hash_table_remove_all (self->priv->roster_groups);
1406 g_hash_table_remove_all (self->priv->displayed_contacts);
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_text_notify_cb (EmpathyLiveSearch *search,
1451 EmpathyRosterView *self)
1453 egg_list_box_refilter (EGG_LIST_BOX (self));
1455 select_first_contact (self);
1459 search_activate_cb (GtkWidget *search,
1460 EmpathyRosterView *self)
1462 EggListBox *box = EGG_LIST_BOX (self);
1465 child = egg_list_box_get_selected_child (box);
1469 empathy_roster_view_child_activated (box, child);
1473 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1474 EmpathyLiveSearch *search)
1476 if (self->priv->search != NULL)
1478 g_signal_handlers_disconnect_by_func (self->priv->search,
1479 search_text_notify_cb, self);
1480 g_signal_handlers_disconnect_by_func (self->priv->search,
1481 search_activate_cb, self);
1483 g_clear_object (&self->priv->search);
1489 self->priv->search = g_object_ref (search);
1491 g_signal_connect (self->priv->search, "notify::text",
1492 G_CALLBACK (search_text_notify_cb), self);
1493 g_signal_connect (self->priv->search, "activate",
1494 G_CALLBACK (search_activate_cb), self);
1498 empathy_roster_view_is_empty (EmpathyRosterView *self)
1500 return self->priv->empty;
1504 empathy_roster_view_is_searching (EmpathyRosterView *self)
1506 return (self->priv->search != NULL &&
1507 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1510 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1511 * Empathy's internals. */
1513 empathy_roster_view_add_event (EmpathyRosterView *self,
1514 FolksIndividual *individual,
1518 GHashTable *contacts;
1520 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1521 if (contacts == NULL)
1524 self->priv->last_event_id++;
1526 g_queue_push_head (self->priv->events,
1527 event_new (self->priv->last_event_id, individual, icon, user_data));
1529 start_flashing (self);
1531 return self->priv->last_event_id;
1535 empathy_roster_view_remove_event (EmpathyRosterView *self,
1540 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1541 l = g_list_next (l))
1543 Event *event = l->data;
1545 if (event->id == event_id)
1547 remove_event (self, event);
1554 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1558 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1560 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1563 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));