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