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>
31 #include <telepathy-yell/telepathy-yell.h>
33 #include <telepathy-logger/log-manager.h>
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
39 #include <geocode-glib/geocode-glib.h>
42 #include "empathy-contact.h"
43 #include "empathy-camera-monitor.h"
44 #include "empathy-individual-manager.h"
45 #include "empathy-utils.h"
46 #include "empathy-enum-types.h"
47 #include "empathy-location.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
50 #include "empathy-debug.h"
52 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
54 TpContact *tp_contact;
56 FolksPersona *persona;
60 EmpathyAvatar *avatar;
61 TpConnectionPresenceType presence;
63 EmpathyCapabilities capabilities;
66 /* Location is composed of string keys and GValues.
67 * Example: a "city" key would have "Helsinki" as string GValue,
68 * a "latitude" would have 65.0 as double GValue.
70 * This is a super set of the location stored in TpContact as we can try add
71 * more fields by searching the address using geoclue.
78 static void contact_finalize (GObject *object);
79 static void contact_get_property (GObject *object, guint param_id,
80 GValue *value, GParamSpec *pspec);
81 static void contact_set_property (GObject *object, guint param_id,
82 const GValue *value, GParamSpec *pspec);
85 static void update_geocode (EmpathyContact *contact);
88 static void empathy_contact_set_location (EmpathyContact *contact,
89 GHashTable *location);
91 static void contact_set_client_types (EmpathyContact *contact,
92 const gchar * const *types);
94 static void set_capabilities_from_tp_caps (EmpathyContact *self,
95 TpCapabilities *caps);
97 static void contact_set_avatar (EmpathyContact *contact,
98 EmpathyAvatar *avatar);
99 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
100 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
103 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
116 PROP_PRESENCE_MESSAGE,
129 static guint signals[LAST_SIGNAL];
131 /* TpContact* -> EmpathyContact*, both borrowed ref */
132 static GHashTable *contacts_table = NULL;
135 tp_contact_notify_cb (TpContact *tp_contact,
139 EmpathyContactPriv *priv = GET_PRIV (contact);
141 /* Forward property notifications */
142 if (!tp_strdiff (param->name, "alias"))
143 g_object_notify (contact, "alias");
144 else if (!tp_strdiff (param->name, "presence-type")) {
145 TpConnectionPresenceType presence;
147 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
148 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
150 priv->presence = presence;
151 g_object_notify (contact, "presence");
153 else if (!tp_strdiff (param->name, "identifier"))
154 g_object_notify (contact, "id");
155 else if (!tp_strdiff (param->name, "handle"))
156 g_object_notify (contact, "handle");
157 else if (!tp_strdiff (param->name, "location"))
159 GHashTable *location;
161 location = tp_contact_get_location (tp_contact);
162 /* This will start a geoclue search to find the address if needed */
163 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
165 else if (!tp_strdiff (param->name, "capabilities"))
167 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
168 tp_contact_get_capabilities (tp_contact));
170 else if (!tp_strdiff (param->name, "avatar-file"))
172 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
174 else if (!tp_strdiff (param->name, "client-types"))
176 contact_set_client_types (EMPATHY_CONTACT (contact),
177 tp_contact_get_client_types (tp_contact));
182 folks_persona_notify_cb (FolksPersona *folks_persona,
186 if (!tp_strdiff (param->name, "presence-message"))
187 g_object_notify (contact, "presence-message");
191 contact_dispose (GObject *object)
193 EmpathyContactPriv *priv = GET_PRIV (object);
195 if (priv->tp_contact != NULL)
197 g_signal_handlers_disconnect_by_func (priv->tp_contact,
198 tp_contact_notify_cb, object);
200 tp_clear_object (&priv->tp_contact);
203 g_object_unref (priv->account);
204 priv->account = NULL;
208 g_signal_handlers_disconnect_by_func (priv->persona,
209 folks_persona_notify_cb, object);
210 g_object_unref (priv->persona);
212 priv->persona = NULL;
214 if (priv->avatar != NULL)
216 empathy_avatar_unref (priv->avatar);
220 if (priv->location != NULL)
222 g_hash_table_unref (priv->location);
223 priv->location = NULL;
226 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
230 contact_constructed (GObject *object)
232 EmpathyContact *contact = (EmpathyContact *) object;
233 EmpathyContactPriv *priv = GET_PRIV (contact);
234 GHashTable *location;
235 TpHandle self_handle;
237 const gchar * const *client_types;
239 if (priv->tp_contact == NULL)
242 priv->presence = empathy_contact_get_presence (contact);
244 location = tp_contact_get_location (priv->tp_contact);
245 if (location != NULL)
246 empathy_contact_set_location (contact, location);
248 client_types = tp_contact_get_client_types (priv->tp_contact);
249 if (client_types != NULL)
250 contact_set_client_types (contact, client_types);
252 set_capabilities_from_tp_caps (contact,
253 tp_contact_get_capabilities (priv->tp_contact));
255 contact_set_avatar_from_tp_contact (contact);
257 /* Set is-user property. Note that it could still be the handle is
258 * different from the connection's self handle, in the case the handle
259 * comes from a group interface. */
260 self_handle = tp_connection_get_self_handle (
261 tp_contact_get_connection (priv->tp_contact));
262 handle = tp_contact_get_handle (priv->tp_contact);
263 empathy_contact_set_is_user (contact, self_handle == handle);
265 g_signal_connect (priv->tp_contact, "notify",
266 G_CALLBACK (tp_contact_notify_cb), contact);
270 empathy_contact_class_init (EmpathyContactClass *class)
272 GObjectClass *object_class;
274 object_class = G_OBJECT_CLASS (class);
276 object_class->finalize = contact_finalize;
277 object_class->dispose = contact_dispose;
278 object_class->get_property = contact_get_property;
279 object_class->set_property = contact_set_property;
280 object_class->constructed = contact_constructed;
282 g_object_class_install_property (object_class,
284 g_param_spec_object ("tp-contact",
286 "The TpContact associated with the contact",
288 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (object_class,
292 g_param_spec_object ("account",
294 "The account associated with the contact",
296 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (object_class,
300 g_param_spec_object ("persona",
302 "The FolksPersona associated with the contact",
304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (object_class,
308 g_param_spec_string ("id",
310 "String identifying contact",
312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (object_class,
316 g_param_spec_string ("alias",
318 "An alias for the contact",
320 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (object_class,
324 g_param_spec_string ("logged-alias",
326 "The alias the user had when a message was logged, "
327 "only set when using empathy_contact_from_tpl_contact()",
329 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (object_class,
333 g_param_spec_boxed ("avatar",
337 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (object_class,
341 g_param_spec_uint ("presence",
343 "Presence of contact",
344 TP_CONNECTION_PRESENCE_TYPE_UNSET,
345 NUM_TP_CONNECTION_PRESENCE_TYPES,
346 TP_CONNECTION_PRESENCE_TYPE_UNSET,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (object_class,
350 PROP_PRESENCE_MESSAGE,
351 g_param_spec_string ("presence-message",
352 "Contact presence message",
353 "Presence message of contact",
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (object_class,
359 g_param_spec_uint ("handle",
361 "The handle of the contact",
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (object_class,
369 g_param_spec_flags ("capabilities",
370 "Contact Capabilities",
371 "Capabilities of the contact",
372 EMPATHY_TYPE_CAPABILITIES,
373 EMPATHY_CAPABILITIES_UNKNOWN,
374 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (object_class,
378 g_param_spec_boolean ("is-user",
380 "Is contact the user",
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 g_object_class_install_property (object_class,
387 g_param_spec_boxed ("location",
389 "Physical location of the contact",
391 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
393 g_object_class_install_property (object_class,
395 g_param_spec_boxed ("client-types",
396 "Contact client types",
397 "Client types of the contact",
399 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
401 signals[PRESENCE_CHANGED] =
402 g_signal_new ("presence-changed",
403 G_TYPE_FROM_CLASS (class),
407 g_cclosure_marshal_generic,
412 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
416 empathy_contact_init (EmpathyContact *contact)
418 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
419 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
421 contact->priv = priv;
423 priv->location = NULL;
424 priv->client_types = NULL;
429 contact_finalize (GObject *object)
431 EmpathyContactPriv *priv;
433 priv = GET_PRIV (object);
435 DEBUG ("finalize: %p", object);
437 g_clear_object (&priv->groups);
438 g_free (priv->alias);
440 g_strfreev (priv->client_types);
442 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
446 empathy_contact_set_capabilities (EmpathyContact *contact,
447 EmpathyCapabilities capabilities)
449 EmpathyContactPriv *priv;
451 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
453 priv = GET_PRIV (contact);
455 if (priv->capabilities == capabilities)
458 priv->capabilities = capabilities;
460 g_object_notify (G_OBJECT (contact), "capabilities");
464 empathy_contact_set_id (EmpathyContact *contact,
467 EmpathyContactPriv *priv;
469 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
470 g_return_if_fail (id != NULL);
472 priv = GET_PRIV (contact);
474 /* We temporally ref the contact because it could be destroyed
475 * during the signal emition */
476 g_object_ref (contact);
477 if (tp_strdiff (id, priv->id))
480 priv->id = g_strdup (id);
482 g_object_notify (G_OBJECT (contact), "id");
483 if (EMP_STR_EMPTY (priv->alias))
484 g_object_notify (G_OBJECT (contact), "alias");
487 g_object_unref (contact);
491 empathy_contact_set_presence (EmpathyContact *contact,
492 TpConnectionPresenceType presence)
494 EmpathyContactPriv *priv;
495 TpConnectionPresenceType old_presence;
497 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
499 priv = GET_PRIV (contact);
501 if (presence == priv->presence)
504 old_presence = priv->presence;
505 priv->presence = presence;
507 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
509 g_object_notify (G_OBJECT (contact), "presence");
513 empathy_contact_set_presence_message (EmpathyContact *contact,
514 const gchar *message)
516 EmpathyContactPriv *priv = GET_PRIV (contact);
518 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
520 if (priv->persona != NULL)
522 folks_presence_details_set_presence_message (
523 FOLKS_PRESENCE_DETAILS (priv->persona), message);
528 empathy_contact_set_handle (EmpathyContact *contact,
531 EmpathyContactPriv *priv;
533 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
535 priv = GET_PRIV (contact);
537 g_object_ref (contact);
538 if (handle != priv->handle)
540 priv->handle = handle;
541 g_object_notify (G_OBJECT (contact), "handle");
543 g_object_unref (contact);
547 contact_get_property (GObject *object,
552 EmpathyContact *contact = EMPATHY_CONTACT (object);
556 case PROP_TP_CONTACT:
557 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
560 g_value_set_object (value, empathy_contact_get_account (contact));
563 g_value_set_object (value, empathy_contact_get_persona (contact));
566 g_value_set_string (value, empathy_contact_get_id (contact));
569 g_value_set_string (value, empathy_contact_get_alias (contact));
571 case PROP_LOGGED_ALIAS:
572 g_value_set_string (value, empathy_contact_get_logged_alias (contact));
575 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
578 g_value_set_uint (value, empathy_contact_get_presence (contact));
580 case PROP_PRESENCE_MESSAGE:
581 g_value_set_string (value, empathy_contact_get_presence_message (contact));
584 g_value_set_uint (value, empathy_contact_get_handle (contact));
586 case PROP_CAPABILITIES:
587 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
590 g_value_set_boolean (value, empathy_contact_is_user (contact));
593 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
599 contact_set_property (GObject *object,
604 EmpathyContact *contact = EMPATHY_CONTACT (object);
605 EmpathyContactPriv *priv = GET_PRIV (object);
609 case PROP_TP_CONTACT:
610 priv->tp_contact = g_value_dup_object (value);
613 g_assert (priv->account == NULL);
614 priv->account = g_value_dup_object (value);
617 empathy_contact_set_persona (contact, g_value_get_object (value));
620 empathy_contact_set_id (contact, g_value_get_string (value));
623 empathy_contact_set_alias (contact, g_value_get_string (value));
625 case PROP_LOGGED_ALIAS:
626 g_assert (priv->logged_alias == NULL);
627 priv->logged_alias = g_value_dup_string (value);
630 empathy_contact_set_presence (contact, g_value_get_uint (value));
632 case PROP_PRESENCE_MESSAGE:
633 empathy_contact_set_presence_message (contact, g_value_get_string (value));
636 empathy_contact_set_handle (contact, g_value_get_uint (value));
638 case PROP_CAPABILITIES:
639 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
642 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
651 remove_tp_contact (gpointer data,
654 g_hash_table_remove (contacts_table, data);
657 static EmpathyContact *
658 empathy_contact_new (TpContact *tp_contact)
660 EmpathyContact *retval;
662 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
664 retval = g_object_new (EMPATHY_TYPE_CONTACT,
665 "tp-contact", tp_contact,
668 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
680 contact_is_tpl_entity (gpointer key,
684 EmpathyContact *contact = value;
685 FindContactData *data = user_data;
686 TpAccount *account = empathy_contact_get_account (contact);
687 const gchar *path = NULL;
690 path = tp_proxy_get_object_path (account);
692 return !tp_strdiff (empathy_contact_get_id (contact),
693 tpl_entity_get_identifier (data->entity)) &&
694 !tp_strdiff (tp_proxy_get_object_path (data->account), path);
698 get_contacts_cb (TpConnection *connection,
700 TpContact * const *contacts,
701 const gchar * const *requested_ids,
702 GHashTable *failed_id_errors,
705 GObject *weak_object)
707 EmpathyContact *self = (EmpathyContact *) weak_object;
708 EmpathyContactPriv *priv = GET_PRIV (self);
709 TpContact *tp_contact;
714 tp_contact = contacts[0];
716 g_return_if_fail (priv->tp_contact == NULL);
717 priv->tp_contact = g_object_ref (tp_contact);
718 g_object_notify (G_OBJECT (self), "tp-contact");
720 /* Update capabilities now that we have a TpContact */
721 set_capabilities_from_tp_caps (self,
722 tp_contact_get_capabilities (tp_contact));
726 empathy_contact_from_tpl_contact (TpAccount *account,
727 TplEntity *tpl_entity)
729 EmpathyContact *retval;
731 EmpathyContact *existing_contact = NULL;
733 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
735 if (contacts_table != NULL)
737 FindContactData data;
739 data.entity = tpl_entity;
740 data.account = account;
742 existing_contact = g_hash_table_find (contacts_table,
743 contact_is_tpl_entity, &data);
746 if (existing_contact != NULL)
748 retval = g_object_new (EMPATHY_TYPE_CONTACT,
749 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
750 "logged-alias", tpl_entity_get_alias (tpl_entity),
758 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
760 id = tpl_entity_get_identifier (tpl_entity);
762 retval = g_object_new (EMPATHY_TYPE_CONTACT,
764 "alias", tpl_entity_get_alias (tpl_entity),
769 /* Try to get a TpContact associated to have at least contact
770 * capabilities if possible. This is useful for CM supporting calling
771 * offline contacts for example. */
772 conn = tp_account_get_connection (account);
775 TpContactFeature features[] = { TP_CONTACT_FEATURE_CAPABILITIES };
776 conn = tp_account_get_connection (account);
778 tp_connection_get_contacts_by_id (conn, 1, &id,
779 G_N_ELEMENTS (features), features, get_contacts_cb,
780 NULL, NULL, G_OBJECT (retval));
784 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
785 contact_load_avatar_cache (retval,
786 tpl_entity_get_avatar_token (tpl_entity));
792 empathy_contact_get_tp_contact (EmpathyContact *contact)
794 EmpathyContactPriv *priv;
796 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
798 priv = GET_PRIV (contact);
800 return priv->tp_contact;
804 empathy_contact_get_id (EmpathyContact *contact)
806 EmpathyContactPriv *priv;
808 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
810 priv = GET_PRIV (contact);
812 if (priv->tp_contact != NULL)
813 return tp_contact_get_identifier (priv->tp_contact);
819 empathy_contact_get_alias (EmpathyContact *contact)
821 EmpathyContactPriv *priv;
822 const gchar *alias = NULL;
824 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
826 priv = GET_PRIV (contact);
828 if (!EMP_STR_EMPTY (priv->alias))
830 else if (priv->tp_contact != NULL)
831 alias = tp_contact_get_alias (priv->tp_contact);
833 if (!EMP_STR_EMPTY (alias))
836 return empathy_contact_get_id (contact);
840 empathy_contact_get_logged_alias (EmpathyContact *contact)
842 EmpathyContactPriv *priv;
844 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
846 priv = GET_PRIV (contact);
848 if (priv->logged_alias != NULL)
849 return priv->logged_alias;
851 return empathy_contact_get_alias (contact);
855 empathy_contact_set_alias (EmpathyContact *contact,
858 EmpathyContactPriv *priv;
859 FolksPersona *persona;
861 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
863 priv = GET_PRIV (contact);
865 g_object_ref (contact);
867 /* Set the alias on the persona if possible */
868 persona = empathy_contact_get_persona (contact);
869 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
871 DEBUG ("Setting alias for contact %s to %s",
872 empathy_contact_get_id (contact), alias);
874 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
877 if (tp_strdiff (alias, priv->alias))
879 g_free (priv->alias);
880 priv->alias = g_strdup (alias);
881 g_object_notify (G_OBJECT (contact), "alias");
884 g_object_unref (contact);
888 groups_change_group_cb (GObject *source,
889 GAsyncResult *result,
892 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
893 GError *error = NULL;
895 folks_group_details_change_group_finish (group_details, result, &error);
898 g_warning ("failed to change group: %s", error->message);
899 g_clear_error (&error);
904 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
907 EmpathyContactPriv *priv;
908 FolksPersona *persona;
910 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
911 g_return_if_fail (group != NULL);
913 priv = GET_PRIV (contact);
915 /* Normally pass through the changes to the persona */
916 persona = empathy_contact_get_persona (contact);
919 if (FOLKS_IS_GROUP_DETAILS (persona))
920 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
921 is_member, groups_change_group_cb, contact);
925 /* If the persona doesn't exist yet, we have to cache the changes until it
927 if (priv->groups == NULL)
929 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
930 g_free, g_str_hash, g_str_equal);
933 gee_collection_add (GEE_COLLECTION (priv->groups), group);
937 empathy_contact_get_avatar (EmpathyContact *contact)
939 EmpathyContactPriv *priv;
941 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
943 priv = GET_PRIV (contact);
949 contact_set_avatar (EmpathyContact *contact,
950 EmpathyAvatar *avatar)
952 EmpathyContactPriv *priv;
954 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
956 priv = GET_PRIV (contact);
958 if (priv->avatar == avatar)
963 empathy_avatar_unref (priv->avatar);
968 priv->avatar = empathy_avatar_ref (avatar);
970 g_object_notify (G_OBJECT (contact), "avatar");
974 empathy_contact_get_account (EmpathyContact *contact)
976 EmpathyContactPriv *priv;
978 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
980 priv = GET_PRIV (contact);
982 if (priv->account == NULL && priv->tp_contact != NULL)
984 TpConnection *connection;
986 /* FIXME: This assume the account manager already exists */
987 connection = tp_contact_get_connection (priv->tp_contact);
989 g_object_ref (tp_connection_get_account (connection));
992 return priv->account;
996 empathy_contact_get_persona (EmpathyContact *contact)
998 EmpathyContactPriv *priv;
1000 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1002 priv = GET_PRIV (contact);
1004 if (priv->persona == NULL && priv->tp_contact != NULL)
1006 TpfPersona *persona;
1008 persona = tpf_persona_dup_for_contact (priv->tp_contact);
1009 if (persona != NULL)
1011 empathy_contact_set_persona (contact, (FolksPersona *) persona);
1012 g_object_unref (persona);
1016 return priv->persona;
1020 empathy_contact_set_persona (EmpathyContact *contact,
1021 FolksPersona *persona)
1023 EmpathyContactPriv *priv;
1025 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1026 g_return_if_fail (TPF_IS_PERSONA (persona));
1028 priv = GET_PRIV (contact);
1030 if (persona == priv->persona)
1033 if (priv->persona != NULL)
1035 g_signal_handlers_disconnect_by_func (priv->persona,
1036 folks_persona_notify_cb, contact);
1037 g_object_unref (priv->persona);
1039 priv->persona = g_object_ref (persona);
1041 g_signal_connect (priv->persona, "notify",
1042 G_CALLBACK (folks_persona_notify_cb), contact);
1044 g_object_notify (G_OBJECT (contact), "persona");
1046 /* Set the persona's alias, since ours could've been set using
1047 * empathy_contact_set_alias() before we had a persona; this happens when
1048 * adding a contact. */
1049 if (priv->alias != NULL)
1050 empathy_contact_set_alias (contact, priv->alias);
1052 /* Set the persona's groups */
1053 if (priv->groups != NULL)
1055 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1056 GEE_SET (priv->groups));
1057 g_object_unref (priv->groups);
1058 priv->groups = NULL;
1063 empathy_contact_get_connection (EmpathyContact *contact)
1065 EmpathyContactPriv *priv;
1067 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1069 priv = GET_PRIV (contact);
1071 if (priv->tp_contact != NULL)
1072 return tp_contact_get_connection (priv->tp_contact);
1077 TpConnectionPresenceType
1078 empathy_contact_get_presence (EmpathyContact *contact)
1080 EmpathyContactPriv *priv;
1082 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1083 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1085 priv = GET_PRIV (contact);
1087 if (priv->tp_contact != NULL)
1088 return tp_contact_get_presence_type (priv->tp_contact);
1090 return priv->presence;
1094 empathy_contact_get_presence_message (EmpathyContact *contact)
1096 EmpathyContactPriv *priv;
1098 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1100 priv = GET_PRIV (contact);
1102 if (priv->persona != NULL)
1103 return folks_presence_details_get_presence_message (
1104 FOLKS_PRESENCE_DETAILS (priv->persona));
1106 if (priv->tp_contact != NULL)
1107 return tp_contact_get_presence_message (priv->tp_contact);
1113 empathy_contact_get_handle (EmpathyContact *contact)
1115 EmpathyContactPriv *priv;
1117 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1119 priv = GET_PRIV (contact);
1121 if (priv->tp_contact != NULL)
1122 return tp_contact_get_handle (priv->tp_contact);
1124 return priv->handle;
1128 empathy_contact_get_capabilities (EmpathyContact *contact)
1130 EmpathyContactPriv *priv;
1132 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1134 priv = GET_PRIV (contact);
1136 return priv->capabilities;
1140 empathy_contact_is_user (EmpathyContact *contact)
1142 EmpathyContactPriv *priv;
1144 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1146 priv = GET_PRIV (contact);
1148 return priv->is_user;
1152 empathy_contact_set_is_user (EmpathyContact *contact,
1155 EmpathyContactPriv *priv;
1157 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1159 priv = GET_PRIV (contact);
1161 if (priv->is_user == is_user)
1164 priv->is_user = is_user;
1166 g_object_notify (G_OBJECT (contact), "is-user");
1170 empathy_contact_is_online (EmpathyContact *contact)
1172 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1174 switch (empathy_contact_get_presence (contact))
1176 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1177 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1178 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1180 /* Contacts without presence are considered online so we can display IRC
1181 * contacts in rooms. */
1182 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1183 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1184 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1185 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1186 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1187 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1194 empathy_contact_get_status (EmpathyContact *contact)
1196 const gchar *message;
1198 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1200 message = empathy_contact_get_presence_message (contact);
1201 if (!EMP_STR_EMPTY (message))
1204 return empathy_presence_get_default_message (
1205 empathy_contact_get_presence (contact));
1209 empathy_contact_can_sms (EmpathyContact *contact)
1211 EmpathyContactPriv *priv;
1213 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1215 priv = GET_PRIV (contact);
1217 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1221 empathy_contact_can_voip (EmpathyContact *contact)
1223 EmpathyContactPriv *priv;
1225 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1227 priv = GET_PRIV (contact);
1229 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1230 EMPATHY_CAPABILITIES_VIDEO);
1234 empathy_contact_can_voip_audio (EmpathyContact *contact)
1236 EmpathyContactPriv *priv;
1238 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1240 priv = GET_PRIV (contact);
1242 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1246 empathy_contact_can_voip_video (EmpathyContact *contact)
1248 EmpathyContactPriv *priv;
1250 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1252 priv = GET_PRIV (contact);
1254 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1258 empathy_contact_can_send_files (EmpathyContact *contact)
1260 EmpathyContactPriv *priv;
1262 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1264 priv = GET_PRIV (contact);
1266 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1270 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1272 EmpathyContactPriv *priv;
1274 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1276 priv = GET_PRIV (contact);
1278 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1282 contact_has_log (EmpathyContact *contact)
1284 TplLogManager *manager;
1288 manager = tpl_log_manager_dup_singleton ();
1289 entity = tpl_entity_new (empathy_contact_get_id (contact),
1290 TPL_ENTITY_CONTACT, NULL, NULL);
1292 have_log = tpl_log_manager_exists (manager,
1293 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1295 g_object_unref (entity);
1296 g_object_unref (manager);
1302 empathy_contact_can_do_action (EmpathyContact *self,
1303 EmpathyActionType action_type)
1305 gboolean sensitivity = FALSE;
1307 switch (action_type)
1309 case EMPATHY_ACTION_CHAT:
1312 case EMPATHY_ACTION_SMS:
1313 sensitivity = empathy_contact_can_sms (self);
1315 case EMPATHY_ACTION_AUDIO_CALL:
1316 sensitivity = empathy_contact_can_voip_audio (self);
1318 case EMPATHY_ACTION_VIDEO_CALL:
1319 sensitivity = empathy_contact_can_voip_video (self);
1321 case EMPATHY_ACTION_VIEW_LOGS:
1322 sensitivity = contact_has_log (self);
1324 case EMPATHY_ACTION_SEND_FILE:
1325 sensitivity = empathy_contact_can_send_files (self);
1327 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1328 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1331 g_assert_not_reached ();
1334 return (sensitivity ? TRUE : FALSE);
1338 contact_get_avatar_filename (EmpathyContact *contact,
1344 gchar *token_escaped;
1346 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1349 token_escaped = tp_escape_as_identifier (token);
1350 account = empathy_contact_get_account (contact);
1352 avatar_path = g_build_filename (g_get_user_cache_dir (),
1355 tp_account_get_connection_manager (account),
1356 tp_account_get_protocol (account),
1358 g_mkdir_with_parents (avatar_path, 0700);
1360 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1362 g_free (token_escaped);
1363 g_free (avatar_path);
1369 contact_load_avatar_cache (EmpathyContact *contact,
1372 EmpathyAvatar *avatar = NULL;
1376 GError *error = NULL;
1378 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1379 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1381 /* Load the avatar from file if it exists */
1382 filename = contact_get_avatar_filename (contact, token);
1383 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1385 if (!g_file_get_contents (filename, &data, &len, &error))
1387 DEBUG ("Failed to load avatar from cache: %s",
1388 error ? error->message : "No error given");
1389 g_clear_error (&error);
1395 DEBUG ("Avatar loaded from %s", filename);
1396 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1397 contact_set_avatar (contact, avatar);
1398 empathy_avatar_unref (avatar);
1404 return data != NULL;
1408 empathy_avatar_get_type (void)
1410 static GType type_id = 0;
1414 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1415 (GBoxedCopyFunc) empathy_avatar_ref,
1416 (GBoxedFreeFunc) empathy_avatar_unref);
1423 * empathy_avatar_new:
1424 * @data: the avatar data
1425 * @len: the size of avatar data
1426 * @format: the mime type of the avatar image
1427 * @filename: the filename where the avatar is stored in cache
1429 * Create a #EmpathyAvatar from the provided data.
1431 * Returns: a new #EmpathyAvatar
1434 empathy_avatar_new (const guchar *data,
1436 const gchar *format,
1437 const gchar *filename)
1439 EmpathyAvatar *avatar;
1441 avatar = g_slice_new0 (EmpathyAvatar);
1442 avatar->data = g_memdup (data, len);
1444 avatar->format = g_strdup (format);
1445 avatar->filename = g_strdup (filename);
1446 avatar->refcount = 1;
1452 empathy_avatar_unref (EmpathyAvatar *avatar)
1454 g_return_if_fail (avatar != NULL);
1457 if (avatar->refcount == 0)
1459 g_free (avatar->data);
1460 g_free (avatar->format);
1461 g_free (avatar->filename);
1462 g_slice_free (EmpathyAvatar, avatar);
1467 empathy_avatar_ref (EmpathyAvatar *avatar)
1469 g_return_val_if_fail (avatar != NULL, NULL);
1477 * empathy_avatar_save_to_file:
1478 * @avatar: the avatar
1479 * @filename: name of a file to write avatar to
1480 * @error: return location for a GError, or NULL
1482 * Save the avatar to a file named filename
1484 * Returns: %TRUE on success, %FALSE if an error occurred
1487 empathy_avatar_save_to_file (EmpathyAvatar *self,
1488 const gchar *filename,
1491 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1496 * empathy_contact_get_location:
1497 * @contact: an #EmpathyContact
1499 * Returns the user's location if available. The keys are defined in
1500 * empathy-location.h. If the contact doesn't have location
1501 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1502 * you are done with the #GHashTable.
1504 * It is composed of string keys and GValues. Keys are
1505 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1506 * Example: a "city" key would have "Helsinki" as string GValue,
1507 * a "latitude" would have 65.0 as double GValue.
1509 * Returns: a #GHashTable of location values
1512 empathy_contact_get_location (EmpathyContact *contact)
1514 EmpathyContactPriv *priv;
1516 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1518 priv = GET_PRIV (contact);
1520 return priv->location;
1524 * empathy_contact_set_location:
1525 * @contact: an #EmpathyContact
1526 * @location: a #GHashTable of the location
1528 * Sets the user's location based on the location #GHashTable passed.
1529 * It is composed of string keys and GValues. Keys are
1530 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1531 * Example: a "city" key would have "Helsinki" as string GValue,
1532 * a "latitude" would have 65.0 as double GValue.
1535 empathy_contact_set_location (EmpathyContact *contact,
1536 GHashTable *location)
1538 EmpathyContactPriv *priv;
1540 g_return_if_fail (EMPATHY_CONTACT (contact));
1541 g_return_if_fail (location != NULL);
1543 priv = GET_PRIV (contact);
1545 if (priv->location != NULL)
1546 g_hash_table_unref (priv->location);
1548 priv->location = g_hash_table_ref (location);
1550 update_geocode (contact);
1552 g_object_notify (G_OBJECT (contact), "location");
1555 const gchar * const *
1556 empathy_contact_get_client_types (EmpathyContact *contact)
1558 EmpathyContactPriv *priv;
1560 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1562 priv = GET_PRIV (contact);
1564 return (const gchar * const *) priv->client_types;
1568 contact_set_client_types (EmpathyContact *contact,
1569 const gchar * const *client_types)
1571 EmpathyContactPriv *priv = GET_PRIV (contact);
1573 if (priv->client_types != NULL)
1574 g_strfreev (priv->client_types);
1576 priv->client_types = g_strdupv ((gchar **) client_types);
1577 g_object_notify (G_OBJECT (contact), "client-types");
1581 * empathy_contact_equal:
1582 * @contact1: an #EmpathyContact
1583 * @contact2: an #EmpathyContact
1585 * Returns FALSE if one of the contacts is NULL but the other is not.
1586 * Otherwise returns TRUE if both pointer are equal or if they bith
1587 * refer to the same id.
1588 * It's only necessary to call this function if your contact objects
1589 * come from logs where contacts are created dynamically and comparing
1590 * pointers is not enough.
1593 empathy_contact_equal (gconstpointer contact1,
1594 gconstpointer contact2)
1601 if ((contact1 == NULL) != (contact2 == NULL)) {
1604 if (contact1 == contact2) {
1607 c1 = EMPATHY_CONTACT (contact1);
1608 c2 = EMPATHY_CONTACT (contact2);
1609 id1 = empathy_contact_get_id (c1);
1610 id2 = empathy_contact_get_id (c2);
1611 if (!tp_strdiff (id1, id2)) {
1618 /* This callback is called by geocode-glib when it found a position
1619 * for the given address. A position is necessary for a contact
1620 * to show up on the map
1623 geocode_cb (GObject *source,
1624 GAsyncResult *result,
1627 EmpathyContact *contact = user_data;
1628 EmpathyContactPriv *priv = GET_PRIV (contact);
1629 GError *error = NULL;
1630 GHashTable *new_location;
1631 GHashTable *resolved = NULL;
1632 gdouble latitude, longitude;
1634 if (priv->location == NULL)
1637 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1640 if (resolved == NULL)
1642 DEBUG ("Failed to resolve geocode: %s", error->message);
1643 g_error_free (error);
1647 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1650 new_location = tp_asv_new (
1651 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1652 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1655 DEBUG ("\t - Latitude: %f", latitude);
1656 DEBUG ("\t - Longitude: %f", longitude);
1658 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1659 * the values we just set. */
1660 tp_g_hash_table_update (new_location, priv->location,
1661 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1663 /* Don't change the accuracy as we used an address to get this position */
1664 g_hash_table_unref (priv->location);
1665 priv->location = new_location;
1666 g_object_notify ((GObject *) contact, "location");
1669 tp_clear_pointer (&resolved, g_hash_table_unref);
1670 g_object_unref (contact);
1674 update_geocode (EmpathyContact *contact)
1676 GeocodeObject *geocode;
1677 GHashTable *location;
1679 location = empathy_contact_get_location (contact);
1680 if (location == NULL ||
1681 g_hash_table_size (location) == 0)
1684 /* No need to search for position if contact published it */
1685 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1686 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1689 geocode = geocode_object_new_for_params (location);
1690 if (geocode == NULL)
1693 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1694 g_object_ref (contact));
1696 g_object_unref (geocode);
1700 static EmpathyCapabilities
1701 tp_caps_to_capabilities (TpCapabilities *caps)
1703 EmpathyCapabilities capabilities = 0;
1707 classes = tp_capabilities_get_channel_classes (caps);
1709 for (i = 0; i < classes->len; i++)
1711 GValueArray *class_struct;
1712 GHashTable *fixed_prop;
1714 TpHandleType handle_type;
1715 const gchar *chan_type;
1717 class_struct = g_ptr_array_index (classes, i);
1718 tp_value_array_unpack (class_struct, 2,
1722 handle_type = tp_asv_get_uint32 (fixed_prop,
1723 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1724 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1727 chan_type = tp_asv_get_string (fixed_prop,
1728 TP_PROP_CHANNEL_CHANNEL_TYPE);
1730 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1732 capabilities |= EMPATHY_CAPABILITIES_FT;
1734 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1736 const gchar *service;
1738 service = tp_asv_get_string (fixed_prop,
1739 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1741 if (!tp_strdiff (service, "rfb"))
1742 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1744 else if (!tp_strdiff (chan_type,
1745 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1749 for (j = 0; allowed_prop[j] != NULL; j++)
1751 if (!tp_strdiff (allowed_prop[j],
1752 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1753 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1754 else if (!tp_strdiff (allowed_prop[j],
1755 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1756 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1759 if (tp_asv_get_boolean (fixed_prop,
1760 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, NULL))
1761 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1762 if (tp_asv_get_boolean (fixed_prop,
1763 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, NULL))
1764 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1766 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1768 if (tp_asv_get_boolean (fixed_prop,
1769 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL))
1770 capabilities |= EMPATHY_CAPABILITIES_SMS;
1772 else if (!tp_strdiff (chan_type,
1773 TPY_IFACE_CHANNEL_TYPE_CALL))
1777 if (tp_asv_get_boolean (fixed_prop,
1778 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, NULL))
1779 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1781 if (tp_asv_get_boolean (fixed_prop,
1782 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, NULL))
1783 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1785 for (j = 0; allowed_prop[j] != NULL; j++)
1787 if (!tp_strdiff (allowed_prop[j],
1788 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO))
1789 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1790 else if (!tp_strdiff (allowed_prop[j],
1791 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO))
1792 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1797 return capabilities;
1801 set_capabilities_from_tp_caps (EmpathyContact *self,
1802 TpCapabilities *caps)
1804 EmpathyCapabilities capabilities;
1809 capabilities = tp_caps_to_capabilities (caps);
1810 empathy_contact_set_capabilities (self, capabilities);
1814 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1816 EmpathyContactPriv *priv = GET_PRIV (contact);
1820 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1821 file = tp_contact_get_avatar_file (priv->tp_contact);
1825 EmpathyAvatar *avatar;
1829 GError *error = NULL;
1831 if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
1833 DEBUG ("Failed to load avatar: %s", error->message);
1835 g_error_free (error);
1836 contact_set_avatar (contact, NULL);
1840 path = g_file_get_path (file);
1842 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1844 contact_set_avatar (contact, avatar);
1845 empathy_avatar_unref (avatar);
1851 contact_set_avatar (contact, NULL);
1856 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1858 EmpathyContact *contact = NULL;
1860 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1862 if (contacts_table == NULL)
1863 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1865 contact = g_hash_table_lookup (contacts_table, tp_contact);
1867 if (contact == NULL)
1869 contact = empathy_contact_new (tp_contact);
1871 /* The hash table does not keep any ref.
1872 * contact keeps a ref to tp_contact, and is removed from the table in
1873 * contact_dispose() */
1874 g_hash_table_insert (contacts_table, tp_contact, contact);
1878 g_object_ref (contact);
1885 presence_cmp_func (EmpathyContact *a,
1888 FolksPresenceDetails *presence_a, *presence_b;
1890 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1891 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1893 /* We negate the result because we're sorting in reverse order (i.e. such that
1894 * the Personas with the highest presence are at the beginning of the list. */
1895 return -folks_presence_details_typecmp (
1896 folks_presence_details_get_presence_type (presence_a),
1897 folks_presence_details_get_presence_type (presence_b));
1901 voip_cmp_func (EmpathyContact *a,
1904 gboolean has_audio_a, has_audio_b;
1905 gboolean has_video_a, has_video_b;
1907 has_audio_a = empathy_contact_can_voip_audio (a);
1908 has_audio_b = empathy_contact_can_voip_audio (b);
1909 has_video_a = empathy_contact_can_voip_video (a);
1910 has_video_b = empathy_contact_can_voip_video (b);
1912 /* First check video */
1913 if (has_video_a == has_video_b)
1915 /* Use audio to to break tie */
1916 if (has_audio_a == has_audio_b)
1918 else if (has_audio_a)
1923 else if (has_video_a)
1930 ft_cmp_func (EmpathyContact *a,
1933 gboolean can_send_files_a, can_send_files_b;
1935 can_send_files_a = empathy_contact_can_send_files (a);
1936 can_send_files_b = empathy_contact_can_send_files (b);
1938 if (can_send_files_a == can_send_files_b)
1940 else if (can_send_files_a)
1947 rfb_stream_tube_cmp_func (EmpathyContact *a,
1950 gboolean rfb_a, rfb_b;
1952 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1953 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1963 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1964 * the same presence, prefer the one which can do both audio *and* video calls,
1965 * over the one which can only do one of the two. */
1967 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1969 gint presence_sort = presence_cmp_func (a, b);
1971 if (presence_sort != 0)
1972 return presence_sort;
1974 return voip_cmp_func (a, b);
1977 /* Sort by presence as with presence_cmp_func() and then break ties using the
1978 * most "capable" individual. So users will be able to pick more actions on
1979 * the contact in the "Contact" menu of the chat window. */
1981 chat_sort_func (EmpathyContact *a,
1986 result = presence_cmp_func (a, b);
1990 /* Prefer individual supporting file transfer */
1991 result = ft_cmp_func (a, b);
1995 /* Check audio/video capabilities */
1996 result = voip_cmp_func (a, b);
2000 /* Check 'Share my destop' feature */
2001 return rfb_stream_tube_cmp_func (a, b);
2005 get_sort_func_for_action (EmpathyActionType action_type)
2007 switch (action_type)
2009 case EMPATHY_ACTION_AUDIO_CALL:
2010 case EMPATHY_ACTION_VIDEO_CALL:
2011 return (GCompareFunc) voip_sort_func;
2012 case EMPATHY_ACTION_CHAT:
2013 return (GCompareFunc) chat_sort_func;
2014 case EMPATHY_ACTION_VIEW_LOGS:
2015 case EMPATHY_ACTION_SEND_FILE:
2016 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
2018 return (GCompareFunc) presence_cmp_func;
2023 * empathy_contact_dup_best_for_action:
2024 * @individual: a #FolksIndividual
2025 * @action_type: the type of action to be performed on the contact
2027 * Chooses a #FolksPersona from the given @individual which is best-suited for
2028 * the given @action_type. "Best-suited" is determined by choosing the persona
2029 * with the highest presence out of all the personas which can perform the given
2030 * @action_type (e.g. are capable of video calling).
2032 * Return value: an #EmpathyContact for the best persona, or %NULL;
2033 * unref with g_object_unref()
2036 empathy_contact_dup_best_for_action (FolksIndividual *individual,
2037 EmpathyActionType action_type)
2042 EmpathyContact *best_contact = NULL;
2044 /* Build a list of EmpathyContacts that we can sort */
2045 personas = folks_individual_get_personas (individual);
2048 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2049 while (gee_iterator_next (iter))
2051 FolksPersona *persona = gee_iterator_get (iter);
2052 TpContact *tp_contact;
2053 EmpathyContact *contact = NULL;
2055 if (!empathy_folks_persona_is_interesting (persona))
2058 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
2059 if (tp_contact == NULL)
2062 contact = empathy_contact_dup_from_tp_contact (tp_contact);
2063 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
2065 /* Only choose the contact if they're actually capable of the specified
2067 if (empathy_contact_can_do_action (contact, action_type))
2068 contacts = g_list_prepend (contacts, g_object_ref (contact));
2071 g_clear_object (&contact);
2072 g_clear_object (&persona);
2074 g_clear_object (&iter);
2076 /* Sort the contacts by some heuristic based on the action type, then take
2077 * the top contact. */
2078 if (contacts != NULL)
2080 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2081 best_contact = g_object_ref (contacts->data);
2084 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2085 g_list_free (contacts);
2087 return best_contact;
2090 #define declare_contact_cb(name) \
2092 contact_##name##_cb (GObject *source, \
2093 GAsyncResult *result, \
2094 gpointer user_data) \
2096 TpContact *contact = (TpContact *) source; \
2097 GError *error = NULL; \
2099 if (!tp_contact_##name##_finish (contact, result, &error)) \
2101 DEBUG ("Failed to ##name## on %s\n", \
2102 tp_contact_get_identifier (contact)); \
2103 g_error_free (error); \
2107 declare_contact_cb (request_subscription)
2108 declare_contact_cb (authorize_publication)
2109 declare_contact_cb (unblock)
2112 empathy_contact_add_to_contact_list (EmpathyContact *self,
2113 const gchar *message)
2115 EmpathyContactPriv *priv = GET_PRIV (self);
2117 g_return_if_fail (priv->tp_contact != NULL);
2119 tp_contact_request_subscription_async (priv->tp_contact, message,
2120 contact_request_subscription_cb, NULL);
2122 tp_contact_authorize_publication_async (priv->tp_contact,
2123 contact_authorize_publication_cb, NULL);
2125 tp_contact_unblock_async (priv->tp_contact, contact_unblock_cb, NULL);
2128 declare_contact_cb (remove)
2131 empathy_contact_remove_from_contact_list (EmpathyContact *self)
2133 EmpathyContactPriv *priv = GET_PRIV (self);
2135 g_return_if_fail (priv->tp_contact != NULL);
2137 tp_contact_remove_async (priv->tp_contact, contact_remove_cb, NULL);