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