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;
323 gboolean show_active;
325 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
327 store = g_object_new (GOSSIP_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_contact_added_cb),
338 g_signal_connect (priv->list,
340 G_CALLBACK (contact_list_store_contact_removed_cb),
343 /* Add contacts already created. Do not highlight them. */
344 show_active = priv->show_active;
345 priv->show_active = FALSE;
346 contacts = empathy_contact_list_get_members (priv->list);
347 for (l = contacts; l; l = l->next) {
348 GossipContact *contact;
352 contact_list_store_contact_added_cb (priv->list, contact, store);
354 g_object_unref (contact);
356 g_list_free (contacts);
357 priv->show_active = show_active;
363 gossip_contact_list_store_get_list_iface (GossipContactListStore *store)
365 GossipContactListStorePriv *priv;
367 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), FALSE);
369 priv = GET_PRIV (store);
375 gossip_contact_list_store_get_show_offline (GossipContactListStore *store)
377 GossipContactListStorePriv *priv;
379 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), FALSE);
381 priv = GET_PRIV (store);
383 return priv->show_offline;
387 gossip_contact_list_store_set_show_offline (GossipContactListStore *store,
388 gboolean show_offline)
390 GossipContactListStorePriv *priv;
392 gboolean show_active;
394 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
396 priv = GET_PRIV (store);
398 priv->show_offline = show_offline;
399 show_active = priv->show_active;
401 /* Disable temporarily. */
402 priv->show_active = FALSE;
404 contacts = empathy_contact_list_get_members (priv->list);
405 for (l = contacts; l; l = l->next) {
406 GossipContact *contact;
408 contact = GOSSIP_CONTACT (l->data);
410 contact_list_store_contact_update (store, contact);
412 g_object_unref (contact);
414 g_list_free (contacts);
416 /* Restore to original setting. */
417 priv->show_active = show_active;
421 gossip_contact_list_store_get_show_avatars (GossipContactListStore *store)
423 GossipContactListStorePriv *priv;
425 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), TRUE);
427 priv = GET_PRIV (store);
429 return priv->show_avatars;
433 gossip_contact_list_store_set_show_avatars (GossipContactListStore *store,
434 gboolean show_avatars)
436 GossipContactListStorePriv *priv;
439 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
441 priv = GET_PRIV (store);
443 priv->show_avatars = show_avatars;
445 model = GTK_TREE_MODEL (store);
447 gtk_tree_model_foreach (model,
448 (GtkTreeModelForeachFunc)
449 contact_list_store_update_list_mode_foreach,
454 gossip_contact_list_store_get_is_compact (GossipContactListStore *store)
456 GossipContactListStorePriv *priv;
458 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), TRUE);
460 priv = GET_PRIV (store);
462 return priv->is_compact;
466 gossip_contact_list_store_set_is_compact (GossipContactListStore *store,
469 GossipContactListStorePriv *priv;
472 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
474 priv = GET_PRIV (store);
476 priv->is_compact = is_compact;
478 model = GTK_TREE_MODEL (store);
480 gtk_tree_model_foreach (model,
481 (GtkTreeModelForeachFunc)
482 contact_list_store_update_list_mode_foreach,
486 GossipContactListStoreSort
487 gossip_contact_list_store_get_sort_criterium (GossipContactListStore *store)
489 GossipContactListStorePriv *priv;
491 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store), 0);
493 priv = GET_PRIV (store);
495 return priv->sort_criterium;
499 gossip_contact_list_store_set_sort_criterium (GossipContactListStore *store,
500 GossipContactListStoreSort sort_criterium)
502 GossipContactListStorePriv *priv;
504 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
506 priv = GET_PRIV (store);
508 priv->sort_criterium = sort_criterium;
510 switch (sort_criterium) {
511 case GOSSIP_CONTACT_LIST_STORE_SORT_STATE:
512 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
517 case GOSSIP_CONTACT_LIST_STORE_SORT_NAME:
518 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
526 gossip_contact_list_store_row_separator_func (GtkTreeModel *model,
530 gboolean is_separator = FALSE;
532 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
534 gtk_tree_model_get (model, iter,
535 COL_IS_SEPARATOR, &is_separator,
542 gossip_contact_list_store_get_parent_group (GtkTreeModel *model,
544 gboolean *path_is_group)
546 GtkTreeIter parent_iter, iter;
550 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
553 *path_is_group = FALSE;
556 if (!gtk_tree_model_get_iter (model, &iter, path)) {
560 gtk_tree_model_get (model, &iter,
561 COL_IS_GROUP, &is_group,
569 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
575 gtk_tree_model_get (model, &iter,
576 COL_IS_GROUP, &is_group,
586 *path_is_group = TRUE;
593 gossip_contact_list_store_search_equal_func (GtkTreeModel *model,
597 gpointer search_data)
599 gchar *name, *name_folded;
603 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
609 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
615 name_folded = g_utf8_casefold (name, -1);
616 key_folded = g_utf8_casefold (key, -1);
618 if (name_folded && key_folded &&
619 strstr (name_folded, key_folded)) {
626 g_free (name_folded);
633 gossip_contact_list_store_set_contact_groups_func (GossipContactListStore *store,
634 GossipContactGroupsFunc func,
637 GossipContactListStorePriv *priv;
640 g_return_if_fail (GOSSIP_IS_CONTACT_LIST_STORE (store));
642 priv = GET_PRIV (store);
645 priv->get_contact_groups = func;
646 priv->get_contact_groups_data = user_data;
648 priv->get_contact_groups = NULL;
649 priv->get_contact_groups_data = NULL;
652 /* If we set a custom function to get contacts groups we have to
653 * disconnect our default notify::groups signal and wait for the user
654 * to call himself gossip_contact_list_store_update_contact_groups ()
655 * when needed. If func is NULL we come back to default.
657 contacts = empathy_contact_list_get_members (priv->list);
658 for (l = contacts; l; l = l->next) {
659 GossipContact *contact;
664 g_signal_handlers_disconnect_by_func (contact,
665 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
668 g_signal_connect (contact, "notify::groups",
669 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
673 gossip_contact_list_store_update_contact_groups (store, contact);
675 g_object_unref (contact);
677 g_list_free (contacts);
681 gossip_contact_list_store_update_contact_groups (GossipContactListStore *store,
682 GossipContact *contact)
684 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' updating groups",
685 gossip_contact_get_name (contact));
687 /* We do this to make sure the groups are correct, if not, we
688 * would have to check the groups already set up for each
689 * contact and then see what has been updated.
691 contact_list_store_remove_contact (store, contact);
692 contact_list_store_add_contact (store, contact);
696 contact_list_store_setup (GossipContactListStore *store)
698 GossipContactListStorePriv *priv;
699 GType types[] = {G_TYPE_STRING, /* Status icon-name */
700 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
701 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
702 G_TYPE_STRING, /* Name */
703 G_TYPE_STRING, /* Status string */
704 G_TYPE_BOOLEAN, /* Show status */
705 GOSSIP_TYPE_CONTACT, /* Contact type */
706 G_TYPE_BOOLEAN, /* Is group */
707 G_TYPE_BOOLEAN, /* Is active */
708 G_TYPE_BOOLEAN, /* Is online */
709 G_TYPE_BOOLEAN}; /* Is separator */
711 priv = GET_PRIV (store);
713 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
716 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
718 contact_list_store_name_sort_func,
720 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
722 contact_list_store_state_sort_func,
725 priv->sort_criterium = GOSSIP_CONTACT_LIST_STORE_SORT_NAME;
726 gossip_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
730 contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
731 GossipContact *contact,
732 GossipContactListStore *store)
734 GossipContactListStorePriv *priv;
736 priv = GET_PRIV (store);
738 gossip_debug (DEBUG_DOMAIN,
739 "Contact:'%s' added",
740 gossip_contact_get_name (contact));
742 if (!priv->get_contact_groups) {
743 g_signal_connect (contact, "notify::groups",
744 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
747 g_signal_connect (contact, "notify::presence",
748 G_CALLBACK (contact_list_store_contact_updated_cb),
750 g_signal_connect (contact, "notify::name",
751 G_CALLBACK (contact_list_store_contact_updated_cb),
753 g_signal_connect (contact, "notify::avatar",
754 G_CALLBACK (contact_list_store_contact_updated_cb),
756 g_signal_connect (contact, "notify::type",
757 G_CALLBACK (contact_list_store_contact_updated_cb),
760 contact_list_store_add_contact (store, contact);
764 contact_list_store_add_contact (GossipContactListStore *store,
765 GossipContact *contact)
767 GossipContactListStorePriv *priv;
771 priv = GET_PRIV (store);
773 if (!priv->show_offline && !gossip_contact_is_online (contact)) {
777 /* If no groups just add it at the top level. */
778 if (priv->get_contact_groups) {
779 groups = priv->get_contact_groups (contact,
780 priv->get_contact_groups_data);
782 groups = gossip_contact_get_groups (contact);
786 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
787 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
788 COL_NAME, gossip_contact_get_name (contact),
789 COL_CONTACT, contact,
791 COL_IS_SEPARATOR, FALSE,
795 /* Else add to each group. */
796 for (l = groups; l; l = l->next) {
797 GtkTreeIter iter_group;
805 contact_list_store_get_group (store, name, &iter_group, NULL, NULL);
807 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
809 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
810 COL_NAME, gossip_contact_get_name (contact),
811 COL_CONTACT, contact,
813 COL_IS_SEPARATOR, FALSE,
817 contact_list_store_contact_update (store, contact);
821 contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
822 GossipContact *contact,
823 GossipContactListStore *store)
825 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
826 gossip_contact_get_name (contact));
828 /* Disconnect signals */
829 g_signal_handlers_disconnect_by_func (contact,
830 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
832 g_signal_handlers_disconnect_by_func (contact,
833 G_CALLBACK (contact_list_store_contact_updated_cb),
836 contact_list_store_remove_contact (store, contact);
840 contact_list_store_remove_contact (GossipContactListStore *store,
841 GossipContact *contact)
843 GossipContactListStorePriv *priv;
847 priv = GET_PRIV (store);
849 iters = contact_list_store_find_contact (store, contact);
855 model = GTK_TREE_MODEL (store);
857 for (l = iters; l; l = l->next) {
860 /* NOTE: it is only <= 2 here because we have
861 * separators after the group name, otherwise it
864 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
865 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
866 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
868 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
872 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
877 contact_list_store_contact_update (GossipContactListStore *store,
878 GossipContact *contact)
880 GossipContactListStorePriv *priv;
881 ShowActiveData *data;
885 gboolean should_be_in_list;
886 gboolean was_online = TRUE;
887 gboolean now_online = FALSE;
888 gboolean set_model = FALSE;
889 gboolean do_remove = FALSE;
890 gboolean do_set_active = FALSE;
891 gboolean do_set_refresh = FALSE;
892 GdkPixbuf *pixbuf_avatar;
894 priv = GET_PRIV (store);
896 model = GTK_TREE_MODEL (store);
898 iters = contact_list_store_find_contact (store, contact);
905 /* Get online state now. */
906 now_online = gossip_contact_is_online (contact);
908 if (priv->show_offline || now_online) {
909 should_be_in_list = TRUE;
911 should_be_in_list = FALSE;
914 if (!in_list && !should_be_in_list) {
916 gossip_debug (DEBUG_DOMAIN,
917 "Contact:'%s' in list:NO, should be:NO",
918 gossip_contact_get_name (contact));
920 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
924 else if (in_list && !should_be_in_list) {
925 gossip_debug (DEBUG_DOMAIN,
926 "Contact:'%s' in list:YES, should be:NO",
927 gossip_contact_get_name (contact));
929 if (priv->show_active) {
931 do_set_active = TRUE;
932 do_set_refresh = TRUE;
935 gossip_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
937 gossip_debug (DEBUG_DOMAIN, "Remove item (now)!");
938 contact_list_store_remove_contact (store, contact);
941 else if (!in_list && should_be_in_list) {
942 gossip_debug (DEBUG_DOMAIN,
943 "Contact:'%s' in list:NO, should be:YES",
944 gossip_contact_get_name (contact));
946 contact_list_store_add_contact (store, contact);
948 if (priv->show_active) {
949 do_set_active = TRUE;
951 gossip_debug (DEBUG_DOMAIN, "Set active (contact added)");
954 gossip_debug (DEBUG_DOMAIN,
955 "Contact:'%s' in list:YES, should be:YES",
956 gossip_contact_get_name (contact));
958 /* Get online state before. */
959 if (iters && g_list_length (iters) > 0) {
960 gtk_tree_model_get (model, iters->data,
961 COL_IS_ONLINE, &was_online,
965 /* Is this really an update or an online/offline. */
966 if (priv->show_active) {
967 if (was_online != now_online) {
968 do_set_active = TRUE;
969 do_set_refresh = TRUE;
971 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
972 was_online ? "online -> offline" :
973 "offline -> online");
975 /* Was TRUE for presence updates. */
976 /* do_set_active = FALSE; */
977 do_set_refresh = TRUE;
979 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated)");
986 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
987 for (l = iters; l && set_model; l = l->next) {
988 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
989 COL_ICON_STATUS, gossip_icon_name_for_contact (contact),
990 COL_PIXBUF_AVATAR, pixbuf_avatar,
991 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
992 COL_NAME, gossip_contact_get_name (contact),
993 COL_STATUS, gossip_contact_get_status (contact),
994 COL_STATUS_VISIBLE, !priv->is_compact,
996 COL_IS_ONLINE, now_online,
997 COL_IS_SEPARATOR, FALSE,
1001 if (pixbuf_avatar) {
1002 g_object_unref (pixbuf_avatar);
1005 if (priv->show_active && do_set_active) {
1006 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1008 if (do_set_active) {
1009 data = contact_list_store_contact_active_new (store, contact, do_remove);
1010 g_timeout_add (ACTIVE_USER_SHOW_TIME,
1011 (GSourceFunc) contact_list_store_contact_active_cb,
1016 /* FIXME: when someone goes online then offline quickly, the
1017 * first timeout sets the user to be inactive and the second
1018 * timeout removes the user from the contact list, really we
1019 * should remove the first timeout.
1021 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1022 g_list_free (iters);
1026 contact_list_store_contact_groups_updated_cb (GossipContact *contact,
1028 GossipContactListStore *store)
1030 gossip_contact_list_store_update_contact_groups (store, contact);
1034 contact_list_store_contact_updated_cb (GossipContact *contact,
1036 GossipContactListStore *store)
1038 gossip_debug (DEBUG_DOMAIN,
1039 "Contact:'%s' updated, checking roster is in sync...",
1040 gossip_contact_get_name (contact));
1042 contact_list_store_contact_update (store, contact);
1046 contact_list_store_contact_set_active (GossipContactListStore *store,
1047 GossipContact *contact,
1049 gboolean set_changed)
1051 GossipContactListStorePriv *priv;
1052 GtkTreeModel *model;
1055 priv = GET_PRIV (store);
1056 model = GTK_TREE_MODEL (store);
1058 iters = contact_list_store_find_contact (store, contact);
1059 for (l = iters; l; l = l->next) {
1062 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1063 COL_IS_ACTIVE, active,
1066 gossip_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1069 path = gtk_tree_model_get_path (model, l->data);
1070 gtk_tree_model_row_changed (model, path, l->data);
1071 gtk_tree_path_free (path);
1075 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1076 g_list_free (iters);
1080 static ShowActiveData *
1081 contact_list_store_contact_active_new (GossipContactListStore *store,
1082 GossipContact *contact,
1085 ShowActiveData *data;
1087 gossip_debug (DEBUG_DOMAIN,
1088 "Contact:'%s' now active, and %s be removed",
1089 gossip_contact_get_name (contact),
1090 remove ? "WILL" : "WILL NOT");
1092 data = g_slice_new0 (ShowActiveData);
1094 data->store = g_object_ref (store);
1095 data->contact = g_object_ref (contact);
1096 data->remove = remove;
1102 contact_list_store_contact_active_free (ShowActiveData *data)
1104 g_object_unref (data->contact);
1105 g_object_unref (data->store);
1107 g_slice_free (ShowActiveData, data);
1111 contact_list_store_contact_active_cb (ShowActiveData *data)
1113 GossipContactListStorePriv *priv;
1115 priv = GET_PRIV (data->store);
1118 !priv->show_offline &&
1119 !gossip_contact_is_online (data->contact)) {
1120 gossip_debug (DEBUG_DOMAIN,
1121 "Contact:'%s' active timeout, removing item",
1122 gossip_contact_get_name (data->contact));
1123 contact_list_store_remove_contact (data->store, data->contact);
1126 gossip_debug (DEBUG_DOMAIN,
1127 "Contact:'%s' no longer active",
1128 gossip_contact_get_name (data->contact));
1130 contact_list_store_contact_set_active (data->store,
1135 contact_list_store_contact_active_free (data);
1141 contact_list_store_get_group_foreach (GtkTreeModel *model,
1149 /* Groups are only at the top level. */
1150 if (gtk_tree_path_get_depth (path) != 1) {
1154 gtk_tree_model_get (model, iter,
1156 COL_IS_GROUP, &is_group,
1159 if (is_group && strcmp (str, fg->name) == 0) {
1170 contact_list_store_get_group (GossipContactListStore *store,
1172 GtkTreeIter *iter_group_to_set,
1173 GtkTreeIter *iter_separator_to_set,
1176 GossipContactListStorePriv *priv;
1177 GtkTreeModel *model;
1178 GtkTreeIter iter_group;
1179 GtkTreeIter iter_separator;
1182 priv = GET_PRIV (store);
1184 memset (&fg, 0, sizeof (fg));
1188 model = GTK_TREE_MODEL (store);
1189 gtk_tree_model_foreach (model,
1190 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1198 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1199 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1200 COL_ICON_STATUS, NULL,
1203 COL_IS_ACTIVE, FALSE,
1204 COL_IS_SEPARATOR, FALSE,
1207 if (iter_group_to_set) {
1208 *iter_group_to_set = iter_group;
1211 gtk_tree_store_append (GTK_TREE_STORE (store),
1214 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1215 COL_IS_SEPARATOR, TRUE,
1218 if (iter_separator_to_set) {
1219 *iter_separator_to_set = iter_separator;
1226 if (iter_group_to_set) {
1227 *iter_group_to_set = fg.iter;
1230 iter_separator = fg.iter;
1232 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1233 gboolean is_separator;
1235 gtk_tree_model_get (model, &iter_separator,
1236 COL_IS_SEPARATOR, &is_separator,
1239 if (is_separator && iter_separator_to_set) {
1240 *iter_separator_to_set = iter_separator;
1247 contact_list_store_state_sort_func (GtkTreeModel *model,
1248 GtkTreeIter *iter_a,
1249 GtkTreeIter *iter_b,
1253 gchar *name_a, *name_b;
1254 gboolean is_separator_a, is_separator_b;
1255 GossipContact *contact_a, *contact_b;
1256 GossipPresence *presence_a, *presence_b;
1257 McPresence state_a, state_b;
1259 gtk_tree_model_get (model, iter_a,
1261 COL_CONTACT, &contact_a,
1262 COL_IS_SEPARATOR, &is_separator_a,
1264 gtk_tree_model_get (model, iter_b,
1266 COL_CONTACT, &contact_b,
1267 COL_IS_SEPARATOR, &is_separator_b,
1270 /* Separator or group? */
1271 if (is_separator_a || is_separator_b) {
1272 if (is_separator_a) {
1274 } else if (is_separator_b) {
1277 } else if (!contact_a && contact_b) {
1279 } else if (contact_a && !contact_b) {
1281 } else if (!contact_a && !contact_b) {
1283 ret_val = g_utf8_collate (name_a, name_b);
1290 /* If we managed to get this far, we can start looking at
1293 presence_a = gossip_contact_get_presence (GOSSIP_CONTACT (contact_a));
1294 presence_b = gossip_contact_get_presence (GOSSIP_CONTACT (contact_b));
1296 if (!presence_a && presence_b) {
1298 } else if (presence_a && !presence_b) {
1300 } else if (!presence_a && !presence_b) {
1301 /* Both offline, sort by name */
1302 ret_val = g_utf8_collate (name_a, name_b);
1304 state_a = gossip_presence_get_state (presence_a);
1305 state_b = gossip_presence_get_state (presence_b);
1307 if (state_a < state_b) {
1309 } else if (state_a > state_b) {
1312 /* Fallback: compare by name */
1313 ret_val = g_utf8_collate (name_a, name_b);
1322 g_object_unref (contact_a);
1326 g_object_unref (contact_b);
1333 contact_list_store_name_sort_func (GtkTreeModel *model,
1334 GtkTreeIter *iter_a,
1335 GtkTreeIter *iter_b,
1338 gchar *name_a, *name_b;
1339 GossipContact *contact_a, *contact_b;
1340 gboolean is_separator_a, is_separator_b;
1343 gtk_tree_model_get (model, iter_a,
1345 COL_CONTACT, &contact_a,
1346 COL_IS_SEPARATOR, &is_separator_a,
1348 gtk_tree_model_get (model, iter_b,
1350 COL_CONTACT, &contact_b,
1351 COL_IS_SEPARATOR, &is_separator_b,
1354 /* If contact is NULL it means it's a group. */
1356 if (is_separator_a || is_separator_b) {
1357 if (is_separator_a) {
1359 } else if (is_separator_b) {
1362 } else if (!contact_a && contact_b) {
1364 } else if (contact_a && !contact_b) {
1367 ret_val = g_utf8_collate (name_a, name_b);
1374 g_object_unref (contact_a);
1378 g_object_unref (contact_b);
1385 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1390 GossipContact *contact;
1392 gtk_tree_model_get (model, iter,
1393 COL_CONTACT, &contact,
1400 if (gossip_contact_equal (contact, fc->contact)) {
1402 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1404 g_object_unref (contact);
1410 contact_list_store_find_contact (GossipContactListStore *store,
1411 GossipContact *contact)
1413 GossipContactListStorePriv *priv;
1414 GtkTreeModel *model;
1418 priv = GET_PRIV (store);
1420 memset (&fc, 0, sizeof (fc));
1422 fc.contact = contact;
1424 model = GTK_TREE_MODEL (store);
1425 gtk_tree_model_foreach (model,
1426 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1437 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1440 GossipContactListStore *store)
1442 GossipContactListStorePriv *priv;
1443 gboolean show_avatar = FALSE;
1445 priv = GET_PRIV (store);
1447 if (priv->show_avatars && !priv->is_compact) {
1451 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1452 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1453 COL_STATUS_VISIBLE, !priv->is_compact,