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