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/interfaces.h>
30 #include <telepathy-glib/util.h>
32 #include <telepathy-logger/contact.h>
33 #endif /* ENABLE_TPL */
36 #include <geoclue/geoclue-geocode.h>
39 #include "empathy-contact.h"
40 #include "empathy-utils.h"
41 #include "empathy-enum-types.h"
42 #include "empathy-marshal.h"
43 #include "empathy-location.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
46 #include "empathy-debug.h"
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
50 TpContact *tp_contact;
54 EmpathyAvatar *avatar;
55 TpConnectionPresenceType presence;
56 gchar *presence_message;
58 EmpathyCapabilities capabilities;
61 /* Location is composed of string keys and GValues.
62 * Example: a "city" key would have "Helsinki" as string GValue,
63 * a "latitude" would have 65.0 as double GValue.
65 * This is a super set of the location stored in TpContact as we can try add
66 * more fields by searching the address using geoclue.
71 static void contact_finalize (GObject *object);
72 static void contact_get_property (GObject *object, guint param_id,
73 GValue *value, GParamSpec *pspec);
74 static void contact_set_property (GObject *object, guint param_id,
75 const GValue *value, GParamSpec *pspec);
78 static void update_geocode (EmpathyContact *contact);
81 static void empathy_contact_set_location (EmpathyContact *contact,
82 GHashTable *location);
84 static void set_capabilities_from_tp_caps (EmpathyContact *self,
85 TpCapabilities *caps);
87 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
98 PROP_PRESENCE_MESSAGE,
110 static guint signals[LAST_SIGNAL];
113 tp_contact_notify_cb (TpContact *tp_contact,
117 EmpathyContactPriv *priv = GET_PRIV (contact);
119 /* Forward property notifications */
120 if (!tp_strdiff (param->name, "alias"))
121 g_object_notify (contact, "name");
122 else if (!tp_strdiff (param->name, "presence-type")) {
123 TpConnectionPresenceType presence;
125 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
126 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
128 priv->presence = presence;
129 g_object_notify (contact, "presence");
131 else if (!tp_strdiff (param->name, "presence-message"))
132 g_object_notify (contact, "presence-message");
133 else if (!tp_strdiff (param->name, "identifier"))
134 g_object_notify (contact, "id");
135 else if (!tp_strdiff (param->name, "handle"))
136 g_object_notify (contact, "handle");
137 else if (!tp_strdiff (param->name, "location"))
139 GHashTable *location;
141 location = tp_contact_get_location (tp_contact);
142 /* This will start a geoclue search to find the address if needed */
143 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
145 else if (!tp_strdiff (param->name, "capabilities"))
147 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
148 tp_contact_get_capabilities (tp_contact));
153 contact_dispose (GObject *object)
155 EmpathyContactPriv *priv = GET_PRIV (object);
157 if (priv->tp_contact)
159 g_signal_handlers_disconnect_by_func (priv->tp_contact,
160 tp_contact_notify_cb, object);
161 g_object_unref (priv->tp_contact);
163 priv->tp_contact = NULL;
166 g_object_unref (priv->account);
167 priv->account = NULL;
169 if (priv->avatar != NULL)
171 empathy_avatar_unref (priv->avatar);
175 if (priv->location != NULL)
177 g_hash_table_unref (priv->location);
178 priv->location = NULL;
181 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
185 empathy_contact_class_init (EmpathyContactClass *class)
187 GObjectClass *object_class;
189 object_class = G_OBJECT_CLASS (class);
191 object_class->finalize = contact_finalize;
192 object_class->dispose = contact_dispose;
193 object_class->get_property = contact_get_property;
194 object_class->set_property = contact_set_property;
196 g_object_class_install_property (object_class,
198 g_param_spec_object ("tp-contact",
200 "The TpContact associated with the contact",
202 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (object_class,
206 g_param_spec_object ("account",
208 "The account associated with the contact",
210 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (object_class,
214 g_param_spec_string ("id",
216 "String identifying contact",
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (object_class,
222 g_param_spec_string ("name",
224 "The name of the contact",
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (object_class,
230 g_param_spec_boxed ("avatar",
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (object_class,
238 g_param_spec_uint ("presence",
240 "Presence of contact",
241 TP_CONNECTION_PRESENCE_TYPE_UNSET,
242 NUM_TP_CONNECTION_PRESENCE_TYPES,
243 TP_CONNECTION_PRESENCE_TYPE_UNSET,
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (object_class,
247 PROP_PRESENCE_MESSAGE,
248 g_param_spec_string ("presence-message",
249 "Contact presence message",
250 "Presence message of contact",
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (object_class,
256 g_param_spec_uint ("handle",
258 "The handle of the contact",
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (object_class,
266 g_param_spec_flags ("capabilities",
267 "Contact Capabilities",
268 "Capabilities of the contact",
269 EMPATHY_TYPE_CAPABILITIES,
270 EMPATHY_CAPABILITIES_UNKNOWN,
271 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (object_class,
275 g_param_spec_boolean ("is-user",
277 "Is contact the user",
279 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282 g_object_class_install_property (object_class,
284 g_param_spec_boxed ("location",
286 "Physical location of the contact",
288 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
290 signals[PRESENCE_CHANGED] =
291 g_signal_new ("presence-changed",
292 G_TYPE_FROM_CLASS (class),
296 _empathy_marshal_VOID__UINT_UINT,
301 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
305 empathy_contact_init (EmpathyContact *contact)
307 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
308 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
310 contact->priv = priv;
312 priv->location = NULL;
316 contact_finalize (GObject *object)
318 EmpathyContactPriv *priv;
320 priv = GET_PRIV (object);
322 DEBUG ("finalize: %p", object);
326 g_free (priv->presence_message);
328 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
332 set_tp_contact (EmpathyContact *contact,
333 TpContact *tp_contact)
335 EmpathyContactPriv *priv = GET_PRIV (contact);
336 GHashTable *location;
338 if (tp_contact == NULL)
341 g_assert (priv->tp_contact == NULL);
342 priv->tp_contact = g_object_ref (tp_contact);
343 priv->presence = empathy_contact_get_presence (contact);
345 location = tp_contact_get_location (tp_contact);
346 if (location != NULL)
347 empathy_contact_set_location (contact, location);
349 set_capabilities_from_tp_caps (contact,
350 tp_contact_get_capabilities (tp_contact));
352 g_signal_connect (priv->tp_contact, "notify",
353 G_CALLBACK (tp_contact_notify_cb), contact);
357 contact_get_property (GObject *object,
362 EmpathyContact *contact = EMPATHY_CONTACT (object);
366 case PROP_TP_CONTACT:
367 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
370 g_value_set_object (value, empathy_contact_get_account (contact));
373 g_value_set_string (value, empathy_contact_get_id (contact));
376 g_value_set_string (value, empathy_contact_get_name (contact));
379 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
382 g_value_set_uint (value, empathy_contact_get_presence (contact));
384 case PROP_PRESENCE_MESSAGE:
385 g_value_set_string (value, empathy_contact_get_presence_message (contact));
388 g_value_set_uint (value, empathy_contact_get_handle (contact));
390 case PROP_CAPABILITIES:
391 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
394 g_value_set_boolean (value, empathy_contact_is_user (contact));
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
403 contact_set_property (GObject *object,
408 EmpathyContact *contact = EMPATHY_CONTACT (object);
409 EmpathyContactPriv *priv = GET_PRIV (object);
413 case PROP_TP_CONTACT:
414 set_tp_contact (contact, g_value_get_object (value));
417 g_assert (priv->account == NULL);
418 priv->account = g_value_dup_object (value);
421 empathy_contact_set_id (contact, g_value_get_string (value));
424 empathy_contact_set_name (contact, g_value_get_string (value));
427 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
430 empathy_contact_set_presence (contact, g_value_get_uint (value));
432 case PROP_PRESENCE_MESSAGE:
433 empathy_contact_set_presence_message (contact, g_value_get_string (value));
436 empathy_contact_set_handle (contact, g_value_get_uint (value));
438 case PROP_CAPABILITIES:
439 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
442 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
451 empathy_contact_new (TpContact *tp_contact)
453 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
455 return g_object_new (EMPATHY_TYPE_CONTACT,
456 "tp-contact", tp_contact,
462 empathy_contact_from_tpl_contact (TpAccount *account,
463 TplContact *tpl_contact)
465 EmpathyContact *retval;
468 g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
470 is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
472 retval = g_object_new (EMPATHY_TYPE_CONTACT,
473 "id", tpl_contact_get_alias (tpl_contact),
474 "name", tpl_contact_get_identifier (tpl_contact),
479 if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
480 empathy_contact_load_avatar_cache (retval,
481 tpl_contact_get_avatar_token (tpl_contact));
485 #endif /* ENABLE_TPL */
488 empathy_contact_new_for_log (TpAccount *account,
493 g_return_val_if_fail (id != NULL, NULL);
494 g_assert (account != NULL);
496 return g_object_new (EMPATHY_TYPE_CONTACT,
505 empathy_contact_get_tp_contact (EmpathyContact *contact)
507 EmpathyContactPriv *priv;
509 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
511 priv = GET_PRIV (contact);
513 return priv->tp_contact;
517 empathy_contact_get_id (EmpathyContact *contact)
519 EmpathyContactPriv *priv;
521 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
523 priv = GET_PRIV (contact);
525 if (priv->tp_contact != NULL)
526 return tp_contact_get_identifier (priv->tp_contact);
532 empathy_contact_set_id (EmpathyContact *contact,
535 EmpathyContactPriv *priv;
537 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
538 g_return_if_fail (id != NULL);
540 priv = GET_PRIV (contact);
542 /* We temporally ref the contact because it could be destroyed
543 * during the signal emition */
544 g_object_ref (contact);
545 if (tp_strdiff (id, priv->id))
548 priv->id = g_strdup (id);
550 g_object_notify (G_OBJECT (contact), "id");
551 if (EMP_STR_EMPTY (priv->name))
552 g_object_notify (G_OBJECT (contact), "name");
555 g_object_unref (contact);
559 empathy_contact_get_name (EmpathyContact *contact)
561 EmpathyContactPriv *priv;
564 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
566 priv = GET_PRIV (contact);
568 if (priv->tp_contact != NULL)
569 alias = tp_contact_get_alias (priv->tp_contact);
573 if (!EMP_STR_EMPTY (alias))
576 return empathy_contact_get_id (contact);
580 empathy_contact_set_name (EmpathyContact *contact,
583 EmpathyContactPriv *priv;
585 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
587 priv = GET_PRIV (contact);
589 g_object_ref (contact);
590 if (tp_strdiff (name, priv->name))
593 priv->name = g_strdup (name);
594 g_object_notify (G_OBJECT (contact), "name");
596 g_object_unref (contact);
600 empathy_contact_get_avatar (EmpathyContact *contact)
602 EmpathyContactPriv *priv;
604 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
606 priv = GET_PRIV (contact);
612 empathy_contact_set_avatar (EmpathyContact *contact,
613 EmpathyAvatar *avatar)
615 EmpathyContactPriv *priv;
617 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
619 priv = GET_PRIV (contact);
621 if (priv->avatar == avatar)
626 empathy_avatar_unref (priv->avatar);
631 priv->avatar = empathy_avatar_ref (avatar);
633 g_object_notify (G_OBJECT (contact), "avatar");
637 empathy_contact_get_account (EmpathyContact *contact)
639 EmpathyContactPriv *priv;
641 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
643 priv = GET_PRIV (contact);
645 if (priv->account == NULL && priv->tp_contact != NULL)
647 TpConnection *connection;
649 /* FIXME: This assume the account manager already exists */
650 connection = tp_contact_get_connection (priv->tp_contact);
652 g_object_ref (empathy_get_account_for_connection (connection));
655 return priv->account;
659 empathy_contact_get_connection (EmpathyContact *contact)
661 EmpathyContactPriv *priv;
663 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
665 priv = GET_PRIV (contact);
667 if (priv->tp_contact != NULL)
668 return tp_contact_get_connection (priv->tp_contact);
673 TpConnectionPresenceType
674 empathy_contact_get_presence (EmpathyContact *contact)
676 EmpathyContactPriv *priv;
678 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
679 TP_CONNECTION_PRESENCE_TYPE_UNSET);
681 priv = GET_PRIV (contact);
683 if (priv->tp_contact != NULL)
684 return tp_contact_get_presence_type (priv->tp_contact);
686 return priv->presence;
690 empathy_contact_set_presence (EmpathyContact *contact,
691 TpConnectionPresenceType presence)
693 EmpathyContactPriv *priv;
694 TpConnectionPresenceType old_presence;
696 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
698 priv = GET_PRIV (contact);
700 if (presence == priv->presence)
703 old_presence = priv->presence;
704 priv->presence = presence;
706 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
708 g_object_notify (G_OBJECT (contact), "presence");
712 empathy_contact_get_presence_message (EmpathyContact *contact)
714 EmpathyContactPriv *priv;
716 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
718 priv = GET_PRIV (contact);
720 if (priv->tp_contact != NULL)
721 return tp_contact_get_presence_message (priv->tp_contact);
723 return priv->presence_message;
727 empathy_contact_set_presence_message (EmpathyContact *contact,
728 const gchar *message)
730 EmpathyContactPriv *priv = GET_PRIV (contact);
732 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
734 if (!tp_strdiff (message, priv->presence_message))
737 g_free (priv->presence_message);
738 priv->presence_message = g_strdup (message);
740 g_object_notify (G_OBJECT (contact), "presence-message");
744 empathy_contact_get_handle (EmpathyContact *contact)
746 EmpathyContactPriv *priv;
748 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
750 priv = GET_PRIV (contact);
752 if (priv->tp_contact != NULL)
753 return tp_contact_get_handle (priv->tp_contact);
759 empathy_contact_set_handle (EmpathyContact *contact,
762 EmpathyContactPriv *priv;
764 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
766 priv = GET_PRIV (contact);
768 g_object_ref (contact);
769 if (handle != priv->handle)
771 priv->handle = handle;
772 g_object_notify (G_OBJECT (contact), "handle");
774 g_object_unref (contact);
778 empathy_contact_get_capabilities (EmpathyContact *contact)
780 EmpathyContactPriv *priv;
782 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
784 priv = GET_PRIV (contact);
786 return priv->capabilities;
790 empathy_contact_set_capabilities (EmpathyContact *contact,
791 EmpathyCapabilities capabilities)
793 EmpathyContactPriv *priv;
795 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
797 priv = GET_PRIV (contact);
799 if (priv->capabilities == capabilities)
802 priv->capabilities = capabilities;
804 g_object_notify (G_OBJECT (contact), "capabilities");
808 empathy_contact_is_user (EmpathyContact *contact)
810 EmpathyContactPriv *priv;
812 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
814 priv = GET_PRIV (contact);
816 return priv->is_user;
820 empathy_contact_set_is_user (EmpathyContact *contact,
823 EmpathyContactPriv *priv;
825 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
827 priv = GET_PRIV (contact);
829 if (priv->is_user == is_user)
832 priv->is_user = is_user;
834 g_object_notify (G_OBJECT (contact), "is-user");
838 empathy_contact_is_online (EmpathyContact *contact)
840 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
842 switch (empathy_contact_get_presence (contact))
844 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
845 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
846 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
854 empathy_contact_get_status (EmpathyContact *contact)
856 const gchar *message;
858 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
860 message = empathy_contact_get_presence_message (contact);
861 if (!EMP_STR_EMPTY (message))
864 return empathy_presence_get_default_message (
865 empathy_contact_get_presence (contact));
869 empathy_contact_can_voip (EmpathyContact *contact)
871 EmpathyContactPriv *priv;
873 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
875 priv = GET_PRIV (contact);
877 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
878 EMPATHY_CAPABILITIES_VIDEO);
882 empathy_contact_can_voip_audio (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_AUDIO;
894 empathy_contact_can_voip_video (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_VIDEO;
906 empathy_contact_can_send_files (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_FT;
918 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
920 EmpathyContactPriv *priv;
922 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
924 priv = GET_PRIV (contact);
926 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
930 contact_get_avatar_filename (EmpathyContact *contact,
936 gchar *token_escaped;
938 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
941 token_escaped = tp_escape_as_identifier (token);
942 account = empathy_contact_get_account (contact);
944 avatar_path = g_build_filename (g_get_user_cache_dir (),
947 tp_account_get_connection_manager (account),
948 tp_account_get_protocol (account),
950 g_mkdir_with_parents (avatar_path, 0700);
952 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
954 g_free (token_escaped);
955 g_free (avatar_path);
961 empathy_contact_load_avatar_data (EmpathyContact *contact,
967 EmpathyAvatar *avatar;
969 GError *error = NULL;
971 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
972 g_return_if_fail (data != NULL);
973 g_return_if_fail (len > 0);
974 g_return_if_fail (format != NULL);
975 g_return_if_fail (!EMP_STR_EMPTY (token));
977 /* Load and set the avatar */
978 filename = contact_get_avatar_filename (contact, token);
979 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
980 g_strdup (token), filename);
981 empathy_contact_set_avatar (contact, avatar);
982 empathy_avatar_unref (avatar);
984 /* Save to cache if not yet in it */
985 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
987 if (!empathy_avatar_save_to_file (avatar, filename, &error))
989 DEBUG ("Failed to save avatar in cache: %s",
990 error ? error->message : "No error given");
991 g_clear_error (&error);
994 DEBUG ("Avatar saved to %s", filename);
999 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1002 EmpathyAvatar *avatar = NULL;
1006 GError *error = NULL;
1008 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1009 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1011 /* Load the avatar from file if it exists */
1012 filename = contact_get_avatar_filename (contact, token);
1013 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1015 if (!g_file_get_contents (filename, &data, &len, &error))
1017 DEBUG ("Failed to load avatar from cache: %s",
1018 error ? error->message : "No error given");
1019 g_clear_error (&error);
1025 DEBUG ("Avatar loaded from %s", filename);
1026 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1028 empathy_contact_set_avatar (contact, avatar);
1029 empathy_avatar_unref (avatar);
1036 return data != NULL;
1040 empathy_avatar_get_type (void)
1042 static GType type_id = 0;
1046 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1047 (GBoxedCopyFunc) empathy_avatar_ref,
1048 (GBoxedFreeFunc) empathy_avatar_unref);
1055 * empathy_avatar_new:
1056 * @data: the avatar data
1057 * @len: the size of avatar data
1058 * @format: the mime type of the avatar image
1059 * @token: the token of the avatar
1060 * @filename: the filename where the avatar is stored in cache
1062 * Create a #EmpathyAvatar from the provided data. This function takes the
1063 * ownership of @data, @format, @token and @filename.
1065 * Returns: a new #EmpathyAvatar
1068 empathy_avatar_new (guchar *data,
1074 EmpathyAvatar *avatar;
1076 avatar = g_slice_new0 (EmpathyAvatar);
1077 avatar->data = data;
1079 avatar->format = format;
1080 avatar->token = token;
1081 avatar->filename = filename;
1082 avatar->refcount = 1;
1088 empathy_avatar_unref (EmpathyAvatar *avatar)
1090 g_return_if_fail (avatar != NULL);
1093 if (avatar->refcount == 0)
1095 g_free (avatar->data);
1096 g_free (avatar->format);
1097 g_free (avatar->token);
1098 g_slice_free (EmpathyAvatar, avatar);
1103 empathy_avatar_ref (EmpathyAvatar *avatar)
1105 g_return_val_if_fail (avatar != NULL, NULL);
1113 * empathy_avatar_save_to_file:
1114 * @avatar: the avatar
1115 * @filename: name of a file to write avatar to
1116 * @error: return location for a GError, or NULL
1118 * Save the avatar to a file named filename
1120 * Returns: %TRUE on success, %FALSE if an error occurred
1123 empathy_avatar_save_to_file (EmpathyAvatar *self,
1124 const gchar *filename,
1127 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1132 * empathy_contact_get_location:
1133 * @contact: an #EmpathyContact
1135 * Returns the user's location if available. The keys are defined in
1136 * empathy-location.h. If the contact doesn't have location
1137 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1138 * you are done with the #GHashTable.
1140 * It is composed of string keys and GValues. Keys are
1141 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1142 * Example: a "city" key would have "Helsinki" as string GValue,
1143 * a "latitude" would have 65.0 as double GValue.
1145 * Returns: a #GHashTable of location values
1148 empathy_contact_get_location (EmpathyContact *contact)
1150 EmpathyContactPriv *priv;
1152 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1154 priv = GET_PRIV (contact);
1156 return priv->location;
1160 * empathy_contact_set_location:
1161 * @contact: an #EmpathyContact
1162 * @location: a #GHashTable of the location
1164 * Sets the user's location based on the location #GHashTable passed.
1165 * It is composed of string keys and GValues. Keys are
1166 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1167 * Example: a "city" key would have "Helsinki" as string GValue,
1168 * a "latitude" would have 65.0 as double GValue.
1171 empathy_contact_set_location (EmpathyContact *contact,
1172 GHashTable *location)
1174 EmpathyContactPriv *priv;
1176 g_return_if_fail (EMPATHY_CONTACT (contact));
1177 g_return_if_fail (location != NULL);
1179 priv = GET_PRIV (contact);
1181 if (priv->location != NULL)
1182 g_hash_table_unref (priv->location);
1184 priv->location = g_hash_table_ref (location);
1186 update_geocode (contact);
1188 g_object_notify (G_OBJECT (contact), "location");
1192 * empathy_contact_equal:
1193 * @contact1: an #EmpathyContact
1194 * @contact2: an #EmpathyContact
1196 * Returns FALSE if one of the contacts is NULL but the other is not.
1197 * Otherwise returns TRUE if both pointer are equal or if they bith
1198 * refer to the same id.
1199 * It's only necessary to call this function if your contact objects
1200 * come from logs where contacts are created dynamically and comparing
1201 * pointers is not enough.
1204 empathy_contact_equal (gconstpointer contact1,
1205 gconstpointer contact2)
1212 if ((contact1 == NULL) != (contact2 == NULL)) {
1215 if (contact1 == contact2) {
1218 c1 = EMPATHY_CONTACT (contact1);
1219 c2 = EMPATHY_CONTACT (contact2);
1220 id1 = empathy_contact_get_id (c1);
1221 id2 = empathy_contact_get_id (c2);
1222 if (!tp_strdiff (id1, id2)) {
1229 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1230 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1232 /* This callback is called by geoclue when it found a position
1233 * for the given address. A position is necessary for a contact
1234 * to show up on the map
1237 geocode_cb (GeoclueGeocode *geocode,
1238 GeocluePositionFields fields,
1242 GeoclueAccuracy *accuracy,
1246 EmpathyContactPriv *priv = GET_PRIV (contact);
1247 GHashTable *new_location;
1249 if (priv->location == NULL)
1254 DEBUG ("Error geocoding location : %s", error->message);
1258 /* No need to change location if we didn't find the position */
1259 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1262 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1265 new_location = tp_asv_new (
1266 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1267 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1270 DEBUG ("\t - Latitude: %f", latitude);
1271 DEBUG ("\t - Longitude: %f", longitude);
1273 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1274 * the values we just set. */
1275 tp_g_hash_table_update (new_location, priv->location,
1276 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1278 /* Set the altitude only if it wasn't defined before */
1279 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1280 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1282 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1284 DEBUG ("\t - Altitude: %f", altitude);
1287 /* Don't change the accuracy as we used an address to get this position */
1288 g_hash_table_unref (priv->location);
1289 priv->location = new_location;
1290 g_object_notify (contact, "location");
1292 g_object_unref (geocode);
1293 g_object_unref (contact);
1297 get_dup_string (GHashTable *location,
1302 value = g_hash_table_lookup (location, key);
1304 return g_value_dup_string (value);
1310 update_geocode (EmpathyContact *contact)
1312 static GeoclueGeocode *geocode;
1314 GHashTable *address;
1315 GHashTable *location;
1317 location = empathy_contact_get_location (contact);
1318 if (location == NULL)
1321 /* No need to search for position if contact published it */
1322 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1323 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1326 if (geocode == NULL)
1328 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1329 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1333 g_object_ref (geocode);
1336 address = geoclue_address_details_new ();
1338 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1341 g_hash_table_insert (address,
1342 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1343 DEBUG ("\t - countrycode: %s", str);
1346 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1349 g_hash_table_insert (address,
1350 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1351 DEBUG ("\t - country: %s", str);
1354 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1357 g_hash_table_insert (address,
1358 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1359 DEBUG ("\t - postalcode: %s", str);
1362 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1365 g_hash_table_insert (address,
1366 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1367 DEBUG ("\t - region: %s", str);
1370 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1373 g_hash_table_insert (address,
1374 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1375 DEBUG ("\t - locality: %s", str);
1378 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1381 g_hash_table_insert (address,
1382 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1383 DEBUG ("\t - street: %s", str);
1386 if (g_hash_table_size (address) > 0)
1388 g_object_ref (contact);
1390 geoclue_geocode_address_to_position_async (geocode, address,
1391 geocode_cb, contact);
1394 g_hash_table_unref (address);
1398 static EmpathyCapabilities
1399 tp_caps_to_capabilities (TpCapabilities *caps)
1401 EmpathyCapabilities capabilities = 0;
1405 classes = tp_capabilities_get_channel_classes (caps);
1407 for (i = 0; i < classes->len; i++)
1409 GValueArray *class_struct;
1410 GHashTable *fixed_prop;
1412 TpHandleType handle_type;
1413 const gchar *chan_type;
1415 class_struct = g_ptr_array_index (classes, i);
1416 tp_value_array_unpack (class_struct, 2,
1420 handle_type = tp_asv_get_uint32 (fixed_prop,
1421 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1422 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1425 chan_type = tp_asv_get_string (fixed_prop,
1426 TP_PROP_CHANNEL_CHANNEL_TYPE);
1428 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1430 capabilities |= EMPATHY_CAPABILITIES_FT;
1432 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1434 capabilities |= EMPATHY_CAPABILITIES_STREAM_TUBE;
1436 else if (!tp_strdiff (chan_type,
1437 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1441 for (j = 0; allowed_prop[j] != NULL; j++)
1443 if (!tp_strdiff (allowed_prop[j],
1444 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1445 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1446 else if (!tp_strdiff (allowed_prop[j],
1447 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1448 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1453 return capabilities;
1457 set_capabilities_from_tp_caps (EmpathyContact *self,
1458 TpCapabilities *caps)
1460 EmpathyCapabilities capabilities;
1465 capabilities = tp_caps_to_capabilities (caps);
1466 empathy_contact_set_capabilities (self, capabilities);