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)
195 g_hash_table_remove (contacts_table, priv->tp_contact);
196 g_signal_handlers_disconnect_by_func (priv->tp_contact,
197 tp_contact_notify_cb, object);
198 g_object_unref (priv->tp_contact);
200 priv->tp_contact = NULL;
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_boxed ("avatar",
328 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (object_class,
332 g_param_spec_uint ("presence",
334 "Presence of contact",
335 TP_CONNECTION_PRESENCE_TYPE_UNSET,
336 NUM_TP_CONNECTION_PRESENCE_TYPES,
337 TP_CONNECTION_PRESENCE_TYPE_UNSET,
338 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (object_class,
341 PROP_PRESENCE_MESSAGE,
342 g_param_spec_string ("presence-message",
343 "Contact presence message",
344 "Presence message of contact",
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (object_class,
350 g_param_spec_uint ("handle",
352 "The handle of the contact",
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (object_class,
360 g_param_spec_flags ("capabilities",
361 "Contact Capabilities",
362 "Capabilities of the contact",
363 EMPATHY_TYPE_CAPABILITIES,
364 EMPATHY_CAPABILITIES_UNKNOWN,
365 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (object_class,
369 g_param_spec_boolean ("is-user",
371 "Is contact the user",
373 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (object_class,
378 g_param_spec_boxed ("location",
380 "Physical location of the contact",
382 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
384 g_object_class_install_property (object_class,
386 g_param_spec_boxed ("client-types",
387 "Contact client types",
388 "Client types of the contact",
390 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
392 signals[PRESENCE_CHANGED] =
393 g_signal_new ("presence-changed",
394 G_TYPE_FROM_CLASS (class),
398 _empathy_marshal_VOID__UINT_UINT,
403 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
407 empathy_contact_init (EmpathyContact *contact)
409 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
410 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
412 contact->priv = priv;
414 priv->location = NULL;
415 priv->client_types = NULL;
420 contact_finalize (GObject *object)
422 EmpathyContactPriv *priv;
424 priv = GET_PRIV (object);
426 DEBUG ("finalize: %p", object);
428 g_clear_object (&priv->groups);
429 g_free (priv->alias);
431 g_strfreev (priv->client_types);
433 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
437 empathy_contact_set_capabilities (EmpathyContact *contact,
438 EmpathyCapabilities capabilities)
440 EmpathyContactPriv *priv;
442 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
444 priv = GET_PRIV (contact);
446 if (priv->capabilities == capabilities)
449 priv->capabilities = capabilities;
451 g_object_notify (G_OBJECT (contact), "capabilities");
455 empathy_contact_set_id (EmpathyContact *contact,
458 EmpathyContactPriv *priv;
460 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
461 g_return_if_fail (id != NULL);
463 priv = GET_PRIV (contact);
465 /* We temporally ref the contact because it could be destroyed
466 * during the signal emition */
467 g_object_ref (contact);
468 if (tp_strdiff (id, priv->id))
471 priv->id = g_strdup (id);
473 g_object_notify (G_OBJECT (contact), "id");
474 if (EMP_STR_EMPTY (priv->alias))
475 g_object_notify (G_OBJECT (contact), "alias");
478 g_object_unref (contact);
482 empathy_contact_set_presence (EmpathyContact *contact,
483 TpConnectionPresenceType presence)
485 EmpathyContactPriv *priv;
486 TpConnectionPresenceType old_presence;
488 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
490 priv = GET_PRIV (contact);
492 if (presence == priv->presence)
495 old_presence = priv->presence;
496 priv->presence = presence;
498 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
500 g_object_notify (G_OBJECT (contact), "presence");
504 empathy_contact_set_presence_message (EmpathyContact *contact,
505 const gchar *message)
507 EmpathyContactPriv *priv = GET_PRIV (contact);
509 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
511 if (priv->persona != NULL)
513 folks_presence_details_set_presence_message (
514 FOLKS_PRESENCE_DETAILS (priv->persona), message);
519 empathy_contact_set_handle (EmpathyContact *contact,
522 EmpathyContactPriv *priv;
524 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
526 priv = GET_PRIV (contact);
528 g_object_ref (contact);
529 if (handle != priv->handle)
531 priv->handle = handle;
532 g_object_notify (G_OBJECT (contact), "handle");
534 g_object_unref (contact);
538 contact_get_property (GObject *object,
543 EmpathyContact *contact = EMPATHY_CONTACT (object);
547 case PROP_TP_CONTACT:
548 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
551 g_value_set_object (value, empathy_contact_get_account (contact));
554 g_value_set_object (value, empathy_contact_get_persona (contact));
557 g_value_set_string (value, empathy_contact_get_id (contact));
560 g_value_set_string (value, empathy_contact_get_alias (contact));
563 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
566 g_value_set_uint (value, empathy_contact_get_presence (contact));
568 case PROP_PRESENCE_MESSAGE:
569 g_value_set_string (value, empathy_contact_get_presence_message (contact));
572 g_value_set_uint (value, empathy_contact_get_handle (contact));
574 case PROP_CAPABILITIES:
575 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
578 g_value_set_boolean (value, empathy_contact_is_user (contact));
581 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
587 contact_set_property (GObject *object,
592 EmpathyContact *contact = EMPATHY_CONTACT (object);
593 EmpathyContactPriv *priv = GET_PRIV (object);
597 case PROP_TP_CONTACT:
598 priv->tp_contact = g_value_dup_object (value);
601 g_assert (priv->account == NULL);
602 priv->account = g_value_dup_object (value);
605 empathy_contact_set_persona (contact, g_value_get_object (value));
608 empathy_contact_set_id (contact, g_value_get_string (value));
611 empathy_contact_set_alias (contact, g_value_get_string (value));
614 empathy_contact_set_presence (contact, g_value_get_uint (value));
616 case PROP_PRESENCE_MESSAGE:
617 empathy_contact_set_presence_message (contact, g_value_get_string (value));
620 empathy_contact_set_handle (contact, g_value_get_uint (value));
622 case PROP_CAPABILITIES:
623 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
626 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
629 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
634 static EmpathyContact *
635 empathy_contact_new (TpContact *tp_contact)
637 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
639 return g_object_new (EMPATHY_TYPE_CONTACT,
640 "tp-contact", tp_contact,
645 empathy_contact_from_tpl_contact (TpAccount *account,
646 TplEntity *tpl_entity)
648 EmpathyContact *retval;
651 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
653 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
655 retval = g_object_new (EMPATHY_TYPE_CONTACT,
656 "id", tpl_entity_get_identifier (tpl_entity),
657 "alias", tpl_entity_get_alias (tpl_entity),
662 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
663 contact_load_avatar_cache (retval,
664 tpl_entity_get_avatar_token (tpl_entity));
670 empathy_contact_get_tp_contact (EmpathyContact *contact)
672 EmpathyContactPriv *priv;
674 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
676 priv = GET_PRIV (contact);
678 return priv->tp_contact;
682 empathy_contact_get_id (EmpathyContact *contact)
684 EmpathyContactPriv *priv;
686 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
688 priv = GET_PRIV (contact);
690 if (priv->tp_contact != NULL)
691 return tp_contact_get_identifier (priv->tp_contact);
697 empathy_contact_get_alias (EmpathyContact *contact)
699 EmpathyContactPriv *priv;
702 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
704 priv = GET_PRIV (contact);
706 if (priv->tp_contact != NULL)
707 alias = tp_contact_get_alias (priv->tp_contact);
711 if (!EMP_STR_EMPTY (alias))
714 return empathy_contact_get_id (contact);
718 empathy_contact_set_alias (EmpathyContact *contact,
721 EmpathyContactPriv *priv;
722 FolksPersona *persona;
724 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
726 priv = GET_PRIV (contact);
728 g_object_ref (contact);
730 /* Set the alias on the persona if possible */
731 persona = empathy_contact_get_persona (contact);
732 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
734 DEBUG ("Setting alias for contact %s to %s",
735 empathy_contact_get_id (contact), alias);
737 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
740 if (tp_strdiff (alias, priv->alias))
742 g_free (priv->alias);
743 priv->alias = g_strdup (alias);
744 g_object_notify (G_OBJECT (contact), "alias");
747 g_object_unref (contact);
751 groups_change_group_cb (GObject *source,
752 GAsyncResult *result,
755 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
756 GError *error = NULL;
758 folks_group_details_change_group_finish (group_details, result, &error);
761 g_warning ("failed to change group: %s", error->message);
762 g_clear_error (&error);
767 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
770 EmpathyContactPriv *priv;
771 FolksPersona *persona;
773 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
774 g_return_if_fail (group != NULL);
776 priv = GET_PRIV (contact);
778 /* Normally pass through the changes to the persona */
779 persona = empathy_contact_get_persona (contact);
782 if (FOLKS_IS_GROUP_DETAILS (persona))
783 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
784 is_member, groups_change_group_cb, contact);
788 /* If the persona doesn't exist yet, we have to cache the changes until it
790 if (priv->groups == NULL)
792 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
793 g_free, g_str_hash, g_str_equal);
796 gee_collection_add (GEE_COLLECTION (priv->groups), group);
800 empathy_contact_get_avatar (EmpathyContact *contact)
802 EmpathyContactPriv *priv;
804 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
806 priv = GET_PRIV (contact);
812 contact_set_avatar (EmpathyContact *contact,
813 EmpathyAvatar *avatar)
815 EmpathyContactPriv *priv;
817 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
819 priv = GET_PRIV (contact);
821 if (priv->avatar == avatar)
826 empathy_avatar_unref (priv->avatar);
831 priv->avatar = empathy_avatar_ref (avatar);
833 g_object_notify (G_OBJECT (contact), "avatar");
837 empathy_contact_get_account (EmpathyContact *contact)
839 EmpathyContactPriv *priv;
841 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
843 priv = GET_PRIV (contact);
845 if (priv->account == NULL && priv->tp_contact != NULL)
847 TpConnection *connection;
849 /* FIXME: This assume the account manager already exists */
850 connection = tp_contact_get_connection (priv->tp_contact);
852 g_object_ref (empathy_get_account_for_connection (connection));
855 return priv->account;
859 empathy_contact_get_persona (EmpathyContact *contact)
861 EmpathyContactPriv *priv;
863 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
865 priv = GET_PRIV (contact);
867 if (priv->persona == NULL && priv->tp_contact != NULL)
869 /* FIXME: This is disgustingly slow */
870 /* Query for the persona */
871 EmpathyIndividualManager *manager;
872 GList *individuals, *l;
874 manager = empathy_individual_manager_dup_singleton ();
875 individuals = empathy_individual_manager_get_members (manager);
877 for (l = individuals; l != NULL; l = l->next)
879 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
882 gboolean persona_found = FALSE;
884 personas = folks_individual_get_personas (individual);
885 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
886 while (!persona_found && gee_iterator_next (iter))
888 TpfPersona *persona = gee_iterator_get (iter);
890 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
892 TpContact *tp_contact = tpf_persona_get_contact (persona);
894 if (tp_contact == priv->tp_contact)
896 /* Found the right persona */
897 empathy_contact_set_persona (contact,
898 (FolksPersona *) persona);
899 persona_found = TRUE;
901 g_clear_object (&persona);
904 g_clear_object (&iter);
907 g_list_free (individuals);
908 g_object_unref (manager);
911 return priv->persona;
915 empathy_contact_set_persona (EmpathyContact *contact,
916 FolksPersona *persona)
918 EmpathyContactPriv *priv;
920 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
921 g_return_if_fail (TPF_IS_PERSONA (persona));
923 priv = GET_PRIV (contact);
925 if (persona == priv->persona)
928 if (priv->persona != NULL)
930 g_signal_handlers_disconnect_by_func (priv->persona,
931 folks_persona_notify_cb, contact);
932 g_object_unref (priv->persona);
934 priv->persona = g_object_ref (persona);
936 g_signal_connect (priv->persona, "notify",
937 G_CALLBACK (folks_persona_notify_cb), contact);
939 g_object_notify (G_OBJECT (contact), "persona");
941 /* Set the persona's alias, since ours could've been set using
942 * empathy_contact_set_alias() before we had a persona; this happens when
943 * adding a contact. */
944 if (priv->alias != NULL)
945 empathy_contact_set_alias (contact, priv->alias);
947 /* Set the persona's groups */
948 if (priv->groups != NULL)
950 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
951 GEE_SET (priv->groups));
952 g_object_unref (priv->groups);
958 empathy_contact_get_connection (EmpathyContact *contact)
960 EmpathyContactPriv *priv;
962 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
964 priv = GET_PRIV (contact);
966 if (priv->tp_contact != NULL)
967 return tp_contact_get_connection (priv->tp_contact);
972 TpConnectionPresenceType
973 empathy_contact_get_presence (EmpathyContact *contact)
975 EmpathyContactPriv *priv;
977 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
978 TP_CONNECTION_PRESENCE_TYPE_UNSET);
980 priv = GET_PRIV (contact);
982 if (priv->tp_contact != NULL)
983 return tp_contact_get_presence_type (priv->tp_contact);
985 return priv->presence;
989 empathy_contact_get_presence_message (EmpathyContact *contact)
991 EmpathyContactPriv *priv;
993 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
995 priv = GET_PRIV (contact);
997 if (priv->persona != NULL)
998 return folks_presence_details_get_presence_message (
999 FOLKS_PRESENCE_DETAILS (priv->persona));
1001 if (priv->tp_contact != NULL)
1002 return tp_contact_get_presence_message (priv->tp_contact);
1008 empathy_contact_get_handle (EmpathyContact *contact)
1010 EmpathyContactPriv *priv;
1012 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1014 priv = GET_PRIV (contact);
1016 if (priv->tp_contact != NULL)
1017 return tp_contact_get_handle (priv->tp_contact);
1019 return priv->handle;
1023 empathy_contact_get_capabilities (EmpathyContact *contact)
1025 EmpathyContactPriv *priv;
1027 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1029 priv = GET_PRIV (contact);
1031 return priv->capabilities;
1035 empathy_contact_is_user (EmpathyContact *contact)
1037 EmpathyContactPriv *priv;
1039 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1041 priv = GET_PRIV (contact);
1043 return priv->is_user;
1047 empathy_contact_set_is_user (EmpathyContact *contact,
1050 EmpathyContactPriv *priv;
1052 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1054 priv = GET_PRIV (contact);
1056 if (priv->is_user == is_user)
1059 priv->is_user = is_user;
1061 g_object_notify (G_OBJECT (contact), "is-user");
1065 empathy_contact_is_online (EmpathyContact *contact)
1067 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1069 switch (empathy_contact_get_presence (contact))
1071 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1072 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1073 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1075 /* Contacts without presence are considered online so we can display IRC
1076 * contacts in rooms. */
1077 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1078 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1079 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1080 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1081 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1082 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1089 empathy_contact_get_status (EmpathyContact *contact)
1091 const gchar *message;
1093 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1095 message = empathy_contact_get_presence_message (contact);
1096 if (!EMP_STR_EMPTY (message))
1099 return empathy_presence_get_default_message (
1100 empathy_contact_get_presence (contact));
1104 empathy_contact_can_sms (EmpathyContact *contact)
1106 EmpathyContactPriv *priv;
1108 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1110 priv = GET_PRIV (contact);
1112 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1116 empathy_contact_can_voip (EmpathyContact *contact)
1118 EmpathyContactPriv *priv;
1120 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1122 priv = GET_PRIV (contact);
1124 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1125 EMPATHY_CAPABILITIES_VIDEO);
1129 empathy_contact_can_voip_audio (EmpathyContact *contact)
1131 EmpathyContactPriv *priv;
1133 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1135 priv = GET_PRIV (contact);
1137 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1141 empathy_contact_can_voip_video (EmpathyContact *contact)
1143 EmpathyContactPriv *priv;
1145 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1147 priv = GET_PRIV (contact);
1149 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1153 empathy_contact_can_send_files (EmpathyContact *contact)
1155 EmpathyContactPriv *priv;
1157 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1159 priv = GET_PRIV (contact);
1161 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1165 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1167 EmpathyContactPriv *priv;
1169 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1171 priv = GET_PRIV (contact);
1173 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1177 contact_has_log (EmpathyContact *contact)
1179 TplLogManager *manager;
1183 manager = tpl_log_manager_dup_singleton ();
1184 entity = tpl_entity_new (empathy_contact_get_id (contact),
1185 TPL_ENTITY_CONTACT, NULL, NULL);
1187 have_log = tpl_log_manager_exists (manager,
1188 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1190 g_object_unref (entity);
1191 g_object_unref (manager);
1197 empathy_contact_can_do_action (EmpathyContact *self,
1198 EmpathyActionType action_type)
1200 gboolean sensitivity = FALSE;
1202 switch (action_type)
1204 case EMPATHY_ACTION_CHAT:
1207 case EMPATHY_ACTION_SMS:
1208 sensitivity = empathy_contact_can_sms (self);
1210 case EMPATHY_ACTION_AUDIO_CALL:
1211 sensitivity = empathy_contact_can_voip_audio (self);
1213 case EMPATHY_ACTION_VIDEO_CALL:
1214 sensitivity = empathy_contact_can_voip_video (self);
1216 case EMPATHY_ACTION_VIEW_LOGS:
1217 sensitivity = contact_has_log (self);
1219 case EMPATHY_ACTION_SEND_FILE:
1220 sensitivity = empathy_contact_can_send_files (self);
1222 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1223 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1226 g_assert_not_reached ();
1229 return (sensitivity ? TRUE : FALSE);
1233 contact_get_avatar_filename (EmpathyContact *contact,
1239 gchar *token_escaped;
1241 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1244 token_escaped = tp_escape_as_identifier (token);
1245 account = empathy_contact_get_account (contact);
1247 avatar_path = g_build_filename (g_get_user_cache_dir (),
1250 tp_account_get_connection_manager (account),
1251 tp_account_get_protocol (account),
1253 g_mkdir_with_parents (avatar_path, 0700);
1255 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1257 g_free (token_escaped);
1258 g_free (avatar_path);
1264 contact_load_avatar_cache (EmpathyContact *contact,
1267 EmpathyAvatar *avatar = NULL;
1271 GError *error = NULL;
1273 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1274 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1276 /* Load the avatar from file if it exists */
1277 filename = contact_get_avatar_filename (contact, token);
1278 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1280 if (!g_file_get_contents (filename, &data, &len, &error))
1282 DEBUG ("Failed to load avatar from cache: %s",
1283 error ? error->message : "No error given");
1284 g_clear_error (&error);
1290 DEBUG ("Avatar loaded from %s", filename);
1291 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1292 contact_set_avatar (contact, avatar);
1293 empathy_avatar_unref (avatar);
1299 return data != NULL;
1303 empathy_avatar_get_type (void)
1305 static GType type_id = 0;
1309 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1310 (GBoxedCopyFunc) empathy_avatar_ref,
1311 (GBoxedFreeFunc) empathy_avatar_unref);
1318 * empathy_avatar_new:
1319 * @data: the avatar data
1320 * @len: the size of avatar data
1321 * @format: the mime type of the avatar image
1322 * @filename: the filename where the avatar is stored in cache
1324 * Create a #EmpathyAvatar from the provided data.
1326 * Returns: a new #EmpathyAvatar
1329 empathy_avatar_new (const guchar *data,
1331 const gchar *format,
1332 const gchar *filename)
1334 EmpathyAvatar *avatar;
1336 avatar = g_slice_new0 (EmpathyAvatar);
1337 avatar->data = g_memdup (data, len);
1339 avatar->format = g_strdup (format);
1340 avatar->filename = g_strdup (filename);
1341 avatar->refcount = 1;
1347 empathy_avatar_unref (EmpathyAvatar *avatar)
1349 g_return_if_fail (avatar != NULL);
1352 if (avatar->refcount == 0)
1354 g_free (avatar->data);
1355 g_free (avatar->format);
1356 g_free (avatar->filename);
1357 g_slice_free (EmpathyAvatar, avatar);
1362 empathy_avatar_ref (EmpathyAvatar *avatar)
1364 g_return_val_if_fail (avatar != NULL, NULL);
1372 * empathy_avatar_save_to_file:
1373 * @avatar: the avatar
1374 * @filename: name of a file to write avatar to
1375 * @error: return location for a GError, or NULL
1377 * Save the avatar to a file named filename
1379 * Returns: %TRUE on success, %FALSE if an error occurred
1382 empathy_avatar_save_to_file (EmpathyAvatar *self,
1383 const gchar *filename,
1386 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1391 * empathy_contact_get_location:
1392 * @contact: an #EmpathyContact
1394 * Returns the user's location if available. The keys are defined in
1395 * empathy-location.h. If the contact doesn't have location
1396 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1397 * you are done with the #GHashTable.
1399 * It is composed of string keys and GValues. Keys are
1400 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1401 * Example: a "city" key would have "Helsinki" as string GValue,
1402 * a "latitude" would have 65.0 as double GValue.
1404 * Returns: a #GHashTable of location values
1407 empathy_contact_get_location (EmpathyContact *contact)
1409 EmpathyContactPriv *priv;
1411 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1413 priv = GET_PRIV (contact);
1415 return priv->location;
1419 * empathy_contact_set_location:
1420 * @contact: an #EmpathyContact
1421 * @location: a #GHashTable of the location
1423 * Sets the user's location based on the location #GHashTable passed.
1424 * It is composed of string keys and GValues. Keys are
1425 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1426 * Example: a "city" key would have "Helsinki" as string GValue,
1427 * a "latitude" would have 65.0 as double GValue.
1430 empathy_contact_set_location (EmpathyContact *contact,
1431 GHashTable *location)
1433 EmpathyContactPriv *priv;
1435 g_return_if_fail (EMPATHY_CONTACT (contact));
1436 g_return_if_fail (location != NULL);
1438 priv = GET_PRIV (contact);
1440 if (priv->location != NULL)
1441 g_hash_table_unref (priv->location);
1443 priv->location = g_hash_table_ref (location);
1445 update_geocode (contact);
1447 g_object_notify (G_OBJECT (contact), "location");
1450 const gchar * const *
1451 empathy_contact_get_client_types (EmpathyContact *contact)
1453 EmpathyContactPriv *priv;
1455 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1457 priv = GET_PRIV (contact);
1459 return (const gchar * const *) priv->client_types;
1463 contact_set_client_types (EmpathyContact *contact,
1464 const gchar * const *client_types)
1466 EmpathyContactPriv *priv = GET_PRIV (contact);
1468 if (priv->client_types != NULL)
1469 g_strfreev (priv->client_types);
1471 priv->client_types = g_strdupv ((gchar **) client_types);
1472 g_object_notify (G_OBJECT (contact), "client-types");
1476 * empathy_contact_equal:
1477 * @contact1: an #EmpathyContact
1478 * @contact2: an #EmpathyContact
1480 * Returns FALSE if one of the contacts is NULL but the other is not.
1481 * Otherwise returns TRUE if both pointer are equal or if they bith
1482 * refer to the same id.
1483 * It's only necessary to call this function if your contact objects
1484 * come from logs where contacts are created dynamically and comparing
1485 * pointers is not enough.
1488 empathy_contact_equal (gconstpointer contact1,
1489 gconstpointer contact2)
1496 if ((contact1 == NULL) != (contact2 == NULL)) {
1499 if (contact1 == contact2) {
1502 c1 = EMPATHY_CONTACT (contact1);
1503 c2 = EMPATHY_CONTACT (contact2);
1504 id1 = empathy_contact_get_id (c1);
1505 id2 = empathy_contact_get_id (c2);
1506 if (!tp_strdiff (id1, id2)) {
1513 /* This callback is called by geocode-glib when it found a position
1514 * for the given address. A position is necessary for a contact
1515 * to show up on the map
1518 geocode_cb (GObject *source,
1519 GAsyncResult *result,
1522 EmpathyContact *contact = user_data;
1523 EmpathyContactPriv *priv = GET_PRIV (contact);
1524 GError *error = NULL;
1525 GHashTable *new_location;
1526 GHashTable *resolved;
1527 gdouble latitude, longitude;
1529 if (priv->location == NULL)
1532 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1535 if (resolved == NULL)
1537 DEBUG ("Failed to resolve geocode: %s", error->message);
1538 g_error_free (error);
1542 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1545 new_location = tp_asv_new (
1546 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1547 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1550 DEBUG ("\t - Latitude: %f", latitude);
1551 DEBUG ("\t - Longitude: %f", longitude);
1553 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1554 * the values we just set. */
1555 tp_g_hash_table_update (new_location, priv->location,
1556 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1558 /* Don't change the accuracy as we used an address to get this position */
1559 g_hash_table_unref (priv->location);
1560 priv->location = new_location;
1561 g_object_notify ((GObject *) contact, "location");
1564 tp_clear_pointer (&result, g_hash_table_unref);
1565 g_object_unref (contact);
1569 update_geocode (EmpathyContact *contact)
1571 GeocodeObject *geocode;
1572 GHashTable *location;
1574 location = empathy_contact_get_location (contact);
1575 if (location == NULL)
1578 /* No need to search for position if contact published it */
1579 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1580 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1583 geocode = geocode_object_new_for_params (location);
1585 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1586 g_object_ref (contact));
1588 g_object_unref (geocode);
1592 static EmpathyCapabilities
1593 tp_caps_to_capabilities (TpCapabilities *caps)
1595 EmpathyCapabilities capabilities = 0;
1599 classes = tp_capabilities_get_channel_classes (caps);
1601 for (i = 0; i < classes->len; i++)
1603 GValueArray *class_struct;
1604 GHashTable *fixed_prop;
1606 TpHandleType handle_type;
1607 const gchar *chan_type;
1609 class_struct = g_ptr_array_index (classes, i);
1610 tp_value_array_unpack (class_struct, 2,
1614 handle_type = tp_asv_get_uint32 (fixed_prop,
1615 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1616 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1619 chan_type = tp_asv_get_string (fixed_prop,
1620 TP_PROP_CHANNEL_CHANNEL_TYPE);
1622 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1624 capabilities |= EMPATHY_CAPABILITIES_FT;
1626 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1628 const gchar *service;
1630 service = tp_asv_get_string (fixed_prop,
1631 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1633 if (!tp_strdiff (service, "rfb"))
1634 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1636 else if (!tp_strdiff (chan_type,
1637 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1641 for (j = 0; allowed_prop[j] != NULL; j++)
1643 if (!tp_strdiff (allowed_prop[j],
1644 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1645 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1646 else if (!tp_strdiff (allowed_prop[j],
1647 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1648 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1651 if (tp_asv_get_boolean (fixed_prop,
1652 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, NULL))
1653 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1654 if (tp_asv_get_boolean (fixed_prop,
1655 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, NULL))
1656 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1658 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1660 if (tp_asv_get_boolean (fixed_prop,
1661 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL))
1662 capabilities |= EMPATHY_CAPABILITIES_SMS;
1664 else if (!tp_strdiff (chan_type,
1665 TPY_IFACE_CHANNEL_TYPE_CALL))
1669 if (tp_asv_get_boolean (fixed_prop,
1670 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, NULL))
1671 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1673 if (tp_asv_get_boolean (fixed_prop,
1674 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, NULL))
1675 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1677 for (j = 0; allowed_prop[j] != NULL; j++)
1679 if (!tp_strdiff (allowed_prop[j],
1680 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO))
1681 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1682 else if (!tp_strdiff (allowed_prop[j],
1683 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO))
1684 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1689 return capabilities;
1693 set_capabilities_from_tp_caps (EmpathyContact *self,
1694 TpCapabilities *caps)
1696 EmpathyCapabilities capabilities;
1701 capabilities = tp_caps_to_capabilities (caps);
1702 empathy_contact_set_capabilities (self, capabilities);
1706 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1708 EmpathyContactPriv *priv = GET_PRIV (contact);
1712 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1713 file = tp_contact_get_avatar_file (priv->tp_contact);
1717 EmpathyAvatar *avatar;
1722 g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
1723 path = g_file_get_path (file);
1725 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1727 contact_set_avatar (contact, avatar);
1728 empathy_avatar_unref (avatar);
1734 contact_set_avatar (contact, NULL);
1739 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1741 EmpathyContact *contact = NULL;
1743 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1745 if (contacts_table == NULL)
1746 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1748 contact = g_hash_table_lookup (contacts_table, tp_contact);
1750 if (contact == NULL)
1752 contact = empathy_contact_new (tp_contact);
1754 /* The hash table does not keep any ref.
1755 * contact keeps a ref to tp_contact, and is removed from the table in
1756 * contact_dispose() */
1757 g_hash_table_insert (contacts_table, tp_contact, contact);
1761 g_object_ref (contact);
1768 presence_cmp_func (EmpathyContact *a,
1771 FolksPresenceDetails *presence_a, *presence_b;
1773 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1774 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1776 /* We negate the result because we're sorting in reverse order (i.e. such that
1777 * the Personas with the highest presence are at the beginning of the list. */
1778 return -folks_presence_details_typecmp (
1779 folks_presence_details_get_presence_type (presence_a),
1780 folks_presence_details_get_presence_type (presence_b));
1784 voip_cmp_func (EmpathyContact *a,
1787 gboolean has_audio_a, has_audio_b;
1788 gboolean has_video_a, has_video_b;
1790 has_audio_a = empathy_contact_can_voip_audio (a);
1791 has_audio_b = empathy_contact_can_voip_audio (b);
1792 has_video_a = empathy_contact_can_voip_video (a);
1793 has_video_b = empathy_contact_can_voip_video (b);
1795 /* First check video */
1796 if (has_video_a == has_video_b)
1798 /* Use audio to to break tie */
1799 if (has_audio_a == has_audio_b)
1801 else if (has_audio_a)
1806 else if (has_video_a)
1813 ft_cmp_func (EmpathyContact *a,
1816 gboolean can_send_files_a, can_send_files_b;
1818 can_send_files_a = empathy_contact_can_send_files (a);
1819 can_send_files_b = empathy_contact_can_send_files (b);
1821 if (can_send_files_a == can_send_files_b)
1823 else if (can_send_files_a)
1830 rfb_stream_tube_cmp_func (EmpathyContact *a,
1833 gboolean rfb_a, rfb_b;
1835 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1836 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1846 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1847 * the same presence, prefer the one which can do both audio *and* video calls,
1848 * over the one which can only do one of the two. */
1850 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1852 gint presence_sort = presence_cmp_func (a, b);
1854 if (presence_sort != 0)
1855 return presence_sort;
1857 return voip_cmp_func (a, b);
1860 /* Sort by presence as with presence_cmp_func() and then break ties using the
1861 * most "capable" individual. So users will be able to pick more actions on
1862 * the contact in the "Contact" menu of the chat window. */
1864 chat_sort_func (EmpathyContact *a,
1869 result = presence_cmp_func (a, b);
1873 /* Prefer individual supporting file transfer */
1874 result = ft_cmp_func (a, b);
1878 /* Check audio/video capabilities */
1879 result = voip_cmp_func (a, b);
1883 /* Check 'Share my destop' feature */
1884 return rfb_stream_tube_cmp_func (a, b);
1888 get_sort_func_for_action (EmpathyActionType action_type)
1890 switch (action_type)
1892 case EMPATHY_ACTION_AUDIO_CALL:
1893 case EMPATHY_ACTION_VIDEO_CALL:
1894 return (GCompareFunc) voip_sort_func;
1895 case EMPATHY_ACTION_CHAT:
1896 return (GCompareFunc) chat_sort_func;
1897 case EMPATHY_ACTION_VIEW_LOGS:
1898 case EMPATHY_ACTION_SEND_FILE:
1899 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1901 return (GCompareFunc) presence_cmp_func;
1906 * empathy_contact_dup_best_for_action:
1907 * @individual: a #FolksIndividual
1908 * @action_type: the type of action to be performed on the contact
1910 * Chooses a #FolksPersona from the given @individual which is best-suited for
1911 * the given @action_type. "Best-suited" is determined by choosing the persona
1912 * with the highest presence out of all the personas which can perform the given
1913 * @action_type (e.g. are capable of video calling).
1915 * Return value: an #EmpathyContact for the best persona, or %NULL;
1916 * unref with g_object_unref()
1919 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1920 EmpathyActionType action_type)
1925 EmpathyContact *best_contact = NULL;
1927 /* Build a list of EmpathyContacts that we can sort */
1928 personas = folks_individual_get_personas (individual);
1931 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1932 while (gee_iterator_next (iter))
1934 FolksPersona *persona = gee_iterator_get (iter);
1935 TpContact *tp_contact;
1936 EmpathyContact *contact = NULL;
1938 if (!empathy_folks_persona_is_interesting (persona))
1941 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1942 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1943 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
1945 /* Only choose the contact if they're actually capable of the specified
1947 if (empathy_contact_can_do_action (contact, action_type))
1948 contacts = g_list_prepend (contacts, g_object_ref (contact));
1951 g_clear_object (&contact);
1952 g_clear_object (&persona);
1954 g_clear_object (&iter);
1956 /* Sort the contacts by some heuristic based on the action type, then take
1957 * the top contact. */
1958 if (contacts != NULL)
1960 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
1961 best_contact = g_object_ref (contacts->data);
1964 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
1965 g_list_free (contacts);
1967 return best_contact;