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