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 (FOLKS_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 if (FOLKS_IS_GROUPS (persona))
828 folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
830 g_hash_table_destroy (priv->groups);
836 empathy_contact_get_connection (EmpathyContact *contact)
838 EmpathyContactPriv *priv;
840 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
842 priv = GET_PRIV (contact);
844 if (priv->tp_contact != NULL)
845 return tp_contact_get_connection (priv->tp_contact);
850 TpConnectionPresenceType
851 empathy_contact_get_presence (EmpathyContact *contact)
853 EmpathyContactPriv *priv;
855 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
856 TP_CONNECTION_PRESENCE_TYPE_UNSET);
858 priv = GET_PRIV (contact);
860 if (priv->tp_contact != NULL)
861 return tp_contact_get_presence_type (priv->tp_contact);
863 return priv->presence;
867 empathy_contact_set_presence (EmpathyContact *contact,
868 TpConnectionPresenceType presence)
870 EmpathyContactPriv *priv;
871 TpConnectionPresenceType old_presence;
873 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
875 priv = GET_PRIV (contact);
877 if (presence == priv->presence)
880 old_presence = priv->presence;
881 priv->presence = presence;
883 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
885 g_object_notify (G_OBJECT (contact), "presence");
889 empathy_contact_get_presence_message (EmpathyContact *contact)
891 EmpathyContactPriv *priv;
893 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
895 priv = GET_PRIV (contact);
897 if (priv->tp_contact != NULL)
898 return tp_contact_get_presence_message (priv->tp_contact);
900 return priv->presence_message;
904 empathy_contact_set_presence_message (EmpathyContact *contact,
905 const gchar *message)
907 EmpathyContactPriv *priv = GET_PRIV (contact);
909 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
911 if (!tp_strdiff (message, priv->presence_message))
914 g_free (priv->presence_message);
915 priv->presence_message = g_strdup (message);
917 g_object_notify (G_OBJECT (contact), "presence-message");
921 empathy_contact_get_handle (EmpathyContact *contact)
923 EmpathyContactPriv *priv;
925 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
927 priv = GET_PRIV (contact);
929 if (priv->tp_contact != NULL)
930 return tp_contact_get_handle (priv->tp_contact);
936 empathy_contact_set_handle (EmpathyContact *contact,
939 EmpathyContactPriv *priv;
941 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
943 priv = GET_PRIV (contact);
945 g_object_ref (contact);
946 if (handle != priv->handle)
948 priv->handle = handle;
949 g_object_notify (G_OBJECT (contact), "handle");
951 g_object_unref (contact);
955 empathy_contact_get_capabilities (EmpathyContact *contact)
957 EmpathyContactPriv *priv;
959 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
961 priv = GET_PRIV (contact);
963 return priv->capabilities;
967 empathy_contact_set_capabilities (EmpathyContact *contact,
968 EmpathyCapabilities capabilities)
970 EmpathyContactPriv *priv;
972 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
974 priv = GET_PRIV (contact);
976 if (priv->capabilities == capabilities)
979 priv->capabilities = capabilities;
981 g_object_notify (G_OBJECT (contact), "capabilities");
985 empathy_contact_is_user (EmpathyContact *contact)
987 EmpathyContactPriv *priv;
989 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
991 priv = GET_PRIV (contact);
993 return priv->is_user;
997 empathy_contact_set_is_user (EmpathyContact *contact,
1000 EmpathyContactPriv *priv;
1002 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1004 priv = GET_PRIV (contact);
1006 if (priv->is_user == is_user)
1009 priv->is_user = is_user;
1011 g_object_notify (G_OBJECT (contact), "is-user");
1015 empathy_contact_is_online (EmpathyContact *contact)
1017 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1019 switch (empathy_contact_get_presence (contact))
1021 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1022 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1023 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1031 empathy_contact_get_status (EmpathyContact *contact)
1033 const gchar *message;
1035 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1037 message = empathy_contact_get_presence_message (contact);
1038 if (!EMP_STR_EMPTY (message))
1041 return empathy_presence_get_default_message (
1042 empathy_contact_get_presence (contact));
1046 empathy_contact_can_voip (EmpathyContact *contact)
1048 EmpathyContactPriv *priv;
1050 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1052 priv = GET_PRIV (contact);
1054 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1055 EMPATHY_CAPABILITIES_VIDEO);
1059 empathy_contact_can_voip_audio (EmpathyContact *contact)
1061 EmpathyContactPriv *priv;
1063 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1065 priv = GET_PRIV (contact);
1067 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1071 empathy_contact_can_voip_video (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_VIDEO;
1083 empathy_contact_can_send_files (EmpathyContact *contact)
1085 EmpathyContactPriv *priv;
1087 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1089 priv = GET_PRIV (contact);
1091 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1095 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1097 EmpathyContactPriv *priv;
1099 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1101 priv = GET_PRIV (contact);
1103 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1107 contact_get_avatar_filename (EmpathyContact *contact,
1113 gchar *token_escaped;
1115 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1118 token_escaped = tp_escape_as_identifier (token);
1119 account = empathy_contact_get_account (contact);
1121 avatar_path = g_build_filename (g_get_user_cache_dir (),
1124 tp_account_get_connection_manager (account),
1125 tp_account_get_protocol (account),
1127 g_mkdir_with_parents (avatar_path, 0700);
1129 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1131 g_free (token_escaped);
1132 g_free (avatar_path);
1138 empathy_contact_load_avatar_cache (EmpathyContact *contact,
1141 EmpathyAvatar *avatar = NULL;
1145 GError *error = NULL;
1147 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1148 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1150 /* Load the avatar from file if it exists */
1151 filename = contact_get_avatar_filename (contact, token);
1152 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1154 if (!g_file_get_contents (filename, &data, &len, &error))
1156 DEBUG ("Failed to load avatar from cache: %s",
1157 error ? error->message : "No error given");
1158 g_clear_error (&error);
1164 DEBUG ("Avatar loaded from %s", filename);
1165 avatar = empathy_avatar_new ((guchar *) data, len, NULL, g_strdup (token),
1167 empathy_contact_set_avatar (contact, avatar);
1168 empathy_avatar_unref (avatar);
1175 return data != NULL;
1179 empathy_avatar_get_type (void)
1181 static GType type_id = 0;
1185 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1186 (GBoxedCopyFunc) empathy_avatar_ref,
1187 (GBoxedFreeFunc) empathy_avatar_unref);
1194 * empathy_avatar_new:
1195 * @data: the avatar data
1196 * @len: the size of avatar data
1197 * @format: the mime type of the avatar image
1198 * @token: the token of the avatar
1199 * @filename: the filename where the avatar is stored in cache
1201 * Create a #EmpathyAvatar from the provided data. This function takes the
1202 * ownership of @data, @format, @token and @filename.
1204 * Returns: a new #EmpathyAvatar
1207 empathy_avatar_new (guchar *data,
1213 EmpathyAvatar *avatar;
1215 avatar = g_slice_new0 (EmpathyAvatar);
1216 avatar->data = data;
1218 avatar->format = format;
1219 avatar->token = token;
1220 avatar->filename = filename;
1221 avatar->refcount = 1;
1227 empathy_avatar_unref (EmpathyAvatar *avatar)
1229 g_return_if_fail (avatar != NULL);
1232 if (avatar->refcount == 0)
1234 g_free (avatar->data);
1235 g_free (avatar->format);
1236 g_free (avatar->token);
1237 g_free (avatar->filename);
1238 g_slice_free (EmpathyAvatar, avatar);
1243 empathy_avatar_ref (EmpathyAvatar *avatar)
1245 g_return_val_if_fail (avatar != NULL, NULL);
1253 * empathy_avatar_save_to_file:
1254 * @avatar: the avatar
1255 * @filename: name of a file to write avatar to
1256 * @error: return location for a GError, or NULL
1258 * Save the avatar to a file named filename
1260 * Returns: %TRUE on success, %FALSE if an error occurred
1263 empathy_avatar_save_to_file (EmpathyAvatar *self,
1264 const gchar *filename,
1267 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1272 * empathy_contact_get_location:
1273 * @contact: an #EmpathyContact
1275 * Returns the user's location if available. The keys are defined in
1276 * empathy-location.h. If the contact doesn't have location
1277 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1278 * you are done with the #GHashTable.
1280 * It is composed of string keys and GValues. Keys are
1281 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1282 * Example: a "city" key would have "Helsinki" as string GValue,
1283 * a "latitude" would have 65.0 as double GValue.
1285 * Returns: a #GHashTable of location values
1288 empathy_contact_get_location (EmpathyContact *contact)
1290 EmpathyContactPriv *priv;
1292 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1294 priv = GET_PRIV (contact);
1296 return priv->location;
1300 * empathy_contact_set_location:
1301 * @contact: an #EmpathyContact
1302 * @location: a #GHashTable of the location
1304 * Sets the user's location based on the location #GHashTable passed.
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.
1311 empathy_contact_set_location (EmpathyContact *contact,
1312 GHashTable *location)
1314 EmpathyContactPriv *priv;
1316 g_return_if_fail (EMPATHY_CONTACT (contact));
1317 g_return_if_fail (location != NULL);
1319 priv = GET_PRIV (contact);
1321 if (priv->location != NULL)
1322 g_hash_table_unref (priv->location);
1324 priv->location = g_hash_table_ref (location);
1326 update_geocode (contact);
1328 g_object_notify (G_OBJECT (contact), "location");
1332 * empathy_contact_equal:
1333 * @contact1: an #EmpathyContact
1334 * @contact2: an #EmpathyContact
1336 * Returns FALSE if one of the contacts is NULL but the other is not.
1337 * Otherwise returns TRUE if both pointer are equal or if they bith
1338 * refer to the same id.
1339 * It's only necessary to call this function if your contact objects
1340 * come from logs where contacts are created dynamically and comparing
1341 * pointers is not enough.
1344 empathy_contact_equal (gconstpointer contact1,
1345 gconstpointer contact2)
1352 if ((contact1 == NULL) != (contact2 == NULL)) {
1355 if (contact1 == contact2) {
1358 c1 = EMPATHY_CONTACT (contact1);
1359 c2 = EMPATHY_CONTACT (contact2);
1360 id1 = empathy_contact_get_id (c1);
1361 id2 = empathy_contact_get_id (c2);
1362 if (!tp_strdiff (id1, id2)) {
1369 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1370 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1372 /* This callback is called by geoclue when it found a position
1373 * for the given address. A position is necessary for a contact
1374 * to show up on the map
1377 geocode_cb (GeoclueGeocode *geocode,
1378 GeocluePositionFields fields,
1382 GeoclueAccuracy *accuracy,
1386 EmpathyContactPriv *priv = GET_PRIV (contact);
1387 GHashTable *new_location;
1389 if (priv->location == NULL)
1394 DEBUG ("Error geocoding location : %s", error->message);
1398 /* No need to change location if we didn't find the position */
1399 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1402 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1405 new_location = tp_asv_new (
1406 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1407 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1410 DEBUG ("\t - Latitude: %f", latitude);
1411 DEBUG ("\t - Longitude: %f", longitude);
1413 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1414 * the values we just set. */
1415 tp_g_hash_table_update (new_location, priv->location,
1416 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1418 /* Set the altitude only if it wasn't defined before */
1419 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1420 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1422 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1424 DEBUG ("\t - Altitude: %f", altitude);
1427 /* Don't change the accuracy as we used an address to get this position */
1428 g_hash_table_unref (priv->location);
1429 priv->location = new_location;
1430 g_object_notify (contact, "location");
1432 g_object_unref (geocode);
1433 g_object_unref (contact);
1437 get_dup_string (GHashTable *location,
1442 value = g_hash_table_lookup (location, key);
1444 return g_value_dup_string (value);
1450 update_geocode (EmpathyContact *contact)
1452 static GeoclueGeocode *geocode;
1454 GHashTable *address;
1455 GHashTable *location;
1457 location = empathy_contact_get_location (contact);
1458 if (location == NULL)
1461 /* No need to search for position if contact published it */
1462 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1463 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1466 if (geocode == NULL)
1468 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1469 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1473 g_object_ref (geocode);
1476 address = geoclue_address_details_new ();
1478 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1481 g_hash_table_insert (address,
1482 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1483 DEBUG ("\t - countrycode: %s", str);
1486 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1489 g_hash_table_insert (address,
1490 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1491 DEBUG ("\t - country: %s", str);
1494 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1497 g_hash_table_insert (address,
1498 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1499 DEBUG ("\t - postalcode: %s", str);
1502 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1505 g_hash_table_insert (address,
1506 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1507 DEBUG ("\t - region: %s", str);
1510 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1513 g_hash_table_insert (address,
1514 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1515 DEBUG ("\t - locality: %s", str);
1518 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1521 g_hash_table_insert (address,
1522 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1523 DEBUG ("\t - street: %s", str);
1526 if (g_hash_table_size (address) > 0)
1528 g_object_ref (contact);
1530 geoclue_geocode_address_to_position_async (geocode, address,
1531 geocode_cb, contact);
1534 g_hash_table_unref (address);
1538 static EmpathyCapabilities
1539 tp_caps_to_capabilities (TpCapabilities *caps)
1541 EmpathyCapabilities capabilities = 0;
1545 classes = tp_capabilities_get_channel_classes (caps);
1547 for (i = 0; i < classes->len; i++)
1549 GValueArray *class_struct;
1550 GHashTable *fixed_prop;
1552 TpHandleType handle_type;
1553 const gchar *chan_type;
1555 class_struct = g_ptr_array_index (classes, i);
1556 tp_value_array_unpack (class_struct, 2,
1560 handle_type = tp_asv_get_uint32 (fixed_prop,
1561 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1562 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1565 chan_type = tp_asv_get_string (fixed_prop,
1566 TP_PROP_CHANNEL_CHANNEL_TYPE);
1568 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1570 capabilities |= EMPATHY_CAPABILITIES_FT;
1572 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1574 const gchar *service;
1576 service = tp_asv_get_string (fixed_prop,
1577 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1579 if (!tp_strdiff (service, "rfb"))
1580 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1582 else if (!tp_strdiff (chan_type,
1583 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1587 for (j = 0; allowed_prop[j] != NULL; j++)
1589 if (!tp_strdiff (allowed_prop[j],
1590 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1591 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1592 else if (!tp_strdiff (allowed_prop[j],
1593 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1594 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1599 return capabilities;
1603 set_capabilities_from_tp_caps (EmpathyContact *self,
1604 TpCapabilities *caps)
1606 EmpathyCapabilities capabilities;
1611 capabilities = tp_caps_to_capabilities (caps);
1612 empathy_contact_set_capabilities (self, capabilities);
1616 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1618 EmpathyContactPriv *priv = GET_PRIV (contact);
1623 token = tp_contact_get_avatar_token (priv->tp_contact);
1624 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1625 file = tp_contact_get_avatar_file (priv->tp_contact);
1629 EmpathyAvatar *avatar;
1633 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1634 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime), g_strdup (token),
1635 g_file_get_path (file));
1636 empathy_contact_set_avatar (contact, avatar);
1637 empathy_avatar_unref (avatar);
1641 empathy_contact_set_avatar (contact, NULL);
1646 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1648 EmpathyContact *contact = NULL;
1650 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1652 if (contacts_table == NULL)
1653 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1655 contact = g_hash_table_lookup (contacts_table, tp_contact);
1657 if (contact == NULL)
1659 contact = empathy_contact_new (tp_contact);
1661 /* The hash table does not keep any ref.
1662 * contact keeps a ref to tp_contact, and is removed from the table in
1663 * contact_dispose() */
1664 g_hash_table_insert (contacts_table, tp_contact, contact);
1668 g_object_ref (contact);