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_favourites_changed_cb (FolksIndividual *individual,
960 EmpathyIndividualStore *self)
962 DEBUG ("Individual %s is %s a favourite",
963 folks_individual_get_id (individual),
964 folks_favourite_details_get_is_favourite (
965 FOLKS_FAVOURITE_DETAILS (individual)) ? "now" : "no longer");
967 individual_store_remove_individual (self, individual);
968 individual_store_add_individual (self, individual);
972 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
973 FolksIndividual *individual)
975 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
977 individual_store_add_individual (self, individual);
979 g_signal_connect (individual, "notify::avatar",
980 (GCallback) individual_store_individual_updated_cb, self);
981 g_signal_connect (individual, "notify::presence-type",
982 (GCallback) individual_store_individual_updated_cb, self);
983 g_signal_connect (individual, "notify::presence-message",
984 (GCallback) individual_store_individual_updated_cb, self);
985 g_signal_connect (individual, "notify::alias",
986 (GCallback) individual_store_individual_updated_cb, self);
987 g_signal_connect (individual, "personas-changed",
988 (GCallback) individual_personas_changed_cb, self);
989 g_signal_connect (individual, "notify::is-favourite",
990 (GCallback) individual_store_favourites_changed_cb, self);
992 /* provide an empty set so the callback can assume non-NULL sets */
993 individual_personas_changed_cb (individual,
994 folks_individual_get_personas (individual), empty_set, self);
995 g_clear_object (&empty_set);
999 individual_store_disconnect_individual (EmpathyIndividualStore *self,
1000 FolksIndividual *individual)
1002 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
1004 /* provide an empty set so the callback can assume non-NULL sets */
1005 individual_personas_changed_cb (individual, empty_set,
1006 folks_individual_get_personas (individual), self);
1007 g_clear_object (&empty_set);
1009 g_signal_handlers_disconnect_by_func (individual,
1010 (GCallback) individual_store_individual_updated_cb, self);
1011 g_signal_handlers_disconnect_by_func (individual,
1012 (GCallback) individual_personas_changed_cb, self);
1013 g_signal_handlers_disconnect_by_func (individual,
1014 (GCallback) individual_store_favourites_changed_cb, self);
1018 individual_store_remove_individual_and_disconnect (
1019 EmpathyIndividualStore *self,
1020 FolksIndividual *individual)
1022 individual_store_disconnect_individual (self, individual);
1023 individual_store_remove_individual (self, individual);
1027 individual_store_members_changed_cb (EmpathyIndividualManager *manager,
1028 const gchar *message,
1032 EmpathyIndividualStore *self)
1036 for (l = added; l; l = l->next)
1038 DEBUG ("Individual %s %s", folks_individual_get_id (l->data), "added");
1040 individual_store_add_individual_and_connect (self, l->data);
1042 for (l = removed; l; l = l->next)
1044 DEBUG ("Individual %s %s",
1045 folks_individual_get_id (l->data), "removed");
1047 individual_store_remove_individual_and_disconnect (self, l->data);
1052 individual_store_groups_changed_cb (EmpathyIndividualManager *manager,
1053 FolksIndividual *individual,
1056 EmpathyIndividualStore *self)
1058 EmpathyIndividualStorePriv *priv;
1059 gboolean show_active;
1061 priv = GET_PRIV (self);
1063 DEBUG ("Updating groups for individual %s",
1064 folks_individual_get_id (individual));
1066 /* We do this to make sure the groups are correct, if not, we
1067 * would have to check the groups already set up for each
1068 * contact and then see what has been updated.
1070 show_active = priv->show_active;
1071 priv->show_active = FALSE;
1072 individual_store_remove_individual (self, individual);
1073 individual_store_add_individual (self, individual);
1074 priv->show_active = show_active;
1078 individual_store_manager_setup (gpointer user_data)
1080 EmpathyIndividualStore *self = user_data;
1081 EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1084 /* Signal connection. */
1086 /* TODO: implement */
1087 DEBUG ("handling individual renames unimplemented");
1089 g_signal_connect (priv->manager,
1091 G_CALLBACK (individual_store_members_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_groups_changed_cb), object);
1173 g_object_unref (priv->manager);
1175 if (priv->inhibit_active)
1177 g_source_remove (priv->inhibit_active);
1180 if (priv->setup_idle_id != 0)
1182 g_source_remove (priv->setup_idle_id);
1185 g_hash_table_destroy (priv->status_icons);
1186 g_hash_table_destroy (priv->folks_individual_cache);
1187 g_hash_table_destroy (priv->empathy_group_cache);
1188 G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
1192 individual_store_get_property (GObject *object,
1197 EmpathyIndividualStorePriv *priv;
1199 priv = GET_PRIV (object);
1203 case PROP_INDIVIDUAL_MANAGER:
1204 g_value_set_object (value, priv->manager);
1206 case PROP_SHOW_AVATARS:
1207 g_value_set_boolean (value, priv->show_avatars);
1209 case PROP_SHOW_PROTOCOLS:
1210 g_value_set_boolean (value, priv->show_protocols);
1212 case PROP_SHOW_GROUPS:
1213 g_value_set_boolean (value, priv->show_groups);
1215 case PROP_IS_COMPACT:
1216 g_value_set_boolean (value, priv->is_compact);
1218 case PROP_SORT_CRITERIUM:
1219 g_value_set_enum (value, priv->sort_criterium);
1222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1228 individual_store_set_property (GObject *object,
1230 const GValue *value,
1235 case PROP_INDIVIDUAL_MANAGER:
1236 individual_store_set_individual_manager (EMPATHY_INDIVIDUAL_STORE
1237 (object), g_value_get_object (value));
1239 case PROP_SHOW_AVATARS:
1240 empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
1241 (object), g_value_get_boolean (value));
1243 case PROP_SHOW_PROTOCOLS:
1244 empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1245 (object), g_value_get_boolean (value));
1247 case PROP_SHOW_GROUPS:
1248 empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1249 (object), g_value_get_boolean (value));
1251 case PROP_IS_COMPACT:
1252 empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1253 (object), g_value_get_boolean (value));
1255 case PROP_SORT_CRITERIUM:
1256 empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1257 (object), g_value_get_enum (value));
1260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1266 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1268 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1270 object_class->dispose = individual_store_dispose;
1271 object_class->get_property = individual_store_get_property;
1272 object_class->set_property = individual_store_set_property;
1274 g_object_class_install_property (object_class,
1275 PROP_INDIVIDUAL_MANAGER,
1276 g_param_spec_object ("individual-manager",
1277 "The individual manager",
1278 "The individual manager",
1279 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1280 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1281 g_object_class_install_property (object_class,
1283 g_param_spec_boolean ("show-avatars",
1285 "Whether contact list should display "
1286 "avatars for contacts", TRUE, G_PARAM_READWRITE));
1287 g_object_class_install_property (object_class,
1288 PROP_SHOW_PROTOCOLS,
1289 g_param_spec_boolean ("show-protocols",
1291 "Whether contact list should display "
1292 "protocols for contacts", FALSE, G_PARAM_READWRITE));
1293 g_object_class_install_property (object_class,
1295 g_param_spec_boolean ("show-groups",
1297 "Whether contact list should display "
1298 "contact groups", TRUE, G_PARAM_READWRITE));
1299 g_object_class_install_property (object_class,
1301 g_param_spec_boolean ("is-compact",
1303 "Whether the contact list is in compact mode or not",
1304 FALSE, G_PARAM_READWRITE));
1306 g_object_class_install_property (object_class,
1307 PROP_SORT_CRITERIUM,
1308 g_param_spec_enum ("sort-criterium",
1310 "The sort criterium to use for sorting the contact list",
1311 EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1312 EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1314 g_type_class_add_private (object_class,
1315 sizeof (EmpathyIndividualStorePriv));
1319 get_position (const char **strv,
1324 for (i = 0; strv[i] != NULL; i++)
1326 if (!tp_strdiff (strv[i], str))
1334 compare_separator_and_groups (gboolean is_separator_a,
1335 gboolean is_separator_b,
1336 const gchar *name_a,
1337 const gchar *name_b,
1338 FolksIndividual *individual_a,
1339 FolksIndividual *individual_b,
1340 gboolean fake_group_a,
1341 gboolean fake_group_b)
1343 /* these two lists are the sorted list of fake groups to include at the
1344 * top and bottom of the roster */
1345 const char *top_groups[] = {
1346 EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1350 const char *bottom_groups[] = {
1351 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1355 if (is_separator_a || is_separator_b)
1357 /* We have at least one separator */
1362 else if (is_separator_b)
1368 /* One group and one contact */
1369 if (!individual_a && individual_b)
1373 else if (individual_a && !individual_b)
1377 else if (!individual_a && !individual_b)
1379 gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1381 a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1382 b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1383 a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1384 b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1386 if (a_in_top && b_in_top)
1388 /* compare positions */
1389 return CLAMP (get_position (top_groups, name_a) -
1390 get_position (top_groups, name_b), -1, 1);
1392 else if (a_in_bottom && b_in_bottom)
1394 /* compare positions */
1395 return CLAMP (get_position (bottom_groups, name_a) -
1396 get_position (bottom_groups, name_b), -1, 1);
1398 else if (a_in_top || b_in_bottom)
1402 else if (b_in_top || a_in_bottom)
1408 return g_utf8_collate (name_a, name_b);
1412 /* Two contacts, ordering depends of the sorting policy */
1417 individual_store_contact_sort (FolksIndividual *individual_a,
1418 FolksIndividual *individual_b)
1421 EmpathyContact *contact_a = NULL, *contact_b = NULL;
1422 TpAccount *account_a, *account_b;
1424 g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1427 ret_val = g_utf8_collate (
1428 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_a)),
1429 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_b)));
1434 contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1435 contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1436 if (contact_a != NULL && contact_b != NULL)
1438 account_a = empathy_contact_get_account (contact_a);
1439 account_b = empathy_contact_get_account (contact_b);
1441 g_assert (account_a != NULL);
1442 g_assert (account_b != NULL);
1445 ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1446 tp_account_get_protocol (account_b));
1452 ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1453 tp_proxy_get_object_path (account_b));
1460 ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1461 folks_individual_get_id (individual_b));
1464 tp_clear_object (&contact_a);
1465 tp_clear_object (&contact_b);
1471 individual_store_state_sort_func (GtkTreeModel *model,
1472 GtkTreeIter *iter_a,
1473 GtkTreeIter *iter_b,
1477 FolksIndividual *individual_a, *individual_b;
1478 gchar *name_a, *name_b;
1479 gboolean is_separator_a, is_separator_b;
1480 gboolean fake_group_a, fake_group_b;
1481 FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1482 TpConnectionPresenceType tp_presence_a, tp_presence_b;
1484 gtk_tree_model_get (model, iter_a,
1485 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1486 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1487 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1488 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1489 gtk_tree_model_get (model, iter_b,
1490 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1491 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1492 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1493 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1495 if (individual_a == NULL || individual_b == NULL)
1497 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1498 name_a, name_b, individual_a, individual_b, fake_group_a,
1503 /* If we managed to get this far, we can start looking at
1506 folks_presence_type_a =
1507 folks_presence_details_get_presence_type (
1508 FOLKS_PRESENCE_DETAILS (individual_a));
1509 folks_presence_type_b =
1510 folks_presence_details_get_presence_type (
1511 FOLKS_PRESENCE_DETAILS (individual_b));
1512 tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1513 tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1515 ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1520 /* Fallback: compare by name et al. */
1521 ret_val = individual_store_contact_sort (individual_a, individual_b);
1527 tp_clear_object (&individual_a);
1528 tp_clear_object (&individual_b);
1534 individual_store_name_sort_func (GtkTreeModel *model,
1535 GtkTreeIter *iter_a,
1536 GtkTreeIter *iter_b,
1539 gchar *name_a, *name_b;
1540 FolksIndividual *individual_a, *individual_b;
1541 gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1543 gboolean fake_group_a, fake_group_b;
1545 gtk_tree_model_get (model, iter_a,
1546 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1547 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1548 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1549 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1550 gtk_tree_model_get (model, iter_b,
1551 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1552 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1553 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1554 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1556 if (individual_a == NULL || individual_b == NULL)
1557 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1558 name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1560 ret_val = individual_store_contact_sort (individual_a, individual_b);
1562 tp_clear_object (&individual_a);
1563 tp_clear_object (&individual_b);
1571 individual_store_setup (EmpathyIndividualStore *self)
1573 EmpathyIndividualStorePriv *priv;
1575 GDK_TYPE_PIXBUF, /* Status pixbuf */
1576 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1577 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1578 G_TYPE_STRING, /* Name */
1579 G_TYPE_UINT, /* Presence type */
1580 G_TYPE_STRING, /* Status string */
1581 G_TYPE_BOOLEAN, /* Compact view */
1582 FOLKS_TYPE_INDIVIDUAL, /* Individual type */
1583 G_TYPE_BOOLEAN, /* Is group */
1584 G_TYPE_BOOLEAN, /* Is active */
1585 G_TYPE_BOOLEAN, /* Is online */
1586 G_TYPE_BOOLEAN, /* Is separator */
1587 G_TYPE_BOOLEAN, /* Can make audio calls */
1588 G_TYPE_BOOLEAN, /* Can make video calls */
1589 G_TYPE_BOOLEAN, /* Is a fake group */
1590 G_TYPE_STRV, /* Client types */
1591 G_TYPE_UINT, /* Event count */
1594 priv = GET_PRIV (self);
1596 gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1597 EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1599 /* Set up sorting */
1600 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1601 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1602 individual_store_name_sort_func, self, NULL);
1603 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1604 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1605 individual_store_state_sort_func, self, NULL);
1607 priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1608 empathy_individual_store_set_sort_criterium (self, priv->sort_criterium);
1612 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1614 EmpathyIndividualStorePriv *priv;
1616 priv = GET_PRIV (self);
1618 priv->show_active = TRUE;
1619 priv->inhibit_active = 0;
1625 g_queue_free_full_row_ref (gpointer data)
1627 GQueue *queue = (GQueue *) data;
1628 g_queue_foreach (queue, (GFunc) gtk_tree_row_reference_free, NULL);
1629 g_queue_free (queue);
1633 empathy_individual_store_init (EmpathyIndividualStore *self)
1635 EmpathyIndividualStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1636 EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1639 priv->show_avatars = TRUE;
1640 priv->show_groups = TRUE;
1641 priv->show_protocols = FALSE;
1642 priv->inhibit_active =
1643 g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1644 (GSourceFunc) individual_store_inhibit_active_cb, self);
1645 priv->status_icons =
1646 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1647 priv->folks_individual_cache = g_hash_table_new_full (NULL, NULL, NULL,
1648 g_queue_free_full_row_ref);
1649 priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
1650 g_str_equal, g_free,
1651 (GDestroyNotify) gtk_tree_row_reference_free);
1652 individual_store_setup (self);
1655 EmpathyIndividualStore *
1656 empathy_individual_store_new (EmpathyIndividualManager *manager)
1658 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1660 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE,
1661 "individual-manager", manager, NULL);
1664 EmpathyIndividualManager *
1665 empathy_individual_store_get_manager (EmpathyIndividualStore *self)
1667 EmpathyIndividualStorePriv *priv;
1669 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), FALSE);
1671 priv = GET_PRIV (self);
1673 return priv->manager;
1677 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1679 EmpathyIndividualStorePriv *priv;
1681 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1683 priv = GET_PRIV (self);
1685 return priv->show_avatars;
1689 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1692 EmpathyIndividualStore *self)
1694 EmpathyIndividualStorePriv *priv;
1695 gboolean show_avatar = FALSE;
1696 FolksIndividual *individual;
1697 GdkPixbuf *pixbuf_status;
1699 priv = GET_PRIV (self);
1701 if (priv->show_avatars && !priv->is_compact)
1706 gtk_tree_model_get (model, iter,
1707 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1709 if (individual == NULL)
1713 /* get icon from hash_table */
1715 empathy_individual_store_get_individual_status_icon (self, individual);
1717 gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1718 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1719 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1720 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact, -1);
1722 g_object_unref (individual);
1728 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1729 gboolean show_avatars)
1731 EmpathyIndividualStorePriv *priv;
1732 GtkTreeModel *model;
1734 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1736 priv = GET_PRIV (self);
1738 priv->show_avatars = show_avatars;
1740 model = GTK_TREE_MODEL (self);
1742 gtk_tree_model_foreach (model,
1743 (GtkTreeModelForeachFunc)
1744 individual_store_update_list_mode_foreach, self);
1746 g_object_notify (G_OBJECT (self), "show-avatars");
1750 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1752 EmpathyIndividualStorePriv *priv;
1754 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1756 priv = GET_PRIV (self);
1758 return priv->show_protocols;
1762 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1763 gboolean show_protocols)
1765 EmpathyIndividualStorePriv *priv;
1766 GtkTreeModel *model;
1768 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1770 priv = GET_PRIV (self);
1772 priv->show_protocols = show_protocols;
1774 model = GTK_TREE_MODEL (self);
1776 gtk_tree_model_foreach (model,
1777 (GtkTreeModelForeachFunc)
1778 individual_store_update_list_mode_foreach, self);
1780 g_object_notify (G_OBJECT (self), "show-protocols");
1784 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1786 EmpathyIndividualStorePriv *priv;
1788 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1790 priv = GET_PRIV (self);
1792 return priv->show_groups;
1796 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1797 gboolean show_groups)
1799 EmpathyIndividualStorePriv *priv;
1801 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1803 priv = GET_PRIV (self);
1805 if (priv->show_groups == show_groups)
1810 priv->show_groups = show_groups;
1812 if (priv->setup_idle_id == 0)
1814 /* Remove all contacts and add them back, not optimized but
1815 * that's the easy way :)
1817 * This is only done if there's not a pending setup idle
1818 * callback, otherwise it will race and the contacts will get
1822 gtk_tree_store_clear (GTK_TREE_STORE (self));
1823 /* Also clear the cache */
1824 g_hash_table_remove_all (priv->folks_individual_cache);
1825 g_hash_table_remove_all (priv->empathy_group_cache);
1827 contacts = empathy_individual_manager_get_members (priv->manager);
1829 individual_store_members_changed_cb (priv->manager,
1830 "re-adding members: toggled group visibility",
1831 contacts, NULL, 0, self);
1832 g_list_free (contacts);
1835 g_object_notify (G_OBJECT (self), "show-groups");
1839 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1841 EmpathyIndividualStorePriv *priv;
1843 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1845 priv = GET_PRIV (self);
1847 return priv->is_compact;
1851 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1852 gboolean is_compact)
1854 EmpathyIndividualStorePriv *priv;
1855 GtkTreeModel *model;
1857 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1859 priv = GET_PRIV (self);
1861 priv->is_compact = is_compact;
1863 model = GTK_TREE_MODEL (self);
1865 gtk_tree_model_foreach (model,
1866 (GtkTreeModelForeachFunc)
1867 individual_store_update_list_mode_foreach, self);
1869 g_object_notify (G_OBJECT (self), "is-compact");
1872 EmpathyIndividualStoreSort
1873 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1875 EmpathyIndividualStorePriv *priv;
1877 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1879 priv = GET_PRIV (self);
1881 return priv->sort_criterium;
1885 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1886 EmpathyIndividualStoreSort sort_criterium)
1888 EmpathyIndividualStorePriv *priv;
1890 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1892 priv = GET_PRIV (self);
1894 priv->sort_criterium = sort_criterium;
1896 switch (sort_criterium)
1898 case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1899 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1900 EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1903 case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1904 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1905 EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1909 g_assert_not_reached ();
1912 g_object_notify (G_OBJECT (self), "sort-criterium");
1916 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1920 gboolean is_separator = FALSE;
1922 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1924 gtk_tree_model_get (model, iter,
1925 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1927 return is_separator;
1931 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1933 gboolean *path_is_group,
1934 gboolean *is_fake_group)
1936 GtkTreeIter parent_iter, iter;
1939 gboolean fake = FALSE;
1941 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1945 *path_is_group = FALSE;
1948 if (!gtk_tree_model_get_iter (model, &iter, path))
1953 gtk_tree_model_get (model, &iter,
1954 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1955 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1962 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1969 gtk_tree_model_get (model, &iter,
1970 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1971 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1972 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1982 *path_is_group = TRUE;
1985 if (is_fake_group != NULL)
1986 *is_fake_group = fake;
1992 individual_store_get_individual_status_icon_with_icon_name (
1993 EmpathyIndividualStore *self,
1994 FolksIndividual *individual,
1995 const gchar *status_icon_name)
1997 GdkPixbuf *pixbuf_status;
1998 EmpathyIndividualStorePriv *priv;
1999 const gchar *protocol_name = NULL;
2000 gchar *icon_name = NULL;
2003 guint contact_count = 0;
2004 EmpathyContact *contact = NULL;
2005 gboolean show_protocols_here;
2007 priv = GET_PRIV (self);
2009 personas = folks_individual_get_personas (individual);
2010 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2011 while (gee_iterator_next (iter))
2013 FolksPersona *persona = gee_iterator_get (iter);
2014 if (empathy_folks_persona_is_interesting (persona))
2017 g_clear_object (&persona);
2019 if (contact_count > 1)
2022 g_clear_object (&iter);
2024 show_protocols_here = (priv->show_protocols && (contact_count == 1));
2025 if (show_protocols_here)
2027 contact = empathy_contact_dup_from_folks_individual (individual);
2028 if (contact != NULL)
2030 protocol_name = empathy_protocol_name_for_contact (contact);
2031 icon_name = g_strdup_printf ("%s-%s", status_icon_name,
2036 g_warning ("Cannot retrieve contact from individual '%s'",
2037 folks_alias_details_get_alias (
2038 FOLKS_ALIAS_DETAILS (individual)));
2045 icon_name = g_strdup_printf ("%s", status_icon_name);
2048 pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
2050 if (pixbuf_status == NULL)
2053 empathy_pixbuf_contact_status_icon_with_icon_name (contact,
2054 status_icon_name, show_protocols_here);
2056 if (pixbuf_status != NULL)
2058 /* pass the reference to the hash table */
2059 g_hash_table_insert (priv->status_icons,
2060 g_strdup (icon_name), pixbuf_status);
2065 tp_clear_object (&contact);
2067 return pixbuf_status;
2071 empathy_individual_store_get_individual_status_icon (
2072 EmpathyIndividualStore *self,
2073 FolksIndividual *individual)
2075 GdkPixbuf *pixbuf_status = NULL;
2076 const gchar *status_icon_name = NULL;
2078 status_icon_name = empathy_icon_name_for_individual (individual);
2079 if (status_icon_name == NULL)
2083 individual_store_get_individual_status_icon_with_icon_name (self,
2084 individual, status_icon_name);
2086 return pixbuf_status;