1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <libempathy/empathy-debug.h>
35 #include "empathy-contact-list-store.h"
36 #include "empathy-contact-groups.h"
37 #include "empathy-ui-utils.h"
38 #include "empathy-gtk-enum-types.h"
40 #define DEBUG_DOMAIN "ContactListStore"
42 /* Active users are those which have recently changed state
43 * (e.g. online, offline or from normal to a busy state).
46 /* Time in seconds user is shown as active */
47 #define ACTIVE_USER_SHOW_TIME 7
49 /* Time in seconds after connecting which we wait before active users are enabled */
50 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
55 EmpathyContactList *list;
56 gboolean show_offline;
57 gboolean show_avatars;
60 EmpathyContactListStoreSort sort_criterium;
62 } EmpathyContactListStorePriv;
71 EmpathyContact *contact;
77 EmpathyContactListStore *store;
78 EmpathyContact *contact;
82 static void empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass);
83 static void empathy_contact_list_store_init (EmpathyContactListStore *list);
84 static void contact_list_store_finalize (GObject *object);
85 static void contact_list_store_get_property (GObject *object,
89 static void contact_list_store_set_property (GObject *object,
93 static gboolean contact_list_store_finalize_foreach (GtkTreeModel *model,
97 static void contact_list_store_setup (EmpathyContactListStore *store);
98 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
99 static void contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
100 EmpathyContact *contact,
101 EmpathyContact *actor,
105 EmpathyContactListStore *store);
106 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
107 EmpathyContact *contact,
110 EmpathyContactListStore *store);
111 static void contact_list_store_add_contact (EmpathyContactListStore *store,
112 EmpathyContact *contact);
113 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
114 EmpathyContact *contact);
115 static void contact_list_store_contact_update (EmpathyContactListStore *store,
116 EmpathyContact *contact);
117 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
119 EmpathyContactListStore *store);
120 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
121 EmpathyContact *contact,
123 gboolean set_changed);
124 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
125 EmpathyContact *contact,
127 static void contact_list_store_contact_active_free (ShowActiveData *data);
128 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
129 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
133 static void contact_list_store_get_group (EmpathyContactListStore *store,
135 GtkTreeIter *iter_group_to_set,
136 GtkTreeIter *iter_separator_to_set,
138 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
142 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
146 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
150 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
151 EmpathyContact *contact);
152 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
155 EmpathyContactListStore *store);
165 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
168 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
170 GObjectClass *object_class = G_OBJECT_CLASS (klass);
172 object_class->finalize = contact_list_store_finalize;
173 object_class->get_property = contact_list_store_get_property;
174 object_class->set_property = contact_list_store_set_property;
176 g_object_class_install_property (object_class,
178 g_param_spec_boolean ("show-offline",
180 "Whether contact list should display "
184 g_object_class_install_property (object_class,
186 g_param_spec_boolean ("show-avatars",
188 "Whether contact list should display "
189 "avatars for contacts",
192 g_object_class_install_property (object_class,
194 g_param_spec_boolean ("is-compact",
196 "Whether the contact list is in compact mode or not",
200 g_object_class_install_property (object_class,
202 g_param_spec_enum ("sort-criterium",
204 "The sort criterium to use for sorting the contact list",
205 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
206 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
209 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
213 empathy_contact_list_store_init (EmpathyContactListStore *store)
215 EmpathyContactListStorePriv *priv;
217 priv = GET_PRIV (store);
219 priv->show_avatars = TRUE;
220 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
221 (GSourceFunc) contact_list_store_inibit_active_cb,
226 contact_list_store_finalize (GObject *object)
228 EmpathyContactListStorePriv *priv;
230 priv = GET_PRIV (object);
232 gtk_tree_model_foreach (GTK_TREE_MODEL (object),
233 (GtkTreeModelForeachFunc) contact_list_store_finalize_foreach,
237 g_signal_handlers_disconnect_by_func (priv->list,
238 G_CALLBACK (contact_list_store_members_changed_cb),
240 g_signal_handlers_disconnect_by_func (priv->list,
241 G_CALLBACK (contact_list_store_groups_changed_cb),
243 g_object_unref (priv->list);
246 if (priv->inhibit_active) {
247 g_source_remove (priv->inhibit_active);
250 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
254 contact_list_store_get_property (GObject *object,
259 EmpathyContactListStorePriv *priv;
261 priv = GET_PRIV (object);
264 case PROP_SHOW_OFFLINE:
265 g_value_set_boolean (value, priv->show_offline);
267 case PROP_SHOW_AVATARS:
268 g_value_set_boolean (value, priv->show_avatars);
270 case PROP_IS_COMPACT:
271 g_value_set_boolean (value, priv->is_compact);
273 case PROP_SORT_CRITERIUM:
274 g_value_set_enum (value, priv->sort_criterium);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
283 contact_list_store_set_property (GObject *object,
288 EmpathyContactListStorePriv *priv;
290 priv = GET_PRIV (object);
293 case PROP_SHOW_OFFLINE:
294 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
295 g_value_get_boolean (value));
297 case PROP_SHOW_AVATARS:
298 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
299 g_value_get_boolean (value));
301 case PROP_IS_COMPACT:
302 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
303 g_value_get_boolean (value));
305 case PROP_SORT_CRITERIUM:
306 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
307 g_value_get_enum (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
315 EmpathyContactListStore *
316 empathy_contact_list_store_new (EmpathyContactList *list_iface)
318 EmpathyContactListStore *store;
319 EmpathyContactListStorePriv *priv;
322 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
324 store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
325 priv = GET_PRIV (store);
327 contact_list_store_setup (store);
328 priv->list = g_object_ref (list_iface);
330 /* Signal connection. */
331 g_signal_connect (priv->list,
333 G_CALLBACK (contact_list_store_members_changed_cb),
335 g_signal_connect (priv->list,
337 G_CALLBACK (contact_list_store_groups_changed_cb),
340 /* Add contacts already created. */
341 contacts = empathy_contact_list_get_members (priv->list);
342 for (l = contacts; l; l = l->next) {
343 contact_list_store_members_changed_cb (priv->list, l->data,
348 g_object_unref (l->data);
350 g_list_free (contacts);
356 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
358 EmpathyContactListStorePriv *priv;
360 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
362 priv = GET_PRIV (store);
368 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
370 EmpathyContactListStorePriv *priv;
372 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
374 priv = GET_PRIV (store);
376 return priv->show_offline;
380 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
381 gboolean show_offline)
383 EmpathyContactListStorePriv *priv;
385 gboolean show_active;
387 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
389 priv = GET_PRIV (store);
391 priv->show_offline = show_offline;
392 show_active = priv->show_active;
394 /* Disable temporarily. */
395 priv->show_active = FALSE;
397 contacts = empathy_contact_list_get_members (priv->list);
398 for (l = contacts; l; l = l->next) {
399 contact_list_store_contact_update (store, l->data);
401 g_object_unref (l->data);
403 g_list_free (contacts);
405 /* Restore to original setting. */
406 priv->show_active = show_active;
410 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
412 EmpathyContactListStorePriv *priv;
414 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
416 priv = GET_PRIV (store);
418 return priv->show_avatars;
422 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
423 gboolean show_avatars)
425 EmpathyContactListStorePriv *priv;
428 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
430 priv = GET_PRIV (store);
432 priv->show_avatars = show_avatars;
434 model = GTK_TREE_MODEL (store);
436 gtk_tree_model_foreach (model,
437 (GtkTreeModelForeachFunc)
438 contact_list_store_update_list_mode_foreach,
443 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
445 EmpathyContactListStorePriv *priv;
447 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
449 priv = GET_PRIV (store);
451 return priv->is_compact;
455 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
458 EmpathyContactListStorePriv *priv;
461 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
463 priv = GET_PRIV (store);
465 priv->is_compact = is_compact;
467 model = GTK_TREE_MODEL (store);
469 gtk_tree_model_foreach (model,
470 (GtkTreeModelForeachFunc)
471 contact_list_store_update_list_mode_foreach,
475 EmpathyContactListStoreSort
476 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
478 EmpathyContactListStorePriv *priv;
480 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
482 priv = GET_PRIV (store);
484 return priv->sort_criterium;
488 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
489 EmpathyContactListStoreSort sort_criterium)
491 EmpathyContactListStorePriv *priv;
493 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
495 priv = GET_PRIV (store);
497 priv->sort_criterium = sort_criterium;
499 switch (sort_criterium) {
500 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
501 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
502 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
506 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
507 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
508 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
515 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
519 gboolean is_separator = FALSE;
521 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
523 gtk_tree_model_get (model, iter,
524 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
531 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
533 gboolean *path_is_group)
535 GtkTreeIter parent_iter, iter;
539 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
542 *path_is_group = FALSE;
545 if (!gtk_tree_model_get_iter (model, &iter, path)) {
549 gtk_tree_model_get (model, &iter,
550 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
551 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
558 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
564 gtk_tree_model_get (model, &iter,
565 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
566 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
575 *path_is_group = TRUE;
582 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
586 gpointer search_data)
588 gchar *name, *name_folded;
592 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
598 gtk_tree_model_get (model, iter,
599 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
606 name_folded = g_utf8_casefold (name, -1);
607 key_folded = g_utf8_casefold (key, -1);
609 if (name_folded && key_folded &&
610 strstr (name_folded, key_folded)) {
617 g_free (name_folded);
624 contact_list_store_finalize_foreach (GtkTreeModel *model,
629 EmpathyContactListStore *store = user_data;
630 EmpathyContact *contact = NULL;
632 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
633 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
637 g_signal_handlers_disconnect_by_func (contact,
638 G_CALLBACK (contact_list_store_contact_updated_cb),
646 contact_list_store_setup (EmpathyContactListStore *store)
648 EmpathyContactListStorePriv *priv;
649 GType types[] = {G_TYPE_STRING, /* Status icon-name */
650 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
651 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
652 G_TYPE_STRING, /* Name */
653 G_TYPE_STRING, /* Status string */
654 G_TYPE_BOOLEAN, /* Show status */
655 EMPATHY_TYPE_CONTACT, /* Contact type */
656 G_TYPE_BOOLEAN, /* Is group */
657 G_TYPE_BOOLEAN, /* Is active */
658 G_TYPE_BOOLEAN, /* Is online */
659 G_TYPE_BOOLEAN, /* Is separator */
660 G_TYPE_BOOLEAN}; /* Can VoIP */
662 priv = GET_PRIV (store);
664 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
665 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
669 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
670 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
671 contact_list_store_name_sort_func,
673 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
674 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
675 contact_list_store_state_sort_func,
678 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
679 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
683 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
685 EmpathyContactListStorePriv *priv;
687 priv = GET_PRIV (store);
689 priv->show_active = TRUE;
690 priv->inhibit_active = 0;
696 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
697 EmpathyContact *contact,
698 EmpathyContact *actor,
702 EmpathyContactListStore *store)
704 EmpathyContactListStorePriv *priv;
706 priv = GET_PRIV (store);
708 empathy_debug (DEBUG_DOMAIN,
709 "Contact %s (%d) %s",
710 empathy_contact_get_id (contact),
711 empathy_contact_get_handle (contact),
712 is_member ? "added" : "removed");
715 g_signal_connect (contact, "notify::presence",
716 G_CALLBACK (contact_list_store_contact_updated_cb),
718 g_signal_connect (contact, "notify::name",
719 G_CALLBACK (contact_list_store_contact_updated_cb),
721 g_signal_connect (contact, "notify::avatar",
722 G_CALLBACK (contact_list_store_contact_updated_cb),
724 g_signal_connect (contact, "notify::capabilities",
725 G_CALLBACK (contact_list_store_contact_updated_cb),
728 contact_list_store_add_contact (store, contact);
730 g_signal_handlers_disconnect_by_func (contact,
731 G_CALLBACK (contact_list_store_contact_updated_cb),
734 contact_list_store_remove_contact (store, contact);
739 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
740 EmpathyContact *contact,
743 EmpathyContactListStore *store)
745 EmpathyContactListStorePriv *priv;
746 gboolean show_active;
748 priv = GET_PRIV (store);
750 empathy_debug (DEBUG_DOMAIN, "Updating groups for contact %s (%d)",
751 empathy_contact_get_id (contact),
752 empathy_contact_get_handle (contact));
754 /* We do this to make sure the groups are correct, if not, we
755 * would have to check the groups already set up for each
756 * contact and then see what has been updated.
758 show_active = priv->show_active;
759 priv->show_active = FALSE;
760 contact_list_store_remove_contact (store, contact);
761 contact_list_store_add_contact (store, contact);
762 priv->show_active = show_active;
766 contact_list_store_add_contact (EmpathyContactListStore *store,
767 EmpathyContact *contact)
769 EmpathyContactListStorePriv *priv;
773 priv = GET_PRIV (store);
775 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
779 groups = empathy_contact_list_get_groups (priv->list, contact);
781 /* If no groups just add it at the top level. */
783 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
784 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
785 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
786 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
787 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
788 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
789 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
793 /* Else add to each group. */
794 for (l = groups; l; l = l->next) {
795 GtkTreeIter iter_group;
797 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
799 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
801 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
802 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
803 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
804 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
805 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
806 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
810 g_list_free (groups);
812 contact_list_store_contact_update (store, contact);
817 contact_list_store_remove_contact (EmpathyContactListStore *store,
818 EmpathyContact *contact)
820 EmpathyContactListStorePriv *priv;
824 priv = GET_PRIV (store);
826 iters = contact_list_store_find_contact (store, contact);
832 model = GTK_TREE_MODEL (store);
834 for (l = iters; l; l = l->next) {
837 /* NOTE: it is only <= 2 here because we have
838 * separators after the group name, otherwise it
841 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
842 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
843 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
845 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
849 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
854 contact_list_store_contact_update (EmpathyContactListStore *store,
855 EmpathyContact *contact)
857 EmpathyContactListStorePriv *priv;
858 ShowActiveData *data;
862 gboolean should_be_in_list;
863 gboolean was_online = TRUE;
864 gboolean now_online = FALSE;
865 gboolean set_model = FALSE;
866 gboolean do_remove = FALSE;
867 gboolean do_set_active = FALSE;
868 gboolean do_set_refresh = FALSE;
869 GdkPixbuf *pixbuf_avatar;
871 priv = GET_PRIV (store);
873 model = GTK_TREE_MODEL (store);
875 iters = contact_list_store_find_contact (store, contact);
882 /* Get online state now. */
883 now_online = empathy_contact_is_online (contact);
885 if (priv->show_offline || now_online) {
886 should_be_in_list = TRUE;
888 should_be_in_list = FALSE;
891 if (!in_list && !should_be_in_list) {
893 empathy_debug (DEBUG_DOMAIN,
894 "Contact:'%s' in list:NO, should be:NO",
895 empathy_contact_get_name (contact));
897 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
901 else if (in_list && !should_be_in_list) {
902 empathy_debug (DEBUG_DOMAIN,
903 "Contact:'%s' in list:YES, should be:NO",
904 empathy_contact_get_name (contact));
906 if (priv->show_active) {
908 do_set_active = TRUE;
909 do_set_refresh = TRUE;
912 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
914 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
915 contact_list_store_remove_contact (store, contact);
918 else if (!in_list && should_be_in_list) {
919 empathy_debug (DEBUG_DOMAIN,
920 "Contact:'%s' in list:NO, should be:YES",
921 empathy_contact_get_name (contact));
923 contact_list_store_add_contact (store, contact);
925 if (priv->show_active) {
926 do_set_active = TRUE;
928 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
931 empathy_debug (DEBUG_DOMAIN,
932 "Contact:'%s' in list:YES, should be:YES",
933 empathy_contact_get_name (contact));
935 /* Get online state before. */
936 if (iters && g_list_length (iters) > 0) {
937 gtk_tree_model_get (model, iters->data,
938 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
942 /* Is this really an update or an online/offline. */
943 if (priv->show_active) {
944 if (was_online != now_online) {
945 do_set_active = TRUE;
946 do_set_refresh = TRUE;
948 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
949 was_online ? "online -> offline" :
950 "offline -> online");
952 /* Was TRUE for presence updates. */
953 /* do_set_active = FALSE; */
954 do_set_refresh = TRUE;
956 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
963 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
964 for (l = iters; l && set_model; l = l->next) {
965 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
966 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
967 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
968 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
969 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
970 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
971 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
972 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
973 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
974 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
975 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
980 g_object_unref (pixbuf_avatar);
983 if (priv->show_active && do_set_active) {
984 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
987 data = contact_list_store_contact_active_new (store, contact, do_remove);
988 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
989 (GSourceFunc) contact_list_store_contact_active_cb,
994 /* FIXME: when someone goes online then offline quickly, the
995 * first timeout sets the user to be inactive and the second
996 * timeout removes the user from the contact list, really we
997 * should remove the first timeout.
999 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1000 g_list_free (iters);
1004 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1006 EmpathyContactListStore *store)
1008 empathy_debug (DEBUG_DOMAIN,
1009 "Contact:'%s' updated, checking roster is in sync...",
1010 empathy_contact_get_name (contact));
1012 contact_list_store_contact_update (store, contact);
1016 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1017 EmpathyContact *contact,
1019 gboolean set_changed)
1021 EmpathyContactListStorePriv *priv;
1022 GtkTreeModel *model;
1025 priv = GET_PRIV (store);
1026 model = GTK_TREE_MODEL (store);
1028 iters = contact_list_store_find_contact (store, contact);
1029 for (l = iters; l; l = l->next) {
1032 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1033 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1036 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1039 path = gtk_tree_model_get_path (model, l->data);
1040 gtk_tree_model_row_changed (model, path, l->data);
1041 gtk_tree_path_free (path);
1045 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1046 g_list_free (iters);
1050 static ShowActiveData *
1051 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1052 EmpathyContact *contact,
1055 ShowActiveData *data;
1057 empathy_debug (DEBUG_DOMAIN,
1058 "Contact:'%s' now active, and %s be removed",
1059 empathy_contact_get_name (contact),
1060 remove ? "WILL" : "WILL NOT");
1062 data = g_slice_new0 (ShowActiveData);
1064 data->store = g_object_ref (store);
1065 data->contact = g_object_ref (contact);
1066 data->remove = remove;
1072 contact_list_store_contact_active_free (ShowActiveData *data)
1074 g_object_unref (data->contact);
1075 g_object_unref (data->store);
1077 g_slice_free (ShowActiveData, data);
1081 contact_list_store_contact_active_cb (ShowActiveData *data)
1083 EmpathyContactListStorePriv *priv;
1085 priv = GET_PRIV (data->store);
1088 !priv->show_offline &&
1089 !empathy_contact_is_online (data->contact)) {
1090 empathy_debug (DEBUG_DOMAIN,
1091 "Contact:'%s' active timeout, removing item",
1092 empathy_contact_get_name (data->contact));
1093 contact_list_store_remove_contact (data->store, data->contact);
1096 empathy_debug (DEBUG_DOMAIN,
1097 "Contact:'%s' no longer active",
1098 empathy_contact_get_name (data->contact));
1100 contact_list_store_contact_set_active (data->store,
1105 contact_list_store_contact_active_free (data);
1111 contact_list_store_get_group_foreach (GtkTreeModel *model,
1119 /* Groups are only at the top level. */
1120 if (gtk_tree_path_get_depth (path) != 1) {
1124 gtk_tree_model_get (model, iter,
1125 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1126 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1129 if (is_group && strcmp (str, fg->name) == 0) {
1140 contact_list_store_get_group (EmpathyContactListStore *store,
1142 GtkTreeIter *iter_group_to_set,
1143 GtkTreeIter *iter_separator_to_set,
1146 EmpathyContactListStorePriv *priv;
1147 GtkTreeModel *model;
1148 GtkTreeIter iter_group;
1149 GtkTreeIter iter_separator;
1152 priv = GET_PRIV (store);
1154 memset (&fg, 0, sizeof (fg));
1158 model = GTK_TREE_MODEL (store);
1159 gtk_tree_model_foreach (model,
1160 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1168 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1169 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1170 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1171 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1172 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1173 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1174 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1177 if (iter_group_to_set) {
1178 *iter_group_to_set = iter_group;
1181 gtk_tree_store_append (GTK_TREE_STORE (store),
1184 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1185 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1188 if (iter_separator_to_set) {
1189 *iter_separator_to_set = iter_separator;
1196 if (iter_group_to_set) {
1197 *iter_group_to_set = fg.iter;
1200 iter_separator = fg.iter;
1202 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1203 gboolean is_separator;
1205 gtk_tree_model_get (model, &iter_separator,
1206 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1209 if (is_separator && iter_separator_to_set) {
1210 *iter_separator_to_set = iter_separator;
1217 contact_list_store_state_sort_func (GtkTreeModel *model,
1218 GtkTreeIter *iter_a,
1219 GtkTreeIter *iter_b,
1223 gchar *name_a, *name_b;
1224 gboolean is_separator_a, is_separator_b;
1225 EmpathyContact *contact_a, *contact_b;
1226 EmpathyPresence *presence_a, *presence_b;
1227 McPresence state_a, state_b;
1229 gtk_tree_model_get (model, iter_a,
1230 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1231 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1232 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1234 gtk_tree_model_get (model, iter_b,
1235 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1236 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1237 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1240 /* Separator or group? */
1241 if (is_separator_a || is_separator_b) {
1242 if (is_separator_a) {
1244 } else if (is_separator_b) {
1247 } else if (!contact_a && contact_b) {
1249 } else if (contact_a && !contact_b) {
1251 } else if (!contact_a && !contact_b) {
1253 ret_val = g_utf8_collate (name_a, name_b);
1260 /* If we managed to get this far, we can start looking at
1263 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1264 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1266 if (!presence_a && presence_b) {
1268 } else if (presence_a && !presence_b) {
1270 } else if (!presence_a && !presence_b) {
1271 /* Both offline, sort by name */
1272 ret_val = g_utf8_collate (name_a, name_b);
1274 state_a = empathy_presence_get_state (presence_a);
1275 state_b = empathy_presence_get_state (presence_b);
1277 if (state_a < state_b) {
1279 } else if (state_a > state_b) {
1282 /* Fallback: compare by name */
1283 ret_val = g_utf8_collate (name_a, name_b);
1292 g_object_unref (contact_a);
1296 g_object_unref (contact_b);
1303 contact_list_store_name_sort_func (GtkTreeModel *model,
1304 GtkTreeIter *iter_a,
1305 GtkTreeIter *iter_b,
1308 gchar *name_a, *name_b;
1309 EmpathyContact *contact_a, *contact_b;
1310 gboolean is_separator_a, is_separator_b;
1313 gtk_tree_model_get (model, iter_a,
1314 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1315 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1316 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1318 gtk_tree_model_get (model, iter_b,
1319 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1320 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1321 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1324 /* If contact is NULL it means it's a group. */
1326 if (is_separator_a || is_separator_b) {
1327 if (is_separator_a) {
1329 } else if (is_separator_b) {
1332 } else if (!contact_a && contact_b) {
1334 } else if (contact_a && !contact_b) {
1337 ret_val = g_utf8_collate (name_a, name_b);
1344 g_object_unref (contact_a);
1348 g_object_unref (contact_b);
1355 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1360 EmpathyContact *contact;
1362 gtk_tree_model_get (model, iter,
1363 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1370 if (empathy_contact_equal (contact, fc->contact)) {
1372 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1374 g_object_unref (contact);
1380 contact_list_store_find_contact (EmpathyContactListStore *store,
1381 EmpathyContact *contact)
1383 EmpathyContactListStorePriv *priv;
1384 GtkTreeModel *model;
1388 priv = GET_PRIV (store);
1390 memset (&fc, 0, sizeof (fc));
1392 fc.contact = contact;
1394 model = GTK_TREE_MODEL (store);
1395 gtk_tree_model_foreach (model,
1396 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1407 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1410 EmpathyContactListStore *store)
1412 EmpathyContactListStorePriv *priv;
1413 gboolean show_avatar = FALSE;
1415 priv = GET_PRIV (store);
1417 if (priv->show_avatars && !priv->is_compact) {
1421 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1422 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1423 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,