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 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
89 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
100 PROP_PRESENCE_MESSAGE,
112 static guint signals[LAST_SIGNAL];
114 /* TpContact* -> EmpathyContact* */
115 static GHashTable *contacts_table = NULL;
118 tp_contact_notify_cb (TpContact *tp_contact,
122 EmpathyContactPriv *priv = GET_PRIV (contact);
124 /* Forward property notifications */
125 if (!tp_strdiff (param->name, "alias"))
126 g_object_notify (contact, "name");
127 else if (!tp_strdiff (param->name, "presence-type")) {
128 TpConnectionPresenceType presence;
130 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
131 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
133 priv->presence = presence;
134 g_object_notify (contact, "presence");
136 else if (!tp_strdiff (param->name, "presence-message"))
137 g_object_notify (contact, "presence-message");
138 else if (!tp_strdiff (param->name, "identifier"))
139 g_object_notify (contact, "id");
140 else if (!tp_strdiff (param->name, "handle"))
141 g_object_notify (contact, "handle");
142 else if (!tp_strdiff (param->name, "location"))
144 GHashTable *location;
146 location = tp_contact_get_location (tp_contact);
147 /* This will start a geoclue search to find the address if needed */
148 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
150 else if (!tp_strdiff (param->name, "capabilities"))
152 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
153 tp_contact_get_capabilities (tp_contact));
155 else if (!tp_strdiff (param->name, "avatar-file"))
157 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
162 contact_dispose (GObject *object)
164 EmpathyContactPriv *priv = GET_PRIV (object);
166 if (priv->tp_contact)
168 g_hash_table_remove (contacts_table, priv->tp_contact);
169 g_signal_handlers_disconnect_by_func (priv->tp_contact,
170 tp_contact_notify_cb, object);
171 g_object_unref (priv->tp_contact);
173 priv->tp_contact = NULL;
176 g_object_unref (priv->account);
177 priv->account = NULL;
179 if (priv->avatar != NULL)
181 empathy_avatar_unref (priv->avatar);
185 if (priv->location != NULL)
187 g_hash_table_unref (priv->location);
188 priv->location = NULL;
191 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
195 empathy_contact_class_init (EmpathyContactClass *class)
197 GObjectClass *object_class;
199 object_class = G_OBJECT_CLASS (class);
201 object_class->finalize = contact_finalize;
202 object_class->dispose = contact_dispose;
203 object_class->get_property = contact_get_property;
204 object_class->set_property = contact_set_property;
206 g_object_class_install_property (object_class,
208 g_param_spec_object ("tp-contact",
210 "The TpContact associated with the contact",
212 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (object_class,
216 g_param_spec_object ("account",
218 "The account associated with the contact",
220 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (object_class,
224 g_param_spec_string ("id",
226 "String identifying contact",
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (object_class,
232 g_param_spec_string ("name",
234 "The name of the contact",
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (object_class,
240 g_param_spec_boxed ("avatar",
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (object_class,
248 g_param_spec_uint ("presence",
250 "Presence of contact",
251 TP_CONNECTION_PRESENCE_TYPE_UNSET,
252 NUM_TP_CONNECTION_PRESENCE_TYPES,
253 TP_CONNECTION_PRESENCE_TYPE_UNSET,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (object_class,
257 PROP_PRESENCE_MESSAGE,
258 g_param_spec_string ("presence-message",
259 "Contact presence message",
260 "Presence message of contact",
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (object_class,
266 g_param_spec_uint ("handle",
268 "The handle of the contact",
272 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (object_class,
276 g_param_spec_flags ("capabilities",
277 "Contact Capabilities",
278 "Capabilities of the contact",
279 EMPATHY_TYPE_CAPABILITIES,
280 EMPATHY_CAPABILITIES_UNKNOWN,
281 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 g_object_class_install_property (object_class,
285 g_param_spec_boolean ("is-user",
287 "Is contact the user",
289 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (object_class,
294 g_param_spec_boxed ("location",
296 "Physical location of the contact",
298 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
300 signals[PRESENCE_CHANGED] =
301 g_signal_new ("presence-changed",
302 G_TYPE_FROM_CLASS (class),
306 _empathy_marshal_VOID__UINT_UINT,
311 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
315 empathy_contact_init (EmpathyContact *contact)
317 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
318 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
320 contact->priv = priv;
322 priv->location = NULL;
326 contact_finalize (GObject *object)
328 EmpathyContactPriv *priv;
330 priv = GET_PRIV (object);
332 DEBUG ("finalize: %p", object);
336 g_free (priv->presence_message);
338 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
342 set_tp_contact (EmpathyContact *contact,
343 TpContact *tp_contact)
345 EmpathyContactPriv *priv = GET_PRIV (contact);
346 GHashTable *location;
347 TpHandle self_handle;
350 if (tp_contact == NULL)
353 g_assert (priv->tp_contact == NULL);
354 priv->tp_contact = g_object_ref (tp_contact);
355 priv->presence = empathy_contact_get_presence (contact);
357 location = tp_contact_get_location (tp_contact);
358 if (location != NULL)
359 empathy_contact_set_location (contact, location);
361 set_capabilities_from_tp_caps (contact,
362 tp_contact_get_capabilities (tp_contact));
364 contact_set_avatar_from_tp_contact (contact);
366 /* Set is-user property. Note that it could still be the handle is
367 * different from the connection's self handle, in the case the handle
368 * comes from a group interface. */
369 self_handle = tp_connection_get_self_handle (
370 tp_contact_get_connection (tp_contact));
371 handle = tp_contact_get_handle (tp_contact);
372 empathy_contact_set_is_user (contact, self_handle == handle);
374 g_signal_connect (priv->tp_contact, "notify",
375 G_CALLBACK (tp_contact_notify_cb), contact);
379 contact_get_property (GObject *object,
384 EmpathyContact *contact = EMPATHY_CONTACT (object);
388 case PROP_TP_CONTACT:
389 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
392 g_value_set_object (value, empathy_contact_get_account (contact));
395 g_value_set_string (value, empathy_contact_get_id (contact));
398 g_value_set_string (value, empathy_contact_get_name (contact));
401 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
404 g_value_set_uint (value, empathy_contact_get_presence (contact));
406 case PROP_PRESENCE_MESSAGE:
407 g_value_set_string (value, empathy_contact_get_presence_message (contact));
410 g_value_set_uint (value, empathy_contact_get_handle (contact));
412 case PROP_CAPABILITIES:
413 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
416 g_value_set_boolean (value, empathy_contact_is_user (contact));
419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
425 contact_set_property (GObject *object,
430 EmpathyContact *contact = EMPATHY_CONTACT (object);
431 EmpathyContactPriv *priv = GET_PRIV (object);
435 case PROP_TP_CONTACT:
436 set_tp_contact (contact, g_value_get_object (value));
439 g_assert (priv->account == NULL);
440 priv->account = g_value_dup_object (value);
443 empathy_contact_set_id (contact, g_value_get_string (value));
446 empathy_contact_set_name (contact, g_value_get_string (value));
449 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
452 empathy_contact_set_presence (contact, g_value_get_uint (value));
454 case PROP_PRESENCE_MESSAGE:
455 empathy_contact_set_presence_message (contact, g_value_get_string (value));
458 empathy_contact_set_handle (contact, g_value_get_uint (value));
460 case PROP_CAPABILITIES:
461 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
464 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
473 empathy_contact_new (TpContact *tp_contact)
475 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
477 return g_object_new (EMPATHY_TYPE_CONTACT,
478 "tp-contact", tp_contact,
484 empathy_contact_from_tpl_contact (TpAccount *account,
485 TplContact *tpl_contact)
487 EmpathyContact *retval;
490 g_return_val_if_fail (TPL_IS_CONTACT (tpl_contact), NULL);
492 is_user = (TPL_CONTACT_USER == tpl_contact_get_contact_type (tpl_contact));
494 retval = g_object_new (EMPATHY_TYPE_CONTACT,
495 "id", tpl_contact_get_alias (tpl_contact),
496 "name", tpl_contact_get_identifier (tpl_contact),
501 if (!EMP_STR_EMPTY (tpl_contact_get_avatar_token (tpl_contact)))
502 empathy_contact_load_avatar_cache (retval,
503 tpl_contact_get_avatar_token (tpl_contact));
507 #endif /* ENABLE_TPL */
510 empathy_contact_new_for_log (TpAccount *account,
515 g_return_val_if_fail (id != NULL, NULL);
516 g_assert (account != NULL);
518 return g_object_new (EMPATHY_TYPE_CONTACT,
527 empathy_contact_get_tp_contact (EmpathyContact *contact)
529 EmpathyContactPriv *priv;
531 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
533 priv = GET_PRIV (contact);
535 return priv->tp_contact;
539 empathy_contact_get_id (EmpathyContact *contact)
541 EmpathyContactPriv *priv;
543 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
545 priv = GET_PRIV (contact);
547 if (priv->tp_contact != NULL)
548 return tp_contact_get_identifier (priv->tp_contact);
554 empathy_contact_set_id (EmpathyContact *contact,
557 EmpathyContactPriv *priv;
559 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
560 g_return_if_fail (id != NULL);
562 priv = GET_PRIV (contact);
564 /* We temporally ref the contact because it could be destroyed
565 * during the signal emition */
566 g_object_ref (contact);
567 if (tp_strdiff (id, priv->id))
570 priv->id = g_strdup (id);
572 g_object_notify (G_OBJECT (contact), "id");
573 if (EMP_STR_EMPTY (priv->name))
574 g_object_notify (G_OBJECT (contact), "name");
577 g_object_unref (contact);
581 empathy_contact_get_name (EmpathyContact *contact)
583 EmpathyContactPriv *priv;
586 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
588 priv = GET_PRIV (contact);
590 if (priv->tp_contact != NULL)
591 alias = tp_contact_get_alias (priv->tp_contact);
595 if (!EMP_STR_EMPTY (alias))
598 return empathy_contact_get_id (contact);
602 empathy_contact_set_name (EmpathyContact *contact,
605 EmpathyContactPriv *priv;
607 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
609 priv = GET_PRIV (contact);
611 g_object_ref (contact);
612 if (tp_strdiff (name, priv->name))
615 priv->name = g_strdup (name);
616 g_object_notify (G_OBJECT (contact), "name");
618 g_object_unref (contact);
622 empathy_contact_get_avatar (EmpathyContact *contact)
624 EmpathyContactPriv *priv;
626 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
628 priv = GET_PRIV (contact);
634 empathy_contact_set_avatar (EmpathyContact *contact,
635 EmpathyAvatar *avatar)
637 EmpathyContactPriv *priv;
639 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
641 priv = GET_PRIV (contact);
643 if (priv->avatar == avatar)
648 empathy_avatar_unref (priv->avatar);
653 priv->avatar = empathy_avatar_ref (avatar);
655 g_object_notify (G_OBJECT (contact), "avatar");
659 empathy_contact_get_account (EmpathyContact *contact)
661 EmpathyContactPriv *priv;
663 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
665 priv = GET_PRIV (contact);
667 if (priv->account == NULL && priv->tp_contact != NULL)
669 TpConnection *connection;
671 /* FIXME: This assume the account manager already exists */
672 connection = tp_contact_get_connection (priv->tp_contact);
674 g_object_ref (empathy_get_account_for_connection (connection));
677 return priv->account;
681 empathy_contact_get_connection (EmpathyContact *contact)
683 EmpathyContactPriv *priv;
685 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
687 priv = GET_PRIV (contact);
689 if (priv->tp_contact != NULL)
690 return tp_contact_get_connection (priv->tp_contact);
695 TpConnectionPresenceType
696 empathy_contact_get_presence (EmpathyContact *contact)
698 EmpathyContactPriv *priv;
700 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
701 TP_CONNECTION_PRESENCE_TYPE_UNSET);
703 priv = GET_PRIV (contact);
705 if (priv->tp_contact != NULL)
706 return tp_contact_get_presence_type (priv->tp_contact);
708 return priv->presence;
712 empathy_contact_set_presence (EmpathyContact *contact,
713 TpConnectionPresenceType presence)
715 EmpathyContactPriv *priv;
716 TpConnectionPresenceType old_presence;
718 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
720 priv = GET_PRIV (contact);
722 if (presence == priv->presence)
725 old_presence = priv->presence;
726 priv->presence = presence;
728 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
730 g_object_notify (G_OBJECT (contact), "presence");
734 empathy_contact_get_presence_message (EmpathyContact *contact)
736 EmpathyContactPriv *priv;
738 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
740 priv = GET_PRIV (contact);
742 if (priv->tp_contact != NULL)
743 return tp_contact_get_presence_message (priv->tp_contact);
745 return priv->presence_message;
749 empathy_contact_set_presence_message (EmpathyContact *contact,
750 const gchar *message)
752 EmpathyContactPriv *priv = GET_PRIV (contact);
754 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
756 if (!tp_strdiff (message, priv->presence_message))
759 g_free (priv->presence_message);
760 priv->presence_message = g_strdup (message);
762 g_object_notify (G_OBJECT (contact), "presence-message");
766 empathy_contact_get_handle (EmpathyContact *contact)
768 EmpathyContactPriv *priv;
770 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
772 priv = GET_PRIV (contact);
774 if (priv->tp_contact != NULL)
775 return tp_contact_get_handle (priv->tp_contact);
781 empathy_contact_set_handle (EmpathyContact *contact,
784 EmpathyContactPriv *priv;
786 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
788 priv = GET_PRIV (contact);
790 g_object_ref (contact);
791 if (handle != priv->handle)
793 priv->handle = handle;
794 g_object_notify (G_OBJECT (contact), "handle");
796 g_object_unref (contact);
800 empathy_contact_get_capabilities (EmpathyContact *contact)
802 EmpathyContactPriv *priv;
804 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
806 priv = GET_PRIV (contact);
808 return priv->capabilities;
812 empathy_contact_set_capabilities (EmpathyContact *contact,
813 EmpathyCapabilities capabilities)
815 EmpathyContactPriv *priv;
817 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
819 priv = GET_PRIV (contact);
821 if (priv->capabilities == capabilities)
824 priv->capabilities = capabilities;
826 g_object_notify (G_OBJECT (contact), "capabilities");
830 empathy_contact_is_user (EmpathyContact *contact)
832 EmpathyContactPriv *priv;
834 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
836 priv = GET_PRIV (contact);
838 return priv->is_user;
842 empathy_contact_set_is_user (EmpathyContact *contact,
845 EmpathyContactPriv *priv;
847 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
849 priv = GET_PRIV (contact);
851 if (priv->is_user == is_user)
854 priv->is_user = is_user;
856 g_object_notify (G_OBJECT (contact), "is-user");
860 empathy_contact_is_online (EmpathyContact *contact)
862 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
864 switch (empathy_contact_get_presence (contact))
866 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
867 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
868 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
876 empathy_contact_get_status (EmpathyContact *contact)
878 const gchar *message;
880 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
882 message = empathy_contact_get_presence_message (contact);
883 if (!EMP_STR_EMPTY (message))
886 return empathy_presence_get_default_message (
887 empathy_contact_get_presence (contact));
891 empathy_contact_can_voip (EmpathyContact *contact)
893 EmpathyContactPriv *priv;
895 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
897 priv = GET_PRIV (contact);
899 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
900 EMPATHY_CAPABILITIES_VIDEO);
904 empathy_contact_can_voip_audio (EmpathyContact *contact)
906 EmpathyContactPriv *priv;
908 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
910 priv = GET_PRIV (contact);
912 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
916 empathy_contact_can_voip_video (EmpathyContact *contact)
918 EmpathyContactPriv *priv;
920 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
922 priv = GET_PRIV (contact);
924 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
928 empathy_contact_can_send_files (EmpathyContact *contact)
930 EmpathyContactPriv *priv;
932 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
934 priv = GET_PRIV (contact);
936 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
940 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
942 EmpathyContactPriv *priv;
944 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
946 priv = GET_PRIV (contact);
948 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
952 contact_get_avatar_filename (EmpathyContact *contact,
958 gchar *token_escaped;
960 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
963 token_escaped = tp_escape_as_identifier (token);
964 account = empathy_contact_get_account (contact);
966 avatar_path = g_build_filename (g_get_user_cache_dir (),
969 tp_account_get_connection_manager (account),
970 tp_account_get_protocol (account),
972 g_mkdir_with_parents (avatar_path, 0700);
974 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
976 g_free (token_escaped);
977 g_free (avatar_path);
983 empathy_contact_load_avatar_cache (EmpathyContact *contact,
986 EmpathyAvatar *avatar = NULL;
990 GError *error = NULL;
992 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
993 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
995 /* Load the avatar from file if it exists */
996 filename = contact_get_avatar_filename (contact, token);
997 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
999 if (!g_file_get_contents (filename, &data, &len, &error))
1001 DEBUG ("Failed to load avatar from cache: %s",
1002 error ? error->message : "No error given");
1003 g_clear_error (&error);
1009 DEBUG ("Avatar loaded from %s", filename);
1010 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1012 empathy_contact_set_avatar (contact, avatar);
1013 empathy_avatar_unref (avatar);
1020 return data != NULL;
1024 empathy_avatar_get_type (void)
1026 static GType type_id = 0;
1030 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1031 (GBoxedCopyFunc) empathy_avatar_ref,
1032 (GBoxedFreeFunc) empathy_avatar_unref);
1039 * empathy_avatar_new:
1040 * @data: the avatar data
1041 * @len: the size of avatar data
1042 * @format: the mime type of the avatar image
1043 * @token: the token of the avatar
1044 * @filename: the filename where the avatar is stored in cache
1046 * Create a #EmpathyAvatar from the provided data. This function takes the
1047 * ownership of @data, @format, @token and @filename.
1049 * Returns: a new #EmpathyAvatar
1052 empathy_avatar_new (guchar *data,
1058 EmpathyAvatar *avatar;
1060 avatar = g_slice_new0 (EmpathyAvatar);
1061 avatar->data = data;
1063 avatar->format = format;
1064 avatar->token = token;
1065 avatar->filename = filename;
1066 avatar->refcount = 1;
1072 empathy_avatar_unref (EmpathyAvatar *avatar)
1074 g_return_if_fail (avatar != NULL);
1077 if (avatar->refcount == 0)
1079 g_free (avatar->data);
1080 g_free (avatar->format);
1081 g_free (avatar->token);
1082 g_slice_free (EmpathyAvatar, avatar);
1087 empathy_avatar_ref (EmpathyAvatar *avatar)
1089 g_return_val_if_fail (avatar != NULL, NULL);
1097 * empathy_avatar_save_to_file:
1098 * @avatar: the avatar
1099 * @filename: name of a file to write avatar to
1100 * @error: return location for a GError, or NULL
1102 * Save the avatar to a file named filename
1104 * Returns: %TRUE on success, %FALSE if an error occurred
1107 empathy_avatar_save_to_file (EmpathyAvatar *self,
1108 const gchar *filename,
1111 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1116 * empathy_contact_get_location:
1117 * @contact: an #EmpathyContact
1119 * Returns the user's location if available. The keys are defined in
1120 * empathy-location.h. If the contact doesn't have location
1121 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1122 * you are done with the #GHashTable.
1124 * It is composed of string keys and GValues. Keys are
1125 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1126 * Example: a "city" key would have "Helsinki" as string GValue,
1127 * a "latitude" would have 65.0 as double GValue.
1129 * Returns: a #GHashTable of location values
1132 empathy_contact_get_location (EmpathyContact *contact)
1134 EmpathyContactPriv *priv;
1136 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1138 priv = GET_PRIV (contact);
1140 return priv->location;
1144 * empathy_contact_set_location:
1145 * @contact: an #EmpathyContact
1146 * @location: a #GHashTable of the location
1148 * Sets the user's location based on the location #GHashTable passed.
1149 * It is composed of string keys and GValues. Keys are
1150 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1151 * Example: a "city" key would have "Helsinki" as string GValue,
1152 * a "latitude" would have 65.0 as double GValue.
1155 empathy_contact_set_location (EmpathyContact *contact,
1156 GHashTable *location)
1158 EmpathyContactPriv *priv;
1160 g_return_if_fail (EMPATHY_CONTACT (contact));
1161 g_return_if_fail (location != NULL);
1163 priv = GET_PRIV (contact);
1165 if (priv->location != NULL)
1166 g_hash_table_unref (priv->location);
1168 priv->location = g_hash_table_ref (location);
1170 update_geocode (contact);
1172 g_object_notify (G_OBJECT (contact), "location");
1176 * empathy_contact_equal:
1177 * @contact1: an #EmpathyContact
1178 * @contact2: an #EmpathyContact
1180 * Returns FALSE if one of the contacts is NULL but the other is not.
1181 * Otherwise returns TRUE if both pointer are equal or if they bith
1182 * refer to the same id.
1183 * It's only necessary to call this function if your contact objects
1184 * come from logs where contacts are created dynamically and comparing
1185 * pointers is not enough.
1188 empathy_contact_equal (gconstpointer contact1,
1189 gconstpointer contact2)
1196 if ((contact1 == NULL) != (contact2 == NULL)) {
1199 if (contact1 == contact2) {
1202 c1 = EMPATHY_CONTACT (contact1);
1203 c2 = EMPATHY_CONTACT (contact2);
1204 id1 = empathy_contact_get_id (c1);
1205 id2 = empathy_contact_get_id (c2);
1206 if (!tp_strdiff (id1, id2)) {
1213 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1214 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1216 /* This callback is called by geoclue when it found a position
1217 * for the given address. A position is necessary for a contact
1218 * to show up on the map
1221 geocode_cb (GeoclueGeocode *geocode,
1222 GeocluePositionFields fields,
1226 GeoclueAccuracy *accuracy,
1230 EmpathyContactPriv *priv = GET_PRIV (contact);
1231 GHashTable *new_location;
1233 if (priv->location == NULL)
1238 DEBUG ("Error geocoding location : %s", error->message);
1242 /* No need to change location if we didn't find the position */
1243 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1246 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1249 new_location = tp_asv_new (
1250 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1251 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1254 DEBUG ("\t - Latitude: %f", latitude);
1255 DEBUG ("\t - Longitude: %f", longitude);
1257 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1258 * the values we just set. */
1259 tp_g_hash_table_update (new_location, priv->location,
1260 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1262 /* Set the altitude only if it wasn't defined before */
1263 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1264 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1266 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1268 DEBUG ("\t - Altitude: %f", altitude);
1271 /* Don't change the accuracy as we used an address to get this position */
1272 g_hash_table_unref (priv->location);
1273 priv->location = new_location;
1274 g_object_notify (contact, "location");
1276 g_object_unref (geocode);
1277 g_object_unref (contact);
1281 get_dup_string (GHashTable *location,
1286 value = g_hash_table_lookup (location, key);
1288 return g_value_dup_string (value);
1294 update_geocode (EmpathyContact *contact)
1296 static GeoclueGeocode *geocode;
1298 GHashTable *address;
1299 GHashTable *location;
1301 location = empathy_contact_get_location (contact);
1302 if (location == NULL)
1305 /* No need to search for position if contact published it */
1306 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1307 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1310 if (geocode == NULL)
1312 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1313 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1317 g_object_ref (geocode);
1320 address = geoclue_address_details_new ();
1322 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1325 g_hash_table_insert (address,
1326 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1327 DEBUG ("\t - countrycode: %s", str);
1330 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1333 g_hash_table_insert (address,
1334 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1335 DEBUG ("\t - country: %s", str);
1338 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1341 g_hash_table_insert (address,
1342 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1343 DEBUG ("\t - postalcode: %s", str);
1346 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1349 g_hash_table_insert (address,
1350 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1351 DEBUG ("\t - region: %s", str);
1354 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1357 g_hash_table_insert (address,
1358 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1359 DEBUG ("\t - locality: %s", str);
1362 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1365 g_hash_table_insert (address,
1366 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1367 DEBUG ("\t - street: %s", str);
1370 if (g_hash_table_size (address) > 0)
1372 g_object_ref (contact);
1374 geoclue_geocode_address_to_position_async (geocode, address,
1375 geocode_cb, contact);
1378 g_hash_table_unref (address);
1382 static EmpathyCapabilities
1383 tp_caps_to_capabilities (TpCapabilities *caps)
1385 EmpathyCapabilities capabilities = 0;
1389 classes = tp_capabilities_get_channel_classes (caps);
1391 for (i = 0; i < classes->len; i++)
1393 GValueArray *class_struct;
1394 GHashTable *fixed_prop;
1396 TpHandleType handle_type;
1397 const gchar *chan_type;
1399 class_struct = g_ptr_array_index (classes, i);
1400 tp_value_array_unpack (class_struct, 2,
1404 handle_type = tp_asv_get_uint32 (fixed_prop,
1405 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1406 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1409 chan_type = tp_asv_get_string (fixed_prop,
1410 TP_PROP_CHANNEL_CHANNEL_TYPE);
1412 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1414 capabilities |= EMPATHY_CAPABILITIES_FT;
1416 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1418 capabilities |= EMPATHY_CAPABILITIES_STREAM_TUBE;
1420 else if (!tp_strdiff (chan_type,
1421 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1425 for (j = 0; allowed_prop[j] != NULL; j++)
1427 if (!tp_strdiff (allowed_prop[j],
1428 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1429 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1430 else if (!tp_strdiff (allowed_prop[j],
1431 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1432 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1437 return capabilities;
1441 set_capabilities_from_tp_caps (EmpathyContact *self,
1442 TpCapabilities *caps)
1444 EmpathyCapabilities capabilities;
1449 capabilities = tp_caps_to_capabilities (caps);
1450 empathy_contact_set_capabilities (self, capabilities);
1454 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1456 EmpathyContactPriv *priv = GET_PRIV (contact);
1461 token = tp_contact_get_avatar_token (priv->tp_contact);
1462 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1463 file = tp_contact_get_avatar_file (priv->tp_contact);
1467 EmpathyAvatar *avatar;
1471 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1472 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1473 g_file_get_path (file));
1474 empathy_contact_set_avatar (contact, avatar);
1475 empathy_avatar_unref (avatar);
1479 empathy_contact_set_avatar (contact, NULL);
1484 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1486 EmpathyContact *contact = NULL;
1488 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1490 if (contacts_table == NULL)
1491 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1493 contact = g_hash_table_lookup (contacts_table, tp_contact);
1495 if (contact == NULL)
1497 contact = empathy_contact_new (tp_contact);
1499 /* The hash table does not keep any ref.
1500 * contact keeps a ref to tp_contact, and is removed from the table in
1501 * contact_dispose() */
1502 g_hash_table_insert (contacts_table, tp_contact, contact);
1506 g_object_ref (contact);