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.
64 * This is a super set of the location stored in TpContact as we can try add
65 * more fields by searching the address using geoclue.
70 static void contact_finalize (GObject *object);
71 static void contact_get_property (GObject *object, guint param_id,
72 GValue *value, GParamSpec *pspec);
73 static void contact_set_property (GObject *object, guint param_id,
74 const GValue *value, GParamSpec *pspec);
77 static void update_geocode (EmpathyContact *contact);
80 static void empathy_contact_set_location (EmpathyContact *contact,
81 GHashTable *location);
83 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
94 PROP_PRESENCE_MESSAGE,
106 static guint signals[LAST_SIGNAL];
109 tp_contact_notify_cb (TpContact *tp_contact,
113 EmpathyContactPriv *priv = GET_PRIV (contact);
115 /* Forward property notifications */
116 if (!tp_strdiff (param->name, "alias"))
117 g_object_notify (contact, "name");
118 else if (!tp_strdiff (param->name, "presence-type")) {
119 TpConnectionPresenceType presence;
121 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
122 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
124 priv->presence = presence;
125 g_object_notify (contact, "presence");
127 else if (!tp_strdiff (param->name, "presence-message"))
128 g_object_notify (contact, "presence-message");
129 else if (!tp_strdiff (param->name, "identifier"))
130 g_object_notify (contact, "id");
131 else if (!tp_strdiff (param->name, "handle"))
132 g_object_notify (contact, "handle");
133 else if (!tp_strdiff (param->name, "location"))
135 GHashTable *location;
137 location = tp_contact_get_location (tp_contact);
138 /* This will start a geoclue search to find the address if needed */
139 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
144 contact_dispose (GObject *object)
146 EmpathyContactPriv *priv = GET_PRIV (object);
148 if (priv->tp_contact)
150 g_signal_handlers_disconnect_by_func (priv->tp_contact,
151 tp_contact_notify_cb, object);
152 g_object_unref (priv->tp_contact);
154 priv->tp_contact = NULL;
157 g_object_unref (priv->account);
158 priv->account = NULL;
160 if (priv->avatar != NULL)
162 empathy_avatar_unref (priv->avatar);
166 if (priv->location != NULL)
168 g_hash_table_unref (priv->location);
169 priv->location = NULL;
172 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
176 empathy_contact_class_init (EmpathyContactClass *class)
178 GObjectClass *object_class;
180 object_class = G_OBJECT_CLASS (class);
182 object_class->finalize = contact_finalize;
183 object_class->dispose = contact_dispose;
184 object_class->get_property = contact_get_property;
185 object_class->set_property = contact_set_property;
187 g_object_class_install_property (object_class,
189 g_param_spec_object ("tp-contact",
191 "The TpContact associated with the contact",
193 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (object_class,
197 g_param_spec_object ("account",
199 "The account associated with the contact",
201 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (object_class,
205 g_param_spec_string ("id",
207 "String identifying contact",
209 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (object_class,
213 g_param_spec_string ("name",
215 "The name of the contact",
217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (object_class,
221 g_param_spec_boxed ("avatar",
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (object_class,
229 g_param_spec_uint ("presence",
231 "Presence of contact",
232 TP_CONNECTION_PRESENCE_TYPE_UNSET,
233 NUM_TP_CONNECTION_PRESENCE_TYPES,
234 TP_CONNECTION_PRESENCE_TYPE_UNSET,
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (object_class,
238 PROP_PRESENCE_MESSAGE,
239 g_param_spec_string ("presence-message",
240 "Contact presence message",
241 "Presence message of contact",
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 g_object_class_install_property (object_class,
247 g_param_spec_uint ("handle",
249 "The handle of the contact",
253 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (object_class,
257 g_param_spec_flags ("capabilities",
258 "Contact Capabilities",
259 "Capabilities of the contact",
260 EMPATHY_TYPE_CAPABILITIES,
261 EMPATHY_CAPABILITIES_UNKNOWN,
262 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (object_class,
266 g_param_spec_boolean ("is-user",
268 "Is contact the user",
270 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (object_class,
275 g_param_spec_boxed ("location",
277 "Physical location of the contact",
279 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
281 signals[PRESENCE_CHANGED] =
282 g_signal_new ("presence-changed",
283 G_TYPE_FROM_CLASS (class),
287 _empathy_marshal_VOID__UINT_UINT,
292 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
296 empathy_contact_init (EmpathyContact *contact)
298 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
299 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
301 contact->priv = priv;
303 priv->location = NULL;
307 contact_finalize (GObject *object)
309 EmpathyContactPriv *priv;
311 priv = GET_PRIV (object);
313 DEBUG ("finalize: %p", object);
317 g_free (priv->presence_message);
319 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
323 set_tp_contact (EmpathyContact *contact,
324 TpContact *tp_contact)
326 EmpathyContactPriv *priv = GET_PRIV (contact);
327 GHashTable *location;
329 if (tp_contact == NULL)
332 g_assert (priv->tp_contact == NULL);
333 priv->tp_contact = g_object_ref (tp_contact);
334 priv->presence = empathy_contact_get_presence (contact);
336 location = tp_contact_get_location (tp_contact);
337 if (location != NULL)
338 empathy_contact_set_location (contact, location);
340 g_signal_connect (priv->tp_contact, "notify",
341 G_CALLBACK (tp_contact_notify_cb), contact);
345 contact_get_property (GObject *object,
350 EmpathyContact *contact = EMPATHY_CONTACT (object);
354 case PROP_TP_CONTACT:
355 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
358 g_value_set_object (value, empathy_contact_get_account (contact));
361 g_value_set_string (value, empathy_contact_get_id (contact));
364 g_value_set_string (value, empathy_contact_get_name (contact));
367 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
370 g_value_set_uint (value, empathy_contact_get_presence (contact));
372 case PROP_PRESENCE_MESSAGE:
373 g_value_set_string (value, empathy_contact_get_presence_message (contact));
376 g_value_set_uint (value, empathy_contact_get_handle (contact));
378 case PROP_CAPABILITIES:
379 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
382 g_value_set_boolean (value, empathy_contact_is_user (contact));
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
391 contact_set_property (GObject *object,
396 EmpathyContact *contact = EMPATHY_CONTACT (object);
397 EmpathyContactPriv *priv = GET_PRIV (object);
401 case PROP_TP_CONTACT:
402 set_tp_contact (contact, g_value_get_object (value));
405 g_assert (priv->account == NULL);
406 priv->account = g_value_dup_object (value);
409 empathy_contact_set_id (contact, g_value_get_string (value));
412 empathy_contact_set_name (contact, g_value_get_string (value));
415 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
418 empathy_contact_set_presence (contact, g_value_get_uint (value));
420 case PROP_PRESENCE_MESSAGE:
421 empathy_contact_set_presence_message (contact, g_value_get_string (value));
424 empathy_contact_set_handle (contact, g_value_get_uint (value));
426 case PROP_CAPABILITIES:
427 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
430 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
433 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
439 empathy_contact_new (TpContact *tp_contact)
441 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
443 return g_object_new (EMPATHY_TYPE_CONTACT,
444 "tp-contact", tp_contact,
450 empathy_contact_from_tpl_contact (TpAccount *account,
451 TplContact *tpl_contact)
453 EmpathyContact *retval;
456 g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
458 is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
460 retval = g_object_new (EMPATHY_TYPE_CONTACT,
461 "id", tpl_contact_get_alias (tpl_contact),
462 "name", tpl_contact_get_identifier (tpl_contact),
467 if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
468 empathy_contact_load_avatar_cache (retval,
469 tpl_contact_get_avatar_token (tpl_contact));
473 #endif /* ENABLE_TPL */
476 empathy_contact_new_for_log (TpAccount *account,
481 g_return_val_if_fail (id != NULL, NULL);
482 g_assert (account != NULL);
484 return g_object_new (EMPATHY_TYPE_CONTACT,
493 empathy_contact_get_tp_contact (EmpathyContact *contact)
495 EmpathyContactPriv *priv;
497 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
499 priv = GET_PRIV (contact);
501 return priv->tp_contact;
505 empathy_contact_get_id (EmpathyContact *contact)
507 EmpathyContactPriv *priv;
509 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
511 priv = GET_PRIV (contact);
513 if (priv->tp_contact != NULL)
514 return tp_contact_get_identifier (priv->tp_contact);
520 empathy_contact_set_id (EmpathyContact *contact,
523 EmpathyContactPriv *priv;
525 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
526 g_return_if_fail (id != NULL);
528 priv = GET_PRIV (contact);
530 /* We temporally ref the contact because it could be destroyed
531 * during the signal emition */
532 g_object_ref (contact);
533 if (tp_strdiff (id, priv->id))
536 priv->id = g_strdup (id);
538 g_object_notify (G_OBJECT (contact), "id");
539 if (EMP_STR_EMPTY (priv->name))
540 g_object_notify (G_OBJECT (contact), "name");
543 g_object_unref (contact);
547 empathy_contact_get_name (EmpathyContact *contact)
549 EmpathyContactPriv *priv;
552 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
554 priv = GET_PRIV (contact);
556 if (priv->tp_contact != NULL)
557 alias = tp_contact_get_alias (priv->tp_contact);
561 if (!EMP_STR_EMPTY (alias))
564 return empathy_contact_get_id (contact);
568 empathy_contact_set_name (EmpathyContact *contact,
571 EmpathyContactPriv *priv;
573 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
575 priv = GET_PRIV (contact);
577 g_object_ref (contact);
578 if (tp_strdiff (name, priv->name))
581 priv->name = g_strdup (name);
582 g_object_notify (G_OBJECT (contact), "name");
584 g_object_unref (contact);
588 empathy_contact_get_avatar (EmpathyContact *contact)
590 EmpathyContactPriv *priv;
592 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
594 priv = GET_PRIV (contact);
600 empathy_contact_set_avatar (EmpathyContact *contact,
601 EmpathyAvatar *avatar)
603 EmpathyContactPriv *priv;
605 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
607 priv = GET_PRIV (contact);
609 if (priv->avatar == avatar)
614 empathy_avatar_unref (priv->avatar);
619 priv->avatar = empathy_avatar_ref (avatar);
621 g_object_notify (G_OBJECT (contact), "avatar");
625 empathy_contact_get_account (EmpathyContact *contact)
627 EmpathyContactPriv *priv;
629 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
631 priv = GET_PRIV (contact);
633 if (priv->account == NULL && priv->tp_contact != NULL)
635 TpConnection *connection;
637 /* FIXME: This assume the account manager already exists */
638 connection = tp_contact_get_connection (priv->tp_contact);
640 g_object_ref (empathy_get_account_for_connection (connection));
643 return priv->account;
647 empathy_contact_get_connection (EmpathyContact *contact)
649 EmpathyContactPriv *priv;
651 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
653 priv = GET_PRIV (contact);
655 if (priv->tp_contact != NULL)
656 return tp_contact_get_connection (priv->tp_contact);
661 TpConnectionPresenceType
662 empathy_contact_get_presence (EmpathyContact *contact)
664 EmpathyContactPriv *priv;
666 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
667 TP_CONNECTION_PRESENCE_TYPE_UNSET);
669 priv = GET_PRIV (contact);
671 if (priv->tp_contact != NULL)
672 return tp_contact_get_presence_type (priv->tp_contact);
674 return priv->presence;
678 empathy_contact_set_presence (EmpathyContact *contact,
679 TpConnectionPresenceType presence)
681 EmpathyContactPriv *priv;
682 TpConnectionPresenceType old_presence;
684 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
686 priv = GET_PRIV (contact);
688 if (presence == priv->presence)
691 old_presence = priv->presence;
692 priv->presence = presence;
694 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
696 g_object_notify (G_OBJECT (contact), "presence");
700 empathy_contact_get_presence_message (EmpathyContact *contact)
702 EmpathyContactPriv *priv;
704 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
706 priv = GET_PRIV (contact);
708 if (priv->tp_contact != NULL)
709 return tp_contact_get_presence_message (priv->tp_contact);
711 return priv->presence_message;
715 empathy_contact_set_presence_message (EmpathyContact *contact,
716 const gchar *message)
718 EmpathyContactPriv *priv = GET_PRIV (contact);
720 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
722 if (!tp_strdiff (message, priv->presence_message))
725 g_free (priv->presence_message);
726 priv->presence_message = g_strdup (message);
728 g_object_notify (G_OBJECT (contact), "presence-message");
732 empathy_contact_get_handle (EmpathyContact *contact)
734 EmpathyContactPriv *priv;
736 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
738 priv = GET_PRIV (contact);
740 if (priv->tp_contact != NULL)
741 return tp_contact_get_handle (priv->tp_contact);
747 empathy_contact_set_handle (EmpathyContact *contact,
750 EmpathyContactPriv *priv;
752 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
754 priv = GET_PRIV (contact);
756 g_object_ref (contact);
757 if (handle != priv->handle)
759 priv->handle = handle;
760 g_object_notify (G_OBJECT (contact), "handle");
762 g_object_unref (contact);
766 empathy_contact_get_capabilities (EmpathyContact *contact)
768 EmpathyContactPriv *priv;
770 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
772 priv = GET_PRIV (contact);
774 return priv->capabilities;
778 empathy_contact_set_capabilities (EmpathyContact *contact,
779 EmpathyCapabilities capabilities)
781 EmpathyContactPriv *priv;
783 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
785 priv = GET_PRIV (contact);
787 if (priv->capabilities == capabilities)
790 priv->capabilities = capabilities;
792 g_object_notify (G_OBJECT (contact), "capabilities");
796 empathy_contact_is_user (EmpathyContact *contact)
798 EmpathyContactPriv *priv;
800 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
802 priv = GET_PRIV (contact);
804 return priv->is_user;
808 empathy_contact_set_is_user (EmpathyContact *contact,
811 EmpathyContactPriv *priv;
813 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
815 priv = GET_PRIV (contact);
817 if (priv->is_user == is_user)
820 priv->is_user = is_user;
822 g_object_notify (G_OBJECT (contact), "is-user");
826 empathy_contact_is_online (EmpathyContact *contact)
828 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
830 switch (empathy_contact_get_presence (contact))
832 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
833 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
834 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
842 empathy_contact_get_status (EmpathyContact *contact)
844 const gchar *message;
846 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
848 message = empathy_contact_get_presence_message (contact);
849 if (!EMP_STR_EMPTY (message))
852 return empathy_presence_get_default_message (
853 empathy_contact_get_presence (contact));
857 empathy_contact_can_voip (EmpathyContact *contact)
859 EmpathyContactPriv *priv;
861 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
863 priv = GET_PRIV (contact);
865 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
866 EMPATHY_CAPABILITIES_VIDEO);
870 empathy_contact_can_voip_audio (EmpathyContact *contact)
872 EmpathyContactPriv *priv;
874 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
876 priv = GET_PRIV (contact);
878 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
882 empathy_contact_can_voip_video (EmpathyContact *contact)
884 EmpathyContactPriv *priv;
886 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
888 priv = GET_PRIV (contact);
890 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
894 empathy_contact_can_send_files (EmpathyContact *contact)
896 EmpathyContactPriv *priv;
898 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
900 priv = GET_PRIV (contact);
902 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
906 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
908 EmpathyContactPriv *priv;
910 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
912 priv = GET_PRIV (contact);
914 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
918 contact_get_avatar_filename (EmpathyContact *contact,
924 gchar *token_escaped;
926 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
929 token_escaped = tp_escape_as_identifier (token);
930 account = empathy_contact_get_account (contact);
932 avatar_path = g_build_filename (g_get_user_cache_dir (),
935 tp_account_get_connection_manager (account),
936 tp_account_get_protocol (account),
938 g_mkdir_with_parents (avatar_path, 0700);
940 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
942 g_free (token_escaped);
943 g_free (avatar_path);
949 empathy_contact_load_avatar_data (EmpathyContact *contact,
955 EmpathyAvatar *avatar;
957 GError *error = NULL;
959 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
960 g_return_if_fail (data != NULL);
961 g_return_if_fail (len > 0);
962 g_return_if_fail (format != NULL);
963 g_return_if_fail (!EMP_STR_EMPTY (token));
965 /* Load and set the avatar */
966 filename = contact_get_avatar_filename (contact, token);
967 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
968 g_strdup (token), filename);
969 empathy_contact_set_avatar (contact, avatar);
970 empathy_avatar_unref (avatar);
972 /* Save to cache if not yet in it */
973 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
975 if (!empathy_avatar_save_to_file (avatar, filename, &error))
977 DEBUG ("Failed to save avatar in cache: %s",
978 error ? error->message : "No error given");
979 g_clear_error (&error);
982 DEBUG ("Avatar saved to %s", filename);
987 empathy_contact_load_avatar_cache (EmpathyContact *contact,
990 EmpathyAvatar *avatar = NULL;
994 GError *error = NULL;
996 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
997 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
999 /* Load the avatar from file if it exists */
1000 filename = contact_get_avatar_filename (contact, token);
1001 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1003 if (!g_file_get_contents (filename, &data, &len, &error))
1005 DEBUG ("Failed to load avatar from cache: %s",
1006 error ? error->message : "No error given");
1007 g_clear_error (&error);
1013 DEBUG ("Avatar loaded from %s", filename);
1014 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1016 empathy_contact_set_avatar (contact, avatar);
1017 empathy_avatar_unref (avatar);
1024 return data != NULL;
1028 empathy_avatar_get_type (void)
1030 static GType type_id = 0;
1034 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1035 (GBoxedCopyFunc) empathy_avatar_ref,
1036 (GBoxedFreeFunc) empathy_avatar_unref);
1043 * empathy_avatar_new:
1044 * @data: the avatar data
1045 * @len: the size of avatar data
1046 * @format: the mime type of the avatar image
1047 * @token: the token of the avatar
1048 * @filename: the filename where the avatar is stored in cache
1050 * Create a #EmpathyAvatar from the provided data. This function takes the
1051 * ownership of @data, @format, @token and @filename.
1053 * Returns: a new #EmpathyAvatar
1056 empathy_avatar_new (guchar *data,
1062 EmpathyAvatar *avatar;
1064 avatar = g_slice_new0 (EmpathyAvatar);
1065 avatar->data = data;
1067 avatar->format = format;
1068 avatar->token = token;
1069 avatar->filename = filename;
1070 avatar->refcount = 1;
1076 empathy_avatar_unref (EmpathyAvatar *avatar)
1078 g_return_if_fail (avatar != NULL);
1081 if (avatar->refcount == 0)
1083 g_free (avatar->data);
1084 g_free (avatar->format);
1085 g_free (avatar->token);
1086 g_slice_free (EmpathyAvatar, avatar);
1091 empathy_avatar_ref (EmpathyAvatar *avatar)
1093 g_return_val_if_fail (avatar != NULL, NULL);
1101 * empathy_avatar_save_to_file:
1102 * @avatar: the avatar
1103 * @filename: name of a file to write avatar to
1104 * @error: return location for a GError, or NULL
1106 * Save the avatar to a file named filename
1108 * Returns: %TRUE on success, %FALSE if an error occurred
1111 empathy_avatar_save_to_file (EmpathyAvatar *self,
1112 const gchar *filename,
1115 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1120 * empathy_contact_get_location:
1121 * @contact: an #EmpathyContact
1123 * Returns the user's location if available. The keys are defined in
1124 * empathy-location.h. If the contact doesn't have location
1125 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1126 * you are done with the #GHashTable.
1128 * It is composed of string keys and GValues. Keys are
1129 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1130 * Example: a "city" key would have "Helsinki" as string GValue,
1131 * a "latitude" would have 65.0 as double GValue.
1133 * Returns: a #GHashTable of location values
1136 empathy_contact_get_location (EmpathyContact *contact)
1138 EmpathyContactPriv *priv;
1140 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1142 priv = GET_PRIV (contact);
1144 return priv->location;
1148 * empathy_contact_set_location:
1149 * @contact: an #EmpathyContact
1150 * @location: a #GHashTable of the location
1152 * Sets the user's location based on the location #GHashTable passed.
1153 * It is composed of string keys and GValues. Keys are
1154 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1155 * Example: a "city" key would have "Helsinki" as string GValue,
1156 * a "latitude" would have 65.0 as double GValue.
1159 empathy_contact_set_location (EmpathyContact *contact,
1160 GHashTable *location)
1162 EmpathyContactPriv *priv;
1164 g_return_if_fail (EMPATHY_CONTACT (contact));
1165 g_return_if_fail (location != NULL);
1167 priv = GET_PRIV (contact);
1169 if (priv->location != NULL)
1170 g_hash_table_unref (priv->location);
1172 priv->location = g_hash_table_ref (location);
1174 update_geocode (contact);
1176 g_object_notify (G_OBJECT (contact), "location");
1180 * empathy_contact_equal:
1181 * @contact1: an #EmpathyContact
1182 * @contact2: an #EmpathyContact
1184 * Returns FALSE if one of the contacts is NULL but the other is not.
1185 * Otherwise returns TRUE if both pointer are equal or if they bith
1186 * refer to the same id.
1187 * It's only necessary to call this function if your contact objects
1188 * come from logs where contacts are created dynamically and comparing
1189 * pointers is not enough.
1192 empathy_contact_equal (gconstpointer contact1,
1193 gconstpointer contact2)
1200 if ((contact1 == NULL) != (contact2 == NULL)) {
1203 if (contact1 == contact2) {
1206 c1 = EMPATHY_CONTACT (contact1);
1207 c2 = EMPATHY_CONTACT (contact2);
1208 id1 = empathy_contact_get_id (c1);
1209 id2 = empathy_contact_get_id (c2);
1210 if (!tp_strdiff (id1, id2)) {
1217 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1218 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1220 /* This callback is called by geoclue when it found a position
1221 * for the given address. A position is necessary for a contact
1222 * to show up on the map
1225 geocode_cb (GeoclueGeocode *geocode,
1226 GeocluePositionFields fields,
1230 GeoclueAccuracy *accuracy,
1234 EmpathyContactPriv *priv = GET_PRIV (contact);
1235 GHashTable *new_location;
1237 if (priv->location == NULL)
1242 DEBUG ("Error geocoding location : %s", error->message);
1246 /* No need to change location if we didn't find the position */
1247 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1250 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1253 new_location = tp_asv_new (
1254 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1255 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1258 DEBUG ("\t - Latitude: %f", latitude);
1259 DEBUG ("\t - Longitude: %f", longitude);
1261 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1262 * the values we just set. */
1263 tp_g_hash_table_update (new_location, priv->location,
1264 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1266 /* Set the altitude only if it wasn't defined before */
1267 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1268 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1270 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1272 DEBUG ("\t - Altitude: %f", altitude);
1275 /* Don't change the accuracy as we used an address to get this position */
1276 g_hash_table_unref (priv->location);
1277 priv->location = new_location;
1278 g_object_notify (contact, "location");
1280 g_object_unref (geocode);
1281 g_object_unref (contact);
1285 get_dup_string (GHashTable *location,
1290 value = g_hash_table_lookup (location, key);
1292 return g_value_dup_string (value);
1298 update_geocode (EmpathyContact *contact)
1300 static GeoclueGeocode *geocode;
1302 GHashTable *address;
1303 GHashTable *location;
1305 location = empathy_contact_get_location (contact);
1306 if (location == NULL)
1309 /* No need to search for position if contact published it */
1310 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1311 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1314 if (geocode == NULL)
1316 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1317 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1321 g_object_ref (geocode);
1324 address = geoclue_address_details_new ();
1326 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1329 g_hash_table_insert (address,
1330 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1331 DEBUG ("\t - countrycode: %s", str);
1334 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1337 g_hash_table_insert (address,
1338 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1339 DEBUG ("\t - country: %s", str);
1342 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1345 g_hash_table_insert (address,
1346 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1347 DEBUG ("\t - postalcode: %s", str);
1350 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1353 g_hash_table_insert (address,
1354 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1355 DEBUG ("\t - region: %s", str);
1358 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1361 g_hash_table_insert (address,
1362 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1363 DEBUG ("\t - locality: %s", str);
1366 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1369 g_hash_table_insert (address,
1370 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1371 DEBUG ("\t - street: %s", str);
1374 if (g_hash_table_size (address) > 0)
1376 g_object_ref (contact);
1378 geoclue_geocode_address_to_position_async (geocode, address,
1379 geocode_cb, contact);
1382 g_hash_table_unref (address);