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_members_changed_cb (EmpathyContactList *list_iface,
99 EmpathyContact *contact,
100 EmpathyContact *actor,
104 EmpathyContactListStore *store);
105 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
106 EmpathyContact *contact,
109 EmpathyContactListStore *store);
110 static void contact_list_store_add_contact (EmpathyContactListStore *store,
111 EmpathyContact *contact);
112 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
113 EmpathyContact *contact);
114 static void contact_list_store_contact_update (EmpathyContactListStore *store,
115 EmpathyContact *contact);
116 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
118 EmpathyContactListStore *store);
119 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
120 EmpathyContact *contact,
122 gboolean set_changed);
123 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
124 EmpathyContact *contact,
126 static void contact_list_store_contact_active_free (ShowActiveData *data);
127 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
128 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
132 static void contact_list_store_get_group (EmpathyContactListStore *store,
134 GtkTreeIter *iter_group_to_set,
135 GtkTreeIter *iter_separator_to_set,
137 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
141 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
145 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
149 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
150 EmpathyContact *contact);
151 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
154 EmpathyContactListStore *store);
164 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
167 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
169 GObjectClass *object_class = G_OBJECT_CLASS (klass);
171 object_class->finalize = contact_list_store_finalize;
172 object_class->get_property = contact_list_store_get_property;
173 object_class->set_property = contact_list_store_set_property;
175 g_object_class_install_property (object_class,
177 g_param_spec_boolean ("show-offline",
179 "Whether contact list should display "
183 g_object_class_install_property (object_class,
185 g_param_spec_boolean ("show-avatars",
187 "Whether contact list should display "
188 "avatars for contacts",
191 g_object_class_install_property (object_class,
193 g_param_spec_boolean ("is-compact",
195 "Whether the contact list is in compact mode or not",
199 g_object_class_install_property (object_class,
201 g_param_spec_enum ("sort-criterium",
203 "The sort criterium to use for sorting the contact list",
204 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
205 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
208 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
212 empathy_contact_list_store_init (EmpathyContactListStore *store)
214 EmpathyContactListStorePriv *priv;
216 priv = GET_PRIV (store);
218 priv->show_avatars = TRUE;
219 priv->inhibit_active = g_timeout_add (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
220 (GSourceFunc) contact_list_store_inibit_active_cb,
225 contact_list_store_finalize (GObject *object)
227 EmpathyContactListStorePriv *priv;
229 priv = GET_PRIV (object);
231 /* FIXME: disconnect all signals on the list and contacts */
234 g_object_unref (priv->list);
237 if (priv->inhibit_active) {
238 g_source_remove (priv->inhibit_active);
241 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
245 contact_list_store_get_property (GObject *object,
250 EmpathyContactListStorePriv *priv;
252 priv = GET_PRIV (object);
255 case PROP_SHOW_OFFLINE:
256 g_value_set_boolean (value, priv->show_offline);
258 case PROP_SHOW_AVATARS:
259 g_value_set_boolean (value, priv->show_avatars);
261 case PROP_IS_COMPACT:
262 g_value_set_boolean (value, priv->is_compact);
264 case PROP_SORT_CRITERIUM:
265 g_value_set_enum (value, priv->sort_criterium);
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
274 contact_list_store_set_property (GObject *object,
279 EmpathyContactListStorePriv *priv;
281 priv = GET_PRIV (object);
284 case PROP_SHOW_OFFLINE:
285 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
286 g_value_get_boolean (value));
288 case PROP_SHOW_AVATARS:
289 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
290 g_value_get_boolean (value));
292 case PROP_IS_COMPACT:
293 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
294 g_value_get_boolean (value));
296 case PROP_SORT_CRITERIUM:
297 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
298 g_value_get_enum (value));
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
306 EmpathyContactListStore *
307 empathy_contact_list_store_new (EmpathyContactList *list_iface)
309 EmpathyContactListStore *store;
310 EmpathyContactListStorePriv *priv;
313 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
315 store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
316 priv = GET_PRIV (store);
318 contact_list_store_setup (store);
319 priv->list = g_object_ref (list_iface);
321 /* Signal connection. */
322 g_signal_connect (priv->list,
324 G_CALLBACK (contact_list_store_members_changed_cb),
326 g_signal_connect (priv->list,
328 G_CALLBACK (contact_list_store_groups_changed_cb),
331 /* Add contacts already created. */
332 contacts = empathy_contact_list_get_members (priv->list);
333 for (l = contacts; l; l = l->next) {
334 contact_list_store_members_changed_cb (priv->list, l->data,
339 g_object_unref (l->data);
341 g_list_free (contacts);
347 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
349 EmpathyContactListStorePriv *priv;
351 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
353 priv = GET_PRIV (store);
359 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
361 EmpathyContactListStorePriv *priv;
363 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
365 priv = GET_PRIV (store);
367 return priv->show_offline;
371 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
372 gboolean show_offline)
374 EmpathyContactListStorePriv *priv;
376 gboolean show_active;
378 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
380 priv = GET_PRIV (store);
382 priv->show_offline = show_offline;
383 show_active = priv->show_active;
385 /* Disable temporarily. */
386 priv->show_active = FALSE;
388 contacts = empathy_contact_list_get_members (priv->list);
389 for (l = contacts; l; l = l->next) {
390 contact_list_store_contact_update (store, l->data);
392 g_object_unref (l->data);
394 g_list_free (contacts);
396 /* Restore to original setting. */
397 priv->show_active = show_active;
401 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
403 EmpathyContactListStorePriv *priv;
405 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
407 priv = GET_PRIV (store);
409 return priv->show_avatars;
413 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
414 gboolean show_avatars)
416 EmpathyContactListStorePriv *priv;
419 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
421 priv = GET_PRIV (store);
423 priv->show_avatars = show_avatars;
425 model = GTK_TREE_MODEL (store);
427 gtk_tree_model_foreach (model,
428 (GtkTreeModelForeachFunc)
429 contact_list_store_update_list_mode_foreach,
434 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
436 EmpathyContactListStorePriv *priv;
438 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
440 priv = GET_PRIV (store);
442 return priv->is_compact;
446 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
449 EmpathyContactListStorePriv *priv;
452 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
454 priv = GET_PRIV (store);
456 priv->is_compact = is_compact;
458 model = GTK_TREE_MODEL (store);
460 gtk_tree_model_foreach (model,
461 (GtkTreeModelForeachFunc)
462 contact_list_store_update_list_mode_foreach,
466 EmpathyContactListStoreSort
467 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
469 EmpathyContactListStorePriv *priv;
471 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
473 priv = GET_PRIV (store);
475 return priv->sort_criterium;
479 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
480 EmpathyContactListStoreSort sort_criterium)
482 EmpathyContactListStorePriv *priv;
484 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
486 priv = GET_PRIV (store);
488 priv->sort_criterium = sort_criterium;
490 switch (sort_criterium) {
491 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
492 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
497 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
498 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
506 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
510 gboolean is_separator = FALSE;
512 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
514 gtk_tree_model_get (model, iter,
515 COL_IS_SEPARATOR, &is_separator,
522 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
524 gboolean *path_is_group)
526 GtkTreeIter parent_iter, iter;
530 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
533 *path_is_group = FALSE;
536 if (!gtk_tree_model_get_iter (model, &iter, path)) {
540 gtk_tree_model_get (model, &iter,
541 COL_IS_GROUP, &is_group,
549 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
555 gtk_tree_model_get (model, &iter,
556 COL_IS_GROUP, &is_group,
566 *path_is_group = TRUE;
573 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
577 gpointer search_data)
579 gchar *name, *name_folded;
583 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
589 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
595 name_folded = g_utf8_casefold (name, -1);
596 key_folded = g_utf8_casefold (key, -1);
598 if (name_folded && key_folded &&
599 strstr (name_folded, key_folded)) {
606 g_free (name_folded);
613 contact_list_store_setup (EmpathyContactListStore *store)
615 EmpathyContactListStorePriv *priv;
616 GType types[] = {G_TYPE_STRING, /* Status icon-name */
617 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
618 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
619 G_TYPE_STRING, /* Name */
620 G_TYPE_STRING, /* Status string */
621 G_TYPE_BOOLEAN, /* Show status */
622 EMPATHY_TYPE_CONTACT, /* Contact type */
623 G_TYPE_BOOLEAN, /* Is group */
624 G_TYPE_BOOLEAN, /* Is active */
625 G_TYPE_BOOLEAN, /* Is online */
626 G_TYPE_BOOLEAN}; /* Is separator */
628 priv = GET_PRIV (store);
630 gtk_tree_store_set_column_types (GTK_TREE_STORE (store), COL_COUNT, types);
633 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
635 contact_list_store_name_sort_func,
637 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
639 contact_list_store_state_sort_func,
642 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
643 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
647 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
649 EmpathyContactListStorePriv *priv;
651 priv = GET_PRIV (store);
653 priv->show_active = TRUE;
654 priv->inhibit_active = 0;
660 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
661 EmpathyContact *contact,
662 EmpathyContact *actor,
666 EmpathyContactListStore *store)
668 EmpathyContactListStorePriv *priv;
670 priv = GET_PRIV (store);
672 empathy_debug (DEBUG_DOMAIN,
673 "Contact %s (%d) %s",
674 empathy_contact_get_id (contact),
675 empathy_contact_get_handle (contact),
676 is_member ? "added" : "removed");
679 g_signal_connect (contact, "notify::presence",
680 G_CALLBACK (contact_list_store_contact_updated_cb),
682 g_signal_connect (contact, "notify::name",
683 G_CALLBACK (contact_list_store_contact_updated_cb),
685 g_signal_connect (contact, "notify::avatar",
686 G_CALLBACK (contact_list_store_contact_updated_cb),
688 g_signal_connect (contact, "notify::type",
689 G_CALLBACK (contact_list_store_contact_updated_cb),
692 contact_list_store_add_contact (store, contact);
694 g_signal_handlers_disconnect_by_func (contact,
695 G_CALLBACK (contact_list_store_contact_updated_cb),
698 contact_list_store_remove_contact (store, contact);
703 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
704 EmpathyContact *contact,
707 EmpathyContactListStore *store)
709 EmpathyContactListStorePriv *priv;
710 gboolean show_active;
712 priv = GET_PRIV (store);
714 empathy_debug (DEBUG_DOMAIN, "Updating groups for contact %s (%d)",
715 empathy_contact_get_id (contact),
716 empathy_contact_get_handle (contact));
718 /* We do this to make sure the groups are correct, if not, we
719 * would have to check the groups already set up for each
720 * contact and then see what has been updated.
722 show_active = priv->show_active;
723 priv->show_active = FALSE;
724 contact_list_store_remove_contact (store, contact);
725 contact_list_store_add_contact (store, contact);
726 priv->show_active = show_active;
730 contact_list_store_add_contact (EmpathyContactListStore *store,
731 EmpathyContact *contact)
733 EmpathyContactListStorePriv *priv;
737 priv = GET_PRIV (store);
739 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
743 groups = empathy_contact_list_get_groups (priv->list, contact);
745 /* If no groups just add it at the top level. */
747 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
748 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
749 COL_NAME, empathy_contact_get_name (contact),
750 COL_CONTACT, contact,
752 COL_IS_SEPARATOR, FALSE,
756 /* Else add to each group. */
757 for (l = groups; l; l = l->next) {
758 GtkTreeIter iter_group;
760 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
762 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
764 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
765 COL_NAME, empathy_contact_get_name (contact),
766 COL_CONTACT, contact,
768 COL_IS_SEPARATOR, FALSE,
772 g_list_free (groups);
774 contact_list_store_contact_update (store, contact);
779 contact_list_store_remove_contact (EmpathyContactListStore *store,
780 EmpathyContact *contact)
782 EmpathyContactListStorePriv *priv;
786 priv = GET_PRIV (store);
788 iters = contact_list_store_find_contact (store, contact);
794 model = GTK_TREE_MODEL (store);
796 for (l = iters; l; l = l->next) {
799 /* NOTE: it is only <= 2 here because we have
800 * separators after the group name, otherwise it
803 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
804 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
805 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
807 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
811 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
816 contact_list_store_contact_update (EmpathyContactListStore *store,
817 EmpathyContact *contact)
819 EmpathyContactListStorePriv *priv;
820 ShowActiveData *data;
824 gboolean should_be_in_list;
825 gboolean was_online = TRUE;
826 gboolean now_online = FALSE;
827 gboolean set_model = FALSE;
828 gboolean do_remove = FALSE;
829 gboolean do_set_active = FALSE;
830 gboolean do_set_refresh = FALSE;
831 GdkPixbuf *pixbuf_avatar;
833 priv = GET_PRIV (store);
835 model = GTK_TREE_MODEL (store);
837 iters = contact_list_store_find_contact (store, contact);
844 /* Get online state now. */
845 now_online = empathy_contact_is_online (contact);
847 if (priv->show_offline || now_online) {
848 should_be_in_list = TRUE;
850 should_be_in_list = FALSE;
853 if (!in_list && !should_be_in_list) {
855 empathy_debug (DEBUG_DOMAIN,
856 "Contact:'%s' in list:NO, should be:NO",
857 empathy_contact_get_name (contact));
859 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
863 else if (in_list && !should_be_in_list) {
864 empathy_debug (DEBUG_DOMAIN,
865 "Contact:'%s' in list:YES, should be:NO",
866 empathy_contact_get_name (contact));
868 if (priv->show_active) {
870 do_set_active = TRUE;
871 do_set_refresh = TRUE;
874 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
876 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
877 contact_list_store_remove_contact (store, contact);
880 else if (!in_list && should_be_in_list) {
881 empathy_debug (DEBUG_DOMAIN,
882 "Contact:'%s' in list:NO, should be:YES",
883 empathy_contact_get_name (contact));
885 contact_list_store_add_contact (store, contact);
887 if (priv->show_active) {
888 do_set_active = TRUE;
890 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
893 empathy_debug (DEBUG_DOMAIN,
894 "Contact:'%s' in list:YES, should be:YES",
895 empathy_contact_get_name (contact));
897 /* Get online state before. */
898 if (iters && g_list_length (iters) > 0) {
899 gtk_tree_model_get (model, iters->data,
900 COL_IS_ONLINE, &was_online,
904 /* Is this really an update or an online/offline. */
905 if (priv->show_active) {
906 if (was_online != now_online) {
907 do_set_active = TRUE;
908 do_set_refresh = TRUE;
910 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
911 was_online ? "online -> offline" :
912 "offline -> online");
914 /* Was TRUE for presence updates. */
915 /* do_set_active = FALSE; */
916 do_set_refresh = TRUE;
918 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
925 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
926 for (l = iters; l && set_model; l = l->next) {
927 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
928 COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
929 COL_PIXBUF_AVATAR, pixbuf_avatar,
930 COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
931 COL_NAME, empathy_contact_get_name (contact),
932 COL_STATUS, empathy_contact_get_status (contact),
933 COL_STATUS_VISIBLE, !priv->is_compact,
935 COL_IS_ONLINE, now_online,
936 COL_IS_SEPARATOR, FALSE,
941 g_object_unref (pixbuf_avatar);
944 if (priv->show_active && do_set_active) {
945 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
948 data = contact_list_store_contact_active_new (store, contact, do_remove);
949 g_timeout_add (ACTIVE_USER_SHOW_TIME,
950 (GSourceFunc) contact_list_store_contact_active_cb,
955 /* FIXME: when someone goes online then offline quickly, the
956 * first timeout sets the user to be inactive and the second
957 * timeout removes the user from the contact list, really we
958 * should remove the first timeout.
960 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
965 contact_list_store_contact_updated_cb (EmpathyContact *contact,
967 EmpathyContactListStore *store)
969 empathy_debug (DEBUG_DOMAIN,
970 "Contact:'%s' updated, checking roster is in sync...",
971 empathy_contact_get_name (contact));
973 contact_list_store_contact_update (store, contact);
977 contact_list_store_contact_set_active (EmpathyContactListStore *store,
978 EmpathyContact *contact,
980 gboolean set_changed)
982 EmpathyContactListStorePriv *priv;
986 priv = GET_PRIV (store);
987 model = GTK_TREE_MODEL (store);
989 iters = contact_list_store_find_contact (store, contact);
990 for (l = iters; l; l = l->next) {
993 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
994 COL_IS_ACTIVE, active,
997 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1000 path = gtk_tree_model_get_path (model, l->data);
1001 gtk_tree_model_row_changed (model, path, l->data);
1002 gtk_tree_path_free (path);
1006 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1007 g_list_free (iters);
1011 static ShowActiveData *
1012 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1013 EmpathyContact *contact,
1016 ShowActiveData *data;
1018 empathy_debug (DEBUG_DOMAIN,
1019 "Contact:'%s' now active, and %s be removed",
1020 empathy_contact_get_name (contact),
1021 remove ? "WILL" : "WILL NOT");
1023 data = g_slice_new0 (ShowActiveData);
1025 data->store = g_object_ref (store);
1026 data->contact = g_object_ref (contact);
1027 data->remove = remove;
1033 contact_list_store_contact_active_free (ShowActiveData *data)
1035 g_object_unref (data->contact);
1036 g_object_unref (data->store);
1038 g_slice_free (ShowActiveData, data);
1042 contact_list_store_contact_active_cb (ShowActiveData *data)
1044 EmpathyContactListStorePriv *priv;
1046 priv = GET_PRIV (data->store);
1049 !priv->show_offline &&
1050 !empathy_contact_is_online (data->contact)) {
1051 empathy_debug (DEBUG_DOMAIN,
1052 "Contact:'%s' active timeout, removing item",
1053 empathy_contact_get_name (data->contact));
1054 contact_list_store_remove_contact (data->store, data->contact);
1057 empathy_debug (DEBUG_DOMAIN,
1058 "Contact:'%s' no longer active",
1059 empathy_contact_get_name (data->contact));
1061 contact_list_store_contact_set_active (data->store,
1066 contact_list_store_contact_active_free (data);
1072 contact_list_store_get_group_foreach (GtkTreeModel *model,
1080 /* Groups are only at the top level. */
1081 if (gtk_tree_path_get_depth (path) != 1) {
1085 gtk_tree_model_get (model, iter,
1087 COL_IS_GROUP, &is_group,
1090 if (is_group && strcmp (str, fg->name) == 0) {
1101 contact_list_store_get_group (EmpathyContactListStore *store,
1103 GtkTreeIter *iter_group_to_set,
1104 GtkTreeIter *iter_separator_to_set,
1107 EmpathyContactListStorePriv *priv;
1108 GtkTreeModel *model;
1109 GtkTreeIter iter_group;
1110 GtkTreeIter iter_separator;
1113 priv = GET_PRIV (store);
1115 memset (&fg, 0, sizeof (fg));
1119 model = GTK_TREE_MODEL (store);
1120 gtk_tree_model_foreach (model,
1121 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1129 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1130 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1131 COL_ICON_STATUS, NULL,
1134 COL_IS_ACTIVE, FALSE,
1135 COL_IS_SEPARATOR, FALSE,
1138 if (iter_group_to_set) {
1139 *iter_group_to_set = iter_group;
1142 gtk_tree_store_append (GTK_TREE_STORE (store),
1145 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1146 COL_IS_SEPARATOR, TRUE,
1149 if (iter_separator_to_set) {
1150 *iter_separator_to_set = iter_separator;
1157 if (iter_group_to_set) {
1158 *iter_group_to_set = fg.iter;
1161 iter_separator = fg.iter;
1163 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1164 gboolean is_separator;
1166 gtk_tree_model_get (model, &iter_separator,
1167 COL_IS_SEPARATOR, &is_separator,
1170 if (is_separator && iter_separator_to_set) {
1171 *iter_separator_to_set = iter_separator;
1178 contact_list_store_state_sort_func (GtkTreeModel *model,
1179 GtkTreeIter *iter_a,
1180 GtkTreeIter *iter_b,
1184 gchar *name_a, *name_b;
1185 gboolean is_separator_a, is_separator_b;
1186 EmpathyContact *contact_a, *contact_b;
1187 EmpathyPresence *presence_a, *presence_b;
1188 McPresence state_a, state_b;
1190 gtk_tree_model_get (model, iter_a,
1192 COL_CONTACT, &contact_a,
1193 COL_IS_SEPARATOR, &is_separator_a,
1195 gtk_tree_model_get (model, iter_b,
1197 COL_CONTACT, &contact_b,
1198 COL_IS_SEPARATOR, &is_separator_b,
1201 /* Separator or group? */
1202 if (is_separator_a || is_separator_b) {
1203 if (is_separator_a) {
1205 } else if (is_separator_b) {
1208 } else if (!contact_a && contact_b) {
1210 } else if (contact_a && !contact_b) {
1212 } else if (!contact_a && !contact_b) {
1214 ret_val = g_utf8_collate (name_a, name_b);
1221 /* If we managed to get this far, we can start looking at
1224 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1225 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1227 if (!presence_a && presence_b) {
1229 } else if (presence_a && !presence_b) {
1231 } else if (!presence_a && !presence_b) {
1232 /* Both offline, sort by name */
1233 ret_val = g_utf8_collate (name_a, name_b);
1235 state_a = empathy_presence_get_state (presence_a);
1236 state_b = empathy_presence_get_state (presence_b);
1238 if (state_a < state_b) {
1240 } else if (state_a > state_b) {
1243 /* Fallback: compare by name */
1244 ret_val = g_utf8_collate (name_a, name_b);
1253 g_object_unref (contact_a);
1257 g_object_unref (contact_b);
1264 contact_list_store_name_sort_func (GtkTreeModel *model,
1265 GtkTreeIter *iter_a,
1266 GtkTreeIter *iter_b,
1269 gchar *name_a, *name_b;
1270 EmpathyContact *contact_a, *contact_b;
1271 gboolean is_separator_a, is_separator_b;
1274 gtk_tree_model_get (model, iter_a,
1276 COL_CONTACT, &contact_a,
1277 COL_IS_SEPARATOR, &is_separator_a,
1279 gtk_tree_model_get (model, iter_b,
1281 COL_CONTACT, &contact_b,
1282 COL_IS_SEPARATOR, &is_separator_b,
1285 /* If contact is NULL it means it's a group. */
1287 if (is_separator_a || is_separator_b) {
1288 if (is_separator_a) {
1290 } else if (is_separator_b) {
1293 } else if (!contact_a && contact_b) {
1295 } else if (contact_a && !contact_b) {
1298 ret_val = g_utf8_collate (name_a, name_b);
1305 g_object_unref (contact_a);
1309 g_object_unref (contact_b);
1316 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1321 EmpathyContact *contact;
1323 gtk_tree_model_get (model, iter,
1324 COL_CONTACT, &contact,
1331 if (empathy_contact_equal (contact, fc->contact)) {
1333 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1335 g_object_unref (contact);
1341 contact_list_store_find_contact (EmpathyContactListStore *store,
1342 EmpathyContact *contact)
1344 EmpathyContactListStorePriv *priv;
1345 GtkTreeModel *model;
1349 priv = GET_PRIV (store);
1351 memset (&fc, 0, sizeof (fc));
1353 fc.contact = contact;
1355 model = GTK_TREE_MODEL (store);
1356 gtk_tree_model_foreach (model,
1357 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1368 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1371 EmpathyContactListStore *store)
1373 EmpathyContactListStorePriv *priv;
1374 gboolean show_avatar = FALSE;
1376 priv = GET_PRIV (store);
1378 if (priv->show_avatars && !priv->is_compact) {
1382 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1383 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1384 COL_STATUS_VISIBLE, !priv->is_compact,