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-2008 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-ui-utils.h"
37 #include "empathy-gtk-enum-types.h"
39 #define DEBUG_DOMAIN "ContactListStore"
41 /* Active users are those which have recently changed state
42 * (e.g. online, offline or from normal to a busy state).
45 /* Time in seconds user is shown as active */
46 #define ACTIVE_USER_SHOW_TIME 7
48 /* Time in seconds after connecting which we wait before active users are enabled */
49 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
54 EmpathyContactList *list;
55 gboolean show_offline;
56 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);
167 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
171 contact_list_store_iface_setup (gpointer user_data)
173 EmpathyContactListStore *store = user_data;
174 EmpathyContactListStorePriv *priv = GET_PRIV (store);
177 /* Signal connection. */
178 g_signal_connect (priv->list,
180 G_CALLBACK (contact_list_store_members_changed_cb),
182 g_signal_connect (priv->list,
184 G_CALLBACK (contact_list_store_groups_changed_cb),
187 /* Add contacts already created. */
188 contacts = empathy_contact_list_get_members (priv->list);
189 for (l = contacts; l; l = l->next) {
190 contact_list_store_members_changed_cb (priv->list, l->data,
195 g_object_unref (l->data);
197 g_list_free (contacts);
204 contact_list_store_set_contact_list (EmpathyContactListStore *store,
205 EmpathyContactList *list_iface)
207 EmpathyContactListStorePriv *priv = GET_PRIV (store);
209 priv->list = g_object_ref (list_iface);
211 /* Let a chance to have all properties set before populating */
212 g_idle_add (contact_list_store_iface_setup,
217 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
219 GObjectClass *object_class = G_OBJECT_CLASS (klass);
221 object_class->finalize = contact_list_store_finalize;
222 object_class->get_property = contact_list_store_get_property;
223 object_class->set_property = contact_list_store_set_property;
225 g_object_class_install_property (object_class,
227 g_param_spec_object ("contact-list",
228 "The contact list iface",
229 "The contact list iface",
230 EMPATHY_TYPE_CONTACT_LIST,
231 G_PARAM_CONSTRUCT_ONLY |
233 g_object_class_install_property (object_class,
235 g_param_spec_boolean ("show-offline",
237 "Whether contact list should display "
241 g_object_class_install_property (object_class,
243 g_param_spec_boolean ("show-avatars",
245 "Whether contact list should display "
246 "avatars for contacts",
249 g_object_class_install_property (object_class,
251 g_param_spec_boolean ("show-groups",
253 "Whether contact list should display "
257 g_object_class_install_property (object_class,
259 g_param_spec_boolean ("is-compact",
261 "Whether the contact list is in compact mode or not",
265 g_object_class_install_property (object_class,
267 g_param_spec_enum ("sort-criterium",
269 "The sort criterium to use for sorting the contact list",
270 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
271 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
274 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
278 empathy_contact_list_store_init (EmpathyContactListStore *store)
280 EmpathyContactListStorePriv *priv;
282 priv = GET_PRIV (store);
284 priv->show_avatars = TRUE;
285 priv->show_groups = TRUE;
286 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
287 (GSourceFunc) contact_list_store_inibit_active_cb,
289 contact_list_store_setup (store);
293 contact_list_store_finalize (GObject *object)
295 EmpathyContactListStorePriv *priv;
297 priv = GET_PRIV (object);
299 gtk_tree_model_foreach (GTK_TREE_MODEL (object),
300 (GtkTreeModelForeachFunc) contact_list_store_finalize_foreach,
304 g_signal_handlers_disconnect_by_func (priv->list,
305 G_CALLBACK (contact_list_store_members_changed_cb),
307 g_signal_handlers_disconnect_by_func (priv->list,
308 G_CALLBACK (contact_list_store_groups_changed_cb),
310 g_object_unref (priv->list);
313 if (priv->inhibit_active) {
314 g_source_remove (priv->inhibit_active);
317 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
321 contact_list_store_get_property (GObject *object,
326 EmpathyContactListStorePriv *priv;
328 priv = GET_PRIV (object);
331 case PROP_CONTACT_LIST:
332 g_value_set_object (value, priv->list);
334 case PROP_SHOW_OFFLINE:
335 g_value_set_boolean (value, priv->show_offline);
337 case PROP_SHOW_AVATARS:
338 g_value_set_boolean (value, priv->show_avatars);
340 case PROP_SHOW_GROUPS:
341 g_value_set_boolean (value, priv->show_groups);
343 case PROP_IS_COMPACT:
344 g_value_set_boolean (value, priv->is_compact);
346 case PROP_SORT_CRITERIUM:
347 g_value_set_enum (value, priv->sort_criterium);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
356 contact_list_store_set_property (GObject *object,
361 EmpathyContactListStorePriv *priv;
363 priv = GET_PRIV (object);
366 case PROP_CONTACT_LIST:
367 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
368 g_value_get_object (value));
370 case PROP_SHOW_OFFLINE:
371 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
372 g_value_get_boolean (value));
374 case PROP_SHOW_AVATARS:
375 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
376 g_value_get_boolean (value));
378 case PROP_SHOW_GROUPS:
379 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
380 g_value_get_boolean (value));
382 case PROP_IS_COMPACT:
383 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
384 g_value_get_boolean (value));
386 case PROP_SORT_CRITERIUM:
387 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
388 g_value_get_enum (value));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
396 EmpathyContactListStore *
397 empathy_contact_list_store_new (EmpathyContactList *list_iface)
399 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
401 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
402 "contact-list", list_iface,
407 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
409 EmpathyContactListStorePriv *priv;
411 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
413 priv = GET_PRIV (store);
419 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
421 EmpathyContactListStorePriv *priv;
423 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
425 priv = GET_PRIV (store);
427 return priv->show_offline;
431 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
432 gboolean show_offline)
434 EmpathyContactListStorePriv *priv;
436 gboolean show_active;
438 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
440 priv = GET_PRIV (store);
442 priv->show_offline = show_offline;
443 show_active = priv->show_active;
445 /* Disable temporarily. */
446 priv->show_active = FALSE;
448 contacts = empathy_contact_list_get_members (priv->list);
449 for (l = contacts; l; l = l->next) {
450 contact_list_store_contact_update (store, l->data);
452 g_object_unref (l->data);
454 g_list_free (contacts);
456 /* Restore to original setting. */
457 priv->show_active = show_active;
459 g_object_notify (G_OBJECT (store), "show-offline");
463 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
465 EmpathyContactListStorePriv *priv;
467 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
469 priv = GET_PRIV (store);
471 return priv->show_avatars;
475 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
476 gboolean show_avatars)
478 EmpathyContactListStorePriv *priv;
481 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
483 priv = GET_PRIV (store);
485 priv->show_avatars = show_avatars;
487 model = GTK_TREE_MODEL (store);
489 gtk_tree_model_foreach (model,
490 (GtkTreeModelForeachFunc)
491 contact_list_store_update_list_mode_foreach,
494 g_object_notify (G_OBJECT (store), "show-avatars");
498 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
500 EmpathyContactListStorePriv *priv;
502 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
504 priv = GET_PRIV (store);
506 return priv->show_groups;
510 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
511 gboolean show_groups)
513 EmpathyContactListStorePriv *priv;
516 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
518 priv = GET_PRIV (store);
520 if (priv->show_groups == show_groups) {
524 priv->show_groups = show_groups;
526 /* Remove all contacts and add them back, not optimized but that's the
528 gtk_tree_store_clear (GTK_TREE_STORE (store));
529 contacts = empathy_contact_list_get_members (priv->list);
530 for (l = contacts; l; l = l->next) {
531 contact_list_store_members_changed_cb (priv->list, l->data,
536 g_object_unref (l->data);
538 g_list_free (contacts);
540 g_object_notify (G_OBJECT (store), "show-groups");
544 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
546 EmpathyContactListStorePriv *priv;
548 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
550 priv = GET_PRIV (store);
552 return priv->is_compact;
556 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
559 EmpathyContactListStorePriv *priv;
562 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
564 priv = GET_PRIV (store);
566 priv->is_compact = is_compact;
568 model = GTK_TREE_MODEL (store);
570 gtk_tree_model_foreach (model,
571 (GtkTreeModelForeachFunc)
572 contact_list_store_update_list_mode_foreach,
575 g_object_notify (G_OBJECT (store), "is-compact");
578 EmpathyContactListStoreSort
579 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
581 EmpathyContactListStorePriv *priv;
583 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
585 priv = GET_PRIV (store);
587 return priv->sort_criterium;
591 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
592 EmpathyContactListStoreSort sort_criterium)
594 EmpathyContactListStorePriv *priv;
596 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
598 priv = GET_PRIV (store);
600 priv->sort_criterium = sort_criterium;
602 switch (sort_criterium) {
603 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
604 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
605 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
609 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
610 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
611 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
616 g_object_notify (G_OBJECT (store), "sort-criterium");
620 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
624 gboolean is_separator = FALSE;
626 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
628 gtk_tree_model_get (model, iter,
629 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
636 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
638 gboolean *path_is_group)
640 GtkTreeIter parent_iter, iter;
644 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
647 *path_is_group = FALSE;
650 if (!gtk_tree_model_get_iter (model, &iter, path)) {
654 gtk_tree_model_get (model, &iter,
655 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
656 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
663 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
669 gtk_tree_model_get (model, &iter,
670 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
671 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
680 *path_is_group = TRUE;
687 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
691 gpointer search_data)
693 gchar *name, *name_folded;
697 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
703 gtk_tree_model_get (model, iter,
704 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
711 name_folded = g_utf8_casefold (name, -1);
712 key_folded = g_utf8_casefold (key, -1);
714 if (name_folded && key_folded &&
715 strstr (name_folded, key_folded)) {
722 g_free (name_folded);
729 contact_list_store_finalize_foreach (GtkTreeModel *model,
734 EmpathyContactListStore *store = user_data;
735 EmpathyContact *contact = NULL;
737 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
738 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
742 g_signal_handlers_disconnect_by_func (contact,
743 G_CALLBACK (contact_list_store_contact_updated_cb),
751 contact_list_store_setup (EmpathyContactListStore *store)
753 EmpathyContactListStorePriv *priv;
754 GType types[] = {G_TYPE_STRING, /* Status icon-name */
755 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
756 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
757 G_TYPE_STRING, /* Name */
758 G_TYPE_STRING, /* Status string */
759 G_TYPE_BOOLEAN, /* Show status */
760 EMPATHY_TYPE_CONTACT, /* Contact type */
761 G_TYPE_BOOLEAN, /* Is group */
762 G_TYPE_BOOLEAN, /* Is active */
763 G_TYPE_BOOLEAN, /* Is online */
764 G_TYPE_BOOLEAN, /* Is separator */
765 G_TYPE_BOOLEAN}; /* Can VoIP */
767 priv = GET_PRIV (store);
769 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
770 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
774 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
775 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
776 contact_list_store_name_sort_func,
778 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
779 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
780 contact_list_store_state_sort_func,
783 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
784 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
788 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
790 EmpathyContactListStorePriv *priv;
792 priv = GET_PRIV (store);
794 priv->show_active = TRUE;
795 priv->inhibit_active = 0;
801 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
802 EmpathyContact *contact,
803 EmpathyContact *actor,
807 EmpathyContactListStore *store)
809 EmpathyContactListStorePriv *priv;
811 priv = GET_PRIV (store);
813 empathy_debug (DEBUG_DOMAIN,
814 "Contact %s (%d) %s",
815 empathy_contact_get_id (contact),
816 empathy_contact_get_handle (contact),
817 is_member ? "added" : "removed");
820 g_signal_connect (contact, "notify::presence",
821 G_CALLBACK (contact_list_store_contact_updated_cb),
823 g_signal_connect (contact, "notify::presence-message",
824 G_CALLBACK (contact_list_store_contact_updated_cb),
826 g_signal_connect (contact, "notify::name",
827 G_CALLBACK (contact_list_store_contact_updated_cb),
829 g_signal_connect (contact, "notify::avatar",
830 G_CALLBACK (contact_list_store_contact_updated_cb),
832 g_signal_connect (contact, "notify::capabilities",
833 G_CALLBACK (contact_list_store_contact_updated_cb),
836 contact_list_store_add_contact (store, contact);
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);
847 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
848 EmpathyContact *contact,
851 EmpathyContactListStore *store)
853 EmpathyContactListStorePriv *priv;
854 gboolean show_active;
856 priv = GET_PRIV (store);
858 empathy_debug (DEBUG_DOMAIN, "Updating groups for contact %s (%d)",
859 empathy_contact_get_id (contact),
860 empathy_contact_get_handle (contact));
862 /* We do this to make sure the groups are correct, if not, we
863 * would have to check the groups already set up for each
864 * contact and then see what has been updated.
866 show_active = priv->show_active;
867 priv->show_active = FALSE;
868 contact_list_store_remove_contact (store, contact);
869 contact_list_store_add_contact (store, contact);
870 priv->show_active = show_active;
874 contact_list_store_add_contact (EmpathyContactListStore *store,
875 EmpathyContact *contact)
877 EmpathyContactListStorePriv *priv;
879 GList *groups = NULL, *l;
881 priv = GET_PRIV (store);
883 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
887 if (priv->show_groups) {
888 groups = empathy_contact_list_get_groups (priv->list, contact);
891 /* If no groups just add it at the top level. */
893 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
894 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
895 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
896 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
897 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
898 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
899 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
903 /* Else add to each group. */
904 for (l = groups; l; l = l->next) {
905 GtkTreeIter iter_group;
907 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
909 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
911 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
912 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
913 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
914 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
915 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
916 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
920 g_list_free (groups);
922 contact_list_store_contact_update (store, contact);
927 contact_list_store_remove_contact (EmpathyContactListStore *store,
928 EmpathyContact *contact)
930 EmpathyContactListStorePriv *priv;
934 priv = GET_PRIV (store);
936 iters = contact_list_store_find_contact (store, contact);
942 model = GTK_TREE_MODEL (store);
944 for (l = iters; l; l = l->next) {
947 /* NOTE: it is only <= 2 here because we have
948 * separators after the group name, otherwise it
951 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
952 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
953 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
955 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
959 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
964 contact_list_store_contact_update (EmpathyContactListStore *store,
965 EmpathyContact *contact)
967 EmpathyContactListStorePriv *priv;
968 ShowActiveData *data;
972 gboolean should_be_in_list;
973 gboolean was_online = TRUE;
974 gboolean now_online = FALSE;
975 gboolean set_model = FALSE;
976 gboolean do_remove = FALSE;
977 gboolean do_set_active = FALSE;
978 gboolean do_set_refresh = FALSE;
979 gboolean show_avatar = FALSE;
980 GdkPixbuf *pixbuf_avatar;
982 priv = GET_PRIV (store);
984 model = GTK_TREE_MODEL (store);
986 iters = contact_list_store_find_contact (store, contact);
993 /* Get online state now. */
994 now_online = empathy_contact_is_online (contact);
996 if (priv->show_offline || now_online) {
997 should_be_in_list = TRUE;
999 should_be_in_list = FALSE;
1002 if (!in_list && !should_be_in_list) {
1003 /* Nothing to do. */
1004 empathy_debug (DEBUG_DOMAIN,
1005 "Contact:'%s' in list:NO, should be:NO",
1006 empathy_contact_get_name (contact));
1008 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1009 g_list_free (iters);
1012 else if (in_list && !should_be_in_list) {
1013 empathy_debug (DEBUG_DOMAIN,
1014 "Contact:'%s' in list:YES, should be:NO",
1015 empathy_contact_get_name (contact));
1017 if (priv->show_active) {
1019 do_set_active = TRUE;
1020 do_set_refresh = TRUE;
1023 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
1025 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
1026 contact_list_store_remove_contact (store, contact);
1029 else if (!in_list && should_be_in_list) {
1030 empathy_debug (DEBUG_DOMAIN,
1031 "Contact:'%s' in list:NO, should be:YES",
1032 empathy_contact_get_name (contact));
1034 contact_list_store_add_contact (store, contact);
1036 if (priv->show_active) {
1037 do_set_active = TRUE;
1039 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
1042 empathy_debug (DEBUG_DOMAIN,
1043 "Contact:'%s' in list:YES, should be:YES",
1044 empathy_contact_get_name (contact));
1046 /* Get online state before. */
1047 if (iters && g_list_length (iters) > 0) {
1048 gtk_tree_model_get (model, iters->data,
1049 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1053 /* Is this really an update or an online/offline. */
1054 if (priv->show_active) {
1055 if (was_online != now_online) {
1056 do_set_active = TRUE;
1057 do_set_refresh = TRUE;
1059 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
1060 was_online ? "online -> offline" :
1061 "offline -> online");
1063 /* Was TRUE for presence updates. */
1064 /* do_set_active = FALSE; */
1065 do_set_refresh = TRUE;
1067 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
1074 if (priv->show_avatars && !priv->is_compact) {
1077 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1078 for (l = iters; l && set_model; l = l->next) {
1079 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1080 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1081 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1082 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1083 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1084 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1085 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1086 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1087 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1088 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1089 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
1093 if (pixbuf_avatar) {
1094 g_object_unref (pixbuf_avatar);
1097 if (priv->show_active && do_set_active) {
1098 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1100 if (do_set_active) {
1101 data = contact_list_store_contact_active_new (store, contact, do_remove);
1102 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1103 (GSourceFunc) contact_list_store_contact_active_cb,
1108 /* FIXME: when someone goes online then offline quickly, the
1109 * first timeout sets the user to be inactive and the second
1110 * timeout removes the user from the contact list, really we
1111 * should remove the first timeout.
1113 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1114 g_list_free (iters);
1118 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1120 EmpathyContactListStore *store)
1122 empathy_debug (DEBUG_DOMAIN,
1123 "Contact:'%s' updated, checking roster is in sync...",
1124 empathy_contact_get_name (contact));
1126 contact_list_store_contact_update (store, contact);
1130 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1131 EmpathyContact *contact,
1133 gboolean set_changed)
1135 EmpathyContactListStorePriv *priv;
1136 GtkTreeModel *model;
1139 priv = GET_PRIV (store);
1140 model = GTK_TREE_MODEL (store);
1142 iters = contact_list_store_find_contact (store, contact);
1143 for (l = iters; l; l = l->next) {
1146 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1147 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1150 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1153 path = gtk_tree_model_get_path (model, l->data);
1154 gtk_tree_model_row_changed (model, path, l->data);
1155 gtk_tree_path_free (path);
1159 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1160 g_list_free (iters);
1164 static ShowActiveData *
1165 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1166 EmpathyContact *contact,
1169 ShowActiveData *data;
1171 empathy_debug (DEBUG_DOMAIN,
1172 "Contact:'%s' now active, and %s be removed",
1173 empathy_contact_get_name (contact),
1174 remove ? "WILL" : "WILL NOT");
1176 data = g_slice_new0 (ShowActiveData);
1178 data->store = g_object_ref (store);
1179 data->contact = g_object_ref (contact);
1180 data->remove = remove;
1186 contact_list_store_contact_active_free (ShowActiveData *data)
1188 g_object_unref (data->contact);
1189 g_object_unref (data->store);
1191 g_slice_free (ShowActiveData, data);
1195 contact_list_store_contact_active_cb (ShowActiveData *data)
1197 EmpathyContactListStorePriv *priv;
1199 priv = GET_PRIV (data->store);
1202 !priv->show_offline &&
1203 !empathy_contact_is_online (data->contact)) {
1204 empathy_debug (DEBUG_DOMAIN,
1205 "Contact:'%s' active timeout, removing item",
1206 empathy_contact_get_name (data->contact));
1207 contact_list_store_remove_contact (data->store, data->contact);
1210 empathy_debug (DEBUG_DOMAIN,
1211 "Contact:'%s' no longer active",
1212 empathy_contact_get_name (data->contact));
1214 contact_list_store_contact_set_active (data->store,
1219 contact_list_store_contact_active_free (data);
1225 contact_list_store_get_group_foreach (GtkTreeModel *model,
1233 /* Groups are only at the top level. */
1234 if (gtk_tree_path_get_depth (path) != 1) {
1238 gtk_tree_model_get (model, iter,
1239 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1240 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1243 if (is_group && strcmp (str, fg->name) == 0) {
1254 contact_list_store_get_group (EmpathyContactListStore *store,
1256 GtkTreeIter *iter_group_to_set,
1257 GtkTreeIter *iter_separator_to_set,
1260 EmpathyContactListStorePriv *priv;
1261 GtkTreeModel *model;
1262 GtkTreeIter iter_group;
1263 GtkTreeIter iter_separator;
1266 priv = GET_PRIV (store);
1268 memset (&fg, 0, sizeof (fg));
1272 model = GTK_TREE_MODEL (store);
1273 gtk_tree_model_foreach (model,
1274 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1282 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1283 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1284 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1285 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1286 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1287 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1288 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1291 if (iter_group_to_set) {
1292 *iter_group_to_set = iter_group;
1295 gtk_tree_store_append (GTK_TREE_STORE (store),
1298 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1299 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1302 if (iter_separator_to_set) {
1303 *iter_separator_to_set = iter_separator;
1310 if (iter_group_to_set) {
1311 *iter_group_to_set = fg.iter;
1314 iter_separator = fg.iter;
1316 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1317 gboolean is_separator;
1319 gtk_tree_model_get (model, &iter_separator,
1320 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1323 if (is_separator && iter_separator_to_set) {
1324 *iter_separator_to_set = iter_separator;
1331 contact_list_store_ordered_presence (McPresence state)
1334 case MC_PRESENCE_UNSET:
1335 case MC_PRESENCE_OFFLINE:
1337 case MC_PRESENCE_AVAILABLE:
1339 case MC_PRESENCE_AWAY:
1341 case MC_PRESENCE_EXTENDED_AWAY:
1343 case MC_PRESENCE_HIDDEN:
1345 case MC_PRESENCE_DO_NOT_DISTURB:
1348 g_return_val_if_reached (6);
1353 contact_list_store_state_sort_func (GtkTreeModel *model,
1354 GtkTreeIter *iter_a,
1355 GtkTreeIter *iter_b,
1359 gchar *name_a, *name_b;
1360 gboolean is_separator_a, is_separator_b;
1361 EmpathyContact *contact_a, *contact_b;
1362 guint presence_a, presence_b;
1364 gtk_tree_model_get (model, iter_a,
1365 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1366 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1367 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1369 gtk_tree_model_get (model, iter_b,
1370 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1371 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1372 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1375 /* Separator or group? */
1376 if (is_separator_a || is_separator_b) {
1377 if (is_separator_a) {
1379 } else if (is_separator_b) {
1382 } else if (!contact_a && contact_b) {
1384 } else if (contact_a && !contact_b) {
1386 } else if (!contact_a && !contact_b) {
1388 ret_val = g_utf8_collate (name_a, name_b);
1395 /* If we managed to get this far, we can start looking at
1398 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1399 presence_a = contact_list_store_ordered_presence (presence_a);
1400 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1401 presence_b = contact_list_store_ordered_presence (presence_b);
1403 if (presence_a < presence_b) {
1405 } else if (presence_a > presence_b) {
1408 /* Fallback: compare by name */
1409 ret_val = g_utf8_collate (name_a, name_b);
1417 g_object_unref (contact_a);
1421 g_object_unref (contact_b);
1428 contact_list_store_name_sort_func (GtkTreeModel *model,
1429 GtkTreeIter *iter_a,
1430 GtkTreeIter *iter_b,
1433 gchar *name_a, *name_b;
1434 EmpathyContact *contact_a, *contact_b;
1435 gboolean is_separator_a, is_separator_b;
1438 gtk_tree_model_get (model, iter_a,
1439 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1440 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1441 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1443 gtk_tree_model_get (model, iter_b,
1444 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1445 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1446 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1449 /* If contact is NULL it means it's a group. */
1451 if (is_separator_a || is_separator_b) {
1452 if (is_separator_a) {
1454 } else if (is_separator_b) {
1457 } else if (!contact_a && contact_b) {
1459 } else if (contact_a && !contact_b) {
1462 ret_val = g_utf8_collate (name_a, name_b);
1469 g_object_unref (contact_a);
1473 g_object_unref (contact_b);
1480 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1485 EmpathyContact *contact;
1487 gtk_tree_model_get (model, iter,
1488 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1495 if (empathy_contact_equal (contact, fc->contact)) {
1497 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1499 g_object_unref (contact);
1505 contact_list_store_find_contact (EmpathyContactListStore *store,
1506 EmpathyContact *contact)
1508 EmpathyContactListStorePriv *priv;
1509 GtkTreeModel *model;
1513 priv = GET_PRIV (store);
1515 memset (&fc, 0, sizeof (fc));
1517 fc.contact = contact;
1519 model = GTK_TREE_MODEL (store);
1520 gtk_tree_model_foreach (model,
1521 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1532 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1535 EmpathyContactListStore *store)
1537 EmpathyContactListStorePriv *priv;
1538 gboolean show_avatar = FALSE;
1540 priv = GET_PRIV (store);
1542 if (priv->show_avatars && !priv->is_compact) {
1546 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1547 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1548 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,