Updated POTFILES.in
[empathy-mirror.git] / libempathy-gtk / empathy-individual-store.c
blobaa3f6334d77f033115ab5ba3a9ae4691367fae13
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
27 #include "config.h"
29 #include <string.h>
31 #include <glib.h>
32 #include <glib/gi18n-lib.h>
33 #include <gtk/gtk.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>
42 #include "empathy-individual-store.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-gtk-enum-types.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
47 #include <libempathy/empathy-debug.h>
49 /* Active users are those which have recently changed state
50 * (e.g. online, offline or from normal to a busy state).
53 /* Time in seconds user is shown as active */
54 #define ACTIVE_USER_SHOW_TIME 7
56 /* Time in seconds after connecting which we wait before active users are enabled */
57 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
59 struct _EmpathyIndividualStorePriv
61 gboolean show_avatars;
62 gboolean show_groups;
63 gboolean is_compact;
64 gboolean show_protocols;
65 EmpathyIndividualStoreSort sort_criterium;
66 guint inhibit_active;
67 gboolean dispose_has_run;
68 GHashTable *status_icons;
69 /* List of owned GCancellables for each pending avatar load operation */
70 GList *avatar_cancellables;
71 /* Hash: FolksIndividual* -> GQueue (GtkTreeIter *) */
72 GHashTable *folks_individual_cache;
73 /* Hash: char *groupname -> GtkTreeIter * */
74 GHashTable *empathy_group_cache;
75 gboolean show_active;
78 typedef struct
80 EmpathyIndividualStore *self;
81 FolksIndividual *individual;
82 gboolean remove;
83 guint timeout;
84 } ShowActiveData;
86 enum
88 PROP_0,
89 PROP_SHOW_AVATARS,
90 PROP_SHOW_PROTOCOLS,
91 PROP_SHOW_GROUPS,
92 PROP_IS_COMPACT,
93 PROP_SORT_CRITERIUM
96 /* prototypes to break cycles */
97 static void individual_store_contact_update (EmpathyIndividualStore *self,
98 FolksIndividual *individual);
100 G_DEFINE_TYPE (EmpathyIndividualStore, empathy_individual_store,
101 GTK_TYPE_TREE_STORE);
103 static const gchar * const *
104 individual_get_client_types (FolksIndividual *individual)
106 GeeSet *personas;
107 GeeIterator *iter;
108 const gchar * const *types = NULL;
109 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
111 personas = folks_individual_get_personas (individual);
112 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
113 while (gee_iterator_next (iter))
115 FolksPresenceDetails *presence;
116 FolksPersona *persona = gee_iterator_get (iter);
118 /* We only want personas which have presence and a TpContact */
119 if (!empathy_folks_persona_is_interesting (persona))
120 goto while_finish;
122 presence = FOLKS_PRESENCE_DETAILS (persona);
124 if (folks_presence_details_typecmp (
125 folks_presence_details_get_presence_type (presence),
126 presence_type) > 0)
128 TpContact *tp_contact;
130 presence_type = folks_presence_details_get_presence_type (presence);
132 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
133 if (tp_contact != NULL)
134 types = tp_contact_get_client_types (tp_contact);
137 while_finish:
138 g_clear_object (&persona);
140 g_clear_object (&iter);
142 return types;
145 static void
146 add_individual_to_store (GtkTreeStore *store,
147 GtkTreeIter *iter,
148 GtkTreeIter *parent,
149 FolksIndividual *individual)
151 EmpathyIndividualStore *self = EMPATHY_INDIVIDUAL_STORE (store);
152 gboolean can_audio_call, can_video_call;
153 const gchar * const *types;
154 GQueue *queue;
156 empathy_individual_can_audio_video_call (individual, &can_audio_call,
157 &can_video_call, NULL);
159 types = individual_get_client_types (individual);
161 gtk_tree_store_insert_with_values (store, iter, parent, 0,
162 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
163 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
164 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, individual,
165 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
166 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
167 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
168 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
169 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
170 -1);
172 queue = g_hash_table_lookup (self->priv->folks_individual_cache, individual);
173 if (queue)
175 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
177 else
179 queue = g_queue_new ();
180 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
181 g_hash_table_insert (self->priv->folks_individual_cache, individual,
182 queue);
186 static void
187 individual_store_get_group (EmpathyIndividualStore *self,
188 const gchar *name,
189 GtkTreeIter *iter_group_to_set,
190 GtkTreeIter *iter_separator_to_set,
191 gboolean *created,
192 gboolean is_fake_group)
194 GtkTreeModel *model;
195 GtkTreeIter iter_group;
196 GtkTreeIter iter_separator;
197 GtkTreeIter *iter;
199 model = GTK_TREE_MODEL (self);
200 iter = g_hash_table_lookup (self->priv->empathy_group_cache, name);
202 if (iter == NULL)
204 if (created)
205 *created = TRUE;
207 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_group,
208 NULL, 0,
209 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, NULL,
210 EMPATHY_INDIVIDUAL_STORE_COL_NAME, name,
211 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, TRUE,
212 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, FALSE,
213 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
214 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, is_fake_group,
215 -1);
217 g_hash_table_insert (self->priv->empathy_group_cache, g_strdup (name),
218 gtk_tree_iter_copy (&iter_group));
220 if (iter_group_to_set)
221 *iter_group_to_set = iter_group;
223 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_separator,
224 &iter_group, 0,
225 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, TRUE,
226 -1);
228 if (iter_separator_to_set)
229 *iter_separator_to_set = iter_separator;
231 else
233 if (created)
234 *created = FALSE;
236 if (iter_group_to_set)
237 *iter_group_to_set = *iter;
239 iter_separator = *iter;
241 if (gtk_tree_model_iter_next (model, &iter_separator))
243 gboolean is_separator;
245 gtk_tree_model_get (model, &iter_separator,
246 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
248 if (is_separator && iter_separator_to_set)
249 *iter_separator_to_set = iter_separator;
254 static GList *
255 individual_store_find_contact (EmpathyIndividualStore *self,
256 FolksIndividual *individual)
258 GQueue *row_refs_queue;
259 GList *i;
260 GList *iters_list = NULL;
262 row_refs_queue = g_hash_table_lookup (self->priv->folks_individual_cache,
263 individual);
264 if (!row_refs_queue)
265 return NULL;
267 for (i = g_queue_peek_head_link (row_refs_queue) ; i != NULL ; i = i->next)
269 GtkTreeIter *iter = i->data;
271 iters_list = g_list_prepend (iters_list, gtk_tree_iter_copy (iter));
274 return iters_list;
277 static void
278 free_iters (GList *iters)
280 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
281 g_list_free (iters);
284 void
285 empathy_individual_store_remove_individual (EmpathyIndividualStore *self,
286 FolksIndividual *individual)
288 GtkTreeModel *model;
289 GQueue *row_refs;
290 GList *l;
292 row_refs = g_hash_table_lookup (self->priv->folks_individual_cache,
293 individual);
294 if (!row_refs)
295 return;
297 /* Clean up model */
298 model = GTK_TREE_MODEL (self);
300 for (l = g_queue_peek_head_link (row_refs); l; l = l->next)
302 GtkTreeIter *iter = l->data;
303 GtkTreeIter parent;
305 /* NOTE: it is only <= 2 here because we have
306 * separators after the group name, otherwise it
307 * should be 1.
309 if (gtk_tree_model_iter_parent (model, &parent, iter) &&
310 gtk_tree_model_iter_n_children (model, &parent) <= 2)
312 gchar *group_name;
313 gtk_tree_model_get (model, &parent,
314 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &group_name,
315 -1);
316 g_hash_table_remove (self->priv->empathy_group_cache,
317 group_name);
318 gtk_tree_store_remove (GTK_TREE_STORE (self), &parent);
320 else
322 gtk_tree_store_remove (GTK_TREE_STORE (self), iter);
326 g_hash_table_remove (self->priv->folks_individual_cache, individual);
329 void
330 empathy_individual_store_add_individual (EmpathyIndividualStore *self,
331 FolksIndividual *individual)
333 GtkTreeIter iter;
334 GeeIterator *group_iter = NULL;
336 if (EMP_STR_EMPTY (folks_alias_details_get_alias (
337 FOLKS_ALIAS_DETAILS (individual))))
338 return;
340 if (self->priv->show_groups)
342 GeeSet *group_set = NULL;
344 group_set = folks_group_details_get_groups (
345 FOLKS_GROUP_DETAILS (individual));
347 if (gee_collection_get_size (GEE_COLLECTION (group_set)) > 0)
348 group_iter = gee_iterable_iterator (GEE_ITERABLE (group_set));
351 /* fall-back groups, in case there are no named groups */
352 if (group_iter == NULL)
354 GtkTreeIter iter_group, *parent;
355 EmpathyContact *contact;
356 TpConnection *connection;
357 gchar *protocol_name = NULL;
359 parent = &iter_group;
361 contact = empathy_contact_dup_from_folks_individual (individual);
362 if (contact != NULL)
364 connection = empathy_contact_get_connection (contact);
365 tp_connection_parse_object_path (connection, &protocol_name, NULL);
368 if (!self->priv->show_groups)
369 parent = NULL;
370 else if (!tp_strdiff (protocol_name, "local-xmpp"))
372 /* these are People Nearby */
373 individual_store_get_group (self,
374 EMPATHY_INDIVIDUAL_STORE_PEOPLE_NEARBY, &iter_group, NULL, NULL,
375 TRUE);
377 else
379 individual_store_get_group (self,
380 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
381 &iter_group, NULL, NULL, TRUE);
384 add_individual_to_store (GTK_TREE_STORE (self), &iter, parent,
385 individual);
387 g_free (protocol_name);
388 g_clear_object (&contact);
391 /* Else add to each group. */
392 while (group_iter != NULL && gee_iterator_next (group_iter))
394 gchar *group_name = gee_iterator_get (group_iter);
395 GtkTreeIter iter_group;
397 individual_store_get_group (self, group_name, &iter_group, NULL, NULL,
398 FALSE);
400 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
401 individual);
403 g_free (group_name);
405 g_clear_object (&group_iter);
407 if (self->priv->show_groups &&
408 folks_favourite_details_get_is_favourite (
409 FOLKS_FAVOURITE_DETAILS (individual)))
411 /* Add contact to the fake 'Favorites' group */
412 GtkTreeIter iter_group;
414 individual_store_get_group (self, EMPATHY_INDIVIDUAL_STORE_FAVORITE,
415 &iter_group, NULL, NULL, TRUE);
417 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
418 individual);
421 individual_store_contact_update (self, individual);
424 static void
425 individual_store_contact_set_active (EmpathyIndividualStore *self,
426 FolksIndividual *individual,
427 gboolean active,
428 gboolean set_changed)
430 GtkTreeModel *model;
431 GList *iters, *l;
433 model = GTK_TREE_MODEL (self);
435 iters = individual_store_find_contact (self, individual);
436 for (l = iters; l; l = l->next)
438 GtkTreePath *path;
440 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
441 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, active,
442 -1);
444 DEBUG ("Set item %s", active ? "active" : "inactive");
446 if (set_changed)
448 path = gtk_tree_model_get_path (model, l->data);
449 gtk_tree_model_row_changed (model, path, l->data);
450 gtk_tree_path_free (path);
454 free_iters (iters);
457 static void individual_store_contact_active_free (ShowActiveData *data);
459 static void
460 individual_store_contact_active_invalidated (ShowActiveData *data,
461 GObject *old_object)
463 /* Remove the timeout and free the struct, since the individual or individual
464 * store has disappeared. */
465 g_source_remove (data->timeout);
467 if (old_object == (GObject *) data->self)
468 data->self = NULL;
469 else if (old_object == (GObject *) data->individual)
470 data->individual = NULL;
471 else
472 g_assert_not_reached ();
474 individual_store_contact_active_free (data);
477 static ShowActiveData *
478 individual_store_contact_active_new (EmpathyIndividualStore *self,
479 FolksIndividual *individual,
480 gboolean remove_)
482 ShowActiveData *data;
484 DEBUG ("Individual'%s' now active, and %s be removed",
485 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
486 remove_ ? "WILL" : "WILL NOT");
488 data = g_slice_new0 (ShowActiveData);
490 /* We don't actually want to force either the IndividualStore or the
491 * Individual to stay alive, since the user could quit Empathy or disable
492 * the account before the contact_active timeout is fired. */
493 g_object_weak_ref (G_OBJECT (self),
494 (GWeakNotify) individual_store_contact_active_invalidated, data);
495 g_object_weak_ref (G_OBJECT (individual),
496 (GWeakNotify) individual_store_contact_active_invalidated, data);
498 data->self = self;
499 data->individual = individual;
500 data->remove = remove_;
501 data->timeout = 0;
503 return data;
506 static void
507 individual_store_contact_active_free (ShowActiveData *data)
509 if (data->self != NULL)
511 g_object_weak_unref (G_OBJECT (data->self),
512 (GWeakNotify) individual_store_contact_active_invalidated, data);
515 if (data->individual != NULL)
517 g_object_weak_unref (G_OBJECT (data->individual),
518 (GWeakNotify) individual_store_contact_active_invalidated, data);
521 g_slice_free (ShowActiveData, data);
524 static gboolean
525 individual_store_contact_active_cb (ShowActiveData *data)
527 if (data->remove)
529 DEBUG ("Individual'%s' active timeout, removing item",
530 folks_alias_details_get_alias (
531 FOLKS_ALIAS_DETAILS (data->individual)));
532 empathy_individual_store_remove_individual (data->self, data->individual);
535 DEBUG ("Individual'%s' no longer active",
536 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (data->individual)));
538 individual_store_contact_set_active (data->self,
539 data->individual, FALSE, TRUE);
541 individual_store_contact_active_free (data);
543 return FALSE;
546 typedef struct {
547 EmpathyIndividualStore *store; /* weak */
548 GCancellable *cancellable; /* owned */
549 } LoadAvatarData;
551 static void
552 individual_avatar_pixbuf_received_cb (FolksIndividual *individual,
553 GAsyncResult *result,
554 LoadAvatarData *data)
556 GError *error = NULL;
557 GdkPixbuf *pixbuf;
559 pixbuf = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
560 result, &error);
562 if (error != NULL)
564 DEBUG ("failed to retrieve pixbuf for individual %s: %s",
565 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
566 error->message);
567 g_clear_error (&error);
569 else if (data->store != NULL)
571 GList *iters, *l;
573 iters = individual_store_find_contact (data->store, individual);
574 for (l = iters; l; l = l->next)
576 gtk_tree_store_set (GTK_TREE_STORE (data->store), l->data,
577 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR, pixbuf,
578 -1);
581 free_iters (iters);
584 /* Free things */
585 if (data->store != NULL)
587 g_object_remove_weak_pointer (G_OBJECT (data->store),
588 (gpointer *) &data->store);
589 data->store->priv->avatar_cancellables = g_list_remove (
590 data->store->priv->avatar_cancellables, data->cancellable);
593 tp_clear_object (&pixbuf);
594 g_object_unref (data->cancellable);
595 g_slice_free (LoadAvatarData, data);
598 static void
599 individual_store_contact_update (EmpathyIndividualStore *self,
600 FolksIndividual *individual)
602 ShowActiveData *data;
603 GtkTreeModel *model;
604 GList *iters, *l;
605 gboolean in_list;
606 gboolean was_online = TRUE;
607 gboolean now_online = FALSE;
608 gboolean set_model = FALSE;
609 gboolean do_remove = FALSE;
610 gboolean do_set_active = FALSE;
611 gboolean do_set_refresh = FALSE;
612 gboolean show_avatar = FALSE;
613 GdkPixbuf *pixbuf_status;
614 LoadAvatarData *load_avatar_data;
616 model = GTK_TREE_MODEL (self);
618 iters = individual_store_find_contact (self, individual);
619 if (!iters)
621 in_list = FALSE;
623 else
625 in_list = TRUE;
628 /* Get online state now. */
629 now_online = folks_presence_details_is_online (
630 FOLKS_PRESENCE_DETAILS (individual));
632 if (!in_list)
634 DEBUG ("Individual'%s' in list:NO, should be:YES",
635 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
637 empathy_individual_store_add_individual (self, individual);
639 if (self->priv->show_active)
641 do_set_active = TRUE;
643 DEBUG ("Set active (individual added)");
646 else
648 DEBUG ("Individual'%s' in list:YES, should be:YES",
649 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
651 /* Get online state before. */
652 if (iters && g_list_length (iters) > 0)
654 gtk_tree_model_get (model, iters->data,
655 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &was_online, -1);
658 /* Is this really an update or an online/offline. */
659 if (self->priv->show_active)
661 if (was_online != now_online)
663 do_set_active = TRUE;
664 do_set_refresh = TRUE;
666 DEBUG ("Set active (individual updated %s)",
667 was_online ? "online -> offline" : "offline -> online");
669 else
671 /* Was TRUE for presence updates. */
672 /* do_set_active = FALSE; */
673 do_set_refresh = TRUE;
675 DEBUG ("Set active (individual updated)");
679 set_model = TRUE;
682 if (self->priv->show_avatars && !self->priv->is_compact)
684 show_avatar = TRUE;
687 /* Load the avatar asynchronously */
688 load_avatar_data = g_slice_new (LoadAvatarData);
689 load_avatar_data->store = self;
690 g_object_add_weak_pointer (G_OBJECT (self),
691 (gpointer *) &load_avatar_data->store);
692 load_avatar_data->cancellable = g_cancellable_new ();
694 self->priv->avatar_cancellables = g_list_prepend (
695 self->priv->avatar_cancellables, load_avatar_data->cancellable);
697 empathy_pixbuf_avatar_from_individual_scaled_async (individual, 32, 32,
698 load_avatar_data->cancellable,
699 (GAsyncReadyCallback) individual_avatar_pixbuf_received_cb,
700 load_avatar_data);
702 pixbuf_status =
703 empathy_individual_store_get_individual_status_icon (self, individual);
705 for (l = iters; l && set_model; l = l->next)
707 gboolean can_audio_call, can_video_call;
708 const gchar * const *types;
710 empathy_individual_can_audio_video_call (individual, &can_audio_call,
711 &can_video_call, NULL);
713 types = individual_get_client_types (individual);
715 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
716 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
717 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
718 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
719 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
720 EMPATHY_INDIVIDUAL_STORE_COL_PRESENCE_TYPE,
721 folks_presence_details_get_presence_type (
722 FOLKS_PRESENCE_DETAILS (individual)),
723 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
724 folks_presence_details_get_presence_message (
725 FOLKS_PRESENCE_DETAILS (individual)),
726 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, self->priv->is_compact,
727 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
728 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, now_online,
729 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
730 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
731 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
732 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
733 -1);
736 if (self->priv->show_active && do_set_active)
738 individual_store_contact_set_active (self, individual, do_set_active,
739 do_set_refresh);
741 if (do_set_active)
743 data =
744 individual_store_contact_active_new (self, individual,
745 do_remove);
746 data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
747 (GSourceFunc) individual_store_contact_active_cb, data);
751 /* FIXME: when someone goes online then offline quickly, the
752 * first timeout sets the user to be inactive and the second
753 * timeout removes the user from the contact list, really we
754 * should remove the first timeout.
756 free_iters (iters);
759 static void
760 individual_store_individual_updated_cb (FolksIndividual *individual,
761 GParamSpec *param,
762 EmpathyIndividualStore *self)
764 DEBUG ("Individual'%s' updated, checking roster is in sync...",
765 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
767 individual_store_contact_update (self, individual);
770 static void
771 individual_store_contact_updated_cb (EmpathyContact *contact,
772 GParamSpec *pspec,
773 EmpathyIndividualStore *self)
775 FolksIndividual *individual;
777 DEBUG ("Contact '%s' updated, checking roster is in sync...",
778 empathy_contact_get_alias (contact));
780 individual = g_object_get_data (G_OBJECT (contact), "individual");
781 if (individual == NULL)
782 return;
784 individual_store_contact_update (self, individual);
787 static void
788 individual_personas_changed_cb (FolksIndividual *individual,
789 GeeSet *added,
790 GeeSet *removed,
791 EmpathyIndividualStore *self)
793 GeeIterator *iter;
795 DEBUG ("Individual '%s' personas-changed.",
796 folks_individual_get_id (individual));
798 iter = gee_iterable_iterator (GEE_ITERABLE (removed));
799 /* FIXME: libfolks hasn't grown capabilities support yet, so we have to go
800 * through the EmpathyContacts for them. */
801 while (gee_iterator_next (iter))
803 TpfPersona *persona = gee_iterator_get (iter);
804 TpContact *tp_contact;
805 EmpathyContact *contact;
807 if (TPF_IS_PERSONA (persona))
809 tp_contact = tpf_persona_get_contact (persona);
810 if (tp_contact != NULL)
812 contact = empathy_contact_dup_from_tp_contact (tp_contact);
813 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
815 g_object_set_data (G_OBJECT (contact), "individual", NULL);
816 g_signal_handlers_disconnect_by_func (contact,
817 (GCallback) individual_store_contact_updated_cb, self);
819 g_object_unref (contact);
823 g_clear_object (&persona);
825 g_clear_object (&iter);
827 iter = gee_iterable_iterator (GEE_ITERABLE (added));
828 while (gee_iterator_next (iter))
830 TpfPersona *persona = gee_iterator_get (iter);
831 TpContact *tp_contact;
832 EmpathyContact *contact;
834 if (TPF_IS_PERSONA (persona))
836 tp_contact = tpf_persona_get_contact (persona);
837 if (tp_contact != NULL)
839 contact = empathy_contact_dup_from_tp_contact (tp_contact);
840 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
842 g_object_set_data (G_OBJECT (contact), "individual", individual);
843 g_signal_connect (contact, "notify::capabilities",
844 (GCallback) individual_store_contact_updated_cb, self);
845 g_signal_connect (contact, "notify::client-types",
846 (GCallback) individual_store_contact_updated_cb, self);
848 g_object_unref (contact);
852 g_clear_object (&persona);
854 g_clear_object (&iter);
857 static void
858 individual_store_favourites_changed_cb (FolksIndividual *individual,
859 GParamSpec *param,
860 EmpathyIndividualStore *self)
862 DEBUG ("Individual %s is %s a favourite",
863 folks_individual_get_id (individual),
864 folks_favourite_details_get_is_favourite (
865 FOLKS_FAVOURITE_DETAILS (individual)) ? "now" : "no longer");
867 empathy_individual_store_remove_individual (self, individual);
868 empathy_individual_store_add_individual (self, individual);
871 void
872 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
873 FolksIndividual *individual)
875 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
877 empathy_individual_store_add_individual (self, individual);
879 g_signal_connect (individual, "notify::avatar",
880 (GCallback) individual_store_individual_updated_cb, self);
881 g_signal_connect (individual, "notify::presence-type",
882 (GCallback) individual_store_individual_updated_cb, self);
883 g_signal_connect (individual, "notify::presence-message",
884 (GCallback) individual_store_individual_updated_cb, self);
885 g_signal_connect (individual, "notify::alias",
886 (GCallback) individual_store_individual_updated_cb, self);
887 g_signal_connect (individual, "personas-changed",
888 (GCallback) individual_personas_changed_cb, self);
889 g_signal_connect (individual, "notify::is-favourite",
890 (GCallback) individual_store_favourites_changed_cb, self);
892 /* provide an empty set so the callback can assume non-NULL sets */
893 individual_personas_changed_cb (individual,
894 folks_individual_get_personas (individual), empty_set, self);
895 g_clear_object (&empty_set);
898 void
899 empathy_individual_store_disconnect_individual (EmpathyIndividualStore *self,
900 FolksIndividual *individual)
902 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
904 /* provide an empty set so the callback can assume non-NULL sets */
905 individual_personas_changed_cb (individual, empty_set,
906 folks_individual_get_personas (individual), self);
907 g_clear_object (&empty_set);
909 g_signal_handlers_disconnect_by_func (individual,
910 (GCallback) individual_store_individual_updated_cb, self);
911 g_signal_handlers_disconnect_by_func (individual,
912 (GCallback) individual_personas_changed_cb, self);
913 g_signal_handlers_disconnect_by_func (individual,
914 (GCallback) individual_store_favourites_changed_cb, self);
917 void
918 individual_store_remove_individual_and_disconnect (
919 EmpathyIndividualStore *self,
920 FolksIndividual *individual)
922 empathy_individual_store_disconnect_individual (self, individual);
923 empathy_individual_store_remove_individual (self, individual);
926 static void
927 individual_store_dispose (GObject *object)
929 EmpathyIndividualStore *self = EMPATHY_INDIVIDUAL_STORE (object);
930 GList *l;
932 if (self->priv->dispose_has_run)
933 return;
934 self->priv->dispose_has_run = TRUE;
936 /* Cancel any pending avatar load operations */
937 for (l = self->priv->avatar_cancellables; l != NULL; l = l->next)
939 /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
940 g_cancellable_cancel (G_CANCELLABLE (l->data));
942 g_list_free (self->priv->avatar_cancellables);
944 if (self->priv->inhibit_active)
946 g_source_remove (self->priv->inhibit_active);
949 g_hash_table_unref (self->priv->status_icons);
950 g_hash_table_unref (self->priv->folks_individual_cache);
951 g_hash_table_unref (self->priv->empathy_group_cache);
952 G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
955 static void
956 individual_store_get_property (GObject *object,
957 guint param_id,
958 GValue *value,
959 GParamSpec *pspec)
961 EmpathyIndividualStore *self = EMPATHY_INDIVIDUAL_STORE (object);
963 switch (param_id)
965 case PROP_SHOW_AVATARS:
966 g_value_set_boolean (value, self->priv->show_avatars);
967 break;
968 case PROP_SHOW_PROTOCOLS:
969 g_value_set_boolean (value, self->priv->show_protocols);
970 break;
971 case PROP_SHOW_GROUPS:
972 g_value_set_boolean (value, self->priv->show_groups);
973 break;
974 case PROP_IS_COMPACT:
975 g_value_set_boolean (value, self->priv->is_compact);
976 break;
977 case PROP_SORT_CRITERIUM:
978 g_value_set_enum (value, self->priv->sort_criterium);
979 break;
980 default:
981 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
982 break;
986 static void
987 individual_store_set_property (GObject *object,
988 guint param_id,
989 const GValue *value,
990 GParamSpec *pspec)
992 switch (param_id)
994 case PROP_SHOW_AVATARS:
995 empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
996 (object), g_value_get_boolean (value));
997 break;
998 case PROP_SHOW_PROTOCOLS:
999 empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1000 (object), g_value_get_boolean (value));
1001 break;
1002 case PROP_SHOW_GROUPS:
1003 empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1004 (object), g_value_get_boolean (value));
1005 break;
1006 case PROP_IS_COMPACT:
1007 empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1008 (object), g_value_get_boolean (value));
1009 break;
1010 case PROP_SORT_CRITERIUM:
1011 empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1012 (object), g_value_get_enum (value));
1013 break;
1014 default:
1015 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1016 break;
1020 static void
1021 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1023 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1025 object_class->dispose = individual_store_dispose;
1026 object_class->get_property = individual_store_get_property;
1027 object_class->set_property = individual_store_set_property;
1029 g_object_class_install_property (object_class,
1030 PROP_SHOW_AVATARS,
1031 g_param_spec_boolean ("show-avatars",
1032 "Show Avatars",
1033 "Whether contact list should display "
1034 "avatars for contacts", TRUE, G_PARAM_READWRITE));
1035 g_object_class_install_property (object_class,
1036 PROP_SHOW_PROTOCOLS,
1037 g_param_spec_boolean ("show-protocols",
1038 "Show Protocols",
1039 "Whether contact list should display "
1040 "protocols for contacts", FALSE, G_PARAM_READWRITE));
1041 g_object_class_install_property (object_class,
1042 PROP_SHOW_GROUPS,
1043 g_param_spec_boolean ("show-groups",
1044 "Show Groups",
1045 "Whether contact list should display "
1046 "contact groups", TRUE, G_PARAM_READWRITE));
1047 g_object_class_install_property (object_class,
1048 PROP_IS_COMPACT,
1049 g_param_spec_boolean ("is-compact",
1050 "Is Compact",
1051 "Whether the contact list is in compact mode or not",
1052 FALSE, G_PARAM_READWRITE));
1054 g_object_class_install_property (object_class,
1055 PROP_SORT_CRITERIUM,
1056 g_param_spec_enum ("sort-criterium",
1057 "Sort citerium",
1058 "The sort criterium to use for sorting the contact list",
1059 EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1060 EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1062 g_type_class_add_private (object_class,
1063 sizeof (EmpathyIndividualStorePriv));
1066 static gint
1067 get_position (const char **strv,
1068 const char *str)
1070 int i;
1072 for (i = 0; strv[i] != NULL; i++)
1074 if (!tp_strdiff (strv[i], str))
1075 return i;
1078 return -1;
1081 static gint
1082 compare_separator_and_groups (gboolean is_separator_a,
1083 gboolean is_separator_b,
1084 const gchar *name_a,
1085 const gchar *name_b,
1086 FolksIndividual *individual_a,
1087 FolksIndividual *individual_b,
1088 gboolean fake_group_a,
1089 gboolean fake_group_b)
1091 /* these two lists are the sorted list of fake groups to include at the
1092 * top and bottom of the roster */
1093 const char *top_groups[] = {
1094 EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1095 NULL
1098 const char *bottom_groups[] = {
1099 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1100 NULL
1103 if (is_separator_a || is_separator_b)
1105 /* We have at least one separator */
1106 if (is_separator_a)
1108 return -1;
1110 else if (is_separator_b)
1112 return 1;
1116 /* One group and one contact */
1117 if (!individual_a && individual_b)
1119 return 1;
1121 else if (individual_a && !individual_b)
1123 return -1;
1125 else if (!individual_a && !individual_b)
1127 gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1129 a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1130 b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1131 a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1132 b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1134 if (a_in_top && b_in_top)
1136 /* compare positions */
1137 return CLAMP (get_position (top_groups, name_a) -
1138 get_position (top_groups, name_b), -1, 1);
1140 else if (a_in_bottom && b_in_bottom)
1142 /* compare positions */
1143 return CLAMP (get_position (bottom_groups, name_a) -
1144 get_position (bottom_groups, name_b), -1, 1);
1146 else if (a_in_top || b_in_bottom)
1148 return -1;
1150 else if (b_in_top || a_in_bottom)
1152 return 1;
1154 else
1156 return g_utf8_collate (name_a, name_b);
1160 /* Two contacts, ordering depends of the sorting policy */
1161 return 0;
1164 static gint
1165 individual_store_contact_sort (FolksIndividual *individual_a,
1166 FolksIndividual *individual_b)
1168 gint ret_val;
1169 EmpathyContact *contact_a = NULL, *contact_b = NULL;
1170 TpAccount *account_a, *account_b;
1172 g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1174 /* alias */
1175 ret_val = g_utf8_collate (
1176 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_a)),
1177 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_b)));
1179 if (ret_val != 0)
1180 goto out;
1182 contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1183 contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1184 if (contact_a != NULL && contact_b != NULL)
1186 account_a = empathy_contact_get_account (contact_a);
1187 account_b = empathy_contact_get_account (contact_b);
1189 g_assert (account_a != NULL);
1190 g_assert (account_b != NULL);
1192 /* protocol */
1193 ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1194 tp_account_get_protocol (account_b));
1196 if (ret_val != 0)
1197 goto out;
1199 /* account ID */
1200 ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1201 tp_proxy_get_object_path (account_b));
1203 if (ret_val != 0)
1204 goto out;
1207 /* identifier */
1208 ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1209 folks_individual_get_id (individual_b));
1211 out:
1212 tp_clear_object (&contact_a);
1213 tp_clear_object (&contact_b);
1215 return ret_val;
1218 static gint
1219 individual_store_state_sort_func (GtkTreeModel *model,
1220 GtkTreeIter *iter_a,
1221 GtkTreeIter *iter_b,
1222 gpointer user_data)
1224 gint ret_val;
1225 FolksIndividual *individual_a, *individual_b;
1226 gchar *name_a, *name_b;
1227 gboolean is_separator_a, is_separator_b;
1228 gboolean fake_group_a, fake_group_b;
1229 FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1230 TpConnectionPresenceType tp_presence_a, tp_presence_b;
1232 gtk_tree_model_get (model, iter_a,
1233 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1234 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1235 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1236 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1237 gtk_tree_model_get (model, iter_b,
1238 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1239 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1240 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1241 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1243 if (individual_a == NULL || individual_b == NULL)
1245 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1246 name_a, name_b, individual_a, individual_b, fake_group_a,
1247 fake_group_b);
1248 goto free_and_out;
1251 /* If we managed to get this far, we can start looking at
1252 * the presences.
1254 folks_presence_type_a =
1255 folks_presence_details_get_presence_type (
1256 FOLKS_PRESENCE_DETAILS (individual_a));
1257 folks_presence_type_b =
1258 folks_presence_details_get_presence_type (
1259 FOLKS_PRESENCE_DETAILS (individual_b));
1260 tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1261 tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1263 ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1264 tp_presence_b);
1266 if (ret_val == 0)
1268 /* Fallback: compare by name et al. */
1269 ret_val = individual_store_contact_sort (individual_a, individual_b);
1272 free_and_out:
1273 g_free (name_a);
1274 g_free (name_b);
1275 tp_clear_object (&individual_a);
1276 tp_clear_object (&individual_b);
1278 return ret_val;
1281 static gint
1282 individual_store_name_sort_func (GtkTreeModel *model,
1283 GtkTreeIter *iter_a,
1284 GtkTreeIter *iter_b,
1285 gpointer user_data)
1287 gchar *name_a, *name_b;
1288 FolksIndividual *individual_a, *individual_b;
1289 gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1290 gint ret_val;
1291 gboolean fake_group_a, fake_group_b;
1293 gtk_tree_model_get (model, iter_a,
1294 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1295 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1296 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1297 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1298 gtk_tree_model_get (model, iter_b,
1299 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1300 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1301 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1302 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1304 if (individual_a == NULL || individual_b == NULL)
1305 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1306 name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1307 else
1308 ret_val = individual_store_contact_sort (individual_a, individual_b);
1310 tp_clear_object (&individual_a);
1311 tp_clear_object (&individual_b);
1312 g_free (name_a);
1313 g_free (name_b);
1315 return ret_val;
1318 static void
1319 individual_store_setup (EmpathyIndividualStore *self)
1321 GType types[] = {
1322 GDK_TYPE_PIXBUF, /* Status pixbuf */
1323 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1324 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1325 G_TYPE_STRING, /* Name */
1326 G_TYPE_UINT, /* Presence type */
1327 G_TYPE_STRING, /* Status string */
1328 G_TYPE_BOOLEAN, /* Compact view */
1329 FOLKS_TYPE_INDIVIDUAL, /* Individual type */
1330 G_TYPE_BOOLEAN, /* Is group */
1331 G_TYPE_BOOLEAN, /* Is active */
1332 G_TYPE_BOOLEAN, /* Is online */
1333 G_TYPE_BOOLEAN, /* Is separator */
1334 G_TYPE_BOOLEAN, /* Can make audio calls */
1335 G_TYPE_BOOLEAN, /* Can make video calls */
1336 G_TYPE_BOOLEAN, /* Is a fake group */
1337 G_TYPE_STRV, /* Client types */
1338 G_TYPE_UINT, /* Event count */
1341 gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1342 EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1344 /* Set up sorting */
1345 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1346 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1347 individual_store_name_sort_func, self, NULL);
1348 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1349 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1350 individual_store_state_sort_func, self, NULL);
1352 self->priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1354 empathy_individual_store_set_sort_criterium (self,
1355 self->priv->sort_criterium);
1358 static gboolean
1359 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1361 self->priv->show_active = TRUE;
1362 self->priv->inhibit_active = 0;
1364 return FALSE;
1367 static void
1368 g_queue_free_full_iter (gpointer data)
1370 GQueue *queue = (GQueue *) data;
1371 g_queue_foreach (queue, (GFunc) gtk_tree_iter_free, NULL);
1372 g_queue_free (queue);
1375 static void
1376 empathy_individual_store_init (EmpathyIndividualStore *self)
1378 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1379 EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1381 self->priv->show_avatars = TRUE;
1382 self->priv->show_groups = TRUE;
1383 self->priv->show_protocols = FALSE;
1384 self->priv->inhibit_active =
1385 g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1386 (GSourceFunc) individual_store_inhibit_active_cb, self);
1387 self->priv->status_icons =
1388 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1389 self->priv->folks_individual_cache = g_hash_table_new_full (NULL, NULL, NULL,
1390 g_queue_free_full_iter);
1391 self->priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
1392 g_str_equal, g_free, (GDestroyNotify) gtk_tree_iter_free);
1393 individual_store_setup (self);
1396 gboolean
1397 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1399 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1401 return self->priv->show_avatars;
1404 static gboolean
1405 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1406 GtkTreePath *path,
1407 GtkTreeIter *iter,
1408 EmpathyIndividualStore *self)
1410 gboolean show_avatar = FALSE;
1411 FolksIndividual *individual;
1412 GdkPixbuf *pixbuf_status;
1414 if (self->priv->show_avatars && !self->priv->is_compact)
1416 show_avatar = TRUE;
1419 gtk_tree_model_get (model, iter,
1420 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1422 if (individual == NULL)
1424 return FALSE;
1426 /* get icon from hash_table */
1427 pixbuf_status =
1428 empathy_individual_store_get_individual_status_icon (self, individual);
1430 gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1431 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1432 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1433 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, self->priv->is_compact, -1);
1435 g_object_unref (individual);
1437 return FALSE;
1440 void
1441 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1442 gboolean show_avatars)
1444 GtkTreeModel *model;
1446 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1448 self->priv->show_avatars = show_avatars;
1450 model = GTK_TREE_MODEL (self);
1452 gtk_tree_model_foreach (model,
1453 (GtkTreeModelForeachFunc)
1454 individual_store_update_list_mode_foreach, self);
1456 g_object_notify (G_OBJECT (self), "show-avatars");
1459 gboolean
1460 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1462 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1464 return self->priv->show_protocols;
1467 void
1468 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1469 gboolean show_protocols)
1471 GtkTreeModel *model;
1473 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1475 self->priv->show_protocols = show_protocols;
1477 model = GTK_TREE_MODEL (self);
1479 gtk_tree_model_foreach (model,
1480 (GtkTreeModelForeachFunc)
1481 individual_store_update_list_mode_foreach, self);
1483 g_object_notify (G_OBJECT (self), "show-protocols");
1486 gboolean
1487 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1489 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1491 return self->priv->show_groups;
1494 void
1495 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1496 gboolean show_groups)
1498 EmpathyIndividualStoreClass *klass;
1500 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1502 klass = EMPATHY_INDIVIDUAL_STORE_GET_CLASS ( self);
1504 if (self->priv->show_groups == show_groups)
1506 return;
1509 self->priv->show_groups = show_groups;
1511 if (!klass->initial_loading (self))
1513 /* Remove all contacts and add them back, not optimized but
1514 * that's the easy way :)
1516 * This is only done if there's not a pending setup idle
1517 * callback, otherwise it will race and the contacts will get
1518 * added twice */
1520 gtk_tree_store_clear (GTK_TREE_STORE (self));
1521 /* Also clear the cache */
1522 g_hash_table_remove_all (self->priv->folks_individual_cache);
1523 g_hash_table_remove_all (self->priv->empathy_group_cache);
1525 klass->reload_individuals (self);
1528 g_object_notify (G_OBJECT (self), "show-groups");
1531 gboolean
1532 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1534 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1536 return self->priv->is_compact;
1539 void
1540 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1541 gboolean is_compact)
1543 GtkTreeModel *model;
1545 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1547 self->priv->is_compact = is_compact;
1549 model = GTK_TREE_MODEL (self);
1551 gtk_tree_model_foreach (model,
1552 (GtkTreeModelForeachFunc)
1553 individual_store_update_list_mode_foreach, self);
1555 g_object_notify (G_OBJECT (self), "is-compact");
1558 EmpathyIndividualStoreSort
1559 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1561 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1563 return self->priv->sort_criterium;
1566 void
1567 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1568 EmpathyIndividualStoreSort sort_criterium)
1570 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1572 self->priv->sort_criterium = sort_criterium;
1574 switch (sort_criterium)
1576 case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1577 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1578 EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1579 break;
1581 case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1582 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1583 EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1584 break;
1586 default:
1587 g_assert_not_reached ();
1590 g_object_notify (G_OBJECT (self), "sort-criterium");
1593 gboolean
1594 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1595 GtkTreeIter *iter,
1596 gpointer data)
1598 gboolean is_separator = FALSE;
1600 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1602 gtk_tree_model_get (model, iter,
1603 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1605 return is_separator;
1608 gchar *
1609 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1610 GtkTreePath *path,
1611 gboolean *path_is_group,
1612 gboolean *is_fake_group)
1614 GtkTreeIter parent_iter, iter;
1615 gchar *name = NULL;
1616 gboolean is_group;
1617 gboolean fake = FALSE;
1619 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1621 if (path_is_group)
1623 *path_is_group = FALSE;
1626 if (!gtk_tree_model_get_iter (model, &iter, path))
1628 return NULL;
1631 gtk_tree_model_get (model, &iter,
1632 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1633 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1635 if (!is_group)
1637 g_free (name);
1638 name = NULL;
1640 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1642 return NULL;
1645 iter = parent_iter;
1647 gtk_tree_model_get (model, &iter,
1648 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1649 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1650 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1651 if (!is_group)
1653 g_free (name);
1654 return NULL;
1658 if (path_is_group)
1660 *path_is_group = TRUE;
1663 if (is_fake_group != NULL)
1664 *is_fake_group = fake;
1666 return name;
1669 static GdkPixbuf *
1670 individual_store_get_individual_status_icon_with_icon_name (
1671 EmpathyIndividualStore *self,
1672 FolksIndividual *individual,
1673 const gchar *status_icon_name)
1675 GdkPixbuf *pixbuf_status;
1676 const gchar *protocol_name = NULL;
1677 gchar *icon_name = NULL;
1678 GeeSet *personas;
1679 GeeIterator *iter;
1680 guint contact_count = 0;
1681 EmpathyContact *contact = NULL;
1682 gboolean show_protocols_here;
1684 personas = folks_individual_get_personas (individual);
1685 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1686 while (gee_iterator_next (iter))
1688 FolksPersona *persona = gee_iterator_get (iter);
1689 if (empathy_folks_persona_is_interesting (persona))
1690 contact_count++;
1692 g_clear_object (&persona);
1694 if (contact_count > 1)
1695 break;
1697 g_clear_object (&iter);
1699 show_protocols_here = (self->priv->show_protocols && (contact_count == 1));
1700 if (show_protocols_here)
1702 contact = empathy_contact_dup_from_folks_individual (individual);
1703 if (contact != NULL)
1705 protocol_name = empathy_protocol_name_for_contact (contact);
1706 icon_name = g_strdup_printf ("%s-%s", status_icon_name,
1707 protocol_name);
1709 else
1711 g_warning ("Cannot retrieve contact from individual '%s'",
1712 folks_alias_details_get_alias (
1713 FOLKS_ALIAS_DETAILS (individual)));
1715 return NULL;
1718 else
1720 icon_name = g_strdup_printf ("%s", status_icon_name);
1723 pixbuf_status = g_hash_table_lookup (self->priv->status_icons, icon_name);
1725 if (pixbuf_status == NULL)
1727 pixbuf_status =
1728 empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1729 status_icon_name, show_protocols_here);
1731 if (pixbuf_status != NULL)
1733 /* pass the reference to the hash table */
1734 g_hash_table_insert (self->priv->status_icons,
1735 g_strdup (icon_name), pixbuf_status);
1739 g_free (icon_name);
1740 tp_clear_object (&contact);
1742 return pixbuf_status;
1745 GdkPixbuf *
1746 empathy_individual_store_get_individual_status_icon (
1747 EmpathyIndividualStore *self,
1748 FolksIndividual *individual)
1750 GdkPixbuf *pixbuf_status = NULL;
1751 const gchar *status_icon_name = NULL;
1753 status_icon_name = empathy_icon_name_for_individual (individual);
1754 if (status_icon_name == NULL)
1755 return NULL;
1757 pixbuf_status =
1758 individual_store_get_individual_status_icon_with_icon_name (self,
1759 individual, status_icon_name);
1761 return pixbuf_status;
1764 void
1765 empathy_individual_store_refresh_individual (EmpathyIndividualStore *self,
1766 FolksIndividual *individual)
1768 gboolean show_active;
1770 show_active = self->priv->show_active;
1771 self->priv->show_active = FALSE;
1772 empathy_individual_store_remove_individual (self, individual);
1773 empathy_individual_store_add_individual (self, individual);
1774 self->priv->show_active = show_active;