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