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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <telepathy-glib/util.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-enum-types.h>
37 #include <libempathy/empathy-contact-manager.h>
39 #include "empathy-contact-list-store.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-gtk-enum-types.h"
43 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
44 #include <libempathy/empathy-debug.h>
46 /* Active users are those which have recently changed state
47 * (e.g. online, offline or from normal to a busy state).
50 /* Time in seconds user is shown as active */
51 #define ACTIVE_USER_SHOW_TIME 7
53 /* Time in seconds after connecting which we wait before active users are enabled */
54 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListStore)
58 EmpathyContactList *list;
59 gboolean show_offline;
60 gboolean show_avatars;
64 EmpathyContactListStoreSort sort_criterium;
67 gboolean dispose_has_run;
68 } EmpathyContactListStorePriv;
77 EmpathyContact *contact;
83 EmpathyContactListStore *store;
84 EmpathyContact *contact;
88 static void contact_list_store_dispose (GObject *object);
89 static void contact_list_store_get_property (GObject *object,
93 static void contact_list_store_set_property (GObject *object,
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);
170 contact_list_store_iface_setup (gpointer user_data)
172 EmpathyContactListStore *store = user_data;
173 EmpathyContactListStorePriv *priv = GET_PRIV (store);
176 /* Signal connection. */
177 g_signal_connect (priv->list,
179 G_CALLBACK (contact_list_store_members_changed_cb),
181 g_signal_connect (priv->list,
183 G_CALLBACK (contact_list_store_groups_changed_cb),
186 /* Add contacts already created. */
187 contacts = empathy_contact_list_get_members (priv->list);
188 for (l = contacts; l; l = l->next) {
189 contact_list_store_members_changed_cb (priv->list, l->data,
194 g_object_unref (l->data);
196 g_list_free (contacts);
198 priv->setup_idle_id = 0;
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 priv->setup_idle_id = g_idle_add (contact_list_store_iface_setup, store);
216 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
218 GObjectClass *object_class = G_OBJECT_CLASS (klass);
220 object_class->dispose = contact_list_store_dispose;
221 object_class->get_property = contact_list_store_get_property;
222 object_class->set_property = contact_list_store_set_property;
224 g_object_class_install_property (object_class,
226 g_param_spec_object ("contact-list",
227 "The contact list iface",
228 "The contact list iface",
229 EMPATHY_TYPE_CONTACT_LIST,
230 G_PARAM_CONSTRUCT_ONLY |
232 g_object_class_install_property (object_class,
234 g_param_spec_boolean ("show-offline",
236 "Whether contact list should display "
240 g_object_class_install_property (object_class,
242 g_param_spec_boolean ("show-avatars",
244 "Whether contact list should display "
245 "avatars for contacts",
248 g_object_class_install_property (object_class,
250 g_param_spec_boolean ("show-groups",
252 "Whether contact list should display "
256 g_object_class_install_property (object_class,
258 g_param_spec_boolean ("is-compact",
260 "Whether the contact list is in compact mode or not",
264 g_object_class_install_property (object_class,
266 g_param_spec_enum ("sort-criterium",
268 "The sort criterium to use for sorting the contact list",
269 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
270 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
273 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
277 empathy_contact_list_store_init (EmpathyContactListStore *store)
279 EmpathyContactListStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (store,
280 EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv);
283 priv->show_avatars = TRUE;
284 priv->show_groups = TRUE;
285 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
286 (GSourceFunc) contact_list_store_inibit_active_cb,
288 contact_list_store_setup (store);
292 contact_list_store_dispose (GObject *object)
294 EmpathyContactListStorePriv *priv = GET_PRIV (object);
297 if (priv->dispose_has_run)
299 priv->dispose_has_run = TRUE;
301 contacts = empathy_contact_list_get_members (priv->list);
302 for (l = contacts; l; l = l->next) {
303 g_signal_handlers_disconnect_by_func (l->data,
304 G_CALLBACK (contact_list_store_contact_updated_cb),
307 g_object_unref (l->data);
309 g_list_free (contacts);
311 g_signal_handlers_disconnect_by_func (priv->list,
312 G_CALLBACK (contact_list_store_members_changed_cb),
314 g_signal_handlers_disconnect_by_func (priv->list,
315 G_CALLBACK (contact_list_store_groups_changed_cb),
317 g_object_unref (priv->list);
319 if (priv->inhibit_active) {
320 g_source_remove (priv->inhibit_active);
323 if (priv->setup_idle_id != 0) {
324 g_source_remove (priv->setup_idle_id);
327 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->dispose (object);
331 contact_list_store_get_property (GObject *object,
336 EmpathyContactListStorePriv *priv;
338 priv = GET_PRIV (object);
341 case PROP_CONTACT_LIST:
342 g_value_set_object (value, priv->list);
344 case PROP_SHOW_OFFLINE:
345 g_value_set_boolean (value, priv->show_offline);
347 case PROP_SHOW_AVATARS:
348 g_value_set_boolean (value, priv->show_avatars);
350 case PROP_SHOW_GROUPS:
351 g_value_set_boolean (value, priv->show_groups);
353 case PROP_IS_COMPACT:
354 g_value_set_boolean (value, priv->is_compact);
356 case PROP_SORT_CRITERIUM:
357 g_value_set_enum (value, priv->sort_criterium);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
366 contact_list_store_set_property (GObject *object,
371 EmpathyContactListStorePriv *priv;
373 priv = GET_PRIV (object);
376 case PROP_CONTACT_LIST:
377 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
378 g_value_get_object (value));
380 case PROP_SHOW_OFFLINE:
381 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
382 g_value_get_boolean (value));
384 case PROP_SHOW_AVATARS:
385 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
386 g_value_get_boolean (value));
388 case PROP_SHOW_GROUPS:
389 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
390 g_value_get_boolean (value));
392 case PROP_IS_COMPACT:
393 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
394 g_value_get_boolean (value));
396 case PROP_SORT_CRITERIUM:
397 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
398 g_value_get_enum (value));
401 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
406 EmpathyContactListStore *
407 empathy_contact_list_store_new (EmpathyContactList *list_iface)
409 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
411 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
412 "contact-list", list_iface,
417 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
419 EmpathyContactListStorePriv *priv;
421 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
423 priv = GET_PRIV (store);
429 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
431 EmpathyContactListStorePriv *priv;
433 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
435 priv = GET_PRIV (store);
437 return priv->show_offline;
441 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
442 gboolean show_offline)
444 EmpathyContactListStorePriv *priv;
446 gboolean show_active;
448 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
450 priv = GET_PRIV (store);
452 priv->show_offline = show_offline;
453 show_active = priv->show_active;
455 /* Disable temporarily. */
456 priv->show_active = FALSE;
458 contacts = empathy_contact_list_get_members (priv->list);
459 for (l = contacts; l; l = l->next) {
460 contact_list_store_contact_update (store, l->data);
462 g_object_unref (l->data);
464 g_list_free (contacts);
466 /* Restore to original setting. */
467 priv->show_active = show_active;
469 g_object_notify (G_OBJECT (store), "show-offline");
473 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
475 EmpathyContactListStorePriv *priv;
477 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
479 priv = GET_PRIV (store);
481 return priv->show_avatars;
485 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
486 gboolean show_avatars)
488 EmpathyContactListStorePriv *priv;
491 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
493 priv = GET_PRIV (store);
495 priv->show_avatars = show_avatars;
497 model = GTK_TREE_MODEL (store);
499 gtk_tree_model_foreach (model,
500 (GtkTreeModelForeachFunc)
501 contact_list_store_update_list_mode_foreach,
504 g_object_notify (G_OBJECT (store), "show-avatars");
508 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
510 EmpathyContactListStorePriv *priv;
512 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
514 priv = GET_PRIV (store);
516 return priv->show_groups;
520 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
521 gboolean show_groups)
523 EmpathyContactListStorePriv *priv;
526 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
528 priv = GET_PRIV (store);
530 if (priv->show_groups == show_groups) {
534 priv->show_groups = show_groups;
536 /* Remove all contacts and add them back, not optimized but that's the
538 gtk_tree_store_clear (GTK_TREE_STORE (store));
539 contacts = empathy_contact_list_get_members (priv->list);
540 for (l = contacts; l; l = l->next) {
541 contact_list_store_members_changed_cb (priv->list, l->data,
546 g_object_unref (l->data);
548 g_list_free (contacts);
550 g_object_notify (G_OBJECT (store), "show-groups");
554 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
556 EmpathyContactListStorePriv *priv;
558 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
560 priv = GET_PRIV (store);
562 return priv->is_compact;
566 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
569 EmpathyContactListStorePriv *priv;
572 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
574 priv = GET_PRIV (store);
576 priv->is_compact = is_compact;
578 model = GTK_TREE_MODEL (store);
580 gtk_tree_model_foreach (model,
581 (GtkTreeModelForeachFunc)
582 contact_list_store_update_list_mode_foreach,
585 g_object_notify (G_OBJECT (store), "is-compact");
588 EmpathyContactListStoreSort
589 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
591 EmpathyContactListStorePriv *priv;
593 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
595 priv = GET_PRIV (store);
597 return priv->sort_criterium;
601 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
602 EmpathyContactListStoreSort sort_criterium)
604 EmpathyContactListStorePriv *priv;
606 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
608 priv = GET_PRIV (store);
610 priv->sort_criterium = sort_criterium;
612 switch (sort_criterium) {
613 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
614 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
615 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
619 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
620 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
621 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
626 g_object_notify (G_OBJECT (store), "sort-criterium");
630 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
634 gboolean is_separator = FALSE;
636 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
638 gtk_tree_model_get (model, iter,
639 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
646 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
648 gboolean *path_is_group)
650 GtkTreeIter parent_iter, iter;
654 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
657 *path_is_group = FALSE;
660 if (!gtk_tree_model_get_iter (model, &iter, path)) {
664 gtk_tree_model_get (model, &iter,
665 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
666 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
673 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
679 gtk_tree_model_get (model, &iter,
680 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
681 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
690 *path_is_group = TRUE;
697 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
701 gpointer search_data)
703 gchar *name, *name_folded;
707 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
713 gtk_tree_model_get (model, iter,
714 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
721 name_folded = g_utf8_casefold (name, -1);
722 key_folded = g_utf8_casefold (key, -1);
724 if (name_folded && key_folded &&
725 strstr (name_folded, key_folded)) {
732 g_free (name_folded);
739 contact_list_store_setup (EmpathyContactListStore *store)
741 EmpathyContactListStorePriv *priv;
743 G_TYPE_STRING, /* Status icon-name */
744 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
745 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
746 G_TYPE_STRING, /* Name */
747 G_TYPE_STRING, /* Status string */
748 G_TYPE_BOOLEAN, /* Show status */
749 EMPATHY_TYPE_CONTACT, /* Contact type */
750 G_TYPE_BOOLEAN, /* Is group */
751 G_TYPE_BOOLEAN, /* Is active */
752 G_TYPE_BOOLEAN, /* Is online */
753 G_TYPE_BOOLEAN, /* Is separator */
754 G_TYPE_BOOLEAN, /* Can make audio calls */
755 G_TYPE_BOOLEAN, /* Can make video calls */
756 EMPATHY_TYPE_CONTACT_LIST_FLAGS, /* Flags */
759 priv = GET_PRIV (store);
761 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
762 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
766 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
767 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
768 contact_list_store_name_sort_func,
770 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
771 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
772 contact_list_store_state_sort_func,
775 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
776 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
780 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
782 EmpathyContactListStorePriv *priv;
784 priv = GET_PRIV (store);
786 priv->show_active = TRUE;
787 priv->inhibit_active = 0;
793 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
794 EmpathyContact *contact,
795 EmpathyContact *actor,
799 EmpathyContactListStore *store)
801 EmpathyContactListStorePriv *priv;
803 priv = GET_PRIV (store);
805 DEBUG ("Contact %s (%d) %s",
806 empathy_contact_get_id (contact),
807 empathy_contact_get_handle (contact),
808 is_member ? "added" : "removed");
811 g_signal_connect (contact, "notify::presence",
812 G_CALLBACK (contact_list_store_contact_updated_cb),
814 g_signal_connect (contact, "notify::presence-message",
815 G_CALLBACK (contact_list_store_contact_updated_cb),
817 g_signal_connect (contact, "notify::name",
818 G_CALLBACK (contact_list_store_contact_updated_cb),
820 g_signal_connect (contact, "notify::avatar",
821 G_CALLBACK (contact_list_store_contact_updated_cb),
823 g_signal_connect (contact, "notify::capabilities",
824 G_CALLBACK (contact_list_store_contact_updated_cb),
827 contact_list_store_add_contact (store, contact);
829 g_signal_handlers_disconnect_by_func (contact,
830 G_CALLBACK (contact_list_store_contact_updated_cb),
833 contact_list_store_remove_contact (store, contact);
838 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
839 EmpathyContact *contact,
842 EmpathyContactListStore *store)
844 EmpathyContactListStorePriv *priv;
845 gboolean show_active;
847 priv = GET_PRIV (store);
849 DEBUG ("Updating groups for contact %s (%d)",
850 empathy_contact_get_id (contact),
851 empathy_contact_get_handle (contact));
853 /* We do this to make sure the groups are correct, if not, we
854 * would have to check the groups already set up for each
855 * contact and then see what has been updated.
857 show_active = priv->show_active;
858 priv->show_active = FALSE;
859 contact_list_store_remove_contact (store, contact);
860 contact_list_store_add_contact (store, contact);
861 priv->show_active = show_active;
865 contact_list_store_add_contact (EmpathyContactListStore *store,
866 EmpathyContact *contact)
868 EmpathyContactListStorePriv *priv;
870 GList *groups = NULL, *l;
871 TpConnection *connection;
872 EmpathyContactListFlags flags = 0;
874 priv = GET_PRIV (store);
876 if (EMP_STR_EMPTY (empathy_contact_get_name (contact)) ||
877 (!priv->show_offline && !empathy_contact_is_online (contact))) {
881 if (priv->show_groups) {
882 groups = empathy_contact_list_get_groups (priv->list, contact);
885 connection = empathy_contact_get_connection (contact);
886 if (EMPATHY_IS_CONTACT_MANAGER (priv->list)) {
887 flags = empathy_contact_manager_get_flags_for_connection (
888 EMPATHY_CONTACT_MANAGER (priv->list), connection);
890 /* If no groups just add it at the top level. */
893 GtkTreeModel *model = GTK_TREE_MODEL (store);
895 if (gtk_tree_model_get_iter_first (model, &iter)) do {
898 gtk_tree_model_get (model, &iter,
899 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &c,
908 } while (gtk_tree_model_iter_next (model, &iter));
910 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
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_AUDIO_CALL,
917 empathy_contact_get_capabilities (contact) &
918 EMPATHY_CAPABILITIES_AUDIO,
919 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
920 empathy_contact_get_capabilities (contact) &
921 EMPATHY_CAPABILITIES_VIDEO,
922 EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
926 /* Else add to each group. */
927 for (l = groups; l; l = l->next) {
928 GtkTreeIter iter_group;
930 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
932 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
934 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
935 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
936 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
937 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
938 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
939 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
940 empathy_contact_get_capabilities (contact) &
941 EMPATHY_CAPABILITIES_AUDIO,
942 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
943 empathy_contact_get_capabilities (contact) &
944 EMPATHY_CAPABILITIES_VIDEO,
945 EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
949 g_list_free (groups);
951 contact_list_store_contact_update (store, contact);
956 contact_list_store_remove_contact (EmpathyContactListStore *store,
957 EmpathyContact *contact)
959 EmpathyContactListStorePriv *priv;
963 priv = GET_PRIV (store);
965 iters = contact_list_store_find_contact (store, contact);
971 model = GTK_TREE_MODEL (store);
973 for (l = iters; l; l = l->next) {
976 /* NOTE: it is only <= 2 here because we have
977 * separators after the group name, otherwise it
980 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
981 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
982 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
984 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
988 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
993 contact_list_store_contact_update (EmpathyContactListStore *store,
994 EmpathyContact *contact)
996 EmpathyContactListStorePriv *priv;
997 ShowActiveData *data;
1001 gboolean should_be_in_list;
1002 gboolean was_online = TRUE;
1003 gboolean now_online = FALSE;
1004 gboolean set_model = FALSE;
1005 gboolean do_remove = FALSE;
1006 gboolean do_set_active = FALSE;
1007 gboolean do_set_refresh = FALSE;
1008 gboolean show_avatar = FALSE;
1009 GdkPixbuf *pixbuf_avatar;
1011 priv = GET_PRIV (store);
1013 model = GTK_TREE_MODEL (store);
1015 iters = contact_list_store_find_contact (store, contact);
1022 /* Get online state now. */
1023 now_online = empathy_contact_is_online (contact);
1025 if (priv->show_offline || now_online) {
1026 should_be_in_list = TRUE;
1028 should_be_in_list = FALSE;
1031 if (!in_list && !should_be_in_list) {
1032 /* Nothing to do. */
1033 DEBUG ("Contact:'%s' in list:NO, should be:NO",
1034 empathy_contact_get_name (contact));
1036 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1037 g_list_free (iters);
1040 else if (in_list && !should_be_in_list) {
1041 DEBUG ("Contact:'%s' in list:YES, should be:NO",
1042 empathy_contact_get_name (contact));
1044 if (priv->show_active) {
1046 do_set_active = TRUE;
1047 do_set_refresh = TRUE;
1050 DEBUG ("Remove item (after timeout)");
1052 DEBUG ("Remove item (now)!");
1053 contact_list_store_remove_contact (store, contact);
1056 else if (!in_list && should_be_in_list) {
1057 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1058 empathy_contact_get_name (contact));
1060 contact_list_store_add_contact (store, contact);
1062 if (priv->show_active) {
1063 do_set_active = TRUE;
1065 DEBUG ("Set active (contact added)");
1068 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1069 empathy_contact_get_name (contact));
1071 /* Get online state before. */
1072 if (iters && g_list_length (iters) > 0) {
1073 gtk_tree_model_get (model, iters->data,
1074 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1078 /* Is this really an update or an online/offline. */
1079 if (priv->show_active) {
1080 if (was_online != now_online) {
1081 do_set_active = TRUE;
1082 do_set_refresh = TRUE;
1084 DEBUG ("Set active (contact updated %s)",
1085 was_online ? "online -> offline" :
1086 "offline -> online");
1088 /* Was TRUE for presence updates. */
1089 /* do_set_active = FALSE; */
1090 do_set_refresh = TRUE;
1092 DEBUG ("Set active (contact updated)");
1099 if (priv->show_avatars && !priv->is_compact) {
1102 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1103 for (l = iters; l && set_model; l = l->next) {
1104 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1105 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1106 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1107 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1108 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1109 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1110 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1111 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1112 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1113 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1114 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1115 empathy_contact_get_capabilities (contact) &
1116 EMPATHY_CAPABILITIES_AUDIO,
1117 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1118 empathy_contact_get_capabilities (contact) &
1119 EMPATHY_CAPABILITIES_VIDEO,
1123 if (pixbuf_avatar) {
1124 g_object_unref (pixbuf_avatar);
1127 if (priv->show_active && do_set_active) {
1128 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1130 if (do_set_active) {
1131 data = contact_list_store_contact_active_new (store, contact, do_remove);
1132 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1133 (GSourceFunc) contact_list_store_contact_active_cb,
1138 /* FIXME: when someone goes online then offline quickly, the
1139 * first timeout sets the user to be inactive and the second
1140 * timeout removes the user from the contact list, really we
1141 * should remove the first timeout.
1143 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1144 g_list_free (iters);
1148 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1150 EmpathyContactListStore *store)
1152 DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1153 empathy_contact_get_name (contact));
1155 contact_list_store_contact_update (store, contact);
1159 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1160 EmpathyContact *contact,
1162 gboolean set_changed)
1164 EmpathyContactListStorePriv *priv;
1165 GtkTreeModel *model;
1168 priv = GET_PRIV (store);
1169 model = GTK_TREE_MODEL (store);
1171 iters = contact_list_store_find_contact (store, contact);
1172 for (l = iters; l; l = l->next) {
1175 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1176 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1179 DEBUG ("Set item %s", active ? "active" : "inactive");
1182 path = gtk_tree_model_get_path (model, l->data);
1183 gtk_tree_model_row_changed (model, path, l->data);
1184 gtk_tree_path_free (path);
1188 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1189 g_list_free (iters);
1193 static ShowActiveData *
1194 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1195 EmpathyContact *contact,
1198 ShowActiveData *data;
1200 DEBUG ("Contact:'%s' now active, and %s be removed",
1201 empathy_contact_get_name (contact),
1202 remove ? "WILL" : "WILL NOT");
1204 data = g_slice_new0 (ShowActiveData);
1206 data->store = g_object_ref (store);
1207 data->contact = g_object_ref (contact);
1208 data->remove = remove;
1214 contact_list_store_contact_active_free (ShowActiveData *data)
1216 g_object_unref (data->contact);
1217 g_object_unref (data->store);
1219 g_slice_free (ShowActiveData, data);
1223 contact_list_store_contact_active_cb (ShowActiveData *data)
1225 EmpathyContactListStorePriv *priv;
1227 priv = GET_PRIV (data->store);
1230 !priv->show_offline &&
1231 !empathy_contact_is_online (data->contact)) {
1232 DEBUG ("Contact:'%s' active timeout, removing item",
1233 empathy_contact_get_name (data->contact));
1234 contact_list_store_remove_contact (data->store, data->contact);
1237 DEBUG ("Contact:'%s' no longer active",
1238 empathy_contact_get_name (data->contact));
1240 contact_list_store_contact_set_active (data->store,
1245 contact_list_store_contact_active_free (data);
1251 contact_list_store_get_group_foreach (GtkTreeModel *model,
1259 /* Groups are only at the top level. */
1260 if (gtk_tree_path_get_depth (path) != 1) {
1264 gtk_tree_model_get (model, iter,
1265 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1266 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1269 if (is_group && !tp_strdiff (str, fg->name)) {
1280 contact_list_store_get_group (EmpathyContactListStore *store,
1282 GtkTreeIter *iter_group_to_set,
1283 GtkTreeIter *iter_separator_to_set,
1286 EmpathyContactListStorePriv *priv;
1287 GtkTreeModel *model;
1288 GtkTreeIter iter_group;
1289 GtkTreeIter iter_separator;
1292 priv = GET_PRIV (store);
1294 memset (&fg, 0, sizeof (fg));
1298 model = GTK_TREE_MODEL (store);
1299 gtk_tree_model_foreach (model,
1300 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1308 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1309 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1310 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1311 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1312 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1313 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1314 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1317 if (iter_group_to_set) {
1318 *iter_group_to_set = iter_group;
1321 gtk_tree_store_append (GTK_TREE_STORE (store),
1324 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1325 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1328 if (iter_separator_to_set) {
1329 *iter_separator_to_set = iter_separator;
1336 if (iter_group_to_set) {
1337 *iter_group_to_set = fg.iter;
1340 iter_separator = fg.iter;
1342 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1343 gboolean is_separator;
1345 gtk_tree_model_get (model, &iter_separator,
1346 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1349 if (is_separator && iter_separator_to_set) {
1350 *iter_separator_to_set = iter_separator;
1357 contact_list_store_state_sort_func (GtkTreeModel *model,
1358 GtkTreeIter *iter_a,
1359 GtkTreeIter *iter_b,
1363 gchar *name_a, *name_b;
1364 gboolean is_separator_a, is_separator_b;
1365 EmpathyContact *contact_a, *contact_b;
1367 gtk_tree_model_get (model, iter_a,
1368 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1369 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1370 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1372 gtk_tree_model_get (model, iter_b,
1373 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1374 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1375 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1378 /* Separator or group? */
1379 if (is_separator_a || is_separator_b) {
1380 if (is_separator_a) {
1382 } else if (is_separator_b) {
1385 } else if (!contact_a && contact_b) {
1387 } else if (contact_a && !contact_b) {
1389 } else if (!contact_a && !contact_b) {
1391 ret_val = g_utf8_collate (name_a, name_b);
1398 /* If we managed to get this far, we can start looking at
1401 ret_val = -tp_connection_presence_type_cmp_availability (
1402 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1403 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1406 /* Fallback: compare by name */
1407 ret_val = g_utf8_collate (name_a, name_b);
1415 g_object_unref (contact_a);
1419 g_object_unref (contact_b);
1426 contact_list_store_name_sort_func (GtkTreeModel *model,
1427 GtkTreeIter *iter_a,
1428 GtkTreeIter *iter_b,
1431 gchar *name_a, *name_b;
1432 EmpathyContact *contact_a, *contact_b;
1433 gboolean is_separator_a, is_separator_b;
1436 gtk_tree_model_get (model, iter_a,
1437 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1438 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1439 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1441 gtk_tree_model_get (model, iter_b,
1442 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1443 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1444 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1447 /* If contact is NULL it means it's a group. */
1449 if (is_separator_a || is_separator_b) {
1450 if (is_separator_a) {
1452 } else if (is_separator_b) {
1455 } else if (!contact_a && contact_b) {
1457 } else if (contact_a && !contact_b) {
1460 ret_val = g_utf8_collate (name_a, name_b);
1467 g_object_unref (contact_a);
1471 g_object_unref (contact_b);
1478 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1483 EmpathyContact *contact;
1485 gtk_tree_model_get (model, iter,
1486 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1489 if (contact == fc->contact) {
1491 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1495 g_object_unref (contact);
1502 contact_list_store_find_contact (EmpathyContactListStore *store,
1503 EmpathyContact *contact)
1505 EmpathyContactListStorePriv *priv;
1506 GtkTreeModel *model;
1510 priv = GET_PRIV (store);
1512 memset (&fc, 0, sizeof (fc));
1514 fc.contact = contact;
1516 model = GTK_TREE_MODEL (store);
1517 gtk_tree_model_foreach (model,
1518 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1529 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1532 EmpathyContactListStore *store)
1534 EmpathyContactListStorePriv *priv;
1535 gboolean show_avatar = FALSE;
1537 priv = GET_PRIV (store);
1539 if (priv->show_avatars && !priv->is_compact) {
1543 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1544 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1545 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,