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