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, "name");
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 ("name",
248 "The name of 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);
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_name (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_name (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 "name", 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->name))
592 g_object_notify (G_OBJECT (contact), "name");
595 g_object_unref (contact);
599 empathy_contact_get_name (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_name (EmpathyContact *contact,
623 EmpathyContactPriv *priv;
625 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
627 priv = GET_PRIV (contact);
629 g_object_ref (contact);
630 if (tp_strdiff (name, priv->name))
633 priv->name = g_strdup (name);
634 g_object_notify (G_OBJECT (contact), "name");
636 g_object_unref (contact);
640 contact_set_aliases_cb (TpConnection *connection,
643 GObject *weak_object)
646 DEBUG ("Error: %s", error->message);
650 empathy_contact_set_alias (EmpathyContact *contact,
653 TpConnection *connection;
654 GHashTable *new_alias;
657 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
659 handle = empathy_contact_get_handle (contact);
661 DEBUG ("Setting alias for contact %s (%d) to %s",
662 empathy_contact_get_id (contact),
665 new_alias = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
668 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), g_strdup (alias));
670 connection = empathy_contact_get_connection (contact);
671 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
672 new_alias, contact_set_aliases_cb, NULL, NULL, NULL);
674 g_hash_table_destroy (new_alias);
678 empathy_contact_get_avatar (EmpathyContact *contact)
680 EmpathyContactPriv *priv;
682 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
684 priv = GET_PRIV (contact);
690 empathy_contact_set_avatar (EmpathyContact *contact,
691 EmpathyAvatar *avatar)
693 EmpathyContactPriv *priv;
695 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
697 priv = GET_PRIV (contact);
699 if (priv->avatar == avatar)
704 empathy_avatar_unref (priv->avatar);
709 priv->avatar = empathy_avatar_ref (avatar);
711 g_object_notify (G_OBJECT (contact), "avatar");
715 empathy_contact_get_account (EmpathyContact *contact)
717 EmpathyContactPriv *priv;
719 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
721 priv = GET_PRIV (contact);
723 if (priv->account == NULL && priv->tp_contact != NULL)
725 TpConnection *connection;
727 /* FIXME: This assume the account manager already exists */
728 connection = tp_contact_get_connection (priv->tp_contact);
730 g_object_ref (empathy_get_account_for_connection (connection));
733 return priv->account;
737 empathy_contact_get_persona (EmpathyContact *contact)
739 EmpathyContactPriv *priv;
741 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
743 priv = GET_PRIV (contact);
745 if (priv->persona == NULL && priv->tp_contact != NULL)
747 /* FIXME: This is disgustingly slow */
748 /* Query for the persona */
749 EmpathyIndividualManager *manager;
750 GList *individuals, *l;
752 manager = empathy_individual_manager_dup_singleton ();
753 individuals = empathy_individual_manager_get_members (manager);
755 for (l = individuals; l != NULL; l = l->next)
758 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
760 personas = folks_individual_get_personas (individual);
761 for (j = personas; j != NULL; j = j->next)
763 TpfPersona *persona = j->data;
765 if (TPF_IS_PERSONA (persona))
767 TpContact *tp_contact = tpf_persona_get_contact (persona);
769 if (tp_contact == priv->tp_contact)
771 /* Found the right persona */
772 priv->persona = g_object_ref (persona);
780 g_list_free (individuals);
781 g_object_unref (manager);
784 return priv->persona;
788 empathy_contact_set_persona (EmpathyContact *contact,
789 FolksPersona *persona)
791 EmpathyContactPriv *priv;
793 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
794 g_return_if_fail (FOLKS_IS_PERSONA (persona));
796 priv = GET_PRIV (contact);
798 if (persona == priv->persona)
801 if (priv->persona != NULL)
802 g_object_unref (priv->persona);
803 priv->persona = g_object_ref (persona);
805 g_object_notify (G_OBJECT (contact), "persona");
809 empathy_contact_get_connection (EmpathyContact *contact)
811 EmpathyContactPriv *priv;
813 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
815 priv = GET_PRIV (contact);
817 if (priv->tp_contact != NULL)
818 return tp_contact_get_connection (priv->tp_contact);
823 TpConnectionPresenceType
824 empathy_contact_get_presence (EmpathyContact *contact)
826 EmpathyContactPriv *priv;
828 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
829 TP_CONNECTION_PRESENCE_TYPE_UNSET);
831 priv = GET_PRIV (contact);
833 if (priv->tp_contact != NULL)
834 return tp_contact_get_presence_type (priv->tp_contact);
836 return priv->presence;
840 empathy_contact_set_presence (EmpathyContact *contact,
841 TpConnectionPresenceType presence)
843 EmpathyContactPriv *priv;
844 TpConnectionPresenceType old_presence;
846 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
848 priv = GET_PRIV (contact);
850 if (presence == priv->presence)
853 old_presence = priv->presence;
854 priv->presence = presence;
856 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
858 g_object_notify (G_OBJECT (contact), "presence");
862 empathy_contact_get_presence_message (EmpathyContact *contact)
864 EmpathyContactPriv *priv;
866 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
868 priv = GET_PRIV (contact);
870 if (priv->tp_contact != NULL)
871 return tp_contact_get_presence_message (priv->tp_contact);
873 return priv->presence_message;
877 empathy_contact_set_presence_message (EmpathyContact *contact,
878 const gchar *message)
880 EmpathyContactPriv *priv = GET_PRIV (contact);
882 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
884 if (!tp_strdiff (message, priv->presence_message))
887 g_free (priv->presence_message);
888 priv->presence_message = g_strdup (message);
890 g_object_notify (G_OBJECT (contact), "presence-message");
894 empathy_contact_get_handle (EmpathyContact *contact)
896 EmpathyContactPriv *priv;
898 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
900 priv = GET_PRIV (contact);
902 if (priv->tp_contact != NULL)
903 return tp_contact_get_handle (priv->tp_contact);
909 empathy_contact_set_handle (EmpathyContact *contact,
912 EmpathyContactPriv *priv;
914 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
916 priv = GET_PRIV (contact);
918 g_object_ref (contact);
919 if (handle != priv->handle)
921 priv->handle = handle;
922 g_object_notify (G_OBJECT (contact), "handle");
924 g_object_unref (contact);
928 empathy_contact_get_capabilities (EmpathyContact *contact)
930 EmpathyContactPriv *priv;
932 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
934 priv = GET_PRIV (contact);
936 return priv->capabilities;
940 empathy_contact_set_capabilities (EmpathyContact *contact,
941 EmpathyCapabilities capabilities)
943 EmpathyContactPriv *priv;
945 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
947 priv = GET_PRIV (contact);
949 if (priv->capabilities == capabilities)
952 priv->capabilities = capabilities;
954 g_object_notify (G_OBJECT (contact), "capabilities");
958 empathy_contact_is_user (EmpathyContact *contact)
960 EmpathyContactPriv *priv;
962 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
964 priv = GET_PRIV (contact);
966 return priv->is_user;
970 empathy_contact_set_is_user (EmpathyContact *contact,
973 EmpathyContactPriv *priv;
975 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
977 priv = GET_PRIV (contact);
979 if (priv->is_user == is_user)
982 priv->is_user = is_user;
984 g_object_notify (G_OBJECT (contact), "is-user");
988 empathy_contact_is_online (EmpathyContact *contact)
990 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
992 switch (empathy_contact_get_presence (contact))
994 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
995 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
996 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1004 empathy_contact_get_status (EmpathyContact *contact)
1006 const gchar *message;
1008 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1010 message = empathy_contact_get_presence_message (contact);
1011 if (!EMP_STR_EMPTY (message))
1014 return empathy_presence_get_default_message (
1015 empathy_contact_get_presence (contact));
1019 empathy_contact_can_voip (EmpathyContact *contact)
1021 EmpathyContactPriv *priv;
1023 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1025 priv = GET_PRIV (contact);
1027 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1028 EMPATHY_CAPABILITIES_VIDEO);
1032 empathy_contact_can_voip_audio (EmpathyContact *contact)
1034 EmpathyContactPriv *priv;
1036 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1038 priv = GET_PRIV (contact);
1040 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1044 empathy_contact_can_voip_video (EmpathyContact *contact)
1046 EmpathyContactPriv *priv;
1048 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1050 priv = GET_PRIV (contact);
1052 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1056 empathy_contact_can_send_files (EmpathyContact *contact)
1058 EmpathyContactPriv *priv;
1060 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1062 priv = GET_PRIV (contact);
1064 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1068 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1070 EmpathyContactPriv *priv;
1072 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1074 priv = GET_PRIV (contact);
1076 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1080 contact_get_avatar_filename (EmpathyContact *contact,
1086 gchar *token_escaped;
1088 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1091 token_escaped = tp_escape_as_identifier (token);
1092 account = empathy_contact_get_account (contact);
1094 avatar_path = g_build_filename (g_get_user_cache_dir (),
1097 tp_account_get_connection_manager (account),
1098 tp_account_get_protocol (account),
1100 g_mkdir_with_parents (avatar_path, 0700);
1102 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1104 g_free (token_escaped);
1105 g_free (avatar_path);
1111 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1114 EmpathyAvatar *avatar = NULL;
1118 GError *error = NULL;
1120 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1121 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1123 /* Load the avatar from file if it exists */
1124 filename = contact_get_avatar_filename (contact, token);
1125 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1127 if (!g_file_get_contents (filename, &data, &len, &error))
1129 DEBUG ("Failed to load avatar from cache: %s",
1130 error ? error->message : "No error given");
1131 g_clear_error (&error);
1137 DEBUG ("Avatar loaded from %s", filename);
1138 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1140 empathy_contact_set_avatar (contact, avatar);
1141 empathy_avatar_unref (avatar);
1148 return data != NULL;
1152 empathy_avatar_get_type (void)
1154 static GType type_id = 0;
1158 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1159 (GBoxedCopyFunc) empathy_avatar_ref,
1160 (GBoxedFreeFunc) empathy_avatar_unref);
1167 * empathy_avatar_new:
1168 * @data: the avatar data
1169 * @len: the size of avatar data
1170 * @format: the mime type of the avatar image
1171 * @token: the token of the avatar
1172 * @filename: the filename where the avatar is stored in cache
1174 * Create a #EmpathyAvatar from the provided data. This function takes the
1175 * ownership of @data, @format, @token and @filename.
1177 * Returns: a new #EmpathyAvatar
1180 empathy_avatar_new (guchar *data,
1186 EmpathyAvatar *avatar;
1188 avatar = g_slice_new0 (EmpathyAvatar);
1189 avatar->data = data;
1191 avatar->format = format;
1192 avatar->token = token;
1193 avatar->filename = filename;
1194 avatar->refcount = 1;
1200 empathy_avatar_unref (EmpathyAvatar *avatar)
1202 g_return_if_fail (avatar != NULL);
1205 if (avatar->refcount == 0)
1207 g_free (avatar->data);
1208 g_free (avatar->format);
1209 g_free (avatar->token);
1210 g_free (avatar->filename);
1211 g_slice_free (EmpathyAvatar, avatar);
1216 empathy_avatar_ref (EmpathyAvatar *avatar)
1218 g_return_val_if_fail (avatar != NULL, NULL);
1226 * empathy_avatar_save_to_file:
1227 * @avatar: the avatar
1228 * @filename: name of a file to write avatar to
1229 * @error: return location for a GError, or NULL
1231 * Save the avatar to a file named filename
1233 * Returns: %TRUE on success, %FALSE if an error occurred
1236 empathy_avatar_save_to_file (EmpathyAvatar *self,
1237 const gchar *filename,
1240 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1245 * empathy_contact_get_location:
1246 * @contact: an #EmpathyContact
1248 * Returns the user's location if available. The keys are defined in
1249 * empathy-location.h. If the contact doesn't have location
1250 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1251 * you are done with the #GHashTable.
1253 * It is composed of string keys and GValues. Keys are
1254 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1255 * Example: a "city" key would have "Helsinki" as string GValue,
1256 * a "latitude" would have 65.0 as double GValue.
1258 * Returns: a #GHashTable of location values
1261 empathy_contact_get_location (EmpathyContact *contact)
1263 EmpathyContactPriv *priv;
1265 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1267 priv = GET_PRIV (contact);
1269 return priv->location;
1273 * empathy_contact_set_location:
1274 * @contact: an #EmpathyContact
1275 * @location: a #GHashTable of the location
1277 * Sets the user's location based on the location #GHashTable passed.
1278 * It is composed of string keys and GValues. Keys are
1279 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1280 * Example: a "city" key would have "Helsinki" as string GValue,
1281 * a "latitude" would have 65.0 as double GValue.
1284 empathy_contact_set_location (EmpathyContact *contact,
1285 GHashTable *location)
1287 EmpathyContactPriv *priv;
1289 g_return_if_fail (EMPATHY_CONTACT (contact));
1290 g_return_if_fail (location != NULL);
1292 priv = GET_PRIV (contact);
1294 if (priv->location != NULL)
1295 g_hash_table_unref (priv->location);
1297 priv->location = g_hash_table_ref (location);
1299 update_geocode (contact);
1301 g_object_notify (G_OBJECT (contact), "location");
1305 * empathy_contact_equal:
1306 * @contact1: an #EmpathyContact
1307 * @contact2: an #EmpathyContact
1309 * Returns FALSE if one of the contacts is NULL but the other is not.
1310 * Otherwise returns TRUE if both pointer are equal or if they bith
1311 * refer to the same id.
1312 * It's only necessary to call this function if your contact objects
1313 * come from logs where contacts are created dynamically and comparing
1314 * pointers is not enough.
1317 empathy_contact_equal (gconstpointer contact1,
1318 gconstpointer contact2)
1325 if ((contact1 == NULL) != (contact2 == NULL)) {
1328 if (contact1 == contact2) {
1331 c1 = EMPATHY_CONTACT (contact1);
1332 c2 = EMPATHY_CONTACT (contact2);
1333 id1 = empathy_contact_get_id (c1);
1334 id2 = empathy_contact_get_id (c2);
1335 if (!tp_strdiff (id1, id2)) {
1342 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1343 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1345 /* This callback is called by geoclue when it found a position
1346 * for the given address. A position is necessary for a contact
1347 * to show up on the map
1350 geocode_cb (GeoclueGeocode *geocode,
1351 GeocluePositionFields fields,
1355 GeoclueAccuracy *accuracy,
1359 EmpathyContactPriv *priv = GET_PRIV (contact);
1360 GHashTable *new_location;
1362 if (priv->location == NULL)
1367 DEBUG ("Error geocoding location : %s", error->message);
1371 /* No need to change location if we didn't find the position */
1372 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1375 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1378 new_location = tp_asv_new (
1379 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1380 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1383 DEBUG ("\t - Latitude: %f", latitude);
1384 DEBUG ("\t - Longitude: %f", longitude);
1386 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1387 * the values we just set. */
1388 tp_g_hash_table_update (new_location, priv->location,
1389 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1391 /* Set the altitude only if it wasn't defined before */
1392 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1393 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1395 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1397 DEBUG ("\t - Altitude: %f", altitude);
1400 /* Don't change the accuracy as we used an address to get this position */
1401 g_hash_table_unref (priv->location);
1402 priv->location = new_location;
1403 g_object_notify (contact, "location");
1405 g_object_unref (geocode);
1406 g_object_unref (contact);
1410 get_dup_string (GHashTable *location,
1415 value = g_hash_table_lookup (location, key);
1417 return g_value_dup_string (value);
1423 update_geocode (EmpathyContact *contact)
1425 static GeoclueGeocode *geocode;
1427 GHashTable *address;
1428 GHashTable *location;
1430 location = empathy_contact_get_location (contact);
1431 if (location == NULL)
1434 /* No need to search for position if contact published it */
1435 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1436 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1439 if (geocode == NULL)
1441 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1442 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1446 g_object_ref (geocode);
1449 address = geoclue_address_details_new ();
1451 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1454 g_hash_table_insert (address,
1455 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1456 DEBUG ("\t - countrycode: %s", str);
1459 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1462 g_hash_table_insert (address,
1463 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1464 DEBUG ("\t - country: %s", str);
1467 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1470 g_hash_table_insert (address,
1471 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1472 DEBUG ("\t - postalcode: %s", str);
1475 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1478 g_hash_table_insert (address,
1479 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1480 DEBUG ("\t - region: %s", str);
1483 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1486 g_hash_table_insert (address,
1487 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1488 DEBUG ("\t - locality: %s", str);
1491 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1494 g_hash_table_insert (address,
1495 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1496 DEBUG ("\t - street: %s", str);
1499 if (g_hash_table_size (address) > 0)
1501 g_object_ref (contact);
1503 geoclue_geocode_address_to_position_async (geocode, address,
1504 geocode_cb, contact);
1507 g_hash_table_unref (address);
1511 static EmpathyCapabilities
1512 tp_caps_to_capabilities (TpCapabilities *caps)
1514 EmpathyCapabilities capabilities = 0;
1518 classes = tp_capabilities_get_channel_classes (caps);
1520 for (i = 0; i < classes->len; i++)
1522 GValueArray *class_struct;
1523 GHashTable *fixed_prop;
1525 TpHandleType handle_type;
1526 const gchar *chan_type;
1528 class_struct = g_ptr_array_index (classes, i);
1529 tp_value_array_unpack (class_struct, 2,
1533 handle_type = tp_asv_get_uint32 (fixed_prop,
1534 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1535 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1538 chan_type = tp_asv_get_string (fixed_prop,
1539 TP_PROP_CHANNEL_CHANNEL_TYPE);
1541 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1543 capabilities |= EMPATHY_CAPABILITIES_FT;
1545 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1547 const gchar *service;
1549 service = tp_asv_get_string (fixed_prop,
1550 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1552 if (!tp_strdiff (service, "rfb"))
1553 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1555 else if (!tp_strdiff (chan_type,
1556 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1560 for (j = 0; allowed_prop[j] != NULL; j++)
1562 if (!tp_strdiff (allowed_prop[j],
1563 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1564 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1565 else if (!tp_strdiff (allowed_prop[j],
1566 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1567 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1572 return capabilities;
1576 set_capabilities_from_tp_caps (EmpathyContact *self,
1577 TpCapabilities *caps)
1579 EmpathyCapabilities capabilities;
1584 capabilities = tp_caps_to_capabilities (caps);
1585 empathy_contact_set_capabilities (self, capabilities);
1589 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1591 EmpathyContactPriv *priv = GET_PRIV (contact);
1596 token = tp_contact_get_avatar_token (priv->tp_contact);
1597 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1598 file = tp_contact_get_avatar_file (priv->tp_contact);
1602 EmpathyAvatar *avatar;
1606 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1607 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1608 g_file_get_path (file));
1609 empathy_contact_set_avatar (contact, avatar);
1610 empathy_avatar_unref (avatar);
1614 empathy_contact_set_avatar (contact, NULL);
1619 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1621 EmpathyContact *contact = NULL;
1623 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1625 if (contacts_table == NULL)
1626 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1628 contact = g_hash_table_lookup (contacts_table, tp_contact);
1630 if (contact == NULL)
1632 contact = empathy_contact_new (tp_contact);
1634 /* The hash table does not keep any ref.
1635 * contact keeps a ref to tp_contact, and is removed from the table in
1636 * contact_dispose() */
1637 g_hash_table_insert (contacts_table, tp_contact, contact);
1641 g_object_ref (contact);