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