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