2 #include "empathy-roster-view.h"
4 #include <glib/gi18n-lib.h>
6 #include "empathy-contact-groups.h"
7 #include "empathy-roster-contact.h"
8 #include "empathy-roster-group.h"
9 #include "empathy-ui-utils.h"
11 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, GTK_TYPE_LIST_BOX)
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
16 /* Delay in milliseconds between the last stroke on the keyboard and the start
17 * of the live search. */
18 #define SEARCH_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;
66 gboolean show_offline;
70 TpawLiveSearch *search;
72 EmpathyRosterModel *model;
75 /* Prototypes to break cycles */
76 static void remove_from_group (EmpathyRosterView *self,
77 FolksIndividual *individual,
83 FolksIndividual *individual;
90 FolksIndividual *individual,
94 Event *event = g_slice_new (Event);
97 event->individual = g_object_ref (individual);
98 event->icon = g_strdup (icon);
99 event->user_data = user_data;
104 event_free (gpointer data)
107 g_object_unref (event->individual);
108 g_free (event->icon);
110 g_slice_free (Event, event);
114 empathy_roster_view_get_property (GObject *object,
119 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
124 g_value_set_object (value, self->priv->model);
126 case PROP_SHOW_OFFLINE:
127 g_value_set_boolean (value, self->priv->show_offline);
129 case PROP_SHOW_GROUPS:
130 g_value_set_boolean (value, self->priv->show_groups);
133 g_value_set_boolean (value, self->priv->empty);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
142 empathy_roster_view_set_property (GObject *object,
147 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
152 g_assert (self->priv->model == NULL);
153 self->priv->model = g_value_dup_object (value);
155 case PROP_SHOW_OFFLINE:
156 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
158 case PROP_SHOW_GROUPS:
159 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
168 roster_contact_changed_cb (GtkListBoxRow *child,
170 EmpathyRosterView *self)
172 gtk_list_box_row_changed (child);
176 add_roster_contact (EmpathyRosterView *self,
177 FolksIndividual *individual,
182 contact = empathy_roster_contact_new (individual, group);
184 /* Need to refilter if online is changed */
185 g_signal_connect (contact, "notify::online",
186 G_CALLBACK (roster_contact_changed_cb), self);
188 /* Need to resort if alias is changed */
189 g_signal_connect (contact, "notify::alias",
190 G_CALLBACK (roster_contact_changed_cb), self);
192 /* Need to resort if most recent event changed */
193 g_signal_connect (contact, "notify::most-recent-event",
194 G_CALLBACK (roster_contact_changed_cb), self);
196 gtk_widget_show (contact);
197 gtk_container_add (GTK_CONTAINER (self), contact);
203 group_expanded_cb (GtkWidget *expander,
205 EmpathyRosterGroup *group)
209 widgets = empathy_roster_group_get_widgets (group);
210 for (l = widgets; l != NULL; l = g_list_next (l))
212 gtk_list_box_row_changed (l->data);
215 g_list_free (widgets);
217 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
218 gtk_expander_get_expanded (group->expander));
221 static EmpathyRosterGroup *
222 lookup_roster_group (EmpathyRosterView *self,
225 return g_hash_table_lookup (self->priv->roster_groups, group);
228 static EmpathyRosterGroup *
229 ensure_roster_group (EmpathyRosterView *self,
232 GtkWidget *roster_group;
234 roster_group = (GtkWidget *) lookup_roster_group (self, group);
235 if (roster_group != NULL)
236 return EMPATHY_ROSTER_GROUP (roster_group);
238 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
239 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
240 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
241 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
243 roster_group = empathy_roster_group_new (group, NULL);
245 gtk_expander_set_expanded (EMPATHY_ROSTER_GROUP (roster_group)->expander,
246 empathy_contact_group_get_expanded (group));
248 g_signal_connect (EMPATHY_ROSTER_GROUP (roster_group)->expander,
249 "notify::expanded", G_CALLBACK (group_expanded_cb), roster_group);
251 gtk_widget_show (roster_group);
252 gtk_container_add (GTK_CONTAINER (self), roster_group);
254 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
257 return EMPATHY_ROSTER_GROUP (roster_group);
261 update_empty (EmpathyRosterView *self,
264 if (self->priv->empty == empty)
267 self->priv->empty = empty;
268 g_object_notify (G_OBJECT (self), "empty");
271 static gboolean filter_group (EmpathyRosterView *self,
272 EmpathyRosterGroup *group);
275 at_least_one_group_displayed (EmpathyRosterView *self)
280 g_hash_table_iter_init (&iter, self->priv->roster_groups);
281 while (g_hash_table_iter_next (&iter, NULL, &v))
283 EmpathyRosterGroup *group = EMPATHY_ROSTER_GROUP (v);
285 if (filter_group (self, group))
293 check_if_empty (EmpathyRosterView *self)
295 /* Roster is considered as empty if there is no contact *and* no group
296 * currently displayed. */
297 if (g_hash_table_size (self->priv->displayed_contacts) != 0 ||
298 at_least_one_group_displayed (self))
300 update_empty (self, FALSE);
304 update_empty (self, TRUE);
308 update_group_widgets (EmpathyRosterView *self,
309 EmpathyRosterGroup *group,
310 EmpathyRosterContact *contact,
313 guint old_count, count;
315 old_count = empathy_roster_group_get_widgets_count (group);
318 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
320 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
322 if (count != old_count)
324 gtk_list_box_row_changed (GTK_LIST_BOX_ROW (group));
326 check_if_empty (self);
331 add_to_group (EmpathyRosterView *self,
332 FolksIndividual *individual,
336 GHashTable *contacts;
337 EmpathyRosterGroup *roster_group = NULL;
339 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
340 if (contacts == NULL)
343 if (g_hash_table_lookup (contacts, group) != NULL)
346 if (tp_strdiff (group, NO_GROUP))
347 roster_group = ensure_roster_group (self, group);
349 contact = add_roster_contact (self, individual, group);
350 g_hash_table_insert (contacts, g_strdup (group), contact);
352 if (roster_group != NULL)
354 update_group_widgets (self, roster_group,
355 EMPATHY_ROSTER_CONTACT (contact), TRUE);
358 if (tp_strdiff (group, NO_GROUP) &&
359 tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED) &&
360 g_hash_table_size (contacts) == 2 /* 1:Ungrouped and 2:first group */)
362 remove_from_group (self, individual,
363 EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
368 individual_favourite_change_cb (FolksIndividual *individual,
370 EmpathyRosterView *self)
372 /* We may have to refilter the contact as only favorite contacts are always
373 * displayed regardless of their presence. */
374 GHashTable *contacts;
377 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
378 if (contacts == NULL)
381 if (self->priv->show_groups)
382 contact = g_hash_table_lookup (contacts,
383 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
385 contact = g_hash_table_lookup (contacts, NO_GROUP);
390 gtk_list_box_row_changed (GTK_LIST_BOX_ROW (contact));
394 individual_added (EmpathyRosterView *self,
395 FolksIndividual *individual)
397 GHashTable *contacts;
399 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
400 if (contacts != NULL)
403 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
405 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
407 if (!self->priv->show_groups)
409 add_to_group (self, individual, NO_GROUP);
415 groups = empathy_roster_model_dup_groups_for_individual (self->priv->model,
418 if (g_list_length (groups) > 0)
420 for (l = groups; l != NULL; l = g_list_next (l))
422 add_to_group (self, individual, l->data);
427 /* No group, adds to Ungrouped */
428 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
431 g_list_free_full (groups, g_free);
434 tp_g_signal_connect_object (individual, "notify::is-favourite",
435 G_CALLBACK (individual_favourite_change_cb), self, 0);
439 set_event_icon_on_individual (EmpathyRosterView *self,
440 FolksIndividual *individual,
443 GHashTable *contacts;
447 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
448 if (contacts == NULL)
451 g_hash_table_iter_init (&iter, contacts);
452 while (g_hash_table_iter_next (&iter, NULL, &v))
454 EmpathyRosterContact *contact =v;
456 empathy_roster_contact_set_event_icon (contact, icon);
461 flash_event (Event *event,
462 EmpathyRosterView *self)
464 set_event_icon_on_individual (self, event->individual, event->icon);
468 unflash_event (Event *event,
469 EmpathyRosterView *self)
471 set_event_icon_on_individual (self, event->individual, NULL);
475 flash_cb (gpointer data)
477 EmpathyRosterView *self = data;
479 if (self->priv->display_flash_event)
481 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
482 self->priv->display_flash_event = FALSE;
486 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
487 self->priv->display_flash_event = TRUE;
494 start_flashing (EmpathyRosterView *self)
496 if (self->priv->flash_id != 0)
499 self->priv->display_flash_event = TRUE;
501 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
506 stop_flashing (EmpathyRosterView *self)
508 if (self->priv->flash_id == 0)
511 g_source_remove (self->priv->flash_id);
512 self->priv->flash_id = 0;
516 remove_event (EmpathyRosterView *self,
519 unflash_event (event, self);
520 g_queue_remove (self->priv->events, event);
522 if (g_queue_get_length (self->priv->events) == 0)
524 stop_flashing (self);
529 remove_all_individual_event (EmpathyRosterView *self,
530 FolksIndividual *individual)
534 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
537 Event *event = l->data;
539 if (event->individual == individual)
541 remove_event (self, event);
548 individual_removed (EmpathyRosterView *self,
549 FolksIndividual *individual)
551 GHashTable *contacts;
555 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
556 if (contacts == NULL)
559 remove_all_individual_event (self, individual);
561 g_hash_table_iter_init (&iter, contacts);
562 while (g_hash_table_iter_next (&iter, &key, &value))
564 const gchar *group_name = key;
565 GtkWidget *contact = value;
566 EmpathyRosterGroup *group;
568 group = lookup_roster_group (self, group_name);
571 update_group_widgets (self, group,
572 EMPATHY_ROSTER_CONTACT (contact), FALSE);
575 gtk_container_remove (GTK_CONTAINER (self), contact);
578 g_hash_table_remove (self->priv->roster_contacts, individual);
582 individual_added_cb (EmpathyRosterModel *model,
583 FolksIndividual *individual,
584 EmpathyRosterView *self)
586 individual_added (self, individual);
590 individual_removed_cb (EmpathyRosterModel *model,
591 FolksIndividual *individual,
592 EmpathyRosterView *self)
594 individual_removed (self, individual);
598 contact_in_top (EmpathyRosterView *self,
599 EmpathyRosterContact *contact)
601 if (!self->priv->show_groups)
603 /* Always display top contacts in non-group mode. */
605 FolksIndividual *individual;
606 gboolean result = FALSE;
608 individual = empathy_roster_contact_get_individual (contact);
610 groups = empathy_roster_model_dup_groups_for_individual (
611 self->priv->model, individual);
613 if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
614 (GCompareFunc) g_strcmp0) != NULL)
617 g_list_free_full (groups, g_free);
622 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
623 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
624 /* If we are displaying contacts, we only want to *always* display the
625 * RosterContact which is displayed at the top; not the ones displayed in
626 * the 'normal' group sections */
633 compare_roster_contacts_by_conversation_time (EmpathyRosterContact *a,
634 EmpathyRosterContact *b)
638 ts_a = empathy_roster_contact_get_most_recent_timestamp (a);
639 ts_b = empathy_roster_contact_get_most_recent_timestamp (b);
641 if (ts_a == ts_b) return 0;
642 if (ts_a > ts_b) return -1;
647 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
648 EmpathyRosterContact *b)
650 FolksIndividual *ind_a, *ind_b;
651 const gchar *alias_a, *alias_b;
653 ind_a = empathy_roster_contact_get_individual (a);
654 ind_b = empathy_roster_contact_get_individual (b);
656 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
657 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
659 return g_utf8_collate (alias_a, alias_b);
663 compare_roster_contacts_no_group (EmpathyRosterView *self,
664 EmpathyRosterContact *a,
665 EmpathyRosterContact *b)
667 gboolean top_a, top_b;
669 top_a = contact_in_top (self, a);
670 top_b = contact_in_top (self, b);
673 /* Both contacts are in the top of the roster (or not). Sort them
675 return compare_roster_contacts_by_conversation_time (a, b);
683 compare_group_names (const gchar *group_a,
684 const gchar *group_b)
686 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
689 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
692 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
694 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
697 return g_utf8_collate (group_a, group_b);
701 compare_roster_contacts_with_groups (EmpathyRosterView *self,
702 EmpathyRosterContact *a,
703 EmpathyRosterContact *b)
705 const gchar *group_a, *group_b;
707 group_a = empathy_roster_contact_get_group (a);
708 group_b = empathy_roster_contact_get_group (b);
710 if (!tp_strdiff (group_a, group_b))
711 /* Same group, compare the contacts */
712 return compare_roster_contacts_by_conversation_time (a, b);
715 return compare_group_names (group_a, group_b);
719 compare_roster_contacts (EmpathyRosterView *self,
720 EmpathyRosterContact *a,
721 EmpathyRosterContact *b)
723 if (!self->priv->show_groups)
724 return compare_roster_contacts_no_group (self, a, b);
726 return compare_roster_contacts_with_groups (self, a, b);
730 compare_roster_groups (EmpathyRosterGroup *a,
731 EmpathyRosterGroup *b)
733 const gchar *name_a, *name_b;
735 name_a = empathy_roster_group_get_name (a);
736 name_b = empathy_roster_group_get_name (b);
738 return compare_group_names (name_a, name_b);
742 compare_contact_group (EmpathyRosterContact *contact,
743 EmpathyRosterGroup *group)
745 const char *contact_group, *group_name;
747 contact_group = empathy_roster_contact_get_group (contact);
748 group_name = empathy_roster_group_get_name (group);
750 if (!tp_strdiff (contact_group, group_name))
751 /* @contact is in @group, @group has to be displayed first */
754 /* @contact is in a different group, sort by group name */
755 return compare_group_names (contact_group, group_name);
759 roster_view_sort (GtkListBoxRow *a,
763 EmpathyRosterView *self = user_data;
765 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
766 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
767 EMPATHY_ROSTER_CONTACT (b));
768 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
769 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
770 EMPATHY_ROSTER_GROUP (b));
771 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
772 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
773 EMPATHY_ROSTER_GROUP (b));
774 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
775 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
776 EMPATHY_ROSTER_GROUP (a));
778 g_return_val_if_reached (0);
782 update_header (GtkListBoxRow *row,
783 GtkListBoxRow *before,
788 /* No separator before the first row */
789 gtk_list_box_row_set_header (row, NULL);
793 if (gtk_list_box_row_get_header (row) != NULL)
796 gtk_list_box_row_set_header (row,
797 gtk_separator_new (GTK_ORIENTATION_HORIZONTAL));
801 is_searching (EmpathyRosterView *self)
803 if (self->priv->search == NULL)
806 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
810 add_to_displayed (EmpathyRosterView *self,
811 EmpathyRosterContact *contact)
813 FolksIndividual *individual;
814 GHashTable *contacts;
818 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
821 g_hash_table_add (self->priv->displayed_contacts, contact);
822 update_empty (self, FALSE);
824 /* Groups of this contact may now be displayed if we just displays the first
825 * child in this group. */
827 if (!self->priv->show_groups)
830 individual = empathy_roster_contact_get_individual (contact);
831 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
832 if (contacts == NULL)
835 g_hash_table_iter_init (&iter, contacts);
836 while (g_hash_table_iter_next (&iter, &k, NULL))
838 const gchar *group_name = k;
839 GtkListBoxRow *group;
841 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
845 gtk_list_box_row_changed (group);
850 remove_from_displayed (EmpathyRosterView *self,
851 EmpathyRosterContact *contact)
853 g_hash_table_remove (self->priv->displayed_contacts, contact);
855 check_if_empty (self);
859 contact_is_favorite (EmpathyRosterContact *contact)
861 FolksIndividual *individual;
863 individual = empathy_roster_contact_get_individual (contact);
865 return folks_favourite_details_get_is_favourite (
866 FOLKS_FAVOURITE_DETAILS (individual));
870 * check if @contact should be displayed according to @self's current status
871 * and without consideration for the state of @contact's groups.
874 contact_should_be_displayed (EmpathyRosterView *self,
875 EmpathyRosterContact *contact)
877 if (is_searching (self))
879 FolksIndividual *individual;
881 individual = empathy_roster_contact_get_individual (contact);
883 return empathy_individual_match_string (individual,
884 tpaw_live_search_get_text (self->priv->search),
885 tpaw_live_search_get_words (self->priv->search));
888 if (self->priv->show_offline)
891 if (contact_in_top (self, contact) &&
892 contact_is_favorite (contact))
893 /* Favorite top contacts are always displayed */
896 return empathy_roster_contact_is_online (contact);
901 filter_contact (EmpathyRosterView *self,
902 EmpathyRosterContact *contact)
906 displayed = contact_should_be_displayed (self, contact);
908 if (self->priv->show_groups)
910 const gchar *group_name;
911 EmpathyRosterGroup *group;
913 group_name = empathy_roster_contact_get_group (contact);
914 group = lookup_roster_group (self, group_name);
918 /* When searching, always display even if the group is closed */
919 if (!is_searching (self) &&
920 !gtk_expander_get_expanded (group->expander))
927 add_to_displayed (self, contact);
931 remove_from_displayed (self, contact);
938 filter_group (EmpathyRosterView *self,
939 EmpathyRosterGroup *group)
942 gboolean result = FALSE;
944 /* Display the group if it contains at least one displayed contact */
945 widgets = empathy_roster_group_get_widgets (group);
946 for (l = widgets; l != NULL; l = g_list_next (l))
948 EmpathyRosterContact *contact = l->data;
950 if (contact_should_be_displayed (self, contact))
957 g_list_free (widgets);
963 filter_list (GtkListBoxRow *child,
966 EmpathyRosterView *self = user_data;
968 if (EMPATHY_IS_ROSTER_CONTACT (child))
969 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
971 else if (EMPATHY_IS_ROSTER_GROUP (child))
972 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
974 g_return_val_if_reached (FALSE);
978 populate_view (EmpathyRosterView *self)
980 GList *individuals, *l;
982 individuals = empathy_roster_model_get_individuals (self->priv->model);
983 for (l = individuals; l != NULL; l = g_list_next (l))
985 FolksIndividual *individual = l->data;
987 individual_added (self, individual);
990 g_list_free (individuals);
994 remove_from_group (EmpathyRosterView *self,
995 FolksIndividual *individual,
998 GHashTable *contacts;
1000 EmpathyRosterGroup *roster_group;
1002 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1003 if (contacts == NULL)
1006 contact = g_hash_table_lookup (contacts, group);
1007 if (contact == NULL)
1010 g_hash_table_remove (contacts, group);
1012 if (g_hash_table_size (contacts) == 0)
1014 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
1017 roster_group = lookup_roster_group (self, group);
1019 if (roster_group != NULL)
1021 update_group_widgets (self, roster_group,
1022 EMPATHY_ROSTER_CONTACT (contact), FALSE);
1025 gtk_container_remove (GTK_CONTAINER (self), contact);
1029 groups_changed_cb (EmpathyRosterModel *model,
1030 FolksIndividual *individual,
1033 EmpathyRosterView *self)
1035 if (!self->priv->show_groups)
1037 gtk_list_box_invalidate_sort (GTK_LIST_BOX (self));
1043 add_to_group (self, individual, group);
1047 remove_from_group (self, individual, group);
1052 empathy_roster_view_constructed (GObject *object)
1054 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1055 void (*chain_up) (GObject *) =
1056 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1058 if (chain_up != NULL)
1061 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1063 /* Get saved group states. */
1064 empathy_contact_groups_get_all ();
1066 populate_view (self);
1068 tp_g_signal_connect_object (self->priv->model, "individual-added",
1069 G_CALLBACK (individual_added_cb), self, 0);
1070 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1071 G_CALLBACK (individual_removed_cb), self, 0);
1072 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1073 G_CALLBACK (groups_changed_cb), self, 0);
1075 gtk_list_box_set_sort_func (GTK_LIST_BOX (self),
1076 roster_view_sort, self, NULL);
1078 gtk_list_box_set_header_func (GTK_LIST_BOX (self), update_header, self, NULL);
1080 gtk_list_box_set_filter_func (GTK_LIST_BOX (self), filter_list, self, NULL);
1082 gtk_list_box_set_activate_on_single_click (GTK_LIST_BOX (self), FALSE);
1086 clear_view (EmpathyRosterView *self)
1088 g_hash_table_remove_all (self->priv->roster_contacts);
1089 g_hash_table_remove_all (self->priv->roster_groups);
1090 g_hash_table_remove_all (self->priv->displayed_contacts);
1092 gtk_container_foreach (GTK_CONTAINER (self),
1093 (GtkCallback) gtk_widget_destroy, NULL);
1097 empathy_roster_view_dispose (GObject *object)
1099 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1100 void (*chain_up) (GObject *) =
1101 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1103 /* Start by clearing the view so our internal hash tables are cleared from
1104 * objects being destroyed. */
1107 stop_flashing (self);
1109 empathy_roster_view_set_live_search (self, NULL);
1110 g_clear_object (&self->priv->model);
1112 if (self->priv->search_id != 0)
1114 g_source_remove (self->priv->search_id);
1115 self->priv->search_id = 0;
1118 if (chain_up != NULL)
1123 empathy_roster_view_finalize (GObject *object)
1125 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1126 void (*chain_up) (GObject *) =
1127 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1129 g_hash_table_unref (self->priv->roster_contacts);
1130 g_hash_table_unref (self->priv->roster_groups);
1131 g_hash_table_unref (self->priv->displayed_contacts);
1132 g_queue_free_full (self->priv->events, event_free);
1134 if (chain_up != NULL)
1139 empathy_roster_view_row_activated (GtkListBox *box,
1142 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1143 EmpathyRosterContact *contact;
1144 FolksIndividual *individual;
1147 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1150 contact = EMPATHY_ROSTER_CONTACT (row);
1151 individual = empathy_roster_contact_get_individual (contact);
1153 /* Activate the oldest event associated with this contact, if any */
1154 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1155 l = g_list_previous (l))
1157 Event *event = l->data;
1159 if (event->individual == individual)
1161 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1167 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1171 fire_popup_individual_menu (EmpathyRosterView *self,
1176 EmpathyRosterContact *contact;
1177 FolksIndividual *individual;
1178 const gchar *active_group;
1180 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1183 contact = EMPATHY_ROSTER_CONTACT (row);
1184 individual = empathy_roster_contact_get_individual (contact);
1186 active_group = empathy_roster_contact_get_group (contact);
1187 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1188 active_group, individual, button, time);
1192 empathy_roster_view_button_press_event (GtkWidget *widget,
1193 GdkEventButton *event)
1195 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1196 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1197 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1199 if (event->button == 3)
1203 row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), event->y);
1207 gtk_list_box_select_row (GTK_LIST_BOX (self), row);
1209 fire_popup_individual_menu (self, row, event->button, event->time);
1213 return chain_up (widget, event);
1217 empathy_roster_view_key_press_event (GtkWidget *widget,
1220 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1221 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1222 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1224 if (event->keyval == GDK_KEY_Menu)
1228 row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
1231 fire_popup_individual_menu (self, row, 0, event->time);
1234 return chain_up (widget, event);
1238 * @out_row: (out) (allow-none)
1241 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1243 GtkListBoxRow **out_row)
1247 row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
1249 if (out_row != NULL)
1252 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1255 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));
1259 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1264 row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
1266 if (EMPATHY_IS_ROSTER_CONTACT (row))
1267 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (row));
1268 else if (EMPATHY_IS_ROSTER_GROUP (row))
1269 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (row));
1275 empathy_roster_view_query_tooltip (GtkWidget *widget,
1278 gboolean keyboard_mode,
1279 GtkTooltip *tooltip)
1281 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1282 FolksIndividual *individual;
1286 individual = empathy_roster_view_get_individual_at_y (self, y, &row);
1287 if (individual == NULL)
1290 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1291 individual, keyboard_mode, tooltip, &result);
1295 GtkAllocation allocation;
1297 gtk_widget_get_allocation (GTK_WIDGET (row), &allocation);
1298 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1305 empathy_roster_view_remove (GtkContainer *container,
1308 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1309 void (*chain_up) (GtkContainer *, GtkWidget *) =
1310 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1312 chain_up (container, widget);
1314 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1315 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1319 empathy_roster_view_class_init (
1320 EmpathyRosterViewClass *klass)
1322 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1323 GtkListBoxClass *box_class = GTK_LIST_BOX_CLASS (klass);
1324 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1325 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1328 oclass->get_property = empathy_roster_view_get_property;
1329 oclass->set_property = empathy_roster_view_set_property;
1330 oclass->constructed = empathy_roster_view_constructed;
1331 oclass->dispose = empathy_roster_view_dispose;
1332 oclass->finalize = empathy_roster_view_finalize;
1334 widget_class->button_press_event = empathy_roster_view_button_press_event;
1335 widget_class->key_press_event = empathy_roster_view_key_press_event;
1336 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1338 container_class->remove = empathy_roster_view_remove;
1340 box_class->row_activated = empathy_roster_view_row_activated;
1342 spec = g_param_spec_object ("model", "Model",
1343 "EmpathyRosterModel",
1344 EMPATHY_TYPE_ROSTER_MODEL,
1345 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1346 g_object_class_install_property (oclass, PROP_MODEL, spec);
1348 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1349 "Show offline contacts",
1351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1352 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1354 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1358 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1360 spec = g_param_spec_boolean ("empty", "Empty",
1361 "Is the view currently empty?",
1363 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1364 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1366 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1367 G_OBJECT_CLASS_TYPE (klass),
1369 0, NULL, NULL, NULL,
1371 1, FOLKS_TYPE_INDIVIDUAL);
1373 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1374 G_OBJECT_CLASS_TYPE (klass),
1376 0, NULL, NULL, NULL,
1378 4, G_TYPE_STRING, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT,
1381 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1382 G_OBJECT_CLASS_TYPE (klass),
1384 0, NULL, NULL, NULL,
1386 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1388 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1389 G_OBJECT_CLASS_TYPE (klass),
1391 0, g_signal_accumulator_true_handled, NULL, NULL,
1393 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1395 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1399 empathy_roster_view_init (EmpathyRosterView *self)
1401 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1402 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1404 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1405 NULL, (GDestroyNotify) g_hash_table_unref);
1406 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1408 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1410 self->priv->events = g_queue_new ();
1412 self->priv->empty = TRUE;
1416 empathy_roster_view_new (EmpathyRosterModel *model)
1418 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1420 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1426 empathy_roster_view_show_offline (EmpathyRosterView *self,
1429 if (self->priv->show_offline == show)
1432 self->priv->show_offline = show;
1433 gtk_list_box_invalidate_filter (GTK_LIST_BOX (self));
1435 g_object_notify (G_OBJECT (self), "show-offline");
1439 empathy_roster_view_show_groups (EmpathyRosterView *self,
1442 if (self->priv->show_groups == show)
1445 self->priv->show_groups = show;
1447 /* TODO: block sort/filter? */
1449 populate_view (self);
1451 g_object_notify (G_OBJECT (self), "show-groups");
1455 select_first_contact (EmpathyRosterView *self)
1457 GList *children, *l;
1459 children = gtk_container_get_children (GTK_CONTAINER (self));
1460 for (l = children; l != NULL; l = g_list_next (l))
1462 GtkWidget *child = l->data;
1464 if (!gtk_widget_get_child_visible (child))
1467 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1470 gtk_list_box_select_row (GTK_LIST_BOX (self), GTK_LIST_BOX_ROW (child));
1474 g_list_free (children);
1478 search_timeout_cb (EmpathyRosterView *self)
1480 gtk_list_box_invalidate_filter (GTK_LIST_BOX (self));
1482 select_first_contact (self);
1484 self->priv->search_id = 0;
1485 return G_SOURCE_REMOVE;
1489 search_text_notify_cb (TpawLiveSearch *search,
1491 EmpathyRosterView *self)
1493 if (self->priv->search_id != 0)
1494 g_source_remove (self->priv->search_id);
1496 self->priv->search_id = g_timeout_add (SEARCH_TIMEOUT,
1497 (GSourceFunc) search_timeout_cb, self);
1501 search_activate_cb (GtkWidget *search,
1502 EmpathyRosterView *self)
1504 GtkListBox *box = GTK_LIST_BOX (self);
1507 row = gtk_list_box_get_selected_row (box);
1511 empathy_roster_view_row_activated (box, row);
1515 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1516 TpawLiveSearch *search)
1518 if (self->priv->search != NULL)
1520 g_signal_handlers_disconnect_by_func (self->priv->search,
1521 search_text_notify_cb, self);
1522 g_signal_handlers_disconnect_by_func (self->priv->search,
1523 search_activate_cb, self);
1525 g_clear_object (&self->priv->search);
1531 self->priv->search = g_object_ref (search);
1533 g_signal_connect (self->priv->search, "notify::text",
1534 G_CALLBACK (search_text_notify_cb), self);
1535 g_signal_connect (self->priv->search, "activate",
1536 G_CALLBACK (search_activate_cb), self);
1540 empathy_roster_view_is_empty (EmpathyRosterView *self)
1542 return self->priv->empty;
1546 empathy_roster_view_is_searching (EmpathyRosterView *self)
1548 return (self->priv->search != NULL &&
1549 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1552 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1553 * Empathy's internals. */
1555 empathy_roster_view_add_event (EmpathyRosterView *self,
1556 FolksIndividual *individual,
1560 GHashTable *contacts;
1562 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1563 if (contacts == NULL)
1566 self->priv->last_event_id++;
1568 g_queue_push_head (self->priv->events,
1569 event_new (self->priv->last_event_id, individual, icon, user_data));
1571 start_flashing (self);
1573 return self->priv->last_event_id;
1577 empathy_roster_view_remove_event (EmpathyRosterView *self,
1582 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1583 l = g_list_next (l))
1585 Event *event = l->data;
1587 if (event->id == event_id)
1589 remove_event (self, event);
1596 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1600 row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
1602 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1605 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));