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);
308 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
311 if (g_list_length (groups) > 0)
313 for (l = groups; l != NULL; l = g_list_next (l))
315 add_to_group (self, individual, l->data);
320 /* No group, adds to Ungrouped */
321 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
324 g_list_free (groups);
329 set_event_icon_on_individual (EmpathyRosterView *self,
330 FolksIndividual *individual,
333 GHashTable *contacts;
337 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
338 if (contacts == NULL)
341 g_hash_table_iter_init (&iter, contacts);
342 while (g_hash_table_iter_next (&iter, NULL, &v))
344 EmpathyRosterContact *contact =v;
346 empathy_roster_contact_set_event_icon (contact, icon);
351 flash_event (Event *event,
352 EmpathyRosterView *self)
354 set_event_icon_on_individual (self, event->individual, event->icon);
358 unflash_event (Event *event,
359 EmpathyRosterView *self)
361 set_event_icon_on_individual (self, event->individual, NULL);
365 flash_cb (gpointer data)
367 EmpathyRosterView *self = data;
369 if (self->priv->display_flash_event)
371 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
372 self->priv->display_flash_event = FALSE;
376 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
377 self->priv->display_flash_event = TRUE;
384 start_flashing (EmpathyRosterView *self)
386 if (self->priv->flash_id != 0)
389 self->priv->display_flash_event = TRUE;
391 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
396 stop_flashing (EmpathyRosterView *self)
398 if (self->priv->flash_id == 0)
401 g_source_remove (self->priv->flash_id);
402 self->priv->flash_id = 0;
406 remove_event (EmpathyRosterView *self,
409 unflash_event (event, self);
410 g_queue_remove (self->priv->events, event);
412 if (g_queue_get_length (self->priv->events) == 0)
414 stop_flashing (self);
419 remove_all_individual_event (EmpathyRosterView *self,
420 FolksIndividual *individual)
424 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
427 Event *event = l->data;
429 if (event->individual == individual)
431 remove_event (self, event);
438 individual_removed (EmpathyRosterView *self,
439 FolksIndividual *individual)
441 GHashTable *contacts;
445 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
446 if (contacts == NULL)
449 remove_all_individual_event (self, individual);
451 g_hash_table_iter_init (&iter, contacts);
452 while (g_hash_table_iter_next (&iter, &key, &value))
454 const gchar *group_name = key;
455 GtkWidget *contact = value;
456 EmpathyRosterGroup *group;
458 group = lookup_roster_group (self, group_name);
461 update_group_widgets (self, group,
462 EMPATHY_ROSTER_CONTACT (contact), FALSE);
465 gtk_container_remove (GTK_CONTAINER (self), contact);
468 g_hash_table_remove (self->priv->roster_contacts, individual);
472 individual_added_cb (EmpathyRosterModel *model,
473 FolksIndividual *individual,
474 EmpathyRosterView *self)
476 individual_added (self, individual);
480 individual_removed_cb (EmpathyRosterModel *model,
481 FolksIndividual *individual,
482 EmpathyRosterView *self)
484 individual_removed (self, individual);
488 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
489 EmpathyRosterContact *b)
491 FolksIndividual *ind_a, *ind_b;
492 const gchar *alias_a, *alias_b;
494 ind_a = empathy_roster_contact_get_individual (a);
495 ind_b = empathy_roster_contact_get_individual (b);
497 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
498 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
500 return g_ascii_strcasecmp (alias_a, alias_b);
504 compare_roster_contacts_no_group (EmpathyRosterView *self,
505 EmpathyRosterContact *a,
506 EmpathyRosterContact *b)
508 gboolean top_a, top_b;
510 top_a = empathy_roster_model_contact_in_top (self->priv->model, a);
511 top_b = empathy_roster_model_contact_in_top (self->priv->model, b);
514 /* Both contacts are in the top of the roster (or not). Sort them
516 return compare_roster_contacts_by_alias (a, b);
524 compare_group_names (const gchar *group_a,
525 const gchar *group_b)
527 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
530 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
533 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
535 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
538 return g_ascii_strcasecmp (group_a, group_b);
542 compare_roster_contacts_with_groups (EmpathyRosterView *self,
543 EmpathyRosterContact *a,
544 EmpathyRosterContact *b)
546 const gchar *group_a, *group_b;
548 group_a = empathy_roster_contact_get_group (a);
549 group_b = empathy_roster_contact_get_group (b);
551 if (!tp_strdiff (group_a, group_b))
552 /* Same group, compare the contacts */
553 return compare_roster_contacts_by_alias (a, b);
556 return compare_group_names (group_a, group_b);
560 compare_roster_contacts (EmpathyRosterView *self,
561 EmpathyRosterContact *a,
562 EmpathyRosterContact *b)
564 if (!self->priv->show_groups)
565 return compare_roster_contacts_no_group (self, a, b);
567 return compare_roster_contacts_with_groups (self, a, b);
571 compare_roster_groups (EmpathyRosterGroup *a,
572 EmpathyRosterGroup *b)
574 const gchar *name_a, *name_b;
576 name_a = empathy_roster_group_get_name (a);
577 name_b = empathy_roster_group_get_name (b);
579 return compare_group_names (name_a, name_b);
583 compare_contact_group (EmpathyRosterContact *contact,
584 EmpathyRosterGroup *group)
586 const char *contact_group, *group_name;
588 contact_group = empathy_roster_contact_get_group (contact);
589 group_name = empathy_roster_group_get_name (group);
591 if (!tp_strdiff (contact_group, group_name))
592 /* @contact is in @group, @group has to be displayed first */
595 /* @contact is in a different group, sort by group name */
596 return compare_group_names (contact_group, group_name);
600 roster_view_sort (gconstpointer a,
604 EmpathyRosterView *self = user_data;
606 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
607 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
608 EMPATHY_ROSTER_CONTACT (b));
609 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
610 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
611 EMPATHY_ROSTER_GROUP (b));
612 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
613 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
614 EMPATHY_ROSTER_GROUP (b));
615 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
616 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
617 EMPATHY_ROSTER_GROUP (a));
619 g_return_val_if_reached (0);
623 update_separator (GtkWidget **separator,
630 /* No separator before the first row */
631 g_clear_object (separator);
635 if (*separator != NULL)
638 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
639 g_object_ref_sink (*separator);
643 is_searching (EmpathyRosterView *self)
645 if (self->priv->search == NULL)
648 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
652 update_empty (EmpathyRosterView *self,
655 if (self->priv->empty == empty)
658 self->priv->empty = empty;
659 g_object_notify (G_OBJECT (self), "empty");
663 add_to_displayed (EmpathyRosterView *self,
664 EmpathyRosterContact *contact)
666 FolksIndividual *individual;
667 GHashTable *contacts;
671 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
674 g_hash_table_add (self->priv->displayed_contacts, contact);
675 update_empty (self, FALSE);
677 /* Groups of this contact may now be displayed if we just displays the first
678 * child in this group. */
680 if (!self->priv->show_groups)
683 individual = empathy_roster_contact_get_individual (contact);
684 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
685 if (contacts == NULL)
688 g_hash_table_iter_init (&iter, contacts);
689 while (g_hash_table_iter_next (&iter, &k, NULL))
691 const gchar *group_name = k;
694 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
698 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
703 remove_from_displayed (EmpathyRosterView *self,
704 EmpathyRosterContact *contact)
706 g_hash_table_remove (self->priv->displayed_contacts, contact);
708 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
709 update_empty (self, TRUE);
713 * check if @contact should be displayed according to @self's current status
714 * and without consideration for the state of @contact's groups.
717 contact_should_be_displayed (EmpathyRosterView *self,
718 EmpathyRosterContact *contact)
720 if (is_searching (self))
722 FolksIndividual *individual;
724 individual = empathy_roster_contact_get_individual (contact);
726 return empathy_individual_match_string (individual,
727 empathy_live_search_get_text (self->priv->search),
728 empathy_live_search_get_words (self->priv->search));
731 if (self->priv->show_offline)
734 if (empathy_roster_model_contact_in_top (self->priv->model,
737 const gchar *group_name;
739 if (!self->priv->show_groups)
740 /* Always display favourite contacts in non-group mode. */
743 group_name = empathy_roster_contact_get_group (contact);
745 if (!tp_strdiff (group_name, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
746 /* Always display favourite contact in group mode only in the
751 return empathy_roster_contact_is_online (contact);
756 filter_contact (EmpathyRosterView *self,
757 EmpathyRosterContact *contact)
761 displayed = contact_should_be_displayed (self, contact);
763 if (self->priv->show_groups)
765 const gchar *group_name;
766 EmpathyRosterGroup *group;
768 group_name = empathy_roster_contact_get_group (contact);
769 group = lookup_roster_group (self, group_name);
773 /* When searching, always display even if the group is closed */
774 if (!is_searching (self) &&
775 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
782 add_to_displayed (self, contact);
786 remove_from_displayed (self, contact);
793 filter_group (EmpathyRosterView *self,
794 EmpathyRosterGroup *group)
798 /* Display the group if it contains at least one displayed contact */
799 widgets = empathy_roster_group_get_widgets (group);
800 for (l = widgets; l != NULL; l = g_list_next (l))
802 EmpathyRosterContact *contact = l->data;
804 if (contact_should_be_displayed (self, contact))
812 filter_list (GtkWidget *child,
815 EmpathyRosterView *self = user_data;
817 if (EMPATHY_IS_ROSTER_CONTACT (child))
818 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
820 else if (EMPATHY_IS_ROSTER_GROUP (child))
821 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
823 g_return_val_if_reached (FALSE);
827 populate_view (EmpathyRosterView *self)
829 GList *individuals, *l;
831 individuals = empathy_roster_model_get_individuals (self->priv->model);
832 for (l = individuals; l != NULL; l = g_list_next (l))
834 FolksIndividual *individual = l->data;
836 individual_added (self, individual);
839 g_list_free (individuals);
843 remove_from_group (EmpathyRosterView *self,
844 FolksIndividual *individual,
847 GHashTable *contacts;
849 EmpathyRosterGroup *roster_group;
851 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
852 if (contacts == NULL)
855 contact = g_hash_table_lookup (contacts, group);
859 g_hash_table_remove (contacts, group);
861 if (g_hash_table_size (contacts) == 0)
863 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
866 roster_group = lookup_roster_group (self, group);
868 if (roster_group != NULL)
870 update_group_widgets (self, roster_group,
871 EMPATHY_ROSTER_CONTACT (contact), FALSE);
874 gtk_container_remove (GTK_CONTAINER (self), contact);
878 groups_changed_cb (EmpathyRosterModel *model,
879 FolksIndividual *individual,
882 EmpathyRosterView *self)
884 if (!self->priv->show_groups)
886 egg_list_box_resort (EGG_LIST_BOX (self));
892 add_to_group (self, individual, group);
896 remove_from_group (self, individual, group);
901 empathy_roster_view_constructed (GObject *object)
903 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
904 void (*chain_up) (GObject *) =
905 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
907 if (chain_up != NULL)
910 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
912 populate_view (self);
914 tp_g_signal_connect_object (self->priv->model, "individual-added",
915 G_CALLBACK (individual_added_cb), self, 0);
916 tp_g_signal_connect_object (self->priv->model, "individual-removed",
917 G_CALLBACK (individual_removed_cb), self, 0);
918 tp_g_signal_connect_object (self->priv->model, "groups-changed",
919 G_CALLBACK (groups_changed_cb), self, 0);
921 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
922 roster_view_sort, self, NULL);
924 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
927 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
929 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
933 empathy_roster_view_dispose (GObject *object)
935 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
936 void (*chain_up) (GObject *) =
937 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
939 stop_flashing (self);
941 empathy_roster_view_set_live_search (self, NULL);
942 g_clear_object (&self->priv->model);
944 if (chain_up != NULL)
949 empathy_roster_view_finalize (GObject *object)
951 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
952 void (*chain_up) (GObject *) =
953 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
955 g_hash_table_unref (self->priv->roster_contacts);
956 g_hash_table_unref (self->priv->roster_groups);
957 g_hash_table_unref (self->priv->displayed_contacts);
958 g_queue_free_full (self->priv->events, event_free);
960 if (chain_up != NULL)
965 empathy_roster_view_child_activated (EggListBox *box,
968 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
969 EmpathyRosterContact *contact;
970 FolksIndividual *individual;
973 if (!EMPATHY_IS_ROSTER_CONTACT (child))
976 contact = EMPATHY_ROSTER_CONTACT (child);
977 individual = empathy_roster_contact_get_individual (contact);
979 /* Activate the oldest event associated with this contact, if any */
980 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
981 l = g_list_previous (l))
983 Event *event = l->data;
985 if (event->individual == individual)
987 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
993 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
997 fire_popup_individual_menu (EmpathyRosterView *self,
1002 EmpathyRosterContact *contact;
1003 FolksIndividual *individual;
1005 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1008 contact = EMPATHY_ROSTER_CONTACT (child);
1009 individual = empathy_roster_contact_get_individual (contact);
1011 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1012 individual, button, time);
1016 empathy_roster_view_button_press_event (GtkWidget *widget,
1017 GdkEventButton *event)
1019 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1020 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1021 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1023 if (event->button == 3)
1027 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1031 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1033 fire_popup_individual_menu (self, child, event->button, event->time);
1037 return chain_up (widget, event);
1041 empathy_roster_view_key_press_event (GtkWidget *widget,
1044 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1045 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1046 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1048 if (event->keyval == GDK_KEY_Menu)
1052 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1055 fire_popup_individual_menu (self, child, 0, event->time);
1058 return chain_up (widget, event);
1062 * @out_child: (out) (allow-none)
1065 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1067 GtkWidget **out_child)
1071 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1073 if (out_child != NULL)
1076 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1079 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1083 * @out_child: (out) (allow-none)
1086 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1091 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1093 if (EMPATHY_IS_ROSTER_CONTACT (child))
1094 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1095 else if (EMPATHY_IS_ROSTER_GROUP (child))
1096 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1102 empathy_roster_view_query_tooltip (GtkWidget *widget,
1105 gboolean keyboard_mode,
1106 GtkTooltip *tooltip)
1108 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1109 FolksIndividual *individual;
1113 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1114 if (individual == NULL)
1117 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1118 individual, keyboard_mode, tooltip, &result);
1122 GtkAllocation allocation;
1124 gtk_widget_get_allocation (child, &allocation);
1125 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1132 empathy_roster_view_remove (GtkContainer *container,
1135 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1136 void (*chain_up) (GtkContainer *, GtkWidget *) =
1137 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1139 chain_up (container, widget);
1141 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1142 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1146 empathy_roster_view_class_init (
1147 EmpathyRosterViewClass *klass)
1149 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1150 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1151 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1152 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1155 oclass->get_property = empathy_roster_view_get_property;
1156 oclass->set_property = empathy_roster_view_set_property;
1157 oclass->constructed = empathy_roster_view_constructed;
1158 oclass->dispose = empathy_roster_view_dispose;
1159 oclass->finalize = empathy_roster_view_finalize;
1161 widget_class->button_press_event = empathy_roster_view_button_press_event;
1162 widget_class->key_press_event = empathy_roster_view_key_press_event;
1163 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1165 container_class->remove = empathy_roster_view_remove;
1167 box_class->child_activated = empathy_roster_view_child_activated;
1169 spec = g_param_spec_object ("model", "Model",
1170 "EmpathyRosterModel",
1171 EMPATHY_TYPE_ROSTER_MODEL,
1172 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1173 g_object_class_install_property (oclass, PROP_MODEL, spec);
1175 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1176 "Show offline contacts",
1178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1179 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1181 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1185 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1187 spec = g_param_spec_boolean ("empty", "Empty",
1188 "Is the view currently empty?",
1190 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1191 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1193 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1194 G_OBJECT_CLASS_TYPE (klass),
1196 0, NULL, NULL, NULL,
1198 1, FOLKS_TYPE_INDIVIDUAL);
1200 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1201 G_OBJECT_CLASS_TYPE (klass),
1203 0, NULL, NULL, NULL,
1205 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1207 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1208 G_OBJECT_CLASS_TYPE (klass),
1210 0, NULL, NULL, NULL,
1212 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1214 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1215 G_OBJECT_CLASS_TYPE (klass),
1217 0, g_signal_accumulator_true_handled, NULL, NULL,
1219 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1221 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1225 empathy_roster_view_init (EmpathyRosterView *self)
1227 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1228 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1230 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1231 NULL, (GDestroyNotify) g_hash_table_unref);
1232 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1234 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1236 self->priv->events = g_queue_new ();
1238 self->priv->empty = TRUE;
1242 empathy_roster_view_new (EmpathyRosterModel *model)
1244 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1246 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1252 empathy_roster_view_show_offline (EmpathyRosterView *self,
1255 if (self->priv->show_offline == show)
1258 self->priv->show_offline = show;
1259 egg_list_box_refilter (EGG_LIST_BOX (self));
1261 g_object_notify (G_OBJECT (self), "show-offline");
1265 clear_view (EmpathyRosterView *self)
1267 gtk_container_foreach (GTK_CONTAINER (self),
1268 (GtkCallback) gtk_widget_destroy, NULL);
1270 g_hash_table_remove_all (self->priv->roster_contacts);
1271 g_hash_table_remove_all (self->priv->roster_groups);
1272 g_hash_table_remove_all (self->priv->displayed_contacts);
1276 empathy_roster_view_show_groups (EmpathyRosterView *self,
1279 if (self->priv->show_groups == show)
1282 self->priv->show_groups = show;
1284 /* TODO: block sort/filter? */
1286 populate_view (self);
1288 g_object_notify (G_OBJECT (self), "show-groups");
1292 select_first_contact (EmpathyRosterView *self)
1294 GList *children, *l;
1296 children = gtk_container_get_children (GTK_CONTAINER (self));
1297 for (l = children; l != NULL; l = g_list_next (l))
1299 GtkWidget *child = l->data;
1301 if (!gtk_widget_get_child_visible (child))
1304 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1307 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1311 g_list_free (children);
1315 search_text_notify_cb (EmpathyLiveSearch *search,
1317 EmpathyRosterView *self)
1319 egg_list_box_refilter (EGG_LIST_BOX (self));
1321 select_first_contact (self);
1325 search_activate_cb (GtkWidget *search,
1326 EmpathyRosterView *self)
1328 EggListBox *box = EGG_LIST_BOX (self);
1331 child = egg_list_box_get_selected_child (box);
1335 empathy_roster_view_child_activated (box, child);
1339 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1340 EmpathyLiveSearch *search)
1342 if (self->priv->search != NULL)
1344 g_signal_handlers_disconnect_by_func (self->priv->search,
1345 search_text_notify_cb, self);
1346 g_signal_handlers_disconnect_by_func (self->priv->search,
1347 search_activate_cb, self);
1349 g_clear_object (&self->priv->search);
1355 self->priv->search = g_object_ref (search);
1357 g_signal_connect (self->priv->search, "notify::text",
1358 G_CALLBACK (search_text_notify_cb), self);
1359 g_signal_connect (self->priv->search, "activate",
1360 G_CALLBACK (search_activate_cb), self);
1364 empathy_roster_view_is_empty (EmpathyRosterView *self)
1366 return self->priv->empty;
1370 empathy_roster_view_is_searching (EmpathyRosterView *self)
1372 return (self->priv->search != NULL &&
1373 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1376 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1377 * Empathy's internals. */
1379 empathy_roster_view_add_event (EmpathyRosterView *self,
1380 FolksIndividual *individual,
1384 GHashTable *contacts;
1386 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1387 if (contacts == NULL)
1390 self->priv->last_event_id++;
1392 g_queue_push_head (self->priv->events,
1393 event_new (self->priv->last_event_id, individual, icon, user_data));
1395 start_flashing (self);
1397 return self->priv->last_event_id;
1401 empathy_roster_view_remove_event (EmpathyRosterView *self,
1406 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1407 l = g_list_next (l))
1409 Event *event = l->data;
1411 if (event->id == event_id)
1413 remove_event (self, event);
1420 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1424 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1426 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1429 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));