]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-persona-store.c
Updated Russian translation
[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_get_alias (FOLKS_ALIAS (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   EmpathyPersonaStorePriv *priv;
190   GtkTreePath *path;
191   GtkTreeIter iter;
192
193   priv = GET_PRIV (self);
194
195   path = find_persona (self, persona);
196   if (path == NULL)
197     return;
198
199   gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
200   gtk_list_store_set (GTK_LIST_STORE (self), &iter,
201       EMPATHY_PERSONA_STORE_COL_IS_ACTIVE, active,
202       -1);
203
204   DEBUG ("Set item %s", active ? "active" : "inactive");
205
206   if (set_changed)
207     gtk_tree_model_row_changed (GTK_TREE_MODEL (self), path, &iter);
208
209   gtk_tree_path_free (path);
210 }
211
212 static gboolean
213 persona_active_cb (ShowActiveData *data)
214 {
215   const gchar *alias = folks_alias_get_alias (FOLKS_ALIAS (data->persona));
216
217   if (data->remove)
218     {
219       DEBUG ("Contact:'%s' active timeout, removing item", alias);
220       remove_persona (data->store, data->persona);
221     }
222
223   DEBUG ("Contact:'%s' no longer active", alias);
224   persona_set_active (data->store, data->persona, FALSE, TRUE);
225
226   persona_active_free (data);
227
228   return FALSE;
229 }
230
231 static void
232 persona_updated_cb (FolksPersona *persona,
233     GParamSpec *pspec,
234     EmpathyPersonaStore *self)
235 {
236   DEBUG ("Contact:'%s' updated, checking roster is in sync...",
237       folks_alias_get_alias (FOLKS_ALIAS (persona)));
238
239   update_persona (self, persona);
240 }
241
242 static void
243 add_persona_and_connect (EmpathyPersonaStore *self,
244     FolksPersona *persona)
245 {
246   /* We don't want any non-Telepathy personas */
247   if (!TPF_IS_PERSONA (persona))
248     return;
249
250   g_signal_connect (persona, "notify::presence",
251       (GCallback) persona_updated_cb, self);
252   g_signal_connect (persona, "notify::presence-message",
253       (GCallback) persona_updated_cb, self);
254   g_signal_connect (persona, "notify::alias",
255       (GCallback) persona_updated_cb, self);
256   g_signal_connect (persona, "notify::avatar",
257       (GCallback) persona_updated_cb, self);
258
259   add_persona (self, persona);
260 }
261
262 static void
263 remove_persona_and_disconnect (EmpathyPersonaStore *self,
264     FolksPersona *persona)
265 {
266   if (!TPF_IS_PERSONA (persona))
267     return;
268
269   g_signal_handlers_disconnect_by_func (persona,
270       (GCallback) persona_updated_cb, self);
271
272   remove_persona (self, persona);
273 }
274
275 static void
276 add_persona (EmpathyPersonaStore *self,
277     FolksPersona *persona)
278 {
279   EmpathyPersonaStorePriv *priv;
280   GtkTreeIter iter;
281   GtkTreePath *path;
282   FolksPersonaStore *store;
283   EmpathyContact *contact;
284   const gchar *alias;
285
286   if (!TPF_IS_PERSONA (persona))
287     return;
288
289   priv = GET_PRIV (self);
290
291   alias = folks_alias_get_alias (FOLKS_ALIAS (persona));
292   if (EMP_STR_EMPTY (alias))
293     return;
294
295   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
296       TPF_PERSONA (persona)));
297   store = folks_persona_get_store (persona);
298
299   gtk_list_store_insert_with_values (GTK_LIST_STORE (self), &iter, 0,
300       EMPATHY_PERSONA_STORE_COL_NAME, alias,
301       EMPATHY_PERSONA_STORE_COL_ACCOUNT_NAME,
302           folks_persona_store_get_display_name (store),
303       EMPATHY_PERSONA_STORE_COL_DISPLAY_ID,
304           folks_persona_get_display_id (persona),
305       EMPATHY_PERSONA_STORE_COL_PERSONA, persona,
306       EMPATHY_PERSONA_STORE_COL_CAN_AUDIO_CALL,
307           empathy_contact_get_capabilities (contact) &
308               EMPATHY_CAPABILITIES_AUDIO,
309       EMPATHY_PERSONA_STORE_COL_CAN_VIDEO_CALL,
310           empathy_contact_get_capabilities (contact) &
311               EMPATHY_CAPABILITIES_VIDEO,
312       -1);
313
314   g_object_unref (contact);
315
316   path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), &iter);
317   g_hash_table_replace (priv->personas, g_object_ref (persona),
318       gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path));
319   gtk_tree_path_free (path);
320
321   update_persona (self, persona);
322 }
323
324 static void
325 remove_persona (EmpathyPersonaStore *self,
326     FolksPersona *persona)
327 {
328   EmpathyPersonaStorePriv *priv;
329   GtkTreePath *path;
330   GtkTreeIter iter;
331
332   if (!TPF_IS_PERSONA (persona))
333     return;
334
335   priv = GET_PRIV (self);
336
337   path = find_persona (self, persona);
338   if (path == NULL)
339     return;
340
341   g_hash_table_remove (priv->personas, persona);
342
343   gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
344   gtk_list_store_remove (GTK_LIST_STORE (self), &iter);
345   gtk_tree_path_free (path);
346 }
347
348 static GdkPixbuf *
349 get_persona_status_icon (EmpathyPersonaStore *self,
350     FolksPersona *persona)
351 {
352   EmpathyPersonaStorePriv *priv = GET_PRIV (self);
353   EmpathyContact *contact;
354   const gchar *protocol_name = NULL;
355   gchar *icon_name = NULL;
356   GdkPixbuf *pixbuf_status = NULL;
357   const gchar *status_icon_name = NULL;
358
359   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
360       TPF_PERSONA (persona)));
361
362   status_icon_name = empathy_icon_name_for_contact (contact);
363   if (status_icon_name == NULL)
364     {
365       g_object_unref (contact);
366       return NULL;
367     }
368
369   if (priv->show_protocols)
370     {
371       protocol_name = empathy_protocol_name_for_contact (contact);
372       icon_name = g_strdup_printf ("%s-%s", status_icon_name, protocol_name);
373     }
374   else
375     {
376       icon_name = g_strdup_printf ("%s", status_icon_name);
377     }
378
379   pixbuf_status = g_hash_table_lookup (priv->status_icons, icon_name);
380
381   if (pixbuf_status == NULL)
382     {
383       pixbuf_status = empathy_pixbuf_contact_status_icon_with_icon_name (
384           contact, status_icon_name, priv->show_protocols);
385
386       if (pixbuf_status != NULL)
387         {
388           g_hash_table_insert (priv->status_icons, g_strdup (icon_name),
389               pixbuf_status);
390         }
391     }
392
393   g_object_unref (contact);
394   g_free (icon_name);
395
396   return pixbuf_status;
397 }
398
399 static void
400 update_persona (EmpathyPersonaStore *self,
401     FolksPersona *persona)
402 {
403   EmpathyPersonaStorePriv *priv = GET_PRIV (self);
404   GtkTreePath *path;
405   gboolean do_set_active = FALSE;
406   gboolean do_set_refresh = FALSE;
407   const gchar *alias;
408
409   path = find_persona (self, persona);
410   alias = folks_alias_get_alias (FOLKS_ALIAS (persona));
411
412   if (path == NULL)
413     {
414       DEBUG ("Contact:'%s' in list:NO, should be:YES", alias);
415
416       add_persona (self, persona);
417
418       if (priv->show_active)
419         {
420           do_set_active = TRUE;
421           DEBUG ("Set active (contact added)");
422         }
423     }
424   else
425     {
426       FolksPersonaStore *store;
427       EmpathyContact *contact;
428       GtkTreeIter iter;
429       GdkPixbuf *pixbuf_avatar;
430       GdkPixbuf *pixbuf_status;
431       gboolean now_online = FALSE;
432       gboolean was_online = TRUE;
433
434       DEBUG ("Contact:'%s' in list:YES, should be:YES", alias);
435
436       gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
437       gtk_tree_path_free (path);
438
439       /* Get online state now. */
440       now_online = folks_presence_is_online (FOLKS_PRESENCE (persona));
441
442       /* Get online state before. */
443       gtk_tree_model_get (GTK_TREE_MODEL (self), &iter,
444           EMPATHY_PERSONA_STORE_COL_IS_ONLINE, &was_online,
445           -1);
446
447       /* Is this really an update or an online/offline. */
448       if (priv->show_active)
449         {
450           if (was_online != now_online)
451             {
452               do_set_active = TRUE;
453               do_set_refresh = TRUE;
454
455               DEBUG ("Set active (contact updated %s)",
456                   was_online ? "online  -> offline" : "offline -> online");
457             }
458           else
459             {
460               /* Was TRUE for presence updates. */
461               /* do_set_active = FALSE;  */
462               do_set_refresh = TRUE;
463               DEBUG ("Set active (contact updated)");
464             }
465         }
466
467       /* We still need to use EmpathyContact for the capabilities stuff */
468       contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
469           TPF_PERSONA (persona)));
470       store = folks_persona_get_store (persona);
471
472       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
473           32, 32);
474       pixbuf_status = get_persona_status_icon (self, persona);
475
476       gtk_list_store_set (GTK_LIST_STORE (self), &iter,
477           EMPATHY_PERSONA_STORE_COL_ICON_STATUS, pixbuf_status,
478           EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR, pixbuf_avatar,
479           EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
480           EMPATHY_PERSONA_STORE_COL_NAME, alias,
481           EMPATHY_PERSONA_STORE_COL_ACCOUNT_NAME,
482               folks_persona_store_get_display_name (store),
483           EMPATHY_PERSONA_STORE_COL_DISPLAY_ID,
484               folks_persona_get_display_id (persona),
485           EMPATHY_PERSONA_STORE_COL_PRESENCE_TYPE,
486               folks_presence_get_presence_type (FOLKS_PRESENCE (persona)),
487           EMPATHY_PERSONA_STORE_COL_STATUS,
488               folks_presence_get_presence_message (FOLKS_PRESENCE (persona)),
489           EMPATHY_PERSONA_STORE_COL_IS_ONLINE, now_online,
490           EMPATHY_PERSONA_STORE_COL_CAN_AUDIO_CALL,
491               empathy_contact_get_capabilities (contact) &
492                 EMPATHY_CAPABILITIES_AUDIO,
493           EMPATHY_PERSONA_STORE_COL_CAN_VIDEO_CALL,
494               empathy_contact_get_capabilities (contact) &
495                 EMPATHY_CAPABILITIES_VIDEO,
496           -1);
497
498       g_object_unref (contact);
499
500       if (pixbuf_avatar)
501         g_object_unref (pixbuf_avatar);
502     }
503
504   if (priv->show_active && do_set_active)
505     {
506       persona_set_active (self, persona, do_set_active, do_set_refresh);
507
508       if (do_set_active)
509         {
510           ShowActiveData *data;
511
512           data = persona_active_new (self, persona, FALSE);
513           data->timeout = g_timeout_add_seconds (ACTIVE_USER_SHOW_TIME,
514               (GSourceFunc) persona_active_cb,
515               data);
516         }
517     }
518
519   /* FIXME: when someone goes online then offline quickly, the
520    * first timeout sets the user to be inactive and the second
521    * timeout removes the user from the contact list, really we
522    * should remove the first timeout.
523    */
524 }
525
526 static void
527 individual_personas_changed_cb (GObject *object,
528     GList *added,
529     GList *removed,
530     EmpathyPersonaStore *self)
531 {
532   GList *l;
533
534   /* Remove the old personas. */
535   for (l = removed; l != NULL; l = l->next)
536     remove_persona_and_disconnect (self, FOLKS_PERSONA (l->data));
537
538   /* Add each of the new personas to the tree model */
539   for (l = added; l != NULL; l = l->next)
540     add_persona_and_connect (self, FOLKS_PERSONA (l->data));
541 }
542
543 static gint
544 sort_personas (FolksPersona *persona_a,
545     FolksPersona *persona_b)
546 {
547   EmpathyContact *contact;
548   TpAccount *account_a, *account_b;
549   gint ret_val;
550
551   g_return_val_if_fail (persona_a != NULL || persona_b != NULL, 0);
552
553   /* alias */
554   ret_val = g_utf8_collate (folks_alias_get_alias (FOLKS_ALIAS (persona_a)),
555           folks_alias_get_alias (FOLKS_ALIAS (persona_b)));
556
557   if (ret_val != 0)
558     goto out;
559
560   /* identifier */
561   ret_val = g_utf8_collate (folks_persona_get_display_id (persona_a),
562           folks_persona_get_display_id (persona_b));
563
564   if (ret_val != 0)
565     goto out;
566
567   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
568       TPF_PERSONA (persona_a)));
569   account_a = empathy_contact_get_account (contact);
570   g_object_unref (contact);
571
572   contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
573       TPF_PERSONA (persona_b)));
574   account_b = empathy_contact_get_account (contact);
575   g_object_unref (contact);
576
577   /* protocol */
578   ret_val = strcmp (tp_account_get_protocol (account_a),
579         tp_account_get_protocol (account_a));
580
581   if (ret_val != 0)
582     goto out;
583
584   /* account ID */
585   ret_val = strcmp (tp_proxy_get_object_path (account_a),
586         tp_proxy_get_object_path (account_a));
587
588 out:
589   return ret_val;
590 }
591
592 static gint
593 state_sort_func (GtkTreeModel *model,
594     GtkTreeIter *iter_a,
595     GtkTreeIter *iter_b,
596     gpointer user_data)
597 {
598   gint ret_val;
599   gchar *name_a, *name_b;
600   FolksPersona *persona_a, *persona_b;
601
602   gtk_tree_model_get (model, iter_a,
603           EMPATHY_PERSONA_STORE_COL_NAME, &name_a,
604           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_a,
605           -1);
606   gtk_tree_model_get (model, iter_b,
607           EMPATHY_PERSONA_STORE_COL_NAME, &name_b,
608           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_b,
609           -1);
610
611   if (persona_a == NULL || persona_b == NULL) {
612     ret_val = 0;
613     goto free_and_out;
614   }
615
616   /* If we managed to get this far, we can start looking at
617    * the presences.
618    */
619   ret_val = -tp_connection_presence_type_cmp_availability (
620       folks_presence_get_presence_type (FOLKS_PRESENCE (persona_a)),
621       folks_presence_get_presence_type (FOLKS_PRESENCE (persona_b)));
622
623   if (ret_val == 0) {
624     /* Fallback: compare by name et al. */
625     ret_val = sort_personas (persona_a, persona_b);
626   }
627
628 free_and_out:
629   g_free (name_a);
630   g_free (name_b);
631
632   tp_clear_object (&persona_a);
633   tp_clear_object (&persona_b);
634
635   return ret_val;
636 }
637
638 static gint
639 name_sort_func (GtkTreeModel *model,
640     GtkTreeIter *iter_a,
641     GtkTreeIter *iter_b,
642     gpointer user_data)
643 {
644   gchar *name_a, *name_b;
645   FolksPersona *persona_a, *persona_b;
646   gint ret_val;
647
648   gtk_tree_model_get (model, iter_a,
649           EMPATHY_PERSONA_STORE_COL_NAME, &name_a,
650           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_a,
651           -1);
652   gtk_tree_model_get (model, iter_b,
653           EMPATHY_PERSONA_STORE_COL_NAME, &name_b,
654           EMPATHY_PERSONA_STORE_COL_PERSONA, &persona_b,
655           -1);
656
657   if (persona_a == NULL || persona_b == NULL)
658     ret_val = 0;
659   else
660     ret_val = sort_personas (persona_a, persona_b);
661
662   tp_clear_object (&persona_a);
663   tp_clear_object (&persona_b);
664
665   return ret_val;
666 }
667
668 static GtkTreePath *
669 find_persona (EmpathyPersonaStore *self,
670     FolksPersona *persona)
671 {
672   EmpathyPersonaStorePriv *priv = GET_PRIV (self);
673   GtkTreeRowReference *row;
674
675   row = g_hash_table_lookup (priv->personas, persona);
676   if (row == NULL)
677     return NULL;
678
679   return gtk_tree_row_reference_get_path (row);
680 }
681
682 static gboolean
683 update_list_mode_foreach (GtkTreeModel *model,
684     GtkTreePath *path,
685     GtkTreeIter *iter,
686     EmpathyPersonaStore *self)
687 {
688   EmpathyPersonaStorePriv *priv;
689   FolksPersona *persona;
690   GdkPixbuf *pixbuf_status;
691
692   priv = GET_PRIV (self);
693
694   gtk_tree_model_get (model, iter,
695       EMPATHY_PERSONA_STORE_COL_PERSONA, &persona,
696       -1);
697
698   if (persona == NULL)
699     return FALSE;
700
701   /* get icon from hash_table */
702   pixbuf_status = get_persona_status_icon (self, persona);
703
704   gtk_list_store_set (GTK_LIST_STORE (self), iter,
705       EMPATHY_PERSONA_STORE_COL_ICON_STATUS, pixbuf_status,
706       EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR_VISIBLE, priv->show_avatars,
707       -1);
708
709   tp_clear_object (&persona);
710
711   return FALSE;
712 }
713
714 static void
715 set_up (EmpathyPersonaStore *self)
716 {
717   EmpathyPersonaStorePriv *priv;
718   GType types[] = {
719     GDK_TYPE_PIXBUF,      /* Status pixbuf */
720     GDK_TYPE_PIXBUF,      /* Avatar pixbuf */
721     G_TYPE_BOOLEAN,       /* Avatar pixbuf visible */
722     G_TYPE_STRING,        /* Name */
723     G_TYPE_STRING,        /* Account name */
724     G_TYPE_STRING,        /* Display ID */
725     G_TYPE_UINT,          /* Presence type */
726     G_TYPE_STRING,        /* Status string */
727     FOLKS_TYPE_PERSONA,   /* Persona */
728     G_TYPE_BOOLEAN,       /* Is active */
729     G_TYPE_BOOLEAN,       /* Is online */
730     G_TYPE_BOOLEAN,       /* Can make audio calls */
731     G_TYPE_BOOLEAN,       /* Can make video calls */
732   };
733
734   priv = GET_PRIV (self);
735
736   gtk_list_store_set_column_types (GTK_LIST_STORE (self),
737       EMPATHY_PERSONA_STORE_COL_COUNT, types);
738
739   /* Set up sorting */
740   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
741       EMPATHY_PERSONA_STORE_COL_NAME, name_sort_func, self, NULL);
742   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self),
743       EMPATHY_PERSONA_STORE_COL_STATUS, state_sort_func, self, NULL);
744
745   priv->sort_criterion = EMPATHY_PERSONA_STORE_SORT_NAME;
746   empathy_persona_store_set_sort_criterion (self, priv->sort_criterion);
747 }
748
749 static void
750 empathy_persona_store_init (EmpathyPersonaStore *self)
751 {
752   EmpathyPersonaStorePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
753       EMPATHY_TYPE_PERSONA_STORE, EmpathyPersonaStorePriv);
754
755   self->priv = priv;
756
757   priv->show_avatars = TRUE;
758   priv->show_protocols = FALSE;
759   priv->inhibit_active = g_timeout_add_seconds (ACTIVE_USER_WAIT_TO_ENABLE_TIME,
760       (GSourceFunc) inhibit_active_cb, self);
761
762   priv->status_icons = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
763       g_object_unref);
764   priv->personas = g_hash_table_new_full (g_direct_hash, g_direct_equal,
765       g_object_unref, (GDestroyNotify) gtk_tree_row_reference_free);
766
767   set_up (self);
768 }
769
770 static void
771 get_property (GObject *object,
772     guint param_id,
773     GValue *value,
774     GParamSpec *pspec)
775 {
776   EmpathyPersonaStorePriv *priv = GET_PRIV (object);
777
778   switch (param_id)
779     {
780       case PROP_INDIVIDUAL:
781         g_value_set_object (value, priv->individual);
782         break;
783       case PROP_SHOW_AVATARS:
784         g_value_set_boolean (value, priv->show_avatars);
785         break;
786       case PROP_SHOW_PROTOCOLS:
787         g_value_set_boolean (value, priv->show_protocols);
788         break;
789       case PROP_SORT_CRITERION:
790         g_value_set_enum (value, priv->sort_criterion);
791         break;
792       default:
793         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
794         break;
795     }
796 }
797
798 static void
799 set_property (GObject *object,
800     guint param_id,
801     const GValue *value,
802     GParamSpec *pspec)
803 {
804   EmpathyPersonaStore *self = EMPATHY_PERSONA_STORE (object);
805
806   switch (param_id)
807     {
808       case PROP_INDIVIDUAL:
809         empathy_persona_store_set_individual (self, g_value_get_object (value));
810         break;
811       case PROP_SHOW_AVATARS:
812         empathy_persona_store_set_show_avatars (self,
813             g_value_get_boolean (value));
814         break;
815       case PROP_SHOW_PROTOCOLS:
816         empathy_persona_store_set_show_protocols (self,
817             g_value_get_boolean (value));
818         break;
819       case PROP_SORT_CRITERION:
820         empathy_persona_store_set_sort_criterion (self,
821             g_value_get_enum (value));
822         break;
823       default:
824         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
825         break;
826     }
827 }
828
829 static void
830 dispose (GObject *object)
831 {
832   EmpathyPersonaStorePriv *priv = GET_PRIV (object);
833
834   empathy_persona_store_set_individual (EMPATHY_PERSONA_STORE (object), NULL);
835
836   if (priv->inhibit_active != 0)
837     {
838       g_source_remove (priv->inhibit_active);
839       priv->inhibit_active = 0;
840     }
841
842   if (priv->setup_idle_id != 0)
843     {
844       g_source_remove (priv->setup_idle_id);
845       priv->setup_idle_id = 0;
846     }
847
848   G_OBJECT_CLASS (empathy_persona_store_parent_class)->dispose (object);
849 }
850
851 static void
852 finalize (GObject *object)
853 {
854   EmpathyPersonaStorePriv *priv = GET_PRIV (object);
855
856   g_hash_table_destroy (priv->status_icons);
857   g_hash_table_destroy (priv->personas);
858
859   G_OBJECT_CLASS (empathy_persona_store_parent_class)->finalize (object);
860 }
861
862 static void
863 empathy_persona_store_class_init (EmpathyPersonaStoreClass *klass)
864 {
865   GObjectClass *object_class = G_OBJECT_CLASS (klass);
866
867   object_class->get_property = get_property;
868   object_class->set_property = set_property;
869   object_class->dispose = dispose;
870   object_class->finalize = finalize;
871
872   /**
873    * EmpathyPersonaStore:individual:
874    *
875    * The #FolksIndividual whose personas should be listed by the store. This
876    * may be %NULL, which results in an empty store.
877    */
878   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
879       g_param_spec_object ("individual",
880           "Individual",
881           "The FolksIndividual whose Personas should be listed by the store.",
882           FOLKS_TYPE_INDIVIDUAL,
883           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
884
885   /**
886    * EmpathyPersonaStore:show-avatars:
887    *
888    * Whether the store should display avatars for personas. This is a property
889    * of the store rather than of #EmpathyPersonaView for efficiency reasons.
890    */
891   g_object_class_install_property (object_class, PROP_SHOW_AVATARS,
892       g_param_spec_boolean ("show-avatars",
893           "Show Avatars",
894           "Whether the store should display avatars for personas.",
895           TRUE,
896           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
897
898   /**
899    * EmpathyPersonaStore:show-protocols:
900    *
901    * Whether the store should display protocol icons for personas. This is a
902    * property of the store rather than of #EmpathyPersonaView because it is
903    * closely tied in with #EmpathyPersonaStore:show-avatars.
904    */
905   g_object_class_install_property (object_class, PROP_SHOW_PROTOCOLS,
906       g_param_spec_boolean ("show-protocols",
907           "Show Protocols",
908           "Whether the store should display protocol icons for personas.",
909           FALSE,
910           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
911
912   /**
913    * EmpathyPersonaStore:sort-criterion:
914    *
915    * The criterion used to sort the personas in the store.
916    */
917   g_object_class_install_property (object_class, PROP_SORT_CRITERION,
918       g_param_spec_enum ("sort-criterion",
919           "Sort criterion",
920           "The sort criterion to use for sorting the persona list",
921           EMPATHY_TYPE_PERSONA_STORE_SORT,
922           EMPATHY_PERSONA_STORE_SORT_NAME,
923           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
924
925   g_type_class_add_private (object_class, sizeof (EmpathyPersonaStorePriv));
926 }
927
928 /**
929  * empathy_persona_store_new:
930  * @individual: the #FolksIndividual whose personas should be used in the store,
931  * or %NULL
932  *
933  * Create a new #EmpathyPersonaStore with the personas from the given
934  * @individual.
935  *
936  * Return value: a new #EmpathyPersonaStore
937  */
938 EmpathyPersonaStore *
939 empathy_persona_store_new (FolksIndividual *individual)
940 {
941   g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
942       NULL);
943
944   return g_object_new (EMPATHY_TYPE_PERSONA_STORE,
945       "individual", individual, NULL);
946 }
947
948 /**
949  * empathy_persona_store_get_individual:
950  * @self: an #EmpathyPersonaStore
951  *
952  * Get the value of #EmpathyPersonaStore:individual.
953  *
954  * Return value: the individual being displayed by the store, or %NULL
955  */
956 FolksIndividual *
957 empathy_persona_store_get_individual (EmpathyPersonaStore *self)
958 {
959   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), NULL);
960
961   return GET_PRIV (self)->individual;
962 }
963
964 /**
965  * empathy_persona_store_set_individual:
966  * @self: an #EmpathyPersonaStore
967  * @individual: the new individual to display in the store, or %NULL
968  *
969  * Set #EmpathyPersonaStore:individual to @individual, replacing the personas
970  * which were in the store with the personas belonging to @individual, or with
971  * nothing if @individual is %NULL.
972  */
973 void
974 empathy_persona_store_set_individual (EmpathyPersonaStore *self,
975     FolksIndividual *individual)
976 {
977   EmpathyPersonaStorePriv *priv;
978
979   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
980   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
981
982   priv = GET_PRIV (self);
983
984   /* Remove the old individual */
985   if (priv->individual != NULL)
986     {
987       GList *personas, *l;
988
989       g_signal_handlers_disconnect_by_func (priv->individual,
990           (GCallback) individual_personas_changed_cb, self);
991
992       /* Disconnect from and remove all personas belonging to this individual */
993       personas = folks_individual_get_personas (priv->individual);
994       for (l = personas; l != NULL; l = l->next)
995         remove_persona_and_disconnect (self, FOLKS_PERSONA (l->data));
996
997       g_object_unref (priv->individual);
998     }
999
1000   priv->individual = individual;
1001
1002   /* Add the new individual */
1003   if (individual != NULL)
1004     {
1005       GList *personas, *l;
1006
1007       g_object_ref (individual);
1008
1009       g_signal_connect (individual, "personas-changed",
1010           (GCallback) individual_personas_changed_cb, self);
1011
1012       /* Add pre-existing Personas */
1013       personas = folks_individual_get_personas (individual);
1014       for (l = personas; l != NULL; l = l->next)
1015         add_persona_and_connect (self, FOLKS_PERSONA (l->data));
1016     }
1017
1018   g_object_notify (G_OBJECT (self), "individual");
1019 }
1020
1021 /**
1022  * empathy_persona_store_get_show_avatars:
1023  * @self: an #EmpathyPersonaStore
1024  *
1025  * Get the value of #EmpathyPersonaStore:show-avatars.
1026  *
1027  * Return value: %TRUE if avatars are made available by the store, %FALSE
1028  * otherwise
1029  */
1030 gboolean
1031 empathy_persona_store_get_show_avatars (EmpathyPersonaStore *self)
1032 {
1033   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), TRUE);
1034
1035   return GET_PRIV (self)->show_avatars;
1036 }
1037
1038 /**
1039  * empathy_persona_store_set_show_avatars:
1040  * @self: an #EmpathyPersonaStore
1041  * @show_avatars: %TRUE to make avatars available through the store, %FALSE
1042  * otherwise
1043  *
1044  * Set #EmpathyPersonaStore:show-avatars to @show_avatars.
1045  */
1046 void
1047 empathy_persona_store_set_show_avatars (EmpathyPersonaStore *self,
1048     gboolean show_avatars)
1049 {
1050   EmpathyPersonaStorePriv *priv;
1051
1052   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1053
1054   priv = GET_PRIV (self);
1055   priv->show_avatars = show_avatars;
1056
1057   gtk_tree_model_foreach (GTK_TREE_MODEL (self),
1058       (GtkTreeModelForeachFunc) update_list_mode_foreach, self);
1059
1060   g_object_notify (G_OBJECT (self), "show-avatars");
1061 }
1062
1063 /**
1064  * empathy_persona_store_get_show_protocols:
1065  * @self: an #EmpathyPersonaStore
1066  *
1067  * Get the value of #EmpathyPersonaStore:show-protocols.
1068  *
1069  * Return value: %TRUE if protocol images are made available by the store,
1070  * %FALSE otherwise
1071  */
1072 gboolean
1073 empathy_persona_store_get_show_protocols (EmpathyPersonaStore *self)
1074 {
1075   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), TRUE);
1076
1077   return GET_PRIV (self)->show_protocols;
1078 }
1079
1080 /**
1081  * empathy_persona_store_set_show_protocols:
1082  * @self: an #EmpathyPersonaStore
1083  * @show_protocols: %TRUE to make protocol images available through the store,
1084  * %FALSE otherwise
1085  *
1086  * Set #EmpathyPersonaStore:show-protocols to @show_protocols.
1087  */
1088 void
1089 empathy_persona_store_set_show_protocols (EmpathyPersonaStore *self,
1090     gboolean show_protocols)
1091 {
1092   EmpathyPersonaStorePriv *priv;
1093
1094   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1095
1096   priv = GET_PRIV (self);
1097   priv->show_protocols = show_protocols;
1098
1099   gtk_tree_model_foreach (GTK_TREE_MODEL (self),
1100       (GtkTreeModelForeachFunc) update_list_mode_foreach, self);
1101
1102   g_object_notify (G_OBJECT (self), "show-protocols");
1103 }
1104
1105 /**
1106  * empathy_persona_store_get_sort_criterion:
1107  * @self: an #EmpathyPersonaStore
1108  *
1109  * Get the value of #EmpathyPersonaStore:sort-criterion.
1110  *
1111  * Return value: the criterion used to sort the personas in the store
1112  */
1113 EmpathyPersonaStoreSort
1114 empathy_persona_store_get_sort_criterion (EmpathyPersonaStore *self)
1115 {
1116   g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (self), 0);
1117
1118   return GET_PRIV (self)->sort_criterion;
1119 }
1120
1121 /**
1122  * empathy_persona_store_set_sort_criterion:
1123  * @self: an #EmpathyPersonaStore
1124  * @show_avatars: a criterion to be used to sort personas in the store
1125  *
1126  * Set #EmpathyPersonaStore:sort-criterion to @sort_criterion.
1127  */
1128 void
1129 empathy_persona_store_set_sort_criterion (EmpathyPersonaStore *self,
1130     EmpathyPersonaStoreSort sort_criterion)
1131 {
1132   EmpathyPersonaStorePriv *priv;
1133
1134   g_return_if_fail (EMPATHY_IS_PERSONA_STORE (self));
1135
1136   priv = GET_PRIV (self);
1137   priv->sort_criterion = sort_criterion;
1138
1139   switch (sort_criterion)
1140     {
1141       case EMPATHY_PERSONA_STORE_SORT_STATE:
1142         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1143             EMPATHY_PERSONA_STORE_COL_STATUS, GTK_SORT_ASCENDING);
1144         break;
1145       case EMPATHY_PERSONA_STORE_SORT_NAME:
1146         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
1147             EMPATHY_PERSONA_STORE_COL_NAME, GTK_SORT_ASCENDING);
1148         break;
1149       default:
1150         g_assert_not_reached ();
1151         break;
1152     }
1153
1154   g_object_notify (G_OBJECT (self), "sort-criterion");
1155 }