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 <telepathy-glib/util.h>
35 #include <libempathy/empathy-utils.h>
36 #include "empathy-contact-list-store.h"
37 #include "empathy-ui-utils.h"
38 #include "empathy-gtk-enum-types.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
41 #include <libempathy/empathy-debug.h>
43 /* Active users are those which have recently changed state
44 * (e.g. online, offline or from normal to a busy state).
47 /* Time in seconds user is shown as active */
48 #define ACTIVE_USER_SHOW_TIME 7
50 /* Time in seconds after connecting which we wait before active users are enabled */
51 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListStore)
55 EmpathyContactList *list;
56 gboolean show_offline;
57 gboolean show_avatars;
61 EmpathyContactListStoreSort sort_criterium;
63 } EmpathyContactListStorePriv;
72 EmpathyContact *contact;
78 EmpathyContactListStore *store;
79 EmpathyContact *contact;
83 static void contact_list_store_finalize (GObject *object);
84 static void contact_list_store_get_property (GObject *object,
88 static void contact_list_store_set_property (GObject *object,
92 static void contact_list_store_setup (EmpathyContactListStore *store);
93 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
94 static void contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
95 EmpathyContact *contact,
96 EmpathyContact *actor,
100 EmpathyContactListStore *store);
101 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
102 EmpathyContact *contact,
105 EmpathyContactListStore *store);
106 static void contact_list_store_add_contact (EmpathyContactListStore *store,
107 EmpathyContact *contact);
108 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
109 EmpathyContact *contact);
110 static void contact_list_store_contact_update (EmpathyContactListStore *store,
111 EmpathyContact *contact);
112 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
114 EmpathyContactListStore *store);
115 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
116 EmpathyContact *contact,
118 gboolean set_changed);
119 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
120 EmpathyContact *contact,
122 static void contact_list_store_contact_active_free (ShowActiveData *data);
123 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
124 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
128 static void contact_list_store_get_group (EmpathyContactListStore *store,
130 GtkTreeIter *iter_group_to_set,
131 GtkTreeIter *iter_separator_to_set,
133 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
137 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
141 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
145 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
146 EmpathyContact *contact);
147 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
150 EmpathyContactListStore *store);
162 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
165 contact_list_store_iface_setup (gpointer user_data)
167 EmpathyContactListStore *store = user_data;
168 EmpathyContactListStorePriv *priv = GET_PRIV (store);
171 /* Signal connection. */
172 g_signal_connect (priv->list,
174 G_CALLBACK (contact_list_store_members_changed_cb),
176 g_signal_connect (priv->list,
178 G_CALLBACK (contact_list_store_groups_changed_cb),
181 /* Add contacts already created. */
182 contacts = empathy_contact_list_get_members (priv->list);
183 for (l = contacts; l; l = l->next) {
184 contact_list_store_members_changed_cb (priv->list, l->data,
189 g_object_unref (l->data);
191 g_list_free (contacts);
198 contact_list_store_set_contact_list (EmpathyContactListStore *store,
199 EmpathyContactList *list_iface)
201 EmpathyContactListStorePriv *priv = GET_PRIV (store);
203 priv->list = g_object_ref (list_iface);
205 /* Let a chance to have all properties set before populating */
206 g_idle_add (contact_list_store_iface_setup, store);
210 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
212 GObjectClass *object_class = G_OBJECT_CLASS (klass);
214 object_class->finalize = contact_list_store_finalize;
215 object_class->get_property = contact_list_store_get_property;
216 object_class->set_property = contact_list_store_set_property;
218 g_object_class_install_property (object_class,
220 g_param_spec_object ("contact-list",
221 "The contact list iface",
222 "The contact list iface",
223 EMPATHY_TYPE_CONTACT_LIST,
224 G_PARAM_CONSTRUCT_ONLY |
226 g_object_class_install_property (object_class,
228 g_param_spec_boolean ("show-offline",
230 "Whether contact list should display "
234 g_object_class_install_property (object_class,
236 g_param_spec_boolean ("show-avatars",
238 "Whether contact list should display "
239 "avatars for contacts",
242 g_object_class_install_property (object_class,
244 g_param_spec_boolean ("show-groups",
246 "Whether contact list should display "
250 g_object_class_install_property (object_class,
252 g_param_spec_boolean ("is-compact",
254 "Whether the contact list is in compact mode or not",
258 g_object_class_install_property (object_class,
260 g_param_spec_enum ("sort-criterium",
262 "The sort criterium to use for sorting the contact list",
263 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
264 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
267 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
271 empathy_contact_list_store_init (EmpathyContactListStore *store)
273 EmpathyContactListStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (store,
274 EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv);
277 priv->show_avatars = TRUE;
278 priv->show_groups = TRUE;
279 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
280 (GSourceFunc) contact_list_store_inibit_active_cb,
282 contact_list_store_setup (store);
286 contact_list_store_finalize (GObject *object)
288 EmpathyContactListStorePriv *priv = GET_PRIV (object);
291 contacts = empathy_contact_list_get_members (priv->list);
292 for (l = contacts; l; l = l->next) {
293 g_signal_handlers_disconnect_by_func (l->data,
294 G_CALLBACK (contact_list_store_contact_updated_cb),
297 g_object_unref (l->data);
299 g_list_free (contacts);
301 g_signal_handlers_disconnect_by_func (priv->list,
302 G_CALLBACK (contact_list_store_members_changed_cb),
304 g_signal_handlers_disconnect_by_func (priv->list,
305 G_CALLBACK (contact_list_store_groups_changed_cb),
307 g_object_unref (priv->list);
309 if (priv->inhibit_active) {
310 g_source_remove (priv->inhibit_active);
313 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
317 contact_list_store_get_property (GObject *object,
322 EmpathyContactListStorePriv *priv;
324 priv = GET_PRIV (object);
327 case PROP_CONTACT_LIST:
328 g_value_set_object (value, priv->list);
330 case PROP_SHOW_OFFLINE:
331 g_value_set_boolean (value, priv->show_offline);
333 case PROP_SHOW_AVATARS:
334 g_value_set_boolean (value, priv->show_avatars);
336 case PROP_SHOW_GROUPS:
337 g_value_set_boolean (value, priv->show_groups);
339 case PROP_IS_COMPACT:
340 g_value_set_boolean (value, priv->is_compact);
342 case PROP_SORT_CRITERIUM:
343 g_value_set_enum (value, priv->sort_criterium);
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
352 contact_list_store_set_property (GObject *object,
357 EmpathyContactListStorePriv *priv;
359 priv = GET_PRIV (object);
362 case PROP_CONTACT_LIST:
363 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
364 g_value_get_object (value));
366 case PROP_SHOW_OFFLINE:
367 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
368 g_value_get_boolean (value));
370 case PROP_SHOW_AVATARS:
371 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
372 g_value_get_boolean (value));
374 case PROP_SHOW_GROUPS:
375 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
376 g_value_get_boolean (value));
378 case PROP_IS_COMPACT:
379 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
380 g_value_get_boolean (value));
382 case PROP_SORT_CRITERIUM:
383 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
384 g_value_get_enum (value));
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
392 EmpathyContactListStore *
393 empathy_contact_list_store_new (EmpathyContactList *list_iface)
395 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
397 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
398 "contact-list", list_iface,
403 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
405 EmpathyContactListStorePriv *priv;
407 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
409 priv = GET_PRIV (store);
415 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
417 EmpathyContactListStorePriv *priv;
419 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
421 priv = GET_PRIV (store);
423 return priv->show_offline;
427 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
428 gboolean show_offline)
430 EmpathyContactListStorePriv *priv;
432 gboolean show_active;
434 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
436 priv = GET_PRIV (store);
438 priv->show_offline = show_offline;
439 show_active = priv->show_active;
441 /* Disable temporarily. */
442 priv->show_active = FALSE;
444 contacts = empathy_contact_list_get_members (priv->list);
445 for (l = contacts; l; l = l->next) {
446 contact_list_store_contact_update (store, l->data);
448 g_object_unref (l->data);
450 g_list_free (contacts);
452 /* Restore to original setting. */
453 priv->show_active = show_active;
455 g_object_notify (G_OBJECT (store), "show-offline");
459 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
461 EmpathyContactListStorePriv *priv;
463 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
465 priv = GET_PRIV (store);
467 return priv->show_avatars;
471 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
472 gboolean show_avatars)
474 EmpathyContactListStorePriv *priv;
477 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
479 priv = GET_PRIV (store);
481 priv->show_avatars = show_avatars;
483 model = GTK_TREE_MODEL (store);
485 gtk_tree_model_foreach (model,
486 (GtkTreeModelForeachFunc)
487 contact_list_store_update_list_mode_foreach,
490 g_object_notify (G_OBJECT (store), "show-avatars");
494 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
496 EmpathyContactListStorePriv *priv;
498 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
500 priv = GET_PRIV (store);
502 return priv->show_groups;
506 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
507 gboolean show_groups)
509 EmpathyContactListStorePriv *priv;
512 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
514 priv = GET_PRIV (store);
516 if (priv->show_groups == show_groups) {
520 priv->show_groups = show_groups;
522 /* Remove all contacts and add them back, not optimized but that's the
524 gtk_tree_store_clear (GTK_TREE_STORE (store));
525 contacts = empathy_contact_list_get_members (priv->list);
526 for (l = contacts; l; l = l->next) {
527 contact_list_store_members_changed_cb (priv->list, l->data,
532 g_object_unref (l->data);
534 g_list_free (contacts);
536 g_object_notify (G_OBJECT (store), "show-groups");
540 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
542 EmpathyContactListStorePriv *priv;
544 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
546 priv = GET_PRIV (store);
548 return priv->is_compact;
552 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
555 EmpathyContactListStorePriv *priv;
558 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
560 priv = GET_PRIV (store);
562 priv->is_compact = is_compact;
564 model = GTK_TREE_MODEL (store);
566 gtk_tree_model_foreach (model,
567 (GtkTreeModelForeachFunc)
568 contact_list_store_update_list_mode_foreach,
571 g_object_notify (G_OBJECT (store), "is-compact");
574 EmpathyContactListStoreSort
575 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
577 EmpathyContactListStorePriv *priv;
579 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
581 priv = GET_PRIV (store);
583 return priv->sort_criterium;
587 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
588 EmpathyContactListStoreSort sort_criterium)
590 EmpathyContactListStorePriv *priv;
592 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
594 priv = GET_PRIV (store);
596 priv->sort_criterium = sort_criterium;
598 switch (sort_criterium) {
599 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
600 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
601 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
605 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
606 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
607 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
612 g_object_notify (G_OBJECT (store), "sort-criterium");
616 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
620 gboolean is_separator = FALSE;
622 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
624 gtk_tree_model_get (model, iter,
625 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
632 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
634 gboolean *path_is_group)
636 GtkTreeIter parent_iter, iter;
640 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
643 *path_is_group = FALSE;
646 if (!gtk_tree_model_get_iter (model, &iter, path)) {
650 gtk_tree_model_get (model, &iter,
651 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
652 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
659 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
665 gtk_tree_model_get (model, &iter,
666 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
667 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
676 *path_is_group = TRUE;
683 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
687 gpointer search_data)
689 gchar *name, *name_folded;
693 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
699 gtk_tree_model_get (model, iter,
700 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
707 name_folded = g_utf8_casefold (name, -1);
708 key_folded = g_utf8_casefold (key, -1);
710 if (name_folded && key_folded &&
711 strstr (name_folded, key_folded)) {
718 g_free (name_folded);
725 contact_list_store_setup (EmpathyContactListStore *store)
727 EmpathyContactListStorePriv *priv;
728 GType types[] = {G_TYPE_STRING, /* Status icon-name */
729 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
730 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
731 G_TYPE_STRING, /* Name */
732 G_TYPE_STRING, /* Status string */
733 G_TYPE_BOOLEAN, /* Show status */
734 EMPATHY_TYPE_CONTACT, /* Contact type */
735 G_TYPE_BOOLEAN, /* Is group */
736 G_TYPE_BOOLEAN, /* Is active */
737 G_TYPE_BOOLEAN, /* Is online */
738 G_TYPE_BOOLEAN, /* Is separator */
739 G_TYPE_BOOLEAN}; /* Can VoIP */
741 priv = GET_PRIV (store);
743 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
744 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
748 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
749 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
750 contact_list_store_name_sort_func,
752 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
753 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
754 contact_list_store_state_sort_func,
757 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
758 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
762 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
764 EmpathyContactListStorePriv *priv;
766 priv = GET_PRIV (store);
768 priv->show_active = TRUE;
769 priv->inhibit_active = 0;
775 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
776 EmpathyContact *contact,
777 EmpathyContact *actor,
781 EmpathyContactListStore *store)
783 EmpathyContactListStorePriv *priv;
785 priv = GET_PRIV (store);
787 DEBUG ("Contact %s (%d) %s",
788 empathy_contact_get_id (contact),
789 empathy_contact_get_handle (contact),
790 is_member ? "added" : "removed");
793 g_signal_connect (contact, "notify::presence",
794 G_CALLBACK (contact_list_store_contact_updated_cb),
796 g_signal_connect (contact, "notify::presence-message",
797 G_CALLBACK (contact_list_store_contact_updated_cb),
799 g_signal_connect (contact, "notify::name",
800 G_CALLBACK (contact_list_store_contact_updated_cb),
802 g_signal_connect (contact, "notify::avatar",
803 G_CALLBACK (contact_list_store_contact_updated_cb),
805 g_signal_connect (contact, "notify::capabilities",
806 G_CALLBACK (contact_list_store_contact_updated_cb),
809 contact_list_store_add_contact (store, contact);
811 g_signal_handlers_disconnect_by_func (contact,
812 G_CALLBACK (contact_list_store_contact_updated_cb),
815 contact_list_store_remove_contact (store, contact);
820 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
821 EmpathyContact *contact,
824 EmpathyContactListStore *store)
826 EmpathyContactListStorePriv *priv;
827 gboolean show_active;
829 priv = GET_PRIV (store);
831 DEBUG ("Updating groups for contact %s (%d)",
832 empathy_contact_get_id (contact),
833 empathy_contact_get_handle (contact));
835 /* We do this to make sure the groups are correct, if not, we
836 * would have to check the groups already set up for each
837 * contact and then see what has been updated.
839 show_active = priv->show_active;
840 priv->show_active = FALSE;
841 contact_list_store_remove_contact (store, contact);
842 contact_list_store_add_contact (store, contact);
843 priv->show_active = show_active;
847 contact_list_store_add_contact (EmpathyContactListStore *store,
848 EmpathyContact *contact)
850 EmpathyContactListStorePriv *priv;
852 GList *groups = NULL, *l;
854 priv = GET_PRIV (store);
856 if (G_STR_EMPTY (empathy_contact_get_name (contact)) ||
857 (!priv->show_offline && !empathy_contact_is_online (contact))) {
861 if (priv->show_groups) {
862 groups = empathy_contact_list_get_groups (priv->list, contact);
865 /* If no groups just add it at the top level. */
867 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
868 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
869 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
870 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
871 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
872 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
873 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
877 /* Else add to each group. */
878 for (l = groups; l; l = l->next) {
879 GtkTreeIter iter_group;
881 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
883 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
885 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
886 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
887 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
888 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
889 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
890 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
894 g_list_free (groups);
896 contact_list_store_contact_update (store, contact);
901 contact_list_store_remove_contact (EmpathyContactListStore *store,
902 EmpathyContact *contact)
904 EmpathyContactListStorePriv *priv;
908 priv = GET_PRIV (store);
910 iters = contact_list_store_find_contact (store, contact);
916 model = GTK_TREE_MODEL (store);
918 for (l = iters; l; l = l->next) {
921 /* NOTE: it is only <= 2 here because we have
922 * separators after the group name, otherwise it
925 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
926 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
927 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
929 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
933 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
938 contact_list_store_contact_update (EmpathyContactListStore *store,
939 EmpathyContact *contact)
941 EmpathyContactListStorePriv *priv;
942 ShowActiveData *data;
946 gboolean should_be_in_list;
947 gboolean was_online = TRUE;
948 gboolean now_online = FALSE;
949 gboolean set_model = FALSE;
950 gboolean do_remove = FALSE;
951 gboolean do_set_active = FALSE;
952 gboolean do_set_refresh = FALSE;
953 gboolean show_avatar = FALSE;
954 GdkPixbuf *pixbuf_avatar;
956 priv = GET_PRIV (store);
958 model = GTK_TREE_MODEL (store);
960 iters = contact_list_store_find_contact (store, contact);
967 /* Get online state now. */
968 now_online = empathy_contact_is_online (contact);
970 if (priv->show_offline || now_online) {
971 should_be_in_list = TRUE;
973 should_be_in_list = FALSE;
976 if (!in_list && !should_be_in_list) {
978 DEBUG ("Contact:'%s' in list:NO, should be:NO",
979 empathy_contact_get_name (contact));
981 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
985 else if (in_list && !should_be_in_list) {
986 DEBUG ("Contact:'%s' in list:YES, should be:NO",
987 empathy_contact_get_name (contact));
989 if (priv->show_active) {
991 do_set_active = TRUE;
992 do_set_refresh = TRUE;
995 DEBUG ("Remove item (after timeout)");
997 DEBUG ("Remove item (now)!");
998 contact_list_store_remove_contact (store, contact);
1001 else if (!in_list && should_be_in_list) {
1002 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1003 empathy_contact_get_name (contact));
1005 contact_list_store_add_contact (store, contact);
1007 if (priv->show_active) {
1008 do_set_active = TRUE;
1010 DEBUG ("Set active (contact added)");
1013 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1014 empathy_contact_get_name (contact));
1016 /* Get online state before. */
1017 if (iters && g_list_length (iters) > 0) {
1018 gtk_tree_model_get (model, iters->data,
1019 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1023 /* Is this really an update or an online/offline. */
1024 if (priv->show_active) {
1025 if (was_online != now_online) {
1026 do_set_active = TRUE;
1027 do_set_refresh = TRUE;
1029 DEBUG ("Set active (contact updated %s)",
1030 was_online ? "online -> offline" :
1031 "offline -> online");
1033 /* Was TRUE for presence updates. */
1034 /* do_set_active = FALSE; */
1035 do_set_refresh = TRUE;
1037 DEBUG ("Set active (contact updated)");
1044 if (priv->show_avatars && !priv->is_compact) {
1047 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1048 for (l = iters; l && set_model; l = l->next) {
1049 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1050 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1051 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1052 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1053 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1054 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1055 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1056 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1057 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1058 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1059 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
1063 if (pixbuf_avatar) {
1064 g_object_unref (pixbuf_avatar);
1067 if (priv->show_active && do_set_active) {
1068 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1070 if (do_set_active) {
1071 data = contact_list_store_contact_active_new (store, contact, do_remove);
1072 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1073 (GSourceFunc) contact_list_store_contact_active_cb,
1078 /* FIXME: when someone goes online then offline quickly, the
1079 * first timeout sets the user to be inactive and the second
1080 * timeout removes the user from the contact list, really we
1081 * should remove the first timeout.
1083 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1084 g_list_free (iters);
1088 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1090 EmpathyContactListStore *store)
1092 DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1093 empathy_contact_get_name (contact));
1095 contact_list_store_contact_update (store, contact);
1099 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1100 EmpathyContact *contact,
1102 gboolean set_changed)
1104 EmpathyContactListStorePriv *priv;
1105 GtkTreeModel *model;
1108 priv = GET_PRIV (store);
1109 model = GTK_TREE_MODEL (store);
1111 iters = contact_list_store_find_contact (store, contact);
1112 for (l = iters; l; l = l->next) {
1115 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1116 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1119 DEBUG ("Set item %s", active ? "active" : "inactive");
1122 path = gtk_tree_model_get_path (model, l->data);
1123 gtk_tree_model_row_changed (model, path, l->data);
1124 gtk_tree_path_free (path);
1128 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1129 g_list_free (iters);
1133 static ShowActiveData *
1134 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1135 EmpathyContact *contact,
1138 ShowActiveData *data;
1140 DEBUG ("Contact:'%s' now active, and %s be removed",
1141 empathy_contact_get_name (contact),
1142 remove ? "WILL" : "WILL NOT");
1144 data = g_slice_new0 (ShowActiveData);
1146 data->store = g_object_ref (store);
1147 data->contact = g_object_ref (contact);
1148 data->remove = remove;
1154 contact_list_store_contact_active_free (ShowActiveData *data)
1156 g_object_unref (data->contact);
1157 g_object_unref (data->store);
1159 g_slice_free (ShowActiveData, data);
1163 contact_list_store_contact_active_cb (ShowActiveData *data)
1165 EmpathyContactListStorePriv *priv;
1167 priv = GET_PRIV (data->store);
1170 !priv->show_offline &&
1171 !empathy_contact_is_online (data->contact)) {
1172 DEBUG ("Contact:'%s' active timeout, removing item",
1173 empathy_contact_get_name (data->contact));
1174 contact_list_store_remove_contact (data->store, data->contact);
1177 DEBUG ("Contact:'%s' no longer active",
1178 empathy_contact_get_name (data->contact));
1180 contact_list_store_contact_set_active (data->store,
1185 contact_list_store_contact_active_free (data);
1191 contact_list_store_get_group_foreach (GtkTreeModel *model,
1199 /* Groups are only at the top level. */
1200 if (gtk_tree_path_get_depth (path) != 1) {
1204 gtk_tree_model_get (model, iter,
1205 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1206 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1209 if (is_group && !tp_strdiff (str, fg->name)) {
1220 contact_list_store_get_group (EmpathyContactListStore *store,
1222 GtkTreeIter *iter_group_to_set,
1223 GtkTreeIter *iter_separator_to_set,
1226 EmpathyContactListStorePriv *priv;
1227 GtkTreeModel *model;
1228 GtkTreeIter iter_group;
1229 GtkTreeIter iter_separator;
1232 priv = GET_PRIV (store);
1234 memset (&fg, 0, sizeof (fg));
1238 model = GTK_TREE_MODEL (store);
1239 gtk_tree_model_foreach (model,
1240 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1248 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1249 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1250 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1251 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1252 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1253 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1254 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1257 if (iter_group_to_set) {
1258 *iter_group_to_set = iter_group;
1261 gtk_tree_store_append (GTK_TREE_STORE (store),
1264 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1265 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1268 if (iter_separator_to_set) {
1269 *iter_separator_to_set = iter_separator;
1276 if (iter_group_to_set) {
1277 *iter_group_to_set = fg.iter;
1280 iter_separator = fg.iter;
1282 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1283 gboolean is_separator;
1285 gtk_tree_model_get (model, &iter_separator,
1286 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1289 if (is_separator && iter_separator_to_set) {
1290 *iter_separator_to_set = iter_separator;
1297 contact_list_store_ordered_presence (McPresence state)
1300 case MC_PRESENCE_UNSET:
1301 case MC_PRESENCE_OFFLINE:
1303 case MC_PRESENCE_AVAILABLE:
1305 case MC_PRESENCE_AWAY:
1307 case MC_PRESENCE_EXTENDED_AWAY:
1309 case MC_PRESENCE_HIDDEN:
1311 case MC_PRESENCE_DO_NOT_DISTURB:
1314 g_return_val_if_reached (6);
1319 contact_list_store_state_sort_func (GtkTreeModel *model,
1320 GtkTreeIter *iter_a,
1321 GtkTreeIter *iter_b,
1325 gchar *name_a, *name_b;
1326 gboolean is_separator_a, is_separator_b;
1327 EmpathyContact *contact_a, *contact_b;
1328 guint presence_a, presence_b;
1330 gtk_tree_model_get (model, iter_a,
1331 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1332 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1333 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1335 gtk_tree_model_get (model, iter_b,
1336 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1337 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1338 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1341 /* Separator or group? */
1342 if (is_separator_a || is_separator_b) {
1343 if (is_separator_a) {
1345 } else if (is_separator_b) {
1348 } else if (!contact_a && contact_b) {
1350 } else if (contact_a && !contact_b) {
1352 } else if (!contact_a && !contact_b) {
1354 ret_val = g_utf8_collate (name_a, name_b);
1361 /* If we managed to get this far, we can start looking at
1364 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1365 presence_a = contact_list_store_ordered_presence (presence_a);
1366 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1367 presence_b = contact_list_store_ordered_presence (presence_b);
1369 if (presence_a < presence_b) {
1371 } else if (presence_a > presence_b) {
1374 /* Fallback: compare by name */
1375 ret_val = g_utf8_collate (name_a, name_b);
1383 g_object_unref (contact_a);
1387 g_object_unref (contact_b);
1394 contact_list_store_name_sort_func (GtkTreeModel *model,
1395 GtkTreeIter *iter_a,
1396 GtkTreeIter *iter_b,
1399 gchar *name_a, *name_b;
1400 EmpathyContact *contact_a, *contact_b;
1401 gboolean is_separator_a, is_separator_b;
1404 gtk_tree_model_get (model, iter_a,
1405 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1406 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1407 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1409 gtk_tree_model_get (model, iter_b,
1410 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1411 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1412 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1415 /* If contact is NULL it means it's a group. */
1417 if (is_separator_a || is_separator_b) {
1418 if (is_separator_a) {
1420 } else if (is_separator_b) {
1423 } else if (!contact_a && contact_b) {
1425 } else if (contact_a && !contact_b) {
1428 ret_val = g_utf8_collate (name_a, name_b);
1435 g_object_unref (contact_a);
1439 g_object_unref (contact_b);
1446 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1451 EmpathyContact *contact;
1453 gtk_tree_model_get (model, iter,
1454 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1457 if (contact == fc->contact) {
1459 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1463 g_object_unref (contact);
1470 contact_list_store_find_contact (EmpathyContactListStore *store,
1471 EmpathyContact *contact)
1473 EmpathyContactListStorePriv *priv;
1474 GtkTreeModel *model;
1478 priv = GET_PRIV (store);
1480 memset (&fc, 0, sizeof (fc));
1482 fc.contact = contact;
1484 model = GTK_TREE_MODEL (store);
1485 gtk_tree_model_foreach (model,
1486 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1497 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1500 EmpathyContactListStore *store)
1502 EmpathyContactListStorePriv *priv;
1503 gboolean show_avatar = FALSE;
1505 priv = GET_PRIV (store);
1507 if (priv->show_avatars && !priv->is_compact) {
1511 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1512 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1513 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,