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