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_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, "identifier"))
139 g_object_notify (contact, "id");
140 else if (!tp_strdiff (param->name, "handle"))
141 g_object_notify (contact, "handle");
142 else if (!tp_strdiff (param->name, "location"))
144 GHashTable *location;
146 location = tp_contact_get_location (tp_contact);
147 /* This will start a geoclue search to find the address if needed */
148 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
150 else if (!tp_strdiff (param->name, "capabilities"))
152 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
153 tp_contact_get_capabilities (tp_contact));
155 else if (!tp_strdiff (param->name, "avatar-file"))
157 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
162 folks_persona_notify_cb (FolksPersona *folks_persona,
166 if (!tp_strdiff (param->name, "presence-message"))
167 g_object_notify (contact, "presence-message");
171 contact_dispose (GObject *object)
173 EmpathyContactPriv *priv = GET_PRIV (object);
175 if (priv->tp_contact)
177 g_hash_table_remove (contacts_table, priv->tp_contact);
178 g_signal_handlers_disconnect_by_func (priv->tp_contact,
179 tp_contact_notify_cb, object);
180 g_object_unref (priv->tp_contact);
182 priv->tp_contact = NULL;
185 g_object_unref (priv->account);
186 priv->account = NULL;
190 g_signal_handlers_disconnect_by_func (priv->persona,
191 folks_persona_notify_cb, object);
192 g_object_unref (priv->persona);
194 priv->persona = NULL;
196 if (priv->avatar != NULL)
198 empathy_avatar_unref (priv->avatar);
202 if (priv->location != NULL)
204 g_hash_table_unref (priv->location);
205 priv->location = NULL;
208 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
212 empathy_contact_class_init (EmpathyContactClass *class)
214 GObjectClass *object_class;
216 object_class = G_OBJECT_CLASS (class);
218 object_class->finalize = contact_finalize;
219 object_class->dispose = contact_dispose;
220 object_class->get_property = contact_get_property;
221 object_class->set_property = contact_set_property;
223 g_object_class_install_property (object_class,
225 g_param_spec_object ("tp-contact",
227 "The TpContact associated with the contact",
229 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 g_object_class_install_property (object_class,
233 g_param_spec_object ("account",
235 "The account associated with the contact",
237 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (object_class,
241 g_param_spec_object ("persona",
243 "The FolksPersona associated with the contact",
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 g_object_class_install_property (object_class,
249 g_param_spec_string ("id",
251 "String identifying contact",
253 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (object_class,
257 g_param_spec_string ("alias",
259 "An alias for the contact",
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (object_class,
265 g_param_spec_boxed ("avatar",
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (object_class,
273 g_param_spec_uint ("presence",
275 "Presence of contact",
276 TP_CONNECTION_PRESENCE_TYPE_UNSET,
277 NUM_TP_CONNECTION_PRESENCE_TYPES,
278 TP_CONNECTION_PRESENCE_TYPE_UNSET,
279 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (object_class,
282 PROP_PRESENCE_MESSAGE,
283 g_param_spec_string ("presence-message",
284 "Contact presence message",
285 "Presence message of contact",
287 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (object_class,
291 g_param_spec_uint ("handle",
293 "The handle of the contact",
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (object_class,
301 g_param_spec_flags ("capabilities",
302 "Contact Capabilities",
303 "Capabilities of the contact",
304 EMPATHY_TYPE_CAPABILITIES,
305 EMPATHY_CAPABILITIES_UNKNOWN,
306 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (object_class,
310 g_param_spec_boolean ("is-user",
312 "Is contact the user",
314 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (object_class,
319 g_param_spec_boxed ("location",
321 "Physical location of the contact",
323 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
325 signals[PRESENCE_CHANGED] =
326 g_signal_new ("presence-changed",
327 G_TYPE_FROM_CLASS (class),
331 _empathy_marshal_VOID__UINT_UINT,
336 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
340 empathy_contact_init (EmpathyContact *contact)
342 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
343 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
345 contact->priv = priv;
347 priv->location = NULL;
352 contact_finalize (GObject *object)
354 EmpathyContactPriv *priv;
356 priv = GET_PRIV (object);
358 DEBUG ("finalize: %p", object);
360 if (priv->groups != NULL)
361 g_hash_table_destroy (priv->groups);
362 g_free (priv->alias);
365 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
369 set_tp_contact (EmpathyContact *contact,
370 TpContact *tp_contact)
372 EmpathyContactPriv *priv = GET_PRIV (contact);
373 GHashTable *location;
374 TpHandle self_handle;
377 if (tp_contact == NULL)
380 g_assert (priv->tp_contact == NULL);
381 priv->tp_contact = g_object_ref (tp_contact);
382 priv->presence = empathy_contact_get_presence (contact);
384 location = tp_contact_get_location (tp_contact);
385 if (location != NULL)
386 empathy_contact_set_location (contact, location);
388 set_capabilities_from_tp_caps (contact,
389 tp_contact_get_capabilities (tp_contact));
391 contact_set_avatar_from_tp_contact (contact);
393 /* Set is-user property. Note that it could still be the handle is
394 * different from the connection's self handle, in the case the handle
395 * comes from a group interface. */
396 self_handle = tp_connection_get_self_handle (
397 tp_contact_get_connection (tp_contact));
398 handle = tp_contact_get_handle (tp_contact);
399 empathy_contact_set_is_user (contact, self_handle == handle);
401 g_signal_connect (priv->tp_contact, "notify",
402 G_CALLBACK (tp_contact_notify_cb), contact);
406 contact_get_property (GObject *object,
411 EmpathyContact *contact = EMPATHY_CONTACT (object);
415 case PROP_TP_CONTACT:
416 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
419 g_value_set_object (value, empathy_contact_get_account (contact));
422 g_value_set_object (value, empathy_contact_get_persona (contact));
425 g_value_set_string (value, empathy_contact_get_id (contact));
428 g_value_set_string (value, empathy_contact_get_alias (contact));
431 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
434 g_value_set_uint (value, empathy_contact_get_presence (contact));
436 case PROP_PRESENCE_MESSAGE:
437 g_value_set_string (value, empathy_contact_get_presence_message (contact));
440 g_value_set_uint (value, empathy_contact_get_handle (contact));
442 case PROP_CAPABILITIES:
443 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
446 g_value_set_boolean (value, empathy_contact_is_user (contact));
449 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
455 contact_set_property (GObject *object,
460 EmpathyContact *contact = EMPATHY_CONTACT (object);
461 EmpathyContactPriv *priv = GET_PRIV (object);
465 case PROP_TP_CONTACT:
466 set_tp_contact (contact, g_value_get_object (value));
469 g_assert (priv->account == NULL);
470 priv->account = g_value_dup_object (value);
473 empathy_contact_set_persona (contact, g_value_get_object (value));
476 empathy_contact_set_id (contact, g_value_get_string (value));
479 empathy_contact_set_alias (contact, g_value_get_string (value));
482 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
485 empathy_contact_set_presence (contact, g_value_get_uint (value));
487 case PROP_PRESENCE_MESSAGE:
488 empathy_contact_set_presence_message (contact, g_value_get_string (value));
491 empathy_contact_set_handle (contact, g_value_get_uint (value));
493 case PROP_CAPABILITIES:
494 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
497 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
506 empathy_contact_new (TpContact *tp_contact)
508 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
510 return g_object_new (EMPATHY_TYPE_CONTACT,
511 "tp-contact", tp_contact,
516 empathy_contact_from_tpl_contact (TpAccount *account,
517 TplEntity *tpl_entity)
519 EmpathyContact *retval;
522 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
524 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
526 retval = g_object_new (EMPATHY_TYPE_CONTACT,
527 "id", tpl_entity_get_alias (tpl_entity),
528 "alias", tpl_entity_get_identifier (tpl_entity),
533 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
534 empathy_contact_load_avatar_cache (retval,
535 tpl_entity_get_avatar_token (tpl_entity));
541 empathy_contact_new_for_log (TpAccount *account,
546 g_return_val_if_fail (id != NULL, NULL);
547 g_assert (account != NULL);
549 return g_object_new (EMPATHY_TYPE_CONTACT,
558 empathy_contact_get_tp_contact (EmpathyContact *contact)
560 EmpathyContactPriv *priv;
562 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
564 priv = GET_PRIV (contact);
566 return priv->tp_contact;
570 empathy_contact_get_id (EmpathyContact *contact)
572 EmpathyContactPriv *priv;
574 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
576 priv = GET_PRIV (contact);
578 if (priv->tp_contact != NULL)
579 return tp_contact_get_identifier (priv->tp_contact);
585 empathy_contact_set_id (EmpathyContact *contact,
588 EmpathyContactPriv *priv;
590 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
591 g_return_if_fail (id != NULL);
593 priv = GET_PRIV (contact);
595 /* We temporally ref the contact because it could be destroyed
596 * during the signal emition */
597 g_object_ref (contact);
598 if (tp_strdiff (id, priv->id))
601 priv->id = g_strdup (id);
603 g_object_notify (G_OBJECT (contact), "id");
604 if (EMP_STR_EMPTY (priv->alias))
605 g_object_notify (G_OBJECT (contact), "alias");
608 g_object_unref (contact);
612 empathy_contact_get_alias (EmpathyContact *contact)
614 EmpathyContactPriv *priv;
617 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
619 priv = GET_PRIV (contact);
621 if (priv->tp_contact != NULL)
622 alias = tp_contact_get_alias (priv->tp_contact);
626 if (!EMP_STR_EMPTY (alias))
629 return empathy_contact_get_id (contact);
633 empathy_contact_set_alias (EmpathyContact *contact,
636 EmpathyContactPriv *priv;
637 FolksPersona *persona;
639 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
641 priv = GET_PRIV (contact);
643 g_object_ref (contact);
645 /* Set the alias on the persona if possible */
646 persona = empathy_contact_get_persona (contact);
647 if (persona != NULL && FOLKS_IS_ALIAS (persona))
649 DEBUG ("Setting alias for contact %s to %s",
650 empathy_contact_get_id (contact), alias);
652 folks_alias_set_alias (FOLKS_ALIAS (persona), alias);
655 if (tp_strdiff (alias, priv->alias))
657 g_free (priv->alias);
658 priv->alias = g_strdup (alias);
659 g_object_notify (G_OBJECT (contact), "alias");
662 g_object_unref (contact);
666 groups_change_group_cb (GObject *source,
667 GAsyncResult *result,
670 FolksGroups *groups = FOLKS_GROUPS (source);
671 GError *error = NULL;
673 folks_groups_change_group_finish (groups, result, &error);
676 g_warning ("failed to change group: %s", error->message);
677 g_clear_error (&error);
682 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
685 EmpathyContactPriv *priv;
686 FolksPersona *persona;
688 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
689 g_return_if_fail (group != NULL);
691 priv = GET_PRIV (contact);
693 /* Normally pass through the changes to the persona */
694 persona = empathy_contact_get_persona (contact);
697 if (FOLKS_IS_GROUPS (persona))
698 folks_groups_change_group (FOLKS_GROUPS (persona), group, is_member,
699 groups_change_group_cb, contact);
703 /* If the persona doesn't exist yet, we have to cache the changes until it
705 if (priv->groups == NULL)
707 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
711 g_hash_table_insert (priv->groups, g_strdup (group),
712 GUINT_TO_POINTER (is_member));
716 empathy_contact_get_avatar (EmpathyContact *contact)
718 EmpathyContactPriv *priv;
720 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
722 priv = GET_PRIV (contact);
728 empathy_contact_set_avatar (EmpathyContact *contact,
729 EmpathyAvatar *avatar)
731 EmpathyContactPriv *priv;
733 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
735 priv = GET_PRIV (contact);
737 if (priv->avatar == avatar)
742 empathy_avatar_unref (priv->avatar);
747 priv->avatar = empathy_avatar_ref (avatar);
749 g_object_notify (G_OBJECT (contact), "avatar");
753 empathy_contact_get_account (EmpathyContact *contact)
755 EmpathyContactPriv *priv;
757 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
759 priv = GET_PRIV (contact);
761 if (priv->account == NULL && priv->tp_contact != NULL)
763 TpConnection *connection;
765 /* FIXME: This assume the account manager already exists */
766 connection = tp_contact_get_connection (priv->tp_contact);
768 g_object_ref (empathy_get_account_for_connection (connection));
771 return priv->account;
775 empathy_contact_get_persona (EmpathyContact *contact)
777 EmpathyContactPriv *priv;
779 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
781 priv = GET_PRIV (contact);
783 if (priv->persona == NULL && priv->tp_contact != NULL)
785 /* FIXME: This is disgustingly slow */
786 /* Query for the persona */
787 EmpathyIndividualManager *manager;
788 GList *individuals, *l;
790 manager = empathy_individual_manager_dup_singleton ();
791 individuals = empathy_individual_manager_get_members (manager);
793 for (l = individuals; l != NULL; l = l->next)
796 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
798 personas = folks_individual_get_personas (individual);
799 for (j = personas; j != NULL; j = j->next)
801 TpfPersona *persona = j->data;
803 if (TPF_IS_PERSONA (persona))
805 TpContact *tp_contact = tpf_persona_get_contact (persona);
807 if (tp_contact == priv->tp_contact)
809 /* Found the right persona */
810 priv->persona = g_object_ref (persona);
818 g_list_free (individuals);
819 g_object_unref (manager);
822 return priv->persona;
826 empathy_contact_set_persona (EmpathyContact *contact,
827 FolksPersona *persona)
829 EmpathyContactPriv *priv;
831 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
832 g_return_if_fail (TPF_IS_PERSONA (persona));
834 priv = GET_PRIV (contact);
836 if (persona == priv->persona)
839 if (priv->persona != NULL)
840 g_object_unref (priv->persona);
841 priv->persona = g_object_ref (persona);
843 g_signal_connect (priv->persona, "notify",
844 G_CALLBACK (folks_persona_notify_cb), contact);
846 g_object_notify (G_OBJECT (contact), "persona");
848 /* Set the persona's alias, since ours could've been set using
849 * empathy_contact_set_alias() before we had a persona; this happens when
850 * adding a contact. */
851 empathy_contact_set_alias (contact, priv->alias);
853 /* Set the persona's groups */
854 if (priv->groups != NULL)
856 folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
857 g_hash_table_destroy (priv->groups);
863 empathy_contact_get_connection (EmpathyContact *contact)
865 EmpathyContactPriv *priv;
867 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
869 priv = GET_PRIV (contact);
871 if (priv->tp_contact != NULL)
872 return tp_contact_get_connection (priv->tp_contact);
877 TpConnectionPresenceType
878 empathy_contact_get_presence (EmpathyContact *contact)
880 EmpathyContactPriv *priv;
882 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
883 TP_CONNECTION_PRESENCE_TYPE_UNSET);
885 priv = GET_PRIV (contact);
887 if (priv->tp_contact != NULL)
888 return tp_contact_get_presence_type (priv->tp_contact);
890 return priv->presence;
894 empathy_contact_set_presence (EmpathyContact *contact,
895 TpConnectionPresenceType presence)
897 EmpathyContactPriv *priv;
898 TpConnectionPresenceType old_presence;
900 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
902 priv = GET_PRIV (contact);
904 if (presence == priv->presence)
907 old_presence = priv->presence;
908 priv->presence = presence;
910 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
912 g_object_notify (G_OBJECT (contact), "presence");
916 empathy_contact_get_presence_message (EmpathyContact *contact)
918 EmpathyContactPriv *priv;
920 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
922 priv = GET_PRIV (contact);
924 if (priv->persona != NULL)
925 return folks_presence_get_presence_message (FOLKS_PRESENCE (priv->persona));
931 empathy_contact_set_presence_message (EmpathyContact *contact,
932 const gchar *message)
934 EmpathyContactPriv *priv = GET_PRIV (contact);
936 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
938 if (priv->persona != NULL)
940 folks_presence_set_presence_message (FOLKS_PRESENCE (priv->persona),
946 empathy_contact_get_handle (EmpathyContact *contact)
948 EmpathyContactPriv *priv;
950 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
952 priv = GET_PRIV (contact);
954 if (priv->tp_contact != NULL)
955 return tp_contact_get_handle (priv->tp_contact);
961 empathy_contact_set_handle (EmpathyContact *contact,
964 EmpathyContactPriv *priv;
966 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
968 priv = GET_PRIV (contact);
970 g_object_ref (contact);
971 if (handle != priv->handle)
973 priv->handle = handle;
974 g_object_notify (G_OBJECT (contact), "handle");
976 g_object_unref (contact);
980 empathy_contact_get_capabilities (EmpathyContact *contact)
982 EmpathyContactPriv *priv;
984 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
986 priv = GET_PRIV (contact);
988 return priv->capabilities;
992 empathy_contact_set_capabilities (EmpathyContact *contact,
993 EmpathyCapabilities capabilities)
995 EmpathyContactPriv *priv;
997 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
999 priv = GET_PRIV (contact);
1001 if (priv->capabilities == capabilities)
1004 priv->capabilities = capabilities;
1006 g_object_notify (G_OBJECT (contact), "capabilities");
1010 empathy_contact_is_user (EmpathyContact *contact)
1012 EmpathyContactPriv *priv;
1014 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1016 priv = GET_PRIV (contact);
1018 return priv->is_user;
1022 empathy_contact_set_is_user (EmpathyContact *contact,
1025 EmpathyContactPriv *priv;
1027 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1029 priv = GET_PRIV (contact);
1031 if (priv->is_user == is_user)
1034 priv->is_user = is_user;
1036 g_object_notify (G_OBJECT (contact), "is-user");
1040 empathy_contact_is_online (EmpathyContact *contact)
1042 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1044 switch (empathy_contact_get_presence (contact))
1046 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1047 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1048 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1056 empathy_contact_get_status (EmpathyContact *contact)
1058 const gchar *message;
1060 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1062 message = empathy_contact_get_presence_message (contact);
1063 if (!EMP_STR_EMPTY (message))
1066 return empathy_presence_get_default_message (
1067 empathy_contact_get_presence (contact));
1071 empathy_contact_can_voip (EmpathyContact *contact)
1073 EmpathyContactPriv *priv;
1075 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1077 priv = GET_PRIV (contact);
1079 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1080 EMPATHY_CAPABILITIES_VIDEO);
1084 empathy_contact_can_voip_audio (EmpathyContact *contact)
1086 EmpathyContactPriv *priv;
1088 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1090 priv = GET_PRIV (contact);
1092 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1096 empathy_contact_can_voip_video (EmpathyContact *contact)
1098 EmpathyContactPriv *priv;
1100 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1102 priv = GET_PRIV (contact);
1104 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1108 empathy_contact_can_send_files (EmpathyContact *contact)
1110 EmpathyContactPriv *priv;
1112 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1114 priv = GET_PRIV (contact);
1116 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1120 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1122 EmpathyContactPriv *priv;
1124 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1126 priv = GET_PRIV (contact);
1128 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1132 contact_get_avatar_filename (EmpathyContact *contact,
1138 gchar *token_escaped;
1140 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1143 token_escaped = tp_escape_as_identifier (token);
1144 account = empathy_contact_get_account (contact);
1146 avatar_path = g_build_filename (g_get_user_cache_dir (),
1149 tp_account_get_connection_manager (account),
1150 tp_account_get_protocol (account),
1152 g_mkdir_with_parents (avatar_path, 0700);
1154 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1156 g_free (token_escaped);
1157 g_free (avatar_path);
1163 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1166 EmpathyAvatar *avatar = NULL;
1170 GError *error = NULL;
1172 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1173 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1175 /* Load the avatar from file if it exists */
1176 filename = contact_get_avatar_filename (contact, token);
1177 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1179 if (!g_file_get_contents (filename, &data, &len, &error))
1181 DEBUG ("Failed to load avatar from cache: %s",
1182 error ? error->message : "No error given");
1183 g_clear_error (&error);
1189 DEBUG ("Avatar loaded from %s", filename);
1190 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1192 empathy_contact_set_avatar (contact, avatar);
1193 empathy_avatar_unref (avatar);
1200 return data != NULL;
1204 empathy_avatar_get_type (void)
1206 static GType type_id = 0;
1210 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1211 (GBoxedCopyFunc) empathy_avatar_ref,
1212 (GBoxedFreeFunc) empathy_avatar_unref);
1219 * empathy_avatar_new:
1220 * @data: the avatar data
1221 * @len: the size of avatar data
1222 * @format: the mime type of the avatar image
1223 * @token: the token of the avatar
1224 * @filename: the filename where the avatar is stored in cache
1226 * Create a #EmpathyAvatar from the provided data. This function takes the
1227 * ownership of @data, @format, @token and @filename.
1229 * Returns: a new #EmpathyAvatar
1232 empathy_avatar_new (guchar *data,
1238 EmpathyAvatar *avatar;
1240 avatar = g_slice_new0 (EmpathyAvatar);
1241 avatar->data = data;
1243 avatar->format = format;
1244 avatar->token = token;
1245 avatar->filename = filename;
1246 avatar->refcount = 1;
1252 empathy_avatar_unref (EmpathyAvatar *avatar)
1254 g_return_if_fail (avatar != NULL);
1257 if (avatar->refcount == 0)
1259 g_free (avatar->data);
1260 g_free (avatar->format);
1261 g_free (avatar->token);
1262 g_free (avatar->filename);
1263 g_slice_free (EmpathyAvatar, avatar);
1268 empathy_avatar_ref (EmpathyAvatar *avatar)
1270 g_return_val_if_fail (avatar != NULL, NULL);
1278 * empathy_avatar_save_to_file:
1279 * @avatar: the avatar
1280 * @filename: name of a file to write avatar to
1281 * @error: return location for a GError, or NULL
1283 * Save the avatar to a file named filename
1285 * Returns: %TRUE on success, %FALSE if an error occurred
1288 empathy_avatar_save_to_file (EmpathyAvatar *self,
1289 const gchar *filename,
1292 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1297 * empathy_contact_get_location:
1298 * @contact: an #EmpathyContact
1300 * Returns the user's location if available. The keys are defined in
1301 * empathy-location.h. If the contact doesn't have location
1302 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1303 * you are done with the #GHashTable.
1305 * It is composed of string keys and GValues. Keys are
1306 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1307 * Example: a "city" key would have "Helsinki" as string GValue,
1308 * a "latitude" would have 65.0 as double GValue.
1310 * Returns: a #GHashTable of location values
1313 empathy_contact_get_location (EmpathyContact *contact)
1315 EmpathyContactPriv *priv;
1317 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1319 priv = GET_PRIV (contact);
1321 return priv->location;
1325 * empathy_contact_set_location:
1326 * @contact: an #EmpathyContact
1327 * @location: a #GHashTable of the location
1329 * Sets the user's location based on the location #GHashTable passed.
1330 * It is composed of string keys and GValues. Keys are
1331 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1332 * Example: a "city" key would have "Helsinki" as string GValue,
1333 * a "latitude" would have 65.0 as double GValue.
1336 empathy_contact_set_location (EmpathyContact *contact,
1337 GHashTable *location)
1339 EmpathyContactPriv *priv;
1341 g_return_if_fail (EMPATHY_CONTACT (contact));
1342 g_return_if_fail (location != NULL);
1344 priv = GET_PRIV (contact);
1346 if (priv->location != NULL)
1347 g_hash_table_unref (priv->location);
1349 priv->location = g_hash_table_ref (location);
1351 update_geocode (contact);
1353 g_object_notify (G_OBJECT (contact), "location");
1357 * empathy_contact_equal:
1358 * @contact1: an #EmpathyContact
1359 * @contact2: an #EmpathyContact
1361 * Returns FALSE if one of the contacts is NULL but the other is not.
1362 * Otherwise returns TRUE if both pointer are equal or if they bith
1363 * refer to the same id.
1364 * It's only necessary to call this function if your contact objects
1365 * come from logs where contacts are created dynamically and comparing
1366 * pointers is not enough.
1369 empathy_contact_equal (gconstpointer contact1,
1370 gconstpointer contact2)
1377 if ((contact1 == NULL) != (contact2 == NULL)) {
1380 if (contact1 == contact2) {
1383 c1 = EMPATHY_CONTACT (contact1);
1384 c2 = EMPATHY_CONTACT (contact2);
1385 id1 = empathy_contact_get_id (c1);
1386 id2 = empathy_contact_get_id (c2);
1387 if (!tp_strdiff (id1, id2)) {
1394 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1395 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1397 /* This callback is called by geoclue when it found a position
1398 * for the given address. A position is necessary for a contact
1399 * to show up on the map
1402 geocode_cb (GeoclueGeocode *geocode,
1403 GeocluePositionFields fields,
1407 GeoclueAccuracy *accuracy,
1411 EmpathyContactPriv *priv = GET_PRIV (contact);
1412 GHashTable *new_location;
1414 if (priv->location == NULL)
1419 DEBUG ("Error geocoding location : %s", error->message);
1423 /* No need to change location if we didn't find the position */
1424 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1427 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1430 new_location = tp_asv_new (
1431 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1432 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1435 DEBUG ("\t - Latitude: %f", latitude);
1436 DEBUG ("\t - Longitude: %f", longitude);
1438 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1439 * the values we just set. */
1440 tp_g_hash_table_update (new_location, priv->location,
1441 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1443 /* Set the altitude only if it wasn't defined before */
1444 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1445 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1447 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1449 DEBUG ("\t - Altitude: %f", altitude);
1452 /* Don't change the accuracy as we used an address to get this position */
1453 g_hash_table_unref (priv->location);
1454 priv->location = new_location;
1455 g_object_notify (contact, "location");
1457 g_object_unref (geocode);
1458 g_object_unref (contact);
1462 get_dup_string (GHashTable *location,
1467 value = g_hash_table_lookup (location, key);
1469 return g_value_dup_string (value);
1475 update_geocode (EmpathyContact *contact)
1477 static GeoclueGeocode *geocode;
1479 GHashTable *address;
1480 GHashTable *location;
1482 location = empathy_contact_get_location (contact);
1483 if (location == NULL)
1486 /* No need to search for position if contact published it */
1487 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1488 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1491 if (geocode == NULL)
1493 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1494 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1498 g_object_ref (geocode);
1501 address = geoclue_address_details_new ();
1503 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1506 g_hash_table_insert (address,
1507 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1508 DEBUG ("\t - countrycode: %s", str);
1511 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1514 g_hash_table_insert (address,
1515 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1516 DEBUG ("\t - country: %s", str);
1519 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1522 g_hash_table_insert (address,
1523 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1524 DEBUG ("\t - postalcode: %s", str);
1527 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1530 g_hash_table_insert (address,
1531 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1532 DEBUG ("\t - region: %s", str);
1535 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1538 g_hash_table_insert (address,
1539 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1540 DEBUG ("\t - locality: %s", str);
1543 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1546 g_hash_table_insert (address,
1547 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1548 DEBUG ("\t - street: %s", str);
1551 if (g_hash_table_size (address) > 0)
1553 g_object_ref (contact);
1555 geoclue_geocode_address_to_position_async (geocode, address,
1556 geocode_cb, contact);
1559 g_hash_table_unref (address);
1563 static EmpathyCapabilities
1564 tp_caps_to_capabilities (TpCapabilities *caps)
1566 EmpathyCapabilities capabilities = 0;
1570 classes = tp_capabilities_get_channel_classes (caps);
1572 for (i = 0; i < classes->len; i++)
1574 GValueArray *class_struct;
1575 GHashTable *fixed_prop;
1577 TpHandleType handle_type;
1578 const gchar *chan_type;
1580 class_struct = g_ptr_array_index (classes, i);
1581 tp_value_array_unpack (class_struct, 2,
1585 handle_type = tp_asv_get_uint32 (fixed_prop,
1586 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1587 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1590 chan_type = tp_asv_get_string (fixed_prop,
1591 TP_PROP_CHANNEL_CHANNEL_TYPE);
1593 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1595 capabilities |= EMPATHY_CAPABILITIES_FT;
1597 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1599 const gchar *service;
1601 service = tp_asv_get_string (fixed_prop,
1602 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1604 if (!tp_strdiff (service, "rfb"))
1605 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1607 else if (!tp_strdiff (chan_type,
1608 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1612 for (j = 0; allowed_prop[j] != NULL; j++)
1614 if (!tp_strdiff (allowed_prop[j],
1615 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1616 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1617 else if (!tp_strdiff (allowed_prop[j],
1618 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1619 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1624 return capabilities;
1628 set_capabilities_from_tp_caps (EmpathyContact *self,
1629 TpCapabilities *caps)
1631 EmpathyCapabilities capabilities;
1636 capabilities = tp_caps_to_capabilities (caps);
1637 empathy_contact_set_capabilities (self, capabilities);
1641 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1643 EmpathyContactPriv *priv = GET_PRIV (contact);
1648 token = tp_contact_get_avatar_token (priv->tp_contact);
1649 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1650 file = tp_contact_get_avatar_file (priv->tp_contact);
1654 EmpathyAvatar *avatar;
1658 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1659 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1660 g_file_get_path (file));
1661 empathy_contact_set_avatar (contact, avatar);
1662 empathy_avatar_unref (avatar);
1666 empathy_contact_set_avatar (contact, NULL);
1671 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1673 EmpathyContact *contact = NULL;
1675 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1677 if (contacts_table == NULL)
1678 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1680 contact = g_hash_table_lookup (contacts_table, tp_contact);
1682 if (contact == NULL)
1684 contact = empathy_contact_new (tp_contact);
1686 /* The hash table does not keep any ref.
1687 * contact keeps a ref to tp_contact, and is removed from the table in
1688 * contact_dispose() */
1689 g_hash_table_insert (contacts_table, tp_contact, contact);
1693 g_object_ref (contact);