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 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
27 SIG_INDIVIDUAL_ACTIVATED,
28 SIG_POPUP_INDIVIDUAL_MENU,
33 static guint signals[LAST_SIGNAL];
35 #define NO_GROUP "X-no-group"
36 #define UNGROUPPED _("Ungroupped")
37 #define TOP_GROUP _("Most Used")
39 struct _EmpathyRosterViewPriv
41 EmpathyIndividualManager *manager;
43 /* FolksIndividual (borrowed) -> GHashTable (
44 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
46 * When not using groups, this hash just have one element mapped
47 * from the special NO_GROUP key. We could use it as a set but
48 * I prefer to stay coherent in the way this hash is managed.
50 GHashTable *roster_contacts;
51 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
52 GHashTable *roster_groups;
53 /* Hash of the EmpathyRosterContact currently displayed */
54 GHashTable *displayed_contacts;
57 /* queue of (Event *). The most recent events are in the head of the queue
58 * so we always display the icon of the oldest one. */
61 gboolean display_flash_event;
63 gboolean show_offline;
67 EmpathyLiveSearch *search;
69 EmpathyRosterViewIndividualTooltipCb individual_tooltip_cb;
70 gpointer individual_tooltip_data;
76 FolksIndividual *individual;
83 FolksIndividual *individual,
87 Event *event = g_slice_new (Event);
90 event->individual = g_object_ref (individual);
91 event->icon = g_strdup (icon);
92 event->user_data = user_data;
97 event_free (gpointer data)
100 g_object_unref (event->individual);
101 g_free (event->icon);
103 g_slice_free (Event, event);
107 empathy_roster_view_get_property (GObject *object,
112 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
117 g_value_set_object (value, self->priv->manager);
119 case PROP_SHOW_OFFLINE:
120 g_value_set_boolean (value, self->priv->show_offline);
122 case PROP_SHOW_GROUPS:
123 g_value_set_boolean (value, self->priv->show_groups);
126 g_value_set_boolean (value, self->priv->empty);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
135 empathy_roster_view_set_property (GObject *object,
140 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
145 g_assert (self->priv->manager == NULL); /* construct only */
146 self->priv->manager = g_value_dup_object (value);
148 case PROP_SHOW_OFFLINE:
149 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
151 case PROP_SHOW_GROUPS:
152 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
161 roster_contact_changed_cb (GtkWidget *child,
163 EmpathyRosterView *self)
165 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
169 add_roster_contact (EmpathyRosterView *self,
170 FolksIndividual *individual,
175 contact = empathy_roster_contact_new (individual, group);
177 /* Need to refilter if online is changed */
178 g_signal_connect (contact, "notify::online",
179 G_CALLBACK (roster_contact_changed_cb), self);
181 /* Need to resort if alias is changed */
182 g_signal_connect (contact, "notify::alias",
183 G_CALLBACK (roster_contact_changed_cb), self);
185 gtk_widget_show (contact);
186 gtk_container_add (GTK_CONTAINER (self), contact);
192 group_expanded_cb (EmpathyRosterGroup *group,
194 EmpathyRosterView *self)
198 widgets = empathy_roster_group_get_widgets (group);
199 for (l = widgets; l != NULL; l = g_list_next (l))
201 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
204 g_list_free (widgets);
207 static EmpathyRosterGroup *
208 lookup_roster_group (EmpathyRosterView *self,
211 return g_hash_table_lookup (self->priv->roster_groups, group);
215 ensure_roster_group (EmpathyRosterView *self,
218 GtkWidget *roster_group;
220 roster_group = (GtkWidget *) lookup_roster_group (self, group);
221 if (roster_group != NULL)
224 roster_group = empathy_roster_group_new (group);
226 g_signal_connect (roster_group, "notify::expanded",
227 G_CALLBACK (group_expanded_cb), self);
229 gtk_widget_show (roster_group);
230 gtk_container_add (GTK_CONTAINER (self), roster_group);
232 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
237 add_to_group (EmpathyRosterView *self,
238 FolksIndividual *individual,
242 GHashTable *contacts;
244 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
245 if (contacts == NULL)
248 if (tp_strdiff (group, NO_GROUP))
249 ensure_roster_group (self, group);
251 contact = add_roster_contact (self, individual, group);
252 g_hash_table_insert (contacts, g_strdup (group), contact);
256 individual_added (EmpathyRosterView *self,
257 FolksIndividual *individual)
259 GHashTable *contacts;
261 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
262 if (contacts != NULL)
265 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
267 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
269 if (!self->priv->show_groups)
271 add_to_group (self, individual, NO_GROUP);
277 groups = folks_group_details_get_groups (
278 FOLKS_GROUP_DETAILS (individual));
280 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
282 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
284 while (iter != NULL && gee_iterator_next (iter))
286 gchar *group = gee_iterator_get (iter);
288 add_to_group (self, individual, group);
293 g_clear_object (&iter);
297 /* No group, adds to Ungroupped */
298 add_to_group (self, individual, UNGROUPPED);
304 update_group_widgets_count (EmpathyRosterView *self,
305 EmpathyRosterGroup *group,
306 EmpathyRosterContact *contact,
311 if (empathy_roster_group_add_widget (group, GTK_WIDGET (contact)) == 1)
313 egg_list_box_child_changed (EGG_LIST_BOX (self),
319 if (empathy_roster_group_remove_widget (group, GTK_WIDGET (contact)) == 0)
321 egg_list_box_child_changed (EGG_LIST_BOX (self),
328 set_event_icon_on_individual (EmpathyRosterView *self,
329 FolksIndividual *individual,
332 GHashTable *contacts;
336 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
337 if (contacts == NULL)
340 g_hash_table_iter_init (&iter, contacts);
341 while (g_hash_table_iter_next (&iter, NULL, &v))
343 EmpathyRosterContact *contact =v;
345 empathy_roster_contact_set_event_icon (contact, icon);
350 flash_event (Event *event,
351 EmpathyRosterView *self)
353 set_event_icon_on_individual (self, event->individual, event->icon);
357 unflash_event (Event *event,
358 EmpathyRosterView *self)
360 set_event_icon_on_individual (self, event->individual, NULL);
364 flash_cb (gpointer data)
366 EmpathyRosterView *self = data;
368 if (self->priv->display_flash_event)
370 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
371 self->priv->display_flash_event = FALSE;
375 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
376 self->priv->display_flash_event = TRUE;
383 start_flashing (EmpathyRosterView *self)
385 if (self->priv->flash_id != 0)
388 self->priv->display_flash_event = TRUE;
390 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
395 stop_flashing (EmpathyRosterView *self)
397 if (self->priv->flash_id == 0)
400 g_source_remove (self->priv->flash_id);
401 self->priv->flash_id = 0;
405 remove_event (EmpathyRosterView *self,
408 unflash_event (event, self);
409 g_queue_remove (self->priv->events, event);
411 if (g_queue_get_length (self->priv->events) == 0)
413 stop_flashing (self);
418 remove_all_individual_event (EmpathyRosterView *self,
419 FolksIndividual *individual)
423 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
426 Event *event = l->data;
428 if (event->individual == individual)
430 remove_event (self, event);
437 individual_removed (EmpathyRosterView *self,
438 FolksIndividual *individual)
440 GHashTable *contacts;
444 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
445 if (contacts == NULL)
448 remove_all_individual_event (self, individual);
450 g_hash_table_iter_init (&iter, contacts);
451 while (g_hash_table_iter_next (&iter, &key, &value))
453 const gchar *group_name = key;
454 GtkWidget *contact = value;
455 EmpathyRosterGroup *group;
457 group = lookup_roster_group (self, group_name);
460 update_group_widgets_count (self, group,
461 EMPATHY_ROSTER_CONTACT (contact), FALSE);
464 gtk_container_remove (GTK_CONTAINER (self), contact);
467 g_hash_table_remove (self->priv->roster_contacts, individual);
471 members_changed_cb (EmpathyIndividualManager *manager,
472 const gchar *message,
475 TpChannelGroupChangeReason reason,
476 EmpathyRosterView *self)
480 for (l = added; l != NULL; l = g_list_next (l))
482 FolksIndividual *individual = l->data;
484 individual_added (self, individual);
487 for (l = removed; l != NULL; l = g_list_next (l))
489 FolksIndividual *individual = l->data;
491 individual_removed (self, individual);
496 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
497 EmpathyRosterContact *b)
499 FolksIndividual *ind_a, *ind_b;
500 const gchar *alias_a, *alias_b;
502 ind_a = empathy_roster_contact_get_individual (a);
503 ind_b = empathy_roster_contact_get_individual (b);
505 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
506 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
508 return g_ascii_strcasecmp (alias_a, alias_b);
512 compare_individual_top_position (EmpathyRosterView *self,
513 EmpathyRosterContact *a,
514 EmpathyRosterContact *b)
516 FolksIndividual *ind_a, *ind_b;
518 gint index_a, index_b;
520 ind_a = empathy_roster_contact_get_individual (a);
521 ind_b = empathy_roster_contact_get_individual (b);
523 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
525 index_a = g_list_index (tops, ind_a);
526 index_b = g_list_index (tops, ind_b);
528 if (index_a == index_b)
537 return index_a - index_b;
541 compare_roster_contacts_no_group (EmpathyRosterView *self,
542 EmpathyRosterContact *a,
543 EmpathyRosterContact *b)
547 top = compare_individual_top_position (self, a, b);
551 return compare_roster_contacts_by_alias (a, b);
555 compare_group_names (const gchar *group_a,
556 const gchar *group_b)
558 if (!tp_strdiff (group_a, TOP_GROUP))
561 if (!tp_strdiff (group_b, TOP_GROUP))
564 return g_ascii_strcasecmp (group_a, group_b);
568 compare_roster_contacts_with_groups (EmpathyRosterView *self,
569 EmpathyRosterContact *a,
570 EmpathyRosterContact *b)
572 const gchar *group_a, *group_b;
574 group_a = empathy_roster_contact_get_group (a);
575 group_b = empathy_roster_contact_get_group (b);
577 if (!tp_strdiff (group_a, group_b))
578 /* Same group, compare the contacts */
579 return compare_roster_contacts_by_alias (a, b);
582 return compare_group_names (group_a, group_b);
586 compare_roster_contacts (EmpathyRosterView *self,
587 EmpathyRosterContact *a,
588 EmpathyRosterContact *b)
590 if (!self->priv->show_groups)
591 return compare_roster_contacts_no_group (self, a, b);
593 return compare_roster_contacts_with_groups (self, a, b);
597 compare_roster_groups (EmpathyRosterGroup *a,
598 EmpathyRosterGroup *b)
600 const gchar *name_a, *name_b;
602 name_a = empathy_roster_group_get_name (a);
603 name_b = empathy_roster_group_get_name (b);
605 return compare_group_names (name_a, name_b);
609 compare_contact_group (EmpathyRosterContact *contact,
610 EmpathyRosterGroup *group)
612 const char *contact_group, *group_name;
614 contact_group = empathy_roster_contact_get_group (contact);
615 group_name = empathy_roster_group_get_name (group);
617 if (!tp_strdiff (contact_group, group_name))
618 /* @contact is in @group, @group has to be displayed first */
621 /* @contact is in a different group, sort by group name */
622 return compare_group_names (contact_group, group_name);
626 roster_view_sort (gconstpointer a,
630 EmpathyRosterView *self = user_data;
632 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
633 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
634 EMPATHY_ROSTER_CONTACT (b));
635 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
636 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
637 EMPATHY_ROSTER_GROUP (b));
638 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
639 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
640 EMPATHY_ROSTER_GROUP (b));
641 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
642 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
643 EMPATHY_ROSTER_GROUP (a));
645 g_return_val_if_reached (0);
649 update_separator (GtkWidget **separator,
656 /* No separator before the first row */
657 g_clear_object (separator);
661 if (*separator != NULL)
664 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
665 g_object_ref_sink (*separator);
669 is_searching (EmpathyRosterView *self)
671 if (self->priv->search == NULL)
674 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
678 update_empty (EmpathyRosterView *self,
681 if (self->priv->empty == empty)
684 self->priv->empty = empty;
685 g_object_notify (G_OBJECT (self), "empty");
689 add_to_displayed (EmpathyRosterView *self,
690 EmpathyRosterContact *contact)
692 g_hash_table_add (self->priv->displayed_contacts, contact);
693 update_empty (self, FALSE);
697 remove_from_displayed (EmpathyRosterView *self,
698 EmpathyRosterContact *contact)
700 g_hash_table_remove (self->priv->displayed_contacts, contact);
702 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
703 update_empty (self, TRUE);
707 filter_contact (EmpathyRosterView *self,
708 EmpathyRosterContact *contact)
712 if (is_searching (self))
714 FolksIndividual *individual;
716 individual = empathy_roster_contact_get_individual (contact);
718 displayed = empathy_individual_match_string (individual,
719 empathy_live_search_get_text (self->priv->search),
720 empathy_live_search_get_words (self->priv->search));
724 if (self->priv->show_offline)
730 displayed = empathy_roster_contact_is_online (contact);
734 if (self->priv->show_groups)
736 const gchar *group_name;
737 EmpathyRosterGroup *group;
739 group_name = empathy_roster_contact_get_group (contact);
740 group = lookup_roster_group (self, group_name);
744 update_group_widgets_count (self, group, contact, displayed);
746 /* When searching, always display even if the group is closed */
747 if (!is_searching (self) &&
748 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
755 add_to_displayed (self, contact);
759 remove_from_displayed (self, contact);
766 filter_group (EmpathyRosterView *self,
767 EmpathyRosterGroup *group)
769 return empathy_roster_group_get_widgets_count (group);
773 filter_list (GtkWidget *child,
776 EmpathyRosterView *self = user_data;
778 if (EMPATHY_IS_ROSTER_CONTACT (child))
779 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
781 else if (EMPATHY_IS_ROSTER_GROUP (child))
782 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
784 g_return_val_if_reached (FALSE);
787 /* @list: GList of EmpathyRosterContact
789 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
792 individual_in_list (FolksIndividual *individual,
797 for (l = list; l != NULL; l = g_list_next (l))
799 EmpathyRosterContact *contact = l->data;
801 if (empathy_roster_contact_get_individual (contact) == individual)
809 populate_view (EmpathyRosterView *self)
811 GList *individuals, *l;
813 individuals = empathy_individual_manager_get_members (self->priv->manager);
814 for (l = individuals; l != NULL; l = g_list_next (l))
816 FolksIndividual *individual = l->data;
818 individual_added (self, individual);
821 g_list_free (individuals);
825 remove_from_group (EmpathyRosterView *self,
826 FolksIndividual *individual,
829 GHashTable *contacts;
831 EmpathyRosterGroup *roster_group;
833 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
834 if (contacts == NULL)
837 contact = g_hash_table_lookup (contacts, group);
841 g_hash_table_remove (contacts, group);
843 if (g_hash_table_size (contacts) == 0)
845 add_to_group (self, individual, UNGROUPPED);
848 roster_group = lookup_roster_group (self, group);
850 if (roster_group != NULL)
852 update_group_widgets_count (self, roster_group,
853 EMPATHY_ROSTER_CONTACT (contact), FALSE);
856 gtk_container_remove (GTK_CONTAINER (self), contact);
860 update_top_contacts (EmpathyRosterView *self)
863 GList *to_add = NULL, *to_remove = NULL;
864 EmpathyRosterGroup *group;
866 if (!self->priv->show_groups)
868 egg_list_box_resort (EGG_LIST_BOX (self));
872 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
874 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
877 to_add = g_list_copy (tops);
883 contacts = empathy_roster_group_get_widgets (group);
885 /* Check which EmpathyRosterContact have to be removed */
886 for (l = contacts; l != NULL; l = g_list_next (l))
888 EmpathyRosterContact *contact = l->data;
889 FolksIndividual *individual;
891 individual = empathy_roster_contact_get_individual (contact);
893 if (g_list_find (tops, individual) == NULL)
894 to_remove = g_list_prepend (to_remove, individual);
897 /* Check which EmpathyRosterContact have to be added */
898 for (l = tops; l != NULL; l = g_list_next (l))
900 FolksIndividual *individual = l->data;
902 if (!individual_in_list (individual, contacts))
903 to_add = g_list_prepend (to_add, individual);
907 for (l = to_add; l != NULL; l = g_list_next (l))
908 add_to_group (self, l->data, TOP_GROUP);
910 for (l = to_remove; l != NULL; l = g_list_next (l))
911 remove_from_group (self, l->data, TOP_GROUP);
913 g_list_free (to_add);
914 g_list_free (to_remove);
918 groups_changed_cb (EmpathyIndividualManager *manager,
919 FolksIndividual *individual,
922 EmpathyRosterView *self)
924 if (!self->priv->show_groups)
929 add_to_group (self, individual, group);
933 remove_from_group (self, individual, group);
938 top_individuals_changed_cb (EmpathyIndividualManager *manager,
940 EmpathyRosterView *self)
942 update_top_contacts (self);
946 empathy_roster_view_constructed (GObject *object)
948 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
949 void (*chain_up) (GObject *) =
950 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
952 if (chain_up != NULL)
955 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
957 populate_view (self);
959 tp_g_signal_connect_object (self->priv->manager, "members-changed",
960 G_CALLBACK (members_changed_cb), self, 0);
961 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
962 G_CALLBACK (groups_changed_cb), self, 0);
963 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
964 G_CALLBACK (top_individuals_changed_cb), self, 0);
966 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
967 roster_view_sort, self, NULL);
969 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
972 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
974 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
978 empathy_roster_view_dispose (GObject *object)
980 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
981 void (*chain_up) (GObject *) =
982 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
984 stop_flashing (self);
986 empathy_roster_view_set_live_search (self, NULL);
987 g_clear_object (&self->priv->manager);
989 if (chain_up != NULL)
994 empathy_roster_view_finalize (GObject *object)
996 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
997 void (*chain_up) (GObject *) =
998 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1000 g_hash_table_unref (self->priv->roster_contacts);
1001 g_hash_table_unref (self->priv->roster_groups);
1002 g_hash_table_unref (self->priv->displayed_contacts);
1003 g_queue_free_full (self->priv->events, event_free);
1005 if (chain_up != NULL)
1010 empathy_roster_view_child_activated (EggListBox *box,
1013 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1014 EmpathyRosterContact *contact;
1015 FolksIndividual *individual;
1018 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1021 contact = EMPATHY_ROSTER_CONTACT (child);
1022 individual = empathy_roster_contact_get_individual (contact);
1024 /* Activate the oldest event associated with this contact, if any */
1025 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1026 l = g_list_previous (l))
1028 Event *event = l->data;
1030 if (event->individual == individual)
1032 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1038 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1042 fire_popup_individual_menu (EmpathyRosterView *self,
1047 EmpathyRosterContact *contact;
1048 FolksIndividual *individual;
1050 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1053 contact = EMPATHY_ROSTER_CONTACT (child);
1054 individual = empathy_roster_contact_get_individual (contact);
1056 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1057 individual, button, time);
1061 empathy_roster_view_button_press_event (GtkWidget *widget,
1062 GdkEventButton *event)
1064 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1065 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1066 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1068 if (event->button == 3)
1072 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1075 fire_popup_individual_menu (self, child, event->button, event->time);
1078 return chain_up (widget, event);
1082 empathy_roster_view_key_press_event (GtkWidget *widget,
1085 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1086 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1087 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1089 if (event->keyval == GDK_KEY_Menu)
1093 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1096 fire_popup_individual_menu (self, child, 0, event->time);
1099 return chain_up (widget, event);
1103 empathy_roster_view_query_tooltip (GtkWidget *widget,
1106 gboolean keyboard_mode,
1107 GtkTooltip *tooltip)
1109 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1111 EmpathyRosterContact *contact;
1112 FolksIndividual *individual;
1114 if (self->priv->individual_tooltip_cb == NULL)
1117 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1118 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1121 contact = EMPATHY_ROSTER_CONTACT (child);
1122 individual = empathy_roster_contact_get_individual (contact);
1124 return self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
1125 tooltip, self->priv->individual_tooltip_data);
1129 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
1130 EmpathyRosterViewIndividualTooltipCb callback,
1133 self->priv->individual_tooltip_cb = callback;
1134 self->priv->individual_tooltip_data = user_data;
1136 gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
1140 empathy_roster_view_remove (GtkContainer *container,
1143 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1144 void (*chain_up) (GtkContainer *, GtkWidget *) =
1145 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1147 chain_up (container, widget);
1149 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1150 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1154 empathy_roster_view_class_init (
1155 EmpathyRosterViewClass *klass)
1157 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1158 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1159 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1160 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1163 oclass->get_property = empathy_roster_view_get_property;
1164 oclass->set_property = empathy_roster_view_set_property;
1165 oclass->constructed = empathy_roster_view_constructed;
1166 oclass->dispose = empathy_roster_view_dispose;
1167 oclass->finalize = empathy_roster_view_finalize;
1169 widget_class->button_press_event = empathy_roster_view_button_press_event;
1170 widget_class->key_press_event = empathy_roster_view_key_press_event;
1171 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1173 container_class->remove = empathy_roster_view_remove;
1175 box_class->child_activated = empathy_roster_view_child_activated;
1177 spec = g_param_spec_object ("manager", "Manager",
1178 "EmpathyIndividualManager",
1179 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1180 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1181 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1183 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1184 "Show offline contacts",
1186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1187 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1189 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1192 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1193 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1195 spec = g_param_spec_boolean ("empty", "Empty",
1196 "Is the view currently empty?",
1198 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1199 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1201 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1202 G_OBJECT_CLASS_TYPE (klass),
1204 0, NULL, NULL, NULL,
1206 1, FOLKS_TYPE_INDIVIDUAL);
1208 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1209 G_OBJECT_CLASS_TYPE (klass),
1211 0, NULL, NULL, NULL,
1213 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1215 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1216 G_OBJECT_CLASS_TYPE (klass),
1218 0, NULL, NULL, NULL,
1220 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1222 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1226 empathy_roster_view_init (EmpathyRosterView *self)
1228 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1229 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1231 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1232 NULL, (GDestroyNotify) g_hash_table_unref);
1233 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1235 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1237 self->priv->events = g_queue_new ();
1239 self->priv->empty = TRUE;
1243 empathy_roster_view_new (EmpathyIndividualManager *manager)
1245 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1247 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1252 EmpathyIndividualManager *
1253 empathy_roster_view_get_manager (EmpathyRosterView *self)
1255 return self->priv->manager;
1259 empathy_roster_view_show_offline (EmpathyRosterView *self,
1262 if (self->priv->show_offline == show)
1265 self->priv->show_offline = show;
1266 egg_list_box_refilter (EGG_LIST_BOX (self));
1268 g_object_notify (G_OBJECT (self), "show-offline");
1272 clear_view (EmpathyRosterView *self)
1274 gtk_container_foreach (GTK_CONTAINER (self),
1275 (GtkCallback) gtk_widget_destroy, NULL);
1277 g_hash_table_remove_all (self->priv->roster_contacts);
1281 empathy_roster_view_show_groups (EmpathyRosterView *self,
1284 if (self->priv->show_groups == show)
1287 self->priv->show_groups = show;
1289 /* TODO: block sort/filter? */
1291 populate_view (self);
1293 g_object_notify (G_OBJECT (self), "show-groups");
1297 select_first_contact (EmpathyRosterView *self)
1299 GList *children, *l;
1301 children = gtk_container_get_children (GTK_CONTAINER (self));
1302 for (l = children; l != NULL; l = g_list_next (l))
1304 GtkWidget *child = l->data;
1306 if (!gtk_widget_get_child_visible (child))
1309 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1312 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1316 g_list_free (children);
1320 search_text_notify_cb (EmpathyLiveSearch *search,
1322 EmpathyRosterView *self)
1324 egg_list_box_refilter (EGG_LIST_BOX (self));
1326 select_first_contact (self);
1330 search_activate_cb (GtkWidget *search,
1331 EmpathyRosterView *self)
1333 EggListBox *box = EGG_LIST_BOX (self);
1336 child = egg_list_box_get_selected_child (box);
1340 empathy_roster_view_child_activated (box, child);
1344 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1345 EmpathyLiveSearch *search)
1347 if (self->priv->search != NULL)
1349 g_signal_handlers_disconnect_by_func (self->priv->search,
1350 search_text_notify_cb, self);
1351 g_signal_handlers_disconnect_by_func (self->priv->search,
1352 search_activate_cb, self);
1354 g_clear_object (&self->priv->search);
1360 self->priv->search = g_object_ref (search);
1362 g_signal_connect (self->priv->search, "notify::text",
1363 G_CALLBACK (search_text_notify_cb), self);
1364 g_signal_connect (self->priv->search, "activate",
1365 G_CALLBACK (search_activate_cb), self);
1369 empathy_roster_view_is_empty (EmpathyRosterView *self)
1371 return self->priv->empty;
1375 empathy_roster_view_is_searching (EmpathyRosterView *self)
1377 return (self->priv->search != NULL &&
1378 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1381 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1382 * Empathy's internals. */
1384 empathy_roster_view_add_event (EmpathyRosterView *self,
1385 FolksIndividual *individual,
1389 GHashTable *contacts;
1391 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1392 if (contacts == NULL)
1395 self->priv->last_event_id++;
1397 g_queue_push_head (self->priv->events,
1398 event_new (self->priv->last_event_id, individual, icon, user_data));
1400 start_flashing (self);
1402 return self->priv->last_event_id;
1406 empathy_roster_view_remove_event (EmpathyRosterView *self,
1411 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1412 l = g_list_next (l))
1414 Event *event = l->data;
1416 if (event->id == event_id)
1418 remove_event (self, event);