3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy/empathy-contact-groups.h>
9 #include <libempathy-gtk/empathy-roster-contact.h>
10 #include <libempathy-gtk/empathy-roster-group.h>
11 #include <libempathy-gtk/empathy-ui-utils.h>
13 #include <libempathy/empathy-utils.h>
15 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
17 /* Flashing delay for icons (milliseconds). */
18 #define FLASH_TIMEOUT 500
31 SIG_INDIVIDUAL_ACTIVATED,
32 SIG_POPUP_INDIVIDUAL_MENU,
34 SIG_INDIVIDUAL_TOOLTIP,
38 static guint signals[LAST_SIGNAL];
40 #define NO_GROUP "X-no-group"
42 struct _EmpathyRosterViewPriv
44 /* FolksIndividual (borrowed) -> GHashTable (
45 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
47 * When not using groups, this hash just have one element mapped
48 * from the special NO_GROUP key. We could use it as a set but
49 * I prefer to stay coherent in the way this hash is managed.
51 GHashTable *roster_contacts;
52 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
53 GHashTable *roster_groups;
54 /* Hash of the EmpathyRosterContact currently displayed */
55 GHashTable *displayed_contacts;
58 /* queue of (Event *). The most recent events are in the head of the queue
59 * so we always display the icon of the oldest one. */
62 gboolean display_flash_event;
64 gboolean show_offline;
68 EmpathyLiveSearch *search;
70 EmpathyRosterModel *model;
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->model);
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->model == NULL);
146 self->priv->model = 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);
206 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
207 gtk_expander_get_expanded (GTK_EXPANDER (group)));
210 static EmpathyRosterGroup *
211 lookup_roster_group (EmpathyRosterView *self,
214 return g_hash_table_lookup (self->priv->roster_groups, group);
217 static EmpathyRosterGroup *
218 ensure_roster_group (EmpathyRosterView *self,
221 GtkWidget *roster_group;
223 roster_group = (GtkWidget *) lookup_roster_group (self, group);
224 if (roster_group != NULL)
225 return EMPATHY_ROSTER_GROUP (roster_group);
227 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
228 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
229 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
230 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
232 roster_group = empathy_roster_group_new (group, NULL);
234 gtk_expander_set_expanded (GTK_EXPANDER (roster_group),
235 empathy_contact_group_get_expanded (group));
237 g_signal_connect (roster_group, "notify::expanded",
238 G_CALLBACK (group_expanded_cb), self);
240 gtk_widget_show (roster_group);
241 gtk_container_add (GTK_CONTAINER (self), roster_group);
243 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
246 return EMPATHY_ROSTER_GROUP (roster_group);
250 update_group_widgets (EmpathyRosterView *self,
251 EmpathyRosterGroup *group,
252 EmpathyRosterContact *contact,
255 guint old_count, count;
257 old_count = empathy_roster_group_get_widgets_count (group);
260 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
262 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
264 if (count != old_count)
265 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
269 add_to_group (EmpathyRosterView *self,
270 FolksIndividual *individual,
274 GHashTable *contacts;
275 EmpathyRosterGroup *roster_group = NULL;
277 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
278 if (contacts == NULL)
281 if (g_hash_table_lookup (contacts, group) != NULL)
284 if (tp_strdiff (group, NO_GROUP))
285 roster_group = ensure_roster_group (self, group);
287 contact = add_roster_contact (self, individual, group);
288 g_hash_table_insert (contacts, g_strdup (group), contact);
290 if (roster_group != NULL)
292 update_group_widgets (self, roster_group,
293 EMPATHY_ROSTER_CONTACT (contact), TRUE);
298 individual_added (EmpathyRosterView *self,
299 FolksIndividual *individual)
301 GHashTable *contacts;
303 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
304 if (contacts != NULL)
307 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
309 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
311 if (!self->priv->show_groups)
313 add_to_group (self, individual, NO_GROUP);
319 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
322 if (g_list_length (groups) > 0)
324 for (l = groups; l != NULL; l = g_list_next (l))
326 add_to_group (self, individual, l->data);
331 /* No group, adds to Ungrouped */
332 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
335 g_list_free (groups);
340 set_event_icon_on_individual (EmpathyRosterView *self,
341 FolksIndividual *individual,
344 GHashTable *contacts;
348 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
349 if (contacts == NULL)
352 g_hash_table_iter_init (&iter, contacts);
353 while (g_hash_table_iter_next (&iter, NULL, &v))
355 EmpathyRosterContact *contact =v;
357 empathy_roster_contact_set_event_icon (contact, icon);
362 flash_event (Event *event,
363 EmpathyRosterView *self)
365 set_event_icon_on_individual (self, event->individual, event->icon);
369 unflash_event (Event *event,
370 EmpathyRosterView *self)
372 set_event_icon_on_individual (self, event->individual, NULL);
376 flash_cb (gpointer data)
378 EmpathyRosterView *self = data;
380 if (self->priv->display_flash_event)
382 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
383 self->priv->display_flash_event = FALSE;
387 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
388 self->priv->display_flash_event = TRUE;
395 start_flashing (EmpathyRosterView *self)
397 if (self->priv->flash_id != 0)
400 self->priv->display_flash_event = TRUE;
402 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
407 stop_flashing (EmpathyRosterView *self)
409 if (self->priv->flash_id == 0)
412 g_source_remove (self->priv->flash_id);
413 self->priv->flash_id = 0;
417 remove_event (EmpathyRosterView *self,
420 unflash_event (event, self);
421 g_queue_remove (self->priv->events, event);
423 if (g_queue_get_length (self->priv->events) == 0)
425 stop_flashing (self);
430 remove_all_individual_event (EmpathyRosterView *self,
431 FolksIndividual *individual)
435 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
438 Event *event = l->data;
440 if (event->individual == individual)
442 remove_event (self, event);
449 individual_removed (EmpathyRosterView *self,
450 FolksIndividual *individual)
452 GHashTable *contacts;
456 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
457 if (contacts == NULL)
460 remove_all_individual_event (self, individual);
462 g_hash_table_iter_init (&iter, contacts);
463 while (g_hash_table_iter_next (&iter, &key, &value))
465 const gchar *group_name = key;
466 GtkWidget *contact = value;
467 EmpathyRosterGroup *group;
469 group = lookup_roster_group (self, group_name);
472 update_group_widgets (self, group,
473 EMPATHY_ROSTER_CONTACT (contact), FALSE);
476 gtk_container_remove (GTK_CONTAINER (self), contact);
479 g_hash_table_remove (self->priv->roster_contacts, individual);
483 individual_added_cb (EmpathyRosterModel *model,
484 FolksIndividual *individual,
485 EmpathyRosterView *self)
487 individual_added (self, individual);
491 individual_removed_cb (EmpathyRosterModel *model,
492 FolksIndividual *individual,
493 EmpathyRosterView *self)
495 individual_removed (self, individual);
499 contact_in_top (EmpathyRosterView *self,
500 EmpathyRosterContact *contact)
502 if (!self->priv->show_groups)
504 /* Always display top contacts in non-group mode. */
506 FolksIndividual *individual;
507 gboolean result = FALSE;
509 individual = empathy_roster_contact_get_individual (contact);
511 groups = empathy_roster_model_get_groups_for_individual (
512 self->priv->model, individual);
514 if (g_list_find (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP) != NULL)
517 g_list_free (groups);
522 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
523 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
524 /* If we are displaying contacts, we only want to *always* display the
525 * RosterContact which is displayed at the top; not the ones displayed in
526 * the 'normal' group sections */
533 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
534 EmpathyRosterContact *b)
536 FolksIndividual *ind_a, *ind_b;
537 const gchar *alias_a, *alias_b;
539 ind_a = empathy_roster_contact_get_individual (a);
540 ind_b = empathy_roster_contact_get_individual (b);
542 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
543 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
545 return g_ascii_strcasecmp (alias_a, alias_b);
549 compare_roster_contacts_no_group (EmpathyRosterView *self,
550 EmpathyRosterContact *a,
551 EmpathyRosterContact *b)
553 gboolean top_a, top_b;
555 top_a = contact_in_top (self, a);
556 top_b = contact_in_top (self, b);
559 /* Both contacts are in the top of the roster (or not). Sort them
561 return compare_roster_contacts_by_alias (a, b);
569 compare_group_names (const gchar *group_a,
570 const gchar *group_b)
572 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
575 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
578 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
580 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
583 return g_ascii_strcasecmp (group_a, group_b);
587 compare_roster_contacts_with_groups (EmpathyRosterView *self,
588 EmpathyRosterContact *a,
589 EmpathyRosterContact *b)
591 const gchar *group_a, *group_b;
593 group_a = empathy_roster_contact_get_group (a);
594 group_b = empathy_roster_contact_get_group (b);
596 if (!tp_strdiff (group_a, group_b))
597 /* Same group, compare the contacts */
598 return compare_roster_contacts_by_alias (a, b);
601 return compare_group_names (group_a, group_b);
605 compare_roster_contacts (EmpathyRosterView *self,
606 EmpathyRosterContact *a,
607 EmpathyRosterContact *b)
609 if (!self->priv->show_groups)
610 return compare_roster_contacts_no_group (self, a, b);
612 return compare_roster_contacts_with_groups (self, a, b);
616 compare_roster_groups (EmpathyRosterGroup *a,
617 EmpathyRosterGroup *b)
619 const gchar *name_a, *name_b;
621 name_a = empathy_roster_group_get_name (a);
622 name_b = empathy_roster_group_get_name (b);
624 return compare_group_names (name_a, name_b);
628 compare_contact_group (EmpathyRosterContact *contact,
629 EmpathyRosterGroup *group)
631 const char *contact_group, *group_name;
633 contact_group = empathy_roster_contact_get_group (contact);
634 group_name = empathy_roster_group_get_name (group);
636 if (!tp_strdiff (contact_group, group_name))
637 /* @contact is in @group, @group has to be displayed first */
640 /* @contact is in a different group, sort by group name */
641 return compare_group_names (contact_group, group_name);
645 roster_view_sort (gconstpointer a,
649 EmpathyRosterView *self = user_data;
651 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
652 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
653 EMPATHY_ROSTER_CONTACT (b));
654 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
655 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
656 EMPATHY_ROSTER_GROUP (b));
657 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
658 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
659 EMPATHY_ROSTER_GROUP (b));
660 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
661 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
662 EMPATHY_ROSTER_GROUP (a));
664 g_return_val_if_reached (0);
668 update_separator (GtkWidget **separator,
675 /* No separator before the first row */
676 g_clear_object (separator);
680 if (*separator != NULL)
683 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
684 g_object_ref_sink (*separator);
688 is_searching (EmpathyRosterView *self)
690 if (self->priv->search == NULL)
693 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
697 update_empty (EmpathyRosterView *self,
700 if (self->priv->empty == empty)
703 self->priv->empty = empty;
704 g_object_notify (G_OBJECT (self), "empty");
708 add_to_displayed (EmpathyRosterView *self,
709 EmpathyRosterContact *contact)
711 FolksIndividual *individual;
712 GHashTable *contacts;
716 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
719 g_hash_table_add (self->priv->displayed_contacts, contact);
720 update_empty (self, FALSE);
722 /* Groups of this contact may now be displayed if we just displays the first
723 * child in this group. */
725 if (!self->priv->show_groups)
728 individual = empathy_roster_contact_get_individual (contact);
729 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
730 if (contacts == NULL)
733 g_hash_table_iter_init (&iter, contacts);
734 while (g_hash_table_iter_next (&iter, &k, NULL))
736 const gchar *group_name = k;
739 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
743 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
748 remove_from_displayed (EmpathyRosterView *self,
749 EmpathyRosterContact *contact)
751 g_hash_table_remove (self->priv->displayed_contacts, contact);
753 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
754 update_empty (self, TRUE);
758 * check if @contact should be displayed according to @self's current status
759 * and without consideration for the state of @contact's groups.
762 contact_should_be_displayed (EmpathyRosterView *self,
763 EmpathyRosterContact *contact)
765 if (is_searching (self))
767 FolksIndividual *individual;
769 individual = empathy_roster_contact_get_individual (contact);
771 return empathy_individual_match_string (individual,
772 empathy_live_search_get_text (self->priv->search),
773 empathy_live_search_get_words (self->priv->search));
776 if (self->priv->show_offline)
779 if (contact_in_top (self, contact))
782 return empathy_roster_contact_is_online (contact);
787 filter_contact (EmpathyRosterView *self,
788 EmpathyRosterContact *contact)
792 displayed = contact_should_be_displayed (self, contact);
794 if (self->priv->show_groups)
796 const gchar *group_name;
797 EmpathyRosterGroup *group;
799 group_name = empathy_roster_contact_get_group (contact);
800 group = lookup_roster_group (self, group_name);
804 /* When searching, always display even if the group is closed */
805 if (!is_searching (self) &&
806 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
813 add_to_displayed (self, contact);
817 remove_from_displayed (self, contact);
824 filter_group (EmpathyRosterView *self,
825 EmpathyRosterGroup *group)
829 /* Display the group if it contains at least one displayed contact */
830 widgets = empathy_roster_group_get_widgets (group);
831 for (l = widgets; l != NULL; l = g_list_next (l))
833 EmpathyRosterContact *contact = l->data;
835 if (contact_should_be_displayed (self, contact))
843 filter_list (GtkWidget *child,
846 EmpathyRosterView *self = user_data;
848 if (EMPATHY_IS_ROSTER_CONTACT (child))
849 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
851 else if (EMPATHY_IS_ROSTER_GROUP (child))
852 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
854 g_return_val_if_reached (FALSE);
858 populate_view (EmpathyRosterView *self)
860 GList *individuals, *l;
862 individuals = empathy_roster_model_get_individuals (self->priv->model);
863 for (l = individuals; l != NULL; l = g_list_next (l))
865 FolksIndividual *individual = l->data;
867 individual_added (self, individual);
870 g_list_free (individuals);
874 remove_from_group (EmpathyRosterView *self,
875 FolksIndividual *individual,
878 GHashTable *contacts;
880 EmpathyRosterGroup *roster_group;
882 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
883 if (contacts == NULL)
886 contact = g_hash_table_lookup (contacts, group);
890 g_hash_table_remove (contacts, group);
892 if (g_hash_table_size (contacts) == 0)
894 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
897 roster_group = lookup_roster_group (self, group);
899 if (roster_group != NULL)
901 update_group_widgets (self, roster_group,
902 EMPATHY_ROSTER_CONTACT (contact), FALSE);
905 gtk_container_remove (GTK_CONTAINER (self), contact);
909 groups_changed_cb (EmpathyRosterModel *model,
910 FolksIndividual *individual,
913 EmpathyRosterView *self)
915 if (!self->priv->show_groups)
917 egg_list_box_resort (EGG_LIST_BOX (self));
923 add_to_group (self, individual, group);
927 remove_from_group (self, individual, group);
932 empathy_roster_view_constructed (GObject *object)
934 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
935 void (*chain_up) (GObject *) =
936 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
938 if (chain_up != NULL)
941 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
943 /* Get saved group states. */
944 empathy_contact_groups_get_all ();
946 populate_view (self);
948 tp_g_signal_connect_object (self->priv->model, "individual-added",
949 G_CALLBACK (individual_added_cb), self, 0);
950 tp_g_signal_connect_object (self->priv->model, "individual-removed",
951 G_CALLBACK (individual_removed_cb), self, 0);
952 tp_g_signal_connect_object (self->priv->model, "groups-changed",
953 G_CALLBACK (groups_changed_cb), self, 0);
955 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
956 roster_view_sort, self, NULL);
958 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
961 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
963 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
967 empathy_roster_view_dispose (GObject *object)
969 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
970 void (*chain_up) (GObject *) =
971 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
973 stop_flashing (self);
975 empathy_roster_view_set_live_search (self, NULL);
976 g_clear_object (&self->priv->model);
978 if (chain_up != NULL)
983 empathy_roster_view_finalize (GObject *object)
985 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
986 void (*chain_up) (GObject *) =
987 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
989 g_hash_table_unref (self->priv->roster_contacts);
990 g_hash_table_unref (self->priv->roster_groups);
991 g_hash_table_unref (self->priv->displayed_contacts);
992 g_queue_free_full (self->priv->events, event_free);
994 if (chain_up != NULL)
999 empathy_roster_view_child_activated (EggListBox *box,
1002 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1003 EmpathyRosterContact *contact;
1004 FolksIndividual *individual;
1007 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1010 contact = EMPATHY_ROSTER_CONTACT (child);
1011 individual = empathy_roster_contact_get_individual (contact);
1013 /* Activate the oldest event associated with this contact, if any */
1014 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1015 l = g_list_previous (l))
1017 Event *event = l->data;
1019 if (event->individual == individual)
1021 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1027 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1031 fire_popup_individual_menu (EmpathyRosterView *self,
1036 EmpathyRosterContact *contact;
1037 FolksIndividual *individual;
1039 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1042 contact = EMPATHY_ROSTER_CONTACT (child);
1043 individual = empathy_roster_contact_get_individual (contact);
1045 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1046 individual, button, time);
1050 empathy_roster_view_button_press_event (GtkWidget *widget,
1051 GdkEventButton *event)
1053 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1054 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1055 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1057 if (event->button == 3)
1061 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1065 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1067 fire_popup_individual_menu (self, child, event->button, event->time);
1071 return chain_up (widget, event);
1075 empathy_roster_view_key_press_event (GtkWidget *widget,
1078 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1079 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1080 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1082 if (event->keyval == GDK_KEY_Menu)
1086 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1089 fire_popup_individual_menu (self, child, 0, event->time);
1092 return chain_up (widget, event);
1096 * @out_child: (out) (allow-none)
1099 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1101 GtkWidget **out_child)
1105 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1107 if (out_child != NULL)
1110 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1113 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1117 * @out_child: (out) (allow-none)
1120 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1125 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1127 if (EMPATHY_IS_ROSTER_CONTACT (child))
1128 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1129 else if (EMPATHY_IS_ROSTER_GROUP (child))
1130 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1136 empathy_roster_view_query_tooltip (GtkWidget *widget,
1139 gboolean keyboard_mode,
1140 GtkTooltip *tooltip)
1142 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1143 FolksIndividual *individual;
1147 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1148 if (individual == NULL)
1151 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1152 individual, keyboard_mode, tooltip, &result);
1156 GtkAllocation allocation;
1158 gtk_widget_get_allocation (child, &allocation);
1159 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1166 empathy_roster_view_remove (GtkContainer *container,
1169 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1170 void (*chain_up) (GtkContainer *, GtkWidget *) =
1171 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1173 chain_up (container, widget);
1175 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1176 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1180 empathy_roster_view_class_init (
1181 EmpathyRosterViewClass *klass)
1183 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1184 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1185 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1186 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1189 oclass->get_property = empathy_roster_view_get_property;
1190 oclass->set_property = empathy_roster_view_set_property;
1191 oclass->constructed = empathy_roster_view_constructed;
1192 oclass->dispose = empathy_roster_view_dispose;
1193 oclass->finalize = empathy_roster_view_finalize;
1195 widget_class->button_press_event = empathy_roster_view_button_press_event;
1196 widget_class->key_press_event = empathy_roster_view_key_press_event;
1197 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1199 container_class->remove = empathy_roster_view_remove;
1201 box_class->child_activated = empathy_roster_view_child_activated;
1203 spec = g_param_spec_object ("model", "Model",
1204 "EmpathyRosterModel",
1205 EMPATHY_TYPE_ROSTER_MODEL,
1206 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1207 g_object_class_install_property (oclass, PROP_MODEL, spec);
1209 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1210 "Show offline contacts",
1212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1213 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1215 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1219 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1221 spec = g_param_spec_boolean ("empty", "Empty",
1222 "Is the view currently empty?",
1224 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1225 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1227 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1228 G_OBJECT_CLASS_TYPE (klass),
1230 0, NULL, NULL, NULL,
1232 1, FOLKS_TYPE_INDIVIDUAL);
1234 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1235 G_OBJECT_CLASS_TYPE (klass),
1237 0, NULL, NULL, NULL,
1239 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1241 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1242 G_OBJECT_CLASS_TYPE (klass),
1244 0, NULL, NULL, NULL,
1246 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1248 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1249 G_OBJECT_CLASS_TYPE (klass),
1251 0, g_signal_accumulator_true_handled, NULL, NULL,
1253 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1255 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1259 empathy_roster_view_init (EmpathyRosterView *self)
1261 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1262 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1264 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1265 NULL, (GDestroyNotify) g_hash_table_unref);
1266 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1268 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1270 self->priv->events = g_queue_new ();
1272 self->priv->empty = TRUE;
1276 empathy_roster_view_new (EmpathyRosterModel *model)
1278 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1280 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1286 empathy_roster_view_show_offline (EmpathyRosterView *self,
1289 if (self->priv->show_offline == show)
1292 self->priv->show_offline = show;
1293 egg_list_box_refilter (EGG_LIST_BOX (self));
1295 g_object_notify (G_OBJECT (self), "show-offline");
1299 clear_view (EmpathyRosterView *self)
1301 gtk_container_foreach (GTK_CONTAINER (self),
1302 (GtkCallback) gtk_widget_destroy, NULL);
1304 g_hash_table_remove_all (self->priv->roster_contacts);
1305 g_hash_table_remove_all (self->priv->roster_groups);
1306 g_hash_table_remove_all (self->priv->displayed_contacts);
1310 empathy_roster_view_show_groups (EmpathyRosterView *self,
1313 if (self->priv->show_groups == show)
1316 self->priv->show_groups = show;
1318 /* TODO: block sort/filter? */
1320 populate_view (self);
1322 g_object_notify (G_OBJECT (self), "show-groups");
1326 select_first_contact (EmpathyRosterView *self)
1328 GList *children, *l;
1330 children = gtk_container_get_children (GTK_CONTAINER (self));
1331 for (l = children; l != NULL; l = g_list_next (l))
1333 GtkWidget *child = l->data;
1335 if (!gtk_widget_get_child_visible (child))
1338 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1341 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1345 g_list_free (children);
1349 search_text_notify_cb (EmpathyLiveSearch *search,
1351 EmpathyRosterView *self)
1353 egg_list_box_refilter (EGG_LIST_BOX (self));
1355 select_first_contact (self);
1359 search_activate_cb (GtkWidget *search,
1360 EmpathyRosterView *self)
1362 EggListBox *box = EGG_LIST_BOX (self);
1365 child = egg_list_box_get_selected_child (box);
1369 empathy_roster_view_child_activated (box, child);
1373 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1374 EmpathyLiveSearch *search)
1376 if (self->priv->search != NULL)
1378 g_signal_handlers_disconnect_by_func (self->priv->search,
1379 search_text_notify_cb, self);
1380 g_signal_handlers_disconnect_by_func (self->priv->search,
1381 search_activate_cb, self);
1383 g_clear_object (&self->priv->search);
1389 self->priv->search = g_object_ref (search);
1391 g_signal_connect (self->priv->search, "notify::text",
1392 G_CALLBACK (search_text_notify_cb), self);
1393 g_signal_connect (self->priv->search, "activate",
1394 G_CALLBACK (search_activate_cb), self);
1398 empathy_roster_view_is_empty (EmpathyRosterView *self)
1400 return self->priv->empty;
1404 empathy_roster_view_is_searching (EmpathyRosterView *self)
1406 return (self->priv->search != NULL &&
1407 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1410 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1411 * Empathy's internals. */
1413 empathy_roster_view_add_event (EmpathyRosterView *self,
1414 FolksIndividual *individual,
1418 GHashTable *contacts;
1420 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1421 if (contacts == NULL)
1424 self->priv->last_event_id++;
1426 g_queue_push_head (self->priv->events,
1427 event_new (self->priv->last_event_id, individual, icon, user_data));
1429 start_flashing (self);
1431 return self->priv->last_event_id;
1435 empathy_roster_view_remove_event (EmpathyRosterView *self,
1440 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1441 l = g_list_next (l))
1443 Event *event = l->data;
1445 if (event->id == event_id)
1447 remove_event (self, event);
1454 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1458 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1460 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1463 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));