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