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