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