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