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