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