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;
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.
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 (EmpathyContact *contact,
89 EmpathyAvatar *avatar);
90 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
91 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
94 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
106 PROP_PRESENCE_MESSAGE,
118 static guint signals[LAST_SIGNAL];
120 /* TpContact* -> EmpathyContact*, both borrowed ref */
121 static GHashTable *contacts_table = NULL;
124 tp_contact_notify_cb (TpContact *tp_contact,
128 EmpathyContactPriv *priv = GET_PRIV (contact);
130 /* Forward property notifications */
131 if (!tp_strdiff (param->name, "alias"))
132 g_object_notify (contact, "alias");
133 else if (!tp_strdiff (param->name, "presence-type")) {
134 TpConnectionPresenceType presence;
136 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
137 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
139 priv->presence = presence;
140 g_object_notify (contact, "presence");
142 else if (!tp_strdiff (param->name, "identifier"))
143 g_object_notify (contact, "id");
144 else if (!tp_strdiff (param->name, "handle"))
145 g_object_notify (contact, "handle");
146 else if (!tp_strdiff (param->name, "location"))
148 GHashTable *location;
150 location = tp_contact_get_location (tp_contact);
151 /* This will start a geoclue search to find the address if needed */
152 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
154 else if (!tp_strdiff (param->name, "capabilities"))
156 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
157 tp_contact_get_capabilities (tp_contact));
159 else if (!tp_strdiff (param->name, "avatar-file"))
161 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
166 folks_persona_notify_cb (FolksPersona *folks_persona,
170 if (!tp_strdiff (param->name, "presence-message"))
171 g_object_notify (contact, "presence-message");
175 contact_dispose (GObject *object)
177 EmpathyContactPriv *priv = GET_PRIV (object);
179 if (priv->tp_contact)
181 g_hash_table_remove (contacts_table, priv->tp_contact);
182 g_signal_handlers_disconnect_by_func (priv->tp_contact,
183 tp_contact_notify_cb, object);
184 g_object_unref (priv->tp_contact);
186 priv->tp_contact = NULL;
189 g_object_unref (priv->account);
190 priv->account = NULL;
194 g_signal_handlers_disconnect_by_func (priv->persona,
195 folks_persona_notify_cb, object);
196 g_object_unref (priv->persona);
198 priv->persona = NULL;
200 if (priv->avatar != NULL)
202 empathy_avatar_unref (priv->avatar);
206 if (priv->location != NULL)
208 g_hash_table_unref (priv->location);
209 priv->location = NULL;
212 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
216 contact_constructed (GObject *object)
218 EmpathyContact *contact = (EmpathyContact *) object;
219 EmpathyContactPriv *priv = GET_PRIV (contact);
220 GHashTable *location;
221 TpHandle self_handle;
224 if (priv->tp_contact == NULL)
227 priv->presence = empathy_contact_get_presence (contact);
229 location = tp_contact_get_location (priv->tp_contact);
230 if (location != NULL)
231 empathy_contact_set_location (contact, location);
233 set_capabilities_from_tp_caps (contact,
234 tp_contact_get_capabilities (priv->tp_contact));
236 contact_set_avatar_from_tp_contact (contact);
238 /* Set is-user property. Note that it could still be the handle is
239 * different from the connection's self handle, in the case the handle
240 * comes from a group interface. */
241 self_handle = tp_connection_get_self_handle (
242 tp_contact_get_connection (priv->tp_contact));
243 handle = tp_contact_get_handle (priv->tp_contact);
244 empathy_contact_set_is_user (contact, self_handle == handle);
246 g_signal_connect (priv->tp_contact, "notify",
247 G_CALLBACK (tp_contact_notify_cb), contact);
251 empathy_contact_class_init (EmpathyContactClass *class)
253 GObjectClass *object_class;
255 object_class = G_OBJECT_CLASS (class);
257 object_class->finalize = contact_finalize;
258 object_class->dispose = contact_dispose;
259 object_class->get_property = contact_get_property;
260 object_class->set_property = contact_set_property;
261 object_class->constructed = contact_constructed;
263 g_object_class_install_property (object_class,
265 g_param_spec_object ("tp-contact",
267 "The TpContact associated with the contact",
269 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (object_class,
273 g_param_spec_object ("account",
275 "The account associated with the contact",
277 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (object_class,
281 g_param_spec_object ("persona",
283 "The FolksPersona associated with the contact",
285 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (object_class,
289 g_param_spec_string ("id",
291 "String identifying contact",
293 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (object_class,
297 g_param_spec_string ("alias",
299 "An alias for the contact",
301 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (object_class,
305 g_param_spec_boxed ("avatar",
309 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (object_class,
313 g_param_spec_uint ("presence",
315 "Presence of contact",
316 TP_CONNECTION_PRESENCE_TYPE_UNSET,
317 NUM_TP_CONNECTION_PRESENCE_TYPES,
318 TP_CONNECTION_PRESENCE_TYPE_UNSET,
319 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (object_class,
322 PROP_PRESENCE_MESSAGE,
323 g_param_spec_string ("presence-message",
324 "Contact presence message",
325 "Presence message of contact",
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (object_class,
331 g_param_spec_uint ("handle",
333 "The handle of the contact",
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (object_class,
341 g_param_spec_flags ("capabilities",
342 "Contact Capabilities",
343 "Capabilities of the contact",
344 EMPATHY_TYPE_CAPABILITIES,
345 EMPATHY_CAPABILITIES_UNKNOWN,
346 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (object_class,
350 g_param_spec_boolean ("is-user",
352 "Is contact the user",
354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (object_class,
359 g_param_spec_boxed ("location",
361 "Physical location of the contact",
363 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
365 signals[PRESENCE_CHANGED] =
366 g_signal_new ("presence-changed",
367 G_TYPE_FROM_CLASS (class),
371 _empathy_marshal_VOID__UINT_UINT,
376 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
380 empathy_contact_init (EmpathyContact *contact)
382 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
383 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
385 contact->priv = priv;
387 priv->location = NULL;
392 contact_finalize (GObject *object)
394 EmpathyContactPriv *priv;
396 priv = GET_PRIV (object);
398 DEBUG ("finalize: %p", object);
400 if (priv->groups != NULL)
401 g_hash_table_destroy (priv->groups);
402 g_free (priv->alias);
405 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
409 empathy_contact_set_capabilities (EmpathyContact *contact,
410 EmpathyCapabilities capabilities)
412 EmpathyContactPriv *priv;
414 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
416 priv = GET_PRIV (contact);
418 if (priv->capabilities == capabilities)
421 priv->capabilities = capabilities;
423 g_object_notify (G_OBJECT (contact), "capabilities");
427 empathy_contact_set_id (EmpathyContact *contact,
430 EmpathyContactPriv *priv;
432 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
433 g_return_if_fail (id != NULL);
435 priv = GET_PRIV (contact);
437 /* We temporally ref the contact because it could be destroyed
438 * during the signal emition */
439 g_object_ref (contact);
440 if (tp_strdiff (id, priv->id))
443 priv->id = g_strdup (id);
445 g_object_notify (G_OBJECT (contact), "id");
446 if (EMP_STR_EMPTY (priv->alias))
447 g_object_notify (G_OBJECT (contact), "alias");
450 g_object_unref (contact);
454 empathy_contact_set_presence (EmpathyContact *contact,
455 TpConnectionPresenceType presence)
457 EmpathyContactPriv *priv;
458 TpConnectionPresenceType old_presence;
460 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
462 priv = GET_PRIV (contact);
464 if (presence == priv->presence)
467 old_presence = priv->presence;
468 priv->presence = presence;
470 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
472 g_object_notify (G_OBJECT (contact), "presence");
476 empathy_contact_set_presence_message (EmpathyContact *contact,
477 const gchar *message)
479 EmpathyContactPriv *priv = GET_PRIV (contact);
481 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
483 if (priv->persona != NULL)
485 folks_presence_set_presence_message (FOLKS_PRESENCE (priv->persona),
491 empathy_contact_set_handle (EmpathyContact *contact,
494 EmpathyContactPriv *priv;
496 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
498 priv = GET_PRIV (contact);
500 g_object_ref (contact);
501 if (handle != priv->handle)
503 priv->handle = handle;
504 g_object_notify (G_OBJECT (contact), "handle");
506 g_object_unref (contact);
510 contact_get_property (GObject *object,
515 EmpathyContact *contact = EMPATHY_CONTACT (object);
519 case PROP_TP_CONTACT:
520 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
523 g_value_set_object (value, empathy_contact_get_account (contact));
526 g_value_set_object (value, empathy_contact_get_persona (contact));
529 g_value_set_string (value, empathy_contact_get_id (contact));
532 g_value_set_string (value, empathy_contact_get_alias (contact));
535 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
538 g_value_set_uint (value, empathy_contact_get_presence (contact));
540 case PROP_PRESENCE_MESSAGE:
541 g_value_set_string (value, empathy_contact_get_presence_message (contact));
544 g_value_set_uint (value, empathy_contact_get_handle (contact));
546 case PROP_CAPABILITIES:
547 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
550 g_value_set_boolean (value, empathy_contact_is_user (contact));
553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
559 contact_set_property (GObject *object,
564 EmpathyContact *contact = EMPATHY_CONTACT (object);
565 EmpathyContactPriv *priv = GET_PRIV (object);
569 case PROP_TP_CONTACT:
570 priv->tp_contact = g_value_dup_object (value);
573 g_assert (priv->account == NULL);
574 priv->account = g_value_dup_object (value);
577 empathy_contact_set_persona (contact, g_value_get_object (value));
580 empathy_contact_set_id (contact, g_value_get_string (value));
583 empathy_contact_set_alias (contact, g_value_get_string (value));
586 empathy_contact_set_presence (contact, g_value_get_uint (value));
588 case PROP_PRESENCE_MESSAGE:
589 empathy_contact_set_presence_message (contact, g_value_get_string (value));
592 empathy_contact_set_handle (contact, g_value_get_uint (value));
594 case PROP_CAPABILITIES:
595 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
598 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
606 static EmpathyContact *
607 empathy_contact_new (TpContact *tp_contact)
609 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
611 return g_object_new (EMPATHY_TYPE_CONTACT,
612 "tp-contact", tp_contact,
617 empathy_contact_from_tpl_contact (TpAccount *account,
618 TplEntity *tpl_entity)
620 EmpathyContact *retval;
623 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
625 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
627 retval = g_object_new (EMPATHY_TYPE_CONTACT,
628 "id", tpl_entity_get_alias (tpl_entity),
629 "alias", tpl_entity_get_identifier (tpl_entity),
634 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
635 contact_load_avatar_cache (retval,
636 tpl_entity_get_avatar_token (tpl_entity));
642 empathy_contact_get_tp_contact (EmpathyContact *contact)
644 EmpathyContactPriv *priv;
646 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
648 priv = GET_PRIV (contact);
650 return priv->tp_contact;
654 empathy_contact_get_id (EmpathyContact *contact)
656 EmpathyContactPriv *priv;
658 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
660 priv = GET_PRIV (contact);
662 if (priv->tp_contact != NULL)
663 return tp_contact_get_identifier (priv->tp_contact);
669 empathy_contact_get_alias (EmpathyContact *contact)
671 EmpathyContactPriv *priv;
674 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
676 priv = GET_PRIV (contact);
678 if (priv->tp_contact != NULL)
679 alias = tp_contact_get_alias (priv->tp_contact);
683 if (!EMP_STR_EMPTY (alias))
686 return empathy_contact_get_id (contact);
690 empathy_contact_set_alias (EmpathyContact *contact,
693 EmpathyContactPriv *priv;
694 FolksPersona *persona;
696 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
698 priv = GET_PRIV (contact);
700 g_object_ref (contact);
702 /* Set the alias on the persona if possible */
703 persona = empathy_contact_get_persona (contact);
704 if (persona != NULL && FOLKS_IS_ALIAS (persona))
706 DEBUG ("Setting alias for contact %s to %s",
707 empathy_contact_get_id (contact), alias);
709 folks_alias_set_alias (FOLKS_ALIAS (persona), alias);
712 if (tp_strdiff (alias, priv->alias))
714 g_free (priv->alias);
715 priv->alias = g_strdup (alias);
716 g_object_notify (G_OBJECT (contact), "alias");
719 g_object_unref (contact);
723 groups_change_group_cb (GObject *source,
724 GAsyncResult *result,
727 FolksGroups *groups = FOLKS_GROUPS (source);
728 GError *error = NULL;
730 folks_groups_change_group_finish (groups, result, &error);
733 g_warning ("failed to change group: %s", error->message);
734 g_clear_error (&error);
739 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
742 EmpathyContactPriv *priv;
743 FolksPersona *persona;
745 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
746 g_return_if_fail (group != NULL);
748 priv = GET_PRIV (contact);
750 /* Normally pass through the changes to the persona */
751 persona = empathy_contact_get_persona (contact);
754 if (FOLKS_IS_GROUPS (persona))
755 folks_groups_change_group (FOLKS_GROUPS (persona), group, is_member,
756 groups_change_group_cb, contact);
760 /* If the persona doesn't exist yet, we have to cache the changes until it
762 if (priv->groups == NULL)
764 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
768 g_hash_table_insert (priv->groups, g_strdup (group),
769 GUINT_TO_POINTER (is_member));
773 empathy_contact_get_avatar (EmpathyContact *contact)
775 EmpathyContactPriv *priv;
777 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
779 priv = GET_PRIV (contact);
785 contact_set_avatar (EmpathyContact *contact,
786 EmpathyAvatar *avatar)
788 EmpathyContactPriv *priv;
790 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
792 priv = GET_PRIV (contact);
794 if (priv->avatar == avatar)
799 empathy_avatar_unref (priv->avatar);
804 priv->avatar = empathy_avatar_ref (avatar);
806 g_object_notify (G_OBJECT (contact), "avatar");
810 empathy_contact_get_account (EmpathyContact *contact)
812 EmpathyContactPriv *priv;
814 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
816 priv = GET_PRIV (contact);
818 if (priv->account == NULL && priv->tp_contact != NULL)
820 TpConnection *connection;
822 /* FIXME: This assume the account manager already exists */
823 connection = tp_contact_get_connection (priv->tp_contact);
825 g_object_ref (empathy_get_account_for_connection (connection));
828 return priv->account;
832 empathy_contact_get_persona (EmpathyContact *contact)
834 EmpathyContactPriv *priv;
836 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
838 priv = GET_PRIV (contact);
840 if (priv->persona == NULL && priv->tp_contact != NULL)
842 /* FIXME: This is disgustingly slow */
843 /* Query for the persona */
844 EmpathyIndividualManager *manager;
845 GList *individuals, *l;
847 manager = empathy_individual_manager_dup_singleton ();
848 individuals = empathy_individual_manager_get_members (manager);
850 for (l = individuals; l != NULL; l = l->next)
853 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
855 personas = folks_individual_get_personas (individual);
856 for (j = personas; j != NULL; j = j->next)
858 TpfPersona *persona = j->data;
860 if (TPF_IS_PERSONA (persona))
862 TpContact *tp_contact = tpf_persona_get_contact (persona);
864 if (tp_contact == priv->tp_contact)
866 /* Found the right persona */
867 empathy_contact_set_persona (contact,
868 (FolksPersona *) persona);
876 g_list_free (individuals);
877 g_object_unref (manager);
880 return priv->persona;
884 empathy_contact_set_persona (EmpathyContact *contact,
885 FolksPersona *persona)
887 EmpathyContactPriv *priv;
889 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
890 g_return_if_fail (TPF_IS_PERSONA (persona));
892 priv = GET_PRIV (contact);
894 if (persona == priv->persona)
897 if (priv->persona != NULL)
899 g_signal_handlers_disconnect_by_func (priv->persona,
900 folks_persona_notify_cb, contact);
901 g_object_unref (priv->persona);
903 priv->persona = g_object_ref (persona);
905 g_signal_connect (priv->persona, "notify",
906 G_CALLBACK (folks_persona_notify_cb), contact);
908 g_object_notify (G_OBJECT (contact), "persona");
910 /* Set the persona's alias, since ours could've been set using
911 * empathy_contact_set_alias() before we had a persona; this happens when
912 * adding a contact. */
913 if (priv->alias != NULL)
914 empathy_contact_set_alias (contact, priv->alias);
916 /* Set the persona's groups */
917 if (priv->groups != NULL)
919 folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
920 g_hash_table_destroy (priv->groups);
926 empathy_contact_get_connection (EmpathyContact *contact)
928 EmpathyContactPriv *priv;
930 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
932 priv = GET_PRIV (contact);
934 if (priv->tp_contact != NULL)
935 return tp_contact_get_connection (priv->tp_contact);
940 TpConnectionPresenceType
941 empathy_contact_get_presence (EmpathyContact *contact)
943 EmpathyContactPriv *priv;
945 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
946 TP_CONNECTION_PRESENCE_TYPE_UNSET);
948 priv = GET_PRIV (contact);
950 if (priv->tp_contact != NULL)
951 return tp_contact_get_presence_type (priv->tp_contact);
953 return priv->presence;
957 empathy_contact_get_presence_message (EmpathyContact *contact)
959 EmpathyContactPriv *priv;
961 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
963 priv = GET_PRIV (contact);
965 if (priv->persona != NULL)
966 return folks_presence_get_presence_message (FOLKS_PRESENCE (priv->persona));
972 empathy_contact_get_handle (EmpathyContact *contact)
974 EmpathyContactPriv *priv;
976 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
978 priv = GET_PRIV (contact);
980 if (priv->tp_contact != NULL)
981 return tp_contact_get_handle (priv->tp_contact);
987 empathy_contact_get_capabilities (EmpathyContact *contact)
989 EmpathyContactPriv *priv;
991 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
993 priv = GET_PRIV (contact);
995 return priv->capabilities;
999 empathy_contact_is_user (EmpathyContact *contact)
1001 EmpathyContactPriv *priv;
1003 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1005 priv = GET_PRIV (contact);
1007 return priv->is_user;
1011 empathy_contact_set_is_user (EmpathyContact *contact,
1014 EmpathyContactPriv *priv;
1016 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1018 priv = GET_PRIV (contact);
1020 if (priv->is_user == is_user)
1023 priv->is_user = is_user;
1025 g_object_notify (G_OBJECT (contact), "is-user");
1029 empathy_contact_is_online (EmpathyContact *contact)
1031 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1033 switch (empathy_contact_get_presence (contact))
1035 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1036 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1037 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1039 /* Contacts without presence are considered online so we can display IRC
1040 * contacts in rooms. */
1041 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1042 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1043 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1044 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1045 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1046 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1053 empathy_contact_get_status (EmpathyContact *contact)
1055 const gchar *message;
1057 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1059 message = empathy_contact_get_presence_message (contact);
1060 if (!EMP_STR_EMPTY (message))
1063 return empathy_presence_get_default_message (
1064 empathy_contact_get_presence (contact));
1068 empathy_contact_can_voip (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_AUDIO |
1077 EMPATHY_CAPABILITIES_VIDEO);
1081 empathy_contact_can_voip_audio (EmpathyContact *contact)
1083 EmpathyContactPriv *priv;
1085 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1087 priv = GET_PRIV (contact);
1089 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1093 empathy_contact_can_voip_video (EmpathyContact *contact)
1095 EmpathyContactPriv *priv;
1097 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1099 priv = GET_PRIV (contact);
1101 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1105 empathy_contact_can_send_files (EmpathyContact *contact)
1107 EmpathyContactPriv *priv;
1109 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1111 priv = GET_PRIV (contact);
1113 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1117 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1119 EmpathyContactPriv *priv;
1121 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1123 priv = GET_PRIV (contact);
1125 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1129 contact_get_avatar_filename (EmpathyContact *contact,
1135 gchar *token_escaped;
1137 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1140 token_escaped = tp_escape_as_identifier (token);
1141 account = empathy_contact_get_account (contact);
1143 avatar_path = g_build_filename (g_get_user_cache_dir (),
1146 tp_account_get_connection_manager (account),
1147 tp_account_get_protocol (account),
1149 g_mkdir_with_parents (avatar_path, 0700);
1151 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1153 g_free (token_escaped);
1154 g_free (avatar_path);
1160 contact_load_avatar_cache (EmpathyContact *contact,
1163 EmpathyAvatar *avatar = NULL;
1167 GError *error = NULL;
1169 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1170 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1172 /* Load the avatar from file if it exists */
1173 filename = contact_get_avatar_filename (contact, token);
1174 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1176 if (!g_file_get_contents (filename, &data, &len, &error))
1178 DEBUG ("Failed to load avatar from cache: %s",
1179 error ? error->message : "No error given");
1180 g_clear_error (&error);
1186 DEBUG ("Avatar loaded from %s", filename);
1187 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1188 contact_set_avatar (contact, avatar);
1189 empathy_avatar_unref (avatar);
1196 return data != NULL;
1200 empathy_avatar_get_type (void)
1202 static GType type_id = 0;
1206 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1207 (GBoxedCopyFunc) empathy_avatar_ref,
1208 (GBoxedFreeFunc) empathy_avatar_unref);
1215 * empathy_avatar_new:
1216 * @data: the avatar data
1217 * @len: the size of avatar data
1218 * @format: the mime type of the avatar image
1219 * @filename: the filename where the avatar is stored in cache
1221 * Create a #EmpathyAvatar from the provided data. This function takes the
1222 * ownership of @data, @format and @filename.
1224 * Returns: a new #EmpathyAvatar
1227 empathy_avatar_new (guchar *data,
1232 EmpathyAvatar *avatar;
1234 avatar = g_slice_new0 (EmpathyAvatar);
1235 avatar->data = data;
1237 avatar->format = format;
1238 avatar->filename = filename;
1239 avatar->refcount = 1;
1245 empathy_avatar_unref (EmpathyAvatar *avatar)
1247 g_return_if_fail (avatar != NULL);
1250 if (avatar->refcount == 0)
1252 g_free (avatar->data);
1253 g_free (avatar->format);
1254 g_free (avatar->filename);
1255 g_slice_free (EmpathyAvatar, avatar);
1260 empathy_avatar_ref (EmpathyAvatar *avatar)
1262 g_return_val_if_fail (avatar != NULL, NULL);
1270 * empathy_avatar_save_to_file:
1271 * @avatar: the avatar
1272 * @filename: name of a file to write avatar to
1273 * @error: return location for a GError, or NULL
1275 * Save the avatar to a file named filename
1277 * Returns: %TRUE on success, %FALSE if an error occurred
1280 empathy_avatar_save_to_file (EmpathyAvatar *self,
1281 const gchar *filename,
1284 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1289 * empathy_contact_get_location:
1290 * @contact: an #EmpathyContact
1292 * Returns the user's location if available. The keys are defined in
1293 * empathy-location.h. If the contact doesn't have location
1294 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1295 * you are done with the #GHashTable.
1297 * It is composed of string keys and GValues. Keys are
1298 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1299 * Example: a "city" key would have "Helsinki" as string GValue,
1300 * a "latitude" would have 65.0 as double GValue.
1302 * Returns: a #GHashTable of location values
1305 empathy_contact_get_location (EmpathyContact *contact)
1307 EmpathyContactPriv *priv;
1309 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1311 priv = GET_PRIV (contact);
1313 return priv->location;
1317 * empathy_contact_set_location:
1318 * @contact: an #EmpathyContact
1319 * @location: a #GHashTable of the location
1321 * Sets the user's location based on the location #GHashTable passed.
1322 * It is composed of string keys and GValues. Keys are
1323 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1324 * Example: a "city" key would have "Helsinki" as string GValue,
1325 * a "latitude" would have 65.0 as double GValue.
1328 empathy_contact_set_location (EmpathyContact *contact,
1329 GHashTable *location)
1331 EmpathyContactPriv *priv;
1333 g_return_if_fail (EMPATHY_CONTACT (contact));
1334 g_return_if_fail (location != NULL);
1336 priv = GET_PRIV (contact);
1338 if (priv->location != NULL)
1339 g_hash_table_unref (priv->location);
1341 priv->location = g_hash_table_ref (location);
1343 update_geocode (contact);
1345 g_object_notify (G_OBJECT (contact), "location");
1349 * empathy_contact_equal:
1350 * @contact1: an #EmpathyContact
1351 * @contact2: an #EmpathyContact
1353 * Returns FALSE if one of the contacts is NULL but the other is not.
1354 * Otherwise returns TRUE if both pointer are equal or if they bith
1355 * refer to the same id.
1356 * It's only necessary to call this function if your contact objects
1357 * come from logs where contacts are created dynamically and comparing
1358 * pointers is not enough.
1361 empathy_contact_equal (gconstpointer contact1,
1362 gconstpointer contact2)
1369 if ((contact1 == NULL) != (contact2 == NULL)) {
1372 if (contact1 == contact2) {
1375 c1 = EMPATHY_CONTACT (contact1);
1376 c2 = EMPATHY_CONTACT (contact2);
1377 id1 = empathy_contact_get_id (c1);
1378 id2 = empathy_contact_get_id (c2);
1379 if (!tp_strdiff (id1, id2)) {
1386 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1387 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1389 /* This callback is called by geoclue when it found a position
1390 * for the given address. A position is necessary for a contact
1391 * to show up on the map
1394 geocode_cb (GeoclueGeocode *geocode,
1395 GeocluePositionFields fields,
1399 GeoclueAccuracy *accuracy,
1403 EmpathyContactPriv *priv = GET_PRIV (contact);
1404 GHashTable *new_location;
1406 if (priv->location == NULL)
1411 DEBUG ("Error geocoding location : %s", error->message);
1415 /* No need to change location if we didn't find the position */
1416 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1419 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1422 new_location = tp_asv_new (
1423 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1424 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1427 DEBUG ("\t - Latitude: %f", latitude);
1428 DEBUG ("\t - Longitude: %f", longitude);
1430 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1431 * the values we just set. */
1432 tp_g_hash_table_update (new_location, priv->location,
1433 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1435 /* Set the altitude only if it wasn't defined before */
1436 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1437 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1439 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1441 DEBUG ("\t - Altitude: %f", altitude);
1444 /* Don't change the accuracy as we used an address to get this position */
1445 g_hash_table_unref (priv->location);
1446 priv->location = new_location;
1447 g_object_notify (contact, "location");
1449 g_object_unref (geocode);
1450 g_object_unref (contact);
1454 get_dup_string (GHashTable *location,
1459 value = g_hash_table_lookup (location, key);
1461 return g_value_dup_string (value);
1467 update_geocode (EmpathyContact *contact)
1469 static GeoclueGeocode *geocode;
1471 GHashTable *address;
1472 GHashTable *location;
1474 location = empathy_contact_get_location (contact);
1475 if (location == NULL)
1478 /* No need to search for position if contact published it */
1479 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1480 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1483 if (geocode == NULL)
1485 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1486 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1490 g_object_ref (geocode);
1493 address = geoclue_address_details_new ();
1495 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1498 g_hash_table_insert (address,
1499 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1500 DEBUG ("\t - countrycode: %s", str);
1503 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1506 g_hash_table_insert (address,
1507 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1508 DEBUG ("\t - country: %s", str);
1511 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1514 g_hash_table_insert (address,
1515 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1516 DEBUG ("\t - postalcode: %s", str);
1519 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1522 g_hash_table_insert (address,
1523 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1524 DEBUG ("\t - region: %s", str);
1527 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1530 g_hash_table_insert (address,
1531 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1532 DEBUG ("\t - locality: %s", str);
1535 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1538 g_hash_table_insert (address,
1539 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1540 DEBUG ("\t - street: %s", str);
1543 if (g_hash_table_size (address) > 0)
1545 g_object_ref (contact);
1547 geoclue_geocode_address_to_position_async (geocode, address,
1548 geocode_cb, contact);
1551 g_hash_table_unref (address);
1555 static EmpathyCapabilities
1556 tp_caps_to_capabilities (TpCapabilities *caps)
1558 EmpathyCapabilities capabilities = 0;
1562 classes = tp_capabilities_get_channel_classes (caps);
1564 for (i = 0; i < classes->len; i++)
1566 GValueArray *class_struct;
1567 GHashTable *fixed_prop;
1569 TpHandleType handle_type;
1570 const gchar *chan_type;
1572 class_struct = g_ptr_array_index (classes, i);
1573 tp_value_array_unpack (class_struct, 2,
1577 handle_type = tp_asv_get_uint32 (fixed_prop,
1578 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1579 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1582 chan_type = tp_asv_get_string (fixed_prop,
1583 TP_PROP_CHANNEL_CHANNEL_TYPE);
1585 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1587 capabilities |= EMPATHY_CAPABILITIES_FT;
1589 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1591 const gchar *service;
1593 service = tp_asv_get_string (fixed_prop,
1594 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1596 if (!tp_strdiff (service, "rfb"))
1597 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1599 else if (!tp_strdiff (chan_type,
1600 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1604 for (j = 0; allowed_prop[j] != NULL; j++)
1606 if (!tp_strdiff (allowed_prop[j],
1607 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1608 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1609 else if (!tp_strdiff (allowed_prop[j],
1610 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1611 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1616 return capabilities;
1620 set_capabilities_from_tp_caps (EmpathyContact *self,
1621 TpCapabilities *caps)
1623 EmpathyCapabilities capabilities;
1628 capabilities = tp_caps_to_capabilities (caps);
1629 empathy_contact_set_capabilities (self, capabilities);
1633 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1635 EmpathyContactPriv *priv = GET_PRIV (contact);
1639 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1640 file = tp_contact_get_avatar_file (priv->tp_contact);
1644 EmpathyAvatar *avatar;
1648 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1649 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime),
1650 g_file_get_path (file));
1651 contact_set_avatar (contact, avatar);
1652 empathy_avatar_unref (avatar);
1656 contact_set_avatar (contact, NULL);
1661 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1663 EmpathyContact *contact = NULL;
1665 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1667 if (contacts_table == NULL)
1668 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1670 contact = g_hash_table_lookup (contacts_table, tp_contact);
1672 if (contact == NULL)
1674 contact = empathy_contact_new (tp_contact);
1676 /* The hash table does not keep any ref.
1677 * contact keeps a ref to tp_contact, and is removed from the table in
1678 * contact_dispose() */
1679 g_hash_table_insert (contacts_table, tp_contact, contact);
1683 g_object_ref (contact);