mock-pkcs11: use g_hash_table_unref()
[nijm-empathy.git] / libempathy-gtk / empathy-individual-store.c
blob9b685ee5f7dbba3547d31e579d879682c947c924
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"
28 #include "empathy-individual-store.h"
30 #include <glib/gi18n-lib.h>
31 #include <tp-account-widgets/tpaw-utils.h>
33 #include "empathy-gtk-enum-types.h"
34 #include "empathy-ui-utils.h"
35 #include "empathy-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include "empathy-debug.h"
40 /* Active users are those which have recently changed state
41 * (e.g. online, offline or from normal to a busy state).
44 /* Time in seconds user is shown as active */
45 #define ACTIVE_USER_SHOW_TIME 7
47 /* Time in seconds after connecting which we wait before active users are enabled */
48 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
50 struct _EmpathyIndividualStorePriv
52 gboolean show_avatars;
53 gboolean show_groups;
54 gboolean is_compact;
55 gboolean show_protocols;
56 EmpathyIndividualStoreSort sort_criterium;
57 guint inhibit_active;
58 gboolean dispose_has_run;
59 GHashTable *status_icons;
60 /* List of owned GCancellables for each pending avatar load operation */
61 GList *avatar_cancellables;
62 /* Hash: FolksIndividual* -> GQueue (GtkTreeIter *) */
63 GHashTable *folks_individual_cache;
64 /* Hash: char *groupname -> GtkTreeIter * */
65 GHashTable *empathy_group_cache;
66 gboolean show_active;
69 typedef struct
71 EmpathyIndividualStore *self;
72 FolksIndividual *individual;
73 gboolean remove;
74 guint timeout;
75 } ShowActiveData;
77 enum
79 PROP_0,
80 PROP_SHOW_AVATARS,
81 PROP_SHOW_PROTOCOLS,
82 PROP_SHOW_GROUPS,
83 PROP_IS_COMPACT,
84 PROP_SORT_CRITERIUM
87 /* prototypes to break cycles */
88 static void individual_store_contact_update (EmpathyIndividualStore *self,
89 FolksIndividual *individual);
91 G_DEFINE_TYPE (EmpathyIndividualStore, empathy_individual_store,
92 GTK_TYPE_TREE_STORE);
94 static void
95 add_individual_to_store (GtkTreeStore *store,
96 GtkTreeIter *iter,
97 GtkTreeIter *parent,
98 FolksIndividual *individual)
100 EmpathyIndividualStore *self = EMPATHY_INDIVIDUAL_STORE (store);
101 gboolean can_audio_call, can_video_call;
102 const gchar * const *types;
103 GQueue *queue;
105 empathy_individual_can_audio_video_call (individual, &can_audio_call,
106 &can_video_call, NULL);
108 types = empathy_individual_get_client_types (individual);
110 gtk_tree_store_insert_with_values (store, iter, parent, 0,
111 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
112 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
113 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, individual,
114 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
115 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
116 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
117 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
118 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
119 -1);
121 queue = g_hash_table_lookup (self->priv->folks_individual_cache, individual);
122 if (queue)
124 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
126 else
128 queue = g_queue_new ();
129 g_queue_push_tail (queue, gtk_tree_iter_copy (iter));
130 g_hash_table_insert (self->priv->folks_individual_cache, individual,
131 queue);
135 static void
136 individual_store_get_group (EmpathyIndividualStore *self,
137 const gchar *name,
138 GtkTreeIter *iter_group_to_set,
139 GtkTreeIter *iter_separator_to_set,
140 gboolean *created,
141 gboolean is_fake_group)
143 GtkTreeModel *model;
144 GtkTreeIter iter_group;
145 GtkTreeIter iter_separator;
146 GtkTreeIter *iter;
148 model = GTK_TREE_MODEL (self);
149 iter = g_hash_table_lookup (self->priv->empathy_group_cache, name);
151 if (iter == NULL)
153 if (created)
154 *created = TRUE;
156 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_group,
157 NULL, 0,
158 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, NULL,
159 EMPATHY_INDIVIDUAL_STORE_COL_NAME, name,
160 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, TRUE,
161 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, FALSE,
162 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
163 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, is_fake_group,
164 -1);
166 g_hash_table_insert (self->priv->empathy_group_cache, g_strdup (name),
167 gtk_tree_iter_copy (&iter_group));
169 if (iter_group_to_set)
170 *iter_group_to_set = iter_group;
172 gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_separator,
173 &iter_group, 0,
174 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, TRUE,
175 -1);
177 if (iter_separator_to_set)
178 *iter_separator_to_set = iter_separator;
180 else
182 if (created)
183 *created = FALSE;
185 if (iter_group_to_set)
186 *iter_group_to_set = *iter;
188 iter_separator = *iter;
190 if (gtk_tree_model_iter_next (model, &iter_separator))
192 gboolean is_separator;
194 gtk_tree_model_get (model, &iter_separator,
195 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
197 if (is_separator && iter_separator_to_set)
198 *iter_separator_to_set = iter_separator;
203 GList *
204 /* (transfer full) free with empathy_individual_store_free_iters() */
205 empathy_individual_store_find_contact (EmpathyIndividualStore *self,
206 FolksIndividual *individual)
208 GQueue *row_refs_queue;
209 GList *i;
210 GList *iters_list = NULL;
212 row_refs_queue = g_hash_table_lookup (self->priv->folks_individual_cache,
213 individual);
214 if (!row_refs_queue)
215 return NULL;
217 for (i = g_queue_peek_head_link (row_refs_queue) ; i != NULL ; i = i->next)
219 GtkTreeIter *iter = i->data;
221 iters_list = g_list_prepend (iters_list, gtk_tree_iter_copy (iter));
224 return iters_list;
227 void
228 empathy_individual_store_free_iters (GList *iters)
230 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
231 g_list_free (iters);
234 void
235 empathy_individual_store_remove_individual (EmpathyIndividualStore *self,
236 FolksIndividual *individual)
238 GtkTreeModel *model;
239 GQueue *row_refs;
240 GList *l;
242 row_refs = g_hash_table_lookup (self->priv->folks_individual_cache,
243 individual);
244 if (!row_refs)
245 return;
247 /* Clean up model */
248 model = GTK_TREE_MODEL (self);
250 for (l = g_queue_peek_head_link (row_refs); l; l = l->next)
252 GtkTreeIter *iter = l->data;
253 GtkTreeIter parent;
255 /* NOTE: it is only <= 2 here because we have
256 * separators after the group name, otherwise it
257 * should be 1.
259 if (gtk_tree_model_iter_parent (model, &parent, iter) &&
260 gtk_tree_model_iter_n_children (model, &parent) <= 2)
262 gchar *group_name;
263 gtk_tree_model_get (model, &parent,
264 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &group_name,
265 -1);
266 g_hash_table_remove (self->priv->empathy_group_cache,
267 group_name);
268 gtk_tree_store_remove (GTK_TREE_STORE (self), &parent);
270 else
272 gtk_tree_store_remove (GTK_TREE_STORE (self), iter);
276 g_hash_table_remove (self->priv->folks_individual_cache, individual);
279 void
280 empathy_individual_store_add_individual (EmpathyIndividualStore *self,
281 FolksIndividual *individual)
283 GtkTreeIter iter, iter_group;
284 GeeSet *group_set = NULL;
285 gboolean grouped = FALSE;
287 if (TPAW_STR_EMPTY (folks_alias_details_get_alias (
288 FOLKS_ALIAS_DETAILS (individual))))
289 return;
291 if (!self->priv->show_groups)
293 /* add our individual to the toplevel of the store */
294 add_individual_to_store (GTK_TREE_STORE (self), &iter, NULL,
295 individual);
297 goto finally;
300 group_set = folks_group_details_get_groups (
301 FOLKS_GROUP_DETAILS (individual));
303 if (gee_collection_get_size (GEE_COLLECTION (group_set)) > 0)
305 /* add the contact to its groups */
306 GeeIterator *group_iter =
307 gee_iterable_iterator (GEE_ITERABLE (group_set));
309 while (group_iter != NULL && gee_iterator_next (group_iter))
311 gchar *group_name = gee_iterator_get (group_iter);
313 individual_store_get_group (self, group_name, &iter_group,
314 NULL, NULL, FALSE);
316 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
317 individual);
318 grouped = TRUE;
320 g_free (group_name);
323 g_clear_object (&group_iter);
325 else
327 /* fall-back groups, in case there are no named groups */
328 EmpathyContact *contact;
329 TpConnection *connection;
330 const gchar *protocol_name = NULL;
332 contact = empathy_contact_dup_from_folks_individual (individual);
333 if (contact != NULL)
335 connection = empathy_contact_get_connection (contact);
336 protocol_name = tp_connection_get_protocol_name (connection);
339 if (!tp_strdiff (protocol_name, "local-xmpp"))
341 /* these are People Nearby */
342 individual_store_get_group (self,
343 EMPATHY_INDIVIDUAL_STORE_PEOPLE_NEARBY, &iter_group, NULL, NULL,
344 TRUE);
345 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
346 individual);
347 grouped = TRUE;
350 g_clear_object (&contact);
353 if (folks_favourite_details_get_is_favourite (
354 FOLKS_FAVOURITE_DETAILS (individual)))
356 /* Add contact to the fake 'Favorites' group */
357 individual_store_get_group (self, EMPATHY_INDIVIDUAL_STORE_FAVORITE,
358 &iter_group, NULL, NULL, TRUE);
360 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
361 individual);
362 grouped = TRUE;
365 if (!grouped)
367 /* Else add the contact to 'Ungrouped' */
368 individual_store_get_group (self,
369 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
370 &iter_group, NULL, NULL, TRUE);
371 add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
372 individual);
376 finally:
377 individual_store_contact_update (self, individual);
380 static void
381 individual_store_contact_set_active (EmpathyIndividualStore *self,
382 FolksIndividual *individual,
383 gboolean active,
384 gboolean set_changed)
386 GtkTreeModel *model;
387 GList *iters, *l;
389 model = GTK_TREE_MODEL (self);
391 iters = empathy_individual_store_find_contact (self, individual);
392 for (l = iters; l; l = l->next)
394 GtkTreePath *path;
396 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
397 EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, active,
398 -1);
400 if (set_changed)
402 path = gtk_tree_model_get_path (model, l->data);
403 gtk_tree_model_row_changed (model, path, l->data);
404 gtk_tree_path_free (path);
408 empathy_individual_store_free_iters (iters);
411 static void individual_store_contact_active_free (ShowActiveData *data);
413 static void
414 individual_store_contact_active_invalidated (ShowActiveData *data,
415 GObject *old_object)
417 /* Remove the timeout and free the struct, since the individual or individual
418 * store has disappeared. */
419 g_source_remove (data->timeout);
421 if (old_object == (GObject *) data->self)
422 data->self = NULL;
423 else if (old_object == (GObject *) data->individual)
424 data->individual = NULL;
425 else
426 g_assert_not_reached ();
428 individual_store_contact_active_free (data);
431 static ShowActiveData *
432 individual_store_contact_active_new (EmpathyIndividualStore *self,
433 FolksIndividual *individual,
434 gboolean remove_)
436 ShowActiveData *data;
438 data = g_slice_new0 (ShowActiveData);
440 /* We don't actually want to force either the IndividualStore or the
441 * Individual to stay alive, since the user could quit Empathy or disable
442 * the account before the contact_active timeout is fired. */
443 g_object_weak_ref (G_OBJECT (self),
444 (GWeakNotify) individual_store_contact_active_invalidated, data);
445 g_object_weak_ref (G_OBJECT (individual),
446 (GWeakNotify) individual_store_contact_active_invalidated, data);
448 data->self = self;
449 data->individual = individual;
450 data->remove = remove_;
451 data->timeout = 0;
453 return data;
456 static void
457 individual_store_contact_active_free (ShowActiveData *data)
459 if (data->self != NULL)
461 g_object_weak_unref (G_OBJECT (data->self),
462 (GWeakNotify) individual_store_contact_active_invalidated, data);
465 if (data->individual != NULL)
467 g_object_weak_unref (G_OBJECT (data->individual),
468 (GWeakNotify) individual_store_contact_active_invalidated, data);
471 g_slice_free (ShowActiveData, data);
474 static gboolean
475 individual_store_contact_active_cb (ShowActiveData *data)
477 if (data->remove)
479 DEBUG ("Individual'%s' active timeout, removing item",
480 folks_alias_details_get_alias (
481 FOLKS_ALIAS_DETAILS (data->individual)));
482 empathy_individual_store_remove_individual (data->self, data->individual);
485 individual_store_contact_set_active (data->self,
486 data->individual, FALSE, TRUE);
488 individual_store_contact_active_free (data);
490 return FALSE;
493 typedef struct {
494 EmpathyIndividualStore *store; /* weak */
495 GCancellable *cancellable; /* owned */
496 } LoadAvatarData;
498 static void
499 individual_avatar_pixbuf_received_cb (FolksIndividual *individual,
500 GAsyncResult *result,
501 LoadAvatarData *data)
503 GError *error = NULL;
504 GdkPixbuf *pixbuf;
506 pixbuf = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
507 result, &error);
509 if (error != NULL)
511 /* No need to display an error if the individal just doesn't have an
512 * avatar */
513 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
515 DEBUG ("failed to retrieve pixbuf for individual %s: %s",
516 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
517 error->message);
520 g_clear_error (&error);
522 else if (data->store != NULL)
524 GList *iters, *l;
526 iters = empathy_individual_store_find_contact (data->store, individual);
527 for (l = iters; l; l = l->next)
529 gtk_tree_store_set (GTK_TREE_STORE (data->store), l->data,
530 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR, pixbuf,
531 -1);
534 empathy_individual_store_free_iters (iters);
537 /* Free things */
538 if (data->store != NULL)
540 g_object_remove_weak_pointer (G_OBJECT (data->store),
541 (gpointer *) &data->store);
542 data->store->priv->avatar_cancellables = g_list_remove (
543 data->store->priv->avatar_cancellables, data->cancellable);
546 tp_clear_object (&pixbuf);
547 g_object_unref (data->cancellable);
548 g_slice_free (LoadAvatarData, data);
551 static void
552 individual_store_contact_update (EmpathyIndividualStore *self,
553 FolksIndividual *individual)
555 ShowActiveData *data;
556 GtkTreeModel *model;
557 GList *iters, *l;
558 gboolean in_list;
559 gboolean was_online = TRUE;
560 gboolean now_online = FALSE;
561 gboolean set_model = FALSE;
562 gboolean do_remove = FALSE;
563 gboolean do_set_active = FALSE;
564 gboolean do_set_refresh = FALSE;
565 gboolean show_avatar = FALSE;
566 GdkPixbuf *pixbuf_status;
567 LoadAvatarData *load_avatar_data;
569 model = GTK_TREE_MODEL (self);
571 iters = empathy_individual_store_find_contact (self, individual);
572 if (!iters)
574 in_list = FALSE;
576 else
578 in_list = TRUE;
581 /* Get online state now. */
582 now_online = folks_presence_details_is_online (
583 FOLKS_PRESENCE_DETAILS (individual));
585 if (!in_list)
587 DEBUG ("Individual'%s' in list:NO, should be:YES",
588 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
590 empathy_individual_store_add_individual (self, individual);
592 if (self->priv->show_active)
594 do_set_active = TRUE;
597 else
599 /* Get online state before. */
600 if (iters && g_list_length (iters) > 0)
602 gtk_tree_model_get (model, iters->data,
603 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &was_online, -1);
606 /* Is this really an update or an online/offline. */
607 if (self->priv->show_active)
609 if (was_online != now_online)
611 do_set_active = TRUE;
612 do_set_refresh = TRUE;
614 else
616 /* Was TRUE for presence updates. */
617 /* do_set_active = FALSE; */
618 do_set_refresh = TRUE;
622 set_model = TRUE;
625 if (self->priv->show_avatars && !self->priv->is_compact)
627 show_avatar = TRUE;
630 /* Load the avatar asynchronously */
631 load_avatar_data = g_slice_new (LoadAvatarData);
632 load_avatar_data->store = self;
633 g_object_add_weak_pointer (G_OBJECT (self),
634 (gpointer *) &load_avatar_data->store);
635 load_avatar_data->cancellable = g_cancellable_new ();
637 self->priv->avatar_cancellables = g_list_prepend (
638 self->priv->avatar_cancellables, load_avatar_data->cancellable);
640 empathy_pixbuf_avatar_from_individual_scaled_async (individual, 32, 32,
641 load_avatar_data->cancellable,
642 (GAsyncReadyCallback) individual_avatar_pixbuf_received_cb,
643 load_avatar_data);
645 pixbuf_status =
646 empathy_individual_store_get_individual_status_icon (self, individual);
648 for (l = iters; l && set_model; l = l->next)
650 gboolean can_audio_call, can_video_call;
651 const gchar * const *types;
653 empathy_individual_can_audio_video_call (individual, &can_audio_call,
654 &can_video_call, NULL);
656 types = empathy_individual_get_client_types (individual);
658 gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
659 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
660 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
661 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
662 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
663 EMPATHY_INDIVIDUAL_STORE_COL_PRESENCE_TYPE,
664 folks_presence_details_get_presence_type (
665 FOLKS_PRESENCE_DETAILS (individual)),
666 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
667 folks_presence_details_get_presence_message (
668 FOLKS_PRESENCE_DETAILS (individual)),
669 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, self->priv->is_compact,
670 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
671 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, now_online,
672 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
673 EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
674 EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
675 EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
676 -1);
679 if (self->priv->show_active && do_set_active)
681 individual_store_contact_set_active (self, individual, do_set_active,
682 do_set_refresh);
684 if (do_set_active)
686 data =
687 individual_store_contact_active_new (self, individual,
688 do_remove);
689 data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
690 (GSourceFunc) individual_store_contact_active_cb, data);
694 /* FIXME: when someone goes online then offline quickly, the
695 * first timeout sets the user to be inactive and the second
696 * timeout removes the user from the contact list, really we
697 * should remove the first timeout.
699 empathy_individual_store_free_iters (iters);
702 static void
703 individual_store_individual_updated_cb (FolksIndividual *individual,
704 GParamSpec *param,
705 EmpathyIndividualStore *self)
707 individual_store_contact_update (self, individual);
710 static void
711 individual_store_contact_updated_cb (EmpathyContact *contact,
712 GParamSpec *pspec,
713 EmpathyIndividualStore *self)
715 FolksIndividual *individual;
717 individual = g_object_get_data (G_OBJECT (contact), "individual");
718 if (individual == NULL)
719 return;
721 individual_store_contact_update (self, individual);
724 static void
725 individual_personas_changed_cb (FolksIndividual *individual,
726 GeeSet *added,
727 GeeSet *removed,
728 EmpathyIndividualStore *self)
730 GeeIterator *iter;
732 iter = gee_iterable_iterator (GEE_ITERABLE (removed));
733 /* FIXME: libfolks hasn't grown capabilities support yet, so we have to go
734 * through the EmpathyContacts for them. */
735 while (gee_iterator_next (iter))
737 TpfPersona *persona = gee_iterator_get (iter);
738 TpContact *tp_contact;
739 EmpathyContact *contact;
741 if (TPF_IS_PERSONA (persona))
743 tp_contact = tpf_persona_get_contact (persona);
744 if (tp_contact != NULL)
746 contact = empathy_contact_dup_from_tp_contact (tp_contact);
747 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
749 g_object_set_data (G_OBJECT (contact), "individual", NULL);
750 g_signal_handlers_disconnect_by_func (contact,
751 (GCallback) individual_store_contact_updated_cb, self);
753 g_object_unref (contact);
757 g_clear_object (&persona);
759 g_clear_object (&iter);
761 iter = gee_iterable_iterator (GEE_ITERABLE (added));
762 while (gee_iterator_next (iter))
764 TpfPersona *persona = gee_iterator_get (iter);
765 TpContact *tp_contact;
766 EmpathyContact *contact;
768 if (TPF_IS_PERSONA (persona))
770 tp_contact = tpf_persona_get_contact (persona);
771 if (tp_contact != NULL)
773 contact = empathy_contact_dup_from_tp_contact (tp_contact);
774 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
776 g_object_set_data (G_OBJECT (contact), "individual", individual);
777 g_signal_connect (contact, "notify::capabilities",
778 (GCallback) individual_store_contact_updated_cb, self);
779 g_signal_connect (contact, "notify::client-types",
780 (GCallback) individual_store_contact_updated_cb, self);
782 g_object_unref (contact);
786 g_clear_object (&persona);
788 g_clear_object (&iter);
791 static void
792 individual_store_favourites_changed_cb (FolksIndividual *individual,
793 GParamSpec *param,
794 EmpathyIndividualStore *self)
796 DEBUG ("Individual %s is %s a favourite",
797 folks_individual_get_id (individual),
798 folks_favourite_details_get_is_favourite (
799 FOLKS_FAVOURITE_DETAILS (individual)) ? "now" : "no longer");
801 empathy_individual_store_remove_individual (self, individual);
802 empathy_individual_store_add_individual (self, individual);
805 void
806 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
807 FolksIndividual *individual)
809 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
811 empathy_individual_store_add_individual (self, individual);
813 g_signal_connect (individual, "notify::avatar",
814 (GCallback) individual_store_individual_updated_cb, self);
815 g_signal_connect (individual, "notify::presence-type",
816 (GCallback) individual_store_individual_updated_cb, self);
817 g_signal_connect (individual, "notify::presence-message",
818 (GCallback) individual_store_individual_updated_cb, self);
819 g_signal_connect (individual, "notify::alias",
820 (GCallback) individual_store_individual_updated_cb, self);
821 g_signal_connect (individual, "personas-changed",
822 (GCallback) individual_personas_changed_cb, self);
823 g_signal_connect (individual, "notify::is-favourite",
824 (GCallback) individual_store_favourites_changed_cb, self);
826 /* provide an empty set so the callback can assume non-NULL sets */
827 individual_personas_changed_cb (individual,
828 folks_individual_get_personas (individual), empty_set, self);
829 g_clear_object (&empty_set);
832 void
833 empathy_individual_store_disconnect_individual (EmpathyIndividualStore *self,
834 FolksIndividual *individual)
836 GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
838 /* provide an empty set so the callback can assume non-NULL sets */
839 individual_personas_changed_cb (individual, empty_set,
840 folks_individual_get_personas (individual), self);
841 g_clear_object (&empty_set);
843 g_signal_handlers_disconnect_by_func (individual,
844 (GCallback) individual_store_individual_updated_cb, self);
845 g_signal_handlers_disconnect_by_func (individual,
846 (GCallback) individual_personas_changed_cb, self);
847 g_signal_handlers_disconnect_by_func (individual,
848 (GCallback) individual_store_favourites_changed_cb, self);
851 void
852 individual_store_remove_individual_and_disconnect (
853 EmpathyIndividualStore *self,
854 FolksIndividual *individual)
856 empathy_individual_store_disconnect_individual (self, individual);
857 empathy_individual_store_remove_individual (self, individual);
860 static void
861 individual_store_dispose (GObject *object)
863 EmpathyIndividualStore *self = EMPATHY_INDIVIDUAL_STORE (object);
864 GList *l;
866 if (self->priv->dispose_has_run)
867 return;
868 self->priv->dispose_has_run = TRUE;
870 /* Cancel any pending avatar load operations */
871 for (l = self->priv->avatar_cancellables; l != NULL; l = l->next)
873 /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
874 g_cancellable_cancel (G_CANCELLABLE (l->data));
876 g_list_free (self->priv->avatar_cancellables);
878 if (self->priv->inhibit_active)
880 g_source_remove (self->priv->inhibit_active);
883 g_hash_table_unref (self->priv->status_icons);
884 g_hash_table_unref (self->priv->folks_individual_cache);
885 g_hash_table_unref (self->priv->empathy_group_cache);
886 G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
889 static void
890 individual_store_get_property (GObject *object,
891 guint param_id,
892 GValue *value,
893 GParamSpec *pspec)
895 EmpathyIndividualStore *self = EMPATHY_INDIVIDUAL_STORE (object);
897 switch (param_id)
899 case PROP_SHOW_AVATARS:
900 g_value_set_boolean (value, self->priv->show_avatars);
901 break;
902 case PROP_SHOW_PROTOCOLS:
903 g_value_set_boolean (value, self->priv->show_protocols);
904 break;
905 case PROP_SHOW_GROUPS:
906 g_value_set_boolean (value, self->priv->show_groups);
907 break;
908 case PROP_IS_COMPACT:
909 g_value_set_boolean (value, self->priv->is_compact);
910 break;
911 case PROP_SORT_CRITERIUM:
912 g_value_set_enum (value, self->priv->sort_criterium);
913 break;
914 default:
915 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
916 break;
920 static void
921 individual_store_set_property (GObject *object,
922 guint param_id,
923 const GValue *value,
924 GParamSpec *pspec)
926 switch (param_id)
928 case PROP_SHOW_AVATARS:
929 empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
930 (object), g_value_get_boolean (value));
931 break;
932 case PROP_SHOW_PROTOCOLS:
933 empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
934 (object), g_value_get_boolean (value));
935 break;
936 case PROP_SHOW_GROUPS:
937 empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
938 (object), g_value_get_boolean (value));
939 break;
940 case PROP_IS_COMPACT:
941 empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
942 (object), g_value_get_boolean (value));
943 break;
944 case PROP_SORT_CRITERIUM:
945 empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
946 (object), g_value_get_enum (value));
947 break;
948 default:
949 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
950 break;
954 static void
955 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
957 GObjectClass *object_class = G_OBJECT_CLASS (klass);
959 object_class->dispose = individual_store_dispose;
960 object_class->get_property = individual_store_get_property;
961 object_class->set_property = individual_store_set_property;
963 g_object_class_install_property (object_class,
964 PROP_SHOW_AVATARS,
965 g_param_spec_boolean ("show-avatars",
966 "Show Avatars",
967 "Whether contact list should display "
968 "avatars for contacts", TRUE, G_PARAM_READWRITE));
969 g_object_class_install_property (object_class,
970 PROP_SHOW_PROTOCOLS,
971 g_param_spec_boolean ("show-protocols",
972 "Show Protocols",
973 "Whether contact list should display "
974 "protocols for contacts", FALSE, G_PARAM_READWRITE));
975 g_object_class_install_property (object_class,
976 PROP_SHOW_GROUPS,
977 g_param_spec_boolean ("show-groups",
978 "Show Groups",
979 "Whether contact list should display "
980 "contact groups", TRUE, G_PARAM_READWRITE));
981 g_object_class_install_property (object_class,
982 PROP_IS_COMPACT,
983 g_param_spec_boolean ("is-compact",
984 "Is Compact",
985 "Whether the contact list is in compact mode or not",
986 FALSE, G_PARAM_READWRITE));
988 g_object_class_install_property (object_class,
989 PROP_SORT_CRITERIUM,
990 g_param_spec_enum ("sort-criterium",
991 "Sort citerium",
992 "The sort criterium to use for sorting the contact list",
993 EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
994 EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
996 g_type_class_add_private (object_class,
997 sizeof (EmpathyIndividualStorePriv));
1000 static gint
1001 get_position (const char **strv,
1002 const char *str)
1004 int i;
1006 for (i = 0; strv[i] != NULL; i++)
1008 if (!tp_strdiff (strv[i], str))
1009 return i;
1012 return -1;
1015 static gint
1016 compare_separator_and_groups (gboolean is_separator_a,
1017 gboolean is_separator_b,
1018 const gchar *name_a,
1019 const gchar *name_b,
1020 FolksIndividual *individual_a,
1021 FolksIndividual *individual_b,
1022 gboolean fake_group_a,
1023 gboolean fake_group_b)
1025 /* these two lists are the sorted list of fake groups to include at the
1026 * top and bottom of the roster */
1027 const char *top_groups[] = {
1028 EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1029 NULL
1032 const char *bottom_groups[] = {
1033 EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1034 NULL
1037 if (is_separator_a || is_separator_b)
1039 /* We have at least one separator */
1040 if (is_separator_a)
1042 return -1;
1044 else if (is_separator_b)
1046 return 1;
1050 /* One group and one contact */
1051 if (!individual_a && individual_b)
1053 return 1;
1055 else if (individual_a && !individual_b)
1057 return -1;
1059 else if (!individual_a && !individual_b)
1061 gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1063 a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1064 b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1065 a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1066 b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1068 if (a_in_top && b_in_top)
1070 /* compare positions */
1071 return CLAMP (get_position (top_groups, name_a) -
1072 get_position (top_groups, name_b), -1, 1);
1074 else if (a_in_bottom && b_in_bottom)
1076 /* compare positions */
1077 return CLAMP (get_position (bottom_groups, name_a) -
1078 get_position (bottom_groups, name_b), -1, 1);
1080 else if (a_in_top || b_in_bottom)
1082 return -1;
1084 else if (b_in_top || a_in_bottom)
1086 return 1;
1088 else
1090 return g_utf8_collate (name_a, name_b);
1094 /* Two contacts, ordering depends of the sorting policy */
1095 return 0;
1098 static gint
1099 individual_store_contact_sort (FolksIndividual *individual_a,
1100 FolksIndividual *individual_b)
1102 gint ret_val;
1103 EmpathyContact *contact_a = NULL, *contact_b = NULL;
1104 TpAccount *account_a, *account_b;
1106 g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1108 /* alias */
1109 ret_val = g_utf8_collate (
1110 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_a)),
1111 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_b)));
1113 if (ret_val != 0)
1114 goto out;
1116 contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1117 contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1118 if (contact_a != NULL && contact_b != NULL)
1120 account_a = empathy_contact_get_account (contact_a);
1121 account_b = empathy_contact_get_account (contact_b);
1123 g_assert (account_a != NULL);
1124 g_assert (account_b != NULL);
1126 /* protocol */
1127 ret_val = g_strcmp0 (tp_account_get_protocol_name (account_a),
1128 tp_account_get_protocol_name (account_b));
1130 if (ret_val != 0)
1131 goto out;
1133 /* account ID */
1134 ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1135 tp_proxy_get_object_path (account_b));
1137 if (ret_val != 0)
1138 goto out;
1141 /* identifier */
1142 ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1143 folks_individual_get_id (individual_b));
1145 out:
1146 tp_clear_object (&contact_a);
1147 tp_clear_object (&contact_b);
1149 return ret_val;
1152 static gint
1153 individual_store_state_sort_func (GtkTreeModel *model,
1154 GtkTreeIter *iter_a,
1155 GtkTreeIter *iter_b,
1156 gpointer user_data)
1158 gint ret_val;
1159 FolksIndividual *individual_a, *individual_b;
1160 gchar *name_a, *name_b;
1161 gboolean is_separator_a, is_separator_b;
1162 gboolean fake_group_a, fake_group_b;
1163 FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1164 TpConnectionPresenceType tp_presence_a, tp_presence_b;
1166 gtk_tree_model_get (model, iter_a,
1167 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1168 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1169 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1170 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1171 gtk_tree_model_get (model, iter_b,
1172 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1173 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1174 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1175 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1177 if (individual_a == NULL || individual_b == NULL)
1179 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1180 name_a, name_b, individual_a, individual_b, fake_group_a,
1181 fake_group_b);
1182 goto free_and_out;
1185 /* If we managed to get this far, we can start looking at
1186 * the presences.
1188 folks_presence_type_a =
1189 folks_presence_details_get_presence_type (
1190 FOLKS_PRESENCE_DETAILS (individual_a));
1191 folks_presence_type_b =
1192 folks_presence_details_get_presence_type (
1193 FOLKS_PRESENCE_DETAILS (individual_b));
1194 tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1195 tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1197 ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1198 tp_presence_b);
1200 if (ret_val == 0)
1202 /* Fallback: compare by name et al. */
1203 ret_val = individual_store_contact_sort (individual_a, individual_b);
1206 free_and_out:
1207 g_free (name_a);
1208 g_free (name_b);
1209 tp_clear_object (&individual_a);
1210 tp_clear_object (&individual_b);
1212 return ret_val;
1215 static gint
1216 individual_store_name_sort_func (GtkTreeModel *model,
1217 GtkTreeIter *iter_a,
1218 GtkTreeIter *iter_b,
1219 gpointer user_data)
1221 gchar *name_a, *name_b;
1222 FolksIndividual *individual_a, *individual_b;
1223 gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1224 gint ret_val;
1225 gboolean fake_group_a, fake_group_b;
1227 gtk_tree_model_get (model, iter_a,
1228 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1229 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1230 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1231 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1232 gtk_tree_model_get (model, iter_b,
1233 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1234 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1235 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1236 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1238 if (individual_a == NULL || individual_b == NULL)
1239 ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1240 name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1241 else
1242 ret_val = individual_store_contact_sort (individual_a, individual_b);
1244 tp_clear_object (&individual_a);
1245 tp_clear_object (&individual_b);
1246 g_free (name_a);
1247 g_free (name_b);
1249 return ret_val;
1252 static void
1253 individual_store_setup (EmpathyIndividualStore *self)
1255 GType types[] = {
1256 GDK_TYPE_PIXBUF, /* Status pixbuf */
1257 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1258 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1259 G_TYPE_STRING, /* Name */
1260 G_TYPE_UINT, /* Presence type */
1261 G_TYPE_STRING, /* Status string */
1262 G_TYPE_BOOLEAN, /* Compact view */
1263 FOLKS_TYPE_INDIVIDUAL, /* Individual type */
1264 G_TYPE_BOOLEAN, /* Is group */
1265 G_TYPE_BOOLEAN, /* Is active */
1266 G_TYPE_BOOLEAN, /* Is online */
1267 G_TYPE_BOOLEAN, /* Is separator */
1268 G_TYPE_BOOLEAN, /* Can make audio calls */
1269 G_TYPE_BOOLEAN, /* Can make video calls */
1270 G_TYPE_BOOLEAN, /* Is a fake group */
1271 G_TYPE_STRV, /* Client types */
1272 G_TYPE_UINT, /* Event count */
1275 gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1276 EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1278 /* Set up sorting */
1279 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1280 EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1281 individual_store_name_sort_func, self, NULL);
1282 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1283 EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1284 individual_store_state_sort_func, self, NULL);
1286 self->priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1288 empathy_individual_store_set_sort_criterium (self,
1289 self->priv->sort_criterium);
1292 static gboolean
1293 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1295 self->priv->show_active = TRUE;
1296 self->priv->inhibit_active = 0;
1298 return FALSE;
1301 static void
1302 g_queue_free_full_iter (gpointer data)
1304 GQueue *queue = (GQueue *) data;
1305 g_queue_foreach (queue, (GFunc) gtk_tree_iter_free, NULL);
1306 g_queue_free (queue);
1309 static void
1310 empathy_individual_store_init (EmpathyIndividualStore *self)
1312 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1313 EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1315 self->priv->show_avatars = TRUE;
1316 self->priv->show_groups = TRUE;
1317 self->priv->show_protocols = FALSE;
1318 self->priv->inhibit_active =
1319 g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1320 (GSourceFunc) individual_store_inhibit_active_cb, self);
1321 self->priv->status_icons =
1322 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1323 self->priv->folks_individual_cache = g_hash_table_new_full (NULL, NULL, NULL,
1324 g_queue_free_full_iter);
1325 self->priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
1326 g_str_equal, g_free, (GDestroyNotify) gtk_tree_iter_free);
1327 individual_store_setup (self);
1330 gboolean
1331 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1333 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1335 return self->priv->show_avatars;
1338 static gboolean
1339 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1340 GtkTreePath *path,
1341 GtkTreeIter *iter,
1342 EmpathyIndividualStore *self)
1344 gboolean show_avatar = FALSE;
1345 FolksIndividual *individual;
1346 GdkPixbuf *pixbuf_status;
1348 if (self->priv->show_avatars && !self->priv->is_compact)
1350 show_avatar = TRUE;
1353 gtk_tree_model_get (model, iter,
1354 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1356 if (individual == NULL)
1358 return FALSE;
1360 /* get icon from hash_table */
1361 pixbuf_status =
1362 empathy_individual_store_get_individual_status_icon (self, individual);
1364 gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1365 EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1366 EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1367 EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, self->priv->is_compact, -1);
1369 g_object_unref (individual);
1371 return FALSE;
1374 void
1375 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1376 gboolean show_avatars)
1378 GtkTreeModel *model;
1380 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1382 self->priv->show_avatars = show_avatars;
1384 model = GTK_TREE_MODEL (self);
1386 gtk_tree_model_foreach (model,
1387 (GtkTreeModelForeachFunc)
1388 individual_store_update_list_mode_foreach, self);
1390 g_object_notify (G_OBJECT (self), "show-avatars");
1393 gboolean
1394 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1396 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1398 return self->priv->show_protocols;
1401 void
1402 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1403 gboolean show_protocols)
1405 GtkTreeModel *model;
1407 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1409 self->priv->show_protocols = show_protocols;
1411 model = GTK_TREE_MODEL (self);
1413 gtk_tree_model_foreach (model,
1414 (GtkTreeModelForeachFunc)
1415 individual_store_update_list_mode_foreach, self);
1417 g_object_notify (G_OBJECT (self), "show-protocols");
1420 gboolean
1421 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1423 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1425 return self->priv->show_groups;
1428 void
1429 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1430 gboolean show_groups)
1432 EmpathyIndividualStoreClass *klass;
1434 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1436 klass = EMPATHY_INDIVIDUAL_STORE_GET_CLASS ( self);
1438 if (self->priv->show_groups == show_groups)
1440 return;
1443 self->priv->show_groups = show_groups;
1445 if (!klass->initial_loading (self))
1447 /* Remove all contacts and add them back, not optimized but
1448 * that's the easy way :)
1450 * This is only done if there's not a pending setup idle
1451 * callback, otherwise it will race and the contacts will get
1452 * added twice */
1454 gtk_tree_store_clear (GTK_TREE_STORE (self));
1455 /* Also clear the cache */
1456 g_hash_table_remove_all (self->priv->folks_individual_cache);
1457 g_hash_table_remove_all (self->priv->empathy_group_cache);
1459 klass->reload_individuals (self);
1462 g_object_notify (G_OBJECT (self), "show-groups");
1465 gboolean
1466 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1468 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1470 return self->priv->is_compact;
1473 void
1474 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1475 gboolean is_compact)
1477 GtkTreeModel *model;
1479 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1481 self->priv->is_compact = is_compact;
1483 model = GTK_TREE_MODEL (self);
1485 gtk_tree_model_foreach (model,
1486 (GtkTreeModelForeachFunc)
1487 individual_store_update_list_mode_foreach, self);
1489 g_object_notify (G_OBJECT (self), "is-compact");
1492 EmpathyIndividualStoreSort
1493 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1495 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1497 return self->priv->sort_criterium;
1500 void
1501 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1502 EmpathyIndividualStoreSort sort_criterium)
1504 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1506 self->priv->sort_criterium = sort_criterium;
1508 switch (sort_criterium)
1510 case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1511 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1512 EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1513 break;
1515 case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1516 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1517 EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1518 break;
1520 default:
1521 g_assert_not_reached ();
1524 g_object_notify (G_OBJECT (self), "sort-criterium");
1527 gboolean
1528 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1529 GtkTreeIter *iter,
1530 gpointer data)
1532 gboolean is_separator = FALSE;
1534 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1536 gtk_tree_model_get (model, iter,
1537 EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1539 return is_separator;
1542 gchar *
1543 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1544 GtkTreePath *path,
1545 gboolean *path_is_group,
1546 gboolean *is_fake_group)
1548 GtkTreeIter parent_iter, iter;
1549 gchar *name = NULL;
1550 gboolean is_group;
1551 gboolean fake = FALSE;
1553 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1555 if (path_is_group)
1557 *path_is_group = FALSE;
1560 if (!gtk_tree_model_get_iter (model, &iter, path))
1562 return NULL;
1565 gtk_tree_model_get (model, &iter,
1566 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1567 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1569 if (!is_group)
1571 g_free (name);
1572 name = NULL;
1574 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1576 return NULL;
1579 iter = parent_iter;
1581 gtk_tree_model_get (model, &iter,
1582 EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1583 EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1584 EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1585 if (!is_group)
1587 g_free (name);
1588 return NULL;
1592 if (path_is_group)
1594 *path_is_group = TRUE;
1597 if (is_fake_group != NULL)
1598 *is_fake_group = fake;
1600 return name;
1603 static GdkPixbuf *
1604 individual_store_get_individual_status_icon_with_icon_name (
1605 EmpathyIndividualStore *self,
1606 FolksIndividual *individual,
1607 const gchar *status_icon_name)
1609 GdkPixbuf *pixbuf_status;
1610 const gchar *protocol_name = NULL;
1611 gchar *icon_name = NULL;
1612 GeeSet *personas;
1613 GeeIterator *iter;
1614 guint contact_count = 0;
1615 EmpathyContact *contact = NULL;
1616 gboolean show_protocols_here;
1618 personas = folks_individual_get_personas (individual);
1619 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1620 while (gee_iterator_next (iter))
1622 FolksPersona *persona = gee_iterator_get (iter);
1623 if (empathy_folks_persona_is_interesting (persona))
1624 contact_count++;
1626 g_clear_object (&persona);
1628 if (contact_count > 1)
1629 break;
1631 g_clear_object (&iter);
1633 show_protocols_here = (self->priv->show_protocols && (contact_count == 1));
1634 if (show_protocols_here)
1636 contact = empathy_contact_dup_from_folks_individual (individual);
1637 if (contact != NULL)
1639 protocol_name = empathy_protocol_name_for_contact (contact);
1640 icon_name = g_strdup_printf ("%s-%s", status_icon_name,
1641 protocol_name);
1643 else
1645 g_warning ("Cannot retrieve contact from individual '%s'",
1646 folks_alias_details_get_alias (
1647 FOLKS_ALIAS_DETAILS (individual)));
1649 return NULL;
1652 else
1654 icon_name = g_strdup_printf ("%s", status_icon_name);
1657 pixbuf_status = g_hash_table_lookup (self->priv->status_icons, icon_name);
1659 if (pixbuf_status == NULL)
1661 pixbuf_status =
1662 empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1663 status_icon_name, show_protocols_here);
1665 if (pixbuf_status != NULL)
1667 /* pass the reference to the hash table */
1668 g_hash_table_insert (self->priv->status_icons,
1669 g_strdup (icon_name), pixbuf_status);
1673 g_free (icon_name);
1674 tp_clear_object (&contact);
1676 return pixbuf_status;
1679 GdkPixbuf *
1680 empathy_individual_store_get_individual_status_icon (
1681 EmpathyIndividualStore *self,
1682 FolksIndividual *individual)
1684 GdkPixbuf *pixbuf_status = NULL;
1685 const gchar *status_icon_name = NULL;
1687 status_icon_name = empathy_icon_name_for_individual (individual);
1688 if (status_icon_name == NULL)
1689 return NULL;
1691 pixbuf_status =
1692 individual_store_get_individual_status_icon_with_icon_name (self,
1693 individual, status_icon_name);
1695 return pixbuf_status;
1698 void
1699 empathy_individual_store_refresh_individual (EmpathyIndividualStore *self,
1700 FolksIndividual *individual)
1702 gboolean show_active;
1704 show_active = self->priv->show_active;
1705 self->priv->show_active = FALSE;
1706 empathy_individual_store_remove_individual (self, individual);
1707 empathy_individual_store_add_individual (self, individual);
1708 self->priv->show_active = show_active;