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