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