1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/account-manager.h>
29 #include <telepathy-glib/util.h>
31 #include <telepathy-logger/contact.h>
32 #endif /* ENABLE_TPL */
35 #include <geoclue/geoclue-geocode.h>
38 #include "empathy-contact.h"
39 #include "empathy-utils.h"
40 #include "empathy-enum-types.h"
41 #include "empathy-marshal.h"
42 #include "empathy-location.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
45 #include "empathy-debug.h"
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
49 TpContact *tp_contact;
53 EmpathyAvatar *avatar;
54 TpConnectionPresenceType presence;
55 gchar *presence_message;
57 EmpathyCapabilities capabilities;
60 /* Location is composed of string keys and GValues.
61 * Example: a "city" key would have "Helsinki" as string GValue,
62 * a "latitude" would have 65.0 as double GValue.
67 static void contact_finalize (GObject *object);
68 static void contact_get_property (GObject *object, guint param_id,
69 GValue *value, GParamSpec *pspec);
70 static void contact_set_property (GObject *object, guint param_id,
71 const GValue *value, GParamSpec *pspec);
73 static void update_geocode (EmpathyContact *contact);
75 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
86 PROP_PRESENCE_MESSAGE,
98 static guint signals[LAST_SIGNAL];
101 tp_contact_notify_cb (TpContact *tp_contact,
105 EmpathyContactPriv *priv = GET_PRIV (contact);
107 /* Forward property notifications */
108 if (!tp_strdiff (param->name, "alias"))
109 g_object_notify (contact, "name");
110 else if (!tp_strdiff (param->name, "presence-type")) {
111 TpConnectionPresenceType presence;
113 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
114 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
116 priv->presence = presence;
117 g_object_notify (contact, "presence");
119 else if (!tp_strdiff (param->name, "presence-message"))
120 g_object_notify (contact, "presence-message");
121 else if (!tp_strdiff (param->name, "identifier"))
122 g_object_notify (contact, "id");
123 else if (!tp_strdiff (param->name, "handle"))
124 g_object_notify (contact, "handle");
128 contact_dispose (GObject *object)
130 EmpathyContactPriv *priv = GET_PRIV (object);
132 if (priv->tp_contact)
134 g_signal_handlers_disconnect_by_func (priv->tp_contact,
135 tp_contact_notify_cb, object);
136 g_object_unref (priv->tp_contact);
138 priv->tp_contact = NULL;
141 g_object_unref (priv->account);
142 priv->account = NULL;
144 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
148 empathy_contact_class_init (EmpathyContactClass *class)
150 GObjectClass *object_class;
152 object_class = G_OBJECT_CLASS (class);
154 object_class->finalize = contact_finalize;
155 object_class->dispose = contact_dispose;
156 object_class->get_property = contact_get_property;
157 object_class->set_property = contact_set_property;
159 g_object_class_install_property (object_class,
161 g_param_spec_object ("tp-contact",
163 "The TpContact associated with the contact",
165 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 g_object_class_install_property (object_class,
169 g_param_spec_object ("account",
171 "The account associated with the contact",
173 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (object_class,
177 g_param_spec_string ("id",
179 "String identifying contact",
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (object_class,
185 g_param_spec_string ("name",
187 "The name of the contact",
189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (object_class,
193 g_param_spec_boxed ("avatar",
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (object_class,
201 g_param_spec_uint ("presence",
203 "Presence of contact",
204 TP_CONNECTION_PRESENCE_TYPE_UNSET,
205 NUM_TP_CONNECTION_PRESENCE_TYPES,
206 TP_CONNECTION_PRESENCE_TYPE_UNSET,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (object_class,
210 PROP_PRESENCE_MESSAGE,
211 g_param_spec_string ("presence-message",
212 "Contact presence message",
213 "Presence message of contact",
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217 g_object_class_install_property (object_class,
219 g_param_spec_uint ("handle",
221 "The handle of the contact",
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (object_class,
229 g_param_spec_flags ("capabilities",
230 "Contact Capabilities",
231 "Capabilities of the contact",
232 EMPATHY_TYPE_CAPABILITIES,
233 EMPATHY_CAPABILITIES_UNKNOWN,
234 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (object_class,
238 g_param_spec_boolean ("is-user",
240 "Is contact the user",
242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 g_object_class_install_property (object_class,
247 g_param_spec_boxed ("location",
249 "Physical location of the contact",
251 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
253 signals[PRESENCE_CHANGED] =
254 g_signal_new ("presence-changed",
255 G_TYPE_FROM_CLASS (class),
259 _empathy_marshal_VOID__UINT_UINT,
264 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
268 empathy_contact_init (EmpathyContact *contact)
270 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
271 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
273 contact->priv = priv;
275 priv->location = NULL;
279 contact_finalize (GObject *object)
281 EmpathyContactPriv *priv;
283 priv = GET_PRIV (object);
285 DEBUG ("finalize: %p", object);
289 g_free (priv->presence_message);
292 empathy_avatar_unref (priv->avatar);
294 if (priv->location != NULL)
295 g_hash_table_unref (priv->location);
297 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
301 set_tp_contact (EmpathyContact *contact,
302 TpContact *tp_contact)
304 EmpathyContactPriv *priv = GET_PRIV (contact);
306 if (tp_contact == NULL)
309 g_assert (priv->tp_contact == NULL);
310 priv->tp_contact = g_object_ref (tp_contact);
311 priv->presence = empathy_contact_get_presence (contact);
313 g_signal_connect (priv->tp_contact, "notify",
314 G_CALLBACK (tp_contact_notify_cb), contact);
318 contact_get_property (GObject *object,
323 EmpathyContact *contact = EMPATHY_CONTACT (object);
327 case PROP_TP_CONTACT:
328 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
331 g_value_set_object (value, empathy_contact_get_account (contact));
334 g_value_set_string (value, empathy_contact_get_id (contact));
337 g_value_set_string (value, empathy_contact_get_name (contact));
340 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
343 g_value_set_uint (value, empathy_contact_get_presence (contact));
345 case PROP_PRESENCE_MESSAGE:
346 g_value_set_string (value, empathy_contact_get_presence_message (contact));
349 g_value_set_uint (value, empathy_contact_get_handle (contact));
351 case PROP_CAPABILITIES:
352 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
355 g_value_set_boolean (value, empathy_contact_is_user (contact));
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
364 contact_set_property (GObject *object,
369 EmpathyContact *contact = EMPATHY_CONTACT (object);
370 EmpathyContactPriv *priv = GET_PRIV (object);
374 case PROP_TP_CONTACT:
375 set_tp_contact (contact, g_value_get_object (value));
378 g_assert (priv->account == NULL);
379 priv->account = g_value_dup_object (value);
382 empathy_contact_set_id (contact, g_value_get_string (value));
385 empathy_contact_set_name (contact, g_value_get_string (value));
388 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
391 empathy_contact_set_presence (contact, g_value_get_uint (value));
393 case PROP_PRESENCE_MESSAGE:
394 empathy_contact_set_presence_message (contact, g_value_get_string (value));
397 empathy_contact_set_handle (contact, g_value_get_uint (value));
399 case PROP_CAPABILITIES:
400 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
403 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
412 empathy_contact_new (TpContact *tp_contact)
414 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
416 return g_object_new (EMPATHY_TYPE_CONTACT,
417 "tp-contact", tp_contact,
423 empathy_contact_from_tpl_contact (TpAccount *account,
424 TplContact *tpl_contact)
426 EmpathyContact *retval;
429 g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
431 is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
433 retval = g_object_new (EMPATHY_TYPE_CONTACT,
434 "id", tpl_contact_get_alias (tpl_contact),
435 "name", tpl_contact_get_identifier (tpl_contact),
440 if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
441 empathy_contact_load_avatar_cache (retval,
442 tpl_contact_get_avatar_token (tpl_contact));
446 #endif /* ENABLE_TPL */
449 empathy_contact_new_for_log (TpAccount *account,
454 g_return_val_if_fail (id != NULL, NULL);
455 g_assert (account != NULL);
457 return g_object_new (EMPATHY_TYPE_CONTACT,
466 empathy_contact_get_tp_contact (EmpathyContact *contact)
468 EmpathyContactPriv *priv;
470 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
472 priv = GET_PRIV (contact);
474 return priv->tp_contact;
478 empathy_contact_get_id (EmpathyContact *contact)
480 EmpathyContactPriv *priv;
482 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
484 priv = GET_PRIV (contact);
486 if (priv->tp_contact != NULL)
487 return tp_contact_get_identifier (priv->tp_contact);
493 empathy_contact_set_id (EmpathyContact *contact,
496 EmpathyContactPriv *priv;
498 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
499 g_return_if_fail (id != NULL);
501 priv = GET_PRIV (contact);
503 /* We temporally ref the contact because it could be destroyed
504 * during the signal emition */
505 g_object_ref (contact);
506 if (tp_strdiff (id, priv->id))
509 priv->id = g_strdup (id);
511 g_object_notify (G_OBJECT (contact), "id");
512 if (EMP_STR_EMPTY (priv->name))
513 g_object_notify (G_OBJECT (contact), "name");
516 g_object_unref (contact);
520 empathy_contact_get_name (EmpathyContact *contact)
522 EmpathyContactPriv *priv;
525 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
527 priv = GET_PRIV (contact);
529 if (priv->tp_contact != NULL)
530 alias = tp_contact_get_alias (priv->tp_contact);
534 if (!EMP_STR_EMPTY (alias))
537 return empathy_contact_get_id (contact);
541 empathy_contact_set_name (EmpathyContact *contact,
544 EmpathyContactPriv *priv;
546 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
548 priv = GET_PRIV (contact);
550 g_object_ref (contact);
551 if (tp_strdiff (name, priv->name))
554 priv->name = g_strdup (name);
555 g_object_notify (G_OBJECT (contact), "name");
557 g_object_unref (contact);
561 empathy_contact_get_avatar (EmpathyContact *contact)
563 EmpathyContactPriv *priv;
565 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
567 priv = GET_PRIV (contact);
573 empathy_contact_set_avatar (EmpathyContact *contact,
574 EmpathyAvatar *avatar)
576 EmpathyContactPriv *priv;
578 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
580 priv = GET_PRIV (contact);
582 if (priv->avatar == avatar)
587 empathy_avatar_unref (priv->avatar);
592 priv->avatar = empathy_avatar_ref (avatar);
594 g_object_notify (G_OBJECT (contact), "avatar");
598 empathy_contact_get_account (EmpathyContact *contact)
600 EmpathyContactPriv *priv;
602 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
604 priv = GET_PRIV (contact);
606 if (priv->account == NULL && priv->tp_contact != NULL)
608 TpConnection *connection;
610 /* FIXME: This assume the account manager already exists */
611 connection = tp_contact_get_connection (priv->tp_contact);
613 g_object_ref (empathy_get_account_for_connection (connection));
616 return priv->account;
620 empathy_contact_get_connection (EmpathyContact *contact)
622 EmpathyContactPriv *priv;
624 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
626 priv = GET_PRIV (contact);
628 if (priv->tp_contact != NULL)
629 return tp_contact_get_connection (priv->tp_contact);
634 TpConnectionPresenceType
635 empathy_contact_get_presence (EmpathyContact *contact)
637 EmpathyContactPriv *priv;
639 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
640 TP_CONNECTION_PRESENCE_TYPE_UNSET);
642 priv = GET_PRIV (contact);
644 if (priv->tp_contact != NULL)
645 return tp_contact_get_presence_type (priv->tp_contact);
647 return priv->presence;
651 empathy_contact_set_presence (EmpathyContact *contact,
652 TpConnectionPresenceType presence)
654 EmpathyContactPriv *priv;
655 TpConnectionPresenceType old_presence;
657 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
659 priv = GET_PRIV (contact);
661 if (presence == priv->presence)
664 old_presence = priv->presence;
665 priv->presence = presence;
667 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
669 g_object_notify (G_OBJECT (contact), "presence");
673 empathy_contact_get_presence_message (EmpathyContact *contact)
675 EmpathyContactPriv *priv;
677 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
679 priv = GET_PRIV (contact);
681 if (priv->tp_contact != NULL)
682 return tp_contact_get_presence_message (priv->tp_contact);
684 return priv->presence_message;
688 empathy_contact_set_presence_message (EmpathyContact *contact,
689 const gchar *message)
691 EmpathyContactPriv *priv = GET_PRIV (contact);
693 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
695 if (!tp_strdiff (message, priv->presence_message))
698 g_free (priv->presence_message);
699 priv->presence_message = g_strdup (message);
701 g_object_notify (G_OBJECT (contact), "presence-message");
705 empathy_contact_get_handle (EmpathyContact *contact)
707 EmpathyContactPriv *priv;
709 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
711 priv = GET_PRIV (contact);
713 if (priv->tp_contact != NULL)
714 return tp_contact_get_handle (priv->tp_contact);
720 empathy_contact_set_handle (EmpathyContact *contact,
723 EmpathyContactPriv *priv;
725 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
727 priv = GET_PRIV (contact);
729 g_object_ref (contact);
730 if (handle != priv->handle)
732 priv->handle = handle;
733 g_object_notify (G_OBJECT (contact), "handle");
735 g_object_unref (contact);
739 empathy_contact_get_capabilities (EmpathyContact *contact)
741 EmpathyContactPriv *priv;
743 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
745 priv = GET_PRIV (contact);
747 return priv->capabilities;
751 empathy_contact_set_capabilities (EmpathyContact *contact,
752 EmpathyCapabilities capabilities)
754 EmpathyContactPriv *priv;
756 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
758 priv = GET_PRIV (contact);
760 if (priv->capabilities == capabilities)
763 priv->capabilities = capabilities;
765 g_object_notify (G_OBJECT (contact), "capabilities");
769 empathy_contact_is_user (EmpathyContact *contact)
771 EmpathyContactPriv *priv;
773 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
775 priv = GET_PRIV (contact);
777 return priv->is_user;
781 empathy_contact_set_is_user (EmpathyContact *contact,
784 EmpathyContactPriv *priv;
786 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
788 priv = GET_PRIV (contact);
790 if (priv->is_user == is_user)
793 priv->is_user = is_user;
795 g_object_notify (G_OBJECT (contact), "is-user");
799 empathy_contact_is_online (EmpathyContact *contact)
801 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
803 switch (empathy_contact_get_presence (contact))
805 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
806 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
807 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
815 empathy_contact_get_status (EmpathyContact *contact)
817 const gchar *message;
819 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
821 message = empathy_contact_get_presence_message (contact);
822 if (!EMP_STR_EMPTY (message))
825 return empathy_presence_get_default_message (
826 empathy_contact_get_presence (contact));
830 empathy_contact_can_voip (EmpathyContact *contact)
832 EmpathyContactPriv *priv;
834 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
836 priv = GET_PRIV (contact);
838 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
839 EMPATHY_CAPABILITIES_VIDEO);
843 empathy_contact_can_voip_audio (EmpathyContact *contact)
845 EmpathyContactPriv *priv;
847 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
849 priv = GET_PRIV (contact);
851 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
855 empathy_contact_can_voip_video (EmpathyContact *contact)
857 EmpathyContactPriv *priv;
859 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
861 priv = GET_PRIV (contact);
863 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
867 empathy_contact_can_send_files (EmpathyContact *contact)
869 EmpathyContactPriv *priv;
871 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
873 priv = GET_PRIV (contact);
875 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
879 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
881 EmpathyContactPriv *priv;
883 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
885 priv = GET_PRIV (contact);
887 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
891 contact_get_avatar_filename (EmpathyContact *contact,
897 gchar *token_escaped;
899 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
902 token_escaped = tp_escape_as_identifier (token);
903 account = empathy_contact_get_account (contact);
905 avatar_path = g_build_filename (g_get_user_cache_dir (),
908 tp_account_get_connection_manager (account),
909 tp_account_get_protocol (account),
911 g_mkdir_with_parents (avatar_path, 0700);
913 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
915 g_free (token_escaped);
916 g_free (avatar_path);
922 empathy_contact_load_avatar_data (EmpathyContact *contact,
928 EmpathyAvatar *avatar;
930 GError *error = NULL;
932 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
933 g_return_if_fail (data != NULL);
934 g_return_if_fail (len > 0);
935 g_return_if_fail (format != NULL);
936 g_return_if_fail (!EMP_STR_EMPTY (token));
938 /* Load and set the avatar */
939 filename = contact_get_avatar_filename (contact, token);
940 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
941 g_strdup (token), filename);
942 empathy_contact_set_avatar (contact, avatar);
943 empathy_avatar_unref (avatar);
945 /* Save to cache if not yet in it */
946 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
948 if (!empathy_avatar_save_to_file (avatar, filename, &error))
950 DEBUG ("Failed to save avatar in cache: %s",
951 error ? error->message : "No error given");
952 g_clear_error (&error);
955 DEBUG ("Avatar saved to %s", filename);
960 empathy_contact_load_avatar_cache (EmpathyContact *contact,
963 EmpathyAvatar *avatar = NULL;
967 GError *error = NULL;
969 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
970 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
972 /* Load the avatar from file if it exists */
973 filename = contact_get_avatar_filename (contact, token);
974 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
976 if (!g_file_get_contents (filename, &data, &len, &error))
978 DEBUG ("Failed to load avatar from cache: %s",
979 error ? error->message : "No error given");
980 g_clear_error (&error);
986 DEBUG ("Avatar loaded from %s", filename);
987 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
989 empathy_contact_set_avatar (contact, avatar);
990 empathy_avatar_unref (avatar);
1001 empathy_avatar_get_type (void)
1003 static GType type_id = 0;
1007 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1008 (GBoxedCopyFunc) empathy_avatar_ref,
1009 (GBoxedFreeFunc) empathy_avatar_unref);
1016 * empathy_avatar_new:
1017 * @data: the avatar data
1018 * @len: the size of avatar data
1019 * @format: the mime type of the avatar image
1020 * @token: the token of the avatar
1021 * @filename: the filename where the avatar is stored in cache
1023 * Create a #EmpathyAvatar from the provided data. This function takes the
1024 * ownership of @data, @format, @token and @filename.
1026 * Returns: a new #EmpathyAvatar
1029 empathy_avatar_new (guchar *data,
1035 EmpathyAvatar *avatar;
1037 avatar = g_slice_new0 (EmpathyAvatar);
1038 avatar->data = data;
1040 avatar->format = format;
1041 avatar->token = token;
1042 avatar->filename = filename;
1043 avatar->refcount = 1;
1049 empathy_avatar_unref (EmpathyAvatar *avatar)
1051 g_return_if_fail (avatar != NULL);
1054 if (avatar->refcount == 0)
1056 g_free (avatar->data);
1057 g_free (avatar->format);
1058 g_free (avatar->token);
1059 g_slice_free (EmpathyAvatar, avatar);
1064 empathy_avatar_ref (EmpathyAvatar *avatar)
1066 g_return_val_if_fail (avatar != NULL, NULL);
1074 * empathy_avatar_save_to_file:
1075 * @avatar: the avatar
1076 * @filename: name of a file to write avatar to
1077 * @error: return location for a GError, or NULL
1079 * Save the avatar to a file named filename
1081 * Returns: %TRUE on success, %FALSE if an error occurred
1084 empathy_avatar_save_to_file (EmpathyAvatar *self,
1085 const gchar *filename,
1088 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1093 * empathy_contact_get_location:
1094 * @contact: an #EmpathyContact
1096 * Returns the user's location if available. The keys are defined in
1097 * empathy-location.h. If the contact doesn't have location
1098 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1099 * you are done with the #GHashTable.
1101 * It is composed of string keys and GValues. Keys are
1102 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1103 * Example: a "city" key would have "Helsinki" as string GValue,
1104 * a "latitude" would have 65.0 as double GValue.
1106 * Returns: a #GHashTable of location values
1109 empathy_contact_get_location (EmpathyContact *contact)
1111 EmpathyContactPriv *priv;
1113 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1115 priv = GET_PRIV (contact);
1117 return priv->location;
1121 * empathy_contact_set_location:
1122 * @contact: an #EmpathyContact
1123 * @location: a #GHashTable of the location
1125 * Sets the user's location based on the location #GHashTable passed.
1126 * It is composed of string keys and GValues. Keys are
1127 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1128 * Example: a "city" key would have "Helsinki" as string GValue,
1129 * a "latitude" would have 65.0 as double GValue.
1132 empathy_contact_set_location (EmpathyContact *contact,
1133 GHashTable *location)
1135 EmpathyContactPriv *priv;
1137 g_return_if_fail (EMPATHY_CONTACT (contact));
1138 g_return_if_fail (location != NULL);
1140 priv = GET_PRIV (contact);
1142 if (priv->location != NULL)
1143 g_hash_table_unref (priv->location);
1145 priv->location = g_hash_table_ref (location);
1146 update_geocode (contact);
1147 g_object_notify (G_OBJECT (contact), "location");
1151 * empathy_contact_equal:
1152 * @contact1: an #EmpathyContact
1153 * @contact2: an #EmpathyContact
1155 * Returns FALSE if one of the contacts is NULL but the other is not.
1156 * Otherwise returns TRUE if both pointer are equal or if they bith
1157 * refer to the same id.
1158 * It's only necessary to call this function if your contact objects
1159 * come from logs where contacts are created dynamically and comparing
1160 * pointers is not enough.
1163 empathy_contact_equal (gconstpointer contact1,
1164 gconstpointer contact2)
1171 if ((contact1 == NULL) != (contact2 == NULL)) {
1174 if (contact1 == contact2) {
1177 c1 = EMPATHY_CONTACT (contact1);
1178 c2 = EMPATHY_CONTACT (contact2);
1179 id1 = empathy_contact_get_id (c1);
1180 id2 = empathy_contact_get_id (c2);
1181 if (!tp_strdiff (id1, id2)) {
1188 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1189 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1191 /* This callback is called by geoclue when it found a position
1192 * for the given address. A position is necessary for a contact
1193 * to show up on the map
1196 geocode_cb (GeoclueGeocode *geocode,
1197 GeocluePositionFields fields,
1201 GeoclueAccuracy *accuracy,
1206 GHashTable *location;
1208 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
1210 if (error != NULL) {
1211 DEBUG ("Error geocoding location : %s", error->message);
1212 g_object_unref (geocode);
1213 g_object_unref (contact);
1217 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
1218 new_value = tp_g_value_slice_new_double (latitude);
1219 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
1221 DEBUG ("\t - Latitude: %f", latitude);
1223 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
1224 new_value = tp_g_value_slice_new_double (longitude);
1225 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
1227 DEBUG ("\t - Longitude: %f", longitude);
1229 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
1230 new_value = tp_g_value_slice_new_double (altitude);
1231 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
1233 DEBUG ("\t - Altitude: %f", altitude);
1236 /* Don't change the accuracy as we used an address to get this position */
1237 g_object_notify (contact, "location");
1238 g_object_unref (geocode);
1239 g_object_unref (contact);
1245 get_dup_string (GHashTable *location,
1250 value = g_hash_table_lookup (location, key);
1252 return g_value_dup_string (value);
1259 update_geocode (EmpathyContact *contact)
1262 static GeoclueGeocode *geocode;
1264 GHashTable *address;
1266 GHashTable *location;
1268 location = empathy_contact_get_location (contact);
1269 if (location == NULL)
1272 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
1276 if (geocode == NULL) {
1277 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1278 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1281 g_object_ref (geocode);
1283 address = geoclue_address_details_new ();
1285 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1287 g_hash_table_insert (address,
1288 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1289 DEBUG ("\t - countrycode: %s", str);
1292 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1294 g_hash_table_insert (address,
1295 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1296 DEBUG ("\t - country: %s", str);
1299 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1301 g_hash_table_insert (address,
1302 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1303 DEBUG ("\t - postalcode: %s", str);
1306 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1308 g_hash_table_insert (address,
1309 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1310 DEBUG ("\t - region: %s", str);
1313 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1315 g_hash_table_insert (address,
1316 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1317 DEBUG ("\t - locality: %s", str);
1320 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1322 g_hash_table_insert (address,
1323 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1324 DEBUG ("\t - street: %s", str);
1327 g_object_ref (contact);
1328 geoclue_geocode_address_to_position_async (geocode, address,
1329 geocode_cb, contact);
1331 g_hash_table_unref (address);