1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <libempathy/empathy-debug.h>
35 #include "empathy-contact-list-store.h"
36 #include "empathy-contact-groups.h"
37 #include "empathy-ui-utils.h"
38 #include "empathy-gtk-enum-types.h"
40 #define DEBUG_DOMAIN "ContactListStore"
42 /* Active users are those which have recently changed state
43 * (e.g. online, offline or from normal to a busy state).
46 /* Time in seconds user is shown as active */
47 #define ACTIVE_USER_SHOW_TIME 7
49 /* Time in seconds after connecting which we wait before active users are enabled */
50 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
54 struct _EmpathyContactListStorePriv {
55 EmpathyContactList *list;
56 gboolean show_offline;
57 gboolean show_avatars;
60 EmpathyContactListStoreSort sort_criterium;
63 EmpathyContactGroupsFunc get_contact_groups;
64 gpointer get_contact_groups_data;
74 EmpathyContact *contact;
80 EmpathyContactListStore *store;
81 EmpathyContact *contact;
85 static void empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass);
86 static void empathy_contact_list_store_init (EmpathyContactListStore *list);
87 static void contact_list_store_finalize (GObject *object);
88 static void contact_list_store_get_property (GObject *object,
92 static void contact_list_store_set_property (GObject *object,
96 static gboolean contact_list_store_finalize_foreach (GtkTreeModel *model,
100 static void contact_list_store_setup (EmpathyContactListStore *store);
101 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
102 static void contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
103 EmpathyContact *contact,
104 EmpathyContact *actor,
108 EmpathyContactListStore *store);
109 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
110 EmpathyContact *contact,
113 EmpathyContactListStore *store);
114 static void contact_list_store_add_contact (EmpathyContactListStore *store,
115 EmpathyContact *contact);
116 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
117 EmpathyContact *contact);
118 static void contact_list_store_contact_update (EmpathyContactListStore *store,
119 EmpathyContact *contact);
120 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
122 EmpathyContactListStore *store);
123 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
124 EmpathyContact *contact,
126 gboolean set_changed);
127 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
128 EmpathyContact *contact,
130 static void contact_list_store_contact_active_free (ShowActiveData *data);
131 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
132 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
136 static void contact_list_store_get_group (EmpathyContactListStore *store,
138 GtkTreeIter *iter_group_to_set,
139 GtkTreeIter *iter_separator_to_set,
141 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
145 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
149 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
153 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
154 EmpathyContact *contact);
155 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
158 EmpathyContactListStore *store);
168 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
171 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
173 GObjectClass *object_class = G_OBJECT_CLASS (klass);
175 object_class->finalize = contact_list_store_finalize;
176 object_class->get_property = contact_list_store_get_property;
177 object_class->set_property = contact_list_store_set_property;
179 g_object_class_install_property (object_class,
181 g_param_spec_boolean ("show-offline",
183 "Whether contact list should display "
187 g_object_class_install_property (object_class,
189 g_param_spec_boolean ("show-avatars",
191 "Whether contact list should display "
192 "avatars for contacts",
195 g_object_class_install_property (object_class,
197 g_param_spec_boolean ("is-compact",
199 "Whether the contact list is in compact mode or not",
203 g_object_class_install_property (object_class,
205 g_param_spec_enum ("sort-criterium",
207 "The sort criterium to use for sorting the contact list",
208 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
209 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
212 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
216 empathy_contact_list_store_init (EmpathyContactListStore *store)
218 EmpathyContactListStorePriv *priv;
220 priv = GET_PRIV (store);
222 priv->show_avatars = TRUE;
223 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
224 (GSourceFunc) contact_list_store_inibit_active_cb,
229 contact_list_store_finalize (GObject *object)
231 EmpathyContactListStorePriv *priv;
233 priv = GET_PRIV (object);
235 gtk_tree_model_foreach (GTK_TREE_MODEL (object),
236 (GtkTreeModelForeachFunc) contact_list_store_finalize_foreach,
240 g_signal_handlers_disconnect_by_func (priv->list,
241 G_CALLBACK (contact_list_store_members_changed_cb),
243 g_signal_handlers_disconnect_by_func (priv->list,
244 G_CALLBACK (contact_list_store_groups_changed_cb),
246 g_object_unref (priv->list);
249 if (priv->inhibit_active) {
250 g_source_remove (priv->inhibit_active);
253 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
257 contact_list_store_get_property (GObject *object,
262 EmpathyContactListStorePriv *priv;
264 priv = GET_PRIV (object);
267 case PROP_SHOW_OFFLINE:
268 g_value_set_boolean (value, priv->show_offline);
270 case PROP_SHOW_AVATARS:
271 g_value_set_boolean (value, priv->show_avatars);
273 case PROP_IS_COMPACT:
274 g_value_set_boolean (value, priv->is_compact);
276 case PROP_SORT_CRITERIUM:
277 g_value_set_enum (value, priv->sort_criterium);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
286 contact_list_store_set_property (GObject *object,
291 EmpathyContactListStorePriv *priv;
293 priv = GET_PRIV (object);
296 case PROP_SHOW_OFFLINE:
297 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
298 g_value_get_boolean (value));
300 case PROP_SHOW_AVATARS:
301 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
302 g_value_get_boolean (value));
304 case PROP_IS_COMPACT:
305 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
306 g_value_get_boolean (value));
308 case PROP_SORT_CRITERIUM:
309 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
310 g_value_get_enum (value));
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
318 EmpathyContactListStore *
319 empathy_contact_list_store_new (EmpathyContactList *list_iface)
321 EmpathyContactListStore *store;
322 EmpathyContactListStorePriv *priv;
325 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
327 store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
328 priv = GET_PRIV (store);
330 contact_list_store_setup (store);
331 priv->list = g_object_ref (list_iface);
333 /* Signal connection. */
334 g_signal_connect (priv->list,
336 G_CALLBACK (contact_list_store_members_changed_cb),
338 g_signal_connect (priv->list,
340 G_CALLBACK (contact_list_store_groups_changed_cb),
343 /* Add contacts already created. */
344 contacts = empathy_contact_list_get_members (priv->list);
345 for (l = contacts; l; l = l->next) {
346 contact_list_store_members_changed_cb (priv->list, l->data,
351 g_object_unref (l->data);
353 g_list_free (contacts);
359 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
361 EmpathyContactListStorePriv *priv;
363 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
365 priv = GET_PRIV (store);
371 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
373 EmpathyContactListStorePriv *priv;
375 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
377 priv = GET_PRIV (store);
379 return priv->show_offline;
383 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
384 gboolean show_offline)
386 EmpathyContactListStorePriv *priv;
388 gboolean show_active;
390 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
392 priv = GET_PRIV (store);
394 priv->show_offline = show_offline;
395 show_active = priv->show_active;
397 /* Disable temporarily. */
398 priv->show_active = FALSE;
400 contacts = empathy_contact_list_get_members (priv->list);
401 for (l = contacts; l; l = l->next) {
402 contact_list_store_contact_update (store, l->data);
404 g_object_unref (l->data);
406 g_list_free (contacts);
408 /* Restore to original setting. */
409 priv->show_active = show_active;
413 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
415 EmpathyContactListStorePriv *priv;
417 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
419 priv = GET_PRIV (store);
421 return priv->show_avatars;
425 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
426 gboolean show_avatars)
428 EmpathyContactListStorePriv *priv;
431 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
433 priv = GET_PRIV (store);
435 priv->show_avatars = show_avatars;
437 model = GTK_TREE_MODEL (store);
439 gtk_tree_model_foreach (model,
440 (GtkTreeModelForeachFunc)
441 contact_list_store_update_list_mode_foreach,
446 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
448 EmpathyContactListStorePriv *priv;
450 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
452 priv = GET_PRIV (store);
454 return priv->is_compact;
458 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
461 EmpathyContactListStorePriv *priv;
464 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
466 priv = GET_PRIV (store);
468 priv->is_compact = is_compact;
470 model = GTK_TREE_MODEL (store);
472 gtk_tree_model_foreach (model,
473 (GtkTreeModelForeachFunc)
474 contact_list_store_update_list_mode_foreach,
478 EmpathyContactListStoreSort
479 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
481 EmpathyContactListStorePriv *priv;
483 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
485 priv = GET_PRIV (store);
487 return priv->sort_criterium;
491 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
492 EmpathyContactListStoreSort sort_criterium)
494 EmpathyContactListStorePriv *priv;
496 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
498 priv = GET_PRIV (store);
500 priv->sort_criterium = sort_criterium;
502 switch (sort_criterium) {
503 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
504 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
509 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
510 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
518 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
522 gboolean is_separator = FALSE;
524 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
526 gtk_tree_model_get (model, iter,
527 COL_IS_SEPARATOR, &is_separator,
534 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
536 gboolean *path_is_group)
538 GtkTreeIter parent_iter, iter;
542 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
545 *path_is_group = FALSE;
548 if (!gtk_tree_model_get_iter (model, &iter, path)) {
552 gtk_tree_model_get (model, &iter,
553 COL_IS_GROUP, &is_group,
561 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
567 gtk_tree_model_get (model, &iter,
568 COL_IS_GROUP, &is_group,
578 *path_is_group = TRUE;
585 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
589 gpointer search_data)
591 gchar *name, *name_folded;
595 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
601 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
607 name_folded = g_utf8_casefold (name, -1);
608 key_folded = g_utf8_casefold (key, -1);
610 if (name_folded && key_folded &&
611 strstr (name_folded, key_folded)) {
618 g_free (name_folded);
625 contact_list_store_finalize_foreach (GtkTreeModel *model,
630 EmpathyContactListStore *store = user_data;
631 EmpathyContact *contact = NULL;
633 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
634 COL_CONTACT, &contact,
638 g_signal_handlers_disconnect_by_func (contact,
639 G_CALLBACK (contact_list_store_contact_updated_cb),
647 contact_list_store_setup (EmpathyContactListStore *store)
649 EmpathyContactListStorePriv *priv;
650 GType types[] = {G_TYPE_STRING, /* Status icon-name */
651 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
652 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
653 G_TYPE_STRING, /* Name */
654 G_TYPE_STRING, /* Status string */
655 G_TYPE_BOOLEAN, /* Show status */
656 EMPATHY_TYPE_CONTACT, /* Contact type */
657 G_TYPE_BOOLEAN, /* Is group */
658 G_TYPE_BOOLEAN, /* Is active */
659 G_TYPE_BOOLEAN, /* Is online */
660 G_TYPE_BOOLEAN, /* Is separator */
661 G_TYPE_BOOLEAN}; /* Can VoIP */
663 priv = GET_PRIV (store);
665 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
668 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
670 contact_list_store_name_sort_func,
672 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
674 contact_list_store_state_sort_func,
677 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
678 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
682 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
684 EmpathyContactListStorePriv *priv;
686 priv = GET_PRIV (store);
688 priv->show_active = TRUE;
689 priv->inhibit_active = 0;
695 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
696 EmpathyContact *contact,
697 EmpathyContact *actor,
701 EmpathyContactListStore *store)
703 EmpathyContactListStorePriv *priv;
705 priv = GET_PRIV (store);
707 empathy_debug (DEBUG_DOMAIN,
708 "Contact %s (%d) %s",
709 empathy_contact_get_id (contact),
710 empathy_contact_get_handle (contact),
711 is_member ? "added" : "removed");
714 g_signal_connect (contact, "notify::presence",
715 G_CALLBACK (contact_list_store_contact_updated_cb),
717 g_signal_connect (contact, "notify::name",
718 G_CALLBACK (contact_list_store_contact_updated_cb),
720 g_signal_connect (contact, "notify::avatar",
721 G_CALLBACK (contact_list_store_contact_updated_cb),
723 g_signal_connect (contact, "notify::capabilities",
724 G_CALLBACK (contact_list_store_contact_updated_cb),
727 contact_list_store_add_contact (store, contact);
729 g_signal_handlers_disconnect_by_func (contact,
730 G_CALLBACK (contact_list_store_contact_updated_cb),
733 contact_list_store_remove_contact (store, contact);
738 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
739 EmpathyContact *contact,
742 EmpathyContactListStore *store)
744 EmpathyContactListStorePriv *priv;
745 gboolean show_active;
747 priv = GET_PRIV (store);
749 empathy_debug (DEBUG_DOMAIN, "Updating groups for contact %s (%d)",
750 empathy_contact_get_id (contact),
751 empathy_contact_get_handle (contact));
753 /* We do this to make sure the groups are correct, if not, we
754 * would have to check the groups already set up for each
755 * contact and then see what has been updated.
757 show_active = priv->show_active;
758 priv->show_active = FALSE;
759 contact_list_store_remove_contact (store, contact);
760 contact_list_store_add_contact (store, contact);
761 priv->show_active = show_active;
765 contact_list_store_add_contact (EmpathyContactListStore *store,
766 EmpathyContact *contact)
768 EmpathyContactListStorePriv *priv;
772 priv = GET_PRIV (store);
774 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
778 groups = empathy_contact_list_get_groups (priv->list, contact);
780 /* If no groups just add it at the top level. */
782 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
783 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
784 COL_NAME, empathy_contact_get_name (contact),
785 COL_CONTACT, contact,
787 COL_IS_SEPARATOR, FALSE,
788 COL_CAN_VOIP, empathy_contact_can_voip (contact),
792 /* Else add to each group. */
793 for (l = groups; l; l = l->next) {
794 GtkTreeIter iter_group;
796 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
798 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
800 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
801 COL_NAME, empathy_contact_get_name (contact),
802 COL_CONTACT, contact,
804 COL_IS_SEPARATOR, FALSE,
805 COL_CAN_VOIP, empathy_contact_can_voip (contact),
809 g_list_free (groups);
811 contact_list_store_contact_update (store, contact);
816 contact_list_store_remove_contact (EmpathyContactListStore *store,
817 EmpathyContact *contact)
819 EmpathyContactListStorePriv *priv;
823 priv = GET_PRIV (store);
825 iters = contact_list_store_find_contact (store, contact);
831 model = GTK_TREE_MODEL (store);
833 for (l = iters; l; l = l->next) {
836 /* NOTE: it is only <= 2 here because we have
837 * separators after the group name, otherwise it
840 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
841 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
842 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
844 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
848 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
853 contact_list_store_contact_update (EmpathyContactListStore *store,
854 EmpathyContact *contact)
856 EmpathyContactListStorePriv *priv;
857 ShowActiveData *data;
861 gboolean should_be_in_list;
862 gboolean was_online = TRUE;
863 gboolean now_online = FALSE;
864 gboolean set_model = FALSE;
865 gboolean do_remove = FALSE;
866 gboolean do_set_active = FALSE;
867 gboolean do_set_refresh = FALSE;
868 GdkPixbuf *pixbuf_avatar;
870 priv = GET_PRIV (store);
872 model = GTK_TREE_MODEL (store);
874 iters = contact_list_store_find_contact (store, contact);
881 /* Get online state now. */
882 now_online = empathy_contact_is_online (contact);
884 if (priv->show_offline || now_online) {
885 should_be_in_list = TRUE;
887 should_be_in_list = FALSE;
890 if (!in_list && !should_be_in_list) {
892 empathy_debug (DEBUG_DOMAIN,
893 "Contact:'%s' in list:NO, should be:NO",
894 empathy_contact_get_name (contact));
896 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
900 else if (in_list && !should_be_in_list) {
901 empathy_debug (DEBUG_DOMAIN,
902 "Contact:'%s' in list:YES, should be:NO",
903 empathy_contact_get_name (contact));
905 if (priv->show_active) {
907 do_set_active = TRUE;
908 do_set_refresh = TRUE;
911 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
913 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
914 contact_list_store_remove_contact (store, contact);
917 else if (!in_list && should_be_in_list) {
918 empathy_debug (DEBUG_DOMAIN,
919 "Contact:'%s' in list:NO, should be:YES",
920 empathy_contact_get_name (contact));
922 contact_list_store_add_contact (store, contact);
924 if (priv->show_active) {
925 do_set_active = TRUE;
927 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
930 empathy_debug (DEBUG_DOMAIN,
931 "Contact:'%s' in list:YES, should be:YES",
932 empathy_contact_get_name (contact));
934 /* Get online state before. */
935 if (iters && g_list_length (iters) > 0) {
936 gtk_tree_model_get (model, iters->data,
937 COL_IS_ONLINE, &was_online,
941 /* Is this really an update or an online/offline. */
942 if (priv->show_active) {
943 if (was_online != now_online) {
944 do_set_active = TRUE;
945 do_set_refresh = TRUE;
947 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
948 was_online ? "online -> offline" :
949 "offline -> online");
951 /* Was TRUE for presence updates. */
952 /* do_set_active = FALSE; */
953 do_set_refresh = TRUE;
955 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
962 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
963 for (l = iters; l && set_model; l = l->next) {
964 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
965 COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
966 COL_PIXBUF_AVATAR, pixbuf_avatar,
967 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
968 COL_NAME, empathy_contact_get_name (contact),
969 COL_STATUS, empathy_contact_get_status (contact),
970 COL_STATUS_VISIBLE, !priv->is_compact,
972 COL_IS_ONLINE, now_online,
973 COL_IS_SEPARATOR, FALSE,
974 COL_CAN_VOIP, empathy_contact_can_voip (contact),
979 g_object_unref (pixbuf_avatar);
982 if (priv->show_active && do_set_active) {
983 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
986 data = contact_list_store_contact_active_new (store, contact, do_remove);
987 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
988 (GSourceFunc) contact_list_store_contact_active_cb,
993 /* FIXME: when someone goes online then offline quickly, the
994 * first timeout sets the user to be inactive and the second
995 * timeout removes the user from the contact list, really we
996 * should remove the first timeout.
998 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1003 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1005 EmpathyContactListStore *store)
1007 empathy_debug (DEBUG_DOMAIN,
1008 "Contact:'%s' updated, checking roster is in sync...",
1009 empathy_contact_get_name (contact));
1011 contact_list_store_contact_update (store, contact);
1015 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1016 EmpathyContact *contact,
1018 gboolean set_changed)
1020 EmpathyContactListStorePriv *priv;
1021 GtkTreeModel *model;
1024 priv = GET_PRIV (store);
1025 model = GTK_TREE_MODEL (store);
1027 iters = contact_list_store_find_contact (store, contact);
1028 for (l = iters; l; l = l->next) {
1031 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1032 COL_IS_ACTIVE, active,
1035 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1038 path = gtk_tree_model_get_path (model, l->data);
1039 gtk_tree_model_row_changed (model, path, l->data);
1040 gtk_tree_path_free (path);
1044 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1045 g_list_free (iters);
1049 static ShowActiveData *
1050 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1051 EmpathyContact *contact,
1054 ShowActiveData *data;
1056 empathy_debug (DEBUG_DOMAIN,
1057 "Contact:'%s' now active, and %s be removed",
1058 empathy_contact_get_name (contact),
1059 remove ? "WILL" : "WILL NOT");
1061 data = g_slice_new0 (ShowActiveData);
1063 data->store = g_object_ref (store);
1064 data->contact = g_object_ref (contact);
1065 data->remove = remove;
1071 contact_list_store_contact_active_free (ShowActiveData *data)
1073 g_object_unref (data->contact);
1074 g_object_unref (data->store);
1076 g_slice_free (ShowActiveData, data);
1080 contact_list_store_contact_active_cb (ShowActiveData *data)
1082 EmpathyContactListStorePriv *priv;
1084 priv = GET_PRIV (data->store);
1087 !priv->show_offline &&
1088 !empathy_contact_is_online (data->contact)) {
1089 empathy_debug (DEBUG_DOMAIN,
1090 "Contact:'%s' active timeout, removing item",
1091 empathy_contact_get_name (data->contact));
1092 contact_list_store_remove_contact (data->store, data->contact);
1095 empathy_debug (DEBUG_DOMAIN,
1096 "Contact:'%s' no longer active",
1097 empathy_contact_get_name (data->contact));
1099 contact_list_store_contact_set_active (data->store,
1104 contact_list_store_contact_active_free (data);
1110 contact_list_store_get_group_foreach (GtkTreeModel *model,
1118 /* Groups are only at the top level. */
1119 if (gtk_tree_path_get_depth (path) != 1) {
1123 gtk_tree_model_get (model, iter,
1125 COL_IS_GROUP, &is_group,
1128 if (is_group && strcmp (str, fg->name) == 0) {
1139 contact_list_store_get_group (EmpathyContactListStore *store,
1141 GtkTreeIter *iter_group_to_set,
1142 GtkTreeIter *iter_separator_to_set,
1145 EmpathyContactListStorePriv *priv;
1146 GtkTreeModel *model;
1147 GtkTreeIter iter_group;
1148 GtkTreeIter iter_separator;
1151 priv = GET_PRIV (store);
1153 memset (&fg, 0, sizeof (fg));
1157 model = GTK_TREE_MODEL (store);
1158 gtk_tree_model_foreach (model,
1159 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1167 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1168 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1169 COL_ICON_STATUS, NULL,
1172 COL_IS_ACTIVE, FALSE,
1173 COL_IS_SEPARATOR, FALSE,
1176 if (iter_group_to_set) {
1177 *iter_group_to_set = iter_group;
1180 gtk_tree_store_append (GTK_TREE_STORE (store),
1183 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1184 COL_IS_SEPARATOR, TRUE,
1187 if (iter_separator_to_set) {
1188 *iter_separator_to_set = iter_separator;
1195 if (iter_group_to_set) {
1196 *iter_group_to_set = fg.iter;
1199 iter_separator = fg.iter;
1201 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1202 gboolean is_separator;
1204 gtk_tree_model_get (model, &iter_separator,
1205 COL_IS_SEPARATOR, &is_separator,
1208 if (is_separator && iter_separator_to_set) {
1209 *iter_separator_to_set = iter_separator;
1216 contact_list_store_state_sort_func (GtkTreeModel *model,
1217 GtkTreeIter *iter_a,
1218 GtkTreeIter *iter_b,
1222 gchar *name_a, *name_b;
1223 gboolean is_separator_a, is_separator_b;
1224 EmpathyContact *contact_a, *contact_b;
1225 EmpathyPresence *presence_a, *presence_b;
1226 McPresence state_a, state_b;
1228 gtk_tree_model_get (model, iter_a,
1230 COL_CONTACT, &contact_a,
1231 COL_IS_SEPARATOR, &is_separator_a,
1233 gtk_tree_model_get (model, iter_b,
1235 COL_CONTACT, &contact_b,
1236 COL_IS_SEPARATOR, &is_separator_b,
1239 /* Separator or group? */
1240 if (is_separator_a || is_separator_b) {
1241 if (is_separator_a) {
1243 } else if (is_separator_b) {
1246 } else if (!contact_a && contact_b) {
1248 } else if (contact_a && !contact_b) {
1250 } else if (!contact_a && !contact_b) {
1252 ret_val = g_utf8_collate (name_a, name_b);
1259 /* If we managed to get this far, we can start looking at
1262 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1263 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1265 if (!presence_a && presence_b) {
1267 } else if (presence_a && !presence_b) {
1269 } else if (!presence_a && !presence_b) {
1270 /* Both offline, sort by name */
1271 ret_val = g_utf8_collate (name_a, name_b);
1273 state_a = empathy_presence_get_state (presence_a);
1274 state_b = empathy_presence_get_state (presence_b);
1276 if (state_a < state_b) {
1278 } else if (state_a > state_b) {
1281 /* Fallback: compare by name */
1282 ret_val = g_utf8_collate (name_a, name_b);
1291 g_object_unref (contact_a);
1295 g_object_unref (contact_b);
1302 contact_list_store_name_sort_func (GtkTreeModel *model,
1303 GtkTreeIter *iter_a,
1304 GtkTreeIter *iter_b,
1307 gchar *name_a, *name_b;
1308 EmpathyContact *contact_a, *contact_b;
1309 gboolean is_separator_a, is_separator_b;
1312 gtk_tree_model_get (model, iter_a,
1314 COL_CONTACT, &contact_a,
1315 COL_IS_SEPARATOR, &is_separator_a,
1317 gtk_tree_model_get (model, iter_b,
1319 COL_CONTACT, &contact_b,
1320 COL_IS_SEPARATOR, &is_separator_b,
1323 /* If contact is NULL it means it's a group. */
1325 if (is_separator_a || is_separator_b) {
1326 if (is_separator_a) {
1328 } else if (is_separator_b) {
1331 } else if (!contact_a && contact_b) {
1333 } else if (contact_a && !contact_b) {
1336 ret_val = g_utf8_collate (name_a, name_b);
1343 g_object_unref (contact_a);
1347 g_object_unref (contact_b);
1354 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1359 EmpathyContact *contact;
1361 gtk_tree_model_get (model, iter,
1362 COL_CONTACT, &contact,
1369 if (empathy_contact_equal (contact, fc->contact)) {
1371 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1373 g_object_unref (contact);
1379 contact_list_store_find_contact (EmpathyContactListStore *store,
1380 EmpathyContact *contact)
1382 EmpathyContactListStorePriv *priv;
1383 GtkTreeModel *model;
1387 priv = GET_PRIV (store);
1389 memset (&fc, 0, sizeof (fc));
1391 fc.contact = contact;
1393 model = GTK_TREE_MODEL (store);
1394 gtk_tree_model_foreach (model,
1395 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1406 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1409 EmpathyContactListStore *store)
1411 EmpathyContactListStorePriv *priv;
1412 gboolean show_avatar = FALSE;
1414 priv = GET_PRIV (store);
1416 if (priv->show_avatars && !priv->is_compact) {
1420 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1421 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1422 COL_STATUS_VISIBLE, !priv->is_compact,