1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <libempathy/empathy-debug.h>
35 #include "empathy-contact-list-store.h"
36 #include "empathy-contact-groups.h"
37 #include "empathy-ui-utils.h"
38 #include "empathy-gtk-enum-types.h"
40 #define DEBUG_DOMAIN "ContactListStore"
42 /* Active users are those which have recently changed state
43 * (e.g. online, offline or from normal to a busy state).
46 /* Time user is shown as active */
47 #define ACTIVE_USER_SHOW_TIME 7000
49 /* Time after connecting which we wait before active users are enabled */
50 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5000
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
54 struct _EmpathyContactListStorePriv {
55 EmpathyContactList *list;
56 gboolean show_offline;
57 gboolean show_avatars;
60 EmpathyContactListStoreSort sort_criterium;
63 EmpathyContactGroupsFunc get_contact_groups;
64 gpointer get_contact_groups_data;
74 EmpathyContact *contact;
80 EmpathyContactListStore *store;
81 EmpathyContact *contact;
85 static void empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass);
86 static void empathy_contact_list_store_init (EmpathyContactListStore *list);
87 static void contact_list_store_finalize (GObject *object);
88 static void contact_list_store_get_property (GObject *object,
92 static void contact_list_store_set_property (GObject *object,
96 static void contact_list_store_setup (EmpathyContactListStore *store);
97 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
98 static void contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
99 EmpathyContact *contact,
100 EmpathyContactListStore *store);
101 static void contact_list_store_add_contact (EmpathyContactListStore *store,
102 EmpathyContact *contact);
103 static void contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
104 EmpathyContact *contact,
105 EmpathyContactListStore *store);
106 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
107 EmpathyContact *contact);
108 static void contact_list_store_contact_update (EmpathyContactListStore *store,
109 EmpathyContact *contact);
110 static void contact_list_store_contact_groups_updated_cb (EmpathyContact *contact,
112 EmpathyContactListStore *store);
113 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
115 EmpathyContactListStore *store);
116 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
117 EmpathyContact *contact,
119 gboolean set_changed);
120 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
121 EmpathyContact *contact,
123 static void contact_list_store_contact_active_free (ShowActiveData *data);
124 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
125 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
129 static void contact_list_store_get_group (EmpathyContactListStore *store,
131 GtkTreeIter *iter_group_to_set,
132 GtkTreeIter *iter_separator_to_set,
134 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
138 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
142 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
146 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
147 EmpathyContact *contact);
148 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
151 EmpathyContactListStore *store);
161 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
164 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
166 GObjectClass *object_class = G_OBJECT_CLASS (klass);
168 object_class->finalize = contact_list_store_finalize;
169 object_class->get_property = contact_list_store_get_property;
170 object_class->set_property = contact_list_store_set_property;
172 g_object_class_install_property (object_class,
174 g_param_spec_boolean ("show-offline",
176 "Whether contact list should display "
180 g_object_class_install_property (object_class,
182 g_param_spec_boolean ("show-avatars",
184 "Whether contact list should display "
185 "avatars for contacts",
188 g_object_class_install_property (object_class,
190 g_param_spec_boolean ("is-compact",
192 "Whether the contact list is in compact mode or not",
196 g_object_class_install_property (object_class,
198 g_param_spec_enum ("sort-criterium",
200 "The sort criterium to use for sorting the contact list",
201 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
202 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
205 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
209 empathy_contact_list_store_init (EmpathyContactListStore *store)
211 EmpathyContactListStorePriv *priv;
213 priv = GET_PRIV (store);
215 priv->show_avatars = TRUE;
216 priv->inhibit_active = g_timeout_add (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
217 (GSourceFunc) contact_list_store_inibit_active_cb,
222 contact_list_store_finalize (GObject *object)
224 EmpathyContactListStorePriv *priv;
226 priv = GET_PRIV (object);
228 /* FIXME: disconnect all signals on the list and contacts */
231 g_object_unref (priv->list);
234 if (priv->inhibit_active) {
235 g_source_remove (priv->inhibit_active);
238 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
242 contact_list_store_get_property (GObject *object,
247 EmpathyContactListStorePriv *priv;
249 priv = GET_PRIV (object);
252 case PROP_SHOW_OFFLINE:
253 g_value_set_boolean (value, priv->show_offline);
255 case PROP_SHOW_AVATARS:
256 g_value_set_boolean (value, priv->show_avatars);
258 case PROP_IS_COMPACT:
259 g_value_set_boolean (value, priv->is_compact);
261 case PROP_SORT_CRITERIUM:
262 g_value_set_enum (value, priv->sort_criterium);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
271 contact_list_store_set_property (GObject *object,
276 EmpathyContactListStorePriv *priv;
278 priv = GET_PRIV (object);
281 case PROP_SHOW_OFFLINE:
282 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
283 g_value_get_boolean (value));
285 case PROP_SHOW_AVATARS:
286 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
287 g_value_get_boolean (value));
289 case PROP_IS_COMPACT:
290 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
291 g_value_get_boolean (value));
293 case PROP_SORT_CRITERIUM:
294 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
295 g_value_get_enum (value));
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
303 EmpathyContactListStore *
304 empathy_contact_list_store_new (EmpathyContactList *list_iface)
306 EmpathyContactListStore *store;
307 EmpathyContactListStorePriv *priv;
310 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
312 store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
313 priv = GET_PRIV (store);
315 contact_list_store_setup (store);
316 priv->list = g_object_ref (list_iface);
318 /* Signal connection. */
319 g_signal_connect (priv->list,
321 G_CALLBACK (contact_list_store_contact_added_cb),
323 g_signal_connect (priv->list,
325 G_CALLBACK (contact_list_store_contact_removed_cb),
328 /* Add contacts already created. */
329 contacts = empathy_contact_list_get_members (priv->list);
330 for (l = contacts; l; l = l->next) {
331 EmpathyContact *contact;
335 contact_list_store_contact_added_cb (priv->list, contact, store);
337 g_object_unref (contact);
339 g_list_free (contacts);
345 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
347 EmpathyContactListStorePriv *priv;
349 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
351 priv = GET_PRIV (store);
357 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
359 EmpathyContactListStorePriv *priv;
361 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
363 priv = GET_PRIV (store);
365 return priv->show_offline;
369 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
370 gboolean show_offline)
372 EmpathyContactListStorePriv *priv;
374 gboolean show_active;
376 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
378 priv = GET_PRIV (store);
380 priv->show_offline = show_offline;
381 show_active = priv->show_active;
383 /* Disable temporarily. */
384 priv->show_active = FALSE;
386 contacts = empathy_contact_list_get_members (priv->list);
387 for (l = contacts; l; l = l->next) {
388 EmpathyContact *contact;
390 contact = EMPATHY_CONTACT (l->data);
392 contact_list_store_contact_update (store, contact);
394 g_object_unref (contact);
396 g_list_free (contacts);
398 /* Restore to original setting. */
399 priv->show_active = show_active;
403 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
405 EmpathyContactListStorePriv *priv;
407 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
409 priv = GET_PRIV (store);
411 return priv->show_avatars;
415 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
416 gboolean show_avatars)
418 EmpathyContactListStorePriv *priv;
421 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
423 priv = GET_PRIV (store);
425 priv->show_avatars = show_avatars;
427 model = GTK_TREE_MODEL (store);
429 gtk_tree_model_foreach (model,
430 (GtkTreeModelForeachFunc)
431 contact_list_store_update_list_mode_foreach,
436 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
438 EmpathyContactListStorePriv *priv;
440 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
442 priv = GET_PRIV (store);
444 return priv->is_compact;
448 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
451 EmpathyContactListStorePriv *priv;
454 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
456 priv = GET_PRIV (store);
458 priv->is_compact = is_compact;
460 model = GTK_TREE_MODEL (store);
462 gtk_tree_model_foreach (model,
463 (GtkTreeModelForeachFunc)
464 contact_list_store_update_list_mode_foreach,
468 EmpathyContactListStoreSort
469 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
471 EmpathyContactListStorePriv *priv;
473 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
475 priv = GET_PRIV (store);
477 return priv->sort_criterium;
481 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
482 EmpathyContactListStoreSort sort_criterium)
484 EmpathyContactListStorePriv *priv;
486 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
488 priv = GET_PRIV (store);
490 priv->sort_criterium = sort_criterium;
492 switch (sort_criterium) {
493 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
494 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
499 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
500 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
508 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
512 gboolean is_separator = FALSE;
514 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
516 gtk_tree_model_get (model, iter,
517 COL_IS_SEPARATOR, &is_separator,
524 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
526 gboolean *path_is_group)
528 GtkTreeIter parent_iter, iter;
532 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
535 *path_is_group = FALSE;
538 if (!gtk_tree_model_get_iter (model, &iter, path)) {
542 gtk_tree_model_get (model, &iter,
543 COL_IS_GROUP, &is_group,
551 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
557 gtk_tree_model_get (model, &iter,
558 COL_IS_GROUP, &is_group,
568 *path_is_group = TRUE;
575 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
579 gpointer search_data)
581 gchar *name, *name_folded;
585 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
591 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
597 name_folded = g_utf8_casefold (name, -1);
598 key_folded = g_utf8_casefold (key, -1);
600 if (name_folded && key_folded &&
601 strstr (name_folded, key_folded)) {
608 g_free (name_folded);
615 empathy_contact_list_store_set_contact_groups_func (EmpathyContactListStore *store,
616 EmpathyContactGroupsFunc func,
619 EmpathyContactListStorePriv *priv;
622 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
624 priv = GET_PRIV (store);
627 priv->get_contact_groups = func;
628 priv->get_contact_groups_data = user_data;
630 priv->get_contact_groups = NULL;
631 priv->get_contact_groups_data = NULL;
634 /* If we set a custom function to get contacts groups we have to
635 * disconnect our default notify::groups signal and wait for the user
636 * to call himself empathy_contact_list_store_update_contact_groups ()
637 * when needed. If func is NULL we come back to default.
639 contacts = empathy_contact_list_get_members (priv->list);
640 for (l = contacts; l; l = l->next) {
641 EmpathyContact *contact;
646 g_signal_handlers_disconnect_by_func (contact,
647 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
650 g_signal_connect (contact, "notify::groups",
651 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
655 empathy_contact_list_store_update_contact_groups (store, contact);
657 g_object_unref (contact);
659 g_list_free (contacts);
663 empathy_contact_list_store_update_contact_groups (EmpathyContactListStore *store,
664 EmpathyContact *contact)
666 EmpathyContactListStorePriv *priv;
667 gboolean show_active;
669 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
670 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
672 priv = GET_PRIV (store);
674 empathy_debug (DEBUG_DOMAIN, "Contact:'%s' updating groups",
675 empathy_contact_get_name (contact));
677 /* We do this to make sure the groups are correct, if not, we
678 * would have to check the groups already set up for each
679 * contact and then see what has been updated.
681 show_active = priv->show_active;
682 priv->show_active = FALSE;
683 contact_list_store_remove_contact (store, contact);
684 contact_list_store_add_contact (store, contact);
685 priv->show_active = show_active;
689 contact_list_store_setup (EmpathyContactListStore *store)
691 EmpathyContactListStorePriv *priv;
692 GType types[] = {G_TYPE_STRING, /* Status icon-name */
693 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
694 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
695 G_TYPE_STRING, /* Name */
696 G_TYPE_STRING, /* Status string */
697 G_TYPE_BOOLEAN, /* Show status */
698 EMPATHY_TYPE_CONTACT, /* Contact type */
699 G_TYPE_BOOLEAN, /* Is group */
700 G_TYPE_BOOLEAN, /* Is active */
701 G_TYPE_BOOLEAN, /* Is online */
702 G_TYPE_BOOLEAN}; /* Is separator */
704 priv = GET_PRIV (store);
706 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
709 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
711 contact_list_store_name_sort_func,
713 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
715 contact_list_store_state_sort_func,
718 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
719 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
723 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
725 EmpathyContactListStorePriv *priv;
727 priv = GET_PRIV (store);
729 priv->show_active = TRUE;
730 priv->inhibit_active = 0;
736 contact_list_store_contact_added_cb (EmpathyContactList *list_iface,
737 EmpathyContact *contact,
738 EmpathyContactListStore *store)
740 EmpathyContactListStorePriv *priv;
742 priv = GET_PRIV (store);
744 empathy_debug (DEBUG_DOMAIN,
745 "Contact:'%s' added",
746 empathy_contact_get_name (contact));
748 if (!priv->get_contact_groups) {
749 g_signal_connect (contact, "notify::groups",
750 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
753 g_signal_connect (contact, "notify::presence",
754 G_CALLBACK (contact_list_store_contact_updated_cb),
756 g_signal_connect (contact, "notify::name",
757 G_CALLBACK (contact_list_store_contact_updated_cb),
759 g_signal_connect (contact, "notify::avatar",
760 G_CALLBACK (contact_list_store_contact_updated_cb),
762 g_signal_connect (contact, "notify::type",
763 G_CALLBACK (contact_list_store_contact_updated_cb),
766 contact_list_store_add_contact (store, contact);
770 contact_list_store_add_contact (EmpathyContactListStore *store,
771 EmpathyContact *contact)
773 EmpathyContactListStorePriv *priv;
777 priv = GET_PRIV (store);
779 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
783 /* If no groups just add it at the top level. */
784 if (priv->get_contact_groups) {
785 groups = priv->get_contact_groups (contact,
786 priv->get_contact_groups_data);
788 groups = empathy_contact_get_groups (contact);
792 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
793 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
794 COL_NAME, empathy_contact_get_name (contact),
795 COL_CONTACT, contact,
797 COL_IS_SEPARATOR, FALSE,
801 /* Else add to each group. */
802 for (l = groups; l; l = l->next) {
803 GtkTreeIter iter_group;
811 contact_list_store_get_group (store, name, &iter_group, NULL, NULL);
813 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
815 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
816 COL_NAME, empathy_contact_get_name (contact),
817 COL_CONTACT, contact,
819 COL_IS_SEPARATOR, FALSE,
823 contact_list_store_contact_update (store, contact);
827 contact_list_store_contact_removed_cb (EmpathyContactList *list_iface,
828 EmpathyContact *contact,
829 EmpathyContactListStore *store)
831 empathy_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
832 empathy_contact_get_name (contact));
834 /* Disconnect signals */
835 g_signal_handlers_disconnect_by_func (contact,
836 G_CALLBACK (contact_list_store_contact_groups_updated_cb),
838 g_signal_handlers_disconnect_by_func (contact,
839 G_CALLBACK (contact_list_store_contact_updated_cb),
842 contact_list_store_remove_contact (store, contact);
846 contact_list_store_remove_contact (EmpathyContactListStore *store,
847 EmpathyContact *contact)
849 EmpathyContactListStorePriv *priv;
853 priv = GET_PRIV (store);
855 iters = contact_list_store_find_contact (store, contact);
861 model = GTK_TREE_MODEL (store);
863 for (l = iters; l; l = l->next) {
866 /* NOTE: it is only <= 2 here because we have
867 * separators after the group name, otherwise it
870 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
871 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
872 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
874 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
878 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
883 contact_list_store_contact_update (EmpathyContactListStore *store,
884 EmpathyContact *contact)
886 EmpathyContactListStorePriv *priv;
887 ShowActiveData *data;
891 gboolean should_be_in_list;
892 gboolean was_online = TRUE;
893 gboolean now_online = FALSE;
894 gboolean set_model = FALSE;
895 gboolean do_remove = FALSE;
896 gboolean do_set_active = FALSE;
897 gboolean do_set_refresh = FALSE;
898 GdkPixbuf *pixbuf_avatar;
900 priv = GET_PRIV (store);
902 model = GTK_TREE_MODEL (store);
904 iters = contact_list_store_find_contact (store, contact);
911 /* Get online state now. */
912 now_online = empathy_contact_is_online (contact);
914 if (priv->show_offline || now_online) {
915 should_be_in_list = TRUE;
917 should_be_in_list = FALSE;
920 if (!in_list && !should_be_in_list) {
922 empathy_debug (DEBUG_DOMAIN,
923 "Contact:'%s' in list:NO, should be:NO",
924 empathy_contact_get_name (contact));
926 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
930 else if (in_list && !should_be_in_list) {
931 empathy_debug (DEBUG_DOMAIN,
932 "Contact:'%s' in list:YES, should be:NO",
933 empathy_contact_get_name (contact));
935 if (priv->show_active) {
937 do_set_active = TRUE;
938 do_set_refresh = TRUE;
941 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
943 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
944 contact_list_store_remove_contact (store, contact);
947 else if (!in_list && should_be_in_list) {
948 empathy_debug (DEBUG_DOMAIN,
949 "Contact:'%s' in list:NO, should be:YES",
950 empathy_contact_get_name (contact));
952 contact_list_store_add_contact (store, contact);
954 if (priv->show_active) {
955 do_set_active = TRUE;
957 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
960 empathy_debug (DEBUG_DOMAIN,
961 "Contact:'%s' in list:YES, should be:YES",
962 empathy_contact_get_name (contact));
964 /* Get online state before. */
965 if (iters && g_list_length (iters) > 0) {
966 gtk_tree_model_get (model, iters->data,
967 COL_IS_ONLINE, &was_online,
971 /* Is this really an update or an online/offline. */
972 if (priv->show_active) {
973 if (was_online != now_online) {
974 do_set_active = TRUE;
975 do_set_refresh = TRUE;
977 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
978 was_online ? "online -> offline" :
979 "offline -> online");
981 /* Was TRUE for presence updates. */
982 /* do_set_active = FALSE; */
983 do_set_refresh = TRUE;
985 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
992 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
993 for (l = iters; l && set_model; l = l->next) {
994 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
995 COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
996 COL_PIXBUF_AVATAR, pixbuf_avatar,
997 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
998 COL_NAME, empathy_contact_get_name (contact),
999 COL_STATUS, empathy_contact_get_status (contact),
1000 COL_STATUS_VISIBLE, !priv->is_compact,
1001 COL_IS_GROUP, FALSE,
1002 COL_IS_ONLINE, now_online,
1003 COL_IS_SEPARATOR, FALSE,
1007 if (pixbuf_avatar) {
1008 g_object_unref (pixbuf_avatar);
1011 if (priv->show_active && do_set_active) {
1012 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1014 if (do_set_active) {
1015 data = contact_list_store_contact_active_new (store, contact, do_remove);
1016 g_timeout_add (ACTIVE_USER_SHOW_TIME,
1017 (GSourceFunc) contact_list_store_contact_active_cb,
1022 /* FIXME: when someone goes online then offline quickly, the
1023 * first timeout sets the user to be inactive and the second
1024 * timeout removes the user from the contact list, really we
1025 * should remove the first timeout.
1027 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1028 g_list_free (iters);
1032 contact_list_store_contact_groups_updated_cb (EmpathyContact *contact,
1034 EmpathyContactListStore *store)
1036 empathy_contact_list_store_update_contact_groups (store, contact);
1040 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1042 EmpathyContactListStore *store)
1044 empathy_debug (DEBUG_DOMAIN,
1045 "Contact:'%s' updated, checking roster is in sync...",
1046 empathy_contact_get_name (contact));
1048 contact_list_store_contact_update (store, contact);
1052 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1053 EmpathyContact *contact,
1055 gboolean set_changed)
1057 EmpathyContactListStorePriv *priv;
1058 GtkTreeModel *model;
1061 priv = GET_PRIV (store);
1062 model = GTK_TREE_MODEL (store);
1064 iters = contact_list_store_find_contact (store, contact);
1065 for (l = iters; l; l = l->next) {
1068 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1069 COL_IS_ACTIVE, active,
1072 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1075 path = gtk_tree_model_get_path (model, l->data);
1076 gtk_tree_model_row_changed (model, path, l->data);
1077 gtk_tree_path_free (path);
1081 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1082 g_list_free (iters);
1086 static ShowActiveData *
1087 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1088 EmpathyContact *contact,
1091 ShowActiveData *data;
1093 empathy_debug (DEBUG_DOMAIN,
1094 "Contact:'%s' now active, and %s be removed",
1095 empathy_contact_get_name (contact),
1096 remove ? "WILL" : "WILL NOT");
1098 data = g_slice_new0 (ShowActiveData);
1100 data->store = g_object_ref (store);
1101 data->contact = g_object_ref (contact);
1102 data->remove = remove;
1108 contact_list_store_contact_active_free (ShowActiveData *data)
1110 g_object_unref (data->contact);
1111 g_object_unref (data->store);
1113 g_slice_free (ShowActiveData, data);
1117 contact_list_store_contact_active_cb (ShowActiveData *data)
1119 EmpathyContactListStorePriv *priv;
1121 priv = GET_PRIV (data->store);
1124 !priv->show_offline &&
1125 !empathy_contact_is_online (data->contact)) {
1126 empathy_debug (DEBUG_DOMAIN,
1127 "Contact:'%s' active timeout, removing item",
1128 empathy_contact_get_name (data->contact));
1129 contact_list_store_remove_contact (data->store, data->contact);
1132 empathy_debug (DEBUG_DOMAIN,
1133 "Contact:'%s' no longer active",
1134 empathy_contact_get_name (data->contact));
1136 contact_list_store_contact_set_active (data->store,
1141 contact_list_store_contact_active_free (data);
1147 contact_list_store_get_group_foreach (GtkTreeModel *model,
1155 /* Groups are only at the top level. */
1156 if (gtk_tree_path_get_depth (path) != 1) {
1160 gtk_tree_model_get (model, iter,
1162 COL_IS_GROUP, &is_group,
1165 if (is_group && strcmp (str, fg->name) == 0) {
1176 contact_list_store_get_group (EmpathyContactListStore *store,
1178 GtkTreeIter *iter_group_to_set,
1179 GtkTreeIter *iter_separator_to_set,
1182 EmpathyContactListStorePriv *priv;
1183 GtkTreeModel *model;
1184 GtkTreeIter iter_group;
1185 GtkTreeIter iter_separator;
1188 priv = GET_PRIV (store);
1190 memset (&fg, 0, sizeof (fg));
1194 model = GTK_TREE_MODEL (store);
1195 gtk_tree_model_foreach (model,
1196 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1204 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1205 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1206 COL_ICON_STATUS, NULL,
1209 COL_IS_ACTIVE, FALSE,
1210 COL_IS_SEPARATOR, FALSE,
1213 if (iter_group_to_set) {
1214 *iter_group_to_set = iter_group;
1217 gtk_tree_store_append (GTK_TREE_STORE (store),
1220 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1221 COL_IS_SEPARATOR, TRUE,
1224 if (iter_separator_to_set) {
1225 *iter_separator_to_set = iter_separator;
1232 if (iter_group_to_set) {
1233 *iter_group_to_set = fg.iter;
1236 iter_separator = fg.iter;
1238 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1239 gboolean is_separator;
1241 gtk_tree_model_get (model, &iter_separator,
1242 COL_IS_SEPARATOR, &is_separator,
1245 if (is_separator && iter_separator_to_set) {
1246 *iter_separator_to_set = iter_separator;
1253 contact_list_store_state_sort_func (GtkTreeModel *model,
1254 GtkTreeIter *iter_a,
1255 GtkTreeIter *iter_b,
1259 gchar *name_a, *name_b;
1260 gboolean is_separator_a, is_separator_b;
1261 EmpathyContact *contact_a, *contact_b;
1262 EmpathyPresence *presence_a, *presence_b;
1263 McPresence state_a, state_b;
1265 gtk_tree_model_get (model, iter_a,
1267 COL_CONTACT, &contact_a,
1268 COL_IS_SEPARATOR, &is_separator_a,
1270 gtk_tree_model_get (model, iter_b,
1272 COL_CONTACT, &contact_b,
1273 COL_IS_SEPARATOR, &is_separator_b,
1276 /* Separator or group? */
1277 if (is_separator_a || is_separator_b) {
1278 if (is_separator_a) {
1280 } else if (is_separator_b) {
1283 } else if (!contact_a && contact_b) {
1285 } else if (contact_a && !contact_b) {
1287 } else if (!contact_a && !contact_b) {
1289 ret_val = g_utf8_collate (name_a, name_b);
1296 /* If we managed to get this far, we can start looking at
1299 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1300 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1302 if (!presence_a && presence_b) {
1304 } else if (presence_a && !presence_b) {
1306 } else if (!presence_a && !presence_b) {
1307 /* Both offline, sort by name */
1308 ret_val = g_utf8_collate (name_a, name_b);
1310 state_a = empathy_presence_get_state (presence_a);
1311 state_b = empathy_presence_get_state (presence_b);
1313 if (state_a < state_b) {
1315 } else if (state_a > state_b) {
1318 /* Fallback: compare by name */
1319 ret_val = g_utf8_collate (name_a, name_b);
1328 g_object_unref (contact_a);
1332 g_object_unref (contact_b);
1339 contact_list_store_name_sort_func (GtkTreeModel *model,
1340 GtkTreeIter *iter_a,
1341 GtkTreeIter *iter_b,
1344 gchar *name_a, *name_b;
1345 EmpathyContact *contact_a, *contact_b;
1346 gboolean is_separator_a, is_separator_b;
1349 gtk_tree_model_get (model, iter_a,
1351 COL_CONTACT, &contact_a,
1352 COL_IS_SEPARATOR, &is_separator_a,
1354 gtk_tree_model_get (model, iter_b,
1356 COL_CONTACT, &contact_b,
1357 COL_IS_SEPARATOR, &is_separator_b,
1360 /* If contact is NULL it means it's a group. */
1362 if (is_separator_a || is_separator_b) {
1363 if (is_separator_a) {
1365 } else if (is_separator_b) {
1368 } else if (!contact_a && contact_b) {
1370 } else if (contact_a && !contact_b) {
1373 ret_val = g_utf8_collate (name_a, name_b);
1380 g_object_unref (contact_a);
1384 g_object_unref (contact_b);
1391 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1396 EmpathyContact *contact;
1398 gtk_tree_model_get (model, iter,
1399 COL_CONTACT, &contact,
1406 if (empathy_contact_equal (contact, fc->contact)) {
1408 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1410 g_object_unref (contact);
1416 contact_list_store_find_contact (EmpathyContactListStore *store,
1417 EmpathyContact *contact)
1419 EmpathyContactListStorePriv *priv;
1420 GtkTreeModel *model;
1424 priv = GET_PRIV (store);
1426 memset (&fc, 0, sizeof (fc));
1428 fc.contact = contact;
1430 model = GTK_TREE_MODEL (store);
1431 gtk_tree_model_foreach (model,
1432 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1443 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1446 EmpathyContactListStore *store)
1448 EmpathyContactListStorePriv *priv;
1449 gboolean show_avatar = FALSE;
1451 priv = GET_PRIV (store);
1453 if (priv->show_avatars && !priv->is_compact) {
1457 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1458 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1459 COL_STATUS_VISIBLE, !priv->is_compact,