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 <telepathy-logger/log-manager.h>
34 #include <folks/folks.h>
35 #include <folks/folks-telepathy.h>
38 #include <geoclue/geoclue-geocode.h>
41 #include "empathy-contact.h"
42 #include "empathy-utils.h"
43 #include "empathy-enum-types.h"
44 #include "empathy-marshal.h"
45 #include "empathy-location.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
48 #include "empathy-debug.h"
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
52 TpContact *tp_contact;
54 FolksPersona *persona;
57 EmpathyAvatar *avatar;
58 TpConnectionPresenceType presence;
60 EmpathyCapabilities capabilities;
63 /* Location is composed of string keys and GValues.
64 * Example: a "city" key would have "Helsinki" as string GValue,
65 * a "latitude" would have 65.0 as double GValue.
67 * This is a super set of the location stored in TpContact as we can try add
68 * more fields by searching the address using geoclue.
74 static void contact_finalize (GObject *object);
75 static void contact_get_property (GObject *object, guint param_id,
76 GValue *value, GParamSpec *pspec);
77 static void contact_set_property (GObject *object, guint param_id,
78 const GValue *value, GParamSpec *pspec);
81 static void update_geocode (EmpathyContact *contact);
84 static void empathy_contact_set_location (EmpathyContact *contact,
85 GHashTable *location);
87 static void set_capabilities_from_tp_caps (EmpathyContact *self,
88 TpCapabilities *caps);
90 static void contact_set_avatar (EmpathyContact *contact,
91 EmpathyAvatar *avatar);
92 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
93 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
96 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
108 PROP_PRESENCE_MESSAGE,
120 static guint signals[LAST_SIGNAL];
122 /* TpContact* -> EmpathyContact*, both borrowed ref */
123 static GHashTable *contacts_table = NULL;
126 tp_contact_notify_cb (TpContact *tp_contact,
130 EmpathyContactPriv *priv = GET_PRIV (contact);
132 /* Forward property notifications */
133 if (!tp_strdiff (param->name, "alias"))
134 g_object_notify (contact, "alias");
135 else if (!tp_strdiff (param->name, "presence-type")) {
136 TpConnectionPresenceType presence;
138 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
139 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
141 priv->presence = presence;
142 g_object_notify (contact, "presence");
144 else if (!tp_strdiff (param->name, "identifier"))
145 g_object_notify (contact, "id");
146 else if (!tp_strdiff (param->name, "handle"))
147 g_object_notify (contact, "handle");
148 else if (!tp_strdiff (param->name, "location"))
150 GHashTable *location;
152 location = tp_contact_get_location (tp_contact);
153 /* This will start a geoclue search to find the address if needed */
154 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
156 else if (!tp_strdiff (param->name, "capabilities"))
158 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
159 tp_contact_get_capabilities (tp_contact));
161 else if (!tp_strdiff (param->name, "avatar-file"))
163 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
168 folks_persona_notify_cb (FolksPersona *folks_persona,
172 if (!tp_strdiff (param->name, "presence-message"))
173 g_object_notify (contact, "presence-message");
177 contact_dispose (GObject *object)
179 EmpathyContactPriv *priv = GET_PRIV (object);
181 if (priv->tp_contact)
183 g_hash_table_remove (contacts_table, priv->tp_contact);
184 g_signal_handlers_disconnect_by_func (priv->tp_contact,
185 tp_contact_notify_cb, object);
186 g_object_unref (priv->tp_contact);
188 priv->tp_contact = NULL;
191 g_object_unref (priv->account);
192 priv->account = NULL;
196 g_signal_handlers_disconnect_by_func (priv->persona,
197 folks_persona_notify_cb, object);
198 g_object_unref (priv->persona);
200 priv->persona = NULL;
202 if (priv->avatar != NULL)
204 empathy_avatar_unref (priv->avatar);
208 if (priv->location != NULL)
210 g_hash_table_unref (priv->location);
211 priv->location = NULL;
214 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
218 contact_constructed (GObject *object)
220 EmpathyContact *contact = (EmpathyContact *) object;
221 EmpathyContactPriv *priv = GET_PRIV (contact);
222 GHashTable *location;
223 TpHandle self_handle;
226 if (priv->tp_contact == NULL)
229 priv->presence = empathy_contact_get_presence (contact);
231 location = tp_contact_get_location (priv->tp_contact);
232 if (location != NULL)
233 empathy_contact_set_location (contact, location);
235 set_capabilities_from_tp_caps (contact,
236 tp_contact_get_capabilities (priv->tp_contact));
238 contact_set_avatar_from_tp_contact (contact);
240 /* Set is-user property. Note that it could still be the handle is
241 * different from the connection's self handle, in the case the handle
242 * comes from a group interface. */
243 self_handle = tp_connection_get_self_handle (
244 tp_contact_get_connection (priv->tp_contact));
245 handle = tp_contact_get_handle (priv->tp_contact);
246 empathy_contact_set_is_user (contact, self_handle == handle);
248 g_signal_connect (priv->tp_contact, "notify",
249 G_CALLBACK (tp_contact_notify_cb), contact);
253 empathy_contact_class_init (EmpathyContactClass *class)
255 GObjectClass *object_class;
257 object_class = G_OBJECT_CLASS (class);
259 object_class->finalize = contact_finalize;
260 object_class->dispose = contact_dispose;
261 object_class->get_property = contact_get_property;
262 object_class->set_property = contact_set_property;
263 object_class->constructed = contact_constructed;
265 g_object_class_install_property (object_class,
267 g_param_spec_object ("tp-contact",
269 "The TpContact associated with the contact",
271 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (object_class,
275 g_param_spec_object ("account",
277 "The account associated with the contact",
279 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (object_class,
283 g_param_spec_object ("persona",
285 "The FolksPersona associated with the contact",
287 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (object_class,
291 g_param_spec_string ("id",
293 "String identifying contact",
295 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (object_class,
299 g_param_spec_string ("alias",
301 "An alias for the contact",
303 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305 g_object_class_install_property (object_class,
307 g_param_spec_boxed ("avatar",
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (object_class,
315 g_param_spec_uint ("presence",
317 "Presence of contact",
318 TP_CONNECTION_PRESENCE_TYPE_UNSET,
319 NUM_TP_CONNECTION_PRESENCE_TYPES,
320 TP_CONNECTION_PRESENCE_TYPE_UNSET,
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (object_class,
324 PROP_PRESENCE_MESSAGE,
325 g_param_spec_string ("presence-message",
326 "Contact presence message",
327 "Presence message of contact",
329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (object_class,
333 g_param_spec_uint ("handle",
335 "The handle of the contact",
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (object_class,
343 g_param_spec_flags ("capabilities",
344 "Contact Capabilities",
345 "Capabilities of the contact",
346 EMPATHY_TYPE_CAPABILITIES,
347 EMPATHY_CAPABILITIES_UNKNOWN,
348 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (object_class,
352 g_param_spec_boolean ("is-user",
354 "Is contact the user",
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (object_class,
361 g_param_spec_boxed ("location",
363 "Physical location of the contact",
365 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
367 signals[PRESENCE_CHANGED] =
368 g_signal_new ("presence-changed",
369 G_TYPE_FROM_CLASS (class),
373 _empathy_marshal_VOID__UINT_UINT,
378 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
382 empathy_contact_init (EmpathyContact *contact)
384 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
385 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
387 contact->priv = priv;
389 priv->location = NULL;
394 contact_finalize (GObject *object)
396 EmpathyContactPriv *priv;
398 priv = GET_PRIV (object);
400 DEBUG ("finalize: %p", object);
402 if (priv->groups != NULL)
403 g_hash_table_destroy (priv->groups);
404 g_free (priv->alias);
407 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
411 empathy_contact_set_capabilities (EmpathyContact *contact,
412 EmpathyCapabilities capabilities)
414 EmpathyContactPriv *priv;
416 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
418 priv = GET_PRIV (contact);
420 if (priv->capabilities == capabilities)
423 priv->capabilities = capabilities;
425 g_object_notify (G_OBJECT (contact), "capabilities");
429 empathy_contact_set_id (EmpathyContact *contact,
432 EmpathyContactPriv *priv;
434 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
435 g_return_if_fail (id != NULL);
437 priv = GET_PRIV (contact);
439 /* We temporally ref the contact because it could be destroyed
440 * during the signal emition */
441 g_object_ref (contact);
442 if (tp_strdiff (id, priv->id))
445 priv->id = g_strdup (id);
447 g_object_notify (G_OBJECT (contact), "id");
448 if (EMP_STR_EMPTY (priv->alias))
449 g_object_notify (G_OBJECT (contact), "alias");
452 g_object_unref (contact);
456 empathy_contact_set_presence (EmpathyContact *contact,
457 TpConnectionPresenceType presence)
459 EmpathyContactPriv *priv;
460 TpConnectionPresenceType old_presence;
462 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
464 priv = GET_PRIV (contact);
466 if (presence == priv->presence)
469 old_presence = priv->presence;
470 priv->presence = presence;
472 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
474 g_object_notify (G_OBJECT (contact), "presence");
478 empathy_contact_set_presence_message (EmpathyContact *contact,
479 const gchar *message)
481 EmpathyContactPriv *priv = GET_PRIV (contact);
483 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
485 if (priv->persona != NULL)
487 folks_presence_set_presence_message (FOLKS_PRESENCE (priv->persona),
493 empathy_contact_set_handle (EmpathyContact *contact,
496 EmpathyContactPriv *priv;
498 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
500 priv = GET_PRIV (contact);
502 g_object_ref (contact);
503 if (handle != priv->handle)
505 priv->handle = handle;
506 g_object_notify (G_OBJECT (contact), "handle");
508 g_object_unref (contact);
512 contact_get_property (GObject *object,
517 EmpathyContact *contact = EMPATHY_CONTACT (object);
521 case PROP_TP_CONTACT:
522 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
525 g_value_set_object (value, empathy_contact_get_account (contact));
528 g_value_set_object (value, empathy_contact_get_persona (contact));
531 g_value_set_string (value, empathy_contact_get_id (contact));
534 g_value_set_string (value, empathy_contact_get_alias (contact));
537 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
540 g_value_set_uint (value, empathy_contact_get_presence (contact));
542 case PROP_PRESENCE_MESSAGE:
543 g_value_set_string (value, empathy_contact_get_presence_message (contact));
546 g_value_set_uint (value, empathy_contact_get_handle (contact));
548 case PROP_CAPABILITIES:
549 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
552 g_value_set_boolean (value, empathy_contact_is_user (contact));
555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
561 contact_set_property (GObject *object,
566 EmpathyContact *contact = EMPATHY_CONTACT (object);
567 EmpathyContactPriv *priv = GET_PRIV (object);
571 case PROP_TP_CONTACT:
572 priv->tp_contact = g_value_dup_object (value);
575 g_assert (priv->account == NULL);
576 priv->account = g_value_dup_object (value);
579 empathy_contact_set_persona (contact, g_value_get_object (value));
582 empathy_contact_set_id (contact, g_value_get_string (value));
585 empathy_contact_set_alias (contact, g_value_get_string (value));
588 empathy_contact_set_presence (contact, g_value_get_uint (value));
590 case PROP_PRESENCE_MESSAGE:
591 empathy_contact_set_presence_message (contact, g_value_get_string (value));
594 empathy_contact_set_handle (contact, g_value_get_uint (value));
596 case PROP_CAPABILITIES:
597 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
600 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
603 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
608 static EmpathyContact *
609 empathy_contact_new (TpContact *tp_contact)
611 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
613 return g_object_new (EMPATHY_TYPE_CONTACT,
614 "tp-contact", tp_contact,
619 empathy_contact_from_tpl_contact (TpAccount *account,
620 TplEntity *tpl_entity)
622 EmpathyContact *retval;
625 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
627 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
629 retval = g_object_new (EMPATHY_TYPE_CONTACT,
630 "id", tpl_entity_get_alias (tpl_entity),
631 "alias", tpl_entity_get_identifier (tpl_entity),
636 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
637 contact_load_avatar_cache (retval,
638 tpl_entity_get_avatar_token (tpl_entity));
644 empathy_contact_get_tp_contact (EmpathyContact *contact)
646 EmpathyContactPriv *priv;
648 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
650 priv = GET_PRIV (contact);
652 return priv->tp_contact;
656 empathy_contact_get_id (EmpathyContact *contact)
658 EmpathyContactPriv *priv;
660 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
662 priv = GET_PRIV (contact);
664 if (priv->tp_contact != NULL)
665 return tp_contact_get_identifier (priv->tp_contact);
671 empathy_contact_get_alias (EmpathyContact *contact)
673 EmpathyContactPriv *priv;
676 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
678 priv = GET_PRIV (contact);
680 if (priv->tp_contact != NULL)
681 alias = tp_contact_get_alias (priv->tp_contact);
685 if (!EMP_STR_EMPTY (alias))
688 return empathy_contact_get_id (contact);
692 empathy_contact_set_alias (EmpathyContact *contact,
695 EmpathyContactPriv *priv;
696 FolksPersona *persona;
698 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
700 priv = GET_PRIV (contact);
702 g_object_ref (contact);
704 /* Set the alias on the persona if possible */
705 persona = empathy_contact_get_persona (contact);
706 if (persona != NULL && FOLKS_IS_ALIAS (persona))
708 DEBUG ("Setting alias for contact %s to %s",
709 empathy_contact_get_id (contact), alias);
711 folks_alias_set_alias (FOLKS_ALIAS (persona), alias);
714 if (tp_strdiff (alias, priv->alias))
716 g_free (priv->alias);
717 priv->alias = g_strdup (alias);
718 g_object_notify (G_OBJECT (contact), "alias");
721 g_object_unref (contact);
725 groups_change_group_cb (GObject *source,
726 GAsyncResult *result,
729 FolksGroups *groups = FOLKS_GROUPS (source);
730 GError *error = NULL;
732 folks_groups_change_group_finish (groups, result, &error);
735 g_warning ("failed to change group: %s", error->message);
736 g_clear_error (&error);
741 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
744 EmpathyContactPriv *priv;
745 FolksPersona *persona;
747 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
748 g_return_if_fail (group != NULL);
750 priv = GET_PRIV (contact);
752 /* Normally pass through the changes to the persona */
753 persona = empathy_contact_get_persona (contact);
756 if (FOLKS_IS_GROUPS (persona))
757 folks_groups_change_group (FOLKS_GROUPS (persona), group, is_member,
758 groups_change_group_cb, contact);
762 /* If the persona doesn't exist yet, we have to cache the changes until it
764 if (priv->groups == NULL)
766 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
770 g_hash_table_insert (priv->groups, g_strdup (group),
771 GUINT_TO_POINTER (is_member));
775 empathy_contact_get_avatar (EmpathyContact *contact)
777 EmpathyContactPriv *priv;
779 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
781 priv = GET_PRIV (contact);
787 contact_set_avatar (EmpathyContact *contact,
788 EmpathyAvatar *avatar)
790 EmpathyContactPriv *priv;
792 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
794 priv = GET_PRIV (contact);
796 if (priv->avatar == avatar)
801 empathy_avatar_unref (priv->avatar);
806 priv->avatar = empathy_avatar_ref (avatar);
808 g_object_notify (G_OBJECT (contact), "avatar");
812 empathy_contact_get_account (EmpathyContact *contact)
814 EmpathyContactPriv *priv;
816 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
818 priv = GET_PRIV (contact);
820 if (priv->account == NULL && priv->tp_contact != NULL)
822 TpConnection *connection;
824 /* FIXME: This assume the account manager already exists */
825 connection = tp_contact_get_connection (priv->tp_contact);
827 g_object_ref (empathy_get_account_for_connection (connection));
830 return priv->account;
834 empathy_contact_get_persona (EmpathyContact *contact)
836 EmpathyContactPriv *priv;
838 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
840 priv = GET_PRIV (contact);
842 if (priv->persona == NULL && priv->tp_contact != NULL)
844 /* FIXME: This is disgustingly slow */
845 /* Query for the persona */
846 EmpathyIndividualManager *manager;
847 GList *individuals, *l;
849 manager = empathy_individual_manager_dup_singleton ();
850 individuals = empathy_individual_manager_get_members (manager);
852 for (l = individuals; l != NULL; l = l->next)
855 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
857 personas = folks_individual_get_personas (individual);
858 for (j = personas; j != NULL; j = j->next)
860 TpfPersona *persona = j->data;
862 if (TPF_IS_PERSONA (persona))
864 TpContact *tp_contact = tpf_persona_get_contact (persona);
866 if (tp_contact == priv->tp_contact)
868 /* Found the right persona */
869 empathy_contact_set_persona (contact,
870 (FolksPersona *) persona);
878 g_list_free (individuals);
879 g_object_unref (manager);
882 return priv->persona;
886 empathy_contact_set_persona (EmpathyContact *contact,
887 FolksPersona *persona)
889 EmpathyContactPriv *priv;
891 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
892 g_return_if_fail (TPF_IS_PERSONA (persona));
894 priv = GET_PRIV (contact);
896 if (persona == priv->persona)
899 if (priv->persona != NULL)
901 g_signal_handlers_disconnect_by_func (priv->persona,
902 folks_persona_notify_cb, contact);
903 g_object_unref (priv->persona);
905 priv->persona = g_object_ref (persona);
907 g_signal_connect (priv->persona, "notify",
908 G_CALLBACK (folks_persona_notify_cb), contact);
910 g_object_notify (G_OBJECT (contact), "persona");
912 /* Set the persona's alias, since ours could've been set using
913 * empathy_contact_set_alias() before we had a persona; this happens when
914 * adding a contact. */
915 if (priv->alias != NULL)
916 empathy_contact_set_alias (contact, priv->alias);
918 /* Set the persona's groups */
919 if (priv->groups != NULL)
921 folks_groups_set_groups (FOLKS_GROUPS (persona), priv->groups);
922 g_hash_table_destroy (priv->groups);
928 empathy_contact_get_connection (EmpathyContact *contact)
930 EmpathyContactPriv *priv;
932 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
934 priv = GET_PRIV (contact);
936 if (priv->tp_contact != NULL)
937 return tp_contact_get_connection (priv->tp_contact);
942 TpConnectionPresenceType
943 empathy_contact_get_presence (EmpathyContact *contact)
945 EmpathyContactPriv *priv;
947 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
948 TP_CONNECTION_PRESENCE_TYPE_UNSET);
950 priv = GET_PRIV (contact);
952 if (priv->tp_contact != NULL)
953 return tp_contact_get_presence_type (priv->tp_contact);
955 return priv->presence;
959 empathy_contact_get_presence_message (EmpathyContact *contact)
961 EmpathyContactPriv *priv;
963 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
965 priv = GET_PRIV (contact);
967 if (priv->persona != NULL)
968 return folks_presence_get_presence_message (FOLKS_PRESENCE (priv->persona));
974 empathy_contact_get_handle (EmpathyContact *contact)
976 EmpathyContactPriv *priv;
978 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
980 priv = GET_PRIV (contact);
982 if (priv->tp_contact != NULL)
983 return tp_contact_get_handle (priv->tp_contact);
989 empathy_contact_get_capabilities (EmpathyContact *contact)
991 EmpathyContactPriv *priv;
993 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
995 priv = GET_PRIV (contact);
997 return priv->capabilities;
1001 empathy_contact_is_user (EmpathyContact *contact)
1003 EmpathyContactPriv *priv;
1005 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1007 priv = GET_PRIV (contact);
1009 return priv->is_user;
1013 empathy_contact_set_is_user (EmpathyContact *contact,
1016 EmpathyContactPriv *priv;
1018 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1020 priv = GET_PRIV (contact);
1022 if (priv->is_user == is_user)
1025 priv->is_user = is_user;
1027 g_object_notify (G_OBJECT (contact), "is-user");
1031 empathy_contact_is_online (EmpathyContact *contact)
1033 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1035 switch (empathy_contact_get_presence (contact))
1037 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1038 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1039 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1041 /* Contacts without presence are considered online so we can display IRC
1042 * contacts in rooms. */
1043 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1044 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1045 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1046 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1047 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1048 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1055 empathy_contact_get_status (EmpathyContact *contact)
1057 const gchar *message;
1059 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1061 message = empathy_contact_get_presence_message (contact);
1062 if (!EMP_STR_EMPTY (message))
1065 return empathy_presence_get_default_message (
1066 empathy_contact_get_presence (contact));
1070 empathy_contact_can_voip (EmpathyContact *contact)
1072 EmpathyContactPriv *priv;
1074 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1076 priv = GET_PRIV (contact);
1078 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1079 EMPATHY_CAPABILITIES_VIDEO);
1083 empathy_contact_can_voip_audio (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_AUDIO;
1095 empathy_contact_can_voip_video (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_VIDEO;
1107 empathy_contact_can_send_files (EmpathyContact *contact)
1109 EmpathyContactPriv *priv;
1111 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1113 priv = GET_PRIV (contact);
1115 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1119 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1121 EmpathyContactPriv *priv;
1123 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1125 priv = GET_PRIV (contact);
1127 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1131 contact_has_log (EmpathyContact *contact)
1133 TplLogManager *manager;
1136 manager = tpl_log_manager_dup_singleton ();
1137 have_log = tpl_log_manager_exists (manager,
1138 empathy_contact_get_account (contact), empathy_contact_get_id (contact),
1140 g_object_unref (manager);
1146 empathy_contact_can_do_action (EmpathyContact *self,
1147 EmpathyActionType action_type)
1149 gboolean sensitivity = FALSE;
1151 switch (action_type)
1153 case EMPATHY_ACTION_CHAT:
1156 case EMPATHY_ACTION_AUDIO_CALL:
1157 sensitivity = empathy_contact_can_voip_audio (self);
1159 case EMPATHY_ACTION_VIDEO_CALL:
1160 sensitivity = empathy_contact_can_voip_video (self);
1162 case EMPATHY_ACTION_VIEW_LOGS:
1163 sensitivity = contact_has_log (self);
1165 case EMPATHY_ACTION_SEND_FILE:
1166 sensitivity = empathy_contact_can_send_files (self);
1168 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1169 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1172 g_assert_not_reached ();
1175 return (sensitivity ? TRUE : FALSE);
1179 contact_get_avatar_filename (EmpathyContact *contact,
1185 gchar *token_escaped;
1187 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1190 token_escaped = tp_escape_as_identifier (token);
1191 account = empathy_contact_get_account (contact);
1193 avatar_path = g_build_filename (g_get_user_cache_dir (),
1196 tp_account_get_connection_manager (account),
1197 tp_account_get_protocol (account),
1199 g_mkdir_with_parents (avatar_path, 0700);
1201 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1203 g_free (token_escaped);
1204 g_free (avatar_path);
1210 contact_load_avatar_cache (EmpathyContact *contact,
1213 EmpathyAvatar *avatar = NULL;
1217 GError *error = NULL;
1219 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1220 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1222 /* Load the avatar from file if it exists */
1223 filename = contact_get_avatar_filename (contact, token);
1224 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1226 if (!g_file_get_contents (filename, &data, &len, &error))
1228 DEBUG ("Failed to load avatar from cache: %s",
1229 error ? error->message : "No error given");
1230 g_clear_error (&error);
1236 DEBUG ("Avatar loaded from %s", filename);
1237 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1238 contact_set_avatar (contact, avatar);
1239 empathy_avatar_unref (avatar);
1246 return data != NULL;
1250 empathy_avatar_get_type (void)
1252 static GType type_id = 0;
1256 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1257 (GBoxedCopyFunc) empathy_avatar_ref,
1258 (GBoxedFreeFunc) empathy_avatar_unref);
1265 * empathy_avatar_new:
1266 * @data: the avatar data
1267 * @len: the size of avatar data
1268 * @format: the mime type of the avatar image
1269 * @filename: the filename where the avatar is stored in cache
1271 * Create a #EmpathyAvatar from the provided data. This function takes the
1272 * ownership of @data, @format and @filename.
1274 * Returns: a new #EmpathyAvatar
1277 empathy_avatar_new (guchar *data,
1282 EmpathyAvatar *avatar;
1284 avatar = g_slice_new0 (EmpathyAvatar);
1285 avatar->data = data;
1287 avatar->format = format;
1288 avatar->filename = filename;
1289 avatar->refcount = 1;
1295 empathy_avatar_unref (EmpathyAvatar *avatar)
1297 g_return_if_fail (avatar != NULL);
1300 if (avatar->refcount == 0)
1302 g_free (avatar->data);
1303 g_free (avatar->format);
1304 g_free (avatar->filename);
1305 g_slice_free (EmpathyAvatar, avatar);
1310 empathy_avatar_ref (EmpathyAvatar *avatar)
1312 g_return_val_if_fail (avatar != NULL, NULL);
1320 * empathy_avatar_save_to_file:
1321 * @avatar: the avatar
1322 * @filename: name of a file to write avatar to
1323 * @error: return location for a GError, or NULL
1325 * Save the avatar to a file named filename
1327 * Returns: %TRUE on success, %FALSE if an error occurred
1330 empathy_avatar_save_to_file (EmpathyAvatar *self,
1331 const gchar *filename,
1334 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1339 * empathy_contact_get_location:
1340 * @contact: an #EmpathyContact
1342 * Returns the user's location if available. The keys are defined in
1343 * empathy-location.h. If the contact doesn't have location
1344 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1345 * you are done with the #GHashTable.
1347 * It is composed of string keys and GValues. Keys are
1348 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1349 * Example: a "city" key would have "Helsinki" as string GValue,
1350 * a "latitude" would have 65.0 as double GValue.
1352 * Returns: a #GHashTable of location values
1355 empathy_contact_get_location (EmpathyContact *contact)
1357 EmpathyContactPriv *priv;
1359 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1361 priv = GET_PRIV (contact);
1363 return priv->location;
1367 * empathy_contact_set_location:
1368 * @contact: an #EmpathyContact
1369 * @location: a #GHashTable of the location
1371 * Sets the user's location based on the location #GHashTable passed.
1372 * It is composed of string keys and GValues. Keys are
1373 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1374 * Example: a "city" key would have "Helsinki" as string GValue,
1375 * a "latitude" would have 65.0 as double GValue.
1378 empathy_contact_set_location (EmpathyContact *contact,
1379 GHashTable *location)
1381 EmpathyContactPriv *priv;
1383 g_return_if_fail (EMPATHY_CONTACT (contact));
1384 g_return_if_fail (location != NULL);
1386 priv = GET_PRIV (contact);
1388 if (priv->location != NULL)
1389 g_hash_table_unref (priv->location);
1391 priv->location = g_hash_table_ref (location);
1393 update_geocode (contact);
1395 g_object_notify (G_OBJECT (contact), "location");
1399 * empathy_contact_equal:
1400 * @contact1: an #EmpathyContact
1401 * @contact2: an #EmpathyContact
1403 * Returns FALSE if one of the contacts is NULL but the other is not.
1404 * Otherwise returns TRUE if both pointer are equal or if they bith
1405 * refer to the same id.
1406 * It's only necessary to call this function if your contact objects
1407 * come from logs where contacts are created dynamically and comparing
1408 * pointers is not enough.
1411 empathy_contact_equal (gconstpointer contact1,
1412 gconstpointer contact2)
1419 if ((contact1 == NULL) != (contact2 == NULL)) {
1422 if (contact1 == contact2) {
1425 c1 = EMPATHY_CONTACT (contact1);
1426 c2 = EMPATHY_CONTACT (contact2);
1427 id1 = empathy_contact_get_id (c1);
1428 id2 = empathy_contact_get_id (c2);
1429 if (!tp_strdiff (id1, id2)) {
1436 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
1437 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
1439 /* This callback is called by geoclue when it found a position
1440 * for the given address. A position is necessary for a contact
1441 * to show up on the map
1444 geocode_cb (GeoclueGeocode *geocode,
1445 GeocluePositionFields fields,
1449 GeoclueAccuracy *accuracy,
1453 EmpathyContactPriv *priv = GET_PRIV (contact);
1454 GHashTable *new_location;
1456 if (priv->location == NULL)
1461 DEBUG ("Error geocoding location : %s", error->message);
1465 /* No need to change location if we didn't find the position */
1466 if (!(fields & GEOCLUE_POSITION_FIELDS_LATITUDE))
1469 if (!(fields & GEOCLUE_POSITION_FIELDS_LONGITUDE))
1472 new_location = tp_asv_new (
1473 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1474 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1477 DEBUG ("\t - Latitude: %f", latitude);
1478 DEBUG ("\t - Longitude: %f", longitude);
1480 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1481 * the values we just set. */
1482 tp_g_hash_table_update (new_location, priv->location,
1483 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1485 /* Set the altitude only if it wasn't defined before */
1486 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE &&
1487 g_hash_table_lookup (new_location, EMPATHY_LOCATION_ALT) == NULL)
1489 tp_asv_set_double (new_location, g_strdup (EMPATHY_LOCATION_ALT),
1491 DEBUG ("\t - Altitude: %f", altitude);
1494 /* Don't change the accuracy as we used an address to get this position */
1495 g_hash_table_unref (priv->location);
1496 priv->location = new_location;
1497 g_object_notify (contact, "location");
1499 g_object_unref (geocode);
1500 g_object_unref (contact);
1504 get_dup_string (GHashTable *location,
1509 value = g_hash_table_lookup (location, key);
1511 return g_value_dup_string (value);
1517 update_geocode (EmpathyContact *contact)
1519 static GeoclueGeocode *geocode;
1521 GHashTable *address;
1522 GHashTable *location;
1524 location = empathy_contact_get_location (contact);
1525 if (location == NULL)
1528 /* No need to search for position if contact published it */
1529 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1530 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1533 if (geocode == NULL)
1535 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
1536 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
1540 g_object_ref (geocode);
1543 address = geoclue_address_details_new ();
1545 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
1548 g_hash_table_insert (address,
1549 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
1550 DEBUG ("\t - countrycode: %s", str);
1553 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
1556 g_hash_table_insert (address,
1557 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
1558 DEBUG ("\t - country: %s", str);
1561 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
1564 g_hash_table_insert (address,
1565 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
1566 DEBUG ("\t - postalcode: %s", str);
1569 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
1572 g_hash_table_insert (address,
1573 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
1574 DEBUG ("\t - region: %s", str);
1577 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
1580 g_hash_table_insert (address,
1581 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
1582 DEBUG ("\t - locality: %s", str);
1585 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
1588 g_hash_table_insert (address,
1589 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
1590 DEBUG ("\t - street: %s", str);
1593 if (g_hash_table_size (address) > 0)
1595 g_object_ref (contact);
1597 geoclue_geocode_address_to_position_async (geocode, address,
1598 geocode_cb, contact);
1601 g_hash_table_unref (address);
1605 static EmpathyCapabilities
1606 tp_caps_to_capabilities (TpCapabilities *caps)
1608 EmpathyCapabilities capabilities = 0;
1612 classes = tp_capabilities_get_channel_classes (caps);
1614 for (i = 0; i < classes->len; i++)
1616 GValueArray *class_struct;
1617 GHashTable *fixed_prop;
1619 TpHandleType handle_type;
1620 const gchar *chan_type;
1622 class_struct = g_ptr_array_index (classes, i);
1623 tp_value_array_unpack (class_struct, 2,
1627 handle_type = tp_asv_get_uint32 (fixed_prop,
1628 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1629 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1632 chan_type = tp_asv_get_string (fixed_prop,
1633 TP_PROP_CHANNEL_CHANNEL_TYPE);
1635 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1637 capabilities |= EMPATHY_CAPABILITIES_FT;
1639 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1641 const gchar *service;
1643 service = tp_asv_get_string (fixed_prop,
1644 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1646 if (!tp_strdiff (service, "rfb"))
1647 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1649 else if (!tp_strdiff (chan_type,
1650 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1654 for (j = 0; allowed_prop[j] != NULL; j++)
1656 if (!tp_strdiff (allowed_prop[j],
1657 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1658 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1659 else if (!tp_strdiff (allowed_prop[j],
1660 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1661 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1666 return capabilities;
1670 set_capabilities_from_tp_caps (EmpathyContact *self,
1671 TpCapabilities *caps)
1673 EmpathyCapabilities capabilities;
1678 capabilities = tp_caps_to_capabilities (caps);
1679 empathy_contact_set_capabilities (self, capabilities);
1683 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1685 EmpathyContactPriv *priv = GET_PRIV (contact);
1689 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1690 file = tp_contact_get_avatar_file (priv->tp_contact);
1694 EmpathyAvatar *avatar;
1698 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1699 avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime),
1700 g_file_get_path (file));
1701 contact_set_avatar (contact, avatar);
1702 empathy_avatar_unref (avatar);
1706 contact_set_avatar (contact, NULL);
1711 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1713 EmpathyContact *contact = NULL;
1715 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1717 if (contacts_table == NULL)
1718 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1720 contact = g_hash_table_lookup (contacts_table, tp_contact);
1722 if (contact == NULL)
1724 contact = empathy_contact_new (tp_contact);
1726 /* The hash table does not keep any ref.
1727 * contact keeps a ref to tp_contact, and is removed from the table in
1728 * contact_dispose() */
1729 g_hash_table_insert (contacts_table, tp_contact, contact);
1733 g_object_ref (contact);
1740 presence_sort_func (EmpathyContact *a, EmpathyContact *b)
1742 FolksPresence *presence_a, *presence_b;
1744 presence_a = FOLKS_PRESENCE (empathy_contact_get_persona (a));
1745 presence_b = FOLKS_PRESENCE (empathy_contact_get_persona (b));
1747 /* We negate the result because we're sorting in reverse order (i.e. such that
1748 * the Personas with the highest presence are at the beginning of the list. */
1749 return -folks_presence_typecmp (folks_presence_get_presence_type (presence_a),
1750 folks_presence_get_presence_type (presence_b));
1753 /* Sort by presence as with presence_sort_func(), but if the two contacts have
1754 * the same presence, prefer the one which can do both audio *and* video calls,
1755 * over the one which can only do one of the two. */
1757 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1759 gboolean has_audio_video_a, has_audio_video_b;
1760 gint presence_sort = presence_sort_func (a, b);
1762 if (presence_sort != 0)
1763 return presence_sort;
1765 has_audio_video_a = empathy_contact_can_voip_audio (a) &&
1766 empathy_contact_can_voip_video (a);
1767 has_audio_video_b = empathy_contact_can_voip_audio (b) &&
1768 empathy_contact_can_voip_video (b);
1770 if (has_audio_video_a && !has_audio_video_b)
1772 else if (!has_audio_video_a && has_audio_video_b)
1779 get_sort_func_for_action (EmpathyActionType action_type)
1781 switch (action_type)
1783 case EMPATHY_ACTION_AUDIO_CALL:
1784 case EMPATHY_ACTION_VIDEO_CALL:
1785 return (GCompareFunc) voip_sort_func;
1786 case EMPATHY_ACTION_CHAT:
1787 case EMPATHY_ACTION_VIEW_LOGS:
1788 case EMPATHY_ACTION_SEND_FILE:
1789 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1791 return (GCompareFunc) presence_sort_func;
1796 * empathy_contact_dup_best_for_action:
1797 * @individual: a #FolksIndividual
1798 * @action_type: the type of action to be performed on the contact
1800 * Chooses a #FolksPersona from the given @individual which is best-suited for
1801 * the given @action_type. "Best-suited" is determined by choosing the persona
1802 * with the highest presence out of all the personas which can perform the given
1803 * @action_type (e.g. are capable of video calling).
1805 * Return value: an #EmpathyContact for the best persona, or %NULL;
1806 * unref with g_object_unref()
1809 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1810 EmpathyActionType action_type)
1812 GList *personas, *contacts, *l;
1813 EmpathyContact *best_contact = NULL;
1815 /* Build a list of EmpathyContacts that we can sort */
1816 personas = folks_individual_get_personas (individual);
1819 for (l = personas; l != NULL; l = l->next)
1821 TpContact *tp_contact;
1822 EmpathyContact *contact;
1824 if (!TPF_IS_PERSONA (l->data))
1827 tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
1828 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1829 empathy_contact_set_persona (contact, FOLKS_PERSONA (l->data));
1831 /* Only choose the contact if they're actually capable of the specified
1833 if (!empathy_contact_can_do_action (contact, action_type))
1835 g_object_unref (contact);
1839 contacts = g_list_prepend (contacts, contact);
1842 /* Sort the contacts by some heuristic based on the action type, then take
1843 * the top contact. */
1844 if (contacts != NULL)
1846 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
1847 best_contact = g_object_ref (contacts->data);
1850 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
1851 g_list_free (contacts);
1853 return best_contact;