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