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