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