]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact.c
empathy-contact: fix coding style
[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   /* Location is composed of string keys and GValues.
54    * Example: a "city" key would have "Helsinki" as string GValue,
55    *          a "latitude" would have 65.0 as double GValue.
56    */
57   GHashTable *location;
58 } EmpathyContactPriv;
59
60 static void contact_finalize (GObject *object);
61 static void contact_get_property (GObject *object, guint param_id,
62     GValue *value, GParamSpec *pspec);
63 static void contact_set_property (GObject *object, guint param_id,
64     const GValue *value, GParamSpec *pspec);
65
66 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
67
68 enum
69 {
70   PROP_0,
71   PROP_TP_CONTACT,
72   PROP_ACCOUNT,
73   PROP_ID,
74   PROP_NAME,
75   PROP_AVATAR,
76   PROP_PRESENCE,
77   PROP_PRESENCE_MESSAGE,
78   PROP_HANDLE,
79   PROP_CAPABILITIES,
80   PROP_IS_USER,
81   PROP_LOCATION
82 };
83
84 enum {
85   PRESENCE_CHANGED,
86   LAST_SIGNAL
87 };
88
89 static guint signals[LAST_SIGNAL];
90
91 static void
92 tp_contact_notify_cb (TpContact *tp_contact,
93                       GParamSpec *param,
94                       GObject *contact)
95 {
96   EmpathyContactPriv *priv = GET_PRIV (contact);
97
98   /* Forward property notifications */
99   if (!tp_strdiff (param->name, "alias"))
100     g_object_notify (contact, "name");
101   else if (!tp_strdiff (param->name, "presence-type")) {
102     McPresence presence;
103
104     presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
105     g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, 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         MC_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         MC_PRESENCE_UNSET,
195         LAST_MC_PRESENCE,
196         MC_PRESENCE_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__ENUM_ENUM,
250                   G_TYPE_NONE,
251                   2, MC_TYPE_PRESENCE,
252                   MC_TYPE_PRESENCE);
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 (McAccount *account,
413                              const gchar *id,
414                              const gchar *name,
415                              gboolean is_user)
416 {
417   g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
418   g_return_val_if_fail (id != NULL, NULL);
419
420   return g_object_new (EMPATHY_TYPE_CONTACT,
421       "account", account,
422       "id", id,
423       "name", name,
424       "is-user", is_user,
425       NULL);
426 }
427
428 TpContact *
429 empathy_contact_get_tp_contact (EmpathyContact *contact)
430 {
431   EmpathyContactPriv *priv;
432
433   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
434
435   priv = GET_PRIV (contact);
436
437   return priv->tp_contact;
438 }
439
440 const gchar *
441 empathy_contact_get_id (EmpathyContact *contact)
442 {
443   EmpathyContactPriv *priv;
444
445   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
446
447   priv = GET_PRIV (contact);
448
449   if (priv->tp_contact != NULL)
450     return tp_contact_get_identifier (priv->tp_contact);
451
452   return priv->id;
453 }
454
455 void
456 empathy_contact_set_id (EmpathyContact *contact,
457                         const gchar *id)
458 {
459   EmpathyContactPriv *priv;
460
461   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
462   g_return_if_fail (id != NULL);
463
464   priv = GET_PRIV (contact);
465
466   /* We temporally ref the contact because it could be destroyed
467    * during the signal emition */
468   g_object_ref (contact);
469   if (tp_strdiff (id, priv->id))
470     {
471       g_free (priv->id);
472       priv->id = g_strdup (id);
473
474       g_object_notify (G_OBJECT (contact), "id");
475       if (EMP_STR_EMPTY (priv->name))
476           g_object_notify (G_OBJECT (contact), "name");
477     }
478
479   g_object_unref (contact);
480 }
481
482 const gchar *
483 empathy_contact_get_name (EmpathyContact *contact)
484 {
485   EmpathyContactPriv *priv;
486
487   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
488
489   priv = GET_PRIV (contact);
490
491   if (priv->tp_contact != NULL)
492     return tp_contact_get_alias (priv->tp_contact);
493
494   if (EMP_STR_EMPTY (priv->name))
495       return empathy_contact_get_id (contact);
496
497   return priv->name;
498 }
499
500 void
501 empathy_contact_set_name (EmpathyContact *contact,
502                           const gchar *name)
503 {
504   EmpathyContactPriv *priv;
505
506   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
507
508   priv = GET_PRIV (contact);
509
510   g_object_ref (contact);
511   if (tp_strdiff (name, priv->name))
512     {
513       g_free (priv->name);
514       priv->name = g_strdup (name);
515       g_object_notify (G_OBJECT (contact), "name");
516     }
517   g_object_unref (contact);
518 }
519
520 EmpathyAvatar *
521 empathy_contact_get_avatar (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   return priv->avatar;
530 }
531
532 void
533 empathy_contact_set_avatar (EmpathyContact *contact,
534                             EmpathyAvatar *avatar)
535 {
536   EmpathyContactPriv *priv;
537
538   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
539
540   priv = GET_PRIV (contact);
541
542   if (priv->avatar == avatar)
543     return;
544
545   if (priv->avatar)
546     {
547       empathy_avatar_unref (priv->avatar);
548       priv->avatar = NULL;
549     }
550
551   if (avatar)
552       priv->avatar = empathy_avatar_ref (avatar);
553
554   g_object_notify (G_OBJECT (contact), "avatar");
555 }
556
557 McAccount *
558 empathy_contact_get_account (EmpathyContact *contact)
559 {
560   EmpathyContactPriv *priv;
561
562   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
563
564   priv = GET_PRIV (contact);
565
566   if (priv->account == NULL && priv->tp_contact != NULL)
567     {
568       EmpathyAccountManager *manager;
569       TpConnection *connection;
570
571       /* FIXME: This assume the account manager already exists */
572       manager = empathy_account_manager_dup_singleton ();
573       connection = tp_contact_get_connection (priv->tp_contact);
574       priv->account = empathy_account_manager_get_account (manager, connection);
575       g_object_ref (priv->account);
576       g_object_unref (manager);
577     }
578
579   return priv->account;
580 }
581
582 TpConnection *
583 empathy_contact_get_connection (EmpathyContact *contact)
584 {
585   EmpathyContactPriv *priv;
586
587   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
588
589   priv = GET_PRIV (contact);
590
591   if (priv->tp_contact != NULL)
592     return tp_contact_get_connection (priv->tp_contact);
593
594   return NULL;
595 }
596
597 static McPresence
598 presence_type_to_mc_presence (TpConnectionPresenceType type)
599 {
600   switch (type)
601     {
602       case TP_CONNECTION_PRESENCE_TYPE_UNSET:
603       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
604       case TP_CONNECTION_PRESENCE_TYPE_ERROR:
605         return MC_PRESENCE_UNSET;
606       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
607         return MC_PRESENCE_OFFLINE;
608       case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
609         return MC_PRESENCE_AVAILABLE;
610       case TP_CONNECTION_PRESENCE_TYPE_AWAY:
611         return MC_PRESENCE_AWAY;
612       case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
613         return MC_PRESENCE_EXTENDED_AWAY;
614       case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
615         return MC_PRESENCE_HIDDEN;
616       case TP_CONNECTION_PRESENCE_TYPE_BUSY:
617         return MC_PRESENCE_DO_NOT_DISTURB;
618     }
619
620   return MC_PRESENCE_UNSET;
621 }
622
623 McPresence
624 empathy_contact_get_presence (EmpathyContact *contact)
625 {
626   EmpathyContactPriv *priv;
627
628   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
629
630   priv = GET_PRIV (contact);
631
632   if (priv->tp_contact != NULL)
633     return presence_type_to_mc_presence (tp_contact_get_presence_type (
634         priv->tp_contact));
635
636   return priv->presence;
637 }
638
639 void
640 empathy_contact_set_presence (EmpathyContact *contact,
641                               McPresence presence)
642 {
643   EmpathyContactPriv *priv;
644   McPresence old_presence;
645
646   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
647
648   priv = GET_PRIV (contact);
649
650   if (presence == priv->presence)
651     return;
652
653   old_presence = priv->presence;
654   priv->presence = presence;
655
656   g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
657
658   g_object_notify (G_OBJECT (contact), "presence");
659 }
660
661 const gchar *
662 empathy_contact_get_presence_message (EmpathyContact *contact)
663 {
664   EmpathyContactPriv *priv;
665
666   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
667
668   priv = GET_PRIV (contact);
669
670   if (priv->tp_contact != NULL)
671     return tp_contact_get_presence_message (priv->tp_contact);
672
673   return priv->presence_message;
674 }
675
676 void
677 empathy_contact_set_presence_message (EmpathyContact *contact,
678                                       const gchar *message)
679 {
680   EmpathyContactPriv *priv = GET_PRIV (contact);
681
682   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
683
684   if (!tp_strdiff (message, priv->presence_message))
685     return;
686
687   g_free (priv->presence_message);
688   priv->presence_message = g_strdup (message);
689
690   g_object_notify (G_OBJECT (contact), "presence-message");
691 }
692
693 guint
694 empathy_contact_get_handle (EmpathyContact *contact)
695 {
696   EmpathyContactPriv *priv;
697
698   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
699
700   priv = GET_PRIV (contact);
701
702   if (priv->tp_contact != NULL)
703     return tp_contact_get_handle (priv->tp_contact);
704
705   return priv->handle;
706 }
707
708 void
709 empathy_contact_set_handle (EmpathyContact *contact,
710                             guint handle)
711 {
712   EmpathyContactPriv *priv;
713
714   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
715
716   priv = GET_PRIV (contact);
717
718   g_object_ref (contact);
719   if (handle != priv->handle)
720     {
721       priv->handle = handle;
722       g_object_notify (G_OBJECT (contact), "handle");
723     }
724   g_object_unref (contact);
725 }
726
727 EmpathyCapabilities
728 empathy_contact_get_capabilities (EmpathyContact *contact)
729 {
730   EmpathyContactPriv *priv;
731
732   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
733
734   priv = GET_PRIV (contact);
735
736   return priv->capabilities;
737 }
738
739 void
740 empathy_contact_set_capabilities (EmpathyContact *contact,
741                                   EmpathyCapabilities capabilities)
742 {
743   EmpathyContactPriv *priv;
744
745   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
746
747   priv = GET_PRIV (contact);
748
749   if (priv->capabilities == capabilities)
750     return;
751
752   priv->capabilities = capabilities;
753
754   g_object_notify (G_OBJECT (contact), "capabilities");
755 }
756
757 gboolean
758 empathy_contact_is_user (EmpathyContact *contact)
759 {
760   EmpathyContactPriv *priv;
761
762   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
763
764   priv = GET_PRIV (contact);
765
766   return priv->is_user;
767 }
768
769 void
770 empathy_contact_set_is_user (EmpathyContact *contact,
771                              gboolean is_user)
772 {
773   EmpathyContactPriv *priv;
774
775   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
776
777   priv = GET_PRIV (contact);
778
779   if (priv->is_user == is_user)
780     return;
781
782   priv->is_user = is_user;
783
784   g_object_notify (G_OBJECT (contact), "is-user");
785 }
786
787 gboolean
788 empathy_contact_is_online (EmpathyContact *contact)
789 {
790   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
791
792   return (empathy_contact_get_presence (contact) > MC_PRESENCE_OFFLINE);
793 }
794
795 const gchar *
796 empathy_contact_get_status (EmpathyContact *contact)
797 {
798   const gchar *message;
799
800   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
801
802   message = empathy_contact_get_presence_message (contact);
803   if (!EMP_STR_EMPTY (message))
804     return message;
805
806   return empathy_presence_get_default_message (
807       empathy_contact_get_presence (contact));
808 }
809
810 gboolean
811 empathy_contact_can_voip (EmpathyContact *contact)
812 {
813   EmpathyContactPriv *priv;
814
815   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
816
817   priv = GET_PRIV (contact);
818
819   return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
820       EMPATHY_CAPABILITIES_VIDEO);
821 }
822
823 gboolean
824 empathy_contact_can_send_files (EmpathyContact *contact)
825 {
826   EmpathyContactPriv *priv;
827
828   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
829
830   priv = GET_PRIV (contact);
831
832   return priv->capabilities & EMPATHY_CAPABILITIES_FT;
833 }
834
835 static gchar *
836 contact_get_avatar_filename (EmpathyContact *contact,
837                              const gchar *token)
838 {
839   EmpathyContactPriv *priv = GET_PRIV (contact);
840   McAccount *account;
841   gchar *avatar_path;
842   gchar *avatar_file;
843   gchar *token_escaped;
844   gchar *contact_escaped;
845
846   if (EMP_STR_EMPTY (priv->id))
847     return NULL;
848
849   contact_escaped = tp_escape_as_identifier (priv->id);
850   token_escaped = tp_escape_as_identifier (token);
851   account = empathy_contact_get_account (contact);
852
853   /* FIXME: Do not use the account, but proto/cm instead */
854   avatar_path = g_build_filename (g_get_user_cache_dir (),
855       PACKAGE_NAME,
856       "avatars",
857       mc_account_get_unique_name (account),
858       contact_escaped,
859       NULL);
860   g_mkdir_with_parents (avatar_path, 0700);
861
862   avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
863
864   g_free (contact_escaped);
865   g_free (token_escaped);
866   g_free (avatar_path);
867
868   return avatar_file;
869 }
870
871 void
872 empathy_contact_load_avatar_data (EmpathyContact *contact,
873                                   const guchar  *data,
874                                   const gsize len,
875                                   const gchar *format,
876                                   const gchar *token)
877 {
878   EmpathyAvatar *avatar;
879   gchar *filename;
880   GError *error = NULL;
881
882   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
883   g_return_if_fail (data != NULL);
884   g_return_if_fail (len > 0);
885   g_return_if_fail (format != NULL);
886   g_return_if_fail (!EMP_STR_EMPTY (token));
887
888   /* Load and set the avatar */
889   avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
890       g_strdup (token));
891   empathy_contact_set_avatar (contact, avatar);
892   empathy_avatar_unref (avatar);
893
894   /* Save to cache if not yet in it */
895   filename = contact_get_avatar_filename (contact, token);
896   if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
897     {
898       if (!empathy_avatar_save_to_file (avatar, filename, &error))
899         {
900           DEBUG ("Failed to save avatar in cache: %s",
901             error ? error->message : "No error given");
902           g_clear_error (&error);
903         }
904       else
905           DEBUG ("Avatar saved to %s", filename);
906     }
907   g_free (filename);
908 }
909
910 gboolean
911 empathy_contact_load_avatar_cache (EmpathyContact *contact,
912                                    const gchar *token)
913 {
914   EmpathyAvatar *avatar = NULL;
915   gchar *filename;
916   gchar *data = NULL;
917   gsize len;
918   GError *error = NULL;
919
920   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
921   g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
922
923   /* Load the avatar from file if it exists */
924   filename = contact_get_avatar_filename (contact, token);
925   if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
926     {
927       if (!g_file_get_contents (filename, &data, &len, &error))
928         {
929           DEBUG ("Failed to load avatar from cache: %s",
930             error ? error->message : "No error given");
931           g_clear_error (&error);
932         }
933     }
934
935   if (data)
936     {
937       DEBUG ("Avatar loaded from %s", filename);
938       avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
939       empathy_contact_set_avatar (contact, avatar);
940       empathy_avatar_unref (avatar);
941     }
942
943   g_free (filename);
944
945   return data != NULL;
946 }
947
948 GType
949 empathy_avatar_get_type (void)
950 {
951   static GType type_id = 0;
952
953   if (!type_id)
954     {
955       type_id = g_boxed_type_register_static ("EmpathyAvatar",
956           (GBoxedCopyFunc) empathy_avatar_ref,
957           (GBoxedFreeFunc) empathy_avatar_unref);
958     }
959
960   return type_id;
961 }
962
963 EmpathyAvatar *
964 empathy_avatar_new (guchar *data,
965                     gsize len,
966                     gchar *format,
967                     gchar *token)
968 {
969   EmpathyAvatar *avatar;
970
971   avatar = g_slice_new0 (EmpathyAvatar);
972   avatar->data = data;
973   avatar->len = len;
974   avatar->format = format;
975   avatar->token = token;
976   avatar->refcount = 1;
977
978   return avatar;
979 }
980
981 void
982 empathy_avatar_unref (EmpathyAvatar *avatar)
983 {
984   g_return_if_fail (avatar != NULL);
985
986   avatar->refcount--;
987   if (avatar->refcount == 0)
988     {
989       g_free (avatar->data);
990       g_free (avatar->format);
991       g_free (avatar->token);
992       g_slice_free (EmpathyAvatar, avatar);
993     }
994 }
995
996 EmpathyAvatar *
997 empathy_avatar_ref (EmpathyAvatar *avatar)
998 {
999   g_return_val_if_fail (avatar != NULL, NULL);
1000
1001   avatar->refcount++;
1002
1003   return avatar;
1004 }
1005
1006 /**
1007  * empathy_avatar_save_to_file:
1008  * @avatar: the avatar
1009  * @filename: name of a file to write avatar to
1010  * @error: return location for a GError, or NULL
1011  *
1012  * Save the avatar to a file named filename
1013  *
1014  * Returns: %TRUE on success, %FALSE if an error occurred
1015  */
1016 gboolean
1017 empathy_avatar_save_to_file (EmpathyAvatar *self,
1018                              const gchar *filename,
1019                              GError **error)
1020 {
1021   return g_file_set_contents (filename, self->data, self->len, error);
1022 }
1023
1024 /**
1025  * empathy_contact_get_location:
1026  * @contact: an #EmpathyContact
1027  *
1028  * Returns the user's location if available.  The keys are defined in
1029  * empathy-location.h. If the contact doesn't have location
1030  * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1031  * you are done with the #GHashTable.
1032  *
1033  * It is composed of string keys and GValues.  Keys are
1034  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1035  * Example: a "city" key would have "Helsinki" as string GValue,
1036  *          a "latitude" would have 65.0 as double GValue.
1037  *
1038  * Returns: a #GHashTable of location values
1039  */
1040 GHashTable *
1041 empathy_contact_get_location (EmpathyContact *contact)
1042 {
1043   EmpathyContactPriv *priv;
1044
1045   g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1046
1047   priv = GET_PRIV (contact);
1048
1049   return priv->location;
1050 }
1051
1052 /**
1053  * empathy_contact_set_location:
1054  * @contact: an #EmpathyContact
1055  * @location: a #GHashTable of the location
1056  *
1057  * Sets the user's location based on the location #GHashTable passed.
1058  * It is composed of string keys and GValues.  Keys are
1059  * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1060  * Example: a "city" key would have "Helsinki" as string GValue,
1061  *          a "latitude" would have 65.0 as double GValue.
1062  */
1063 void
1064 empathy_contact_set_location (EmpathyContact *contact,
1065                               GHashTable *location)
1066 {
1067   EmpathyContactPriv *priv;
1068
1069   g_return_if_fail (EMPATHY_CONTACT (contact));
1070   g_return_if_fail (location != NULL);
1071
1072   priv = GET_PRIV (contact);
1073
1074   if (priv->location != NULL)
1075     g_hash_table_unref (priv->location);
1076
1077   priv->location = g_hash_table_ref (location);
1078   g_object_notify (G_OBJECT (contact), "location");
1079 }