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 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
669 EmpathyContactPriv *priv;
670 FolksPersona *persona;
672 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
673 g_return_if_fail (group != NULL);
675 priv = GET_PRIV (contact);
677 /* Normally pass through the changes to the persona */
678 persona = empathy_contact_get_persona (contact);
681 if (FOLKS_IS_GROUPS (persona))
682 folks_groups_change_group (FOLKS_GROUPS (persona), group, is_member);
686 /* If the persona doesn't exist yet, we have to cache the changes until it
688 if (priv->groups == NULL)
690 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
694 g_hash_table_insert (priv->groups, g_strdup (group),
695 GUINT_TO_POINTER (is_member));
699 empathy_contact_get_avatar (EmpathyContact *contact)
701 EmpathyContactPriv *priv;
703 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
705 priv = GET_PRIV (contact);
711 empathy_contact_set_avatar (EmpathyContact *contact,
712 EmpathyAvatar *avatar)
714 EmpathyContactPriv *priv;
716 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
718 priv = GET_PRIV (contact);
720 if (priv->avatar == avatar)
725 empathy_avatar_unref (priv->avatar);
730 priv->avatar = empathy_avatar_ref (avatar);
732 g_object_notify (G_OBJECT (contact), "avatar");
736 empathy_contact_get_account (EmpathyContact *contact)
738 EmpathyContactPriv *priv;
740 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
742 priv = GET_PRIV (contact);
744 if (priv->account == NULL && priv->tp_contact != NULL)
746 TpConnection *connection;
748 /* FIXME: This assume the account manager already exists */
749 connection = tp_contact_get_connection (priv->tp_contact);
751 g_object_ref (empathy_get_account_for_connection (connection));
754 return priv->account;
758 empathy_contact_get_persona (EmpathyContact *contact)
760 EmpathyContactPriv *priv;
762 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
764 priv = GET_PRIV (contact);
766 if (priv->persona == NULL && priv->tp_contact != NULL)
768 /* FIXME: This is disgustingly slow */
769 /* Query for the persona */
770 EmpathyIndividualManager *manager;
771 GList *individuals, *l;
773 manager = empathy_individual_manager_dup_singleton ();
774 individuals = empathy_individual_manager_get_members (manager);
776 for (l = individuals; l != NULL; l = l->next)
779 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
781 personas = folks_individual_get_personas (individual);
782 for (j = personas; j != NULL; j = j->next)
784 TpfPersona *persona = j->data;
786 if (TPF_IS_PERSONA (persona))
788 TpContact *tp_contact = tpf_persona_get_contact (persona);
790 if (tp_contact == priv->tp_contact)
792 /* Found the right persona */
793 priv->persona = g_object_ref (persona);
801 g_list_free (individuals);
802 g_object_unref (manager);
805 return priv->persona;
809 empathy_contact_set_persona (EmpathyContact *contact,
810 FolksPersona *persona)
812 EmpathyContactPriv *priv;
814 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
815 g_return_if_fail (TPF_IS_PERSONA (persona));
817 priv = GET_PRIV (contact);
819 if (persona == priv->persona)
822 if (priv->persona != NULL)
823 g_object_unref (priv->persona);
824 priv->persona = g_object_ref (persona);
826 g_signal_connect (priv->persona, "notify",
827 G_CALLBACK (folks_persona_notify_cb), contact);
829 g_object_notify (G_OBJECT (contact), "persona");
831 /* Set the persona's alias, since ours could've been set using
832 * empathy_contact_set_alias() before we had a persona; this happens when
833 * adding a contact. */
834 empathy_contact_set_alias (contact, priv->alias);
836 /* Set the persona's groups */
837 if (priv->groups != NULL)
839 folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
840 g_hash_table_destroy (priv->groups);
846 empathy_contact_get_connection (EmpathyContact *contact)
848 EmpathyContactPriv *priv;
850 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
852 priv = GET_PRIV (contact);
854 if (priv->tp_contact != NULL)
855 return tp_contact_get_connection (priv->tp_contact);
860 TpConnectionPresenceType
861 empathy_contact_get_presence (EmpathyContact *contact)
863 EmpathyContactPriv *priv;
865 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
866 TP_CONNECTION_PRESENCE_TYPE_UNSET);
868 priv = GET_PRIV (contact);
870 if (priv->tp_contact != NULL)
871 return tp_contact_get_presence_type (priv->tp_contact);
873 return priv->presence;
877 empathy_contact_set_presence (EmpathyContact *contact,
878 TpConnectionPresenceType presence)
880 EmpathyContactPriv *priv;
881 TpConnectionPresenceType old_presence;
883 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
885 priv = GET_PRIV (contact);
887 if (presence == priv->presence)
890 old_presence = priv->presence;
891 priv->presence = presence;
893 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
895 g_object_notify (G_OBJECT (contact), "presence");
899 empathy_contact_get_presence_message (EmpathyContact *contact)
901 EmpathyContactPriv *priv;
903 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
905 priv = GET_PRIV (contact);
907 if (priv->persona != NULL)
908 return folks_presence_get_presence_message (FOLKS_PRESENCE (priv->persona));
914 empathy_contact_set_presence_message (EmpathyContact *contact,
915 const gchar *message)
917 EmpathyContactPriv *priv = GET_PRIV (contact);
919 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
921 if (priv->persona != NULL)
923 folks_presence_set_presence_message (FOLKS_PRESENCE (priv->persona),
929 empathy_contact_get_handle (EmpathyContact *contact)
931 EmpathyContactPriv *priv;
933 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
935 priv = GET_PRIV (contact);
937 if (priv->tp_contact != NULL)
938 return tp_contact_get_handle (priv->tp_contact);
944 empathy_contact_set_handle (EmpathyContact *contact,
947 EmpathyContactPriv *priv;
949 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
951 priv = GET_PRIV (contact);
953 g_object_ref (contact);
954 if (handle != priv->handle)
956 priv->handle = handle;
957 g_object_notify (G_OBJECT (contact), "handle");
959 g_object_unref (contact);
963 empathy_contact_get_capabilities (EmpathyContact *contact)
965 EmpathyContactPriv *priv;
967 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
969 priv = GET_PRIV (contact);
971 return priv->capabilities;
975 empathy_contact_set_capabilities (EmpathyContact *contact,
976 EmpathyCapabilities capabilities)
978 EmpathyContactPriv *priv;
980 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
982 priv = GET_PRIV (contact);
984 if (priv->capabilities == capabilities)
987 priv->capabilities = capabilities;
989 g_object_notify (G_OBJECT (contact), "capabilities");
993 empathy_contact_is_user (EmpathyContact *contact)
995 EmpathyContactPriv *priv;
997 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
999 priv = GET_PRIV (contact);
1001 return priv->is_user;
1005 empathy_contact_set_is_user (EmpathyContact *contact,
1008 EmpathyContactPriv *priv;
1010 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1012 priv = GET_PRIV (contact);
1014 if (priv->is_user == is_user)
1017 priv->is_user = is_user;
1019 g_object_notify (G_OBJECT (contact), "is-user");
1023 empathy_contact_is_online (EmpathyContact *contact)
1025 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1027 switch (empathy_contact_get_presence (contact))
1029 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1030 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1031 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1039 empathy_contact_get_status (EmpathyContact *contact)
1041 const gchar *message;
1043 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1045 message = empathy_contact_get_presence_message (contact);
1046 if (!EMP_STR_EMPTY (message))
1049 return empathy_presence_get_default_message (
1050 empathy_contact_get_presence (contact));
1054 empathy_contact_can_voip (EmpathyContact *contact)
1056 EmpathyContactPriv *priv;
1058 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1060 priv = GET_PRIV (contact);
1062 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1063 EMPATHY_CAPABILITIES_VIDEO);
1067 empathy_contact_can_voip_audio (EmpathyContact *contact)
1069 EmpathyContactPriv *priv;
1071 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1073 priv = GET_PRIV (contact);
1075 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1079 empathy_contact_can_voip_video (EmpathyContact *contact)
1081 EmpathyContactPriv *priv;
1083 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1085 priv = GET_PRIV (contact);
1087 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1091 empathy_contact_can_send_files (EmpathyContact *contact)
1093 EmpathyContactPriv *priv;
1095 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1097 priv = GET_PRIV (contact);
1099 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1103 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1105 EmpathyContactPriv *priv;
1107 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1109 priv = GET_PRIV (contact);
1111 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1115 contact_get_avatar_filename (EmpathyContact *contact,
1121 gchar *token_escaped;
1123 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1126 token_escaped = tp_escape_as_identifier (token);
1127 account = empathy_contact_get_account (contact);
1129 avatar_path = g_build_filename (g_get_user_cache_dir (),
1132 tp_account_get_connection_manager (account),
1133 tp_account_get_protocol (account),
1135 g_mkdir_with_parents (avatar_path, 0700);
1137 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1139 g_free (token_escaped);
1140 g_free (avatar_path);
1146 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1149 EmpathyAvatar *avatar = NULL;
1153 GError *error = NULL;
1155 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1156 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1158 /* Load the avatar from file if it exists */
1159 filename = contact_get_avatar_filename (contact, token);
1160 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1162 if (!g_file_get_contents (filename, &data, &len, &error))
1164 DEBUG ("Failed to load avatar from cache: %s",
1165 error ? error->message : "No error given");
1166 g_clear_error (&error);
1172 DEBUG ("Avatar loaded from %s", filename);
1173 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1175 empathy_contact_set_avatar (contact, avatar);
1176 empathy_avatar_unref (avatar);
1183 return data != NULL;
1187 empathy_avatar_get_type (void)
1189 static GType type_id = 0;
1193 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1194 (GBoxedCopyFunc) empathy_avatar_ref,
1195 (GBoxedFreeFunc) empathy_avatar_unref);
1202 * empathy_avatar_new:
1203 * @data: the avatar data
1204 * @len: the size of avatar data
1205 * @format: the mime type of the avatar image
1206 * @token: the token of the avatar
1207 * @filename: the filename where the avatar is stored in cache
1209 * Create a #EmpathyAvatar from the provided data. This function takes the
1210 * ownership of @data, @format, @token and @filename.
1212 * Returns: a new #EmpathyAvatar
1215 empathy_avatar_new (guchar *data,
1221 EmpathyAvatar *avatar;
1223 avatar = g_slice_new0 (EmpathyAvatar);
1224 avatar->data = data;
1226 avatar->format = format;
1227 avatar->token = token;
1228 avatar->filename = filename;
1229 avatar->refcount = 1;
1235 empathy_avatar_unref (EmpathyAvatar *avatar)
1237 g_return_if_fail (avatar != NULL);
1240 if (avatar->refcount == 0)
1242 g_free (avatar->data);
1243 g_free (avatar->format);
1244 g_free (avatar->token);
1245 g_free (avatar->filename);
1246 g_slice_free (EmpathyAvatar, avatar);
1251 empathy_avatar_ref (EmpathyAvatar *avatar)
1253 g_return_val_if_fail (avatar != NULL, NULL);
1261 * empathy_avatar_save_to_file:
1262 * @avatar: the avatar
1263 * @filename: name of a file to write avatar to
1264 * @error: return location for a GError, or NULL
1266 * Save the avatar to a file named filename
1268 * Returns: %TRUE on success, %FALSE if an error occurred
1271 empathy_avatar_save_to_file (EmpathyAvatar *self,
1272 const gchar *filename,
1275 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1280 * empathy_contact_get_location:
1281 * @contact: an #EmpathyContact
1283 * Returns the user's location if available. The keys are defined in
1284 * empathy-location.h. If the contact doesn't have location
1285 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1286 * you are done with the #GHashTable.
1288 * It is composed of string keys and GValues. Keys are
1289 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1290 * Example: a "city" key would have "Helsinki" as string GValue,
1291 * a "latitude" would have 65.0 as double GValue.
1293 * Returns: a #GHashTable of location values
1296 empathy_contact_get_location (EmpathyContact *contact)
1298 EmpathyContactPriv *priv;
1300 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1302 priv = GET_PRIV (contact);
1304 return priv->location;
1308 * empathy_contact_set_location:
1309 * @contact: an #EmpathyContact
1310 * @location: a #GHashTable of the location
1312 * Sets the user's location based on the location #GHashTable passed.
1313 * It is composed of string keys and GValues. Keys are
1314 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1315 * Example: a "city" key would have "Helsinki" as string GValue,
1316 * a "latitude" would have 65.0 as double GValue.
1319 empathy_contact_set_location (EmpathyContact *contact,
1320 GHashTable *location)
1322 EmpathyContactPriv *priv;
1324 g_return_if_fail (EMPATHY_CONTACT (contact));
1325 g_return_if_fail (location != NULL);
1327 priv = GET_PRIV (contact);
1329 if (priv->location != NULL)
1330 g_hash_table_unref (priv->location);
1332 priv->location = g_hash_table_ref (location);
1334 update_geocode (contact);
1336 g_object_notify (G_OBJECT (contact), "location");
1340 * empathy_contact_equal:
1341 * @contact1: an #EmpathyContact
1342 * @contact2: an #EmpathyContact
1344 * Returns FALSE if one of the contacts is NULL but the other is not.
1345 * Otherwise returns TRUE if both pointer are equal or if they bith
1346 * refer to the same id.
1347 * It's only necessary to call this function if your contact objects
1348 * come from logs where contacts are created dynamically and comparing
1349 * pointers is not enough.
1352 empathy_contact_equal (gconstpointer contact1,
1353 gconstpointer contact2)
1360 if ((contact1 == NULL) != (contact2 == NULL)) {
1363 if (contact1 == contact2) {
1366 c1 = EMPATHY_CONTACT (contact1);
1367 c2 = EMPATHY_CONTACT (contact2);
1368 id1 = empathy_contact_get_id (c1);
1369 id2 = empathy_contact_get_id (c2);
1370 if (!tp_strdiff (id1, id2)) {
1377 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1378 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1380 /* This callback is called by geoclue when it found a position
1381 * for the given address. A position is necessary for a contact
1382 * to show up on the map
1385 geocode_cb (GeoclueGeocode *geocode,
1386 GeocluePositionFields fields,
1390 GeoclueAccuracy *accuracy,
1394 EmpathyContactPriv *priv = GET_PRIV (contact);
1395 GHashTable *new_location;
1397 if (priv->location == NULL)
1402 DEBUG ("Error geocoding location : %s", error->message);
1406 /* No need to change location if we didn't find the position */
1407 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1410 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1413 new_location = tp_asv_new (
1414 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1415 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1418 DEBUG ("\t - Latitude: %f", latitude);
1419 DEBUG ("\t - Longitude: %f", longitude);
1421 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1422 * the values we just set. */
1423 tp_g_hash_table_update (new_location, priv->location,
1424 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1426 /* Set the altitude only if it wasn't defined before */
1427 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1428 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1430 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1432 DEBUG ("\t - Altitude: %f", altitude);
1435 /* Don't change the accuracy as we used an address to get this position */
1436 g_hash_table_unref (priv->location);
1437 priv->location = new_location;
1438 g_object_notify (contact, "location");
1440 g_object_unref (geocode);
1441 g_object_unref (contact);
1445 get_dup_string (GHashTable *location,
1450 value = g_hash_table_lookup (location, key);
1452 return g_value_dup_string (value);
1458 update_geocode (EmpathyContact *contact)
1460 static GeoclueGeocode *geocode;
1462 GHashTable *address;
1463 GHashTable *location;
1465 location = empathy_contact_get_location (contact);
1466 if (location == NULL)
1469 /* No need to search for position if contact published it */
1470 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1471 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1474 if (geocode == NULL)
1476 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1477 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1481 g_object_ref (geocode);
1484 address = geoclue_address_details_new ();
1486 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1489 g_hash_table_insert (address,
1490 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1491 DEBUG ("\t - countrycode: %s", str);
1494 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1497 g_hash_table_insert (address,
1498 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1499 DEBUG ("\t - country: %s", str);
1502 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1505 g_hash_table_insert (address,
1506 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1507 DEBUG ("\t - postalcode: %s", str);
1510 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1513 g_hash_table_insert (address,
1514 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1515 DEBUG ("\t - region: %s", str);
1518 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1521 g_hash_table_insert (address,
1522 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1523 DEBUG ("\t - locality: %s", str);
1526 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1529 g_hash_table_insert (address,
1530 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1531 DEBUG ("\t - street: %s", str);
1534 if (g_hash_table_size (address) > 0)
1536 g_object_ref (contact);
1538 geoclue_geocode_address_to_position_async (geocode, address,
1539 geocode_cb, contact);
1542 g_hash_table_unref (address);
1546 static EmpathyCapabilities
1547 tp_caps_to_capabilities (TpCapabilities *caps)
1549 EmpathyCapabilities capabilities = 0;
1553 classes = tp_capabilities_get_channel_classes (caps);
1555 for (i = 0; i < classes->len; i++)
1557 GValueArray *class_struct;
1558 GHashTable *fixed_prop;
1560 TpHandleType handle_type;
1561 const gchar *chan_type;
1563 class_struct = g_ptr_array_index (classes, i);
1564 tp_value_array_unpack (class_struct, 2,
1568 handle_type = tp_asv_get_uint32 (fixed_prop,
1569 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1570 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1573 chan_type = tp_asv_get_string (fixed_prop,
1574 TP_PROP_CHANNEL_CHANNEL_TYPE);
1576 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1578 capabilities |= EMPATHY_CAPABILITIES_FT;
1580 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1582 const gchar *service;
1584 service = tp_asv_get_string (fixed_prop,
1585 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1587 if (!tp_strdiff (service, "rfb"))
1588 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1590 else if (!tp_strdiff (chan_type,
1591 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1595 for (j = 0; allowed_prop[j] != NULL; j++)
1597 if (!tp_strdiff (allowed_prop[j],
1598 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1599 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1600 else if (!tp_strdiff (allowed_prop[j],
1601 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1602 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1607 return capabilities;
1611 set_capabilities_from_tp_caps (EmpathyContact *self,
1612 TpCapabilities *caps)
1614 EmpathyCapabilities capabilities;
1619 capabilities = tp_caps_to_capabilities (caps);
1620 empathy_contact_set_capabilities (self, capabilities);
1624 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1626 EmpathyContactPriv *priv = GET_PRIV (contact);
1631 token = tp_contact_get_avatar_token (priv->tp_contact);
1632 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1633 file = tp_contact_get_avatar_file (priv->tp_contact);
1637 EmpathyAvatar *avatar;
1641 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1642 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1643 g_file_get_path (file));
1644 empathy_contact_set_avatar (contact, avatar);
1645 empathy_avatar_unref (avatar);
1649 empathy_contact_set_avatar (contact, NULL);
1654 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1656 EmpathyContact *contact = NULL;
1658 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1660 if (contacts_table == NULL)
1661 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1663 contact = g_hash_table_lookup (contacts_table, tp_contact);
1665 if (contact == NULL)
1667 contact = empathy_contact_new (tp_contact);
1669 /* The hash table does not keep any ref.
1670 * contact keeps a ref to tp_contact, and is removed from the table in
1671 * contact_dispose() */
1672 g_hash_table_insert (contacts_table, tp_contact, contact);
1676 g_object_ref (contact);