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