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>
33 #include <geoclue/geoclue-geocode.h>
36 #include "empathy-contact.h"
37 #include "empathy-utils.h"
38 #include "empathy-enum-types.h"
39 #include "empathy-marshal.h"
40 #include "empathy-location.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
43 #include "empathy-debug.h"
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
47 TpContact *tp_contact;
51 EmpathyAvatar *avatar;
52 TpConnectionPresenceType presence;
53 gchar *presence_message;
55 EmpathyCapabilities capabilities;
58 /* Location is composed of string keys and GValues.
59 * Example: a "city" key would have "Helsinki" as string GValue,
60 * a "latitude" would have 65.0 as double GValue.
62 * This is a super set of the location stored in TpContact as we can try add
63 * more fields by searching the address using geoclue.
68 static void contact_finalize (GObject *object);
69 static void contact_get_property (GObject *object, guint param_id,
70 GValue *value, GParamSpec *pspec);
71 static void contact_set_property (GObject *object, guint param_id,
72 const GValue *value, GParamSpec *pspec);
75 static void update_geocode (EmpathyContact *contact);
78 static void empathy_contact_set_location (EmpathyContact *contact,
79 GHashTable *location);
81 static void set_capabilities_from_tp_caps (EmpathyContact *self,
82 TpCapabilities *caps);
84 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
86 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
97 PROP_PRESENCE_MESSAGE,
109 static guint signals[LAST_SIGNAL];
111 /* TpContact* -> EmpathyContact*, both borrowed ref */
112 static GHashTable *contacts_table = NULL;
115 tp_contact_notify_cb (TpContact *tp_contact,
119 EmpathyContactPriv *priv = GET_PRIV (contact);
121 /* Forward property notifications */
122 if (!tp_strdiff (param->name, "alias"))
123 g_object_notify (contact, "name");
124 else if (!tp_strdiff (param->name, "presence-type")) {
125 TpConnectionPresenceType presence;
127 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
128 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
130 priv->presence = presence;
131 g_object_notify (contact, "presence");
133 else if (!tp_strdiff (param->name, "presence-message"))
134 g_object_notify (contact, "presence-message");
135 else if (!tp_strdiff (param->name, "identifier"))
136 g_object_notify (contact, "id");
137 else if (!tp_strdiff (param->name, "handle"))
138 g_object_notify (contact, "handle");
139 else if (!tp_strdiff (param->name, "location"))
141 GHashTable *location;
143 location = tp_contact_get_location (tp_contact);
144 /* This will start a geoclue search to find the address if needed */
145 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
147 else if (!tp_strdiff (param->name, "capabilities"))
149 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
150 tp_contact_get_capabilities (tp_contact));
152 else if (!tp_strdiff (param->name, "avatar-file"))
154 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
159 contact_dispose (GObject *object)
161 EmpathyContactPriv *priv = GET_PRIV (object);
163 if (priv->tp_contact)
165 g_hash_table_remove (contacts_table, priv->tp_contact);
166 g_signal_handlers_disconnect_by_func (priv->tp_contact,
167 tp_contact_notify_cb, object);
168 g_object_unref (priv->tp_contact);
170 priv->tp_contact = NULL;
173 g_object_unref (priv->account);
174 priv->account = NULL;
176 if (priv->avatar != NULL)
178 empathy_avatar_unref (priv->avatar);
182 if (priv->location != NULL)
184 g_hash_table_unref (priv->location);
185 priv->location = NULL;
188 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
192 empathy_contact_class_init (EmpathyContactClass *class)
194 GObjectClass *object_class;
196 object_class = G_OBJECT_CLASS (class);
198 object_class->finalize = contact_finalize;
199 object_class->dispose = contact_dispose;
200 object_class->get_property = contact_get_property;
201 object_class->set_property = contact_set_property;
203 g_object_class_install_property (object_class,
205 g_param_spec_object ("tp-contact",
207 "The TpContact associated with the contact",
209 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (object_class,
213 g_param_spec_object ("account",
215 "The account associated with the contact",
217 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (object_class,
221 g_param_spec_string ("id",
223 "String identifying contact",
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (object_class,
229 g_param_spec_string ("name",
231 "The name of the contact",
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (object_class,
237 g_param_spec_boxed ("avatar",
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (object_class,
245 g_param_spec_uint ("presence",
247 "Presence of contact",
248 TP_CONNECTION_PRESENCE_TYPE_UNSET,
249 NUM_TP_CONNECTION_PRESENCE_TYPES,
250 TP_CONNECTION_PRESENCE_TYPE_UNSET,
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (object_class,
254 PROP_PRESENCE_MESSAGE,
255 g_param_spec_string ("presence-message",
256 "Contact presence message",
257 "Presence message of contact",
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (object_class,
263 g_param_spec_uint ("handle",
265 "The handle of the contact",
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (object_class,
273 g_param_spec_flags ("capabilities",
274 "Contact Capabilities",
275 "Capabilities of the contact",
276 EMPATHY_TYPE_CAPABILITIES,
277 EMPATHY_CAPABILITIES_UNKNOWN,
278 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (object_class,
282 g_param_spec_boolean ("is-user",
284 "Is contact the user",
286 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (object_class,
291 g_param_spec_boxed ("location",
293 "Physical location of the contact",
295 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
297 signals[PRESENCE_CHANGED] =
298 g_signal_new ("presence-changed",
299 G_TYPE_FROM_CLASS (class),
303 _empathy_marshal_VOID__UINT_UINT,
308 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
312 empathy_contact_init (EmpathyContact *contact)
314 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
315 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
317 contact->priv = priv;
319 priv->location = NULL;
323 contact_finalize (GObject *object)
325 EmpathyContactPriv *priv;
327 priv = GET_PRIV (object);
329 DEBUG ("finalize: %p", object);
333 g_free (priv->presence_message);
335 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
339 set_tp_contact (EmpathyContact *contact,
340 TpContact *tp_contact)
342 EmpathyContactPriv *priv = GET_PRIV (contact);
343 GHashTable *location;
344 TpHandle self_handle;
347 if (tp_contact == NULL)
350 g_assert (priv->tp_contact == NULL);
351 priv->tp_contact = g_object_ref (tp_contact);
352 priv->presence = empathy_contact_get_presence (contact);
354 location = tp_contact_get_location (tp_contact);
355 if (location != NULL)
356 empathy_contact_set_location (contact, location);
358 set_capabilities_from_tp_caps (contact,
359 tp_contact_get_capabilities (tp_contact));
361 contact_set_avatar_from_tp_contact (contact);
363 /* Set is-user property. Note that it could still be the handle is
364 * different from the connection's self handle, in the case the handle
365 * comes from a group interface. */
366 self_handle = tp_connection_get_self_handle (
367 tp_contact_get_connection (tp_contact));
368 handle = tp_contact_get_handle (tp_contact);
369 empathy_contact_set_is_user (contact, self_handle == handle);
371 g_signal_connect (priv->tp_contact, "notify",
372 G_CALLBACK (tp_contact_notify_cb), contact);
376 contact_get_property (GObject *object,
381 EmpathyContact *contact = EMPATHY_CONTACT (object);
385 case PROP_TP_CONTACT:
386 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
389 g_value_set_object (value, empathy_contact_get_account (contact));
392 g_value_set_string (value, empathy_contact_get_id (contact));
395 g_value_set_string (value, empathy_contact_get_name (contact));
398 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
401 g_value_set_uint (value, empathy_contact_get_presence (contact));
403 case PROP_PRESENCE_MESSAGE:
404 g_value_set_string (value, empathy_contact_get_presence_message (contact));
407 g_value_set_uint (value, empathy_contact_get_handle (contact));
409 case PROP_CAPABILITIES:
410 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
413 g_value_set_boolean (value, empathy_contact_is_user (contact));
416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
422 contact_set_property (GObject *object,
427 EmpathyContact *contact = EMPATHY_CONTACT (object);
428 EmpathyContactPriv *priv = GET_PRIV (object);
432 case PROP_TP_CONTACT:
433 set_tp_contact (contact, g_value_get_object (value));
436 g_assert (priv->account == NULL);
437 priv->account = g_value_dup_object (value);
440 empathy_contact_set_id (contact, g_value_get_string (value));
443 empathy_contact_set_name (contact, g_value_get_string (value));
446 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
449 empathy_contact_set_presence (contact, g_value_get_uint (value));
451 case PROP_PRESENCE_MESSAGE:
452 empathy_contact_set_presence_message (contact, g_value_get_string (value));
455 empathy_contact_set_handle (contact, g_value_get_uint (value));
457 case PROP_CAPABILITIES:
458 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
461 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
464 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
470 empathy_contact_new (TpContact *tp_contact)
472 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
474 return g_object_new (EMPATHY_TYPE_CONTACT,
475 "tp-contact", tp_contact,
480 empathy_contact_from_tpl_contact (TpAccount *account,
481 TplEntity *tpl_entity)
483 EmpathyContact *retval;
486 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
488 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
490 retval = g_object_new (EMPATHY_TYPE_CONTACT,
491 "id", tpl_entity_get_alias (tpl_entity),
492 "name", tpl_entity_get_identifier (tpl_entity),
497 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
498 empathy_contact_load_avatar_cache (retval,
499 tpl_entity_get_avatar_token (tpl_entity));
505 empathy_contact_new_for_log (TpAccount *account,
510 g_return_val_if_fail (id != NULL, NULL);
511 g_assert (account != NULL);
513 return g_object_new (EMPATHY_TYPE_CONTACT,
522 empathy_contact_get_tp_contact (EmpathyContact *contact)
524 EmpathyContactPriv *priv;
526 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
528 priv = GET_PRIV (contact);
530 return priv->tp_contact;
534 empathy_contact_get_id (EmpathyContact *contact)
536 EmpathyContactPriv *priv;
538 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
540 priv = GET_PRIV (contact);
542 if (priv->tp_contact != NULL)
543 return tp_contact_get_identifier (priv->tp_contact);
549 empathy_contact_set_id (EmpathyContact *contact,
552 EmpathyContactPriv *priv;
554 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
555 g_return_if_fail (id != NULL);
557 priv = GET_PRIV (contact);
559 /* We temporally ref the contact because it could be destroyed
560 * during the signal emition */
561 g_object_ref (contact);
562 if (tp_strdiff (id, priv->id))
565 priv->id = g_strdup (id);
567 g_object_notify (G_OBJECT (contact), "id");
568 if (EMP_STR_EMPTY (priv->name))
569 g_object_notify (G_OBJECT (contact), "name");
572 g_object_unref (contact);
576 empathy_contact_get_name (EmpathyContact *contact)
578 EmpathyContactPriv *priv;
581 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
583 priv = GET_PRIV (contact);
585 if (priv->tp_contact != NULL)
586 alias = tp_contact_get_alias (priv->tp_contact);
590 if (!EMP_STR_EMPTY (alias))
593 return empathy_contact_get_id (contact);
597 empathy_contact_set_name (EmpathyContact *contact,
600 EmpathyContactPriv *priv;
602 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
604 priv = GET_PRIV (contact);
606 g_object_ref (contact);
607 if (tp_strdiff (name, priv->name))
610 priv->name = g_strdup (name);
611 g_object_notify (G_OBJECT (contact), "name");
613 g_object_unref (contact);
617 contact_set_aliases_cb (TpConnection *connection,
620 GObject *weak_object)
623 DEBUG ("Error: %s", error->message);
627 empathy_contact_set_alias (EmpathyContact *contact,
630 TpConnection *connection;
631 GHashTable *new_alias;
634 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
636 handle = empathy_contact_get_handle (contact);
638 DEBUG ("Setting alias for contact %s (%d) to %s",
639 empathy_contact_get_id (contact),
642 new_alias = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
645 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), g_strdup (alias));
647 connection = empathy_contact_get_connection (contact);
648 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
649 new_alias, contact_set_aliases_cb, NULL, NULL, NULL);
651 g_hash_table_destroy (new_alias);
655 empathy_contact_get_avatar (EmpathyContact *contact)
657 EmpathyContactPriv *priv;
659 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
661 priv = GET_PRIV (contact);
667 empathy_contact_set_avatar (EmpathyContact *contact,
668 EmpathyAvatar *avatar)
670 EmpathyContactPriv *priv;
672 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
674 priv = GET_PRIV (contact);
676 if (priv->avatar == avatar)
681 empathy_avatar_unref (priv->avatar);
686 priv->avatar = empathy_avatar_ref (avatar);
688 g_object_notify (G_OBJECT (contact), "avatar");
692 empathy_contact_get_account (EmpathyContact *contact)
694 EmpathyContactPriv *priv;
696 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
698 priv = GET_PRIV (contact);
700 if (priv->account == NULL && priv->tp_contact != NULL)
702 TpConnection *connection;
704 /* FIXME: This assume the account manager already exists */
705 connection = tp_contact_get_connection (priv->tp_contact);
707 g_object_ref (empathy_get_account_for_connection (connection));
710 return priv->account;
714 empathy_contact_get_connection (EmpathyContact *contact)
716 EmpathyContactPriv *priv;
718 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
720 priv = GET_PRIV (contact);
722 if (priv->tp_contact != NULL)
723 return tp_contact_get_connection (priv->tp_contact);
728 TpConnectionPresenceType
729 empathy_contact_get_presence (EmpathyContact *contact)
731 EmpathyContactPriv *priv;
733 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
734 TP_CONNECTION_PRESENCE_TYPE_UNSET);
736 priv = GET_PRIV (contact);
738 if (priv->tp_contact != NULL)
739 return tp_contact_get_presence_type (priv->tp_contact);
741 return priv->presence;
745 empathy_contact_set_presence (EmpathyContact *contact,
746 TpConnectionPresenceType presence)
748 EmpathyContactPriv *priv;
749 TpConnectionPresenceType old_presence;
751 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
753 priv = GET_PRIV (contact);
755 if (presence == priv->presence)
758 old_presence = priv->presence;
759 priv->presence = presence;
761 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
763 g_object_notify (G_OBJECT (contact), "presence");
767 empathy_contact_get_presence_message (EmpathyContact *contact)
769 EmpathyContactPriv *priv;
771 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
773 priv = GET_PRIV (contact);
775 if (priv->tp_contact != NULL)
776 return tp_contact_get_presence_message (priv->tp_contact);
778 return priv->presence_message;
782 empathy_contact_set_presence_message (EmpathyContact *contact,
783 const gchar *message)
785 EmpathyContactPriv *priv = GET_PRIV (contact);
787 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
789 if (!tp_strdiff (message, priv->presence_message))
792 g_free (priv->presence_message);
793 priv->presence_message = g_strdup (message);
795 g_object_notify (G_OBJECT (contact), "presence-message");
799 empathy_contact_get_handle (EmpathyContact *contact)
801 EmpathyContactPriv *priv;
803 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
805 priv = GET_PRIV (contact);
807 if (priv->tp_contact != NULL)
808 return tp_contact_get_handle (priv->tp_contact);
814 empathy_contact_set_handle (EmpathyContact *contact,
817 EmpathyContactPriv *priv;
819 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
821 priv = GET_PRIV (contact);
823 g_object_ref (contact);
824 if (handle != priv->handle)
826 priv->handle = handle;
827 g_object_notify (G_OBJECT (contact), "handle");
829 g_object_unref (contact);
833 empathy_contact_get_capabilities (EmpathyContact *contact)
835 EmpathyContactPriv *priv;
837 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
839 priv = GET_PRIV (contact);
841 return priv->capabilities;
845 empathy_contact_set_capabilities (EmpathyContact *contact,
846 EmpathyCapabilities capabilities)
848 EmpathyContactPriv *priv;
850 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
852 priv = GET_PRIV (contact);
854 if (priv->capabilities == capabilities)
857 priv->capabilities = capabilities;
859 g_object_notify (G_OBJECT (contact), "capabilities");
863 empathy_contact_is_user (EmpathyContact *contact)
865 EmpathyContactPriv *priv;
867 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
869 priv = GET_PRIV (contact);
871 return priv->is_user;
875 empathy_contact_set_is_user (EmpathyContact *contact,
878 EmpathyContactPriv *priv;
880 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
882 priv = GET_PRIV (contact);
884 if (priv->is_user == is_user)
887 priv->is_user = is_user;
889 g_object_notify (G_OBJECT (contact), "is-user");
893 empathy_contact_is_online (EmpathyContact *contact)
895 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
897 switch (empathy_contact_get_presence (contact))
899 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
900 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
901 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
909 empathy_contact_get_status (EmpathyContact *contact)
911 const gchar *message;
913 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
915 message = empathy_contact_get_presence_message (contact);
916 if (!EMP_STR_EMPTY (message))
919 return empathy_presence_get_default_message (
920 empathy_contact_get_presence (contact));
924 empathy_contact_can_voip (EmpathyContact *contact)
926 EmpathyContactPriv *priv;
928 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
930 priv = GET_PRIV (contact);
932 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
933 EMPATHY_CAPABILITIES_VIDEO);
937 empathy_contact_can_voip_audio (EmpathyContact *contact)
939 EmpathyContactPriv *priv;
941 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
943 priv = GET_PRIV (contact);
945 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
949 empathy_contact_can_voip_video (EmpathyContact *contact)
951 EmpathyContactPriv *priv;
953 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
955 priv = GET_PRIV (contact);
957 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
961 empathy_contact_can_send_files (EmpathyContact *contact)
963 EmpathyContactPriv *priv;
965 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
967 priv = GET_PRIV (contact);
969 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
973 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
975 EmpathyContactPriv *priv;
977 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
979 priv = GET_PRIV (contact);
981 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
985 contact_get_avatar_filename (EmpathyContact *contact,
991 gchar *token_escaped;
993 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
996 token_escaped = tp_escape_as_identifier (token);
997 account = empathy_contact_get_account (contact);
999 avatar_path = g_build_filename (g_get_user_cache_dir (),
1002 tp_account_get_connection_manager (account),
1003 tp_account_get_protocol (account),
1005 g_mkdir_with_parents (avatar_path, 0700);
1007 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1009 g_free (token_escaped);
1010 g_free (avatar_path);
1016 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1019 EmpathyAvatar *avatar = NULL;
1023 GError *error = NULL;
1025 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1026 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1028 /* Load the avatar from file if it exists */
1029 filename = contact_get_avatar_filename (contact, token);
1030 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1032 if (!g_file_get_contents (filename, &data, &len, &error))
1034 DEBUG ("Failed to load avatar from cache: %s",
1035 error ? error->message : "No error given");
1036 g_clear_error (&error);
1042 DEBUG ("Avatar loaded from %s", filename);
1043 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1045 empathy_contact_set_avatar (contact, avatar);
1046 empathy_avatar_unref (avatar);
1053 return data != NULL;
1057 empathy_avatar_get_type (void)
1059 static GType type_id = 0;
1063 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1064 (GBoxedCopyFunc) empathy_avatar_ref,
1065 (GBoxedFreeFunc) empathy_avatar_unref);
1072 * empathy_avatar_new:
1073 * @data: the avatar data
1074 * @len: the size of avatar data
1075 * @format: the mime type of the avatar image
1076 * @token: the token of the avatar
1077 * @filename: the filename where the avatar is stored in cache
1079 * Create a #EmpathyAvatar from the provided data. This function takes the
1080 * ownership of @data, @format, @token and @filename.
1082 * Returns: a new #EmpathyAvatar
1085 empathy_avatar_new (guchar *data,
1091 EmpathyAvatar *avatar;
1093 avatar = g_slice_new0 (EmpathyAvatar);
1094 avatar->data = data;
1096 avatar->format = format;
1097 avatar->token = token;
1098 avatar->filename = filename;
1099 avatar->refcount = 1;
1105 empathy_avatar_unref (EmpathyAvatar *avatar)
1107 g_return_if_fail (avatar != NULL);
1110 if (avatar->refcount == 0)
1112 g_free (avatar->data);
1113 g_free (avatar->format);
1114 g_free (avatar->token);
1115 g_slice_free (EmpathyAvatar, avatar);
1120 empathy_avatar_ref (EmpathyAvatar *avatar)
1122 g_return_val_if_fail (avatar != NULL, NULL);
1130 * empathy_avatar_save_to_file:
1131 * @avatar: the avatar
1132 * @filename: name of a file to write avatar to
1133 * @error: return location for a GError, or NULL
1135 * Save the avatar to a file named filename
1137 * Returns: %TRUE on success, %FALSE if an error occurred
1140 empathy_avatar_save_to_file (EmpathyAvatar *self,
1141 const gchar *filename,
1144 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1149 * empathy_contact_get_location:
1150 * @contact: an #EmpathyContact
1152 * Returns the user's location if available. The keys are defined in
1153 * empathy-location.h. If the contact doesn't have location
1154 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1155 * you are done with the #GHashTable.
1157 * It is composed of string keys and GValues. Keys are
1158 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1159 * Example: a "city" key would have "Helsinki" as string GValue,
1160 * a "latitude" would have 65.0 as double GValue.
1162 * Returns: a #GHashTable of location values
1165 empathy_contact_get_location (EmpathyContact *contact)
1167 EmpathyContactPriv *priv;
1169 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1171 priv = GET_PRIV (contact);
1173 return priv->location;
1177 * empathy_contact_set_location:
1178 * @contact: an #EmpathyContact
1179 * @location: a #GHashTable of the location
1181 * Sets the user's location based on the location #GHashTable passed.
1182 * It is composed of string keys and GValues. Keys are
1183 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1184 * Example: a "city" key would have "Helsinki" as string GValue,
1185 * a "latitude" would have 65.0 as double GValue.
1188 empathy_contact_set_location (EmpathyContact *contact,
1189 GHashTable *location)
1191 EmpathyContactPriv *priv;
1193 g_return_if_fail (EMPATHY_CONTACT (contact));
1194 g_return_if_fail (location != NULL);
1196 priv = GET_PRIV (contact);
1198 if (priv->location != NULL)
1199 g_hash_table_unref (priv->location);
1201 priv->location = g_hash_table_ref (location);
1203 update_geocode (contact);
1205 g_object_notify (G_OBJECT (contact), "location");
1209 * empathy_contact_equal:
1210 * @contact1: an #EmpathyContact
1211 * @contact2: an #EmpathyContact
1213 * Returns FALSE if one of the contacts is NULL but the other is not.
1214 * Otherwise returns TRUE if both pointer are equal or if they bith
1215 * refer to the same id.
1216 * It's only necessary to call this function if your contact objects
1217 * come from logs where contacts are created dynamically and comparing
1218 * pointers is not enough.
1221 empathy_contact_equal (gconstpointer contact1,
1222 gconstpointer contact2)
1229 if ((contact1 == NULL) != (contact2 == NULL)) {
1232 if (contact1 == contact2) {
1235 c1 = EMPATHY_CONTACT (contact1);
1236 c2 = EMPATHY_CONTACT (contact2);
1237 id1 = empathy_contact_get_id (c1);
1238 id2 = empathy_contact_get_id (c2);
1239 if (!tp_strdiff (id1, id2)) {
1246 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1247 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1249 /* This callback is called by geoclue when it found a position
1250 * for the given address. A position is necessary for a contact
1251 * to show up on the map
1254 geocode_cb (GeoclueGeocode *geocode,
1255 GeocluePositionFields fields,
1259 GeoclueAccuracy *accuracy,
1263 EmpathyContactPriv *priv = GET_PRIV (contact);
1264 GHashTable *new_location;
1266 if (priv->location == NULL)
1271 DEBUG ("Error geocoding location : %s", error->message);
1275 /* No need to change location if we didn't find the position */
1276 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1279 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1282 new_location = tp_asv_new (
1283 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1284 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1287 DEBUG ("\t - Latitude: %f", latitude);
1288 DEBUG ("\t - Longitude: %f", longitude);
1290 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1291 * the values we just set. */
1292 tp_g_hash_table_update (new_location, priv->location,
1293 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1295 /* Set the altitude only if it wasn't defined before */
1296 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1297 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1299 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1301 DEBUG ("\t - Altitude: %f", altitude);
1304 /* Don't change the accuracy as we used an address to get this position */
1305 g_hash_table_unref (priv->location);
1306 priv->location = new_location;
1307 g_object_notify (contact, "location");
1309 g_object_unref (geocode);
1310 g_object_unref (contact);
1314 get_dup_string (GHashTable *location,
1319 value = g_hash_table_lookup (location, key);
1321 return g_value_dup_string (value);
1327 update_geocode (EmpathyContact *contact)
1329 static GeoclueGeocode *geocode;
1331 GHashTable *address;
1332 GHashTable *location;
1334 location = empathy_contact_get_location (contact);
1335 if (location == NULL)
1338 /* No need to search for position if contact published it */
1339 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1340 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1343 if (geocode == NULL)
1345 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1346 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1350 g_object_ref (geocode);
1353 address = geoclue_address_details_new ();
1355 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1358 g_hash_table_insert (address,
1359 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1360 DEBUG ("\t - countrycode: %s", str);
1363 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1366 g_hash_table_insert (address,
1367 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1368 DEBUG ("\t - country: %s", str);
1371 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1374 g_hash_table_insert (address,
1375 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1376 DEBUG ("\t - postalcode: %s", str);
1379 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1382 g_hash_table_insert (address,
1383 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1384 DEBUG ("\t - region: %s", str);
1387 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1390 g_hash_table_insert (address,
1391 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1392 DEBUG ("\t - locality: %s", str);
1395 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1398 g_hash_table_insert (address,
1399 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1400 DEBUG ("\t - street: %s", str);
1403 if (g_hash_table_size (address) > 0)
1405 g_object_ref (contact);
1407 geoclue_geocode_address_to_position_async (geocode, address,
1408 geocode_cb, contact);
1411 g_hash_table_unref (address);
1415 static EmpathyCapabilities
1416 tp_caps_to_capabilities (TpCapabilities *caps)
1418 EmpathyCapabilities capabilities = 0;
1422 classes = tp_capabilities_get_channel_classes (caps);
1424 for (i = 0; i < classes->len; i++)
1426 GValueArray *class_struct;
1427 GHashTable *fixed_prop;
1429 TpHandleType handle_type;
1430 const gchar *chan_type;
1432 class_struct = g_ptr_array_index (classes, i);
1433 tp_value_array_unpack (class_struct, 2,
1437 handle_type = tp_asv_get_uint32 (fixed_prop,
1438 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1439 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1442 chan_type = tp_asv_get_string (fixed_prop,
1443 TP_PROP_CHANNEL_CHANNEL_TYPE);
1445 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1447 capabilities |= EMPATHY_CAPABILITIES_FT;
1449 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1451 const gchar *service;
1453 service = tp_asv_get_string (fixed_prop,
1454 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1456 if (!tp_strdiff (service, "rfb"))
1457 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1459 else if (!tp_strdiff (chan_type,
1460 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1464 for (j = 0; allowed_prop[j] != NULL; j++)
1466 if (!tp_strdiff (allowed_prop[j],
1467 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1468 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1469 else if (!tp_strdiff (allowed_prop[j],
1470 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1471 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1476 return capabilities;
1480 set_capabilities_from_tp_caps (EmpathyContact *self,
1481 TpCapabilities *caps)
1483 EmpathyCapabilities capabilities;
1488 capabilities = tp_caps_to_capabilities (caps);
1489 empathy_contact_set_capabilities (self, capabilities);
1493 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1495 EmpathyContactPriv *priv = GET_PRIV (contact);
1500 token = tp_contact_get_avatar_token (priv->tp_contact);
1501 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1502 file = tp_contact_get_avatar_file (priv->tp_contact);
1506 EmpathyAvatar *avatar;
1510 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1511 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1512 g_file_get_path (file));
1513 empathy_contact_set_avatar (contact, avatar);
1514 empathy_avatar_unref (avatar);
1518 empathy_contact_set_avatar (contact, NULL);
1523 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1525 EmpathyContact *contact = NULL;
1527 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1529 if (contacts_table == NULL)
1530 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1532 contact = g_hash_table_lookup (contacts_table, tp_contact);
1534 if (contact == NULL)
1536 contact = empathy_contact_new (tp_contact);
1538 /* The hash table does not keep any ref.
1539 * contact keeps a ref to tp_contact, and is removed from the table in
1540 * contact_dispose() */
1541 g_hash_table_insert (contacts_table, tp_contact, contact);
1545 g_object_ref (contact);