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