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 "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 make audio calls */
740 G_TYPE_BOOLEAN}; /* Can make video calls */
742 priv = GET_PRIV (store);
744 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
745 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
749 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
750 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
751 contact_list_store_name_sort_func,
753 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
754 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
755 contact_list_store_state_sort_func,
758 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
759 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
763 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
765 EmpathyContactListStorePriv *priv;
767 priv = GET_PRIV (store);
769 priv->show_active = TRUE;
770 priv->inhibit_active = 0;
776 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
777 EmpathyContact *contact,
778 EmpathyContact *actor,
782 EmpathyContactListStore *store)
784 EmpathyContactListStorePriv *priv;
786 priv = GET_PRIV (store);
788 DEBUG ("Contact %s (%d) %s",
789 empathy_contact_get_id (contact),
790 empathy_contact_get_handle (contact),
791 is_member ? "added" : "removed");
794 g_signal_connect (contact, "notify::presence",
795 G_CALLBACK (contact_list_store_contact_updated_cb),
797 g_signal_connect (contact, "notify::presence-message",
798 G_CALLBACK (contact_list_store_contact_updated_cb),
800 g_signal_connect (contact, "notify::name",
801 G_CALLBACK (contact_list_store_contact_updated_cb),
803 g_signal_connect (contact, "notify::avatar",
804 G_CALLBACK (contact_list_store_contact_updated_cb),
806 g_signal_connect (contact, "notify::capabilities",
807 G_CALLBACK (contact_list_store_contact_updated_cb),
810 contact_list_store_add_contact (store, contact);
812 g_signal_handlers_disconnect_by_func (contact,
813 G_CALLBACK (contact_list_store_contact_updated_cb),
816 contact_list_store_remove_contact (store, contact);
821 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
822 EmpathyContact *contact,
825 EmpathyContactListStore *store)
827 EmpathyContactListStorePriv *priv;
828 gboolean show_active;
830 priv = GET_PRIV (store);
832 DEBUG ("Updating groups for contact %s (%d)",
833 empathy_contact_get_id (contact),
834 empathy_contact_get_handle (contact));
836 /* We do this to make sure the groups are correct, if not, we
837 * would have to check the groups already set up for each
838 * contact and then see what has been updated.
840 show_active = priv->show_active;
841 priv->show_active = FALSE;
842 contact_list_store_remove_contact (store, contact);
843 contact_list_store_add_contact (store, contact);
844 priv->show_active = show_active;
848 contact_list_store_add_contact (EmpathyContactListStore *store,
849 EmpathyContact *contact)
851 EmpathyContactListStorePriv *priv;
853 GList *groups = NULL, *l;
855 priv = GET_PRIV (store);
857 if (EMP_STR_EMPTY (empathy_contact_get_name (contact)) ||
858 (!priv->show_offline && !empathy_contact_is_online (contact))) {
862 if (priv->show_groups) {
863 groups = empathy_contact_list_get_groups (priv->list, contact);
866 /* If no groups just add it at the top level. */
868 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
869 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
870 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
871 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
872 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
873 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
874 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
875 empathy_contact_get_capabilities (contact) &
876 EMPATHY_CAPABILITIES_AUDIO,
877 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
878 empathy_contact_get_capabilities (contact) &
879 EMPATHY_CAPABILITIES_VIDEO,
883 /* Else add to each group. */
884 for (l = groups; l; l = l->next) {
885 GtkTreeIter iter_group;
887 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
889 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
891 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
892 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
893 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
894 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
895 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
896 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
897 empathy_contact_get_capabilities (contact) &
898 EMPATHY_CAPABILITIES_AUDIO,
899 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
900 empathy_contact_get_capabilities (contact) &
901 EMPATHY_CAPABILITIES_VIDEO,
905 g_list_free (groups);
907 contact_list_store_contact_update (store, contact);
912 contact_list_store_remove_contact (EmpathyContactListStore *store,
913 EmpathyContact *contact)
915 EmpathyContactListStorePriv *priv;
919 priv = GET_PRIV (store);
921 iters = contact_list_store_find_contact (store, contact);
927 model = GTK_TREE_MODEL (store);
929 for (l = iters; l; l = l->next) {
932 /* NOTE: it is only <= 2 here because we have
933 * separators after the group name, otherwise it
936 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
937 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
938 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
940 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
944 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
949 contact_list_store_contact_update (EmpathyContactListStore *store,
950 EmpathyContact *contact)
952 EmpathyContactListStorePriv *priv;
953 ShowActiveData *data;
957 gboolean should_be_in_list;
958 gboolean was_online = TRUE;
959 gboolean now_online = FALSE;
960 gboolean set_model = FALSE;
961 gboolean do_remove = FALSE;
962 gboolean do_set_active = FALSE;
963 gboolean do_set_refresh = FALSE;
964 gboolean show_avatar = FALSE;
965 GdkPixbuf *pixbuf_avatar;
967 priv = GET_PRIV (store);
969 model = GTK_TREE_MODEL (store);
971 iters = contact_list_store_find_contact (store, contact);
978 /* Get online state now. */
979 now_online = empathy_contact_is_online (contact);
981 if (priv->show_offline || now_online) {
982 should_be_in_list = TRUE;
984 should_be_in_list = FALSE;
987 if (!in_list && !should_be_in_list) {
989 DEBUG ("Contact:'%s' in list:NO, should be:NO",
990 empathy_contact_get_name (contact));
992 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
996 else if (in_list && !should_be_in_list) {
997 DEBUG ("Contact:'%s' in list:YES, should be:NO",
998 empathy_contact_get_name (contact));
1000 if (priv->show_active) {
1002 do_set_active = TRUE;
1003 do_set_refresh = TRUE;
1006 DEBUG ("Remove item (after timeout)");
1008 DEBUG ("Remove item (now)!");
1009 contact_list_store_remove_contact (store, contact);
1012 else if (!in_list && should_be_in_list) {
1013 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1014 empathy_contact_get_name (contact));
1016 contact_list_store_add_contact (store, contact);
1018 if (priv->show_active) {
1019 do_set_active = TRUE;
1021 DEBUG ("Set active (contact added)");
1024 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1025 empathy_contact_get_name (contact));
1027 /* Get online state before. */
1028 if (iters && g_list_length (iters) > 0) {
1029 gtk_tree_model_get (model, iters->data,
1030 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1034 /* Is this really an update or an online/offline. */
1035 if (priv->show_active) {
1036 if (was_online != now_online) {
1037 do_set_active = TRUE;
1038 do_set_refresh = TRUE;
1040 DEBUG ("Set active (contact updated %s)",
1041 was_online ? "online -> offline" :
1042 "offline -> online");
1044 /* Was TRUE for presence updates. */
1045 /* do_set_active = FALSE; */
1046 do_set_refresh = TRUE;
1048 DEBUG ("Set active (contact updated)");
1055 if (priv->show_avatars && !priv->is_compact) {
1058 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1059 for (l = iters; l && set_model; l = l->next) {
1060 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1061 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1062 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1063 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1064 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1065 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1066 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1067 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1068 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1069 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1070 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1071 empathy_contact_get_capabilities (contact) &
1072 EMPATHY_CAPABILITIES_AUDIO,
1073 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1074 empathy_contact_get_capabilities (contact) &
1075 EMPATHY_CAPABILITIES_VIDEO,
1079 if (pixbuf_avatar) {
1080 g_object_unref (pixbuf_avatar);
1083 if (priv->show_active && do_set_active) {
1084 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1086 if (do_set_active) {
1087 data = contact_list_store_contact_active_new (store, contact, do_remove);
1088 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1089 (GSourceFunc) contact_list_store_contact_active_cb,
1094 /* FIXME: when someone goes online then offline quickly, the
1095 * first timeout sets the user to be inactive and the second
1096 * timeout removes the user from the contact list, really we
1097 * should remove the first timeout.
1099 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1100 g_list_free (iters);
1104 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1106 EmpathyContactListStore *store)
1108 DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1109 empathy_contact_get_name (contact));
1111 contact_list_store_contact_update (store, contact);
1115 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1116 EmpathyContact *contact,
1118 gboolean set_changed)
1120 EmpathyContactListStorePriv *priv;
1121 GtkTreeModel *model;
1124 priv = GET_PRIV (store);
1125 model = GTK_TREE_MODEL (store);
1127 iters = contact_list_store_find_contact (store, contact);
1128 for (l = iters; l; l = l->next) {
1131 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1132 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1135 DEBUG ("Set item %s", active ? "active" : "inactive");
1138 path = gtk_tree_model_get_path (model, l->data);
1139 gtk_tree_model_row_changed (model, path, l->data);
1140 gtk_tree_path_free (path);
1144 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1145 g_list_free (iters);
1149 static ShowActiveData *
1150 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1151 EmpathyContact *contact,
1154 ShowActiveData *data;
1156 DEBUG ("Contact:'%s' now active, and %s be removed",
1157 empathy_contact_get_name (contact),
1158 remove ? "WILL" : "WILL NOT");
1160 data = g_slice_new0 (ShowActiveData);
1162 data->store = g_object_ref (store);
1163 data->contact = g_object_ref (contact);
1164 data->remove = remove;
1170 contact_list_store_contact_active_free (ShowActiveData *data)
1172 g_object_unref (data->contact);
1173 g_object_unref (data->store);
1175 g_slice_free (ShowActiveData, data);
1179 contact_list_store_contact_active_cb (ShowActiveData *data)
1181 EmpathyContactListStorePriv *priv;
1183 priv = GET_PRIV (data->store);
1186 !priv->show_offline &&
1187 !empathy_contact_is_online (data->contact)) {
1188 DEBUG ("Contact:'%s' active timeout, removing item",
1189 empathy_contact_get_name (data->contact));
1190 contact_list_store_remove_contact (data->store, data->contact);
1193 DEBUG ("Contact:'%s' no longer active",
1194 empathy_contact_get_name (data->contact));
1196 contact_list_store_contact_set_active (data->store,
1201 contact_list_store_contact_active_free (data);
1207 contact_list_store_get_group_foreach (GtkTreeModel *model,
1215 /* Groups are only at the top level. */
1216 if (gtk_tree_path_get_depth (path) != 1) {
1220 gtk_tree_model_get (model, iter,
1221 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1222 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1225 if (is_group && !tp_strdiff (str, fg->name)) {
1236 contact_list_store_get_group (EmpathyContactListStore *store,
1238 GtkTreeIter *iter_group_to_set,
1239 GtkTreeIter *iter_separator_to_set,
1242 EmpathyContactListStorePriv *priv;
1243 GtkTreeModel *model;
1244 GtkTreeIter iter_group;
1245 GtkTreeIter iter_separator;
1248 priv = GET_PRIV (store);
1250 memset (&fg, 0, sizeof (fg));
1254 model = GTK_TREE_MODEL (store);
1255 gtk_tree_model_foreach (model,
1256 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1264 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1265 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1266 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1267 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1268 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1269 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1270 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1273 if (iter_group_to_set) {
1274 *iter_group_to_set = iter_group;
1277 gtk_tree_store_append (GTK_TREE_STORE (store),
1280 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1281 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1284 if (iter_separator_to_set) {
1285 *iter_separator_to_set = iter_separator;
1292 if (iter_group_to_set) {
1293 *iter_group_to_set = fg.iter;
1296 iter_separator = fg.iter;
1298 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1299 gboolean is_separator;
1301 gtk_tree_model_get (model, &iter_separator,
1302 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1305 if (is_separator && iter_separator_to_set) {
1306 *iter_separator_to_set = iter_separator;
1313 contact_list_store_state_sort_func (GtkTreeModel *model,
1314 GtkTreeIter *iter_a,
1315 GtkTreeIter *iter_b,
1319 gchar *name_a, *name_b;
1320 gboolean is_separator_a, is_separator_b;
1321 EmpathyContact *contact_a, *contact_b;
1323 gtk_tree_model_get (model, iter_a,
1324 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1325 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1326 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1328 gtk_tree_model_get (model, iter_b,
1329 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1330 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1331 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1334 /* Separator or group? */
1335 if (is_separator_a || is_separator_b) {
1336 if (is_separator_a) {
1338 } else if (is_separator_b) {
1341 } else if (!contact_a && contact_b) {
1343 } else if (contact_a && !contact_b) {
1345 } else if (!contact_a && !contact_b) {
1347 ret_val = g_utf8_collate (name_a, name_b);
1354 /* If we managed to get this far, we can start looking at
1357 ret_val = -tp_connection_presence_type_cmp_availability (
1358 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1359 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1362 /* Fallback: compare by name */
1363 ret_val = g_utf8_collate (name_a, name_b);
1371 g_object_unref (contact_a);
1375 g_object_unref (contact_b);
1382 contact_list_store_name_sort_func (GtkTreeModel *model,
1383 GtkTreeIter *iter_a,
1384 GtkTreeIter *iter_b,
1387 gchar *name_a, *name_b;
1388 EmpathyContact *contact_a, *contact_b;
1389 gboolean is_separator_a, is_separator_b;
1392 gtk_tree_model_get (model, iter_a,
1393 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1394 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1395 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1397 gtk_tree_model_get (model, iter_b,
1398 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1399 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1400 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1403 /* If contact is NULL it means it's a group. */
1405 if (is_separator_a || is_separator_b) {
1406 if (is_separator_a) {
1408 } else if (is_separator_b) {
1411 } else if (!contact_a && contact_b) {
1413 } else if (contact_a && !contact_b) {
1416 ret_val = g_utf8_collate (name_a, name_b);
1423 g_object_unref (contact_a);
1427 g_object_unref (contact_b);
1434 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1439 EmpathyContact *contact;
1441 gtk_tree_model_get (model, iter,
1442 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1445 if (contact == fc->contact) {
1447 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1451 g_object_unref (contact);
1458 contact_list_store_find_contact (EmpathyContactListStore *store,
1459 EmpathyContact *contact)
1461 EmpathyContactListStorePriv *priv;
1462 GtkTreeModel *model;
1466 priv = GET_PRIV (store);
1468 memset (&fc, 0, sizeof (fc));
1470 fc.contact = contact;
1472 model = GTK_TREE_MODEL (store);
1473 gtk_tree_model_foreach (model,
1474 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1485 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1488 EmpathyContactListStore *store)
1490 EmpathyContactListStorePriv *priv;
1491 gboolean show_avatar = FALSE;
1493 priv = GET_PRIV (store);
1495 if (priv->show_avatars && !priv->is_compact) {
1499 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1500 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1501 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,