3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy-gtk/empathy-roster-contact.h>
8 #include <libempathy-gtk/empathy-roster-group.h>
9 #include <libempathy-gtk/empathy-ui-utils.h>
11 #include <libempathy/empathy-utils.h>
13 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
15 /* Flashing delay for icons (milliseconds). */
16 #define FLASH_TIMEOUT 500
29 SIG_INDIVIDUAL_ACTIVATED,
30 SIG_POPUP_INDIVIDUAL_MENU,
32 SIG_INDIVIDUAL_TOOLTIP,
36 static guint signals[LAST_SIGNAL];
38 #define NO_GROUP "X-no-group"
40 struct _EmpathyRosterViewPriv
42 /* FolksIndividual (borrowed) -> GHashTable (
43 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
45 * When not using groups, this hash just have one element mapped
46 * from the special NO_GROUP key. We could use it as a set but
47 * I prefer to stay coherent in the way this hash is managed.
49 GHashTable *roster_contacts;
50 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
51 GHashTable *roster_groups;
52 /* Hash of the EmpathyRosterContact currently displayed */
53 GHashTable *displayed_contacts;
56 /* queue of (Event *). The most recent events are in the head of the queue
57 * so we always display the icon of the oldest one. */
60 gboolean display_flash_event;
62 gboolean show_offline;
66 EmpathyLiveSearch *search;
68 EmpathyRosterModel *model;
74 FolksIndividual *individual;
81 FolksIndividual *individual,
85 Event *event = g_slice_new (Event);
88 event->individual = g_object_ref (individual);
89 event->icon = g_strdup (icon);
90 event->user_data = user_data;
95 event_free (gpointer data)
98 g_object_unref (event->individual);
101 g_slice_free (Event, event);
105 empathy_roster_view_get_property (GObject *object,
110 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
115 g_value_set_object (value, self->priv->model);
117 case PROP_SHOW_OFFLINE:
118 g_value_set_boolean (value, self->priv->show_offline);
120 case PROP_SHOW_GROUPS:
121 g_value_set_boolean (value, self->priv->show_groups);
124 g_value_set_boolean (value, self->priv->empty);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
133 empathy_roster_view_set_property (GObject *object,
138 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
143 g_assert (self->priv->model == NULL);
144 self->priv->model = g_value_dup_object (value);
146 case PROP_SHOW_OFFLINE:
147 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
149 case PROP_SHOW_GROUPS:
150 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 roster_contact_changed_cb (GtkWidget *child,
161 EmpathyRosterView *self)
163 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
167 add_roster_contact (EmpathyRosterView *self,
168 FolksIndividual *individual,
173 contact = empathy_roster_contact_new (individual, group);
175 /* Need to refilter if online is changed */
176 g_signal_connect (contact, "notify::online",
177 G_CALLBACK (roster_contact_changed_cb), self);
179 /* Need to resort if alias is changed */
180 g_signal_connect (contact, "notify::alias",
181 G_CALLBACK (roster_contact_changed_cb), self);
183 gtk_widget_show (contact);
184 gtk_container_add (GTK_CONTAINER (self), contact);
190 group_expanded_cb (EmpathyRosterGroup *group,
192 EmpathyRosterView *self)
196 widgets = empathy_roster_group_get_widgets (group);
197 for (l = widgets; l != NULL; l = g_list_next (l))
199 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
202 g_list_free (widgets);
205 static EmpathyRosterGroup *
206 lookup_roster_group (EmpathyRosterView *self,
209 return g_hash_table_lookup (self->priv->roster_groups, group);
212 static EmpathyRosterGroup *
213 ensure_roster_group (EmpathyRosterView *self,
216 GtkWidget *roster_group;
218 roster_group = (GtkWidget *) lookup_roster_group (self, group);
219 if (roster_group != NULL)
220 return EMPATHY_ROSTER_GROUP (roster_group);
222 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
223 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
224 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
225 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
227 roster_group = empathy_roster_group_new (group, NULL);
229 g_signal_connect (roster_group, "notify::expanded",
230 G_CALLBACK (group_expanded_cb), self);
232 gtk_widget_show (roster_group);
233 gtk_container_add (GTK_CONTAINER (self), roster_group);
235 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
238 return EMPATHY_ROSTER_GROUP (roster_group);
242 update_group_widgets (EmpathyRosterView *self,
243 EmpathyRosterGroup *group,
244 EmpathyRosterContact *contact,
247 guint old_count, count;
249 old_count = empathy_roster_group_get_widgets_count (group);
252 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
254 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
256 if (count != old_count)
257 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
261 add_to_group (EmpathyRosterView *self,
262 FolksIndividual *individual,
266 GHashTable *contacts;
267 EmpathyRosterGroup *roster_group = NULL;
269 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
270 if (contacts == NULL)
273 if (tp_strdiff (group, NO_GROUP))
274 roster_group = ensure_roster_group (self, group);
276 contact = add_roster_contact (self, individual, group);
277 g_hash_table_insert (contacts, g_strdup (group), contact);
279 if (roster_group != NULL)
281 update_group_widgets (self, roster_group,
282 EMPATHY_ROSTER_CONTACT (contact), TRUE);
287 individual_added (EmpathyRosterView *self,
288 FolksIndividual *individual)
290 GHashTable *contacts;
292 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
293 if (contacts != NULL)
296 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
298 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
300 if (!self->priv->show_groups)
302 add_to_group (self, individual, NO_GROUP);
308 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
311 if (g_list_length (groups) > 0)
313 for (l = groups; l != NULL; l = g_list_next (l))
315 add_to_group (self, individual, l->data);
320 /* No group, adds to Ungrouped */
321 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
324 g_list_free (groups);
329 set_event_icon_on_individual (EmpathyRosterView *self,
330 FolksIndividual *individual,
333 GHashTable *contacts;
337 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
338 if (contacts == NULL)
341 g_hash_table_iter_init (&iter, contacts);
342 while (g_hash_table_iter_next (&iter, NULL, &v))
344 EmpathyRosterContact *contact =v;
346 empathy_roster_contact_set_event_icon (contact, icon);
351 flash_event (Event *event,
352 EmpathyRosterView *self)
354 set_event_icon_on_individual (self, event->individual, event->icon);
358 unflash_event (Event *event,
359 EmpathyRosterView *self)
361 set_event_icon_on_individual (self, event->individual, NULL);
365 flash_cb (gpointer data)
367 EmpathyRosterView *self = data;
369 if (self->priv->display_flash_event)
371 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
372 self->priv->display_flash_event = FALSE;
376 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
377 self->priv->display_flash_event = TRUE;
384 start_flashing (EmpathyRosterView *self)
386 if (self->priv->flash_id != 0)
389 self->priv->display_flash_event = TRUE;
391 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
396 stop_flashing (EmpathyRosterView *self)
398 if (self->priv->flash_id == 0)
401 g_source_remove (self->priv->flash_id);
402 self->priv->flash_id = 0;
406 remove_event (EmpathyRosterView *self,
409 unflash_event (event, self);
410 g_queue_remove (self->priv->events, event);
412 if (g_queue_get_length (self->priv->events) == 0)
414 stop_flashing (self);
419 remove_all_individual_event (EmpathyRosterView *self,
420 FolksIndividual *individual)
424 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
427 Event *event = l->data;
429 if (event->individual == individual)
431 remove_event (self, event);
438 individual_removed (EmpathyRosterView *self,
439 FolksIndividual *individual)
441 GHashTable *contacts;
445 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
446 if (contacts == NULL)
449 remove_all_individual_event (self, individual);
451 g_hash_table_iter_init (&iter, contacts);
452 while (g_hash_table_iter_next (&iter, &key, &value))
454 const gchar *group_name = key;
455 GtkWidget *contact = value;
456 EmpathyRosterGroup *group;
458 group = lookup_roster_group (self, group_name);
461 update_group_widgets (self, group,
462 EMPATHY_ROSTER_CONTACT (contact), FALSE);
465 gtk_container_remove (GTK_CONTAINER (self), contact);
468 g_hash_table_remove (self->priv->roster_contacts, individual);
472 individual_added_cb (EmpathyRosterModel *model,
473 FolksIndividual *individual,
474 EmpathyRosterView *self)
476 individual_added (self, individual);
480 individual_removed_cb (EmpathyRosterModel *model,
481 FolksIndividual *individual,
482 EmpathyRosterView *self)
484 individual_removed (self, individual);
488 contact_in_top (EmpathyRosterView *self,
489 EmpathyRosterContact *contact)
491 if (!self->priv->show_groups)
493 /* Always display top contacts in non-group mode. */
495 FolksIndividual *individual;
496 gboolean result = FALSE;
498 individual = empathy_roster_contact_get_individual (contact);
500 groups = empathy_roster_model_get_groups_for_individual (
501 self->priv->model, individual);
503 if (g_list_find (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP) != NULL)
506 g_list_free (groups);
511 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
512 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
513 /* If we are displaying contacts, we only want to *always* display the
514 * RosterContact which is displayed at the top; not the ones displayed in
515 * the 'normal' group sections */
522 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
523 EmpathyRosterContact *b)
525 FolksIndividual *ind_a, *ind_b;
526 const gchar *alias_a, *alias_b;
528 ind_a = empathy_roster_contact_get_individual (a);
529 ind_b = empathy_roster_contact_get_individual (b);
531 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
532 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
534 return g_ascii_strcasecmp (alias_a, alias_b);
538 compare_roster_contacts_no_group (EmpathyRosterView *self,
539 EmpathyRosterContact *a,
540 EmpathyRosterContact *b)
542 gboolean top_a, top_b;
544 top_a = contact_in_top (self, a);
545 top_b = contact_in_top (self, b);
548 /* Both contacts are in the top of the roster (or not). Sort them
550 return compare_roster_contacts_by_alias (a, b);
558 compare_group_names (const gchar *group_a,
559 const gchar *group_b)
561 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
564 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
567 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
569 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
572 return g_ascii_strcasecmp (group_a, group_b);
576 compare_roster_contacts_with_groups (EmpathyRosterView *self,
577 EmpathyRosterContact *a,
578 EmpathyRosterContact *b)
580 const gchar *group_a, *group_b;
582 group_a = empathy_roster_contact_get_group (a);
583 group_b = empathy_roster_contact_get_group (b);
585 if (!tp_strdiff (group_a, group_b))
586 /* Same group, compare the contacts */
587 return compare_roster_contacts_by_alias (a, b);
590 return compare_group_names (group_a, group_b);
594 compare_roster_contacts (EmpathyRosterView *self,
595 EmpathyRosterContact *a,
596 EmpathyRosterContact *b)
598 if (!self->priv->show_groups)
599 return compare_roster_contacts_no_group (self, a, b);
601 return compare_roster_contacts_with_groups (self, a, b);
605 compare_roster_groups (EmpathyRosterGroup *a,
606 EmpathyRosterGroup *b)
608 const gchar *name_a, *name_b;
610 name_a = empathy_roster_group_get_name (a);
611 name_b = empathy_roster_group_get_name (b);
613 return compare_group_names (name_a, name_b);
617 compare_contact_group (EmpathyRosterContact *contact,
618 EmpathyRosterGroup *group)
620 const char *contact_group, *group_name;
622 contact_group = empathy_roster_contact_get_group (contact);
623 group_name = empathy_roster_group_get_name (group);
625 if (!tp_strdiff (contact_group, group_name))
626 /* @contact is in @group, @group has to be displayed first */
629 /* @contact is in a different group, sort by group name */
630 return compare_group_names (contact_group, group_name);
634 roster_view_sort (gconstpointer a,
638 EmpathyRosterView *self = user_data;
640 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
641 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
642 EMPATHY_ROSTER_CONTACT (b));
643 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
644 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
645 EMPATHY_ROSTER_GROUP (b));
646 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
647 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
648 EMPATHY_ROSTER_GROUP (b));
649 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
650 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
651 EMPATHY_ROSTER_GROUP (a));
653 g_return_val_if_reached (0);
657 update_separator (GtkWidget **separator,
664 /* No separator before the first row */
665 g_clear_object (separator);
669 if (*separator != NULL)
672 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
673 g_object_ref_sink (*separator);
677 is_searching (EmpathyRosterView *self)
679 if (self->priv->search == NULL)
682 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
686 update_empty (EmpathyRosterView *self,
689 if (self->priv->empty == empty)
692 self->priv->empty = empty;
693 g_object_notify (G_OBJECT (self), "empty");
697 add_to_displayed (EmpathyRosterView *self,
698 EmpathyRosterContact *contact)
700 FolksIndividual *individual;
701 GHashTable *contacts;
705 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
708 g_hash_table_add (self->priv->displayed_contacts, contact);
709 update_empty (self, FALSE);
711 /* Groups of this contact may now be displayed if we just displays the first
712 * child in this group. */
714 if (!self->priv->show_groups)
717 individual = empathy_roster_contact_get_individual (contact);
718 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
719 if (contacts == NULL)
722 g_hash_table_iter_init (&iter, contacts);
723 while (g_hash_table_iter_next (&iter, &k, NULL))
725 const gchar *group_name = k;
728 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
732 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
737 remove_from_displayed (EmpathyRosterView *self,
738 EmpathyRosterContact *contact)
740 g_hash_table_remove (self->priv->displayed_contacts, contact);
742 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
743 update_empty (self, TRUE);
747 * check if @contact should be displayed according to @self's current status
748 * and without consideration for the state of @contact's groups.
751 contact_should_be_displayed (EmpathyRosterView *self,
752 EmpathyRosterContact *contact)
754 if (is_searching (self))
756 FolksIndividual *individual;
758 individual = empathy_roster_contact_get_individual (contact);
760 return empathy_individual_match_string (individual,
761 empathy_live_search_get_text (self->priv->search),
762 empathy_live_search_get_words (self->priv->search));
765 if (self->priv->show_offline)
768 if (contact_in_top (self, contact))
771 return empathy_roster_contact_is_online (contact);
776 filter_contact (EmpathyRosterView *self,
777 EmpathyRosterContact *contact)
781 displayed = contact_should_be_displayed (self, contact);
783 if (self->priv->show_groups)
785 const gchar *group_name;
786 EmpathyRosterGroup *group;
788 group_name = empathy_roster_contact_get_group (contact);
789 group = lookup_roster_group (self, group_name);
793 /* When searching, always display even if the group is closed */
794 if (!is_searching (self) &&
795 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
802 add_to_displayed (self, contact);
806 remove_from_displayed (self, contact);
813 filter_group (EmpathyRosterView *self,
814 EmpathyRosterGroup *group)
818 /* Display the group if it contains at least one displayed contact */
819 widgets = empathy_roster_group_get_widgets (group);
820 for (l = widgets; l != NULL; l = g_list_next (l))
822 EmpathyRosterContact *contact = l->data;
824 if (contact_should_be_displayed (self, contact))
832 filter_list (GtkWidget *child,
835 EmpathyRosterView *self = user_data;
837 if (EMPATHY_IS_ROSTER_CONTACT (child))
838 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
840 else if (EMPATHY_IS_ROSTER_GROUP (child))
841 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
843 g_return_val_if_reached (FALSE);
847 populate_view (EmpathyRosterView *self)
849 GList *individuals, *l;
851 individuals = empathy_roster_model_get_individuals (self->priv->model);
852 for (l = individuals; l != NULL; l = g_list_next (l))
854 FolksIndividual *individual = l->data;
856 individual_added (self, individual);
859 g_list_free (individuals);
863 remove_from_group (EmpathyRosterView *self,
864 FolksIndividual *individual,
867 GHashTable *contacts;
869 EmpathyRosterGroup *roster_group;
871 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
872 if (contacts == NULL)
875 contact = g_hash_table_lookup (contacts, group);
879 g_hash_table_remove (contacts, group);
881 if (g_hash_table_size (contacts) == 0)
883 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
886 roster_group = lookup_roster_group (self, group);
888 if (roster_group != NULL)
890 update_group_widgets (self, roster_group,
891 EMPATHY_ROSTER_CONTACT (contact), FALSE);
894 gtk_container_remove (GTK_CONTAINER (self), contact);
898 groups_changed_cb (EmpathyRosterModel *model,
899 FolksIndividual *individual,
902 EmpathyRosterView *self)
904 if (!self->priv->show_groups)
906 egg_list_box_resort (EGG_LIST_BOX (self));
912 add_to_group (self, individual, group);
916 remove_from_group (self, individual, group);
921 empathy_roster_view_constructed (GObject *object)
923 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
924 void (*chain_up) (GObject *) =
925 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
927 if (chain_up != NULL)
930 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
932 populate_view (self);
934 tp_g_signal_connect_object (self->priv->model, "individual-added",
935 G_CALLBACK (individual_added_cb), self, 0);
936 tp_g_signal_connect_object (self->priv->model, "individual-removed",
937 G_CALLBACK (individual_removed_cb), self, 0);
938 tp_g_signal_connect_object (self->priv->model, "groups-changed",
939 G_CALLBACK (groups_changed_cb), self, 0);
941 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
942 roster_view_sort, self, NULL);
944 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
947 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
949 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
953 empathy_roster_view_dispose (GObject *object)
955 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
956 void (*chain_up) (GObject *) =
957 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
959 stop_flashing (self);
961 empathy_roster_view_set_live_search (self, NULL);
962 g_clear_object (&self->priv->model);
964 if (chain_up != NULL)
969 empathy_roster_view_finalize (GObject *object)
971 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
972 void (*chain_up) (GObject *) =
973 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
975 g_hash_table_unref (self->priv->roster_contacts);
976 g_hash_table_unref (self->priv->roster_groups);
977 g_hash_table_unref (self->priv->displayed_contacts);
978 g_queue_free_full (self->priv->events, event_free);
980 if (chain_up != NULL)
985 empathy_roster_view_child_activated (EggListBox *box,
988 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
989 EmpathyRosterContact *contact;
990 FolksIndividual *individual;
993 if (!EMPATHY_IS_ROSTER_CONTACT (child))
996 contact = EMPATHY_ROSTER_CONTACT (child);
997 individual = empathy_roster_contact_get_individual (contact);
999 /* Activate the oldest event associated with this contact, if any */
1000 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1001 l = g_list_previous (l))
1003 Event *event = l->data;
1005 if (event->individual == individual)
1007 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1013 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1017 fire_popup_individual_menu (EmpathyRosterView *self,
1022 EmpathyRosterContact *contact;
1023 FolksIndividual *individual;
1025 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1028 contact = EMPATHY_ROSTER_CONTACT (child);
1029 individual = empathy_roster_contact_get_individual (contact);
1031 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1032 individual, button, time);
1036 empathy_roster_view_button_press_event (GtkWidget *widget,
1037 GdkEventButton *event)
1039 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1040 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1041 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1043 if (event->button == 3)
1047 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1051 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1053 fire_popup_individual_menu (self, child, event->button, event->time);
1057 return chain_up (widget, event);
1061 empathy_roster_view_key_press_event (GtkWidget *widget,
1064 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1065 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1066 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1068 if (event->keyval == GDK_KEY_Menu)
1072 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1075 fire_popup_individual_menu (self, child, 0, event->time);
1078 return chain_up (widget, event);
1082 * @out_child: (out) (allow-none)
1085 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1087 GtkWidget **out_child)
1091 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1093 if (out_child != NULL)
1096 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1099 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1103 * @out_child: (out) (allow-none)
1106 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1111 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1113 if (EMPATHY_IS_ROSTER_CONTACT (child))
1114 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1115 else if (EMPATHY_IS_ROSTER_GROUP (child))
1116 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1122 empathy_roster_view_query_tooltip (GtkWidget *widget,
1125 gboolean keyboard_mode,
1126 GtkTooltip *tooltip)
1128 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1129 FolksIndividual *individual;
1133 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1134 if (individual == NULL)
1137 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1138 individual, keyboard_mode, tooltip, &result);
1142 GtkAllocation allocation;
1144 gtk_widget_get_allocation (child, &allocation);
1145 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1152 empathy_roster_view_remove (GtkContainer *container,
1155 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1156 void (*chain_up) (GtkContainer *, GtkWidget *) =
1157 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1159 chain_up (container, widget);
1161 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1162 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1166 empathy_roster_view_class_init (
1167 EmpathyRosterViewClass *klass)
1169 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1170 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1171 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1172 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1175 oclass->get_property = empathy_roster_view_get_property;
1176 oclass->set_property = empathy_roster_view_set_property;
1177 oclass->constructed = empathy_roster_view_constructed;
1178 oclass->dispose = empathy_roster_view_dispose;
1179 oclass->finalize = empathy_roster_view_finalize;
1181 widget_class->button_press_event = empathy_roster_view_button_press_event;
1182 widget_class->key_press_event = empathy_roster_view_key_press_event;
1183 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1185 container_class->remove = empathy_roster_view_remove;
1187 box_class->child_activated = empathy_roster_view_child_activated;
1189 spec = g_param_spec_object ("model", "Model",
1190 "EmpathyRosterModel",
1191 EMPATHY_TYPE_ROSTER_MODEL,
1192 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1193 g_object_class_install_property (oclass, PROP_MODEL, spec);
1195 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1196 "Show offline contacts",
1198 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1199 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1201 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1204 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1205 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1207 spec = g_param_spec_boolean ("empty", "Empty",
1208 "Is the view currently empty?",
1210 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1211 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1213 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1214 G_OBJECT_CLASS_TYPE (klass),
1216 0, NULL, NULL, NULL,
1218 1, FOLKS_TYPE_INDIVIDUAL);
1220 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1221 G_OBJECT_CLASS_TYPE (klass),
1223 0, NULL, NULL, NULL,
1225 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1227 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1228 G_OBJECT_CLASS_TYPE (klass),
1230 0, NULL, NULL, NULL,
1232 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1234 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1235 G_OBJECT_CLASS_TYPE (klass),
1237 0, g_signal_accumulator_true_handled, NULL, NULL,
1239 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1241 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1245 empathy_roster_view_init (EmpathyRosterView *self)
1247 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1248 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1250 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1251 NULL, (GDestroyNotify) g_hash_table_unref);
1252 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1254 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1256 self->priv->events = g_queue_new ();
1258 self->priv->empty = TRUE;
1262 empathy_roster_view_new (EmpathyRosterModel *model)
1264 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1266 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1272 empathy_roster_view_show_offline (EmpathyRosterView *self,
1275 if (self->priv->show_offline == show)
1278 self->priv->show_offline = show;
1279 egg_list_box_refilter (EGG_LIST_BOX (self));
1281 g_object_notify (G_OBJECT (self), "show-offline");
1285 clear_view (EmpathyRosterView *self)
1287 gtk_container_foreach (GTK_CONTAINER (self),
1288 (GtkCallback) gtk_widget_destroy, NULL);
1290 g_hash_table_remove_all (self->priv->roster_contacts);
1291 g_hash_table_remove_all (self->priv->roster_groups);
1292 g_hash_table_remove_all (self->priv->displayed_contacts);
1296 empathy_roster_view_show_groups (EmpathyRosterView *self,
1299 if (self->priv->show_groups == show)
1302 self->priv->show_groups = show;
1304 /* TODO: block sort/filter? */
1306 populate_view (self);
1308 g_object_notify (G_OBJECT (self), "show-groups");
1312 select_first_contact (EmpathyRosterView *self)
1314 GList *children, *l;
1316 children = gtk_container_get_children (GTK_CONTAINER (self));
1317 for (l = children; l != NULL; l = g_list_next (l))
1319 GtkWidget *child = l->data;
1321 if (!gtk_widget_get_child_visible (child))
1324 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1327 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1331 g_list_free (children);
1335 search_text_notify_cb (EmpathyLiveSearch *search,
1337 EmpathyRosterView *self)
1339 egg_list_box_refilter (EGG_LIST_BOX (self));
1341 select_first_contact (self);
1345 search_activate_cb (GtkWidget *search,
1346 EmpathyRosterView *self)
1348 EggListBox *box = EGG_LIST_BOX (self);
1351 child = egg_list_box_get_selected_child (box);
1355 empathy_roster_view_child_activated (box, child);
1359 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1360 EmpathyLiveSearch *search)
1362 if (self->priv->search != NULL)
1364 g_signal_handlers_disconnect_by_func (self->priv->search,
1365 search_text_notify_cb, self);
1366 g_signal_handlers_disconnect_by_func (self->priv->search,
1367 search_activate_cb, self);
1369 g_clear_object (&self->priv->search);
1375 self->priv->search = g_object_ref (search);
1377 g_signal_connect (self->priv->search, "notify::text",
1378 G_CALLBACK (search_text_notify_cb), self);
1379 g_signal_connect (self->priv->search, "activate",
1380 G_CALLBACK (search_activate_cb), self);
1384 empathy_roster_view_is_empty (EmpathyRosterView *self)
1386 return self->priv->empty;
1390 empathy_roster_view_is_searching (EmpathyRosterView *self)
1392 return (self->priv->search != NULL &&
1393 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1396 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1397 * Empathy's internals. */
1399 empathy_roster_view_add_event (EmpathyRosterView *self,
1400 FolksIndividual *individual,
1404 GHashTable *contacts;
1406 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1407 if (contacts == NULL)
1410 self->priv->last_event_id++;
1412 g_queue_push_head (self->priv->events,
1413 event_new (self->priv->last_event_id, individual, icon, user_data));
1415 start_flashing (self);
1417 return self->priv->last_event_id;
1421 empathy_roster_view_remove_event (EmpathyRosterView *self,
1426 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1427 l = g_list_next (l))
1429 Event *event = l->data;
1431 if (event->id == event_id)
1433 remove_event (self, event);
1440 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1444 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1446 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1449 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));