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 (g_hash_table_lookup (contacts, group) != NULL)
276 if (tp_strdiff (group, NO_GROUP))
277 roster_group = ensure_roster_group (self, group);
279 contact = add_roster_contact (self, individual, group);
280 g_hash_table_insert (contacts, g_strdup (group), contact);
282 if (roster_group != NULL)
284 update_group_widgets (self, roster_group,
285 EMPATHY_ROSTER_CONTACT (contact), TRUE);
290 individual_added (EmpathyRosterView *self,
291 FolksIndividual *individual)
293 GHashTable *contacts;
295 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
296 if (contacts != NULL)
299 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
301 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
303 if (!self->priv->show_groups)
305 add_to_group (self, individual, NO_GROUP);
311 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
314 if (g_list_length (groups) > 0)
316 for (l = groups; l != NULL; l = g_list_next (l))
318 add_to_group (self, individual, l->data);
323 /* No group, adds to Ungrouped */
324 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
327 g_list_free (groups);
332 set_event_icon_on_individual (EmpathyRosterView *self,
333 FolksIndividual *individual,
336 GHashTable *contacts;
340 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
341 if (contacts == NULL)
344 g_hash_table_iter_init (&iter, contacts);
345 while (g_hash_table_iter_next (&iter, NULL, &v))
347 EmpathyRosterContact *contact =v;
349 empathy_roster_contact_set_event_icon (contact, icon);
354 flash_event (Event *event,
355 EmpathyRosterView *self)
357 set_event_icon_on_individual (self, event->individual, event->icon);
361 unflash_event (Event *event,
362 EmpathyRosterView *self)
364 set_event_icon_on_individual (self, event->individual, NULL);
368 flash_cb (gpointer data)
370 EmpathyRosterView *self = data;
372 if (self->priv->display_flash_event)
374 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
375 self->priv->display_flash_event = FALSE;
379 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
380 self->priv->display_flash_event = TRUE;
387 start_flashing (EmpathyRosterView *self)
389 if (self->priv->flash_id != 0)
392 self->priv->display_flash_event = TRUE;
394 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
399 stop_flashing (EmpathyRosterView *self)
401 if (self->priv->flash_id == 0)
404 g_source_remove (self->priv->flash_id);
405 self->priv->flash_id = 0;
409 remove_event (EmpathyRosterView *self,
412 unflash_event (event, self);
413 g_queue_remove (self->priv->events, event);
415 if (g_queue_get_length (self->priv->events) == 0)
417 stop_flashing (self);
422 remove_all_individual_event (EmpathyRosterView *self,
423 FolksIndividual *individual)
427 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
430 Event *event = l->data;
432 if (event->individual == individual)
434 remove_event (self, event);
441 individual_removed (EmpathyRosterView *self,
442 FolksIndividual *individual)
444 GHashTable *contacts;
448 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
449 if (contacts == NULL)
452 remove_all_individual_event (self, individual);
454 g_hash_table_iter_init (&iter, contacts);
455 while (g_hash_table_iter_next (&iter, &key, &value))
457 const gchar *group_name = key;
458 GtkWidget *contact = value;
459 EmpathyRosterGroup *group;
461 group = lookup_roster_group (self, group_name);
464 update_group_widgets (self, group,
465 EMPATHY_ROSTER_CONTACT (contact), FALSE);
468 gtk_container_remove (GTK_CONTAINER (self), contact);
471 g_hash_table_remove (self->priv->roster_contacts, individual);
475 individual_added_cb (EmpathyRosterModel *model,
476 FolksIndividual *individual,
477 EmpathyRosterView *self)
479 individual_added (self, individual);
483 individual_removed_cb (EmpathyRosterModel *model,
484 FolksIndividual *individual,
485 EmpathyRosterView *self)
487 individual_removed (self, individual);
491 contact_in_top (EmpathyRosterView *self,
492 EmpathyRosterContact *contact)
494 if (!self->priv->show_groups)
496 /* Always display top contacts in non-group mode. */
498 FolksIndividual *individual;
499 gboolean result = FALSE;
501 individual = empathy_roster_contact_get_individual (contact);
503 groups = empathy_roster_model_get_groups_for_individual (
504 self->priv->model, individual);
506 if (g_list_find (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP) != NULL)
509 g_list_free (groups);
514 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
515 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
516 /* If we are displaying contacts, we only want to *always* display the
517 * RosterContact which is displayed at the top; not the ones displayed in
518 * the 'normal' group sections */
525 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
526 EmpathyRosterContact *b)
528 FolksIndividual *ind_a, *ind_b;
529 const gchar *alias_a, *alias_b;
531 ind_a = empathy_roster_contact_get_individual (a);
532 ind_b = empathy_roster_contact_get_individual (b);
534 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
535 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
537 return g_ascii_strcasecmp (alias_a, alias_b);
541 compare_roster_contacts_no_group (EmpathyRosterView *self,
542 EmpathyRosterContact *a,
543 EmpathyRosterContact *b)
545 gboolean top_a, top_b;
547 top_a = contact_in_top (self, a);
548 top_b = contact_in_top (self, b);
551 /* Both contacts are in the top of the roster (or not). Sort them
553 return compare_roster_contacts_by_alias (a, b);
561 compare_group_names (const gchar *group_a,
562 const gchar *group_b)
564 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
567 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
570 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
572 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
575 return g_ascii_strcasecmp (group_a, group_b);
579 compare_roster_contacts_with_groups (EmpathyRosterView *self,
580 EmpathyRosterContact *a,
581 EmpathyRosterContact *b)
583 const gchar *group_a, *group_b;
585 group_a = empathy_roster_contact_get_group (a);
586 group_b = empathy_roster_contact_get_group (b);
588 if (!tp_strdiff (group_a, group_b))
589 /* Same group, compare the contacts */
590 return compare_roster_contacts_by_alias (a, b);
593 return compare_group_names (group_a, group_b);
597 compare_roster_contacts (EmpathyRosterView *self,
598 EmpathyRosterContact *a,
599 EmpathyRosterContact *b)
601 if (!self->priv->show_groups)
602 return compare_roster_contacts_no_group (self, a, b);
604 return compare_roster_contacts_with_groups (self, a, b);
608 compare_roster_groups (EmpathyRosterGroup *a,
609 EmpathyRosterGroup *b)
611 const gchar *name_a, *name_b;
613 name_a = empathy_roster_group_get_name (a);
614 name_b = empathy_roster_group_get_name (b);
616 return compare_group_names (name_a, name_b);
620 compare_contact_group (EmpathyRosterContact *contact,
621 EmpathyRosterGroup *group)
623 const char *contact_group, *group_name;
625 contact_group = empathy_roster_contact_get_group (contact);
626 group_name = empathy_roster_group_get_name (group);
628 if (!tp_strdiff (contact_group, group_name))
629 /* @contact is in @group, @group has to be displayed first */
632 /* @contact is in a different group, sort by group name */
633 return compare_group_names (contact_group, group_name);
637 roster_view_sort (gconstpointer a,
641 EmpathyRosterView *self = user_data;
643 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
644 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
645 EMPATHY_ROSTER_CONTACT (b));
646 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
647 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
648 EMPATHY_ROSTER_GROUP (b));
649 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
650 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
651 EMPATHY_ROSTER_GROUP (b));
652 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
653 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
654 EMPATHY_ROSTER_GROUP (a));
656 g_return_val_if_reached (0);
660 update_separator (GtkWidget **separator,
667 /* No separator before the first row */
668 g_clear_object (separator);
672 if (*separator != NULL)
675 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
676 g_object_ref_sink (*separator);
680 is_searching (EmpathyRosterView *self)
682 if (self->priv->search == NULL)
685 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
689 update_empty (EmpathyRosterView *self,
692 if (self->priv->empty == empty)
695 self->priv->empty = empty;
696 g_object_notify (G_OBJECT (self), "empty");
700 add_to_displayed (EmpathyRosterView *self,
701 EmpathyRosterContact *contact)
703 FolksIndividual *individual;
704 GHashTable *contacts;
708 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
711 g_hash_table_add (self->priv->displayed_contacts, contact);
712 update_empty (self, FALSE);
714 /* Groups of this contact may now be displayed if we just displays the first
715 * child in this group. */
717 if (!self->priv->show_groups)
720 individual = empathy_roster_contact_get_individual (contact);
721 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
722 if (contacts == NULL)
725 g_hash_table_iter_init (&iter, contacts);
726 while (g_hash_table_iter_next (&iter, &k, NULL))
728 const gchar *group_name = k;
731 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
735 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
740 remove_from_displayed (EmpathyRosterView *self,
741 EmpathyRosterContact *contact)
743 g_hash_table_remove (self->priv->displayed_contacts, contact);
745 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
746 update_empty (self, TRUE);
750 * check if @contact should be displayed according to @self's current status
751 * and without consideration for the state of @contact's groups.
754 contact_should_be_displayed (EmpathyRosterView *self,
755 EmpathyRosterContact *contact)
757 if (is_searching (self))
759 FolksIndividual *individual;
761 individual = empathy_roster_contact_get_individual (contact);
763 return empathy_individual_match_string (individual,
764 empathy_live_search_get_text (self->priv->search),
765 empathy_live_search_get_words (self->priv->search));
768 if (self->priv->show_offline)
771 if (contact_in_top (self, contact))
774 return empathy_roster_contact_is_online (contact);
779 filter_contact (EmpathyRosterView *self,
780 EmpathyRosterContact *contact)
784 displayed = contact_should_be_displayed (self, contact);
786 if (self->priv->show_groups)
788 const gchar *group_name;
789 EmpathyRosterGroup *group;
791 group_name = empathy_roster_contact_get_group (contact);
792 group = lookup_roster_group (self, group_name);
796 /* When searching, always display even if the group is closed */
797 if (!is_searching (self) &&
798 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
805 add_to_displayed (self, contact);
809 remove_from_displayed (self, contact);
816 filter_group (EmpathyRosterView *self,
817 EmpathyRosterGroup *group)
821 /* Display the group if it contains at least one displayed contact */
822 widgets = empathy_roster_group_get_widgets (group);
823 for (l = widgets; l != NULL; l = g_list_next (l))
825 EmpathyRosterContact *contact = l->data;
827 if (contact_should_be_displayed (self, contact))
835 filter_list (GtkWidget *child,
838 EmpathyRosterView *self = user_data;
840 if (EMPATHY_IS_ROSTER_CONTACT (child))
841 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
843 else if (EMPATHY_IS_ROSTER_GROUP (child))
844 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
846 g_return_val_if_reached (FALSE);
850 populate_view (EmpathyRosterView *self)
852 GList *individuals, *l;
854 individuals = empathy_roster_model_get_individuals (self->priv->model);
855 for (l = individuals; l != NULL; l = g_list_next (l))
857 FolksIndividual *individual = l->data;
859 individual_added (self, individual);
862 g_list_free (individuals);
866 remove_from_group (EmpathyRosterView *self,
867 FolksIndividual *individual,
870 GHashTable *contacts;
872 EmpathyRosterGroup *roster_group;
874 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
875 if (contacts == NULL)
878 contact = g_hash_table_lookup (contacts, group);
882 g_hash_table_remove (contacts, group);
884 if (g_hash_table_size (contacts) == 0)
886 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
889 roster_group = lookup_roster_group (self, group);
891 if (roster_group != NULL)
893 update_group_widgets (self, roster_group,
894 EMPATHY_ROSTER_CONTACT (contact), FALSE);
897 gtk_container_remove (GTK_CONTAINER (self), contact);
901 groups_changed_cb (EmpathyRosterModel *model,
902 FolksIndividual *individual,
905 EmpathyRosterView *self)
907 if (!self->priv->show_groups)
909 egg_list_box_resort (EGG_LIST_BOX (self));
915 add_to_group (self, individual, group);
919 remove_from_group (self, individual, group);
924 empathy_roster_view_constructed (GObject *object)
926 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
927 void (*chain_up) (GObject *) =
928 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
930 if (chain_up != NULL)
933 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
935 populate_view (self);
937 tp_g_signal_connect_object (self->priv->model, "individual-added",
938 G_CALLBACK (individual_added_cb), self, 0);
939 tp_g_signal_connect_object (self->priv->model, "individual-removed",
940 G_CALLBACK (individual_removed_cb), self, 0);
941 tp_g_signal_connect_object (self->priv->model, "groups-changed",
942 G_CALLBACK (groups_changed_cb), self, 0);
944 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
945 roster_view_sort, self, NULL);
947 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
950 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
952 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
956 empathy_roster_view_dispose (GObject *object)
958 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
959 void (*chain_up) (GObject *) =
960 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
962 stop_flashing (self);
964 empathy_roster_view_set_live_search (self, NULL);
965 g_clear_object (&self->priv->model);
967 if (chain_up != NULL)
972 empathy_roster_view_finalize (GObject *object)
974 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
975 void (*chain_up) (GObject *) =
976 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
978 g_hash_table_unref (self->priv->roster_contacts);
979 g_hash_table_unref (self->priv->roster_groups);
980 g_hash_table_unref (self->priv->displayed_contacts);
981 g_queue_free_full (self->priv->events, event_free);
983 if (chain_up != NULL)
988 empathy_roster_view_child_activated (EggListBox *box,
991 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
992 EmpathyRosterContact *contact;
993 FolksIndividual *individual;
996 if (!EMPATHY_IS_ROSTER_CONTACT (child))
999 contact = EMPATHY_ROSTER_CONTACT (child);
1000 individual = empathy_roster_contact_get_individual (contact);
1002 /* Activate the oldest event associated with this contact, if any */
1003 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1004 l = g_list_previous (l))
1006 Event *event = l->data;
1008 if (event->individual == individual)
1010 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1016 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1020 fire_popup_individual_menu (EmpathyRosterView *self,
1025 EmpathyRosterContact *contact;
1026 FolksIndividual *individual;
1028 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1031 contact = EMPATHY_ROSTER_CONTACT (child);
1032 individual = empathy_roster_contact_get_individual (contact);
1034 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1035 individual, button, time);
1039 empathy_roster_view_button_press_event (GtkWidget *widget,
1040 GdkEventButton *event)
1042 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1043 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1044 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1046 if (event->button == 3)
1050 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1054 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1056 fire_popup_individual_menu (self, child, event->button, event->time);
1060 return chain_up (widget, event);
1064 empathy_roster_view_key_press_event (GtkWidget *widget,
1067 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1068 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1069 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1071 if (event->keyval == GDK_KEY_Menu)
1075 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1078 fire_popup_individual_menu (self, child, 0, event->time);
1081 return chain_up (widget, event);
1085 * @out_child: (out) (allow-none)
1088 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1090 GtkWidget **out_child)
1094 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1096 if (out_child != NULL)
1099 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1102 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1106 * @out_child: (out) (allow-none)
1109 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1114 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1116 if (EMPATHY_IS_ROSTER_CONTACT (child))
1117 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1118 else if (EMPATHY_IS_ROSTER_GROUP (child))
1119 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1125 empathy_roster_view_query_tooltip (GtkWidget *widget,
1128 gboolean keyboard_mode,
1129 GtkTooltip *tooltip)
1131 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1132 FolksIndividual *individual;
1136 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1137 if (individual == NULL)
1140 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1141 individual, keyboard_mode, tooltip, &result);
1145 GtkAllocation allocation;
1147 gtk_widget_get_allocation (child, &allocation);
1148 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1155 empathy_roster_view_remove (GtkContainer *container,
1158 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1159 void (*chain_up) (GtkContainer *, GtkWidget *) =
1160 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1162 chain_up (container, widget);
1164 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1165 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1169 empathy_roster_view_class_init (
1170 EmpathyRosterViewClass *klass)
1172 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1173 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1174 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1175 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1178 oclass->get_property = empathy_roster_view_get_property;
1179 oclass->set_property = empathy_roster_view_set_property;
1180 oclass->constructed = empathy_roster_view_constructed;
1181 oclass->dispose = empathy_roster_view_dispose;
1182 oclass->finalize = empathy_roster_view_finalize;
1184 widget_class->button_press_event = empathy_roster_view_button_press_event;
1185 widget_class->key_press_event = empathy_roster_view_key_press_event;
1186 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1188 container_class->remove = empathy_roster_view_remove;
1190 box_class->child_activated = empathy_roster_view_child_activated;
1192 spec = g_param_spec_object ("model", "Model",
1193 "EmpathyRosterModel",
1194 EMPATHY_TYPE_ROSTER_MODEL,
1195 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1196 g_object_class_install_property (oclass, PROP_MODEL, spec);
1198 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1199 "Show offline contacts",
1201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1202 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1204 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1208 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1210 spec = g_param_spec_boolean ("empty", "Empty",
1211 "Is the view currently empty?",
1213 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1214 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1216 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1217 G_OBJECT_CLASS_TYPE (klass),
1219 0, NULL, NULL, NULL,
1221 1, FOLKS_TYPE_INDIVIDUAL);
1223 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1224 G_OBJECT_CLASS_TYPE (klass),
1226 0, NULL, NULL, NULL,
1228 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1230 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1231 G_OBJECT_CLASS_TYPE (klass),
1233 0, NULL, NULL, NULL,
1235 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1237 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1238 G_OBJECT_CLASS_TYPE (klass),
1240 0, g_signal_accumulator_true_handled, NULL, NULL,
1242 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1244 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1248 empathy_roster_view_init (EmpathyRosterView *self)
1250 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1251 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1253 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1254 NULL, (GDestroyNotify) g_hash_table_unref);
1255 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1257 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1259 self->priv->events = g_queue_new ();
1261 self->priv->empty = TRUE;
1265 empathy_roster_view_new (EmpathyRosterModel *model)
1267 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1269 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1275 empathy_roster_view_show_offline (EmpathyRosterView *self,
1278 if (self->priv->show_offline == show)
1281 self->priv->show_offline = show;
1282 egg_list_box_refilter (EGG_LIST_BOX (self));
1284 g_object_notify (G_OBJECT (self), "show-offline");
1288 clear_view (EmpathyRosterView *self)
1290 gtk_container_foreach (GTK_CONTAINER (self),
1291 (GtkCallback) gtk_widget_destroy, NULL);
1293 g_hash_table_remove_all (self->priv->roster_contacts);
1294 g_hash_table_remove_all (self->priv->roster_groups);
1295 g_hash_table_remove_all (self->priv->displayed_contacts);
1299 empathy_roster_view_show_groups (EmpathyRosterView *self,
1302 if (self->priv->show_groups == show)
1305 self->priv->show_groups = show;
1307 /* TODO: block sort/filter? */
1309 populate_view (self);
1311 g_object_notify (G_OBJECT (self), "show-groups");
1315 select_first_contact (EmpathyRosterView *self)
1317 GList *children, *l;
1319 children = gtk_container_get_children (GTK_CONTAINER (self));
1320 for (l = children; l != NULL; l = g_list_next (l))
1322 GtkWidget *child = l->data;
1324 if (!gtk_widget_get_child_visible (child))
1327 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1330 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1334 g_list_free (children);
1338 search_text_notify_cb (EmpathyLiveSearch *search,
1340 EmpathyRosterView *self)
1342 egg_list_box_refilter (EGG_LIST_BOX (self));
1344 select_first_contact (self);
1348 search_activate_cb (GtkWidget *search,
1349 EmpathyRosterView *self)
1351 EggListBox *box = EGG_LIST_BOX (self);
1354 child = egg_list_box_get_selected_child (box);
1358 empathy_roster_view_child_activated (box, child);
1362 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1363 EmpathyLiveSearch *search)
1365 if (self->priv->search != NULL)
1367 g_signal_handlers_disconnect_by_func (self->priv->search,
1368 search_text_notify_cb, self);
1369 g_signal_handlers_disconnect_by_func (self->priv->search,
1370 search_activate_cb, self);
1372 g_clear_object (&self->priv->search);
1378 self->priv->search = g_object_ref (search);
1380 g_signal_connect (self->priv->search, "notify::text",
1381 G_CALLBACK (search_text_notify_cb), self);
1382 g_signal_connect (self->priv->search, "activate",
1383 G_CALLBACK (search_activate_cb), self);
1387 empathy_roster_view_is_empty (EmpathyRosterView *self)
1389 return self->priv->empty;
1393 empathy_roster_view_is_searching (EmpathyRosterView *self)
1395 return (self->priv->search != NULL &&
1396 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1399 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1400 * Empathy's internals. */
1402 empathy_roster_view_add_event (EmpathyRosterView *self,
1403 FolksIndividual *individual,
1407 GHashTable *contacts;
1409 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1410 if (contacts == NULL)
1413 self->priv->last_event_id++;
1415 g_queue_push_head (self->priv->events,
1416 event_new (self->priv->last_event_id, individual, icon, user_data));
1418 start_flashing (self);
1420 return self->priv->last_event_id;
1424 empathy_roster_view_remove_event (EmpathyRosterView *self,
1429 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1430 l = g_list_next (l))
1432 Event *event = l->data;
1434 if (event->id == event_id)
1436 remove_event (self, event);
1443 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1447 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1449 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1452 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));