]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact.c
Added functions to determine if a contact has video capabilities
[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/util.h>
29
30 #include "empathy-contact.h"
31 #include "empathy-account-manager.h"
32 #include "empathy-utils.h"
33 #include "empathy-enum-types.h"
34 #include "empathy-marshal.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include "empathy-debug.h"
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
40 typedef struct {
41   TpContact *tp_contact;
42   EmpathyAccount *account;
43   gchar *id;
44   gchar *name;
45   EmpathyAvatar *avatar;
46   TpConnectionPresenceType presence;
47   gchar *presence_message;
48   guint handle;
49   EmpathyCapabilities capabilities;
50   gboolean is_user;
51   guint hash;
52   /* Location is composed of string keys and GValues.
53    * Example: a "city" key would have "Helsinki" as string GValue,
54    *          a "latitude" would have 65.0 as double GValue.
55    */
56   GHashTable *location;
57 } EmpathyContactPriv;
58
59 static void contact_finalize (GObject *object);
60 static void contact_get_property (GObject *object, guint param_id,
61     GValue *value, GParamSpec *pspec);
62 static void contact_set_property (GObject *object, guint param_id,
63     const GValue *value, GParamSpec *pspec);
64
65 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
66
67 enum
68 {
69   PROP_0,
70   PROP_TP_CONTACT,
71   PROP_ACCOUNT,
72   PROP_ID,
73   PROP_NAME,
74   PROP_AVATAR,
75   PROP_PRESENCE,
76   PROP_PRESENCE_MESSAGE,
77   PROP_HANDLE,
78   PROP_CAPABILITIES,
79   PROP_IS_USER,
80   PROP_LOCATION
81 };
82
83 enum {
84   PRESENCE_CHANGED,
85   LAST_SIGNAL
86 };
87
88 static guint signals[LAST_SIGNAL];
89
90 static void
91 tp_contact_notify_cb (TpContact *tp_contact,
92                       GParamSpec *param,
93                       GObject *contact)
94 {
95   EmpathyContactPriv *priv = GET_PRIV (contact);
96
97   /* Forward property notifications */
98   if (!tp_strdiff (param->name, "alias"))
99     g_object_notify (contact, "name");
100   else if (!tp_strdiff (param->name, "presence-type")) {
101     TpConnectionPresenceType presence;
102
103     presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
104     g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
105       priv->presence);
106     priv->presence = presence;
107     g_object_notify (contact, "presence");
108   }
109   else if (!tp_strdiff (param->name, "presence-message"))
110     g_object_notify (contact, "presence-message");
111   else if (!tp_strdiff (param->name, "identifier"))
112     g_object_notify (contact, "id");
113   else if (!tp_strdiff (param->name, "handle"))
114     g_object_notify (contact, "handle");
115 }
116
117 static void
118 contact_dispose (GObject *object)
119 {
120   EmpathyContactPriv *priv = GET_PRIV (object);
121
122   if (priv->tp_contact)
123     {
124       g_signal_handlers_disconnect_by_func (priv->tp_contact,
125           tp_contact_notify_cb, object);
126       g_object_unref (priv->tp_contact);
127     }
128   priv->tp_contact = NULL;
129
130   if (priv->account)
131     g_object_unref (priv->account);
132   priv->account = NULL;
133
134   G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
135 }
136
137 static void
138 empathy_contact_class_init (EmpathyContactClass *class)
139 {
140   GObjectClass *object_class;
141
142   object_class = G_OBJECT_CLASS (class);
143
144   object_class->finalize = contact_finalize;
145   object_class->dispose = contact_dispose;
146   object_class->get_property = contact_get_property;
147   object_class->set_property = contact_set_property;
148
149   g_object_class_install_property (object_class,
150       PROP_TP_CONTACT,
151       g_param_spec_object ("tp-contact",
152         "TpContact",
153         "The TpContact associated with the contact",
154         TP_TYPE_CONTACT,
155         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156
157   g_object_class_install_property (object_class,
158       PROP_ACCOUNT,
159       g_param_spec_object ("account",
160         "The account",
161         "The account associated with the contact",
162         EMPATHY_TYPE_ACCOUNT,
163         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164
165   g_object_class_install_property (object_class,
166       PROP_ID,
167       g_param_spec_string ("id",
168         "Contact id",
169         "String identifying contact",
170         NULL,
171         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172
173   g_object_class_install_property (object_class,
174       PROP_NAME,
175       g_param_spec_string ("name",
176         "Contact Name",
177         "The name of the contact",
178         NULL,
179         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180
181   g_object_class_install_property (object_class,
182       PROP_AVATAR,
183       g_param_spec_boxed ("avatar",
184         "Avatar image",
185         "The avatar image",
186         EMPATHY_TYPE_AVATAR,
187         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188
189   g_object_class_install_property (object_class,
190       PROP_PRESENCE,
191       g_param_spec_uint ("presence",
192         "Contact presence",
193         "Presence of contact",
194         TP_CONNECTION_PRESENCE_TYPE_UNSET,
195         NUM_TP_CONNECTION_PRESENCE_TYPES,
196         TP_CONNECTION_PRESENCE_TYPE_UNSET,
197         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198
199   g_object_class_install_property (object_class,
200       PROP_PRESENCE_MESSAGE,
201       g_param_spec_string ("presence-message",
202         "Contact presence message",
203         "Presence message of contact",
204         NULL,
205         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206
207   g_object_class_install_property (object_class,
208       PROP_HANDLE,
209       g_param_spec_uint ("handle",
210         "Contact Handle",
211         "The handle of the contact",
212         0,
213         G_MAXUINT,
214         0,
215         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216
217   g_object_class_install_property (object_class,
218       PROP_CAPABILITIES,
219       g_param_spec_flags ("capabilities",
220         "Contact Capabilities",
221         "Capabilities of the contact",
222         EMPATHY_TYPE_CAPABILITIES,
223         EMPATHY_CAPABILITIES_UNKNOWN,
224         G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225
226   g_object_class_install_property (object_class,
227       PROP_IS_USER,
228       g_param_spec_boolean ("is-user",
229         "Contact is-user",
230         "Is contact the user",
231         FALSE,
232         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
234
235   g_object_class_install_property (object_class,
236       PROP_LOCATION,
237       g_param_spec_boxed ("location",
238         "Contact location",
239         "Physical location of the contact",
240         G_TYPE_HASH_TABLE,
241         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
242
243   signals[PRESENCE_CHANGED] =
244     g_signal_new ("presence-changed",
245                   G_TYPE_FROM_CLASS (class),
246                   G_SIGNAL_RUN_LAST,
247                   0,
248                   NULL, NULL,
249                   _empathy_marshal_VOID__UINT_UINT,
250                   G_TYPE_NONE,
251                   2, G_TYPE_UINT,
252                   G_TYPE_UINT);
253
254   g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
255 }
256
257 static void
258 empathy_contact_init (EmpathyContact *contact)
259 {
260   EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
261     EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
262
263   contact->priv = priv;
264
265   priv->location = NULL;
266 }
267
268 static void
269 contact_finalize (GObject *object)
270 {
271   EmpathyContactPriv *priv;
272
273   priv = GET_PRIV (object);
274
275   DEBUG ("finalize: %p", object);
276
277   g_free (priv->name);
278   g_free (priv->id);
279   g_free (priv->presence_message);
280
281   if (priv->avatar)
282       empathy_avatar_unref (priv->avatar);
283
284   if (priv->location != NULL)
285       g_hash_table_unref (priv->location);
286
287   G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
288 }
289
290 static void
291 set_tp_contact (EmpathyContact *contact,
292                 TpContact *tp_contact)
293 {
294   EmpathyContactPriv *priv = GET_PRIV (contact);
295
296   if (tp_contact == NULL)
297     return;
298
299   g_assert (priv->tp_contact == NULL);
300   priv->tp_contact = g_object_ref (tp_contact);
301   priv->presence = empathy_contact_get_presence (contact);
302
303   g_signal_connect (priv->tp_contact, "notify",
304     G_CALLBACK (tp_contact_notify_cb), contact);
305 }
306
307 static void
308 contact_get_property (GObject *object,
309                       guint param_id,
310                       GValue *value,
311                       GParamSpec *pspec)
312 {
313   EmpathyContact *contact = EMPATHY_CONTACT (object);
314
315   switch (param_id)
316     {
317       case PROP_TP_CONTACT:
318         g_value_set_object (value, empathy_contact_get_tp_contact (contact));
319         break;
320       case PROP_ACCOUNT:
321         g_value_set_object (value, empathy_contact_get_account (contact));
322         break;
323       case PROP_ID:
324         g_value_set_string (value, empathy_contact_get_id (contact));
325         break;
326       case PROP_NAME:
327         g_value_set_string (value, empathy_contact_get_name (contact));
328         break;
329       case PROP_AVATAR:
330         g_value_set_boxed (value, empathy_contact_get_avatar (contact));
331         break;
332       case PROP_PRESENCE:
333         g_value_set_uint (value, empathy_contact_get_presence (contact));
334         break;
335       case PROP_PRESENCE_MESSAGE:
336         g_value_set_string (value, empathy_contact_get_presence_message (contact));
337         break;
338       case PROP_HANDLE:
339         g_value_set_uint (value, empathy_contact_get_handle (contact));
340         break;
341       case PROP_CAPABILITIES:
342         g_value_set_flags (value, empathy_contact_get_capabilities (contact));
343         break;
344       case PROP_IS_USER:
345         g_value_set_boolean (value, empathy_contact_is_user (contact));
346         break;
347       default:
348         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
349         break;
350     };
351 }
352
353 static void
354 contact_set_property (GObject *object,
355                       guint param_id,
356                       const GValue *value,
357                       GParamSpec *pspec)
358 {
359   EmpathyContact *contact = EMPATHY_CONTACT (object);
360   EmpathyContactPriv *priv = GET_PRIV (object);
361
362   switch (param_id)
363     {
364       case PROP_TP_CONTACT:
365         set_tp_contact (contact, g_value_get_object (value));
366         break;
367       case PROP_ACCOUNT:
368         g_assert (priv->account == NULL);
369         priv->account = g_value_dup_object (value);
370         break;
371       case PROP_ID:
372         empathy_contact_set_id (contact, g_value_get_string (value));
373         break;
374       case PROP_NAME:
375         empathy_contact_set_name (contact, g_value_get_string (value));
376         break;
377       case PROP_AVATAR:
378         empathy_contact_set_avatar (contact, g_value_get_boxed (value));
379         break;
380       case PROP_PRESENCE:
381         empathy_contact_set_presence (contact, g_value_get_uint (value));
382         break;
383       case PROP_PRESENCE_MESSAGE:
384         empathy_contact_set_presence_message (contact, g_value_get_string (value));
385         break;
386       case PROP_HANDLE:
387         empathy_contact_set_handle (contact, g_value_get_uint (value));
388         break;
389       case PROP_CAPABILITIES:
390         empathy_contact_set_capabilities (contact, g_value_get_flags (value));
391         break;
392       case PROP_IS_USER:
393         empathy_contact_set_is_user (contact, g_value_get_boolean (value));
394         break;
395       default:
396         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
397         break;
398     };
399 }
400
401 EmpathyContact *
402 empathy_contact_new (TpContact *tp_contact)
403 {
404   g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
405
406   return g_object_new (EMPATHY_TYPE_CONTACT,
407       "tp-contact", tp_contact,
408       NULL);
409 }
410
411 EmpathyContact *
412 empathy_contact_new_for_log (EmpathyAccount *account,
413                              const gchar *id,
414                              const gchar *name,
415                              gboolean is_user)
416 {
417   g_return_val_if_fail (id != NULL, NULL);
418
419   return g_object_new (EMPATHY_TYPE_CONTACT,
420       "account", account,
421       "id", id,
422       "name", name,
423       "is-user", is_user,
424       NULL);
425 }
426
427 TpContact *
428 empathy_contact_get_tp_contact (EmpathyContact *contact)
429 {
430   EmpathyContactPriv *priv;
431
432   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
433
434   priv = GET_PRIV (contact);
435
436   return priv->tp_contact;
437 }
438
439 const gchar *
440 empathy_contact_get_id (EmpathyContact *contact)
441 {
442   EmpathyContactPriv *priv;
443
444   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
445
446   priv = GET_PRIV (contact);
447
448   if (priv->tp_contact != NULL)
449     return tp_contact_get_identifier (priv->tp_contact);
450
451   return priv->id;
452 }
453
454 void
455 empathy_contact_set_id (EmpathyContact *contact,
456                         const gchar *id)
457 {
458   EmpathyContactPriv *priv;
459
460   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
461   g_return_if_fail (id != NULL);
462
463   priv = GET_PRIV (contact);
464
465   /* We temporally ref the contact because it could be destroyed
466    * during the signal emition */
467   g_object_ref (contact);
468   if (tp_strdiff (id, priv->id))
469     {
470       g_free (priv->id);
471       priv->id = g_strdup (id);
472
473       g_object_notify (G_OBJECT (contact), "id");
474       if (EMP_STR_EMPTY (priv->name))
475           g_object_notify (G_OBJECT (contact), "name");
476     }
477
478   g_object_unref (contact);
479 }
480
481 const gchar *
482 empathy_contact_get_name (EmpathyContact *contact)
483 {
484   EmpathyContactPriv *priv;
485
486   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
487
488   priv = GET_PRIV (contact);
489
490   if (priv->tp_contact != NULL)
491     return tp_contact_get_alias (priv->tp_contact);
492
493   if (EMP_STR_EMPTY (priv->name))
494       return empathy_contact_get_id (contact);
495
496   return priv->name;
497 }
498
499 void
500 empathy_contact_set_name (EmpathyContact *contact,
501                           const gchar *name)
502 {
503   EmpathyContactPriv *priv;
504
505   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
506
507   priv = GET_PRIV (contact);
508
509   g_object_ref (contact);
510   if (tp_strdiff (name, priv->name))
511     {
512       g_free (priv->name);
513       priv->name = g_strdup (name);
514       g_object_notify (G_OBJECT (contact), "name");
515     }
516   g_object_unref (contact);
517 }
518
519 EmpathyAvatar *
520 empathy_contact_get_avatar (EmpathyContact *contact)
521 {
522   EmpathyContactPriv *priv;
523
524   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
525
526   priv = GET_PRIV (contact);
527
528   return priv->avatar;
529 }
530
531 void
532 empathy_contact_set_avatar (EmpathyContact *contact,
533                             EmpathyAvatar *avatar)
534 {
535   EmpathyContactPriv *priv;
536
537   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
538
539   priv = GET_PRIV (contact);
540
541   if (priv->avatar == avatar)
542     return;
543
544   if (priv->avatar)
545     {
546       empathy_avatar_unref (priv->avatar);
547       priv->avatar = NULL;
548     }
549
550   if (avatar)
551       priv->avatar = empathy_avatar_ref (avatar);
552
553   g_object_notify (G_OBJECT (contact), "avatar");
554 }
555
556 EmpathyAccount *
557 empathy_contact_get_account (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->account == NULL && priv->tp_contact != NULL)
566     {
567       EmpathyAccountManager *manager;
568       TpConnection *connection;
569
570       /* FIXME: This assume the account manager already exists */
571       manager = empathy_account_manager_dup_singleton ();
572       connection = tp_contact_get_connection (priv->tp_contact);
573       priv->account = empathy_account_manager_get_account (manager, connection);
574       g_object_ref (priv->account);
575       g_object_unref (manager);
576     }
577
578   return priv->account;
579 }
580
581 TpConnection *
582 empathy_contact_get_connection (EmpathyContact *contact)
583 {
584   EmpathyContactPriv *priv;
585
586   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
587
588   priv = GET_PRIV (contact);
589
590   if (priv->tp_contact != NULL)
591     return tp_contact_get_connection (priv->tp_contact);
592
593   return NULL;
594 }
595
596 TpConnectionPresenceType
597 empathy_contact_get_presence (EmpathyContact *contact)
598 {
599   EmpathyContactPriv *priv;
600
601   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
602     TP_CONNECTION_PRESENCE_TYPE_UNSET);
603
604   priv = GET_PRIV (contact);
605
606   if (priv->tp_contact != NULL)
607     return tp_contact_get_presence_type (priv->tp_contact);
608
609   return priv->presence;
610 }
611
612 void
613 empathy_contact_set_presence (EmpathyContact *contact,
614                               TpConnectionPresenceType presence)
615 {
616   EmpathyContactPriv *priv;
617   TpConnectionPresenceType old_presence;
618
619   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
620
621   priv = GET_PRIV (contact);
622
623   if (presence == priv->presence)
624     return;
625
626   old_presence = priv->presence;
627   priv->presence = presence;
628
629   g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
630
631   g_object_notify (G_OBJECT (contact), "presence");
632 }
633
634 const gchar *
635 empathy_contact_get_presence_message (EmpathyContact *contact)
636 {
637   EmpathyContactPriv *priv;
638
639   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
640
641   priv = GET_PRIV (contact);
642
643   if (priv->tp_contact != NULL)
644     return tp_contact_get_presence_message (priv->tp_contact);
645
646   return priv->presence_message;
647 }
648
649 void
650 empathy_contact_set_presence_message (EmpathyContact *contact,
651                                       const gchar *message)
652 {
653   EmpathyContactPriv *priv = GET_PRIV (contact);
654
655   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
656
657   if (!tp_strdiff (message, priv->presence_message))
658     return;
659
660   g_free (priv->presence_message);
661   priv->presence_message = g_strdup (message);
662
663   g_object_notify (G_OBJECT (contact), "presence-message");
664 }
665
666 guint
667 empathy_contact_get_handle (EmpathyContact *contact)
668 {
669   EmpathyContactPriv *priv;
670
671   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
672
673   priv = GET_PRIV (contact);
674
675   if (priv->tp_contact != NULL)
676     return tp_contact_get_handle (priv->tp_contact);
677
678   return priv->handle;
679 }
680
681 void
682 empathy_contact_set_handle (EmpathyContact *contact,
683                             guint handle)
684 {
685   EmpathyContactPriv *priv;
686
687   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
688
689   priv = GET_PRIV (contact);
690
691   g_object_ref (contact);
692   if (handle != priv->handle)
693     {
694       priv->handle = handle;
695       g_object_notify (G_OBJECT (contact), "handle");
696     }
697   g_object_unref (contact);
698 }
699
700 EmpathyCapabilities
701 empathy_contact_get_capabilities (EmpathyContact *contact)
702 {
703   EmpathyContactPriv *priv;
704
705   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
706
707   priv = GET_PRIV (contact);
708
709   return priv->capabilities;
710 }
711
712 void
713 empathy_contact_set_capabilities (EmpathyContact *contact,
714                                   EmpathyCapabilities capabilities)
715 {
716   EmpathyContactPriv *priv;
717
718   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
719
720   priv = GET_PRIV (contact);
721
722   if (priv->capabilities == capabilities)
723     return;
724
725   priv->capabilities = capabilities;
726
727   g_object_notify (G_OBJECT (contact), "capabilities");
728 }
729
730 gboolean
731 empathy_contact_is_user (EmpathyContact *contact)
732 {
733   EmpathyContactPriv *priv;
734
735   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
736
737   priv = GET_PRIV (contact);
738
739   return priv->is_user;
740 }
741
742 void
743 empathy_contact_set_is_user (EmpathyContact *contact,
744                              gboolean is_user)
745 {
746   EmpathyContactPriv *priv;
747
748   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
749
750   priv = GET_PRIV (contact);
751
752   if (priv->is_user == is_user)
753     return;
754
755   priv->is_user = is_user;
756
757   g_object_notify (G_OBJECT (contact), "is-user");
758 }
759
760 gboolean
761 empathy_contact_is_online (EmpathyContact *contact)
762 {
763   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
764
765   switch (empathy_contact_get_presence (contact))
766     {
767       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
768       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
769       case TP_CONNECTION_PRESENCE_TYPE_ERROR:
770         return FALSE;
771       default:
772         return TRUE;
773     }
774 }
775
776 const gchar *
777 empathy_contact_get_status (EmpathyContact *contact)
778 {
779   const gchar *message;
780
781   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
782
783   message = empathy_contact_get_presence_message (contact);
784   if (!EMP_STR_EMPTY (message))
785     return message;
786
787   return empathy_presence_get_default_message (
788       empathy_contact_get_presence (contact));
789 }
790
791 gboolean
792 empathy_contact_can_voip (EmpathyContact *contact)
793 {
794   EmpathyContactPriv *priv;
795
796   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
797
798   priv = GET_PRIV (contact);
799
800   return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
801       EMPATHY_CAPABILITIES_VIDEO);
802 }
803
804 gboolean
805 empathy_contact_can_voip_audio (EmpathyContact *contact)
806 {
807   EmpathyContactPriv *priv;
808
809   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
810
811   priv = GET_PRIV (contact);
812
813   return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
814 }
815
816 gboolean
817 empathy_contact_can_voip_video (EmpathyContact *contact)
818 {
819   EmpathyContactPriv *priv;
820
821   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
822
823   priv = GET_PRIV (contact);
824
825   return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
826 }
827
828 gboolean
829 empathy_contact_can_send_files (EmpathyContact *contact)
830 {
831   EmpathyContactPriv *priv;
832
833   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
834
835   priv = GET_PRIV (contact);
836
837   return priv->capabilities & EMPATHY_CAPABILITIES_FT;
838 }
839
840 gboolean
841 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
842 {
843   EmpathyContactPriv *priv;
844
845   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
846
847   priv = GET_PRIV (contact);
848
849   return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
850 }
851
852 static gchar *
853 contact_get_avatar_filename (EmpathyContact *contact,
854                              const gchar *token)
855 {
856   EmpathyAccount *account;
857   gchar *avatar_path;
858   gchar *avatar_file;
859   gchar *token_escaped;
860   gchar *contact_escaped;
861
862   if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
863     return NULL;
864
865   contact_escaped = tp_escape_as_identifier (empathy_contact_get_id (contact));
866   token_escaped = tp_escape_as_identifier (token);
867   account = empathy_contact_get_account (contact);
868
869   /* FIXME: Do not use the account, but proto/cm instead */
870   avatar_path = g_build_filename (g_get_user_cache_dir (),
871       PACKAGE_NAME,
872       "avatars",
873       empathy_account_get_unique_name (account),
874       contact_escaped,
875       NULL);
876   g_mkdir_with_parents (avatar_path, 0700);
877
878   avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
879
880   g_free (contact_escaped);
881   g_free (token_escaped);
882   g_free (avatar_path);
883
884   return avatar_file;
885 }
886
887 void
888 empathy_contact_load_avatar_data (EmpathyContact *contact,
889                                   const guchar *data,
890                                   const gsize len,
891                                   const gchar *format,
892                                   const gchar *token)
893 {
894   EmpathyAvatar *avatar;
895   gchar *filename;
896   GError *error = NULL;
897
898   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
899   g_return_if_fail (data != NULL);
900   g_return_if_fail (len > 0);
901   g_return_if_fail (format != NULL);
902   g_return_if_fail (!EMP_STR_EMPTY (token));
903
904   /* Load and set the avatar */
905   filename = contact_get_avatar_filename (contact, token);
906   avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
907       g_strdup (token), filename);
908   empathy_contact_set_avatar (contact, avatar);
909   empathy_avatar_unref (avatar);
910
911   /* Save to cache if not yet in it */
912   if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
913     {
914       if (!empathy_avatar_save_to_file (avatar, filename, &error))
915         {
916           DEBUG ("Failed to save avatar in cache: %s",
917             error ? error->message : "No error given");
918           g_clear_error (&error);
919         }
920       else
921           DEBUG ("Avatar saved to %s", filename);
922     }
923 }
924
925 gboolean
926 empathy_contact_load_avatar_cache (EmpathyContact *contact,
927                                    const gchar *token)
928 {
929   EmpathyAvatar *avatar = NULL;
930   gchar *filename;
931   gchar *data = NULL;
932   gsize len;
933   GError *error = NULL;
934
935   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
936   g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
937
938   /* Load the avatar from file if it exists */
939   filename = contact_get_avatar_filename (contact, token);
940   if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
941     {
942       if (!g_file_get_contents (filename, &data, &len, &error))
943         {
944           DEBUG ("Failed to load avatar from cache: %s",
945             error ? error->message : "No error given");
946           g_clear_error (&error);
947         }
948     }
949
950   if (data)
951     {
952       DEBUG ("Avatar loaded from %s", filename);
953       avatar = empathy_avatar_new (data, len, NULL, g_strdup (token), filename);
954       empathy_contact_set_avatar (contact, avatar);
955       empathy_avatar_unref (avatar);
956     }
957
958   return data != NULL;
959 }
960
961 GType
962 empathy_avatar_get_type (void)
963 {
964   static GType type_id = 0;
965
966   if (!type_id)
967     {
968       type_id = g_boxed_type_register_static ("EmpathyAvatar",
969           (GBoxedCopyFunc) empathy_avatar_ref,
970           (GBoxedFreeFunc) empathy_avatar_unref);
971     }
972
973   return type_id;
974 }
975
976 /**
977  * empathy_avatar_new:
978  * @data: the avatar data
979  * @len: the size of avatar data
980  * @format: the mime type of the avatar image
981  * @token: the token of the avatar
982  * @filename: the filename where the avatar is stored in cache
983  *
984  * Create a #EmpathyAvatar from the provided data. This function takes the
985  * ownership of @data, @format, @token and @filename.
986  *
987  * Returns: a new #EmpathyAvatar
988  */
989 EmpathyAvatar *
990 empathy_avatar_new (guchar *data,
991                     gsize len,
992                     gchar *format,
993                     gchar *token,
994                     gchar *filename)
995 {
996   EmpathyAvatar *avatar;
997
998   avatar = g_slice_new0 (EmpathyAvatar);
999   avatar->data = data;
1000   avatar->len = len;
1001   avatar->format = format;
1002   avatar->token = token;
1003   avatar->filename = filename;
1004   avatar->refcount = 1;
1005
1006   return avatar;
1007 }
1008
1009 void
1010 empathy_avatar_unref (EmpathyAvatar *avatar)
1011 {
1012   g_return_if_fail (avatar != NULL);
1013
1014   avatar->refcount--;
1015   if (avatar->refcount == 0)
1016     {
1017       g_free (avatar->data);
1018       g_free (avatar->format);
1019       g_free (avatar->token);
1020       g_slice_free (EmpathyAvatar, avatar);
1021     }
1022 }
1023
1024 EmpathyAvatar *
1025 empathy_avatar_ref (EmpathyAvatar *avatar)
1026 {
1027   g_return_val_if_fail (avatar != NULL, NULL);
1028
1029   avatar->refcount++;
1030
1031   return avatar;
1032 }
1033
1034 /**
1035  * empathy_avatar_save_to_file:
1036  * @avatar: the avatar
1037  * @filename: name of a file to write avatar to
1038  * @error: return location for a GError, or NULL
1039  *
1040  * Save the avatar to a file named filename
1041  *
1042  * Returns: %TRUE on success, %FALSE if an error occurred
1043  */
1044 gboolean
1045 empathy_avatar_save_to_file (EmpathyAvatar *self,
1046                              const gchar *filename,
1047                              GError **error)
1048 {
1049   return g_file_set_contents (filename, self->data, self->len, error);
1050 }
1051
1052 /**
1053  * empathy_contact_get_location:
1054  * @contact: an #EmpathyContact
1055  *
1056  * Returns the user's location if available.  The keys are defined in
1057  * empathy-location.h. If the contact doesn't have location
1058  * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1059  * you are done with the #GHashTable.
1060  *
1061  * It is composed of string keys and GValues.  Keys are
1062  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1063  * Example: a "city" key would have "Helsinki" as string GValue,
1064  *          a "latitude" would have 65.0 as double GValue.
1065  *
1066  * Returns: a #GHashTable of location values
1067  */
1068 GHashTable *
1069 empathy_contact_get_location (EmpathyContact *contact)
1070 {
1071   EmpathyContactPriv *priv;
1072
1073   g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1074
1075   priv = GET_PRIV (contact);
1076
1077   return priv->location;
1078 }
1079
1080 /**
1081  * empathy_contact_set_location:
1082  * @contact: an #EmpathyContact
1083  * @location: a #GHashTable of the location
1084  *
1085  * Sets the user's location based on the location #GHashTable passed.
1086  * It is composed of string keys and GValues.  Keys are
1087  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1088  * Example: a "city" key would have "Helsinki" as string GValue,
1089  *          a "latitude" would have 65.0 as double GValue.
1090  */
1091 void
1092 empathy_contact_set_location (EmpathyContact *contact,
1093                               GHashTable *location)
1094 {
1095   EmpathyContactPriv *priv;
1096
1097   g_return_if_fail (EMPATHY_CONTACT (contact));
1098   g_return_if_fail (location != NULL);
1099
1100   priv = GET_PRIV (contact);
1101
1102   if (priv->location != NULL)
1103     g_hash_table_unref (priv->location);
1104
1105   priv->location = g_hash_table_ref (location);
1106   g_object_notify (G_OBJECT (contact), "location");
1107 }
1108
1109 /**
1110  * empathy_contact_equal:
1111  * @contact1: an #EmpathyContact
1112  * @contact2: an #EmpathyContact
1113  *
1114  * Returns FALSE if one of the contacts is NULL but the other is not.
1115  * Otherwise returns TRUE if both pointer are equal or if they bith
1116  * refer to the same id.
1117  * It's only necessary to call this function if your contact objects
1118  * come from logs where contacts are created dynamically and comparing
1119  * pointers is not enough.
1120  */
1121 gboolean
1122 empathy_contact_equal (gconstpointer contact1,
1123                        gconstpointer contact2)
1124 {
1125   EmpathyContact *c1;
1126   EmpathyContact *c2;
1127   const gchar *id1;
1128   const gchar *id2;
1129
1130   if ((contact1 == NULL) != (contact2 == NULL)) {
1131     return FALSE;
1132   }
1133   if (contact1 == contact2) {
1134     return TRUE;
1135   }
1136   c1 = EMPATHY_CONTACT (contact1);
1137   c2 = EMPATHY_CONTACT (contact2);
1138   id1 = empathy_contact_get_id (c1);
1139   id2 = empathy_contact_get_id (c2);
1140   if (!tp_strdiff (id1, id2)) {
1141     return TRUE;
1142   }
1143   return FALSE;
1144 }