1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/account-manager.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
36 #include <geoclue/geoclue-geocode.h>
39 #include "empathy-contact.h"
40 #include "empathy-utils.h"
41 #include "empathy-enum-types.h"
42 #include "empathy-marshal.h"
43 #include "empathy-location.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
46 #include "empathy-debug.h"
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
50 TpContact *tp_contact;
52 FolksPersona *persona;
55 EmpathyAvatar *avatar;
56 TpConnectionPresenceType presence;
57 gchar *presence_message;
59 EmpathyCapabilities capabilities;
62 /* Location is composed of string keys and GValues.
63 * Example: a "city" key would have "Helsinki" as string GValue,
64 * a "latitude" would have 65.0 as double GValue.
66 * This is a super set of the location stored in TpContact as we can try add
67 * more fields by searching the address using geoclue.
73 static void contact_finalize (GObject *object);
74 static void contact_get_property (GObject *object, guint param_id,
75 GValue *value, GParamSpec *pspec);
76 static void contact_set_property (GObject *object, guint param_id,
77 const GValue *value, GParamSpec *pspec);
80 static void update_geocode (EmpathyContact *contact);
83 static void empathy_contact_set_location (EmpathyContact *contact,
84 GHashTable *location);
86 static void set_capabilities_from_tp_caps (EmpathyContact *self,
87 TpCapabilities *caps);
89 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
91 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
103 PROP_PRESENCE_MESSAGE,
115 static guint signals[LAST_SIGNAL];
117 /* TpContact* -> EmpathyContact*, both borrowed ref */
118 static GHashTable *contacts_table = NULL;
121 tp_contact_notify_cb (TpContact *tp_contact,
125 EmpathyContactPriv *priv = GET_PRIV (contact);
127 /* Forward property notifications */
128 if (!tp_strdiff (param->name, "alias"))
129 g_object_notify (contact, "alias");
130 else if (!tp_strdiff (param->name, "presence-type")) {
131 TpConnectionPresenceType presence;
133 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
134 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
136 priv->presence = presence;
137 g_object_notify (contact, "presence");
139 else if (!tp_strdiff (param->name, "presence-message"))
140 g_object_notify (contact, "presence-message");
141 else if (!tp_strdiff (param->name, "identifier"))
142 g_object_notify (contact, "id");
143 else if (!tp_strdiff (param->name, "handle"))
144 g_object_notify (contact, "handle");
145 else if (!tp_strdiff (param->name, "location"))
147 GHashTable *location;
149 location = tp_contact_get_location (tp_contact);
150 /* This will start a geoclue search to find the address if needed */
151 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
153 else if (!tp_strdiff (param->name, "capabilities"))
155 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
156 tp_contact_get_capabilities (tp_contact));
158 else if (!tp_strdiff (param->name, "avatar-file"))
160 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
165 contact_dispose (GObject *object)
167 EmpathyContactPriv *priv = GET_PRIV (object);
169 if (priv->tp_contact)
171 g_hash_table_remove (contacts_table, priv->tp_contact);
172 g_signal_handlers_disconnect_by_func (priv->tp_contact,
173 tp_contact_notify_cb, object);
174 g_object_unref (priv->tp_contact);
176 priv->tp_contact = NULL;
179 g_object_unref (priv->account);
180 priv->account = NULL;
183 g_object_unref (priv->persona);
184 priv->persona = NULL;
186 if (priv->avatar != NULL)
188 empathy_avatar_unref (priv->avatar);
192 if (priv->location != NULL)
194 g_hash_table_unref (priv->location);
195 priv->location = NULL;
198 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
202 empathy_contact_class_init (EmpathyContactClass *class)
204 GObjectClass *object_class;
206 object_class = G_OBJECT_CLASS (class);
208 object_class->finalize = contact_finalize;
209 object_class->dispose = contact_dispose;
210 object_class->get_property = contact_get_property;
211 object_class->set_property = contact_set_property;
213 g_object_class_install_property (object_class,
215 g_param_spec_object ("tp-contact",
217 "The TpContact associated with the contact",
219 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (object_class,
223 g_param_spec_object ("account",
225 "The account associated with the contact",
227 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (object_class,
231 g_param_spec_object ("persona",
233 "The FolksPersona associated with the contact",
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (object_class,
239 g_param_spec_string ("id",
241 "String identifying contact",
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 g_object_class_install_property (object_class,
247 g_param_spec_string ("alias",
249 "An alias for the contact",
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (object_class,
255 g_param_spec_boxed ("avatar",
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (object_class,
263 g_param_spec_uint ("presence",
265 "Presence of contact",
266 TP_CONNECTION_PRESENCE_TYPE_UNSET,
267 NUM_TP_CONNECTION_PRESENCE_TYPES,
268 TP_CONNECTION_PRESENCE_TYPE_UNSET,
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (object_class,
272 PROP_PRESENCE_MESSAGE,
273 g_param_spec_string ("presence-message",
274 "Contact presence message",
275 "Presence message of contact",
277 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (object_class,
281 g_param_spec_uint ("handle",
283 "The handle of the contact",
287 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (object_class,
291 g_param_spec_flags ("capabilities",
292 "Contact Capabilities",
293 "Capabilities of the contact",
294 EMPATHY_TYPE_CAPABILITIES,
295 EMPATHY_CAPABILITIES_UNKNOWN,
296 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (object_class,
300 g_param_spec_boolean ("is-user",
302 "Is contact the user",
304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (object_class,
309 g_param_spec_boxed ("location",
311 "Physical location of the contact",
313 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315 signals[PRESENCE_CHANGED] =
316 g_signal_new ("presence-changed",
317 G_TYPE_FROM_CLASS (class),
321 _empathy_marshal_VOID__UINT_UINT,
326 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
330 empathy_contact_init (EmpathyContact *contact)
332 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
333 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
335 contact->priv = priv;
337 priv->location = NULL;
342 contact_finalize (GObject *object)
344 EmpathyContactPriv *priv;
346 priv = GET_PRIV (object);
348 DEBUG ("finalize: %p", object);
350 if (priv->groups != NULL)
351 g_hash_table_destroy (priv->groups);
352 g_free (priv->alias);
354 g_free (priv->presence_message);
356 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
360 set_tp_contact (EmpathyContact *contact,
361 TpContact *tp_contact)
363 EmpathyContactPriv *priv = GET_PRIV (contact);
364 GHashTable *location;
365 TpHandle self_handle;
368 if (tp_contact == NULL)
371 g_assert (priv->tp_contact == NULL);
372 priv->tp_contact = g_object_ref (tp_contact);
373 priv->presence = empathy_contact_get_presence (contact);
375 location = tp_contact_get_location (tp_contact);
376 if (location != NULL)
377 empathy_contact_set_location (contact, location);
379 set_capabilities_from_tp_caps (contact,
380 tp_contact_get_capabilities (tp_contact));
382 contact_set_avatar_from_tp_contact (contact);
384 /* Set is-user property. Note that it could still be the handle is
385 * different from the connection's self handle, in the case the handle
386 * comes from a group interface. */
387 self_handle = tp_connection_get_self_handle (
388 tp_contact_get_connection (tp_contact));
389 handle = tp_contact_get_handle (tp_contact);
390 empathy_contact_set_is_user (contact, self_handle == handle);
392 g_signal_connect (priv->tp_contact, "notify",
393 G_CALLBACK (tp_contact_notify_cb), contact);
397 contact_get_property (GObject *object,
402 EmpathyContact *contact = EMPATHY_CONTACT (object);
406 case PROP_TP_CONTACT:
407 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
410 g_value_set_object (value, empathy_contact_get_account (contact));
413 g_value_set_object (value, empathy_contact_get_persona (contact));
416 g_value_set_string (value, empathy_contact_get_id (contact));
419 g_value_set_string (value, empathy_contact_get_alias (contact));
422 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
425 g_value_set_uint (value, empathy_contact_get_presence (contact));
427 case PROP_PRESENCE_MESSAGE:
428 g_value_set_string (value, empathy_contact_get_presence_message (contact));
431 g_value_set_uint (value, empathy_contact_get_handle (contact));
433 case PROP_CAPABILITIES:
434 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
437 g_value_set_boolean (value, empathy_contact_is_user (contact));
440 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
446 contact_set_property (GObject *object,
451 EmpathyContact *contact = EMPATHY_CONTACT (object);
452 EmpathyContactPriv *priv = GET_PRIV (object);
456 case PROP_TP_CONTACT:
457 set_tp_contact (contact, g_value_get_object (value));
460 g_assert (priv->account == NULL);
461 priv->account = g_value_dup_object (value);
464 empathy_contact_set_persona (contact, g_value_get_object (value));
467 empathy_contact_set_id (contact, g_value_get_string (value));
470 empathy_contact_set_alias (contact, g_value_get_string (value));
473 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
476 empathy_contact_set_presence (contact, g_value_get_uint (value));
478 case PROP_PRESENCE_MESSAGE:
479 empathy_contact_set_presence_message (contact, g_value_get_string (value));
482 empathy_contact_set_handle (contact, g_value_get_uint (value));
484 case PROP_CAPABILITIES:
485 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
488 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
491 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
497 empathy_contact_new (TpContact *tp_contact)
499 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
501 return g_object_new (EMPATHY_TYPE_CONTACT,
502 "tp-contact", tp_contact,
507 empathy_contact_from_tpl_contact (TpAccount *account,
508 TplEntity *tpl_entity)
510 EmpathyContact *retval;
513 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
515 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
517 retval = g_object_new (EMPATHY_TYPE_CONTACT,
518 "id", tpl_entity_get_alias (tpl_entity),
519 "alias", tpl_entity_get_identifier (tpl_entity),
524 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
525 empathy_contact_load_avatar_cache (retval,
526 tpl_entity_get_avatar_token (tpl_entity));
532 empathy_contact_new_for_log (TpAccount *account,
537 g_return_val_if_fail (id != NULL, NULL);
538 g_assert (account != NULL);
540 return g_object_new (EMPATHY_TYPE_CONTACT,
549 empathy_contact_get_tp_contact (EmpathyContact *contact)
551 EmpathyContactPriv *priv;
553 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
555 priv = GET_PRIV (contact);
557 return priv->tp_contact;
561 empathy_contact_get_id (EmpathyContact *contact)
563 EmpathyContactPriv *priv;
565 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
567 priv = GET_PRIV (contact);
569 if (priv->tp_contact != NULL)
570 return tp_contact_get_identifier (priv->tp_contact);
576 empathy_contact_set_id (EmpathyContact *contact,
579 EmpathyContactPriv *priv;
581 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
582 g_return_if_fail (id != NULL);
584 priv = GET_PRIV (contact);
586 /* We temporally ref the contact because it could be destroyed
587 * during the signal emition */
588 g_object_ref (contact);
589 if (tp_strdiff (id, priv->id))
592 priv->id = g_strdup (id);
594 g_object_notify (G_OBJECT (contact), "id");
595 if (EMP_STR_EMPTY (priv->alias))
596 g_object_notify (G_OBJECT (contact), "alias");
599 g_object_unref (contact);
603 empathy_contact_get_alias (EmpathyContact *contact)
605 EmpathyContactPriv *priv;
608 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
610 priv = GET_PRIV (contact);
612 if (priv->tp_contact != NULL)
613 alias = tp_contact_get_alias (priv->tp_contact);
617 if (!EMP_STR_EMPTY (alias))
620 return empathy_contact_get_id (contact);
624 empathy_contact_set_alias (EmpathyContact *contact,
627 EmpathyContactPriv *priv;
628 FolksPersona *persona;
630 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
632 priv = GET_PRIV (contact);
634 g_object_ref (contact);
636 /* Set the alias on the persona if possible */
637 persona = empathy_contact_get_persona (contact);
638 if (persona != NULL && FOLKS_IS_ALIAS (persona))
640 DEBUG ("Setting alias for contact %s to %s",
641 empathy_contact_get_id (contact), alias);
643 folks_alias_set_alias (FOLKS_ALIAS (persona), alias);
646 if (tp_strdiff (alias, priv->alias))
648 g_free (priv->alias);
649 priv->alias = g_strdup (alias);
650 g_object_notify (G_OBJECT (contact), "alias");
653 g_object_unref (contact);
657 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
660 EmpathyContactPriv *priv;
661 FolksPersona *persona;
663 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
664 g_return_if_fail (group != NULL);
666 priv = GET_PRIV (contact);
668 /* Normally pass through the changes to the persona */
669 persona = empathy_contact_get_persona (contact);
672 if (FOLKS_IS_GROUPS (persona))
673 folks_groups_change_group (FOLKS_GROUPS (persona), group, is_member);
677 /* If the persona doesn't exist yet, we have to cache the changes until it
679 if (priv->groups == NULL)
681 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
685 g_hash_table_insert (priv->groups, g_strdup (group),
686 GUINT_TO_POINTER (is_member));
690 empathy_contact_get_avatar (EmpathyContact *contact)
692 EmpathyContactPriv *priv;
694 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
696 priv = GET_PRIV (contact);
702 empathy_contact_set_avatar (EmpathyContact *contact,
703 EmpathyAvatar *avatar)
705 EmpathyContactPriv *priv;
707 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
709 priv = GET_PRIV (contact);
711 if (priv->avatar == avatar)
716 empathy_avatar_unref (priv->avatar);
721 priv->avatar = empathy_avatar_ref (avatar);
723 g_object_notify (G_OBJECT (contact), "avatar");
727 empathy_contact_get_account (EmpathyContact *contact)
729 EmpathyContactPriv *priv;
731 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
733 priv = GET_PRIV (contact);
735 if (priv->account == NULL && priv->tp_contact != NULL)
737 TpConnection *connection;
739 /* FIXME: This assume the account manager already exists */
740 connection = tp_contact_get_connection (priv->tp_contact);
742 g_object_ref (empathy_get_account_for_connection (connection));
745 return priv->account;
749 empathy_contact_get_persona (EmpathyContact *contact)
751 EmpathyContactPriv *priv;
753 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
755 priv = GET_PRIV (contact);
757 if (priv->persona == NULL && priv->tp_contact != NULL)
759 /* FIXME: This is disgustingly slow */
760 /* Query for the persona */
761 EmpathyIndividualManager *manager;
762 GList *individuals, *l;
764 manager = empathy_individual_manager_dup_singleton ();
765 individuals = empathy_individual_manager_get_members (manager);
767 for (l = individuals; l != NULL; l = l->next)
770 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
772 personas = folks_individual_get_personas (individual);
773 for (j = personas; j != NULL; j = j->next)
775 TpfPersona *persona = j->data;
777 if (TPF_IS_PERSONA (persona))
779 TpContact *tp_contact = tpf_persona_get_contact (persona);
781 if (tp_contact == priv->tp_contact)
783 /* Found the right persona */
784 priv->persona = g_object_ref (persona);
792 g_list_free (individuals);
793 g_object_unref (manager);
796 return priv->persona;
800 empathy_contact_set_persona (EmpathyContact *contact,
801 FolksPersona *persona)
803 EmpathyContactPriv *priv;
805 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
806 g_return_if_fail (TPF_IS_PERSONA (persona));
808 priv = GET_PRIV (contact);
810 if (persona == priv->persona)
813 if (priv->persona != NULL)
814 g_object_unref (priv->persona);
815 priv->persona = g_object_ref (persona);
817 g_object_notify (G_OBJECT (contact), "persona");
819 /* Set the persona's alias, since ours could've been set using
820 * empathy_contact_set_alias() before we had a persona; this happens when
821 * adding a contact. */
822 empathy_contact_set_alias (contact, priv->alias);
824 /* Set the persona's groups */
825 if (priv->groups != NULL)
827 folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
828 g_hash_table_destroy (priv->groups);
834 empathy_contact_get_connection (EmpathyContact *contact)
836 EmpathyContactPriv *priv;
838 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
840 priv = GET_PRIV (contact);
842 if (priv->tp_contact != NULL)
843 return tp_contact_get_connection (priv->tp_contact);
848 TpConnectionPresenceType
849 empathy_contact_get_presence (EmpathyContact *contact)
851 EmpathyContactPriv *priv;
853 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
854 TP_CONNECTION_PRESENCE_TYPE_UNSET);
856 priv = GET_PRIV (contact);
858 if (priv->tp_contact != NULL)
859 return tp_contact_get_presence_type (priv->tp_contact);
861 return priv->presence;
865 empathy_contact_set_presence (EmpathyContact *contact,
866 TpConnectionPresenceType presence)
868 EmpathyContactPriv *priv;
869 TpConnectionPresenceType old_presence;
871 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
873 priv = GET_PRIV (contact);
875 if (presence == priv->presence)
878 old_presence = priv->presence;
879 priv->presence = presence;
881 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
883 g_object_notify (G_OBJECT (contact), "presence");
887 empathy_contact_get_presence_message (EmpathyContact *contact)
889 EmpathyContactPriv *priv;
891 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
893 priv = GET_PRIV (contact);
895 if (priv->tp_contact != NULL)
896 return tp_contact_get_presence_message (priv->tp_contact);
898 return priv->presence_message;
902 empathy_contact_set_presence_message (EmpathyContact *contact,
903 const gchar *message)
905 EmpathyContactPriv *priv = GET_PRIV (contact);
907 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
909 if (!tp_strdiff (message, priv->presence_message))
912 g_free (priv->presence_message);
913 priv->presence_message = g_strdup (message);
915 g_object_notify (G_OBJECT (contact), "presence-message");
919 empathy_contact_get_handle (EmpathyContact *contact)
921 EmpathyContactPriv *priv;
923 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
925 priv = GET_PRIV (contact);
927 if (priv->tp_contact != NULL)
928 return tp_contact_get_handle (priv->tp_contact);
934 empathy_contact_set_handle (EmpathyContact *contact,
937 EmpathyContactPriv *priv;
939 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
941 priv = GET_PRIV (contact);
943 g_object_ref (contact);
944 if (handle != priv->handle)
946 priv->handle = handle;
947 g_object_notify (G_OBJECT (contact), "handle");
949 g_object_unref (contact);
953 empathy_contact_get_capabilities (EmpathyContact *contact)
955 EmpathyContactPriv *priv;
957 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
959 priv = GET_PRIV (contact);
961 return priv->capabilities;
965 empathy_contact_set_capabilities (EmpathyContact *contact,
966 EmpathyCapabilities capabilities)
968 EmpathyContactPriv *priv;
970 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
972 priv = GET_PRIV (contact);
974 if (priv->capabilities == capabilities)
977 priv->capabilities = capabilities;
979 g_object_notify (G_OBJECT (contact), "capabilities");
983 empathy_contact_is_user (EmpathyContact *contact)
985 EmpathyContactPriv *priv;
987 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
989 priv = GET_PRIV (contact);
991 return priv->is_user;
995 empathy_contact_set_is_user (EmpathyContact *contact,
998 EmpathyContactPriv *priv;
1000 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1002 priv = GET_PRIV (contact);
1004 if (priv->is_user == is_user)
1007 priv->is_user = is_user;
1009 g_object_notify (G_OBJECT (contact), "is-user");
1013 empathy_contact_is_online (EmpathyContact *contact)
1015 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1017 switch (empathy_contact_get_presence (contact))
1019 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1020 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1021 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1029 empathy_contact_get_status (EmpathyContact *contact)
1031 const gchar *message;
1033 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1035 message = empathy_contact_get_presence_message (contact);
1036 if (!EMP_STR_EMPTY (message))
1039 return empathy_presence_get_default_message (
1040 empathy_contact_get_presence (contact));
1044 empathy_contact_can_voip (EmpathyContact *contact)
1046 EmpathyContactPriv *priv;
1048 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1050 priv = GET_PRIV (contact);
1052 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1053 EMPATHY_CAPABILITIES_VIDEO);
1057 empathy_contact_can_voip_audio (EmpathyContact *contact)
1059 EmpathyContactPriv *priv;
1061 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1063 priv = GET_PRIV (contact);
1065 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1069 empathy_contact_can_voip_video (EmpathyContact *contact)
1071 EmpathyContactPriv *priv;
1073 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1075 priv = GET_PRIV (contact);
1077 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1081 empathy_contact_can_send_files (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_FT;
1093 empathy_contact_can_use_rfb_stream_tube (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_RFB_STREAM_TUBE;
1105 contact_get_avatar_filename (EmpathyContact *contact,
1111 gchar *token_escaped;
1113 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1116 token_escaped = tp_escape_as_identifier (token);
1117 account = empathy_contact_get_account (contact);
1119 avatar_path = g_build_filename (g_get_user_cache_dir (),
1122 tp_account_get_connection_manager (account),
1123 tp_account_get_protocol (account),
1125 g_mkdir_with_parents (avatar_path, 0700);
1127 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1129 g_free (token_escaped);
1130 g_free (avatar_path);
1136 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1139 EmpathyAvatar *avatar = NULL;
1143 GError *error = NULL;
1145 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1146 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1148 /* Load the avatar from file if it exists */
1149 filename = contact_get_avatar_filename (contact, token);
1150 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1152 if (!g_file_get_contents (filename, &data, &len, &error))
1154 DEBUG ("Failed to load avatar from cache: %s",
1155 error ? error->message : "No error given");
1156 g_clear_error (&error);
1162 DEBUG ("Avatar loaded from %s", filename);
1163 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1165 empathy_contact_set_avatar (contact, avatar);
1166 empathy_avatar_unref (avatar);
1173 return data != NULL;
1177 empathy_avatar_get_type (void)
1179 static GType type_id = 0;
1183 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1184 (GBoxedCopyFunc) empathy_avatar_ref,
1185 (GBoxedFreeFunc) empathy_avatar_unref);
1192 * empathy_avatar_new:
1193 * @data: the avatar data
1194 * @len: the size of avatar data
1195 * @format: the mime type of the avatar image
1196 * @token: the token of the avatar
1197 * @filename: the filename where the avatar is stored in cache
1199 * Create a #EmpathyAvatar from the provided data. This function takes the
1200 * ownership of @data, @format, @token and @filename.
1202 * Returns: a new #EmpathyAvatar
1205 empathy_avatar_new (guchar *data,
1211 EmpathyAvatar *avatar;
1213 avatar = g_slice_new0 (EmpathyAvatar);
1214 avatar->data = data;
1216 avatar->format = format;
1217 avatar->token = token;
1218 avatar->filename = filename;
1219 avatar->refcount = 1;
1225 empathy_avatar_unref (EmpathyAvatar *avatar)
1227 g_return_if_fail (avatar != NULL);
1230 if (avatar->refcount == 0)
1232 g_free (avatar->data);
1233 g_free (avatar->format);
1234 g_free (avatar->token);
1235 g_free (avatar->filename);
1236 g_slice_free (EmpathyAvatar, avatar);
1241 empathy_avatar_ref (EmpathyAvatar *avatar)
1243 g_return_val_if_fail (avatar != NULL, NULL);
1251 * empathy_avatar_save_to_file:
1252 * @avatar: the avatar
1253 * @filename: name of a file to write avatar to
1254 * @error: return location for a GError, or NULL
1256 * Save the avatar to a file named filename
1258 * Returns: %TRUE on success, %FALSE if an error occurred
1261 empathy_avatar_save_to_file (EmpathyAvatar *self,
1262 const gchar *filename,
1265 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1270 * empathy_contact_get_location:
1271 * @contact: an #EmpathyContact
1273 * Returns the user's location if available. The keys are defined in
1274 * empathy-location.h. If the contact doesn't have location
1275 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1276 * you are done with the #GHashTable.
1278 * It is composed of string keys and GValues. Keys are
1279 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1280 * Example: a "city" key would have "Helsinki" as string GValue,
1281 * a "latitude" would have 65.0 as double GValue.
1283 * Returns: a #GHashTable of location values
1286 empathy_contact_get_location (EmpathyContact *contact)
1288 EmpathyContactPriv *priv;
1290 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1292 priv = GET_PRIV (contact);
1294 return priv->location;
1298 * empathy_contact_set_location:
1299 * @contact: an #EmpathyContact
1300 * @location: a #GHashTable of the location
1302 * Sets the user's location based on the location #GHashTable passed.
1303 * It is composed of string keys and GValues. Keys are
1304 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1305 * Example: a "city" key would have "Helsinki" as string GValue,
1306 * a "latitude" would have 65.0 as double GValue.
1309 empathy_contact_set_location (EmpathyContact *contact,
1310 GHashTable *location)
1312 EmpathyContactPriv *priv;
1314 g_return_if_fail (EMPATHY_CONTACT (contact));
1315 g_return_if_fail (location != NULL);
1317 priv = GET_PRIV (contact);
1319 if (priv->location != NULL)
1320 g_hash_table_unref (priv->location);
1322 priv->location = g_hash_table_ref (location);
1324 update_geocode (contact);
1326 g_object_notify (G_OBJECT (contact), "location");
1330 * empathy_contact_equal:
1331 * @contact1: an #EmpathyContact
1332 * @contact2: an #EmpathyContact
1334 * Returns FALSE if one of the contacts is NULL but the other is not.
1335 * Otherwise returns TRUE if both pointer are equal or if they bith
1336 * refer to the same id.
1337 * It's only necessary to call this function if your contact objects
1338 * come from logs where contacts are created dynamically and comparing
1339 * pointers is not enough.
1342 empathy_contact_equal (gconstpointer contact1,
1343 gconstpointer contact2)
1350 if ((contact1 == NULL) != (contact2 == NULL)) {
1353 if (contact1 == contact2) {
1356 c1 = EMPATHY_CONTACT (contact1);
1357 c2 = EMPATHY_CONTACT (contact2);
1358 id1 = empathy_contact_get_id (c1);
1359 id2 = empathy_contact_get_id (c2);
1360 if (!tp_strdiff (id1, id2)) {
1367 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1368 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1370 /* This callback is called by geoclue when it found a position
1371 * for the given address. A position is necessary for a contact
1372 * to show up on the map
1375 geocode_cb (GeoclueGeocode *geocode,
1376 GeocluePositionFields fields,
1380 GeoclueAccuracy *accuracy,
1384 EmpathyContactPriv *priv = GET_PRIV (contact);
1385 GHashTable *new_location;
1387 if (priv->location == NULL)
1392 DEBUG ("Error geocoding location : %s", error->message);
1396 /* No need to change location if we didn't find the position */
1397 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1400 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1403 new_location = tp_asv_new (
1404 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1405 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1408 DEBUG ("\t - Latitude: %f", latitude);
1409 DEBUG ("\t - Longitude: %f", longitude);
1411 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1412 * the values we just set. */
1413 tp_g_hash_table_update (new_location, priv->location,
1414 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1416 /* Set the altitude only if it wasn't defined before */
1417 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1418 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1420 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1422 DEBUG ("\t - Altitude: %f", altitude);
1425 /* Don't change the accuracy as we used an address to get this position */
1426 g_hash_table_unref (priv->location);
1427 priv->location = new_location;
1428 g_object_notify (contact, "location");
1430 g_object_unref (geocode);
1431 g_object_unref (contact);
1435 get_dup_string (GHashTable *location,
1440 value = g_hash_table_lookup (location, key);
1442 return g_value_dup_string (value);
1448 update_geocode (EmpathyContact *contact)
1450 static GeoclueGeocode *geocode;
1452 GHashTable *address;
1453 GHashTable *location;
1455 location = empathy_contact_get_location (contact);
1456 if (location == NULL)
1459 /* No need to search for position if contact published it */
1460 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1461 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1464 if (geocode == NULL)
1466 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1467 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1471 g_object_ref (geocode);
1474 address = geoclue_address_details_new ();
1476 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1479 g_hash_table_insert (address,
1480 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1481 DEBUG ("\t - countrycode: %s", str);
1484 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1487 g_hash_table_insert (address,
1488 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1489 DEBUG ("\t - country: %s", str);
1492 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1495 g_hash_table_insert (address,
1496 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1497 DEBUG ("\t - postalcode: %s", str);
1500 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1503 g_hash_table_insert (address,
1504 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1505 DEBUG ("\t - region: %s", str);
1508 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1511 g_hash_table_insert (address,
1512 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1513 DEBUG ("\t - locality: %s", str);
1516 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1519 g_hash_table_insert (address,
1520 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1521 DEBUG ("\t - street: %s", str);
1524 if (g_hash_table_size (address) > 0)
1526 g_object_ref (contact);
1528 geoclue_geocode_address_to_position_async (geocode, address,
1529 geocode_cb, contact);
1532 g_hash_table_unref (address);
1536 static EmpathyCapabilities
1537 tp_caps_to_capabilities (TpCapabilities *caps)
1539 EmpathyCapabilities capabilities = 0;
1543 classes = tp_capabilities_get_channel_classes (caps);
1545 for (i = 0; i < classes->len; i++)
1547 GValueArray *class_struct;
1548 GHashTable *fixed_prop;
1550 TpHandleType handle_type;
1551 const gchar *chan_type;
1553 class_struct = g_ptr_array_index (classes, i);
1554 tp_value_array_unpack (class_struct, 2,
1558 handle_type = tp_asv_get_uint32 (fixed_prop,
1559 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1560 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1563 chan_type = tp_asv_get_string (fixed_prop,
1564 TP_PROP_CHANNEL_CHANNEL_TYPE);
1566 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1568 capabilities |= EMPATHY_CAPABILITIES_FT;
1570 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1572 const gchar *service;
1574 service = tp_asv_get_string (fixed_prop,
1575 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1577 if (!tp_strdiff (service, "rfb"))
1578 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1580 else if (!tp_strdiff (chan_type,
1581 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1585 for (j = 0; allowed_prop[j] != NULL; j++)
1587 if (!tp_strdiff (allowed_prop[j],
1588 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1589 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1590 else if (!tp_strdiff (allowed_prop[j],
1591 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1592 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1597 return capabilities;
1601 set_capabilities_from_tp_caps (EmpathyContact *self,
1602 TpCapabilities *caps)
1604 EmpathyCapabilities capabilities;
1609 capabilities = tp_caps_to_capabilities (caps);
1610 empathy_contact_set_capabilities (self, capabilities);
1614 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1616 EmpathyContactPriv *priv = GET_PRIV (contact);
1621 token = tp_contact_get_avatar_token (priv->tp_contact);
1622 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1623 file = tp_contact_get_avatar_file (priv->tp_contact);
1627 EmpathyAvatar *avatar;
1631 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1632 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1633 g_file_get_path (file));
1634 empathy_contact_set_avatar (contact, avatar);
1635 empathy_avatar_unref (avatar);
1639 empathy_contact_set_avatar (contact, NULL);
1644 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1646 EmpathyContact *contact = NULL;
1648 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1650 if (contacts_table == NULL)
1651 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1653 contact = g_hash_table_lookup (contacts_table, tp_contact);
1655 if (contact == NULL)
1657 contact = empathy_contact_new (tp_contact);
1659 /* The hash table does not keep any ref.
1660 * contact keeps a ref to tp_contact, and is removed from the table in
1661 * contact_dispose() */
1662 g_hash_table_insert (contacts_table, tp_contact, contact);
1666 g_object_ref (contact);