]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact.c
Rename empathy_contact_new_static to empathy_contact_new_for_log
[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 EmpathyContact *
392 empathy_contact_new_for_log (McAccount *account,
393                              const gchar *id,
394                              const gchar *name,
395                              gboolean is_user)
396 {
397   g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
398   g_return_val_if_fail (id != NULL, NULL);
399
400   return g_object_new (EMPATHY_TYPE_CONTACT,
401       "account", account,
402       "id", id,
403       "name", name,
404       "is-user", is_user,
405       NULL);
406 }
407
408 TpContact *
409 empathy_contact_get_tp_contact (EmpathyContact *contact)
410 {
411   EmpathyContactPriv *priv;
412
413   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
414
415   priv = GET_PRIV (contact);
416
417   return priv->tp_contact;
418 }
419
420 const gchar *
421 empathy_contact_get_id (EmpathyContact *contact)
422 {
423   EmpathyContactPriv *priv;
424
425   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
426
427   priv = GET_PRIV (contact);
428
429   if (priv->tp_contact != NULL)
430     return tp_contact_get_identifier (priv->tp_contact);
431
432   return priv->id;
433 }
434
435 void
436 empathy_contact_set_id (EmpathyContact *contact,
437                         const gchar *id)
438 {
439   EmpathyContactPriv *priv;
440
441   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
442   g_return_if_fail (id != NULL);
443
444   priv = GET_PRIV (contact);
445
446   /* We temporally ref the contact because it could be destroyed
447    * during the signal emition */
448   g_object_ref (contact);
449   if (tp_strdiff (id, priv->id))
450     {
451       g_free (priv->id);
452       priv->id = g_strdup (id);
453
454       g_object_notify (G_OBJECT (contact), "id");
455       if (EMP_STR_EMPTY (priv->name))
456           g_object_notify (G_OBJECT (contact), "name");
457     }
458
459   g_object_unref (contact);
460 }
461
462 const gchar *
463 empathy_contact_get_name (EmpathyContact *contact)
464 {
465   EmpathyContactPriv *priv;
466
467   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
468
469   priv = GET_PRIV (contact);
470
471   if (priv->tp_contact != NULL)
472     return tp_contact_get_alias (priv->tp_contact);
473
474   if (EMP_STR_EMPTY (priv->name))
475       return empathy_contact_get_id (contact);
476
477   return priv->name;
478 }
479
480 void
481 empathy_contact_set_name (EmpathyContact *contact,
482                           const gchar *name)
483 {
484   EmpathyContactPriv *priv;
485
486   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
487
488   priv = GET_PRIV (contact);
489
490   g_object_ref (contact);
491   if (tp_strdiff (name, priv->name))
492     {
493       g_free (priv->name);
494       priv->name = g_strdup (name);
495       g_object_notify (G_OBJECT (contact), "name");
496     }
497   g_object_unref (contact);
498 }
499
500 EmpathyAvatar *
501 empathy_contact_get_avatar (EmpathyContact *contact)
502 {
503   EmpathyContactPriv *priv;
504
505   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
506
507   priv = GET_PRIV (contact);
508
509   return priv->avatar;
510 }
511
512 void
513 empathy_contact_set_avatar (EmpathyContact *contact,
514                             EmpathyAvatar *avatar)
515 {
516   EmpathyContactPriv *priv;
517
518   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
519
520   priv = GET_PRIV (contact);
521
522   if (priv->avatar == avatar)
523     return;
524
525   if (priv->avatar)
526     {
527       empathy_avatar_unref (priv->avatar);
528       priv->avatar = NULL;
529     }
530
531   if (avatar)
532       priv->avatar = empathy_avatar_ref (avatar);
533
534   g_object_notify (G_OBJECT (contact), "avatar");
535 }
536
537 McAccount *
538 empathy_contact_get_account (EmpathyContact *contact)
539 {
540   EmpathyContactPriv *priv;
541
542   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
543
544   priv = GET_PRIV (contact);
545
546   if (priv->account == NULL && priv->tp_contact != NULL)
547     {
548       EmpathyAccountManager *manager;
549       TpConnection *connection;
550
551       /* FIXME: This assume the account manager already exists */
552       manager = empathy_account_manager_dup_singleton ();
553       connection = tp_contact_get_connection (priv->tp_contact);
554       priv->account = empathy_account_manager_get_account (manager, connection);
555       g_object_ref (priv->account);
556       g_object_unref (manager);
557     }
558
559   return priv->account;
560 }
561
562 TpConnection *
563 empathy_contact_get_connection (EmpathyContact *contact)
564 {
565   EmpathyContactPriv *priv;
566
567   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
568
569   priv = GET_PRIV (contact);
570
571   if (priv->tp_contact != NULL)
572     return tp_contact_get_connection (priv->tp_contact);
573
574   return NULL;
575 }
576
577 static McPresence
578 presence_type_to_mc_presence (TpConnectionPresenceType type)
579 {
580   switch (type)
581     {
582       case TP_CONNECTION_PRESENCE_TYPE_UNSET:
583       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
584       case TP_CONNECTION_PRESENCE_TYPE_ERROR:
585         return MC_PRESENCE_UNSET;
586       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
587         return MC_PRESENCE_OFFLINE;
588       case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
589         return MC_PRESENCE_AVAILABLE;
590       case TP_CONNECTION_PRESENCE_TYPE_AWAY:
591         return MC_PRESENCE_AWAY;
592       case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
593         return MC_PRESENCE_EXTENDED_AWAY;
594       case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
595         return MC_PRESENCE_HIDDEN;
596       case TP_CONNECTION_PRESENCE_TYPE_BUSY:
597         return MC_PRESENCE_DO_NOT_DISTURB;
598     }
599
600   return MC_PRESENCE_UNSET;
601 }
602
603 McPresence
604 empathy_contact_get_presence (EmpathyContact *contact)
605 {
606   EmpathyContactPriv *priv;
607
608   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
609
610   priv = GET_PRIV (contact);
611
612   if (priv->tp_contact != NULL)
613     return presence_type_to_mc_presence (tp_contact_get_presence_type (
614         priv->tp_contact));
615
616   return priv->presence;
617 }
618
619 void
620 empathy_contact_set_presence (EmpathyContact *contact,
621                               McPresence presence)
622 {
623   EmpathyContactPriv *priv;
624   McPresence old_presence;
625
626   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
627
628   priv = GET_PRIV (contact);
629
630   if (presence == priv->presence)
631     return;
632
633   old_presence = priv->presence;
634   priv->presence = presence;
635
636   g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
637
638   g_object_notify (G_OBJECT (contact), "presence");
639 }
640
641 const gchar *
642 empathy_contact_get_presence_message (EmpathyContact *contact)
643 {
644   EmpathyContactPriv *priv;
645
646   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
647
648   priv = GET_PRIV (contact);
649
650   if (priv->tp_contact != NULL)
651     return tp_contact_get_presence_message (priv->tp_contact);
652
653   return priv->presence_message;
654 }
655
656 void
657 empathy_contact_set_presence_message (EmpathyContact *contact,
658                                       const gchar *message)
659 {
660   EmpathyContactPriv *priv = GET_PRIV (contact);
661
662   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
663
664   if (!tp_strdiff (message, priv->presence_message))
665     return;
666
667   g_free (priv->presence_message);
668   priv->presence_message = g_strdup (message);
669
670   g_object_notify (G_OBJECT (contact), "presence-message");
671 }
672
673 guint
674 empathy_contact_get_handle (EmpathyContact *contact)
675 {
676   EmpathyContactPriv *priv;
677
678   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
679
680   priv = GET_PRIV (contact);
681
682   if (priv->tp_contact != NULL)
683     return tp_contact_get_handle (priv->tp_contact);
684
685   return priv->handle;
686 }
687
688 void
689 empathy_contact_set_handle (EmpathyContact *contact,
690                             guint handle)
691 {
692   EmpathyContactPriv *priv;
693
694   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
695
696   priv = GET_PRIV (contact);
697
698   g_object_ref (contact);
699   if (handle != priv->handle)
700     {
701       priv->handle = handle;
702       g_object_notify (G_OBJECT (contact), "handle");
703     }
704   g_object_unref (contact);
705 }
706
707 EmpathyCapabilities
708 empathy_contact_get_capabilities (EmpathyContact *contact)
709 {
710   EmpathyContactPriv *priv;
711
712   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
713
714   priv = GET_PRIV (contact);
715
716   return priv->capabilities;
717 }
718
719 void
720 empathy_contact_set_capabilities (EmpathyContact *contact,
721                                   EmpathyCapabilities capabilities)
722 {
723   EmpathyContactPriv *priv;
724
725   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
726
727   priv = GET_PRIV (contact);
728
729   if (priv->capabilities == capabilities)
730     return;
731
732   priv->capabilities = capabilities;
733
734   g_object_notify (G_OBJECT (contact), "capabilities");
735 }
736
737 gboolean
738 empathy_contact_is_user (EmpathyContact *contact)
739 {
740   EmpathyContactPriv *priv;
741
742   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
743
744   priv = GET_PRIV (contact);
745
746   return priv->is_user;
747 }
748
749 void
750 empathy_contact_set_is_user (EmpathyContact *contact,
751                              gboolean is_user)
752 {
753   EmpathyContactPriv *priv;
754
755   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
756
757   priv = GET_PRIV (contact);
758
759   if (priv->is_user == is_user)
760     return;
761
762   priv->is_user = is_user;
763
764   g_object_notify (G_OBJECT (contact), "is-user");
765 }
766
767 gboolean
768 empathy_contact_is_online (EmpathyContact *contact)
769 {
770   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
771
772   return (empathy_contact_get_presence (contact) > MC_PRESENCE_OFFLINE);
773 }
774
775 const gchar *
776 empathy_contact_get_status (EmpathyContact *contact)
777 {
778   const gchar *message;
779
780   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
781
782   message = empathy_contact_get_presence_message (contact);
783   if (!EMP_STR_EMPTY (message))
784     return message;
785
786   return empathy_presence_get_default_message (
787       empathy_contact_get_presence (contact));
788 }
789
790 gboolean
791 empathy_contact_can_voip (EmpathyContact *contact)
792 {
793   EmpathyContactPriv *priv;
794
795   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
796
797   priv = GET_PRIV (contact);
798
799   return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
800       EMPATHY_CAPABILITIES_VIDEO);
801 }
802
803 gboolean
804 empathy_contact_can_send_files (EmpathyContact *contact)
805 {
806   EmpathyContactPriv *priv;
807
808   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
809
810   priv = GET_PRIV (contact);
811
812   return priv->capabilities & EMPATHY_CAPABILITIES_FT;
813 }
814
815 static gchar *
816 contact_get_avatar_filename (EmpathyContact *contact,
817                              const gchar *token)
818 {
819   EmpathyContactPriv *priv = GET_PRIV (contact);
820   McAccount *account;
821   gchar *avatar_path;
822   gchar *avatar_file;
823   gchar *token_escaped;
824   gchar *contact_escaped;
825
826   if (EMP_STR_EMPTY (priv->id))
827     return NULL;
828
829   contact_escaped = tp_escape_as_identifier (priv->id);
830   token_escaped = tp_escape_as_identifier (token);
831   account = empathy_contact_get_account (contact);
832
833   /* FIXME: Do not use the account, but proto/cm instead */
834   avatar_path = g_build_filename (g_get_user_cache_dir (),
835       PACKAGE_NAME,
836       "avatars",
837       mc_account_get_unique_name (account),
838       contact_escaped,
839       NULL);
840   g_mkdir_with_parents (avatar_path, 0700);
841
842   avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
843
844   g_free (contact_escaped);
845   g_free (token_escaped);
846   g_free (avatar_path);
847
848   return avatar_file;
849 }
850
851 void
852 empathy_contact_load_avatar_data (EmpathyContact *contact,
853                                   const guchar  *data,
854                                   const gsize len,
855                                   const gchar *format,
856                                   const gchar *token)
857 {
858   EmpathyAvatar *avatar;
859   gchar *filename;
860   GError *error = NULL;
861
862   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
863   g_return_if_fail (data != NULL);
864   g_return_if_fail (len > 0);
865   g_return_if_fail (format != NULL);
866   g_return_if_fail (!EMP_STR_EMPTY (token));
867
868   /* Load and set the avatar */
869   avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
870       g_strdup (token));
871   empathy_contact_set_avatar (contact, avatar);
872   empathy_avatar_unref (avatar);
873
874   /* Save to cache if not yet in it */
875   filename = contact_get_avatar_filename (contact, token);
876   if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
877     {
878       if (!empathy_avatar_save_to_file (avatar, filename, &error))
879         {
880           DEBUG ("Failed to save avatar in cache: %s",
881             error ? error->message : "No error given");
882           g_clear_error (&error);
883         }
884       else
885           DEBUG ("Avatar saved to %s", filename);
886     }
887   g_free (filename);
888 }
889
890 gboolean
891 empathy_contact_load_avatar_cache (EmpathyContact *contact,
892                                    const gchar *token)
893 {
894   EmpathyAvatar *avatar = NULL;
895   gchar *filename;
896   gchar *data = NULL;
897   gsize len;
898   GError *error = NULL;
899
900   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
901   g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
902
903   /* Load the avatar from file if it exists */
904   filename = contact_get_avatar_filename (contact, token);
905   if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
906     {
907       if (!g_file_get_contents (filename, &data, &len, &error))
908         {
909           DEBUG ("Failed to load avatar from cache: %s",
910             error ? error->message : "No error given");
911           g_clear_error (&error);
912         }
913     }
914
915   if (data)
916     {
917       DEBUG ("Avatar loaded from %s", filename);
918       avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
919       empathy_contact_set_avatar (contact, avatar);
920       empathy_avatar_unref (avatar);
921     }
922
923   g_free (filename);
924
925   return data != NULL;
926 }
927
928 GType
929 empathy_avatar_get_type (void)
930 {
931   static GType type_id = 0;
932
933   if (!type_id)
934     {
935       type_id = g_boxed_type_register_static ("EmpathyAvatar",
936           (GBoxedCopyFunc) empathy_avatar_ref,
937           (GBoxedFreeFunc) empathy_avatar_unref);
938     }
939
940   return type_id;
941 }
942
943 EmpathyAvatar *
944 empathy_avatar_new (guchar *data,
945                     gsize len,
946                     gchar *format,
947                     gchar *token)
948 {
949   EmpathyAvatar *avatar;
950
951   avatar = g_slice_new0 (EmpathyAvatar);
952   avatar->data = data;
953   avatar->len = len;
954   avatar->format = format;
955   avatar->token = token;
956   avatar->refcount = 1;
957
958   return avatar;
959 }
960
961 void
962 empathy_avatar_unref (EmpathyAvatar *avatar)
963 {
964   g_return_if_fail (avatar != NULL);
965
966   avatar->refcount--;
967   if (avatar->refcount == 0)
968     {
969       g_free (avatar->data);
970       g_free (avatar->format);
971       g_free (avatar->token);
972       g_slice_free (EmpathyAvatar, avatar);
973     }
974 }
975
976 EmpathyAvatar *
977 empathy_avatar_ref (EmpathyAvatar *avatar)
978 {
979   g_return_val_if_fail (avatar != NULL, NULL);
980
981   avatar->refcount++;
982
983   return avatar;
984 }
985
986 /**
987  * empathy_avatar_save_to_file:
988  * @avatar: the avatar
989  * @filename: name of a file to write avatar to
990  * @error: return location for a GError, or NULL
991  *
992  * Save the avatar to a file named filename
993  *
994  * Returns: %TRUE on success, %FALSE if an error occurred 
995  */
996 gboolean
997 empathy_avatar_save_to_file (EmpathyAvatar *self,
998                              const gchar *filename,
999                              GError **error)
1000 {
1001   return g_file_set_contents (filename, self->data, self->len, error);
1002 }
1003