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"
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), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
53 struct _EmpathyContactListStorePriv {
54 EmpathyContactList *list;
55 gboolean show_offline;
56 gboolean show_avatars;
59 EmpathyContactListStoreSort sort_criterium;
62 EmpathyContactGroupsFunc get_contact_groups;
63 gpointer get_contact_groups_data;
73 EmpathyContact *contact;
79 EmpathyContactListStore *store;
80 EmpathyContact *contact;
84 static void empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass);
85 static void empathy_contact_list_store_init (EmpathyContactListStore *list);
86 static void contact_list_store_finalize (GObject *object);
87 static void contact_list_store_get_property (GObject *object,
91 static void contact_list_store_set_property (GObject *object,
95 static void contact_list_store_setup (EmpathyContactListStore *store);
96 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
97 static void contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
98 EmpathyContact *contact,
99 EmpathyContactListStore *store);
100 static void contact_list_store_add_contact (EmpathyContactListStore *store,
101 EmpathyContact *contact);
102 static void contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
103 EmpathyContact *contact,
104 EmpathyContactListStore *store);
105 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
106 EmpathyContact *contact);
107 static void contact_list_store_contact_update (EmpathyContactListStore *store,
108 EmpathyContact *contact);
109 static void contact_list_store_contact_groups_updated_cb (EmpathyContact *contact,
111 EmpathyContactListStore *store);
112 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
114 EmpathyContactListStore *store);
115 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
116 EmpathyContact *contact,
118 gboolean set_changed);
119 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
120 EmpathyContact *contact,
122 static void contact_list_store_contact_active_free (ShowActiveData *data);
123 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
124 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
128 static void contact_list_store_get_group (EmpathyContactListStore *store,
130 GtkTreeIter *iter_group_to_set,
131 GtkTreeIter *iter_separator_to_set,
133 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
137 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
141 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
145 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
146 EmpathyContact *contact);
147 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
150 EmpathyContactListStore *store);
161 empathy_contact_list_store_sort_get_type (void)
163 static GType etype = 0;
166 static const GEnumValue values[] = {
167 { EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
168 "EMPATHY_CONTACT_LIST_STORE_SORT_NAME",
170 { EMPATHY_CONTACT_LIST_STORE_SORT_STATE,
171 "EMPATHY_CONTACT_LIST_STORE_SORT_STATE",
176 etype = g_enum_register_static ("EmpathyContactListStoreSort", values);
182 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
185 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
187 GObjectClass *object_class = G_OBJECT_CLASS (klass);
189 object_class->finalize = contact_list_store_finalize;
190 object_class->get_property = contact_list_store_get_property;
191 object_class->set_property = contact_list_store_set_property;
193 g_object_class_install_property (object_class,
195 g_param_spec_boolean ("show-offline",
197 "Whether contact list should display "
201 g_object_class_install_property (object_class,
203 g_param_spec_boolean ("show-avatars",
205 "Whether contact list should display "
206 "avatars for contacts",
209 g_object_class_install_property (object_class,
211 g_param_spec_boolean ("is-compact",
213 "Whether the contact list is in compact mode or not",
217 g_object_class_install_property (object_class,
219 g_param_spec_enum ("sort-criterium",
221 "The sort criterium to use for sorting the contact list",
222 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
223 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
226 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
230 empathy_contact_list_store_init (EmpathyContactListStore *store)
232 EmpathyContactListStorePriv *priv;
234 priv = GET_PRIV (store);
236 priv->show_avatars = TRUE;
237 priv->inhibit_active = g_timeout_add (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
238 (GSourceFunc) contact_list_store_inibit_active_cb,
243 contact_list_store_finalize (GObject *object)
245 EmpathyContactListStorePriv *priv;
247 priv = GET_PRIV (object);
249 /* FIXME: disconnect all signals on the list and contacts */
252 g_object_unref (priv->list);
255 if (priv->inhibit_active) {
256 g_source_remove (priv->inhibit_active);
259 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
263 contact_list_store_get_property (GObject *object,
268 EmpathyContactListStorePriv *priv;
270 priv = GET_PRIV (object);
273 case PROP_SHOW_OFFLINE:
274 g_value_set_boolean (value, priv->show_offline);
276 case PROP_SHOW_AVATARS:
277 g_value_set_boolean (value, priv->show_avatars);
279 case PROP_IS_COMPACT:
280 g_value_set_boolean (value, priv->is_compact);
282 case PROP_SORT_CRITERIUM:
283 g_value_set_enum (value, priv->sort_criterium);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
292 contact_list_store_set_property (GObject *object,
297 EmpathyContactListStorePriv *priv;
299 priv = GET_PRIV (object);
302 case PROP_SHOW_OFFLINE:
303 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
304 g_value_get_boolean (value));
306 case PROP_SHOW_AVATARS:
307 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
308 g_value_get_boolean (value));
310 case PROP_IS_COMPACT:
311 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
312 g_value_get_boolean (value));
314 case PROP_SORT_CRITERIUM:
315 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
316 g_value_get_enum (value));
319 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
324 EmpathyContactListStore *
325 empathy_contact_list_store_new (EmpathyContactList *list_iface)
327 EmpathyContactListStore *store;
328 EmpathyContactListStorePriv *priv;
331 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
333 store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
334 priv = GET_PRIV (store);
336 contact_list_store_setup (store);
337 priv->list = g_object_ref (list_iface);
339 /* Signal connection. */
340 g_signal_connect (priv->list,
342 G_CALLBACK (contact_list_store_contact_added_cb),
344 g_signal_connect (priv->list,
346 G_CALLBACK (contact_list_store_contact_removed_cb),
349 /* Add contacts already created. */
350 contacts = empathy_contact_list_get_members (priv->list);
351 for (l = contacts; l; l = l->next) {
352 EmpathyContact *contact;
356 contact_list_store_contact_added_cb (priv->list, contact, store);
358 g_object_unref (contact);
360 g_list_free (contacts);
366 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
368 EmpathyContactListStorePriv *priv;
370 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
372 priv = GET_PRIV (store);
378 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
380 EmpathyContactListStorePriv *priv;
382 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
384 priv = GET_PRIV (store);
386 return priv->show_offline;
390 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
391 gboolean show_offline)
393 EmpathyContactListStorePriv *priv;
395 gboolean show_active;
397 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
399 priv = GET_PRIV (store);
401 priv->show_offline = show_offline;
402 show_active = priv->show_active;
404 /* Disable temporarily. */
405 priv->show_active = FALSE;
407 contacts = empathy_contact_list_get_members (priv->list);
408 for (l = contacts; l; l = l->next) {
409 EmpathyContact *contact;
411 contact = EMPATHY_CONTACT (l->data);
413 contact_list_store_contact_update (store, contact);
415 g_object_unref (contact);
417 g_list_free (contacts);
419 /* Restore to original setting. */
420 priv->show_active = show_active;
424 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
426 EmpathyContactListStorePriv *priv;
428 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
430 priv = GET_PRIV (store);
432 return priv->show_avatars;
436 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
437 gboolean show_avatars)
439 EmpathyContactListStorePriv *priv;
442 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
444 priv = GET_PRIV (store);
446 priv->show_avatars = show_avatars;
448 model = GTK_TREE_MODEL (store);
450 gtk_tree_model_foreach (model,
451 (GtkTreeModelForeachFunc)
452 contact_list_store_update_list_mode_foreach,
457 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
459 EmpathyContactListStorePriv *priv;
461 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
463 priv = GET_PRIV (store);
465 return priv->is_compact;
469 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
472 EmpathyContactListStorePriv *priv;
475 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
477 priv = GET_PRIV (store);
479 priv->is_compact = is_compact;
481 model = GTK_TREE_MODEL (store);
483 gtk_tree_model_foreach (model,
484 (GtkTreeModelForeachFunc)
485 contact_list_store_update_list_mode_foreach,
489 EmpathyContactListStoreSort
490 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
492 EmpathyContactListStorePriv *priv;
494 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
496 priv = GET_PRIV (store);
498 return priv->sort_criterium;
502 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
503 EmpathyContactListStoreSort sort_criterium)
505 EmpathyContactListStorePriv *priv;
507 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
509 priv = GET_PRIV (store);
511 priv->sort_criterium = sort_criterium;
513 switch (sort_criterium) {
514 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
515 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
520 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
521 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
529 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
533 gboolean is_separator = FALSE;
535 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
537 gtk_tree_model_get (model, iter,
538 COL_IS_SEPARATOR, &is_separator,
545 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
547 gboolean *path_is_group)
549 GtkTreeIter parent_iter, iter;
553 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
556 *path_is_group = FALSE;
559 if (!gtk_tree_model_get_iter (model, &iter, path)) {
563 gtk_tree_model_get (model, &iter,
564 COL_IS_GROUP, &is_group,
572 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
578 gtk_tree_model_get (model, &iter,
579 COL_IS_GROUP, &is_group,
589 *path_is_group = TRUE;
596 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
600 gpointer search_data)
602 gchar *name, *name_folded;
606 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
612 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
618 name_folded = g_utf8_casefold (name, -1);
619 key_folded = g_utf8_casefold (key, -1);
621 if (name_folded && key_folded &&
622 strstr (name_folded, key_folded)) {
629 g_free (name_folded);
636 empathy_contact_list_store_set_contact_groups_func (EmpathyContactListStore *store,
637 EmpathyContactGroupsFunc func,
640 EmpathyContactListStorePriv *priv;
643 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
645 priv = GET_PRIV (store);
648 priv->get_contact_groups = func;
649 priv->get_contact_groups_data = user_data;
651 priv->get_contact_groups = NULL;
652 priv->get_contact_groups_data = NULL;
655 /* If we set a custom function to get contacts groups we have to
656 * disconnect our default notify::groups signal and wait for the user
657 * to call himself empathy_contact_list_store_update_contact_groups ()
658 * when needed. If func is NULL we come back to default.
660 contacts = empathy_contact_list_get_members (priv->list);
661 for (l = contacts; l; l = l->next) {
662 EmpathyContact *contact;
667 g_signal_handlers_disconnect_by_func (contact,
668 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
671 g_signal_connect (contact, "notify::groups",
672 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
676 empathy_contact_list_store_update_contact_groups (store, contact);
678 g_object_unref (contact);
680 g_list_free (contacts);
684 empathy_contact_list_store_update_contact_groups (EmpathyContactListStore *store,
685 EmpathyContact *contact)
687 EmpathyContactListStorePriv *priv;
688 gboolean show_active;
690 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
691 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
693 priv = GET_PRIV (store);
695 empathy_debug (DEBUG_DOMAIN, "Contact:'%s' updating groups",
696 empathy_contact_get_name (contact));
698 /* We do this to make sure the groups are correct, if not, we
699 * would have to check the groups already set up for each
700 * contact and then see what has been updated.
702 show_active = priv->show_active;
703 priv->show_active = FALSE;
704 contact_list_store_remove_contact (store, contact);
705 contact_list_store_add_contact (store, contact);
706 priv->show_active = show_active;
710 contact_list_store_setup (EmpathyContactListStore *store)
712 EmpathyContactListStorePriv *priv;
713 GType types[] = {G_TYPE_STRING, /* Status icon-name */
714 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
715 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
716 G_TYPE_STRING, /* Name */
717 G_TYPE_STRING, /* Status string */
718 G_TYPE_BOOLEAN, /* Show status */
719 EMPATHY_TYPE_CONTACT, /* Contact type */
720 G_TYPE_BOOLEAN, /* Is group */
721 G_TYPE_BOOLEAN, /* Is active */
722 G_TYPE_BOOLEAN, /* Is online */
723 G_TYPE_BOOLEAN}; /* Is separator */
725 priv = GET_PRIV (store);
727 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
730 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
732 contact_list_store_name_sort_func,
734 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
736 contact_list_store_state_sort_func,
739 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
740 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
744 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
746 EmpathyContactListStorePriv *priv;
748 priv = GET_PRIV (store);
750 priv->show_active = TRUE;
751 priv->inhibit_active = 0;
757 contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
758 EmpathyContact *contact,
759 EmpathyContactListStore *store)
761 EmpathyContactListStorePriv *priv;
763 priv = GET_PRIV (store);
765 empathy_debug (DEBUG_DOMAIN,
766 "Contact:'%s' added",
767 empathy_contact_get_name (contact));
769 if (!priv->get_contact_groups) {
770 g_signal_connect (contact, "notify::groups",
771 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
774 g_signal_connect (contact, "notify::presence",
775 G_CALLBACK (contact_list_store_contact_updated_cb),
777 g_signal_connect (contact, "notify::name",
778 G_CALLBACK (contact_list_store_contact_updated_cb),
780 g_signal_connect (contact, "notify::avatar",
781 G_CALLBACK (contact_list_store_contact_updated_cb),
783 g_signal_connect (contact, "notify::type",
784 G_CALLBACK (contact_list_store_contact_updated_cb),
787 contact_list_store_add_contact (store, contact);
791 contact_list_store_add_contact (EmpathyContactListStore *store,
792 EmpathyContact *contact)
794 EmpathyContactListStorePriv *priv;
798 priv = GET_PRIV (store);
800 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
804 /* If no groups just add it at the top level. */
805 if (priv->get_contact_groups) {
806 groups = priv->get_contact_groups (contact,
807 priv->get_contact_groups_data);
809 groups = empathy_contact_get_groups (contact);
813 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
814 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
815 COL_NAME, empathy_contact_get_name (contact),
816 COL_CONTACT, contact,
818 COL_IS_SEPARATOR, FALSE,
822 /* Else add to each group. */
823 for (l = groups; l; l = l->next) {
824 GtkTreeIter iter_group;
832 contact_list_store_get_group (store, name, &iter_group, NULL, NULL);
834 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
836 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
837 COL_NAME, empathy_contact_get_name (contact),
838 COL_CONTACT, contact,
840 COL_IS_SEPARATOR, FALSE,
844 contact_list_store_contact_update (store, contact);
848 contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
849 EmpathyContact *contact,
850 EmpathyContactListStore *store)
852 empathy_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
853 empathy_contact_get_name (contact));
855 /* Disconnect signals */
856 g_signal_handlers_disconnect_by_func (contact,
857 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
859 g_signal_handlers_disconnect_by_func (contact,
860 G_CALLBACK (contact_list_store_contact_updated_cb),
863 contact_list_store_remove_contact (store, contact);
867 contact_list_store_remove_contact (EmpathyContactListStore *store,
868 EmpathyContact *contact)
870 EmpathyContactListStorePriv *priv;
874 priv = GET_PRIV (store);
876 iters = contact_list_store_find_contact (store, contact);
882 model = GTK_TREE_MODEL (store);
884 for (l = iters; l; l = l->next) {
887 /* NOTE: it is only <= 2 here because we have
888 * separators after the group name, otherwise it
891 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
892 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
893 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
895 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
899 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
904 contact_list_store_contact_update (EmpathyContactListStore *store,
905 EmpathyContact *contact)
907 EmpathyContactListStorePriv *priv;
908 ShowActiveData *data;
912 gboolean should_be_in_list;
913 gboolean was_online = TRUE;
914 gboolean now_online = FALSE;
915 gboolean set_model = FALSE;
916 gboolean do_remove = FALSE;
917 gboolean do_set_active = FALSE;
918 gboolean do_set_refresh = FALSE;
919 GdkPixbuf *pixbuf_avatar;
921 priv = GET_PRIV (store);
923 model = GTK_TREE_MODEL (store);
925 iters = contact_list_store_find_contact (store, contact);
932 /* Get online state now. */
933 now_online = empathy_contact_is_online (contact);
935 if (priv->show_offline || now_online) {
936 should_be_in_list = TRUE;
938 should_be_in_list = FALSE;
941 if (!in_list && !should_be_in_list) {
943 empathy_debug (DEBUG_DOMAIN,
944 "Contact:'%s' in list:NO, should be:NO",
945 empathy_contact_get_name (contact));
947 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
951 else if (in_list && !should_be_in_list) {
952 empathy_debug (DEBUG_DOMAIN,
953 "Contact:'%s' in list:YES, should be:NO",
954 empathy_contact_get_name (contact));
956 if (priv->show_active) {
958 do_set_active = TRUE;
959 do_set_refresh = TRUE;
962 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
964 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
965 contact_list_store_remove_contact (store, contact);
968 else if (!in_list && should_be_in_list) {
969 empathy_debug (DEBUG_DOMAIN,
970 "Contact:'%s' in list:NO, should be:YES",
971 empathy_contact_get_name (contact));
973 contact_list_store_add_contact (store, contact);
975 if (priv->show_active) {
976 do_set_active = TRUE;
978 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
981 empathy_debug (DEBUG_DOMAIN,
982 "Contact:'%s' in list:YES, should be:YES",
983 empathy_contact_get_name (contact));
985 /* Get online state before. */
986 if (iters && g_list_length (iters) > 0) {
987 gtk_tree_model_get (model, iters->data,
988 COL_IS_ONLINE, &was_online,
992 /* Is this really an update or an online/offline. */
993 if (priv->show_active) {
994 if (was_online != now_online) {
995 do_set_active = TRUE;
996 do_set_refresh = TRUE;
998 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
999 was_online ? "online -> offline" :
1000 "offline -> online");
1002 /* Was TRUE for presence updates. */
1003 /* do_set_active = FALSE; */
1004 do_set_refresh = TRUE;
1006 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
1013 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1014 for (l = iters; l && set_model; l = l->next) {
1015 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1016 COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1017 COL_PIXBUF_AVATAR, pixbuf_avatar,
1018 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
1019 COL_NAME, empathy_contact_get_name (contact),
1020 COL_STATUS, empathy_contact_get_status (contact),
1021 COL_STATUS_VISIBLE, !priv->is_compact,
1022 COL_IS_GROUP, FALSE,
1023 COL_IS_ONLINE, now_online,
1024 COL_IS_SEPARATOR, FALSE,
1028 if (pixbuf_avatar) {
1029 g_object_unref (pixbuf_avatar);
1032 if (priv->show_active && do_set_active) {
1033 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1035 if (do_set_active) {
1036 data = contact_list_store_contact_active_new (store, contact, do_remove);
1037 g_timeout_add (ACTIVE_USER_SHOW_TIME,
1038 (GSourceFunc) contact_list_store_contact_active_cb,
1043 /* FIXME: when someone goes online then offline quickly, the
1044 * first timeout sets the user to be inactive and the second
1045 * timeout removes the user from the contact list, really we
1046 * should remove the first timeout.
1048 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1049 g_list_free (iters);
1053 contact_list_store_contact_groups_updated_cb (EmpathyContact *contact,
1055 EmpathyContactListStore *store)
1057 empathy_contact_list_store_update_contact_groups (store, contact);
1061 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1063 EmpathyContactListStore *store)
1065 empathy_debug (DEBUG_DOMAIN,
1066 "Contact:'%s' updated, checking roster is in sync...",
1067 empathy_contact_get_name (contact));
1069 contact_list_store_contact_update (store, contact);
1073 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1074 EmpathyContact *contact,
1076 gboolean set_changed)
1078 EmpathyContactListStorePriv *priv;
1079 GtkTreeModel *model;
1082 priv = GET_PRIV (store);
1083 model = GTK_TREE_MODEL (store);
1085 iters = contact_list_store_find_contact (store, contact);
1086 for (l = iters; l; l = l->next) {
1089 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1090 COL_IS_ACTIVE, active,
1093 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1096 path = gtk_tree_model_get_path (model, l->data);
1097 gtk_tree_model_row_changed (model, path, l->data);
1098 gtk_tree_path_free (path);
1102 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1103 g_list_free (iters);
1107 static ShowActiveData *
1108 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1109 EmpathyContact *contact,
1112 ShowActiveData *data;
1114 empathy_debug (DEBUG_DOMAIN,
1115 "Contact:'%s' now active, and %s be removed",
1116 empathy_contact_get_name (contact),
1117 remove ? "WILL" : "WILL NOT");
1119 data = g_slice_new0 (ShowActiveData);
1121 data->store = g_object_ref (store);
1122 data->contact = g_object_ref (contact);
1123 data->remove = remove;
1129 contact_list_store_contact_active_free (ShowActiveData *data)
1131 g_object_unref (data->contact);
1132 g_object_unref (data->store);
1134 g_slice_free (ShowActiveData, data);
1138 contact_list_store_contact_active_cb (ShowActiveData *data)
1140 EmpathyContactListStorePriv *priv;
1142 priv = GET_PRIV (data->store);
1145 !priv->show_offline &&
1146 !empathy_contact_is_online (data->contact)) {
1147 empathy_debug (DEBUG_DOMAIN,
1148 "Contact:'%s' active timeout, removing item",
1149 empathy_contact_get_name (data->contact));
1150 contact_list_store_remove_contact (data->store, data->contact);
1153 empathy_debug (DEBUG_DOMAIN,
1154 "Contact:'%s' no longer active",
1155 empathy_contact_get_name (data->contact));
1157 contact_list_store_contact_set_active (data->store,
1162 contact_list_store_contact_active_free (data);
1168 contact_list_store_get_group_foreach (GtkTreeModel *model,
1176 /* Groups are only at the top level. */
1177 if (gtk_tree_path_get_depth (path) != 1) {
1181 gtk_tree_model_get (model, iter,
1183 COL_IS_GROUP, &is_group,
1186 if (is_group && strcmp (str, fg->name) == 0) {
1197 contact_list_store_get_group (EmpathyContactListStore *store,
1199 GtkTreeIter *iter_group_to_set,
1200 GtkTreeIter *iter_separator_to_set,
1203 EmpathyContactListStorePriv *priv;
1204 GtkTreeModel *model;
1205 GtkTreeIter iter_group;
1206 GtkTreeIter iter_separator;
1209 priv = GET_PRIV (store);
1211 memset (&fg, 0, sizeof (fg));
1215 model = GTK_TREE_MODEL (store);
1216 gtk_tree_model_foreach (model,
1217 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1225 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1226 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1227 COL_ICON_STATUS, NULL,
1230 COL_IS_ACTIVE, FALSE,
1231 COL_IS_SEPARATOR, FALSE,
1234 if (iter_group_to_set) {
1235 *iter_group_to_set = iter_group;
1238 gtk_tree_store_append (GTK_TREE_STORE (store),
1241 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1242 COL_IS_SEPARATOR, TRUE,
1245 if (iter_separator_to_set) {
1246 *iter_separator_to_set = iter_separator;
1253 if (iter_group_to_set) {
1254 *iter_group_to_set = fg.iter;
1257 iter_separator = fg.iter;
1259 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1260 gboolean is_separator;
1262 gtk_tree_model_get (model, &iter_separator,
1263 COL_IS_SEPARATOR, &is_separator,
1266 if (is_separator && iter_separator_to_set) {
1267 *iter_separator_to_set = iter_separator;
1274 contact_list_store_state_sort_func (GtkTreeModel *model,
1275 GtkTreeIter *iter_a,
1276 GtkTreeIter *iter_b,
1280 gchar *name_a, *name_b;
1281 gboolean is_separator_a, is_separator_b;
1282 EmpathyContact *contact_a, *contact_b;
1283 EmpathyPresence *presence_a, *presence_b;
1284 McPresence state_a, state_b;
1286 gtk_tree_model_get (model, iter_a,
1288 COL_CONTACT, &contact_a,
1289 COL_IS_SEPARATOR, &is_separator_a,
1291 gtk_tree_model_get (model, iter_b,
1293 COL_CONTACT, &contact_b,
1294 COL_IS_SEPARATOR, &is_separator_b,
1297 /* Separator or group? */
1298 if (is_separator_a || is_separator_b) {
1299 if (is_separator_a) {
1301 } else if (is_separator_b) {
1304 } else if (!contact_a && contact_b) {
1306 } else if (contact_a && !contact_b) {
1308 } else if (!contact_a && !contact_b) {
1310 ret_val = g_utf8_collate (name_a, name_b);
1317 /* If we managed to get this far, we can start looking at
1320 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1321 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1323 if (!presence_a && presence_b) {
1325 } else if (presence_a && !presence_b) {
1327 } else if (!presence_a && !presence_b) {
1328 /* Both offline, sort by name */
1329 ret_val = g_utf8_collate (name_a, name_b);
1331 state_a = empathy_presence_get_state (presence_a);
1332 state_b = empathy_presence_get_state (presence_b);
1334 if (state_a < state_b) {
1336 } else if (state_a > state_b) {
1339 /* Fallback: compare by name */
1340 ret_val = g_utf8_collate (name_a, name_b);
1349 g_object_unref (contact_a);
1353 g_object_unref (contact_b);
1360 contact_list_store_name_sort_func (GtkTreeModel *model,
1361 GtkTreeIter *iter_a,
1362 GtkTreeIter *iter_b,
1365 gchar *name_a, *name_b;
1366 EmpathyContact *contact_a, *contact_b;
1367 gboolean is_separator_a, is_separator_b;
1370 gtk_tree_model_get (model, iter_a,
1372 COL_CONTACT, &contact_a,
1373 COL_IS_SEPARATOR, &is_separator_a,
1375 gtk_tree_model_get (model, iter_b,
1377 COL_CONTACT, &contact_b,
1378 COL_IS_SEPARATOR, &is_separator_b,
1381 /* If contact is NULL it means it's a group. */
1383 if (is_separator_a || is_separator_b) {
1384 if (is_separator_a) {
1386 } else if (is_separator_b) {
1389 } else if (!contact_a && contact_b) {
1391 } else if (contact_a && !contact_b) {
1394 ret_val = g_utf8_collate (name_a, name_b);
1401 g_object_unref (contact_a);
1405 g_object_unref (contact_b);
1412 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1417 EmpathyContact *contact;
1419 gtk_tree_model_get (model, iter,
1420 COL_CONTACT, &contact,
1427 if (empathy_contact_equal (contact, fc->contact)) {
1429 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1431 g_object_unref (contact);
1437 contact_list_store_find_contact (EmpathyContactListStore *store,
1438 EmpathyContact *contact)
1440 EmpathyContactListStorePriv *priv;
1441 GtkTreeModel *model;
1445 priv = GET_PRIV (store);
1447 memset (&fc, 0, sizeof (fc));
1449 fc.contact = contact;
1451 model = GTK_TREE_MODEL (store);
1452 gtk_tree_model_foreach (model,
1453 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1464 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1467 EmpathyContactListStore *store)
1469 EmpathyContactListStorePriv *priv;
1470 gboolean show_avatar = FALSE;
1472 priv = GET_PRIV (store);
1474 if (priv->show_avatars && !priv->is_compact) {
1478 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1479 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1480 COL_STATUS_VISIBLE, !priv->is_compact,