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-individual-manager.h"
44 #include "empathy-utils.h"
45 #include "empathy-enum-types.h"
46 #include "empathy-marshal.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;
59 EmpathyAvatar *avatar;
60 TpConnectionPresenceType presence;
62 EmpathyCapabilities capabilities;
65 /* Location is composed of string keys and GValues.
66 * Example: a "city" key would have "Helsinki" as string GValue,
67 * a "latitude" would have 65.0 as double GValue.
69 * This is a super set of the location stored in TpContact as we can try add
70 * more fields by searching the address using geoclue.
77 static void contact_finalize (GObject *object);
78 static void contact_get_property (GObject *object, guint param_id,
79 GValue *value, GParamSpec *pspec);
80 static void contact_set_property (GObject *object, guint param_id,
81 const GValue *value, GParamSpec *pspec);
84 static void update_geocode (EmpathyContact *contact);
87 static void empathy_contact_set_location (EmpathyContact *contact,
88 GHashTable *location);
90 static void contact_set_client_types (EmpathyContact *contact,
91 const gchar * const *types);
93 static void set_capabilities_from_tp_caps (EmpathyContact *self,
94 TpCapabilities *caps);
96 static void contact_set_avatar (EmpathyContact *contact,
97 EmpathyAvatar *avatar);
98 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
99 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
102 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
114 PROP_PRESENCE_MESSAGE,
127 static guint signals[LAST_SIGNAL];
129 /* TpContact* -> EmpathyContact*, both borrowed ref */
130 static GHashTable *contacts_table = NULL;
133 tp_contact_notify_cb (TpContact *tp_contact,
137 EmpathyContactPriv *priv = GET_PRIV (contact);
139 /* Forward property notifications */
140 if (!tp_strdiff (param->name, "alias"))
141 g_object_notify (contact, "alias");
142 else if (!tp_strdiff (param->name, "presence-type")) {
143 TpConnectionPresenceType presence;
145 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
146 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
148 priv->presence = presence;
149 g_object_notify (contact, "presence");
151 else if (!tp_strdiff (param->name, "identifier"))
152 g_object_notify (contact, "id");
153 else if (!tp_strdiff (param->name, "handle"))
154 g_object_notify (contact, "handle");
155 else if (!tp_strdiff (param->name, "location"))
157 GHashTable *location;
159 location = tp_contact_get_location (tp_contact);
160 /* This will start a geoclue search to find the address if needed */
161 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
163 else if (!tp_strdiff (param->name, "capabilities"))
165 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
166 tp_contact_get_capabilities (tp_contact));
168 else if (!tp_strdiff (param->name, "avatar-file"))
170 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
172 else if (!tp_strdiff (param->name, "client-types"))
174 contact_set_client_types (EMPATHY_CONTACT (contact),
175 tp_contact_get_client_types (tp_contact));
180 folks_persona_notify_cb (FolksPersona *folks_persona,
184 if (!tp_strdiff (param->name, "presence-message"))
185 g_object_notify (contact, "presence-message");
189 contact_dispose (GObject *object)
191 EmpathyContactPriv *priv = GET_PRIV (object);
193 if (priv->tp_contact != NULL)
195 g_signal_handlers_disconnect_by_func (priv->tp_contact,
196 tp_contact_notify_cb, object);
198 tp_clear_object (&priv->tp_contact);
201 g_object_unref (priv->account);
202 priv->account = NULL;
206 g_signal_handlers_disconnect_by_func (priv->persona,
207 folks_persona_notify_cb, object);
208 g_object_unref (priv->persona);
210 priv->persona = NULL;
212 if (priv->avatar != NULL)
214 empathy_avatar_unref (priv->avatar);
218 if (priv->location != NULL)
220 g_hash_table_unref (priv->location);
221 priv->location = NULL;
224 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
228 contact_constructed (GObject *object)
230 EmpathyContact *contact = (EmpathyContact *) object;
231 EmpathyContactPriv *priv = GET_PRIV (contact);
232 GHashTable *location;
233 TpHandle self_handle;
235 const gchar * const *client_types;
237 if (priv->tp_contact == NULL)
240 priv->presence = empathy_contact_get_presence (contact);
242 location = tp_contact_get_location (priv->tp_contact);
243 if (location != NULL)
244 empathy_contact_set_location (contact, location);
246 client_types = tp_contact_get_client_types (priv->tp_contact);
247 if (client_types != NULL)
248 contact_set_client_types (contact, client_types);
250 set_capabilities_from_tp_caps (contact,
251 tp_contact_get_capabilities (priv->tp_contact));
253 contact_set_avatar_from_tp_contact (contact);
255 /* Set is-user property. Note that it could still be the handle is
256 * different from the connection's self handle, in the case the handle
257 * comes from a group interface. */
258 self_handle = tp_connection_get_self_handle (
259 tp_contact_get_connection (priv->tp_contact));
260 handle = tp_contact_get_handle (priv->tp_contact);
261 empathy_contact_set_is_user (contact, self_handle == handle);
263 g_signal_connect (priv->tp_contact, "notify",
264 G_CALLBACK (tp_contact_notify_cb), contact);
268 empathy_contact_class_init (EmpathyContactClass *class)
270 GObjectClass *object_class;
272 object_class = G_OBJECT_CLASS (class);
274 object_class->finalize = contact_finalize;
275 object_class->dispose = contact_dispose;
276 object_class->get_property = contact_get_property;
277 object_class->set_property = contact_set_property;
278 object_class->constructed = contact_constructed;
280 g_object_class_install_property (object_class,
282 g_param_spec_object ("tp-contact",
284 "The TpContact associated with the contact",
286 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288 g_object_class_install_property (object_class,
290 g_param_spec_object ("account",
292 "The account associated with the contact",
294 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (object_class,
298 g_param_spec_object ("persona",
300 "The FolksPersona associated with the contact",
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (object_class,
306 g_param_spec_string ("id",
308 "String identifying contact",
310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (object_class,
314 g_param_spec_string ("alias",
316 "An alias for the contact",
318 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (object_class,
322 g_param_spec_boxed ("avatar",
326 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (object_class,
330 g_param_spec_uint ("presence",
332 "Presence of contact",
333 TP_CONNECTION_PRESENCE_TYPE_UNSET,
334 NUM_TP_CONNECTION_PRESENCE_TYPES,
335 TP_CONNECTION_PRESENCE_TYPE_UNSET,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338 g_object_class_install_property (object_class,
339 PROP_PRESENCE_MESSAGE,
340 g_param_spec_string ("presence-message",
341 "Contact presence message",
342 "Presence message of contact",
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (object_class,
348 g_param_spec_uint ("handle",
350 "The handle of the contact",
354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (object_class,
358 g_param_spec_flags ("capabilities",
359 "Contact Capabilities",
360 "Capabilities of the contact",
361 EMPATHY_TYPE_CAPABILITIES,
362 EMPATHY_CAPABILITIES_UNKNOWN,
363 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (object_class,
367 g_param_spec_boolean ("is-user",
369 "Is contact the user",
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (object_class,
376 g_param_spec_boxed ("location",
378 "Physical location of the contact",
380 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
382 g_object_class_install_property (object_class,
384 g_param_spec_boxed ("client-types",
385 "Contact client types",
386 "Client types of the contact",
388 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
390 signals[PRESENCE_CHANGED] =
391 g_signal_new ("presence-changed",
392 G_TYPE_FROM_CLASS (class),
396 _empathy_marshal_VOID__UINT_UINT,
401 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
405 empathy_contact_init (EmpathyContact *contact)
407 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
408 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
410 contact->priv = priv;
412 priv->location = NULL;
413 priv->client_types = NULL;
418 contact_finalize (GObject *object)
420 EmpathyContactPriv *priv;
422 priv = GET_PRIV (object);
424 DEBUG ("finalize: %p", object);
426 g_clear_object (&priv->groups);
427 g_free (priv->alias);
429 g_strfreev (priv->client_types);
431 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
435 empathy_contact_set_capabilities (EmpathyContact *contact,
436 EmpathyCapabilities capabilities)
438 EmpathyContactPriv *priv;
440 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
442 priv = GET_PRIV (contact);
444 if (priv->capabilities == capabilities)
447 priv->capabilities = capabilities;
449 g_object_notify (G_OBJECT (contact), "capabilities");
453 empathy_contact_set_id (EmpathyContact *contact,
456 EmpathyContactPriv *priv;
458 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
459 g_return_if_fail (id != NULL);
461 priv = GET_PRIV (contact);
463 /* We temporally ref the contact because it could be destroyed
464 * during the signal emition */
465 g_object_ref (contact);
466 if (tp_strdiff (id, priv->id))
469 priv->id = g_strdup (id);
471 g_object_notify (G_OBJECT (contact), "id");
472 if (EMP_STR_EMPTY (priv->alias))
473 g_object_notify (G_OBJECT (contact), "alias");
476 g_object_unref (contact);
480 empathy_contact_set_presence (EmpathyContact *contact,
481 TpConnectionPresenceType presence)
483 EmpathyContactPriv *priv;
484 TpConnectionPresenceType old_presence;
486 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
488 priv = GET_PRIV (contact);
490 if (presence == priv->presence)
493 old_presence = priv->presence;
494 priv->presence = presence;
496 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
498 g_object_notify (G_OBJECT (contact), "presence");
502 empathy_contact_set_presence_message (EmpathyContact *contact,
503 const gchar *message)
505 EmpathyContactPriv *priv = GET_PRIV (contact);
507 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
509 if (priv->persona != NULL)
511 folks_presence_details_set_presence_message (
512 FOLKS_PRESENCE_DETAILS (priv->persona), message);
517 empathy_contact_set_handle (EmpathyContact *contact,
520 EmpathyContactPriv *priv;
522 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
524 priv = GET_PRIV (contact);
526 g_object_ref (contact);
527 if (handle != priv->handle)
529 priv->handle = handle;
530 g_object_notify (G_OBJECT (contact), "handle");
532 g_object_unref (contact);
536 contact_get_property (GObject *object,
541 EmpathyContact *contact = EMPATHY_CONTACT (object);
545 case PROP_TP_CONTACT:
546 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
549 g_value_set_object (value, empathy_contact_get_account (contact));
552 g_value_set_object (value, empathy_contact_get_persona (contact));
555 g_value_set_string (value, empathy_contact_get_id (contact));
558 g_value_set_string (value, empathy_contact_get_alias (contact));
561 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
564 g_value_set_uint (value, empathy_contact_get_presence (contact));
566 case PROP_PRESENCE_MESSAGE:
567 g_value_set_string (value, empathy_contact_get_presence_message (contact));
570 g_value_set_uint (value, empathy_contact_get_handle (contact));
572 case PROP_CAPABILITIES:
573 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
576 g_value_set_boolean (value, empathy_contact_is_user (contact));
579 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
585 contact_set_property (GObject *object,
590 EmpathyContact *contact = EMPATHY_CONTACT (object);
591 EmpathyContactPriv *priv = GET_PRIV (object);
595 case PROP_TP_CONTACT:
596 priv->tp_contact = g_value_dup_object (value);
599 g_assert (priv->account == NULL);
600 priv->account = g_value_dup_object (value);
603 empathy_contact_set_persona (contact, g_value_get_object (value));
606 empathy_contact_set_id (contact, g_value_get_string (value));
609 empathy_contact_set_alias (contact, g_value_get_string (value));
612 empathy_contact_set_presence (contact, g_value_get_uint (value));
614 case PROP_PRESENCE_MESSAGE:
615 empathy_contact_set_presence_message (contact, g_value_get_string (value));
618 empathy_contact_set_handle (contact, g_value_get_uint (value));
620 case PROP_CAPABILITIES:
621 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
624 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
627 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
633 remove_tp_contact (gpointer data,
636 g_hash_table_remove (contacts_table, data);
639 static EmpathyContact *
640 empathy_contact_new (TpContact *tp_contact)
642 EmpathyContact *retval;
644 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
646 retval = g_object_new (EMPATHY_TYPE_CONTACT,
647 "tp-contact", tp_contact,
650 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
662 contact_is_tpl_entity (gpointer key,
666 EmpathyContact *contact = value;
667 FindContactData *data = user_data;
669 return !tp_strdiff (empathy_contact_get_id (contact),
670 tpl_entity_get_identifier (data->entity)) &&
671 !tp_strdiff (tp_proxy_get_object_path (data->account),
672 tp_proxy_get_object_path (
673 empathy_contact_get_account (contact)));
677 empathy_contact_from_tpl_contact (TpAccount *account,
678 TplEntity *tpl_entity)
680 EmpathyContact *retval;
682 EmpathyContact *existing_contact = NULL;
684 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
686 if (contacts_table != NULL)
688 FindContactData data;
690 data.entity = tpl_entity;
691 data.account = account;
693 existing_contact = g_hash_table_find (contacts_table,
694 contact_is_tpl_entity, &data);
697 if (existing_contact != NULL)
699 EmpathyContactPriv *priv;
701 retval = g_object_new (EMPATHY_TYPE_CONTACT,
702 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
705 priv = GET_PRIV (retval);
707 /* contact_set_property() calls empathy_contact_set_alias(), which
708 * tries to set the alias on the FolksPersona, but we don't want to
709 * do that when creating an EmpathyContact from a TplEntity. So just
710 * set priv->alias instead of passing it to g_object_new() instead. */
711 g_free (priv->alias);
712 priv->alias = g_strdup (tpl_entity_get_alias (tpl_entity));
716 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
718 retval = g_object_new (EMPATHY_TYPE_CONTACT,
719 "id", tpl_entity_get_identifier (tpl_entity),
720 "alias", tpl_entity_get_alias (tpl_entity),
726 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
727 contact_load_avatar_cache (retval,
728 tpl_entity_get_avatar_token (tpl_entity));
734 empathy_contact_get_tp_contact (EmpathyContact *contact)
736 EmpathyContactPriv *priv;
738 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
740 priv = GET_PRIV (contact);
742 return priv->tp_contact;
746 empathy_contact_get_id (EmpathyContact *contact)
748 EmpathyContactPriv *priv;
750 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
752 priv = GET_PRIV (contact);
754 if (priv->tp_contact != NULL)
755 return tp_contact_get_identifier (priv->tp_contact);
761 empathy_contact_get_alias (EmpathyContact *contact)
763 EmpathyContactPriv *priv;
764 const gchar *alias = NULL;
766 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
768 priv = GET_PRIV (contact);
770 if (!EMP_STR_EMPTY (priv->alias))
772 else if (priv->tp_contact != NULL)
773 alias = tp_contact_get_alias (priv->tp_contact);
775 if (!EMP_STR_EMPTY (alias))
778 return empathy_contact_get_id (contact);
782 empathy_contact_set_alias (EmpathyContact *contact,
785 EmpathyContactPriv *priv;
786 FolksPersona *persona;
788 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
790 priv = GET_PRIV (contact);
792 g_object_ref (contact);
794 /* Set the alias on the persona if possible */
795 persona = empathy_contact_get_persona (contact);
796 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
798 DEBUG ("Setting alias for contact %s to %s",
799 empathy_contact_get_id (contact), alias);
801 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
804 if (tp_strdiff (alias, priv->alias))
806 g_free (priv->alias);
807 priv->alias = g_strdup (alias);
808 g_object_notify (G_OBJECT (contact), "alias");
811 g_object_unref (contact);
815 groups_change_group_cb (GObject *source,
816 GAsyncResult *result,
819 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
820 GError *error = NULL;
822 folks_group_details_change_group_finish (group_details, result, &error);
825 g_warning ("failed to change group: %s", error->message);
826 g_clear_error (&error);
831 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
834 EmpathyContactPriv *priv;
835 FolksPersona *persona;
837 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
838 g_return_if_fail (group != NULL);
840 priv = GET_PRIV (contact);
842 /* Normally pass through the changes to the persona */
843 persona = empathy_contact_get_persona (contact);
846 if (FOLKS_IS_GROUP_DETAILS (persona))
847 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
848 is_member, groups_change_group_cb, contact);
852 /* If the persona doesn't exist yet, we have to cache the changes until it
854 if (priv->groups == NULL)
856 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
857 g_free, g_str_hash, g_str_equal);
860 gee_collection_add (GEE_COLLECTION (priv->groups), group);
864 empathy_contact_get_avatar (EmpathyContact *contact)
866 EmpathyContactPriv *priv;
868 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
870 priv = GET_PRIV (contact);
876 contact_set_avatar (EmpathyContact *contact,
877 EmpathyAvatar *avatar)
879 EmpathyContactPriv *priv;
881 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
883 priv = GET_PRIV (contact);
885 if (priv->avatar == avatar)
890 empathy_avatar_unref (priv->avatar);
895 priv->avatar = empathy_avatar_ref (avatar);
897 g_object_notify (G_OBJECT (contact), "avatar");
901 empathy_contact_get_account (EmpathyContact *contact)
903 EmpathyContactPriv *priv;
905 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
907 priv = GET_PRIV (contact);
909 if (priv->account == NULL && priv->tp_contact != NULL)
911 TpConnection *connection;
913 /* FIXME: This assume the account manager already exists */
914 connection = tp_contact_get_connection (priv->tp_contact);
916 g_object_ref (empathy_get_account_for_connection (connection));
919 return priv->account;
923 empathy_contact_get_persona (EmpathyContact *contact)
925 EmpathyContactPriv *priv;
927 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
929 priv = GET_PRIV (contact);
931 if (priv->persona == NULL && priv->tp_contact != NULL)
933 /* FIXME: This is disgustingly slow */
934 /* Query for the persona */
935 EmpathyIndividualManager *manager;
936 GList *individuals, *l;
938 manager = empathy_individual_manager_dup_singleton ();
939 individuals = empathy_individual_manager_get_members (manager);
941 for (l = individuals; l != NULL; l = l->next)
943 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
946 gboolean persona_found = FALSE;
948 personas = folks_individual_get_personas (individual);
949 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
950 while (!persona_found && gee_iterator_next (iter))
952 TpfPersona *persona = gee_iterator_get (iter);
954 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
956 TpContact *tp_contact = tpf_persona_get_contact (persona);
958 if (tp_contact == priv->tp_contact)
960 /* Found the right persona */
961 empathy_contact_set_persona (contact,
962 (FolksPersona *) persona);
963 persona_found = TRUE;
965 g_clear_object (&persona);
968 g_clear_object (&iter);
971 g_list_free (individuals);
972 g_object_unref (manager);
975 return priv->persona;
979 empathy_contact_set_persona (EmpathyContact *contact,
980 FolksPersona *persona)
982 EmpathyContactPriv *priv;
984 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
985 g_return_if_fail (TPF_IS_PERSONA (persona));
987 priv = GET_PRIV (contact);
989 if (persona == priv->persona)
992 if (priv->persona != NULL)
994 g_signal_handlers_disconnect_by_func (priv->persona,
995 folks_persona_notify_cb, contact);
996 g_object_unref (priv->persona);
998 priv->persona = g_object_ref (persona);
1000 g_signal_connect (priv->persona, "notify",
1001 G_CALLBACK (folks_persona_notify_cb), contact);
1003 g_object_notify (G_OBJECT (contact), "persona");
1005 /* Set the persona's alias, since ours could've been set using
1006 * empathy_contact_set_alias() before we had a persona; this happens when
1007 * adding a contact. */
1008 if (priv->alias != NULL)
1009 empathy_contact_set_alias (contact, priv->alias);
1011 /* Set the persona's groups */
1012 if (priv->groups != NULL)
1014 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1015 GEE_SET (priv->groups));
1016 g_object_unref (priv->groups);
1017 priv->groups = NULL;
1022 empathy_contact_get_connection (EmpathyContact *contact)
1024 EmpathyContactPriv *priv;
1026 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1028 priv = GET_PRIV (contact);
1030 if (priv->tp_contact != NULL)
1031 return tp_contact_get_connection (priv->tp_contact);
1036 TpConnectionPresenceType
1037 empathy_contact_get_presence (EmpathyContact *contact)
1039 EmpathyContactPriv *priv;
1041 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1042 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1044 priv = GET_PRIV (contact);
1046 if (priv->tp_contact != NULL)
1047 return tp_contact_get_presence_type (priv->tp_contact);
1049 return priv->presence;
1053 empathy_contact_get_presence_message (EmpathyContact *contact)
1055 EmpathyContactPriv *priv;
1057 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1059 priv = GET_PRIV (contact);
1061 if (priv->persona != NULL)
1062 return folks_presence_details_get_presence_message (
1063 FOLKS_PRESENCE_DETAILS (priv->persona));
1065 if (priv->tp_contact != NULL)
1066 return tp_contact_get_presence_message (priv->tp_contact);
1072 empathy_contact_get_handle (EmpathyContact *contact)
1074 EmpathyContactPriv *priv;
1076 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1078 priv = GET_PRIV (contact);
1080 if (priv->tp_contact != NULL)
1081 return tp_contact_get_handle (priv->tp_contact);
1083 return priv->handle;
1087 empathy_contact_get_capabilities (EmpathyContact *contact)
1089 EmpathyContactPriv *priv;
1091 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1093 priv = GET_PRIV (contact);
1095 return priv->capabilities;
1099 empathy_contact_is_user (EmpathyContact *contact)
1101 EmpathyContactPriv *priv;
1103 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1105 priv = GET_PRIV (contact);
1107 return priv->is_user;
1111 empathy_contact_set_is_user (EmpathyContact *contact,
1114 EmpathyContactPriv *priv;
1116 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1118 priv = GET_PRIV (contact);
1120 if (priv->is_user == is_user)
1123 priv->is_user = is_user;
1125 g_object_notify (G_OBJECT (contact), "is-user");
1129 empathy_contact_is_online (EmpathyContact *contact)
1131 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1133 switch (empathy_contact_get_presence (contact))
1135 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1136 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1137 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1139 /* Contacts without presence are considered online so we can display IRC
1140 * contacts in rooms. */
1141 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1142 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1143 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1144 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1145 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1146 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1153 empathy_contact_get_status (EmpathyContact *contact)
1155 const gchar *message;
1157 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1159 message = empathy_contact_get_presence_message (contact);
1160 if (!EMP_STR_EMPTY (message))
1163 return empathy_presence_get_default_message (
1164 empathy_contact_get_presence (contact));
1168 empathy_contact_can_sms (EmpathyContact *contact)
1170 EmpathyContactPriv *priv;
1172 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1174 priv = GET_PRIV (contact);
1176 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1180 empathy_contact_can_voip (EmpathyContact *contact)
1182 EmpathyContactPriv *priv;
1184 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1186 priv = GET_PRIV (contact);
1188 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1189 EMPATHY_CAPABILITIES_VIDEO);
1193 empathy_contact_can_voip_audio (EmpathyContact *contact)
1195 EmpathyContactPriv *priv;
1197 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1199 priv = GET_PRIV (contact);
1201 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1205 empathy_contact_can_voip_video (EmpathyContact *contact)
1207 EmpathyContactPriv *priv;
1209 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1211 priv = GET_PRIV (contact);
1213 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1217 empathy_contact_can_send_files (EmpathyContact *contact)
1219 EmpathyContactPriv *priv;
1221 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1223 priv = GET_PRIV (contact);
1225 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1229 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1231 EmpathyContactPriv *priv;
1233 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1235 priv = GET_PRIV (contact);
1237 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1241 contact_has_log (EmpathyContact *contact)
1243 TplLogManager *manager;
1247 manager = tpl_log_manager_dup_singleton ();
1248 entity = tpl_entity_new (empathy_contact_get_id (contact),
1249 TPL_ENTITY_CONTACT, NULL, NULL);
1251 have_log = tpl_log_manager_exists (manager,
1252 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1254 g_object_unref (entity);
1255 g_object_unref (manager);
1261 empathy_contact_can_do_action (EmpathyContact *self,
1262 EmpathyActionType action_type)
1264 gboolean sensitivity = FALSE;
1266 switch (action_type)
1268 case EMPATHY_ACTION_CHAT:
1271 case EMPATHY_ACTION_SMS:
1272 sensitivity = empathy_contact_can_sms (self);
1274 case EMPATHY_ACTION_AUDIO_CALL:
1275 sensitivity = empathy_contact_can_voip_audio (self);
1277 case EMPATHY_ACTION_VIDEO_CALL:
1278 sensitivity = empathy_contact_can_voip_video (self);
1280 case EMPATHY_ACTION_VIEW_LOGS:
1281 sensitivity = contact_has_log (self);
1283 case EMPATHY_ACTION_SEND_FILE:
1284 sensitivity = empathy_contact_can_send_files (self);
1286 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1287 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1290 g_assert_not_reached ();
1293 return (sensitivity ? TRUE : FALSE);
1297 contact_get_avatar_filename (EmpathyContact *contact,
1303 gchar *token_escaped;
1305 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1308 token_escaped = tp_escape_as_identifier (token);
1309 account = empathy_contact_get_account (contact);
1311 avatar_path = g_build_filename (g_get_user_cache_dir (),
1314 tp_account_get_connection_manager (account),
1315 tp_account_get_protocol (account),
1317 g_mkdir_with_parents (avatar_path, 0700);
1319 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1321 g_free (token_escaped);
1322 g_free (avatar_path);
1328 contact_load_avatar_cache (EmpathyContact *contact,
1331 EmpathyAvatar *avatar = NULL;
1335 GError *error = NULL;
1337 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1338 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1340 /* Load the avatar from file if it exists */
1341 filename = contact_get_avatar_filename (contact, token);
1342 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1344 if (!g_file_get_contents (filename, &data, &len, &error))
1346 DEBUG ("Failed to load avatar from cache: %s",
1347 error ? error->message : "No error given");
1348 g_clear_error (&error);
1354 DEBUG ("Avatar loaded from %s", filename);
1355 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1356 contact_set_avatar (contact, avatar);
1357 empathy_avatar_unref (avatar);
1363 return data != NULL;
1367 empathy_avatar_get_type (void)
1369 static GType type_id = 0;
1373 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1374 (GBoxedCopyFunc) empathy_avatar_ref,
1375 (GBoxedFreeFunc) empathy_avatar_unref);
1382 * empathy_avatar_new:
1383 * @data: the avatar data
1384 * @len: the size of avatar data
1385 * @format: the mime type of the avatar image
1386 * @filename: the filename where the avatar is stored in cache
1388 * Create a #EmpathyAvatar from the provided data.
1390 * Returns: a new #EmpathyAvatar
1393 empathy_avatar_new (const guchar *data,
1395 const gchar *format,
1396 const gchar *filename)
1398 EmpathyAvatar *avatar;
1400 avatar = g_slice_new0 (EmpathyAvatar);
1401 avatar->data = g_memdup (data, len);
1403 avatar->format = g_strdup (format);
1404 avatar->filename = g_strdup (filename);
1405 avatar->refcount = 1;
1411 empathy_avatar_unref (EmpathyAvatar *avatar)
1413 g_return_if_fail (avatar != NULL);
1416 if (avatar->refcount == 0)
1418 g_free (avatar->data);
1419 g_free (avatar->format);
1420 g_free (avatar->filename);
1421 g_slice_free (EmpathyAvatar, avatar);
1426 empathy_avatar_ref (EmpathyAvatar *avatar)
1428 g_return_val_if_fail (avatar != NULL, NULL);
1436 * empathy_avatar_save_to_file:
1437 * @avatar: the avatar
1438 * @filename: name of a file to write avatar to
1439 * @error: return location for a GError, or NULL
1441 * Save the avatar to a file named filename
1443 * Returns: %TRUE on success, %FALSE if an error occurred
1446 empathy_avatar_save_to_file (EmpathyAvatar *self,
1447 const gchar *filename,
1450 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1455 * empathy_contact_get_location:
1456 * @contact: an #EmpathyContact
1458 * Returns the user's location if available. The keys are defined in
1459 * empathy-location.h. If the contact doesn't have location
1460 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1461 * you are done with the #GHashTable.
1463 * It is composed of string keys and GValues. Keys are
1464 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1465 * Example: a "city" key would have "Helsinki" as string GValue,
1466 * a "latitude" would have 65.0 as double GValue.
1468 * Returns: a #GHashTable of location values
1471 empathy_contact_get_location (EmpathyContact *contact)
1473 EmpathyContactPriv *priv;
1475 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1477 priv = GET_PRIV (contact);
1479 return priv->location;
1483 * empathy_contact_set_location:
1484 * @contact: an #EmpathyContact
1485 * @location: a #GHashTable of the location
1487 * Sets the user's location based on the location #GHashTable passed.
1488 * It is composed of string keys and GValues. Keys are
1489 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1490 * Example: a "city" key would have "Helsinki" as string GValue,
1491 * a "latitude" would have 65.0 as double GValue.
1494 empathy_contact_set_location (EmpathyContact *contact,
1495 GHashTable *location)
1497 EmpathyContactPriv *priv;
1499 g_return_if_fail (EMPATHY_CONTACT (contact));
1500 g_return_if_fail (location != NULL);
1502 priv = GET_PRIV (contact);
1504 if (priv->location != NULL)
1505 g_hash_table_unref (priv->location);
1507 priv->location = g_hash_table_ref (location);
1509 update_geocode (contact);
1511 g_object_notify (G_OBJECT (contact), "location");
1514 const gchar * const *
1515 empathy_contact_get_client_types (EmpathyContact *contact)
1517 EmpathyContactPriv *priv;
1519 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1521 priv = GET_PRIV (contact);
1523 return (const gchar * const *) priv->client_types;
1527 contact_set_client_types (EmpathyContact *contact,
1528 const gchar * const *client_types)
1530 EmpathyContactPriv *priv = GET_PRIV (contact);
1532 if (priv->client_types != NULL)
1533 g_strfreev (priv->client_types);
1535 priv->client_types = g_strdupv ((gchar **) client_types);
1536 g_object_notify (G_OBJECT (contact), "client-types");
1540 * empathy_contact_equal:
1541 * @contact1: an #EmpathyContact
1542 * @contact2: an #EmpathyContact
1544 * Returns FALSE if one of the contacts is NULL but the other is not.
1545 * Otherwise returns TRUE if both pointer are equal or if they bith
1546 * refer to the same id.
1547 * It's only necessary to call this function if your contact objects
1548 * come from logs where contacts are created dynamically and comparing
1549 * pointers is not enough.
1552 empathy_contact_equal (gconstpointer contact1,
1553 gconstpointer contact2)
1560 if ((contact1 == NULL) != (contact2 == NULL)) {
1563 if (contact1 == contact2) {
1566 c1 = EMPATHY_CONTACT (contact1);
1567 c2 = EMPATHY_CONTACT (contact2);
1568 id1 = empathy_contact_get_id (c1);
1569 id2 = empathy_contact_get_id (c2);
1570 if (!tp_strdiff (id1, id2)) {
1577 /* This callback is called by geocode-glib when it found a position
1578 * for the given address. A position is necessary for a contact
1579 * to show up on the map
1582 geocode_cb (GObject *source,
1583 GAsyncResult *result,
1586 EmpathyContact *contact = user_data;
1587 EmpathyContactPriv *priv = GET_PRIV (contact);
1588 GError *error = NULL;
1589 GHashTable *new_location;
1590 GHashTable *resolved;
1591 gdouble latitude, longitude;
1593 if (priv->location == NULL)
1596 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1599 if (resolved == NULL)
1601 DEBUG ("Failed to resolve geocode: %s", error->message);
1602 g_error_free (error);
1606 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1609 new_location = tp_asv_new (
1610 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1611 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1614 DEBUG ("\t - Latitude: %f", latitude);
1615 DEBUG ("\t - Longitude: %f", longitude);
1617 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1618 * the values we just set. */
1619 tp_g_hash_table_update (new_location, priv->location,
1620 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1622 /* Don't change the accuracy as we used an address to get this position */
1623 g_hash_table_unref (priv->location);
1624 priv->location = new_location;
1625 g_object_notify ((GObject *) contact, "location");
1628 tp_clear_pointer (&result, g_hash_table_unref);
1629 g_object_unref (contact);
1633 update_geocode (EmpathyContact *contact)
1635 GeocodeObject *geocode;
1636 GHashTable *location;
1638 location = empathy_contact_get_location (contact);
1639 if (location == NULL)
1642 /* No need to search for position if contact published it */
1643 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1644 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1647 geocode = geocode_object_new_for_params (location);
1649 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1650 g_object_ref (contact));
1652 g_object_unref (geocode);
1656 static EmpathyCapabilities
1657 tp_caps_to_capabilities (TpCapabilities *caps)
1659 EmpathyCapabilities capabilities = 0;
1663 classes = tp_capabilities_get_channel_classes (caps);
1665 for (i = 0; i < classes->len; i++)
1667 GValueArray *class_struct;
1668 GHashTable *fixed_prop;
1670 TpHandleType handle_type;
1671 const gchar *chan_type;
1673 class_struct = g_ptr_array_index (classes, i);
1674 tp_value_array_unpack (class_struct, 2,
1678 handle_type = tp_asv_get_uint32 (fixed_prop,
1679 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1680 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1683 chan_type = tp_asv_get_string (fixed_prop,
1684 TP_PROP_CHANNEL_CHANNEL_TYPE);
1686 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1688 capabilities |= EMPATHY_CAPABILITIES_FT;
1690 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1692 const gchar *service;
1694 service = tp_asv_get_string (fixed_prop,
1695 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1697 if (!tp_strdiff (service, "rfb"))
1698 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1700 else if (!tp_strdiff (chan_type,
1701 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1705 for (j = 0; allowed_prop[j] != NULL; j++)
1707 if (!tp_strdiff (allowed_prop[j],
1708 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1709 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1710 else if (!tp_strdiff (allowed_prop[j],
1711 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1712 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1715 if (tp_asv_get_boolean (fixed_prop,
1716 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, NULL))
1717 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1718 if (tp_asv_get_boolean (fixed_prop,
1719 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, NULL))
1720 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1722 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1724 if (tp_asv_get_boolean (fixed_prop,
1725 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL))
1726 capabilities |= EMPATHY_CAPABILITIES_SMS;
1728 else if (!tp_strdiff (chan_type,
1729 TPY_IFACE_CHANNEL_TYPE_CALL))
1733 if (tp_asv_get_boolean (fixed_prop,
1734 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, NULL))
1735 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1737 if (tp_asv_get_boolean (fixed_prop,
1738 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, NULL))
1739 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1741 for (j = 0; allowed_prop[j] != NULL; j++)
1743 if (!tp_strdiff (allowed_prop[j],
1744 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO))
1745 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1746 else if (!tp_strdiff (allowed_prop[j],
1747 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO))
1748 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1753 return capabilities;
1757 set_capabilities_from_tp_caps (EmpathyContact *self,
1758 TpCapabilities *caps)
1760 EmpathyCapabilities capabilities;
1765 capabilities = tp_caps_to_capabilities (caps);
1766 empathy_contact_set_capabilities (self, capabilities);
1770 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1772 EmpathyContactPriv *priv = GET_PRIV (contact);
1776 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1777 file = tp_contact_get_avatar_file (priv->tp_contact);
1781 EmpathyAvatar *avatar;
1786 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1787 path = g_file_get_path (file);
1789 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1791 contact_set_avatar (contact, avatar);
1792 empathy_avatar_unref (avatar);
1798 contact_set_avatar (contact, NULL);
1803 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1805 EmpathyContact *contact = NULL;
1807 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1809 if (contacts_table == NULL)
1810 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1812 contact = g_hash_table_lookup (contacts_table, tp_contact);
1814 if (contact == NULL)
1816 contact = empathy_contact_new (tp_contact);
1818 /* The hash table does not keep any ref.
1819 * contact keeps a ref to tp_contact, and is removed from the table in
1820 * contact_dispose() */
1821 g_hash_table_insert (contacts_table, tp_contact, contact);
1825 g_object_ref (contact);
1832 presence_cmp_func (EmpathyContact *a,
1835 FolksPresenceDetails *presence_a, *presence_b;
1837 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1838 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1840 /* We negate the result because we're sorting in reverse order (i.e. such that
1841 * the Personas with the highest presence are at the beginning of the list. */
1842 return -folks_presence_details_typecmp (
1843 folks_presence_details_get_presence_type (presence_a),
1844 folks_presence_details_get_presence_type (presence_b));
1848 voip_cmp_func (EmpathyContact *a,
1851 gboolean has_audio_a, has_audio_b;
1852 gboolean has_video_a, has_video_b;
1854 has_audio_a = empathy_contact_can_voip_audio (a);
1855 has_audio_b = empathy_contact_can_voip_audio (b);
1856 has_video_a = empathy_contact_can_voip_video (a);
1857 has_video_b = empathy_contact_can_voip_video (b);
1859 /* First check video */
1860 if (has_video_a == has_video_b)
1862 /* Use audio to to break tie */
1863 if (has_audio_a == has_audio_b)
1865 else if (has_audio_a)
1870 else if (has_video_a)
1877 ft_cmp_func (EmpathyContact *a,
1880 gboolean can_send_files_a, can_send_files_b;
1882 can_send_files_a = empathy_contact_can_send_files (a);
1883 can_send_files_b = empathy_contact_can_send_files (b);
1885 if (can_send_files_a == can_send_files_b)
1887 else if (can_send_files_a)
1894 rfb_stream_tube_cmp_func (EmpathyContact *a,
1897 gboolean rfb_a, rfb_b;
1899 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1900 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1910 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1911 * the same presence, prefer the one which can do both audio *and* video calls,
1912 * over the one which can only do one of the two. */
1914 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1916 gint presence_sort = presence_cmp_func (a, b);
1918 if (presence_sort != 0)
1919 return presence_sort;
1921 return voip_cmp_func (a, b);
1924 /* Sort by presence as with presence_cmp_func() and then break ties using the
1925 * most "capable" individual. So users will be able to pick more actions on
1926 * the contact in the "Contact" menu of the chat window. */
1928 chat_sort_func (EmpathyContact *a,
1933 result = presence_cmp_func (a, b);
1937 /* Prefer individual supporting file transfer */
1938 result = ft_cmp_func (a, b);
1942 /* Check audio/video capabilities */
1943 result = voip_cmp_func (a, b);
1947 /* Check 'Share my destop' feature */
1948 return rfb_stream_tube_cmp_func (a, b);
1952 get_sort_func_for_action (EmpathyActionType action_type)
1954 switch (action_type)
1956 case EMPATHY_ACTION_AUDIO_CALL:
1957 case EMPATHY_ACTION_VIDEO_CALL:
1958 return (GCompareFunc) voip_sort_func;
1959 case EMPATHY_ACTION_CHAT:
1960 return (GCompareFunc) chat_sort_func;
1961 case EMPATHY_ACTION_VIEW_LOGS:
1962 case EMPATHY_ACTION_SEND_FILE:
1963 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1965 return (GCompareFunc) presence_cmp_func;
1970 * empathy_contact_dup_best_for_action:
1971 * @individual: a #FolksIndividual
1972 * @action_type: the type of action to be performed on the contact
1974 * Chooses a #FolksPersona from the given @individual which is best-suited for
1975 * the given @action_type. "Best-suited" is determined by choosing the persona
1976 * with the highest presence out of all the personas which can perform the given
1977 * @action_type (e.g. are capable of video calling).
1979 * Return value: an #EmpathyContact for the best persona, or %NULL;
1980 * unref with g_object_unref()
1983 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1984 EmpathyActionType action_type)
1989 EmpathyContact *best_contact = NULL;
1991 /* Build a list of EmpathyContacts that we can sort */
1992 personas = folks_individual_get_personas (individual);
1995 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1996 while (gee_iterator_next (iter))
1998 FolksPersona *persona = gee_iterator_get (iter);
1999 TpContact *tp_contact;
2000 EmpathyContact *contact = NULL;
2002 if (!empathy_folks_persona_is_interesting (persona))
2005 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
2006 contact = empathy_contact_dup_from_tp_contact (tp_contact);
2007 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
2009 /* Only choose the contact if they're actually capable of the specified
2011 if (empathy_contact_can_do_action (contact, action_type))
2012 contacts = g_list_prepend (contacts, g_object_ref (contact));
2015 g_clear_object (&contact);
2016 g_clear_object (&persona);
2018 g_clear_object (&iter);
2020 /* Sort the contacts by some heuristic based on the action type, then take
2021 * the top contact. */
2022 if (contacts != NULL)
2024 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2025 best_contact = g_object_ref (contacts->data);
2028 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2029 g_list_free (contacts);
2031 return best_contact;