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