]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-persona-store.c
Add ax_config_dir from audacity
[empathy.git] / libempathy-gtk / empathy-persona-store.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2008, 2010 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Mikael Hallendal <micke@imendio.com>
21  *          Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  *          Philip Withnall <philip.withnall@collabora.co.uk>
24  *
25  * Based off EmpathyContactListStore.
26  */
27
28 #include "config.h"
29
30 #include <string.h>
31
32 #include <glib.h>
33 #include <glib/gi18n-lib.h>
34 #include <gtk/gtk.h>
35
36 #include <telepathy-glib/util.h>
37
38 #include <folks/folks.h>
39 #include <folks/folks-telepathy.h>
40
41 #include <libempathy/empathy-utils.h>
42
43 #include "empathy-persona-store.h"
44 #include "empathy-gtk-enum-types.h"
45 #include "empathy-ui-utils.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 /* Time in seconds user is shown as active */
54 #define ACTIVE_USER_SHOW_TIME 7
55
56 /* Time in seconds after connecting which we wait before active users are
57  * enabled */
58 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5
59
60 static void add_persona (EmpathyPersonaStore *self,
61     FolksPersona *persona);
62 static GtkTreePath * find_persona (EmpathyPersonaStore *self,
63     FolksPersona *persona);
64 static void update_persona (EmpathyPersonaStore *self,
65     FolksPersona *persona);
66 static void remove_persona (EmpathyPersonaStore *self,
67     FolksPersona *persona);
68
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPersonaStore)
70
71 typedef struct
72 {
73   FolksIndividual *individual; /* owned */
74   GHashTable *personas; /* owned Persona -> owned GtkTreeRowReference */
75
76   gboolean show_avatars;
77   gboolean show_protocols;
78   gboolean show_active;
79   EmpathyPersonaStoreSort sort_criterion;
80
81   guint inhibit_active;
82   guint setup_idle_id;
83
84   GHashTable *status_icons; /* owned icon name -> owned GdkPixbuf */
85 } EmpathyPersonaStorePriv;
86
87 enum {
88   PROP_0,
89   PROP_INDIVIDUAL,
90   PROP_SHOW_AVATARS,
91   PROP_SHOW_PROTOCOLS,
92   PROP_SORT_CRITERION
93 };
94
95 G_DEFINE_TYPE (EmpathyPersonaStore, empathy_persona_store, GTK_TYPE_LIST_STORE);
96
97 static gboolean
98 inhibit_active_cb (EmpathyPersonaStore *store)
99 {
100   EmpathyPersonaStorePriv *priv;
101
102   priv = GET_PRIV (store);
103
104   priv->show_active = TRUE;
105   priv->inhibit_active = 0;
106
107   return FALSE;
108 }
109
110 typedef struct {
111   EmpathyPersonaStore *store;
112   FolksPersona *persona;
113   gboolean remove;
114   guint timeout;
115 } ShowActiveData;
116
117 static void persona_active_free (ShowActiveData *data);
118
119 static void
120 persona_active_invalidated (ShowActiveData *data,
121     GObject *old_object)
122 {
123   /* Remove the timeout and free the struct, since the persona or persona
124    * store has disappeared. */
125   g_source_remove (data->timeout);
126
127   if (old_object == (GObject *) data->store)
128     data->store = NULL;
129   else if (old_object == (GObject *) data->persona)
130     data->persona = NULL;
131   else
132     g_assert_not_reached ();
133
134   persona_active_free (data);
135 }
136
137 static ShowActiveData *
138 persona_active_new (EmpathyPersonaStore *self,
139     FolksPersona *persona,
140     gboolean remove_)
141 {
142   ShowActiveData *data;
143
144   DEBUG ("Contact:'%s' now active, and %s be removed",
145       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona)),
146       remove_ ? "WILL" : "WILL NOT");
147
148   data = g_slice_new0 (ShowActiveData);
149
150   /* We don't actually want to force either the PersonaStore or the
151    * Persona to stay alive, since the user could quit Empathy or disable
152    * the account before the persona_active timeout is fired. */
153   g_object_weak_ref (G_OBJECT (self),
154       (GWeakNotify) persona_active_invalidated, data);
155   g_object_weak_ref (G_OBJECT (persona),
156       (GWeakNotify) persona_active_invalidated, data);
157
158   data->store = self;
159   data->persona = persona;
160   data->remove = remove_;
161
162   return data;
163 }
164
165 static void
166 persona_active_free (ShowActiveData *data)
167 {
168   if (data->store != NULL)
169     {
170       g_object_weak_unref (G_OBJECT (data->store),
171           (GWeakNotify) persona_active_invalidated, data);
172     }
173
174   if (data->persona != NULL)
175     {
176       g_object_weak_unref (G_OBJECT (data->persona),
177           (GWeakNotify) persona_active_invalidated, data);
178     }
179
180   g_slice_free (ShowActiveData, data);
181 }
182
183 static void
184 persona_set_active (EmpathyPersonaStore *self,
185     FolksPersona *persona,
186     gboolean active,
187     gboolean set_changed)
188 {
189   GtkTreePath *path;
190   GtkTreeIter iter;
191
192   path = find_persona (self, persona);
193   if (path == NULL)
194     return;
195
196   gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
197   gtk_list_store_set (GTK_LIST_STORE (self), &iter,
198       EMPATHY_PERSONA_STORE_COL_IS_ACTIVE, active,
199       -1);
200
201   DEBUG ("Set item %s", active ? "active" : "inactive");
202
203   if (set_changed)
204     gtk_tree_model_row_changed (GTK_TREE_MODEL (self), path, &iter);
205
206   gtk_tree_path_free (path);
207 }
208
209 static gboolean
210 persona_active_cb (ShowActiveData *data)
211 {
212   const gchar *alias =
213       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (data->persona));
214
215   if (data->remove)
216     {
217       DEBUG ("Contact:'%s' active timeout, removing item", alias);
218       remove_persona (data->store, data->persona);
219     }
220
221   DEBUG ("Contact:'%s' no longer active", alias);
222   persona_set_active (data->store, data->persona, FALSE, TRUE);
223
224   persona_active_free (data);
225
226   return FALSE;
227 }
228
229 static void
230 persona_updated_cb (FolksPersona *persona,
231     GParamSpec *pspec,
232     EmpathyPersonaStore *self)
233 {
234   DEBUG ("Contact:'%s' updated, checking roster is in sync...",
235       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona)));
236
237   update_persona (self, persona);
238 }
239
240 static void
241 add_persona_and_connect (EmpathyPersonaStore *self,
242     FolksPersona *persona)
243 {
244   /* We don't want any non-Telepathy personas */
245   if (!TPF_IS_PERSONA (persona))
246     return;
247
248   g_signal_connect (persona, "notify::presence",
249       (GCallback) persona_updated_cb, self);
250   g_signal_connect (persona, "notify::presence-message",
251       (GCallback) persona_updated_cb, self);
252   g_signal_connect (persona, "notify::alias",
253       (GCallback) persona_updated_cb, self);
254   g_signal_connect (persona, "notify::avatar",
255       (GCallback) persona_updated_cb, self);
256
257   add_persona (self, persona);
258 }
259
260 static void
261 remove_persona_and_disconnect (EmpathyPersonaStore *self,
262     FolksPersona *persona)
263 {
264   if (!TPF_IS_PERSONA (persona))
265     return;
266
267   g_signal_handlers_disconnect_by_func (persona,
268       (GCallback) persona_updated_cb, self);
269
270   remove_persona (self, persona);
271 }
272
273 static void
274 add_persona (EmpathyPersonaStore *self,
275     FolksPersona *persona)
276 {
277   EmpathyPersonaStorePriv *priv;
278   GtkTreeIter iter;
279   GtkTreePath *path;
280   FolksPersonaStore *store;
281   EmpathyContact *contact;
282   const gchar *alias;
283
284   if (!TPF_IS_PERSONA (persona))
285     return;
286
287   priv = GET_PRIV (self);
288
289   alias = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona));
290   if (EMP_STR_EMPTY (alias))
291     return;
292
293   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
294       TPF_PERSONA (persona)));
295   store = folks_persona_get_store (persona);
296
297   gtk_list_store_insert_with_values (GTK_LIST_STORE (self), &iter, 0,
298       EMPATHY_PERSONA_STORE_COL_NAME, alias,
299       EMPATHY_PERSONA_STORE_COL_ACCOUNT_NAME,
300           folks_persona_store_get_display_name (store),
301       EMPATHY_PERSONA_STORE_COL_DISPLAY_ID,
302           folks_persona_get_display_id (persona),
303       EMPATHY_PERSONA_STORE_COL_PERSONA, persona,
304       EMPATHY_PERSONA_STORE_COL_CAN_AUDIO_CALL,
305           empathy_contact_get_capabilities (contact) &
306               EMPATHY_CAPABILITIES_AUDIO,
307       EMPATHY_PERSONA_STORE_COL_CAN_VIDEO_CALL,
308           empathy_contact_get_capabilities (contact) &
309               EMPATHY_CAPABILITIES_VIDEO,
310       -1);
311
312   g_object_unref (contact);
313
314   path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), &iter);
315   g_hash_table_replace (priv->personas, g_object_ref (persona),
316       gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path));
317   gtk_tree_path_free (path);
318
319   update_persona (self, persona);
320 }
321
322 static void
323 remove_persona (EmpathyPersonaStore *self,
324     FolksPersona *persona)
325 {
326   EmpathyPersonaStorePriv *priv;
327   GtkTreePath *path;
328   GtkTreeIter iter;
329
330   if (!TPF_IS_PERSONA (persona))
331     return;
332
333   priv = GET_PRIV (self);
334
335   path = find_persona (self, persona);
336   if (path == NULL)
337     return;
338
339   g_hash_table_remove (priv->personas, persona);
340
341   gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
342   gtk_list_store_remove (GTK_LIST_STORE (self), &iter);
343   gtk_tree_path_free (path);
344 }
345
346 static GdkPixbuf *
347 get_persona_status_icon (EmpathyPersonaStore *self,
348     FolksPersona *persona)
349 {
350   EmpathyPersonaStorePriv *priv = GET_PRIV (self);
351   EmpathyContact *contact;
352   const gchar *protocol_name = NULL;
353   gchar *icon_name = NULL;
354   GdkPixbuf *pixbuf_status = NULL;
355   const gchar *status_icon_name = NULL;
356
357   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
358       TPF_PERSONA (persona)));
359
360   status_icon_name = empathy_icon_name_for_contact (contact);
361   if (status_icon_name == NULL)
362     {
363       g_object_unref (contact);
364       return NULL;
365     }
366
367   if (priv->show_protocols)
368     {
369       protocol_name = empathy_protocol_name_for_contact (contact);
370       icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
371     }
372   else
373     {
374       icon_name = g_strdup_printf ("%s", status_icon_name);
375     }
376
377   pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
378
379   if (pixbuf_status == NULL)
380     {
381       pixbuf_status = empathy_pixbuf_contact_status_icon_with_icon_name (
382           contact, status_icon_name, priv->show_protocols);
383
384       if (pixbuf_status != NULL)
385         {
386           g_hash_table_insert (priv->status_icons, g_strdup (icon_name),
387               pixbuf_status);
388         }
389     }
390
391   g_object_unref (contact);
392   g_free (icon_name);
393
394   return pixbuf_status;
395 }
396
397 static void
398 update_persona (EmpathyPersonaStore *self,
399     FolksPersona *persona)
400 {
401   EmpathyPersonaStorePriv *priv = GET_PRIV (self);
402   GtkTreePath *path;
403   gboolean do_set_active = FALSE;
404   gboolean do_set_refresh = FALSE;
405   const gchar *alias;
406
407   path = find_persona (self, persona);
408   alias = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona));
409
410   if (path == NULL)
411     {
412       DEBUG ("Contact:'%s' in list:NO, should be:YES", alias);
413
414       add_persona (self, persona);
415
416       if (priv->show_active)
417         {
418           do_set_active = TRUE;
419           DEBUG ("Set active (contact added)");
420         }
421     }
422   else
423     {
424       FolksPersonaStore *store;
425       EmpathyContact *contact;
426       GtkTreeIter iter;
427       GdkPixbuf *pixbuf_avatar;
428       GdkPixbuf *pixbuf_status;
429       gboolean now_online = FALSE;
430       gboolean was_online = TRUE;
431
432       DEBUG ("Contact:'%s' in list:YES, should be:YES", alias);
433
434       gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
435       gtk_tree_path_free (path);
436
437       /* Get online state now. */
438       now_online = folks_presence_details_is_online (
439           FOLKS_PRESENCE_DETAILS (persona));
440
441       /* Get online state before. */
442       gtk_tree_model_get (GTK_TREE_MODEL (self), &iter,
443           EMPATHY_PERSONA_STORE_COL_IS_ONLINE, &was_online,
444           -1);
445
446       /* Is this really an update or an online/offline. */
447       if (priv->show_active)
448         {
449           if (was_online != now_online)
450             {
451               do_set_active = TRUE;
452               do_set_refresh = TRUE;
453
454               DEBUG ("Set active (contact updated %s)",
455                   was_online ? "online  -> offline" : "offline -> online");
456             }
457           else
458             {
459               /* Was TRUE for presence updates. */
460               /* do_set_active = FALSE;  */
461               do_set_refresh = TRUE;
462               DEBUG ("Set active (contact updated)");
463             }
464         }
465
466       /* We still need to use EmpathyContact for the capabilities stuff */
467       contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
468           TPF_PERSONA (persona)));
469       store = folks_persona_get_store (persona);
470
471       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
472           32, 32);
473       pixbuf_status = get_persona_status_icon (self, persona);
474
475       gtk_list_store_set (GTK_LIST_STORE (self), &iter,
476           EMPATHY_PERSONA_STORE_COL_ICON_STATUS, pixbuf_status,
477           EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
478           EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
479           EMPATHY_PERSONA_STORE_COL_NAME, alias,
480           EMPATHY_PERSONA_STORE_COL_ACCOUNT_NAME,
481               folks_persona_store_get_display_name (store),
482           EMPATHY_PERSONA_STORE_COL_DISPLAY_ID,
483               folks_persona_get_display_id (persona),
484           EMPATHY_PERSONA_STORE_COL_PRESENCE_TYPE,
485               folks_presence_details_get_presence_type (
486                   FOLKS_PRESENCE_DETAILS (persona)),
487           EMPATHY_PERSONA_STORE_COL_STATUS,
488               folks_presence_details_get_presence_message (
489                   FOLKS_PRESENCE_DETAILS (persona)),
490           EMPATHY_PERSONA_STORE_COL_IS_ONLINE, now_online,
491           EMPATHY_PERSONA_STORE_COL_CAN_AUDIO_CALL,
492               empathy_contact_get_capabilities (contact) &
493                 EMPATHY_CAPABILITIES_AUDIO,
494           EMPATHY_PERSONA_STORE_COL_CAN_VIDEO_CALL,
495               empathy_contact_get_capabilities (contact) &
496                 EMPATHY_CAPABILITIES_VIDEO,
497           -1);
498
499       g_object_unref (contact);
500
501       if (pixbuf_avatar)
502         g_object_unref (pixbuf_avatar);
503     }
504
505   if (priv->show_active && do_set_active)
506     {
507       persona_set_active (self, persona, do_set_active, do_set_refresh);
508
509       if (do_set_active)
510         {
511           ShowActiveData *data;
512
513           data = persona_active_new (self, persona, FALSE);
514           data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
515               (GSourceFunc) persona_active_cb,
516               data);
517         }
518     }
519
520   /* FIXME: when someone goes online then offline quickly, the
521    * first timeout sets the user to be inactive and the second
522    * timeout removes the user from the contact list, really we
523    * should remove the first timeout.
524    */
525 }
526
527 static void
528 individual_personas_changed_cb (GObject *object,
529     GeeSet *added,
530     GeeSet *removed,
531     EmpathyPersonaStore *self)
532 {
533   GeeIterator *iter;
534
535   /* One of the personas' row references might hold the last reference to the
536    * PersonaStore, so we need to keep a reference ourselves so we don't get
537    * finalised. */
538   g_object_ref (self);
539
540   /* Remove the old personas. */
541   iter = gee_iterable_iterator (GEE_ITERABLE (removed));
542   while (gee_iterator_next (iter))
543     {
544       FolksPersona *persona = gee_iterator_get (iter);
545       remove_persona_and_disconnect (self, persona);
546       g_clear_object (&persona);
547     }
548   g_clear_object (&iter);
549
550   /* Add each of the new personas to the tree model */
551   iter = gee_iterable_iterator (GEE_ITERABLE (added));
552   while (gee_iterator_next (iter))
553     {
554       FolksPersona *persona = gee_iterator_get (iter);
555       add_persona_and_connect (self, persona);
556       g_clear_object (&persona);
557     }
558   g_clear_object (&iter);
559
560   g_object_unref (self);
561 }
562
563 static gint
564 sort_personas (FolksPersona *persona_a,
565     FolksPersona *persona_b)
566 {
567   EmpathyContact *contact;
568   TpAccount *account_a, *account_b;
569   gint ret_val;
570
571   g_return_val_if_fail (persona_a != NULL || persona_b != NULL, 0);
572
573   /* alias */
574   ret_val = g_utf8_collate (
575       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona_a)),
576       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona_b)));
577
578   if (ret_val != 0)
579     goto out;
580
581   /* identifier */
582   ret_val = g_utf8_collate (folks_persona_get_display_id (persona_a),
583           folks_persona_get_display_id (persona_b));
584
585   if (ret_val != 0)
586     goto out;
587
588   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
589       TPF_PERSONA (persona_a)));
590   account_a = empathy_contact_get_account (contact);
591   g_object_unref (contact);
592
593   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
594       TPF_PERSONA (persona_b)));
595   account_b = empathy_contact_get_account (contact);
596   g_object_unref (contact);
597
598   /* protocol */
599   ret_val = strcmp (tp_account_get_protocol (account_a),
600         tp_account_get_protocol (account_b));
601
602   if (ret_val != 0)
603     goto out;
604
605   /* account ID */
606   ret_val = strcmp (tp_proxy_get_object_path (account_a),
607         tp_proxy_get_object_path (account_b));
608
609 out:
610   return ret_val;
611 }
612
613 static gint
614 state_sort_func (GtkTreeModel *model,
615     GtkTreeIter *iter_a,
616     GtkTreeIter *iter_b,
617     gpointer user_data)
618 {
619   gint ret_val;
620   gchar *name_a, *name_b;
621   FolksPersona *persona_a, *persona_b;
622
623   gtk_tree_model_get (model, iter_a,
624           EMPATHY_PERSONA_STORE_COL_NAME, &name_a,
625           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_a,
626           -1);
627   gtk_tree_model_get (model, iter_b,
628           EMPATHY_PERSONA_STORE_COL_NAME, &name_b,
629           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_b,
630           -1);
631
632   if (persona_a == NULL || persona_b == NULL) {
633     ret_val = 0;
634     goto free_and_out;
635   }
636
637   /* If we managed to get this far, we can start looking at
638    * the presences.
639    */
640   ret_val = -tp_connection_presence_type_cmp_availability (
641       folks_presence_details_get_presence_type (
642           FOLKS_PRESENCE_DETAILS (persona_a)),
643       folks_presence_details_get_presence_type (
644           FOLKS_PRESENCE_DETAILS (persona_b)));
645
646   if (ret_val == 0) {
647     /* Fallback: compare by name et al. */
648     ret_val = sort_personas (persona_a, persona_b);
649   }
650
651 free_and_out:
652   g_free (name_a);
653   g_free (name_b);
654
655   tp_clear_object (&persona_a);
656   tp_clear_object (&persona_b);
657
658   return ret_val;
659 }
660
661 static gint
662 name_sort_func (GtkTreeModel *model,
663     GtkTreeIter *iter_a,
664     GtkTreeIter *iter_b,
665     gpointer user_data)
666 {
667   gchar *name_a, *name_b;
668   FolksPersona *persona_a, *persona_b;
669   gint ret_val;
670
671   gtk_tree_model_get (model, iter_a,
672           EMPATHY_PERSONA_STORE_COL_NAME, &name_a,
673           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_a,
674           -1);
675   gtk_tree_model_get (model, iter_b,
676           EMPATHY_PERSONA_STORE_COL_NAME, &name_b,
677           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_b,
678           -1);
679
680   if (persona_a == NULL || persona_b == NULL)
681     ret_val = 0;
682   else
683     ret_val = sort_personas (persona_a, persona_b);
684
685   tp_clear_object (&persona_a);
686   tp_clear_object (&persona_b);
687
688   return ret_val;
689 }
690
691 static GtkTreePath *
692 find_persona (EmpathyPersonaStore *self,
693     FolksPersona *persona)
694 {
695   EmpathyPersonaStorePriv *priv = GET_PRIV (self);
696   GtkTreeRowReference *row;
697
698   row = g_hash_table_lookup (priv->personas, persona);
699   if (row == NULL)
700     return NULL;
701
702   return gtk_tree_row_reference_get_path (row);
703 }
704
705 static gboolean
706 update_list_mode_foreach (GtkTreeModel *model,
707     GtkTreePath *path,
708     GtkTreeIter *iter,
709     EmpathyPersonaStore *self)
710 {
711   EmpathyPersonaStorePriv *priv;
712   FolksPersona *persona;
713   GdkPixbuf *pixbuf_status;
714
715   priv = GET_PRIV (self);
716
717   gtk_tree_model_get (model, iter,
718       EMPATHY_PERSONA_STORE_COL_PERSONA, &persona,
719       -1);
720
721   if (persona == NULL)
722     return FALSE;
723
724   /* get icon from hash_table */
725   pixbuf_status = get_persona_status_icon (self, persona);
726
727   gtk_list_store_set (GTK_LIST_STORE (self), iter,
728       EMPATHY_PERSONA_STORE_COL_ICON_STATUS, pixbuf_status,
729       EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
730       -1);
731
732   tp_clear_object (&persona);
733
734   return FALSE;
735 }
736
737 static void
738 set_up (EmpathyPersonaStore *self)
739 {
740   EmpathyPersonaStorePriv *priv;
741   GType types[] = {
742     GDK_TYPE_PIXBUF,      /* Status pixbuf */
743     GDK_TYPE_PIXBUF,      /* Avatar pixbuf */
744     G_TYPE_BOOLEAN,       /* Avatar pixbuf visible */
745     G_TYPE_STRING,        /* Name */
746     G_TYPE_STRING,        /* Account name */
747     G_TYPE_STRING,        /* Display ID */
748     G_TYPE_UINT,          /* Presence type */
749     G_TYPE_STRING,        /* Status string */
750     FOLKS_TYPE_PERSONA,   /* Persona */
751     G_TYPE_BOOLEAN,       /* Is active */
752     G_TYPE_BOOLEAN,       /* Is online */
753     G_TYPE_BOOLEAN,       /* Can make audio calls */
754     G_TYPE_BOOLEAN,       /* Can make video calls */
755   };
756
757   priv = GET_PRIV (self);
758
759   gtk_list_store_set_column_types (GTK_LIST_STORE (self),
760       EMPATHY_PERSONA_STORE_COL_COUNT, types);
761
762   /* Set up sorting */
763   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
764       EMPATHY_PERSONA_STORE_COL_NAME, name_sort_func, self, NULL);
765   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
766       EMPATHY_PERSONA_STORE_COL_STATUS, state_sort_func, self, NULL);
767
768   priv->sort_criterion = EMPATHY_PERSONA_STORE_SORT_NAME;
769   empathy_persona_store_set_sort_criterion (self, priv->sort_criterion);
770 }
771
772 static void
773 empathy_persona_store_init (EmpathyPersonaStore *self)
774 {
775   EmpathyPersonaStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
776       EMPATHY_TYPE_PERSONA_STORE, EmpathyPersonaStorePriv);
777
778   self->priv = priv;
779
780   priv->show_avatars = TRUE;
781   priv->show_protocols = FALSE;
782   priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
783       (GSourceFunc) inhibit_active_cb, self);
784
785   priv->status_icons = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
786       g_object_unref);
787   priv->personas = g_hash_table_new_full (g_direct_hash, g_direct_equal,
788       g_object_unref, (GDestroyNotify) gtk_tree_row_reference_free);
789
790   set_up (self);
791 }
792
793 static void
794 get_property (GObject *object,
795     guint param_id,
796     GValue *value,
797     GParamSpec *pspec)
798 {
799   EmpathyPersonaStorePriv *priv = GET_PRIV (object);
800
801   switch (param_id)
802     {
803       case PROP_INDIVIDUAL:
804         g_value_set_object (value, priv->individual);
805         break;
806       case PROP_SHOW_AVATARS:
807         g_value_set_boolean (value, priv->show_avatars);
808         break;
809       case PROP_SHOW_PROTOCOLS:
810         g_value_set_boolean (value, priv->show_protocols);
811         break;
812       case PROP_SORT_CRITERION:
813         g_value_set_enum (value, priv->sort_criterion);
814         break;
815       default:
816         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
817         break;
818     }
819 }
820
821 static void
822 set_property (GObject *object,
823     guint param_id,
824     const GValue *value,
825     GParamSpec *pspec)
826 {
827   EmpathyPersonaStore *self = EMPATHY_PERSONA_STORE (object);
828
829   switch (param_id)
830     {
831       case PROP_INDIVIDUAL:
832         empathy_persona_store_set_individual (self, g_value_get_object (value));
833         break;
834       case PROP_SHOW_AVATARS:
835         empathy_persona_store_set_show_avatars (self,
836             g_value_get_boolean (value));
837         break;
838       case PROP_SHOW_PROTOCOLS:
839         empathy_persona_store_set_show_protocols (self,
840             g_value_get_boolean (value));
841         break;
842       case PROP_SORT_CRITERION:
843         empathy_persona_store_set_sort_criterion (self,
844             g_value_get_enum (value));
845         break;
846       default:
847         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
848         break;
849     }
850 }
851
852 static void
853 dispose (GObject *object)
854 {
855   EmpathyPersonaStorePriv *priv = GET_PRIV (object);
856
857   empathy_persona_store_set_individual (EMPATHY_PERSONA_STORE (object), NULL);
858
859   if (priv->inhibit_active != 0)
860     {
861       g_source_remove (priv->inhibit_active);
862       priv->inhibit_active = 0;
863     }
864
865   if (priv->setup_idle_id != 0)
866     {
867       g_source_remove (priv->setup_idle_id);
868       priv->setup_idle_id = 0;
869     }
870
871   G_OBJECT_CLASS (empathy_persona_store_parent_class)->dispose (object);
872 }
873
874 static void
875 finalize (GObject *object)
876 {
877   EmpathyPersonaStorePriv *priv = GET_PRIV (object);
878
879   g_hash_table_destroy (priv->status_icons);
880   g_hash_table_destroy (priv->personas);
881
882   G_OBJECT_CLASS (empathy_persona_store_parent_class)->finalize (object);
883 }
884
885 static void
886 empathy_persona_store_class_init (EmpathyPersonaStoreClass *klass)
887 {
888   GObjectClass *object_class = G_OBJECT_CLASS (klass);
889
890   object_class->get_property = get_property;
891   object_class->set_property = set_property;
892   object_class->dispose = dispose;
893   object_class->finalize = finalize;
894
895   /**
896    * EmpathyPersonaStore:individual:
897    *
898    * The #FolksIndividual whose personas should be listed by the store. This
899    * may be %NULL, which results in an empty store.
900    */
901   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
902       g_param_spec_object ("individual",
903           "Individual",
904           "The FolksIndividual whose Personas should be listed by the store.",
905           FOLKS_TYPE_INDIVIDUAL,
906           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
907
908   /**
909    * EmpathyPersonaStore:show-avatars:
910    *
911    * Whether the store should display avatars for personas. This is a property
912    * of the store rather than of #EmpathyPersonaView for efficiency reasons.
913    */
914   g_object_class_install_property (object_class, PROP_SHOW_AVATARS,
915       g_param_spec_boolean ("show-avatars",
916           "Show Avatars",
917           "Whether the store should display avatars for personas.",
918           TRUE,
919           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
920
921   /**
922    * EmpathyPersonaStore:show-protocols:
923    *
924    * Whether the store should display protocol icons for personas. This is a
925    * property of the store rather than of #EmpathyPersonaView because it is
926    * closely tied in with #EmpathyPersonaStore:show-avatars.
927    */
928   g_object_class_install_property (object_class, PROP_SHOW_PROTOCOLS,
929       g_param_spec_boolean ("show-protocols",
930           "Show Protocols",
931           "Whether the store should display protocol icons for personas.",
932           FALSE,
933           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
934
935   /**
936    * EmpathyPersonaStore:sort-criterion:
937    *
938    * The criterion used to sort the personas in the store.
939    */
940   g_object_class_install_property (object_class, PROP_SORT_CRITERION,
941       g_param_spec_enum ("sort-criterion",
942           "Sort criterion",
943           "The sort criterion to use for sorting the persona list",
944           EMPATHY_TYPE_PERSONA_STORE_SORT,
945           EMPATHY_PERSONA_STORE_SORT_NAME,
946           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
947
948   g_type_class_add_private (object_class, sizeof (EmpathyPersonaStorePriv));
949 }
950
951 /**
952  * empathy_persona_store_new:
953  * @individual: the #FolksIndividual whose personas should be used in the store,
954  * or %NULL
955  *
956  * Create a new #EmpathyPersonaStore with the personas from the given
957  * @individual.
958  *
959  * Return value: a new #EmpathyPersonaStore
960  */
961 EmpathyPersonaStore *
962 empathy_persona_store_new (FolksIndividual *individual)
963 {
964   g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
965       NULL);
966
967   return g_object_new (EMPATHY_TYPE_PERSONA_STORE,
968       "individual", individual, NULL);
969 }
970
971 /**
972  * empathy_persona_store_get_individual:
973  * @self: an #EmpathyPersonaStore
974  *
975  * Get the value of #EmpathyPersonaStore:individual.
976  *
977  * Return value: the individual being displayed by the store, or %NULL
978  */
979 FolksIndividual *
980 empathy_persona_store_get_individual (EmpathyPersonaStore *self)
981 {
982   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), NULL);
983
984   return GET_PRIV (self)->individual;
985 }
986
987 /**
988  * empathy_persona_store_set_individual:
989  * @self: an #EmpathyPersonaStore
990  * @individual: the new individual to display in the store, or %NULL
991  *
992  * Set #EmpathyPersonaStore:individual to @individual, replacing the personas
993  * which were in the store with the personas belonging to @individual, or with
994  * nothing if @individual is %NULL.
995  */
996 void
997 empathy_persona_store_set_individual (EmpathyPersonaStore *self,
998     FolksIndividual *individual)
999 {
1000   EmpathyPersonaStorePriv *priv;
1001
1002   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1003   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
1004
1005   priv = GET_PRIV (self);
1006
1007   /* Remove the old individual */
1008   if (priv->individual != NULL)
1009     {
1010       GeeSet *personas;
1011       GeeIterator *iter;
1012
1013       g_signal_handlers_disconnect_by_func (priv->individual,
1014           (GCallback) individual_personas_changed_cb, self);
1015
1016       /* Disconnect from and remove all personas belonging to this individual */
1017       personas = folks_individual_get_personas (priv->individual);
1018       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1019       while (gee_iterator_next (iter))
1020         {
1021           FolksPersona *persona = gee_iterator_get (iter);
1022           remove_persona_and_disconnect (self, persona);
1023           g_clear_object (&persona);
1024         }
1025       g_clear_object (&iter);
1026
1027       g_object_unref (priv->individual);
1028     }
1029
1030   priv->individual = individual;
1031
1032   /* Add the new individual */
1033   if (individual != NULL)
1034     {
1035       GeeSet *personas;
1036       GeeIterator *iter;
1037
1038       g_object_ref (individual);
1039
1040       g_signal_connect (individual, "personas-changed",
1041           (GCallback) individual_personas_changed_cb, self);
1042
1043       /* Add pre-existing Personas */
1044
1045       personas = folks_individual_get_personas (individual);
1046       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1047       while (gee_iterator_next (iter))
1048         {
1049           FolksPersona *persona = gee_iterator_get (iter);
1050           add_persona_and_connect (self, persona);
1051           g_clear_object (&persona);
1052         }
1053       g_clear_object (&iter);
1054     }
1055
1056   g_object_notify (G_OBJECT (self), "individual");
1057 }
1058
1059 /**
1060  * empathy_persona_store_get_show_avatars:
1061  * @self: an #EmpathyPersonaStore
1062  *
1063  * Get the value of #EmpathyPersonaStore:show-avatars.
1064  *
1065  * Return value: %TRUE if avatars are made available by the store, %FALSE
1066  * otherwise
1067  */
1068 gboolean
1069 empathy_persona_store_get_show_avatars (EmpathyPersonaStore *self)
1070 {
1071   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), TRUE);
1072
1073   return GET_PRIV (self)->show_avatars;
1074 }
1075
1076 /**
1077  * empathy_persona_store_set_show_avatars:
1078  * @self: an #EmpathyPersonaStore
1079  * @show_avatars: %TRUE to make avatars available through the store, %FALSE
1080  * otherwise
1081  *
1082  * Set #EmpathyPersonaStore:show-avatars to @show_avatars.
1083  */
1084 void
1085 empathy_persona_store_set_show_avatars (EmpathyPersonaStore *self,
1086     gboolean show_avatars)
1087 {
1088   EmpathyPersonaStorePriv *priv;
1089
1090   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1091
1092   priv = GET_PRIV (self);
1093   priv->show_avatars = show_avatars;
1094
1095   gtk_tree_model_foreach (GTK_TREE_MODEL (self),
1096       (GtkTreeModelForeachFunc) update_list_mode_foreach, self);
1097
1098   g_object_notify (G_OBJECT (self), "show-avatars");
1099 }
1100
1101 /**
1102  * empathy_persona_store_get_show_protocols:
1103  * @self: an #EmpathyPersonaStore
1104  *
1105  * Get the value of #EmpathyPersonaStore:show-protocols.
1106  *
1107  * Return value: %TRUE if protocol images are made available by the store,
1108  * %FALSE otherwise
1109  */
1110 gboolean
1111 empathy_persona_store_get_show_protocols (EmpathyPersonaStore *self)
1112 {
1113   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), TRUE);
1114
1115   return GET_PRIV (self)->show_protocols;
1116 }
1117
1118 /**
1119  * empathy_persona_store_set_show_protocols:
1120  * @self: an #EmpathyPersonaStore
1121  * @show_protocols: %TRUE to make protocol images available through the store,
1122  * %FALSE otherwise
1123  *
1124  * Set #EmpathyPersonaStore:show-protocols to @show_protocols.
1125  */
1126 void
1127 empathy_persona_store_set_show_protocols (EmpathyPersonaStore *self,
1128     gboolean show_protocols)
1129 {
1130   EmpathyPersonaStorePriv *priv;
1131
1132   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1133
1134   priv = GET_PRIV (self);
1135   priv->show_protocols = show_protocols;
1136
1137   gtk_tree_model_foreach (GTK_TREE_MODEL (self),
1138       (GtkTreeModelForeachFunc) update_list_mode_foreach, self);
1139
1140   g_object_notify (G_OBJECT (self), "show-protocols");
1141 }
1142
1143 /**
1144  * empathy_persona_store_get_sort_criterion:
1145  * @self: an #EmpathyPersonaStore
1146  *
1147  * Get the value of #EmpathyPersonaStore:sort-criterion.
1148  *
1149  * Return value: the criterion used to sort the personas in the store
1150  */
1151 EmpathyPersonaStoreSort
1152 empathy_persona_store_get_sort_criterion (EmpathyPersonaStore *self)
1153 {
1154   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), 0);
1155
1156   return GET_PRIV (self)->sort_criterion;
1157 }
1158
1159 /**
1160  * empathy_persona_store_set_sort_criterion:
1161  * @self: an #EmpathyPersonaStore
1162  * @show_avatars: a criterion to be used to sort personas in the store
1163  *
1164  * Set #EmpathyPersonaStore:sort-criterion to @sort_criterion.
1165  */
1166 void
1167 empathy_persona_store_set_sort_criterion (EmpathyPersonaStore *self,
1168     EmpathyPersonaStoreSort sort_criterion)
1169 {
1170   EmpathyPersonaStorePriv *priv;
1171
1172   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1173
1174   priv = GET_PRIV (self);
1175   priv->sort_criterion = sort_criterion;
1176
1177   switch (sort_criterion)
1178     {
1179       case EMPATHY_PERSONA_STORE_SORT_STATE:
1180         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1181             EMPATHY_PERSONA_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1182         break;
1183       case EMPATHY_PERSONA_STORE_SORT_NAME:
1184         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1185             EMPATHY_PERSONA_STORE_COL_NAME, GTK_SORT_ASCENDING);
1186         break;
1187       default:
1188         g_assert_not_reached ();
1189         break;
1190     }
1191
1192   g_object_notify (G_OBJECT (self), "sort-criterion");
1193 }