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