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>
23 #include "empathy-contact.h"
26 #include <geocode-glib/geocode-glib.h>
29 #include "empathy-location.h"
30 #include "empathy-utils.h"
31 #include "empathy-enum-types.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
34 #include "empathy-debug.h"
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
38 TpContact *tp_contact;
40 FolksPersona *persona;
44 EmpathyAvatar *avatar;
45 TpConnectionPresenceType presence;
47 EmpathyCapabilities capabilities;
49 /* Location is composed of string keys and GValues.
50 * Example: a "city" key would have "Helsinki" as string GValue,
51 * a "latitude" would have 65.0 as double GValue.
53 * This is a super set of the location stored in TpContact as we can try add
54 * more fields by searching the address using geoclue.
61 static void contact_finalize (GObject *object);
62 static void contact_get_property (GObject *object, guint param_id,
63 GValue *value, GParamSpec *pspec);
64 static void contact_set_property (GObject *object, guint param_id,
65 const GValue *value, GParamSpec *pspec);
68 static void update_geocode (EmpathyContact *contact);
71 static void empathy_contact_set_location (EmpathyContact *contact,
72 GHashTable *location);
74 static void contact_set_client_types (EmpathyContact *contact,
75 const gchar * const *types);
77 static void set_capabilities_from_tp_caps (EmpathyContact *self,
78 TpCapabilities *caps);
80 static void contact_set_avatar (EmpathyContact *contact,
81 EmpathyAvatar *avatar);
82 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
83 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
86 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
99 PROP_PRESENCE_MESSAGE,
112 static guint signals[LAST_SIGNAL];
114 /* TpContact* -> EmpathyContact*, both borrowed ref */
115 static GHashTable *contacts_table = NULL;
118 tp_contact_notify_cb (TpContact *tp_contact,
122 EmpathyContactPriv *priv = GET_PRIV (contact);
124 /* Forward property notifications */
125 if (!tp_strdiff (param->name, "alias"))
126 g_object_notify (contact, "alias");
127 else if (!tp_strdiff (param->name, "presence-type")) {
128 TpConnectionPresenceType presence;
130 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
131 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
133 priv->presence = presence;
134 g_object_notify (contact, "presence");
136 else if (!tp_strdiff (param->name, "identifier"))
137 g_object_notify (contact, "id");
138 else if (!tp_strdiff (param->name, "handle"))
139 g_object_notify (contact, "handle");
140 else if (!tp_strdiff (param->name, "location"))
142 GHashTable *location;
144 location = tp_contact_get_location (tp_contact);
145 /* This will start a geoclue search to find the address if needed */
146 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
148 else if (!tp_strdiff (param->name, "capabilities"))
150 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
151 tp_contact_get_capabilities (tp_contact));
153 else if (!tp_strdiff (param->name, "avatar-file"))
155 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
157 else if (!tp_strdiff (param->name, "client-types"))
159 contact_set_client_types (EMPATHY_CONTACT (contact),
160 tp_contact_get_client_types (tp_contact));
165 folks_persona_notify_cb (FolksPersona *folks_persona,
169 if (!tp_strdiff (param->name, "presence-message"))
170 g_object_notify (contact, "presence-message");
174 contact_dispose (GObject *object)
176 EmpathyContactPriv *priv = GET_PRIV (object);
178 if (priv->tp_contact != NULL)
180 g_signal_handlers_disconnect_by_func (priv->tp_contact,
181 tp_contact_notify_cb, object);
183 tp_clear_object (&priv->tp_contact);
186 g_object_unref (priv->account);
187 priv->account = NULL;
191 g_signal_handlers_disconnect_by_func (priv->persona,
192 folks_persona_notify_cb, object);
193 g_object_unref (priv->persona);
195 priv->persona = NULL;
197 if (priv->avatar != NULL)
199 empathy_avatar_unref (priv->avatar);
203 if (priv->location != NULL)
205 g_hash_table_unref (priv->location);
206 priv->location = NULL;
209 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
213 contact_constructed (GObject *object)
215 EmpathyContact *contact = (EmpathyContact *) object;
216 EmpathyContactPriv *priv = GET_PRIV (contact);
217 GHashTable *location;
218 TpContact *self_contact;
219 const gchar * const *client_types;
221 if (priv->tp_contact == NULL)
224 priv->presence = empathy_contact_get_presence (contact);
226 location = tp_contact_get_location (priv->tp_contact);
227 if (location != NULL)
228 empathy_contact_set_location (contact, location);
230 client_types = tp_contact_get_client_types (priv->tp_contact);
231 if (client_types != NULL)
232 contact_set_client_types (contact, client_types);
234 set_capabilities_from_tp_caps (contact,
235 tp_contact_get_capabilities (priv->tp_contact));
237 contact_set_avatar_from_tp_contact (contact);
239 /* Set is-user property. Note that it could still be the handle is
240 * different from the connection's self handle, in the case the handle
241 * comes from a group interface. */
242 self_contact = tp_connection_get_self_contact (
243 tp_contact_get_connection (priv->tp_contact));
244 empathy_contact_set_is_user (contact, self_contact == priv->tp_contact);
246 g_signal_connect (priv->tp_contact, "notify",
247 G_CALLBACK (tp_contact_notify_cb), contact);
251 empathy_contact_class_init (EmpathyContactClass *class)
253 GObjectClass *object_class;
255 object_class = G_OBJECT_CLASS (class);
257 object_class->finalize = contact_finalize;
258 object_class->dispose = contact_dispose;
259 object_class->get_property = contact_get_property;
260 object_class->set_property = contact_set_property;
261 object_class->constructed = contact_constructed;
263 g_object_class_install_property (object_class,
265 g_param_spec_object ("tp-contact",
267 "The TpContact associated with the contact",
269 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (object_class,
273 g_param_spec_object ("account",
275 "The account associated with the contact",
277 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (object_class,
281 g_param_spec_object ("persona",
283 "The FolksPersona associated with the contact",
285 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (object_class,
289 g_param_spec_string ("id",
291 "String identifying contact",
293 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (object_class,
297 g_param_spec_string ("alias",
299 "An alias for the contact",
301 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (object_class,
305 g_param_spec_string ("logged-alias",
307 "The alias the user had when a message was logged, "
308 "only set when using empathy_contact_from_tpl_contact()",
310 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (object_class,
314 g_param_spec_boxed ("avatar",
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (object_class,
322 g_param_spec_uint ("presence",
324 "Presence of contact",
325 TP_CONNECTION_PRESENCE_TYPE_UNSET,
326 NUM_TP_CONNECTION_PRESENCE_TYPES,
327 TP_CONNECTION_PRESENCE_TYPE_UNSET,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (object_class,
331 PROP_PRESENCE_MESSAGE,
332 g_param_spec_string ("presence-message",
333 "Contact presence message",
334 "Presence message of contact",
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338 g_object_class_install_property (object_class,
340 g_param_spec_uint ("handle",
342 "The handle of the contact",
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (object_class,
350 g_param_spec_flags ("capabilities",
351 "Contact Capabilities",
352 "Capabilities of the contact",
353 EMPATHY_TYPE_CAPABILITIES,
354 EMPATHY_CAPABILITIES_UNKNOWN,
355 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (object_class,
359 g_param_spec_boolean ("is-user",
361 "Is contact the user",
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (object_class,
368 g_param_spec_boxed ("location",
370 "Physical location of the contact",
372 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (object_class,
376 g_param_spec_boxed ("client-types",
377 "Contact client types",
378 "Client types of the contact",
380 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
382 signals[PRESENCE_CHANGED] =
383 g_signal_new ("presence-changed",
384 G_TYPE_FROM_CLASS (class),
388 g_cclosure_marshal_generic,
393 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
397 empathy_contact_init (EmpathyContact *contact)
399 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
400 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
402 contact->priv = priv;
404 priv->location = NULL;
405 priv->client_types = NULL;
410 contact_finalize (GObject *object)
412 EmpathyContactPriv *priv;
414 priv = GET_PRIV (object);
416 DEBUG ("finalize: %p", object);
418 g_clear_object (&priv->groups);
419 g_free (priv->alias);
420 g_free (priv->logged_alias);
422 g_strfreev (priv->client_types);
424 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
428 empathy_contact_set_capabilities (EmpathyContact *contact,
429 EmpathyCapabilities capabilities)
431 EmpathyContactPriv *priv;
433 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
435 priv = GET_PRIV (contact);
437 if (priv->capabilities == capabilities)
440 priv->capabilities = capabilities;
442 g_object_notify (G_OBJECT (contact), "capabilities");
446 empathy_contact_set_id (EmpathyContact *contact,
449 EmpathyContactPriv *priv;
451 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
452 g_return_if_fail (id != NULL);
454 priv = GET_PRIV (contact);
456 /* We temporally ref the contact because it could be destroyed
457 * during the signal emition */
458 g_object_ref (contact);
459 if (tp_strdiff (id, priv->id))
462 priv->id = g_strdup (id);
464 g_object_notify (G_OBJECT (contact), "id");
465 if (EMP_STR_EMPTY (priv->alias))
466 g_object_notify (G_OBJECT (contact), "alias");
469 g_object_unref (contact);
473 empathy_contact_set_presence (EmpathyContact *contact,
474 TpConnectionPresenceType presence)
476 EmpathyContactPriv *priv;
477 TpConnectionPresenceType old_presence;
479 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
481 priv = GET_PRIV (contact);
483 if (presence == priv->presence)
486 old_presence = priv->presence;
487 priv->presence = presence;
489 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
491 g_object_notify (G_OBJECT (contact), "presence");
495 empathy_contact_set_presence_message (EmpathyContact *contact,
496 const gchar *message)
498 EmpathyContactPriv *priv = GET_PRIV (contact);
500 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
502 if (priv->persona != NULL)
504 folks_presence_details_set_presence_message (
505 FOLKS_PRESENCE_DETAILS (priv->persona), message);
510 empathy_contact_set_handle (EmpathyContact *contact,
513 EmpathyContactPriv *priv;
515 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
517 priv = GET_PRIV (contact);
519 g_object_ref (contact);
520 if (handle != priv->handle)
522 priv->handle = handle;
523 g_object_notify (G_OBJECT (contact), "handle");
525 g_object_unref (contact);
529 contact_get_property (GObject *object,
534 EmpathyContact *contact = EMPATHY_CONTACT (object);
538 case PROP_TP_CONTACT:
539 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
542 g_value_set_object (value, empathy_contact_get_account (contact));
545 g_value_set_object (value, empathy_contact_get_persona (contact));
548 g_value_set_string (value, empathy_contact_get_id (contact));
551 g_value_set_string (value, empathy_contact_get_alias (contact));
553 case PROP_LOGGED_ALIAS:
554 g_value_set_string (value, empathy_contact_get_logged_alias (contact));
557 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
560 g_value_set_uint (value, empathy_contact_get_presence (contact));
562 case PROP_PRESENCE_MESSAGE:
563 g_value_set_string (value, empathy_contact_get_presence_message (contact));
566 g_value_set_uint (value, empathy_contact_get_handle (contact));
568 case PROP_CAPABILITIES:
569 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
572 g_value_set_boolean (value, empathy_contact_is_user (contact));
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
581 contact_set_property (GObject *object,
586 EmpathyContact *contact = EMPATHY_CONTACT (object);
587 EmpathyContactPriv *priv = GET_PRIV (object);
591 case PROP_TP_CONTACT:
592 priv->tp_contact = g_value_dup_object (value);
595 g_assert (priv->account == NULL);
596 priv->account = g_value_dup_object (value);
599 empathy_contact_set_persona (contact, g_value_get_object (value));
602 empathy_contact_set_id (contact, g_value_get_string (value));
605 empathy_contact_set_alias (contact, g_value_get_string (value));
607 case PROP_LOGGED_ALIAS:
608 g_assert (priv->logged_alias == NULL);
609 priv->logged_alias = g_value_dup_string (value);
612 empathy_contact_set_presence (contact, g_value_get_uint (value));
614 case PROP_PRESENCE_MESSAGE:
615 empathy_contact_set_presence_message (contact, g_value_get_string (value));
618 empathy_contact_set_handle (contact, g_value_get_uint (value));
620 case PROP_CAPABILITIES:
621 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
624 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
627 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
633 remove_tp_contact (gpointer data,
636 g_hash_table_remove (contacts_table, data);
639 static EmpathyContact *
640 empathy_contact_new (TpContact *tp_contact)
642 EmpathyContact *retval;
644 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
646 retval = g_object_new (EMPATHY_TYPE_CONTACT,
647 "tp-contact", tp_contact,
650 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
662 contact_is_tpl_entity (gpointer key,
666 EmpathyContact *contact = value;
667 FindContactData *data = user_data;
668 TpAccount *account = empathy_contact_get_account (contact);
669 const gchar *path = NULL;
672 path = tp_proxy_get_object_path (account);
674 return !tp_strdiff (empathy_contact_get_id (contact),
675 tpl_entity_get_identifier (data->entity)) &&
676 !tp_strdiff (tp_proxy_get_object_path (data->account), path);
680 get_contacts_cb (GObject *source,
681 GAsyncResult *result,
684 TpWeakRef *wr = user_data;
685 EmpathyContactPriv *priv;
686 EmpathyContact *self;
688 self = tp_weak_ref_dup_object (wr);
692 priv = GET_PRIV (self);
694 g_return_if_fail (priv->tp_contact == NULL);
696 priv->tp_contact = tp_connection_dup_contact_by_id_finish (
697 TP_CONNECTION (source), result, NULL);
698 if (priv->tp_contact == NULL)
701 g_object_notify (G_OBJECT (self), "tp-contact");
703 /* Update capabilities now that we have a TpContact */
704 set_capabilities_from_tp_caps (self,
705 tp_contact_get_capabilities (priv->tp_contact));
708 g_clear_object (&self);
709 tp_weak_ref_destroy (wr);
713 empathy_contact_from_tpl_contact (TpAccount *account,
714 TplEntity *tpl_entity)
716 EmpathyContact *retval;
718 EmpathyContact *existing_contact = NULL;
720 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
722 if (contacts_table != NULL)
724 FindContactData data;
726 data.entity = tpl_entity;
727 data.account = account;
729 existing_contact = g_hash_table_find (contacts_table,
730 contact_is_tpl_entity, &data);
733 if (existing_contact != NULL)
735 retval = g_object_new (EMPATHY_TYPE_CONTACT,
736 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
737 "logged-alias", tpl_entity_get_alias (tpl_entity),
745 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
747 id = tpl_entity_get_identifier (tpl_entity);
749 retval = g_object_new (EMPATHY_TYPE_CONTACT,
751 "alias", tpl_entity_get_alias (tpl_entity),
756 /* Try to get a TpContact associated to have at least contact
757 * capabilities if possible. This is useful for CM supporting calling
758 * offline contacts for example. */
759 conn = tp_account_get_connection (account);
762 TpContactFeature features[] = { TP_CONTACT_FEATURE_CAPABILITIES };
763 conn = tp_account_get_connection (account);
765 tp_connection_dup_contact_by_id_async (conn, id,
766 G_N_ELEMENTS (features), features, get_contacts_cb,
767 tp_weak_ref_new (retval, NULL, NULL));
771 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
772 contact_load_avatar_cache (retval,
773 tpl_entity_get_avatar_token (tpl_entity));
779 empathy_contact_get_tp_contact (EmpathyContact *contact)
781 EmpathyContactPriv *priv;
783 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
785 priv = GET_PRIV (contact);
787 return priv->tp_contact;
791 empathy_contact_get_id (EmpathyContact *contact)
793 EmpathyContactPriv *priv;
795 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
797 priv = GET_PRIV (contact);
799 if (priv->tp_contact != NULL)
800 return tp_contact_get_identifier (priv->tp_contact);
806 empathy_contact_get_alias (EmpathyContact *contact)
808 EmpathyContactPriv *priv;
809 const gchar *alias = NULL;
811 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
813 priv = GET_PRIV (contact);
815 if (!EMP_STR_EMPTY (priv->alias))
817 else if (priv->tp_contact != NULL)
818 alias = tp_contact_get_alias (priv->tp_contact);
820 if (!EMP_STR_EMPTY (alias))
823 return empathy_contact_get_id (contact);
827 empathy_contact_get_logged_alias (EmpathyContact *contact)
829 EmpathyContactPriv *priv;
831 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
833 priv = GET_PRIV (contact);
835 if (priv->logged_alias != NULL)
836 return priv->logged_alias;
838 return empathy_contact_get_alias (contact);
842 empathy_contact_set_alias (EmpathyContact *contact,
845 EmpathyContactPriv *priv;
846 FolksPersona *persona;
848 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
850 priv = GET_PRIV (contact);
852 g_object_ref (contact);
854 /* Set the alias on the persona if possible */
855 persona = empathy_contact_get_persona (contact);
856 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
858 DEBUG ("Setting alias for contact %s to %s",
859 empathy_contact_get_id (contact), alias);
861 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
864 if (tp_strdiff (alias, priv->alias))
866 g_free (priv->alias);
867 priv->alias = g_strdup (alias);
868 g_object_notify (G_OBJECT (contact), "alias");
871 g_object_unref (contact);
875 groups_change_group_cb (GObject *source,
876 GAsyncResult *result,
879 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
880 GError *error = NULL;
882 folks_group_details_change_group_finish (group_details, result, &error);
885 g_warning ("failed to change group: %s", error->message);
886 g_clear_error (&error);
891 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
894 EmpathyContactPriv *priv;
895 FolksPersona *persona;
897 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
898 g_return_if_fail (group != NULL);
900 priv = GET_PRIV (contact);
902 /* Normally pass through the changes to the persona */
903 persona = empathy_contact_get_persona (contact);
906 if (FOLKS_IS_GROUP_DETAILS (persona))
907 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
908 is_member, groups_change_group_cb, contact);
912 /* If the persona doesn't exist yet, we have to cache the changes until it
914 if (priv->groups == NULL)
916 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
917 g_free, NULL, NULL, NULL, NULL, NULL, NULL);
920 gee_collection_add (GEE_COLLECTION (priv->groups), group);
924 empathy_contact_get_avatar (EmpathyContact *contact)
926 EmpathyContactPriv *priv;
928 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
930 priv = GET_PRIV (contact);
936 contact_set_avatar (EmpathyContact *contact,
937 EmpathyAvatar *avatar)
939 EmpathyContactPriv *priv;
941 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
943 priv = GET_PRIV (contact);
945 if (priv->avatar == avatar)
950 empathy_avatar_unref (priv->avatar);
955 priv->avatar = empathy_avatar_ref (avatar);
957 g_object_notify (G_OBJECT (contact), "avatar");
961 empathy_contact_get_account (EmpathyContact *contact)
963 EmpathyContactPriv *priv;
965 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
967 priv = GET_PRIV (contact);
969 if (priv->account == NULL && priv->tp_contact != NULL)
971 TpConnection *connection;
973 /* FIXME: This assume the account manager already exists */
974 connection = tp_contact_get_connection (priv->tp_contact);
976 g_object_ref (tp_connection_get_account (connection));
979 return priv->account;
983 empathy_contact_get_persona (EmpathyContact *contact)
985 EmpathyContactPriv *priv;
987 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
989 priv = GET_PRIV (contact);
991 if (priv->persona == NULL && priv->tp_contact != NULL)
995 persona = tpf_persona_dup_for_contact (priv->tp_contact);
998 empathy_contact_set_persona (contact, (FolksPersona *) persona);
999 g_object_unref (persona);
1003 return priv->persona;
1007 empathy_contact_set_persona (EmpathyContact *contact,
1008 FolksPersona *persona)
1010 EmpathyContactPriv *priv;
1012 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1013 g_return_if_fail (TPF_IS_PERSONA (persona));
1015 priv = GET_PRIV (contact);
1017 if (persona == priv->persona)
1020 if (priv->persona != NULL)
1022 g_signal_handlers_disconnect_by_func (priv->persona,
1023 folks_persona_notify_cb, contact);
1024 g_object_unref (priv->persona);
1026 priv->persona = g_object_ref (persona);
1028 g_signal_connect (priv->persona, "notify",
1029 G_CALLBACK (folks_persona_notify_cb), contact);
1031 g_object_notify (G_OBJECT (contact), "persona");
1033 /* Set the persona's alias, since ours could've been set using
1034 * empathy_contact_set_alias() before we had a persona; this happens when
1035 * adding a contact. */
1036 if (priv->alias != NULL)
1037 empathy_contact_set_alias (contact, priv->alias);
1039 /* Set the persona's groups */
1040 if (priv->groups != NULL)
1042 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1043 GEE_SET (priv->groups));
1044 g_object_unref (priv->groups);
1045 priv->groups = NULL;
1050 empathy_contact_get_connection (EmpathyContact *contact)
1052 EmpathyContactPriv *priv;
1054 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1056 priv = GET_PRIV (contact);
1058 if (priv->tp_contact != NULL)
1059 return tp_contact_get_connection (priv->tp_contact);
1064 TpConnectionPresenceType
1065 empathy_contact_get_presence (EmpathyContact *contact)
1067 EmpathyContactPriv *priv;
1069 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1070 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1072 priv = GET_PRIV (contact);
1074 if (priv->tp_contact != NULL)
1075 return tp_contact_get_presence_type (priv->tp_contact);
1077 return priv->presence;
1081 empathy_contact_get_presence_message (EmpathyContact *contact)
1083 EmpathyContactPriv *priv;
1085 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1087 priv = GET_PRIV (contact);
1089 if (priv->persona != NULL)
1090 return folks_presence_details_get_presence_message (
1091 FOLKS_PRESENCE_DETAILS (priv->persona));
1093 if (priv->tp_contact != NULL)
1094 return tp_contact_get_presence_message (priv->tp_contact);
1100 empathy_contact_get_handle (EmpathyContact *contact)
1102 EmpathyContactPriv *priv;
1104 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1106 priv = GET_PRIV (contact);
1108 if (priv->tp_contact != NULL)
1109 return tp_contact_get_handle (priv->tp_contact);
1111 return priv->handle;
1115 empathy_contact_get_capabilities (EmpathyContact *contact)
1117 EmpathyContactPriv *priv;
1119 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1121 priv = GET_PRIV (contact);
1123 return priv->capabilities;
1127 empathy_contact_is_user (EmpathyContact *contact)
1129 EmpathyContactPriv *priv;
1131 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1133 priv = GET_PRIV (contact);
1135 return priv->is_user;
1139 empathy_contact_set_is_user (EmpathyContact *contact,
1142 EmpathyContactPriv *priv;
1144 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1146 priv = GET_PRIV (contact);
1148 if (priv->is_user == is_user)
1151 priv->is_user = is_user;
1153 g_object_notify (G_OBJECT (contact), "is-user");
1157 empathy_contact_is_online (EmpathyContact *contact)
1159 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1161 switch (empathy_contact_get_presence (contact))
1163 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1164 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1165 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1167 /* Contacts without presence are considered online so we can display IRC
1168 * contacts in rooms. */
1169 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1170 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1171 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1172 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1173 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1174 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1181 empathy_contact_get_status (EmpathyContact *contact)
1183 const gchar *message;
1185 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1187 message = empathy_contact_get_presence_message (contact);
1188 if (!EMP_STR_EMPTY (message))
1191 return empathy_presence_get_default_message (
1192 empathy_contact_get_presence (contact));
1196 empathy_contact_can_sms (EmpathyContact *contact)
1198 EmpathyContactPriv *priv;
1200 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1202 priv = GET_PRIV (contact);
1204 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1208 empathy_contact_can_voip (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_AUDIO |
1217 EMPATHY_CAPABILITIES_VIDEO);
1221 empathy_contact_can_voip_audio (EmpathyContact *contact)
1223 EmpathyContactPriv *priv;
1225 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1227 priv = GET_PRIV (contact);
1229 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1233 empathy_contact_can_voip_video (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_VIDEO;
1245 empathy_contact_can_send_files (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_FT;
1257 empathy_contact_can_use_rfb_stream_tube (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_RFB_STREAM_TUBE;
1269 contact_has_log (EmpathyContact *contact)
1271 TplLogManager *manager;
1275 manager = tpl_log_manager_dup_singleton ();
1276 entity = tpl_entity_new (empathy_contact_get_id (contact),
1277 TPL_ENTITY_CONTACT, NULL, NULL);
1279 have_log = tpl_log_manager_exists (manager,
1280 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1282 g_object_unref (entity);
1283 g_object_unref (manager);
1289 empathy_contact_can_do_action (EmpathyContact *self,
1290 EmpathyActionType action_type)
1292 gboolean sensitivity = FALSE;
1294 switch (action_type)
1296 case EMPATHY_ACTION_CHAT:
1299 case EMPATHY_ACTION_SMS:
1300 sensitivity = empathy_contact_can_sms (self);
1302 case EMPATHY_ACTION_AUDIO_CALL:
1303 sensitivity = empathy_contact_can_voip_audio (self);
1305 case EMPATHY_ACTION_VIDEO_CALL:
1306 sensitivity = empathy_contact_can_voip_video (self);
1308 case EMPATHY_ACTION_VIEW_LOGS:
1309 sensitivity = contact_has_log (self);
1311 case EMPATHY_ACTION_SEND_FILE:
1312 sensitivity = empathy_contact_can_send_files (self);
1314 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1315 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1318 g_assert_not_reached ();
1321 return (sensitivity ? TRUE : FALSE);
1325 contact_get_avatar_filename (EmpathyContact *contact,
1331 gchar *token_escaped;
1333 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1336 token_escaped = tp_escape_as_identifier (token);
1337 account = empathy_contact_get_account (contact);
1339 avatar_path = g_build_filename (g_get_user_cache_dir (),
1342 tp_account_get_cm_name (account),
1343 tp_account_get_protocol_name (account),
1345 g_mkdir_with_parents (avatar_path, 0700);
1347 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1349 g_free (token_escaped);
1350 g_free (avatar_path);
1356 contact_load_avatar_cache (EmpathyContact *contact,
1359 EmpathyAvatar *avatar = NULL;
1363 GError *error = NULL;
1365 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1366 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1368 /* Load the avatar from file if it exists */
1369 filename = contact_get_avatar_filename (contact, token);
1370 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1372 if (!g_file_get_contents (filename, &data, &len, &error))
1374 DEBUG ("Failed to load avatar from cache: %s",
1375 error ? error->message : "No error given");
1376 g_clear_error (&error);
1382 DEBUG ("Avatar loaded from %s", filename);
1383 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1384 contact_set_avatar (contact, avatar);
1385 empathy_avatar_unref (avatar);
1391 return data != NULL;
1395 empathy_avatar_get_type (void)
1397 static GType type_id = 0;
1401 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1402 (GBoxedCopyFunc) empathy_avatar_ref,
1403 (GBoxedFreeFunc) empathy_avatar_unref);
1410 * empathy_avatar_new:
1411 * @data: the avatar data
1412 * @len: the size of avatar data
1413 * @format: the mime type of the avatar image
1414 * @filename: the filename where the avatar is stored in cache
1416 * Create a #EmpathyAvatar from the provided data.
1418 * Returns: a new #EmpathyAvatar
1421 empathy_avatar_new (const guchar *data,
1423 const gchar *format,
1424 const gchar *filename)
1426 EmpathyAvatar *avatar;
1428 avatar = g_slice_new0 (EmpathyAvatar);
1429 avatar->data = g_memdup (data, len);
1431 avatar->format = g_strdup (format);
1432 avatar->filename = g_strdup (filename);
1433 avatar->refcount = 1;
1439 empathy_avatar_unref (EmpathyAvatar *avatar)
1441 g_return_if_fail (avatar != NULL);
1444 if (avatar->refcount == 0)
1446 g_free (avatar->data);
1447 g_free (avatar->format);
1448 g_free (avatar->filename);
1449 g_slice_free (EmpathyAvatar, avatar);
1454 empathy_avatar_ref (EmpathyAvatar *avatar)
1456 g_return_val_if_fail (avatar != NULL, NULL);
1464 * empathy_avatar_save_to_file:
1465 * @avatar: the avatar
1466 * @filename: name of a file to write avatar to
1467 * @error: return location for a GError, or NULL
1469 * Save the avatar to a file named filename
1471 * Returns: %TRUE on success, %FALSE if an error occurred
1474 empathy_avatar_save_to_file (EmpathyAvatar *self,
1475 const gchar *filename,
1478 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1483 * empathy_contact_get_location:
1484 * @contact: an #EmpathyContact
1486 * Returns the user's location if available. The keys are defined in
1487 * empathy-location.h. If the contact doesn't have location
1488 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1489 * you are done with the #GHashTable.
1491 * It is composed of string keys and GValues. Keys are
1492 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1493 * Example: a "city" key would have "Helsinki" as string GValue,
1494 * a "latitude" would have 65.0 as double GValue.
1496 * Returns: a #GHashTable of location values
1499 empathy_contact_get_location (EmpathyContact *contact)
1501 EmpathyContactPriv *priv;
1503 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1505 priv = GET_PRIV (contact);
1507 return priv->location;
1511 * empathy_contact_set_location:
1512 * @contact: an #EmpathyContact
1513 * @location: a #GHashTable of the location
1515 * Sets the user's location based on the location #GHashTable passed.
1516 * It is composed of string keys and GValues. Keys are
1517 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1518 * Example: a "city" key would have "Helsinki" as string GValue,
1519 * a "latitude" would have 65.0 as double GValue.
1522 empathy_contact_set_location (EmpathyContact *contact,
1523 GHashTable *location)
1525 EmpathyContactPriv *priv;
1527 g_return_if_fail (EMPATHY_CONTACT (contact));
1528 g_return_if_fail (location != NULL);
1530 priv = GET_PRIV (contact);
1532 if (priv->location != NULL)
1533 g_hash_table_unref (priv->location);
1535 priv->location = g_hash_table_ref (location);
1537 update_geocode (contact);
1539 g_object_notify (G_OBJECT (contact), "location");
1542 const gchar * const *
1543 empathy_contact_get_client_types (EmpathyContact *contact)
1545 EmpathyContactPriv *priv;
1547 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1549 priv = GET_PRIV (contact);
1551 return (const gchar * const *) priv->client_types;
1555 contact_set_client_types (EmpathyContact *contact,
1556 const gchar * const *client_types)
1558 EmpathyContactPriv *priv = GET_PRIV (contact);
1560 if (priv->client_types != NULL)
1561 g_strfreev (priv->client_types);
1563 priv->client_types = g_strdupv ((gchar **) client_types);
1564 g_object_notify (G_OBJECT (contact), "client-types");
1568 * empathy_contact_equal:
1569 * @contact1: an #EmpathyContact
1570 * @contact2: an #EmpathyContact
1572 * Returns FALSE if one of the contacts is NULL but the other is not.
1573 * Otherwise returns TRUE if both pointer are equal or if they bith
1574 * refer to the same id.
1575 * It's only necessary to call this function if your contact objects
1576 * come from logs where contacts are created dynamically and comparing
1577 * pointers is not enough.
1580 empathy_contact_equal (gconstpointer contact1,
1581 gconstpointer contact2)
1588 if ((contact1 == NULL) != (contact2 == NULL)) {
1591 if (contact1 == contact2) {
1594 c1 = EMPATHY_CONTACT (contact1);
1595 c2 = EMPATHY_CONTACT (contact2);
1596 id1 = empathy_contact_get_id (c1);
1597 id2 = empathy_contact_get_id (c2);
1598 if (!tp_strdiff (id1, id2)) {
1605 /* This callback is called by geocode-glib when it found a position
1606 * for the given address. A position is necessary for a contact
1607 * to show up on the map
1610 geocode_cb (GObject *source,
1611 GAsyncResult *result,
1614 EmpathyContact *contact = user_data;
1615 EmpathyContactPriv *priv = GET_PRIV (contact);
1616 GError *error = NULL;
1618 GeocodeLocation *loc;
1619 GHashTable *new_location;
1620 GHashTable *resolved = NULL;
1622 if (priv->location == NULL)
1625 res = geocode_forward_search_finish (GEOCODE_FORWARD (source), result,
1630 DEBUG ("Failed to resolve geocode: %s", error->message);
1631 g_error_free (error);
1637 new_location = tp_asv_new (
1638 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, geocode_location_get_latitude (loc),
1639 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, geocode_location_get_longitude (loc),
1642 DEBUG ("\t - Latitude: %f", geocode_location_get_latitude (loc));
1643 DEBUG ("\t - Longitude: %f", geocode_location_get_longitude (loc));
1645 g_list_free_full (res, g_object_unref);
1647 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1648 * the values we just set. */
1649 tp_g_hash_table_update (new_location, priv->location,
1650 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1652 /* Don't change the accuracy as we used an address to get this position */
1653 g_hash_table_unref (priv->location);
1654 priv->location = new_location;
1655 g_object_notify ((GObject *) contact, "location");
1658 tp_clear_pointer (&resolved, g_hash_table_unref);
1659 g_object_unref (contact);
1663 update_geocode (EmpathyContact *contact)
1665 GeocodeForward *geocode;
1666 GHashTable *location;
1668 location = empathy_contact_get_location (contact);
1669 if (location == NULL ||
1670 g_hash_table_size (location) == 0)
1673 /* No need to search for position if contact published it */
1674 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1675 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1678 geocode = geocode_forward_new_for_params (location);
1679 if (geocode == NULL)
1682 geocode_forward_search_async (geocode, NULL, geocode_cb,
1683 g_object_ref (contact));
1685 g_object_unref (geocode);
1689 static EmpathyCapabilities
1690 tp_caps_to_capabilities (TpCapabilities *caps)
1692 EmpathyCapabilities capabilities = 0;
1694 if (tp_capabilities_supports_file_transfer (caps))
1695 capabilities |= EMPATHY_CAPABILITIES_FT;
1697 if (tp_capabilities_supports_stream_tubes (caps, TP_HANDLE_TYPE_CONTACT,
1699 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1701 if (tp_capabilities_supports_audio_video_call (caps, TP_HANDLE_TYPE_CONTACT))
1703 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1704 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1706 else if (tp_capabilities_supports_audio_call (caps, TP_HANDLE_TYPE_CONTACT))
1708 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1711 if (tp_capabilities_supports_sms (caps))
1712 capabilities |= EMPATHY_CAPABILITIES_SMS;
1714 return capabilities;
1718 set_capabilities_from_tp_caps (EmpathyContact *self,
1719 TpCapabilities *caps)
1721 EmpathyCapabilities capabilities;
1726 capabilities = tp_caps_to_capabilities (caps);
1727 empathy_contact_set_capabilities (self, capabilities);
1731 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1733 EmpathyContactPriv *priv = GET_PRIV (contact);
1737 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1738 file = tp_contact_get_avatar_file (priv->tp_contact);
1742 EmpathyAvatar *avatar;
1746 GError *error = NULL;
1748 if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
1750 DEBUG ("Failed to load avatar: %s", error->message);
1752 g_error_free (error);
1753 contact_set_avatar (contact, NULL);
1757 path = g_file_get_path (file);
1759 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1761 contact_set_avatar (contact, avatar);
1762 empathy_avatar_unref (avatar);
1768 contact_set_avatar (contact, NULL);
1773 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1775 EmpathyContact *contact = NULL;
1777 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1779 if (contacts_table == NULL)
1780 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1782 contact = g_hash_table_lookup (contacts_table, tp_contact);
1784 if (contact == NULL)
1786 contact = empathy_contact_new (tp_contact);
1788 /* The hash table does not keep any ref.
1789 * contact keeps a ref to tp_contact, and is removed from the table in
1790 * contact_dispose() */
1791 g_hash_table_insert (contacts_table, tp_contact, contact);
1795 g_object_ref (contact);
1802 presence_cmp_func (EmpathyContact *a,
1805 FolksPresenceDetails *presence_a, *presence_b;
1807 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1808 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1810 /* We negate the result because we're sorting in reverse order (i.e. such that
1811 * the Personas with the highest presence are at the beginning of the list. */
1812 return -folks_presence_details_typecmp (
1813 folks_presence_details_get_presence_type (presence_a),
1814 folks_presence_details_get_presence_type (presence_b));
1818 voip_cmp_func (EmpathyContact *a,
1821 gboolean has_audio_a, has_audio_b;
1822 gboolean has_video_a, has_video_b;
1824 has_audio_a = empathy_contact_can_voip_audio (a);
1825 has_audio_b = empathy_contact_can_voip_audio (b);
1826 has_video_a = empathy_contact_can_voip_video (a);
1827 has_video_b = empathy_contact_can_voip_video (b);
1829 /* First check video */
1830 if (has_video_a == has_video_b)
1832 /* Use audio to to break tie */
1833 if (has_audio_a == has_audio_b)
1835 else if (has_audio_a)
1840 else if (has_video_a)
1847 ft_cmp_func (EmpathyContact *a,
1850 gboolean can_send_files_a, can_send_files_b;
1852 can_send_files_a = empathy_contact_can_send_files (a);
1853 can_send_files_b = empathy_contact_can_send_files (b);
1855 if (can_send_files_a == can_send_files_b)
1857 else if (can_send_files_a)
1864 rfb_stream_tube_cmp_func (EmpathyContact *a,
1867 gboolean rfb_a, rfb_b;
1869 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1870 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1880 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1881 * the same presence, prefer the one which can do both audio *and* video calls,
1882 * over the one which can only do one of the two. */
1884 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1886 gint presence_sort = presence_cmp_func (a, b);
1888 if (presence_sort != 0)
1889 return presence_sort;
1891 return voip_cmp_func (a, b);
1894 /* Sort by presence as with presence_cmp_func() and then break ties using the
1895 * most "capable" individual. So users will be able to pick more actions on
1896 * the contact in the "Contact" menu of the chat window. */
1898 chat_sort_func (EmpathyContact *a,
1903 result = presence_cmp_func (a, b);
1907 /* Prefer individual supporting file transfer */
1908 result = ft_cmp_func (a, b);
1912 /* Check audio/video capabilities */
1913 result = voip_cmp_func (a, b);
1917 /* Check 'Share my destop' feature */
1918 return rfb_stream_tube_cmp_func (a, b);
1922 get_sort_func_for_action (EmpathyActionType action_type)
1924 switch (action_type)
1926 case EMPATHY_ACTION_AUDIO_CALL:
1927 case EMPATHY_ACTION_VIDEO_CALL:
1928 return (GCompareFunc) voip_sort_func;
1929 case EMPATHY_ACTION_CHAT:
1930 return (GCompareFunc) chat_sort_func;
1931 case EMPATHY_ACTION_VIEW_LOGS:
1932 case EMPATHY_ACTION_SEND_FILE:
1933 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1935 return (GCompareFunc) presence_cmp_func;
1940 * empathy_contact_dup_best_for_action:
1941 * @individual: a #FolksIndividual
1942 * @action_type: the type of action to be performed on the contact
1944 * Chooses a #FolksPersona from the given @individual which is best-suited for
1945 * the given @action_type. "Best-suited" is determined by choosing the persona
1946 * with the highest presence out of all the personas which can perform the given
1947 * @action_type (e.g. are capable of video calling).
1949 * Return value: an #EmpathyContact for the best persona, or %NULL;
1950 * unref with g_object_unref()
1953 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1954 EmpathyActionType action_type)
1959 EmpathyContact *best_contact = NULL;
1961 /* Build a list of EmpathyContacts that we can sort */
1962 personas = folks_individual_get_personas (individual);
1965 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1966 while (gee_iterator_next (iter))
1968 FolksPersona *persona = gee_iterator_get (iter);
1969 TpContact *tp_contact;
1970 EmpathyContact *contact = NULL;
1972 if (!empathy_folks_persona_is_interesting (persona))
1975 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1976 if (tp_contact == NULL)
1979 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1980 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
1982 /* Only choose the contact if they're actually capable of the specified
1984 if (empathy_contact_can_do_action (contact, action_type))
1985 contacts = g_list_prepend (contacts, g_object_ref (contact));
1988 g_clear_object (&contact);
1989 g_clear_object (&persona);
1991 g_clear_object (&iter);
1993 /* Sort the contacts by some heuristic based on the action type, then take
1994 * the top contact. */
1995 if (contacts != NULL)
1997 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
1998 best_contact = g_object_ref (contacts->data);
2001 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2002 g_list_free (contacts);
2004 return best_contact;
2007 #define declare_contact_cb(name) \
2009 contact_##name##_cb (GObject *source, \
2010 GAsyncResult *result, \
2011 gpointer user_data) \
2013 TpContact *contact = (TpContact *) source; \
2014 GError *error = NULL; \
2016 if (!tp_contact_##name##_finish (contact, result, &error)) \
2018 DEBUG ("Failed to ##name## on %s\n", \
2019 tp_contact_get_identifier (contact)); \
2020 g_error_free (error); \
2024 declare_contact_cb (request_subscription)
2025 declare_contact_cb (authorize_publication)
2026 declare_contact_cb (unblock)
2029 empathy_contact_add_to_contact_list (EmpathyContact *self,
2030 const gchar *message)
2032 EmpathyContactPriv *priv = GET_PRIV (self);
2034 g_return_if_fail (priv->tp_contact != NULL);
2036 tp_contact_request_subscription_async (priv->tp_contact, message,
2037 contact_request_subscription_cb, NULL);
2039 tp_contact_authorize_publication_async (priv->tp_contact,
2040 contact_authorize_publication_cb, NULL);
2042 tp_contact_unblock_async (priv->tp_contact, contact_unblock_cb, NULL);
2045 declare_contact_cb (remove)
2048 empathy_contact_remove_from_contact_list (EmpathyContact *self)
2050 EmpathyContactPriv *priv = GET_PRIV (self);
2052 g_return_if_fail (priv->tp_contact != NULL);
2054 tp_contact_remove_async (priv->tp_contact, contact_remove_cb, NULL);