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);
76 static void update_geocode (EmpathyContact *contact);
78 static void empathy_contact_set_location (EmpathyContact *contact,
79 GHashTable *location);
81 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
92 PROP_PRESENCE_MESSAGE,
104 static guint signals[LAST_SIGNAL];
107 tp_contact_notify_cb (TpContact *tp_contact,
111 EmpathyContactPriv *priv = GET_PRIV (contact);
113 /* Forward property notifications */
114 if (!tp_strdiff (param->name, "alias"))
115 g_object_notify (contact, "name");
116 else if (!tp_strdiff (param->name, "presence-type")) {
117 TpConnectionPresenceType presence;
119 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
120 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
122 priv->presence = presence;
123 g_object_notify (contact, "presence");
125 else if (!tp_strdiff (param->name, "presence-message"))
126 g_object_notify (contact, "presence-message");
127 else if (!tp_strdiff (param->name, "identifier"))
128 g_object_notify (contact, "id");
129 else if (!tp_strdiff (param->name, "handle"))
130 g_object_notify (contact, "handle");
131 else if (!tp_strdiff (param->name, "location"))
133 GHashTable *location;
135 location = tp_contact_get_location (tp_contact);
136 /* This will start a geoclue search to find the address if needed */
137 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
142 contact_dispose (GObject *object)
144 EmpathyContactPriv *priv = GET_PRIV (object);
146 if (priv->tp_contact)
148 g_signal_handlers_disconnect_by_func (priv->tp_contact,
149 tp_contact_notify_cb, object);
150 g_object_unref (priv->tp_contact);
152 priv->tp_contact = NULL;
155 g_object_unref (priv->account);
156 priv->account = NULL;
158 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
162 empathy_contact_class_init (EmpathyContactClass *class)
164 GObjectClass *object_class;
166 object_class = G_OBJECT_CLASS (class);
168 object_class->finalize = contact_finalize;
169 object_class->dispose = contact_dispose;
170 object_class->get_property = contact_get_property;
171 object_class->set_property = contact_set_property;
173 g_object_class_install_property (object_class,
175 g_param_spec_object ("tp-contact",
177 "The TpContact associated with the contact",
179 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (object_class,
183 g_param_spec_object ("account",
185 "The account associated with the contact",
187 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (object_class,
191 g_param_spec_string ("id",
193 "String identifying contact",
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (object_class,
199 g_param_spec_string ("name",
201 "The name of the contact",
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 g_object_class_install_property (object_class,
207 g_param_spec_boxed ("avatar",
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213 g_object_class_install_property (object_class,
215 g_param_spec_uint ("presence",
217 "Presence of contact",
218 TP_CONNECTION_PRESENCE_TYPE_UNSET,
219 NUM_TP_CONNECTION_PRESENCE_TYPES,
220 TP_CONNECTION_PRESENCE_TYPE_UNSET,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 g_object_class_install_property (object_class,
224 PROP_PRESENCE_MESSAGE,
225 g_param_spec_string ("presence-message",
226 "Contact presence message",
227 "Presence message of contact",
229 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 g_object_class_install_property (object_class,
233 g_param_spec_uint ("handle",
235 "The handle of the contact",
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (object_class,
243 g_param_spec_flags ("capabilities",
244 "Contact Capabilities",
245 "Capabilities of the contact",
246 EMPATHY_TYPE_CAPABILITIES,
247 EMPATHY_CAPABILITIES_UNKNOWN,
248 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (object_class,
252 g_param_spec_boolean ("is-user",
254 "Is contact the user",
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (object_class,
261 g_param_spec_boxed ("location",
263 "Physical location of the contact",
265 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
267 signals[PRESENCE_CHANGED] =
268 g_signal_new ("presence-changed",
269 G_TYPE_FROM_CLASS (class),
273 _empathy_marshal_VOID__UINT_UINT,
278 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
282 empathy_contact_init (EmpathyContact *contact)
284 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
285 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
287 contact->priv = priv;
289 priv->location = NULL;
293 contact_finalize (GObject *object)
295 EmpathyContactPriv *priv;
297 priv = GET_PRIV (object);
299 DEBUG ("finalize: %p", object);
303 g_free (priv->presence_message);
306 empathy_avatar_unref (priv->avatar);
308 if (priv->location != NULL)
309 g_hash_table_unref (priv->location);
311 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
315 set_tp_contact (EmpathyContact *contact,
316 TpContact *tp_contact)
318 EmpathyContactPriv *priv = GET_PRIV (contact);
319 GHashTable *location;
321 if (tp_contact == NULL)
324 g_assert (priv->tp_contact == NULL);
325 priv->tp_contact = g_object_ref (tp_contact);
326 priv->presence = empathy_contact_get_presence (contact);
328 location = tp_contact_get_location (tp_contact);
329 if (location != NULL)
330 empathy_contact_set_location (contact, location);
332 g_signal_connect (priv->tp_contact, "notify",
333 G_CALLBACK (tp_contact_notify_cb), contact);
337 contact_get_property (GObject *object,
342 EmpathyContact *contact = EMPATHY_CONTACT (object);
346 case PROP_TP_CONTACT:
347 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
350 g_value_set_object (value, empathy_contact_get_account (contact));
353 g_value_set_string (value, empathy_contact_get_id (contact));
356 g_value_set_string (value, empathy_contact_get_name (contact));
359 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
362 g_value_set_uint (value, empathy_contact_get_presence (contact));
364 case PROP_PRESENCE_MESSAGE:
365 g_value_set_string (value, empathy_contact_get_presence_message (contact));
368 g_value_set_uint (value, empathy_contact_get_handle (contact));
370 case PROP_CAPABILITIES:
371 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
374 g_value_set_boolean (value, empathy_contact_is_user (contact));
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
383 contact_set_property (GObject *object,
388 EmpathyContact *contact = EMPATHY_CONTACT (object);
389 EmpathyContactPriv *priv = GET_PRIV (object);
393 case PROP_TP_CONTACT:
394 set_tp_contact (contact, g_value_get_object (value));
397 g_assert (priv->account == NULL);
398 priv->account = g_value_dup_object (value);
401 empathy_contact_set_id (contact, g_value_get_string (value));
404 empathy_contact_set_name (contact, g_value_get_string (value));
407 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
410 empathy_contact_set_presence (contact, g_value_get_uint (value));
412 case PROP_PRESENCE_MESSAGE:
413 empathy_contact_set_presence_message (contact, g_value_get_string (value));
416 empathy_contact_set_handle (contact, g_value_get_uint (value));
418 case PROP_CAPABILITIES:
419 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
422 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
431 empathy_contact_new (TpContact *tp_contact)
433 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
435 return g_object_new (EMPATHY_TYPE_CONTACT,
436 "tp-contact", tp_contact,
442 empathy_contact_from_tpl_contact (TpAccount *account,
443 TplContact *tpl_contact)
445 EmpathyContact *retval;
448 g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
450 is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
452 retval = g_object_new (EMPATHY_TYPE_CONTACT,
453 "id", tpl_contact_get_alias (tpl_contact),
454 "name", tpl_contact_get_identifier (tpl_contact),
459 if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
460 empathy_contact_load_avatar_cache (retval,
461 tpl_contact_get_avatar_token (tpl_contact));
465 #endif /* ENABLE_TPL */
468 empathy_contact_new_for_log (TpAccount *account,
473 g_return_val_if_fail (id != NULL, NULL);
474 g_assert (account != NULL);
476 return g_object_new (EMPATHY_TYPE_CONTACT,
485 empathy_contact_get_tp_contact (EmpathyContact *contact)
487 EmpathyContactPriv *priv;
489 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
491 priv = GET_PRIV (contact);
493 return priv->tp_contact;
497 empathy_contact_get_id (EmpathyContact *contact)
499 EmpathyContactPriv *priv;
501 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
503 priv = GET_PRIV (contact);
505 if (priv->tp_contact != NULL)
506 return tp_contact_get_identifier (priv->tp_contact);
512 empathy_contact_set_id (EmpathyContact *contact,
515 EmpathyContactPriv *priv;
517 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
518 g_return_if_fail (id != NULL);
520 priv = GET_PRIV (contact);
522 /* We temporally ref the contact because it could be destroyed
523 * during the signal emition */
524 g_object_ref (contact);
525 if (tp_strdiff (id, priv->id))
528 priv->id = g_strdup (id);
530 g_object_notify (G_OBJECT (contact), "id");
531 if (EMP_STR_EMPTY (priv->name))
532 g_object_notify (G_OBJECT (contact), "name");
535 g_object_unref (contact);
539 empathy_contact_get_name (EmpathyContact *contact)
541 EmpathyContactPriv *priv;
544 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
546 priv = GET_PRIV (contact);
548 if (priv->tp_contact != NULL)
549 alias = tp_contact_get_alias (priv->tp_contact);
553 if (!EMP_STR_EMPTY (alias))
556 return empathy_contact_get_id (contact);
560 empathy_contact_set_name (EmpathyContact *contact,
563 EmpathyContactPriv *priv;
565 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
567 priv = GET_PRIV (contact);
569 g_object_ref (contact);
570 if (tp_strdiff (name, priv->name))
573 priv->name = g_strdup (name);
574 g_object_notify (G_OBJECT (contact), "name");
576 g_object_unref (contact);
580 empathy_contact_get_avatar (EmpathyContact *contact)
582 EmpathyContactPriv *priv;
584 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
586 priv = GET_PRIV (contact);
592 empathy_contact_set_avatar (EmpathyContact *contact,
593 EmpathyAvatar *avatar)
595 EmpathyContactPriv *priv;
597 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
599 priv = GET_PRIV (contact);
601 if (priv->avatar == avatar)
606 empathy_avatar_unref (priv->avatar);
611 priv->avatar = empathy_avatar_ref (avatar);
613 g_object_notify (G_OBJECT (contact), "avatar");
617 empathy_contact_get_account (EmpathyContact *contact)
619 EmpathyContactPriv *priv;
621 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
623 priv = GET_PRIV (contact);
625 if (priv->account == NULL && priv->tp_contact != NULL)
627 TpConnection *connection;
629 /* FIXME: This assume the account manager already exists */
630 connection = tp_contact_get_connection (priv->tp_contact);
632 g_object_ref (empathy_get_account_for_connection (connection));
635 return priv->account;
639 empathy_contact_get_connection (EmpathyContact *contact)
641 EmpathyContactPriv *priv;
643 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
645 priv = GET_PRIV (contact);
647 if (priv->tp_contact != NULL)
648 return tp_contact_get_connection (priv->tp_contact);
653 TpConnectionPresenceType
654 empathy_contact_get_presence (EmpathyContact *contact)
656 EmpathyContactPriv *priv;
658 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
659 TP_CONNECTION_PRESENCE_TYPE_UNSET);
661 priv = GET_PRIV (contact);
663 if (priv->tp_contact != NULL)
664 return tp_contact_get_presence_type (priv->tp_contact);
666 return priv->presence;
670 empathy_contact_set_presence (EmpathyContact *contact,
671 TpConnectionPresenceType presence)
673 EmpathyContactPriv *priv;
674 TpConnectionPresenceType old_presence;
676 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
678 priv = GET_PRIV (contact);
680 if (presence == priv->presence)
683 old_presence = priv->presence;
684 priv->presence = presence;
686 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
688 g_object_notify (G_OBJECT (contact), "presence");
692 empathy_contact_get_presence_message (EmpathyContact *contact)
694 EmpathyContactPriv *priv;
696 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
698 priv = GET_PRIV (contact);
700 if (priv->tp_contact != NULL)
701 return tp_contact_get_presence_message (priv->tp_contact);
703 return priv->presence_message;
707 empathy_contact_set_presence_message (EmpathyContact *contact,
708 const gchar *message)
710 EmpathyContactPriv *priv = GET_PRIV (contact);
712 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
714 if (!tp_strdiff (message, priv->presence_message))
717 g_free (priv->presence_message);
718 priv->presence_message = g_strdup (message);
720 g_object_notify (G_OBJECT (contact), "presence-message");
724 empathy_contact_get_handle (EmpathyContact *contact)
726 EmpathyContactPriv *priv;
728 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
730 priv = GET_PRIV (contact);
732 if (priv->tp_contact != NULL)
733 return tp_contact_get_handle (priv->tp_contact);
739 empathy_contact_set_handle (EmpathyContact *contact,
742 EmpathyContactPriv *priv;
744 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
746 priv = GET_PRIV (contact);
748 g_object_ref (contact);
749 if (handle != priv->handle)
751 priv->handle = handle;
752 g_object_notify (G_OBJECT (contact), "handle");
754 g_object_unref (contact);
758 empathy_contact_get_capabilities (EmpathyContact *contact)
760 EmpathyContactPriv *priv;
762 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
764 priv = GET_PRIV (contact);
766 return priv->capabilities;
770 empathy_contact_set_capabilities (EmpathyContact *contact,
771 EmpathyCapabilities capabilities)
773 EmpathyContactPriv *priv;
775 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
777 priv = GET_PRIV (contact);
779 if (priv->capabilities == capabilities)
782 priv->capabilities = capabilities;
784 g_object_notify (G_OBJECT (contact), "capabilities");
788 empathy_contact_is_user (EmpathyContact *contact)
790 EmpathyContactPriv *priv;
792 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
794 priv = GET_PRIV (contact);
796 return priv->is_user;
800 empathy_contact_set_is_user (EmpathyContact *contact,
803 EmpathyContactPriv *priv;
805 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
807 priv = GET_PRIV (contact);
809 if (priv->is_user == is_user)
812 priv->is_user = is_user;
814 g_object_notify (G_OBJECT (contact), "is-user");
818 empathy_contact_is_online (EmpathyContact *contact)
820 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
822 switch (empathy_contact_get_presence (contact))
824 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
825 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
826 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
834 empathy_contact_get_status (EmpathyContact *contact)
836 const gchar *message;
838 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
840 message = empathy_contact_get_presence_message (contact);
841 if (!EMP_STR_EMPTY (message))
844 return empathy_presence_get_default_message (
845 empathy_contact_get_presence (contact));
849 empathy_contact_can_voip (EmpathyContact *contact)
851 EmpathyContactPriv *priv;
853 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
855 priv = GET_PRIV (contact);
857 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
858 EMPATHY_CAPABILITIES_VIDEO);
862 empathy_contact_can_voip_audio (EmpathyContact *contact)
864 EmpathyContactPriv *priv;
866 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
868 priv = GET_PRIV (contact);
870 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
874 empathy_contact_can_voip_video (EmpathyContact *contact)
876 EmpathyContactPriv *priv;
878 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
880 priv = GET_PRIV (contact);
882 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
886 empathy_contact_can_send_files (EmpathyContact *contact)
888 EmpathyContactPriv *priv;
890 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
892 priv = GET_PRIV (contact);
894 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
898 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
900 EmpathyContactPriv *priv;
902 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
904 priv = GET_PRIV (contact);
906 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
910 contact_get_avatar_filename (EmpathyContact *contact,
916 gchar *token_escaped;
918 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
921 token_escaped = tp_escape_as_identifier (token);
922 account = empathy_contact_get_account (contact);
924 avatar_path = g_build_filename (g_get_user_cache_dir (),
927 tp_account_get_connection_manager (account),
928 tp_account_get_protocol (account),
930 g_mkdir_with_parents (avatar_path, 0700);
932 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
934 g_free (token_escaped);
935 g_free (avatar_path);
941 empathy_contact_load_avatar_data (EmpathyContact *contact,
947 EmpathyAvatar *avatar;
949 GError *error = NULL;
951 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
952 g_return_if_fail (data != NULL);
953 g_return_if_fail (len > 0);
954 g_return_if_fail (format != NULL);
955 g_return_if_fail (!EMP_STR_EMPTY (token));
957 /* Load and set the avatar */
958 filename = contact_get_avatar_filename (contact, token);
959 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
960 g_strdup (token), filename);
961 empathy_contact_set_avatar (contact, avatar);
962 empathy_avatar_unref (avatar);
964 /* Save to cache if not yet in it */
965 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
967 if (!empathy_avatar_save_to_file (avatar, filename, &error))
969 DEBUG ("Failed to save avatar in cache: %s",
970 error ? error->message : "No error given");
971 g_clear_error (&error);
974 DEBUG ("Avatar saved to %s", filename);
979 empathy_contact_load_avatar_cache (EmpathyContact *contact,
982 EmpathyAvatar *avatar = NULL;
986 GError *error = NULL;
988 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
989 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
991 /* Load the avatar from file if it exists */
992 filename = contact_get_avatar_filename (contact, token);
993 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
995 if (!g_file_get_contents (filename, &data, &len, &error))
997 DEBUG ("Failed to load avatar from cache: %s",
998 error ? error->message : "No error given");
999 g_clear_error (&error);
1005 DEBUG ("Avatar loaded from %s", filename);
1006 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1008 empathy_contact_set_avatar (contact, avatar);
1009 empathy_avatar_unref (avatar);
1016 return data != NULL;
1020 empathy_avatar_get_type (void)
1022 static GType type_id = 0;
1026 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1027 (GBoxedCopyFunc) empathy_avatar_ref,
1028 (GBoxedFreeFunc) empathy_avatar_unref);
1035 * empathy_avatar_new:
1036 * @data: the avatar data
1037 * @len: the size of avatar data
1038 * @format: the mime type of the avatar image
1039 * @token: the token of the avatar
1040 * @filename: the filename where the avatar is stored in cache
1042 * Create a #EmpathyAvatar from the provided data. This function takes the
1043 * ownership of @data, @format, @token and @filename.
1045 * Returns: a new #EmpathyAvatar
1048 empathy_avatar_new (guchar *data,
1054 EmpathyAvatar *avatar;
1056 avatar = g_slice_new0 (EmpathyAvatar);
1057 avatar->data = data;
1059 avatar->format = format;
1060 avatar->token = token;
1061 avatar->filename = filename;
1062 avatar->refcount = 1;
1068 empathy_avatar_unref (EmpathyAvatar *avatar)
1070 g_return_if_fail (avatar != NULL);
1073 if (avatar->refcount == 0)
1075 g_free (avatar->data);
1076 g_free (avatar->format);
1077 g_free (avatar->token);
1078 g_slice_free (EmpathyAvatar, avatar);
1083 empathy_avatar_ref (EmpathyAvatar *avatar)
1085 g_return_val_if_fail (avatar != NULL, NULL);
1093 * empathy_avatar_save_to_file:
1094 * @avatar: the avatar
1095 * @filename: name of a file to write avatar to
1096 * @error: return location for a GError, or NULL
1098 * Save the avatar to a file named filename
1100 * Returns: %TRUE on success, %FALSE if an error occurred
1103 empathy_avatar_save_to_file (EmpathyAvatar *self,
1104 const gchar *filename,
1107 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1112 * empathy_contact_get_location:
1113 * @contact: an #EmpathyContact
1115 * Returns the user's location if available. The keys are defined in
1116 * empathy-location.h. If the contact doesn't have location
1117 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1118 * you are done with the #GHashTable.
1120 * It is composed of string keys and GValues. Keys are
1121 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1122 * Example: a "city" key would have "Helsinki" as string GValue,
1123 * a "latitude" would have 65.0 as double GValue.
1125 * Returns: a #GHashTable of location values
1128 empathy_contact_get_location (EmpathyContact *contact)
1130 EmpathyContactPriv *priv;
1132 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1134 priv = GET_PRIV (contact);
1136 return priv->location;
1140 * empathy_contact_set_location:
1141 * @contact: an #EmpathyContact
1142 * @location: a #GHashTable of the location
1144 * Sets the user's location based on the location #GHashTable passed.
1145 * It is composed of string keys and GValues. Keys are
1146 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1147 * Example: a "city" key would have "Helsinki" as string GValue,
1148 * a "latitude" would have 65.0 as double GValue.
1151 empathy_contact_set_location (EmpathyContact *contact,
1152 GHashTable *location)
1154 EmpathyContactPriv *priv;
1156 g_return_if_fail (EMPATHY_CONTACT (contact));
1157 g_return_if_fail (location != NULL);
1159 priv = GET_PRIV (contact);
1161 if (priv->location != NULL)
1162 g_hash_table_unref (priv->location);
1164 priv->location = g_hash_table_ref (location);
1165 update_geocode (contact);
1166 g_object_notify (G_OBJECT (contact), "location");
1170 * empathy_contact_equal:
1171 * @contact1: an #EmpathyContact
1172 * @contact2: an #EmpathyContact
1174 * Returns FALSE if one of the contacts is NULL but the other is not.
1175 * Otherwise returns TRUE if both pointer are equal or if they bith
1176 * refer to the same id.
1177 * It's only necessary to call this function if your contact objects
1178 * come from logs where contacts are created dynamically and comparing
1179 * pointers is not enough.
1182 empathy_contact_equal (gconstpointer contact1,
1183 gconstpointer contact2)
1190 if ((contact1 == NULL) != (contact2 == NULL)) {
1193 if (contact1 == contact2) {
1196 c1 = EMPATHY_CONTACT (contact1);
1197 c2 = EMPATHY_CONTACT (contact2);
1198 id1 = empathy_contact_get_id (c1);
1199 id2 = empathy_contact_get_id (c2);
1200 if (!tp_strdiff (id1, id2)) {
1207 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1208 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1210 /* This callback is called by geoclue when it found a position
1211 * for the given address. A position is necessary for a contact
1212 * to show up on the map
1215 geocode_cb (GeoclueGeocode *geocode,
1216 GeocluePositionFields fields,
1220 GeoclueAccuracy *accuracy,
1225 GHashTable *location;
1227 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
1229 if (error != NULL) {
1230 DEBUG ("Error geocoding location : %s", error->message);
1231 g_object_unref (geocode);
1232 g_object_unref (contact);
1236 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
1237 new_value = tp_g_value_slice_new_double (latitude);
1238 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
1240 DEBUG ("\t - Latitude: %f", latitude);
1242 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
1243 new_value = tp_g_value_slice_new_double (longitude);
1244 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
1246 DEBUG ("\t - Longitude: %f", longitude);
1248 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
1249 new_value = tp_g_value_slice_new_double (altitude);
1250 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
1252 DEBUG ("\t - Altitude: %f", altitude);
1255 /* Don't change the accuracy as we used an address to get this position */
1256 g_object_notify (contact, "location");
1257 g_object_unref (geocode);
1258 g_object_unref (contact);
1264 get_dup_string (GHashTable *location,
1269 value = g_hash_table_lookup (location, key);
1271 return g_value_dup_string (value);
1278 update_geocode (EmpathyContact *contact)
1281 static GeoclueGeocode *geocode;
1283 GHashTable *address;
1285 GHashTable *location;
1287 location = empathy_contact_get_location (contact);
1288 if (location == NULL)
1291 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
1295 if (geocode == NULL) {
1296 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1297 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1300 g_object_ref (geocode);
1302 address = geoclue_address_details_new ();
1304 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1306 g_hash_table_insert (address,
1307 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1308 DEBUG ("\t - countrycode: %s", str);
1311 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1313 g_hash_table_insert (address,
1314 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1315 DEBUG ("\t - country: %s", str);
1318 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1320 g_hash_table_insert (address,
1321 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1322 DEBUG ("\t - postalcode: %s", str);
1325 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1327 g_hash_table_insert (address,
1328 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1329 DEBUG ("\t - region: %s", str);
1332 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1334 g_hash_table_insert (address,
1335 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1336 DEBUG ("\t - locality: %s", str);
1339 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1341 g_hash_table_insert (address,
1342 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1343 DEBUG ("\t - street: %s", str);
1346 g_object_ref (contact);
1347 geoclue_geocode_address_to_position_async (geocode, address,
1348 geocode_cb, contact);
1350 g_hash_table_unref (address);