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 <folks/folks.h>
33 #include <folks/folks-telepathy.h>
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;
52 FolksPersona *persona;
55 EmpathyAvatar *avatar;
56 TpConnectionPresenceType presence;
57 gchar *presence_message;
59 EmpathyCapabilities capabilities;
62 /* Location is composed of string keys and GValues.
63 * Example: a "city" key would have "Helsinki" as string GValue,
64 * a "latitude" would have 65.0 as double GValue.
66 * This is a super set of the location stored in TpContact as we can try add
67 * more fields by searching the address using geoclue.
72 static void contact_finalize (GObject *object);
73 static void contact_get_property (GObject *object, guint param_id,
74 GValue *value, GParamSpec *pspec);
75 static void contact_set_property (GObject *object, guint param_id,
76 const GValue *value, GParamSpec *pspec);
79 static void update_geocode (EmpathyContact *contact);
82 static void empathy_contact_set_location (EmpathyContact *contact,
83 GHashTable *location);
85 static void set_capabilities_from_tp_caps (EmpathyContact *self,
86 TpCapabilities *caps);
88 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
90 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
102 PROP_PRESENCE_MESSAGE,
114 static guint signals[LAST_SIGNAL];
116 /* TpContact* -> EmpathyContact*, both borrowed ref */
117 static GHashTable *contacts_table = NULL;
120 tp_contact_notify_cb (TpContact *tp_contact,
124 EmpathyContactPriv *priv = GET_PRIV (contact);
126 /* Forward property notifications */
127 if (!tp_strdiff (param->name, "alias"))
128 g_object_notify (contact, "alias");
129 else if (!tp_strdiff (param->name, "presence-type")) {
130 TpConnectionPresenceType presence;
132 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
133 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
135 priv->presence = presence;
136 g_object_notify (contact, "presence");
138 else if (!tp_strdiff (param->name, "presence-message"))
139 g_object_notify (contact, "presence-message");
140 else if (!tp_strdiff (param->name, "identifier"))
141 g_object_notify (contact, "id");
142 else if (!tp_strdiff (param->name, "handle"))
143 g_object_notify (contact, "handle");
144 else if (!tp_strdiff (param->name, "location"))
146 GHashTable *location;
148 location = tp_contact_get_location (tp_contact);
149 /* This will start a geoclue search to find the address if needed */
150 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
152 else if (!tp_strdiff (param->name, "capabilities"))
154 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
155 tp_contact_get_capabilities (tp_contact));
157 else if (!tp_strdiff (param->name, "avatar-file"))
159 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
164 contact_dispose (GObject *object)
166 EmpathyContactPriv *priv = GET_PRIV (object);
168 if (priv->tp_contact)
170 g_hash_table_remove (contacts_table, priv->tp_contact);
171 g_signal_handlers_disconnect_by_func (priv->tp_contact,
172 tp_contact_notify_cb, object);
173 g_object_unref (priv->tp_contact);
175 priv->tp_contact = NULL;
178 g_object_unref (priv->account);
179 priv->account = NULL;
182 g_object_unref (priv->persona);
183 priv->persona = NULL;
185 if (priv->avatar != NULL)
187 empathy_avatar_unref (priv->avatar);
191 if (priv->location != NULL)
193 g_hash_table_unref (priv->location);
194 priv->location = NULL;
197 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
201 empathy_contact_class_init (EmpathyContactClass *class)
203 GObjectClass *object_class;
205 object_class = G_OBJECT_CLASS (class);
207 object_class->finalize = contact_finalize;
208 object_class->dispose = contact_dispose;
209 object_class->get_property = contact_get_property;
210 object_class->set_property = contact_set_property;
212 g_object_class_install_property (object_class,
214 g_param_spec_object ("tp-contact",
216 "The TpContact associated with the contact",
218 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (object_class,
222 g_param_spec_object ("account",
224 "The account associated with the contact",
226 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (object_class,
230 g_param_spec_object ("persona",
232 "The FolksPersona associated with the contact",
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (object_class,
238 g_param_spec_string ("id",
240 "String identifying contact",
242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (object_class,
246 g_param_spec_string ("alias",
248 "An alias for the contact",
250 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (object_class,
254 g_param_spec_boxed ("avatar",
258 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (object_class,
262 g_param_spec_uint ("presence",
264 "Presence of contact",
265 TP_CONNECTION_PRESENCE_TYPE_UNSET,
266 NUM_TP_CONNECTION_PRESENCE_TYPES,
267 TP_CONNECTION_PRESENCE_TYPE_UNSET,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (object_class,
271 PROP_PRESENCE_MESSAGE,
272 g_param_spec_string ("presence-message",
273 "Contact presence message",
274 "Presence message of contact",
276 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278 g_object_class_install_property (object_class,
280 g_param_spec_uint ("handle",
282 "The handle of the contact",
286 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288 g_object_class_install_property (object_class,
290 g_param_spec_flags ("capabilities",
291 "Contact Capabilities",
292 "Capabilities of the contact",
293 EMPATHY_TYPE_CAPABILITIES,
294 EMPATHY_CAPABILITIES_UNKNOWN,
295 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (object_class,
299 g_param_spec_boolean ("is-user",
301 "Is contact the user",
303 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (object_class,
308 g_param_spec_boxed ("location",
310 "Physical location of the contact",
312 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
314 signals[PRESENCE_CHANGED] =
315 g_signal_new ("presence-changed",
316 G_TYPE_FROM_CLASS (class),
320 _empathy_marshal_VOID__UINT_UINT,
325 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
329 empathy_contact_init (EmpathyContact *contact)
331 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
332 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
334 contact->priv = priv;
336 priv->location = NULL;
340 contact_finalize (GObject *object)
342 EmpathyContactPriv *priv;
344 priv = GET_PRIV (object);
346 DEBUG ("finalize: %p", object);
348 g_free (priv->alias);
350 g_free (priv->presence_message);
352 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
356 set_tp_contact (EmpathyContact *contact,
357 TpContact *tp_contact)
359 EmpathyContactPriv *priv = GET_PRIV (contact);
360 GHashTable *location;
361 TpHandle self_handle;
364 if (tp_contact == NULL)
367 g_assert (priv->tp_contact == NULL);
368 priv->tp_contact = g_object_ref (tp_contact);
369 priv->presence = empathy_contact_get_presence (contact);
371 location = tp_contact_get_location (tp_contact);
372 if (location != NULL)
373 empathy_contact_set_location (contact, location);
375 set_capabilities_from_tp_caps (contact,
376 tp_contact_get_capabilities (tp_contact));
378 contact_set_avatar_from_tp_contact (contact);
380 /* Set is-user property. Note that it could still be the handle is
381 * different from the connection's self handle, in the case the handle
382 * comes from a group interface. */
383 self_handle = tp_connection_get_self_handle (
384 tp_contact_get_connection (tp_contact));
385 handle = tp_contact_get_handle (tp_contact);
386 empathy_contact_set_is_user (contact, self_handle == handle);
388 g_signal_connect (priv->tp_contact, "notify",
389 G_CALLBACK (tp_contact_notify_cb), contact);
393 contact_get_property (GObject *object,
398 EmpathyContact *contact = EMPATHY_CONTACT (object);
402 case PROP_TP_CONTACT:
403 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
406 g_value_set_object (value, empathy_contact_get_account (contact));
409 g_value_set_object (value, empathy_contact_get_persona (contact));
412 g_value_set_string (value, empathy_contact_get_id (contact));
415 g_value_set_string (value, empathy_contact_get_alias (contact));
418 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
421 g_value_set_uint (value, empathy_contact_get_presence (contact));
423 case PROP_PRESENCE_MESSAGE:
424 g_value_set_string (value, empathy_contact_get_presence_message (contact));
427 g_value_set_uint (value, empathy_contact_get_handle (contact));
429 case PROP_CAPABILITIES:
430 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
433 g_value_set_boolean (value, empathy_contact_is_user (contact));
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
442 contact_set_property (GObject *object,
447 EmpathyContact *contact = EMPATHY_CONTACT (object);
448 EmpathyContactPriv *priv = GET_PRIV (object);
452 case PROP_TP_CONTACT:
453 set_tp_contact (contact, g_value_get_object (value));
456 g_assert (priv->account == NULL);
457 priv->account = g_value_dup_object (value);
460 empathy_contact_set_persona (contact, g_value_get_object (value));
463 empathy_contact_set_id (contact, g_value_get_string (value));
466 empathy_contact_set_alias (contact, g_value_get_string (value));
469 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
472 empathy_contact_set_presence (contact, g_value_get_uint (value));
474 case PROP_PRESENCE_MESSAGE:
475 empathy_contact_set_presence_message (contact, g_value_get_string (value));
478 empathy_contact_set_handle (contact, g_value_get_uint (value));
480 case PROP_CAPABILITIES:
481 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
484 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
493 empathy_contact_new (TpContact *tp_contact)
495 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
497 return g_object_new (EMPATHY_TYPE_CONTACT,
498 "tp-contact", tp_contact,
503 empathy_contact_from_tpl_contact (TpAccount *account,
504 TplEntity *tpl_entity)
506 EmpathyContact *retval;
509 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
511 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
513 retval = g_object_new (EMPATHY_TYPE_CONTACT,
514 "id", tpl_entity_get_alias (tpl_entity),
515 "alias", tpl_entity_get_identifier (tpl_entity),
520 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
521 empathy_contact_load_avatar_cache (retval,
522 tpl_entity_get_avatar_token (tpl_entity));
528 empathy_contact_new_for_log (TpAccount *account,
533 g_return_val_if_fail (id != NULL, NULL);
534 g_assert (account != NULL);
536 return g_object_new (EMPATHY_TYPE_CONTACT,
545 empathy_contact_get_tp_contact (EmpathyContact *contact)
547 EmpathyContactPriv *priv;
549 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
551 priv = GET_PRIV (contact);
553 return priv->tp_contact;
557 empathy_contact_get_id (EmpathyContact *contact)
559 EmpathyContactPriv *priv;
561 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
563 priv = GET_PRIV (contact);
565 if (priv->tp_contact != NULL)
566 return tp_contact_get_identifier (priv->tp_contact);
572 empathy_contact_set_id (EmpathyContact *contact,
575 EmpathyContactPriv *priv;
577 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
578 g_return_if_fail (id != NULL);
580 priv = GET_PRIV (contact);
582 /* We temporally ref the contact because it could be destroyed
583 * during the signal emition */
584 g_object_ref (contact);
585 if (tp_strdiff (id, priv->id))
588 priv->id = g_strdup (id);
590 g_object_notify (G_OBJECT (contact), "id");
591 if (EMP_STR_EMPTY (priv->alias))
592 g_object_notify (G_OBJECT (contact), "alias");
595 g_object_unref (contact);
599 empathy_contact_get_alias (EmpathyContact *contact)
601 EmpathyContactPriv *priv;
604 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
606 priv = GET_PRIV (contact);
608 if (priv->tp_contact != NULL)
609 alias = tp_contact_get_alias (priv->tp_contact);
613 if (!EMP_STR_EMPTY (alias))
616 return empathy_contact_get_id (contact);
620 empathy_contact_set_alias (EmpathyContact *contact,
623 EmpathyContactPriv *priv;
624 FolksPersona *persona;
626 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
628 priv = GET_PRIV (contact);
630 g_object_ref (contact);
632 /* Set the alias on the persona if possible */
633 persona = empathy_contact_get_persona (contact);
634 if (persona != NULL && FOLKS_IS_ALIAS (persona))
636 DEBUG ("Setting alias for contact %s to %s",
637 empathy_contact_get_id (contact), alias);
639 folks_alias_set_alias (FOLKS_ALIAS (persona), alias);
642 if (tp_strdiff (alias, priv->alias))
644 g_free (priv->alias);
645 priv->alias = g_strdup (alias);
646 g_object_notify (G_OBJECT (contact), "alias");
649 g_object_unref (contact);
653 empathy_contact_get_avatar (EmpathyContact *contact)
655 EmpathyContactPriv *priv;
657 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
659 priv = GET_PRIV (contact);
665 empathy_contact_set_avatar (EmpathyContact *contact,
666 EmpathyAvatar *avatar)
668 EmpathyContactPriv *priv;
670 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
672 priv = GET_PRIV (contact);
674 if (priv->avatar == avatar)
679 empathy_avatar_unref (priv->avatar);
684 priv->avatar = empathy_avatar_ref (avatar);
686 g_object_notify (G_OBJECT (contact), "avatar");
690 empathy_contact_get_account (EmpathyContact *contact)
692 EmpathyContactPriv *priv;
694 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
696 priv = GET_PRIV (contact);
698 if (priv->account == NULL && priv->tp_contact != NULL)
700 TpConnection *connection;
702 /* FIXME: This assume the account manager already exists */
703 connection = tp_contact_get_connection (priv->tp_contact);
705 g_object_ref (empathy_get_account_for_connection (connection));
708 return priv->account;
712 empathy_contact_get_persona (EmpathyContact *contact)
714 EmpathyContactPriv *priv;
716 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
718 priv = GET_PRIV (contact);
720 if (priv->persona == NULL && priv->tp_contact != NULL)
722 /* FIXME: This is disgustingly slow */
723 /* Query for the persona */
724 EmpathyIndividualManager *manager;
725 GList *individuals, *l;
727 manager = empathy_individual_manager_dup_singleton ();
728 individuals = empathy_individual_manager_get_members (manager);
730 for (l = individuals; l != NULL; l = l->next)
733 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
735 personas = folks_individual_get_personas (individual);
736 for (j = personas; j != NULL; j = j->next)
738 TpfPersona *persona = j->data;
740 if (TPF_IS_PERSONA (persona))
742 TpContact *tp_contact = tpf_persona_get_contact (persona);
744 if (tp_contact == priv->tp_contact)
746 /* Found the right persona */
747 priv->persona = g_object_ref (persona);
755 g_list_free (individuals);
756 g_object_unref (manager);
759 return priv->persona;
763 empathy_contact_set_persona (EmpathyContact *contact,
764 FolksPersona *persona)
766 EmpathyContactPriv *priv;
768 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
769 g_return_if_fail (FOLKS_IS_PERSONA (persona));
771 priv = GET_PRIV (contact);
773 if (persona == priv->persona)
776 if (priv->persona != NULL)
777 g_object_unref (priv->persona);
778 priv->persona = g_object_ref (persona);
780 g_object_notify (G_OBJECT (contact), "persona");
782 /* Set the persona's alias, since ours could've been set using
783 * empathy_contact_set_alias() before we had a persona; this happens when
784 * adding a contact. */
785 empathy_contact_set_alias (contact, priv->alias);
789 empathy_contact_get_connection (EmpathyContact *contact)
791 EmpathyContactPriv *priv;
793 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
795 priv = GET_PRIV (contact);
797 if (priv->tp_contact != NULL)
798 return tp_contact_get_connection (priv->tp_contact);
803 TpConnectionPresenceType
804 empathy_contact_get_presence (EmpathyContact *contact)
806 EmpathyContactPriv *priv;
808 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
809 TP_CONNECTION_PRESENCE_TYPE_UNSET);
811 priv = GET_PRIV (contact);
813 if (priv->tp_contact != NULL)
814 return tp_contact_get_presence_type (priv->tp_contact);
816 return priv->presence;
820 empathy_contact_set_presence (EmpathyContact *contact,
821 TpConnectionPresenceType presence)
823 EmpathyContactPriv *priv;
824 TpConnectionPresenceType old_presence;
826 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
828 priv = GET_PRIV (contact);
830 if (presence == priv->presence)
833 old_presence = priv->presence;
834 priv->presence = presence;
836 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
838 g_object_notify (G_OBJECT (contact), "presence");
842 empathy_contact_get_presence_message (EmpathyContact *contact)
844 EmpathyContactPriv *priv;
846 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
848 priv = GET_PRIV (contact);
850 if (priv->tp_contact != NULL)
851 return tp_contact_get_presence_message (priv->tp_contact);
853 return priv->presence_message;
857 empathy_contact_set_presence_message (EmpathyContact *contact,
858 const gchar *message)
860 EmpathyContactPriv *priv = GET_PRIV (contact);
862 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
864 if (!tp_strdiff (message, priv->presence_message))
867 g_free (priv->presence_message);
868 priv->presence_message = g_strdup (message);
870 g_object_notify (G_OBJECT (contact), "presence-message");
874 empathy_contact_get_handle (EmpathyContact *contact)
876 EmpathyContactPriv *priv;
878 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
880 priv = GET_PRIV (contact);
882 if (priv->tp_contact != NULL)
883 return tp_contact_get_handle (priv->tp_contact);
889 empathy_contact_set_handle (EmpathyContact *contact,
892 EmpathyContactPriv *priv;
894 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
896 priv = GET_PRIV (contact);
898 g_object_ref (contact);
899 if (handle != priv->handle)
901 priv->handle = handle;
902 g_object_notify (G_OBJECT (contact), "handle");
904 g_object_unref (contact);
908 empathy_contact_get_capabilities (EmpathyContact *contact)
910 EmpathyContactPriv *priv;
912 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
914 priv = GET_PRIV (contact);
916 return priv->capabilities;
920 empathy_contact_set_capabilities (EmpathyContact *contact,
921 EmpathyCapabilities capabilities)
923 EmpathyContactPriv *priv;
925 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
927 priv = GET_PRIV (contact);
929 if (priv->capabilities == capabilities)
932 priv->capabilities = capabilities;
934 g_object_notify (G_OBJECT (contact), "capabilities");
938 empathy_contact_is_user (EmpathyContact *contact)
940 EmpathyContactPriv *priv;
942 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
944 priv = GET_PRIV (contact);
946 return priv->is_user;
950 empathy_contact_set_is_user (EmpathyContact *contact,
953 EmpathyContactPriv *priv;
955 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
957 priv = GET_PRIV (contact);
959 if (priv->is_user == is_user)
962 priv->is_user = is_user;
964 g_object_notify (G_OBJECT (contact), "is-user");
968 empathy_contact_is_online (EmpathyContact *contact)
970 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
972 switch (empathy_contact_get_presence (contact))
974 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
975 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
976 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
984 empathy_contact_get_status (EmpathyContact *contact)
986 const gchar *message;
988 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
990 message = empathy_contact_get_presence_message (contact);
991 if (!EMP_STR_EMPTY (message))
994 return empathy_presence_get_default_message (
995 empathy_contact_get_presence (contact));
999 empathy_contact_can_voip (EmpathyContact *contact)
1001 EmpathyContactPriv *priv;
1003 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1005 priv = GET_PRIV (contact);
1007 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1008 EMPATHY_CAPABILITIES_VIDEO);
1012 empathy_contact_can_voip_audio (EmpathyContact *contact)
1014 EmpathyContactPriv *priv;
1016 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1018 priv = GET_PRIV (contact);
1020 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1024 empathy_contact_can_voip_video (EmpathyContact *contact)
1026 EmpathyContactPriv *priv;
1028 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1030 priv = GET_PRIV (contact);
1032 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1036 empathy_contact_can_send_files (EmpathyContact *contact)
1038 EmpathyContactPriv *priv;
1040 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1042 priv = GET_PRIV (contact);
1044 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1048 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1050 EmpathyContactPriv *priv;
1052 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1054 priv = GET_PRIV (contact);
1056 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1060 contact_get_avatar_filename (EmpathyContact *contact,
1066 gchar *token_escaped;
1068 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1071 token_escaped = tp_escape_as_identifier (token);
1072 account = empathy_contact_get_account (contact);
1074 avatar_path = g_build_filename (g_get_user_cache_dir (),
1077 tp_account_get_connection_manager (account),
1078 tp_account_get_protocol (account),
1080 g_mkdir_with_parents (avatar_path, 0700);
1082 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1084 g_free (token_escaped);
1085 g_free (avatar_path);
1091 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1094 EmpathyAvatar *avatar = NULL;
1098 GError *error = NULL;
1100 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1101 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1103 /* Load the avatar from file if it exists */
1104 filename = contact_get_avatar_filename (contact, token);
1105 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1107 if (!g_file_get_contents (filename, &data, &len, &error))
1109 DEBUG ("Failed to load avatar from cache: %s",
1110 error ? error->message : "No error given");
1111 g_clear_error (&error);
1117 DEBUG ("Avatar loaded from %s", filename);
1118 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1120 empathy_contact_set_avatar (contact, avatar);
1121 empathy_avatar_unref (avatar);
1128 return data != NULL;
1132 empathy_avatar_get_type (void)
1134 static GType type_id = 0;
1138 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1139 (GBoxedCopyFunc) empathy_avatar_ref,
1140 (GBoxedFreeFunc) empathy_avatar_unref);
1147 * empathy_avatar_new:
1148 * @data: the avatar data
1149 * @len: the size of avatar data
1150 * @format: the mime type of the avatar image
1151 * @token: the token of the avatar
1152 * @filename: the filename where the avatar is stored in cache
1154 * Create a #EmpathyAvatar from the provided data. This function takes the
1155 * ownership of @data, @format, @token and @filename.
1157 * Returns: a new #EmpathyAvatar
1160 empathy_avatar_new (guchar *data,
1166 EmpathyAvatar *avatar;
1168 avatar = g_slice_new0 (EmpathyAvatar);
1169 avatar->data = data;
1171 avatar->format = format;
1172 avatar->token = token;
1173 avatar->filename = filename;
1174 avatar->refcount = 1;
1180 empathy_avatar_unref (EmpathyAvatar *avatar)
1182 g_return_if_fail (avatar != NULL);
1185 if (avatar->refcount == 0)
1187 g_free (avatar->data);
1188 g_free (avatar->format);
1189 g_free (avatar->token);
1190 g_free (avatar->filename);
1191 g_slice_free (EmpathyAvatar, avatar);
1196 empathy_avatar_ref (EmpathyAvatar *avatar)
1198 g_return_val_if_fail (avatar != NULL, NULL);
1206 * empathy_avatar_save_to_file:
1207 * @avatar: the avatar
1208 * @filename: name of a file to write avatar to
1209 * @error: return location for a GError, or NULL
1211 * Save the avatar to a file named filename
1213 * Returns: %TRUE on success, %FALSE if an error occurred
1216 empathy_avatar_save_to_file (EmpathyAvatar *self,
1217 const gchar *filename,
1220 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1225 * empathy_contact_get_location:
1226 * @contact: an #EmpathyContact
1228 * Returns the user's location if available. The keys are defined in
1229 * empathy-location.h. If the contact doesn't have location
1230 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1231 * you are done with the #GHashTable.
1233 * It is composed of string keys and GValues. Keys are
1234 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1235 * Example: a "city" key would have "Helsinki" as string GValue,
1236 * a "latitude" would have 65.0 as double GValue.
1238 * Returns: a #GHashTable of location values
1241 empathy_contact_get_location (EmpathyContact *contact)
1243 EmpathyContactPriv *priv;
1245 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1247 priv = GET_PRIV (contact);
1249 return priv->location;
1253 * empathy_contact_set_location:
1254 * @contact: an #EmpathyContact
1255 * @location: a #GHashTable of the location
1257 * Sets the user's location based on the location #GHashTable passed.
1258 * It is composed of string keys and GValues. Keys are
1259 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1260 * Example: a "city" key would have "Helsinki" as string GValue,
1261 * a "latitude" would have 65.0 as double GValue.
1264 empathy_contact_set_location (EmpathyContact *contact,
1265 GHashTable *location)
1267 EmpathyContactPriv *priv;
1269 g_return_if_fail (EMPATHY_CONTACT (contact));
1270 g_return_if_fail (location != NULL);
1272 priv = GET_PRIV (contact);
1274 if (priv->location != NULL)
1275 g_hash_table_unref (priv->location);
1277 priv->location = g_hash_table_ref (location);
1279 update_geocode (contact);
1281 g_object_notify (G_OBJECT (contact), "location");
1285 * empathy_contact_equal:
1286 * @contact1: an #EmpathyContact
1287 * @contact2: an #EmpathyContact
1289 * Returns FALSE if one of the contacts is NULL but the other is not.
1290 * Otherwise returns TRUE if both pointer are equal or if they bith
1291 * refer to the same id.
1292 * It's only necessary to call this function if your contact objects
1293 * come from logs where contacts are created dynamically and comparing
1294 * pointers is not enough.
1297 empathy_contact_equal (gconstpointer contact1,
1298 gconstpointer contact2)
1305 if ((contact1 == NULL) != (contact2 == NULL)) {
1308 if (contact1 == contact2) {
1311 c1 = EMPATHY_CONTACT (contact1);
1312 c2 = EMPATHY_CONTACT (contact2);
1313 id1 = empathy_contact_get_id (c1);
1314 id2 = empathy_contact_get_id (c2);
1315 if (!tp_strdiff (id1, id2)) {
1322 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1323 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1325 /* This callback is called by geoclue when it found a position
1326 * for the given address. A position is necessary for a contact
1327 * to show up on the map
1330 geocode_cb (GeoclueGeocode *geocode,
1331 GeocluePositionFields fields,
1335 GeoclueAccuracy *accuracy,
1339 EmpathyContactPriv *priv = GET_PRIV (contact);
1340 GHashTable *new_location;
1342 if (priv->location == NULL)
1347 DEBUG ("Error geocoding location : %s", error->message);
1351 /* No need to change location if we didn't find the position */
1352 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1355 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1358 new_location = tp_asv_new (
1359 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1360 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1363 DEBUG ("\t - Latitude: %f", latitude);
1364 DEBUG ("\t - Longitude: %f", longitude);
1366 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1367 * the values we just set. */
1368 tp_g_hash_table_update (new_location, priv->location,
1369 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1371 /* Set the altitude only if it wasn't defined before */
1372 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1373 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1375 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1377 DEBUG ("\t - Altitude: %f", altitude);
1380 /* Don't change the accuracy as we used an address to get this position */
1381 g_hash_table_unref (priv->location);
1382 priv->location = new_location;
1383 g_object_notify (contact, "location");
1385 g_object_unref (geocode);
1386 g_object_unref (contact);
1390 get_dup_string (GHashTable *location,
1395 value = g_hash_table_lookup (location, key);
1397 return g_value_dup_string (value);
1403 update_geocode (EmpathyContact *contact)
1405 static GeoclueGeocode *geocode;
1407 GHashTable *address;
1408 GHashTable *location;
1410 location = empathy_contact_get_location (contact);
1411 if (location == NULL)
1414 /* No need to search for position if contact published it */
1415 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1416 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1419 if (geocode == NULL)
1421 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1422 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1426 g_object_ref (geocode);
1429 address = geoclue_address_details_new ();
1431 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1434 g_hash_table_insert (address,
1435 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1436 DEBUG ("\t - countrycode: %s", str);
1439 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1442 g_hash_table_insert (address,
1443 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1444 DEBUG ("\t - country: %s", str);
1447 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1450 g_hash_table_insert (address,
1451 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1452 DEBUG ("\t - postalcode: %s", str);
1455 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1458 g_hash_table_insert (address,
1459 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1460 DEBUG ("\t - region: %s", str);
1463 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1466 g_hash_table_insert (address,
1467 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1468 DEBUG ("\t - locality: %s", str);
1471 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1474 g_hash_table_insert (address,
1475 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1476 DEBUG ("\t - street: %s", str);
1479 if (g_hash_table_size (address) > 0)
1481 g_object_ref (contact);
1483 geoclue_geocode_address_to_position_async (geocode, address,
1484 geocode_cb, contact);
1487 g_hash_table_unref (address);
1491 static EmpathyCapabilities
1492 tp_caps_to_capabilities (TpCapabilities *caps)
1494 EmpathyCapabilities capabilities = 0;
1498 classes = tp_capabilities_get_channel_classes (caps);
1500 for (i = 0; i < classes->len; i++)
1502 GValueArray *class_struct;
1503 GHashTable *fixed_prop;
1505 TpHandleType handle_type;
1506 const gchar *chan_type;
1508 class_struct = g_ptr_array_index (classes, i);
1509 tp_value_array_unpack (class_struct, 2,
1513 handle_type = tp_asv_get_uint32 (fixed_prop,
1514 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1515 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1518 chan_type = tp_asv_get_string (fixed_prop,
1519 TP_PROP_CHANNEL_CHANNEL_TYPE);
1521 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1523 capabilities |= EMPATHY_CAPABILITIES_FT;
1525 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1527 const gchar *service;
1529 service = tp_asv_get_string (fixed_prop,
1530 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1532 if (!tp_strdiff (service, "rfb"))
1533 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1535 else if (!tp_strdiff (chan_type,
1536 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1540 for (j = 0; allowed_prop[j] != NULL; j++)
1542 if (!tp_strdiff (allowed_prop[j],
1543 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1544 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1545 else if (!tp_strdiff (allowed_prop[j],
1546 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1547 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1552 return capabilities;
1556 set_capabilities_from_tp_caps (EmpathyContact *self,
1557 TpCapabilities *caps)
1559 EmpathyCapabilities capabilities;
1564 capabilities = tp_caps_to_capabilities (caps);
1565 empathy_contact_set_capabilities (self, capabilities);
1569 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1571 EmpathyContactPriv *priv = GET_PRIV (contact);
1576 token = tp_contact_get_avatar_token (priv->tp_contact);
1577 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1578 file = tp_contact_get_avatar_file (priv->tp_contact);
1582 EmpathyAvatar *avatar;
1586 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1587 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1588 g_file_get_path (file));
1589 empathy_contact_set_avatar (contact, avatar);
1590 empathy_avatar_unref (avatar);
1594 empathy_contact_set_avatar (contact, NULL);
1599 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1601 EmpathyContact *contact = NULL;
1603 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1605 if (contacts_table == NULL)
1606 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1608 contact = g_hash_table_lookup (contacts_table, tp_contact);
1610 if (contact == NULL)
1612 contact = empathy_contact_new (tp_contact);
1614 /* The hash table does not keep any ref.
1615 * contact keeps a ref to tp_contact, and is removed from the table in
1616 * contact_dispose() */
1617 g_hash_table_insert (contacts_table, tp_contact, contact);
1621 g_object_ref (contact);