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/gossip-debug.h>
35 #include "gossip-contact-list-store.h"
36 #include "gossip-contact-groups.h"
37 #include "gossip-ui-utils.h"
39 #define DEBUG_DOMAIN "ContactListStore"
41 /* Active users are those which have recently changed state
42 * (e.g. online, offline or from normal to a busy state).
45 /* Time user is shown as active */
46 #define ACTIVE_USER_SHOW_TIME 7000
48 /* Time after connecting which we wait before active users are enabled */
49 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5000
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT_LIST_STORE, GossipContactListStorePriv))
53 struct _GossipContactListStorePriv {
54 EmpathyContactList *list;
55 gboolean show_offline;
56 gboolean show_avatars;
59 GossipContactListStoreSort sort_criterium;
61 GossipContactGroupsFunc get_contact_groups;
62 gpointer get_contact_groups_data;
72 GossipContact *contact;
78 GossipContactListStore *store;
79 GossipContact *contact;
83 static void gossip_contact_list_store_class_init (GossipContactListStoreClass *klass);
84 static void gossip_contact_list_store_init (GossipContactListStore *list);
85 static void contact_list_store_finalize (GObject *object);
86 static void contact_list_store_get_property (GObject *object,
90 static void contact_list_store_set_property (GObject *object,
94 static void contact_list_store_setup (GossipContactListStore *store);
95 static void contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
96 GossipContact *contact,
97 GossipContactListStore *store);
98 static void contact_list_store_add_contact (GossipContactListStore *store,
99 GossipContact *contact);
100 static void contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
101 GossipContact *contact,
102 GossipContactListStore *store);
103 static void contact_list_store_remove_contact (GossipContactListStore *store,
104 GossipContact *contact);
105 static void contact_list_store_contact_update (GossipContactListStore *store,
106 GossipContact *contact);
107 static void contact_list_store_contact_groups_updated_cb (GossipContact *contact,
109 GossipContactListStore *store);
110 static void contact_list_store_contact_updated_cb (GossipContact *contact,
112 GossipContactListStore *store);
113 static void contact_list_store_contact_set_active (GossipContactListStore *store,
114 GossipContact *contact,
116 gboolean set_changed);
117 static ShowActiveData * contact_list_store_contact_active_new (GossipContactListStore *store,
118 GossipContact *contact,
120 static void contact_list_store_contact_active_free (ShowActiveData *data);
121 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
122 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
126 static void contact_list_store_get_group (GossipContactListStore *store,
128 GtkTreeIter *iter_group_to_set,
129 GtkTreeIter *iter_separator_to_set,
131 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
135 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
139 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
143 static GList * contact_list_store_find_contact (GossipContactListStore *store,
144 GossipContact *contact);
145 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
148 GossipContactListStore *store);
159 gossip_contact_list_store_sort_get_type (void)
161 static GType etype = 0;
164 static const GEnumValue values[] = {
165 { GOSSIP_CONTACT_LIST_STORE_SORT_NAME,
166 "GOSSIP_CONTACT_LIST_STORE_SORT_NAME",
168 { GOSSIP_CONTACT_LIST_STORE_SORT_STATE,
169 "GOSSIP_CONTACT_LIST_STORE_SORT_STATE",
174 etype = g_enum_register_static ("GossipContactListStoreSort", values);
180 G_DEFINE_TYPE (GossipContactListStore, gossip_contact_list_store, GTK_TYPE_TREE_STORE);
183 gossip_contact_list_store_class_init (GossipContactListStoreClass *klass)
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
187 object_class->finalize = contact_list_store_finalize;
188 object_class->get_property = contact_list_store_get_property;
189 object_class->set_property = contact_list_store_set_property;
191 g_object_class_install_property (object_class,
193 g_param_spec_boolean ("show-offline",
195 "Whether contact list should display "
199 g_object_class_install_property (object_class,
201 g_param_spec_boolean ("show-avatars",
203 "Whether contact list should display "
204 "avatars for contacts",
207 g_object_class_install_property (object_class,
209 g_param_spec_boolean ("is-compact",
211 "Whether the contact list is in compact mode or not",
215 g_object_class_install_property (object_class,
217 g_param_spec_enum ("sort-criterium",
219 "The sort criterium to use for sorting the contact list",
220 GOSSIP_TYPE_CONTACT_LIST_STORE_SORT,
221 GOSSIP_CONTACT_LIST_STORE_SORT_NAME,
224 g_type_class_add_private (object_class, sizeof (GossipContactListStorePriv));
228 gossip_contact_list_store_init (GossipContactListStore *store)
230 GossipContactListStorePriv *priv;
232 priv = GET_PRIV (store);
234 priv->is_compact = FALSE;
235 priv->show_active = TRUE;
236 priv->show_avatars = TRUE;
240 contact_list_store_finalize (GObject *object)
242 GossipContactListStorePriv *priv;
244 priv = GET_PRIV (object);
246 /* FIXME: disconnect all signals on the list and contacts */
249 g_object_unref (priv->list);
252 G_OBJECT_CLASS (gossip_contact_list_store_parent_class)->finalize (object);
256 contact_list_store_get_property (GObject *object,
261 GossipContactListStorePriv *priv;
263 priv = GET_PRIV (object);
266 case PROP_SHOW_OFFLINE:
267 g_value_set_boolean (value, priv->show_offline);
269 case PROP_SHOW_AVATARS:
270 g_value_set_boolean (value, priv->show_avatars);
272 case PROP_IS_COMPACT:
273 g_value_set_boolean (value, priv->is_compact);
275 case PROP_SORT_CRITERIUM:
276 g_value_set_enum (value, priv->sort_criterium);
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
285 contact_list_store_set_property (GObject *object,
290 GossipContactListStorePriv *priv;
292 priv = GET_PRIV (object);
295 case PROP_SHOW_OFFLINE:
296 gossip_contact_list_store_set_show_offline (GOSSIP_CONTACT_LIST_STORE (object),
297 g_value_get_boolean (value));
299 case PROP_SHOW_AVATARS:
300 gossip_contact_list_store_set_show_avatars (GOSSIP_CONTACT_LIST_STORE (object),
301 g_value_get_boolean (value));
303 case PROP_IS_COMPACT:
304 gossip_contact_list_store_set_is_compact (GOSSIP_CONTACT_LIST_STORE (object),
305 g_value_get_boolean (value));
307 case PROP_SORT_CRITERIUM:
308 gossip_contact_list_store_set_sort_criterium (GOSSIP_CONTACT_LIST_STORE (object),
309 g_value_get_enum (value));
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
317 GossipContactListStore *
318 gossip_contact_list_store_new (EmpathyContactList *list_iface)
320 GossipContactListStore *store;
321 GossipContactListStorePriv *priv;
324 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
326 store = g_object_new (GOSSIP_TYPE_CONTACT_LIST_STORE, NULL);
327 priv = GET_PRIV (store);
329 contact_list_store_setup (store);
330 priv->list = g_object_ref (list_iface);
332 /* Signal connection. */
333 g_signal_connect (priv->list,
335 G_CALLBACK (contact_list_store_contact_added_cb),
337 g_signal_connect (priv->list,
339 G_CALLBACK (contact_list_store_contact_removed_cb),
342 /* Add contacts already created */
343 contacts = empathy_contact_list_get_contacts (priv->list);
344 for (l = contacts; l; l = l->next) {
345 GossipContact *contact;
349 contact_list_store_contact_added_cb (priv->list, contact, store);
351 g_object_unref (contact);
353 g_list_free (contacts);
359 gossip_contact_list_store_get_list_iface (GossipContactListStore *store)
361 GossipContactListStorePriv *priv;
363 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), FALSE);
365 priv = GET_PRIV (store);
371 gossip_contact_list_store_get_show_offline (GossipContactListStore *store)
373 GossipContactListStorePriv *priv;
375 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), FALSE);
377 priv = GET_PRIV (store);
379 return priv->show_offline;
383 gossip_contact_list_store_set_show_offline (GossipContactListStore *store,
384 gboolean show_offline)
386 GossipContactListStorePriv *priv;
388 gboolean show_active;
390 g_return_if_fail (GOSSIP_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_contacts (priv->list);
401 for (l = contacts; l; l = l->next) {
402 GossipContact *contact;
404 contact = GOSSIP_CONTACT (l->data);
406 contact_list_store_contact_update (store, contact);
408 g_object_unref (contact);
410 g_list_free (contacts);
412 /* Restore to original setting. */
413 priv->show_active = show_active;
417 gossip_contact_list_store_get_show_avatars (GossipContactListStore *store)
419 GossipContactListStorePriv *priv;
421 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), TRUE);
423 priv = GET_PRIV (store);
425 return priv->show_avatars;
429 gossip_contact_list_store_set_show_avatars (GossipContactListStore *store,
430 gboolean show_avatars)
432 GossipContactListStorePriv *priv;
435 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
437 priv = GET_PRIV (store);
439 priv->show_avatars = show_avatars;
441 model = GTK_TREE_MODEL (store);
443 gtk_tree_model_foreach (model,
444 (GtkTreeModelForeachFunc)
445 contact_list_store_update_list_mode_foreach,
450 gossip_contact_list_store_get_is_compact (GossipContactListStore *store)
452 GossipContactListStorePriv *priv;
454 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), TRUE);
456 priv = GET_PRIV (store);
458 return priv->is_compact;
462 gossip_contact_list_store_set_is_compact (GossipContactListStore *store,
465 GossipContactListStorePriv *priv;
468 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
470 priv = GET_PRIV (store);
472 priv->is_compact = is_compact;
474 model = GTK_TREE_MODEL (store);
476 gtk_tree_model_foreach (model,
477 (GtkTreeModelForeachFunc)
478 contact_list_store_update_list_mode_foreach,
482 GossipContactListStoreSort
483 gossip_contact_list_store_get_sort_criterium (GossipContactListStore *store)
485 GossipContactListStorePriv *priv;
487 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), 0);
489 priv = GET_PRIV (store);
491 return priv->sort_criterium;
495 gossip_contact_list_store_set_sort_criterium (GossipContactListStore *store,
496 GossipContactListStoreSort sort_criterium)
498 GossipContactListStorePriv *priv;
500 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
502 priv = GET_PRIV (store);
504 priv->sort_criterium = sort_criterium;
506 switch (sort_criterium) {
507 case GOSSIP_CONTACT_LIST_STORE_SORT_STATE:
508 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
513 case GOSSIP_CONTACT_LIST_STORE_SORT_NAME:
514 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
522 gossip_contact_list_store_row_separator_func (GtkTreeModel *model,
526 gboolean is_separator = FALSE;
528 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
530 gtk_tree_model_get (model, iter,
531 COL_IS_SEPARATOR, &is_separator,
538 gossip_contact_list_store_get_parent_group (GtkTreeModel *model,
540 gboolean *path_is_group)
542 GtkTreeIter parent_iter, iter;
546 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
549 *path_is_group = FALSE;
552 if (!gtk_tree_model_get_iter (model, &iter, path)) {
556 gtk_tree_model_get (model, &iter,
557 COL_IS_GROUP, &is_group,
565 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
571 gtk_tree_model_get (model, &iter,
572 COL_IS_GROUP, &is_group,
582 *path_is_group = TRUE;
589 gossip_contact_list_store_search_equal_func (GtkTreeModel *model,
593 gpointer search_data)
595 gchar *name, *name_folded;
599 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
605 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
611 name_folded = g_utf8_casefold (name, -1);
612 key_folded = g_utf8_casefold (key, -1);
614 if (name_folded && key_folded &&
615 strstr (name_folded, key_folded)) {
622 g_free (name_folded);
629 gossip_contact_list_store_set_contact_groups_func (GossipContactListStore *store,
630 GossipContactGroupsFunc func,
633 GossipContactListStorePriv *priv;
636 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
638 priv = GET_PRIV (store);
641 priv->get_contact_groups = func;
642 priv->get_contact_groups_data = user_data;
644 priv->get_contact_groups = NULL;
645 priv->get_contact_groups_data = NULL;
648 /* If we set a custom function to get contacts groups we have to
649 * disconnect our default notify::groups signal and wait for the user
650 * to call himself gossip_contact_list_store_update_contact_groups ()
651 * when needed. If func is NULL we come back to default.
653 contacts = empathy_contact_list_get_contacts (priv->list);
654 for (l = contacts; l; l = l->next) {
655 GossipContact *contact;
660 g_signal_handlers_disconnect_by_func (contact,
661 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
664 g_signal_connect (contact, "notify::groups",
665 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
669 gossip_contact_list_store_update_contact_groups (store, contact);
671 g_object_unref (contact);
673 g_list_free (contacts);
677 gossip_contact_list_store_update_contact_groups (GossipContactListStore *store,
678 GossipContact *contact)
680 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' updating groups",
681 gossip_contact_get_name (contact));
683 /* We do this to make sure the groups are correct, if not, we
684 * would have to check the groups already set up for each
685 * contact and then see what has been updated.
687 contact_list_store_remove_contact (store, contact);
688 contact_list_store_add_contact (store, contact);
692 contact_list_store_setup (GossipContactListStore *store)
694 GossipContactListStorePriv *priv;
695 GType types[] = {G_TYPE_STRING, /* Status icon-name */
696 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
697 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
698 G_TYPE_STRING, /* Name */
699 G_TYPE_STRING, /* Status string */
700 G_TYPE_BOOLEAN, /* Show status */
701 GOSSIP_TYPE_CONTACT, /* Contact type */
702 G_TYPE_BOOLEAN, /* Is group */
703 G_TYPE_BOOLEAN, /* Is active */
704 G_TYPE_BOOLEAN, /* Is online */
705 G_TYPE_BOOLEAN}; /* Is separator */
707 priv = GET_PRIV (store);
709 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
712 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
714 contact_list_store_name_sort_func,
716 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
718 contact_list_store_state_sort_func,
721 gossip_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
725 contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
726 GossipContact *contact,
727 GossipContactListStore *store)
729 GossipContactListStorePriv *priv;
731 priv = GET_PRIV (store);
733 gossip_debug (DEBUG_DOMAIN,
734 "Contact:'%s' added",
735 gossip_contact_get_name (contact));
737 if (!priv->get_contact_groups) {
738 g_signal_connect (contact, "notify::groups",
739 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
742 g_signal_connect (contact, "notify::presence",
743 G_CALLBACK (contact_list_store_contact_updated_cb),
745 g_signal_connect (contact, "notify::name",
746 G_CALLBACK (contact_list_store_contact_updated_cb),
748 g_signal_connect (contact, "notify::avatar",
749 G_CALLBACK (contact_list_store_contact_updated_cb),
751 g_signal_connect (contact, "notify::type",
752 G_CALLBACK (contact_list_store_contact_updated_cb),
755 contact_list_store_add_contact (store, contact);
759 contact_list_store_add_contact (GossipContactListStore *store,
760 GossipContact *contact)
762 GossipContactListStorePriv *priv;
766 priv = GET_PRIV (store);
768 if (!priv->show_offline && !gossip_contact_is_online (contact)) {
772 /* If no groups just add it at the top level. */
773 if (priv->get_contact_groups) {
774 groups = priv->get_contact_groups (contact,
775 priv->get_contact_groups_data);
777 groups = gossip_contact_get_groups (contact);
781 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
782 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
783 COL_NAME, gossip_contact_get_name (contact),
784 COL_CONTACT, contact,
786 COL_IS_SEPARATOR, FALSE,
790 /* Else add to each group. */
791 for (l = groups; l; l = l->next) {
792 GtkTreeIter iter_group;
800 contact_list_store_get_group (store, name, &iter_group, NULL, NULL);
802 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
804 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
805 COL_NAME, gossip_contact_get_name (contact),
806 COL_CONTACT, contact,
808 COL_IS_SEPARATOR, FALSE,
812 contact_list_store_contact_update (store, contact);
816 contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
817 GossipContact *contact,
818 GossipContactListStore *store)
820 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
821 gossip_contact_get_name (contact));
823 /* Disconnect signals */
824 g_signal_handlers_disconnect_by_func (contact,
825 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
827 g_signal_handlers_disconnect_by_func (contact,
828 G_CALLBACK (contact_list_store_contact_updated_cb),
831 contact_list_store_remove_contact (store, contact);
835 contact_list_store_remove_contact (GossipContactListStore *store,
836 GossipContact *contact)
838 GossipContactListStorePriv *priv;
842 priv = GET_PRIV (store);
844 iters = contact_list_store_find_contact (store, contact);
850 model = GTK_TREE_MODEL (store);
852 for (l = iters; l; l = l->next) {
855 /* NOTE: it is only <= 2 here because we have
856 * separators after the group name, otherwise it
859 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
860 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
861 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
863 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
867 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
872 contact_list_store_contact_update (GossipContactListStore *store,
873 GossipContact *contact)
875 GossipContactListStorePriv *priv;
876 ShowActiveData *data;
880 gboolean should_be_in_list;
881 gboolean was_online = TRUE;
882 gboolean now_online = FALSE;
883 gboolean set_model = FALSE;
884 gboolean do_remove = FALSE;
885 gboolean do_set_active = FALSE;
886 gboolean do_set_refresh = FALSE;
887 GdkPixbuf *pixbuf_avatar;
889 priv = GET_PRIV (store);
891 model = GTK_TREE_MODEL (store);
893 iters = contact_list_store_find_contact (store, contact);
900 /* Get online state now. */
901 now_online = gossip_contact_is_online (contact);
903 if (priv->show_offline || now_online) {
904 should_be_in_list = TRUE;
906 should_be_in_list = FALSE;
909 if (!in_list && !should_be_in_list) {
911 gossip_debug (DEBUG_DOMAIN,
912 "Contact:'%s' in list:NO, should be:NO",
913 gossip_contact_get_name (contact));
915 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
919 else if (in_list && !should_be_in_list) {
920 gossip_debug (DEBUG_DOMAIN,
921 "Contact:'%s' in list:YES, should be:NO",
922 gossip_contact_get_name (contact));
924 if (priv->show_active) {
926 do_set_active = TRUE;
927 do_set_refresh = TRUE;
930 gossip_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
932 gossip_debug (DEBUG_DOMAIN, "Remove item (now)!");
933 contact_list_store_remove_contact (store, contact);
936 else if (!in_list && should_be_in_list) {
937 gossip_debug (DEBUG_DOMAIN,
938 "Contact:'%s' in list:NO, should be:YES",
939 gossip_contact_get_name (contact));
941 contact_list_store_add_contact (store, contact);
943 if (priv->show_active) {
944 do_set_active = TRUE;
946 gossip_debug (DEBUG_DOMAIN, "Set active (contact added)");
949 gossip_debug (DEBUG_DOMAIN,
950 "Contact:'%s' in list:YES, should be:YES",
951 gossip_contact_get_name (contact));
953 /* Get online state before. */
954 if (iters && g_list_length (iters) > 0) {
955 gtk_tree_model_get (model, iters->data,
956 COL_IS_ONLINE, &was_online,
960 /* Is this really an update or an online/offline. */
961 if (priv->show_active) {
962 if (was_online != now_online) {
963 do_set_active = TRUE;
964 do_set_refresh = TRUE;
966 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
967 was_online ? "online -> offline" :
968 "offline -> online");
970 /* Was TRUE for presence updates. */
971 /* do_set_active = FALSE; */
972 do_set_refresh = TRUE;
974 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated)");
981 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
982 for (l = iters; l && set_model; l = l->next) {
983 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
984 COL_ICON_STATUS, gossip_icon_name_for_contact (contact),
985 COL_PIXBUF_AVATAR, pixbuf_avatar,
986 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
987 COL_NAME, gossip_contact_get_name (contact),
988 COL_STATUS, gossip_contact_get_status (contact),
989 COL_STATUS_VISIBLE, !priv->is_compact,
991 COL_IS_ONLINE, now_online,
992 COL_IS_SEPARATOR, FALSE,
997 g_object_unref (pixbuf_avatar);
1000 if (priv->show_active && do_set_active) {
1001 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1003 if (do_set_active) {
1004 data = contact_list_store_contact_active_new (store, contact, do_remove);
1005 g_timeout_add (ACTIVE_USER_SHOW_TIME,
1006 (GSourceFunc) contact_list_store_contact_active_cb,
1011 /* FIXME: when someone goes online then offline quickly, the
1012 * first timeout sets the user to be inactive and the second
1013 * timeout removes the user from the contact list, really we
1014 * should remove the first timeout.
1016 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1017 g_list_free (iters);
1021 contact_list_store_contact_groups_updated_cb (GossipContact *contact,
1023 GossipContactListStore *store)
1025 gossip_contact_list_store_update_contact_groups (store, contact);
1029 contact_list_store_contact_updated_cb (GossipContact *contact,
1031 GossipContactListStore *store)
1033 gossip_debug (DEBUG_DOMAIN,
1034 "Contact:'%s' updated, checking roster is in sync...",
1035 gossip_contact_get_name (contact));
1037 contact_list_store_contact_update (store, contact);
1041 contact_list_store_contact_set_active (GossipContactListStore *store,
1042 GossipContact *contact,
1044 gboolean set_changed)
1046 GossipContactListStorePriv *priv;
1047 GtkTreeModel *model;
1050 priv = GET_PRIV (store);
1051 model = GTK_TREE_MODEL (store);
1053 iters = contact_list_store_find_contact (store, contact);
1054 for (l = iters; l; l = l->next) {
1057 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1058 COL_IS_ACTIVE, active,
1061 gossip_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1064 path = gtk_tree_model_get_path (model, l->data);
1065 gtk_tree_model_row_changed (model, path, l->data);
1066 gtk_tree_path_free (path);
1070 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1071 g_list_free (iters);
1075 static ShowActiveData *
1076 contact_list_store_contact_active_new (GossipContactListStore *store,
1077 GossipContact *contact,
1080 ShowActiveData *data;
1082 gossip_debug (DEBUG_DOMAIN,
1083 "Contact:'%s' now active, and %s be removed",
1084 gossip_contact_get_name (contact),
1085 remove ? "WILL" : "WILL NOT");
1087 data = g_slice_new0 (ShowActiveData);
1089 data->store = g_object_ref (store);
1090 data->contact = g_object_ref (contact);
1091 data->remove = remove;
1097 contact_list_store_contact_active_free (ShowActiveData *data)
1099 g_object_unref (data->contact);
1100 g_object_unref (data->store);
1102 g_slice_free (ShowActiveData, data);
1106 contact_list_store_contact_active_cb (ShowActiveData *data)
1108 GossipContactListStorePriv *priv;
1110 priv = GET_PRIV (data->store);
1113 !priv->show_offline &&
1114 !gossip_contact_is_online (data->contact)) {
1115 gossip_debug (DEBUG_DOMAIN,
1116 "Contact:'%s' active timeout, removing item",
1117 gossip_contact_get_name (data->contact));
1118 contact_list_store_remove_contact (data->store, data->contact);
1121 gossip_debug (DEBUG_DOMAIN,
1122 "Contact:'%s' no longer active",
1123 gossip_contact_get_name (data->contact));
1125 contact_list_store_contact_set_active (data->store,
1130 contact_list_store_contact_active_free (data);
1136 contact_list_store_get_group_foreach (GtkTreeModel *model,
1144 /* Groups are only at the top level. */
1145 if (gtk_tree_path_get_depth (path) != 1) {
1149 gtk_tree_model_get (model, iter,
1151 COL_IS_GROUP, &is_group,
1154 if (is_group && strcmp (str, fg->name) == 0) {
1165 contact_list_store_get_group (GossipContactListStore *store,
1167 GtkTreeIter *iter_group_to_set,
1168 GtkTreeIter *iter_separator_to_set,
1171 GossipContactListStorePriv *priv;
1172 GtkTreeModel *model;
1173 GtkTreeIter iter_group;
1174 GtkTreeIter iter_separator;
1177 priv = GET_PRIV (store);
1179 memset (&fg, 0, sizeof (fg));
1183 model = GTK_TREE_MODEL (store);
1184 gtk_tree_model_foreach (model,
1185 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1193 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1194 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1195 COL_ICON_STATUS, NULL,
1198 COL_IS_ACTIVE, FALSE,
1199 COL_IS_SEPARATOR, FALSE,
1202 if (iter_group_to_set) {
1203 *iter_group_to_set = iter_group;
1206 gtk_tree_store_append (GTK_TREE_STORE (store),
1209 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1210 COL_IS_SEPARATOR, TRUE,
1213 if (iter_separator_to_set) {
1214 *iter_separator_to_set = iter_separator;
1221 if (iter_group_to_set) {
1222 *iter_group_to_set = fg.iter;
1225 iter_separator = fg.iter;
1227 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1228 gboolean is_separator;
1230 gtk_tree_model_get (model, &iter_separator,
1231 COL_IS_SEPARATOR, &is_separator,
1234 if (is_separator && iter_separator_to_set) {
1235 *iter_separator_to_set = iter_separator;
1242 contact_list_store_state_sort_func (GtkTreeModel *model,
1243 GtkTreeIter *iter_a,
1244 GtkTreeIter *iter_b,
1248 gchar *name_a, *name_b;
1249 gboolean is_separator_a, is_separator_b;
1250 GossipContact *contact_a, *contact_b;
1251 GossipPresence *presence_a, *presence_b;
1252 McPresence state_a, state_b;
1254 gtk_tree_model_get (model, iter_a,
1256 COL_CONTACT, &contact_a,
1257 COL_IS_SEPARATOR, &is_separator_a,
1259 gtk_tree_model_get (model, iter_b,
1261 COL_CONTACT, &contact_b,
1262 COL_IS_SEPARATOR, &is_separator_b,
1265 /* Separator or group? */
1266 if (is_separator_a || is_separator_b) {
1267 if (is_separator_a) {
1269 } else if (is_separator_b) {
1272 } else if (!contact_a && contact_b) {
1274 } else if (contact_a && !contact_b) {
1276 } else if (!contact_a && !contact_b) {
1278 ret_val = g_utf8_collate (name_a, name_b);
1285 /* If we managed to get this far, we can start looking at
1288 presence_a = gossip_contact_get_presence (GOSSIP_CONTACT (contact_a));
1289 presence_b = gossip_contact_get_presence (GOSSIP_CONTACT (contact_b));
1291 if (!presence_a && presence_b) {
1293 } else if (presence_a && !presence_b) {
1295 } else if (!presence_a && !presence_b) {
1296 /* Both offline, sort by name */
1297 ret_val = g_utf8_collate (name_a, name_b);
1299 state_a = gossip_presence_get_state (presence_a);
1300 state_b = gossip_presence_get_state (presence_b);
1302 if (state_a < state_b) {
1304 } else if (state_a > state_b) {
1307 /* Fallback: compare by name */
1308 ret_val = g_utf8_collate (name_a, name_b);
1317 g_object_unref (contact_a);
1321 g_object_unref (contact_b);
1328 contact_list_store_name_sort_func (GtkTreeModel *model,
1329 GtkTreeIter *iter_a,
1330 GtkTreeIter *iter_b,
1333 gchar *name_a, *name_b;
1334 GossipContact *contact_a, *contact_b;
1335 gboolean is_separator_a, is_separator_b;
1338 gtk_tree_model_get (model, iter_a,
1340 COL_CONTACT, &contact_a,
1341 COL_IS_SEPARATOR, &is_separator_a,
1343 gtk_tree_model_get (model, iter_b,
1345 COL_CONTACT, &contact_b,
1346 COL_IS_SEPARATOR, &is_separator_b,
1349 /* If contact is NULL it means it's a group. */
1351 if (is_separator_a || is_separator_b) {
1352 if (is_separator_a) {
1354 } else if (is_separator_b) {
1357 } else if (!contact_a && contact_b) {
1359 } else if (contact_a && !contact_b) {
1362 ret_val = g_utf8_collate (name_a, name_b);
1369 g_object_unref (contact_a);
1373 g_object_unref (contact_b);
1380 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1385 GossipContact *contact;
1387 gtk_tree_model_get (model, iter,
1388 COL_CONTACT, &contact,
1395 if (gossip_contact_equal (contact, fc->contact)) {
1397 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1399 g_object_unref (contact);
1405 contact_list_store_find_contact (GossipContactListStore *store,
1406 GossipContact *contact)
1408 GossipContactListStorePriv *priv;
1409 GtkTreeModel *model;
1413 priv = GET_PRIV (store);
1415 memset (&fc, 0, sizeof (fc));
1417 fc.contact = contact;
1419 model = GTK_TREE_MODEL (store);
1420 gtk_tree_model_foreach (model,
1421 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1432 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1435 GossipContactListStore *store)
1437 GossipContactListStorePriv *priv;
1438 gboolean show_avatar = FALSE;
1440 priv = GET_PRIV (store);
1442 if (priv->show_avatars && !priv->is_compact) {
1446 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1447 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1448 COL_STATUS_VISIBLE, !priv->is_compact,