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