]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact.c
Merge EmpathyContact:name and *_set_alias() to EmpathyContact:alias
[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
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
34
35 #if HAVE_GEOCLUE
36 #include <geoclue/geoclue-geocode.h>
37 #endif
38
39 #include "empathy-contact.h"
40 #include "empathy-utils.h"
41 #include "empathy-enum-types.h"
42 #include "empathy-marshal.h"
43 #include "empathy-location.h"
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
46 #include "empathy-debug.h"
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
49 typedef struct {
50   TpContact *tp_contact;
51   TpAccount *account;
52   FolksPersona *persona;
53   gchar *id;
54   gchar *alias;
55   EmpathyAvatar *avatar;
56   TpConnectionPresenceType presence;
57   gchar *presence_message;
58   guint handle;
59   EmpathyCapabilities capabilities;
60   gboolean is_user;
61   guint hash;
62   /* Location is composed of string keys and GValues.
63    * Example: a "city" key would have "Helsinki" as string GValue,
64    *          a "latitude" would have 65.0 as double GValue.
65    *
66    * This is a super set of the location stored in TpContact as we can try add
67    * more fields by searching the address using geoclue.
68    */
69   GHashTable *location;
70 } EmpathyContactPriv;
71
72 static void contact_finalize (GObject *object);
73 static void contact_get_property (GObject *object, guint param_id,
74     GValue *value, GParamSpec *pspec);
75 static void contact_set_property (GObject *object, guint param_id,
76     const GValue *value, GParamSpec *pspec);
77
78 #if HAVE_GEOCLUE
79 static void update_geocode (EmpathyContact *contact);
80 #endif
81
82 static void empathy_contact_set_location (EmpathyContact *contact,
83     GHashTable *location);
84
85 static void set_capabilities_from_tp_caps (EmpathyContact *self,
86     TpCapabilities *caps);
87
88 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
89
90 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
91
92 enum
93 {
94   PROP_0,
95   PROP_TP_CONTACT,
96   PROP_ACCOUNT,
97   PROP_PERSONA,
98   PROP_ID,
99   PROP_ALIAS,
100   PROP_AVATAR,
101   PROP_PRESENCE,
102   PROP_PRESENCE_MESSAGE,
103   PROP_HANDLE,
104   PROP_CAPABILITIES,
105   PROP_IS_USER,
106   PROP_LOCATION
107 };
108
109 enum {
110   PRESENCE_CHANGED,
111   LAST_SIGNAL
112 };
113
114 static guint signals[LAST_SIGNAL];
115
116 /* TpContact* -> EmpathyContact*, both borrowed ref */
117 static GHashTable *contacts_table = NULL;
118
119 static void
120 tp_contact_notify_cb (TpContact *tp_contact,
121                       GParamSpec *param,
122                       GObject *contact)
123 {
124   EmpathyContactPriv *priv = GET_PRIV (contact);
125
126   /* Forward property notifications */
127   if (!tp_strdiff (param->name, "alias"))
128     g_object_notify (contact, "alias");
129   else if (!tp_strdiff (param->name, "presence-type")) {
130     TpConnectionPresenceType presence;
131
132     presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
133     g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
134       priv->presence);
135     priv->presence = presence;
136     g_object_notify (contact, "presence");
137   }
138   else if (!tp_strdiff (param->name, "presence-message"))
139     g_object_notify (contact, "presence-message");
140   else if (!tp_strdiff (param->name, "identifier"))
141     g_object_notify (contact, "id");
142   else if (!tp_strdiff (param->name, "handle"))
143     g_object_notify (contact, "handle");
144   else if (!tp_strdiff (param->name, "location"))
145     {
146       GHashTable *location;
147
148       location = tp_contact_get_location (tp_contact);
149       /* This will start a geoclue search to find the address if needed */
150       empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
151     }
152   else if (!tp_strdiff (param->name, "capabilities"))
153     {
154       set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
155           tp_contact_get_capabilities (tp_contact));
156     }
157   else if (!tp_strdiff (param->name, "avatar-file"))
158     {
159       contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
160     }
161 }
162
163 static void
164 contact_dispose (GObject *object)
165 {
166   EmpathyContactPriv *priv = GET_PRIV (object);
167
168   if (priv->tp_contact)
169     {
170       g_hash_table_remove (contacts_table, priv->tp_contact);
171       g_signal_handlers_disconnect_by_func (priv->tp_contact,
172           tp_contact_notify_cb, object);
173       g_object_unref (priv->tp_contact);
174     }
175   priv->tp_contact = NULL;
176
177   if (priv->account)
178     g_object_unref (priv->account);
179   priv->account = NULL;
180
181   if (priv->persona)
182     g_object_unref (priv->persona);
183   priv->persona = NULL;
184
185   if (priv->avatar != NULL)
186     {
187       empathy_avatar_unref (priv->avatar);
188       priv->avatar = NULL;
189     }
190
191   if (priv->location != NULL)
192     {
193       g_hash_table_unref (priv->location);
194       priv->location = NULL;
195     }
196
197   G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
198 }
199
200 static void
201 empathy_contact_class_init (EmpathyContactClass *class)
202 {
203   GObjectClass *object_class;
204
205   object_class = G_OBJECT_CLASS (class);
206
207   object_class->finalize = contact_finalize;
208   object_class->dispose = contact_dispose;
209   object_class->get_property = contact_get_property;
210   object_class->set_property = contact_set_property;
211
212   g_object_class_install_property (object_class,
213       PROP_TP_CONTACT,
214       g_param_spec_object ("tp-contact",
215         "TpContact",
216         "The TpContact associated with the contact",
217         TP_TYPE_CONTACT,
218         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219
220   g_object_class_install_property (object_class,
221       PROP_ACCOUNT,
222       g_param_spec_object ("account",
223         "The account",
224         "The account associated with the contact",
225         TP_TYPE_ACCOUNT,
226         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227
228   g_object_class_install_property (object_class,
229       PROP_PERSONA,
230       g_param_spec_object ("persona",
231         "Persona",
232         "The FolksPersona associated with the contact",
233         FOLKS_TYPE_PERSONA,
234         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235
236   g_object_class_install_property (object_class,
237       PROP_ID,
238       g_param_spec_string ("id",
239         "Contact id",
240         "String identifying contact",
241         NULL,
242         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243
244   g_object_class_install_property (object_class,
245       PROP_ALIAS,
246       g_param_spec_string ("alias",
247         "Contact alias",
248         "An alias for the contact",
249         NULL,
250         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251
252   g_object_class_install_property (object_class,
253       PROP_AVATAR,
254       g_param_spec_boxed ("avatar",
255         "Avatar image",
256         "The avatar image",
257         EMPATHY_TYPE_AVATAR,
258         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259
260   g_object_class_install_property (object_class,
261       PROP_PRESENCE,
262       g_param_spec_uint ("presence",
263         "Contact presence",
264         "Presence of contact",
265         TP_CONNECTION_PRESENCE_TYPE_UNSET,
266         NUM_TP_CONNECTION_PRESENCE_TYPES,
267         TP_CONNECTION_PRESENCE_TYPE_UNSET,
268         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269
270   g_object_class_install_property (object_class,
271       PROP_PRESENCE_MESSAGE,
272       g_param_spec_string ("presence-message",
273         "Contact presence message",
274         "Presence message of contact",
275         NULL,
276         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278   g_object_class_install_property (object_class,
279       PROP_HANDLE,
280       g_param_spec_uint ("handle",
281         "Contact Handle",
282         "The handle of the contact",
283         0,
284         G_MAXUINT,
285         0,
286         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287
288   g_object_class_install_property (object_class,
289       PROP_CAPABILITIES,
290       g_param_spec_flags ("capabilities",
291         "Contact Capabilities",
292         "Capabilities of the contact",
293         EMPATHY_TYPE_CAPABILITIES,
294         EMPATHY_CAPABILITIES_UNKNOWN,
295         G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296
297   g_object_class_install_property (object_class,
298       PROP_IS_USER,
299       g_param_spec_boolean ("is-user",
300         "Contact is-user",
301         "Is contact the user",
302         FALSE,
303         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304
305
306   g_object_class_install_property (object_class,
307       PROP_LOCATION,
308       g_param_spec_boxed ("location",
309         "Contact location",
310         "Physical location of the contact",
311         G_TYPE_HASH_TABLE,
312         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
313
314   signals[PRESENCE_CHANGED] =
315     g_signal_new ("presence-changed",
316                   G_TYPE_FROM_CLASS (class),
317                   G_SIGNAL_RUN_LAST,
318                   0,
319                   NULL, NULL,
320                   _empathy_marshal_VOID__UINT_UINT,
321                   G_TYPE_NONE,
322                   2, G_TYPE_UINT,
323                   G_TYPE_UINT);
324
325   g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
326 }
327
328 static void
329 empathy_contact_init (EmpathyContact *contact)
330 {
331   EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
332     EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
333
334   contact->priv = priv;
335
336   priv->location = NULL;
337 }
338
339 static void
340 contact_finalize (GObject *object)
341 {
342   EmpathyContactPriv *priv;
343
344   priv = GET_PRIV (object);
345
346   DEBUG ("finalize: %p", object);
347
348   g_free (priv->alias);
349   g_free (priv->id);
350   g_free (priv->presence_message);
351
352   G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
353 }
354
355 static void
356 set_tp_contact (EmpathyContact *contact,
357                 TpContact *tp_contact)
358 {
359   EmpathyContactPriv *priv = GET_PRIV (contact);
360   GHashTable *location;
361   TpHandle self_handle;
362   TpHandle handle;
363
364   if (tp_contact == NULL)
365     return;
366
367   g_assert (priv->tp_contact == NULL);
368   priv->tp_contact = g_object_ref (tp_contact);
369   priv->presence = empathy_contact_get_presence (contact);
370
371   location = tp_contact_get_location (tp_contact);
372   if (location != NULL)
373     empathy_contact_set_location (contact, location);
374
375   set_capabilities_from_tp_caps (contact,
376       tp_contact_get_capabilities (tp_contact));
377
378   contact_set_avatar_from_tp_contact (contact);
379
380   /* Set is-user property. Note that it could still be the handle is
381    * different from the connection's self handle, in the case the handle
382    * comes from a group interface. */
383   self_handle = tp_connection_get_self_handle (
384       tp_contact_get_connection (tp_contact));
385   handle = tp_contact_get_handle (tp_contact);
386   empathy_contact_set_is_user (contact, self_handle == handle);
387
388   g_signal_connect (priv->tp_contact, "notify",
389     G_CALLBACK (tp_contact_notify_cb), contact);
390 }
391
392 static void
393 contact_get_property (GObject *object,
394                       guint param_id,
395                       GValue *value,
396                       GParamSpec *pspec)
397 {
398   EmpathyContact *contact = EMPATHY_CONTACT (object);
399
400   switch (param_id)
401     {
402       case PROP_TP_CONTACT:
403         g_value_set_object (value, empathy_contact_get_tp_contact (contact));
404         break;
405       case PROP_ACCOUNT:
406         g_value_set_object (value, empathy_contact_get_account (contact));
407         break;
408       case PROP_PERSONA:
409         g_value_set_object (value, empathy_contact_get_persona (contact));
410         break;
411       case PROP_ID:
412         g_value_set_string (value, empathy_contact_get_id (contact));
413         break;
414       case PROP_ALIAS:
415         g_value_set_string (value, empathy_contact_get_alias (contact));
416         break;
417       case PROP_AVATAR:
418         g_value_set_boxed (value, empathy_contact_get_avatar (contact));
419         break;
420       case PROP_PRESENCE:
421         g_value_set_uint (value, empathy_contact_get_presence (contact));
422         break;
423       case PROP_PRESENCE_MESSAGE:
424         g_value_set_string (value, empathy_contact_get_presence_message (contact));
425         break;
426       case PROP_HANDLE:
427         g_value_set_uint (value, empathy_contact_get_handle (contact));
428         break;
429       case PROP_CAPABILITIES:
430         g_value_set_flags (value, empathy_contact_get_capabilities (contact));
431         break;
432       case PROP_IS_USER:
433         g_value_set_boolean (value, empathy_contact_is_user (contact));
434         break;
435       default:
436         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
437         break;
438     };
439 }
440
441 static void
442 contact_set_property (GObject *object,
443                       guint param_id,
444                       const GValue *value,
445                       GParamSpec *pspec)
446 {
447   EmpathyContact *contact = EMPATHY_CONTACT (object);
448   EmpathyContactPriv *priv = GET_PRIV (object);
449
450   switch (param_id)
451     {
452       case PROP_TP_CONTACT:
453         set_tp_contact (contact, g_value_get_object (value));
454         break;
455       case PROP_ACCOUNT:
456         g_assert (priv->account == NULL);
457         priv->account = g_value_dup_object (value);
458         break;
459       case PROP_PERSONA:
460         empathy_contact_set_persona (contact, g_value_get_object (value));
461         break;
462       case PROP_ID:
463         empathy_contact_set_id (contact, g_value_get_string (value));
464         break;
465       case PROP_ALIAS:
466         empathy_contact_set_alias (contact, g_value_get_string (value));
467         break;
468       case PROP_AVATAR:
469         empathy_contact_set_avatar (contact, g_value_get_boxed (value));
470         break;
471       case PROP_PRESENCE:
472         empathy_contact_set_presence (contact, g_value_get_uint (value));
473         break;
474       case PROP_PRESENCE_MESSAGE:
475         empathy_contact_set_presence_message (contact, g_value_get_string (value));
476         break;
477       case PROP_HANDLE:
478         empathy_contact_set_handle (contact, g_value_get_uint (value));
479         break;
480       case PROP_CAPABILITIES:
481         empathy_contact_set_capabilities (contact, g_value_get_flags (value));
482         break;
483       case PROP_IS_USER:
484         empathy_contact_set_is_user (contact, g_value_get_boolean (value));
485         break;
486       default:
487         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
488         break;
489     };
490 }
491
492 EmpathyContact *
493 empathy_contact_new (TpContact *tp_contact)
494 {
495   g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
496
497   return g_object_new (EMPATHY_TYPE_CONTACT,
498       "tp-contact", tp_contact,
499       NULL);
500 }
501
502 EmpathyContact *
503 empathy_contact_from_tpl_contact (TpAccount *account,
504     TplEntity *tpl_entity)
505 {
506   EmpathyContact *retval;
507   gboolean is_user;
508
509   g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
510
511   is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
512
513   retval = g_object_new (EMPATHY_TYPE_CONTACT,
514       "id", tpl_entity_get_alias (tpl_entity),
515       "alias", tpl_entity_get_identifier (tpl_entity),
516       "account", account,
517       "is-user", is_user,
518       NULL);
519
520   if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
521     empathy_contact_load_avatar_cache (retval,
522         tpl_entity_get_avatar_token (tpl_entity));
523
524   return retval;
525 }
526
527 EmpathyContact *
528 empathy_contact_new_for_log (TpAccount *account,
529                              const gchar *id,
530                              const gchar *alias,
531                              gboolean is_user)
532 {
533   g_return_val_if_fail (id != NULL, NULL);
534   g_assert (account != NULL);
535
536   return g_object_new (EMPATHY_TYPE_CONTACT,
537       "account", account,
538       "id", id,
539       "alias", alias,
540       "is-user", is_user,
541       NULL);
542 }
543
544 TpContact *
545 empathy_contact_get_tp_contact (EmpathyContact *contact)
546 {
547   EmpathyContactPriv *priv;
548
549   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
550
551   priv = GET_PRIV (contact);
552
553   return priv->tp_contact;
554 }
555
556 const gchar *
557 empathy_contact_get_id (EmpathyContact *contact)
558 {
559   EmpathyContactPriv *priv;
560
561   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
562
563   priv = GET_PRIV (contact);
564
565   if (priv->tp_contact != NULL)
566     return tp_contact_get_identifier (priv->tp_contact);
567
568   return priv->id;
569 }
570
571 void
572 empathy_contact_set_id (EmpathyContact *contact,
573                         const gchar *id)
574 {
575   EmpathyContactPriv *priv;
576
577   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
578   g_return_if_fail (id != NULL);
579
580   priv = GET_PRIV (contact);
581
582   /* We temporally ref the contact because it could be destroyed
583    * during the signal emition */
584   g_object_ref (contact);
585   if (tp_strdiff (id, priv->id))
586     {
587       g_free (priv->id);
588       priv->id = g_strdup (id);
589
590       g_object_notify (G_OBJECT (contact), "id");
591       if (EMP_STR_EMPTY (priv->alias))
592           g_object_notify (G_OBJECT (contact), "alias");
593     }
594
595   g_object_unref (contact);
596 }
597
598 const gchar *
599 empathy_contact_get_alias (EmpathyContact *contact)
600 {
601   EmpathyContactPriv *priv;
602   const gchar        *alias;
603
604   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
605
606   priv = GET_PRIV (contact);
607
608   if (priv->tp_contact != NULL)
609     alias = tp_contact_get_alias (priv->tp_contact);
610   else
611     alias = priv->alias;
612
613   if (!EMP_STR_EMPTY (alias))
614     return alias;
615   else
616     return empathy_contact_get_id (contact);
617 }
618
619 void
620 empathy_contact_set_alias (EmpathyContact *contact,
621                           const gchar *alias)
622 {
623   EmpathyContactPriv *priv;
624   FolksPersona *persona;
625
626   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
627
628   priv = GET_PRIV (contact);
629
630   g_object_ref (contact);
631
632   if (tp_strdiff (alias, priv->alias))
633     {
634       g_free (priv->alias);
635       priv->alias = g_strdup (alias);
636       g_object_notify (G_OBJECT (contact), "alias");
637     }
638
639   g_object_unref (contact);
640 }
641
642 EmpathyAvatar *
643 empathy_contact_get_avatar (EmpathyContact *contact)
644 {
645   EmpathyContactPriv *priv;
646
647   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
648
649   priv = GET_PRIV (contact);
650
651   return priv->avatar;
652 }
653
654 void
655 empathy_contact_set_avatar (EmpathyContact *contact,
656                             EmpathyAvatar *avatar)
657 {
658   EmpathyContactPriv *priv;
659
660   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
661
662   priv = GET_PRIV (contact);
663
664   if (priv->avatar == avatar)
665     return;
666
667   if (priv->avatar)
668     {
669       empathy_avatar_unref (priv->avatar);
670       priv->avatar = NULL;
671     }
672
673   if (avatar)
674       priv->avatar = empathy_avatar_ref (avatar);
675
676   g_object_notify (G_OBJECT (contact), "avatar");
677 }
678
679 TpAccount *
680 empathy_contact_get_account (EmpathyContact *contact)
681 {
682   EmpathyContactPriv *priv;
683
684   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
685
686   priv = GET_PRIV (contact);
687
688   if (priv->account == NULL && priv->tp_contact != NULL)
689     {
690       TpConnection *connection;
691
692       /* FIXME: This assume the account manager already exists */
693       connection = tp_contact_get_connection (priv->tp_contact);
694       priv->account =
695         g_object_ref (empathy_get_account_for_connection (connection));
696     }
697
698   return priv->account;
699 }
700
701 FolksPersona *
702 empathy_contact_get_persona (EmpathyContact *contact)
703 {
704   EmpathyContactPriv *priv;
705
706   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
707
708   priv = GET_PRIV (contact);
709
710   if (priv->persona == NULL && priv->tp_contact != NULL)
711     {
712       /* FIXME: This is disgustingly slow */
713       /* Query for the persona */
714       EmpathyIndividualManager *manager;
715       GList *individuals, *l;
716
717       manager = empathy_individual_manager_dup_singleton ();
718       individuals = empathy_individual_manager_get_members (manager);
719
720       for (l = individuals; l != NULL; l = l->next)
721         {
722           GList *personas, *j;
723           FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
724
725           personas = folks_individual_get_personas (individual);
726           for (j = personas; j != NULL; j = j->next)
727             {
728               TpfPersona *persona = j->data;
729
730               if (TPF_IS_PERSONA (persona))
731                 {
732                   TpContact *tp_contact = tpf_persona_get_contact (persona);
733
734                   if (tp_contact == priv->tp_contact)
735                     {
736                       /* Found the right persona */
737                       priv->persona = g_object_ref (persona);
738                       goto finished;
739                     }
740                 }
741             }
742         }
743
744 finished:
745       g_list_free (individuals);
746       g_object_unref (manager);
747     }
748
749   return priv->persona;
750 }
751
752 void
753 empathy_contact_set_persona (EmpathyContact *contact,
754     FolksPersona *persona)
755 {
756   EmpathyContactPriv *priv;
757
758   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
759   g_return_if_fail (FOLKS_IS_PERSONA (persona));
760
761   priv = GET_PRIV (contact);
762
763   if (persona == priv->persona)
764     return;
765
766   if (priv->persona != NULL)
767     g_object_unref (priv->persona);
768   priv->persona = g_object_ref (persona);
769
770   g_object_notify (G_OBJECT (contact), "persona");
771 }
772
773 TpConnection *
774 empathy_contact_get_connection (EmpathyContact *contact)
775 {
776   EmpathyContactPriv *priv;
777
778   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
779
780   priv = GET_PRIV (contact);
781
782   if (priv->tp_contact != NULL)
783     return tp_contact_get_connection (priv->tp_contact);
784
785   return NULL;
786 }
787
788 TpConnectionPresenceType
789 empathy_contact_get_presence (EmpathyContact *contact)
790 {
791   EmpathyContactPriv *priv;
792
793   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
794     TP_CONNECTION_PRESENCE_TYPE_UNSET);
795
796   priv = GET_PRIV (contact);
797
798   if (priv->tp_contact != NULL)
799     return tp_contact_get_presence_type (priv->tp_contact);
800
801   return priv->presence;
802 }
803
804 void
805 empathy_contact_set_presence (EmpathyContact *contact,
806                               TpConnectionPresenceType presence)
807 {
808   EmpathyContactPriv *priv;
809   TpConnectionPresenceType old_presence;
810
811   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
812
813   priv = GET_PRIV (contact);
814
815   if (presence == priv->presence)
816     return;
817
818   old_presence = priv->presence;
819   priv->presence = presence;
820
821   g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
822
823   g_object_notify (G_OBJECT (contact), "presence");
824 }
825
826 const gchar *
827 empathy_contact_get_presence_message (EmpathyContact *contact)
828 {
829   EmpathyContactPriv *priv;
830
831   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
832
833   priv = GET_PRIV (contact);
834
835   if (priv->tp_contact != NULL)
836     return tp_contact_get_presence_message (priv->tp_contact);
837
838   return priv->presence_message;
839 }
840
841 void
842 empathy_contact_set_presence_message (EmpathyContact *contact,
843                                       const gchar *message)
844 {
845   EmpathyContactPriv *priv = GET_PRIV (contact);
846
847   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
848
849   if (!tp_strdiff (message, priv->presence_message))
850     return;
851
852   g_free (priv->presence_message);
853   priv->presence_message = g_strdup (message);
854
855   g_object_notify (G_OBJECT (contact), "presence-message");
856 }
857
858 guint
859 empathy_contact_get_handle (EmpathyContact *contact)
860 {
861   EmpathyContactPriv *priv;
862
863   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
864
865   priv = GET_PRIV (contact);
866
867   if (priv->tp_contact != NULL)
868     return tp_contact_get_handle (priv->tp_contact);
869
870   return priv->handle;
871 }
872
873 void
874 empathy_contact_set_handle (EmpathyContact *contact,
875                             guint handle)
876 {
877   EmpathyContactPriv *priv;
878
879   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
880
881   priv = GET_PRIV (contact);
882
883   g_object_ref (contact);
884   if (handle != priv->handle)
885     {
886       priv->handle = handle;
887       g_object_notify (G_OBJECT (contact), "handle");
888     }
889   g_object_unref (contact);
890 }
891
892 EmpathyCapabilities
893 empathy_contact_get_capabilities (EmpathyContact *contact)
894 {
895   EmpathyContactPriv *priv;
896
897   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
898
899   priv = GET_PRIV (contact);
900
901   return priv->capabilities;
902 }
903
904 void
905 empathy_contact_set_capabilities (EmpathyContact *contact,
906                                   EmpathyCapabilities capabilities)
907 {
908   EmpathyContactPriv *priv;
909
910   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
911
912   priv = GET_PRIV (contact);
913
914   if (priv->capabilities == capabilities)
915     return;
916
917   priv->capabilities = capabilities;
918
919   g_object_notify (G_OBJECT (contact), "capabilities");
920 }
921
922 gboolean
923 empathy_contact_is_user (EmpathyContact *contact)
924 {
925   EmpathyContactPriv *priv;
926
927   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
928
929   priv = GET_PRIV (contact);
930
931   return priv->is_user;
932 }
933
934 void
935 empathy_contact_set_is_user (EmpathyContact *contact,
936                              gboolean is_user)
937 {
938   EmpathyContactPriv *priv;
939
940   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
941
942   priv = GET_PRIV (contact);
943
944   if (priv->is_user == is_user)
945     return;
946
947   priv->is_user = is_user;
948
949   g_object_notify (G_OBJECT (contact), "is-user");
950 }
951
952 gboolean
953 empathy_contact_is_online (EmpathyContact *contact)
954 {
955   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
956
957   switch (empathy_contact_get_presence (contact))
958     {
959       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
960       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
961       case TP_CONNECTION_PRESENCE_TYPE_ERROR:
962         return FALSE;
963       default:
964         return TRUE;
965     }
966 }
967
968 const gchar *
969 empathy_contact_get_status (EmpathyContact *contact)
970 {
971   const gchar *message;
972
973   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
974
975   message = empathy_contact_get_presence_message (contact);
976   if (!EMP_STR_EMPTY (message))
977     return message;
978
979   return empathy_presence_get_default_message (
980       empathy_contact_get_presence (contact));
981 }
982
983 gboolean
984 empathy_contact_can_voip (EmpathyContact *contact)
985 {
986   EmpathyContactPriv *priv;
987
988   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
989
990   priv = GET_PRIV (contact);
991
992   return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
993       EMPATHY_CAPABILITIES_VIDEO);
994 }
995
996 gboolean
997 empathy_contact_can_voip_audio (EmpathyContact *contact)
998 {
999   EmpathyContactPriv *priv;
1000
1001   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1002
1003   priv = GET_PRIV (contact);
1004
1005   return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1006 }
1007
1008 gboolean
1009 empathy_contact_can_voip_video (EmpathyContact *contact)
1010 {
1011   EmpathyContactPriv *priv;
1012
1013   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1014
1015   priv = GET_PRIV (contact);
1016
1017   return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1018 }
1019
1020 gboolean
1021 empathy_contact_can_send_files (EmpathyContact *contact)
1022 {
1023   EmpathyContactPriv *priv;
1024
1025   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1026
1027   priv = GET_PRIV (contact);
1028
1029   return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1030 }
1031
1032 gboolean
1033 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1034 {
1035   EmpathyContactPriv *priv;
1036
1037   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1038
1039   priv = GET_PRIV (contact);
1040
1041   return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1042 }
1043
1044 static gchar *
1045 contact_get_avatar_filename (EmpathyContact *contact,
1046                              const gchar *token)
1047 {
1048   TpAccount *account;
1049   gchar *avatar_path;
1050   gchar *avatar_file;
1051   gchar *token_escaped;
1052
1053   if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1054     return NULL;
1055
1056   token_escaped = tp_escape_as_identifier (token);
1057   account = empathy_contact_get_account (contact);
1058
1059   avatar_path = g_build_filename (g_get_user_cache_dir (),
1060       "telepathy",
1061       "avatars",
1062       tp_account_get_connection_manager (account),
1063       tp_account_get_protocol (account),
1064       NULL);
1065   g_mkdir_with_parents (avatar_path, 0700);
1066
1067   avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1068
1069   g_free (token_escaped);
1070   g_free (avatar_path);
1071
1072   return avatar_file;
1073 }
1074
1075 gboolean
1076 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1077                                    const gchar *token)
1078 {
1079   EmpathyAvatar *avatar = NULL;
1080   gchar *filename;
1081   gchar *data = NULL;
1082   gsize len;
1083   GError *error = NULL;
1084
1085   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1086   g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1087
1088   /* Load the avatar from file if it exists */
1089   filename = contact_get_avatar_filename (contact, token);
1090   if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1091     {
1092       if (!g_file_get_contents (filename, &data, &len, &error))
1093         {
1094           DEBUG ("Failed to load avatar from cache: %s",
1095             error ? error->message : "No error given");
1096           g_clear_error (&error);
1097         }
1098     }
1099
1100   if (data)
1101     {
1102       DEBUG ("Avatar loaded from %s", filename);
1103       avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1104           filename);
1105       empathy_contact_set_avatar (contact, avatar);
1106       empathy_avatar_unref (avatar);
1107     }
1108   else
1109     {
1110       g_free (filename);
1111     }
1112
1113   return data != NULL;
1114 }
1115
1116 GType
1117 empathy_avatar_get_type (void)
1118 {
1119   static GType type_id = 0;
1120
1121   if (!type_id)
1122     {
1123       type_id = g_boxed_type_register_static ("EmpathyAvatar",
1124           (GBoxedCopyFunc) empathy_avatar_ref,
1125           (GBoxedFreeFunc) empathy_avatar_unref);
1126     }
1127
1128   return type_id;
1129 }
1130
1131 /**
1132  * empathy_avatar_new:
1133  * @data: the avatar data
1134  * @len: the size of avatar data
1135  * @format: the mime type of the avatar image
1136  * @token: the token of the avatar
1137  * @filename: the filename where the avatar is stored in cache
1138  *
1139  * Create a #EmpathyAvatar from the provided data. This function takes the
1140  * ownership of @data, @format, @token and @filename.
1141  *
1142  * Returns: a new #EmpathyAvatar
1143  */
1144 EmpathyAvatar *
1145 empathy_avatar_new (guchar *data,
1146                     gsize len,
1147                     gchar *format,
1148                     gchar *token,
1149                     gchar *filename)
1150 {
1151   EmpathyAvatar *avatar;
1152
1153   avatar = g_slice_new0 (EmpathyAvatar);
1154   avatar->data = data;
1155   avatar->len = len;
1156   avatar->format = format;
1157   avatar->token = token;
1158   avatar->filename = filename;
1159   avatar->refcount = 1;
1160
1161   return avatar;
1162 }
1163
1164 void
1165 empathy_avatar_unref (EmpathyAvatar *avatar)
1166 {
1167   g_return_if_fail (avatar != NULL);
1168
1169   avatar->refcount--;
1170   if (avatar->refcount == 0)
1171     {
1172       g_free (avatar->data);
1173       g_free (avatar->format);
1174       g_free (avatar->token);
1175       g_free (avatar->filename);
1176       g_slice_free (EmpathyAvatar, avatar);
1177     }
1178 }
1179
1180 EmpathyAvatar *
1181 empathy_avatar_ref (EmpathyAvatar *avatar)
1182 {
1183   g_return_val_if_fail (avatar != NULL, NULL);
1184
1185   avatar->refcount++;
1186
1187   return avatar;
1188 }
1189
1190 /**
1191  * empathy_avatar_save_to_file:
1192  * @avatar: the avatar
1193  * @filename: name of a file to write avatar to
1194  * @error: return location for a GError, or NULL
1195  *
1196  * Save the avatar to a file named filename
1197  *
1198  * Returns: %TRUE on success, %FALSE if an error occurred
1199  */
1200 gboolean
1201 empathy_avatar_save_to_file (EmpathyAvatar *self,
1202                              const gchar *filename,
1203                              GError **error)
1204 {
1205   return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1206       error);
1207 }
1208
1209 /**
1210  * empathy_contact_get_location:
1211  * @contact: an #EmpathyContact
1212  *
1213  * Returns the user's location if available.  The keys are defined in
1214  * empathy-location.h. If the contact doesn't have location
1215  * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1216  * you are done with the #GHashTable.
1217  *
1218  * It is composed of string keys and GValues.  Keys are
1219  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1220  * Example: a "city" key would have "Helsinki" as string GValue,
1221  *          a "latitude" would have 65.0 as double GValue.
1222  *
1223  * Returns: a #GHashTable of location values
1224  */
1225 GHashTable *
1226 empathy_contact_get_location (EmpathyContact *contact)
1227 {
1228   EmpathyContactPriv *priv;
1229
1230   g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1231
1232   priv = GET_PRIV (contact);
1233
1234   return priv->location;
1235 }
1236
1237 /**
1238  * empathy_contact_set_location:
1239  * @contact: an #EmpathyContact
1240  * @location: a #GHashTable of the location
1241  *
1242  * Sets the user's location based on the location #GHashTable passed.
1243  * It is composed of string keys and GValues.  Keys are
1244  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1245  * Example: a "city" key would have "Helsinki" as string GValue,
1246  *          a "latitude" would have 65.0 as double GValue.
1247  */
1248 static void
1249 empathy_contact_set_location (EmpathyContact *contact,
1250     GHashTable *location)
1251 {
1252   EmpathyContactPriv *priv;
1253
1254   g_return_if_fail (EMPATHY_CONTACT (contact));
1255   g_return_if_fail (location != NULL);
1256
1257   priv = GET_PRIV (contact);
1258
1259   if (priv->location != NULL)
1260     g_hash_table_unref (priv->location);
1261
1262   priv->location = g_hash_table_ref (location);
1263 #if HAVE_GEOCLUE
1264   update_geocode (contact);
1265 #endif
1266   g_object_notify (G_OBJECT (contact), "location");
1267 }
1268
1269 /**
1270  * empathy_contact_equal:
1271  * @contact1: an #EmpathyContact
1272  * @contact2: an #EmpathyContact
1273  *
1274  * Returns FALSE if one of the contacts is NULL but the other is not.
1275  * Otherwise returns TRUE if both pointer are equal or if they bith
1276  * refer to the same id.
1277  * It's only necessary to call this function if your contact objects
1278  * come from logs where contacts are created dynamically and comparing
1279  * pointers is not enough.
1280  */
1281 gboolean
1282 empathy_contact_equal (gconstpointer contact1,
1283                        gconstpointer contact2)
1284 {
1285   EmpathyContact *c1;
1286   EmpathyContact *c2;
1287   const gchar *id1;
1288   const gchar *id2;
1289
1290   if ((contact1 == NULL) != (contact2 == NULL)) {
1291     return FALSE;
1292   }
1293   if (contact1 == contact2) {
1294     return TRUE;
1295   }
1296   c1 = EMPATHY_CONTACT (contact1);
1297   c2 = EMPATHY_CONTACT (contact2);
1298   id1 = empathy_contact_get_id (c1);
1299   id2 = empathy_contact_get_id (c2);
1300   if (!tp_strdiff (id1, id2)) {
1301     return TRUE;
1302   }
1303   return FALSE;
1304 }
1305
1306 #if HAVE_GEOCLUE
1307 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1308 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1309
1310 /* This callback is called by geoclue when it found a position
1311  * for the given address.  A position is necessary for a contact
1312  * to show up on the map
1313  */
1314 static void
1315 geocode_cb (GeoclueGeocode *geocode,
1316     GeocluePositionFields fields,
1317     double latitude,
1318     double longitude,
1319     double altitude,
1320     GeoclueAccuracy *accuracy,
1321     GError *error,
1322     gpointer contact)
1323 {
1324   EmpathyContactPriv *priv = GET_PRIV (contact);
1325   GHashTable *new_location;
1326
1327   if (priv->location == NULL)
1328     goto out;
1329
1330   if (error != NULL)
1331     {
1332       DEBUG ("Error geocoding location : %s", error->message);
1333       goto out;
1334     }
1335
1336   /* No need to change location if we didn't find the position */
1337   if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1338     goto out;
1339
1340   if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1341     goto out;
1342
1343   new_location = tp_asv_new (
1344       EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1345       EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1346       NULL);
1347
1348   DEBUG ("\t - Latitude: %f", latitude);
1349   DEBUG ("\t - Longitude: %f", longitude);
1350
1351   /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1352    * the values we just set. */
1353   tp_g_hash_table_update (new_location, priv->location,
1354       (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1355
1356   /* Set the altitude only if it wasn't defined before */
1357   if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1358       g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1359     {
1360       tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1361           altitude);
1362       DEBUG ("\t - Altitude: %f", altitude);
1363     }
1364
1365   /* Don't change the accuracy as we used an address to get this position */
1366   g_hash_table_unref (priv->location);
1367   priv->location = new_location;
1368   g_object_notify (contact, "location");
1369 out:
1370   g_object_unref (geocode);
1371   g_object_unref (contact);
1372 }
1373
1374 static gchar *
1375 get_dup_string (GHashTable *location,
1376     gchar *key)
1377 {
1378   GValue *value;
1379
1380   value = g_hash_table_lookup (location, key);
1381   if (value != NULL)
1382     return g_value_dup_string (value);
1383
1384   return NULL;
1385 }
1386
1387 static void
1388 update_geocode (EmpathyContact *contact)
1389 {
1390   static GeoclueGeocode *geocode;
1391   gchar *str;
1392   GHashTable *address;
1393   GHashTable *location;
1394
1395   location = empathy_contact_get_location (contact);
1396   if (location == NULL)
1397     return;
1398
1399   /* No need to search for position if contact published it */
1400   if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1401       g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1402     return;
1403
1404   if (geocode == NULL)
1405     {
1406       geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1407       g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1408     }
1409   else
1410     {
1411       g_object_ref (geocode);
1412     }
1413
1414   address = geoclue_address_details_new ();
1415
1416   str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1417   if (str != NULL)
1418     {
1419       g_hash_table_insert (address,
1420         g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1421       DEBUG ("\t - countrycode: %s", str);
1422     }
1423
1424   str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1425   if (str != NULL)
1426     {
1427       g_hash_table_insert (address,
1428         g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1429       DEBUG ("\t - country: %s", str);
1430     }
1431
1432   str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1433   if (str != NULL)
1434     {
1435       g_hash_table_insert (address,
1436         g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1437       DEBUG ("\t - postalcode: %s", str);
1438     }
1439
1440   str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1441   if (str != NULL)
1442     {
1443       g_hash_table_insert (address,
1444         g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1445       DEBUG ("\t - region: %s", str);
1446     }
1447
1448   str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1449   if (str != NULL)
1450     {
1451       g_hash_table_insert (address,
1452         g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1453       DEBUG ("\t - locality: %s", str);
1454     }
1455
1456   str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1457   if (str != NULL)
1458     {
1459       g_hash_table_insert (address,
1460         g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1461       DEBUG ("\t - street: %s", str);
1462     }
1463
1464   if (g_hash_table_size (address) > 0)
1465     {
1466       g_object_ref (contact);
1467
1468       geoclue_geocode_address_to_position_async (geocode, address,
1469           geocode_cb, contact);
1470     }
1471
1472   g_hash_table_unref (address);
1473 }
1474 #endif
1475
1476 static EmpathyCapabilities
1477 tp_caps_to_capabilities (TpCapabilities *caps)
1478 {
1479   EmpathyCapabilities capabilities = 0;
1480   guint i;
1481   GPtrArray *classes;
1482
1483   classes = tp_capabilities_get_channel_classes (caps);
1484
1485   for (i = 0; i < classes->len; i++)
1486     {
1487       GValueArray *class_struct;
1488       GHashTable *fixed_prop;
1489       GStrv allowed_prop;
1490       TpHandleType handle_type;
1491       const gchar *chan_type;
1492
1493       class_struct = g_ptr_array_index (classes, i);
1494       tp_value_array_unpack (class_struct, 2,
1495           &fixed_prop,
1496           &allowed_prop);
1497
1498       handle_type = tp_asv_get_uint32 (fixed_prop,
1499           TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1500       if (handle_type != TP_HANDLE_TYPE_CONTACT)
1501         continue;
1502
1503       chan_type = tp_asv_get_string (fixed_prop,
1504           TP_PROP_CHANNEL_CHANNEL_TYPE);
1505
1506       if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1507         {
1508           capabilities |= EMPATHY_CAPABILITIES_FT;
1509         }
1510       else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1511         {
1512           const gchar *service;
1513
1514           service = tp_asv_get_string (fixed_prop,
1515               TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1516
1517           if (!tp_strdiff (service, "rfb"))
1518             capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1519         }
1520       else if (!tp_strdiff (chan_type,
1521         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1522         {
1523           guint j;
1524
1525           for (j = 0; allowed_prop[j] != NULL; j++)
1526             {
1527               if (!tp_strdiff (allowed_prop[j],
1528                     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1529                 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1530               else if (!tp_strdiff (allowed_prop[j],
1531                     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1532                 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1533             }
1534         }
1535     }
1536
1537   return capabilities;
1538 }
1539
1540 static void
1541 set_capabilities_from_tp_caps (EmpathyContact *self,
1542     TpCapabilities *caps)
1543 {
1544   EmpathyCapabilities capabilities;
1545
1546   if (caps == NULL)
1547     return;
1548
1549   capabilities = tp_caps_to_capabilities (caps);
1550   empathy_contact_set_capabilities (self, capabilities);
1551 }
1552
1553 static void
1554 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1555 {
1556   EmpathyContactPriv *priv = GET_PRIV (contact);
1557   const gchar *mime;
1558   const gchar *token;
1559   GFile *file;
1560
1561   token = tp_contact_get_avatar_token (priv->tp_contact);
1562   mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1563   file = tp_contact_get_avatar_file (priv->tp_contact);
1564
1565   if (file != NULL)
1566     {
1567       EmpathyAvatar *avatar;
1568       gchar *data;
1569       gsize len;
1570
1571       g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1572       avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1573           g_file_get_path (file));
1574       empathy_contact_set_avatar (contact, avatar);
1575       empathy_avatar_unref (avatar);
1576     }
1577   else
1578     {
1579       empathy_contact_set_avatar (contact, NULL);
1580     }
1581 }
1582
1583 EmpathyContact *
1584 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1585 {
1586   EmpathyContact *contact = NULL;
1587
1588   g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1589
1590   if (contacts_table == NULL)
1591     contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1592   else
1593     contact = g_hash_table_lookup (contacts_table, tp_contact);
1594
1595   if (contact == NULL)
1596     {
1597       contact = empathy_contact_new (tp_contact);
1598
1599       /* The hash table does not keep any ref.
1600        * contact keeps a ref to tp_contact, and is removed from the table in
1601        * contact_dispose() */
1602       g_hash_table_insert (contacts_table, tp_contact, contact);
1603     }
1604   else
1605     {
1606       g_object_ref (contact);
1607     }
1608
1609   return contact;
1610 }
1611