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