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-2010 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>
24 * Travis Reitter <travis.reitter@collabora.co.uk>
32 #include <glib/gi18n-lib.h>
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-enum-types.h>
41 #include <libempathy/empathy-individual-manager.h>
43 #include "empathy-individual-store.h"
44 #include "empathy-ui-utils.h"
45 #include "empathy-gtk-enum-types.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
48 #include <libempathy/empathy-debug.h>
50 /* Active users are those which have recently changed state
51 * (e.g. online, offline or from normal to a busy state).
54 /* Time in seconds user is shown as active */
55 #define ACTIVE_USER_SHOW_TIME 7
57 /* Time in seconds after connecting which we wait before active users are enabled */
58 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualStore)
63 EmpathyIndividualManager *manager;
64 gboolean show_avatars;
67 gboolean show_protocols;
69 EmpathyIndividualStoreSort sort_criterium;
72 gboolean dispose_has_run;
73 GHashTable *status_icons;
74 /* List of owned GCancellables for each pending avatar load operation */
75 GList *avatar_cancellables;
76 /* Hash: FolksIndividual* -> GQueue (GtkTreeIter *) */
77 GHashTable *folks_individual_cache;
78 /* Hash: char *groupname -> GtkTreeIter * */
79 GHashTable *empathy_group_cache;
80 } EmpathyIndividualStorePriv;
84 EmpathyIndividualStore *self;
85 FolksIndividual *individual;
93 PROP_INDIVIDUAL_MANAGER,
101 /* prototypes to break cycles */
102 static void individual_store_contact_update (EmpathyIndividualStore *self,
103 FolksIndividual *individual);
105 G_DEFINE_TYPE (EmpathyIndividualStore, empathy_individual_store,
106 GTK_TYPE_TREE_STORE);
108 /* Calculate whether the Individual can do audio or video calls.
109 * FIXME: We can remove this once libfolks has grown capabilities support
110 * again: bgo#626179. */
112 individual_can_audio_video_call (FolksIndividual *individual,
113 gboolean *can_audio_call,
114 gboolean *can_video_call)
118 gboolean can_audio = FALSE, can_video = FALSE;
120 personas = folks_individual_get_personas (individual);
121 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
122 while (gee_iterator_next (iter))
124 FolksPersona *persona = gee_iterator_get (iter);
125 TpContact *tp_contact;
127 if (!empathy_folks_persona_is_interesting (persona))
130 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
131 if (tp_contact != NULL)
133 EmpathyContact *contact;
135 contact = empathy_contact_dup_from_tp_contact (tp_contact);
136 empathy_contact_set_persona (contact, persona);
138 can_audio = can_audio || empathy_contact_get_capabilities (contact) &
139 EMPATHY_CAPABILITIES_AUDIO;
140 can_video = can_video || empathy_contact_get_capabilities (contact) &
141 EMPATHY_CAPABILITIES_VIDEO;
143 g_object_unref (contact);
146 g_clear_object (&persona);
148 if (can_audio && can_video)
151 g_clear_object (&iter);
153 *can_audio_call = can_audio;
154 *can_video_call = can_video;
157 static const gchar * const *
158 individual_get_client_types (FolksIndividual *individual)
162 const gchar * const *types = NULL;
163 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
165 personas = folks_individual_get_personas (individual);
166 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
167 while (gee_iterator_next (iter))
169 FolksPresenceDetails *presence;
170 FolksPersona *persona = gee_iterator_get (iter);
172 /* We only want personas which have presence and a TpContact */
173 if (!empathy_folks_persona_is_interesting (persona))
176 presence = FOLKS_PRESENCE_DETAILS (persona);
178 if (folks_presence_details_typecmp (
179 folks_presence_details_get_presence_type (presence),
182 TpContact *tp_contact;
184 presence_type = folks_presence_details_get_presence_type (presence);
186 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
187 if (tp_contact != NULL)
188 types = tp_contact_get_client_types (tp_contact);
192 g_clear_object (&persona);
194 g_clear_object (&iter);
200 add_individual_to_store (GtkTreeStore *self,
203 FolksIndividual *individual)
205 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
206 gboolean can_audio_call, can_video_call;
207 const gchar * const *types;
210 individual_can_audio_video_call (individual, &can_audio_call,
213 types = individual_get_client_types (individual);
215 gtk_tree_store_insert_with_values (self, iter, parent, 0,
216 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
217 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
218 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, individual,
219 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
220 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
221 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
222 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
223 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
226 queue = g_hash_table_lookup (priv->folks_individual_cache, individual);
229 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
233 queue = g_queue_new ();
234 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
235 g_hash_table_insert (priv->folks_individual_cache, individual,
241 individual_store_get_group (EmpathyIndividualStore *self,
243 GtkTreeIter *iter_group_to_set,
244 GtkTreeIter *iter_separator_to_set,
246 gboolean is_fake_group)
248 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
250 GtkTreeIter iter_group;
251 GtkTreeIter iter_separator;
254 model = GTK_TREE_MODEL (self);
255 iter = g_hash_table_lookup (priv->empathy_group_cache, name);
262 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_group,
264 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, NULL,
265 EMPATHY_INDIVIDUAL_STORE_COL_NAME, name,
266 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, TRUE,
267 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, FALSE,
268 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
269 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, is_fake_group,
272 g_hash_table_insert (priv->empathy_group_cache, g_strdup (name),
273 gtk_tree_iter_copy (&iter_group));
275 if (iter_group_to_set)
276 *iter_group_to_set = iter_group;
278 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_separator,
280 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, TRUE,
283 if (iter_separator_to_set)
284 *iter_separator_to_set = iter_separator;
291 if (iter_group_to_set)
292 *iter_group_to_set = *iter;
294 iter_separator = *iter;
296 if (gtk_tree_model_iter_next (model, &iter_separator))
298 gboolean is_separator;
300 gtk_tree_model_get (model, &iter_separator,
301 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
303 if (is_separator && iter_separator_to_set)
304 *iter_separator_to_set = iter_separator;
310 individual_store_find_contact (EmpathyIndividualStore *self,
311 FolksIndividual *individual)
313 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
314 GQueue *row_refs_queue;
316 GList *iters_list = NULL;
318 row_refs_queue = g_hash_table_lookup (priv->folks_individual_cache,
323 for (i = g_queue_peek_head_link (row_refs_queue) ; i != NULL ; i = i->next)
325 GtkTreeIter *iter = i->data;
327 iters_list = g_list_prepend (iters_list, gtk_tree_iter_copy (iter));
334 free_iters (GList *iters)
336 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
341 individual_store_remove_individual (EmpathyIndividualStore *self,
342 FolksIndividual *individual)
344 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
349 row_refs = g_hash_table_lookup (priv->folks_individual_cache, individual);
354 model = GTK_TREE_MODEL (self);
356 for (l = g_queue_peek_head_link (row_refs); l; l = l->next)
358 GtkTreeIter *iter = l->data;
361 /* NOTE: it is only <= 2 here because we have
362 * separators after the group name, otherwise it
365 if (gtk_tree_model_iter_parent (model, &parent, iter) &&
366 gtk_tree_model_iter_n_children (model, &parent) <= 2)
369 gtk_tree_model_get (model, &parent,
370 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &group_name,
372 g_hash_table_remove (priv->empathy_group_cache,
374 gtk_tree_store_remove (GTK_TREE_STORE (self), &parent);
378 gtk_tree_store_remove (GTK_TREE_STORE (self), iter);
382 g_hash_table_remove (priv->folks_individual_cache, individual);
386 individual_store_add_individual (EmpathyIndividualStore *self,
387 FolksIndividual *individual)
389 EmpathyIndividualStorePriv *priv;
391 GeeIterator *group_iter = NULL;
393 priv = GET_PRIV (self);
395 if (EMP_STR_EMPTY (folks_alias_details_get_alias (
396 FOLKS_ALIAS_DETAILS (individual))))
399 if (priv->show_groups)
401 GeeSet *group_set = NULL;
403 group_set = folks_group_details_get_groups (
404 FOLKS_GROUP_DETAILS (individual));
406 if (gee_collection_get_size (GEE_COLLECTION (group_set)) > 0)
407 group_iter = gee_iterable_iterator (GEE_ITERABLE (group_set));
410 /* fall-back groups, in case there are no named groups */
411 if (group_iter == NULL)
413 GtkTreeIter iter_group, *parent;
414 EmpathyContact *contact;
415 TpConnection *connection;
416 gchar *protocol_name = NULL;
418 parent = &iter_group;
420 contact = empathy_contact_dup_from_folks_individual (individual);
423 connection = empathy_contact_get_connection (contact);
424 tp_connection_parse_object_path (connection, &protocol_name, NULL);
427 if (!priv->show_groups)
429 else if (!tp_strdiff (protocol_name, "local-xmpp"))
431 /* these are People Nearby */
432 individual_store_get_group (self,
433 EMPATHY_INDIVIDUAL_STORE_PEOPLE_NEARBY, &iter_group, NULL, NULL,
438 individual_store_get_group (self,
439 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
440 &iter_group, NULL, NULL, TRUE);
443 add_individual_to_store (GTK_TREE_STORE (self), &iter, parent,
446 g_free (protocol_name);
447 g_clear_object (&contact);
450 /* Else add to each group. */
451 while (group_iter != NULL && gee_iterator_next (group_iter))
453 gchar *group_name = gee_iterator_get (group_iter);
454 GtkTreeIter iter_group;
456 individual_store_get_group (self, group_name, &iter_group, NULL, NULL,
459 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
464 g_clear_object (&group_iter);
466 if (priv->show_groups &&
467 folks_favourite_details_get_is_favourite (
468 FOLKS_FAVOURITE_DETAILS (individual)))
470 /* Add contact to the fake 'Favorites' group */
471 GtkTreeIter iter_group;
473 individual_store_get_group (self, EMPATHY_INDIVIDUAL_STORE_FAVORITE,
474 &iter_group, NULL, NULL, TRUE);
476 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
480 individual_store_contact_update (self, individual);
484 individual_store_contact_set_active (EmpathyIndividualStore *self,
485 FolksIndividual *individual,
487 gboolean set_changed)
492 model = GTK_TREE_MODEL (self);
494 iters = individual_store_find_contact (self, individual);
495 for (l = iters; l; l = l->next)
499 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
500 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, active,
503 DEBUG ("Set item %s", active ? "active" : "inactive");
507 path = gtk_tree_model_get_path (model, l->data);
508 gtk_tree_model_row_changed (model, path, l->data);
509 gtk_tree_path_free (path);
516 static void individual_store_contact_active_free (ShowActiveData *data);
519 individual_store_contact_active_invalidated (ShowActiveData *data,
522 /* Remove the timeout and free the struct, since the individual or individual
523 * store has disappeared. */
524 g_source_remove (data->timeout);
526 if (old_object == (GObject *) data->self)
528 else if (old_object == (GObject *) data->individual)
529 data->individual = NULL;
531 g_assert_not_reached ();
533 individual_store_contact_active_free (data);
536 static ShowActiveData *
537 individual_store_contact_active_new (EmpathyIndividualStore *self,
538 FolksIndividual *individual,
541 ShowActiveData *data;
543 DEBUG ("Individual'%s' now active, and %s be removed",
544 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
545 remove_ ? "WILL" : "WILL NOT");
547 data = g_slice_new0 (ShowActiveData);
549 /* We don't actually want to force either the IndividualStore or the
550 * Individual to stay alive, since the user could quit Empathy or disable
551 * the account before the contact_active timeout is fired. */
552 g_object_weak_ref (G_OBJECT (self),
553 (GWeakNotify) individual_store_contact_active_invalidated, data);
554 g_object_weak_ref (G_OBJECT (individual),
555 (GWeakNotify) individual_store_contact_active_invalidated, data);
558 data->individual = individual;
559 data->remove = remove_;
566 individual_store_contact_active_free (ShowActiveData *data)
568 if (data->self != NULL)
570 g_object_weak_unref (G_OBJECT (data->self),
571 (GWeakNotify) individual_store_contact_active_invalidated, data);
574 if (data->individual != NULL)
576 g_object_weak_unref (G_OBJECT (data->individual),
577 (GWeakNotify) individual_store_contact_active_invalidated, data);
580 g_slice_free (ShowActiveData, data);
584 individual_store_contact_active_cb (ShowActiveData *data)
588 DEBUG ("Individual'%s' active timeout, removing item",
589 folks_alias_details_get_alias (
590 FOLKS_ALIAS_DETAILS (data->individual)));
591 individual_store_remove_individual (data->self, data->individual);
594 DEBUG ("Individual'%s' no longer active",
595 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (data->individual)));
597 individual_store_contact_set_active (data->self,
598 data->individual, FALSE, TRUE);
600 individual_store_contact_active_free (data);
606 EmpathyIndividualStore *store; /* weak */
607 GCancellable *cancellable; /* owned */
611 individual_avatar_pixbuf_received_cb (FolksIndividual *individual,
612 GAsyncResult *result,
613 LoadAvatarData *data)
615 GError *error = NULL;
618 pixbuf = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
623 DEBUG ("failed to retrieve pixbuf for individual %s: %s",
624 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
626 g_clear_error (&error);
628 else if (data->store != NULL)
632 iters = individual_store_find_contact (data->store, individual);
633 for (l = iters; l; l = l->next)
635 gtk_tree_store_set (GTK_TREE_STORE (data->store), l->data,
636 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR, pixbuf,
644 if (data->store != NULL)
646 EmpathyIndividualStorePriv *priv = GET_PRIV (data->store);
648 g_object_remove_weak_pointer (G_OBJECT (data->store),
649 (gpointer *) &data->store);
650 priv->avatar_cancellables = g_list_remove (priv->avatar_cancellables,
654 tp_clear_object (&pixbuf);
655 g_object_unref (data->cancellable);
656 g_slice_free (LoadAvatarData, data);
660 individual_store_contact_update (EmpathyIndividualStore *self,
661 FolksIndividual *individual)
663 EmpathyIndividualStorePriv *priv;
664 ShowActiveData *data;
668 gboolean was_online = TRUE;
669 gboolean now_online = FALSE;
670 gboolean set_model = FALSE;
671 gboolean do_remove = FALSE;
672 gboolean do_set_active = FALSE;
673 gboolean do_set_refresh = FALSE;
674 gboolean show_avatar = FALSE;
675 GdkPixbuf *pixbuf_status;
676 LoadAvatarData *load_avatar_data;
678 priv = GET_PRIV (self);
680 model = GTK_TREE_MODEL (self);
682 iters = individual_store_find_contact (self, individual);
692 /* Get online state now. */
693 now_online = folks_presence_details_is_online (
694 FOLKS_PRESENCE_DETAILS (individual));
698 DEBUG ("Individual'%s' in list:NO, should be:YES",
699 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
701 individual_store_add_individual (self, individual);
703 if (priv->show_active)
705 do_set_active = TRUE;
707 DEBUG ("Set active (individual added)");
712 DEBUG ("Individual'%s' in list:YES, should be:YES",
713 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
715 /* Get online state before. */
716 if (iters && g_list_length (iters) > 0)
718 gtk_tree_model_get (model, iters->data,
719 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &was_online, -1);
722 /* Is this really an update or an online/offline. */
723 if (priv->show_active)
725 if (was_online != now_online)
727 do_set_active = TRUE;
728 do_set_refresh = TRUE;
730 DEBUG ("Set active (individual updated %s)",
731 was_online ? "online -> offline" : "offline -> online");
735 /* Was TRUE for presence updates. */
736 /* do_set_active = FALSE; */
737 do_set_refresh = TRUE;
739 DEBUG ("Set active (individual updated)");
746 if (priv->show_avatars && !priv->is_compact)
751 /* Load the avatar asynchronously */
752 load_avatar_data = g_slice_new (LoadAvatarData);
753 load_avatar_data->store = self;
754 g_object_add_weak_pointer (G_OBJECT (self),
755 (gpointer *) &load_avatar_data->store);
756 load_avatar_data->cancellable = g_cancellable_new ();
758 priv->avatar_cancellables = g_list_prepend (priv->avatar_cancellables,
759 load_avatar_data->cancellable);
760 empathy_pixbuf_avatar_from_individual_scaled_async (individual, 32, 32,
761 load_avatar_data->cancellable,
762 (GAsyncReadyCallback) individual_avatar_pixbuf_received_cb,
766 empathy_individual_store_get_individual_status_icon (self, individual);
768 for (l = iters; l && set_model; l = l->next)
770 gboolean can_audio_call, can_video_call;
771 const gchar * const *types;
773 individual_can_audio_video_call (individual, &can_audio_call,
776 types = individual_get_client_types (individual);
778 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
779 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
780 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
781 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
782 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
783 EMPATHY_INDIVIDUAL_STORE_COL_PRESENCE_TYPE,
784 folks_presence_details_get_presence_type (
785 FOLKS_PRESENCE_DETAILS (individual)),
786 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
787 folks_presence_details_get_presence_message (
788 FOLKS_PRESENCE_DETAILS (individual)),
789 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact,
790 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
791 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, now_online,
792 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
793 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
794 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
795 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
799 if (priv->show_active && do_set_active)
801 individual_store_contact_set_active (self, individual, do_set_active,
807 individual_store_contact_active_new (self, individual,
809 data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
810 (GSourceFunc) individual_store_contact_active_cb, data);
814 /* FIXME: when someone goes online then offline quickly, the
815 * first timeout sets the user to be inactive and the second
816 * timeout removes the user from the contact list, really we
817 * should remove the first timeout.
823 individual_store_individual_updated_cb (FolksIndividual *individual,
825 EmpathyIndividualStore *self)
827 DEBUG ("Individual'%s' updated, checking roster is in sync...",
828 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
830 individual_store_contact_update (self, individual);
834 individual_store_contact_updated_cb (EmpathyContact *contact,
836 EmpathyIndividualStore *self)
838 FolksIndividual *individual;
840 DEBUG ("Contact '%s' updated, checking roster is in sync...",
841 empathy_contact_get_alias (contact));
843 individual = g_object_get_data (G_OBJECT (contact), "individual");
844 if (individual == NULL)
847 individual_store_contact_update (self, individual);
851 individual_personas_changed_cb (FolksIndividual *individual,
854 EmpathyIndividualStore *self)
858 DEBUG ("Individual '%s' personas-changed.",
859 folks_individual_get_id (individual));
861 iter = gee_iterable_iterator (GEE_ITERABLE (removed));
862 /* FIXME: libfolks hasn't grown capabilities support yet, so we have to go
863 * through the EmpathyContacts for them. */
864 while (gee_iterator_next (iter))
866 TpfPersona *persona = gee_iterator_get (iter);
867 TpContact *tp_contact;
868 EmpathyContact *contact;
870 if (TPF_IS_PERSONA (persona))
872 tp_contact = tpf_persona_get_contact (persona);
873 if (tp_contact != NULL)
875 contact = empathy_contact_dup_from_tp_contact (tp_contact);
876 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
878 g_object_set_data (G_OBJECT (contact), "individual", NULL);
879 g_signal_handlers_disconnect_by_func (contact,
880 (GCallback) individual_store_contact_updated_cb, self);
882 g_object_unref (contact);
886 g_clear_object (&persona);
888 g_clear_object (&iter);
890 iter = gee_iterable_iterator (GEE_ITERABLE (added));
891 while (gee_iterator_next (iter))
893 TpfPersona *persona = gee_iterator_get (iter);
894 TpContact *tp_contact;
895 EmpathyContact *contact;
897 if (TPF_IS_PERSONA (persona))
899 tp_contact = tpf_persona_get_contact (persona);
900 if (tp_contact != NULL)
902 contact = empathy_contact_dup_from_tp_contact (tp_contact);
903 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
905 g_object_set_data (G_OBJECT (contact), "individual", individual);
906 g_signal_connect (contact, "notify::capabilities",
907 (GCallback) individual_store_contact_updated_cb, self);
908 g_signal_connect (contact, "notify::client-types",
909 (GCallback) individual_store_contact_updated_cb, self);
911 g_object_unref (contact);
915 g_clear_object (&persona);
917 g_clear_object (&iter);
921 individual_store_favourites_changed_cb (FolksIndividual *individual,
923 EmpathyIndividualStore *self)
925 DEBUG ("Individual %s is %s a favourite",
926 folks_individual_get_id (individual),
927 folks_favourite_details_get_is_favourite (
928 FOLKS_FAVOURITE_DETAILS (individual)) ? "now" : "no longer");
930 individual_store_remove_individual (self, individual);
931 individual_store_add_individual (self, individual);
935 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
936 FolksIndividual *individual)
938 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
940 individual_store_add_individual (self, individual);
942 g_signal_connect (individual, "notify::avatar",
943 (GCallback) individual_store_individual_updated_cb, self);
944 g_signal_connect (individual, "notify::presence-type",
945 (GCallback) individual_store_individual_updated_cb, self);
946 g_signal_connect (individual, "notify::presence-message",
947 (GCallback) individual_store_individual_updated_cb, self);
948 g_signal_connect (individual, "notify::alias",
949 (GCallback) individual_store_individual_updated_cb, self);
950 g_signal_connect (individual, "personas-changed",
951 (GCallback) individual_personas_changed_cb, self);
952 g_signal_connect (individual, "notify::is-favourite",
953 (GCallback) individual_store_favourites_changed_cb, self);
955 /* provide an empty set so the callback can assume non-NULL sets */
956 individual_personas_changed_cb (individual,
957 folks_individual_get_personas (individual), empty_set, self);
958 g_clear_object (&empty_set);
962 individual_store_disconnect_individual (EmpathyIndividualStore *self,
963 FolksIndividual *individual)
965 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
967 /* provide an empty set so the callback can assume non-NULL sets */
968 individual_personas_changed_cb (individual, empty_set,
969 folks_individual_get_personas (individual), self);
970 g_clear_object (&empty_set);
972 g_signal_handlers_disconnect_by_func (individual,
973 (GCallback) individual_store_individual_updated_cb, self);
974 g_signal_handlers_disconnect_by_func (individual,
975 (GCallback) individual_personas_changed_cb, self);
976 g_signal_handlers_disconnect_by_func (individual,
977 (GCallback) individual_store_favourites_changed_cb, self);
981 individual_store_remove_individual_and_disconnect (
982 EmpathyIndividualStore *self,
983 FolksIndividual *individual)
985 individual_store_disconnect_individual (self, individual);
986 individual_store_remove_individual (self, individual);
990 individual_store_members_changed_cb (EmpathyIndividualManager *manager,
991 const gchar *message,
995 EmpathyIndividualStore *self)
999 for (l = added; l; l = l->next)
1001 DEBUG ("Individual %s %s", folks_individual_get_id (l->data), "added");
1003 individual_store_add_individual_and_connect (self, l->data);
1005 for (l = removed; l; l = l->next)
1007 DEBUG ("Individual %s %s",
1008 folks_individual_get_id (l->data), "removed");
1010 individual_store_remove_individual_and_disconnect (self, l->data);
1015 individual_store_groups_changed_cb (EmpathyIndividualManager *manager,
1016 FolksIndividual *individual,
1019 EmpathyIndividualStore *self)
1021 EmpathyIndividualStorePriv *priv;
1022 gboolean show_active;
1024 priv = GET_PRIV (self);
1026 DEBUG ("Updating groups for individual %s",
1027 folks_individual_get_id (individual));
1029 /* We do this to make sure the groups are correct, if not, we
1030 * would have to check the groups already set up for each
1031 * contact and then see what has been updated.
1033 show_active = priv->show_active;
1034 priv->show_active = FALSE;
1035 individual_store_remove_individual (self, individual);
1036 individual_store_add_individual (self, individual);
1037 priv->show_active = show_active;
1041 individual_store_manager_setup (gpointer user_data)
1043 EmpathyIndividualStore *self = user_data;
1044 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1047 /* Signal connection. */
1049 /* TODO: implement */
1050 DEBUG ("handling individual renames unimplemented");
1052 g_signal_connect (priv->manager,
1054 G_CALLBACK (individual_store_members_changed_cb), self);
1056 g_signal_connect (priv->manager,
1058 G_CALLBACK (individual_store_groups_changed_cb), self);
1060 /* Add contacts already created. */
1061 individuals = empathy_individual_manager_get_members (priv->manager);
1062 if (individuals != NULL && FOLKS_IS_INDIVIDUAL (individuals->data))
1064 individual_store_members_changed_cb (priv->manager, "initial add",
1065 individuals, NULL, 0, self);
1066 g_list_free (individuals);
1069 priv->setup_idle_id = 0;
1074 individual_store_set_individual_manager (EmpathyIndividualStore *self,
1075 EmpathyIndividualManager *manager)
1077 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1079 priv->manager = g_object_ref (manager);
1081 /* Let a chance to have all properties set before populating */
1082 priv->setup_idle_id = g_idle_add (individual_store_manager_setup, self);
1086 individual_store_member_renamed_cb (EmpathyIndividualManager *manager,
1087 FolksIndividual *old_individual,
1088 FolksIndividual *new_individual,
1090 const gchar *message,
1091 EmpathyIndividualStore *self)
1093 DEBUG ("Individual %s renamed to %s",
1094 folks_individual_get_id (old_individual),
1095 folks_individual_get_id (new_individual));
1097 /* add the new contact */
1098 individual_store_add_individual_and_connect (self, new_individual);
1100 /* remove old contact */
1101 individual_store_remove_individual_and_disconnect (self, old_individual);
1105 individual_store_dispose (GObject *object)
1107 EmpathyIndividualStorePriv *priv = GET_PRIV (object);
1108 GList *individuals, *l;
1110 if (priv->dispose_has_run)
1112 priv->dispose_has_run = TRUE;
1114 /* Cancel any pending avatar load operations */
1115 for (l = priv->avatar_cancellables; l != NULL; l = l->next)
1117 /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
1118 g_cancellable_cancel (G_CANCELLABLE (l->data));
1120 g_list_free (priv->avatar_cancellables);
1122 individuals = empathy_individual_manager_get_members (priv->manager);
1123 for (l = individuals; l; l = l->next)
1125 individual_store_disconnect_individual (EMPATHY_INDIVIDUAL_STORE (object),
1126 FOLKS_INDIVIDUAL (l->data));
1128 g_list_free (individuals);
1130 g_signal_handlers_disconnect_by_func (priv->manager,
1131 G_CALLBACK (individual_store_member_renamed_cb), object);
1132 g_signal_handlers_disconnect_by_func (priv->manager,
1133 G_CALLBACK (individual_store_members_changed_cb), object);
1134 g_signal_handlers_disconnect_by_func (priv->manager,
1135 G_CALLBACK (individual_store_groups_changed_cb), object);
1136 g_object_unref (priv->manager);
1138 if (priv->inhibit_active)
1140 g_source_remove (priv->inhibit_active);
1143 if (priv->setup_idle_id != 0)
1145 g_source_remove (priv->setup_idle_id);
1148 g_hash_table_destroy (priv->status_icons);
1149 g_hash_table_destroy (priv->folks_individual_cache);
1150 g_hash_table_destroy (priv->empathy_group_cache);
1151 G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
1155 individual_store_get_property (GObject *object,
1160 EmpathyIndividualStorePriv *priv;
1162 priv = GET_PRIV (object);
1166 case PROP_INDIVIDUAL_MANAGER:
1167 g_value_set_object (value, priv->manager);
1169 case PROP_SHOW_AVATARS:
1170 g_value_set_boolean (value, priv->show_avatars);
1172 case PROP_SHOW_PROTOCOLS:
1173 g_value_set_boolean (value, priv->show_protocols);
1175 case PROP_SHOW_GROUPS:
1176 g_value_set_boolean (value, priv->show_groups);
1178 case PROP_IS_COMPACT:
1179 g_value_set_boolean (value, priv->is_compact);
1181 case PROP_SORT_CRITERIUM:
1182 g_value_set_enum (value, priv->sort_criterium);
1185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1191 individual_store_set_property (GObject *object,
1193 const GValue *value,
1198 case PROP_INDIVIDUAL_MANAGER:
1199 individual_store_set_individual_manager (EMPATHY_INDIVIDUAL_STORE
1200 (object), g_value_get_object (value));
1202 case PROP_SHOW_AVATARS:
1203 empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
1204 (object), g_value_get_boolean (value));
1206 case PROP_SHOW_PROTOCOLS:
1207 empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1208 (object), g_value_get_boolean (value));
1210 case PROP_SHOW_GROUPS:
1211 empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1212 (object), g_value_get_boolean (value));
1214 case PROP_IS_COMPACT:
1215 empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1216 (object), g_value_get_boolean (value));
1218 case PROP_SORT_CRITERIUM:
1219 empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1220 (object), g_value_get_enum (value));
1223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1229 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1231 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1233 object_class->dispose = individual_store_dispose;
1234 object_class->get_property = individual_store_get_property;
1235 object_class->set_property = individual_store_set_property;
1237 g_object_class_install_property (object_class,
1238 PROP_INDIVIDUAL_MANAGER,
1239 g_param_spec_object ("individual-manager",
1240 "The individual manager",
1241 "The individual manager",
1242 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1243 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1244 g_object_class_install_property (object_class,
1246 g_param_spec_boolean ("show-avatars",
1248 "Whether contact list should display "
1249 "avatars for contacts", TRUE, G_PARAM_READWRITE));
1250 g_object_class_install_property (object_class,
1251 PROP_SHOW_PROTOCOLS,
1252 g_param_spec_boolean ("show-protocols",
1254 "Whether contact list should display "
1255 "protocols for contacts", FALSE, G_PARAM_READWRITE));
1256 g_object_class_install_property (object_class,
1258 g_param_spec_boolean ("show-groups",
1260 "Whether contact list should display "
1261 "contact groups", TRUE, G_PARAM_READWRITE));
1262 g_object_class_install_property (object_class,
1264 g_param_spec_boolean ("is-compact",
1266 "Whether the contact list is in compact mode or not",
1267 FALSE, G_PARAM_READWRITE));
1269 g_object_class_install_property (object_class,
1270 PROP_SORT_CRITERIUM,
1271 g_param_spec_enum ("sort-criterium",
1273 "The sort criterium to use for sorting the contact list",
1274 EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1275 EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1277 g_type_class_add_private (object_class,
1278 sizeof (EmpathyIndividualStorePriv));
1282 get_position (const char **strv,
1287 for (i = 0; strv[i] != NULL; i++)
1289 if (!tp_strdiff (strv[i], str))
1297 compare_separator_and_groups (gboolean is_separator_a,
1298 gboolean is_separator_b,
1299 const gchar *name_a,
1300 const gchar *name_b,
1301 FolksIndividual *individual_a,
1302 FolksIndividual *individual_b,
1303 gboolean fake_group_a,
1304 gboolean fake_group_b)
1306 /* these two lists are the sorted list of fake groups to include at the
1307 * top and bottom of the roster */
1308 const char *top_groups[] = {
1309 EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1313 const char *bottom_groups[] = {
1314 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1318 if (is_separator_a || is_separator_b)
1320 /* We have at least one separator */
1325 else if (is_separator_b)
1331 /* One group and one contact */
1332 if (!individual_a && individual_b)
1336 else if (individual_a && !individual_b)
1340 else if (!individual_a && !individual_b)
1342 gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1344 a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1345 b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1346 a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1347 b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1349 if (a_in_top && b_in_top)
1351 /* compare positions */
1352 return CLAMP (get_position (top_groups, name_a) -
1353 get_position (top_groups, name_b), -1, 1);
1355 else if (a_in_bottom && b_in_bottom)
1357 /* compare positions */
1358 return CLAMP (get_position (bottom_groups, name_a) -
1359 get_position (bottom_groups, name_b), -1, 1);
1361 else if (a_in_top || b_in_bottom)
1365 else if (b_in_top || a_in_bottom)
1371 return g_utf8_collate (name_a, name_b);
1375 /* Two contacts, ordering depends of the sorting policy */
1380 individual_store_contact_sort (FolksIndividual *individual_a,
1381 FolksIndividual *individual_b)
1384 EmpathyContact *contact_a = NULL, *contact_b = NULL;
1385 TpAccount *account_a, *account_b;
1387 g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1390 ret_val = g_utf8_collate (
1391 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_a)),
1392 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_b)));
1397 contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1398 contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1399 if (contact_a != NULL && contact_b != NULL)
1401 account_a = empathy_contact_get_account (contact_a);
1402 account_b = empathy_contact_get_account (contact_b);
1404 g_assert (account_a != NULL);
1405 g_assert (account_b != NULL);
1408 ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1409 tp_account_get_protocol (account_b));
1415 ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1416 tp_proxy_get_object_path (account_b));
1423 ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1424 folks_individual_get_id (individual_b));
1427 tp_clear_object (&contact_a);
1428 tp_clear_object (&contact_b);
1434 individual_store_state_sort_func (GtkTreeModel *model,
1435 GtkTreeIter *iter_a,
1436 GtkTreeIter *iter_b,
1440 FolksIndividual *individual_a, *individual_b;
1441 gchar *name_a, *name_b;
1442 gboolean is_separator_a, is_separator_b;
1443 gboolean fake_group_a, fake_group_b;
1444 FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1445 TpConnectionPresenceType tp_presence_a, tp_presence_b;
1447 gtk_tree_model_get (model, iter_a,
1448 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1449 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1450 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1451 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1452 gtk_tree_model_get (model, iter_b,
1453 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1454 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1455 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1456 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1458 if (individual_a == NULL || individual_b == NULL)
1460 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1461 name_a, name_b, individual_a, individual_b, fake_group_a,
1466 /* If we managed to get this far, we can start looking at
1469 folks_presence_type_a =
1470 folks_presence_details_get_presence_type (
1471 FOLKS_PRESENCE_DETAILS (individual_a));
1472 folks_presence_type_b =
1473 folks_presence_details_get_presence_type (
1474 FOLKS_PRESENCE_DETAILS (individual_b));
1475 tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1476 tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1478 ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1483 /* Fallback: compare by name et al. */
1484 ret_val = individual_store_contact_sort (individual_a, individual_b);
1490 tp_clear_object (&individual_a);
1491 tp_clear_object (&individual_b);
1497 individual_store_name_sort_func (GtkTreeModel *model,
1498 GtkTreeIter *iter_a,
1499 GtkTreeIter *iter_b,
1502 gchar *name_a, *name_b;
1503 FolksIndividual *individual_a, *individual_b;
1504 gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1506 gboolean fake_group_a, fake_group_b;
1508 gtk_tree_model_get (model, iter_a,
1509 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1510 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1511 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1512 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1513 gtk_tree_model_get (model, iter_b,
1514 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1515 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1516 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1517 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1519 if (individual_a == NULL || individual_b == NULL)
1520 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1521 name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1523 ret_val = individual_store_contact_sort (individual_a, individual_b);
1525 tp_clear_object (&individual_a);
1526 tp_clear_object (&individual_b);
1534 individual_store_setup (EmpathyIndividualStore *self)
1536 EmpathyIndividualStorePriv *priv;
1538 GDK_TYPE_PIXBUF, /* Status pixbuf */
1539 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1540 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1541 G_TYPE_STRING, /* Name */
1542 G_TYPE_UINT, /* Presence type */
1543 G_TYPE_STRING, /* Status string */
1544 G_TYPE_BOOLEAN, /* Compact view */
1545 FOLKS_TYPE_INDIVIDUAL, /* Individual type */
1546 G_TYPE_BOOLEAN, /* Is group */
1547 G_TYPE_BOOLEAN, /* Is active */
1548 G_TYPE_BOOLEAN, /* Is online */
1549 G_TYPE_BOOLEAN, /* Is separator */
1550 G_TYPE_BOOLEAN, /* Can make audio calls */
1551 G_TYPE_BOOLEAN, /* Can make video calls */
1552 G_TYPE_BOOLEAN, /* Is a fake group */
1553 G_TYPE_STRV, /* Client types */
1554 G_TYPE_UINT, /* Event count */
1557 priv = GET_PRIV (self);
1559 gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1560 EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1562 /* Set up sorting */
1563 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1564 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1565 individual_store_name_sort_func, self, NULL);
1566 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1567 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1568 individual_store_state_sort_func, self, NULL);
1570 priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1571 empathy_individual_store_set_sort_criterium (self, priv->sort_criterium);
1575 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1577 EmpathyIndividualStorePriv *priv;
1579 priv = GET_PRIV (self);
1581 priv->show_active = TRUE;
1582 priv->inhibit_active = 0;
1588 g_queue_free_full_iter (gpointer data)
1590 GQueue *queue = (GQueue *) data;
1591 g_queue_foreach (queue, (GFunc) gtk_tree_iter_free, NULL);
1592 g_queue_free (queue);
1596 empathy_individual_store_init (EmpathyIndividualStore *self)
1598 EmpathyIndividualStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1599 EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1602 priv->show_avatars = TRUE;
1603 priv->show_groups = TRUE;
1604 priv->show_protocols = FALSE;
1605 priv->inhibit_active =
1606 g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1607 (GSourceFunc) individual_store_inhibit_active_cb, self);
1608 priv->status_icons =
1609 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1610 priv->folks_individual_cache = g_hash_table_new_full (NULL, NULL, NULL,
1611 g_queue_free_full_iter);
1612 priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
1613 g_str_equal, g_free, (GDestroyNotify) gtk_tree_iter_free);
1614 individual_store_setup (self);
1617 EmpathyIndividualStore *
1618 empathy_individual_store_new (EmpathyIndividualManager *manager)
1620 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1622 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE,
1623 "individual-manager", manager, NULL);
1626 EmpathyIndividualManager *
1627 empathy_individual_store_get_manager (EmpathyIndividualStore *self)
1629 EmpathyIndividualStorePriv *priv;
1631 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), FALSE);
1633 priv = GET_PRIV (self);
1635 return priv->manager;
1639 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1641 EmpathyIndividualStorePriv *priv;
1643 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1645 priv = GET_PRIV (self);
1647 return priv->show_avatars;
1651 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1654 EmpathyIndividualStore *self)
1656 EmpathyIndividualStorePriv *priv;
1657 gboolean show_avatar = FALSE;
1658 FolksIndividual *individual;
1659 GdkPixbuf *pixbuf_status;
1661 priv = GET_PRIV (self);
1663 if (priv->show_avatars && !priv->is_compact)
1668 gtk_tree_model_get (model, iter,
1669 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1671 if (individual == NULL)
1675 /* get icon from hash_table */
1677 empathy_individual_store_get_individual_status_icon (self, individual);
1679 gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1680 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1681 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1682 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact, -1);
1684 g_object_unref (individual);
1690 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1691 gboolean show_avatars)
1693 EmpathyIndividualStorePriv *priv;
1694 GtkTreeModel *model;
1696 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1698 priv = GET_PRIV (self);
1700 priv->show_avatars = show_avatars;
1702 model = GTK_TREE_MODEL (self);
1704 gtk_tree_model_foreach (model,
1705 (GtkTreeModelForeachFunc)
1706 individual_store_update_list_mode_foreach, self);
1708 g_object_notify (G_OBJECT (self), "show-avatars");
1712 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1714 EmpathyIndividualStorePriv *priv;
1716 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1718 priv = GET_PRIV (self);
1720 return priv->show_protocols;
1724 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1725 gboolean show_protocols)
1727 EmpathyIndividualStorePriv *priv;
1728 GtkTreeModel *model;
1730 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1732 priv = GET_PRIV (self);
1734 priv->show_protocols = show_protocols;
1736 model = GTK_TREE_MODEL (self);
1738 gtk_tree_model_foreach (model,
1739 (GtkTreeModelForeachFunc)
1740 individual_store_update_list_mode_foreach, self);
1742 g_object_notify (G_OBJECT (self), "show-protocols");
1746 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1748 EmpathyIndividualStorePriv *priv;
1750 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1752 priv = GET_PRIV (self);
1754 return priv->show_groups;
1758 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1759 gboolean show_groups)
1761 EmpathyIndividualStorePriv *priv;
1763 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1765 priv = GET_PRIV (self);
1767 if (priv->show_groups == show_groups)
1772 priv->show_groups = show_groups;
1774 if (priv->setup_idle_id == 0)
1776 /* Remove all contacts and add them back, not optimized but
1777 * that's the easy way :)
1779 * This is only done if there's not a pending setup idle
1780 * callback, otherwise it will race and the contacts will get
1784 gtk_tree_store_clear (GTK_TREE_STORE (self));
1785 /* Also clear the cache */
1786 g_hash_table_remove_all (priv->folks_individual_cache);
1787 g_hash_table_remove_all (priv->empathy_group_cache);
1789 contacts = empathy_individual_manager_get_members (priv->manager);
1791 individual_store_members_changed_cb (priv->manager,
1792 "re-adding members: toggled group visibility",
1793 contacts, NULL, 0, self);
1794 g_list_free (contacts);
1797 g_object_notify (G_OBJECT (self), "show-groups");
1801 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1803 EmpathyIndividualStorePriv *priv;
1805 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1807 priv = GET_PRIV (self);
1809 return priv->is_compact;
1813 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1814 gboolean is_compact)
1816 EmpathyIndividualStorePriv *priv;
1817 GtkTreeModel *model;
1819 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1821 priv = GET_PRIV (self);
1823 priv->is_compact = is_compact;
1825 model = GTK_TREE_MODEL (self);
1827 gtk_tree_model_foreach (model,
1828 (GtkTreeModelForeachFunc)
1829 individual_store_update_list_mode_foreach, self);
1831 g_object_notify (G_OBJECT (self), "is-compact");
1834 EmpathyIndividualStoreSort
1835 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1837 EmpathyIndividualStorePriv *priv;
1839 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1841 priv = GET_PRIV (self);
1843 return priv->sort_criterium;
1847 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1848 EmpathyIndividualStoreSort sort_criterium)
1850 EmpathyIndividualStorePriv *priv;
1852 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1854 priv = GET_PRIV (self);
1856 priv->sort_criterium = sort_criterium;
1858 switch (sort_criterium)
1860 case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1861 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1862 EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1865 case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1866 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1867 EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1871 g_assert_not_reached ();
1874 g_object_notify (G_OBJECT (self), "sort-criterium");
1878 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1882 gboolean is_separator = FALSE;
1884 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1886 gtk_tree_model_get (model, iter,
1887 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1889 return is_separator;
1893 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1895 gboolean *path_is_group,
1896 gboolean *is_fake_group)
1898 GtkTreeIter parent_iter, iter;
1901 gboolean fake = FALSE;
1903 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1907 *path_is_group = FALSE;
1910 if (!gtk_tree_model_get_iter (model, &iter, path))
1915 gtk_tree_model_get (model, &iter,
1916 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1917 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1924 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1931 gtk_tree_model_get (model, &iter,
1932 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1933 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1934 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1944 *path_is_group = TRUE;
1947 if (is_fake_group != NULL)
1948 *is_fake_group = fake;
1954 individual_store_get_individual_status_icon_with_icon_name (
1955 EmpathyIndividualStore *self,
1956 FolksIndividual *individual,
1957 const gchar *status_icon_name)
1959 GdkPixbuf *pixbuf_status;
1960 EmpathyIndividualStorePriv *priv;
1961 const gchar *protocol_name = NULL;
1962 gchar *icon_name = NULL;
1965 guint contact_count = 0;
1966 EmpathyContact *contact = NULL;
1967 gboolean show_protocols_here;
1969 priv = GET_PRIV (self);
1971 personas = folks_individual_get_personas (individual);
1972 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1973 while (gee_iterator_next (iter))
1975 FolksPersona *persona = gee_iterator_get (iter);
1976 if (empathy_folks_persona_is_interesting (persona))
1979 g_clear_object (&persona);
1981 if (contact_count > 1)
1984 g_clear_object (&iter);
1986 show_protocols_here = (priv->show_protocols && (contact_count == 1));
1987 if (show_protocols_here)
1989 contact = empathy_contact_dup_from_folks_individual (individual);
1990 if (contact != NULL)
1992 protocol_name = empathy_protocol_name_for_contact (contact);
1993 icon_name = g_strdup_printf ("%s-%s", status_icon_name,
1998 g_warning ("Cannot retrieve contact from individual '%s'",
1999 folks_alias_details_get_alias (
2000 FOLKS_ALIAS_DETAILS (individual)));
2007 icon_name = g_strdup_printf ("%s", status_icon_name);
2010 pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
2012 if (pixbuf_status == NULL)
2015 empathy_pixbuf_contact_status_icon_with_icon_name (contact,
2016 status_icon_name, show_protocols_here);
2018 if (pixbuf_status != NULL)
2020 /* pass the reference to the hash table */
2021 g_hash_table_insert (priv->status_icons,
2022 g_strdup (icon_name), pixbuf_status);
2027 tp_clear_object (&contact);
2029 return pixbuf_status;
2033 empathy_individual_store_get_individual_status_icon (
2034 EmpathyIndividualStore *self,
2035 FolksIndividual *individual)
2037 GdkPixbuf *pixbuf_status = NULL;
2038 const gchar *status_icon_name = NULL;
2040 status_icon_name = empathy_icon_name_for_individual (individual);
2041 if (status_icon_name == NULL)
2045 individual_store_get_individual_status_icon_with_icon_name (self,
2046 individual, status_icon_name);
2048 return pixbuf_status;