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 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
164 empathy_contact_class_init (EmpathyContactClass *class)
166 GObjectClass *object_class;
168 object_class = G_OBJECT_CLASS (class);
170 object_class->finalize = contact_finalize;
171 object_class->dispose = contact_dispose;
172 object_class->get_property = contact_get_property;
173 object_class->set_property = contact_set_property;
175 g_object_class_install_property (object_class,
177 g_param_spec_object ("tp-contact",
179 "The TpContact associated with the contact",
181 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (object_class,
185 g_param_spec_object ("account",
187 "The account associated with the contact",
189 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (object_class,
193 g_param_spec_string ("id",
195 "String identifying contact",
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (object_class,
201 g_param_spec_string ("name",
203 "The name of the contact",
205 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (object_class,
209 g_param_spec_boxed ("avatar",
213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (object_class,
217 g_param_spec_uint ("presence",
219 "Presence of contact",
220 TP_CONNECTION_PRESENCE_TYPE_UNSET,
221 NUM_TP_CONNECTION_PRESENCE_TYPES,
222 TP_CONNECTION_PRESENCE_TYPE_UNSET,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (object_class,
226 PROP_PRESENCE_MESSAGE,
227 g_param_spec_string ("presence-message",
228 "Contact presence message",
229 "Presence message of contact",
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property (object_class,
235 g_param_spec_uint ("handle",
237 "The handle of the contact",
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (object_class,
245 g_param_spec_flags ("capabilities",
246 "Contact Capabilities",
247 "Capabilities of the contact",
248 EMPATHY_TYPE_CAPABILITIES,
249 EMPATHY_CAPABILITIES_UNKNOWN,
250 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (object_class,
254 g_param_spec_boolean ("is-user",
256 "Is contact the user",
258 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (object_class,
263 g_param_spec_boxed ("location",
265 "Physical location of the contact",
267 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
269 signals[PRESENCE_CHANGED] =
270 g_signal_new ("presence-changed",
271 G_TYPE_FROM_CLASS (class),
275 _empathy_marshal_VOID__UINT_UINT,
280 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
284 empathy_contact_init (EmpathyContact *contact)
286 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
287 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
289 contact->priv = priv;
291 priv->location = NULL;
295 contact_finalize (GObject *object)
297 EmpathyContactPriv *priv;
299 priv = GET_PRIV (object);
301 DEBUG ("finalize: %p", object);
305 g_free (priv->presence_message);
308 empathy_avatar_unref (priv->avatar);
310 if (priv->location != NULL)
311 g_hash_table_unref (priv->location);
313 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
317 set_tp_contact (EmpathyContact *contact,
318 TpContact *tp_contact)
320 EmpathyContactPriv *priv = GET_PRIV (contact);
321 GHashTable *location;
323 if (tp_contact == NULL)
326 g_assert (priv->tp_contact == NULL);
327 priv->tp_contact = g_object_ref (tp_contact);
328 priv->presence = empathy_contact_get_presence (contact);
330 location = tp_contact_get_location (tp_contact);
331 if (location != NULL)
332 empathy_contact_set_location (contact, location);
334 g_signal_connect (priv->tp_contact, "notify",
335 G_CALLBACK (tp_contact_notify_cb), contact);
339 contact_get_property (GObject *object,
344 EmpathyContact *contact = EMPATHY_CONTACT (object);
348 case PROP_TP_CONTACT:
349 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
352 g_value_set_object (value, empathy_contact_get_account (contact));
355 g_value_set_string (value, empathy_contact_get_id (contact));
358 g_value_set_string (value, empathy_contact_get_name (contact));
361 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
364 g_value_set_uint (value, empathy_contact_get_presence (contact));
366 case PROP_PRESENCE_MESSAGE:
367 g_value_set_string (value, empathy_contact_get_presence_message (contact));
370 g_value_set_uint (value, empathy_contact_get_handle (contact));
372 case PROP_CAPABILITIES:
373 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
376 g_value_set_boolean (value, empathy_contact_is_user (contact));
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
385 contact_set_property (GObject *object,
390 EmpathyContact *contact = EMPATHY_CONTACT (object);
391 EmpathyContactPriv *priv = GET_PRIV (object);
395 case PROP_TP_CONTACT:
396 set_tp_contact (contact, g_value_get_object (value));
399 g_assert (priv->account == NULL);
400 priv->account = g_value_dup_object (value);
403 empathy_contact_set_id (contact, g_value_get_string (value));
406 empathy_contact_set_name (contact, g_value_get_string (value));
409 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
412 empathy_contact_set_presence (contact, g_value_get_uint (value));
414 case PROP_PRESENCE_MESSAGE:
415 empathy_contact_set_presence_message (contact, g_value_get_string (value));
418 empathy_contact_set_handle (contact, g_value_get_uint (value));
420 case PROP_CAPABILITIES:
421 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
424 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
433 empathy_contact_new (TpContact *tp_contact)
435 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
437 return g_object_new (EMPATHY_TYPE_CONTACT,
438 "tp-contact", tp_contact,
444 empathy_contact_from_tpl_contact (TpAccount *account,
445 TplContact *tpl_contact)
447 EmpathyContact *retval;
450 g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
452 is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
454 retval = g_object_new (EMPATHY_TYPE_CONTACT,
455 "id", tpl_contact_get_alias (tpl_contact),
456 "name", tpl_contact_get_identifier (tpl_contact),
461 if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
462 empathy_contact_load_avatar_cache (retval,
463 tpl_contact_get_avatar_token (tpl_contact));
467 #endif /* ENABLE_TPL */
470 empathy_contact_new_for_log (TpAccount *account,
475 g_return_val_if_fail (id != NULL, NULL);
476 g_assert (account != NULL);
478 return g_object_new (EMPATHY_TYPE_CONTACT,
487 empathy_contact_get_tp_contact (EmpathyContact *contact)
489 EmpathyContactPriv *priv;
491 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
493 priv = GET_PRIV (contact);
495 return priv->tp_contact;
499 empathy_contact_get_id (EmpathyContact *contact)
501 EmpathyContactPriv *priv;
503 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
505 priv = GET_PRIV (contact);
507 if (priv->tp_contact != NULL)
508 return tp_contact_get_identifier (priv->tp_contact);
514 empathy_contact_set_id (EmpathyContact *contact,
517 EmpathyContactPriv *priv;
519 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
520 g_return_if_fail (id != NULL);
522 priv = GET_PRIV (contact);
524 /* We temporally ref the contact because it could be destroyed
525 * during the signal emition */
526 g_object_ref (contact);
527 if (tp_strdiff (id, priv->id))
530 priv->id = g_strdup (id);
532 g_object_notify (G_OBJECT (contact), "id");
533 if (EMP_STR_EMPTY (priv->name))
534 g_object_notify (G_OBJECT (contact), "name");
537 g_object_unref (contact);
541 empathy_contact_get_name (EmpathyContact *contact)
543 EmpathyContactPriv *priv;
546 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
548 priv = GET_PRIV (contact);
550 if (priv->tp_contact != NULL)
551 alias = tp_contact_get_alias (priv->tp_contact);
555 if (!EMP_STR_EMPTY (alias))
558 return empathy_contact_get_id (contact);
562 empathy_contact_set_name (EmpathyContact *contact,
565 EmpathyContactPriv *priv;
567 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
569 priv = GET_PRIV (contact);
571 g_object_ref (contact);
572 if (tp_strdiff (name, priv->name))
575 priv->name = g_strdup (name);
576 g_object_notify (G_OBJECT (contact), "name");
578 g_object_unref (contact);
582 empathy_contact_get_avatar (EmpathyContact *contact)
584 EmpathyContactPriv *priv;
586 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
588 priv = GET_PRIV (contact);
594 empathy_contact_set_avatar (EmpathyContact *contact,
595 EmpathyAvatar *avatar)
597 EmpathyContactPriv *priv;
599 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
601 priv = GET_PRIV (contact);
603 if (priv->avatar == avatar)
608 empathy_avatar_unref (priv->avatar);
613 priv->avatar = empathy_avatar_ref (avatar);
615 g_object_notify (G_OBJECT (contact), "avatar");
619 empathy_contact_get_account (EmpathyContact *contact)
621 EmpathyContactPriv *priv;
623 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
625 priv = GET_PRIV (contact);
627 if (priv->account == NULL && priv->tp_contact != NULL)
629 TpConnection *connection;
631 /* FIXME: This assume the account manager already exists */
632 connection = tp_contact_get_connection (priv->tp_contact);
634 g_object_ref (empathy_get_account_for_connection (connection));
637 return priv->account;
641 empathy_contact_get_connection (EmpathyContact *contact)
643 EmpathyContactPriv *priv;
645 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
647 priv = GET_PRIV (contact);
649 if (priv->tp_contact != NULL)
650 return tp_contact_get_connection (priv->tp_contact);
655 TpConnectionPresenceType
656 empathy_contact_get_presence (EmpathyContact *contact)
658 EmpathyContactPriv *priv;
660 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
661 TP_CONNECTION_PRESENCE_TYPE_UNSET);
663 priv = GET_PRIV (contact);
665 if (priv->tp_contact != NULL)
666 return tp_contact_get_presence_type (priv->tp_contact);
668 return priv->presence;
672 empathy_contact_set_presence (EmpathyContact *contact,
673 TpConnectionPresenceType presence)
675 EmpathyContactPriv *priv;
676 TpConnectionPresenceType old_presence;
678 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
680 priv = GET_PRIV (contact);
682 if (presence == priv->presence)
685 old_presence = priv->presence;
686 priv->presence = presence;
688 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
690 g_object_notify (G_OBJECT (contact), "presence");
694 empathy_contact_get_presence_message (EmpathyContact *contact)
696 EmpathyContactPriv *priv;
698 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
700 priv = GET_PRIV (contact);
702 if (priv->tp_contact != NULL)
703 return tp_contact_get_presence_message (priv->tp_contact);
705 return priv->presence_message;
709 empathy_contact_set_presence_message (EmpathyContact *contact,
710 const gchar *message)
712 EmpathyContactPriv *priv = GET_PRIV (contact);
714 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
716 if (!tp_strdiff (message, priv->presence_message))
719 g_free (priv->presence_message);
720 priv->presence_message = g_strdup (message);
722 g_object_notify (G_OBJECT (contact), "presence-message");
726 empathy_contact_get_handle (EmpathyContact *contact)
728 EmpathyContactPriv *priv;
730 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
732 priv = GET_PRIV (contact);
734 if (priv->tp_contact != NULL)
735 return tp_contact_get_handle (priv->tp_contact);
741 empathy_contact_set_handle (EmpathyContact *contact,
744 EmpathyContactPriv *priv;
746 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
748 priv = GET_PRIV (contact);
750 g_object_ref (contact);
751 if (handle != priv->handle)
753 priv->handle = handle;
754 g_object_notify (G_OBJECT (contact), "handle");
756 g_object_unref (contact);
760 empathy_contact_get_capabilities (EmpathyContact *contact)
762 EmpathyContactPriv *priv;
764 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
766 priv = GET_PRIV (contact);
768 return priv->capabilities;
772 empathy_contact_set_capabilities (EmpathyContact *contact,
773 EmpathyCapabilities capabilities)
775 EmpathyContactPriv *priv;
777 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
779 priv = GET_PRIV (contact);
781 if (priv->capabilities == capabilities)
784 priv->capabilities = capabilities;
786 g_object_notify (G_OBJECT (contact), "capabilities");
790 empathy_contact_is_user (EmpathyContact *contact)
792 EmpathyContactPriv *priv;
794 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
796 priv = GET_PRIV (contact);
798 return priv->is_user;
802 empathy_contact_set_is_user (EmpathyContact *contact,
805 EmpathyContactPriv *priv;
807 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
809 priv = GET_PRIV (contact);
811 if (priv->is_user == is_user)
814 priv->is_user = is_user;
816 g_object_notify (G_OBJECT (contact), "is-user");
820 empathy_contact_is_online (EmpathyContact *contact)
822 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
824 switch (empathy_contact_get_presence (contact))
826 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
827 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
828 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
836 empathy_contact_get_status (EmpathyContact *contact)
838 const gchar *message;
840 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
842 message = empathy_contact_get_presence_message (contact);
843 if (!EMP_STR_EMPTY (message))
846 return empathy_presence_get_default_message (
847 empathy_contact_get_presence (contact));
851 empathy_contact_can_voip (EmpathyContact *contact)
853 EmpathyContactPriv *priv;
855 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
857 priv = GET_PRIV (contact);
859 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
860 EMPATHY_CAPABILITIES_VIDEO);
864 empathy_contact_can_voip_audio (EmpathyContact *contact)
866 EmpathyContactPriv *priv;
868 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
870 priv = GET_PRIV (contact);
872 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
876 empathy_contact_can_voip_video (EmpathyContact *contact)
878 EmpathyContactPriv *priv;
880 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
882 priv = GET_PRIV (contact);
884 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
888 empathy_contact_can_send_files (EmpathyContact *contact)
890 EmpathyContactPriv *priv;
892 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
894 priv = GET_PRIV (contact);
896 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
900 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
902 EmpathyContactPriv *priv;
904 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
906 priv = GET_PRIV (contact);
908 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
912 contact_get_avatar_filename (EmpathyContact *contact,
918 gchar *token_escaped;
920 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
923 token_escaped = tp_escape_as_identifier (token);
924 account = empathy_contact_get_account (contact);
926 avatar_path = g_build_filename (g_get_user_cache_dir (),
929 tp_account_get_connection_manager (account),
930 tp_account_get_protocol (account),
932 g_mkdir_with_parents (avatar_path, 0700);
934 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
936 g_free (token_escaped);
937 g_free (avatar_path);
943 empathy_contact_load_avatar_data (EmpathyContact *contact,
949 EmpathyAvatar *avatar;
951 GError *error = NULL;
953 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
954 g_return_if_fail (data != NULL);
955 g_return_if_fail (len > 0);
956 g_return_if_fail (format != NULL);
957 g_return_if_fail (!EMP_STR_EMPTY (token));
959 /* Load and set the avatar */
960 filename = contact_get_avatar_filename (contact, token);
961 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
962 g_strdup (token), filename);
963 empathy_contact_set_avatar (contact, avatar);
964 empathy_avatar_unref (avatar);
966 /* Save to cache if not yet in it */
967 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
969 if (!empathy_avatar_save_to_file (avatar, filename, &error))
971 DEBUG ("Failed to save avatar in cache: %s",
972 error ? error->message : "No error given");
973 g_clear_error (&error);
976 DEBUG ("Avatar saved to %s", filename);
981 empathy_contact_load_avatar_cache (EmpathyContact *contact,
984 EmpathyAvatar *avatar = NULL;
988 GError *error = NULL;
990 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
991 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
993 /* Load the avatar from file if it exists */
994 filename = contact_get_avatar_filename (contact, token);
995 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
997 if (!g_file_get_contents (filename, &data, &len, &error))
999 DEBUG ("Failed to load avatar from cache: %s",
1000 error ? error->message : "No error given");
1001 g_clear_error (&error);
1007 DEBUG ("Avatar loaded from %s", filename);
1008 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1010 empathy_contact_set_avatar (contact, avatar);
1011 empathy_avatar_unref (avatar);
1018 return data != NULL;
1022 empathy_avatar_get_type (void)
1024 static GType type_id = 0;
1028 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1029 (GBoxedCopyFunc) empathy_avatar_ref,
1030 (GBoxedFreeFunc) empathy_avatar_unref);
1037 * empathy_avatar_new:
1038 * @data: the avatar data
1039 * @len: the size of avatar data
1040 * @format: the mime type of the avatar image
1041 * @token: the token of the avatar
1042 * @filename: the filename where the avatar is stored in cache
1044 * Create a #EmpathyAvatar from the provided data. This function takes the
1045 * ownership of @data, @format, @token and @filename.
1047 * Returns: a new #EmpathyAvatar
1050 empathy_avatar_new (guchar *data,
1056 EmpathyAvatar *avatar;
1058 avatar = g_slice_new0 (EmpathyAvatar);
1059 avatar->data = data;
1061 avatar->format = format;
1062 avatar->token = token;
1063 avatar->filename = filename;
1064 avatar->refcount = 1;
1070 empathy_avatar_unref (EmpathyAvatar *avatar)
1072 g_return_if_fail (avatar != NULL);
1075 if (avatar->refcount == 0)
1077 g_free (avatar->data);
1078 g_free (avatar->format);
1079 g_free (avatar->token);
1080 g_slice_free (EmpathyAvatar, avatar);
1085 empathy_avatar_ref (EmpathyAvatar *avatar)
1087 g_return_val_if_fail (avatar != NULL, NULL);
1095 * empathy_avatar_save_to_file:
1096 * @avatar: the avatar
1097 * @filename: name of a file to write avatar to
1098 * @error: return location for a GError, or NULL
1100 * Save the avatar to a file named filename
1102 * Returns: %TRUE on success, %FALSE if an error occurred
1105 empathy_avatar_save_to_file (EmpathyAvatar *self,
1106 const gchar *filename,
1109 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1114 * empathy_contact_get_location:
1115 * @contact: an #EmpathyContact
1117 * Returns the user's location if available. The keys are defined in
1118 * empathy-location.h. If the contact doesn't have location
1119 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1120 * you are done with the #GHashTable.
1122 * It is composed of string keys and GValues. Keys are
1123 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1124 * Example: a "city" key would have "Helsinki" as string GValue,
1125 * a "latitude" would have 65.0 as double GValue.
1127 * Returns: a #GHashTable of location values
1130 empathy_contact_get_location (EmpathyContact *contact)
1132 EmpathyContactPriv *priv;
1134 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1136 priv = GET_PRIV (contact);
1138 return priv->location;
1142 * empathy_contact_set_location:
1143 * @contact: an #EmpathyContact
1144 * @location: a #GHashTable of the location
1146 * Sets the user's location based on the location #GHashTable passed.
1147 * It is composed of string keys and GValues. Keys are
1148 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1149 * Example: a "city" key would have "Helsinki" as string GValue,
1150 * a "latitude" would have 65.0 as double GValue.
1153 empathy_contact_set_location (EmpathyContact *contact,
1154 GHashTable *location)
1156 EmpathyContactPriv *priv;
1158 g_return_if_fail (EMPATHY_CONTACT (contact));
1159 g_return_if_fail (location != NULL);
1161 priv = GET_PRIV (contact);
1163 if (priv->location != NULL)
1164 g_hash_table_unref (priv->location);
1166 priv->location = g_hash_table_ref (location);
1168 update_geocode (contact);
1170 g_object_notify (G_OBJECT (contact), "location");
1174 * empathy_contact_equal:
1175 * @contact1: an #EmpathyContact
1176 * @contact2: an #EmpathyContact
1178 * Returns FALSE if one of the contacts is NULL but the other is not.
1179 * Otherwise returns TRUE if both pointer are equal or if they bith
1180 * refer to the same id.
1181 * It's only necessary to call this function if your contact objects
1182 * come from logs where contacts are created dynamically and comparing
1183 * pointers is not enough.
1186 empathy_contact_equal (gconstpointer contact1,
1187 gconstpointer contact2)
1194 if ((contact1 == NULL) != (contact2 == NULL)) {
1197 if (contact1 == contact2) {
1200 c1 = EMPATHY_CONTACT (contact1);
1201 c2 = EMPATHY_CONTACT (contact2);
1202 id1 = empathy_contact_get_id (c1);
1203 id2 = empathy_contact_get_id (c2);
1204 if (!tp_strdiff (id1, id2)) {
1211 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1212 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1214 /* This callback is called by geoclue when it found a position
1215 * for the given address. A position is necessary for a contact
1216 * to show up on the map
1219 geocode_cb (GeoclueGeocode *geocode,
1220 GeocluePositionFields fields,
1224 GeoclueAccuracy *accuracy,
1229 GHashTable *location;
1231 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
1235 DEBUG ("Error geocoding location : %s", error->message);
1236 g_object_unref (geocode);
1237 g_object_unref (contact);
1241 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
1243 new_value = tp_g_value_slice_new_double (latitude);
1244 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
1246 DEBUG ("\t - Latitude: %f", latitude);
1249 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
1251 new_value = tp_g_value_slice_new_double (longitude);
1252 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
1254 DEBUG ("\t - Longitude: %f", longitude);
1257 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
1259 new_value = tp_g_value_slice_new_double (altitude);
1260 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
1262 DEBUG ("\t - Altitude: %f", altitude);
1265 /* Don't change the accuracy as we used an address to get this position */
1266 g_object_notify (contact, "location");
1267 g_object_unref (geocode);
1268 g_object_unref (contact);
1272 get_dup_string (GHashTable *location,
1277 value = g_hash_table_lookup (location, key);
1279 return g_value_dup_string (value);
1285 update_geocode (EmpathyContact *contact)
1287 static GeoclueGeocode *geocode;
1289 GHashTable *address;
1291 GHashTable *location;
1293 location = empathy_contact_get_location (contact);
1294 if (location == NULL)
1297 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
1301 if (geocode == NULL)
1303 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1304 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1308 g_object_ref (geocode);
1311 address = geoclue_address_details_new ();
1313 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1316 g_hash_table_insert (address,
1317 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1318 DEBUG ("\t - countrycode: %s", str);
1321 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1324 g_hash_table_insert (address,
1325 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1326 DEBUG ("\t - country: %s", str);
1329 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1332 g_hash_table_insert (address,
1333 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1334 DEBUG ("\t - postalcode: %s", str);
1337 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1340 g_hash_table_insert (address,
1341 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1342 DEBUG ("\t - region: %s", str);
1345 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1348 g_hash_table_insert (address,
1349 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1350 DEBUG ("\t - locality: %s", str);
1353 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1356 g_hash_table_insert (address,
1357 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1358 DEBUG ("\t - street: %s", str);
1361 g_object_ref (contact);
1362 geoclue_geocode_address_to_position_async (geocode, address,
1363 geocode_cb, contact);
1365 g_hash_table_unref (address);