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 * check if @contact should be displayed according to @self's current status
708 * and without consideration for the state of @contact's groups.
711 contact_should_be_displayed (EmpathyRosterView *self,
712 EmpathyRosterContact *contact)
714 if (is_searching (self))
716 FolksIndividual *individual;
718 individual = empathy_roster_contact_get_individual (contact);
720 return empathy_individual_match_string (individual,
721 empathy_live_search_get_text (self->priv->search),
722 empathy_live_search_get_words (self->priv->search));
726 if (self->priv->show_offline)
732 return empathy_roster_contact_is_online (contact);
738 filter_contact (EmpathyRosterView *self,
739 EmpathyRosterContact *contact)
743 displayed = contact_should_be_displayed (self, contact);
745 if (self->priv->show_groups)
747 const gchar *group_name;
748 EmpathyRosterGroup *group;
750 group_name = empathy_roster_contact_get_group (contact);
751 group = lookup_roster_group (self, group_name);
755 update_group_widgets_count (self, group, contact, displayed);
757 /* When searching, always display even if the group is closed */
758 if (!is_searching (self) &&
759 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
766 add_to_displayed (self, contact);
770 remove_from_displayed (self, contact);
777 filter_group (EmpathyRosterView *self,
778 EmpathyRosterGroup *group)
780 return empathy_roster_group_get_widgets_count (group);
784 filter_list (GtkWidget *child,
787 EmpathyRosterView *self = user_data;
789 if (EMPATHY_IS_ROSTER_CONTACT (child))
790 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
792 else if (EMPATHY_IS_ROSTER_GROUP (child))
793 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
795 g_return_val_if_reached (FALSE);
798 /* @list: GList of EmpathyRosterContact
800 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
803 individual_in_list (FolksIndividual *individual,
808 for (l = list; l != NULL; l = g_list_next (l))
810 EmpathyRosterContact *contact = l->data;
812 if (empathy_roster_contact_get_individual (contact) == individual)
820 populate_view (EmpathyRosterView *self)
822 GList *individuals, *l;
824 individuals = empathy_individual_manager_get_members (self->priv->manager);
825 for (l = individuals; l != NULL; l = g_list_next (l))
827 FolksIndividual *individual = l->data;
829 individual_added (self, individual);
832 g_list_free (individuals);
836 remove_from_group (EmpathyRosterView *self,
837 FolksIndividual *individual,
840 GHashTable *contacts;
842 EmpathyRosterGroup *roster_group;
844 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
845 if (contacts == NULL)
848 contact = g_hash_table_lookup (contacts, group);
852 g_hash_table_remove (contacts, group);
854 if (g_hash_table_size (contacts) == 0)
856 add_to_group (self, individual, UNGROUPPED);
859 roster_group = lookup_roster_group (self, group);
861 if (roster_group != NULL)
863 update_group_widgets_count (self, roster_group,
864 EMPATHY_ROSTER_CONTACT (contact), FALSE);
867 gtk_container_remove (GTK_CONTAINER (self), contact);
871 update_top_contacts (EmpathyRosterView *self)
874 GList *to_add = NULL, *to_remove = NULL;
875 EmpathyRosterGroup *group;
877 if (!self->priv->show_groups)
879 egg_list_box_resort (EGG_LIST_BOX (self));
883 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
885 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
888 to_add = g_list_copy (tops);
894 contacts = empathy_roster_group_get_widgets (group);
896 /* Check which EmpathyRosterContact have to be removed */
897 for (l = contacts; l != NULL; l = g_list_next (l))
899 EmpathyRosterContact *contact = l->data;
900 FolksIndividual *individual;
902 individual = empathy_roster_contact_get_individual (contact);
904 if (g_list_find (tops, individual) == NULL)
905 to_remove = g_list_prepend (to_remove, individual);
908 /* Check which EmpathyRosterContact have to be added */
909 for (l = tops; l != NULL; l = g_list_next (l))
911 FolksIndividual *individual = l->data;
913 if (!individual_in_list (individual, contacts))
914 to_add = g_list_prepend (to_add, individual);
918 for (l = to_add; l != NULL; l = g_list_next (l))
919 add_to_group (self, l->data, TOP_GROUP);
921 for (l = to_remove; l != NULL; l = g_list_next (l))
922 remove_from_group (self, l->data, TOP_GROUP);
924 g_list_free (to_add);
925 g_list_free (to_remove);
929 groups_changed_cb (EmpathyIndividualManager *manager,
930 FolksIndividual *individual,
933 EmpathyRosterView *self)
935 if (!self->priv->show_groups)
940 add_to_group (self, individual, group);
944 remove_from_group (self, individual, group);
949 top_individuals_changed_cb (EmpathyIndividualManager *manager,
951 EmpathyRosterView *self)
953 update_top_contacts (self);
957 empathy_roster_view_constructed (GObject *object)
959 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
960 void (*chain_up) (GObject *) =
961 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
963 if (chain_up != NULL)
966 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
968 populate_view (self);
970 tp_g_signal_connect_object (self->priv->manager, "members-changed",
971 G_CALLBACK (members_changed_cb), self, 0);
972 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
973 G_CALLBACK (groups_changed_cb), self, 0);
974 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
975 G_CALLBACK (top_individuals_changed_cb), self, 0);
977 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
978 roster_view_sort, self, NULL);
980 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
983 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
985 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
989 empathy_roster_view_dispose (GObject *object)
991 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
992 void (*chain_up) (GObject *) =
993 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
995 stop_flashing (self);
997 empathy_roster_view_set_live_search (self, NULL);
998 g_clear_object (&self->priv->manager);
1000 if (chain_up != NULL)
1005 empathy_roster_view_finalize (GObject *object)
1007 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1008 void (*chain_up) (GObject *) =
1009 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1011 g_hash_table_unref (self->priv->roster_contacts);
1012 g_hash_table_unref (self->priv->roster_groups);
1013 g_hash_table_unref (self->priv->displayed_contacts);
1014 g_queue_free_full (self->priv->events, event_free);
1016 if (chain_up != NULL)
1021 empathy_roster_view_child_activated (EggListBox *box,
1024 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1025 EmpathyRosterContact *contact;
1026 FolksIndividual *individual;
1029 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1032 contact = EMPATHY_ROSTER_CONTACT (child);
1033 individual = empathy_roster_contact_get_individual (contact);
1035 /* Activate the oldest event associated with this contact, if any */
1036 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1037 l = g_list_previous (l))
1039 Event *event = l->data;
1041 if (event->individual == individual)
1043 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1049 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1053 fire_popup_individual_menu (EmpathyRosterView *self,
1058 EmpathyRosterContact *contact;
1059 FolksIndividual *individual;
1061 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1064 contact = EMPATHY_ROSTER_CONTACT (child);
1065 individual = empathy_roster_contact_get_individual (contact);
1067 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1068 individual, button, time);
1072 empathy_roster_view_button_press_event (GtkWidget *widget,
1073 GdkEventButton *event)
1075 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1076 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1077 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1079 if (event->button == 3)
1083 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1086 fire_popup_individual_menu (self, child, event->button, event->time);
1089 return chain_up (widget, event);
1093 empathy_roster_view_key_press_event (GtkWidget *widget,
1096 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1097 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1098 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1100 if (event->keyval == GDK_KEY_Menu)
1104 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1107 fire_popup_individual_menu (self, child, 0, event->time);
1110 return chain_up (widget, event);
1114 empathy_roster_view_query_tooltip (GtkWidget *widget,
1117 gboolean keyboard_mode,
1118 GtkTooltip *tooltip)
1120 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1122 EmpathyRosterContact *contact;
1123 FolksIndividual *individual;
1125 if (self->priv->individual_tooltip_cb == NULL)
1128 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1129 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1132 contact = EMPATHY_ROSTER_CONTACT (child);
1133 individual = empathy_roster_contact_get_individual (contact);
1135 return self->priv->individual_tooltip_cb (self, individual, keyboard_mode,
1136 tooltip, self->priv->individual_tooltip_data);
1140 empathy_roster_view_set_individual_tooltip_cb (EmpathyRosterView *self,
1141 EmpathyRosterViewIndividualTooltipCb callback,
1144 self->priv->individual_tooltip_cb = callback;
1145 self->priv->individual_tooltip_data = user_data;
1147 gtk_widget_set_has_tooltip (GTK_WIDGET (self), callback != NULL);
1151 empathy_roster_view_remove (GtkContainer *container,
1154 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1155 void (*chain_up) (GtkContainer *, GtkWidget *) =
1156 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1158 chain_up (container, widget);
1160 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1161 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1165 empathy_roster_view_class_init (
1166 EmpathyRosterViewClass *klass)
1168 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1169 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1170 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1171 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1174 oclass->get_property = empathy_roster_view_get_property;
1175 oclass->set_property = empathy_roster_view_set_property;
1176 oclass->constructed = empathy_roster_view_constructed;
1177 oclass->dispose = empathy_roster_view_dispose;
1178 oclass->finalize = empathy_roster_view_finalize;
1180 widget_class->button_press_event = empathy_roster_view_button_press_event;
1181 widget_class->key_press_event = empathy_roster_view_key_press_event;
1182 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1184 container_class->remove = empathy_roster_view_remove;
1186 box_class->child_activated = empathy_roster_view_child_activated;
1188 spec = g_param_spec_object ("manager", "Manager",
1189 "EmpathyIndividualManager",
1190 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1191 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1192 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1194 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1195 "Show offline contacts",
1197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1198 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1200 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1204 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1206 spec = g_param_spec_boolean ("empty", "Empty",
1207 "Is the view currently empty?",
1209 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1210 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1212 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1213 G_OBJECT_CLASS_TYPE (klass),
1215 0, NULL, NULL, NULL,
1217 1, FOLKS_TYPE_INDIVIDUAL);
1219 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1220 G_OBJECT_CLASS_TYPE (klass),
1222 0, NULL, NULL, NULL,
1224 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1226 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1227 G_OBJECT_CLASS_TYPE (klass),
1229 0, NULL, NULL, NULL,
1231 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1233 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1237 empathy_roster_view_init (EmpathyRosterView *self)
1239 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1240 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1242 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1243 NULL, (GDestroyNotify) g_hash_table_unref);
1244 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1246 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1248 self->priv->events = g_queue_new ();
1250 self->priv->empty = TRUE;
1254 empathy_roster_view_new (EmpathyIndividualManager *manager)
1256 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1258 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1263 EmpathyIndividualManager *
1264 empathy_roster_view_get_manager (EmpathyRosterView *self)
1266 return self->priv->manager;
1270 empathy_roster_view_show_offline (EmpathyRosterView *self,
1273 if (self->priv->show_offline == show)
1276 self->priv->show_offline = show;
1277 egg_list_box_refilter (EGG_LIST_BOX (self));
1279 g_object_notify (G_OBJECT (self), "show-offline");
1283 clear_view (EmpathyRosterView *self)
1285 gtk_container_foreach (GTK_CONTAINER (self),
1286 (GtkCallback) gtk_widget_destroy, NULL);
1288 g_hash_table_remove_all (self->priv->roster_contacts);
1292 empathy_roster_view_show_groups (EmpathyRosterView *self,
1295 if (self->priv->show_groups == show)
1298 self->priv->show_groups = show;
1300 /* TODO: block sort/filter? */
1302 populate_view (self);
1304 g_object_notify (G_OBJECT (self), "show-groups");
1308 select_first_contact (EmpathyRosterView *self)
1310 GList *children, *l;
1312 children = gtk_container_get_children (GTK_CONTAINER (self));
1313 for (l = children; l != NULL; l = g_list_next (l))
1315 GtkWidget *child = l->data;
1317 if (!gtk_widget_get_child_visible (child))
1320 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1323 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1327 g_list_free (children);
1331 search_text_notify_cb (EmpathyLiveSearch *search,
1333 EmpathyRosterView *self)
1335 egg_list_box_refilter (EGG_LIST_BOX (self));
1337 select_first_contact (self);
1341 search_activate_cb (GtkWidget *search,
1342 EmpathyRosterView *self)
1344 EggListBox *box = EGG_LIST_BOX (self);
1347 child = egg_list_box_get_selected_child (box);
1351 empathy_roster_view_child_activated (box, child);
1355 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1356 EmpathyLiveSearch *search)
1358 if (self->priv->search != NULL)
1360 g_signal_handlers_disconnect_by_func (self->priv->search,
1361 search_text_notify_cb, self);
1362 g_signal_handlers_disconnect_by_func (self->priv->search,
1363 search_activate_cb, self);
1365 g_clear_object (&self->priv->search);
1371 self->priv->search = g_object_ref (search);
1373 g_signal_connect (self->priv->search, "notify::text",
1374 G_CALLBACK (search_text_notify_cb), self);
1375 g_signal_connect (self->priv->search, "activate",
1376 G_CALLBACK (search_activate_cb), self);
1380 empathy_roster_view_is_empty (EmpathyRosterView *self)
1382 return self->priv->empty;
1386 empathy_roster_view_is_searching (EmpathyRosterView *self)
1388 return (self->priv->search != NULL &&
1389 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1392 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1393 * Empathy's internals. */
1395 empathy_roster_view_add_event (EmpathyRosterView *self,
1396 FolksIndividual *individual,
1400 GHashTable *contacts;
1402 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1403 if (contacts == NULL)
1406 self->priv->last_event_id++;
1408 g_queue_push_head (self->priv->events,
1409 event_new (self->priv->last_event_id, individual, icon, user_data));
1411 start_flashing (self);
1413 return self->priv->last_event_id;
1417 empathy_roster_view_remove_event (EmpathyRosterView *self,
1422 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1423 l = g_list_next (l))
1425 Event *event = l->data;
1427 if (event->id == event_id)
1429 remove_event (self, event);