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