1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/account-manager.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
36 #include <geocode-glib/geocode-glib.h>
39 #include "empathy-contact.h"
40 #include "empathy-camera-monitor.h"
41 #include "empathy-individual-manager.h"
42 #include "empathy-utils.h"
43 #include "empathy-enum-types.h"
44 #include "empathy-location.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
47 #include "empathy-debug.h"
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
51 TpContact *tp_contact;
53 FolksPersona *persona;
57 EmpathyAvatar *avatar;
58 TpConnectionPresenceType presence;
60 EmpathyCapabilities capabilities;
62 /* Location is composed of string keys and GValues.
63 * Example: a "city" key would have "Helsinki" as string GValue,
64 * a "latitude" would have 65.0 as double GValue.
66 * This is a super set of the location stored in TpContact as we can try add
67 * more fields by searching the address using geoclue.
74 static void contact_finalize (GObject *object);
75 static void contact_get_property (GObject *object, guint param_id,
76 GValue *value, GParamSpec *pspec);
77 static void contact_set_property (GObject *object, guint param_id,
78 const GValue *value, GParamSpec *pspec);
81 static void update_geocode (EmpathyContact *contact);
84 static void empathy_contact_set_location (EmpathyContact *contact,
85 GHashTable *location);
87 static void contact_set_client_types (EmpathyContact *contact,
88 const gchar * const *types);
90 static void set_capabilities_from_tp_caps (EmpathyContact *self,
91 TpCapabilities *caps);
93 static void contact_set_avatar (EmpathyContact *contact,
94 EmpathyAvatar *avatar);
95 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
96 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
99 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
112 PROP_PRESENCE_MESSAGE,
125 static guint signals[LAST_SIGNAL];
127 /* TpContact* -> EmpathyContact*, both borrowed ref */
128 static GHashTable *contacts_table = NULL;
131 tp_contact_notify_cb (TpContact *tp_contact,
135 EmpathyContactPriv *priv = GET_PRIV (contact);
137 /* Forward property notifications */
138 if (!tp_strdiff (param->name, "alias"))
139 g_object_notify (contact, "alias");
140 else if (!tp_strdiff (param->name, "presence-type")) {
141 TpConnectionPresenceType presence;
143 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
144 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
146 priv->presence = presence;
147 g_object_notify (contact, "presence");
149 else if (!tp_strdiff (param->name, "identifier"))
150 g_object_notify (contact, "id");
151 else if (!tp_strdiff (param->name, "handle"))
152 g_object_notify (contact, "handle");
153 else if (!tp_strdiff (param->name, "location"))
155 GHashTable *location;
157 location = tp_contact_get_location (tp_contact);
158 /* This will start a geoclue search to find the address if needed */
159 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
161 else if (!tp_strdiff (param->name, "capabilities"))
163 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
164 tp_contact_get_capabilities (tp_contact));
166 else if (!tp_strdiff (param->name, "avatar-file"))
168 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
170 else if (!tp_strdiff (param->name, "client-types"))
172 contact_set_client_types (EMPATHY_CONTACT (contact),
173 tp_contact_get_client_types (tp_contact));
178 folks_persona_notify_cb (FolksPersona *folks_persona,
182 if (!tp_strdiff (param->name, "presence-message"))
183 g_object_notify (contact, "presence-message");
187 contact_dispose (GObject *object)
189 EmpathyContactPriv *priv = GET_PRIV (object);
191 if (priv->tp_contact != NULL)
193 g_signal_handlers_disconnect_by_func (priv->tp_contact,
194 tp_contact_notify_cb, object);
196 tp_clear_object (&priv->tp_contact);
199 g_object_unref (priv->account);
200 priv->account = NULL;
204 g_signal_handlers_disconnect_by_func (priv->persona,
205 folks_persona_notify_cb, object);
206 g_object_unref (priv->persona);
208 priv->persona = NULL;
210 if (priv->avatar != NULL)
212 empathy_avatar_unref (priv->avatar);
216 if (priv->location != NULL)
218 g_hash_table_unref (priv->location);
219 priv->location = NULL;
222 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
226 contact_constructed (GObject *object)
228 EmpathyContact *contact = (EmpathyContact *) object;
229 EmpathyContactPriv *priv = GET_PRIV (contact);
230 GHashTable *location;
231 TpContact *self_contact;
232 const gchar * const *client_types;
234 if (priv->tp_contact == NULL)
237 priv->presence = empathy_contact_get_presence (contact);
239 location = tp_contact_get_location (priv->tp_contact);
240 if (location != NULL)
241 empathy_contact_set_location (contact, location);
243 client_types = tp_contact_get_client_types (priv->tp_contact);
244 if (client_types != NULL)
245 contact_set_client_types (contact, client_types);
247 set_capabilities_from_tp_caps (contact,
248 tp_contact_get_capabilities (priv->tp_contact));
250 contact_set_avatar_from_tp_contact (contact);
252 /* Set is-user property. Note that it could still be the handle is
253 * different from the connection's self handle, in the case the handle
254 * comes from a group interface. */
255 self_contact = tp_connection_get_self_contact (
256 tp_contact_get_connection (priv->tp_contact));
257 empathy_contact_set_is_user (contact, self_contact == priv->tp_contact);
259 g_signal_connect (priv->tp_contact, "notify",
260 G_CALLBACK (tp_contact_notify_cb), contact);
264 empathy_contact_class_init (EmpathyContactClass *class)
266 GObjectClass *object_class;
268 object_class = G_OBJECT_CLASS (class);
270 object_class->finalize = contact_finalize;
271 object_class->dispose = contact_dispose;
272 object_class->get_property = contact_get_property;
273 object_class->set_property = contact_set_property;
274 object_class->constructed = contact_constructed;
276 g_object_class_install_property (object_class,
278 g_param_spec_object ("tp-contact",
280 "The TpContact associated with the contact",
282 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (object_class,
286 g_param_spec_object ("account",
288 "The account associated with the contact",
290 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (object_class,
294 g_param_spec_object ("persona",
296 "The FolksPersona associated with the contact",
298 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (object_class,
302 g_param_spec_string ("id",
304 "String identifying contact",
306 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (object_class,
310 g_param_spec_string ("alias",
312 "An alias for the contact",
314 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (object_class,
318 g_param_spec_string ("logged-alias",
320 "The alias the user had when a message was logged, "
321 "only set when using empathy_contact_from_tpl_contact()",
323 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325 g_object_class_install_property (object_class,
327 g_param_spec_boxed ("avatar",
331 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (object_class,
335 g_param_spec_uint ("presence",
337 "Presence of contact",
338 TP_CONNECTION_PRESENCE_TYPE_UNSET,
339 NUM_TP_CONNECTION_PRESENCE_TYPES,
340 TP_CONNECTION_PRESENCE_TYPE_UNSET,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (object_class,
344 PROP_PRESENCE_MESSAGE,
345 g_param_spec_string ("presence-message",
346 "Contact presence message",
347 "Presence message of contact",
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (object_class,
353 g_param_spec_uint ("handle",
355 "The handle of the contact",
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (object_class,
363 g_param_spec_flags ("capabilities",
364 "Contact Capabilities",
365 "Capabilities of the contact",
366 EMPATHY_TYPE_CAPABILITIES,
367 EMPATHY_CAPABILITIES_UNKNOWN,
368 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (object_class,
372 g_param_spec_boolean ("is-user",
374 "Is contact the user",
376 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
379 g_object_class_install_property (object_class,
381 g_param_spec_boxed ("location",
383 "Physical location of the contact",
385 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (object_class,
389 g_param_spec_boxed ("client-types",
390 "Contact client types",
391 "Client types of the contact",
393 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
395 signals[PRESENCE_CHANGED] =
396 g_signal_new ("presence-changed",
397 G_TYPE_FROM_CLASS (class),
401 g_cclosure_marshal_generic,
406 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
410 empathy_contact_init (EmpathyContact *contact)
412 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
413 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
415 contact->priv = priv;
417 priv->location = NULL;
418 priv->client_types = NULL;
423 contact_finalize (GObject *object)
425 EmpathyContactPriv *priv;
427 priv = GET_PRIV (object);
429 DEBUG ("finalize: %p", object);
431 g_clear_object (&priv->groups);
432 g_free (priv->alias);
434 g_strfreev (priv->client_types);
436 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
440 empathy_contact_set_capabilities (EmpathyContact *contact,
441 EmpathyCapabilities capabilities)
443 EmpathyContactPriv *priv;
445 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
447 priv = GET_PRIV (contact);
449 if (priv->capabilities == capabilities)
452 priv->capabilities = capabilities;
454 g_object_notify (G_OBJECT (contact), "capabilities");
458 empathy_contact_set_id (EmpathyContact *contact,
461 EmpathyContactPriv *priv;
463 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
464 g_return_if_fail (id != NULL);
466 priv = GET_PRIV (contact);
468 /* We temporally ref the contact because it could be destroyed
469 * during the signal emition */
470 g_object_ref (contact);
471 if (tp_strdiff (id, priv->id))
474 priv->id = g_strdup (id);
476 g_object_notify (G_OBJECT (contact), "id");
477 if (EMP_STR_EMPTY (priv->alias))
478 g_object_notify (G_OBJECT (contact), "alias");
481 g_object_unref (contact);
485 empathy_contact_set_presence (EmpathyContact *contact,
486 TpConnectionPresenceType presence)
488 EmpathyContactPriv *priv;
489 TpConnectionPresenceType old_presence;
491 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
493 priv = GET_PRIV (contact);
495 if (presence == priv->presence)
498 old_presence = priv->presence;
499 priv->presence = presence;
501 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
503 g_object_notify (G_OBJECT (contact), "presence");
507 empathy_contact_set_presence_message (EmpathyContact *contact,
508 const gchar *message)
510 EmpathyContactPriv *priv = GET_PRIV (contact);
512 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
514 if (priv->persona != NULL)
516 folks_presence_details_set_presence_message (
517 FOLKS_PRESENCE_DETAILS (priv->persona), message);
522 empathy_contact_set_handle (EmpathyContact *contact,
525 EmpathyContactPriv *priv;
527 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
529 priv = GET_PRIV (contact);
531 g_object_ref (contact);
532 if (handle != priv->handle)
534 priv->handle = handle;
535 g_object_notify (G_OBJECT (contact), "handle");
537 g_object_unref (contact);
541 contact_get_property (GObject *object,
546 EmpathyContact *contact = EMPATHY_CONTACT (object);
550 case PROP_TP_CONTACT:
551 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
554 g_value_set_object (value, empathy_contact_get_account (contact));
557 g_value_set_object (value, empathy_contact_get_persona (contact));
560 g_value_set_string (value, empathy_contact_get_id (contact));
563 g_value_set_string (value, empathy_contact_get_alias (contact));
565 case PROP_LOGGED_ALIAS:
566 g_value_set_string (value, empathy_contact_get_logged_alias (contact));
569 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
572 g_value_set_uint (value, empathy_contact_get_presence (contact));
574 case PROP_PRESENCE_MESSAGE:
575 g_value_set_string (value, empathy_contact_get_presence_message (contact));
578 g_value_set_uint (value, empathy_contact_get_handle (contact));
580 case PROP_CAPABILITIES:
581 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
584 g_value_set_boolean (value, empathy_contact_is_user (contact));
587 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
593 contact_set_property (GObject *object,
598 EmpathyContact *contact = EMPATHY_CONTACT (object);
599 EmpathyContactPriv *priv = GET_PRIV (object);
603 case PROP_TP_CONTACT:
604 priv->tp_contact = g_value_dup_object (value);
607 g_assert (priv->account == NULL);
608 priv->account = g_value_dup_object (value);
611 empathy_contact_set_persona (contact, g_value_get_object (value));
614 empathy_contact_set_id (contact, g_value_get_string (value));
617 empathy_contact_set_alias (contact, g_value_get_string (value));
619 case PROP_LOGGED_ALIAS:
620 g_assert (priv->logged_alias == NULL);
621 priv->logged_alias = g_value_dup_string (value);
624 empathy_contact_set_presence (contact, g_value_get_uint (value));
626 case PROP_PRESENCE_MESSAGE:
627 empathy_contact_set_presence_message (contact, g_value_get_string (value));
630 empathy_contact_set_handle (contact, g_value_get_uint (value));
632 case PROP_CAPABILITIES:
633 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
636 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
639 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
645 remove_tp_contact (gpointer data,
648 g_hash_table_remove (contacts_table, data);
651 static EmpathyContact *
652 empathy_contact_new (TpContact *tp_contact)
654 EmpathyContact *retval;
656 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
658 retval = g_object_new (EMPATHY_TYPE_CONTACT,
659 "tp-contact", tp_contact,
662 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
674 contact_is_tpl_entity (gpointer key,
678 EmpathyContact *contact = value;
679 FindContactData *data = user_data;
680 TpAccount *account = empathy_contact_get_account (contact);
681 const gchar *path = NULL;
684 path = tp_proxy_get_object_path (account);
686 return !tp_strdiff (empathy_contact_get_id (contact),
687 tpl_entity_get_identifier (data->entity)) &&
688 !tp_strdiff (tp_proxy_get_object_path (data->account), path);
692 get_contacts_cb (GObject *source,
693 GAsyncResult *result,
696 TpWeakRef *wr = user_data;
697 EmpathyContactPriv *priv;
698 EmpathyContact *self;
700 self = tp_weak_ref_dup_object (wr);
704 priv = GET_PRIV (self);
706 g_return_if_fail (priv->tp_contact == NULL);
708 priv->tp_contact = tp_connection_dup_contact_by_id_finish (
709 TP_CONNECTION (source), result, NULL);
710 if (priv->tp_contact == NULL)
713 g_object_notify (G_OBJECT (self), "tp-contact");
715 /* Update capabilities now that we have a TpContact */
716 set_capabilities_from_tp_caps (self,
717 tp_contact_get_capabilities (priv->tp_contact));
720 g_clear_object (&self);
721 tp_weak_ref_destroy (wr);
725 empathy_contact_from_tpl_contact (TpAccount *account,
726 TplEntity *tpl_entity)
728 EmpathyContact *retval;
730 EmpathyContact *existing_contact = NULL;
732 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
734 if (contacts_table != NULL)
736 FindContactData data;
738 data.entity = tpl_entity;
739 data.account = account;
741 existing_contact = g_hash_table_find (contacts_table,
742 contact_is_tpl_entity, &data);
745 if (existing_contact != NULL)
747 retval = g_object_new (EMPATHY_TYPE_CONTACT,
748 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
749 "logged-alias", tpl_entity_get_alias (tpl_entity),
757 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
759 id = tpl_entity_get_identifier (tpl_entity);
761 retval = g_object_new (EMPATHY_TYPE_CONTACT,
763 "alias", tpl_entity_get_alias (tpl_entity),
768 /* Try to get a TpContact associated to have at least contact
769 * capabilities if possible. This is useful for CM supporting calling
770 * offline contacts for example. */
771 conn = tp_account_get_connection (account);
774 TpContactFeature features[] = { TP_CONTACT_FEATURE_CAPABILITIES };
775 conn = tp_account_get_connection (account);
777 tp_connection_dup_contact_by_id_async (conn, id,
778 G_N_ELEMENTS (features), features, get_contacts_cb,
779 tp_weak_ref_new (retval, NULL, NULL));
783 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
784 contact_load_avatar_cache (retval,
785 tpl_entity_get_avatar_token (tpl_entity));
791 empathy_contact_get_tp_contact (EmpathyContact *contact)
793 EmpathyContactPriv *priv;
795 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
797 priv = GET_PRIV (contact);
799 return priv->tp_contact;
803 empathy_contact_get_id (EmpathyContact *contact)
805 EmpathyContactPriv *priv;
807 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
809 priv = GET_PRIV (contact);
811 if (priv->tp_contact != NULL)
812 return tp_contact_get_identifier (priv->tp_contact);
818 empathy_contact_get_alias (EmpathyContact *contact)
820 EmpathyContactPriv *priv;
821 const gchar *alias = NULL;
823 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
825 priv = GET_PRIV (contact);
827 if (!EMP_STR_EMPTY (priv->alias))
829 else if (priv->tp_contact != NULL)
830 alias = tp_contact_get_alias (priv->tp_contact);
832 if (!EMP_STR_EMPTY (alias))
835 return empathy_contact_get_id (contact);
839 empathy_contact_get_logged_alias (EmpathyContact *contact)
841 EmpathyContactPriv *priv;
843 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
845 priv = GET_PRIV (contact);
847 if (priv->logged_alias != NULL)
848 return priv->logged_alias;
850 return empathy_contact_get_alias (contact);
854 empathy_contact_set_alias (EmpathyContact *contact,
857 EmpathyContactPriv *priv;
858 FolksPersona *persona;
860 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
862 priv = GET_PRIV (contact);
864 g_object_ref (contact);
866 /* Set the alias on the persona if possible */
867 persona = empathy_contact_get_persona (contact);
868 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
870 DEBUG ("Setting alias for contact %s to %s",
871 empathy_contact_get_id (contact), alias);
873 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
876 if (tp_strdiff (alias, priv->alias))
878 g_free (priv->alias);
879 priv->alias = g_strdup (alias);
880 g_object_notify (G_OBJECT (contact), "alias");
883 g_object_unref (contact);
887 groups_change_group_cb (GObject *source,
888 GAsyncResult *result,
891 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
892 GError *error = NULL;
894 folks_group_details_change_group_finish (group_details, result, &error);
897 g_warning ("failed to change group: %s", error->message);
898 g_clear_error (&error);
903 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
906 EmpathyContactPriv *priv;
907 FolksPersona *persona;
909 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
910 g_return_if_fail (group != NULL);
912 priv = GET_PRIV (contact);
914 /* Normally pass through the changes to the persona */
915 persona = empathy_contact_get_persona (contact);
918 if (FOLKS_IS_GROUP_DETAILS (persona))
919 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
920 is_member, groups_change_group_cb, contact);
924 /* If the persona doesn't exist yet, we have to cache the changes until it
926 if (priv->groups == NULL)
928 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
929 g_free, g_str_hash, g_str_equal);
932 gee_collection_add (GEE_COLLECTION (priv->groups), group);
936 empathy_contact_get_avatar (EmpathyContact *contact)
938 EmpathyContactPriv *priv;
940 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
942 priv = GET_PRIV (contact);
948 contact_set_avatar (EmpathyContact *contact,
949 EmpathyAvatar *avatar)
951 EmpathyContactPriv *priv;
953 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
955 priv = GET_PRIV (contact);
957 if (priv->avatar == avatar)
962 empathy_avatar_unref (priv->avatar);
967 priv->avatar = empathy_avatar_ref (avatar);
969 g_object_notify (G_OBJECT (contact), "avatar");
973 empathy_contact_get_account (EmpathyContact *contact)
975 EmpathyContactPriv *priv;
977 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
979 priv = GET_PRIV (contact);
981 if (priv->account == NULL && priv->tp_contact != NULL)
983 TpConnection *connection;
985 /* FIXME: This assume the account manager already exists */
986 connection = tp_contact_get_connection (priv->tp_contact);
988 g_object_ref (tp_connection_get_account (connection));
991 return priv->account;
995 empathy_contact_get_persona (EmpathyContact *contact)
997 EmpathyContactPriv *priv;
999 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1001 priv = GET_PRIV (contact);
1003 if (priv->persona == NULL && priv->tp_contact != NULL)
1005 TpfPersona *persona;
1007 persona = tpf_persona_dup_for_contact (priv->tp_contact);
1008 if (persona != NULL)
1010 empathy_contact_set_persona (contact, (FolksPersona *) persona);
1011 g_object_unref (persona);
1015 return priv->persona;
1019 empathy_contact_set_persona (EmpathyContact *contact,
1020 FolksPersona *persona)
1022 EmpathyContactPriv *priv;
1024 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1025 g_return_if_fail (TPF_IS_PERSONA (persona));
1027 priv = GET_PRIV (contact);
1029 if (persona == priv->persona)
1032 if (priv->persona != NULL)
1034 g_signal_handlers_disconnect_by_func (priv->persona,
1035 folks_persona_notify_cb, contact);
1036 g_object_unref (priv->persona);
1038 priv->persona = g_object_ref (persona);
1040 g_signal_connect (priv->persona, "notify",
1041 G_CALLBACK (folks_persona_notify_cb), contact);
1043 g_object_notify (G_OBJECT (contact), "persona");
1045 /* Set the persona's alias, since ours could've been set using
1046 * empathy_contact_set_alias() before we had a persona; this happens when
1047 * adding a contact. */
1048 if (priv->alias != NULL)
1049 empathy_contact_set_alias (contact, priv->alias);
1051 /* Set the persona's groups */
1052 if (priv->groups != NULL)
1054 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1055 GEE_SET (priv->groups));
1056 g_object_unref (priv->groups);
1057 priv->groups = NULL;
1062 empathy_contact_get_connection (EmpathyContact *contact)
1064 EmpathyContactPriv *priv;
1066 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1068 priv = GET_PRIV (contact);
1070 if (priv->tp_contact != NULL)
1071 return tp_contact_get_connection (priv->tp_contact);
1076 TpConnectionPresenceType
1077 empathy_contact_get_presence (EmpathyContact *contact)
1079 EmpathyContactPriv *priv;
1081 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1082 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1084 priv = GET_PRIV (contact);
1086 if (priv->tp_contact != NULL)
1087 return tp_contact_get_presence_type (priv->tp_contact);
1089 return priv->presence;
1093 empathy_contact_get_presence_message (EmpathyContact *contact)
1095 EmpathyContactPriv *priv;
1097 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1099 priv = GET_PRIV (contact);
1101 if (priv->persona != NULL)
1102 return folks_presence_details_get_presence_message (
1103 FOLKS_PRESENCE_DETAILS (priv->persona));
1105 if (priv->tp_contact != NULL)
1106 return tp_contact_get_presence_message (priv->tp_contact);
1112 empathy_contact_get_handle (EmpathyContact *contact)
1114 EmpathyContactPriv *priv;
1116 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1118 priv = GET_PRIV (contact);
1120 if (priv->tp_contact != NULL)
1121 return tp_contact_get_handle (priv->tp_contact);
1123 return priv->handle;
1127 empathy_contact_get_capabilities (EmpathyContact *contact)
1129 EmpathyContactPriv *priv;
1131 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1133 priv = GET_PRIV (contact);
1135 return priv->capabilities;
1139 empathy_contact_is_user (EmpathyContact *contact)
1141 EmpathyContactPriv *priv;
1143 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1145 priv = GET_PRIV (contact);
1147 return priv->is_user;
1151 empathy_contact_set_is_user (EmpathyContact *contact,
1154 EmpathyContactPriv *priv;
1156 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1158 priv = GET_PRIV (contact);
1160 if (priv->is_user == is_user)
1163 priv->is_user = is_user;
1165 g_object_notify (G_OBJECT (contact), "is-user");
1169 empathy_contact_is_online (EmpathyContact *contact)
1171 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1173 switch (empathy_contact_get_presence (contact))
1175 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1176 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1177 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1179 /* Contacts without presence are considered online so we can display IRC
1180 * contacts in rooms. */
1181 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1182 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1183 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1184 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1185 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1186 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1193 empathy_contact_get_status (EmpathyContact *contact)
1195 const gchar *message;
1197 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1199 message = empathy_contact_get_presence_message (contact);
1200 if (!EMP_STR_EMPTY (message))
1203 return empathy_presence_get_default_message (
1204 empathy_contact_get_presence (contact));
1208 empathy_contact_can_sms (EmpathyContact *contact)
1210 EmpathyContactPriv *priv;
1212 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1214 priv = GET_PRIV (contact);
1216 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1220 empathy_contact_can_voip (EmpathyContact *contact)
1222 EmpathyContactPriv *priv;
1224 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1226 priv = GET_PRIV (contact);
1228 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1229 EMPATHY_CAPABILITIES_VIDEO);
1233 empathy_contact_can_voip_audio (EmpathyContact *contact)
1235 EmpathyContactPriv *priv;
1237 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1239 priv = GET_PRIV (contact);
1241 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1245 empathy_contact_can_voip_video (EmpathyContact *contact)
1247 EmpathyContactPriv *priv;
1249 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1251 priv = GET_PRIV (contact);
1253 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1257 empathy_contact_can_send_files (EmpathyContact *contact)
1259 EmpathyContactPriv *priv;
1261 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1263 priv = GET_PRIV (contact);
1265 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1269 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1271 EmpathyContactPriv *priv;
1273 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1275 priv = GET_PRIV (contact);
1277 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1281 contact_has_log (EmpathyContact *contact)
1283 TplLogManager *manager;
1287 manager = tpl_log_manager_dup_singleton ();
1288 entity = tpl_entity_new (empathy_contact_get_id (contact),
1289 TPL_ENTITY_CONTACT, NULL, NULL);
1291 have_log = tpl_log_manager_exists (manager,
1292 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1294 g_object_unref (entity);
1295 g_object_unref (manager);
1301 empathy_contact_can_do_action (EmpathyContact *self,
1302 EmpathyActionType action_type)
1304 gboolean sensitivity = FALSE;
1306 switch (action_type)
1308 case EMPATHY_ACTION_CHAT:
1311 case EMPATHY_ACTION_SMS:
1312 sensitivity = empathy_contact_can_sms (self);
1314 case EMPATHY_ACTION_AUDIO_CALL:
1315 sensitivity = empathy_contact_can_voip_audio (self);
1317 case EMPATHY_ACTION_VIDEO_CALL:
1318 sensitivity = empathy_contact_can_voip_video (self);
1320 case EMPATHY_ACTION_VIEW_LOGS:
1321 sensitivity = contact_has_log (self);
1323 case EMPATHY_ACTION_SEND_FILE:
1324 sensitivity = empathy_contact_can_send_files (self);
1326 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1327 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1330 g_assert_not_reached ();
1333 return (sensitivity ? TRUE : FALSE);
1337 contact_get_avatar_filename (EmpathyContact *contact,
1343 gchar *token_escaped;
1345 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1348 token_escaped = tp_escape_as_identifier (token);
1349 account = empathy_contact_get_account (contact);
1351 avatar_path = g_build_filename (g_get_user_cache_dir (),
1354 tp_account_get_cm_name (account),
1355 tp_account_get_protocol_name (account),
1357 g_mkdir_with_parents (avatar_path, 0700);
1359 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1361 g_free (token_escaped);
1362 g_free (avatar_path);
1368 contact_load_avatar_cache (EmpathyContact *contact,
1371 EmpathyAvatar *avatar = NULL;
1375 GError *error = NULL;
1377 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1378 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1380 /* Load the avatar from file if it exists */
1381 filename = contact_get_avatar_filename (contact, token);
1382 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1384 if (!g_file_get_contents (filename, &data, &len, &error))
1386 DEBUG ("Failed to load avatar from cache: %s",
1387 error ? error->message : "No error given");
1388 g_clear_error (&error);
1394 DEBUG ("Avatar loaded from %s", filename);
1395 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1396 contact_set_avatar (contact, avatar);
1397 empathy_avatar_unref (avatar);
1403 return data != NULL;
1407 empathy_avatar_get_type (void)
1409 static GType type_id = 0;
1413 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1414 (GBoxedCopyFunc) empathy_avatar_ref,
1415 (GBoxedFreeFunc) empathy_avatar_unref);
1422 * empathy_avatar_new:
1423 * @data: the avatar data
1424 * @len: the size of avatar data
1425 * @format: the mime type of the avatar image
1426 * @filename: the filename where the avatar is stored in cache
1428 * Create a #EmpathyAvatar from the provided data.
1430 * Returns: a new #EmpathyAvatar
1433 empathy_avatar_new (const guchar *data,
1435 const gchar *format,
1436 const gchar *filename)
1438 EmpathyAvatar *avatar;
1440 avatar = g_slice_new0 (EmpathyAvatar);
1441 avatar->data = g_memdup (data, len);
1443 avatar->format = g_strdup (format);
1444 avatar->filename = g_strdup (filename);
1445 avatar->refcount = 1;
1451 empathy_avatar_unref (EmpathyAvatar *avatar)
1453 g_return_if_fail (avatar != NULL);
1456 if (avatar->refcount == 0)
1458 g_free (avatar->data);
1459 g_free (avatar->format);
1460 g_free (avatar->filename);
1461 g_slice_free (EmpathyAvatar, avatar);
1466 empathy_avatar_ref (EmpathyAvatar *avatar)
1468 g_return_val_if_fail (avatar != NULL, NULL);
1476 * empathy_avatar_save_to_file:
1477 * @avatar: the avatar
1478 * @filename: name of a file to write avatar to
1479 * @error: return location for a GError, or NULL
1481 * Save the avatar to a file named filename
1483 * Returns: %TRUE on success, %FALSE if an error occurred
1486 empathy_avatar_save_to_file (EmpathyAvatar *self,
1487 const gchar *filename,
1490 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1495 * empathy_contact_get_location:
1496 * @contact: an #EmpathyContact
1498 * Returns the user's location if available. The keys are defined in
1499 * empathy-location.h. If the contact doesn't have location
1500 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1501 * you are done with the #GHashTable.
1503 * It is composed of string keys and GValues. Keys are
1504 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1505 * Example: a "city" key would have "Helsinki" as string GValue,
1506 * a "latitude" would have 65.0 as double GValue.
1508 * Returns: a #GHashTable of location values
1511 empathy_contact_get_location (EmpathyContact *contact)
1513 EmpathyContactPriv *priv;
1515 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1517 priv = GET_PRIV (contact);
1519 return priv->location;
1523 * empathy_contact_set_location:
1524 * @contact: an #EmpathyContact
1525 * @location: a #GHashTable of the location
1527 * Sets the user's location based on the location #GHashTable passed.
1528 * It is composed of string keys and GValues. Keys are
1529 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1530 * Example: a "city" key would have "Helsinki" as string GValue,
1531 * a "latitude" would have 65.0 as double GValue.
1534 empathy_contact_set_location (EmpathyContact *contact,
1535 GHashTable *location)
1537 EmpathyContactPriv *priv;
1539 g_return_if_fail (EMPATHY_CONTACT (contact));
1540 g_return_if_fail (location != NULL);
1542 priv = GET_PRIV (contact);
1544 if (priv->location != NULL)
1545 g_hash_table_unref (priv->location);
1547 priv->location = g_hash_table_ref (location);
1549 update_geocode (contact);
1551 g_object_notify (G_OBJECT (contact), "location");
1554 const gchar * const *
1555 empathy_contact_get_client_types (EmpathyContact *contact)
1557 EmpathyContactPriv *priv;
1559 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1561 priv = GET_PRIV (contact);
1563 return (const gchar * const *) priv->client_types;
1567 contact_set_client_types (EmpathyContact *contact,
1568 const gchar * const *client_types)
1570 EmpathyContactPriv *priv = GET_PRIV (contact);
1572 if (priv->client_types != NULL)
1573 g_strfreev (priv->client_types);
1575 priv->client_types = g_strdupv ((gchar **) client_types);
1576 g_object_notify (G_OBJECT (contact), "client-types");
1580 * empathy_contact_equal:
1581 * @contact1: an #EmpathyContact
1582 * @contact2: an #EmpathyContact
1584 * Returns FALSE if one of the contacts is NULL but the other is not.
1585 * Otherwise returns TRUE if both pointer are equal or if they bith
1586 * refer to the same id.
1587 * It's only necessary to call this function if your contact objects
1588 * come from logs where contacts are created dynamically and comparing
1589 * pointers is not enough.
1592 empathy_contact_equal (gconstpointer contact1,
1593 gconstpointer contact2)
1600 if ((contact1 == NULL) != (contact2 == NULL)) {
1603 if (contact1 == contact2) {
1606 c1 = EMPATHY_CONTACT (contact1);
1607 c2 = EMPATHY_CONTACT (contact2);
1608 id1 = empathy_contact_get_id (c1);
1609 id2 = empathy_contact_get_id (c2);
1610 if (!tp_strdiff (id1, id2)) {
1617 /* This callback is called by geocode-glib when it found a position
1618 * for the given address. A position is necessary for a contact
1619 * to show up on the map
1622 geocode_cb (GObject *source,
1623 GAsyncResult *result,
1626 EmpathyContact *contact = user_data;
1627 EmpathyContactPriv *priv = GET_PRIV (contact);
1628 GError *error = NULL;
1629 GHashTable *new_location;
1630 GHashTable *resolved = NULL;
1631 gdouble latitude, longitude;
1633 if (priv->location == NULL)
1636 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1639 if (resolved == NULL)
1641 DEBUG ("Failed to resolve geocode: %s", error->message);
1642 g_error_free (error);
1646 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1649 new_location = tp_asv_new (
1650 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1651 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1654 DEBUG ("\t - Latitude: %f", latitude);
1655 DEBUG ("\t - Longitude: %f", longitude);
1657 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1658 * the values we just set. */
1659 tp_g_hash_table_update (new_location, priv->location,
1660 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1662 /* Don't change the accuracy as we used an address to get this position */
1663 g_hash_table_unref (priv->location);
1664 priv->location = new_location;
1665 g_object_notify ((GObject *) contact, "location");
1668 tp_clear_pointer (&resolved, g_hash_table_unref);
1669 g_object_unref (contact);
1673 update_geocode (EmpathyContact *contact)
1675 GeocodeObject *geocode;
1676 GHashTable *location;
1678 location = empathy_contact_get_location (contact);
1679 if (location == NULL ||
1680 g_hash_table_size (location) == 0)
1683 /* No need to search for position if contact published it */
1684 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1685 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1688 geocode = geocode_object_new_for_params (location);
1689 if (geocode == NULL)
1692 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1693 g_object_ref (contact));
1695 g_object_unref (geocode);
1699 static EmpathyCapabilities
1700 tp_caps_to_capabilities (TpCapabilities *caps)
1702 EmpathyCapabilities capabilities = 0;
1704 if (tp_capabilities_supports_file_transfer (caps))
1705 capabilities |= EMPATHY_CAPABILITIES_FT;
1707 if (tp_capabilities_supports_stream_tubes (caps, TP_HANDLE_TYPE_CONTACT,
1709 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1711 if (tp_capabilities_supports_audio_video_call (caps, TP_HANDLE_TYPE_CONTACT))
1713 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1714 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1716 else if (tp_capabilities_supports_audio_call (caps, TP_HANDLE_TYPE_CONTACT))
1718 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1721 if (tp_capabilities_supports_sms (caps))
1722 capabilities |= EMPATHY_CAPABILITIES_SMS;
1724 return capabilities;
1728 set_capabilities_from_tp_caps (EmpathyContact *self,
1729 TpCapabilities *caps)
1731 EmpathyCapabilities capabilities;
1736 capabilities = tp_caps_to_capabilities (caps);
1737 empathy_contact_set_capabilities (self, capabilities);
1741 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1743 EmpathyContactPriv *priv = GET_PRIV (contact);
1747 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1748 file = tp_contact_get_avatar_file (priv->tp_contact);
1752 EmpathyAvatar *avatar;
1756 GError *error = NULL;
1758 if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
1760 DEBUG ("Failed to load avatar: %s", error->message);
1762 g_error_free (error);
1763 contact_set_avatar (contact, NULL);
1767 path = g_file_get_path (file);
1769 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1771 contact_set_avatar (contact, avatar);
1772 empathy_avatar_unref (avatar);
1778 contact_set_avatar (contact, NULL);
1783 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1785 EmpathyContact *contact = NULL;
1787 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1789 if (contacts_table == NULL)
1790 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1792 contact = g_hash_table_lookup (contacts_table, tp_contact);
1794 if (contact == NULL)
1796 contact = empathy_contact_new (tp_contact);
1798 /* The hash table does not keep any ref.
1799 * contact keeps a ref to tp_contact, and is removed from the table in
1800 * contact_dispose() */
1801 g_hash_table_insert (contacts_table, tp_contact, contact);
1805 g_object_ref (contact);
1812 presence_cmp_func (EmpathyContact *a,
1815 FolksPresenceDetails *presence_a, *presence_b;
1817 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1818 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1820 /* We negate the result because we're sorting in reverse order (i.e. such that
1821 * the Personas with the highest presence are at the beginning of the list. */
1822 return -folks_presence_details_typecmp (
1823 folks_presence_details_get_presence_type (presence_a),
1824 folks_presence_details_get_presence_type (presence_b));
1828 voip_cmp_func (EmpathyContact *a,
1831 gboolean has_audio_a, has_audio_b;
1832 gboolean has_video_a, has_video_b;
1834 has_audio_a = empathy_contact_can_voip_audio (a);
1835 has_audio_b = empathy_contact_can_voip_audio (b);
1836 has_video_a = empathy_contact_can_voip_video (a);
1837 has_video_b = empathy_contact_can_voip_video (b);
1839 /* First check video */
1840 if (has_video_a == has_video_b)
1842 /* Use audio to to break tie */
1843 if (has_audio_a == has_audio_b)
1845 else if (has_audio_a)
1850 else if (has_video_a)
1857 ft_cmp_func (EmpathyContact *a,
1860 gboolean can_send_files_a, can_send_files_b;
1862 can_send_files_a = empathy_contact_can_send_files (a);
1863 can_send_files_b = empathy_contact_can_send_files (b);
1865 if (can_send_files_a == can_send_files_b)
1867 else if (can_send_files_a)
1874 rfb_stream_tube_cmp_func (EmpathyContact *a,
1877 gboolean rfb_a, rfb_b;
1879 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1880 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1890 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1891 * the same presence, prefer the one which can do both audio *and* video calls,
1892 * over the one which can only do one of the two. */
1894 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1896 gint presence_sort = presence_cmp_func (a, b);
1898 if (presence_sort != 0)
1899 return presence_sort;
1901 return voip_cmp_func (a, b);
1904 /* Sort by presence as with presence_cmp_func() and then break ties using the
1905 * most "capable" individual. So users will be able to pick more actions on
1906 * the contact in the "Contact" menu of the chat window. */
1908 chat_sort_func (EmpathyContact *a,
1913 result = presence_cmp_func (a, b);
1917 /* Prefer individual supporting file transfer */
1918 result = ft_cmp_func (a, b);
1922 /* Check audio/video capabilities */
1923 result = voip_cmp_func (a, b);
1927 /* Check 'Share my destop' feature */
1928 return rfb_stream_tube_cmp_func (a, b);
1932 get_sort_func_for_action (EmpathyActionType action_type)
1934 switch (action_type)
1936 case EMPATHY_ACTION_AUDIO_CALL:
1937 case EMPATHY_ACTION_VIDEO_CALL:
1938 return (GCompareFunc) voip_sort_func;
1939 case EMPATHY_ACTION_CHAT:
1940 return (GCompareFunc) chat_sort_func;
1941 case EMPATHY_ACTION_VIEW_LOGS:
1942 case EMPATHY_ACTION_SEND_FILE:
1943 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1945 return (GCompareFunc) presence_cmp_func;
1950 * empathy_contact_dup_best_for_action:
1951 * @individual: a #FolksIndividual
1952 * @action_type: the type of action to be performed on the contact
1954 * Chooses a #FolksPersona from the given @individual which is best-suited for
1955 * the given @action_type. "Best-suited" is determined by choosing the persona
1956 * with the highest presence out of all the personas which can perform the given
1957 * @action_type (e.g. are capable of video calling).
1959 * Return value: an #EmpathyContact for the best persona, or %NULL;
1960 * unref with g_object_unref()
1963 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1964 EmpathyActionType action_type)
1969 EmpathyContact *best_contact = NULL;
1971 /* Build a list of EmpathyContacts that we can sort */
1972 personas = folks_individual_get_personas (individual);
1975 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1976 while (gee_iterator_next (iter))
1978 FolksPersona *persona = gee_iterator_get (iter);
1979 TpContact *tp_contact;
1980 EmpathyContact *contact = NULL;
1982 if (!empathy_folks_persona_is_interesting (persona))
1985 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1986 if (tp_contact == NULL)
1989 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1990 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
1992 /* Only choose the contact if they're actually capable of the specified
1994 if (empathy_contact_can_do_action (contact, action_type))
1995 contacts = g_list_prepend (contacts, g_object_ref (contact));
1998 g_clear_object (&contact);
1999 g_clear_object (&persona);
2001 g_clear_object (&iter);
2003 /* Sort the contacts by some heuristic based on the action type, then take
2004 * the top contact. */
2005 if (contacts != NULL)
2007 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2008 best_contact = g_object_ref (contacts->data);
2011 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2012 g_list_free (contacts);
2014 return best_contact;
2017 #define declare_contact_cb(name) \
2019 contact_##name##_cb (GObject *source, \
2020 GAsyncResult *result, \
2021 gpointer user_data) \
2023 TpContact *contact = (TpContact *) source; \
2024 GError *error = NULL; \
2026 if (!tp_contact_##name##_finish (contact, result, &error)) \
2028 DEBUG ("Failed to ##name## on %s\n", \
2029 tp_contact_get_identifier (contact)); \
2030 g_error_free (error); \
2034 declare_contact_cb (request_subscription)
2035 declare_contact_cb (authorize_publication)
2036 declare_contact_cb (unblock)
2039 empathy_contact_add_to_contact_list (EmpathyContact *self,
2040 const gchar *message)
2042 EmpathyContactPriv *priv = GET_PRIV (self);
2044 g_return_if_fail (priv->tp_contact != NULL);
2046 tp_contact_request_subscription_async (priv->tp_contact, message,
2047 contact_request_subscription_cb, NULL);
2049 tp_contact_authorize_publication_async (priv->tp_contact,
2050 contact_authorize_publication_cb, NULL);
2052 tp_contact_unblock_async (priv->tp_contact, contact_unblock_cb, NULL);
2055 declare_contact_cb (remove)
2058 empathy_contact_remove_from_contact_list (EmpathyContact *self)
2060 EmpathyContactPriv *priv = GET_PRIV (self);
2062 g_return_if_fail (priv->tp_contact != NULL);
2064 tp_contact_remove_async (priv->tp_contact, contact_remove_cb, NULL);