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;
63 /* Location is composed of string keys and GValues.
64 * Example: a "city" key would have "Helsinki" as string GValue,
65 * a "latitude" would have 65.0 as double GValue.
67 * This is a super set of the location stored in TpContact as we can try add
68 * more fields by searching the address using geoclue.
75 static void contact_finalize (GObject *object);
76 static void contact_get_property (GObject *object, guint param_id,
77 GValue *value, GParamSpec *pspec);
78 static void contact_set_property (GObject *object, guint param_id,
79 const GValue *value, GParamSpec *pspec);
82 static void update_geocode (EmpathyContact *contact);
85 static void empathy_contact_set_location (EmpathyContact *contact,
86 GHashTable *location);
88 static void contact_set_client_types (EmpathyContact *contact,
89 const gchar * const *types);
91 static void set_capabilities_from_tp_caps (EmpathyContact *self,
92 TpCapabilities *caps);
94 static void contact_set_avatar (EmpathyContact *contact,
95 EmpathyAvatar *avatar);
96 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
97 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
100 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
113 PROP_PRESENCE_MESSAGE,
126 static guint signals[LAST_SIGNAL];
128 /* TpContact* -> EmpathyContact*, both borrowed ref */
129 static GHashTable *contacts_table = NULL;
132 tp_contact_notify_cb (TpContact *tp_contact,
136 EmpathyContactPriv *priv = GET_PRIV (contact);
138 /* Forward property notifications */
139 if (!tp_strdiff (param->name, "alias"))
140 g_object_notify (contact, "alias");
141 else if (!tp_strdiff (param->name, "presence-type")) {
142 TpConnectionPresenceType presence;
144 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
145 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
147 priv->presence = presence;
148 g_object_notify (contact, "presence");
150 else if (!tp_strdiff (param->name, "identifier"))
151 g_object_notify (contact, "id");
152 else if (!tp_strdiff (param->name, "handle"))
153 g_object_notify (contact, "handle");
154 else if (!tp_strdiff (param->name, "location"))
156 GHashTable *location;
158 location = tp_contact_get_location (tp_contact);
159 /* This will start a geoclue search to find the address if needed */
160 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
162 else if (!tp_strdiff (param->name, "capabilities"))
164 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
165 tp_contact_get_capabilities (tp_contact));
167 else if (!tp_strdiff (param->name, "avatar-file"))
169 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
171 else if (!tp_strdiff (param->name, "client-types"))
173 contact_set_client_types (EMPATHY_CONTACT (contact),
174 tp_contact_get_client_types (tp_contact));
179 folks_persona_notify_cb (FolksPersona *folks_persona,
183 if (!tp_strdiff (param->name, "presence-message"))
184 g_object_notify (contact, "presence-message");
188 contact_dispose (GObject *object)
190 EmpathyContactPriv *priv = GET_PRIV (object);
192 if (priv->tp_contact != NULL)
194 g_signal_handlers_disconnect_by_func (priv->tp_contact,
195 tp_contact_notify_cb, object);
197 tp_clear_object (&priv->tp_contact);
200 g_object_unref (priv->account);
201 priv->account = NULL;
205 g_signal_handlers_disconnect_by_func (priv->persona,
206 folks_persona_notify_cb, object);
207 g_object_unref (priv->persona);
209 priv->persona = NULL;
211 if (priv->avatar != NULL)
213 empathy_avatar_unref (priv->avatar);
217 if (priv->location != NULL)
219 g_hash_table_unref (priv->location);
220 priv->location = NULL;
223 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
227 contact_constructed (GObject *object)
229 EmpathyContact *contact = (EmpathyContact *) object;
230 EmpathyContactPriv *priv = GET_PRIV (contact);
231 GHashTable *location;
232 TpContact *self_contact;
233 const gchar * const *client_types;
235 if (priv->tp_contact == NULL)
238 priv->presence = empathy_contact_get_presence (contact);
240 location = tp_contact_get_location (priv->tp_contact);
241 if (location != NULL)
242 empathy_contact_set_location (contact, location);
244 client_types = tp_contact_get_client_types (priv->tp_contact);
245 if (client_types != NULL)
246 contact_set_client_types (contact, client_types);
248 set_capabilities_from_tp_caps (contact,
249 tp_contact_get_capabilities (priv->tp_contact));
251 contact_set_avatar_from_tp_contact (contact);
253 /* Set is-user property. Note that it could still be the handle is
254 * different from the connection's self handle, in the case the handle
255 * comes from a group interface. */
256 self_contact = tp_connection_get_self_contact (
257 tp_contact_get_connection (priv->tp_contact));
258 empathy_contact_set_is_user (contact, self_contact == priv->tp_contact);
260 g_signal_connect (priv->tp_contact, "notify",
261 G_CALLBACK (tp_contact_notify_cb), contact);
265 empathy_contact_class_init (EmpathyContactClass *class)
267 GObjectClass *object_class;
269 object_class = G_OBJECT_CLASS (class);
271 object_class->finalize = contact_finalize;
272 object_class->dispose = contact_dispose;
273 object_class->get_property = contact_get_property;
274 object_class->set_property = contact_set_property;
275 object_class->constructed = contact_constructed;
277 g_object_class_install_property (object_class,
279 g_param_spec_object ("tp-contact",
281 "The TpContact associated with the contact",
283 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property (object_class,
287 g_param_spec_object ("account",
289 "The account associated with the contact",
291 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
293 g_object_class_install_property (object_class,
295 g_param_spec_object ("persona",
297 "The FolksPersona associated with the contact",
299 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (object_class,
303 g_param_spec_string ("id",
305 "String identifying contact",
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (object_class,
311 g_param_spec_string ("alias",
313 "An alias for the contact",
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (object_class,
319 g_param_spec_string ("logged-alias",
321 "The alias the user had when a message was logged, "
322 "only set when using empathy_contact_from_tpl_contact()",
324 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (object_class,
328 g_param_spec_boxed ("avatar",
332 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (object_class,
336 g_param_spec_uint ("presence",
338 "Presence of contact",
339 TP_CONNECTION_PRESENCE_TYPE_UNSET,
340 NUM_TP_CONNECTION_PRESENCE_TYPES,
341 TP_CONNECTION_PRESENCE_TYPE_UNSET,
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (object_class,
345 PROP_PRESENCE_MESSAGE,
346 g_param_spec_string ("presence-message",
347 "Contact presence message",
348 "Presence message of contact",
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (object_class,
354 g_param_spec_uint ("handle",
356 "The handle of the contact",
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362 g_object_class_install_property (object_class,
364 g_param_spec_flags ("capabilities",
365 "Contact Capabilities",
366 "Capabilities of the contact",
367 EMPATHY_TYPE_CAPABILITIES,
368 EMPATHY_CAPABILITIES_UNKNOWN,
369 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (object_class,
373 g_param_spec_boolean ("is-user",
375 "Is contact the user",
377 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (object_class,
382 g_param_spec_boxed ("location",
384 "Physical location of the contact",
386 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (object_class,
390 g_param_spec_boxed ("client-types",
391 "Contact client types",
392 "Client types of the contact",
394 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
396 signals[PRESENCE_CHANGED] =
397 g_signal_new ("presence-changed",
398 G_TYPE_FROM_CLASS (class),
402 g_cclosure_marshal_generic,
407 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
411 empathy_contact_init (EmpathyContact *contact)
413 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
414 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
416 contact->priv = priv;
418 priv->location = NULL;
419 priv->client_types = NULL;
424 contact_finalize (GObject *object)
426 EmpathyContactPriv *priv;
428 priv = GET_PRIV (object);
430 DEBUG ("finalize: %p", object);
432 g_clear_object (&priv->groups);
433 g_free (priv->alias);
435 g_strfreev (priv->client_types);
437 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
441 empathy_contact_set_capabilities (EmpathyContact *contact,
442 EmpathyCapabilities capabilities)
444 EmpathyContactPriv *priv;
446 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
448 priv = GET_PRIV (contact);
450 if (priv->capabilities == capabilities)
453 priv->capabilities = capabilities;
455 g_object_notify (G_OBJECT (contact), "capabilities");
459 empathy_contact_set_id (EmpathyContact *contact,
462 EmpathyContactPriv *priv;
464 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
465 g_return_if_fail (id != NULL);
467 priv = GET_PRIV (contact);
469 /* We temporally ref the contact because it could be destroyed
470 * during the signal emition */
471 g_object_ref (contact);
472 if (tp_strdiff (id, priv->id))
475 priv->id = g_strdup (id);
477 g_object_notify (G_OBJECT (contact), "id");
478 if (EMP_STR_EMPTY (priv->alias))
479 g_object_notify (G_OBJECT (contact), "alias");
482 g_object_unref (contact);
486 empathy_contact_set_presence (EmpathyContact *contact,
487 TpConnectionPresenceType presence)
489 EmpathyContactPriv *priv;
490 TpConnectionPresenceType old_presence;
492 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
494 priv = GET_PRIV (contact);
496 if (presence == priv->presence)
499 old_presence = priv->presence;
500 priv->presence = presence;
502 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
504 g_object_notify (G_OBJECT (contact), "presence");
508 empathy_contact_set_presence_message (EmpathyContact *contact,
509 const gchar *message)
511 EmpathyContactPriv *priv = GET_PRIV (contact);
513 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
515 if (priv->persona != NULL)
517 folks_presence_details_set_presence_message (
518 FOLKS_PRESENCE_DETAILS (priv->persona), message);
523 empathy_contact_set_handle (EmpathyContact *contact,
526 EmpathyContactPriv *priv;
528 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
530 priv = GET_PRIV (contact);
532 g_object_ref (contact);
533 if (handle != priv->handle)
535 priv->handle = handle;
536 g_object_notify (G_OBJECT (contact), "handle");
538 g_object_unref (contact);
542 contact_get_property (GObject *object,
547 EmpathyContact *contact = EMPATHY_CONTACT (object);
551 case PROP_TP_CONTACT:
552 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
555 g_value_set_object (value, empathy_contact_get_account (contact));
558 g_value_set_object (value, empathy_contact_get_persona (contact));
561 g_value_set_string (value, empathy_contact_get_id (contact));
564 g_value_set_string (value, empathy_contact_get_alias (contact));
566 case PROP_LOGGED_ALIAS:
567 g_value_set_string (value, empathy_contact_get_logged_alias (contact));
570 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
573 g_value_set_uint (value, empathy_contact_get_presence (contact));
575 case PROP_PRESENCE_MESSAGE:
576 g_value_set_string (value, empathy_contact_get_presence_message (contact));
579 g_value_set_uint (value, empathy_contact_get_handle (contact));
581 case PROP_CAPABILITIES:
582 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
585 g_value_set_boolean (value, empathy_contact_is_user (contact));
588 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
594 contact_set_property (GObject *object,
599 EmpathyContact *contact = EMPATHY_CONTACT (object);
600 EmpathyContactPriv *priv = GET_PRIV (object);
604 case PROP_TP_CONTACT:
605 priv->tp_contact = g_value_dup_object (value);
608 g_assert (priv->account == NULL);
609 priv->account = g_value_dup_object (value);
612 empathy_contact_set_persona (contact, g_value_get_object (value));
615 empathy_contact_set_id (contact, g_value_get_string (value));
618 empathy_contact_set_alias (contact, g_value_get_string (value));
620 case PROP_LOGGED_ALIAS:
621 g_assert (priv->logged_alias == NULL);
622 priv->logged_alias = g_value_dup_string (value);
625 empathy_contact_set_presence (contact, g_value_get_uint (value));
627 case PROP_PRESENCE_MESSAGE:
628 empathy_contact_set_presence_message (contact, g_value_get_string (value));
631 empathy_contact_set_handle (contact, g_value_get_uint (value));
633 case PROP_CAPABILITIES:
634 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
637 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
640 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
646 remove_tp_contact (gpointer data,
649 g_hash_table_remove (contacts_table, data);
652 static EmpathyContact *
653 empathy_contact_new (TpContact *tp_contact)
655 EmpathyContact *retval;
657 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
659 retval = g_object_new (EMPATHY_TYPE_CONTACT,
660 "tp-contact", tp_contact,
663 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
675 contact_is_tpl_entity (gpointer key,
679 EmpathyContact *contact = value;
680 FindContactData *data = user_data;
681 TpAccount *account = empathy_contact_get_account (contact);
682 const gchar *path = NULL;
685 path = tp_proxy_get_object_path (account);
687 return !tp_strdiff (empathy_contact_get_id (contact),
688 tpl_entity_get_identifier (data->entity)) &&
689 !tp_strdiff (tp_proxy_get_object_path (data->account), path);
693 get_contacts_cb (TpConnection *connection,
695 TpContact * const *contacts,
696 const gchar * const *requested_ids,
697 GHashTable *failed_id_errors,
700 GObject *weak_object)
702 EmpathyContact *self = (EmpathyContact *) weak_object;
703 EmpathyContactPriv *priv = GET_PRIV (self);
704 TpContact *tp_contact;
709 tp_contact = contacts[0];
711 g_return_if_fail (priv->tp_contact == NULL);
712 priv->tp_contact = g_object_ref (tp_contact);
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 (tp_contact));
721 empathy_contact_from_tpl_contact (TpAccount *account,
722 TplEntity *tpl_entity)
724 EmpathyContact *retval;
726 EmpathyContact *existing_contact = NULL;
728 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
730 if (contacts_table != NULL)
732 FindContactData data;
734 data.entity = tpl_entity;
735 data.account = account;
737 existing_contact = g_hash_table_find (contacts_table,
738 contact_is_tpl_entity, &data);
741 if (existing_contact != NULL)
743 retval = g_object_new (EMPATHY_TYPE_CONTACT,
744 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
745 "logged-alias", tpl_entity_get_alias (tpl_entity),
753 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
755 id = tpl_entity_get_identifier (tpl_entity);
757 retval = g_object_new (EMPATHY_TYPE_CONTACT,
759 "alias", tpl_entity_get_alias (tpl_entity),
764 /* Try to get a TpContact associated to have at least contact
765 * capabilities if possible. This is useful for CM supporting calling
766 * offline contacts for example. */
767 conn = tp_account_get_connection (account);
770 TpContactFeature features[] = { TP_CONTACT_FEATURE_CAPABILITIES };
771 conn = tp_account_get_connection (account);
773 tp_connection_get_contacts_by_id (conn, 1, &id,
774 G_N_ELEMENTS (features), features, get_contacts_cb,
775 NULL, NULL, G_OBJECT (retval));
779 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
780 contact_load_avatar_cache (retval,
781 tpl_entity_get_avatar_token (tpl_entity));
787 empathy_contact_get_tp_contact (EmpathyContact *contact)
789 EmpathyContactPriv *priv;
791 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
793 priv = GET_PRIV (contact);
795 return priv->tp_contact;
799 empathy_contact_get_id (EmpathyContact *contact)
801 EmpathyContactPriv *priv;
803 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
805 priv = GET_PRIV (contact);
807 if (priv->tp_contact != NULL)
808 return tp_contact_get_identifier (priv->tp_contact);
814 empathy_contact_get_alias (EmpathyContact *contact)
816 EmpathyContactPriv *priv;
817 const gchar *alias = NULL;
819 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
821 priv = GET_PRIV (contact);
823 if (!EMP_STR_EMPTY (priv->alias))
825 else if (priv->tp_contact != NULL)
826 alias = tp_contact_get_alias (priv->tp_contact);
828 if (!EMP_STR_EMPTY (alias))
831 return empathy_contact_get_id (contact);
835 empathy_contact_get_logged_alias (EmpathyContact *contact)
837 EmpathyContactPriv *priv;
839 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
841 priv = GET_PRIV (contact);
843 if (priv->logged_alias != NULL)
844 return priv->logged_alias;
846 return empathy_contact_get_alias (contact);
850 empathy_contact_set_alias (EmpathyContact *contact,
853 EmpathyContactPriv *priv;
854 FolksPersona *persona;
856 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
858 priv = GET_PRIV (contact);
860 g_object_ref (contact);
862 /* Set the alias on the persona if possible */
863 persona = empathy_contact_get_persona (contact);
864 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
866 DEBUG ("Setting alias for contact %s to %s",
867 empathy_contact_get_id (contact), alias);
869 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
872 if (tp_strdiff (alias, priv->alias))
874 g_free (priv->alias);
875 priv->alias = g_strdup (alias);
876 g_object_notify (G_OBJECT (contact), "alias");
879 g_object_unref (contact);
883 groups_change_group_cb (GObject *source,
884 GAsyncResult *result,
887 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
888 GError *error = NULL;
890 folks_group_details_change_group_finish (group_details, result, &error);
893 g_warning ("failed to change group: %s", error->message);
894 g_clear_error (&error);
899 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
902 EmpathyContactPriv *priv;
903 FolksPersona *persona;
905 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
906 g_return_if_fail (group != NULL);
908 priv = GET_PRIV (contact);
910 /* Normally pass through the changes to the persona */
911 persona = empathy_contact_get_persona (contact);
914 if (FOLKS_IS_GROUP_DETAILS (persona))
915 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
916 is_member, groups_change_group_cb, contact);
920 /* If the persona doesn't exist yet, we have to cache the changes until it
922 if (priv->groups == NULL)
924 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
925 g_free, g_str_hash, g_str_equal);
928 gee_collection_add (GEE_COLLECTION (priv->groups), group);
932 empathy_contact_get_avatar (EmpathyContact *contact)
934 EmpathyContactPriv *priv;
936 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
938 priv = GET_PRIV (contact);
944 contact_set_avatar (EmpathyContact *contact,
945 EmpathyAvatar *avatar)
947 EmpathyContactPriv *priv;
949 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
951 priv = GET_PRIV (contact);
953 if (priv->avatar == avatar)
958 empathy_avatar_unref (priv->avatar);
963 priv->avatar = empathy_avatar_ref (avatar);
965 g_object_notify (G_OBJECT (contact), "avatar");
969 empathy_contact_get_account (EmpathyContact *contact)
971 EmpathyContactPriv *priv;
973 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
975 priv = GET_PRIV (contact);
977 if (priv->account == NULL && priv->tp_contact != NULL)
979 TpConnection *connection;
981 /* FIXME: This assume the account manager already exists */
982 connection = tp_contact_get_connection (priv->tp_contact);
984 g_object_ref (tp_connection_get_account (connection));
987 return priv->account;
991 empathy_contact_get_persona (EmpathyContact *contact)
993 EmpathyContactPriv *priv;
995 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
997 priv = GET_PRIV (contact);
999 if (priv->persona == NULL && priv->tp_contact != NULL)
1001 TpfPersona *persona;
1003 persona = tpf_persona_dup_for_contact (priv->tp_contact);
1004 if (persona != NULL)
1006 empathy_contact_set_persona (contact, (FolksPersona *) persona);
1007 g_object_unref (persona);
1011 return priv->persona;
1015 empathy_contact_set_persona (EmpathyContact *contact,
1016 FolksPersona *persona)
1018 EmpathyContactPriv *priv;
1020 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1021 g_return_if_fail (TPF_IS_PERSONA (persona));
1023 priv = GET_PRIV (contact);
1025 if (persona == priv->persona)
1028 if (priv->persona != NULL)
1030 g_signal_handlers_disconnect_by_func (priv->persona,
1031 folks_persona_notify_cb, contact);
1032 g_object_unref (priv->persona);
1034 priv->persona = g_object_ref (persona);
1036 g_signal_connect (priv->persona, "notify",
1037 G_CALLBACK (folks_persona_notify_cb), contact);
1039 g_object_notify (G_OBJECT (contact), "persona");
1041 /* Set the persona's alias, since ours could've been set using
1042 * empathy_contact_set_alias() before we had a persona; this happens when
1043 * adding a contact. */
1044 if (priv->alias != NULL)
1045 empathy_contact_set_alias (contact, priv->alias);
1047 /* Set the persona's groups */
1048 if (priv->groups != NULL)
1050 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1051 GEE_SET (priv->groups));
1052 g_object_unref (priv->groups);
1053 priv->groups = NULL;
1058 empathy_contact_get_connection (EmpathyContact *contact)
1060 EmpathyContactPriv *priv;
1062 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1064 priv = GET_PRIV (contact);
1066 if (priv->tp_contact != NULL)
1067 return tp_contact_get_connection (priv->tp_contact);
1072 TpConnectionPresenceType
1073 empathy_contact_get_presence (EmpathyContact *contact)
1075 EmpathyContactPriv *priv;
1077 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1078 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1080 priv = GET_PRIV (contact);
1082 if (priv->tp_contact != NULL)
1083 return tp_contact_get_presence_type (priv->tp_contact);
1085 return priv->presence;
1089 empathy_contact_get_presence_message (EmpathyContact *contact)
1091 EmpathyContactPriv *priv;
1093 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1095 priv = GET_PRIV (contact);
1097 if (priv->persona != NULL)
1098 return folks_presence_details_get_presence_message (
1099 FOLKS_PRESENCE_DETAILS (priv->persona));
1101 if (priv->tp_contact != NULL)
1102 return tp_contact_get_presence_message (priv->tp_contact);
1108 empathy_contact_get_handle (EmpathyContact *contact)
1110 EmpathyContactPriv *priv;
1112 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1114 priv = GET_PRIV (contact);
1116 if (priv->tp_contact != NULL)
1117 return tp_contact_get_handle (priv->tp_contact);
1119 return priv->handle;
1123 empathy_contact_get_capabilities (EmpathyContact *contact)
1125 EmpathyContactPriv *priv;
1127 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1129 priv = GET_PRIV (contact);
1131 return priv->capabilities;
1135 empathy_contact_is_user (EmpathyContact *contact)
1137 EmpathyContactPriv *priv;
1139 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1141 priv = GET_PRIV (contact);
1143 return priv->is_user;
1147 empathy_contact_set_is_user (EmpathyContact *contact,
1150 EmpathyContactPriv *priv;
1152 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1154 priv = GET_PRIV (contact);
1156 if (priv->is_user == is_user)
1159 priv->is_user = is_user;
1161 g_object_notify (G_OBJECT (contact), "is-user");
1165 empathy_contact_is_online (EmpathyContact *contact)
1167 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1169 switch (empathy_contact_get_presence (contact))
1171 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1172 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1173 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1175 /* Contacts without presence are considered online so we can display IRC
1176 * contacts in rooms. */
1177 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1178 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1179 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1180 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1181 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1182 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1189 empathy_contact_get_status (EmpathyContact *contact)
1191 const gchar *message;
1193 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1195 message = empathy_contact_get_presence_message (contact);
1196 if (!EMP_STR_EMPTY (message))
1199 return empathy_presence_get_default_message (
1200 empathy_contact_get_presence (contact));
1204 empathy_contact_can_sms (EmpathyContact *contact)
1206 EmpathyContactPriv *priv;
1208 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1210 priv = GET_PRIV (contact);
1212 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1216 empathy_contact_can_voip (EmpathyContact *contact)
1218 EmpathyContactPriv *priv;
1220 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1222 priv = GET_PRIV (contact);
1224 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1225 EMPATHY_CAPABILITIES_VIDEO);
1229 empathy_contact_can_voip_audio (EmpathyContact *contact)
1231 EmpathyContactPriv *priv;
1233 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1235 priv = GET_PRIV (contact);
1237 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1241 empathy_contact_can_voip_video (EmpathyContact *contact)
1243 EmpathyContactPriv *priv;
1245 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1247 priv = GET_PRIV (contact);
1249 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1253 empathy_contact_can_send_files (EmpathyContact *contact)
1255 EmpathyContactPriv *priv;
1257 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1259 priv = GET_PRIV (contact);
1261 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1265 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1267 EmpathyContactPriv *priv;
1269 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1271 priv = GET_PRIV (contact);
1273 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1277 contact_has_log (EmpathyContact *contact)
1279 TplLogManager *manager;
1283 manager = tpl_log_manager_dup_singleton ();
1284 entity = tpl_entity_new (empathy_contact_get_id (contact),
1285 TPL_ENTITY_CONTACT, NULL, NULL);
1287 have_log = tpl_log_manager_exists (manager,
1288 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1290 g_object_unref (entity);
1291 g_object_unref (manager);
1297 empathy_contact_can_do_action (EmpathyContact *self,
1298 EmpathyActionType action_type)
1300 gboolean sensitivity = FALSE;
1302 switch (action_type)
1304 case EMPATHY_ACTION_CHAT:
1307 case EMPATHY_ACTION_SMS:
1308 sensitivity = empathy_contact_can_sms (self);
1310 case EMPATHY_ACTION_AUDIO_CALL:
1311 sensitivity = empathy_contact_can_voip_audio (self);
1313 case EMPATHY_ACTION_VIDEO_CALL:
1314 sensitivity = empathy_contact_can_voip_video (self);
1316 case EMPATHY_ACTION_VIEW_LOGS:
1317 sensitivity = contact_has_log (self);
1319 case EMPATHY_ACTION_SEND_FILE:
1320 sensitivity = empathy_contact_can_send_files (self);
1322 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1323 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1326 g_assert_not_reached ();
1329 return (sensitivity ? TRUE : FALSE);
1333 contact_get_avatar_filename (EmpathyContact *contact,
1339 gchar *token_escaped;
1341 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1344 token_escaped = tp_escape_as_identifier (token);
1345 account = empathy_contact_get_account (contact);
1347 avatar_path = g_build_filename (g_get_user_cache_dir (),
1350 tp_account_get_connection_manager (account),
1351 tp_account_get_protocol (account),
1353 g_mkdir_with_parents (avatar_path, 0700);
1355 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1357 g_free (token_escaped);
1358 g_free (avatar_path);
1364 contact_load_avatar_cache (EmpathyContact *contact,
1367 EmpathyAvatar *avatar = NULL;
1371 GError *error = NULL;
1373 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1374 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1376 /* Load the avatar from file if it exists */
1377 filename = contact_get_avatar_filename (contact, token);
1378 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1380 if (!g_file_get_contents (filename, &data, &len, &error))
1382 DEBUG ("Failed to load avatar from cache: %s",
1383 error ? error->message : "No error given");
1384 g_clear_error (&error);
1390 DEBUG ("Avatar loaded from %s", filename);
1391 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1392 contact_set_avatar (contact, avatar);
1393 empathy_avatar_unref (avatar);
1399 return data != NULL;
1403 empathy_avatar_get_type (void)
1405 static GType type_id = 0;
1409 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1410 (GBoxedCopyFunc) empathy_avatar_ref,
1411 (GBoxedFreeFunc) empathy_avatar_unref);
1418 * empathy_avatar_new:
1419 * @data: the avatar data
1420 * @len: the size of avatar data
1421 * @format: the mime type of the avatar image
1422 * @filename: the filename where the avatar is stored in cache
1424 * Create a #EmpathyAvatar from the provided data.
1426 * Returns: a new #EmpathyAvatar
1429 empathy_avatar_new (const guchar *data,
1431 const gchar *format,
1432 const gchar *filename)
1434 EmpathyAvatar *avatar;
1436 avatar = g_slice_new0 (EmpathyAvatar);
1437 avatar->data = g_memdup (data, len);
1439 avatar->format = g_strdup (format);
1440 avatar->filename = g_strdup (filename);
1441 avatar->refcount = 1;
1447 empathy_avatar_unref (EmpathyAvatar *avatar)
1449 g_return_if_fail (avatar != NULL);
1452 if (avatar->refcount == 0)
1454 g_free (avatar->data);
1455 g_free (avatar->format);
1456 g_free (avatar->filename);
1457 g_slice_free (EmpathyAvatar, avatar);
1462 empathy_avatar_ref (EmpathyAvatar *avatar)
1464 g_return_val_if_fail (avatar != NULL, NULL);
1472 * empathy_avatar_save_to_file:
1473 * @avatar: the avatar
1474 * @filename: name of a file to write avatar to
1475 * @error: return location for a GError, or NULL
1477 * Save the avatar to a file named filename
1479 * Returns: %TRUE on success, %FALSE if an error occurred
1482 empathy_avatar_save_to_file (EmpathyAvatar *self,
1483 const gchar *filename,
1486 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1491 * empathy_contact_get_location:
1492 * @contact: an #EmpathyContact
1494 * Returns the user's location if available. The keys are defined in
1495 * empathy-location.h. If the contact doesn't have location
1496 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1497 * you are done with the #GHashTable.
1499 * It is composed of string keys and GValues. Keys are
1500 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1501 * Example: a "city" key would have "Helsinki" as string GValue,
1502 * a "latitude" would have 65.0 as double GValue.
1504 * Returns: a #GHashTable of location values
1507 empathy_contact_get_location (EmpathyContact *contact)
1509 EmpathyContactPriv *priv;
1511 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1513 priv = GET_PRIV (contact);
1515 return priv->location;
1519 * empathy_contact_set_location:
1520 * @contact: an #EmpathyContact
1521 * @location: a #GHashTable of the location
1523 * Sets the user's location based on the location #GHashTable passed.
1524 * It is composed of string keys and GValues. Keys are
1525 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1526 * Example: a "city" key would have "Helsinki" as string GValue,
1527 * a "latitude" would have 65.0 as double GValue.
1530 empathy_contact_set_location (EmpathyContact *contact,
1531 GHashTable *location)
1533 EmpathyContactPriv *priv;
1535 g_return_if_fail (EMPATHY_CONTACT (contact));
1536 g_return_if_fail (location != NULL);
1538 priv = GET_PRIV (contact);
1540 if (priv->location != NULL)
1541 g_hash_table_unref (priv->location);
1543 priv->location = g_hash_table_ref (location);
1545 update_geocode (contact);
1547 g_object_notify (G_OBJECT (contact), "location");
1550 const gchar * const *
1551 empathy_contact_get_client_types (EmpathyContact *contact)
1553 EmpathyContactPriv *priv;
1555 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1557 priv = GET_PRIV (contact);
1559 return (const gchar * const *) priv->client_types;
1563 contact_set_client_types (EmpathyContact *contact,
1564 const gchar * const *client_types)
1566 EmpathyContactPriv *priv = GET_PRIV (contact);
1568 if (priv->client_types != NULL)
1569 g_strfreev (priv->client_types);
1571 priv->client_types = g_strdupv ((gchar **) client_types);
1572 g_object_notify (G_OBJECT (contact), "client-types");
1576 * empathy_contact_equal:
1577 * @contact1: an #EmpathyContact
1578 * @contact2: an #EmpathyContact
1580 * Returns FALSE if one of the contacts is NULL but the other is not.
1581 * Otherwise returns TRUE if both pointer are equal or if they bith
1582 * refer to the same id.
1583 * It's only necessary to call this function if your contact objects
1584 * come from logs where contacts are created dynamically and comparing
1585 * pointers is not enough.
1588 empathy_contact_equal (gconstpointer contact1,
1589 gconstpointer contact2)
1596 if ((contact1 == NULL) != (contact2 == NULL)) {
1599 if (contact1 == contact2) {
1602 c1 = EMPATHY_CONTACT (contact1);
1603 c2 = EMPATHY_CONTACT (contact2);
1604 id1 = empathy_contact_get_id (c1);
1605 id2 = empathy_contact_get_id (c2);
1606 if (!tp_strdiff (id1, id2)) {
1613 /* This callback is called by geocode-glib when it found a position
1614 * for the given address. A position is necessary for a contact
1615 * to show up on the map
1618 geocode_cb (GObject *source,
1619 GAsyncResult *result,
1622 EmpathyContact *contact = user_data;
1623 EmpathyContactPriv *priv = GET_PRIV (contact);
1624 GError *error = NULL;
1625 GHashTable *new_location;
1626 GHashTable *resolved = NULL;
1627 gdouble latitude, longitude;
1629 if (priv->location == NULL)
1632 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1635 if (resolved == NULL)
1637 DEBUG ("Failed to resolve geocode: %s", error->message);
1638 g_error_free (error);
1642 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1645 new_location = tp_asv_new (
1646 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1647 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1650 DEBUG ("\t - Latitude: %f", latitude);
1651 DEBUG ("\t - Longitude: %f", longitude);
1653 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1654 * the values we just set. */
1655 tp_g_hash_table_update (new_location, priv->location,
1656 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1658 /* Don't change the accuracy as we used an address to get this position */
1659 g_hash_table_unref (priv->location);
1660 priv->location = new_location;
1661 g_object_notify ((GObject *) contact, "location");
1664 tp_clear_pointer (&resolved, g_hash_table_unref);
1665 g_object_unref (contact);
1669 update_geocode (EmpathyContact *contact)
1671 GeocodeObject *geocode;
1672 GHashTable *location;
1674 location = empathy_contact_get_location (contact);
1675 if (location == NULL ||
1676 g_hash_table_size (location) == 0)
1679 /* No need to search for position if contact published it */
1680 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1681 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1684 geocode = geocode_object_new_for_params (location);
1685 if (geocode == NULL)
1688 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1689 g_object_ref (contact));
1691 g_object_unref (geocode);
1695 static EmpathyCapabilities
1696 tp_caps_to_capabilities (TpCapabilities *caps)
1698 EmpathyCapabilities capabilities = 0;
1700 if (tp_capabilities_supports_file_transfer (caps))
1701 capabilities |= EMPATHY_CAPABILITIES_FT;
1703 if (tp_capabilities_supports_stream_tubes (caps, TP_HANDLE_TYPE_CONTACT,
1705 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1707 if (tp_capabilities_supports_audio_video_call (caps, TP_HANDLE_TYPE_CONTACT))
1709 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1710 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1712 else if (tp_capabilities_supports_audio_call (caps, TP_HANDLE_TYPE_CONTACT))
1714 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1717 if (tp_capabilities_supports_sms (caps))
1718 capabilities |= EMPATHY_CAPABILITIES_SMS;
1720 return capabilities;
1724 set_capabilities_from_tp_caps (EmpathyContact *self,
1725 TpCapabilities *caps)
1727 EmpathyCapabilities capabilities;
1732 capabilities = tp_caps_to_capabilities (caps);
1733 empathy_contact_set_capabilities (self, capabilities);
1737 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1739 EmpathyContactPriv *priv = GET_PRIV (contact);
1743 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1744 file = tp_contact_get_avatar_file (priv->tp_contact);
1748 EmpathyAvatar *avatar;
1752 GError *error = NULL;
1754 if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
1756 DEBUG ("Failed to load avatar: %s", error->message);
1758 g_error_free (error);
1759 contact_set_avatar (contact, NULL);
1763 path = g_file_get_path (file);
1765 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1767 contact_set_avatar (contact, avatar);
1768 empathy_avatar_unref (avatar);
1774 contact_set_avatar (contact, NULL);
1779 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1781 EmpathyContact *contact = NULL;
1783 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1785 if (contacts_table == NULL)
1786 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1788 contact = g_hash_table_lookup (contacts_table, tp_contact);
1790 if (contact == NULL)
1792 contact = empathy_contact_new (tp_contact);
1794 /* The hash table does not keep any ref.
1795 * contact keeps a ref to tp_contact, and is removed from the table in
1796 * contact_dispose() */
1797 g_hash_table_insert (contacts_table, tp_contact, contact);
1801 g_object_ref (contact);
1808 presence_cmp_func (EmpathyContact *a,
1811 FolksPresenceDetails *presence_a, *presence_b;
1813 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1814 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1816 /* We negate the result because we're sorting in reverse order (i.e. such that
1817 * the Personas with the highest presence are at the beginning of the list. */
1818 return -folks_presence_details_typecmp (
1819 folks_presence_details_get_presence_type (presence_a),
1820 folks_presence_details_get_presence_type (presence_b));
1824 voip_cmp_func (EmpathyContact *a,
1827 gboolean has_audio_a, has_audio_b;
1828 gboolean has_video_a, has_video_b;
1830 has_audio_a = empathy_contact_can_voip_audio (a);
1831 has_audio_b = empathy_contact_can_voip_audio (b);
1832 has_video_a = empathy_contact_can_voip_video (a);
1833 has_video_b = empathy_contact_can_voip_video (b);
1835 /* First check video */
1836 if (has_video_a == has_video_b)
1838 /* Use audio to to break tie */
1839 if (has_audio_a == has_audio_b)
1841 else if (has_audio_a)
1846 else if (has_video_a)
1853 ft_cmp_func (EmpathyContact *a,
1856 gboolean can_send_files_a, can_send_files_b;
1858 can_send_files_a = empathy_contact_can_send_files (a);
1859 can_send_files_b = empathy_contact_can_send_files (b);
1861 if (can_send_files_a == can_send_files_b)
1863 else if (can_send_files_a)
1870 rfb_stream_tube_cmp_func (EmpathyContact *a,
1873 gboolean rfb_a, rfb_b;
1875 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1876 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1886 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1887 * the same presence, prefer the one which can do both audio *and* video calls,
1888 * over the one which can only do one of the two. */
1890 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1892 gint presence_sort = presence_cmp_func (a, b);
1894 if (presence_sort != 0)
1895 return presence_sort;
1897 return voip_cmp_func (a, b);
1900 /* Sort by presence as with presence_cmp_func() and then break ties using the
1901 * most "capable" individual. So users will be able to pick more actions on
1902 * the contact in the "Contact" menu of the chat window. */
1904 chat_sort_func (EmpathyContact *a,
1909 result = presence_cmp_func (a, b);
1913 /* Prefer individual supporting file transfer */
1914 result = ft_cmp_func (a, b);
1918 /* Check audio/video capabilities */
1919 result = voip_cmp_func (a, b);
1923 /* Check 'Share my destop' feature */
1924 return rfb_stream_tube_cmp_func (a, b);
1928 get_sort_func_for_action (EmpathyActionType action_type)
1930 switch (action_type)
1932 case EMPATHY_ACTION_AUDIO_CALL:
1933 case EMPATHY_ACTION_VIDEO_CALL:
1934 return (GCompareFunc) voip_sort_func;
1935 case EMPATHY_ACTION_CHAT:
1936 return (GCompareFunc) chat_sort_func;
1937 case EMPATHY_ACTION_VIEW_LOGS:
1938 case EMPATHY_ACTION_SEND_FILE:
1939 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1941 return (GCompareFunc) presence_cmp_func;
1946 * empathy_contact_dup_best_for_action:
1947 * @individual: a #FolksIndividual
1948 * @action_type: the type of action to be performed on the contact
1950 * Chooses a #FolksPersona from the given @individual which is best-suited for
1951 * the given @action_type. "Best-suited" is determined by choosing the persona
1952 * with the highest presence out of all the personas which can perform the given
1953 * @action_type (e.g. are capable of video calling).
1955 * Return value: an #EmpathyContact for the best persona, or %NULL;
1956 * unref with g_object_unref()
1959 empathy_contact_dup_best_for_action (FolksIndividual *individual,
1960 EmpathyActionType action_type)
1965 EmpathyContact *best_contact = NULL;
1967 /* Build a list of EmpathyContacts that we can sort */
1968 personas = folks_individual_get_personas (individual);
1971 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1972 while (gee_iterator_next (iter))
1974 FolksPersona *persona = gee_iterator_get (iter);
1975 TpContact *tp_contact;
1976 EmpathyContact *contact = NULL;
1978 if (!empathy_folks_persona_is_interesting (persona))
1981 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1982 if (tp_contact == NULL)
1985 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1986 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
1988 /* Only choose the contact if they're actually capable of the specified
1990 if (empathy_contact_can_do_action (contact, action_type))
1991 contacts = g_list_prepend (contacts, g_object_ref (contact));
1994 g_clear_object (&contact);
1995 g_clear_object (&persona);
1997 g_clear_object (&iter);
1999 /* Sort the contacts by some heuristic based on the action type, then take
2000 * the top contact. */
2001 if (contacts != NULL)
2003 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2004 best_contact = g_object_ref (contacts->data);
2007 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2008 g_list_free (contacts);
2010 return best_contact;
2013 #define declare_contact_cb(name) \
2015 contact_##name##_cb (GObject *source, \
2016 GAsyncResult *result, \
2017 gpointer user_data) \
2019 TpContact *contact = (TpContact *) source; \
2020 GError *error = NULL; \
2022 if (!tp_contact_##name##_finish (contact, result, &error)) \
2024 DEBUG ("Failed to ##name## on %s\n", \
2025 tp_contact_get_identifier (contact)); \
2026 g_error_free (error); \
2030 declare_contact_cb (request_subscription)
2031 declare_contact_cb (authorize_publication)
2032 declare_contact_cb (unblock)
2035 empathy_contact_add_to_contact_list (EmpathyContact *self,
2036 const gchar *message)
2038 EmpathyContactPriv *priv = GET_PRIV (self);
2040 g_return_if_fail (priv->tp_contact != NULL);
2042 tp_contact_request_subscription_async (priv->tp_contact, message,
2043 contact_request_subscription_cb, NULL);
2045 tp_contact_authorize_publication_async (priv->tp_contact,
2046 contact_authorize_publication_cb, NULL);
2048 tp_contact_unblock_async (priv->tp_contact, contact_unblock_cb, NULL);
2051 declare_contact_cb (remove)
2054 empathy_contact_remove_from_contact_list (EmpathyContact *self)
2056 EmpathyContactPriv *priv = GET_PRIV (self);
2058 g_return_if_fail (priv->tp_contact != NULL);
2060 tp_contact_remove_async (priv->tp_contact, contact_remove_cb, NULL);