]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-store.c
Allow cancellation of avatar load operations
[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   EmpathyContact *contact;
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 EmpathyContact for them. */
818   contact = empathy_contact_dup_from_folks_individual (individual);
819   g_object_set_data (G_OBJECT (contact), "individual", individual);
820   g_signal_connect (contact, "notify::capabilities",
821       G_CALLBACK (individual_store_contact_updated_cb), self);
822   g_object_unref (contact);
823
824   individual_store_add_individual (self, individual);
825 }
826
827 static void
828 individual_store_remove_individual_and_disconnect (
829     EmpathyIndividualStore *self,
830     FolksIndividual *individual)
831 {
832   g_signal_handlers_disconnect_by_func (individual,
833       G_CALLBACK (individual_store_individual_updated_cb), self);
834   g_signal_handlers_disconnect_by_func (individual,
835       G_CALLBACK (individual_store_contact_updated_cb), self);
836
837   individual_store_remove_individual (self, individual);
838 }
839
840 static void
841 individual_store_members_changed_cb (EmpathyIndividualManager *manager,
842     const gchar *message,
843     GList *added,
844     GList *removed,
845     guint reason,
846     EmpathyIndividualStore *self)
847 {
848   GList *l;
849
850   for (l = added; l; l = l->next)
851     {
852       DEBUG ("Individual %s %s", folks_individual_get_id (l->data), "added");
853
854       individual_store_add_individual_and_connect (self, l->data);
855     }
856   for (l = removed; l; l = l->next)
857     {
858       DEBUG ("Individual %s %s",
859           folks_individual_get_id (l->data), "removed");
860
861       individual_store_remove_individual_and_disconnect (self, l->data);
862     }
863 }
864
865 static void
866 individual_store_favourites_changed_cb (EmpathyIndividualManager *manager,
867     FolksIndividual *individual,
868     gboolean is_favourite,
869     EmpathyIndividualStore *self)
870 {
871   EmpathyIndividualStorePriv *priv;
872
873   priv = GET_PRIV (self);
874
875   DEBUG ("Individual %s is %s a favourite",
876       folks_individual_get_id (individual),
877       is_favourite ? "now" : "no longer");
878
879   individual_store_remove_individual (self, individual);
880   individual_store_add_individual (self, individual);
881 }
882
883 static void
884 individual_store_groups_changed_cb (EmpathyIndividualManager *manager,
885     FolksIndividual *individual,
886     gchar *group,
887     gboolean is_member,
888     EmpathyIndividualStore *self)
889 {
890   EmpathyIndividualStorePriv *priv;
891   gboolean show_active;
892
893   priv = GET_PRIV (self);
894
895   DEBUG ("Updating groups for individual %s",
896       folks_individual_get_id (individual));
897
898   /* We do this to make sure the groups are correct, if not, we
899    * would have to check the groups already set up for each
900    * contact and then see what has been updated.
901    */
902   show_active = priv->show_active;
903   priv->show_active = FALSE;
904   individual_store_remove_individual (self, individual);
905   individual_store_add_individual (self, individual);
906   priv->show_active = show_active;
907 }
908
909 static gboolean
910 individual_store_manager_setup (gpointer user_data)
911 {
912   EmpathyIndividualStore *self = user_data;
913   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
914   GList *individuals;
915
916   /* Signal connection. */
917
918   /* TODO: implement */
919   DEBUG ("handling individual renames unimplemented");
920
921   g_signal_connect (priv->manager,
922       "members-changed",
923       G_CALLBACK (individual_store_members_changed_cb), self);
924
925   g_signal_connect (priv->manager,
926       "favourites-changed",
927       G_CALLBACK (individual_store_favourites_changed_cb), self);
928
929   g_signal_connect (priv->manager,
930       "groups-changed",
931       G_CALLBACK (individual_store_groups_changed_cb), self);
932
933   /* Add contacts already created. */
934   individuals = empathy_individual_manager_get_members (priv->manager);
935   if (individuals != NULL && FOLKS_IS_INDIVIDUAL (individuals->data))
936     {
937       individual_store_members_changed_cb (priv->manager, "initial add",
938           individuals, NULL, 0, self);
939       g_list_free (individuals);
940     }
941
942   priv->setup_idle_id = 0;
943   return FALSE;
944 }
945
946 static void
947 individual_store_set_individual_manager (EmpathyIndividualStore *self,
948     EmpathyIndividualManager *manager)
949 {
950   EmpathyIndividualStorePriv *priv = GET_PRIV (self);
951
952   priv->manager = g_object_ref (manager);
953
954   /* Let a chance to have all properties set before populating */
955   priv->setup_idle_id = g_idle_add (individual_store_manager_setup, self);
956 }
957
958 static void
959 individual_store_member_renamed_cb (EmpathyIndividualManager *manager,
960     FolksIndividual *old_individual,
961     FolksIndividual *new_individual,
962     guint reason,
963     const gchar *message,
964     EmpathyIndividualStore *self)
965 {
966   EmpathyIndividualStorePriv *priv;
967
968   priv = GET_PRIV (self);
969
970   DEBUG ("Individual %s renamed to %s",
971       folks_individual_get_id (old_individual),
972       folks_individual_get_id (new_individual));
973
974   /* add the new contact */
975   individual_store_add_individual_and_connect (self, new_individual);
976
977   /* remove old contact */
978   individual_store_remove_individual_and_disconnect (self, old_individual);
979 }
980
981 static void
982 individual_store_dispose (GObject *object)
983 {
984   EmpathyIndividualStorePriv *priv = GET_PRIV (object);
985   GList *contacts, *l;
986
987   if (priv->dispose_has_run)
988     return;
989   priv->dispose_has_run = TRUE;
990
991   /* Cancel any pending avatar load operations */
992   for (l = priv->avatar_cancellables; l != NULL; l = l->next)
993     {
994       /* The cancellables are freed in individual_avatar_pixbuf_received_cb() */
995       g_cancellable_cancel (G_CANCELLABLE (l->data));
996     }
997   g_list_free (priv->avatar_cancellables);
998
999   contacts = empathy_individual_manager_get_members (priv->manager);
1000   for (l = contacts; l; l = l->next)
1001     {
1002       g_signal_handlers_disconnect_by_func (l->data,
1003           G_CALLBACK (individual_store_individual_updated_cb), object);
1004       g_signal_handlers_disconnect_by_func (l->data,
1005           G_CALLBACK (individual_store_contact_updated_cb), object);
1006     }
1007   g_list_free (contacts);
1008
1009   g_signal_handlers_disconnect_by_func (priv->manager,
1010       G_CALLBACK (individual_store_member_renamed_cb), object);
1011   g_signal_handlers_disconnect_by_func (priv->manager,
1012       G_CALLBACK (individual_store_members_changed_cb), object);
1013   g_signal_handlers_disconnect_by_func (priv->manager,
1014       G_CALLBACK (individual_store_favourites_changed_cb), object);
1015   g_signal_handlers_disconnect_by_func (priv->manager,
1016       G_CALLBACK (individual_store_groups_changed_cb), object);
1017   g_object_unref (priv->manager);
1018
1019   if (priv->inhibit_active)
1020     {
1021       g_source_remove (priv->inhibit_active);
1022     }
1023
1024   if (priv->setup_idle_id != 0)
1025     {
1026       g_source_remove (priv->setup_idle_id);
1027     }
1028
1029   g_hash_table_destroy (priv->status_icons);
1030   G_OBJECT_CLASS (empathy_individual_store_parent_class)->dispose (object);
1031 }
1032
1033 static void
1034 individual_store_get_property (GObject *object,
1035     guint param_id,
1036     GValue *value,
1037     GParamSpec *pspec)
1038 {
1039   EmpathyIndividualStorePriv *priv;
1040
1041   priv = GET_PRIV (object);
1042
1043   switch (param_id)
1044     {
1045     case PROP_INDIVIDUAL_MANAGER:
1046       g_value_set_object (value, priv->manager);
1047       break;
1048     case PROP_SHOW_AVATARS:
1049       g_value_set_boolean (value, priv->show_avatars);
1050       break;
1051     case PROP_SHOW_PROTOCOLS:
1052       g_value_set_boolean (value, priv->show_protocols);
1053       break;
1054     case PROP_SHOW_GROUPS:
1055       g_value_set_boolean (value, priv->show_groups);
1056       break;
1057     case PROP_IS_COMPACT:
1058       g_value_set_boolean (value, priv->is_compact);
1059       break;
1060     case PROP_SORT_CRITERIUM:
1061       g_value_set_enum (value, priv->sort_criterium);
1062       break;
1063     default:
1064       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1065       break;
1066     };
1067 }
1068
1069 static void
1070 individual_store_set_property (GObject *object,
1071     guint param_id,
1072     const GValue *value,
1073     GParamSpec *pspec)
1074 {
1075   EmpathyIndividualStorePriv *priv;
1076
1077   priv = GET_PRIV (object);
1078
1079   switch (param_id)
1080     {
1081     case PROP_INDIVIDUAL_MANAGER:
1082       individual_store_set_individual_manager (EMPATHY_INDIVIDUAL_STORE
1083           (object), g_value_get_object (value));
1084       break;
1085     case PROP_SHOW_AVATARS:
1086       empathy_individual_store_set_show_avatars (EMPATHY_INDIVIDUAL_STORE
1087           (object), g_value_get_boolean (value));
1088       break;
1089     case PROP_SHOW_PROTOCOLS:
1090       empathy_individual_store_set_show_protocols (EMPATHY_INDIVIDUAL_STORE
1091           (object), g_value_get_boolean (value));
1092       break;
1093     case PROP_SHOW_GROUPS:
1094       empathy_individual_store_set_show_groups (EMPATHY_INDIVIDUAL_STORE
1095           (object), g_value_get_boolean (value));
1096       break;
1097     case PROP_IS_COMPACT:
1098       empathy_individual_store_set_is_compact (EMPATHY_INDIVIDUAL_STORE
1099           (object), g_value_get_boolean (value));
1100       break;
1101     case PROP_SORT_CRITERIUM:
1102       empathy_individual_store_set_sort_criterium (EMPATHY_INDIVIDUAL_STORE
1103           (object), g_value_get_enum (value));
1104       break;
1105     default:
1106       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1107       break;
1108     };
1109 }
1110
1111 static void
1112 empathy_individual_store_class_init (EmpathyIndividualStoreClass *klass)
1113 {
1114   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1115
1116   object_class->dispose = individual_store_dispose;
1117   object_class->get_property = individual_store_get_property;
1118   object_class->set_property = individual_store_set_property;
1119
1120   g_object_class_install_property (object_class,
1121       PROP_INDIVIDUAL_MANAGER,
1122       g_param_spec_object ("individual-manager",
1123           "The individual manager",
1124           "The individual manager",
1125           EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1126           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
1127   g_object_class_install_property (object_class,
1128       PROP_SHOW_AVATARS,
1129       g_param_spec_boolean ("show-avatars",
1130           "Show Avatars",
1131           "Whether contact list should display "
1132           "avatars for contacts", TRUE, G_PARAM_READWRITE));
1133   g_object_class_install_property (object_class,
1134       PROP_SHOW_PROTOCOLS,
1135       g_param_spec_boolean ("show-protocols",
1136           "Show Protocols",
1137           "Whether contact list should display "
1138           "protocols for contacts", FALSE, G_PARAM_READWRITE));
1139   g_object_class_install_property (object_class,
1140       PROP_SHOW_GROUPS,
1141       g_param_spec_boolean ("show-groups",
1142           "Show Groups",
1143           "Whether contact list should display "
1144           "contact groups", TRUE, G_PARAM_READWRITE));
1145   g_object_class_install_property (object_class,
1146       PROP_IS_COMPACT,
1147       g_param_spec_boolean ("is-compact",
1148           "Is Compact",
1149           "Whether the contact list is in compact mode or not",
1150           FALSE, G_PARAM_READWRITE));
1151
1152   g_object_class_install_property (object_class,
1153       PROP_SORT_CRITERIUM,
1154       g_param_spec_enum ("sort-criterium",
1155           "Sort citerium",
1156           "The sort criterium to use for sorting the contact list",
1157           EMPATHY_TYPE_INDIVIDUAL_STORE_SORT,
1158           EMPATHY_INDIVIDUAL_STORE_SORT_NAME, G_PARAM_READWRITE));
1159
1160   g_type_class_add_private (object_class,
1161       sizeof (EmpathyIndividualStorePriv));
1162 }
1163
1164 static gint
1165 get_position (const char **strv,
1166     const char *str)
1167 {
1168   int i;
1169
1170   for (i = 0; strv[i] != NULL; i++)
1171     {
1172       if (!tp_strdiff (strv[i], str))
1173         return i;
1174     }
1175
1176   return -1;
1177 }
1178
1179 static gint
1180 compare_separator_and_groups (gboolean is_separator_a,
1181     gboolean is_separator_b,
1182     const gchar *name_a,
1183     const gchar *name_b,
1184     FolksIndividual *individual_a,
1185     FolksIndividual *individual_b,
1186     gboolean fake_group_a,
1187     gboolean fake_group_b)
1188 {
1189   /* these two lists are the sorted list of fake groups to include at the
1190    * top and bottom of the roster */
1191   const char *top_groups[] = {
1192     EMPATHY_INDIVIDUAL_STORE_FAVORITE,
1193     NULL
1194   };
1195
1196   const char *bottom_groups[] = {
1197     EMPATHY_INDIVIDUAL_STORE_UNGROUPED,
1198     NULL
1199   };
1200
1201   if (is_separator_a || is_separator_b)
1202     {
1203       /* We have at least one separator */
1204       if (is_separator_a)
1205         {
1206           return -1;
1207         }
1208       else if (is_separator_b)
1209         {
1210           return 1;
1211         }
1212     }
1213
1214   /* One group and one contact */
1215   if (!individual_a && individual_b)
1216     {
1217       return 1;
1218     }
1219   else if (individual_a && !individual_b)
1220     {
1221       return -1;
1222     }
1223   else if (!individual_a && !individual_b)
1224     {
1225       gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
1226
1227       a_in_top = fake_group_a && tp_strv_contains (top_groups, name_a);
1228       b_in_top = fake_group_b && tp_strv_contains (top_groups, name_b);
1229       a_in_bottom = fake_group_a && tp_strv_contains (bottom_groups, name_a);
1230       b_in_bottom = fake_group_b && tp_strv_contains (bottom_groups, name_b);
1231
1232       if (a_in_top && b_in_top)
1233         {
1234           /* compare positions */
1235           return CLAMP (get_position (top_groups, name_a) -
1236               get_position (top_groups, name_b), -1, 1);
1237         }
1238       else if (a_in_bottom && b_in_bottom)
1239         {
1240           /* compare positions */
1241           return CLAMP (get_position (bottom_groups, name_a) -
1242               get_position (bottom_groups, name_b), -1, 1);
1243         }
1244       else if (a_in_top || b_in_bottom)
1245         {
1246           return -1;
1247         }
1248       else if (b_in_top || a_in_bottom)
1249         {
1250           return 1;
1251         }
1252       else
1253         {
1254           return g_utf8_collate (name_a, name_b);
1255         }
1256     }
1257
1258   /* Two contacts, ordering depends of the sorting policy */
1259   return 0;
1260 }
1261
1262 static gint
1263 individual_store_contact_sort (FolksIndividual *individual_a,
1264     FolksIndividual *individual_b)
1265 {
1266   gint ret_val;
1267   EmpathyContact *contact_a = NULL, *contact_b = NULL;
1268   TpAccount *account_a, *account_b;
1269
1270   g_return_val_if_fail (individual_a != NULL || individual_b != NULL, 0);
1271
1272   /* alias */
1273   ret_val = g_utf8_collate (folks_individual_get_alias (individual_a),
1274       folks_individual_get_alias (individual_b));
1275
1276   if (ret_val != 0)
1277     goto out;
1278
1279   contact_a = empathy_contact_dup_from_folks_individual (individual_a);
1280   contact_b = empathy_contact_dup_from_folks_individual (individual_b);
1281   account_a = empathy_contact_get_account (contact_a);
1282   account_b = empathy_contact_get_account (contact_b);
1283
1284   /* protocol */
1285   ret_val = g_strcmp0 (tp_account_get_protocol (account_a),
1286       tp_account_get_protocol (account_b));
1287
1288   if (ret_val != 0)
1289     goto out;
1290
1291   /* account ID */
1292   ret_val = g_strcmp0 (tp_proxy_get_object_path (account_a),
1293       tp_proxy_get_object_path (account_b));
1294
1295   if (ret_val != 0)
1296     goto out;
1297
1298   /* identifier */
1299   ret_val = g_utf8_collate (folks_individual_get_id (individual_a),
1300       folks_individual_get_id (individual_b));
1301
1302 out:
1303   tp_clear_object (&contact_a);
1304   tp_clear_object (&contact_b);
1305
1306   return ret_val;
1307 }
1308
1309 static gint
1310 individual_store_state_sort_func (GtkTreeModel *model,
1311     GtkTreeIter *iter_a,
1312     GtkTreeIter *iter_b,
1313     gpointer user_data)
1314 {
1315   gint ret_val;
1316   FolksIndividual *individual_a, *individual_b;
1317   gchar *name_a, *name_b;
1318   gboolean is_separator_a, is_separator_b;
1319   gboolean fake_group_a, fake_group_b;
1320   FolksPresenceType folks_presence_type_a, folks_presence_type_b;
1321   TpConnectionPresenceType tp_presence_a, tp_presence_b;
1322
1323   gtk_tree_model_get (model, iter_a,
1324       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1325       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1326       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1327       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1328   gtk_tree_model_get (model, iter_b,
1329       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1330       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1331       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1332       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1333
1334   if (individual_a == NULL || individual_b == NULL)
1335     {
1336       ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1337           name_a, name_b, individual_a, individual_b, fake_group_a,
1338           fake_group_b);
1339       goto free_and_out;
1340     }
1341
1342   /* If we managed to get this far, we can start looking at
1343    * the presences.
1344    */
1345   folks_presence_type_a = folks_individual_get_presence_type (individual_a);
1346   folks_presence_type_b = folks_individual_get_presence_type (individual_b);
1347   tp_presence_a = empathy_folks_presence_type_to_tp (folks_presence_type_a);
1348   tp_presence_b = empathy_folks_presence_type_to_tp (folks_presence_type_b);
1349
1350   ret_val = -tp_connection_presence_type_cmp_availability (tp_presence_a,
1351       tp_presence_b);
1352
1353   if (ret_val == 0)
1354     {
1355       /* Fallback: compare by name et al. */
1356       ret_val = individual_store_contact_sort (individual_a, individual_b);
1357     }
1358
1359 free_and_out:
1360   g_free (name_a);
1361   g_free (name_b);
1362   tp_clear_object (&individual_a);
1363   tp_clear_object (&individual_b);
1364
1365   return ret_val;
1366 }
1367
1368 static gint
1369 individual_store_name_sort_func (GtkTreeModel *model,
1370     GtkTreeIter *iter_a,
1371     GtkTreeIter *iter_b,
1372     gpointer user_data)
1373 {
1374   gchar *name_a, *name_b;
1375   FolksIndividual *individual_a, *individual_b;
1376   gboolean is_separator_a = FALSE, is_separator_b = FALSE;
1377   gint ret_val;
1378   gboolean fake_group_a, fake_group_b;
1379
1380   gtk_tree_model_get (model, iter_a,
1381       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_a,
1382       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_a,
1383       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_a,
1384       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_a, -1);
1385   gtk_tree_model_get (model, iter_b,
1386       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name_b,
1387       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual_b,
1388       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator_b,
1389       EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake_group_b, -1);
1390
1391   if (individual_a == NULL || individual_b == NULL)
1392     ret_val = compare_separator_and_groups (is_separator_a, is_separator_b,
1393         name_a, name_b, individual_a, individual_b, fake_group_a, fake_group_b);
1394   else
1395     ret_val = individual_store_contact_sort (individual_a, individual_b);
1396
1397   tp_clear_object (&individual_a);
1398   tp_clear_object (&individual_b);
1399
1400   return ret_val;
1401 }
1402
1403 static void
1404 individual_store_setup (EmpathyIndividualStore *self)
1405 {
1406   EmpathyIndividualStorePriv *priv;
1407   GType types[] = {
1408     GDK_TYPE_PIXBUF,            /* Status pixbuf */
1409     GDK_TYPE_PIXBUF,            /* Avatar pixbuf */
1410     G_TYPE_BOOLEAN,             /* Avatar pixbuf visible */
1411     G_TYPE_STRING,              /* Name */
1412     G_TYPE_UINT,                /* Presence type */
1413     G_TYPE_STRING,              /* Status string */
1414     G_TYPE_BOOLEAN,             /* Compact view */
1415     FOLKS_TYPE_INDIVIDUAL,      /* Individual type */
1416     G_TYPE_BOOLEAN,             /* Is group */
1417     G_TYPE_BOOLEAN,             /* Is active */
1418     G_TYPE_BOOLEAN,             /* Is online */
1419     G_TYPE_BOOLEAN,             /* Is separator */
1420     G_TYPE_BOOLEAN,             /* Can make audio calls */
1421     G_TYPE_BOOLEAN,             /* Can make video calls */
1422     EMPATHY_TYPE_INDIVIDUAL_MANAGER_FLAGS,      /* Flags */
1423     G_TYPE_BOOLEAN,             /* Is a fake group */
1424   };
1425
1426   priv = GET_PRIV (self);
1427
1428   gtk_tree_store_set_column_types (GTK_TREE_STORE (self),
1429       EMPATHY_INDIVIDUAL_STORE_COL_COUNT, types);
1430
1431   /* Set up sorting */
1432   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1433       EMPATHY_INDIVIDUAL_STORE_COL_NAME,
1434       individual_store_name_sort_func, self, NULL);
1435   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
1436       EMPATHY_INDIVIDUAL_STORE_COL_STATUS,
1437       individual_store_state_sort_func, self, NULL);
1438
1439   priv->sort_criterium = EMPATHY_INDIVIDUAL_STORE_SORT_NAME;
1440   empathy_individual_store_set_sort_criterium (self, priv->sort_criterium);
1441 }
1442
1443 static gboolean
1444 individual_store_inhibit_active_cb (EmpathyIndividualStore *self)
1445 {
1446   EmpathyIndividualStorePriv *priv;
1447
1448   priv = GET_PRIV (self);
1449
1450   priv->show_active = TRUE;
1451   priv->inhibit_active = 0;
1452
1453   return FALSE;
1454 }
1455
1456 static void
1457 empathy_individual_store_init (EmpathyIndividualStore *self)
1458 {
1459   EmpathyIndividualStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1460       EMPATHY_TYPE_INDIVIDUAL_STORE, EmpathyIndividualStorePriv);
1461
1462   self->priv = priv;
1463   priv->show_avatars = TRUE;
1464   priv->show_groups = TRUE;
1465   priv->show_protocols = FALSE;
1466   priv->inhibit_active =
1467       g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
1468       (GSourceFunc) individual_store_inhibit_active_cb, self);
1469   priv->status_icons =
1470       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1471   individual_store_setup (self);
1472 }
1473
1474 EmpathyIndividualStore *
1475 empathy_individual_store_new (EmpathyIndividualManager *manager)
1476 {
1477   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1478
1479   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE,
1480       "individual-manager", manager, NULL);
1481 }
1482
1483 EmpathyIndividualManager *
1484 empathy_individual_store_get_manager (EmpathyIndividualStore *self)
1485 {
1486   EmpathyIndividualStorePriv *priv;
1487
1488   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), FALSE);
1489
1490   priv = GET_PRIV (self);
1491
1492   return priv->manager;
1493 }
1494
1495 gboolean
1496 empathy_individual_store_get_show_avatars (EmpathyIndividualStore *self)
1497 {
1498   EmpathyIndividualStorePriv *priv;
1499
1500   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1501
1502   priv = GET_PRIV (self);
1503
1504   return priv->show_avatars;
1505 }
1506
1507 static gboolean
1508 individual_store_update_list_mode_foreach (GtkTreeModel *model,
1509     GtkTreePath *path,
1510     GtkTreeIter *iter,
1511     EmpathyIndividualStore *self)
1512 {
1513   EmpathyIndividualStorePriv *priv;
1514   gboolean show_avatar = FALSE;
1515   FolksIndividual *individual;
1516   GdkPixbuf *pixbuf_status;
1517
1518   priv = GET_PRIV (self);
1519
1520   if (priv->show_avatars && !priv->is_compact)
1521     {
1522       show_avatar = TRUE;
1523     }
1524
1525   gtk_tree_model_get (model, iter,
1526       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual, -1);
1527
1528   if (individual == NULL)
1529     {
1530       return FALSE;
1531     }
1532   /* get icon from hash_table */
1533   pixbuf_status =
1534       empathy_individual_store_get_individual_status_icon (self, individual);
1535
1536   gtk_tree_store_set (GTK_TREE_STORE (self), iter,
1537       EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf_status,
1538       EMPATHY_INDIVIDUAL_STORE_COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1539       EMPATHY_INDIVIDUAL_STORE_COL_COMPACT, priv->is_compact, -1);
1540
1541   g_object_unref (individual);
1542
1543   return FALSE;
1544 }
1545
1546 void
1547 empathy_individual_store_set_show_avatars (EmpathyIndividualStore *self,
1548     gboolean show_avatars)
1549 {
1550   EmpathyIndividualStorePriv *priv;
1551   GtkTreeModel *model;
1552
1553   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1554
1555   priv = GET_PRIV (self);
1556
1557   priv->show_avatars = show_avatars;
1558
1559   model = GTK_TREE_MODEL (self);
1560
1561   gtk_tree_model_foreach (model,
1562       (GtkTreeModelForeachFunc)
1563       individual_store_update_list_mode_foreach, self);
1564
1565   g_object_notify (G_OBJECT (self), "show-avatars");
1566 }
1567
1568 gboolean
1569 empathy_individual_store_get_show_protocols (EmpathyIndividualStore *self)
1570 {
1571   EmpathyIndividualStorePriv *priv;
1572
1573   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1574
1575   priv = GET_PRIV (self);
1576
1577   return priv->show_protocols;
1578 }
1579
1580 void
1581 empathy_individual_store_set_show_protocols (EmpathyIndividualStore *self,
1582     gboolean show_protocols)
1583 {
1584   EmpathyIndividualStorePriv *priv;
1585   GtkTreeModel *model;
1586
1587   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1588
1589   priv = GET_PRIV (self);
1590
1591   priv->show_protocols = show_protocols;
1592
1593   model = GTK_TREE_MODEL (self);
1594
1595   gtk_tree_model_foreach (model,
1596       (GtkTreeModelForeachFunc)
1597       individual_store_update_list_mode_foreach, self);
1598
1599   g_object_notify (G_OBJECT (self), "show-protocols");
1600 }
1601
1602 gboolean
1603 empathy_individual_store_get_show_groups (EmpathyIndividualStore *self)
1604 {
1605   EmpathyIndividualStorePriv *priv;
1606
1607   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1608
1609   priv = GET_PRIV (self);
1610
1611   return priv->show_groups;
1612 }
1613
1614 void
1615 empathy_individual_store_set_show_groups (EmpathyIndividualStore *self,
1616     gboolean show_groups)
1617 {
1618   EmpathyIndividualStorePriv *priv;
1619
1620   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1621
1622   priv = GET_PRIV (self);
1623
1624   if (priv->show_groups == show_groups)
1625     {
1626       return;
1627     }
1628
1629   priv->show_groups = show_groups;
1630
1631   if (priv->setup_idle_id == 0)
1632     {
1633       /* Remove all contacts and add them back, not optimized but
1634        * that's the easy way :)
1635        *
1636        * This is only done if there's not a pending setup idle
1637        * callback, otherwise it will race and the contacts will get
1638        * added twice */
1639       GList *contacts;
1640
1641       gtk_tree_store_clear (GTK_TREE_STORE (self));
1642       contacts = empathy_individual_manager_get_members (priv->manager);
1643
1644       individual_store_members_changed_cb (priv->manager,
1645           "re-adding members: toggled group visibility",
1646           contacts, NULL, 0, self);
1647       g_list_free (contacts);
1648     }
1649
1650   g_object_notify (G_OBJECT (self), "show-groups");
1651 }
1652
1653 gboolean
1654 empathy_individual_store_get_is_compact (EmpathyIndividualStore *self)
1655 {
1656   EmpathyIndividualStorePriv *priv;
1657
1658   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), TRUE);
1659
1660   priv = GET_PRIV (self);
1661
1662   return priv->is_compact;
1663 }
1664
1665 void
1666 empathy_individual_store_set_is_compact (EmpathyIndividualStore *self,
1667     gboolean is_compact)
1668 {
1669   EmpathyIndividualStorePriv *priv;
1670   GtkTreeModel *model;
1671
1672   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1673
1674   priv = GET_PRIV (self);
1675
1676   priv->is_compact = is_compact;
1677
1678   model = GTK_TREE_MODEL (self);
1679
1680   gtk_tree_model_foreach (model,
1681       (GtkTreeModelForeachFunc)
1682       individual_store_update_list_mode_foreach, self);
1683
1684   g_object_notify (G_OBJECT (self), "is-compact");
1685 }
1686
1687 EmpathyIndividualStoreSort
1688 empathy_individual_store_get_sort_criterium (EmpathyIndividualStore *self)
1689 {
1690   EmpathyIndividualStorePriv *priv;
1691
1692   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self), 0);
1693
1694   priv = GET_PRIV (self);
1695
1696   return priv->sort_criterium;
1697 }
1698
1699 void
1700 empathy_individual_store_set_sort_criterium (EmpathyIndividualStore *self,
1701     EmpathyIndividualStoreSort sort_criterium)
1702 {
1703   EmpathyIndividualStorePriv *priv;
1704
1705   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_STORE (self));
1706
1707   priv = GET_PRIV (self);
1708
1709   priv->sort_criterium = sort_criterium;
1710
1711   switch (sort_criterium)
1712     {
1713     case EMPATHY_INDIVIDUAL_STORE_SORT_STATE:
1714       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1715           EMPATHY_INDIVIDUAL_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1716       break;
1717
1718     case EMPATHY_INDIVIDUAL_STORE_SORT_NAME:
1719       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1720           EMPATHY_INDIVIDUAL_STORE_COL_NAME, GTK_SORT_ASCENDING);
1721       break;
1722
1723     default:
1724       g_assert_not_reached ();
1725     }
1726
1727   g_object_notify (G_OBJECT (self), "sort-criterium");
1728 }
1729
1730 gboolean
1731 empathy_individual_store_row_separator_func (GtkTreeModel *model,
1732     GtkTreeIter *iter,
1733     gpointer data)
1734 {
1735   gboolean is_separator = FALSE;
1736
1737   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
1738
1739   gtk_tree_model_get (model, iter,
1740       EMPATHY_INDIVIDUAL_STORE_COL_IS_SEPARATOR, &is_separator, -1);
1741
1742   return is_separator;
1743 }
1744
1745 gchar *
1746 empathy_individual_store_get_parent_group (GtkTreeModel *model,
1747     GtkTreePath *path,
1748     gboolean *path_is_group,
1749     gboolean *is_fake_group)
1750 {
1751   GtkTreeIter parent_iter, iter;
1752   gchar *name = NULL;
1753   gboolean is_group;
1754   gboolean fake;
1755
1756   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1757
1758   if (path_is_group)
1759     {
1760       *path_is_group = FALSE;
1761     }
1762
1763   if (!gtk_tree_model_get_iter (model, &iter, path))
1764     {
1765       return NULL;
1766     }
1767
1768   gtk_tree_model_get (model, &iter,
1769       EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1770       EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name, -1);
1771
1772   if (!is_group)
1773     {
1774       g_free (name);
1775       name = NULL;
1776
1777       if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1778         {
1779           return NULL;
1780         }
1781
1782       iter = parent_iter;
1783
1784       gtk_tree_model_get (model, &iter,
1785           EMPATHY_INDIVIDUAL_STORE_COL_IS_GROUP, &is_group,
1786           EMPATHY_INDIVIDUAL_STORE_COL_NAME, &name,
1787           EMPATHY_INDIVIDUAL_STORE_COL_IS_FAKE_GROUP, &fake, -1);
1788       if (!is_group)
1789         {
1790           g_free (name);
1791           return NULL;
1792         }
1793     }
1794
1795   if (path_is_group)
1796     {
1797       *path_is_group = TRUE;
1798     }
1799
1800   if (is_fake_group != NULL)
1801     *is_fake_group = fake;
1802
1803   return name;
1804 }
1805
1806 static GdkPixbuf *
1807 individual_store_get_individual_status_icon_with_icon_name (
1808     EmpathyIndividualStore *self,
1809     FolksIndividual *individual,
1810     const gchar *status_icon_name)
1811 {
1812   GdkPixbuf *pixbuf_status = NULL;
1813   EmpathyIndividualStorePriv *priv;
1814   const gchar *protocol_name = NULL;
1815   gchar *icon_name = NULL;
1816   GList *personas, *l;
1817   guint contact_count;
1818   EmpathyContact *contact = NULL;
1819   gboolean show_protocols_here;
1820
1821   priv = GET_PRIV (self);
1822
1823   personas = folks_individual_get_personas (individual);
1824   for (l = personas, contact_count = 0; l; l = l->next)
1825     {
1826       if (TPF_IS_PERSONA (l->data))
1827         contact_count++;
1828
1829       if (contact_count > 1)
1830         break;
1831     }
1832
1833   show_protocols_here = (priv->show_protocols && (contact_count == 1));
1834   if (show_protocols_here)
1835     {
1836       contact = empathy_contact_dup_from_folks_individual (individual);
1837       protocol_name = empathy_protocol_name_for_contact (contact);
1838       icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
1839     }
1840   else
1841     {
1842       icon_name = g_strdup_printf ("%s", status_icon_name);
1843     }
1844   if (pixbuf_status == NULL)
1845     {
1846       pixbuf_status =
1847           empathy_pixbuf_contact_status_icon_with_icon_name (contact,
1848           status_icon_name, priv->show_protocols);
1849       if (pixbuf_status != NULL)
1850         {
1851           g_hash_table_insert (priv->status_icons,
1852               g_strdup (icon_name), pixbuf_status);
1853         }
1854     }
1855
1856   g_free (icon_name);
1857   tp_clear_object (&contact);
1858
1859   return pixbuf_status;
1860 }
1861
1862 GdkPixbuf *
1863 empathy_individual_store_get_individual_status_icon (
1864     EmpathyIndividualStore *self,
1865     FolksIndividual *individual)
1866 {
1867   GdkPixbuf *pixbuf_status = NULL;
1868   const gchar *status_icon_name = NULL;
1869
1870   status_icon_name = empathy_icon_name_for_individual (individual);
1871   if (status_icon_name == NULL)
1872     return NULL;
1873
1874   pixbuf_status =
1875       individual_store_get_individual_status_icon_with_icon_name (self,
1876       individual, status_icon_name);
1877
1878   return pixbuf_status;
1879 }