1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
33 #include <telepathy-glib/util.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-enum-types.h>
37 #include <libempathy/empathy-contact-manager.h>
39 #include "empathy-contact-list-store.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-gtk-enum-types.h"
43 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
44 #include <libempathy/empathy-debug.h>
46 /* Active users are those which have recently changed state
47 * (e.g. online, offline or from normal to a busy state).
50 /* Time in seconds user is shown as active */
51 #define ACTIVE_USER_SHOW_TIME 7
53 /* Time in seconds after connecting which we wait before active users are enabled */
54 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListStore)
58 EmpathyContactList *list;
59 gboolean show_offline;
60 gboolean show_avatars;
64 EmpathyContactListStoreSort sort_criterium;
66 } EmpathyContactListStorePriv;
75 EmpathyContact *contact;
81 EmpathyContactListStore *store;
82 EmpathyContact *contact;
86 static void contact_list_store_finalize (GObject *object);
87 static void contact_list_store_get_property (GObject *object,
91 static void contact_list_store_set_property (GObject *object,
95 static void contact_list_store_setup (EmpathyContactListStore *store);
96 static gboolean contact_list_store_inibit_active_cb (EmpathyContactListStore *store);
97 static void contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
98 EmpathyContact *contact,
99 EmpathyContact *actor,
103 EmpathyContactListStore *store);
104 static void contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
105 EmpathyContact *contact,
108 EmpathyContactListStore *store);
109 static void contact_list_store_add_contact (EmpathyContactListStore *store,
110 EmpathyContact *contact);
111 static void contact_list_store_remove_contact (EmpathyContactListStore *store,
112 EmpathyContact *contact);
113 static void contact_list_store_contact_update (EmpathyContactListStore *store,
114 EmpathyContact *contact);
115 static void contact_list_store_contact_updated_cb (EmpathyContact *contact,
117 EmpathyContactListStore *store);
118 static void contact_list_store_contact_set_active (EmpathyContactListStore *store,
119 EmpathyContact *contact,
121 gboolean set_changed);
122 static ShowActiveData * contact_list_store_contact_active_new (EmpathyContactListStore *store,
123 EmpathyContact *contact,
125 static void contact_list_store_contact_active_free (ShowActiveData *data);
126 static gboolean contact_list_store_contact_active_cb (ShowActiveData *data);
127 static gboolean contact_list_store_get_group_foreach (GtkTreeModel *model,
131 static void contact_list_store_get_group (EmpathyContactListStore *store,
133 GtkTreeIter *iter_group_to_set,
134 GtkTreeIter *iter_separator_to_set,
136 static gint contact_list_store_state_sort_func (GtkTreeModel *model,
140 static gint contact_list_store_name_sort_func (GtkTreeModel *model,
144 static gboolean contact_list_store_find_contact_foreach (GtkTreeModel *model,
148 static GList * contact_list_store_find_contact (EmpathyContactListStore *store,
149 EmpathyContact *contact);
150 static gboolean contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
153 EmpathyContactListStore *store);
165 G_DEFINE_TYPE (EmpathyContactListStore, empathy_contact_list_store, GTK_TYPE_TREE_STORE);
168 contact_list_store_iface_setup (gpointer user_data)
170 EmpathyContactListStore *store = user_data;
171 EmpathyContactListStorePriv *priv = GET_PRIV (store);
174 /* Signal connection. */
175 g_signal_connect (priv->list,
177 G_CALLBACK (contact_list_store_members_changed_cb),
179 g_signal_connect (priv->list,
181 G_CALLBACK (contact_list_store_groups_changed_cb),
184 /* Add contacts already created. */
185 contacts = empathy_contact_list_get_members (priv->list);
186 for (l = contacts; l; l = l->next) {
187 contact_list_store_members_changed_cb (priv->list, l->data,
192 g_object_unref (l->data);
194 g_list_free (contacts);
201 contact_list_store_set_contact_list (EmpathyContactListStore *store,
202 EmpathyContactList *list_iface)
204 EmpathyContactListStorePriv *priv = GET_PRIV (store);
206 priv->list = g_object_ref (list_iface);
208 /* Let a chance to have all properties set before populating */
209 g_idle_add (contact_list_store_iface_setup, store);
213 empathy_contact_list_store_class_init (EmpathyContactListStoreClass *klass)
215 GObjectClass *object_class = G_OBJECT_CLASS (klass);
217 object_class->finalize = contact_list_store_finalize;
218 object_class->get_property = contact_list_store_get_property;
219 object_class->set_property = contact_list_store_set_property;
221 g_object_class_install_property (object_class,
223 g_param_spec_object ("contact-list",
224 "The contact list iface",
225 "The contact list iface",
226 EMPATHY_TYPE_CONTACT_LIST,
227 G_PARAM_CONSTRUCT_ONLY |
229 g_object_class_install_property (object_class,
231 g_param_spec_boolean ("show-offline",
233 "Whether contact list should display "
237 g_object_class_install_property (object_class,
239 g_param_spec_boolean ("show-avatars",
241 "Whether contact list should display "
242 "avatars for contacts",
245 g_object_class_install_property (object_class,
247 g_param_spec_boolean ("show-groups",
249 "Whether contact list should display "
253 g_object_class_install_property (object_class,
255 g_param_spec_boolean ("is-compact",
257 "Whether the contact list is in compact mode or not",
261 g_object_class_install_property (object_class,
263 g_param_spec_enum ("sort-criterium",
265 "The sort criterium to use for sorting the contact list",
266 EMPATHY_TYPE_CONTACT_LIST_STORE_SORT,
267 EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
270 g_type_class_add_private (object_class, sizeof (EmpathyContactListStorePriv));
274 empathy_contact_list_store_init (EmpathyContactListStore *store)
276 EmpathyContactListStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (store,
277 EMPATHY_TYPE_CONTACT_LIST_STORE, EmpathyContactListStorePriv);
280 priv->show_avatars = TRUE;
281 priv->show_groups = TRUE;
282 priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
283 (GSourceFunc) contact_list_store_inibit_active_cb,
285 contact_list_store_setup (store);
289 contact_list_store_finalize (GObject *object)
291 EmpathyContactListStorePriv *priv = GET_PRIV (object);
294 contacts = empathy_contact_list_get_members (priv->list);
295 for (l = contacts; l; l = l->next) {
296 g_signal_handlers_disconnect_by_func (l->data,
297 G_CALLBACK (contact_list_store_contact_updated_cb),
300 g_object_unref (l->data);
302 g_list_free (contacts);
304 g_signal_handlers_disconnect_by_func (priv->list,
305 G_CALLBACK (contact_list_store_members_changed_cb),
307 g_signal_handlers_disconnect_by_func (priv->list,
308 G_CALLBACK (contact_list_store_groups_changed_cb),
310 g_object_unref (priv->list);
312 if (priv->inhibit_active) {
313 g_source_remove (priv->inhibit_active);
316 G_OBJECT_CLASS (empathy_contact_list_store_parent_class)->finalize (object);
320 contact_list_store_get_property (GObject *object,
325 EmpathyContactListStorePriv *priv;
327 priv = GET_PRIV (object);
330 case PROP_CONTACT_LIST:
331 g_value_set_object (value, priv->list);
333 case PROP_SHOW_OFFLINE:
334 g_value_set_boolean (value, priv->show_offline);
336 case PROP_SHOW_AVATARS:
337 g_value_set_boolean (value, priv->show_avatars);
339 case PROP_SHOW_GROUPS:
340 g_value_set_boolean (value, priv->show_groups);
342 case PROP_IS_COMPACT:
343 g_value_set_boolean (value, priv->is_compact);
345 case PROP_SORT_CRITERIUM:
346 g_value_set_enum (value, priv->sort_criterium);
349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
355 contact_list_store_set_property (GObject *object,
360 EmpathyContactListStorePriv *priv;
362 priv = GET_PRIV (object);
365 case PROP_CONTACT_LIST:
366 contact_list_store_set_contact_list (EMPATHY_CONTACT_LIST_STORE (object),
367 g_value_get_object (value));
369 case PROP_SHOW_OFFLINE:
370 empathy_contact_list_store_set_show_offline (EMPATHY_CONTACT_LIST_STORE (object),
371 g_value_get_boolean (value));
373 case PROP_SHOW_AVATARS:
374 empathy_contact_list_store_set_show_avatars (EMPATHY_CONTACT_LIST_STORE (object),
375 g_value_get_boolean (value));
377 case PROP_SHOW_GROUPS:
378 empathy_contact_list_store_set_show_groups (EMPATHY_CONTACT_LIST_STORE (object),
379 g_value_get_boolean (value));
381 case PROP_IS_COMPACT:
382 empathy_contact_list_store_set_is_compact (EMPATHY_CONTACT_LIST_STORE (object),
383 g_value_get_boolean (value));
385 case PROP_SORT_CRITERIUM:
386 empathy_contact_list_store_set_sort_criterium (EMPATHY_CONTACT_LIST_STORE (object),
387 g_value_get_enum (value));
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
395 EmpathyContactListStore *
396 empathy_contact_list_store_new (EmpathyContactList *list_iface)
398 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list_iface), NULL);
400 return g_object_new (EMPATHY_TYPE_CONTACT_LIST_STORE,
401 "contact-list", list_iface,
406 empathy_contact_list_store_get_list_iface (EmpathyContactListStore *store)
408 EmpathyContactListStorePriv *priv;
410 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
412 priv = GET_PRIV (store);
418 empathy_contact_list_store_get_show_offline (EmpathyContactListStore *store)
420 EmpathyContactListStorePriv *priv;
422 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), FALSE);
424 priv = GET_PRIV (store);
426 return priv->show_offline;
430 empathy_contact_list_store_set_show_offline (EmpathyContactListStore *store,
431 gboolean show_offline)
433 EmpathyContactListStorePriv *priv;
435 gboolean show_active;
437 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
439 priv = GET_PRIV (store);
441 priv->show_offline = show_offline;
442 show_active = priv->show_active;
444 /* Disable temporarily. */
445 priv->show_active = FALSE;
447 contacts = empathy_contact_list_get_members (priv->list);
448 for (l = contacts; l; l = l->next) {
449 contact_list_store_contact_update (store, l->data);
451 g_object_unref (l->data);
453 g_list_free (contacts);
455 /* Restore to original setting. */
456 priv->show_active = show_active;
458 g_object_notify (G_OBJECT (store), "show-offline");
462 empathy_contact_list_store_get_show_avatars (EmpathyContactListStore *store)
464 EmpathyContactListStorePriv *priv;
466 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
468 priv = GET_PRIV (store);
470 return priv->show_avatars;
474 empathy_contact_list_store_set_show_avatars (EmpathyContactListStore *store,
475 gboolean show_avatars)
477 EmpathyContactListStorePriv *priv;
480 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
482 priv = GET_PRIV (store);
484 priv->show_avatars = show_avatars;
486 model = GTK_TREE_MODEL (store);
488 gtk_tree_model_foreach (model,
489 (GtkTreeModelForeachFunc)
490 contact_list_store_update_list_mode_foreach,
493 g_object_notify (G_OBJECT (store), "show-avatars");
497 empathy_contact_list_store_get_show_groups (EmpathyContactListStore *store)
499 EmpathyContactListStorePriv *priv;
501 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
503 priv = GET_PRIV (store);
505 return priv->show_groups;
509 empathy_contact_list_store_set_show_groups (EmpathyContactListStore *store,
510 gboolean show_groups)
512 EmpathyContactListStorePriv *priv;
515 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
517 priv = GET_PRIV (store);
519 if (priv->show_groups == show_groups) {
523 priv->show_groups = show_groups;
525 /* Remove all contacts and add them back, not optimized but that's the
527 gtk_tree_store_clear (GTK_TREE_STORE (store));
528 contacts = empathy_contact_list_get_members (priv->list);
529 for (l = contacts; l; l = l->next) {
530 contact_list_store_members_changed_cb (priv->list, l->data,
535 g_object_unref (l->data);
537 g_list_free (contacts);
539 g_object_notify (G_OBJECT (store), "show-groups");
543 empathy_contact_list_store_get_is_compact (EmpathyContactListStore *store)
545 EmpathyContactListStorePriv *priv;
547 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), TRUE);
549 priv = GET_PRIV (store);
551 return priv->is_compact;
555 empathy_contact_list_store_set_is_compact (EmpathyContactListStore *store,
558 EmpathyContactListStorePriv *priv;
561 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
563 priv = GET_PRIV (store);
565 priv->is_compact = is_compact;
567 model = GTK_TREE_MODEL (store);
569 gtk_tree_model_foreach (model,
570 (GtkTreeModelForeachFunc)
571 contact_list_store_update_list_mode_foreach,
574 g_object_notify (G_OBJECT (store), "is-compact");
577 EmpathyContactListStoreSort
578 empathy_contact_list_store_get_sort_criterium (EmpathyContactListStore *store)
580 EmpathyContactListStorePriv *priv;
582 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), 0);
584 priv = GET_PRIV (store);
586 return priv->sort_criterium;
590 empathy_contact_list_store_set_sort_criterium (EmpathyContactListStore *store,
591 EmpathyContactListStoreSort sort_criterium)
593 EmpathyContactListStorePriv *priv;
595 g_return_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store));
597 priv = GET_PRIV (store);
599 priv->sort_criterium = sort_criterium;
601 switch (sort_criterium) {
602 case EMPATHY_CONTACT_LIST_STORE_SORT_STATE:
603 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
604 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
608 case EMPATHY_CONTACT_LIST_STORE_SORT_NAME:
609 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
610 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
615 g_object_notify (G_OBJECT (store), "sort-criterium");
619 empathy_contact_list_store_row_separator_func (GtkTreeModel *model,
623 gboolean is_separator = FALSE;
625 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
627 gtk_tree_model_get (model, iter,
628 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
635 empathy_contact_list_store_get_parent_group (GtkTreeModel *model,
637 gboolean *path_is_group)
639 GtkTreeIter parent_iter, iter;
643 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
646 *path_is_group = FALSE;
649 if (!gtk_tree_model_get_iter (model, &iter, path)) {
653 gtk_tree_model_get (model, &iter,
654 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
655 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
662 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
668 gtk_tree_model_get (model, &iter,
669 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
670 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
679 *path_is_group = TRUE;
686 empathy_contact_list_store_search_equal_func (GtkTreeModel *model,
690 gpointer search_data)
692 gchar *name, *name_folded;
696 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
702 gtk_tree_model_get (model, iter,
703 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
710 name_folded = g_utf8_casefold (name, -1);
711 key_folded = g_utf8_casefold (key, -1);
713 if (name_folded && key_folded &&
714 strstr (name_folded, key_folded)) {
721 g_free (name_folded);
728 contact_list_store_setup (EmpathyContactListStore *store)
730 EmpathyContactListStorePriv *priv;
732 G_TYPE_STRING, /* Status icon-name */
733 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
734 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
735 G_TYPE_STRING, /* Name */
736 G_TYPE_STRING, /* Status string */
737 G_TYPE_BOOLEAN, /* Show status */
738 EMPATHY_TYPE_CONTACT, /* Contact type */
739 G_TYPE_BOOLEAN, /* Is group */
740 G_TYPE_BOOLEAN, /* Is active */
741 G_TYPE_BOOLEAN, /* Is online */
742 G_TYPE_BOOLEAN, /* Is separator */
743 G_TYPE_BOOLEAN, /* Can make audio calls */
744 G_TYPE_BOOLEAN, /* Can make video calls */
745 EMPATHY_TYPE_CONTACT_LIST_FLAGS, /* Flags */
748 priv = GET_PRIV (store);
750 gtk_tree_store_set_column_types (GTK_TREE_STORE (store),
751 EMPATHY_CONTACT_LIST_STORE_COL_COUNT,
755 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
756 EMPATHY_CONTACT_LIST_STORE_COL_NAME,
757 contact_list_store_name_sort_func,
759 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
760 EMPATHY_CONTACT_LIST_STORE_COL_STATUS,
761 contact_list_store_state_sort_func,
764 priv->sort_criterium = EMPATHY_CONTACT_LIST_STORE_SORT_NAME;
765 empathy_contact_list_store_set_sort_criterium (store, priv->sort_criterium);
769 contact_list_store_inibit_active_cb (EmpathyContactListStore *store)
771 EmpathyContactListStorePriv *priv;
773 priv = GET_PRIV (store);
775 priv->show_active = TRUE;
776 priv->inhibit_active = 0;
782 contact_list_store_members_changed_cb (EmpathyContactList *list_iface,
783 EmpathyContact *contact,
784 EmpathyContact *actor,
788 EmpathyContactListStore *store)
790 EmpathyContactListStorePriv *priv;
792 priv = GET_PRIV (store);
794 DEBUG ("Contact %s (%d) %s",
795 empathy_contact_get_id (contact),
796 empathy_contact_get_handle (contact),
797 is_member ? "added" : "removed");
800 g_signal_connect (contact, "notify::presence",
801 G_CALLBACK (contact_list_store_contact_updated_cb),
803 g_signal_connect (contact, "notify::presence-message",
804 G_CALLBACK (contact_list_store_contact_updated_cb),
806 g_signal_connect (contact, "notify::name",
807 G_CALLBACK (contact_list_store_contact_updated_cb),
809 g_signal_connect (contact, "notify::avatar",
810 G_CALLBACK (contact_list_store_contact_updated_cb),
812 g_signal_connect (contact, "notify::capabilities",
813 G_CALLBACK (contact_list_store_contact_updated_cb),
816 contact_list_store_add_contact (store, contact);
818 g_signal_handlers_disconnect_by_func (contact,
819 G_CALLBACK (contact_list_store_contact_updated_cb),
822 contact_list_store_remove_contact (store, contact);
827 contact_list_store_groups_changed_cb (EmpathyContactList *list_iface,
828 EmpathyContact *contact,
831 EmpathyContactListStore *store)
833 EmpathyContactListStorePriv *priv;
834 gboolean show_active;
836 priv = GET_PRIV (store);
838 DEBUG ("Updating groups for contact %s (%d)",
839 empathy_contact_get_id (contact),
840 empathy_contact_get_handle (contact));
842 /* We do this to make sure the groups are correct, if not, we
843 * would have to check the groups already set up for each
844 * contact and then see what has been updated.
846 show_active = priv->show_active;
847 priv->show_active = FALSE;
848 contact_list_store_remove_contact (store, contact);
849 contact_list_store_add_contact (store, contact);
850 priv->show_active = show_active;
854 contact_list_store_add_contact (EmpathyContactListStore *store,
855 EmpathyContact *contact)
857 EmpathyContactListStorePriv *priv;
859 GList *groups = NULL, *l;
860 TpConnection *connection;
861 EmpathyContactListFlags flags = 0;
863 priv = GET_PRIV (store);
865 if (EMP_STR_EMPTY (empathy_contact_get_name (contact)) ||
866 (!priv->show_offline && !empathy_contact_is_online (contact))) {
870 if (priv->show_groups) {
871 groups = empathy_contact_list_get_groups (priv->list, contact);
874 connection = empathy_contact_get_connection (contact);
875 if (EMPATHY_IS_CONTACT_MANAGER (priv->list)) {
876 flags = empathy_contact_manager_get_flags_for_connection (
877 EMPATHY_CONTACT_MANAGER (priv->list), connection);
879 /* If no groups just add it at the top level. */
882 GtkTreeModel *model = GTK_TREE_MODEL (store);
884 if (gtk_tree_model_get_iter_first (model, &iter)) do {
887 gtk_tree_model_get (model, &iter,
888 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &c,
897 } while (gtk_tree_model_iter_next (model, &iter));
899 gtk_tree_store_append (GTK_TREE_STORE (store), &iter, NULL);
900 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
901 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
902 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
903 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
904 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
905 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
906 empathy_contact_get_capabilities (contact) &
907 EMPATHY_CAPABILITIES_AUDIO,
908 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
909 empathy_contact_get_capabilities (contact) &
910 EMPATHY_CAPABILITIES_VIDEO,
911 EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
915 /* Else add to each group. */
916 for (l = groups; l; l = l->next) {
917 GtkTreeIter iter_group;
919 contact_list_store_get_group (store, l->data, &iter_group, NULL, NULL);
921 gtk_tree_store_insert_after (GTK_TREE_STORE (store), &iter,
923 gtk_tree_store_set (GTK_TREE_STORE (store), &iter,
924 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
925 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, contact,
926 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
927 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
928 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
929 empathy_contact_get_capabilities (contact) &
930 EMPATHY_CAPABILITIES_AUDIO,
931 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
932 empathy_contact_get_capabilities (contact) &
933 EMPATHY_CAPABILITIES_VIDEO,
934 EMPATHY_CONTACT_LIST_STORE_COL_FLAGS, flags,
938 g_list_free (groups);
940 contact_list_store_contact_update (store, contact);
945 contact_list_store_remove_contact (EmpathyContactListStore *store,
946 EmpathyContact *contact)
948 EmpathyContactListStorePriv *priv;
952 priv = GET_PRIV (store);
954 iters = contact_list_store_find_contact (store, contact);
960 model = GTK_TREE_MODEL (store);
962 for (l = iters; l; l = l->next) {
965 /* NOTE: it is only <= 2 here because we have
966 * separators after the group name, otherwise it
969 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
970 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
971 gtk_tree_store_remove (GTK_TREE_STORE (store), &parent);
973 gtk_tree_store_remove (GTK_TREE_STORE (store), l->data);
977 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
982 contact_list_store_contact_update (EmpathyContactListStore *store,
983 EmpathyContact *contact)
985 EmpathyContactListStorePriv *priv;
986 ShowActiveData *data;
990 gboolean should_be_in_list;
991 gboolean was_online = TRUE;
992 gboolean now_online = FALSE;
993 gboolean set_model = FALSE;
994 gboolean do_remove = FALSE;
995 gboolean do_set_active = FALSE;
996 gboolean do_set_refresh = FALSE;
997 gboolean show_avatar = FALSE;
998 GdkPixbuf *pixbuf_avatar;
1000 priv = GET_PRIV (store);
1002 model = GTK_TREE_MODEL (store);
1004 iters = contact_list_store_find_contact (store, contact);
1011 /* Get online state now. */
1012 now_online = empathy_contact_is_online (contact);
1014 if (priv->show_offline || now_online) {
1015 should_be_in_list = TRUE;
1017 should_be_in_list = FALSE;
1020 if (!in_list && !should_be_in_list) {
1021 /* Nothing to do. */
1022 DEBUG ("Contact:'%s' in list:NO, should be:NO",
1023 empathy_contact_get_name (contact));
1025 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1026 g_list_free (iters);
1029 else if (in_list && !should_be_in_list) {
1030 DEBUG ("Contact:'%s' in list:YES, should be:NO",
1031 empathy_contact_get_name (contact));
1033 if (priv->show_active) {
1035 do_set_active = TRUE;
1036 do_set_refresh = TRUE;
1039 DEBUG ("Remove item (after timeout)");
1041 DEBUG ("Remove item (now)!");
1042 contact_list_store_remove_contact (store, contact);
1045 else if (!in_list && should_be_in_list) {
1046 DEBUG ("Contact:'%s' in list:NO, should be:YES",
1047 empathy_contact_get_name (contact));
1049 contact_list_store_add_contact (store, contact);
1051 if (priv->show_active) {
1052 do_set_active = TRUE;
1054 DEBUG ("Set active (contact added)");
1057 DEBUG ("Contact:'%s' in list:YES, should be:YES",
1058 empathy_contact_get_name (contact));
1060 /* Get online state before. */
1061 if (iters && g_list_length (iters) > 0) {
1062 gtk_tree_model_get (model, iters->data,
1063 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, &was_online,
1067 /* Is this really an update or an online/offline. */
1068 if (priv->show_active) {
1069 if (was_online != now_online) {
1070 do_set_active = TRUE;
1071 do_set_refresh = TRUE;
1073 DEBUG ("Set active (contact updated %s)",
1074 was_online ? "online -> offline" :
1075 "offline -> online");
1077 /* Was TRUE for presence updates. */
1078 /* do_set_active = FALSE; */
1079 do_set_refresh = TRUE;
1081 DEBUG ("Set active (contact updated)");
1088 if (priv->show_avatars && !priv->is_compact) {
1091 pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1092 for (l = iters; l && set_model; l = l->next) {
1093 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1094 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, empathy_icon_name_for_contact (contact),
1095 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
1096 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1097 EMPATHY_CONTACT_LIST_STORE_COL_NAME, empathy_contact_get_name (contact),
1098 EMPATHY_CONTACT_LIST_STORE_COL_STATUS, empathy_contact_get_status (contact),
1099 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,
1100 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, FALSE,
1101 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, now_online,
1102 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1103 EMPATHY_CONTACT_LIST_STORE_COL_CAN_AUDIO_CALL,
1104 empathy_contact_get_capabilities (contact) &
1105 EMPATHY_CAPABILITIES_AUDIO,
1106 EMPATHY_CONTACT_LIST_STORE_COL_CAN_VIDEO_CALL,
1107 empathy_contact_get_capabilities (contact) &
1108 EMPATHY_CAPABILITIES_VIDEO,
1112 if (pixbuf_avatar) {
1113 g_object_unref (pixbuf_avatar);
1116 if (priv->show_active && do_set_active) {
1117 contact_list_store_contact_set_active (store, contact, do_set_active, do_set_refresh);
1119 if (do_set_active) {
1120 data = contact_list_store_contact_active_new (store, contact, do_remove);
1121 g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
1122 (GSourceFunc) contact_list_store_contact_active_cb,
1127 /* FIXME: when someone goes online then offline quickly, the
1128 * first timeout sets the user to be inactive and the second
1129 * timeout removes the user from the contact list, really we
1130 * should remove the first timeout.
1132 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1133 g_list_free (iters);
1137 contact_list_store_contact_updated_cb (EmpathyContact *contact,
1139 EmpathyContactListStore *store)
1141 DEBUG ("Contact:'%s' updated, checking roster is in sync...",
1142 empathy_contact_get_name (contact));
1144 contact_list_store_contact_update (store, contact);
1148 contact_list_store_contact_set_active (EmpathyContactListStore *store,
1149 EmpathyContact *contact,
1151 gboolean set_changed)
1153 EmpathyContactListStorePriv *priv;
1154 GtkTreeModel *model;
1157 priv = GET_PRIV (store);
1158 model = GTK_TREE_MODEL (store);
1160 iters = contact_list_store_find_contact (store, contact);
1161 for (l = iters; l; l = l->next) {
1164 gtk_tree_store_set (GTK_TREE_STORE (store), l->data,
1165 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, active,
1168 DEBUG ("Set item %s", active ? "active" : "inactive");
1171 path = gtk_tree_model_get_path (model, l->data);
1172 gtk_tree_model_row_changed (model, path, l->data);
1173 gtk_tree_path_free (path);
1177 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1178 g_list_free (iters);
1182 static ShowActiveData *
1183 contact_list_store_contact_active_new (EmpathyContactListStore *store,
1184 EmpathyContact *contact,
1187 ShowActiveData *data;
1189 DEBUG ("Contact:'%s' now active, and %s be removed",
1190 empathy_contact_get_name (contact),
1191 remove ? "WILL" : "WILL NOT");
1193 data = g_slice_new0 (ShowActiveData);
1195 data->store = g_object_ref (store);
1196 data->contact = g_object_ref (contact);
1197 data->remove = remove;
1203 contact_list_store_contact_active_free (ShowActiveData *data)
1205 g_object_unref (data->contact);
1206 g_object_unref (data->store);
1208 g_slice_free (ShowActiveData, data);
1212 contact_list_store_contact_active_cb (ShowActiveData *data)
1214 EmpathyContactListStorePriv *priv;
1216 priv = GET_PRIV (data->store);
1219 !priv->show_offline &&
1220 !empathy_contact_is_online (data->contact)) {
1221 DEBUG ("Contact:'%s' active timeout, removing item",
1222 empathy_contact_get_name (data->contact));
1223 contact_list_store_remove_contact (data->store, data->contact);
1226 DEBUG ("Contact:'%s' no longer active",
1227 empathy_contact_get_name (data->contact));
1229 contact_list_store_contact_set_active (data->store,
1234 contact_list_store_contact_active_free (data);
1240 contact_list_store_get_group_foreach (GtkTreeModel *model,
1248 /* Groups are only at the top level. */
1249 if (gtk_tree_path_get_depth (path) != 1) {
1253 gtk_tree_model_get (model, iter,
1254 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &str,
1255 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, &is_group,
1258 if (is_group && !tp_strdiff (str, fg->name)) {
1269 contact_list_store_get_group (EmpathyContactListStore *store,
1271 GtkTreeIter *iter_group_to_set,
1272 GtkTreeIter *iter_separator_to_set,
1275 EmpathyContactListStorePriv *priv;
1276 GtkTreeModel *model;
1277 GtkTreeIter iter_group;
1278 GtkTreeIter iter_separator;
1281 priv = GET_PRIV (store);
1283 memset (&fg, 0, sizeof (fg));
1287 model = GTK_TREE_MODEL (store);
1288 gtk_tree_model_foreach (model,
1289 (GtkTreeModelForeachFunc) contact_list_store_get_group_foreach,
1297 gtk_tree_store_append (GTK_TREE_STORE (store), &iter_group, NULL);
1298 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_group,
1299 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL,
1300 EMPATHY_CONTACT_LIST_STORE_COL_NAME, name,
1301 EMPATHY_CONTACT_LIST_STORE_COL_IS_GROUP, TRUE,
1302 EMPATHY_CONTACT_LIST_STORE_COL_IS_ACTIVE, FALSE,
1303 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, FALSE,
1306 if (iter_group_to_set) {
1307 *iter_group_to_set = iter_group;
1310 gtk_tree_store_append (GTK_TREE_STORE (store),
1313 gtk_tree_store_set (GTK_TREE_STORE (store), &iter_separator,
1314 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, TRUE,
1317 if (iter_separator_to_set) {
1318 *iter_separator_to_set = iter_separator;
1325 if (iter_group_to_set) {
1326 *iter_group_to_set = fg.iter;
1329 iter_separator = fg.iter;
1331 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1332 gboolean is_separator;
1334 gtk_tree_model_get (model, &iter_separator,
1335 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator,
1338 if (is_separator && iter_separator_to_set) {
1339 *iter_separator_to_set = iter_separator;
1346 contact_list_store_state_sort_func (GtkTreeModel *model,
1347 GtkTreeIter *iter_a,
1348 GtkTreeIter *iter_b,
1352 gchar *name_a, *name_b;
1353 gboolean is_separator_a, is_separator_b;
1354 EmpathyContact *contact_a, *contact_b;
1356 gtk_tree_model_get (model, iter_a,
1357 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1358 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1359 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1361 gtk_tree_model_get (model, iter_b,
1362 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1363 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1364 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1367 /* Separator or group? */
1368 if (is_separator_a || is_separator_b) {
1369 if (is_separator_a) {
1371 } else if (is_separator_b) {
1374 } else if (!contact_a && contact_b) {
1376 } else if (contact_a && !contact_b) {
1378 } else if (!contact_a && !contact_b) {
1380 ret_val = g_utf8_collate (name_a, name_b);
1387 /* If we managed to get this far, we can start looking at
1390 ret_val = -tp_connection_presence_type_cmp_availability (
1391 empathy_contact_get_presence (EMPATHY_CONTACT (contact_a)),
1392 empathy_contact_get_presence (EMPATHY_CONTACT (contact_b)));
1395 /* Fallback: compare by name */
1396 ret_val = g_utf8_collate (name_a, name_b);
1404 g_object_unref (contact_a);
1408 g_object_unref (contact_b);
1415 contact_list_store_name_sort_func (GtkTreeModel *model,
1416 GtkTreeIter *iter_a,
1417 GtkTreeIter *iter_b,
1420 gchar *name_a, *name_b;
1421 EmpathyContact *contact_a, *contact_b;
1422 gboolean is_separator_a, is_separator_b;
1425 gtk_tree_model_get (model, iter_a,
1426 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_a,
1427 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_a,
1428 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_a,
1430 gtk_tree_model_get (model, iter_b,
1431 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name_b,
1432 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact_b,
1433 EMPATHY_CONTACT_LIST_STORE_COL_IS_SEPARATOR, &is_separator_b,
1436 /* If contact is NULL it means it's a group. */
1438 if (is_separator_a || is_separator_b) {
1439 if (is_separator_a) {
1441 } else if (is_separator_b) {
1444 } else if (!contact_a && contact_b) {
1446 } else if (contact_a && !contact_b) {
1449 ret_val = g_utf8_collate (name_a, name_b);
1456 g_object_unref (contact_a);
1460 g_object_unref (contact_b);
1467 contact_list_store_find_contact_foreach (GtkTreeModel *model,
1472 EmpathyContact *contact;
1474 gtk_tree_model_get (model, iter,
1475 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
1478 if (contact == fc->contact) {
1480 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
1484 g_object_unref (contact);
1491 contact_list_store_find_contact (EmpathyContactListStore *store,
1492 EmpathyContact *contact)
1494 EmpathyContactListStorePriv *priv;
1495 GtkTreeModel *model;
1499 priv = GET_PRIV (store);
1501 memset (&fc, 0, sizeof (fc));
1503 fc.contact = contact;
1505 model = GTK_TREE_MODEL (store);
1506 gtk_tree_model_foreach (model,
1507 (GtkTreeModelForeachFunc) contact_list_store_find_contact_foreach,
1518 contact_list_store_update_list_mode_foreach (GtkTreeModel *model,
1521 EmpathyContactListStore *store)
1523 EmpathyContactListStorePriv *priv;
1524 gboolean show_avatar = FALSE;
1526 priv = GET_PRIV (store);
1528 if (priv->show_avatars && !priv->is_compact) {
1532 gtk_tree_store_set (GTK_TREE_STORE (store), iter,
1533 EMPATHY_CONTACT_LIST_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1534 EMPATHY_CONTACT_LIST_STORE_COL_STATUS_VISIBLE, !priv->is_compact,