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