]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact.c
empathy_contact_from_tpl_contact: check the accounts match
[empathy.git] / libempathy / empathy-contact.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25
26 #include <glib/gi18n-lib.h>
27
28 #include <telepathy-glib/account-manager.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-yell/telepathy-yell.h>
32
33 #include <telepathy-logger/log-manager.h>
34
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
37
38 #ifdef HAVE_GEOCODE
39 #include <geocode-glib/geocode-glib.h>
40 #endif
41
42 #include "empathy-contact.h"
43 #include "empathy-individual-manager.h"
44 #include "empathy-utils.h"
45 #include "empathy-enum-types.h"
46 #include "empathy-marshal.h"
47 #include "empathy-location.h"
48
49 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
50 #include "empathy-debug.h"
51
52 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
53 typedef struct {
54   TpContact *tp_contact;
55   TpAccount *account;
56   FolksPersona *persona;
57   gchar *id;
58   gchar *alias;
59   EmpathyAvatar *avatar;
60   TpConnectionPresenceType presence;
61   guint handle;
62   EmpathyCapabilities capabilities;
63   gboolean is_user;
64   guint hash;
65   /* Location is composed of string keys and GValues.
66    * Example: a "city" key would have "Helsinki" as string GValue,
67    *          a "latitude" would have 65.0 as double GValue.
68    *
69    * This is a super set of the location stored in TpContact as we can try add
70    * more fields by searching the address using geoclue.
71    */
72   GHashTable *location;
73   GeeHashSet *groups;
74   gchar **client_types;
75 } EmpathyContactPriv;
76
77 static void contact_finalize (GObject *object);
78 static void contact_get_property (GObject *object, guint param_id,
79     GValue *value, GParamSpec *pspec);
80 static void contact_set_property (GObject *object, guint param_id,
81     const GValue *value, GParamSpec *pspec);
82
83 #ifdef HAVE_GEOCODE
84 static void update_geocode (EmpathyContact *contact);
85 #endif
86
87 static void empathy_contact_set_location (EmpathyContact *contact,
88     GHashTable *location);
89
90 static void contact_set_client_types (EmpathyContact *contact,
91     const gchar * const *types);
92
93 static void set_capabilities_from_tp_caps (EmpathyContact *self,
94     TpCapabilities *caps);
95
96 static void contact_set_avatar (EmpathyContact *contact,
97     EmpathyAvatar *avatar);
98 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
99 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
100     const gchar *token);
101
102 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
103
104 enum
105 {
106   PROP_0,
107   PROP_TP_CONTACT,
108   PROP_ACCOUNT,
109   PROP_PERSONA,
110   PROP_ID,
111   PROP_ALIAS,
112   PROP_AVATAR,
113   PROP_PRESENCE,
114   PROP_PRESENCE_MESSAGE,
115   PROP_HANDLE,
116   PROP_CAPABILITIES,
117   PROP_IS_USER,
118   PROP_LOCATION,
119   PROP_CLIENT_TYPES
120 };
121
122 enum {
123   PRESENCE_CHANGED,
124   LAST_SIGNAL
125 };
126
127 static guint signals[LAST_SIGNAL];
128
129 /* TpContact* -> EmpathyContact*, both borrowed ref */
130 static GHashTable *contacts_table = NULL;
131
132 static void
133 tp_contact_notify_cb (TpContact *tp_contact,
134                       GParamSpec *param,
135                       GObject *contact)
136 {
137   EmpathyContactPriv *priv = GET_PRIV (contact);
138
139   /* Forward property notifications */
140   if (!tp_strdiff (param->name, "alias"))
141     g_object_notify (contact, "alias");
142   else if (!tp_strdiff (param->name, "presence-type")) {
143     TpConnectionPresenceType presence;
144
145     presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
146     g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
147       priv->presence);
148     priv->presence = presence;
149     g_object_notify (contact, "presence");
150   }
151   else if (!tp_strdiff (param->name, "identifier"))
152     g_object_notify (contact, "id");
153   else if (!tp_strdiff (param->name, "handle"))
154     g_object_notify (contact, "handle");
155   else if (!tp_strdiff (param->name, "location"))
156     {
157       GHashTable *location;
158
159       location = tp_contact_get_location (tp_contact);
160       /* This will start a geoclue search to find the address if needed */
161       empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
162     }
163   else if (!tp_strdiff (param->name, "capabilities"))
164     {
165       set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
166           tp_contact_get_capabilities (tp_contact));
167     }
168   else if (!tp_strdiff (param->name, "avatar-file"))
169     {
170       contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
171     }
172   else if (!tp_strdiff (param->name, "client-types"))
173     {
174       contact_set_client_types (EMPATHY_CONTACT (contact),
175           tp_contact_get_client_types (tp_contact));
176     }
177 }
178
179 static void
180 folks_persona_notify_cb (FolksPersona *folks_persona,
181                          GParamSpec *param,
182                          GObject *contact)
183 {
184   if (!tp_strdiff (param->name, "presence-message"))
185     g_object_notify (contact, "presence-message");
186 }
187
188 static void
189 contact_dispose (GObject *object)
190 {
191   EmpathyContactPriv *priv = GET_PRIV (object);
192
193   if (priv->tp_contact != NULL)
194     {
195       g_signal_handlers_disconnect_by_func (priv->tp_contact,
196           tp_contact_notify_cb, object);
197     }
198   tp_clear_object (&priv->tp_contact);
199
200   if (priv->account)
201     g_object_unref (priv->account);
202   priv->account = NULL;
203
204   if (priv->persona)
205     {
206       g_signal_handlers_disconnect_by_func (priv->persona,
207           folks_persona_notify_cb, object);
208       g_object_unref (priv->persona);
209     }
210   priv->persona = NULL;
211
212   if (priv->avatar != NULL)
213     {
214       empathy_avatar_unref (priv->avatar);
215       priv->avatar = NULL;
216     }
217
218   if (priv->location != NULL)
219     {
220       g_hash_table_unref (priv->location);
221       priv->location = NULL;
222     }
223
224   G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
225 }
226
227 static void
228 contact_constructed (GObject *object)
229 {
230   EmpathyContact *contact = (EmpathyContact *) object;
231   EmpathyContactPriv *priv = GET_PRIV (contact);
232   GHashTable *location;
233   TpHandle self_handle;
234   TpHandle handle;
235   const gchar * const *client_types;
236
237   if (priv->tp_contact == NULL)
238     return;
239
240   priv->presence = empathy_contact_get_presence (contact);
241
242   location = tp_contact_get_location (priv->tp_contact);
243   if (location != NULL)
244     empathy_contact_set_location (contact, location);
245
246   client_types = tp_contact_get_client_types (priv->tp_contact);
247   if (client_types != NULL)
248     contact_set_client_types (contact, client_types);
249
250   set_capabilities_from_tp_caps (contact,
251       tp_contact_get_capabilities (priv->tp_contact));
252
253   contact_set_avatar_from_tp_contact (contact);
254
255   /* Set is-user property. Note that it could still be the handle is
256    * different from the connection's self handle, in the case the handle
257    * comes from a group interface. */
258   self_handle = tp_connection_get_self_handle (
259       tp_contact_get_connection (priv->tp_contact));
260   handle = tp_contact_get_handle (priv->tp_contact);
261   empathy_contact_set_is_user (contact, self_handle == handle);
262
263   g_signal_connect (priv->tp_contact, "notify",
264     G_CALLBACK (tp_contact_notify_cb), contact);
265 }
266
267 static void
268 empathy_contact_class_init (EmpathyContactClass *class)
269 {
270   GObjectClass *object_class;
271
272   object_class = G_OBJECT_CLASS (class);
273
274   object_class->finalize = contact_finalize;
275   object_class->dispose = contact_dispose;
276   object_class->get_property = contact_get_property;
277   object_class->set_property = contact_set_property;
278   object_class->constructed = contact_constructed;
279
280   g_object_class_install_property (object_class,
281       PROP_TP_CONTACT,
282       g_param_spec_object ("tp-contact",
283         "TpContact",
284         "The TpContact associated with the contact",
285         TP_TYPE_CONTACT,
286         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287
288   g_object_class_install_property (object_class,
289       PROP_ACCOUNT,
290       g_param_spec_object ("account",
291         "The account",
292         "The account associated with the contact",
293         TP_TYPE_ACCOUNT,
294         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295
296   g_object_class_install_property (object_class,
297       PROP_PERSONA,
298       g_param_spec_object ("persona",
299         "Persona",
300         "The FolksPersona associated with the contact",
301         FOLKS_TYPE_PERSONA,
302         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303
304   g_object_class_install_property (object_class,
305       PROP_ID,
306       g_param_spec_string ("id",
307         "Contact id",
308         "String identifying contact",
309         NULL,
310         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311
312   g_object_class_install_property (object_class,
313       PROP_ALIAS,
314       g_param_spec_string ("alias",
315         "Contact alias",
316         "An alias for the contact",
317         NULL,
318         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319
320   g_object_class_install_property (object_class,
321       PROP_AVATAR,
322       g_param_spec_boxed ("avatar",
323         "Avatar image",
324         "The avatar image",
325         EMPATHY_TYPE_AVATAR,
326         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327
328   g_object_class_install_property (object_class,
329       PROP_PRESENCE,
330       g_param_spec_uint ("presence",
331         "Contact presence",
332         "Presence of contact",
333         TP_CONNECTION_PRESENCE_TYPE_UNSET,
334         NUM_TP_CONNECTION_PRESENCE_TYPES,
335         TP_CONNECTION_PRESENCE_TYPE_UNSET,
336         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337
338   g_object_class_install_property (object_class,
339       PROP_PRESENCE_MESSAGE,
340       g_param_spec_string ("presence-message",
341         "Contact presence message",
342         "Presence message of contact",
343         NULL,
344         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345
346   g_object_class_install_property (object_class,
347       PROP_HANDLE,
348       g_param_spec_uint ("handle",
349         "Contact Handle",
350         "The handle of the contact",
351         0,
352         G_MAXUINT,
353         0,
354         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355
356   g_object_class_install_property (object_class,
357       PROP_CAPABILITIES,
358       g_param_spec_flags ("capabilities",
359         "Contact Capabilities",
360         "Capabilities of the contact",
361         EMPATHY_TYPE_CAPABILITIES,
362         EMPATHY_CAPABILITIES_UNKNOWN,
363         G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364
365   g_object_class_install_property (object_class,
366       PROP_IS_USER,
367       g_param_spec_boolean ("is-user",
368         "Contact is-user",
369         "Is contact the user",
370         FALSE,
371         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372
373
374   g_object_class_install_property (object_class,
375       PROP_LOCATION,
376       g_param_spec_boxed ("location",
377         "Contact location",
378         "Physical location of the contact",
379         G_TYPE_HASH_TABLE,
380         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
381
382   g_object_class_install_property (object_class,
383       PROP_CLIENT_TYPES,
384       g_param_spec_boxed ("client-types",
385         "Contact client types",
386         "Client types of the contact",
387         G_TYPE_STRV,
388         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
389
390   signals[PRESENCE_CHANGED] =
391     g_signal_new ("presence-changed",
392                   G_TYPE_FROM_CLASS (class),
393                   G_SIGNAL_RUN_LAST,
394                   0,
395                   NULL, NULL,
396                   _empathy_marshal_VOID__UINT_UINT,
397                   G_TYPE_NONE,
398                   2, G_TYPE_UINT,
399                   G_TYPE_UINT);
400
401   g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
402 }
403
404 static void
405 empathy_contact_init (EmpathyContact *contact)
406 {
407   EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
408     EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
409
410   contact->priv = priv;
411
412   priv->location = NULL;
413   priv->client_types = NULL;
414   priv->groups = NULL;
415 }
416
417 static void
418 contact_finalize (GObject *object)
419 {
420   EmpathyContactPriv *priv;
421
422   priv = GET_PRIV (object);
423
424   DEBUG ("finalize: %p", object);
425
426   g_clear_object (&priv->groups);
427   g_free (priv->alias);
428   g_free (priv->id);
429   g_strfreev (priv->client_types);
430
431   G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
432 }
433
434 static void
435 empathy_contact_set_capabilities (EmpathyContact *contact,
436                                   EmpathyCapabilities capabilities)
437 {
438   EmpathyContactPriv *priv;
439
440   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
441
442   priv = GET_PRIV (contact);
443
444   if (priv->capabilities == capabilities)
445     return;
446
447   priv->capabilities = capabilities;
448
449   g_object_notify (G_OBJECT (contact), "capabilities");
450 }
451
452 static void
453 empathy_contact_set_id (EmpathyContact *contact,
454                         const gchar *id)
455 {
456   EmpathyContactPriv *priv;
457
458   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
459   g_return_if_fail (id != NULL);
460
461   priv = GET_PRIV (contact);
462
463   /* We temporally ref the contact because it could be destroyed
464    * during the signal emition */
465   g_object_ref (contact);
466   if (tp_strdiff (id, priv->id))
467     {
468       g_free (priv->id);
469       priv->id = g_strdup (id);
470
471       g_object_notify (G_OBJECT (contact), "id");
472       if (EMP_STR_EMPTY (priv->alias))
473           g_object_notify (G_OBJECT (contact), "alias");
474     }
475
476   g_object_unref (contact);
477 }
478
479 static void
480 empathy_contact_set_presence (EmpathyContact *contact,
481                               TpConnectionPresenceType presence)
482 {
483   EmpathyContactPriv *priv;
484   TpConnectionPresenceType old_presence;
485
486   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
487
488   priv = GET_PRIV (contact);
489
490   if (presence == priv->presence)
491     return;
492
493   old_presence = priv->presence;
494   priv->presence = presence;
495
496   g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
497
498   g_object_notify (G_OBJECT (contact), "presence");
499 }
500
501 static void
502 empathy_contact_set_presence_message (EmpathyContact *contact,
503                                       const gchar *message)
504 {
505   EmpathyContactPriv *priv = GET_PRIV (contact);
506
507   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
508
509   if (priv->persona != NULL)
510     {
511       folks_presence_details_set_presence_message (
512           FOLKS_PRESENCE_DETAILS (priv->persona), message);
513     }
514 }
515
516 static void
517 empathy_contact_set_handle (EmpathyContact *contact,
518                             guint handle)
519 {
520   EmpathyContactPriv *priv;
521
522   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
523
524   priv = GET_PRIV (contact);
525
526   g_object_ref (contact);
527   if (handle != priv->handle)
528     {
529       priv->handle = handle;
530       g_object_notify (G_OBJECT (contact), "handle");
531     }
532   g_object_unref (contact);
533 }
534
535 static void
536 contact_get_property (GObject *object,
537                       guint param_id,
538                       GValue *value,
539                       GParamSpec *pspec)
540 {
541   EmpathyContact *contact = EMPATHY_CONTACT (object);
542
543   switch (param_id)
544     {
545       case PROP_TP_CONTACT:
546         g_value_set_object (value, empathy_contact_get_tp_contact (contact));
547         break;
548       case PROP_ACCOUNT:
549         g_value_set_object (value, empathy_contact_get_account (contact));
550         break;
551       case PROP_PERSONA:
552         g_value_set_object (value, empathy_contact_get_persona (contact));
553         break;
554       case PROP_ID:
555         g_value_set_string (value, empathy_contact_get_id (contact));
556         break;
557       case PROP_ALIAS:
558         g_value_set_string (value, empathy_contact_get_alias (contact));
559         break;
560       case PROP_AVATAR:
561         g_value_set_boxed (value, empathy_contact_get_avatar (contact));
562         break;
563       case PROP_PRESENCE:
564         g_value_set_uint (value, empathy_contact_get_presence (contact));
565         break;
566       case PROP_PRESENCE_MESSAGE:
567         g_value_set_string (value, empathy_contact_get_presence_message (contact));
568         break;
569       case PROP_HANDLE:
570         g_value_set_uint (value, empathy_contact_get_handle (contact));
571         break;
572       case PROP_CAPABILITIES:
573         g_value_set_flags (value, empathy_contact_get_capabilities (contact));
574         break;
575       case PROP_IS_USER:
576         g_value_set_boolean (value, empathy_contact_is_user (contact));
577         break;
578       default:
579         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
580         break;
581     };
582 }
583
584 static void
585 contact_set_property (GObject *object,
586                       guint param_id,
587                       const GValue *value,
588                       GParamSpec *pspec)
589 {
590   EmpathyContact *contact = EMPATHY_CONTACT (object);
591   EmpathyContactPriv *priv = GET_PRIV (object);
592
593   switch (param_id)
594     {
595       case PROP_TP_CONTACT:
596         priv->tp_contact = g_value_dup_object (value);
597         break;
598       case PROP_ACCOUNT:
599         g_assert (priv->account == NULL);
600         priv->account = g_value_dup_object (value);
601         break;
602       case PROP_PERSONA:
603         empathy_contact_set_persona (contact, g_value_get_object (value));
604         break;
605       case PROP_ID:
606         empathy_contact_set_id (contact, g_value_get_string (value));
607         break;
608       case PROP_ALIAS:
609         empathy_contact_set_alias (contact, g_value_get_string (value));
610         break;
611       case PROP_PRESENCE:
612         empathy_contact_set_presence (contact, g_value_get_uint (value));
613         break;
614       case PROP_PRESENCE_MESSAGE:
615         empathy_contact_set_presence_message (contact, g_value_get_string (value));
616         break;
617       case PROP_HANDLE:
618         empathy_contact_set_handle (contact, g_value_get_uint (value));
619         break;
620       case PROP_CAPABILITIES:
621         empathy_contact_set_capabilities (contact, g_value_get_flags (value));
622         break;
623       case PROP_IS_USER:
624         empathy_contact_set_is_user (contact, g_value_get_boolean (value));
625         break;
626       default:
627         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
628         break;
629     };
630 }
631
632 static void
633 remove_tp_contact (gpointer data,
634     GObject *object)
635 {
636   g_hash_table_remove (contacts_table, data);
637 }
638
639 static EmpathyContact *
640 empathy_contact_new (TpContact *tp_contact)
641 {
642   EmpathyContact *retval;
643
644   g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
645
646   retval = g_object_new (EMPATHY_TYPE_CONTACT,
647       "tp-contact", tp_contact,
648       NULL);
649
650   g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
651
652   return retval;
653 }
654
655 typedef struct
656 {
657   TplEntity *entity;
658   TpAccount *account;
659 } FindContactData;
660
661 static gboolean
662 contact_is_tpl_entity (gpointer key,
663     gpointer value,
664     gpointer user_data)
665 {
666   EmpathyContact *contact = value;
667   FindContactData *data = user_data;
668
669   return !tp_strdiff (empathy_contact_get_id (contact),
670               tpl_entity_get_identifier (data->entity)) &&
671          !tp_strdiff (tp_proxy_get_object_path (data->account),
672               tp_proxy_get_object_path (
673                   empathy_contact_get_account (contact)));
674 }
675
676 EmpathyContact *
677 empathy_contact_from_tpl_contact (TpAccount *account,
678     TplEntity *tpl_entity)
679 {
680   EmpathyContact *retval;
681   gboolean is_user;
682   EmpathyContact *existing_contact = NULL;
683
684   g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
685
686   if (contacts_table != NULL)
687     {
688       FindContactData data;
689
690       data.entity = tpl_entity;
691       data.account = account;
692
693       existing_contact = g_hash_table_find (contacts_table,
694         contact_is_tpl_entity, &data);
695     }
696
697   if (existing_contact != NULL)
698     {
699       EmpathyContactPriv *priv;
700
701       retval = g_object_new (EMPATHY_TYPE_CONTACT,
702           "tp-contact", empathy_contact_get_tp_contact (existing_contact),
703           NULL);
704
705       priv = GET_PRIV (retval);
706
707       /* contact_set_property() calls empathy_contact_set_alias(), which
708        * tries to set the alias on the FolksPersona, but we don't want to
709        * do that when creating an EmpathyContact from a TplEntity. So just
710        * set priv->alias instead of passing it to g_object_new() instead. */
711       g_free (priv->alias);
712       priv->alias = g_strdup (tpl_entity_get_alias (tpl_entity));
713     }
714   else
715     {
716       is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
717
718       retval = g_object_new (EMPATHY_TYPE_CONTACT,
719           "id", tpl_entity_get_identifier (tpl_entity),
720           "alias", tpl_entity_get_alias (tpl_entity),
721           "account", account,
722           "is-user", is_user,
723           NULL);
724     }
725
726   if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
727     contact_load_avatar_cache (retval,
728         tpl_entity_get_avatar_token (tpl_entity));
729
730   return retval;
731 }
732
733 TpContact *
734 empathy_contact_get_tp_contact (EmpathyContact *contact)
735 {
736   EmpathyContactPriv *priv;
737
738   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
739
740   priv = GET_PRIV (contact);
741
742   return priv->tp_contact;
743 }
744
745 const gchar *
746 empathy_contact_get_id (EmpathyContact *contact)
747 {
748   EmpathyContactPriv *priv;
749
750   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
751
752   priv = GET_PRIV (contact);
753
754   if (priv->tp_contact != NULL)
755     return tp_contact_get_identifier (priv->tp_contact);
756
757   return priv->id;
758 }
759
760 const gchar *
761 empathy_contact_get_alias (EmpathyContact *contact)
762 {
763   EmpathyContactPriv *priv;
764   const gchar        *alias = NULL;
765
766   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
767
768   priv = GET_PRIV (contact);
769
770   if (!EMP_STR_EMPTY (priv->alias))
771     alias = priv->alias;
772   else if (priv->tp_contact != NULL)
773     alias = tp_contact_get_alias (priv->tp_contact);
774
775   if (!EMP_STR_EMPTY (alias))
776     return alias;
777   else
778     return empathy_contact_get_id (contact);
779 }
780
781 void
782 empathy_contact_set_alias (EmpathyContact *contact,
783                           const gchar *alias)
784 {
785   EmpathyContactPriv *priv;
786   FolksPersona *persona;
787
788   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
789
790   priv = GET_PRIV (contact);
791
792   g_object_ref (contact);
793
794   /* Set the alias on the persona if possible */
795   persona = empathy_contact_get_persona (contact);
796   if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
797     {
798       DEBUG ("Setting alias for contact %s to %s",
799           empathy_contact_get_id (contact), alias);
800
801       folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
802     }
803
804   if (tp_strdiff (alias, priv->alias))
805     {
806       g_free (priv->alias);
807       priv->alias = g_strdup (alias);
808       g_object_notify (G_OBJECT (contact), "alias");
809     }
810
811   g_object_unref (contact);
812 }
813
814 static void
815 groups_change_group_cb (GObject *source,
816     GAsyncResult *result,
817     gpointer user_data)
818 {
819   FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
820   GError *error = NULL;
821
822   folks_group_details_change_group_finish (group_details, result, &error);
823   if (error != NULL)
824     {
825       g_warning ("failed to change group: %s", error->message);
826       g_clear_error (&error);
827     }
828 }
829
830 void
831 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
832     gboolean is_member)
833 {
834   EmpathyContactPriv *priv;
835   FolksPersona *persona;
836
837   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
838   g_return_if_fail (group != NULL);
839
840   priv = GET_PRIV (contact);
841
842   /* Normally pass through the changes to the persona */
843   persona = empathy_contact_get_persona (contact);
844   if (persona != NULL)
845     {
846       if (FOLKS_IS_GROUP_DETAILS (persona))
847         folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
848             is_member, groups_change_group_cb, contact);
849       return;
850     }
851
852   /* If the persona doesn't exist yet, we have to cache the changes until it
853    * does */
854   if (priv->groups == NULL)
855     {
856       priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
857           g_free, g_str_hash, g_str_equal);
858     }
859
860   gee_collection_add (GEE_COLLECTION (priv->groups), group);
861 }
862
863 EmpathyAvatar *
864 empathy_contact_get_avatar (EmpathyContact *contact)
865 {
866   EmpathyContactPriv *priv;
867
868   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
869
870   priv = GET_PRIV (contact);
871
872   return priv->avatar;
873 }
874
875 static void
876 contact_set_avatar (EmpathyContact *contact,
877                     EmpathyAvatar *avatar)
878 {
879   EmpathyContactPriv *priv;
880
881   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
882
883   priv = GET_PRIV (contact);
884
885   if (priv->avatar == avatar)
886     return;
887
888   if (priv->avatar)
889     {
890       empathy_avatar_unref (priv->avatar);
891       priv->avatar = NULL;
892     }
893
894   if (avatar)
895       priv->avatar = empathy_avatar_ref (avatar);
896
897   g_object_notify (G_OBJECT (contact), "avatar");
898 }
899
900 TpAccount *
901 empathy_contact_get_account (EmpathyContact *contact)
902 {
903   EmpathyContactPriv *priv;
904
905   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
906
907   priv = GET_PRIV (contact);
908
909   if (priv->account == NULL && priv->tp_contact != NULL)
910     {
911       TpConnection *connection;
912
913       /* FIXME: This assume the account manager already exists */
914       connection = tp_contact_get_connection (priv->tp_contact);
915       priv->account =
916         g_object_ref (empathy_get_account_for_connection (connection));
917     }
918
919   return priv->account;
920 }
921
922 FolksPersona *
923 empathy_contact_get_persona (EmpathyContact *contact)
924 {
925   EmpathyContactPriv *priv;
926
927   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
928
929   priv = GET_PRIV (contact);
930
931   if (priv->persona == NULL && priv->tp_contact != NULL)
932     {
933       /* FIXME: This is disgustingly slow */
934       /* Query for the persona */
935       EmpathyIndividualManager *manager;
936       GList *individuals, *l;
937
938       manager = empathy_individual_manager_dup_singleton ();
939       individuals = empathy_individual_manager_get_members (manager);
940
941       for (l = individuals; l != NULL; l = l->next)
942         {
943           FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
944           GeeSet *personas;
945           GeeIterator *iter;
946           gboolean persona_found = FALSE;
947
948           personas = folks_individual_get_personas (individual);
949           iter = gee_iterable_iterator (GEE_ITERABLE (personas));
950           while (!persona_found && gee_iterator_next (iter))
951             {
952               TpfPersona *persona = gee_iterator_get (iter);
953
954               if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
955                 {
956                   TpContact *tp_contact = tpf_persona_get_contact (persona);
957
958                   if (tp_contact == priv->tp_contact)
959                     {
960                       /* Found the right persona */
961                       empathy_contact_set_persona (contact,
962                           (FolksPersona *) persona);
963                       persona_found = TRUE;
964                     }
965                   g_clear_object (&persona);
966                 }
967             }
968           g_clear_object (&iter);
969         }
970
971       g_list_free (individuals);
972       g_object_unref (manager);
973     }
974
975   return priv->persona;
976 }
977
978 void
979 empathy_contact_set_persona (EmpathyContact *contact,
980     FolksPersona *persona)
981 {
982   EmpathyContactPriv *priv;
983
984   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
985   g_return_if_fail (TPF_IS_PERSONA (persona));
986
987   priv = GET_PRIV (contact);
988
989   if (persona == priv->persona)
990     return;
991
992   if (priv->persona != NULL)
993     {
994       g_signal_handlers_disconnect_by_func (priv->persona,
995           folks_persona_notify_cb, contact);
996       g_object_unref (priv->persona);
997     }
998   priv->persona = g_object_ref (persona);
999
1000   g_signal_connect (priv->persona, "notify",
1001     G_CALLBACK (folks_persona_notify_cb), contact);
1002
1003   g_object_notify (G_OBJECT (contact), "persona");
1004
1005   /* Set the persona's alias, since ours could've been set using
1006    * empathy_contact_set_alias() before we had a persona; this happens when
1007    * adding a contact. */
1008   if (priv->alias != NULL)
1009     empathy_contact_set_alias (contact, priv->alias);
1010
1011   /* Set the persona's groups */
1012   if (priv->groups != NULL)
1013     {
1014       folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1015           GEE_SET (priv->groups));
1016       g_object_unref (priv->groups);
1017       priv->groups = NULL;
1018     }
1019 }
1020
1021 TpConnection *
1022 empathy_contact_get_connection (EmpathyContact *contact)
1023 {
1024   EmpathyContactPriv *priv;
1025
1026   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1027
1028   priv = GET_PRIV (contact);
1029
1030   if (priv->tp_contact != NULL)
1031     return tp_contact_get_connection (priv->tp_contact);
1032
1033   return NULL;
1034 }
1035
1036 TpConnectionPresenceType
1037 empathy_contact_get_presence (EmpathyContact *contact)
1038 {
1039   EmpathyContactPriv *priv;
1040
1041   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1042     TP_CONNECTION_PRESENCE_TYPE_UNSET);
1043
1044   priv = GET_PRIV (contact);
1045
1046   if (priv->tp_contact != NULL)
1047     return tp_contact_get_presence_type (priv->tp_contact);
1048
1049   return priv->presence;
1050 }
1051
1052 const gchar *
1053 empathy_contact_get_presence_message (EmpathyContact *contact)
1054 {
1055   EmpathyContactPriv *priv;
1056
1057   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1058
1059   priv = GET_PRIV (contact);
1060
1061   if (priv->persona != NULL)
1062     return folks_presence_details_get_presence_message (
1063         FOLKS_PRESENCE_DETAILS (priv->persona));
1064
1065   if (priv->tp_contact != NULL)
1066     return tp_contact_get_presence_message (priv->tp_contact);
1067
1068   return NULL;
1069 }
1070
1071 guint
1072 empathy_contact_get_handle (EmpathyContact *contact)
1073 {
1074   EmpathyContactPriv *priv;
1075
1076   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1077
1078   priv = GET_PRIV (contact);
1079
1080   if (priv->tp_contact != NULL)
1081     return tp_contact_get_handle (priv->tp_contact);
1082
1083   return priv->handle;
1084 }
1085
1086 EmpathyCapabilities
1087 empathy_contact_get_capabilities (EmpathyContact *contact)
1088 {
1089   EmpathyContactPriv *priv;
1090
1091   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1092
1093   priv = GET_PRIV (contact);
1094
1095   return priv->capabilities;
1096 }
1097
1098 gboolean
1099 empathy_contact_is_user (EmpathyContact *contact)
1100 {
1101   EmpathyContactPriv *priv;
1102
1103   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1104
1105   priv = GET_PRIV (contact);
1106
1107   return priv->is_user;
1108 }
1109
1110 void
1111 empathy_contact_set_is_user (EmpathyContact *contact,
1112                              gboolean is_user)
1113 {
1114   EmpathyContactPriv *priv;
1115
1116   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1117
1118   priv = GET_PRIV (contact);
1119
1120   if (priv->is_user == is_user)
1121     return;
1122
1123   priv->is_user = is_user;
1124
1125   g_object_notify (G_OBJECT (contact), "is-user");
1126 }
1127
1128 gboolean
1129 empathy_contact_is_online (EmpathyContact *contact)
1130 {
1131   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1132
1133   switch (empathy_contact_get_presence (contact))
1134     {
1135       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1136       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1137       case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1138         return FALSE;
1139       /* Contacts without presence are considered online so we can display IRC
1140        * contacts in rooms. */
1141       case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1142       case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1143       case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1144       case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1145       case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1146       case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1147       default:
1148         return TRUE;
1149     }
1150 }
1151
1152 const gchar *
1153 empathy_contact_get_status (EmpathyContact *contact)
1154 {
1155   const gchar *message;
1156
1157   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1158
1159   message = empathy_contact_get_presence_message (contact);
1160   if (!EMP_STR_EMPTY (message))
1161     return message;
1162
1163   return empathy_presence_get_default_message (
1164       empathy_contact_get_presence (contact));
1165 }
1166
1167 gboolean
1168 empathy_contact_can_sms (EmpathyContact *contact)
1169 {
1170   EmpathyContactPriv *priv;
1171
1172   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1173
1174   priv = GET_PRIV (contact);
1175
1176   return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1177 }
1178
1179 gboolean
1180 empathy_contact_can_voip (EmpathyContact *contact)
1181 {
1182   EmpathyContactPriv *priv;
1183
1184   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1185
1186   priv = GET_PRIV (contact);
1187
1188   return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1189       EMPATHY_CAPABILITIES_VIDEO);
1190 }
1191
1192 gboolean
1193 empathy_contact_can_voip_audio (EmpathyContact *contact)
1194 {
1195   EmpathyContactPriv *priv;
1196
1197   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1198
1199   priv = GET_PRIV (contact);
1200
1201   return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1202 }
1203
1204 gboolean
1205 empathy_contact_can_voip_video (EmpathyContact *contact)
1206 {
1207   EmpathyContactPriv *priv;
1208
1209   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1210
1211   priv = GET_PRIV (contact);
1212
1213   return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1214 }
1215
1216 gboolean
1217 empathy_contact_can_send_files (EmpathyContact *contact)
1218 {
1219   EmpathyContactPriv *priv;
1220
1221   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1222
1223   priv = GET_PRIV (contact);
1224
1225   return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1226 }
1227
1228 gboolean
1229 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1230 {
1231   EmpathyContactPriv *priv;
1232
1233   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1234
1235   priv = GET_PRIV (contact);
1236
1237   return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1238 }
1239
1240 static gboolean
1241 contact_has_log (EmpathyContact *contact)
1242 {
1243   TplLogManager *manager;
1244   TplEntity *entity;
1245   gboolean have_log;
1246
1247   manager = tpl_log_manager_dup_singleton ();
1248   entity = tpl_entity_new (empathy_contact_get_id (contact),
1249       TPL_ENTITY_CONTACT, NULL, NULL);
1250
1251   have_log = tpl_log_manager_exists (manager,
1252       empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1253
1254   g_object_unref (entity);
1255   g_object_unref (manager);
1256
1257   return have_log;
1258 }
1259
1260 gboolean
1261 empathy_contact_can_do_action (EmpathyContact *self,
1262     EmpathyActionType action_type)
1263 {
1264   gboolean sensitivity = FALSE;
1265
1266   switch (action_type)
1267     {
1268       case EMPATHY_ACTION_CHAT:
1269         sensitivity = TRUE;
1270         break;
1271       case EMPATHY_ACTION_SMS:
1272         sensitivity = empathy_contact_can_sms (self);
1273         break;
1274       case EMPATHY_ACTION_AUDIO_CALL:
1275         sensitivity = empathy_contact_can_voip_audio (self);
1276         break;
1277       case EMPATHY_ACTION_VIDEO_CALL:
1278         sensitivity = empathy_contact_can_voip_video (self);
1279         break;
1280       case EMPATHY_ACTION_VIEW_LOGS:
1281         sensitivity = contact_has_log (self);
1282         break;
1283       case EMPATHY_ACTION_SEND_FILE:
1284         sensitivity = empathy_contact_can_send_files (self);
1285         break;
1286       case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1287         sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1288         break;
1289       default:
1290         g_assert_not_reached ();
1291     }
1292
1293   return (sensitivity ? TRUE : FALSE);
1294 }
1295
1296 static gchar *
1297 contact_get_avatar_filename (EmpathyContact *contact,
1298                              const gchar *token)
1299 {
1300   TpAccount *account;
1301   gchar *avatar_path;
1302   gchar *avatar_file;
1303   gchar *token_escaped;
1304
1305   if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1306     return NULL;
1307
1308   token_escaped = tp_escape_as_identifier (token);
1309   account = empathy_contact_get_account (contact);
1310
1311   avatar_path = g_build_filename (g_get_user_cache_dir (),
1312       "telepathy",
1313       "avatars",
1314       tp_account_get_connection_manager (account),
1315       tp_account_get_protocol (account),
1316       NULL);
1317   g_mkdir_with_parents (avatar_path, 0700);
1318
1319   avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1320
1321   g_free (token_escaped);
1322   g_free (avatar_path);
1323
1324   return avatar_file;
1325 }
1326
1327 static gboolean
1328 contact_load_avatar_cache (EmpathyContact *contact,
1329                            const gchar *token)
1330 {
1331   EmpathyAvatar *avatar = NULL;
1332   gchar *filename;
1333   gchar *data = NULL;
1334   gsize len;
1335   GError *error = NULL;
1336
1337   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1338   g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1339
1340   /* Load the avatar from file if it exists */
1341   filename = contact_get_avatar_filename (contact, token);
1342   if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1343     {
1344       if (!g_file_get_contents (filename, &data, &len, &error))
1345         {
1346           DEBUG ("Failed to load avatar from cache: %s",
1347             error ? error->message : "No error given");
1348           g_clear_error (&error);
1349         }
1350     }
1351
1352   if (data != NULL)
1353     {
1354       DEBUG ("Avatar loaded from %s", filename);
1355       avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1356       contact_set_avatar (contact, avatar);
1357       empathy_avatar_unref (avatar);
1358     }
1359
1360   g_free (data);
1361   g_free (filename);
1362
1363   return data != NULL;
1364 }
1365
1366 GType
1367 empathy_avatar_get_type (void)
1368 {
1369   static GType type_id = 0;
1370
1371   if (!type_id)
1372     {
1373       type_id = g_boxed_type_register_static ("EmpathyAvatar",
1374           (GBoxedCopyFunc) empathy_avatar_ref,
1375           (GBoxedFreeFunc) empathy_avatar_unref);
1376     }
1377
1378   return type_id;
1379 }
1380
1381 /**
1382  * empathy_avatar_new:
1383  * @data: the avatar data
1384  * @len: the size of avatar data
1385  * @format: the mime type of the avatar image
1386  * @filename: the filename where the avatar is stored in cache
1387  *
1388  * Create a #EmpathyAvatar from the provided data.
1389  *
1390  * Returns: a new #EmpathyAvatar
1391  */
1392 EmpathyAvatar *
1393 empathy_avatar_new (const guchar *data,
1394                     gsize len,
1395                     const gchar *format,
1396                     const gchar *filename)
1397 {
1398   EmpathyAvatar *avatar;
1399
1400   avatar = g_slice_new0 (EmpathyAvatar);
1401   avatar->data = g_memdup (data, len);
1402   avatar->len = len;
1403   avatar->format = g_strdup (format);
1404   avatar->filename = g_strdup (filename);
1405   avatar->refcount = 1;
1406
1407   return avatar;
1408 }
1409
1410 void
1411 empathy_avatar_unref (EmpathyAvatar *avatar)
1412 {
1413   g_return_if_fail (avatar != NULL);
1414
1415   avatar->refcount--;
1416   if (avatar->refcount == 0)
1417     {
1418       g_free (avatar->data);
1419       g_free (avatar->format);
1420       g_free (avatar->filename);
1421       g_slice_free (EmpathyAvatar, avatar);
1422     }
1423 }
1424
1425 EmpathyAvatar *
1426 empathy_avatar_ref (EmpathyAvatar *avatar)
1427 {
1428   g_return_val_if_fail (avatar != NULL, NULL);
1429
1430   avatar->refcount++;
1431
1432   return avatar;
1433 }
1434
1435 /**
1436  * empathy_avatar_save_to_file:
1437  * @avatar: the avatar
1438  * @filename: name of a file to write avatar to
1439  * @error: return location for a GError, or NULL
1440  *
1441  * Save the avatar to a file named filename
1442  *
1443  * Returns: %TRUE on success, %FALSE if an error occurred
1444  */
1445 gboolean
1446 empathy_avatar_save_to_file (EmpathyAvatar *self,
1447                              const gchar *filename,
1448                              GError **error)
1449 {
1450   return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1451       error);
1452 }
1453
1454 /**
1455  * empathy_contact_get_location:
1456  * @contact: an #EmpathyContact
1457  *
1458  * Returns the user's location if available.  The keys are defined in
1459  * empathy-location.h. If the contact doesn't have location
1460  * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1461  * you are done with the #GHashTable.
1462  *
1463  * It is composed of string keys and GValues.  Keys are
1464  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1465  * Example: a "city" key would have "Helsinki" as string GValue,
1466  *          a "latitude" would have 65.0 as double GValue.
1467  *
1468  * Returns: a #GHashTable of location values
1469  */
1470 GHashTable *
1471 empathy_contact_get_location (EmpathyContact *contact)
1472 {
1473   EmpathyContactPriv *priv;
1474
1475   g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1476
1477   priv = GET_PRIV (contact);
1478
1479   return priv->location;
1480 }
1481
1482 /**
1483  * empathy_contact_set_location:
1484  * @contact: an #EmpathyContact
1485  * @location: a #GHashTable of the location
1486  *
1487  * Sets the user's location based on the location #GHashTable passed.
1488  * It is composed of string keys and GValues.  Keys are
1489  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1490  * Example: a "city" key would have "Helsinki" as string GValue,
1491  *          a "latitude" would have 65.0 as double GValue.
1492  */
1493 static void
1494 empathy_contact_set_location (EmpathyContact *contact,
1495     GHashTable *location)
1496 {
1497   EmpathyContactPriv *priv;
1498
1499   g_return_if_fail (EMPATHY_CONTACT (contact));
1500   g_return_if_fail (location != NULL);
1501
1502   priv = GET_PRIV (contact);
1503
1504   if (priv->location != NULL)
1505     g_hash_table_unref (priv->location);
1506
1507   priv->location = g_hash_table_ref (location);
1508 #ifdef HAVE_GEOCODE
1509   update_geocode (contact);
1510 #endif
1511   g_object_notify (G_OBJECT (contact), "location");
1512 }
1513
1514 const gchar * const *
1515 empathy_contact_get_client_types (EmpathyContact *contact)
1516 {
1517   EmpathyContactPriv *priv;
1518
1519   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1520
1521   priv = GET_PRIV (contact);
1522
1523   return (const gchar * const *) priv->client_types;
1524 }
1525
1526 static void
1527 contact_set_client_types (EmpathyContact *contact,
1528     const gchar * const *client_types)
1529 {
1530   EmpathyContactPriv *priv = GET_PRIV (contact);
1531
1532   if (priv->client_types != NULL)
1533     g_strfreev (priv->client_types);
1534
1535   priv->client_types = g_strdupv ((gchar **) client_types);
1536   g_object_notify (G_OBJECT (contact), "client-types");
1537 }
1538
1539 /**
1540  * empathy_contact_equal:
1541  * @contact1: an #EmpathyContact
1542  * @contact2: an #EmpathyContact
1543  *
1544  * Returns FALSE if one of the contacts is NULL but the other is not.
1545  * Otherwise returns TRUE if both pointer are equal or if they bith
1546  * refer to the same id.
1547  * It's only necessary to call this function if your contact objects
1548  * come from logs where contacts are created dynamically and comparing
1549  * pointers is not enough.
1550  */
1551 gboolean
1552 empathy_contact_equal (gconstpointer contact1,
1553                        gconstpointer contact2)
1554 {
1555   EmpathyContact *c1;
1556   EmpathyContact *c2;
1557   const gchar *id1;
1558   const gchar *id2;
1559
1560   if ((contact1 == NULL) != (contact2 == NULL)) {
1561     return FALSE;
1562   }
1563   if (contact1 == contact2) {
1564     return TRUE;
1565   }
1566   c1 = EMPATHY_CONTACT (contact1);
1567   c2 = EMPATHY_CONTACT (contact2);
1568   id1 = empathy_contact_get_id (c1);
1569   id2 = empathy_contact_get_id (c2);
1570   if (!tp_strdiff (id1, id2)) {
1571     return TRUE;
1572   }
1573   return FALSE;
1574 }
1575
1576 #ifdef HAVE_GEOCODE
1577 /* This callback is called by geocode-glib when it found a position
1578  * for the given address.  A position is necessary for a contact
1579  * to show up on the map
1580  */
1581 static void
1582 geocode_cb (GObject *source,
1583     GAsyncResult *result,
1584     gpointer user_data)
1585 {
1586   EmpathyContact *contact = user_data;
1587   EmpathyContactPriv *priv = GET_PRIV (contact);
1588   GError *error = NULL;
1589   GHashTable *new_location;
1590   GHashTable *resolved;
1591   gdouble latitude, longitude;
1592
1593   if (priv->location == NULL)
1594     goto out;
1595
1596   resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1597       &error);
1598
1599   if (resolved == NULL)
1600     {
1601       DEBUG ("Failed to resolve geocode: %s", error->message);
1602       g_error_free (error);
1603       goto out;
1604     }
1605
1606   if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1607     goto out;
1608
1609   new_location = tp_asv_new (
1610       EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1611       EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1612       NULL);
1613
1614   DEBUG ("\t - Latitude: %f", latitude);
1615   DEBUG ("\t - Longitude: %f", longitude);
1616
1617   /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1618    * the values we just set. */
1619   tp_g_hash_table_update (new_location, priv->location,
1620       (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1621
1622   /* Don't change the accuracy as we used an address to get this position */
1623   g_hash_table_unref (priv->location);
1624   priv->location = new_location;
1625   g_object_notify ((GObject *) contact, "location");
1626
1627 out:
1628   tp_clear_pointer (&result, g_hash_table_unref);
1629   g_object_unref (contact);
1630 }
1631
1632 static void
1633 update_geocode (EmpathyContact *contact)
1634 {
1635   GeocodeObject *geocode;
1636   GHashTable *location;
1637
1638   location = empathy_contact_get_location (contact);
1639   if (location == NULL)
1640     return;
1641
1642   /* No need to search for position if contact published it */
1643   if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1644       g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1645     return;
1646
1647   geocode = geocode_object_new_for_params (location);
1648
1649   geocode_object_resolve_async (geocode, NULL, geocode_cb,
1650       g_object_ref (contact));
1651
1652   g_object_unref (geocode);
1653 }
1654 #endif
1655
1656 static EmpathyCapabilities
1657 tp_caps_to_capabilities (TpCapabilities *caps)
1658 {
1659   EmpathyCapabilities capabilities = 0;
1660   guint i;
1661   GPtrArray *classes;
1662
1663   classes = tp_capabilities_get_channel_classes (caps);
1664
1665   for (i = 0; i < classes->len; i++)
1666     {
1667       GValueArray *class_struct;
1668       GHashTable *fixed_prop;
1669       GStrv allowed_prop;
1670       TpHandleType handle_type;
1671       const gchar *chan_type;
1672
1673       class_struct = g_ptr_array_index (classes, i);
1674       tp_value_array_unpack (class_struct, 2,
1675           &fixed_prop,
1676           &allowed_prop);
1677
1678       handle_type = tp_asv_get_uint32 (fixed_prop,
1679           TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1680       if (handle_type != TP_HANDLE_TYPE_CONTACT)
1681         continue;
1682
1683       chan_type = tp_asv_get_string (fixed_prop,
1684           TP_PROP_CHANNEL_CHANNEL_TYPE);
1685
1686       if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1687         {
1688           capabilities |= EMPATHY_CAPABILITIES_FT;
1689         }
1690       else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1691         {
1692           const gchar *service;
1693
1694           service = tp_asv_get_string (fixed_prop,
1695               TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1696
1697           if (!tp_strdiff (service, "rfb"))
1698             capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1699         }
1700       else if (!tp_strdiff (chan_type,
1701         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1702         {
1703           guint j;
1704
1705           for (j = 0; allowed_prop[j] != NULL; j++)
1706             {
1707               if (!tp_strdiff (allowed_prop[j],
1708                     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1709                 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1710               else if (!tp_strdiff (allowed_prop[j],
1711                     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1712                 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1713             }
1714
1715           if (tp_asv_get_boolean (fixed_prop,
1716                     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, NULL))
1717             capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1718           if (tp_asv_get_boolean (fixed_prop,
1719                     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, NULL))
1720             capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1721         }
1722       else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1723         {
1724           if (tp_asv_get_boolean (fixed_prop,
1725                 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL))
1726             capabilities |= EMPATHY_CAPABILITIES_SMS;
1727         }
1728       else if (!tp_strdiff (chan_type,
1729         TPY_IFACE_CHANNEL_TYPE_CALL))
1730         {
1731           guint j;
1732
1733           if (tp_asv_get_boolean (fixed_prop,
1734               TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, NULL))
1735             capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1736
1737           if (tp_asv_get_boolean (fixed_prop,
1738               TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, NULL))
1739             capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1740
1741           for (j = 0; allowed_prop[j] != NULL; j++)
1742             {
1743               if (!tp_strdiff (allowed_prop[j],
1744                     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO))
1745                 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1746               else if (!tp_strdiff (allowed_prop[j],
1747                     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO))
1748                 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1749             }
1750         }
1751     }
1752
1753   return capabilities;
1754 }
1755
1756 static void
1757 set_capabilities_from_tp_caps (EmpathyContact *self,
1758     TpCapabilities *caps)
1759 {
1760   EmpathyCapabilities capabilities;
1761
1762   if (caps == NULL)
1763     return;
1764
1765   capabilities = tp_caps_to_capabilities (caps);
1766   empathy_contact_set_capabilities (self, capabilities);
1767 }
1768
1769 static void
1770 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1771 {
1772   EmpathyContactPriv *priv = GET_PRIV (contact);
1773   const gchar *mime;
1774   GFile *file;
1775
1776   mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1777   file = tp_contact_get_avatar_file (priv->tp_contact);
1778
1779   if (file != NULL)
1780     {
1781       EmpathyAvatar *avatar;
1782       gchar *data;
1783       gsize len;
1784       gchar *path;
1785
1786       g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1787       path = g_file_get_path (file);
1788
1789       avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1790
1791       contact_set_avatar (contact, avatar);
1792       empathy_avatar_unref (avatar);
1793       g_free (path);
1794       g_free (data);
1795     }
1796   else
1797     {
1798       contact_set_avatar (contact, NULL);
1799     }
1800 }
1801
1802 EmpathyContact *
1803 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1804 {
1805   EmpathyContact *contact = NULL;
1806
1807   g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1808
1809   if (contacts_table == NULL)
1810     contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1811   else
1812     contact = g_hash_table_lookup (contacts_table, tp_contact);
1813
1814   if (contact == NULL)
1815     {
1816       contact = empathy_contact_new (tp_contact);
1817
1818       /* The hash table does not keep any ref.
1819        * contact keeps a ref to tp_contact, and is removed from the table in
1820        * contact_dispose() */
1821       g_hash_table_insert (contacts_table, tp_contact, contact);
1822     }
1823   else
1824     {
1825       g_object_ref (contact);
1826     }
1827
1828   return contact;
1829 }
1830
1831 static int
1832 presence_cmp_func (EmpathyContact *a,
1833     EmpathyContact *b)
1834 {
1835   FolksPresenceDetails *presence_a, *presence_b;
1836
1837   presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1838   presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1839
1840   /* We negate the result because we're sorting in reverse order (i.e. such that
1841    * the Personas with the highest presence are at the beginning of the list. */
1842   return -folks_presence_details_typecmp (
1843       folks_presence_details_get_presence_type (presence_a),
1844       folks_presence_details_get_presence_type (presence_b));
1845 }
1846
1847 static gint
1848 voip_cmp_func (EmpathyContact *a,
1849     EmpathyContact *b)
1850 {
1851   gboolean has_audio_a, has_audio_b;
1852   gboolean has_video_a, has_video_b;
1853
1854   has_audio_a = empathy_contact_can_voip_audio (a);
1855   has_audio_b = empathy_contact_can_voip_audio (b);
1856   has_video_a = empathy_contact_can_voip_video (a);
1857   has_video_b = empathy_contact_can_voip_video (b);
1858
1859   /* First check video */
1860   if (has_video_a == has_video_b)
1861     {
1862       /* Use audio to to break tie */
1863       if (has_audio_a == has_audio_b)
1864         return 0;
1865       else if (has_audio_a)
1866         return -1;
1867       else
1868         return 1;
1869     }
1870   else if (has_video_a)
1871     return -1;
1872   else
1873     return 1;
1874 }
1875
1876 static gint
1877 ft_cmp_func (EmpathyContact *a,
1878     EmpathyContact *b)
1879 {
1880   gboolean can_send_files_a, can_send_files_b;
1881
1882   can_send_files_a = empathy_contact_can_send_files (a);
1883   can_send_files_b = empathy_contact_can_send_files (b);
1884
1885   if (can_send_files_a == can_send_files_b)
1886     return 0;
1887   else if (can_send_files_a)
1888     return -1;
1889   else
1890     return 1;
1891 }
1892
1893 static gint
1894 rfb_stream_tube_cmp_func (EmpathyContact *a,
1895     EmpathyContact *b)
1896 {
1897   gboolean rfb_a, rfb_b;
1898
1899   rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1900   rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1901
1902   if (rfb_a == rfb_b)
1903     return 0;
1904   else if (rfb_a)
1905     return -1;
1906   else
1907     return 1;
1908 }
1909
1910 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1911  * the same presence, prefer the one which can do both audio *and* video calls,
1912  * over the one which can only do one of the two. */
1913 static int
1914 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1915 {
1916   gint presence_sort = presence_cmp_func (a, b);
1917
1918   if (presence_sort != 0)
1919     return presence_sort;
1920
1921   return voip_cmp_func (a, b);
1922 }
1923
1924 /* Sort by presence as with presence_cmp_func() and then break ties using the
1925  * most "capable" individual. So users will be able to pick more actions on
1926  * the contact in the "Contact" menu of the chat window. */
1927 static gint
1928 chat_sort_func (EmpathyContact *a,
1929     EmpathyContact *b)
1930 {
1931   gint result;
1932
1933   result = presence_cmp_func (a, b);
1934   if (result != 0)
1935     return result;
1936
1937   /* Prefer individual supporting file transfer */
1938   result = ft_cmp_func (a, b);
1939   if (result != 0)
1940     return result;
1941
1942   /* Check audio/video capabilities */
1943   result = voip_cmp_func (a, b);
1944   if (result != 0)
1945     return result;
1946
1947   /* Check 'Share my destop' feature */
1948   return rfb_stream_tube_cmp_func (a, b);
1949 }
1950
1951 static GCompareFunc
1952 get_sort_func_for_action (EmpathyActionType action_type)
1953 {
1954   switch (action_type)
1955     {
1956       case EMPATHY_ACTION_AUDIO_CALL:
1957       case EMPATHY_ACTION_VIDEO_CALL:
1958         return (GCompareFunc) voip_sort_func;
1959       case EMPATHY_ACTION_CHAT:
1960         return (GCompareFunc) chat_sort_func;
1961       case EMPATHY_ACTION_VIEW_LOGS:
1962       case EMPATHY_ACTION_SEND_FILE:
1963       case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1964       default:
1965         return (GCompareFunc) presence_cmp_func;
1966     }
1967 }
1968
1969 /**
1970  * empathy_contact_dup_best_for_action:
1971  * @individual: a #FolksIndividual
1972  * @action_type: the type of action to be performed on the contact
1973  *
1974  * Chooses a #FolksPersona from the given @individual which is best-suited for
1975  * the given @action_type. "Best-suited" is determined by choosing the persona
1976  * with the highest presence out of all the personas which can perform the given
1977  * @action_type (e.g. are capable of video calling).
1978  *
1979  * Return value: an #EmpathyContact for the best persona, or %NULL;
1980  * unref with g_object_unref()
1981  */
1982 EmpathyContact *
1983 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1984     EmpathyActionType action_type)
1985 {
1986   GeeSet *personas;
1987   GeeIterator *iter;
1988   GList *contacts;
1989   EmpathyContact *best_contact = NULL;
1990
1991   /* Build a list of EmpathyContacts that we can sort */
1992   personas = folks_individual_get_personas (individual);
1993   contacts = NULL;
1994
1995   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1996   while (gee_iterator_next (iter))
1997     {
1998       FolksPersona *persona = gee_iterator_get (iter);
1999       TpContact *tp_contact;
2000       EmpathyContact *contact = NULL;
2001
2002       if (!empathy_folks_persona_is_interesting (persona))
2003         goto while_finish;
2004
2005       tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
2006       contact = empathy_contact_dup_from_tp_contact (tp_contact);
2007       empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
2008
2009       /* Only choose the contact if they're actually capable of the specified
2010        * action. */
2011       if (empathy_contact_can_do_action (contact, action_type))
2012         contacts = g_list_prepend (contacts, g_object_ref (contact));
2013
2014 while_finish:
2015       g_clear_object (&contact);
2016       g_clear_object (&persona);
2017     }
2018   g_clear_object (&iter);
2019
2020   /* Sort the contacts by some heuristic based on the action type, then take
2021    * the top contact. */
2022   if (contacts != NULL)
2023     {
2024       contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2025       best_contact = g_object_ref (contacts->data);
2026     }
2027
2028   g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2029   g_list_free (contacts);
2030
2031   return best_contact;
2032 }