]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-store.c
Updated Persian translation
[empathy.git] / libempathy-gtk / empathy-individual-store.c
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.
5  *
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.
10  *
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.
15  *
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
20  *
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>
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include <glib.h>
32 #include <glib/gi18n-lib.h>
33 #include <gtk/gtk.h>
34
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
37 #include <telepathy-glib/util.h>
38
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-enum-types.h>
41 #include <libempathy/empathy-individual-manager.h>
42
43 #include "empathy-individual-store.h"
44 #include "empathy-ui-utils.h"
45 #include "empathy-gtk-enum-types.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
48 #include <libempathy/empathy-debug.h>
49
50 /* Active users are those which have recently changed state
51  * (e.g. online, offline or from normal to a busy state).
52  */
53
54 /* Time in seconds user is shown as active */
55 #define ACTIVE_USER_SHOW_TIME 7
56
57 /* Time in seconds after connecting which we wait before active users are enabled */
58 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
59
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualStore)
61 typedef struct
62 {
63   EmpathyIndividualManager *manager;
64   gboolean show_avatars;
65   gboolean show_groups;
66   gboolean is_compact;
67   gboolean show_protocols;
68   gboolean show_active;
69   EmpathyIndividualStoreSort sort_criterium;
70   guint inhibit_active;
71   guint setup_idle_id;
72   gboolean dispose_has_run;
73   GHashTable *status_icons;
74   /* List of owned GCancellables for each pending avatar load operation */
75   GList *avatar_cancellables;
76   /* Hash: FolksIndividual* -> GQueue (GtkTreeRowReference) */
77   GHashTable                  *folks_individual_cache;
78   /* Hash: char *groupname -> GtkTreeRowReference *row */
79   GHashTable                  *empathy_group_cache;
80 } EmpathyIndividualStorePriv;
81
82 typedef struct
83 {
84   EmpathyIndividualStore *self;
85   FolksIndividual *individual;
86   gboolean remove;
87   guint timeout;
88 } ShowActiveData;
89
90 enum
91 {
92   PROP_0,
93   PROP_INDIVIDUAL_MANAGER,
94   PROP_SHOW_AVATARS,
95   PROP_SHOW_PROTOCOLS,
96   PROP_SHOW_GROUPS,
97   PROP_IS_COMPACT,
98   PROP_SORT_CRITERIUM
99 };
100
101 /* prototypes to break cycles */
102 static void individual_store_contact_update (EmpathyIndividualStore *self,
103     FolksIndividual *individual);
104
105 G_DEFINE_TYPE (EmpathyIndividualStore, empathy_individual_store,
106     GTK_TYPE_TREE_STORE);
107
108 /* Calculate whether the Individual can do audio or video calls.
109  * FIXME: We can remove this once libfolks has grown capabilities support
110  * again: bgo#626179. */
111 static void
112 individual_can_audio_video_call (FolksIndividual *individual,
113     gboolean *can_audio_call,
114     gboolean *can_video_call)
115 {
116   GeeSet *personas;
117   GeeIterator *iter;
118   gboolean can_audio = FALSE, can_video = FALSE;
119
120   personas = folks_individual_get_personas (individual);
121   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
122   while (gee_iterator_next (iter))
123     {
124       FolksPersona *persona = gee_iterator_get (iter);
125       TpContact *tp_contact;
126
127       if (!empathy_folks_persona_is_interesting (persona))
128         goto while_finish;
129
130       tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
131       if (tp_contact != NULL)
132         {
133           EmpathyContact *contact;
134
135           contact = empathy_contact_dup_from_tp_contact (tp_contact);
136           empathy_contact_set_persona (contact, persona);
137
138           can_audio = can_audio || empathy_contact_get_capabilities (contact) &
139               EMPATHY_CAPABILITIES_AUDIO;
140           can_video = can_video || empathy_contact_get_capabilities (contact) &
141               EMPATHY_CAPABILITIES_VIDEO;
142
143           g_object_unref (contact);
144         }
145 while_finish:
146       g_clear_object (&persona);
147
148       if (can_audio && can_video)
149         break;
150     }
151   g_clear_object (&iter);
152
153   *can_audio_call = can_audio;
154   *can_video_call = can_video;
155 }
156
157 static const gchar * const *
158 individual_get_client_types (FolksIndividual *individual)
159 {
160   GeeSet *personas;
161   GeeIterator *iter;
162   const gchar * const *types = NULL;
163   FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
164
165   personas = folks_individual_get_personas (individual);
166   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
167   while (gee_iterator_next (iter))
168     {
169       FolksPresenceDetails *presence;
170       FolksPersona *persona = gee_iterator_get (iter);
171
172       /* We only want personas which have presence and a TpContact */
173       if (!empathy_folks_persona_is_interesting (persona))
174         goto while_finish;
175
176       presence = FOLKS_PRESENCE_DETAILS (persona);
177
178       if (folks_presence_details_typecmp (
179               folks_presence_details_get_presence_type (presence),
180               presence_type) > 0)
181         {
182           TpContact *tp_contact;
183
184           presence_type = folks_presence_details_get_presence_type (presence);
185
186           tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
187           if (tp_contact != NULL)
188             types = tp_contact_get_client_types (tp_contact);
189         }
190
191 while_finish:
192       g_clear_object (&persona);
193     }
194   g_clear_object (&iter);
195
196   return types;
197 }
198
199 static void
200 add_individual_to_store (GtkTreeStore *self,
201     GtkTreeIter *iter,
202     GtkTreeIter *parent,
203     FolksIndividual *individual)
204 {
205   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
206   gboolean can_audio_call, can_video_call;
207   const gchar * const *types;
208   GtkTreeRowReference *row_ref;
209   GtkTreePath *path;
210   GQueue *queue;
211
212   individual_can_audio_video_call (individual, &can_audio_call,
213       &can_video_call);
214
215   types = individual_get_client_types (individual);
216
217   gtk_tree_store_insert_with_values (self, iter, parent, 0,
218       EMPATHY_INDIVIDUAL_STORE_COL_NAME,
219       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
220       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, individual,
221       EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
222       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
223       EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
224       EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
225       EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
226       -1);
227
228   path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), iter);
229   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path);
230   queue = g_hash_table_lookup (priv->folks_individual_cache, individual);
231   if (queue)
232     {
233       g_queue_push_tail (queue, row_ref);
234     }
235   else
236     {
237       queue = g_queue_new ();
238       g_queue_push_tail (queue, row_ref);
239       g_hash_table_insert (priv->folks_individual_cache, individual,
240           queue);
241     }
242   gtk_tree_path_free (path);
243 }
244
245 static void
246 individual_store_get_group (EmpathyIndividualStore *self,
247     const gchar *name,
248     GtkTreeIter *iter_group_to_set,
249     GtkTreeIter *iter_separator_to_set,
250     gboolean *created,
251     gboolean is_fake_group)
252 {
253   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
254   GtkTreeModel *model;
255   GtkTreeIter iter_group;
256   GtkTreeIter iter_separator;
257   GtkTreeRowReference         *row_ref;
258
259   model = GTK_TREE_MODEL (self);
260   row_ref = g_hash_table_lookup (priv->empathy_group_cache, name);
261
262   if (row_ref == NULL)
263     {
264       GtkTreePath *path;
265
266       if (created)
267         *created = TRUE;
268
269       gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_group,
270           NULL, 0,
271           EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, NULL,
272           EMPATHY_INDIVIDUAL_STORE_COL_NAME, name,
273           EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, TRUE,
274           EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, FALSE,
275           EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
276           EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, is_fake_group,
277           -1);
278
279       path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), &iter_group);
280       row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path);
281       g_hash_table_insert (priv->empathy_group_cache,
282         g_strdup (name), row_ref);
283       gtk_tree_path_free (path);
284
285       if (iter_group_to_set)
286         *iter_group_to_set = iter_group;
287
288       gtk_tree_store_insert_with_values (GTK_TREE_STORE (self), &iter_separator,
289           &iter_group, 0,
290           EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, TRUE,
291           -1);
292
293       if (iter_separator_to_set)
294         *iter_separator_to_set = iter_separator;
295     }
296   else
297     {
298       GtkTreePath *path = gtk_tree_row_reference_get_path (row_ref);
299       GtkTreeIter iter;
300
301       if (!gtk_tree_model_get_iter (model, &iter, path)) {
302         gtk_tree_path_free (path);
303         return;
304       }
305       gtk_tree_path_free (path);
306
307       if (created)
308         *created = FALSE;
309
310       if (iter_group_to_set)
311         *iter_group_to_set = iter;
312
313       iter_separator = iter;
314
315       if (gtk_tree_model_iter_next (model, &iter_separator))
316         {
317           gboolean is_separator;
318
319           gtk_tree_model_get (model, &iter_separator,
320               EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
321
322           if (is_separator && iter_separator_to_set)
323             *iter_separator_to_set = iter_separator;
324         }
325     }
326 }
327
328 static GList *
329 individual_store_find_contact (EmpathyIndividualStore *self,
330     FolksIndividual *individual)
331 {
332   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
333   GtkTreeModel *model;
334   GQueue *row_refs_queue;
335   GList *i;
336   GList *iters_list = NULL;
337
338   model = GTK_TREE_MODEL (self);
339   row_refs_queue = g_hash_table_lookup (priv->folks_individual_cache,
340       individual);
341   if (!row_refs_queue)
342     return NULL;
343
344   for (i = g_queue_peek_head_link (row_refs_queue) ; i != NULL ; i = i->next)
345     {
346       GtkTreePath *path = gtk_tree_row_reference_get_path (i->data);
347       GtkTreeIter iter;
348       if (!gtk_tree_model_get_iter (model, &iter, path))
349         {
350           gtk_tree_path_free (path);
351           continue;
352         }
353       gtk_tree_path_free (path);
354       iters_list = g_list_prepend
355           (iters_list, gtk_tree_iter_copy (&iter));
356     }
357
358   return iters_list;
359 }
360
361 static void
362 free_iters (GList *iters)
363 {
364   g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
365   g_list_free (iters);
366 }
367
368 static void
369 individual_store_remove_individual (EmpathyIndividualStore *self,
370     FolksIndividual *individual)
371 {
372   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
373   GtkTreeModel *model;
374   GQueue *row_refs;
375   GList *l;
376
377   row_refs = g_hash_table_lookup (priv->folks_individual_cache, individual);
378   if (!row_refs)
379     return;
380
381   /* Clean up model */
382   model = GTK_TREE_MODEL (self);
383
384   for (l = g_queue_peek_head_link (row_refs); l; l = l->next)
385     {
386       GtkTreePath *path = gtk_tree_row_reference_get_path (l->data);
387       GtkTreeIter iter;
388       GtkTreeIter parent;
389
390       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter,
391                                     path))
392         {
393           gtk_tree_path_free (path);
394           continue;
395         }
396       gtk_tree_path_free (path);
397
398       /* NOTE: it is only <= 2 here because we have
399        * separators after the group name, otherwise it
400        * should be 1.
401        */
402       if (gtk_tree_model_iter_parent (model, &parent, &iter) &&
403           gtk_tree_model_iter_n_children (model, &parent) <= 2)
404         {
405           gchar *group_name;
406           gtk_tree_model_get (model, &parent,
407               EMPATHY_CONTACT_LIST_STORE_COL_NAME, &group_name,
408               -1);
409           g_hash_table_remove (priv->empathy_group_cache,
410               group_name);
411           gtk_tree_store_remove (GTK_TREE_STORE (self), &parent);
412         }
413       else
414         {
415           gtk_tree_store_remove (GTK_TREE_STORE (self), &iter);
416         }
417     }
418
419   g_hash_table_remove (priv->folks_individual_cache, individual);
420 }
421
422 static void
423 individual_store_add_individual (EmpathyIndividualStore *self,
424     FolksIndividual *individual)
425 {
426   EmpathyIndividualStorePriv *priv;
427   GtkTreeIter iter;
428   GeeIterator *group_iter = NULL;
429
430   priv = GET_PRIV (self);
431
432   if (EMP_STR_EMPTY (folks_alias_details_get_alias (
433           FOLKS_ALIAS_DETAILS (individual))))
434     return;
435
436   if (priv->show_groups)
437     {
438       GeeSet *group_set = NULL;
439
440       group_set = folks_group_details_get_groups (
441           FOLKS_GROUP_DETAILS (individual));
442
443       if (gee_collection_get_size (GEE_COLLECTION (group_set)) > 0)
444         group_iter = gee_iterable_iterator (GEE_ITERABLE (group_set));
445     }
446
447   /* fall-back groups, in case there are no named groups */
448   if (group_iter == NULL)
449     {
450       GtkTreeIter iter_group, *parent;
451       EmpathyContact *contact;
452       TpConnection *connection;
453       gchar *protocol_name = NULL;
454
455       parent = &iter_group;
456
457       contact = empathy_contact_dup_from_folks_individual (individual);
458       if (contact != NULL)
459         {
460           connection = empathy_contact_get_connection (contact);
461           tp_connection_parse_object_path (connection, &protocol_name, NULL);
462         }
463
464       if (!priv->show_groups)
465         parent = NULL;
466       else if (!tp_strdiff (protocol_name, "local-xmpp"))
467         {
468           /* these are People Nearby */
469           individual_store_get_group (self,
470               EMPATHY_INDIVIDUAL_STORE_PEOPLE_NEARBY, &iter_group, NULL, NULL,
471               TRUE);
472         }
473       else
474         {
475           individual_store_get_group (self,
476               EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
477               &iter_group, NULL, NULL, TRUE);
478         }
479
480       add_individual_to_store (GTK_TREE_STORE (self), &iter, parent,
481           individual);
482
483       g_free (protocol_name);
484       g_clear_object (&contact);
485     }
486
487   /* Else add to each group. */
488   while (group_iter != NULL && gee_iterator_next (group_iter))
489     {
490       gchar *group_name = gee_iterator_get (group_iter);
491       GtkTreeIter iter_group;
492
493       individual_store_get_group (self, group_name, &iter_group, NULL, NULL,
494           FALSE);
495
496       add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
497           individual);
498
499       g_free (group_name);
500     }
501   g_clear_object (&group_iter);
502
503   if (priv->show_groups &&
504       folks_favourite_details_get_is_favourite (
505           FOLKS_FAVOURITE_DETAILS (individual)))
506     {
507       /* Add contact to the fake 'Favorites' group */
508       GtkTreeIter iter_group;
509
510       individual_store_get_group (self, EMPATHY_INDIVIDUAL_STORE_FAVORITE,
511           &iter_group, NULL, NULL, TRUE);
512
513       add_individual_to_store (GTK_TREE_STORE (self), &iter, &iter_group,
514           individual);
515     }
516
517   individual_store_contact_update (self, individual);
518 }
519
520 static void
521 individual_store_contact_set_active (EmpathyIndividualStore *self,
522     FolksIndividual *individual,
523     gboolean active,
524     gboolean set_changed)
525 {
526   GtkTreeModel *model;
527   GList *iters, *l;
528
529   model = GTK_TREE_MODEL (self);
530
531   iters = individual_store_find_contact (self, individual);
532   for (l = iters; l; l = l->next)
533     {
534       GtkTreePath *path;
535
536       gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
537           EMPATHY_INDIVIDUAL_STORE_COL_IS_ACTIVE, active,
538           -1);
539
540       DEBUG ("Set item %s", active ? "active" : "inactive");
541
542       if (set_changed)
543         {
544           path = gtk_tree_model_get_path (model, l->data);
545           gtk_tree_model_row_changed (model, path, l->data);
546           gtk_tree_path_free (path);
547         }
548     }
549
550   free_iters (iters);
551 }
552
553 static void individual_store_contact_active_free (ShowActiveData *data);
554
555 static void
556 individual_store_contact_active_invalidated (ShowActiveData *data,
557     GObject *old_object)
558 {
559   /* Remove the timeout and free the struct, since the individual or individual
560    * store has disappeared. */
561   g_source_remove (data->timeout);
562
563   if (old_object == (GObject *) data->self)
564     data->self = NULL;
565   else if (old_object == (GObject *) data->individual)
566     data->individual = NULL;
567   else
568     g_assert_not_reached ();
569
570   individual_store_contact_active_free (data);
571 }
572
573 static ShowActiveData *
574 individual_store_contact_active_new (EmpathyIndividualStore *self,
575     FolksIndividual *individual,
576     gboolean remove_)
577 {
578   ShowActiveData *data;
579
580   DEBUG ("Individual'%s' now active, and %s be removed",
581       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
582       remove_ ? "WILL" : "WILL NOT");
583
584   data = g_slice_new0 (ShowActiveData);
585
586   /* We don't actually want to force either the IndividualStore or the
587    * Individual to stay alive, since the user could quit Empathy or disable
588    * the account before the contact_active timeout is fired. */
589   g_object_weak_ref (G_OBJECT (self),
590       (GWeakNotify) individual_store_contact_active_invalidated, data);
591   g_object_weak_ref (G_OBJECT (individual),
592       (GWeakNotify) individual_store_contact_active_invalidated, data);
593
594   data->self = self;
595   data->individual = individual;
596   data->remove = remove_;
597   data->timeout = 0;
598
599   return data;
600 }
601
602 static void
603 individual_store_contact_active_free (ShowActiveData *data)
604 {
605   if (data->self != NULL)
606     {
607       g_object_weak_unref (G_OBJECT (data->self),
608           (GWeakNotify) individual_store_contact_active_invalidated, data);
609     }
610
611   if (data->individual != NULL)
612     {
613       g_object_weak_unref (G_OBJECT (data->individual),
614           (GWeakNotify) individual_store_contact_active_invalidated, data);
615     }
616
617   g_slice_free (ShowActiveData, data);
618 }
619
620 static gboolean
621 individual_store_contact_active_cb (ShowActiveData *data)
622 {
623   if (data->remove)
624     {
625       DEBUG ("Individual'%s' active timeout, removing item",
626           folks_alias_details_get_alias (
627             FOLKS_ALIAS_DETAILS (data->individual)));
628       individual_store_remove_individual (data->self, data->individual);
629     }
630
631   DEBUG ("Individual'%s' no longer active",
632       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (data->individual)));
633
634   individual_store_contact_set_active (data->self,
635       data->individual, FALSE, TRUE);
636
637   individual_store_contact_active_free (data);
638
639   return FALSE;
640 }
641
642 typedef struct {
643   EmpathyIndividualStore *store; /* weak */
644   GCancellable *cancellable; /* owned */
645 } LoadAvatarData;
646
647 static void
648 individual_avatar_pixbuf_received_cb (FolksIndividual *individual,
649     GAsyncResult *result,
650     LoadAvatarData *data)
651 {
652   GError *error = NULL;
653   GdkPixbuf *pixbuf;
654
655   pixbuf = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
656       result, &error);
657
658   if (error != NULL)
659     {
660       DEBUG ("failed to retrieve pixbuf for individual %s: %s",
661           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
662           error->message);
663       g_clear_error (&error);
664     }
665   else if (data->store != NULL)
666     {
667       GList *iters, *l;
668
669       iters = individual_store_find_contact (data->store, individual);
670       for (l = iters; l; l = l->next)
671         {
672           gtk_tree_store_set (GTK_TREE_STORE (data->store), l->data,
673               EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR, pixbuf,
674               -1);
675         }
676
677       free_iters (iters);
678     }
679
680   /* Free things */
681   if (data->store != NULL)
682     {
683       EmpathyIndividualStorePriv *priv = GET_PRIV (data->store);
684
685       g_object_remove_weak_pointer (G_OBJECT (data->store),
686           (gpointer *) &data->store);
687       priv->avatar_cancellables = g_list_remove (priv->avatar_cancellables,
688           data->cancellable);
689     }
690
691   tp_clear_object (&pixbuf);
692   g_object_unref (data->cancellable);
693   g_slice_free (LoadAvatarData, data);
694 }
695
696 static void
697 individual_store_contact_update (EmpathyIndividualStore *self,
698     FolksIndividual *individual)
699 {
700   EmpathyIndividualStorePriv *priv;
701   ShowActiveData *data;
702   GtkTreeModel *model;
703   GList *iters, *l;
704   gboolean in_list;
705   gboolean was_online = TRUE;
706   gboolean now_online = FALSE;
707   gboolean set_model = FALSE;
708   gboolean do_remove = FALSE;
709   gboolean do_set_active = FALSE;
710   gboolean do_set_refresh = FALSE;
711   gboolean show_avatar = FALSE;
712   GdkPixbuf *pixbuf_status;
713   LoadAvatarData *load_avatar_data;
714
715   priv = GET_PRIV (self);
716
717   model = GTK_TREE_MODEL (self);
718
719   iters = individual_store_find_contact (self, individual);
720   if (!iters)
721     {
722       in_list = FALSE;
723     }
724   else
725     {
726       in_list = TRUE;
727     }
728
729   /* Get online state now. */
730   now_online = folks_presence_details_is_online (
731       FOLKS_PRESENCE_DETAILS (individual));
732
733   if (!in_list)
734     {
735       DEBUG ("Individual'%s' in list:NO, should be:YES",
736           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
737
738       individual_store_add_individual (self, individual);
739
740       if (priv->show_active)
741         {
742           do_set_active = TRUE;
743
744           DEBUG ("Set active (individual added)");
745         }
746     }
747   else
748     {
749       DEBUG ("Individual'%s' in list:YES, should be:YES",
750           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
751
752       /* Get online state before. */
753       if (iters && g_list_length (iters) > 0)
754         {
755           gtk_tree_model_get (model, iters->data,
756               EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &was_online, -1);
757         }
758
759       /* Is this really an update or an online/offline. */
760       if (priv->show_active)
761         {
762           if (was_online != now_online)
763             {
764               do_set_active = TRUE;
765               do_set_refresh = TRUE;
766
767               DEBUG ("Set active (individual updated %s)",
768                   was_online ? "online  -> offline" : "offline -> online");
769             }
770           else
771             {
772               /* Was TRUE for presence updates. */
773               /* do_set_active = FALSE;  */
774               do_set_refresh = TRUE;
775
776               DEBUG ("Set active (individual updated)");
777             }
778         }
779
780       set_model = TRUE;
781     }
782
783   if (priv->show_avatars && !priv->is_compact)
784     {
785       show_avatar = TRUE;
786     }
787
788   /* Load the avatar asynchronously */
789   load_avatar_data = g_slice_new (LoadAvatarData);
790   load_avatar_data->store = self;
791   g_object_add_weak_pointer (G_OBJECT (self),
792       (gpointer *) &load_avatar_data->store);
793   load_avatar_data->cancellable = g_cancellable_new ();
794
795   priv->avatar_cancellables = g_list_prepend (priv->avatar_cancellables,
796       load_avatar_data->cancellable);
797   empathy_pixbuf_avatar_from_individual_scaled_async (individual, 32, 32,
798       load_avatar_data->cancellable,
799       (GAsyncReadyCallback) individual_avatar_pixbuf_received_cb,
800       load_avatar_data);
801
802   pixbuf_status =
803       empathy_individual_store_get_individual_status_icon (self, individual);
804
805   for (l = iters; l && set_model; l = l->next)
806     {
807       gboolean can_audio_call, can_video_call;
808       const gchar * const *types;
809
810       individual_can_audio_video_call (individual, &can_audio_call,
811           &can_video_call);
812
813       types = individual_get_client_types (individual);
814
815       gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
816           EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
817           EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
818           EMPATHY_INDIVIDUAL_STORE_COL_NAME,
819             folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)),
820           EMPATHY_INDIVIDUAL_STORE_COL_PRESENCE_TYPE,
821             folks_presence_details_get_presence_type (
822                 FOLKS_PRESENCE_DETAILS (individual)),
823           EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
824             folks_presence_details_get_presence_message (
825                 FOLKS_PRESENCE_DETAILS (individual)),
826           EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact,
827           EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
828           EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, now_online,
829           EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
830           EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
831           EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
832           EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
833           -1);
834     }
835
836   if (priv->show_active && do_set_active)
837     {
838       individual_store_contact_set_active (self, individual, do_set_active,
839           do_set_refresh);
840
841       if (do_set_active)
842         {
843           data =
844               individual_store_contact_active_new (self, individual,
845               do_remove);
846           data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
847               (GSourceFunc) individual_store_contact_active_cb, data);
848         }
849     }
850
851   /* FIXME: when someone goes online then offline quickly, the
852    * first timeout sets the user to be inactive and the second
853    * timeout removes the user from the contact list, really we
854    * should remove the first timeout.
855    */
856   free_iters (iters);
857 }
858
859 static void
860 individual_store_individual_updated_cb (FolksIndividual *individual,
861     GParamSpec *param,
862     EmpathyIndividualStore *self)
863 {
864   DEBUG ("Individual'%s' updated, checking roster is in sync...",
865       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
866
867   individual_store_contact_update (self, individual);
868 }
869
870 static void
871 individual_store_contact_updated_cb (EmpathyContact *contact,
872     GParamSpec *pspec,
873     EmpathyIndividualStore *self)
874 {
875   FolksIndividual *individual;
876
877   DEBUG ("Contact '%s' updated, checking roster is in sync...",
878       empathy_contact_get_alias (contact));
879
880   individual = g_object_get_data (G_OBJECT (contact), "individual");
881   if (individual == NULL)
882     return;
883
884   individual_store_contact_update (self, individual);
885 }
886
887 static void
888 individual_personas_changed_cb (FolksIndividual *individual,
889     GeeSet *added,
890     GeeSet *removed,
891     EmpathyIndividualStore *self)
892 {
893   GeeIterator *iter;
894
895   DEBUG ("Individual '%s' personas-changed.",
896       folks_individual_get_id (individual));
897
898   iter = gee_iterable_iterator (GEE_ITERABLE (removed));
899   /* FIXME: libfolks hasn't grown capabilities support yet, so we have to go
900    * through the EmpathyContacts for them. */
901   while (gee_iterator_next (iter))
902     {
903       TpfPersona *persona = gee_iterator_get (iter);
904       TpContact *tp_contact;
905       EmpathyContact *contact;
906
907       if (TPF_IS_PERSONA (persona))
908         {
909           tp_contact = tpf_persona_get_contact (persona);
910           if (tp_contact != NULL)
911             {
912               contact = empathy_contact_dup_from_tp_contact (tp_contact);
913               empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
914
915               g_object_set_data (G_OBJECT (contact), "individual", NULL);
916               g_signal_handlers_disconnect_by_func (contact,
917                   (GCallback) individual_store_contact_updated_cb, self);
918
919               g_object_unref (contact);
920             }
921         }
922
923       g_clear_object (&persona);
924     }
925   g_clear_object (&iter);
926
927   iter = gee_iterable_iterator (GEE_ITERABLE (added));
928   while (gee_iterator_next (iter))
929     {
930       TpfPersona *persona = gee_iterator_get (iter);
931       TpContact *tp_contact;
932       EmpathyContact *contact;
933
934       if (TPF_IS_PERSONA (persona))
935         {
936           tp_contact = tpf_persona_get_contact (persona);
937           if (tp_contact != NULL)
938             {
939               contact = empathy_contact_dup_from_tp_contact (tp_contact);
940               empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
941
942               g_object_set_data (G_OBJECT (contact), "individual", individual);
943               g_signal_connect (contact, "notify::capabilities",
944                   (GCallback) individual_store_contact_updated_cb, self);
945               g_signal_connect (contact, "notify::client-types",
946                   (GCallback) individual_store_contact_updated_cb, self);
947
948               g_object_unref (contact);
949             }
950         }
951
952       g_clear_object (&persona);
953     }
954   g_clear_object (&iter);
955 }
956
957 void
958 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
959     FolksIndividual *individual)
960 {
961   GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
962
963   individual_store_add_individual (self, individual);
964
965   g_signal_connect (individual, "notify::avatar",
966       (GCallback) individual_store_individual_updated_cb, self);
967   g_signal_connect (individual, "notify::presence-type",
968       (GCallback) individual_store_individual_updated_cb, self);
969   g_signal_connect (individual, "notify::presence-message",
970       (GCallback) individual_store_individual_updated_cb, self);
971   g_signal_connect (individual, "notify::alias",
972       (GCallback) individual_store_individual_updated_cb, self);
973   g_signal_connect (individual, "personas-changed",
974       (GCallback) individual_personas_changed_cb, self);
975
976   /* provide an empty set so the callback can assume non-NULL sets */
977   individual_personas_changed_cb (individual,
978       folks_individual_get_personas (individual), empty_set, self);
979   g_clear_object (&empty_set);
980 }
981
982 static void
983 individual_store_disconnect_individual (EmpathyIndividualStore *self,
984     FolksIndividual *individual)
985 {
986   GeeSet *empty_set = gee_set_empty (G_TYPE_NONE, NULL, NULL);
987
988   /* provide an empty set so the callback can assume non-NULL sets */
989   individual_personas_changed_cb (individual, empty_set,
990       folks_individual_get_personas (individual), self);
991   g_clear_object (&empty_set);
992
993   g_signal_handlers_disconnect_by_func (individual,
994       (GCallback) individual_store_individual_updated_cb, self);
995   g_signal_handlers_disconnect_by_func (individual,
996       (GCallback) individual_personas_changed_cb, self);
997 }
998
999 void
1000 individual_store_remove_individual_and_disconnect (
1001     EmpathyIndividualStore *self,
1002     FolksIndividual *individual)
1003 {
1004   individual_store_disconnect_individual (self, individual);
1005   individual_store_remove_individual (self, individual);
1006 }
1007
1008 static void
1009 individual_store_members_changed_cb (EmpathyIndividualManager *manager,
1010     const gchar *message,
1011     GList *added,
1012     GList *removed,
1013     guint reason,
1014     EmpathyIndividualStore *self)
1015 {
1016   GList *l;
1017
1018   for (l = added; l; l = l->next)
1019     {
1020       DEBUG ("Individual %s %s", folks_individual_get_id (l->data), "added");
1021
1022       individual_store_add_individual_and_connect (self, l->data);
1023     }
1024   for (l = removed; l; l = l->next)
1025     {
1026       DEBUG ("Individual %s %s",
1027           folks_individual_get_id (l->data), "removed");
1028
1029       individual_store_remove_individual_and_disconnect (self, l->data);
1030     }
1031 }
1032
1033 static void
1034 individual_store_favourites_changed_cb (EmpathyIndividualManager *manager,
1035     FolksIndividual *individual,
1036     gboolean is_favourite,
1037     EmpathyIndividualStore *self)
1038 {
1039   DEBUG ("Individual %s is %s a favourite",
1040       folks_individual_get_id (individual),
1041       is_favourite ? "now" : "no longer");
1042
1043   individual_store_remove_individual (self, individual);
1044   individual_store_add_individual (self, individual);
1045 }
1046
1047 static void
1048 individual_store_groups_changed_cb (EmpathyIndividualManager *manager,
1049     FolksIndividual *individual,
1050     gchar *group,
1051     gboolean is_member,
1052     EmpathyIndividualStore *self)
1053 {
1054   EmpathyIndividualStorePriv *priv;
1055   gboolean show_active;
1056
1057   priv = GET_PRIV (self);
1058
1059   DEBUG ("Updating groups for individual %s",
1060       folks_individual_get_id (individual));
1061
1062   /* We do this to make sure the groups are correct, if not, we
1063    * would have to check the groups already set up for each
1064    * contact and then see what has been updated.
1065    */
1066   show_active = priv->show_active;
1067   priv->show_active = FALSE;
1068   individual_store_remove_individual (self, individual);
1069   individual_store_add_individual (self, individual);
1070   priv->show_active = show_active;
1071 }
1072
1073 static gboolean
1074 individual_store_manager_setup (gpointer user_data)
1075 {
1076   EmpathyIndividualStore *self = user_data;
1077   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1078   GList *individuals;
1079
1080   /* Signal connection. */
1081
1082   /* TODO: implement */
1083   DEBUG ("handling individual renames unimplemented");
1084
1085   g_signal_connect (priv->manager,
1086       "members-changed",
1087       G_CALLBACK (individual_store_members_changed_cb), self);
1088
1089   g_signal_connect (priv->manager,
1090       "favourites-changed",
1091       G_CALLBACK (individual_store_favourites_changed_cb), self);
1092
1093   g_signal_connect (priv->manager,
1094       "groups-changed",
1095       G_CALLBACK (individual_store_groups_changed_cb), self);
1096
1097   /* Add contacts already created. */
1098   individuals = empathy_individual_manager_get_members (priv->manager);
1099   if (individuals != NULL && FOLKS_IS_INDIVIDUAL (individuals->data))
1100     {
1101       individual_store_members_changed_cb (priv->manager, "initial add",
1102           individuals, NULL, 0, self);
1103       g_list_free (individuals);
1104     }
1105
1106   priv->setup_idle_id = 0;
1107   return FALSE;
1108 }
1109
1110 static void
1111 individual_store_set_individual_manager (EmpathyIndividualStore *self,
1112     EmpathyIndividualManager *manager)
1113 {
1114   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1115
1116   priv->manager = g_object_ref (manager);
1117
1118   /* Let a chance to have all properties set before populating */
1119   priv->setup_idle_id = g_idle_add (individual_store_manager_setup, self);
1120 }
1121
1122 static void
1123 individual_store_member_renamed_cb (EmpathyIndividualManager *manager,
1124     FolksIndividual *old_individual,
1125     FolksIndividual *new_individual,
1126     guint reason,
1127     const gchar *message,
1128     EmpathyIndividualStore *self)
1129 {
1130   DEBUG ("Individual %s renamed to %s",
1131       folks_individual_get_id (old_individual),
1132       folks_individual_get_id (new_individual));
1133
1134   /* add the new contact */
1135   individual_store_add_individual_and_connect (self, new_individual);
1136
1137   /* remove old contact */
1138   individual_store_remove_individual_and_disconnect (self, old_individual);
1139 }
1140
1141 static void
1142 individual_store_dispose (GObject *object)
1143 {
1144   EmpathyIndividualStorePriv *priv = GET_PRIV (object);
1145   GList *individuals, *l;
1146
1147   if (priv->dispose_has_run)
1148     return;
1149   priv->dispose_has_run = TRUE;
1150
1151   /* Cancel any pending avatar load operations */
1152   for (l = priv->avatar_cancellables; l != NULL; l = l->next)
1153     {
1154       /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
1155       g_cancellable_cancel (G_CANCELLABLE (l->data));
1156     }
1157   g_list_free (priv->avatar_cancellables);
1158
1159   individuals = empathy_individual_manager_get_members (priv->manager);
1160   for (l = individuals; l; l = l->next)
1161     {
1162       individual_store_disconnect_individual (EMPATHY_INDIVIDUAL_STORE (object),
1163           FOLKS_INDIVIDUAL (l->data));
1164     }
1165   g_list_free (individuals);
1166
1167   g_signal_handlers_disconnect_by_func (priv->manager,
1168       G_CALLBACK (individual_store_member_renamed_cb), object);
1169   g_signal_handlers_disconnect_by_func (priv->manager,
1170       G_CALLBACK (individual_store_members_changed_cb), object);
1171   g_signal_handlers_disconnect_by_func (priv->manager,
1172       G_CALLBACK (individual_store_favourites_changed_cb), object);
1173   g_signal_handlers_disconnect_by_func (priv->manager,
1174       G_CALLBACK (individual_store_groups_changed_cb), object);
1175   g_object_unref (priv->manager);
1176
1177   if (priv->inhibit_active)
1178     {
1179       g_source_remove (priv->inhibit_active);
1180     }
1181
1182   if (priv->setup_idle_id != 0)
1183     {
1184       g_source_remove (priv->setup_idle_id);
1185     }
1186
1187   g_hash_table_destroy (priv->status_icons);
1188   g_hash_table_destroy (priv->folks_individual_cache);
1189   g_hash_table_destroy (priv->empathy_group_cache);
1190   G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
1191 }
1192
1193 static void
1194 individual_store_get_property (GObject *object,
1195     guint param_id,
1196     GValue *value,
1197     GParamSpec *pspec)
1198 {
1199   EmpathyIndividualStorePriv *priv;
1200
1201   priv = GET_PRIV (object);
1202
1203   switch (param_id)
1204     {
1205     case PROP_INDIVIDUAL_MANAGER:
1206       g_value_set_object (value, priv->manager);
1207       break;
1208     case PROP_SHOW_AVATARS:
1209       g_value_set_boolean (value, priv->show_avatars);
1210       break;
1211     case PROP_SHOW_PROTOCOLS:
1212       g_value_set_boolean (value, priv->show_protocols);
1213       break;
1214     case PROP_SHOW_GROUPS:
1215       g_value_set_boolean (value, priv->show_groups);
1216       break;
1217     case PROP_IS_COMPACT:
1218       g_value_set_boolean (value, priv->is_compact);
1219       break;
1220     case PROP_SORT_CRITERIUM:
1221       g_value_set_enum (value, priv->sort_criterium);
1222       break;
1223     default:
1224       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1225       break;
1226     };
1227 }
1228
1229 static void
1230 individual_store_set_property (GObject *object,
1231     guint param_id,
1232     const GValue *value,
1233     GParamSpec *pspec)
1234 {
1235   switch (param_id)
1236     {
1237     case PROP_INDIVIDUAL_MANAGER:
1238       individual_store_set_individual_manager (EMPATHY_INDIVIDUAL_STORE
1239           (object), g_value_get_object (value));
1240       break;
1241     case PROP_SHOW_AVATARS:
1242       empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
1243           (object), g_value_get_boolean (value));
1244       break;
1245     case PROP_SHOW_PROTOCOLS:
1246       empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1247           (object), g_value_get_boolean (value));
1248       break;
1249     case PROP_SHOW_GROUPS:
1250       empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1251           (object), g_value_get_boolean (value));
1252       break;
1253     case PROP_IS_COMPACT:
1254       empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1255           (object), g_value_get_boolean (value));
1256       break;
1257     case PROP_SORT_CRITERIUM:
1258       empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1259           (object), g_value_get_enum (value));
1260       break;
1261     default:
1262       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1263       break;
1264     };
1265 }
1266
1267 static void
1268 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1269 {
1270   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1271
1272   object_class->dispose = individual_store_dispose;
1273   object_class->get_property = individual_store_get_property;
1274   object_class->set_property = individual_store_set_property;
1275
1276   g_object_class_install_property (object_class,
1277       PROP_INDIVIDUAL_MANAGER,
1278       g_param_spec_object ("individual-manager",
1279           "The individual manager",
1280           "The individual manager",
1281           EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1282           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1283   g_object_class_install_property (object_class,
1284       PROP_SHOW_AVATARS,
1285       g_param_spec_boolean ("show-avatars",
1286           "Show Avatars",
1287           "Whether contact list should display "
1288           "avatars for contacts", TRUE, G_PARAM_READWRITE));
1289   g_object_class_install_property (object_class,
1290       PROP_SHOW_PROTOCOLS,
1291       g_param_spec_boolean ("show-protocols",
1292           "Show Protocols",
1293           "Whether contact list should display "
1294           "protocols for contacts", FALSE, G_PARAM_READWRITE));
1295   g_object_class_install_property (object_class,
1296       PROP_SHOW_GROUPS,
1297       g_param_spec_boolean ("show-groups",
1298           "Show Groups",
1299           "Whether contact list should display "
1300           "contact groups", TRUE, G_PARAM_READWRITE));
1301   g_object_class_install_property (object_class,
1302       PROP_IS_COMPACT,
1303       g_param_spec_boolean ("is-compact",
1304           "Is Compact",
1305           "Whether the contact list is in compact mode or not",
1306           FALSE, G_PARAM_READWRITE));
1307
1308   g_object_class_install_property (object_class,
1309       PROP_SORT_CRITERIUM,
1310       g_param_spec_enum ("sort-criterium",
1311           "Sort citerium",
1312           "The sort criterium to use for sorting the contact list",
1313           EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1314           EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1315
1316   g_type_class_add_private (object_class,
1317       sizeof (EmpathyIndividualStorePriv));
1318 }
1319
1320 static gint
1321 get_position (const char **strv,
1322     const char *str)
1323 {
1324   int i;
1325
1326   for (i = 0; strv[i] != NULL; i++)
1327     {
1328       if (!tp_strdiff (strv[i], str))
1329         return i;
1330     }
1331
1332   return -1;
1333 }
1334
1335 static gint
1336 compare_separator_and_groups (gboolean is_separator_a,
1337     gboolean is_separator_b,
1338     const gchar *name_a,
1339     const gchar *name_b,
1340     FolksIndividual *individual_a,
1341     FolksIndividual *individual_b,
1342     gboolean fake_group_a,
1343     gboolean fake_group_b)
1344 {
1345   /* these two lists are the sorted list of fake groups to include at the
1346    * top and bottom of the roster */
1347   const char *top_groups[] = {
1348     EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1349     NULL
1350   };
1351
1352   const char *bottom_groups[] = {
1353     EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1354     NULL
1355   };
1356
1357   if (is_separator_a || is_separator_b)
1358     {
1359       /* We have at least one separator */
1360       if (is_separator_a)
1361         {
1362           return -1;
1363         }
1364       else if (is_separator_b)
1365         {
1366           return 1;
1367         }
1368     }
1369
1370   /* One group and one contact */
1371   if (!individual_a && individual_b)
1372     {
1373       return 1;
1374     }
1375   else if (individual_a && !individual_b)
1376     {
1377       return -1;
1378     }
1379   else if (!individual_a && !individual_b)
1380     {
1381       gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1382
1383       a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1384       b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1385       a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1386       b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1387
1388       if (a_in_top && b_in_top)
1389         {
1390           /* compare positions */
1391           return CLAMP (get_position (top_groups, name_a) -
1392               get_position (top_groups, name_b), -1, 1);
1393         }
1394       else if (a_in_bottom && b_in_bottom)
1395         {
1396           /* compare positions */
1397           return CLAMP (get_position (bottom_groups, name_a) -
1398               get_position (bottom_groups, name_b), -1, 1);
1399         }
1400       else if (a_in_top || b_in_bottom)
1401         {
1402           return -1;
1403         }
1404       else if (b_in_top || a_in_bottom)
1405         {
1406           return 1;
1407         }
1408       else
1409         {
1410           return g_utf8_collate (name_a, name_b);
1411         }
1412     }
1413
1414   /* Two contacts, ordering depends of the sorting policy */
1415   return 0;
1416 }
1417
1418 static gint
1419 individual_store_contact_sort (FolksIndividual *individual_a,
1420     FolksIndividual *individual_b)
1421 {
1422   gint ret_val;
1423   EmpathyContact *contact_a = NULL, *contact_b = NULL;
1424   TpAccount *account_a, *account_b;
1425
1426   g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1427
1428   /* alias */
1429   ret_val = g_utf8_collate (
1430       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_a)),
1431       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual_b)));
1432
1433   if (ret_val != 0)
1434     goto out;
1435
1436   contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1437   contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1438   if (contact_a != NULL && contact_b != NULL)
1439     {
1440       account_a = empathy_contact_get_account (contact_a);
1441       account_b = empathy_contact_get_account (contact_b);
1442
1443       g_assert (account_a != NULL);
1444       g_assert (account_b != NULL);
1445
1446       /* protocol */
1447       ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1448           tp_account_get_protocol (account_b));
1449
1450       if (ret_val != 0)
1451         goto out;
1452
1453       /* account ID */
1454       ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1455           tp_proxy_get_object_path (account_b));
1456
1457       if (ret_val != 0)
1458         goto out;
1459     }
1460
1461   /* identifier */
1462   ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1463       folks_individual_get_id (individual_b));
1464
1465 out:
1466   tp_clear_object (&contact_a);
1467   tp_clear_object (&contact_b);
1468
1469   return ret_val;
1470 }
1471
1472 static gint
1473 individual_store_state_sort_func (GtkTreeModel *model,
1474     GtkTreeIter *iter_a,
1475     GtkTreeIter *iter_b,
1476     gpointer user_data)
1477 {
1478   gint ret_val;
1479   FolksIndividual *individual_a, *individual_b;
1480   gchar *name_a, *name_b;
1481   gboolean is_separator_a, is_separator_b;
1482   gboolean fake_group_a, fake_group_b;
1483   FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1484   TpConnectionPresenceType tp_presence_a, tp_presence_b;
1485
1486   gtk_tree_model_get (model, iter_a,
1487       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1488       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1489       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1490       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1491   gtk_tree_model_get (model, iter_b,
1492       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1493       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1494       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1495       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1496
1497   if (individual_a == NULL || individual_b == NULL)
1498     {
1499       ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1500           name_a, name_b, individual_a, individual_b, fake_group_a,
1501           fake_group_b);
1502       goto free_and_out;
1503     }
1504
1505   /* If we managed to get this far, we can start looking at
1506    * the presences.
1507    */
1508   folks_presence_type_a =
1509       folks_presence_details_get_presence_type (
1510           FOLKS_PRESENCE_DETAILS (individual_a));
1511   folks_presence_type_b =
1512       folks_presence_details_get_presence_type (
1513           FOLKS_PRESENCE_DETAILS (individual_b));
1514   tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1515   tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1516
1517   ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1518       tp_presence_b);
1519
1520   if (ret_val == 0)
1521     {
1522       /* Fallback: compare by name et al. */
1523       ret_val = individual_store_contact_sort (individual_a, individual_b);
1524     }
1525
1526 free_and_out:
1527   g_free (name_a);
1528   g_free (name_b);
1529   tp_clear_object (&individual_a);
1530   tp_clear_object (&individual_b);
1531
1532   return ret_val;
1533 }
1534
1535 static gint
1536 individual_store_name_sort_func (GtkTreeModel *model,
1537     GtkTreeIter *iter_a,
1538     GtkTreeIter *iter_b,
1539     gpointer user_data)
1540 {
1541   gchar *name_a, *name_b;
1542   FolksIndividual *individual_a, *individual_b;
1543   gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1544   gint ret_val;
1545   gboolean fake_group_a, fake_group_b;
1546
1547   gtk_tree_model_get (model, iter_a,
1548       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1549       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1550       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1551       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1552   gtk_tree_model_get (model, iter_b,
1553       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1554       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1555       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1556       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1557
1558   if (individual_a == NULL || individual_b == NULL)
1559     ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1560         name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1561   else
1562     ret_val = individual_store_contact_sort (individual_a, individual_b);
1563
1564   tp_clear_object (&individual_a);
1565   tp_clear_object (&individual_b);
1566   g_free (name_a);
1567   g_free (name_b);
1568
1569   return ret_val;
1570 }
1571
1572 static void
1573 individual_store_setup (EmpathyIndividualStore *self)
1574 {
1575   EmpathyIndividualStorePriv *priv;
1576   GType types[] = {
1577     GDK_TYPE_PIXBUF,            /* Status pixbuf */
1578     GDK_TYPE_PIXBUF,            /* Avatar pixbuf */
1579     G_TYPE_BOOLEAN,             /* Avatar pixbuf visible */
1580     G_TYPE_STRING,              /* Name */
1581     G_TYPE_UINT,                /* Presence type */
1582     G_TYPE_STRING,              /* Status string */
1583     G_TYPE_BOOLEAN,             /* Compact view */
1584     FOLKS_TYPE_INDIVIDUAL,      /* Individual type */
1585     G_TYPE_BOOLEAN,             /* Is group */
1586     G_TYPE_BOOLEAN,             /* Is active */
1587     G_TYPE_BOOLEAN,             /* Is online */
1588     G_TYPE_BOOLEAN,             /* Is separator */
1589     G_TYPE_BOOLEAN,             /* Can make audio calls */
1590     G_TYPE_BOOLEAN,             /* Can make video calls */
1591     G_TYPE_BOOLEAN,             /* Is a fake group */
1592     G_TYPE_STRV,                /* Client types */
1593     G_TYPE_UINT,                /* Event count */
1594   };
1595
1596   priv = GET_PRIV (self);
1597
1598   gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1599       EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1600
1601   /* Set up sorting */
1602   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1603       EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1604       individual_store_name_sort_func, self, NULL);
1605   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1606       EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1607       individual_store_state_sort_func, self, NULL);
1608
1609   priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1610   empathy_individual_store_set_sort_criterium (self, priv->sort_criterium);
1611 }
1612
1613 static gboolean
1614 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1615 {
1616   EmpathyIndividualStorePriv *priv;
1617
1618   priv = GET_PRIV (self);
1619
1620   priv->show_active = TRUE;
1621   priv->inhibit_active = 0;
1622
1623   return FALSE;
1624 }
1625
1626 static void
1627 g_queue_free_full_row_ref (gpointer data)
1628 {
1629   GQueue *queue = (GQueue *) data;
1630   g_queue_foreach (queue, (GFunc) gtk_tree_row_reference_free, NULL);
1631   g_queue_free (queue);
1632 }
1633
1634 static void
1635 empathy_individual_store_init (EmpathyIndividualStore *self)
1636 {
1637   EmpathyIndividualStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1638       EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1639
1640   self->priv = priv;
1641   priv->show_avatars = TRUE;
1642   priv->show_groups = TRUE;
1643   priv->show_protocols = FALSE;
1644   priv->inhibit_active =
1645       g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1646       (GSourceFunc) individual_store_inhibit_active_cb, self);
1647   priv->status_icons =
1648       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1649   priv->folks_individual_cache = g_hash_table_new_full (NULL, NULL, NULL,
1650       g_queue_free_full_row_ref);
1651   priv->empathy_group_cache = g_hash_table_new_full (g_str_hash,
1652       g_str_equal, g_free,
1653       (GDestroyNotify) gtk_tree_row_reference_free);
1654   individual_store_setup (self);
1655 }
1656
1657 EmpathyIndividualStore *
1658 empathy_individual_store_new (EmpathyIndividualManager *manager)
1659 {
1660   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1661
1662   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE,
1663       "individual-manager", manager, NULL);
1664 }
1665
1666 EmpathyIndividualManager *
1667 empathy_individual_store_get_manager (EmpathyIndividualStore *self)
1668 {
1669   EmpathyIndividualStorePriv *priv;
1670
1671   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), FALSE);
1672
1673   priv = GET_PRIV (self);
1674
1675   return priv->manager;
1676 }
1677
1678 gboolean
1679 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1680 {
1681   EmpathyIndividualStorePriv *priv;
1682
1683   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1684
1685   priv = GET_PRIV (self);
1686
1687   return priv->show_avatars;
1688 }
1689
1690 static gboolean
1691 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1692     GtkTreePath *path,
1693     GtkTreeIter *iter,
1694     EmpathyIndividualStore *self)
1695 {
1696   EmpathyIndividualStorePriv *priv;
1697   gboolean show_avatar = FALSE;
1698   FolksIndividual *individual;
1699   GdkPixbuf *pixbuf_status;
1700
1701   priv = GET_PRIV (self);
1702
1703   if (priv->show_avatars && !priv->is_compact)
1704     {
1705       show_avatar = TRUE;
1706     }
1707
1708   gtk_tree_model_get (model, iter,
1709       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1710
1711   if (individual == NULL)
1712     {
1713       return FALSE;
1714     }
1715   /* get icon from hash_table */
1716   pixbuf_status =
1717       empathy_individual_store_get_individual_status_icon (self, individual);
1718
1719   gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1720       EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1721       EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1722       EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact, -1);
1723
1724   g_object_unref (individual);
1725
1726   return FALSE;
1727 }
1728
1729 void
1730 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1731     gboolean show_avatars)
1732 {
1733   EmpathyIndividualStorePriv *priv;
1734   GtkTreeModel *model;
1735
1736   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1737
1738   priv = GET_PRIV (self);
1739
1740   priv->show_avatars = show_avatars;
1741
1742   model = GTK_TREE_MODEL (self);
1743
1744   gtk_tree_model_foreach (model,
1745       (GtkTreeModelForeachFunc)
1746       individual_store_update_list_mode_foreach, self);
1747
1748   g_object_notify (G_OBJECT (self), "show-avatars");
1749 }
1750
1751 gboolean
1752 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1753 {
1754   EmpathyIndividualStorePriv *priv;
1755
1756   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1757
1758   priv = GET_PRIV (self);
1759
1760   return priv->show_protocols;
1761 }
1762
1763 void
1764 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1765     gboolean show_protocols)
1766 {
1767   EmpathyIndividualStorePriv *priv;
1768   GtkTreeModel *model;
1769
1770   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1771
1772   priv = GET_PRIV (self);
1773
1774   priv->show_protocols = show_protocols;
1775
1776   model = GTK_TREE_MODEL (self);
1777
1778   gtk_tree_model_foreach (model,
1779       (GtkTreeModelForeachFunc)
1780       individual_store_update_list_mode_foreach, self);
1781
1782   g_object_notify (G_OBJECT (self), "show-protocols");
1783 }
1784
1785 gboolean
1786 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1787 {
1788   EmpathyIndividualStorePriv *priv;
1789
1790   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1791
1792   priv = GET_PRIV (self);
1793
1794   return priv->show_groups;
1795 }
1796
1797 void
1798 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1799     gboolean show_groups)
1800 {
1801   EmpathyIndividualStorePriv *priv;
1802
1803   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1804
1805   priv = GET_PRIV (self);
1806
1807   if (priv->show_groups == show_groups)
1808     {
1809       return;
1810     }
1811
1812   priv->show_groups = show_groups;
1813
1814   if (priv->setup_idle_id == 0)
1815     {
1816       /* Remove all contacts and add them back, not optimized but
1817        * that's the easy way :)
1818        *
1819        * This is only done if there's not a pending setup idle
1820        * callback, otherwise it will race and the contacts will get
1821        * added twice */
1822       GList *contacts;
1823
1824       gtk_tree_store_clear (GTK_TREE_STORE (self));
1825       /* Also clear the cache */
1826       g_hash_table_remove_all (priv->folks_individual_cache);
1827       g_hash_table_remove_all (priv->empathy_group_cache);
1828
1829       contacts = empathy_individual_manager_get_members (priv->manager);
1830
1831       individual_store_members_changed_cb (priv->manager,
1832           "re-adding members: toggled group visibility",
1833           contacts, NULL, 0, self);
1834       g_list_free (contacts);
1835     }
1836
1837   g_object_notify (G_OBJECT (self), "show-groups");
1838 }
1839
1840 gboolean
1841 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1842 {
1843   EmpathyIndividualStorePriv *priv;
1844
1845   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1846
1847   priv = GET_PRIV (self);
1848
1849   return priv->is_compact;
1850 }
1851
1852 void
1853 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1854     gboolean is_compact)
1855 {
1856   EmpathyIndividualStorePriv *priv;
1857   GtkTreeModel *model;
1858
1859   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1860
1861   priv = GET_PRIV (self);
1862
1863   priv->is_compact = is_compact;
1864
1865   model = GTK_TREE_MODEL (self);
1866
1867   gtk_tree_model_foreach (model,
1868       (GtkTreeModelForeachFunc)
1869       individual_store_update_list_mode_foreach, self);
1870
1871   g_object_notify (G_OBJECT (self), "is-compact");
1872 }
1873
1874 EmpathyIndividualStoreSort
1875 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1876 {
1877   EmpathyIndividualStorePriv *priv;
1878
1879   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1880
1881   priv = GET_PRIV (self);
1882
1883   return priv->sort_criterium;
1884 }
1885
1886 void
1887 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1888     EmpathyIndividualStoreSort sort_criterium)
1889 {
1890   EmpathyIndividualStorePriv *priv;
1891
1892   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1893
1894   priv = GET_PRIV (self);
1895
1896   priv->sort_criterium = sort_criterium;
1897
1898   switch (sort_criterium)
1899     {
1900     case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1901       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1902           EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1903       break;
1904
1905     case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1906       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1907           EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1908       break;
1909
1910     default:
1911       g_assert_not_reached ();
1912     }
1913
1914   g_object_notify (G_OBJECT (self), "sort-criterium");
1915 }
1916
1917 gboolean
1918 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1919     GtkTreeIter *iter,
1920     gpointer data)
1921 {
1922   gboolean is_separator = FALSE;
1923
1924   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1925
1926   gtk_tree_model_get (model, iter,
1927       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1928
1929   return is_separator;
1930 }
1931
1932 gchar *
1933 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1934     GtkTreePath *path,
1935     gboolean *path_is_group,
1936     gboolean *is_fake_group)
1937 {
1938   GtkTreeIter parent_iter, iter;
1939   gchar *name = NULL;
1940   gboolean is_group;
1941   gboolean fake = FALSE;
1942
1943   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1944
1945   if (path_is_group)
1946     {
1947       *path_is_group = FALSE;
1948     }
1949
1950   if (!gtk_tree_model_get_iter (model, &iter, path))
1951     {
1952       return NULL;
1953     }
1954
1955   gtk_tree_model_get (model, &iter,
1956       EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1957       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1958
1959   if (!is_group)
1960     {
1961       g_free (name);
1962       name = NULL;
1963
1964       if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1965         {
1966           return NULL;
1967         }
1968
1969       iter = parent_iter;
1970
1971       gtk_tree_model_get (model, &iter,
1972           EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1973           EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1974           EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1975       if (!is_group)
1976         {
1977           g_free (name);
1978           return NULL;
1979         }
1980     }
1981
1982   if (path_is_group)
1983     {
1984       *path_is_group = TRUE;
1985     }
1986
1987   if (is_fake_group != NULL)
1988     *is_fake_group = fake;
1989
1990   return name;
1991 }
1992
1993 static GdkPixbuf *
1994 individual_store_get_individual_status_icon_with_icon_name (
1995     EmpathyIndividualStore *self,
1996     FolksIndividual *individual,
1997     const gchar *status_icon_name)
1998 {
1999   GdkPixbuf *pixbuf_status;
2000   EmpathyIndividualStorePriv *priv;
2001   const gchar *protocol_name = NULL;
2002   gchar *icon_name = NULL;
2003   GeeSet *personas;
2004   GeeIterator *iter;
2005   guint contact_count = 0;
2006   EmpathyContact *contact = NULL;
2007   gboolean show_protocols_here;
2008
2009   priv = GET_PRIV (self);
2010
2011   personas = folks_individual_get_personas (individual);
2012   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2013   while (gee_iterator_next (iter))
2014     {
2015       FolksPersona *persona = gee_iterator_get (iter);
2016       if (empathy_folks_persona_is_interesting (persona))
2017         contact_count++;
2018
2019       g_clear_object (&persona);
2020
2021       if (contact_count > 1)
2022         break;
2023     }
2024   g_clear_object (&iter);
2025
2026   show_protocols_here = (priv->show_protocols && (contact_count == 1));
2027   if (show_protocols_here)
2028     {
2029       contact = empathy_contact_dup_from_folks_individual (individual);
2030       if (contact != NULL)
2031         {
2032           protocol_name = empathy_protocol_name_for_contact (contact);
2033           icon_name = g_strdup_printf ("%s-%s", status_icon_name,
2034               protocol_name);
2035         }
2036       else
2037         {
2038           g_warning ("Cannot retrieve contact from individual '%s'",
2039               folks_alias_details_get_alias (
2040                 FOLKS_ALIAS_DETAILS (individual)));
2041
2042           return NULL;
2043         }
2044     }
2045   else
2046     {
2047       icon_name = g_strdup_printf ("%s", status_icon_name);
2048     }
2049
2050   pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
2051
2052   if (pixbuf_status == NULL)
2053     {
2054       pixbuf_status =
2055           empathy_pixbuf_contact_status_icon_with_icon_name (contact,
2056           status_icon_name, show_protocols_here);
2057
2058       if (pixbuf_status != NULL)
2059         {
2060           /* pass the reference to the hash table */
2061           g_hash_table_insert (priv->status_icons,
2062               g_strdup (icon_name), pixbuf_status);
2063         }
2064     }
2065
2066   g_free (icon_name);
2067   tp_clear_object (&contact);
2068
2069   return pixbuf_status;
2070 }
2071
2072 GdkPixbuf *
2073 empathy_individual_store_get_individual_status_icon (
2074     EmpathyIndividualStore *self,
2075     FolksIndividual *individual)
2076 {
2077   GdkPixbuf *pixbuf_status = NULL;
2078   const gchar *status_icon_name = NULL;
2079
2080   status_icon_name = empathy_icon_name_for_individual (individual);
2081   if (status_icon_name == NULL)
2082     return NULL;
2083
2084   pixbuf_status =
2085       individual_store_get_individual_status_icon_with_icon_name (self,
2086       individual, status_icon_name);
2087
2088   return pixbuf_status;
2089 }