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