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 (GtkTreeRowReference) */
77 GHashTable *folks_individual_cache;
78 /* Hash: char *groupname -> GtkTreeRowReference *row */
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;
208 GtkTreeRowReference *row_ref;
212 individual_can_audio_video_call (individual, &can_audio_call,
215 types = individual_get_client_types (individual);
217 gtk_tree_store_insert_with_values (self, iter, parent, 0,
218 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
219 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
220 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, individual,
221 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
222 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
223 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
224 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
225 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
228 path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), iter);
229 row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path);
230 queue = g_hash_table_lookup (priv->folks_individual_cache, individual);
233 g_queue_push_tail (queue, row_ref);
237 queue = g_queue_new ();
238 g_queue_push_tail (queue, row_ref);
239 g_hash_table_insert (priv->folks_individual_cache, individual,
242 gtk_tree_path_free (path);
246 individual_store_get_group (EmpathyIndividualStore *self,
248 GtkTreeIter *iter_group_to_set,
249 GtkTreeIter *iter_separator_to_set,
251 gboolean is_fake_group)
253 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
255 GtkTreeIter iter_group;
256 GtkTreeIter iter_separator;
257 GtkTreeRowReference *row_ref;
259 model = GTK_TREE_MODEL (self);
260 row_ref = g_hash_table_lookup (priv->empathy_group_cache, name);
269 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_group,
271 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, NULL,
272 EMPATHY_INDIVIDUAL_STORE_COL_NAME, name,
273 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, TRUE,
274 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, FALSE,
275 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
276 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, is_fake_group,
279 path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), &iter_group);
280 row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path);
281 g_hash_table_insert (priv->empathy_group_cache,
282 g_strdup (name), row_ref);
283 gtk_tree_path_free (path);
285 if (iter_group_to_set)
286 *iter_group_to_set = iter_group;
288 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_separator,
290 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, TRUE,
293 if (iter_separator_to_set)
294 *iter_separator_to_set = iter_separator;
298 GtkTreePath *path = gtk_tree_row_reference_get_path (row_ref);
301 if (!gtk_tree_model_get_iter (model, &iter, path)) {
302 gtk_tree_path_free (path);
305 gtk_tree_path_free (path);
310 if (iter_group_to_set)
311 *iter_group_to_set = iter;
313 iter_separator = iter;
315 if (gtk_tree_model_iter_next (model, &iter_separator))
317 gboolean is_separator;
319 gtk_tree_model_get (model, &iter_separator,
320 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
322 if (is_separator && iter_separator_to_set)
323 *iter_separator_to_set = iter_separator;
329 individual_store_find_contact (EmpathyIndividualStore *self,
330 FolksIndividual *individual)
332 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
334 GQueue *row_refs_queue;
336 GList *iters_list = NULL;
338 model = GTK_TREE_MODEL (self);
339 row_refs_queue = g_hash_table_lookup (priv->folks_individual_cache,
344 for (i = g_queue_peek_head_link (row_refs_queue) ; i != NULL ; i = i->next)
346 GtkTreePath *path = gtk_tree_row_reference_get_path (i->data);
348 if (!gtk_tree_model_get_iter (model, &iter, path))
350 gtk_tree_path_free (path);
353 gtk_tree_path_free (path);
354 iters_list = g_list_prepend
355 (iters_list, gtk_tree_iter_copy (&iter));
362 free_iters (GList *iters)
364 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
369 individual_store_remove_individual (EmpathyIndividualStore *self,
370 FolksIndividual *individual)
372 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
377 row_refs = g_hash_table_lookup (priv->folks_individual_cache, individual);
382 model = GTK_TREE_MODEL (self);
384 for (l = g_queue_peek_head_link (row_refs); l; l = l->next)
386 GtkTreePath *path = gtk_tree_row_reference_get_path (l->data);
390 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter,
393 gtk_tree_path_free (path);
396 gtk_tree_path_free (path);
398 /* NOTE: it is only <= 2 here because we have
399 * separators after the group name, otherwise it
402 if (gtk_tree_model_iter_parent (model, &parent, &iter) &&
403 gtk_tree_model_iter_n_children (model, &parent) <= 2)
406 gtk_tree_model_get (model, &parent,
407 EMPATHY_CONTACT_LIST_STORE_COL_NAME, &group_name,
409 g_hash_table_remove (priv->empathy_group_cache,
411 gtk_tree_store_remove (GTK_TREE_STORE (self), &parent);
415 gtk_tree_store_remove (GTK_TREE_STORE (self), &iter);
419 g_hash_table_remove (priv->folks_individual_cache, individual);
423 individual_store_add_individual (EmpathyIndividualStore *self,
424 FolksIndividual *individual)
426 EmpathyIndividualStorePriv *priv;
428 GeeIterator *group_iter = NULL;
430 priv = GET_PRIV (self);
432 if (EMP_STR_EMPTY (folks_alias_details_get_alias (
433 FOLKS_ALIAS_DETAILS (individual))))
436 if (priv->show_groups)
438 GeeSet *group_set = NULL;
440 group_set = folks_group_details_get_groups (
441 FOLKS_GROUP_DETAILS (individual));
443 if (gee_collection_get_size (GEE_COLLECTION (group_set)) > 0)
444 group_iter = gee_iterable_iterator (GEE_ITERABLE (group_set));
447 /* fall-back groups, in case there are no named groups */
448 if (group_iter == NULL)
450 GtkTreeIter iter_group, *parent;
451 EmpathyContact *contact;
452 TpConnection *connection;
453 gchar *protocol_name = NULL;
455 parent = &iter_group;
457 contact = empathy_contact_dup_from_folks_individual (individual);
460 connection = empathy_contact_get_connection (contact);
461 tp_connection_parse_object_path (connection, &protocol_name, NULL);
464 if (!priv->show_groups)
466 else if (!tp_strdiff (protocol_name, "local-xmpp"))
468 /* these are People Nearby */
469 individual_store_get_group (self,
470 EMPATHY_INDIVIDUAL_STORE_PEOPLE_NEARBY, &iter_group, NULL, NULL,
475 individual_store_get_group (self,
476 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
477 &iter_group, NULL, NULL, TRUE);
480 add_individual_to_store (GTK_TREE_STORE (self), &iter, parent,
483 g_free (protocol_name);
484 g_clear_object (&contact);
487 /* Else add to each group. */
488 while (group_iter != NULL && gee_iterator_next (group_iter))
490 gchar *group_name = gee_iterator_get (group_iter);
491 GtkTreeIter iter_group;
493 individual_store_get_group (self, group_name, &iter_group, NULL, NULL,
496 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
501 g_clear_object (&group_iter);
503 if (priv->show_groups &&
504 folks_favourite_details_get_is_favourite (
505 FOLKS_FAVOURITE_DETAILS (individual)))
507 /* Add contact to the fake 'Favorites' group */
508 GtkTreeIter iter_group;
510 individual_store_get_group (self, EMPATHY_INDIVIDUAL_STORE_FAVORITE,
511 &iter_group, NULL, NULL, TRUE);
513 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
517 individual_store_contact_update (self, individual);
521 individual_store_contact_set_active (EmpathyIndividualStore *self,
522 FolksIndividual *individual,
524 gboolean set_changed)
529 model = GTK_TREE_MODEL (self);
531 iters = individual_store_find_contact (self, individual);
532 for (l = iters; l; l = l->next)
536 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
537 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, active,
540 DEBUG ("Set item %s", active ? "active" : "inactive");
544 path = gtk_tree_model_get_path (model, l->data);
545 gtk_tree_model_row_changed (model, path, l->data);
546 gtk_tree_path_free (path);
553 static void individual_store_contact_active_free (ShowActiveData *data);
556 individual_store_contact_active_invalidated (ShowActiveData *data,
559 /* Remove the timeout and free the struct, since the individual or individual
560 * store has disappeared. */
561 g_source_remove (data->timeout);
563 if (old_object == (GObject *) data->self)
565 else if (old_object == (GObject *) data->individual)
566 data->individual = NULL;
568 g_assert_not_reached ();
570 individual_store_contact_active_free (data);
573 static ShowActiveData *
574 individual_store_contact_active_new (EmpathyIndividualStore *self,
575 FolksIndividual *individual,
578 ShowActiveData *data;
580 DEBUG ("Individual'%s' now active, and %s be removed",
581 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
582 remove_ ? "WILL" : "WILL NOT");
584 data = g_slice_new0 (ShowActiveData);
586 /* We don't actually want to force either the IndividualStore or the
587 * Individual to stay alive, since the user could quit Empathy or disable
588 * the account before the contact_active timeout is fired. */
589 g_object_weak_ref (G_OBJECT (self),
590 (GWeakNotify) individual_store_contact_active_invalidated, data);
591 g_object_weak_ref (G_OBJECT (individual),
592 (GWeakNotify) individual_store_contact_active_invalidated, data);
595 data->individual = individual;
596 data->remove = remove_;
603 individual_store_contact_active_free (ShowActiveData *data)
605 if (data->self != NULL)
607 g_object_weak_unref (G_OBJECT (data->self),
608 (GWeakNotify) individual_store_contact_active_invalidated, data);
611 if (data->individual != NULL)
613 g_object_weak_unref (G_OBJECT (data->individual),
614 (GWeakNotify) individual_store_contact_active_invalidated, data);
617 g_slice_free (ShowActiveData, data);
621 individual_store_contact_active_cb (ShowActiveData *data)
625 DEBUG ("Individual'%s' active timeout, removing item",
626 folks_alias_details_get_alias (
627 FOLKS_ALIAS_DETAILS (data->individual)));
628 individual_store_remove_individual (data->self, data->individual);
631 DEBUG ("Individual'%s' no longer active",
632 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (data->individual)));
634 individual_store_contact_set_active (data->self,
635 data->individual, FALSE, TRUE);
637 individual_store_contact_active_free (data);
643 EmpathyIndividualStore *store; /* weak */
644 GCancellable *cancellable; /* owned */
648 individual_avatar_pixbuf_received_cb (FolksIndividual *individual,
649 GAsyncResult *result,
650 LoadAvatarData *data)
652 GError *error = NULL;
655 pixbuf = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
660 DEBUG ("failed to retrieve pixbuf for individual %s: %s",
661 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
663 g_clear_error (&error);
665 else if (data->store != NULL)
669 iters = individual_store_find_contact (data->store, individual);
670 for (l = iters; l; l = l->next)
672 gtk_tree_store_set (GTK_TREE_STORE (data->store), l->data,
673 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR, pixbuf,
681 if (data->store != NULL)
683 EmpathyIndividualStorePriv *priv = GET_PRIV (data->store);
685 g_object_remove_weak_pointer (G_OBJECT (data->store),
686 (gpointer *) &data->store);
687 priv->avatar_cancellables = g_list_remove (priv->avatar_cancellables,
691 tp_clear_object (&pixbuf);
692 g_object_unref (data->cancellable);
693 g_slice_free (LoadAvatarData, data);
697 individual_store_contact_update (EmpathyIndividualStore *self,
698 FolksIndividual *individual)
700 EmpathyIndividualStorePriv *priv;
701 ShowActiveData *data;
705 gboolean was_online = TRUE;
706 gboolean now_online = FALSE;
707 gboolean set_model = FALSE;
708 gboolean do_remove = FALSE;
709 gboolean do_set_active = FALSE;
710 gboolean do_set_refresh = FALSE;
711 gboolean show_avatar = FALSE;
712 GdkPixbuf *pixbuf_status;
713 LoadAvatarData *load_avatar_data;
715 priv = GET_PRIV (self);
717 model = GTK_TREE_MODEL (self);
719 iters = individual_store_find_contact (self, individual);
729 /* Get online state now. */
730 now_online = folks_presence_details_is_online (
731 FOLKS_PRESENCE_DETAILS (individual));
735 DEBUG ("Individual'%s' in list:NO, should be:YES",
736 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
738 individual_store_add_individual (self, individual);
740 if (priv->show_active)
742 do_set_active = TRUE;
744 DEBUG ("Set active (individual added)");
749 DEBUG ("Individual'%s' in list:YES, should be:YES",
750 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
752 /* Get online state before. */
753 if (iters && g_list_length (iters) > 0)
755 gtk_tree_model_get (model, iters->data,
756 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &was_online, -1);
759 /* Is this really an update or an online/offline. */
760 if (priv->show_active)
762 if (was_online != now_online)
764 do_set_active = TRUE;
765 do_set_refresh = TRUE;
767 DEBUG ("Set active (individual updated %s)",
768 was_online ? "online -> offline" : "offline -> online");
772 /* Was TRUE for presence updates. */
773 /* do_set_active = FALSE; */
774 do_set_refresh = TRUE;
776 DEBUG ("Set active (individual updated)");
783 if (priv->show_avatars && !priv->is_compact)
788 /* Load the avatar asynchronously */
789 load_avatar_data = g_slice_new (LoadAvatarData);
790 load_avatar_data->store = self;
791 g_object_add_weak_pointer (G_OBJECT (self),
792 (gpointer *) &load_avatar_data->store);
793 load_avatar_data->cancellable = g_cancellable_new ();
795 priv->avatar_cancellables = g_list_prepend (priv->avatar_cancellables,
796 load_avatar_data->cancellable);
797 empathy_pixbuf_avatar_from_individual_scaled_async (individual, 32, 32,
798 load_avatar_data->cancellable,
799 (GAsyncReadyCallback) individual_avatar_pixbuf_received_cb,
803 empathy_individual_store_get_individual_status_icon (self, individual);
805 for (l = iters; l && set_model; l = l->next)
807 gboolean can_audio_call, can_video_call;
808 const gchar * const *types;
810 individual_can_audio_video_call (individual, &can_audio_call,
813 types = individual_get_client_types (individual);
815 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
816 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
817 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
818 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
819 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
820 EMPATHY_INDIVIDUAL_STORE_COL_PRESENCE_TYPE,
821 folks_presence_details_get_presence_type (
822 FOLKS_PRESENCE_DETAILS (individual)),
823 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
824 folks_presence_details_get_presence_message (
825 FOLKS_PRESENCE_DETAILS (individual)),
826 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact,
827 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
828 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, now_online,
829 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
830 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
831 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
832 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
836 if (priv->show_active && do_set_active)
838 individual_store_contact_set_active (self, individual, do_set_active,
844 individual_store_contact_active_new (self, individual,
846 data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
847 (GSourceFunc) individual_store_contact_active_cb, data);
851 /* FIXME: when someone goes online then offline quickly, the
852 * first timeout sets the user to be inactive and the second
853 * timeout removes the user from the contact list, really we
854 * should remove the first timeout.
860 individual_store_individual_updated_cb (FolksIndividual *individual,
862 EmpathyIndividualStore *self)
864 DEBUG ("Individual'%s' updated, checking roster is in sync...",
865 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
867 individual_store_contact_update (self, individual);
871 individual_store_contact_updated_cb (EmpathyContact *contact,
873 EmpathyIndividualStore *self)
875 FolksIndividual *individual;
877 DEBUG ("Contact '%s' updated, checking roster is in sync...",
878 empathy_contact_get_alias (contact));
880 individual = g_object_get_data (G_OBJECT (contact), "individual");
881 if (individual == NULL)
884 individual_store_contact_update (self, individual);
888 individual_personas_changed_cb (FolksIndividual *individual,
891 EmpathyIndividualStore *self)
895 DEBUG ("Individual '%s' personas-changed.",
896 folks_individual_get_id (individual));
898 iter = gee_iterable_iterator (GEE_ITERABLE (removed));
899 /* FIXME: libfolks hasn't grown capabilities support yet, so we have to go
900 * through the EmpathyContacts for them. */
901 while (gee_iterator_next (iter))
903 TpfPersona *persona = gee_iterator_get (iter);
904 TpContact *tp_contact;
905 EmpathyContact *contact;
907 if (TPF_IS_PERSONA (persona))
909 tp_contact = tpf_persona_get_contact (persona);
910 if (tp_contact != NULL)
912 contact = empathy_contact_dup_from_tp_contact (tp_contact);
913 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
915 g_object_set_data (G_OBJECT (contact), "individual", NULL);
916 g_signal_handlers_disconnect_by_func (contact,
917 (GCallback) individual_store_contact_updated_cb, self);
919 g_object_unref (contact);
923 g_clear_object (&persona);
925 g_clear_object (&iter);
927 iter = gee_iterable_iterator (GEE_ITERABLE (added));
928 while (gee_iterator_next (iter))
930 TpfPersona *persona = gee_iterator_get (iter);
931 TpContact *tp_contact;
932 EmpathyContact *contact;
934 if (TPF_IS_PERSONA (persona))
936 tp_contact = tpf_persona_get_contact (persona);
937 if (tp_contact != NULL)
939 contact = empathy_contact_dup_from_tp_contact (tp_contact);
940 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
942 g_object_set_data (G_OBJECT (contact), "individual", individual);
943 g_signal_connect (contact, "notify::capabilities",
944 (GCallback) individual_store_contact_updated_cb, self);
945 g_signal_connect (contact, "notify::client-types",
946 (GCallback) individual_store_contact_updated_cb, self);
948 g_object_unref (contact);
952 g_clear_object (&persona);
954 g_clear_object (&iter);
958 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
959 FolksIndividual *individual)
961 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
963 individual_store_add_individual (self, individual);
965 g_signal_connect (individual, "notify::avatar",
966 (GCallback) individual_store_individual_updated_cb, self);
967 g_signal_connect (individual, "notify::presence-type",
968 (GCallback) individual_store_individual_updated_cb, self);
969 g_signal_connect (individual, "notify::presence-message",
970 (GCallback) individual_store_individual_updated_cb, self);
971 g_signal_connect (individual, "notify::alias",
972 (GCallback) individual_store_individual_updated_cb, self);
973 g_signal_connect (individual, "personas-changed",
974 (GCallback) individual_personas_changed_cb, self);
976 /* provide an empty set so the callback can assume non-NULL sets */
977 individual_personas_changed_cb (individual,
978 folks_individual_get_personas (individual), empty_set, self);
979 g_clear_object (&empty_set);
983 individual_store_disconnect_individual (EmpathyIndividualStore *self,
984 FolksIndividual *individual)
986 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
988 /* provide an empty set so the callback can assume non-NULL sets */
989 individual_personas_changed_cb (individual, empty_set,
990 folks_individual_get_personas (individual), self);
991 g_clear_object (&empty_set);
993 g_signal_handlers_disconnect_by_func (individual,
994 (GCallback) individual_store_individual_updated_cb, self);
995 g_signal_handlers_disconnect_by_func (individual,
996 (GCallback) individual_personas_changed_cb, self);
1000 individual_store_remove_individual_and_disconnect (
1001 EmpathyIndividualStore *self,
1002 FolksIndividual *individual)
1004 individual_store_disconnect_individual (self, individual);
1005 individual_store_remove_individual (self, individual);
1009 individual_store_members_changed_cb (EmpathyIndividualManager *manager,
1010 const gchar *message,
1014 EmpathyIndividualStore *self)
1018 for (l = added; l; l = l->next)
1020 DEBUG ("Individual %s %s", folks_individual_get_id (l->data), "added");
1022 individual_store_add_individual_and_connect (self, l->data);
1024 for (l = removed; l; l = l->next)
1026 DEBUG ("Individual %s %s",
1027 folks_individual_get_id (l->data), "removed");
1029 individual_store_remove_individual_and_disconnect (self, l->data);
1034 individual_store_favourites_changed_cb (EmpathyIndividualManager *manager,
1035 FolksIndividual *individual,
1036 gboolean is_favourite,
1037 EmpathyIndividualStore *self)
1039 DEBUG ("Individual %s is %s a favourite",
1040 folks_individual_get_id (individual),
1041 is_favourite ? "now" : "no longer");
1043 individual_store_remove_individual (self, individual);
1044 individual_store_add_individual (self, individual);
1048 individual_store_groups_changed_cb (EmpathyIndividualManager *manager,
1049 FolksIndividual *individual,
1052 EmpathyIndividualStore *self)
1054 EmpathyIndividualStorePriv *priv;
1055 gboolean show_active;
1057 priv = GET_PRIV (self);
1059 DEBUG ("Updating groups for individual %s",
1060 folks_individual_get_id (individual));
1062 /* We do this to make sure the groups are correct, if not, we
1063 * would have to check the groups already set up for each
1064 * contact and then see what has been updated.
1066 show_active = priv->show_active;
1067 priv->show_active = FALSE;
1068 individual_store_remove_individual (self, individual);
1069 individual_store_add_individual (self, individual);
1070 priv->show_active = show_active;
1074 individual_store_manager_setup (gpointer user_data)
1076 EmpathyIndividualStore *self = user_data;
1077 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1080 /* Signal connection. */
1082 /* TODO: implement */
1083 DEBUG ("handling individual renames unimplemented");
1085 g_signal_connect (priv->manager,
1087 G_CALLBACK (individual_store_members_changed_cb), self);
1089 g_signal_connect (priv->manager,
1090 "favourites-changed",
1091 G_CALLBACK (individual_store_favourites_changed_cb), self);
1093 g_signal_connect (priv->manager,
1095 G_CALLBACK (individual_store_groups_changed_cb), self);
1097 /* Add contacts already created. */
1098 individuals = empathy_individual_manager_get_members (priv->manager);
1099 if (individuals != NULL && FOLKS_IS_INDIVIDUAL (individuals->data))
1101 individual_store_members_changed_cb (priv->manager, "initial add",
1102 individuals, NULL, 0, self);
1103 g_list_free (individuals);
1106 priv->setup_idle_id = 0;
1111 individual_store_set_individual_manager (EmpathyIndividualStore *self,
1112 EmpathyIndividualManager *manager)
1114 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1116 priv->manager = g_object_ref (manager);
1118 /* Let a chance to have all properties set before populating */
1119 priv->setup_idle_id = g_idle_add (individual_store_manager_setup, self);
1123 individual_store_member_renamed_cb (EmpathyIndividualManager *manager,
1124 FolksIndividual *old_individual,
1125 FolksIndividual *new_individual,
1127 const gchar *message,
1128 EmpathyIndividualStore *self)
1130 DEBUG ("Individual %s renamed to %s",
1131 folks_individual_get_id (old_individual),
1132 folks_individual_get_id (new_individual));
1134 /* add the new contact */
1135 individual_store_add_individual_and_connect (self, new_individual);
1137 /* remove old contact */
1138 individual_store_remove_individual_and_disconnect (self, old_individual);
1142 individual_store_dispose (GObject *object)
1144 EmpathyIndividualStorePriv *priv = GET_PRIV (object);
1145 GList *individuals, *l;
1147 if (priv->dispose_has_run)
1149 priv->dispose_has_run = TRUE;
1151 /* Cancel any pending avatar load operations */
1152 for (l = priv->avatar_cancellables; l != NULL; l = l->next)
1154 /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
1155 g_cancellable_cancel (G_CANCELLABLE (l->data));
1157 g_list_free (priv->avatar_cancellables);
1159 individuals = empathy_individual_manager_get_members (priv->manager);
1160 for (l = individuals; l; l = l->next)
1162 individual_store_disconnect_individual (EMPATHY_INDIVIDUAL_STORE (object),
1163 FOLKS_INDIVIDUAL (l->data));
1165 g_list_free (individuals);
1167 g_signal_handlers_disconnect_by_func (priv->manager,
1168 G_CALLBACK (individual_store_member_renamed_cb), object);
1169 g_signal_handlers_disconnect_by_func (priv->manager,
1170 G_CALLBACK (individual_store_members_changed_cb), object);
1171 g_signal_handlers_disconnect_by_func (priv->manager,
1172 G_CALLBACK (individual_store_favourites_changed_cb), object);
1173 g_signal_handlers_disconnect_by_func (priv->manager,
1174 G_CALLBACK (individual_store_groups_changed_cb), object);
1175 g_object_unref (priv->manager);
1177 if (priv->inhibit_active)
1179 g_source_remove (priv->inhibit_active);
1182 if (priv->setup_idle_id != 0)
1184 g_source_remove (priv->setup_idle_id);
1187 g_hash_table_destroy (priv->status_icons);
1188 g_hash_table_destroy (priv->folks_individual_cache);
1189 g_hash_table_destroy (priv->empathy_group_cache);
1190 G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
1194 individual_store_get_property (GObject *object,
1199 EmpathyIndividualStorePriv *priv;
1201 priv = GET_PRIV (object);
1205 case PROP_INDIVIDUAL_MANAGER:
1206 g_value_set_object (value, priv->manager);
1208 case PROP_SHOW_AVATARS:
1209 g_value_set_boolean (value, priv->show_avatars);
1211 case PROP_SHOW_PROTOCOLS:
1212 g_value_set_boolean (value, priv->show_protocols);
1214 case PROP_SHOW_GROUPS:
1215 g_value_set_boolean (value, priv->show_groups);
1217 case PROP_IS_COMPACT:
1218 g_value_set_boolean (value, priv->is_compact);
1220 case PROP_SORT_CRITERIUM:
1221 g_value_set_enum (value, priv->sort_criterium);
1224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1230 individual_store_set_property (GObject *object,
1232 const GValue *value,
1237 case PROP_INDIVIDUAL_MANAGER:
1238 individual_store_set_individual_manager (EMPATHY_INDIVIDUAL_STORE
1239 (object), g_value_get_object (value));
1241 case PROP_SHOW_AVATARS:
1242 empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
1243 (object), g_value_get_boolean (value));
1245 case PROP_SHOW_PROTOCOLS:
1246 empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1247 (object), g_value_get_boolean (value));
1249 case PROP_SHOW_GROUPS:
1250 empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1251 (object), g_value_get_boolean (value));
1253 case PROP_IS_COMPACT:
1254 empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1255 (object), g_value_get_boolean (value));
1257 case PROP_SORT_CRITERIUM:
1258 empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1259 (object), g_value_get_enum (value));
1262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1268 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1270 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1272 object_class->dispose = individual_store_dispose;
1273 object_class->get_property = individual_store_get_property;
1274 object_class->set_property = individual_store_set_property;
1276 g_object_class_install_property (object_class,
1277 PROP_INDIVIDUAL_MANAGER,
1278 g_param_spec_object ("individual-manager",
1279 "The individual manager",
1280 "The individual manager",
1281 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1282 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1283 g_object_class_install_property (object_class,
1285 g_param_spec_boolean ("show-avatars",
1287 "Whether contact list should display "
1288 "avatars for contacts", TRUE, G_PARAM_READWRITE));
1289 g_object_class_install_property (object_class,
1290 PROP_SHOW_PROTOCOLS,
1291 g_param_spec_boolean ("show-protocols",
1293 "Whether contact list should display "
1294 "protocols for contacts", FALSE, G_PARAM_READWRITE));
1295 g_object_class_install_property (object_class,
1297 g_param_spec_boolean ("show-groups",
1299 "Whether contact list should display "
1300 "contact groups", TRUE, G_PARAM_READWRITE));
1301 g_object_class_install_property (object_class,
1303 g_param_spec_boolean ("is-compact",
1305 "Whether the contact list is in compact mode or not",
1306 FALSE, G_PARAM_READWRITE));
1308 g_object_class_install_property (object_class,
1309 PROP_SORT_CRITERIUM,
1310 g_param_spec_enum ("sort-criterium",
1312 "The sort criterium to use for sorting the contact list",
1313 EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1314 EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1316 g_type_class_add_private (object_class,
1317 sizeof (EmpathyIndividualStorePriv));
1321 get_position (const char **strv,
1326 for (i = 0; strv[i] != NULL; i++)
1328 if (!tp_strdiff (strv[i], str))
1336 compare_separator_and_groups (gboolean is_separator_a,
1337 gboolean is_separator_b,
1338 const gchar *name_a,
1339 const gchar *name_b,
1340 FolksIndividual *individual_a,
1341 FolksIndividual *individual_b,
1342 gboolean fake_group_a,
1343 gboolean fake_group_b)
1345 /* these two lists are the sorted list of fake groups to include at the
1346 * top and bottom of the roster */
1347 const char *top_groups[] = {
1348 EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1352 const char *bottom_groups[] = {
1353 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1357 if (is_separator_a || is_separator_b)
1359 /* We have at least one separator */
1364 else if (is_separator_b)
1370 /* One group and one contact */
1371 if (!individual_a && individual_b)
1375 else if (individual_a && !individual_b)
1379 else if (!individual_a && !individual_b)
1381 gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1383 a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1384 b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1385 a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1386 b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1388 if (a_in_top && b_in_top)
1390 /* compare positions */
1391 return CLAMP (get_position (top_groups, name_a) -
1392 get_position (top_groups, name_b), -1, 1);
1394 else if (a_in_bottom && b_in_bottom)
1396 /* compare positions */
1397 return CLAMP (get_position (bottom_groups, name_a) -
1398 get_position (bottom_groups, name_b), -1, 1);
1400 else if (a_in_top || b_in_bottom)
1404 else if (b_in_top || a_in_bottom)
1410 return g_utf8_collate (name_a, name_b);
1414 /* Two contacts, ordering depends of the sorting policy */
1419 individual_store_contact_sort (FolksIndividual *individual_a,
1420 FolksIndividual *individual_b)
1423 EmpathyContact *contact_a = NULL, *contact_b = NULL;
1424 TpAccount *account_a, *account_b;
1426 g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1429 ret_val = g_utf8_collate (
1430 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_a)),
1431 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_b)));
1436 contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1437 contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1438 if (contact_a != NULL && contact_b != NULL)
1440 account_a = empathy_contact_get_account (contact_a);
1441 account_b = empathy_contact_get_account (contact_b);
1443 g_assert (account_a != NULL);
1444 g_assert (account_b != NULL);
1447 ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1448 tp_account_get_protocol (account_b));
1454 ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1455 tp_proxy_get_object_path (account_b));
1462 ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1463 folks_individual_get_id (individual_b));
1466 tp_clear_object (&contact_a);
1467 tp_clear_object (&contact_b);
1473 individual_store_state_sort_func (GtkTreeModel *model,
1474 GtkTreeIter *iter_a,
1475 GtkTreeIter *iter_b,
1479 FolksIndividual *individual_a, *individual_b;
1480 gchar *name_a, *name_b;
1481 gboolean is_separator_a, is_separator_b;
1482 gboolean fake_group_a, fake_group_b;
1483 FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1484 TpConnectionPresenceType tp_presence_a, tp_presence_b;
1486 gtk_tree_model_get (model, iter_a,
1487 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1488 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1489 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1490 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1491 gtk_tree_model_get (model, iter_b,
1492 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1493 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1494 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1495 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1497 if (individual_a == NULL || individual_b == NULL)
1499 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1500 name_a, name_b, individual_a, individual_b, fake_group_a,
1505 /* If we managed to get this far, we can start looking at
1508 folks_presence_type_a =
1509 folks_presence_details_get_presence_type (
1510 FOLKS_PRESENCE_DETAILS (individual_a));
1511 folks_presence_type_b =
1512 folks_presence_details_get_presence_type (
1513 FOLKS_PRESENCE_DETAILS (individual_b));
1514 tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1515 tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1517 ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1522 /* Fallback: compare by name et al. */
1523 ret_val = individual_store_contact_sort (individual_a, individual_b);
1529 tp_clear_object (&individual_a);
1530 tp_clear_object (&individual_b);
1536 individual_store_name_sort_func (GtkTreeModel *model,
1537 GtkTreeIter *iter_a,
1538 GtkTreeIter *iter_b,
1541 gchar *name_a, *name_b;
1542 FolksIndividual *individual_a, *individual_b;
1543 gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1545 gboolean fake_group_a, fake_group_b;
1547 gtk_tree_model_get (model, iter_a,
1548 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1549 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1550 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1551 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1552 gtk_tree_model_get (model, iter_b,
1553 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1554 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1555 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1556 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1558 if (individual_a == NULL || individual_b == NULL)
1559 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1560 name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1562 ret_val = individual_store_contact_sort (individual_a, individual_b);
1564 tp_clear_object (&individual_a);
1565 tp_clear_object (&individual_b);
1573 individual_store_setup (EmpathyIndividualStore *self)
1575 EmpathyIndividualStorePriv *priv;
1577 GDK_TYPE_PIXBUF, /* Status pixbuf */
1578 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1579 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1580 G_TYPE_STRING, /* Name */
1581 G_TYPE_UINT, /* Presence type */
1582 G_TYPE_STRING, /* Status string */
1583 G_TYPE_BOOLEAN, /* Compact view */
1584 FOLKS_TYPE_INDIVIDUAL, /* Individual type */
1585 G_TYPE_BOOLEAN, /* Is group */
1586 G_TYPE_BOOLEAN, /* Is active */
1587 G_TYPE_BOOLEAN, /* Is online */
1588 G_TYPE_BOOLEAN, /* Is separator */
1589 G_TYPE_BOOLEAN, /* Can make audio calls */
1590 G_TYPE_BOOLEAN, /* Can make video calls */
1591 G_TYPE_BOOLEAN, /* Is a fake group */
1592 G_TYPE_STRV, /* Client types */
1593 G_TYPE_UINT, /* Event count */
1596 priv = GET_PRIV (self);
1598 gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1599 EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1601 /* Set up sorting */
1602 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1603 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1604 individual_store_name_sort_func, self, NULL);
1605 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1606 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1607 individual_store_state_sort_func, self, NULL);
1609 priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1610 empathy_individual_store_set_sort_criterium (self, priv->sort_criterium);
1614 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1616 EmpathyIndividualStorePriv *priv;
1618 priv = GET_PRIV (self);
1620 priv->show_active = TRUE;
1621 priv->inhibit_active = 0;
1627 g_queue_free_full_row_ref (gpointer data)
1629 GQueue *queue = (GQueue *) data;
1630 g_queue_foreach (queue, (GFunc) gtk_tree_row_reference_free, NULL);
1631 g_queue_free (queue);
1635 empathy_individual_store_init (EmpathyIndividualStore *self)
1637 EmpathyIndividualStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1638 EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1641 priv->show_avatars = TRUE;
1642 priv->show_groups = TRUE;
1643 priv->show_protocols = FALSE;
1644 priv->inhibit_active =
1645 g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1646 (GSourceFunc) individual_store_inhibit_active_cb, self);
1647 priv->status_icons =
1648 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1649 priv->folks_individual_cache = g_hash_table_new_full (NULL, NULL, NULL,
1650 g_queue_free_full_row_ref);
1651 priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
1652 g_str_equal, g_free,
1653 (GDestroyNotify) gtk_tree_row_reference_free);
1654 individual_store_setup (self);
1657 EmpathyIndividualStore *
1658 empathy_individual_store_new (EmpathyIndividualManager *manager)
1660 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1662 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE,
1663 "individual-manager", manager, NULL);
1666 EmpathyIndividualManager *
1667 empathy_individual_store_get_manager (EmpathyIndividualStore *self)
1669 EmpathyIndividualStorePriv *priv;
1671 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), FALSE);
1673 priv = GET_PRIV (self);
1675 return priv->manager;
1679 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1681 EmpathyIndividualStorePriv *priv;
1683 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1685 priv = GET_PRIV (self);
1687 return priv->show_avatars;
1691 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1694 EmpathyIndividualStore *self)
1696 EmpathyIndividualStorePriv *priv;
1697 gboolean show_avatar = FALSE;
1698 FolksIndividual *individual;
1699 GdkPixbuf *pixbuf_status;
1701 priv = GET_PRIV (self);
1703 if (priv->show_avatars && !priv->is_compact)
1708 gtk_tree_model_get (model, iter,
1709 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1711 if (individual == NULL)
1715 /* get icon from hash_table */
1717 empathy_individual_store_get_individual_status_icon (self, individual);
1719 gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1720 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1721 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1722 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact, -1);
1724 g_object_unref (individual);
1730 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1731 gboolean show_avatars)
1733 EmpathyIndividualStorePriv *priv;
1734 GtkTreeModel *model;
1736 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1738 priv = GET_PRIV (self);
1740 priv->show_avatars = show_avatars;
1742 model = GTK_TREE_MODEL (self);
1744 gtk_tree_model_foreach (model,
1745 (GtkTreeModelForeachFunc)
1746 individual_store_update_list_mode_foreach, self);
1748 g_object_notify (G_OBJECT (self), "show-avatars");
1752 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1754 EmpathyIndividualStorePriv *priv;
1756 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1758 priv = GET_PRIV (self);
1760 return priv->show_protocols;
1764 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1765 gboolean show_protocols)
1767 EmpathyIndividualStorePriv *priv;
1768 GtkTreeModel *model;
1770 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1772 priv = GET_PRIV (self);
1774 priv->show_protocols = show_protocols;
1776 model = GTK_TREE_MODEL (self);
1778 gtk_tree_model_foreach (model,
1779 (GtkTreeModelForeachFunc)
1780 individual_store_update_list_mode_foreach, self);
1782 g_object_notify (G_OBJECT (self), "show-protocols");
1786 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1788 EmpathyIndividualStorePriv *priv;
1790 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1792 priv = GET_PRIV (self);
1794 return priv->show_groups;
1798 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1799 gboolean show_groups)
1801 EmpathyIndividualStorePriv *priv;
1803 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1805 priv = GET_PRIV (self);
1807 if (priv->show_groups == show_groups)
1812 priv->show_groups = show_groups;
1814 if (priv->setup_idle_id == 0)
1816 /* Remove all contacts and add them back, not optimized but
1817 * that's the easy way :)
1819 * This is only done if there's not a pending setup idle
1820 * callback, otherwise it will race and the contacts will get
1824 gtk_tree_store_clear (GTK_TREE_STORE (self));
1825 /* Also clear the cache */
1826 g_hash_table_remove_all (priv->folks_individual_cache);
1827 g_hash_table_remove_all (priv->empathy_group_cache);
1829 contacts = empathy_individual_manager_get_members (priv->manager);
1831 individual_store_members_changed_cb (priv->manager,
1832 "re-adding members: toggled group visibility",
1833 contacts, NULL, 0, self);
1834 g_list_free (contacts);
1837 g_object_notify (G_OBJECT (self), "show-groups");
1841 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1843 EmpathyIndividualStorePriv *priv;
1845 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1847 priv = GET_PRIV (self);
1849 return priv->is_compact;
1853 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1854 gboolean is_compact)
1856 EmpathyIndividualStorePriv *priv;
1857 GtkTreeModel *model;
1859 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1861 priv = GET_PRIV (self);
1863 priv->is_compact = is_compact;
1865 model = GTK_TREE_MODEL (self);
1867 gtk_tree_model_foreach (model,
1868 (GtkTreeModelForeachFunc)
1869 individual_store_update_list_mode_foreach, self);
1871 g_object_notify (G_OBJECT (self), "is-compact");
1874 EmpathyIndividualStoreSort
1875 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1877 EmpathyIndividualStorePriv *priv;
1879 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1881 priv = GET_PRIV (self);
1883 return priv->sort_criterium;
1887 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1888 EmpathyIndividualStoreSort sort_criterium)
1890 EmpathyIndividualStorePriv *priv;
1892 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1894 priv = GET_PRIV (self);
1896 priv->sort_criterium = sort_criterium;
1898 switch (sort_criterium)
1900 case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1901 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1902 EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1905 case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1906 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1907 EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1911 g_assert_not_reached ();
1914 g_object_notify (G_OBJECT (self), "sort-criterium");
1918 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1922 gboolean is_separator = FALSE;
1924 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1926 gtk_tree_model_get (model, iter,
1927 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1929 return is_separator;
1933 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1935 gboolean *path_is_group,
1936 gboolean *is_fake_group)
1938 GtkTreeIter parent_iter, iter;
1941 gboolean fake = FALSE;
1943 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1947 *path_is_group = FALSE;
1950 if (!gtk_tree_model_get_iter (model, &iter, path))
1955 gtk_tree_model_get (model, &iter,
1956 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1957 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1964 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1971 gtk_tree_model_get (model, &iter,
1972 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1973 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1974 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1984 *path_is_group = TRUE;
1987 if (is_fake_group != NULL)
1988 *is_fake_group = fake;
1994 individual_store_get_individual_status_icon_with_icon_name (
1995 EmpathyIndividualStore *self,
1996 FolksIndividual *individual,
1997 const gchar *status_icon_name)
1999 GdkPixbuf *pixbuf_status;
2000 EmpathyIndividualStorePriv *priv;
2001 const gchar *protocol_name = NULL;
2002 gchar *icon_name = NULL;
2005 guint contact_count = 0;
2006 EmpathyContact *contact = NULL;
2007 gboolean show_protocols_here;
2009 priv = GET_PRIV (self);
2011 personas = folks_individual_get_personas (individual);
2012 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2013 while (gee_iterator_next (iter))
2015 FolksPersona *persona = gee_iterator_get (iter);
2016 if (empathy_folks_persona_is_interesting (persona))
2019 g_clear_object (&persona);
2021 if (contact_count > 1)
2024 g_clear_object (&iter);
2026 show_protocols_here = (priv->show_protocols && (contact_count == 1));
2027 if (show_protocols_here)
2029 contact = empathy_contact_dup_from_folks_individual (individual);
2030 if (contact != NULL)
2032 protocol_name = empathy_protocol_name_for_contact (contact);
2033 icon_name = g_strdup_printf ("%s-%s", status_icon_name,
2038 g_warning ("Cannot retrieve contact from individual '%s'",
2039 folks_alias_details_get_alias (
2040 FOLKS_ALIAS_DETAILS (individual)));
2047 icon_name = g_strdup_printf ("%s", status_icon_name);
2050 pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
2052 if (pixbuf_status == NULL)
2055 empathy_pixbuf_contact_status_icon_with_icon_name (contact,
2056 status_icon_name, show_protocols_here);
2058 if (pixbuf_status != NULL)
2060 /* pass the reference to the hash table */
2061 g_hash_table_insert (priv->status_icons,
2062 g_strdup (icon_name), pixbuf_status);
2067 tp_clear_object (&contact);
2069 return pixbuf_status;
2073 empathy_individual_store_get_individual_status_icon (
2074 EmpathyIndividualStore *self,
2075 FolksIndividual *individual)
2077 GdkPixbuf *pixbuf_status = NULL;
2078 const gchar *status_icon_name = NULL;
2080 status_icon_name = empathy_icon_name_for_individual (individual);
2081 if (status_icon_name == NULL)
2085 individual_store_get_individual_status_icon_with_icon_name (self,
2086 individual, status_icon_name);
2088 return pixbuf_status;