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*, both borrowed ref */
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 contact_set_aliases_cb (TpConnection *connection,
625 GObject *weak_object)
628 DEBUG ("Error: %s", error->message);
632 empathy_contact_set_alias (EmpathyContact *contact,
635 TpConnection *connection;
636 GHashTable *new_alias;
639 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
641 handle = empathy_contact_get_handle (contact);
643 DEBUG ("Setting alias for contact %s (%d) to %s",
644 empathy_contact_get_id (contact),
647 new_alias = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
650 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), g_strdup (alias));
652 connection = empathy_contact_get_connection (contact);
653 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
654 new_alias, contact_set_aliases_cb, NULL, NULL, NULL);
656 g_hash_table_destroy (new_alias);
660 empathy_contact_get_avatar (EmpathyContact *contact)
662 EmpathyContactPriv *priv;
664 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
666 priv = GET_PRIV (contact);
672 empathy_contact_set_avatar (EmpathyContact *contact,
673 EmpathyAvatar *avatar)
675 EmpathyContactPriv *priv;
677 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
679 priv = GET_PRIV (contact);
681 if (priv->avatar == avatar)
686 empathy_avatar_unref (priv->avatar);
691 priv->avatar = empathy_avatar_ref (avatar);
693 g_object_notify (G_OBJECT (contact), "avatar");
697 empathy_contact_get_account (EmpathyContact *contact)
699 EmpathyContactPriv *priv;
701 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
703 priv = GET_PRIV (contact);
705 if (priv->account == NULL && priv->tp_contact != NULL)
707 TpConnection *connection;
709 /* FIXME: This assume the account manager already exists */
710 connection = tp_contact_get_connection (priv->tp_contact);
712 g_object_ref (empathy_get_account_for_connection (connection));
715 return priv->account;
719 empathy_contact_get_connection (EmpathyContact *contact)
721 EmpathyContactPriv *priv;
723 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
725 priv = GET_PRIV (contact);
727 if (priv->tp_contact != NULL)
728 return tp_contact_get_connection (priv->tp_contact);
733 TpConnectionPresenceType
734 empathy_contact_get_presence (EmpathyContact *contact)
736 EmpathyContactPriv *priv;
738 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
739 TP_CONNECTION_PRESENCE_TYPE_UNSET);
741 priv = GET_PRIV (contact);
743 if (priv->tp_contact != NULL)
744 return tp_contact_get_presence_type (priv->tp_contact);
746 return priv->presence;
750 empathy_contact_set_presence (EmpathyContact *contact,
751 TpConnectionPresenceType presence)
753 EmpathyContactPriv *priv;
754 TpConnectionPresenceType old_presence;
756 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
758 priv = GET_PRIV (contact);
760 if (presence == priv->presence)
763 old_presence = priv->presence;
764 priv->presence = presence;
766 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
768 g_object_notify (G_OBJECT (contact), "presence");
772 empathy_contact_get_presence_message (EmpathyContact *contact)
774 EmpathyContactPriv *priv;
776 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
778 priv = GET_PRIV (contact);
780 if (priv->tp_contact != NULL)
781 return tp_contact_get_presence_message (priv->tp_contact);
783 return priv->presence_message;
787 empathy_contact_set_presence_message (EmpathyContact *contact,
788 const gchar *message)
790 EmpathyContactPriv *priv = GET_PRIV (contact);
792 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
794 if (!tp_strdiff (message, priv->presence_message))
797 g_free (priv->presence_message);
798 priv->presence_message = g_strdup (message);
800 g_object_notify (G_OBJECT (contact), "presence-message");
804 empathy_contact_get_handle (EmpathyContact *contact)
806 EmpathyContactPriv *priv;
808 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
810 priv = GET_PRIV (contact);
812 if (priv->tp_contact != NULL)
813 return tp_contact_get_handle (priv->tp_contact);
819 empathy_contact_set_handle (EmpathyContact *contact,
822 EmpathyContactPriv *priv;
824 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
826 priv = GET_PRIV (contact);
828 g_object_ref (contact);
829 if (handle != priv->handle)
831 priv->handle = handle;
832 g_object_notify (G_OBJECT (contact), "handle");
834 g_object_unref (contact);
838 empathy_contact_get_capabilities (EmpathyContact *contact)
840 EmpathyContactPriv *priv;
842 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
844 priv = GET_PRIV (contact);
846 return priv->capabilities;
850 empathy_contact_set_capabilities (EmpathyContact *contact,
851 EmpathyCapabilities capabilities)
853 EmpathyContactPriv *priv;
855 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
857 priv = GET_PRIV (contact);
859 if (priv->capabilities == capabilities)
862 priv->capabilities = capabilities;
864 g_object_notify (G_OBJECT (contact), "capabilities");
868 empathy_contact_is_user (EmpathyContact *contact)
870 EmpathyContactPriv *priv;
872 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
874 priv = GET_PRIV (contact);
876 return priv->is_user;
880 empathy_contact_set_is_user (EmpathyContact *contact,
883 EmpathyContactPriv *priv;
885 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
887 priv = GET_PRIV (contact);
889 if (priv->is_user == is_user)
892 priv->is_user = is_user;
894 g_object_notify (G_OBJECT (contact), "is-user");
898 empathy_contact_is_online (EmpathyContact *contact)
900 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
902 switch (empathy_contact_get_presence (contact))
904 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
905 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
906 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
914 empathy_contact_get_status (EmpathyContact *contact)
916 const gchar *message;
918 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
920 message = empathy_contact_get_presence_message (contact);
921 if (!EMP_STR_EMPTY (message))
924 return empathy_presence_get_default_message (
925 empathy_contact_get_presence (contact));
929 empathy_contact_can_voip (EmpathyContact *contact)
931 EmpathyContactPriv *priv;
933 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
935 priv = GET_PRIV (contact);
937 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
938 EMPATHY_CAPABILITIES_VIDEO);
942 empathy_contact_can_voip_audio (EmpathyContact *contact)
944 EmpathyContactPriv *priv;
946 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
948 priv = GET_PRIV (contact);
950 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
954 empathy_contact_can_voip_video (EmpathyContact *contact)
956 EmpathyContactPriv *priv;
958 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
960 priv = GET_PRIV (contact);
962 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
966 empathy_contact_can_send_files (EmpathyContact *contact)
968 EmpathyContactPriv *priv;
970 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
972 priv = GET_PRIV (contact);
974 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
978 empathy_contact_can_use_stream_tube (EmpathyContact *contact)
980 EmpathyContactPriv *priv;
982 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
984 priv = GET_PRIV (contact);
986 return priv->capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE;
990 contact_get_avatar_filename (EmpathyContact *contact,
996 gchar *token_escaped;
998 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1001 token_escaped = tp_escape_as_identifier (token);
1002 account = empathy_contact_get_account (contact);
1004 avatar_path = g_build_filename (g_get_user_cache_dir (),
1007 tp_account_get_connection_manager (account),
1008 tp_account_get_protocol (account),
1010 g_mkdir_with_parents (avatar_path, 0700);
1012 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1014 g_free (token_escaped);
1015 g_free (avatar_path);
1021 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1024 EmpathyAvatar *avatar = NULL;
1028 GError *error = NULL;
1030 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1031 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1033 /* Load the avatar from file if it exists */
1034 filename = contact_get_avatar_filename (contact, token);
1035 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1037 if (!g_file_get_contents (filename, &data, &len, &error))
1039 DEBUG ("Failed to load avatar from cache: %s",
1040 error ? error->message : "No error given");
1041 g_clear_error (&error);
1047 DEBUG ("Avatar loaded from %s", filename);
1048 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1050 empathy_contact_set_avatar (contact, avatar);
1051 empathy_avatar_unref (avatar);
1058 return data != NULL;
1062 empathy_avatar_get_type (void)
1064 static GType type_id = 0;
1068 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1069 (GBoxedCopyFunc) empathy_avatar_ref,
1070 (GBoxedFreeFunc) empathy_avatar_unref);
1077 * empathy_avatar_new:
1078 * @data: the avatar data
1079 * @len: the size of avatar data
1080 * @format: the mime type of the avatar image
1081 * @token: the token of the avatar
1082 * @filename: the filename where the avatar is stored in cache
1084 * Create a #EmpathyAvatar from the provided data. This function takes the
1085 * ownership of @data, @format, @token and @filename.
1087 * Returns: a new #EmpathyAvatar
1090 empathy_avatar_new (guchar *data,
1096 EmpathyAvatar *avatar;
1098 avatar = g_slice_new0 (EmpathyAvatar);
1099 avatar->data = data;
1101 avatar->format = format;
1102 avatar->token = token;
1103 avatar->filename = filename;
1104 avatar->refcount = 1;
1110 empathy_avatar_unref (EmpathyAvatar *avatar)
1112 g_return_if_fail (avatar != NULL);
1115 if (avatar->refcount == 0)
1117 g_free (avatar->data);
1118 g_free (avatar->format);
1119 g_free (avatar->token);
1120 g_slice_free (EmpathyAvatar, avatar);
1125 empathy_avatar_ref (EmpathyAvatar *avatar)
1127 g_return_val_if_fail (avatar != NULL, NULL);
1135 * empathy_avatar_save_to_file:
1136 * @avatar: the avatar
1137 * @filename: name of a file to write avatar to
1138 * @error: return location for a GError, or NULL
1140 * Save the avatar to a file named filename
1142 * Returns: %TRUE on success, %FALSE if an error occurred
1145 empathy_avatar_save_to_file (EmpathyAvatar *self,
1146 const gchar *filename,
1149 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1154 * empathy_contact_get_location:
1155 * @contact: an #EmpathyContact
1157 * Returns the user's location if available. The keys are defined in
1158 * empathy-location.h. If the contact doesn't have location
1159 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1160 * you are done with the #GHashTable.
1162 * It is composed of string keys and GValues. Keys are
1163 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1164 * Example: a "city" key would have "Helsinki" as string GValue,
1165 * a "latitude" would have 65.0 as double GValue.
1167 * Returns: a #GHashTable of location values
1170 empathy_contact_get_location (EmpathyContact *contact)
1172 EmpathyContactPriv *priv;
1174 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1176 priv = GET_PRIV (contact);
1178 return priv->location;
1182 * empathy_contact_set_location:
1183 * @contact: an #EmpathyContact
1184 * @location: a #GHashTable of the location
1186 * Sets the user's location based on the location #GHashTable passed.
1187 * It is composed of string keys and GValues. Keys are
1188 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1189 * Example: a "city" key would have "Helsinki" as string GValue,
1190 * a "latitude" would have 65.0 as double GValue.
1193 empathy_contact_set_location (EmpathyContact *contact,
1194 GHashTable *location)
1196 EmpathyContactPriv *priv;
1198 g_return_if_fail (EMPATHY_CONTACT (contact));
1199 g_return_if_fail (location != NULL);
1201 priv = GET_PRIV (contact);
1203 if (priv->location != NULL)
1204 g_hash_table_unref (priv->location);
1206 priv->location = g_hash_table_ref (location);
1208 update_geocode (contact);
1210 g_object_notify (G_OBJECT (contact), "location");
1214 * empathy_contact_equal:
1215 * @contact1: an #EmpathyContact
1216 * @contact2: an #EmpathyContact
1218 * Returns FALSE if one of the contacts is NULL but the other is not.
1219 * Otherwise returns TRUE if both pointer are equal or if they bith
1220 * refer to the same id.
1221 * It's only necessary to call this function if your contact objects
1222 * come from logs where contacts are created dynamically and comparing
1223 * pointers is not enough.
1226 empathy_contact_equal (gconstpointer contact1,
1227 gconstpointer contact2)
1234 if ((contact1 == NULL) != (contact2 == NULL)) {
1237 if (contact1 == contact2) {
1240 c1 = EMPATHY_CONTACT (contact1);
1241 c2 = EMPATHY_CONTACT (contact2);
1242 id1 = empathy_contact_get_id (c1);
1243 id2 = empathy_contact_get_id (c2);
1244 if (!tp_strdiff (id1, id2)) {
1251 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1252 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1254 /* This callback is called by geoclue when it found a position
1255 * for the given address. A position is necessary for a contact
1256 * to show up on the map
1259 geocode_cb (GeoclueGeocode *geocode,
1260 GeocluePositionFields fields,
1264 GeoclueAccuracy *accuracy,
1268 EmpathyContactPriv *priv = GET_PRIV (contact);
1269 GHashTable *new_location;
1271 if (priv->location == NULL)
1276 DEBUG ("Error geocoding location : %s", error->message);
1280 /* No need to change location if we didn't find the position */
1281 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1284 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1287 new_location = tp_asv_new (
1288 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1289 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1292 DEBUG ("\t - Latitude: %f", latitude);
1293 DEBUG ("\t - Longitude: %f", longitude);
1295 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1296 * the values we just set. */
1297 tp_g_hash_table_update (new_location, priv->location,
1298 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1300 /* Set the altitude only if it wasn't defined before */
1301 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1302 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1304 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1306 DEBUG ("\t - Altitude: %f", altitude);
1309 /* Don't change the accuracy as we used an address to get this position */
1310 g_hash_table_unref (priv->location);
1311 priv->location = new_location;
1312 g_object_notify (contact, "location");
1314 g_object_unref (geocode);
1315 g_object_unref (contact);
1319 get_dup_string (GHashTable *location,
1324 value = g_hash_table_lookup (location, key);
1326 return g_value_dup_string (value);
1332 update_geocode (EmpathyContact *contact)
1334 static GeoclueGeocode *geocode;
1336 GHashTable *address;
1337 GHashTable *location;
1339 location = empathy_contact_get_location (contact);
1340 if (location == NULL)
1343 /* No need to search for position if contact published it */
1344 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1345 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1348 if (geocode == NULL)
1350 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1351 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1355 g_object_ref (geocode);
1358 address = geoclue_address_details_new ();
1360 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1363 g_hash_table_insert (address,
1364 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1365 DEBUG ("\t - countrycode: %s", str);
1368 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1371 g_hash_table_insert (address,
1372 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1373 DEBUG ("\t - country: %s", str);
1376 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1379 g_hash_table_insert (address,
1380 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1381 DEBUG ("\t - postalcode: %s", str);
1384 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1387 g_hash_table_insert (address,
1388 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1389 DEBUG ("\t - region: %s", str);
1392 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1395 g_hash_table_insert (address,
1396 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1397 DEBUG ("\t - locality: %s", str);
1400 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1403 g_hash_table_insert (address,
1404 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1405 DEBUG ("\t - street: %s", str);
1408 if (g_hash_table_size (address) > 0)
1410 g_object_ref (contact);
1412 geoclue_geocode_address_to_position_async (geocode, address,
1413 geocode_cb, contact);
1416 g_hash_table_unref (address);
1420 static EmpathyCapabilities
1421 tp_caps_to_capabilities (TpCapabilities *caps)
1423 EmpathyCapabilities capabilities = 0;
1427 classes = tp_capabilities_get_channel_classes (caps);
1429 for (i = 0; i < classes->len; i++)
1431 GValueArray *class_struct;
1432 GHashTable *fixed_prop;
1434 TpHandleType handle_type;
1435 const gchar *chan_type;
1437 class_struct = g_ptr_array_index (classes, i);
1438 tp_value_array_unpack (class_struct, 2,
1442 handle_type = tp_asv_get_uint32 (fixed_prop,
1443 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1444 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1447 chan_type = tp_asv_get_string (fixed_prop,
1448 TP_PROP_CHANNEL_CHANNEL_TYPE);
1450 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1452 capabilities |= EMPATHY_CAPABILITIES_FT;
1454 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1456 capabilities |= EMPATHY_CAPABILITIES_STREAM_TUBE;
1458 else if (!tp_strdiff (chan_type,
1459 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1463 for (j = 0; allowed_prop[j] != NULL; j++)
1465 if (!tp_strdiff (allowed_prop[j],
1466 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1467 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1468 else if (!tp_strdiff (allowed_prop[j],
1469 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1470 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1475 return capabilities;
1479 set_capabilities_from_tp_caps (EmpathyContact *self,
1480 TpCapabilities *caps)
1482 EmpathyCapabilities capabilities;
1487 capabilities = tp_caps_to_capabilities (caps);
1488 empathy_contact_set_capabilities (self, capabilities);
1492 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1494 EmpathyContactPriv *priv = GET_PRIV (contact);
1499 token = tp_contact_get_avatar_token (priv->tp_contact);
1500 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1501 file = tp_contact_get_avatar_file (priv->tp_contact);
1505 EmpathyAvatar *avatar;
1509 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1510 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1511 g_file_get_path (file));
1512 empathy_contact_set_avatar (contact, avatar);
1513 empathy_avatar_unref (avatar);
1517 empathy_contact_set_avatar (contact, NULL);
1522 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1524 EmpathyContact *contact = NULL;
1526 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1528 if (contacts_table == NULL)
1529 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1531 contact = g_hash_table_lookup (contacts_table, tp_contact);
1533 if (contact == NULL)
1535 contact = empathy_contact_new (tp_contact);
1537 /* The hash table does not keep any ref.
1538 * contact keeps a ref to tp_contact, and is removed from the table in
1539 * contact_dispose() */
1540 g_hash_table_insert (contacts_table, tp_contact, contact);
1544 g_object_ref (contact);