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 <folks/folks.h>
29 #include <folks/folks-telepathy.h>
32 #include <geocode-glib/geocode-glib.h>
35 #include "empathy-contact.h"
36 #include "empathy-camera-monitor.h"
37 #include "empathy-individual-manager.h"
38 #include "empathy-utils.h"
39 #include "empathy-enum-types.h"
40 #include "empathy-location.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
43 #include "empathy-debug.h"
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
47 TpContact *tp_contact;
49 FolksPersona *persona;
53 EmpathyAvatar *avatar;
54 TpConnectionPresenceType presence;
56 EmpathyCapabilities capabilities;
58 /* Location is composed of string keys and GValues.
59 * Example: a "city" key would have "Helsinki" as string GValue,
60 * a "latitude" would have 65.0 as double GValue.
62 * This is a super set of the location stored in TpContact as we can try add
63 * more fields by searching the address using geoclue.
70 static void contact_finalize (GObject *object);
71 static void contact_get_property (GObject *object, guint param_id,
72 GValue *value, GParamSpec *pspec);
73 static void contact_set_property (GObject *object, guint param_id,
74 const GValue *value, GParamSpec *pspec);
77 static void update_geocode (EmpathyContact *contact);
80 static void empathy_contact_set_location (EmpathyContact *contact,
81 GHashTable *location);
83 static void contact_set_client_types (EmpathyContact *contact,
84 const gchar * const *types);
86 static void set_capabilities_from_tp_caps (EmpathyContact *self,
87 TpCapabilities *caps);
89 static void contact_set_avatar (EmpathyContact *contact,
90 EmpathyAvatar *avatar);
91 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
92 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
95 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
108 PROP_PRESENCE_MESSAGE,
121 static guint signals[LAST_SIGNAL];
123 /* TpContact* -> EmpathyContact*, both borrowed ref */
124 static GHashTable *contacts_table = NULL;
127 tp_contact_notify_cb (TpContact *tp_contact,
131 EmpathyContactPriv *priv = GET_PRIV (contact);
133 /* Forward property notifications */
134 if (!tp_strdiff (param->name, "alias"))
135 g_object_notify (contact, "alias");
136 else if (!tp_strdiff (param->name, "presence-type")) {
137 TpConnectionPresenceType presence;
139 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
140 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
142 priv->presence = presence;
143 g_object_notify (contact, "presence");
145 else if (!tp_strdiff (param->name, "identifier"))
146 g_object_notify (contact, "id");
147 else if (!tp_strdiff (param->name, "handle"))
148 g_object_notify (contact, "handle");
149 else if (!tp_strdiff (param->name, "location"))
151 GHashTable *location;
153 location = tp_contact_get_location (tp_contact);
154 /* This will start a geoclue search to find the address if needed */
155 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
157 else if (!tp_strdiff (param->name, "capabilities"))
159 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
160 tp_contact_get_capabilities (tp_contact));
162 else if (!tp_strdiff (param->name, "avatar-file"))
164 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
166 else if (!tp_strdiff (param->name, "client-types"))
168 contact_set_client_types (EMPATHY_CONTACT (contact),
169 tp_contact_get_client_types (tp_contact));
174 folks_persona_notify_cb (FolksPersona *folks_persona,
178 if (!tp_strdiff (param->name, "presence-message"))
179 g_object_notify (contact, "presence-message");
183 contact_dispose (GObject *object)
185 EmpathyContactPriv *priv = GET_PRIV (object);
187 if (priv->tp_contact != NULL)
189 g_signal_handlers_disconnect_by_func (priv->tp_contact,
190 tp_contact_notify_cb, object);
192 tp_clear_object (&priv->tp_contact);
195 g_object_unref (priv->account);
196 priv->account = NULL;
200 g_signal_handlers_disconnect_by_func (priv->persona,
201 folks_persona_notify_cb, object);
202 g_object_unref (priv->persona);
204 priv->persona = NULL;
206 if (priv->avatar != NULL)
208 empathy_avatar_unref (priv->avatar);
212 if (priv->location != NULL)
214 g_hash_table_unref (priv->location);
215 priv->location = NULL;
218 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
222 contact_constructed (GObject *object)
224 EmpathyContact *contact = (EmpathyContact *) object;
225 EmpathyContactPriv *priv = GET_PRIV (contact);
226 GHashTable *location;
227 TpContact *self_contact;
228 const gchar * const *client_types;
230 if (priv->tp_contact == NULL)
233 priv->presence = empathy_contact_get_presence (contact);
235 location = tp_contact_get_location (priv->tp_contact);
236 if (location != NULL)
237 empathy_contact_set_location (contact, location);
239 client_types = tp_contact_get_client_types (priv->tp_contact);
240 if (client_types != NULL)
241 contact_set_client_types (contact, client_types);
243 set_capabilities_from_tp_caps (contact,
244 tp_contact_get_capabilities (priv->tp_contact));
246 contact_set_avatar_from_tp_contact (contact);
248 /* Set is-user property. Note that it could still be the handle is
249 * different from the connection's self handle, in the case the handle
250 * comes from a group interface. */
251 self_contact = tp_connection_get_self_contact (
252 tp_contact_get_connection (priv->tp_contact));
253 empathy_contact_set_is_user (contact, self_contact == priv->tp_contact);
255 g_signal_connect (priv->tp_contact, "notify",
256 G_CALLBACK (tp_contact_notify_cb), contact);
260 empathy_contact_class_init (EmpathyContactClass *class)
262 GObjectClass *object_class;
264 object_class = G_OBJECT_CLASS (class);
266 object_class->finalize = contact_finalize;
267 object_class->dispose = contact_dispose;
268 object_class->get_property = contact_get_property;
269 object_class->set_property = contact_set_property;
270 object_class->constructed = contact_constructed;
272 g_object_class_install_property (object_class,
274 g_param_spec_object ("tp-contact",
276 "The TpContact associated with the contact",
278 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (object_class,
282 g_param_spec_object ("account",
284 "The account 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 ("persona",
292 "The FolksPersona associated with the contact",
294 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (object_class,
298 g_param_spec_string ("id",
300 "String identifying contact",
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (object_class,
306 g_param_spec_string ("alias",
308 "An alias for the contact",
310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (object_class,
314 g_param_spec_string ("logged-alias",
316 "The alias the user had when a message was logged, "
317 "only set when using empathy_contact_from_tpl_contact()",
319 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (object_class,
323 g_param_spec_boxed ("avatar",
327 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (object_class,
331 g_param_spec_uint ("presence",
333 "Presence of contact",
334 TP_CONNECTION_PRESENCE_TYPE_UNSET,
335 NUM_TP_CONNECTION_PRESENCE_TYPES,
336 TP_CONNECTION_PRESENCE_TYPE_UNSET,
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (object_class,
340 PROP_PRESENCE_MESSAGE,
341 g_param_spec_string ("presence-message",
342 "Contact presence message",
343 "Presence message of contact",
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (object_class,
349 g_param_spec_uint ("handle",
351 "The handle of the contact",
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (object_class,
359 g_param_spec_flags ("capabilities",
360 "Contact Capabilities",
361 "Capabilities of the contact",
362 EMPATHY_TYPE_CAPABILITIES,
363 EMPATHY_CAPABILITIES_UNKNOWN,
364 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (object_class,
368 g_param_spec_boolean ("is-user",
370 "Is contact the user",
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (object_class,
377 g_param_spec_boxed ("location",
379 "Physical location of the contact",
381 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (object_class,
385 g_param_spec_boxed ("client-types",
386 "Contact client types",
387 "Client types of the contact",
389 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
391 signals[PRESENCE_CHANGED] =
392 g_signal_new ("presence-changed",
393 G_TYPE_FROM_CLASS (class),
397 g_cclosure_marshal_generic,
402 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
406 empathy_contact_init (EmpathyContact *contact)
408 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
409 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
411 contact->priv = priv;
413 priv->location = NULL;
414 priv->client_types = NULL;
419 contact_finalize (GObject *object)
421 EmpathyContactPriv *priv;
423 priv = GET_PRIV (object);
425 DEBUG ("finalize: %p", object);
427 g_clear_object (&priv->groups);
428 g_free (priv->alias);
429 g_free (priv->logged_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));
562 case PROP_LOGGED_ALIAS:
563 g_value_set_string (value, empathy_contact_get_logged_alias (contact));
566 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
569 g_value_set_uint (value, empathy_contact_get_presence (contact));
571 case PROP_PRESENCE_MESSAGE:
572 g_value_set_string (value, empathy_contact_get_presence_message (contact));
575 g_value_set_uint (value, empathy_contact_get_handle (contact));
577 case PROP_CAPABILITIES:
578 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
581 g_value_set_boolean (value, empathy_contact_is_user (contact));
584 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
590 contact_set_property (GObject *object,
595 EmpathyContact *contact = EMPATHY_CONTACT (object);
596 EmpathyContactPriv *priv = GET_PRIV (object);
600 case PROP_TP_CONTACT:
601 priv->tp_contact = g_value_dup_object (value);
604 g_assert (priv->account == NULL);
605 priv->account = g_value_dup_object (value);
608 empathy_contact_set_persona (contact, g_value_get_object (value));
611 empathy_contact_set_id (contact, g_value_get_string (value));
614 empathy_contact_set_alias (contact, g_value_get_string (value));
616 case PROP_LOGGED_ALIAS:
617 g_assert (priv->logged_alias == NULL);
618 priv->logged_alias = g_value_dup_string (value);
621 empathy_contact_set_presence (contact, g_value_get_uint (value));
623 case PROP_PRESENCE_MESSAGE:
624 empathy_contact_set_presence_message (contact, g_value_get_string (value));
627 empathy_contact_set_handle (contact, g_value_get_uint (value));
629 case PROP_CAPABILITIES:
630 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
633 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
636 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
642 remove_tp_contact (gpointer data,
645 g_hash_table_remove (contacts_table, data);
648 static EmpathyContact *
649 empathy_contact_new (TpContact *tp_contact)
651 EmpathyContact *retval;
653 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
655 retval = g_object_new (EMPATHY_TYPE_CONTACT,
656 "tp-contact", tp_contact,
659 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
671 contact_is_tpl_entity (gpointer key,
675 EmpathyContact *contact = value;
676 FindContactData *data = user_data;
677 TpAccount *account = empathy_contact_get_account (contact);
678 const gchar *path = NULL;
681 path = tp_proxy_get_object_path (account);
683 return !tp_strdiff (empathy_contact_get_id (contact),
684 tpl_entity_get_identifier (data->entity)) &&
685 !tp_strdiff (tp_proxy_get_object_path (data->account), path);
689 get_contacts_cb (GObject *source,
690 GAsyncResult *result,
693 TpWeakRef *wr = user_data;
694 EmpathyContactPriv *priv;
695 EmpathyContact *self;
697 self = tp_weak_ref_dup_object (wr);
701 priv = GET_PRIV (self);
703 g_return_if_fail (priv->tp_contact == NULL);
705 priv->tp_contact = tp_connection_dup_contact_by_id_finish (
706 TP_CONNECTION (source), result, NULL);
707 if (priv->tp_contact == NULL)
710 g_object_notify (G_OBJECT (self), "tp-contact");
712 /* Update capabilities now that we have a TpContact */
713 set_capabilities_from_tp_caps (self,
714 tp_contact_get_capabilities (priv->tp_contact));
717 g_clear_object (&self);
718 tp_weak_ref_destroy (wr);
722 empathy_contact_from_tpl_contact (TpAccount *account,
723 TplEntity *tpl_entity)
725 EmpathyContact *retval;
727 EmpathyContact *existing_contact = NULL;
729 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
731 if (contacts_table != NULL)
733 FindContactData data;
735 data.entity = tpl_entity;
736 data.account = account;
738 existing_contact = g_hash_table_find (contacts_table,
739 contact_is_tpl_entity, &data);
742 if (existing_contact != NULL)
744 retval = g_object_new (EMPATHY_TYPE_CONTACT,
745 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
746 "logged-alias", tpl_entity_get_alias (tpl_entity),
754 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
756 id = tpl_entity_get_identifier (tpl_entity);
758 retval = g_object_new (EMPATHY_TYPE_CONTACT,
760 "alias", tpl_entity_get_alias (tpl_entity),
765 /* Try to get a TpContact associated to have at least contact
766 * capabilities if possible. This is useful for CM supporting calling
767 * offline contacts for example. */
768 conn = tp_account_get_connection (account);
771 TpContactFeature features[] = { TP_CONTACT_FEATURE_CAPABILITIES };
772 conn = tp_account_get_connection (account);
774 tp_connection_dup_contact_by_id_async (conn, id,
775 G_N_ELEMENTS (features), features, get_contacts_cb,
776 tp_weak_ref_new (retval, NULL, NULL));
780 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
781 contact_load_avatar_cache (retval,
782 tpl_entity_get_avatar_token (tpl_entity));
788 empathy_contact_get_tp_contact (EmpathyContact *contact)
790 EmpathyContactPriv *priv;
792 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
794 priv = GET_PRIV (contact);
796 return priv->tp_contact;
800 empathy_contact_get_id (EmpathyContact *contact)
802 EmpathyContactPriv *priv;
804 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
806 priv = GET_PRIV (contact);
808 if (priv->tp_contact != NULL)
809 return tp_contact_get_identifier (priv->tp_contact);
815 empathy_contact_get_alias (EmpathyContact *contact)
817 EmpathyContactPriv *priv;
818 const gchar *alias = NULL;
820 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
822 priv = GET_PRIV (contact);
824 if (!EMP_STR_EMPTY (priv->alias))
826 else if (priv->tp_contact != NULL)
827 alias = tp_contact_get_alias (priv->tp_contact);
829 if (!EMP_STR_EMPTY (alias))
832 return empathy_contact_get_id (contact);
836 empathy_contact_get_logged_alias (EmpathyContact *contact)
838 EmpathyContactPriv *priv;
840 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
842 priv = GET_PRIV (contact);
844 if (priv->logged_alias != NULL)
845 return priv->logged_alias;
847 return empathy_contact_get_alias (contact);
851 empathy_contact_set_alias (EmpathyContact *contact,
854 EmpathyContactPriv *priv;
855 FolksPersona *persona;
857 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
859 priv = GET_PRIV (contact);
861 g_object_ref (contact);
863 /* Set the alias on the persona if possible */
864 persona = empathy_contact_get_persona (contact);
865 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
867 DEBUG ("Setting alias for contact %s to %s",
868 empathy_contact_get_id (contact), alias);
870 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
873 if (tp_strdiff (alias, priv->alias))
875 g_free (priv->alias);
876 priv->alias = g_strdup (alias);
877 g_object_notify (G_OBJECT (contact), "alias");
880 g_object_unref (contact);
884 groups_change_group_cb (GObject *source,
885 GAsyncResult *result,
888 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
889 GError *error = NULL;
891 folks_group_details_change_group_finish (group_details, result, &error);
894 g_warning ("failed to change group: %s", error->message);
895 g_clear_error (&error);
900 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
903 EmpathyContactPriv *priv;
904 FolksPersona *persona;
906 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
907 g_return_if_fail (group != NULL);
909 priv = GET_PRIV (contact);
911 /* Normally pass through the changes to the persona */
912 persona = empathy_contact_get_persona (contact);
915 if (FOLKS_IS_GROUP_DETAILS (persona))
916 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
917 is_member, groups_change_group_cb, contact);
921 /* If the persona doesn't exist yet, we have to cache the changes until it
923 if (priv->groups == NULL)
925 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
926 g_free, g_str_hash, g_str_equal);
929 gee_collection_add (GEE_COLLECTION (priv->groups), group);
933 empathy_contact_get_avatar (EmpathyContact *contact)
935 EmpathyContactPriv *priv;
937 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
939 priv = GET_PRIV (contact);
945 contact_set_avatar (EmpathyContact *contact,
946 EmpathyAvatar *avatar)
948 EmpathyContactPriv *priv;
950 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
952 priv = GET_PRIV (contact);
954 if (priv->avatar == avatar)
959 empathy_avatar_unref (priv->avatar);
964 priv->avatar = empathy_avatar_ref (avatar);
966 g_object_notify (G_OBJECT (contact), "avatar");
970 empathy_contact_get_account (EmpathyContact *contact)
972 EmpathyContactPriv *priv;
974 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
976 priv = GET_PRIV (contact);
978 if (priv->account == NULL && priv->tp_contact != NULL)
980 TpConnection *connection;
982 /* FIXME: This assume the account manager already exists */
983 connection = tp_contact_get_connection (priv->tp_contact);
985 g_object_ref (tp_connection_get_account (connection));
988 return priv->account;
992 empathy_contact_get_persona (EmpathyContact *contact)
994 EmpathyContactPriv *priv;
996 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
998 priv = GET_PRIV (contact);
1000 if (priv->persona == NULL && priv->tp_contact != NULL)
1002 TpfPersona *persona;
1004 persona = tpf_persona_dup_for_contact (priv->tp_contact);
1005 if (persona != NULL)
1007 empathy_contact_set_persona (contact, (FolksPersona *) persona);
1008 g_object_unref (persona);
1012 return priv->persona;
1016 empathy_contact_set_persona (EmpathyContact *contact,
1017 FolksPersona *persona)
1019 EmpathyContactPriv *priv;
1021 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1022 g_return_if_fail (TPF_IS_PERSONA (persona));
1024 priv = GET_PRIV (contact);
1026 if (persona == priv->persona)
1029 if (priv->persona != NULL)
1031 g_signal_handlers_disconnect_by_func (priv->persona,
1032 folks_persona_notify_cb, contact);
1033 g_object_unref (priv->persona);
1035 priv->persona = g_object_ref (persona);
1037 g_signal_connect (priv->persona, "notify",
1038 G_CALLBACK (folks_persona_notify_cb), contact);
1040 g_object_notify (G_OBJECT (contact), "persona");
1042 /* Set the persona's alias, since ours could've been set using
1043 * empathy_contact_set_alias() before we had a persona; this happens when
1044 * adding a contact. */
1045 if (priv->alias != NULL)
1046 empathy_contact_set_alias (contact, priv->alias);
1048 /* Set the persona's groups */
1049 if (priv->groups != NULL)
1051 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1052 GEE_SET (priv->groups));
1053 g_object_unref (priv->groups);
1054 priv->groups = NULL;
1059 empathy_contact_get_connection (EmpathyContact *contact)
1061 EmpathyContactPriv *priv;
1063 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1065 priv = GET_PRIV (contact);
1067 if (priv->tp_contact != NULL)
1068 return tp_contact_get_connection (priv->tp_contact);
1073 TpConnectionPresenceType
1074 empathy_contact_get_presence (EmpathyContact *contact)
1076 EmpathyContactPriv *priv;
1078 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1079 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1081 priv = GET_PRIV (contact);
1083 if (priv->tp_contact != NULL)
1084 return tp_contact_get_presence_type (priv->tp_contact);
1086 return priv->presence;
1090 empathy_contact_get_presence_message (EmpathyContact *contact)
1092 EmpathyContactPriv *priv;
1094 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1096 priv = GET_PRIV (contact);
1098 if (priv->persona != NULL)
1099 return folks_presence_details_get_presence_message (
1100 FOLKS_PRESENCE_DETAILS (priv->persona));
1102 if (priv->tp_contact != NULL)
1103 return tp_contact_get_presence_message (priv->tp_contact);
1109 empathy_contact_get_handle (EmpathyContact *contact)
1111 EmpathyContactPriv *priv;
1113 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1115 priv = GET_PRIV (contact);
1117 if (priv->tp_contact != NULL)
1118 return tp_contact_get_handle (priv->tp_contact);
1120 return priv->handle;
1124 empathy_contact_get_capabilities (EmpathyContact *contact)
1126 EmpathyContactPriv *priv;
1128 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1130 priv = GET_PRIV (contact);
1132 return priv->capabilities;
1136 empathy_contact_is_user (EmpathyContact *contact)
1138 EmpathyContactPriv *priv;
1140 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1142 priv = GET_PRIV (contact);
1144 return priv->is_user;
1148 empathy_contact_set_is_user (EmpathyContact *contact,
1151 EmpathyContactPriv *priv;
1153 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1155 priv = GET_PRIV (contact);
1157 if (priv->is_user == is_user)
1160 priv->is_user = is_user;
1162 g_object_notify (G_OBJECT (contact), "is-user");
1166 empathy_contact_is_online (EmpathyContact *contact)
1168 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1170 switch (empathy_contact_get_presence (contact))
1172 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1173 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1174 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1176 /* Contacts without presence are considered online so we can display IRC
1177 * contacts in rooms. */
1178 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1179 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1180 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1181 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1182 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1183 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1190 empathy_contact_get_status (EmpathyContact *contact)
1192 const gchar *message;
1194 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1196 message = empathy_contact_get_presence_message (contact);
1197 if (!EMP_STR_EMPTY (message))
1200 return empathy_presence_get_default_message (
1201 empathy_contact_get_presence (contact));
1205 empathy_contact_can_sms (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_SMS;
1217 empathy_contact_can_voip (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_AUDIO |
1226 EMPATHY_CAPABILITIES_VIDEO);
1230 empathy_contact_can_voip_audio (EmpathyContact *contact)
1232 EmpathyContactPriv *priv;
1234 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1236 priv = GET_PRIV (contact);
1238 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1242 empathy_contact_can_voip_video (EmpathyContact *contact)
1244 EmpathyContactPriv *priv;
1246 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1248 priv = GET_PRIV (contact);
1250 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1254 empathy_contact_can_send_files (EmpathyContact *contact)
1256 EmpathyContactPriv *priv;
1258 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1260 priv = GET_PRIV (contact);
1262 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1266 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1268 EmpathyContactPriv *priv;
1270 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1272 priv = GET_PRIV (contact);
1274 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1278 contact_has_log (EmpathyContact *contact)
1280 TplLogManager *manager;
1284 manager = tpl_log_manager_dup_singleton ();
1285 entity = tpl_entity_new (empathy_contact_get_id (contact),
1286 TPL_ENTITY_CONTACT, NULL, NULL);
1288 have_log = tpl_log_manager_exists (manager,
1289 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1291 g_object_unref (entity);
1292 g_object_unref (manager);
1298 empathy_contact_can_do_action (EmpathyContact *self,
1299 EmpathyActionType action_type)
1301 gboolean sensitivity = FALSE;
1303 switch (action_type)
1305 case EMPATHY_ACTION_CHAT:
1308 case EMPATHY_ACTION_SMS:
1309 sensitivity = empathy_contact_can_sms (self);
1311 case EMPATHY_ACTION_AUDIO_CALL:
1312 sensitivity = empathy_contact_can_voip_audio (self);
1314 case EMPATHY_ACTION_VIDEO_CALL:
1315 sensitivity = empathy_contact_can_voip_video (self);
1317 case EMPATHY_ACTION_VIEW_LOGS:
1318 sensitivity = contact_has_log (self);
1320 case EMPATHY_ACTION_SEND_FILE:
1321 sensitivity = empathy_contact_can_send_files (self);
1323 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1324 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1327 g_assert_not_reached ();
1330 return (sensitivity ? TRUE : FALSE);
1334 contact_get_avatar_filename (EmpathyContact *contact,
1340 gchar *token_escaped;
1342 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1345 token_escaped = tp_escape_as_identifier (token);
1346 account = empathy_contact_get_account (contact);
1348 avatar_path = g_build_filename (g_get_user_cache_dir (),
1351 tp_account_get_cm_name (account),
1352 tp_account_get_protocol_name (account),
1354 g_mkdir_with_parents (avatar_path, 0700);
1356 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1358 g_free (token_escaped);
1359 g_free (avatar_path);
1365 contact_load_avatar_cache (EmpathyContact *contact,
1368 EmpathyAvatar *avatar = NULL;
1372 GError *error = NULL;
1374 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1375 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1377 /* Load the avatar from file if it exists */
1378 filename = contact_get_avatar_filename (contact, token);
1379 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1381 if (!g_file_get_contents (filename, &data, &len, &error))
1383 DEBUG ("Failed to load avatar from cache: %s",
1384 error ? error->message : "No error given");
1385 g_clear_error (&error);
1391 DEBUG ("Avatar loaded from %s", filename);
1392 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1393 contact_set_avatar (contact, avatar);
1394 empathy_avatar_unref (avatar);
1400 return data != NULL;
1404 empathy_avatar_get_type (void)
1406 static GType type_id = 0;
1410 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1411 (GBoxedCopyFunc) empathy_avatar_ref,
1412 (GBoxedFreeFunc) empathy_avatar_unref);
1419 * empathy_avatar_new:
1420 * @data: the avatar data
1421 * @len: the size of avatar data
1422 * @format: the mime type of the avatar image
1423 * @filename: the filename where the avatar is stored in cache
1425 * Create a #EmpathyAvatar from the provided data.
1427 * Returns: a new #EmpathyAvatar
1430 empathy_avatar_new (const guchar *data,
1432 const gchar *format,
1433 const gchar *filename)
1435 EmpathyAvatar *avatar;
1437 avatar = g_slice_new0 (EmpathyAvatar);
1438 avatar->data = g_memdup (data, len);
1440 avatar->format = g_strdup (format);
1441 avatar->filename = g_strdup (filename);
1442 avatar->refcount = 1;
1448 empathy_avatar_unref (EmpathyAvatar *avatar)
1450 g_return_if_fail (avatar != NULL);
1453 if (avatar->refcount == 0)
1455 g_free (avatar->data);
1456 g_free (avatar->format);
1457 g_free (avatar->filename);
1458 g_slice_free (EmpathyAvatar, avatar);
1463 empathy_avatar_ref (EmpathyAvatar *avatar)
1465 g_return_val_if_fail (avatar != NULL, NULL);
1473 * empathy_avatar_save_to_file:
1474 * @avatar: the avatar
1475 * @filename: name of a file to write avatar to
1476 * @error: return location for a GError, or NULL
1478 * Save the avatar to a file named filename
1480 * Returns: %TRUE on success, %FALSE if an error occurred
1483 empathy_avatar_save_to_file (EmpathyAvatar *self,
1484 const gchar *filename,
1487 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1492 * empathy_contact_get_location:
1493 * @contact: an #EmpathyContact
1495 * Returns the user's location if available. The keys are defined in
1496 * empathy-location.h. If the contact doesn't have location
1497 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1498 * you are done with the #GHashTable.
1500 * It is composed of string keys and GValues. Keys are
1501 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1502 * Example: a "city" key would have "Helsinki" as string GValue,
1503 * a "latitude" would have 65.0 as double GValue.
1505 * Returns: a #GHashTable of location values
1508 empathy_contact_get_location (EmpathyContact *contact)
1510 EmpathyContactPriv *priv;
1512 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1514 priv = GET_PRIV (contact);
1516 return priv->location;
1520 * empathy_contact_set_location:
1521 * @contact: an #EmpathyContact
1522 * @location: a #GHashTable of the location
1524 * Sets the user's location based on the location #GHashTable passed.
1525 * It is composed of string keys and GValues. Keys are
1526 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1527 * Example: a "city" key would have "Helsinki" as string GValue,
1528 * a "latitude" would have 65.0 as double GValue.
1531 empathy_contact_set_location (EmpathyContact *contact,
1532 GHashTable *location)
1534 EmpathyContactPriv *priv;
1536 g_return_if_fail (EMPATHY_CONTACT (contact));
1537 g_return_if_fail (location != NULL);
1539 priv = GET_PRIV (contact);
1541 if (priv->location != NULL)
1542 g_hash_table_unref (priv->location);
1544 priv->location = g_hash_table_ref (location);
1546 update_geocode (contact);
1548 g_object_notify (G_OBJECT (contact), "location");
1551 const gchar * const *
1552 empathy_contact_get_client_types (EmpathyContact *contact)
1554 EmpathyContactPriv *priv;
1556 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1558 priv = GET_PRIV (contact);
1560 return (const gchar * const *) priv->client_types;
1564 contact_set_client_types (EmpathyContact *contact,
1565 const gchar * const *client_types)
1567 EmpathyContactPriv *priv = GET_PRIV (contact);
1569 if (priv->client_types != NULL)
1570 g_strfreev (priv->client_types);
1572 priv->client_types = g_strdupv ((gchar **) client_types);
1573 g_object_notify (G_OBJECT (contact), "client-types");
1577 * empathy_contact_equal:
1578 * @contact1: an #EmpathyContact
1579 * @contact2: an #EmpathyContact
1581 * Returns FALSE if one of the contacts is NULL but the other is not.
1582 * Otherwise returns TRUE if both pointer are equal or if they bith
1583 * refer to the same id.
1584 * It's only necessary to call this function if your contact objects
1585 * come from logs where contacts are created dynamically and comparing
1586 * pointers is not enough.
1589 empathy_contact_equal (gconstpointer contact1,
1590 gconstpointer contact2)
1597 if ((contact1 == NULL) != (contact2 == NULL)) {
1600 if (contact1 == contact2) {
1603 c1 = EMPATHY_CONTACT (contact1);
1604 c2 = EMPATHY_CONTACT (contact2);
1605 id1 = empathy_contact_get_id (c1);
1606 id2 = empathy_contact_get_id (c2);
1607 if (!tp_strdiff (id1, id2)) {
1614 /* This callback is called by geocode-glib when it found a position
1615 * for the given address. A position is necessary for a contact
1616 * to show up on the map
1619 geocode_cb (GObject *source,
1620 GAsyncResult *result,
1623 EmpathyContact *contact = user_data;
1624 EmpathyContactPriv *priv = GET_PRIV (contact);
1625 GError *error = NULL;
1626 GHashTable *new_location;
1627 GHashTable *resolved = NULL;
1628 gdouble latitude, longitude;
1630 if (priv->location == NULL)
1633 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1636 if (resolved == NULL)
1638 DEBUG ("Failed to resolve geocode: %s", error->message);
1639 g_error_free (error);
1643 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1646 new_location = tp_asv_new (
1647 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1648 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1651 DEBUG ("\t - Latitude: %f", latitude);
1652 DEBUG ("\t - Longitude: %f", longitude);
1654 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1655 * the values we just set. */
1656 tp_g_hash_table_update (new_location, priv->location,
1657 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1659 /* Don't change the accuracy as we used an address to get this position */
1660 g_hash_table_unref (priv->location);
1661 priv->location = new_location;
1662 g_object_notify ((GObject *) contact, "location");
1665 tp_clear_pointer (&resolved, g_hash_table_unref);
1666 g_object_unref (contact);
1670 update_geocode (EmpathyContact *contact)
1672 GeocodeObject *geocode;
1673 GHashTable *location;
1675 location = empathy_contact_get_location (contact);
1676 if (location == NULL ||
1677 g_hash_table_size (location) == 0)
1680 /* No need to search for position if contact published it */
1681 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1682 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1685 geocode = geocode_object_new_for_params (location);
1686 if (geocode == NULL)
1689 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1690 g_object_ref (contact));
1692 g_object_unref (geocode);
1696 static EmpathyCapabilities
1697 tp_caps_to_capabilities (TpCapabilities *caps)
1699 EmpathyCapabilities capabilities = 0;
1701 if (tp_capabilities_supports_file_transfer (caps))
1702 capabilities |= EMPATHY_CAPABILITIES_FT;
1704 if (tp_capabilities_supports_stream_tubes (caps, TP_HANDLE_TYPE_CONTACT,
1706 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1708 if (tp_capabilities_supports_audio_video_call (caps, TP_HANDLE_TYPE_CONTACT))
1710 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1711 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1713 else if (tp_capabilities_supports_audio_call (caps, TP_HANDLE_TYPE_CONTACT))
1715 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1718 if (tp_capabilities_supports_sms (caps))
1719 capabilities |= EMPATHY_CAPABILITIES_SMS;
1721 return capabilities;
1725 set_capabilities_from_tp_caps (EmpathyContact *self,
1726 TpCapabilities *caps)
1728 EmpathyCapabilities capabilities;
1733 capabilities = tp_caps_to_capabilities (caps);
1734 empathy_contact_set_capabilities (self, capabilities);
1738 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1740 EmpathyContactPriv *priv = GET_PRIV (contact);
1744 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1745 file = tp_contact_get_avatar_file (priv->tp_contact);
1749 EmpathyAvatar *avatar;
1753 GError *error = NULL;
1755 if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
1757 DEBUG ("Failed to load avatar: %s", error->message);
1759 g_error_free (error);
1760 contact_set_avatar (contact, NULL);
1764 path = g_file_get_path (file);
1766 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1768 contact_set_avatar (contact, avatar);
1769 empathy_avatar_unref (avatar);
1775 contact_set_avatar (contact, NULL);
1780 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1782 EmpathyContact *contact = NULL;
1784 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1786 if (contacts_table == NULL)
1787 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1789 contact = g_hash_table_lookup (contacts_table, tp_contact);
1791 if (contact == NULL)
1793 contact = empathy_contact_new (tp_contact);
1795 /* The hash table does not keep any ref.
1796 * contact keeps a ref to tp_contact, and is removed from the table in
1797 * contact_dispose() */
1798 g_hash_table_insert (contacts_table, tp_contact, contact);
1802 g_object_ref (contact);
1809 presence_cmp_func (EmpathyContact *a,
1812 FolksPresenceDetails *presence_a, *presence_b;
1814 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1815 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1817 /* We negate the result because we're sorting in reverse order (i.e. such that
1818 * the Personas with the highest presence are at the beginning of the list. */
1819 return -folks_presence_details_typecmp (
1820 folks_presence_details_get_presence_type (presence_a),
1821 folks_presence_details_get_presence_type (presence_b));
1825 voip_cmp_func (EmpathyContact *a,
1828 gboolean has_audio_a, has_audio_b;
1829 gboolean has_video_a, has_video_b;
1831 has_audio_a = empathy_contact_can_voip_audio (a);
1832 has_audio_b = empathy_contact_can_voip_audio (b);
1833 has_video_a = empathy_contact_can_voip_video (a);
1834 has_video_b = empathy_contact_can_voip_video (b);
1836 /* First check video */
1837 if (has_video_a == has_video_b)
1839 /* Use audio to to break tie */
1840 if (has_audio_a == has_audio_b)
1842 else if (has_audio_a)
1847 else if (has_video_a)
1854 ft_cmp_func (EmpathyContact *a,
1857 gboolean can_send_files_a, can_send_files_b;
1859 can_send_files_a = empathy_contact_can_send_files (a);
1860 can_send_files_b = empathy_contact_can_send_files (b);
1862 if (can_send_files_a == can_send_files_b)
1864 else if (can_send_files_a)
1871 rfb_stream_tube_cmp_func (EmpathyContact *a,
1874 gboolean rfb_a, rfb_b;
1876 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1877 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1887 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1888 * the same presence, prefer the one which can do both audio *and* video calls,
1889 * over the one which can only do one of the two. */
1891 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1893 gint presence_sort = presence_cmp_func (a, b);
1895 if (presence_sort != 0)
1896 return presence_sort;
1898 return voip_cmp_func (a, b);
1901 /* Sort by presence as with presence_cmp_func() and then break ties using the
1902 * most "capable" individual. So users will be able to pick more actions on
1903 * the contact in the "Contact" menu of the chat window. */
1905 chat_sort_func (EmpathyContact *a,
1910 result = presence_cmp_func (a, b);
1914 /* Prefer individual supporting file transfer */
1915 result = ft_cmp_func (a, b);
1919 /* Check audio/video capabilities */
1920 result = voip_cmp_func (a, b);
1924 /* Check 'Share my destop' feature */
1925 return rfb_stream_tube_cmp_func (a, b);
1929 get_sort_func_for_action (EmpathyActionType action_type)
1931 switch (action_type)
1933 case EMPATHY_ACTION_AUDIO_CALL:
1934 case EMPATHY_ACTION_VIDEO_CALL:
1935 return (GCompareFunc) voip_sort_func;
1936 case EMPATHY_ACTION_CHAT:
1937 return (GCompareFunc) chat_sort_func;
1938 case EMPATHY_ACTION_VIEW_LOGS:
1939 case EMPATHY_ACTION_SEND_FILE:
1940 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1942 return (GCompareFunc) presence_cmp_func;
1947 * empathy_contact_dup_best_for_action:
1948 * @individual: a #FolksIndividual
1949 * @action_type: the type of action to be performed on the contact
1951 * Chooses a #FolksPersona from the given @individual which is best-suited for
1952 * the given @action_type. "Best-suited" is determined by choosing the persona
1953 * with the highest presence out of all the personas which can perform the given
1954 * @action_type (e.g. are capable of video calling).
1956 * Return value: an #EmpathyContact for the best persona, or %NULL;
1957 * unref with g_object_unref()
1960 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1961 EmpathyActionType action_type)
1966 EmpathyContact *best_contact = NULL;
1968 /* Build a list of EmpathyContacts that we can sort */
1969 personas = folks_individual_get_personas (individual);
1972 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1973 while (gee_iterator_next (iter))
1975 FolksPersona *persona = gee_iterator_get (iter);
1976 TpContact *tp_contact;
1977 EmpathyContact *contact = NULL;
1979 if (!empathy_folks_persona_is_interesting (persona))
1982 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1983 if (tp_contact == NULL)
1986 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1987 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
1989 /* Only choose the contact if they're actually capable of the specified
1991 if (empathy_contact_can_do_action (contact, action_type))
1992 contacts = g_list_prepend (contacts, g_object_ref (contact));
1995 g_clear_object (&contact);
1996 g_clear_object (&persona);
1998 g_clear_object (&iter);
2000 /* Sort the contacts by some heuristic based on the action type, then take
2001 * the top contact. */
2002 if (contacts != NULL)
2004 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2005 best_contact = g_object_ref (contacts->data);
2008 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2009 g_list_free (contacts);
2011 return best_contact;
2014 #define declare_contact_cb(name) \
2016 contact_##name##_cb (GObject *source, \
2017 GAsyncResult *result, \
2018 gpointer user_data) \
2020 TpContact *contact = (TpContact *) source; \
2021 GError *error = NULL; \
2023 if (!tp_contact_##name##_finish (contact, result, &error)) \
2025 DEBUG ("Failed to ##name## on %s\n", \
2026 tp_contact_get_identifier (contact)); \
2027 g_error_free (error); \
2031 declare_contact_cb (request_subscription)
2032 declare_contact_cb (authorize_publication)
2033 declare_contact_cb (unblock)
2036 empathy_contact_add_to_contact_list (EmpathyContact *self,
2037 const gchar *message)
2039 EmpathyContactPriv *priv = GET_PRIV (self);
2041 g_return_if_fail (priv->tp_contact != NULL);
2043 tp_contact_request_subscription_async (priv->tp_contact, message,
2044 contact_request_subscription_cb, NULL);
2046 tp_contact_authorize_publication_async (priv->tp_contact,
2047 contact_authorize_publication_cb, NULL);
2049 tp_contact_unblock_async (priv->tp_contact, contact_unblock_cb, NULL);
2052 declare_contact_cb (remove)
2055 empathy_contact_remove_from_contact_list (EmpathyContact *self)
2057 EmpathyContactPriv *priv = GET_PRIV (self);
2059 g_return_if_fail (priv->tp_contact != NULL);
2061 tp_contact_remove_async (priv->tp_contact, contact_remove_cb, NULL);