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