1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <libempathy/empathy-debug.h>
35 #include "empathy-contact-list-store.h"
36 #include "empathy-ui-utils.h"
37 #include "empathy-gtk-enum-types.h"
39 #define DEBUG_DOMAIN "ContactListStore"
41 /* Active users are those which have recently changed state
42 * (e.g. online, offline or from normal to a busy state).
45 /* Time in seconds user is shown as active */
46 #define ACTIVE_USER_SHOW_TIME 7
48 /* Time in seconds after connecting which we wait before active users are enabled */
49 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
54 EmpathyContactList *list;
55 gboolean show_offline;
56 gboolean show_avatars;
59 EmpathyContactListStoreSort sort_criterium;
61 } EmpathyContactListStorePriv;
70 EmpathyContact *contact;
76 EmpathyContactListStore *store;
77 EmpathyContact *contact;
81 static void empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass);
82 static void empathy_contact_list_store_init (EmpathyContactListStore *list);
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 gboolean contact_list_store_finalize_foreach (GtkTreeModel *model,
96 static void contact_list_store_setup (EmpathyContactListStore *store);
97 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
98 static void contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
99 EmpathyContact *contact,
100 EmpathyContact *actor,
104 EmpathyContactListStore *store);
105 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
106 EmpathyContact *contact,
109 EmpathyContactListStore *store);
110 static void contact_list_store_add_contact (EmpathyContactListStore *store,
111 EmpathyContact *contact);
112 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
113 EmpathyContact *contact);
114 static void contact_list_store_contact_update (EmpathyContactListStore *store,
115 EmpathyContact *contact);
116 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
118 EmpathyContactListStore *store);
119 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
120 EmpathyContact *contact,
122 gboolean set_changed);
123 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
124 EmpathyContact *contact,
126 static void contact_list_store_contact_active_free (ShowActiveData *data);
127 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
128 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
132 static void contact_list_store_get_group (EmpathyContactListStore *store,
134 GtkTreeIter *iter_group_to_set,
135 GtkTreeIter *iter_separator_to_set,
137 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
141 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
145 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
149 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
150 EmpathyContact *contact);
151 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
154 EmpathyContactListStore *store);
164 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
167 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
169 GObjectClass *object_class = G_OBJECT_CLASS (klass);
171 object_class->finalize = contact_list_store_finalize;
172 object_class->get_property = contact_list_store_get_property;
173 object_class->set_property = contact_list_store_set_property;
175 g_object_class_install_property (object_class,
177 g_param_spec_boolean ("show-offline",
179 "Whether contact list should display "
183 g_object_class_install_property (object_class,
185 g_param_spec_boolean ("show-avatars",
187 "Whether contact list should display "
188 "avatars for contacts",
191 g_object_class_install_property (object_class,
193 g_param_spec_boolean ("is-compact",
195 "Whether the contact list is in compact mode or not",
199 g_object_class_install_property (object_class,
201 g_param_spec_enum ("sort-criterium",
203 "The sort criterium to use for sorting the contact list",
204 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
205 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
208 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
212 empathy_contact_list_store_init (EmpathyContactListStore *store)
214 EmpathyContactListStorePriv *priv;
216 priv = GET_PRIV (store);
218 priv->show_avatars = TRUE;
219 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
220 (GSourceFunc) contact_list_store_inibit_active_cb,
225 contact_list_store_finalize (GObject *object)
227 EmpathyContactListStorePriv *priv;
229 priv = GET_PRIV (object);
231 gtk_tree_model_foreach (GTK_TREE_MODEL (object),
232 (GtkTreeModelForeachFunc) contact_list_store_finalize_foreach,
236 g_signal_handlers_disconnect_by_func (priv->list,
237 G_CALLBACK (contact_list_store_members_changed_cb),
239 g_signal_handlers_disconnect_by_func (priv->list,
240 G_CALLBACK (contact_list_store_groups_changed_cb),
242 g_object_unref (priv->list);
245 if (priv->inhibit_active) {
246 g_source_remove (priv->inhibit_active);
249 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
253 contact_list_store_get_property (GObject *object,
258 EmpathyContactListStorePriv *priv;
260 priv = GET_PRIV (object);
263 case PROP_SHOW_OFFLINE:
264 g_value_set_boolean (value, priv->show_offline);
266 case PROP_SHOW_AVATARS:
267 g_value_set_boolean (value, priv->show_avatars);
269 case PROP_IS_COMPACT:
270 g_value_set_boolean (value, priv->is_compact);
272 case PROP_SORT_CRITERIUM:
273 g_value_set_enum (value, priv->sort_criterium);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
282 contact_list_store_set_property (GObject *object,
287 EmpathyContactListStorePriv *priv;
289 priv = GET_PRIV (object);
292 case PROP_SHOW_OFFLINE:
293 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
294 g_value_get_boolean (value));
296 case PROP_SHOW_AVATARS:
297 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
298 g_value_get_boolean (value));
300 case PROP_IS_COMPACT:
301 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
302 g_value_get_boolean (value));
304 case PROP_SORT_CRITERIUM:
305 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
306 g_value_get_enum (value));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
314 EmpathyContactListStore *
315 empathy_contact_list_store_new (EmpathyContactList *list_iface)
317 EmpathyContactListStore *store;
318 EmpathyContactListStorePriv *priv;
321 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
323 store = g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE, NULL);
324 priv = GET_PRIV (store);
326 contact_list_store_setup (store);
327 priv->list = g_object_ref (list_iface);
329 /* Signal connection. */
330 g_signal_connect (priv->list,
332 G_CALLBACK (contact_list_store_members_changed_cb),
334 g_signal_connect (priv->list,
336 G_CALLBACK (contact_list_store_groups_changed_cb),
339 /* Add contacts already created. */
340 contacts = empathy_contact_list_get_members (priv->list);
341 for (l = contacts; l; l = l->next) {
342 contact_list_store_members_changed_cb (priv->list, l->data,
347 g_object_unref (l->data);
349 g_list_free (contacts);
355 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
357 EmpathyContactListStorePriv *priv;
359 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
361 priv = GET_PRIV (store);
367 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
369 EmpathyContactListStorePriv *priv;
371 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
373 priv = GET_PRIV (store);
375 return priv->show_offline;
379 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
380 gboolean show_offline)
382 EmpathyContactListStorePriv *priv;
384 gboolean show_active;
386 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
388 priv = GET_PRIV (store);
390 priv->show_offline = show_offline;
391 show_active = priv->show_active;
393 /* Disable temporarily. */
394 priv->show_active = FALSE;
396 contacts = empathy_contact_list_get_members (priv->list);
397 for (l = contacts; l; l = l->next) {
398 contact_list_store_contact_update (store, l->data);
400 g_object_unref (l->data);
402 g_list_free (contacts);
404 /* Restore to original setting. */
405 priv->show_active = show_active;
409 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
411 EmpathyContactListStorePriv *priv;
413 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
415 priv = GET_PRIV (store);
417 return priv->show_avatars;
421 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
422 gboolean show_avatars)
424 EmpathyContactListStorePriv *priv;
427 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
429 priv = GET_PRIV (store);
431 priv->show_avatars = show_avatars;
433 model = GTK_TREE_MODEL (store);
435 gtk_tree_model_foreach (model,
436 (GtkTreeModelForeachFunc)
437 contact_list_store_update_list_mode_foreach,
442 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
444 EmpathyContactListStorePriv *priv;
446 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
448 priv = GET_PRIV (store);
450 return priv->is_compact;
454 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
457 EmpathyContactListStorePriv *priv;
460 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
462 priv = GET_PRIV (store);
464 priv->is_compact = is_compact;
466 model = GTK_TREE_MODEL (store);
468 gtk_tree_model_foreach (model,
469 (GtkTreeModelForeachFunc)
470 contact_list_store_update_list_mode_foreach,
474 EmpathyContactListStoreSort
475 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
477 EmpathyContactListStorePriv *priv;
479 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
481 priv = GET_PRIV (store);
483 return priv->sort_criterium;
487 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
488 EmpathyContactListStoreSort sort_criterium)
490 EmpathyContactListStorePriv *priv;
492 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
494 priv = GET_PRIV (store);
496 priv->sort_criterium = sort_criterium;
498 switch (sort_criterium) {
499 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
500 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
501 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
505 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
506 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
507 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
514 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
518 gboolean is_separator = FALSE;
520 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
522 gtk_tree_model_get (model, iter,
523 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
530 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
532 gboolean *path_is_group)
534 GtkTreeIter parent_iter, iter;
538 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
541 *path_is_group = FALSE;
544 if (!gtk_tree_model_get_iter (model, &iter, path)) {
548 gtk_tree_model_get (model, &iter,
549 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
550 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
557 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
563 gtk_tree_model_get (model, &iter,
564 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
565 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
574 *path_is_group = TRUE;
581 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
585 gpointer search_data)
587 gchar *name, *name_folded;
591 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
597 gtk_tree_model_get (model, iter,
598 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
605 name_folded = g_utf8_casefold (name, -1);
606 key_folded = g_utf8_casefold (key, -1);
608 if (name_folded && key_folded &&
609 strstr (name_folded, key_folded)) {
616 g_free (name_folded);
623 contact_list_store_finalize_foreach (GtkTreeModel *model,
628 EmpathyContactListStore *store = user_data;
629 EmpathyContact *contact = NULL;
631 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
632 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
636 g_signal_handlers_disconnect_by_func (contact,
637 G_CALLBACK (contact_list_store_contact_updated_cb),
645 contact_list_store_setup (EmpathyContactListStore *store)
647 EmpathyContactListStorePriv *priv;
648 GType types[] = {G_TYPE_STRING, /* Status icon-name */
649 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
650 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
651 G_TYPE_STRING, /* Name */
652 G_TYPE_STRING, /* Status string */
653 G_TYPE_BOOLEAN, /* Show status */
654 EMPATHY_TYPE_CONTACT, /* Contact type */
655 G_TYPE_BOOLEAN, /* Is group */
656 G_TYPE_BOOLEAN, /* Is active */
657 G_TYPE_BOOLEAN, /* Is online */
658 G_TYPE_BOOLEAN, /* Is separator */
659 G_TYPE_BOOLEAN}; /* Can VoIP */
661 priv = GET_PRIV (store);
663 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
664 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
668 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
669 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
670 contact_list_store_name_sort_func,
672 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
673 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
674 contact_list_store_state_sort_func,
677 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
678 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
682 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
684 EmpathyContactListStorePriv *priv;
686 priv = GET_PRIV (store);
688 priv->show_active = TRUE;
689 priv->inhibit_active = 0;
695 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
696 EmpathyContact *contact,
697 EmpathyContact *actor,
701 EmpathyContactListStore *store)
703 EmpathyContactListStorePriv *priv;
705 priv = GET_PRIV (store);
707 empathy_debug (DEBUG_DOMAIN,
708 "Contact %s (%d) %s",
709 empathy_contact_get_id (contact),
710 empathy_contact_get_handle (contact),
711 is_member ? "added" : "removed");
714 g_signal_connect (contact, "notify::presence",
715 G_CALLBACK (contact_list_store_contact_updated_cb),
717 g_signal_connect (contact, "notify::name",
718 G_CALLBACK (contact_list_store_contact_updated_cb),
720 g_signal_connect (contact, "notify::avatar",
721 G_CALLBACK (contact_list_store_contact_updated_cb),
723 g_signal_connect (contact, "notify::capabilities",
724 G_CALLBACK (contact_list_store_contact_updated_cb),
727 contact_list_store_add_contact (store, contact);
729 g_signal_handlers_disconnect_by_func (contact,
730 G_CALLBACK (contact_list_store_contact_updated_cb),
733 contact_list_store_remove_contact (store, contact);
738 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
739 EmpathyContact *contact,
742 EmpathyContactListStore *store)
744 EmpathyContactListStorePriv *priv;
745 gboolean show_active;
747 priv = GET_PRIV (store);
749 empathy_debug (DEBUG_DOMAIN, "Updating groups for contact %s (%d)",
750 empathy_contact_get_id (contact),
751 empathy_contact_get_handle (contact));
753 /* We do this to make sure the groups are correct, if not, we
754 * would have to check the groups already set up for each
755 * contact and then see what has been updated.
757 show_active = priv->show_active;
758 priv->show_active = FALSE;
759 contact_list_store_remove_contact (store, contact);
760 contact_list_store_add_contact (store, contact);
761 priv->show_active = show_active;
765 contact_list_store_add_contact (EmpathyContactListStore *store,
766 EmpathyContact *contact)
768 EmpathyContactListStorePriv *priv;
772 priv = GET_PRIV (store);
774 if (!priv->show_offline && !empathy_contact_is_online (contact)) {
778 groups = empathy_contact_list_get_groups (priv->list, contact);
780 /* If no groups just add it at the top level. */
782 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
783 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
784 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
785 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
786 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
787 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
788 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
792 /* Else add to each group. */
793 for (l = groups; l; l = l->next) {
794 GtkTreeIter iter_group;
796 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
798 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
800 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
801 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
802 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
803 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
804 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
805 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
809 g_list_free (groups);
811 contact_list_store_contact_update (store, contact);
816 contact_list_store_remove_contact (EmpathyContactListStore *store,
817 EmpathyContact *contact)
819 EmpathyContactListStorePriv *priv;
823 priv = GET_PRIV (store);
825 iters = contact_list_store_find_contact (store, contact);
831 model = GTK_TREE_MODEL (store);
833 for (l = iters; l; l = l->next) {
836 /* NOTE: it is only <= 2 here because we have
837 * separators after the group name, otherwise it
840 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
841 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
842 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
844 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
848 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
853 contact_list_store_contact_update (EmpathyContactListStore *store,
854 EmpathyContact *contact)
856 EmpathyContactListStorePriv *priv;
857 ShowActiveData *data;
861 gboolean should_be_in_list;
862 gboolean was_online = TRUE;
863 gboolean now_online = FALSE;
864 gboolean set_model = FALSE;
865 gboolean do_remove = FALSE;
866 gboolean do_set_active = FALSE;
867 gboolean do_set_refresh = FALSE;
868 GdkPixbuf *pixbuf_avatar;
870 priv = GET_PRIV (store);
872 model = GTK_TREE_MODEL (store);
874 iters = contact_list_store_find_contact (store, contact);
881 /* Get online state now. */
882 now_online = empathy_contact_is_online (contact);
884 if (priv->show_offline || now_online) {
885 should_be_in_list = TRUE;
887 should_be_in_list = FALSE;
890 if (!in_list && !should_be_in_list) {
892 empathy_debug (DEBUG_DOMAIN,
893 "Contact:'%s' in list:NO, should be:NO",
894 empathy_contact_get_name (contact));
896 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
900 else if (in_list && !should_be_in_list) {
901 empathy_debug (DEBUG_DOMAIN,
902 "Contact:'%s' in list:YES, should be:NO",
903 empathy_contact_get_name (contact));
905 if (priv->show_active) {
907 do_set_active = TRUE;
908 do_set_refresh = TRUE;
911 empathy_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
913 empathy_debug (DEBUG_DOMAIN, "Remove item (now)!");
914 contact_list_store_remove_contact (store, contact);
917 else if (!in_list && should_be_in_list) {
918 empathy_debug (DEBUG_DOMAIN,
919 "Contact:'%s' in list:NO, should be:YES",
920 empathy_contact_get_name (contact));
922 contact_list_store_add_contact (store, contact);
924 if (priv->show_active) {
925 do_set_active = TRUE;
927 empathy_debug (DEBUG_DOMAIN, "Set active (contact added)");
930 empathy_debug (DEBUG_DOMAIN,
931 "Contact:'%s' in list:YES, should be:YES",
932 empathy_contact_get_name (contact));
934 /* Get online state before. */
935 if (iters && g_list_length (iters) > 0) {
936 gtk_tree_model_get (model, iters->data,
937 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
941 /* Is this really an update or an online/offline. */
942 if (priv->show_active) {
943 if (was_online != now_online) {
944 do_set_active = TRUE;
945 do_set_refresh = TRUE;
947 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated %s)",
948 was_online ? "online -> offline" :
949 "offline -> online");
951 /* Was TRUE for presence updates. */
952 /* do_set_active = FALSE; */
953 do_set_refresh = TRUE;
955 empathy_debug (DEBUG_DOMAIN, "Set active (contact updated)");
962 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
963 for (l = iters; l && set_model; l = l->next) {
964 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
965 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
966 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
967 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
968 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
969 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
970 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
971 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
972 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
973 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
974 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
979 g_object_unref (pixbuf_avatar);
982 if (priv->show_active && do_set_active) {
983 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
986 data = contact_list_store_contact_active_new (store, contact, do_remove);
987 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
988 (GSourceFunc) contact_list_store_contact_active_cb,
993 /* FIXME: when someone goes online then offline quickly, the
994 * first timeout sets the user to be inactive and the second
995 * timeout removes the user from the contact list, really we
996 * should remove the first timeout.
998 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1003 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1005 EmpathyContactListStore *store)
1007 empathy_debug (DEBUG_DOMAIN,
1008 "Contact:'%s' updated, checking roster is in sync...",
1009 empathy_contact_get_name (contact));
1011 contact_list_store_contact_update (store, contact);
1015 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1016 EmpathyContact *contact,
1018 gboolean set_changed)
1020 EmpathyContactListStorePriv *priv;
1021 GtkTreeModel *model;
1024 priv = GET_PRIV (store);
1025 model = GTK_TREE_MODEL (store);
1027 iters = contact_list_store_find_contact (store, contact);
1028 for (l = iters; l; l = l->next) {
1031 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1032 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1035 empathy_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
1038 path = gtk_tree_model_get_path (model, l->data);
1039 gtk_tree_model_row_changed (model, path, l->data);
1040 gtk_tree_path_free (path);
1044 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1045 g_list_free (iters);
1049 static ShowActiveData *
1050 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1051 EmpathyContact *contact,
1054 ShowActiveData *data;
1056 empathy_debug (DEBUG_DOMAIN,
1057 "Contact:'%s' now active, and %s be removed",
1058 empathy_contact_get_name (contact),
1059 remove ? "WILL" : "WILL NOT");
1061 data = g_slice_new0 (ShowActiveData);
1063 data->store = g_object_ref (store);
1064 data->contact = g_object_ref (contact);
1065 data->remove = remove;
1071 contact_list_store_contact_active_free (ShowActiveData *data)
1073 g_object_unref (data->contact);
1074 g_object_unref (data->store);
1076 g_slice_free (ShowActiveData, data);
1080 contact_list_store_contact_active_cb (ShowActiveData *data)
1082 EmpathyContactListStorePriv *priv;
1084 priv = GET_PRIV (data->store);
1087 !priv->show_offline &&
1088 !empathy_contact_is_online (data->contact)) {
1089 empathy_debug (DEBUG_DOMAIN,
1090 "Contact:'%s' active timeout, removing item",
1091 empathy_contact_get_name (data->contact));
1092 contact_list_store_remove_contact (data->store, data->contact);
1095 empathy_debug (DEBUG_DOMAIN,
1096 "Contact:'%s' no longer active",
1097 empathy_contact_get_name (data->contact));
1099 contact_list_store_contact_set_active (data->store,
1104 contact_list_store_contact_active_free (data);
1110 contact_list_store_get_group_foreach (GtkTreeModel *model,
1118 /* Groups are only at the top level. */
1119 if (gtk_tree_path_get_depth (path) != 1) {
1123 gtk_tree_model_get (model, iter,
1124 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1125 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1128 if (is_group && strcmp (str, fg->name) == 0) {
1139 contact_list_store_get_group (EmpathyContactListStore *store,
1141 GtkTreeIter *iter_group_to_set,
1142 GtkTreeIter *iter_separator_to_set,
1145 EmpathyContactListStorePriv *priv;
1146 GtkTreeModel *model;
1147 GtkTreeIter iter_group;
1148 GtkTreeIter iter_separator;
1151 priv = GET_PRIV (store);
1153 memset (&fg, 0, sizeof (fg));
1157 model = GTK_TREE_MODEL (store);
1158 gtk_tree_model_foreach (model,
1159 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1167 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1168 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1169 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1170 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1171 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1172 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1173 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1176 if (iter_group_to_set) {
1177 *iter_group_to_set = iter_group;
1180 gtk_tree_store_append (GTK_TREE_STORE (store),
1183 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1184 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1187 if (iter_separator_to_set) {
1188 *iter_separator_to_set = iter_separator;
1195 if (iter_group_to_set) {
1196 *iter_group_to_set = fg.iter;
1199 iter_separator = fg.iter;
1201 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1202 gboolean is_separator;
1204 gtk_tree_model_get (model, &iter_separator,
1205 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1208 if (is_separator && iter_separator_to_set) {
1209 *iter_separator_to_set = iter_separator;
1216 contact_list_store_ordered_presence (McPresence state)
1219 case MC_PRESENCE_UNSET:
1220 case MC_PRESENCE_OFFLINE:
1222 case MC_PRESENCE_AVAILABLE:
1224 case MC_PRESENCE_AWAY:
1226 case MC_PRESENCE_EXTENDED_AWAY:
1228 case MC_PRESENCE_HIDDEN:
1230 case MC_PRESENCE_DO_NOT_DISTURB:
1233 g_return_val_if_reached (6);
1238 contact_list_store_state_sort_func (GtkTreeModel *model,
1239 GtkTreeIter *iter_a,
1240 GtkTreeIter *iter_b,
1244 gchar *name_a, *name_b;
1245 gboolean is_separator_a, is_separator_b;
1246 EmpathyContact *contact_a, *contact_b;
1247 EmpathyPresence *presence_a, *presence_b;
1249 gtk_tree_model_get (model, iter_a,
1250 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1251 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1252 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1254 gtk_tree_model_get (model, iter_b,
1255 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1256 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1257 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1260 /* Separator or group? */
1261 if (is_separator_a || is_separator_b) {
1262 if (is_separator_a) {
1264 } else if (is_separator_b) {
1267 } else if (!contact_a && contact_b) {
1269 } else if (contact_a && !contact_b) {
1271 } else if (!contact_a && !contact_b) {
1273 ret_val = g_utf8_collate (name_a, name_b);
1280 /* If we managed to get this far, we can start looking at
1283 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1284 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1286 if (!presence_a && presence_b) {
1288 } else if (presence_a && !presence_b) {
1290 } else if (!presence_a && !presence_b) {
1291 /* Both offline, sort by name */
1292 ret_val = g_utf8_collate (name_a, name_b);
1294 guint state_a, state_b;
1296 state_a = empathy_presence_get_state (presence_a);
1297 state_b = empathy_presence_get_state (presence_b);
1299 state_a = contact_list_store_ordered_presence (state_a);
1300 state_b = contact_list_store_ordered_presence (state_b);
1302 if (state_a < state_b) {
1304 } else if (state_a > state_b) {
1307 /* Fallback: compare by name */
1308 ret_val = g_utf8_collate (name_a, name_b);
1317 g_object_unref (contact_a);
1321 g_object_unref (contact_b);
1328 contact_list_store_name_sort_func (GtkTreeModel *model,
1329 GtkTreeIter *iter_a,
1330 GtkTreeIter *iter_b,
1333 gchar *name_a, *name_b;
1334 EmpathyContact *contact_a, *contact_b;
1335 gboolean is_separator_a, is_separator_b;
1338 gtk_tree_model_get (model, iter_a,
1339 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1340 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1341 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1343 gtk_tree_model_get (model, iter_b,
1344 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1345 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1346 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1349 /* If contact is NULL it means it's a group. */
1351 if (is_separator_a || is_separator_b) {
1352 if (is_separator_a) {
1354 } else if (is_separator_b) {
1357 } else if (!contact_a && contact_b) {
1359 } else if (contact_a && !contact_b) {
1362 ret_val = g_utf8_collate (name_a, name_b);
1369 g_object_unref (contact_a);
1373 g_object_unref (contact_b);
1380 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1385 EmpathyContact *contact;
1387 gtk_tree_model_get (model, iter,
1388 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1395 if (empathy_contact_equal (contact, fc->contact)) {
1397 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1399 g_object_unref (contact);
1405 contact_list_store_find_contact (EmpathyContactListStore *store,
1406 EmpathyContact *contact)
1408 EmpathyContactListStorePriv *priv;
1409 GtkTreeModel *model;
1413 priv = GET_PRIV (store);
1415 memset (&fc, 0, sizeof (fc));
1417 fc.contact = contact;
1419 model = GTK_TREE_MODEL (store);
1420 gtk_tree_model_foreach (model,
1421 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1432 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1435 EmpathyContactListStore *store)
1437 EmpathyContactListStorePriv *priv;
1438 gboolean show_avatar = FALSE;
1440 priv = GET_PRIV (store);
1442 if (priv->show_avatars && !priv->is_compact) {
1446 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1447 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1448 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,