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