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 "empathy-contact-list-store.h"
36 #include "empathy-ui-utils.h"
37 #include "empathy-gtk-enum-types.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
40 #include <libempathy/empathy-debug.h>
42 /* Active users are those which have recently changed state
43 * (e.g. online, offline or from normal to a busy state).
46 /* Time in seconds user is shown as active */
47 #define ACTIVE_USER_SHOW_TIME 7
49 /* Time in seconds after connecting which we wait before active users are enabled */
50 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv))
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 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass);
84 static void empathy_contact_list_store_init (EmpathyContactListStore *list);
85 static void contact_list_store_finalize (GObject *object);
86 static void contact_list_store_get_property (GObject *object,
90 static void contact_list_store_set_property (GObject *object,
94 static void contact_list_store_setup (EmpathyContactListStore *store);
95 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
96 static void contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
97 EmpathyContact *contact,
98 EmpathyContact *actor,
102 EmpathyContactListStore *store);
103 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
104 EmpathyContact *contact,
107 EmpathyContactListStore *store);
108 static void contact_list_store_add_contact (EmpathyContactListStore *store,
109 EmpathyContact *contact);
110 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
111 EmpathyContact *contact);
112 static void contact_list_store_contact_update (EmpathyContactListStore *store,
113 EmpathyContact *contact);
114 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
116 EmpathyContactListStore *store);
117 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
118 EmpathyContact *contact,
120 gboolean set_changed);
121 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
122 EmpathyContact *contact,
124 static void contact_list_store_contact_active_free (ShowActiveData *data);
125 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
126 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
130 static void contact_list_store_get_group (EmpathyContactListStore *store,
132 GtkTreeIter *iter_group_to_set,
133 GtkTreeIter *iter_separator_to_set,
135 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
139 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
143 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
147 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
148 EmpathyContact *contact);
149 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
152 EmpathyContactListStore *store);
164 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
167 contact_list_store_iface_setup (gpointer user_data)
169 EmpathyContactListStore *store = user_data;
170 EmpathyContactListStorePriv *priv = GET_PRIV (store);
173 /* Signal connection. */
174 g_signal_connect (priv->list,
176 G_CALLBACK (contact_list_store_members_changed_cb),
178 g_signal_connect (priv->list,
180 G_CALLBACK (contact_list_store_groups_changed_cb),
183 /* Add contacts already created. */
184 contacts = empathy_contact_list_get_members (priv->list);
185 for (l = contacts; l; l = l->next) {
186 contact_list_store_members_changed_cb (priv->list, l->data,
191 g_object_unref (l->data);
193 g_list_free (contacts);
200 contact_list_store_set_contact_list (EmpathyContactListStore *store,
201 EmpathyContactList *list_iface)
203 EmpathyContactListStorePriv *priv = GET_PRIV (store);
205 priv->list = g_object_ref (list_iface);
207 /* Let a chance to have all properties set before populating */
208 g_idle_add (contact_list_store_iface_setup, store);
212 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
214 GObjectClass *object_class = G_OBJECT_CLASS (klass);
216 object_class->finalize = contact_list_store_finalize;
217 object_class->get_property = contact_list_store_get_property;
218 object_class->set_property = contact_list_store_set_property;
220 g_object_class_install_property (object_class,
222 g_param_spec_object ("contact-list",
223 "The contact list iface",
224 "The contact list iface",
225 EMPATHY_TYPE_CONTACT_LIST,
226 G_PARAM_CONSTRUCT_ONLY |
228 g_object_class_install_property (object_class,
230 g_param_spec_boolean ("show-offline",
232 "Whether contact list should display "
236 g_object_class_install_property (object_class,
238 g_param_spec_boolean ("show-avatars",
240 "Whether contact list should display "
241 "avatars for contacts",
244 g_object_class_install_property (object_class,
246 g_param_spec_boolean ("show-groups",
248 "Whether contact list should display "
252 g_object_class_install_property (object_class,
254 g_param_spec_boolean ("is-compact",
256 "Whether the contact list is in compact mode or not",
260 g_object_class_install_property (object_class,
262 g_param_spec_enum ("sort-criterium",
264 "The sort criterium to use for sorting the contact list",
265 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
266 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
269 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
273 empathy_contact_list_store_init (EmpathyContactListStore *store)
275 EmpathyContactListStorePriv *priv;
277 priv = GET_PRIV (store);
279 priv->show_avatars = TRUE;
280 priv->show_groups = TRUE;
281 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
282 (GSourceFunc) contact_list_store_inibit_active_cb,
284 contact_list_store_setup (store);
288 contact_list_store_finalize (GObject *object)
290 EmpathyContactListStorePriv *priv = GET_PRIV (object);
293 contacts = empathy_contact_list_get_members (priv->list);
294 for (l = contacts; l; l = l->next) {
295 g_signal_handlers_disconnect_by_func (l->data,
296 G_CALLBACK (contact_list_store_contact_updated_cb),
299 g_object_unref (l->data);
301 g_list_free (contacts);
303 g_signal_handlers_disconnect_by_func (priv->list,
304 G_CALLBACK (contact_list_store_members_changed_cb),
306 g_signal_handlers_disconnect_by_func (priv->list,
307 G_CALLBACK (contact_list_store_groups_changed_cb),
309 g_object_unref (priv->list);
311 if (priv->inhibit_active) {
312 g_source_remove (priv->inhibit_active);
315 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
319 contact_list_store_get_property (GObject *object,
324 EmpathyContactListStorePriv *priv;
326 priv = GET_PRIV (object);
329 case PROP_CONTACT_LIST:
330 g_value_set_object (value, priv->list);
332 case PROP_SHOW_OFFLINE:
333 g_value_set_boolean (value, priv->show_offline);
335 case PROP_SHOW_AVATARS:
336 g_value_set_boolean (value, priv->show_avatars);
338 case PROP_SHOW_GROUPS:
339 g_value_set_boolean (value, priv->show_groups);
341 case PROP_IS_COMPACT:
342 g_value_set_boolean (value, priv->is_compact);
344 case PROP_SORT_CRITERIUM:
345 g_value_set_enum (value, priv->sort_criterium);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
354 contact_list_store_set_property (GObject *object,
359 EmpathyContactListStorePriv *priv;
361 priv = GET_PRIV (object);
364 case PROP_CONTACT_LIST:
365 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
366 g_value_get_object (value));
368 case PROP_SHOW_OFFLINE:
369 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
370 g_value_get_boolean (value));
372 case PROP_SHOW_AVATARS:
373 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
374 g_value_get_boolean (value));
376 case PROP_SHOW_GROUPS:
377 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
378 g_value_get_boolean (value));
380 case PROP_IS_COMPACT:
381 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
382 g_value_get_boolean (value));
384 case PROP_SORT_CRITERIUM:
385 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
386 g_value_get_enum (value));
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
394 EmpathyContactListStore *
395 empathy_contact_list_store_new (EmpathyContactList *list_iface)
397 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
399 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
400 "contact-list", list_iface,
405 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
407 EmpathyContactListStorePriv *priv;
409 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
411 priv = GET_PRIV (store);
417 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
419 EmpathyContactListStorePriv *priv;
421 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
423 priv = GET_PRIV (store);
425 return priv->show_offline;
429 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
430 gboolean show_offline)
432 EmpathyContactListStorePriv *priv;
434 gboolean show_active;
436 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
438 priv = GET_PRIV (store);
440 priv->show_offline = show_offline;
441 show_active = priv->show_active;
443 /* Disable temporarily. */
444 priv->show_active = FALSE;
446 contacts = empathy_contact_list_get_members (priv->list);
447 for (l = contacts; l; l = l->next) {
448 contact_list_store_contact_update (store, l->data);
450 g_object_unref (l->data);
452 g_list_free (contacts);
454 /* Restore to original setting. */
455 priv->show_active = show_active;
457 g_object_notify (G_OBJECT (store), "show-offline");
461 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
463 EmpathyContactListStorePriv *priv;
465 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
467 priv = GET_PRIV (store);
469 return priv->show_avatars;
473 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
474 gboolean show_avatars)
476 EmpathyContactListStorePriv *priv;
479 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
481 priv = GET_PRIV (store);
483 priv->show_avatars = show_avatars;
485 model = GTK_TREE_MODEL (store);
487 gtk_tree_model_foreach (model,
488 (GtkTreeModelForeachFunc)
489 contact_list_store_update_list_mode_foreach,
492 g_object_notify (G_OBJECT (store), "show-avatars");
496 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
498 EmpathyContactListStorePriv *priv;
500 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
502 priv = GET_PRIV (store);
504 return priv->show_groups;
508 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
509 gboolean show_groups)
511 EmpathyContactListStorePriv *priv;
514 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
516 priv = GET_PRIV (store);
518 if (priv->show_groups == show_groups) {
522 priv->show_groups = show_groups;
524 /* Remove all contacts and add them back, not optimized but that's the
526 gtk_tree_store_clear (GTK_TREE_STORE (store));
527 contacts = empathy_contact_list_get_members (priv->list);
528 for (l = contacts; l; l = l->next) {
529 contact_list_store_members_changed_cb (priv->list, l->data,
534 g_object_unref (l->data);
536 g_list_free (contacts);
538 g_object_notify (G_OBJECT (store), "show-groups");
542 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
544 EmpathyContactListStorePriv *priv;
546 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
548 priv = GET_PRIV (store);
550 return priv->is_compact;
554 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
557 EmpathyContactListStorePriv *priv;
560 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
562 priv = GET_PRIV (store);
564 priv->is_compact = is_compact;
566 model = GTK_TREE_MODEL (store);
568 gtk_tree_model_foreach (model,
569 (GtkTreeModelForeachFunc)
570 contact_list_store_update_list_mode_foreach,
573 g_object_notify (G_OBJECT (store), "is-compact");
576 EmpathyContactListStoreSort
577 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
579 EmpathyContactListStorePriv *priv;
581 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
583 priv = GET_PRIV (store);
585 return priv->sort_criterium;
589 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
590 EmpathyContactListStoreSort sort_criterium)
592 EmpathyContactListStorePriv *priv;
594 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
596 priv = GET_PRIV (store);
598 priv->sort_criterium = sort_criterium;
600 switch (sort_criterium) {
601 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
602 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
603 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
607 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
608 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
609 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
614 g_object_notify (G_OBJECT (store), "sort-criterium");
618 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
622 gboolean is_separator = FALSE;
624 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
626 gtk_tree_model_get (model, iter,
627 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
634 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
636 gboolean *path_is_group)
638 GtkTreeIter parent_iter, iter;
642 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
645 *path_is_group = FALSE;
648 if (!gtk_tree_model_get_iter (model, &iter, path)) {
652 gtk_tree_model_get (model, &iter,
653 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
654 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
661 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
667 gtk_tree_model_get (model, &iter,
668 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
669 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
678 *path_is_group = TRUE;
685 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
689 gpointer search_data)
691 gchar *name, *name_folded;
695 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
701 gtk_tree_model_get (model, iter,
702 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
709 name_folded = g_utf8_casefold (name, -1);
710 key_folded = g_utf8_casefold (key, -1);
712 if (name_folded && key_folded &&
713 strstr (name_folded, key_folded)) {
720 g_free (name_folded);
727 contact_list_store_setup (EmpathyContactListStore *store)
729 EmpathyContactListStorePriv *priv;
730 GType types[] = {G_TYPE_STRING, /* Status icon-name */
731 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
732 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
733 G_TYPE_STRING, /* Name */
734 G_TYPE_STRING, /* Status string */
735 G_TYPE_BOOLEAN, /* Show status */
736 EMPATHY_TYPE_CONTACT, /* Contact type */
737 G_TYPE_BOOLEAN, /* Is group */
738 G_TYPE_BOOLEAN, /* Is active */
739 G_TYPE_BOOLEAN, /* Is online */
740 G_TYPE_BOOLEAN, /* Is separator */
741 G_TYPE_BOOLEAN}; /* Can VoIP */
743 priv = GET_PRIV (store);
745 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
746 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
750 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
751 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
752 contact_list_store_name_sort_func,
754 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
755 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
756 contact_list_store_state_sort_func,
759 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
760 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
764 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
766 EmpathyContactListStorePriv *priv;
768 priv = GET_PRIV (store);
770 priv->show_active = TRUE;
771 priv->inhibit_active = 0;
777 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
778 EmpathyContact *contact,
779 EmpathyContact *actor,
783 EmpathyContactListStore *store)
785 EmpathyContactListStorePriv *priv;
787 priv = GET_PRIV (store);
789 DEBUG ("Contact %s (%d) %s",
790 empathy_contact_get_id (contact),
791 empathy_contact_get_handle (contact),
792 is_member ? "added" : "removed");
795 g_signal_connect (contact, "notify::presence",
796 G_CALLBACK (contact_list_store_contact_updated_cb),
798 g_signal_connect (contact, "notify::presence-message",
799 G_CALLBACK (contact_list_store_contact_updated_cb),
801 g_signal_connect (contact, "notify::name",
802 G_CALLBACK (contact_list_store_contact_updated_cb),
804 g_signal_connect (contact, "notify::avatar",
805 G_CALLBACK (contact_list_store_contact_updated_cb),
807 g_signal_connect (contact, "notify::capabilities",
808 G_CALLBACK (contact_list_store_contact_updated_cb),
811 contact_list_store_add_contact (store, contact);
813 g_signal_handlers_disconnect_by_func (contact,
814 G_CALLBACK (contact_list_store_contact_updated_cb),
817 contact_list_store_remove_contact (store, contact);
822 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
823 EmpathyContact *contact,
826 EmpathyContactListStore *store)
828 EmpathyContactListStorePriv *priv;
829 gboolean show_active;
831 priv = GET_PRIV (store);
833 DEBUG ("Updating groups for contact %s (%d)",
834 empathy_contact_get_id (contact),
835 empathy_contact_get_handle (contact));
837 /* We do this to make sure the groups are correct, if not, we
838 * would have to check the groups already set up for each
839 * contact and then see what has been updated.
841 show_active = priv->show_active;
842 priv->show_active = FALSE;
843 contact_list_store_remove_contact (store, contact);
844 contact_list_store_add_contact (store, contact);
845 priv->show_active = show_active;
849 contact_list_store_add_contact (EmpathyContactListStore *store,
850 EmpathyContact *contact)
852 EmpathyContactListStorePriv *priv;
854 GList *groups = NULL, *l;
856 priv = GET_PRIV (store);
858 if (G_STR_EMPTY (empathy_contact_get_name (contact)) ||
859 (!priv->show_offline && !empathy_contact_is_online (contact))) {
863 if (priv->show_groups) {
864 groups = empathy_contact_list_get_groups (priv->list, contact);
867 /* If no groups just add it at the top level. */
869 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
870 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
871 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
872 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
873 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
874 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
875 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
879 /* Else add to each group. */
880 for (l = groups; l; l = l->next) {
881 GtkTreeIter iter_group;
883 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
885 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
887 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
888 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
889 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
890 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
891 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
892 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
896 g_list_free (groups);
898 contact_list_store_contact_update (store, contact);
903 contact_list_store_remove_contact (EmpathyContactListStore *store,
904 EmpathyContact *contact)
906 EmpathyContactListStorePriv *priv;
910 priv = GET_PRIV (store);
912 iters = contact_list_store_find_contact (store, contact);
918 model = GTK_TREE_MODEL (store);
920 for (l = iters; l; l = l->next) {
923 /* NOTE: it is only <= 2 here because we have
924 * separators after the group name, otherwise it
927 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
928 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
929 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
931 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
935 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
940 contact_list_store_contact_update (EmpathyContactListStore *store,
941 EmpathyContact *contact)
943 EmpathyContactListStorePriv *priv;
944 ShowActiveData *data;
948 gboolean should_be_in_list;
949 gboolean was_online = TRUE;
950 gboolean now_online = FALSE;
951 gboolean set_model = FALSE;
952 gboolean do_remove = FALSE;
953 gboolean do_set_active = FALSE;
954 gboolean do_set_refresh = FALSE;
955 gboolean show_avatar = FALSE;
956 GdkPixbuf *pixbuf_avatar;
958 priv = GET_PRIV (store);
960 model = GTK_TREE_MODEL (store);
962 iters = contact_list_store_find_contact (store, contact);
969 /* Get online state now. */
970 now_online = empathy_contact_is_online (contact);
972 if (priv->show_offline || now_online) {
973 should_be_in_list = TRUE;
975 should_be_in_list = FALSE;
978 if (!in_list && !should_be_in_list) {
980 DEBUG ("Contact:'%s' in list:NO, should be:NO",
981 empathy_contact_get_name (contact));
983 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
987 else if (in_list && !should_be_in_list) {
988 DEBUG ("Contact:'%s' in list:YES, should be:NO",
989 empathy_contact_get_name (contact));
991 if (priv->show_active) {
993 do_set_active = TRUE;
994 do_set_refresh = TRUE;
997 DEBUG ("Remove item (after timeout)");
999 DEBUG ("Remove item (now)!");
1000 contact_list_store_remove_contact (store, contact);
1003 else if (!in_list && should_be_in_list) {
1004 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1005 empathy_contact_get_name (contact));
1007 contact_list_store_add_contact (store, contact);
1009 if (priv->show_active) {
1010 do_set_active = TRUE;
1012 DEBUG ("Set active (contact added)");
1015 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1016 empathy_contact_get_name (contact));
1018 /* Get online state before. */
1019 if (iters && g_list_length (iters) > 0) {
1020 gtk_tree_model_get (model, iters->data,
1021 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1025 /* Is this really an update or an online/offline. */
1026 if (priv->show_active) {
1027 if (was_online != now_online) {
1028 do_set_active = TRUE;
1029 do_set_refresh = TRUE;
1031 DEBUG ("Set active (contact updated %s)",
1032 was_online ? "online -> offline" :
1033 "offline -> online");
1035 /* Was TRUE for presence updates. */
1036 /* do_set_active = FALSE; */
1037 do_set_refresh = TRUE;
1039 DEBUG ("Set active (contact updated)");
1046 if (priv->show_avatars && !priv->is_compact) {
1049 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1050 for (l = iters; l && set_model; l = l->next) {
1051 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1052 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1053 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1054 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1055 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1056 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1057 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1058 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1059 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1060 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1061 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VOIP, empathy_contact_can_voip (contact),
1065 if (pixbuf_avatar) {
1066 g_object_unref (pixbuf_avatar);
1069 if (priv->show_active && do_set_active) {
1070 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1072 if (do_set_active) {
1073 data = contact_list_store_contact_active_new (store, contact, do_remove);
1074 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1075 (GSourceFunc) contact_list_store_contact_active_cb,
1080 /* FIXME: when someone goes online then offline quickly, the
1081 * first timeout sets the user to be inactive and the second
1082 * timeout removes the user from the contact list, really we
1083 * should remove the first timeout.
1085 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1086 g_list_free (iters);
1090 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1092 EmpathyContactListStore *store)
1094 DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1095 empathy_contact_get_name (contact));
1097 contact_list_store_contact_update (store, contact);
1101 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1102 EmpathyContact *contact,
1104 gboolean set_changed)
1106 EmpathyContactListStorePriv *priv;
1107 GtkTreeModel *model;
1110 priv = GET_PRIV (store);
1111 model = GTK_TREE_MODEL (store);
1113 iters = contact_list_store_find_contact (store, contact);
1114 for (l = iters; l; l = l->next) {
1117 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1118 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1121 DEBUG ("Set item %s", active ? "active" : "inactive");
1124 path = gtk_tree_model_get_path (model, l->data);
1125 gtk_tree_model_row_changed (model, path, l->data);
1126 gtk_tree_path_free (path);
1130 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1131 g_list_free (iters);
1135 static ShowActiveData *
1136 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1137 EmpathyContact *contact,
1140 ShowActiveData *data;
1142 DEBUG ("Contact:'%s' now active, and %s be removed",
1143 empathy_contact_get_name (contact),
1144 remove ? "WILL" : "WILL NOT");
1146 data = g_slice_new0 (ShowActiveData);
1148 data->store = g_object_ref (store);
1149 data->contact = g_object_ref (contact);
1150 data->remove = remove;
1156 contact_list_store_contact_active_free (ShowActiveData *data)
1158 g_object_unref (data->contact);
1159 g_object_unref (data->store);
1161 g_slice_free (ShowActiveData, data);
1165 contact_list_store_contact_active_cb (ShowActiveData *data)
1167 EmpathyContactListStorePriv *priv;
1169 priv = GET_PRIV (data->store);
1172 !priv->show_offline &&
1173 !empathy_contact_is_online (data->contact)) {
1174 DEBUG ("Contact:'%s' active timeout, removing item",
1175 empathy_contact_get_name (data->contact));
1176 contact_list_store_remove_contact (data->store, data->contact);
1179 DEBUG ("Contact:'%s' no longer active",
1180 empathy_contact_get_name (data->contact));
1182 contact_list_store_contact_set_active (data->store,
1187 contact_list_store_contact_active_free (data);
1193 contact_list_store_get_group_foreach (GtkTreeModel *model,
1201 /* Groups are only at the top level. */
1202 if (gtk_tree_path_get_depth (path) != 1) {
1206 gtk_tree_model_get (model, iter,
1207 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1208 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1211 if (is_group && !tp_strdiff (str, fg->name)) {
1222 contact_list_store_get_group (EmpathyContactListStore *store,
1224 GtkTreeIter *iter_group_to_set,
1225 GtkTreeIter *iter_separator_to_set,
1228 EmpathyContactListStorePriv *priv;
1229 GtkTreeModel *model;
1230 GtkTreeIter iter_group;
1231 GtkTreeIter iter_separator;
1234 priv = GET_PRIV (store);
1236 memset (&fg, 0, sizeof (fg));
1240 model = GTK_TREE_MODEL (store);
1241 gtk_tree_model_foreach (model,
1242 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1250 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1251 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1252 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1253 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1254 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1255 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1256 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1259 if (iter_group_to_set) {
1260 *iter_group_to_set = iter_group;
1263 gtk_tree_store_append (GTK_TREE_STORE (store),
1266 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1267 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1270 if (iter_separator_to_set) {
1271 *iter_separator_to_set = iter_separator;
1278 if (iter_group_to_set) {
1279 *iter_group_to_set = fg.iter;
1282 iter_separator = fg.iter;
1284 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1285 gboolean is_separator;
1287 gtk_tree_model_get (model, &iter_separator,
1288 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1291 if (is_separator && iter_separator_to_set) {
1292 *iter_separator_to_set = iter_separator;
1299 contact_list_store_ordered_presence (McPresence state)
1302 case MC_PRESENCE_UNSET:
1303 case MC_PRESENCE_OFFLINE:
1305 case MC_PRESENCE_AVAILABLE:
1307 case MC_PRESENCE_AWAY:
1309 case MC_PRESENCE_EXTENDED_AWAY:
1311 case MC_PRESENCE_HIDDEN:
1313 case MC_PRESENCE_DO_NOT_DISTURB:
1316 g_return_val_if_reached (6);
1321 contact_list_store_state_sort_func (GtkTreeModel *model,
1322 GtkTreeIter *iter_a,
1323 GtkTreeIter *iter_b,
1327 gchar *name_a, *name_b;
1328 gboolean is_separator_a, is_separator_b;
1329 EmpathyContact *contact_a, *contact_b;
1330 guint presence_a, presence_b;
1332 gtk_tree_model_get (model, iter_a,
1333 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1334 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1335 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1337 gtk_tree_model_get (model, iter_b,
1338 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1339 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1340 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1343 /* Separator or group? */
1344 if (is_separator_a || is_separator_b) {
1345 if (is_separator_a) {
1347 } else if (is_separator_b) {
1350 } else if (!contact_a && contact_b) {
1352 } else if (contact_a && !contact_b) {
1354 } else if (!contact_a && !contact_b) {
1356 ret_val = g_utf8_collate (name_a, name_b);
1363 /* If we managed to get this far, we can start looking at
1366 presence_a = empathy_contact_get_presence (EMPATHY_CONTACT (contact_a));
1367 presence_a = contact_list_store_ordered_presence (presence_a);
1368 presence_b = empathy_contact_get_presence (EMPATHY_CONTACT (contact_b));
1369 presence_b = contact_list_store_ordered_presence (presence_b);
1371 if (presence_a < presence_b) {
1373 } else if (presence_a > presence_b) {
1376 /* Fallback: compare by name */
1377 ret_val = g_utf8_collate (name_a, name_b);
1385 g_object_unref (contact_a);
1389 g_object_unref (contact_b);
1396 contact_list_store_name_sort_func (GtkTreeModel *model,
1397 GtkTreeIter *iter_a,
1398 GtkTreeIter *iter_b,
1401 gchar *name_a, *name_b;
1402 EmpathyContact *contact_a, *contact_b;
1403 gboolean is_separator_a, is_separator_b;
1406 gtk_tree_model_get (model, iter_a,
1407 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1408 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1409 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1411 gtk_tree_model_get (model, iter_b,
1412 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1413 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1414 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1417 /* If contact is NULL it means it's a group. */
1419 if (is_separator_a || is_separator_b) {
1420 if (is_separator_a) {
1422 } else if (is_separator_b) {
1425 } else if (!contact_a && contact_b) {
1427 } else if (contact_a && !contact_b) {
1430 ret_val = g_utf8_collate (name_a, name_b);
1437 g_object_unref (contact_a);
1441 g_object_unref (contact_b);
1448 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1453 EmpathyContact *contact;
1455 gtk_tree_model_get (model, iter,
1456 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1459 if (contact == fc->contact) {
1461 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1465 g_object_unref (contact);
1472 contact_list_store_find_contact (EmpathyContactListStore *store,
1473 EmpathyContact *contact)
1475 EmpathyContactListStorePriv *priv;
1476 GtkTreeModel *model;
1480 priv = GET_PRIV (store);
1482 memset (&fc, 0, sizeof (fc));
1484 fc.contact = contact;
1486 model = GTK_TREE_MODEL (store);
1487 gtk_tree_model_foreach (model,
1488 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1499 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1502 EmpathyContactListStore *store)
1504 EmpathyContactListStorePriv *priv;
1505 gboolean show_avatar = FALSE;
1507 priv = GET_PRIV (store);
1509 if (priv->show_avatars && !priv->is_compact) {
1513 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1514 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1515 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,