]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-store.c
empathy_individual_view_get_group_menu: don't leak the group
[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       free_iters (iters);
656     }
657
658   /* Free things */
659   if (data->store != NULL)
660     {
661       EmpathyIndividualStorePriv *priv = GET_PRIV (data->store);
662
663       g_object_remove_weak_pointer (G_OBJECT (data->store),
664           (gpointer *) &data->store);
665       priv->avatar_cancellables = g_list_remove (priv->avatar_cancellables,
666           data->cancellable);
667     }
668
669   g_object_unref (data->cancellable);
670   g_slice_free (LoadAvatarData, data);
671 }
672
673 static void
674 individual_store_contact_update (EmpathyIndividualStore *self,
675     FolksIndividual *individual)
676 {
677   EmpathyIndividualStorePriv *priv;
678   ShowActiveData *data;
679   GtkTreeModel *model;
680   GList *iters, *l;
681   gboolean in_list;
682   gboolean was_online = TRUE;
683   gboolean now_online = FALSE;
684   gboolean set_model = FALSE;
685   gboolean do_remove = FALSE;
686   gboolean do_set_active = FALSE;
687   gboolean do_set_refresh = FALSE;
688   gboolean show_avatar = FALSE;
689   GdkPixbuf *pixbuf_status;
690   LoadAvatarData *load_avatar_data;
691
692   priv = GET_PRIV (self);
693
694   model = GTK_TREE_MODEL (self);
695
696   iters = individual_store_find_contact (self, individual);
697   if (!iters)
698     {
699       in_list = FALSE;
700     }
701   else
702     {
703       in_list = TRUE;
704     }
705
706   /* Get online state now. */
707   now_online = folks_presence_is_online (FOLKS_PRESENCE (individual));
708
709   if (!in_list)
710     {
711       DEBUG ("Individual'%s' in list:NO, should be:YES",
712           folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)));
713
714       individual_store_add_individual (self, individual);
715
716       if (priv->show_active)
717         {
718           do_set_active = TRUE;
719
720           DEBUG ("Set active (individual added)");
721         }
722     }
723   else
724     {
725       DEBUG ("Individual'%s' in list:YES, should be:YES",
726           folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)));
727
728       /* Get online state before. */
729       if (iters && g_list_length (iters) > 0)
730         {
731           gtk_tree_model_get (model, iters->data,
732               EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &was_online, -1);
733         }
734
735       /* Is this really an update or an online/offline. */
736       if (priv->show_active)
737         {
738           if (was_online != now_online)
739             {
740               do_set_active = TRUE;
741               do_set_refresh = TRUE;
742
743               DEBUG ("Set active (individual updated %s)",
744                   was_online ? "online  -> offline" : "offline -> online");
745             }
746           else
747             {
748               /* Was TRUE for presence updates. */
749               /* do_set_active = FALSE;  */
750               do_set_refresh = TRUE;
751
752               DEBUG ("Set active (individual updated)");
753             }
754         }
755
756       set_model = TRUE;
757     }
758
759   if (priv->show_avatars && !priv->is_compact)
760     {
761       show_avatar = TRUE;
762     }
763
764   /* Load the avatar asynchronously */
765   load_avatar_data = g_slice_new (LoadAvatarData);
766   load_avatar_data->store = self;
767   g_object_add_weak_pointer (G_OBJECT (self),
768       (gpointer *) &load_avatar_data->store);
769   load_avatar_data->cancellable = g_cancellable_new ();
770
771   priv->avatar_cancellables = g_list_prepend (priv->avatar_cancellables,
772       load_avatar_data->cancellable);
773   empathy_pixbuf_avatar_from_individual_scaled_async (individual, 32, 32,
774       load_avatar_data->cancellable,
775       (GAsyncReadyCallback) individual_avatar_pixbuf_received_cb,
776       load_avatar_data);
777
778   pixbuf_status =
779       empathy_individual_store_get_individual_status_icon (self, individual);
780
781   for (l = iters; l && set_model; l = l->next)
782     {
783       gboolean can_audio_call, can_video_call;
784       const gchar * const *types;
785
786       individual_can_audio_video_call (individual, &can_audio_call,
787           &can_video_call);
788
789       types = individual_get_client_types (individual);
790
791       gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
792           EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
793           EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
794           EMPATHY_INDIVIDUAL_STORE_COL_NAME,
795             folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)),
796           EMPATHY_INDIVIDUAL_STORE_COL_PRESENCE_TYPE,
797             folks_presence_get_presence_type (FOLKS_PRESENCE (individual)),
798           EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
799             folks_presence_get_presence_message (FOLKS_PRESENCE (individual)),
800           EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact,
801           EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, FALSE,
802           EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, now_online,
803           EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, FALSE,
804           EMPATHY_INDIVIDUAL_STORE_COL_CAN_AUDIO_CALL, can_audio_call,
805           EMPATHY_INDIVIDUAL_STORE_COL_CAN_VIDEO_CALL, can_video_call,
806           EMPATHY_INDIVIDUAL_STORE_COL_CLIENT_TYPES, types,
807           -1);
808     }
809
810   if (priv->show_active && do_set_active)
811     {
812       individual_store_contact_set_active (self, individual, do_set_active,
813           do_set_refresh);
814
815       if (do_set_active)
816         {
817           data =
818               individual_store_contact_active_new (self, individual,
819               do_remove);
820           data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
821               (GSourceFunc) individual_store_contact_active_cb, data);
822         }
823     }
824
825   /* FIXME: when someone goes online then offline quickly, the
826    * first timeout sets the user to be inactive and the second
827    * timeout removes the user from the contact list, really we
828    * should remove the first timeout.
829    */
830   free_iters (iters);
831 }
832
833 static void
834 individual_store_individual_updated_cb (FolksIndividual *individual,
835     GParamSpec *param,
836     EmpathyIndividualStore *self)
837 {
838   DEBUG ("Individual'%s' updated, checking roster is in sync...",
839       folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)));
840
841   individual_store_contact_update (self, individual);
842 }
843
844 static void
845 individual_store_contact_updated_cb (EmpathyContact *contact,
846     GParamSpec *pspec,
847     EmpathyIndividualStore *self)
848 {
849   FolksIndividual *individual;
850
851   DEBUG ("Contact '%s' updated, checking roster is in sync...",
852       empathy_contact_get_alias (contact));
853
854   individual = g_object_get_data (G_OBJECT (contact), "individual");
855   if (individual == NULL)
856     return;
857
858   individual_store_contact_update (self, individual);
859 }
860
861 static void
862 individual_personas_changed_cb (FolksIndividual *individual,
863     GList *added,
864     GList *removed,
865     EmpathyIndividualStore *self)
866 {
867   GList *l;
868
869   DEBUG ("Individual '%s' personas-changed.",
870       folks_individual_get_id (individual));
871
872   /* FIXME: libfolks hasn't grown capabilities support yet, so we have to go
873    * through the EmpathyContacts for them. */
874   for (l = removed; l != NULL; l = l->next)
875     {
876       TpContact *tp_contact;
877       EmpathyContact *contact;
878
879       if (!TPF_IS_PERSONA (l->data))
880         continue;
881
882       tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
883       contact = empathy_contact_dup_from_tp_contact (tp_contact);
884       empathy_contact_set_persona (contact, FOLKS_PERSONA (l->data));
885
886       g_object_set_data (G_OBJECT (contact), "individual", NULL);
887       g_signal_handlers_disconnect_by_func (contact,
888           (GCallback) individual_store_contact_updated_cb, self);
889
890       g_object_unref (contact);
891     }
892
893   for (l = added; l != NULL; l = l->next)
894     {
895       TpContact *tp_contact;
896       EmpathyContact *contact;
897
898       if (!TPF_IS_PERSONA (l->data))
899         continue;
900
901       tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
902       contact = empathy_contact_dup_from_tp_contact (tp_contact);
903       empathy_contact_set_persona (contact, FOLKS_PERSONA (l->data));
904
905       g_object_set_data (G_OBJECT (contact), "individual", individual);
906       g_signal_connect (contact, "notify::capabilities",
907           (GCallback) individual_store_contact_updated_cb, self);
908       g_signal_connect (contact, "notify::client-types",
909           (GCallback) individual_store_contact_updated_cb, self);
910
911       g_object_unref (contact);
912     }
913 }
914
915 static void
916 individual_store_add_individual_and_connect (EmpathyIndividualStore *self,
917     FolksIndividual *individual)
918 {
919   individual_store_add_individual (self, individual);
920
921   g_signal_connect (individual, "notify::avatar",
922       (GCallback) individual_store_individual_updated_cb, self);
923   g_signal_connect (individual, "notify::presence-type",
924       (GCallback) individual_store_individual_updated_cb, self);
925   g_signal_connect (individual, "notify::presence-message",
926       (GCallback) individual_store_individual_updated_cb, self);
927   g_signal_connect (individual, "notify::alias",
928       (GCallback) individual_store_individual_updated_cb, self);
929   g_signal_connect (individual, "personas-changed",
930       (GCallback) individual_personas_changed_cb, self);
931
932   individual_personas_changed_cb (individual,
933       folks_individual_get_personas (individual), NULL, self);
934 }
935
936 static void
937 individual_store_disconnect_individual (EmpathyIndividualStore *self,
938     FolksIndividual *individual)
939 {
940   individual_personas_changed_cb (individual, NULL,
941       folks_individual_get_personas (individual), self);
942
943   g_signal_handlers_disconnect_by_func (individual,
944       (GCallback) individual_store_individual_updated_cb, self);
945   g_signal_handlers_disconnect_by_func (individual,
946       (GCallback) individual_personas_changed_cb, self);
947 }
948
949 static void
950 individual_store_remove_individual_and_disconnect (
951     EmpathyIndividualStore *self,
952     FolksIndividual *individual)
953 {
954   individual_store_disconnect_individual (self, individual);
955   individual_store_remove_individual (self, individual);
956 }
957
958 static void
959 individual_store_members_changed_cb (EmpathyIndividualManager *manager,
960     const gchar *message,
961     GList *added,
962     GList *removed,
963     guint reason,
964     EmpathyIndividualStore *self)
965 {
966   GList *l;
967
968   for (l = added; l; l = l->next)
969     {
970       DEBUG ("Individual %s %s", folks_individual_get_id (l->data), "added");
971
972       individual_store_add_individual_and_connect (self, l->data);
973     }
974   for (l = removed; l; l = l->next)
975     {
976       DEBUG ("Individual %s %s",
977           folks_individual_get_id (l->data), "removed");
978
979       individual_store_remove_individual_and_disconnect (self, l->data);
980     }
981 }
982
983 static void
984 individual_store_favourites_changed_cb (EmpathyIndividualManager *manager,
985     FolksIndividual *individual,
986     gboolean is_favourite,
987     EmpathyIndividualStore *self)
988 {
989   EmpathyIndividualStorePriv *priv;
990
991   priv = GET_PRIV (self);
992
993   DEBUG ("Individual %s is %s a favourite",
994       folks_individual_get_id (individual),
995       is_favourite ? "now" : "no longer");
996
997   individual_store_remove_individual (self, individual);
998   individual_store_add_individual (self, individual);
999 }
1000
1001 static void
1002 individual_store_groups_changed_cb (EmpathyIndividualManager *manager,
1003     FolksIndividual *individual,
1004     gchar *group,
1005     gboolean is_member,
1006     EmpathyIndividualStore *self)
1007 {
1008   EmpathyIndividualStorePriv *priv;
1009   gboolean show_active;
1010
1011   priv = GET_PRIV (self);
1012
1013   DEBUG ("Updating groups for individual %s",
1014       folks_individual_get_id (individual));
1015
1016   /* We do this to make sure the groups are correct, if not, we
1017    * would have to check the groups already set up for each
1018    * contact and then see what has been updated.
1019    */
1020   show_active = priv->show_active;
1021   priv->show_active = FALSE;
1022   individual_store_remove_individual (self, individual);
1023   individual_store_add_individual (self, individual);
1024   priv->show_active = show_active;
1025 }
1026
1027 static gboolean
1028 individual_store_manager_setup (gpointer user_data)
1029 {
1030   EmpathyIndividualStore *self = user_data;
1031   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1032   GList *individuals;
1033
1034   /* Signal connection. */
1035
1036   /* TODO: implement */
1037   DEBUG ("handling individual renames unimplemented");
1038
1039   g_signal_connect (priv->manager,
1040       "members-changed",
1041       G_CALLBACK (individual_store_members_changed_cb), self);
1042
1043   g_signal_connect (priv->manager,
1044       "favourites-changed",
1045       G_CALLBACK (individual_store_favourites_changed_cb), self);
1046
1047   g_signal_connect (priv->manager,
1048       "groups-changed",
1049       G_CALLBACK (individual_store_groups_changed_cb), self);
1050
1051   /* Add contacts already created. */
1052   individuals = empathy_individual_manager_get_members (priv->manager);
1053   if (individuals != NULL && FOLKS_IS_INDIVIDUAL (individuals->data))
1054     {
1055       individual_store_members_changed_cb (priv->manager, "initial add",
1056           individuals, NULL, 0, self);
1057       g_list_free (individuals);
1058     }
1059
1060   priv->setup_idle_id = 0;
1061   return FALSE;
1062 }
1063
1064 static void
1065 individual_store_set_individual_manager (EmpathyIndividualStore *self,
1066     EmpathyIndividualManager *manager)
1067 {
1068   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
1069
1070   priv->manager = g_object_ref (manager);
1071
1072   /* Let a chance to have all properties set before populating */
1073   priv->setup_idle_id = g_idle_add (individual_store_manager_setup, self);
1074 }
1075
1076 static void
1077 individual_store_member_renamed_cb (EmpathyIndividualManager *manager,
1078     FolksIndividual *old_individual,
1079     FolksIndividual *new_individual,
1080     guint reason,
1081     const gchar *message,
1082     EmpathyIndividualStore *self)
1083 {
1084   EmpathyIndividualStorePriv *priv;
1085
1086   priv = GET_PRIV (self);
1087
1088   DEBUG ("Individual %s renamed to %s",
1089       folks_individual_get_id (old_individual),
1090       folks_individual_get_id (new_individual));
1091
1092   /* add the new contact */
1093   individual_store_add_individual_and_connect (self, new_individual);
1094
1095   /* remove old contact */
1096   individual_store_remove_individual_and_disconnect (self, old_individual);
1097 }
1098
1099 static void
1100 individual_store_dispose (GObject *object)
1101 {
1102   EmpathyIndividualStorePriv *priv = GET_PRIV (object);
1103   GList *individuals, *l;
1104
1105   if (priv->dispose_has_run)
1106     return;
1107   priv->dispose_has_run = TRUE;
1108
1109   /* Cancel any pending avatar load operations */
1110   for (l = priv->avatar_cancellables; l != NULL; l = l->next)
1111     {
1112       /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
1113       g_cancellable_cancel (G_CANCELLABLE (l->data));
1114     }
1115   g_list_free (priv->avatar_cancellables);
1116
1117   individuals = empathy_individual_manager_get_members (priv->manager);
1118   for (l = individuals; l; l = l->next)
1119     {
1120       individual_store_disconnect_individual (EMPATHY_INDIVIDUAL_STORE (object),
1121           FOLKS_INDIVIDUAL (l->data));
1122     }
1123   g_list_free (individuals);
1124
1125   g_signal_handlers_disconnect_by_func (priv->manager,
1126       G_CALLBACK (individual_store_member_renamed_cb), object);
1127   g_signal_handlers_disconnect_by_func (priv->manager,
1128       G_CALLBACK (individual_store_members_changed_cb), object);
1129   g_signal_handlers_disconnect_by_func (priv->manager,
1130       G_CALLBACK (individual_store_favourites_changed_cb), object);
1131   g_signal_handlers_disconnect_by_func (priv->manager,
1132       G_CALLBACK (individual_store_groups_changed_cb), object);
1133   g_object_unref (priv->manager);
1134
1135   if (priv->inhibit_active)
1136     {
1137       g_source_remove (priv->inhibit_active);
1138     }
1139
1140   if (priv->setup_idle_id != 0)
1141     {
1142       g_source_remove (priv->setup_idle_id);
1143     }
1144
1145   g_hash_table_destroy (priv->status_icons);
1146   G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
1147 }
1148
1149 static void
1150 individual_store_get_property (GObject *object,
1151     guint param_id,
1152     GValue *value,
1153     GParamSpec *pspec)
1154 {
1155   EmpathyIndividualStorePriv *priv;
1156
1157   priv = GET_PRIV (object);
1158
1159   switch (param_id)
1160     {
1161     case PROP_INDIVIDUAL_MANAGER:
1162       g_value_set_object (value, priv->manager);
1163       break;
1164     case PROP_SHOW_AVATARS:
1165       g_value_set_boolean (value, priv->show_avatars);
1166       break;
1167     case PROP_SHOW_PROTOCOLS:
1168       g_value_set_boolean (value, priv->show_protocols);
1169       break;
1170     case PROP_SHOW_GROUPS:
1171       g_value_set_boolean (value, priv->show_groups);
1172       break;
1173     case PROP_IS_COMPACT:
1174       g_value_set_boolean (value, priv->is_compact);
1175       break;
1176     case PROP_SORT_CRITERIUM:
1177       g_value_set_enum (value, priv->sort_criterium);
1178       break;
1179     default:
1180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1181       break;
1182     };
1183 }
1184
1185 static void
1186 individual_store_set_property (GObject *object,
1187     guint param_id,
1188     const GValue *value,
1189     GParamSpec *pspec)
1190 {
1191   EmpathyIndividualStorePriv *priv;
1192
1193   priv = GET_PRIV (object);
1194
1195   switch (param_id)
1196     {
1197     case PROP_INDIVIDUAL_MANAGER:
1198       individual_store_set_individual_manager (EMPATHY_INDIVIDUAL_STORE
1199           (object), g_value_get_object (value));
1200       break;
1201     case PROP_SHOW_AVATARS:
1202       empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
1203           (object), g_value_get_boolean (value));
1204       break;
1205     case PROP_SHOW_PROTOCOLS:
1206       empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1207           (object), g_value_get_boolean (value));
1208       break;
1209     case PROP_SHOW_GROUPS:
1210       empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1211           (object), g_value_get_boolean (value));
1212       break;
1213     case PROP_IS_COMPACT:
1214       empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1215           (object), g_value_get_boolean (value));
1216       break;
1217     case PROP_SORT_CRITERIUM:
1218       empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1219           (object), g_value_get_enum (value));
1220       break;
1221     default:
1222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1223       break;
1224     };
1225 }
1226
1227 static void
1228 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1229 {
1230   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1231
1232   object_class->dispose = individual_store_dispose;
1233   object_class->get_property = individual_store_get_property;
1234   object_class->set_property = individual_store_set_property;
1235
1236   g_object_class_install_property (object_class,
1237       PROP_INDIVIDUAL_MANAGER,
1238       g_param_spec_object ("individual-manager",
1239           "The individual manager",
1240           "The individual manager",
1241           EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1242           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1243   g_object_class_install_property (object_class,
1244       PROP_SHOW_AVATARS,
1245       g_param_spec_boolean ("show-avatars",
1246           "Show Avatars",
1247           "Whether contact list should display "
1248           "avatars for contacts", TRUE, G_PARAM_READWRITE));
1249   g_object_class_install_property (object_class,
1250       PROP_SHOW_PROTOCOLS,
1251       g_param_spec_boolean ("show-protocols",
1252           "Show Protocols",
1253           "Whether contact list should display "
1254           "protocols for contacts", FALSE, G_PARAM_READWRITE));
1255   g_object_class_install_property (object_class,
1256       PROP_SHOW_GROUPS,
1257       g_param_spec_boolean ("show-groups",
1258           "Show Groups",
1259           "Whether contact list should display "
1260           "contact groups", TRUE, G_PARAM_READWRITE));
1261   g_object_class_install_property (object_class,
1262       PROP_IS_COMPACT,
1263       g_param_spec_boolean ("is-compact",
1264           "Is Compact",
1265           "Whether the contact list is in compact mode or not",
1266           FALSE, G_PARAM_READWRITE));
1267
1268   g_object_class_install_property (object_class,
1269       PROP_SORT_CRITERIUM,
1270       g_param_spec_enum ("sort-criterium",
1271           "Sort citerium",
1272           "The sort criterium to use for sorting the contact list",
1273           EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1274           EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1275
1276   g_type_class_add_private (object_class,
1277       sizeof (EmpathyIndividualStorePriv));
1278 }
1279
1280 static gint
1281 get_position (const char **strv,
1282     const char *str)
1283 {
1284   int i;
1285
1286   for (i = 0; strv[i] != NULL; i++)
1287     {
1288       if (!tp_strdiff (strv[i], str))
1289         return i;
1290     }
1291
1292   return -1;
1293 }
1294
1295 static gint
1296 compare_separator_and_groups (gboolean is_separator_a,
1297     gboolean is_separator_b,
1298     const gchar *name_a,
1299     const gchar *name_b,
1300     FolksIndividual *individual_a,
1301     FolksIndividual *individual_b,
1302     gboolean fake_group_a,
1303     gboolean fake_group_b)
1304 {
1305   /* these two lists are the sorted list of fake groups to include at the
1306    * top and bottom of the roster */
1307   const char *top_groups[] = {
1308     EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1309     NULL
1310   };
1311
1312   const char *bottom_groups[] = {
1313     EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1314     NULL
1315   };
1316
1317   if (is_separator_a || is_separator_b)
1318     {
1319       /* We have at least one separator */
1320       if (is_separator_a)
1321         {
1322           return -1;
1323         }
1324       else if (is_separator_b)
1325         {
1326           return 1;
1327         }
1328     }
1329
1330   /* One group and one contact */
1331   if (!individual_a && individual_b)
1332     {
1333       return 1;
1334     }
1335   else if (individual_a && !individual_b)
1336     {
1337       return -1;
1338     }
1339   else if (!individual_a && !individual_b)
1340     {
1341       gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1342
1343       a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1344       b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1345       a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1346       b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1347
1348       if (a_in_top && b_in_top)
1349         {
1350           /* compare positions */
1351           return CLAMP (get_position (top_groups, name_a) -
1352               get_position (top_groups, name_b), -1, 1);
1353         }
1354       else if (a_in_bottom && b_in_bottom)
1355         {
1356           /* compare positions */
1357           return CLAMP (get_position (bottom_groups, name_a) -
1358               get_position (bottom_groups, name_b), -1, 1);
1359         }
1360       else if (a_in_top || b_in_bottom)
1361         {
1362           return -1;
1363         }
1364       else if (b_in_top || a_in_bottom)
1365         {
1366           return 1;
1367         }
1368       else
1369         {
1370           return g_utf8_collate (name_a, name_b);
1371         }
1372     }
1373
1374   /* Two contacts, ordering depends of the sorting policy */
1375   return 0;
1376 }
1377
1378 static gint
1379 individual_store_contact_sort (FolksIndividual *individual_a,
1380     FolksIndividual *individual_b)
1381 {
1382   gint ret_val;
1383   EmpathyContact *contact_a = NULL, *contact_b = NULL;
1384   TpAccount *account_a, *account_b;
1385
1386   g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1387
1388   /* alias */
1389   ret_val = g_utf8_collate (
1390       folks_aliasable_get_alias (FOLKS_ALIASABLE (individual_a)),
1391       folks_aliasable_get_alias (FOLKS_ALIASABLE (individual_b)));
1392
1393   if (ret_val != 0)
1394     goto out;
1395
1396   contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1397   contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1398   account_a = empathy_contact_get_account (contact_a);
1399   account_b = empathy_contact_get_account (contact_b);
1400
1401   g_assert (account_a != NULL);
1402   g_assert (account_b != NULL);
1403
1404   /* protocol */
1405   ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1406       tp_account_get_protocol (account_b));
1407
1408   if (ret_val != 0)
1409     goto out;
1410
1411   /* account ID */
1412   ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1413       tp_proxy_get_object_path (account_b));
1414
1415   if (ret_val != 0)
1416     goto out;
1417
1418   /* identifier */
1419   ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1420       folks_individual_get_id (individual_b));
1421
1422 out:
1423   tp_clear_object (&contact_a);
1424   tp_clear_object (&contact_b);
1425
1426   return ret_val;
1427 }
1428
1429 static gint
1430 individual_store_state_sort_func (GtkTreeModel *model,
1431     GtkTreeIter *iter_a,
1432     GtkTreeIter *iter_b,
1433     gpointer user_data)
1434 {
1435   gint ret_val;
1436   FolksIndividual *individual_a, *individual_b;
1437   gchar *name_a, *name_b;
1438   gboolean is_separator_a, is_separator_b;
1439   gboolean fake_group_a, fake_group_b;
1440   FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1441   TpConnectionPresenceType tp_presence_a, tp_presence_b;
1442
1443   gtk_tree_model_get (model, iter_a,
1444       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1445       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1446       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1447       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1448   gtk_tree_model_get (model, iter_b,
1449       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1450       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1451       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1452       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1453
1454   if (individual_a == NULL || individual_b == NULL)
1455     {
1456       ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1457           name_a, name_b, individual_a, individual_b, fake_group_a,
1458           fake_group_b);
1459       goto free_and_out;
1460     }
1461
1462   /* If we managed to get this far, we can start looking at
1463    * the presences.
1464    */
1465   folks_presence_type_a =
1466       folks_presence_get_presence_type (FOLKS_PRESENCE (individual_a));
1467   folks_presence_type_b =
1468       folks_presence_get_presence_type (FOLKS_PRESENCE (individual_b));
1469   tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1470   tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1471
1472   ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1473       tp_presence_b);
1474
1475   if (ret_val == 0)
1476     {
1477       /* Fallback: compare by name et al. */
1478       ret_val = individual_store_contact_sort (individual_a, individual_b);
1479     }
1480
1481 free_and_out:
1482   g_free (name_a);
1483   g_free (name_b);
1484   tp_clear_object (&individual_a);
1485   tp_clear_object (&individual_b);
1486
1487   return ret_val;
1488 }
1489
1490 static gint
1491 individual_store_name_sort_func (GtkTreeModel *model,
1492     GtkTreeIter *iter_a,
1493     GtkTreeIter *iter_b,
1494     gpointer user_data)
1495 {
1496   gchar *name_a, *name_b;
1497   FolksIndividual *individual_a, *individual_b;
1498   gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1499   gint ret_val;
1500   gboolean fake_group_a, fake_group_b;
1501
1502   gtk_tree_model_get (model, iter_a,
1503       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1504       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1505       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1506       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1507   gtk_tree_model_get (model, iter_b,
1508       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1509       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1510       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1511       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1512
1513   if (individual_a == NULL || individual_b == NULL)
1514     ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1515         name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1516   else
1517     ret_val = individual_store_contact_sort (individual_a, individual_b);
1518
1519   tp_clear_object (&individual_a);
1520   tp_clear_object (&individual_b);
1521   g_free (name_a);
1522   g_free (name_b);
1523
1524   return ret_val;
1525 }
1526
1527 static void
1528 individual_store_setup (EmpathyIndividualStore *self)
1529 {
1530   EmpathyIndividualStorePriv *priv;
1531   GType types[] = {
1532     GDK_TYPE_PIXBUF,            /* Status pixbuf */
1533     GDK_TYPE_PIXBUF,            /* Avatar pixbuf */
1534     G_TYPE_BOOLEAN,             /* Avatar pixbuf visible */
1535     G_TYPE_STRING,              /* Name */
1536     G_TYPE_UINT,                /* Presence type */
1537     G_TYPE_STRING,              /* Status string */
1538     G_TYPE_BOOLEAN,             /* Compact view */
1539     FOLKS_TYPE_INDIVIDUAL,      /* Individual type */
1540     G_TYPE_BOOLEAN,             /* Is group */
1541     G_TYPE_BOOLEAN,             /* Is active */
1542     G_TYPE_BOOLEAN,             /* Is online */
1543     G_TYPE_BOOLEAN,             /* Is separator */
1544     G_TYPE_BOOLEAN,             /* Can make audio calls */
1545     G_TYPE_BOOLEAN,             /* Can make video calls */
1546     G_TYPE_BOOLEAN,             /* Is a fake group */
1547     G_TYPE_STRV,                /* Client types */
1548   };
1549
1550   priv = GET_PRIV (self);
1551
1552   gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1553       EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1554
1555   /* Set up sorting */
1556   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1557       EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1558       individual_store_name_sort_func, self, NULL);
1559   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1560       EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1561       individual_store_state_sort_func, self, NULL);
1562
1563   priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1564   empathy_individual_store_set_sort_criterium (self, priv->sort_criterium);
1565 }
1566
1567 static gboolean
1568 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1569 {
1570   EmpathyIndividualStorePriv *priv;
1571
1572   priv = GET_PRIV (self);
1573
1574   priv->show_active = TRUE;
1575   priv->inhibit_active = 0;
1576
1577   return FALSE;
1578 }
1579
1580 static void
1581 empathy_individual_store_init (EmpathyIndividualStore *self)
1582 {
1583   EmpathyIndividualStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1584       EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1585
1586   self->priv = priv;
1587   priv->show_avatars = TRUE;
1588   priv->show_groups = TRUE;
1589   priv->show_protocols = FALSE;
1590   priv->inhibit_active =
1591       g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1592       (GSourceFunc) individual_store_inhibit_active_cb, self);
1593   priv->status_icons =
1594       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1595   individual_store_setup (self);
1596 }
1597
1598 EmpathyIndividualStore *
1599 empathy_individual_store_new (EmpathyIndividualManager *manager)
1600 {
1601   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1602
1603   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE,
1604       "individual-manager", manager, NULL);
1605 }
1606
1607 EmpathyIndividualManager *
1608 empathy_individual_store_get_manager (EmpathyIndividualStore *self)
1609 {
1610   EmpathyIndividualStorePriv *priv;
1611
1612   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), FALSE);
1613
1614   priv = GET_PRIV (self);
1615
1616   return priv->manager;
1617 }
1618
1619 gboolean
1620 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1621 {
1622   EmpathyIndividualStorePriv *priv;
1623
1624   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1625
1626   priv = GET_PRIV (self);
1627
1628   return priv->show_avatars;
1629 }
1630
1631 static gboolean
1632 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1633     GtkTreePath *path,
1634     GtkTreeIter *iter,
1635     EmpathyIndividualStore *self)
1636 {
1637   EmpathyIndividualStorePriv *priv;
1638   gboolean show_avatar = FALSE;
1639   FolksIndividual *individual;
1640   GdkPixbuf *pixbuf_status;
1641
1642   priv = GET_PRIV (self);
1643
1644   if (priv->show_avatars && !priv->is_compact)
1645     {
1646       show_avatar = TRUE;
1647     }
1648
1649   gtk_tree_model_get (model, iter,
1650       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1651
1652   if (individual == NULL)
1653     {
1654       return FALSE;
1655     }
1656   /* get icon from hash_table */
1657   pixbuf_status =
1658       empathy_individual_store_get_individual_status_icon (self, individual);
1659
1660   gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1661       EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1662       EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1663       EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact, -1);
1664
1665   g_object_unref (individual);
1666
1667   return FALSE;
1668 }
1669
1670 void
1671 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1672     gboolean show_avatars)
1673 {
1674   EmpathyIndividualStorePriv *priv;
1675   GtkTreeModel *model;
1676
1677   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1678
1679   priv = GET_PRIV (self);
1680
1681   priv->show_avatars = show_avatars;
1682
1683   model = GTK_TREE_MODEL (self);
1684
1685   gtk_tree_model_foreach (model,
1686       (GtkTreeModelForeachFunc)
1687       individual_store_update_list_mode_foreach, self);
1688
1689   g_object_notify (G_OBJECT (self), "show-avatars");
1690 }
1691
1692 gboolean
1693 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1694 {
1695   EmpathyIndividualStorePriv *priv;
1696
1697   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1698
1699   priv = GET_PRIV (self);
1700
1701   return priv->show_protocols;
1702 }
1703
1704 void
1705 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1706     gboolean show_protocols)
1707 {
1708   EmpathyIndividualStorePriv *priv;
1709   GtkTreeModel *model;
1710
1711   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1712
1713   priv = GET_PRIV (self);
1714
1715   priv->show_protocols = show_protocols;
1716
1717   model = GTK_TREE_MODEL (self);
1718
1719   gtk_tree_model_foreach (model,
1720       (GtkTreeModelForeachFunc)
1721       individual_store_update_list_mode_foreach, self);
1722
1723   g_object_notify (G_OBJECT (self), "show-protocols");
1724 }
1725
1726 gboolean
1727 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1728 {
1729   EmpathyIndividualStorePriv *priv;
1730
1731   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1732
1733   priv = GET_PRIV (self);
1734
1735   return priv->show_groups;
1736 }
1737
1738 void
1739 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1740     gboolean show_groups)
1741 {
1742   EmpathyIndividualStorePriv *priv;
1743
1744   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1745
1746   priv = GET_PRIV (self);
1747
1748   if (priv->show_groups == show_groups)
1749     {
1750       return;
1751     }
1752
1753   priv->show_groups = show_groups;
1754
1755   if (priv->setup_idle_id == 0)
1756     {
1757       /* Remove all contacts and add them back, not optimized but
1758        * that's the easy way :)
1759        *
1760        * This is only done if there's not a pending setup idle
1761        * callback, otherwise it will race and the contacts will get
1762        * added twice */
1763       GList *contacts;
1764
1765       gtk_tree_store_clear (GTK_TREE_STORE (self));
1766       contacts = empathy_individual_manager_get_members (priv->manager);
1767
1768       individual_store_members_changed_cb (priv->manager,
1769           "re-adding members: toggled group visibility",
1770           contacts, NULL, 0, self);
1771       g_list_free (contacts);
1772     }
1773
1774   g_object_notify (G_OBJECT (self), "show-groups");
1775 }
1776
1777 gboolean
1778 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1779 {
1780   EmpathyIndividualStorePriv *priv;
1781
1782   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1783
1784   priv = GET_PRIV (self);
1785
1786   return priv->is_compact;
1787 }
1788
1789 void
1790 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1791     gboolean is_compact)
1792 {
1793   EmpathyIndividualStorePriv *priv;
1794   GtkTreeModel *model;
1795
1796   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1797
1798   priv = GET_PRIV (self);
1799
1800   priv->is_compact = is_compact;
1801
1802   model = GTK_TREE_MODEL (self);
1803
1804   gtk_tree_model_foreach (model,
1805       (GtkTreeModelForeachFunc)
1806       individual_store_update_list_mode_foreach, self);
1807
1808   g_object_notify (G_OBJECT (self), "is-compact");
1809 }
1810
1811 EmpathyIndividualStoreSort
1812 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1813 {
1814   EmpathyIndividualStorePriv *priv;
1815
1816   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1817
1818   priv = GET_PRIV (self);
1819
1820   return priv->sort_criterium;
1821 }
1822
1823 void
1824 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1825     EmpathyIndividualStoreSort sort_criterium)
1826 {
1827   EmpathyIndividualStorePriv *priv;
1828
1829   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1830
1831   priv = GET_PRIV (self);
1832
1833   priv->sort_criterium = sort_criterium;
1834
1835   switch (sort_criterium)
1836     {
1837     case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1838       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1839           EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1840       break;
1841
1842     case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1843       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1844           EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1845       break;
1846
1847     default:
1848       g_assert_not_reached ();
1849     }
1850
1851   g_object_notify (G_OBJECT (self), "sort-criterium");
1852 }
1853
1854 gboolean
1855 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1856     GtkTreeIter *iter,
1857     gpointer data)
1858 {
1859   gboolean is_separator = FALSE;
1860
1861   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1862
1863   gtk_tree_model_get (model, iter,
1864       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1865
1866   return is_separator;
1867 }
1868
1869 gchar *
1870 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1871     GtkTreePath *path,
1872     gboolean *path_is_group,
1873     gboolean *is_fake_group)
1874 {
1875   GtkTreeIter parent_iter, iter;
1876   gchar *name = NULL;
1877   gboolean is_group;
1878   gboolean fake = FALSE;
1879
1880   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1881
1882   if (path_is_group)
1883     {
1884       *path_is_group = FALSE;
1885     }
1886
1887   if (!gtk_tree_model_get_iter (model, &iter, path))
1888     {
1889       return NULL;
1890     }
1891
1892   gtk_tree_model_get (model, &iter,
1893       EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1894       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1895
1896   if (!is_group)
1897     {
1898       g_free (name);
1899       name = NULL;
1900
1901       if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1902         {
1903           return NULL;
1904         }
1905
1906       iter = parent_iter;
1907
1908       gtk_tree_model_get (model, &iter,
1909           EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1910           EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1911           EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1912       if (!is_group)
1913         {
1914           g_free (name);
1915           return NULL;
1916         }
1917     }
1918
1919   if (path_is_group)
1920     {
1921       *path_is_group = TRUE;
1922     }
1923
1924   if (is_fake_group != NULL)
1925     *is_fake_group = fake;
1926
1927   return name;
1928 }
1929
1930 static GdkPixbuf *
1931 individual_store_get_individual_status_icon_with_icon_name (
1932     EmpathyIndividualStore *self,
1933     FolksIndividual *individual,
1934     const gchar *status_icon_name)
1935 {
1936   GdkPixbuf *pixbuf_status = NULL;
1937   EmpathyIndividualStorePriv *priv;
1938   const gchar *protocol_name = NULL;
1939   gchar *icon_name = NULL;
1940   GList *personas, *l;
1941   guint contact_count;
1942   EmpathyContact *contact = NULL;
1943   gboolean show_protocols_here;
1944
1945   priv = GET_PRIV (self);
1946
1947   personas = folks_individual_get_personas (individual);
1948   for (l = personas, contact_count = 0; l; l = l->next)
1949     {
1950       if (TPF_IS_PERSONA (l->data))
1951         contact_count++;
1952
1953       if (contact_count > 1)
1954         break;
1955     }
1956
1957   show_protocols_here = (priv->show_protocols && (contact_count == 1));
1958   if (show_protocols_here)
1959     {
1960       contact = empathy_contact_dup_from_folks_individual (individual);
1961       protocol_name = empathy_protocol_name_for_contact (contact);
1962       icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
1963     }
1964   else
1965     {
1966       icon_name = g_strdup_printf ("%s", status_icon_name);
1967     }
1968   if (pixbuf_status == NULL)
1969     {
1970       pixbuf_status =
1971           empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1972           status_icon_name, show_protocols_here);
1973       if (pixbuf_status != NULL)
1974         {
1975           g_hash_table_insert (priv->status_icons,
1976               g_strdup (icon_name), pixbuf_status);
1977         }
1978     }
1979
1980   g_free (icon_name);
1981   tp_clear_object (&contact);
1982
1983   return pixbuf_status;
1984 }
1985
1986 GdkPixbuf *
1987 empathy_individual_store_get_individual_status_icon (
1988     EmpathyIndividualStore *self,
1989     FolksIndividual *individual)
1990 {
1991   GdkPixbuf *pixbuf_status = NULL;
1992   const gchar *status_icon_name = NULL;
1993
1994   status_icon_name = empathy_icon_name_for_individual (individual);
1995   if (status_icon_name == NULL)
1996     return NULL;
1997
1998   pixbuf_status =
1999       individual_store_get_individual_status_icon_with_icon_name (self,
2000       individual, status_icon_name);
2001
2002   return pixbuf_status;
2003 }