1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/account-manager.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-yell/telepathy-yell.h>
33 #include <telepathy-logger/log-manager.h>
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
39 #include <geocode-glib/geocode-glib.h>
42 #include "empathy-contact.h"
43 #include "empathy-camera-monitor.h"
44 #include "empathy-individual-manager.h"
45 #include "empathy-utils.h"
46 #include "empathy-enum-types.h"
47 #include "empathy-marshal.h"
48 #include "empathy-location.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
51 #include "empathy-debug.h"
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
55 TpContact *tp_contact;
57 FolksPersona *persona;
61 EmpathyAvatar *avatar;
62 TpConnectionPresenceType presence;
64 EmpathyCapabilities capabilities;
67 /* Location is composed of string keys and GValues.
68 * Example: a "city" key would have "Helsinki" as string GValue,
69 * a "latitude" would have 65.0 as double GValue.
71 * This is a super set of the location stored in TpContact as we can try add
72 * more fields by searching the address using geoclue.
79 static void contact_finalize (GObject *object);
80 static void contact_get_property (GObject *object, guint param_id,
81 GValue *value, GParamSpec *pspec);
82 static void contact_set_property (GObject *object, guint param_id,
83 const GValue *value, GParamSpec *pspec);
86 static void update_geocode (EmpathyContact *contact);
89 static void empathy_contact_set_location (EmpathyContact *contact,
90 GHashTable *location);
92 static void contact_set_client_types (EmpathyContact *contact,
93 const gchar * const *types);
95 static void set_capabilities_from_tp_caps (EmpathyContact *self,
96 TpCapabilities *caps);
98 static void contact_set_avatar (EmpathyContact *contact,
99 EmpathyAvatar *avatar);
100 static void contact_set_avatar_from_tp_contact (EmpathyContact *contact);
101 static gboolean contact_load_avatar_cache (EmpathyContact *contact,
104 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
117 PROP_PRESENCE_MESSAGE,
130 static guint signals[LAST_SIGNAL];
132 /* TpContact* -> EmpathyContact*, both borrowed ref */
133 static GHashTable *contacts_table = NULL;
136 tp_contact_notify_cb (TpContact *tp_contact,
140 EmpathyContactPriv *priv = GET_PRIV (contact);
142 /* Forward property notifications */
143 if (!tp_strdiff (param->name, "alias"))
144 g_object_notify (contact, "alias");
145 else if (!tp_strdiff (param->name, "presence-type")) {
146 TpConnectionPresenceType presence;
148 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
149 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence,
151 priv->presence = presence;
152 g_object_notify (contact, "presence");
154 else if (!tp_strdiff (param->name, "identifier"))
155 g_object_notify (contact, "id");
156 else if (!tp_strdiff (param->name, "handle"))
157 g_object_notify (contact, "handle");
158 else if (!tp_strdiff (param->name, "location"))
160 GHashTable *location;
162 location = tp_contact_get_location (tp_contact);
163 /* This will start a geoclue search to find the address if needed */
164 empathy_contact_set_location (EMPATHY_CONTACT (contact), location);
166 else if (!tp_strdiff (param->name, "capabilities"))
168 set_capabilities_from_tp_caps (EMPATHY_CONTACT (contact),
169 tp_contact_get_capabilities (tp_contact));
171 else if (!tp_strdiff (param->name, "avatar-file"))
173 contact_set_avatar_from_tp_contact (EMPATHY_CONTACT (contact));
175 else if (!tp_strdiff (param->name, "client-types"))
177 contact_set_client_types (EMPATHY_CONTACT (contact),
178 tp_contact_get_client_types (tp_contact));
183 folks_persona_notify_cb (FolksPersona *folks_persona,
187 if (!tp_strdiff (param->name, "presence-message"))
188 g_object_notify (contact, "presence-message");
192 contact_dispose (GObject *object)
194 EmpathyContactPriv *priv = GET_PRIV (object);
196 if (priv->tp_contact != NULL)
198 g_signal_handlers_disconnect_by_func (priv->tp_contact,
199 tp_contact_notify_cb, object);
201 tp_clear_object (&priv->tp_contact);
204 g_object_unref (priv->account);
205 priv->account = NULL;
209 g_signal_handlers_disconnect_by_func (priv->persona,
210 folks_persona_notify_cb, object);
211 g_object_unref (priv->persona);
213 priv->persona = NULL;
215 if (priv->avatar != NULL)
217 empathy_avatar_unref (priv->avatar);
221 if (priv->location != NULL)
223 g_hash_table_unref (priv->location);
224 priv->location = NULL;
227 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
231 contact_constructed (GObject *object)
233 EmpathyContact *contact = (EmpathyContact *) object;
234 EmpathyContactPriv *priv = GET_PRIV (contact);
235 GHashTable *location;
236 TpHandle self_handle;
238 const gchar * const *client_types;
240 if (priv->tp_contact == NULL)
243 priv->presence = empathy_contact_get_presence (contact);
245 location = tp_contact_get_location (priv->tp_contact);
246 if (location != NULL)
247 empathy_contact_set_location (contact, location);
249 client_types = tp_contact_get_client_types (priv->tp_contact);
250 if (client_types != NULL)
251 contact_set_client_types (contact, client_types);
253 set_capabilities_from_tp_caps (contact,
254 tp_contact_get_capabilities (priv->tp_contact));
256 contact_set_avatar_from_tp_contact (contact);
258 /* Set is-user property. Note that it could still be the handle is
259 * different from the connection's self handle, in the case the handle
260 * comes from a group interface. */
261 self_handle = tp_connection_get_self_handle (
262 tp_contact_get_connection (priv->tp_contact));
263 handle = tp_contact_get_handle (priv->tp_contact);
264 empathy_contact_set_is_user (contact, self_handle == handle);
266 g_signal_connect (priv->tp_contact, "notify",
267 G_CALLBACK (tp_contact_notify_cb), contact);
271 empathy_contact_class_init (EmpathyContactClass *class)
273 GObjectClass *object_class;
275 object_class = G_OBJECT_CLASS (class);
277 object_class->finalize = contact_finalize;
278 object_class->dispose = contact_dispose;
279 object_class->get_property = contact_get_property;
280 object_class->set_property = contact_set_property;
281 object_class->constructed = contact_constructed;
283 g_object_class_install_property (object_class,
285 g_param_spec_object ("tp-contact",
287 "The TpContact associated with the contact",
289 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (object_class,
293 g_param_spec_object ("account",
295 "The account associated with the contact",
297 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (object_class,
301 g_param_spec_object ("persona",
303 "The FolksPersona associated with the contact",
305 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (object_class,
309 g_param_spec_string ("id",
311 "String identifying contact",
313 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (object_class,
317 g_param_spec_string ("alias",
319 "An alias for the contact",
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (object_class,
325 g_param_spec_string ("logged-alias",
327 "The alias the user had when a message was logged, "
328 "only set when using empathy_contact_from_tpl_contact()",
330 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (object_class,
334 g_param_spec_boxed ("avatar",
338 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (object_class,
342 g_param_spec_uint ("presence",
344 "Presence of contact",
345 TP_CONNECTION_PRESENCE_TYPE_UNSET,
346 NUM_TP_CONNECTION_PRESENCE_TYPES,
347 TP_CONNECTION_PRESENCE_TYPE_UNSET,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (object_class,
351 PROP_PRESENCE_MESSAGE,
352 g_param_spec_string ("presence-message",
353 "Contact presence message",
354 "Presence message of contact",
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (object_class,
360 g_param_spec_uint ("handle",
362 "The handle of the contact",
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 g_object_class_install_property (object_class,
370 g_param_spec_flags ("capabilities",
371 "Contact Capabilities",
372 "Capabilities of the contact",
373 EMPATHY_TYPE_CAPABILITIES,
374 EMPATHY_CAPABILITIES_UNKNOWN,
375 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (object_class,
379 g_param_spec_boolean ("is-user",
381 "Is contact the user",
383 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 g_object_class_install_property (object_class,
388 g_param_spec_boxed ("location",
390 "Physical location of the contact",
392 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
394 g_object_class_install_property (object_class,
396 g_param_spec_boxed ("client-types",
397 "Contact client types",
398 "Client types of the contact",
400 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
402 signals[PRESENCE_CHANGED] =
403 g_signal_new ("presence-changed",
404 G_TYPE_FROM_CLASS (class),
408 _empathy_marshal_VOID__UINT_UINT,
413 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
417 empathy_contact_init (EmpathyContact *contact)
419 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
420 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
422 contact->priv = priv;
424 priv->location = NULL;
425 priv->client_types = NULL;
430 contact_finalize (GObject *object)
432 EmpathyContactPriv *priv;
434 priv = GET_PRIV (object);
436 DEBUG ("finalize: %p", object);
438 g_clear_object (&priv->groups);
439 g_free (priv->alias);
441 g_strfreev (priv->client_types);
443 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
447 empathy_contact_set_capabilities (EmpathyContact *contact,
448 EmpathyCapabilities capabilities)
450 EmpathyContactPriv *priv;
452 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
454 priv = GET_PRIV (contact);
456 if (priv->capabilities == capabilities)
459 priv->capabilities = capabilities;
461 g_object_notify (G_OBJECT (contact), "capabilities");
465 empathy_contact_set_id (EmpathyContact *contact,
468 EmpathyContactPriv *priv;
470 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
471 g_return_if_fail (id != NULL);
473 priv = GET_PRIV (contact);
475 /* We temporally ref the contact because it could be destroyed
476 * during the signal emition */
477 g_object_ref (contact);
478 if (tp_strdiff (id, priv->id))
481 priv->id = g_strdup (id);
483 g_object_notify (G_OBJECT (contact), "id");
484 if (EMP_STR_EMPTY (priv->alias))
485 g_object_notify (G_OBJECT (contact), "alias");
488 g_object_unref (contact);
492 empathy_contact_set_presence (EmpathyContact *contact,
493 TpConnectionPresenceType presence)
495 EmpathyContactPriv *priv;
496 TpConnectionPresenceType old_presence;
498 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
500 priv = GET_PRIV (contact);
502 if (presence == priv->presence)
505 old_presence = priv->presence;
506 priv->presence = presence;
508 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
510 g_object_notify (G_OBJECT (contact), "presence");
514 empathy_contact_set_presence_message (EmpathyContact *contact,
515 const gchar *message)
517 EmpathyContactPriv *priv = GET_PRIV (contact);
519 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
521 if (priv->persona != NULL)
523 folks_presence_details_set_presence_message (
524 FOLKS_PRESENCE_DETAILS (priv->persona), message);
529 empathy_contact_set_handle (EmpathyContact *contact,
532 EmpathyContactPriv *priv;
534 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
536 priv = GET_PRIV (contact);
538 g_object_ref (contact);
539 if (handle != priv->handle)
541 priv->handle = handle;
542 g_object_notify (G_OBJECT (contact), "handle");
544 g_object_unref (contact);
548 contact_get_property (GObject *object,
553 EmpathyContact *contact = EMPATHY_CONTACT (object);
557 case PROP_TP_CONTACT:
558 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
561 g_value_set_object (value, empathy_contact_get_account (contact));
564 g_value_set_object (value, empathy_contact_get_persona (contact));
567 g_value_set_string (value, empathy_contact_get_id (contact));
570 g_value_set_string (value, empathy_contact_get_alias (contact));
572 case PROP_LOGGED_ALIAS:
573 g_value_set_string (value, empathy_contact_get_logged_alias (contact));
576 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
579 g_value_set_uint (value, empathy_contact_get_presence (contact));
581 case PROP_PRESENCE_MESSAGE:
582 g_value_set_string (value, empathy_contact_get_presence_message (contact));
585 g_value_set_uint (value, empathy_contact_get_handle (contact));
587 case PROP_CAPABILITIES:
588 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
591 g_value_set_boolean (value, empathy_contact_is_user (contact));
594 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
600 contact_set_property (GObject *object,
605 EmpathyContact *contact = EMPATHY_CONTACT (object);
606 EmpathyContactPriv *priv = GET_PRIV (object);
610 case PROP_TP_CONTACT:
611 priv->tp_contact = g_value_dup_object (value);
614 g_assert (priv->account == NULL);
615 priv->account = g_value_dup_object (value);
618 empathy_contact_set_persona (contact, g_value_get_object (value));
621 empathy_contact_set_id (contact, g_value_get_string (value));
624 empathy_contact_set_alias (contact, g_value_get_string (value));
626 case PROP_LOGGED_ALIAS:
627 g_assert (priv->logged_alias == NULL);
628 priv->logged_alias = g_value_dup_string (value);
631 empathy_contact_set_presence (contact, g_value_get_uint (value));
633 case PROP_PRESENCE_MESSAGE:
634 empathy_contact_set_presence_message (contact, g_value_get_string (value));
637 empathy_contact_set_handle (contact, g_value_get_uint (value));
639 case PROP_CAPABILITIES:
640 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
643 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
646 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
652 remove_tp_contact (gpointer data,
655 g_hash_table_remove (contacts_table, data);
658 static EmpathyContact *
659 empathy_contact_new (TpContact *tp_contact)
661 EmpathyContact *retval;
663 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
665 retval = g_object_new (EMPATHY_TYPE_CONTACT,
666 "tp-contact", tp_contact,
669 g_object_weak_ref (G_OBJECT (retval), remove_tp_contact, tp_contact);
681 contact_is_tpl_entity (gpointer key,
685 EmpathyContact *contact = value;
686 FindContactData *data = user_data;
687 TpAccount *account = empathy_contact_get_account (contact);
688 const gchar *path = NULL;
691 path = tp_proxy_get_object_path (account);
693 return !tp_strdiff (empathy_contact_get_id (contact),
694 tpl_entity_get_identifier (data->entity)) &&
695 !tp_strdiff (tp_proxy_get_object_path (data->account), path);
699 empathy_contact_from_tpl_contact (TpAccount *account,
700 TplEntity *tpl_entity)
702 EmpathyContact *retval;
704 EmpathyContact *existing_contact = NULL;
706 g_return_val_if_fail (TPL_IS_ENTITY (tpl_entity), NULL);
708 if (contacts_table != NULL)
710 FindContactData data;
712 data.entity = tpl_entity;
713 data.account = account;
715 existing_contact = g_hash_table_find (contacts_table,
716 contact_is_tpl_entity, &data);
719 if (existing_contact != NULL)
721 retval = g_object_new (EMPATHY_TYPE_CONTACT,
722 "tp-contact", empathy_contact_get_tp_contact (existing_contact),
723 "logged-alias", tpl_entity_get_alias (tpl_entity),
728 is_user = (TPL_ENTITY_SELF == tpl_entity_get_entity_type (tpl_entity));
730 retval = g_object_new (EMPATHY_TYPE_CONTACT,
731 "id", tpl_entity_get_identifier (tpl_entity),
732 "alias", tpl_entity_get_alias (tpl_entity),
738 if (!EMP_STR_EMPTY (tpl_entity_get_avatar_token (tpl_entity)))
739 contact_load_avatar_cache (retval,
740 tpl_entity_get_avatar_token (tpl_entity));
746 empathy_contact_get_tp_contact (EmpathyContact *contact)
748 EmpathyContactPriv *priv;
750 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
752 priv = GET_PRIV (contact);
754 return priv->tp_contact;
758 empathy_contact_get_id (EmpathyContact *contact)
760 EmpathyContactPriv *priv;
762 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
764 priv = GET_PRIV (contact);
766 if (priv->tp_contact != NULL)
767 return tp_contact_get_identifier (priv->tp_contact);
773 empathy_contact_get_alias (EmpathyContact *contact)
775 EmpathyContactPriv *priv;
776 const gchar *alias = NULL;
778 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
780 priv = GET_PRIV (contact);
782 if (!EMP_STR_EMPTY (priv->alias))
784 else if (priv->tp_contact != NULL)
785 alias = tp_contact_get_alias (priv->tp_contact);
787 if (!EMP_STR_EMPTY (alias))
790 return empathy_contact_get_id (contact);
794 empathy_contact_get_logged_alias (EmpathyContact *contact)
796 EmpathyContactPriv *priv;
798 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
800 priv = GET_PRIV (contact);
802 if (priv->logged_alias != NULL)
803 return priv->logged_alias;
805 return empathy_contact_get_alias (contact);
809 empathy_contact_set_alias (EmpathyContact *contact,
812 EmpathyContactPriv *priv;
813 FolksPersona *persona;
815 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
817 priv = GET_PRIV (contact);
819 g_object_ref (contact);
821 /* Set the alias on the persona if possible */
822 persona = empathy_contact_get_persona (contact);
823 if (persona != NULL && FOLKS_IS_ALIAS_DETAILS (persona))
825 DEBUG ("Setting alias for contact %s to %s",
826 empathy_contact_get_id (contact), alias);
828 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (persona), alias);
831 if (tp_strdiff (alias, priv->alias))
833 g_free (priv->alias);
834 priv->alias = g_strdup (alias);
835 g_object_notify (G_OBJECT (contact), "alias");
838 g_object_unref (contact);
842 groups_change_group_cb (GObject *source,
843 GAsyncResult *result,
846 FolksGroupDetails *group_details = FOLKS_GROUP_DETAILS (source);
847 GError *error = NULL;
849 folks_group_details_change_group_finish (group_details, result, &error);
852 g_warning ("failed to change group: %s", error->message);
853 g_clear_error (&error);
858 empathy_contact_change_group (EmpathyContact *contact, const gchar *group,
861 EmpathyContactPriv *priv;
862 FolksPersona *persona;
864 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
865 g_return_if_fail (group != NULL);
867 priv = GET_PRIV (contact);
869 /* Normally pass through the changes to the persona */
870 persona = empathy_contact_get_persona (contact);
873 if (FOLKS_IS_GROUP_DETAILS (persona))
874 folks_group_details_change_group (FOLKS_GROUP_DETAILS (persona), group,
875 is_member, groups_change_group_cb, contact);
879 /* If the persona doesn't exist yet, we have to cache the changes until it
881 if (priv->groups == NULL)
883 priv->groups = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup,
884 g_free, g_str_hash, g_str_equal);
887 gee_collection_add (GEE_COLLECTION (priv->groups), group);
891 empathy_contact_get_avatar (EmpathyContact *contact)
893 EmpathyContactPriv *priv;
895 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
897 priv = GET_PRIV (contact);
903 contact_set_avatar (EmpathyContact *contact,
904 EmpathyAvatar *avatar)
906 EmpathyContactPriv *priv;
908 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
910 priv = GET_PRIV (contact);
912 if (priv->avatar == avatar)
917 empathy_avatar_unref (priv->avatar);
922 priv->avatar = empathy_avatar_ref (avatar);
924 g_object_notify (G_OBJECT (contact), "avatar");
928 empathy_contact_get_account (EmpathyContact *contact)
930 EmpathyContactPriv *priv;
932 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
934 priv = GET_PRIV (contact);
936 if (priv->account == NULL && priv->tp_contact != NULL)
938 TpConnection *connection;
940 /* FIXME: This assume the account manager already exists */
941 connection = tp_contact_get_connection (priv->tp_contact);
943 g_object_ref (tp_connection_get_account (connection));
946 return priv->account;
950 empathy_contact_get_persona (EmpathyContact *contact)
952 EmpathyContactPriv *priv;
954 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
956 priv = GET_PRIV (contact);
958 if (priv->persona == NULL && priv->tp_contact != NULL)
960 /* FIXME: This is disgustingly slow */
961 /* Query for the persona */
962 EmpathyIndividualManager *manager;
963 GList *individuals, *l;
965 manager = empathy_individual_manager_dup_singleton ();
966 individuals = empathy_individual_manager_get_members (manager);
968 for (l = individuals; l != NULL; l = l->next)
970 FolksIndividual *individual = FOLKS_INDIVIDUAL (l->data);
973 gboolean persona_found = FALSE;
975 personas = folks_individual_get_personas (individual);
976 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
977 while (!persona_found && gee_iterator_next (iter))
979 TpfPersona *persona = gee_iterator_get (iter);
981 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
983 TpContact *tp_contact = tpf_persona_get_contact (persona);
985 if (tp_contact == priv->tp_contact)
987 /* Found the right persona */
988 empathy_contact_set_persona (contact,
989 (FolksPersona *) persona);
990 persona_found = TRUE;
992 g_clear_object (&persona);
995 g_clear_object (&iter);
998 g_list_free (individuals);
999 g_object_unref (manager);
1002 return priv->persona;
1006 empathy_contact_set_persona (EmpathyContact *contact,
1007 FolksPersona *persona)
1009 EmpathyContactPriv *priv;
1011 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1012 g_return_if_fail (TPF_IS_PERSONA (persona));
1014 priv = GET_PRIV (contact);
1016 if (persona == priv->persona)
1019 if (priv->persona != NULL)
1021 g_signal_handlers_disconnect_by_func (priv->persona,
1022 folks_persona_notify_cb, contact);
1023 g_object_unref (priv->persona);
1025 priv->persona = g_object_ref (persona);
1027 g_signal_connect (priv->persona, "notify",
1028 G_CALLBACK (folks_persona_notify_cb), contact);
1030 g_object_notify (G_OBJECT (contact), "persona");
1032 /* Set the persona's alias, since ours could've been set using
1033 * empathy_contact_set_alias() before we had a persona; this happens when
1034 * adding a contact. */
1035 if (priv->alias != NULL)
1036 empathy_contact_set_alias (contact, priv->alias);
1038 /* Set the persona's groups */
1039 if (priv->groups != NULL)
1041 folks_group_details_set_groups (FOLKS_GROUP_DETAILS (persona),
1042 GEE_SET (priv->groups));
1043 g_object_unref (priv->groups);
1044 priv->groups = NULL;
1049 empathy_contact_get_connection (EmpathyContact *contact)
1051 EmpathyContactPriv *priv;
1053 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1055 priv = GET_PRIV (contact);
1057 if (priv->tp_contact != NULL)
1058 return tp_contact_get_connection (priv->tp_contact);
1063 TpConnectionPresenceType
1064 empathy_contact_get_presence (EmpathyContact *contact)
1066 EmpathyContactPriv *priv;
1068 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
1069 TP_CONNECTION_PRESENCE_TYPE_UNSET);
1071 priv = GET_PRIV (contact);
1073 if (priv->tp_contact != NULL)
1074 return tp_contact_get_presence_type (priv->tp_contact);
1076 return priv->presence;
1080 empathy_contact_get_presence_message (EmpathyContact *contact)
1082 EmpathyContactPriv *priv;
1084 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1086 priv = GET_PRIV (contact);
1088 if (priv->persona != NULL)
1089 return folks_presence_details_get_presence_message (
1090 FOLKS_PRESENCE_DETAILS (priv->persona));
1092 if (priv->tp_contact != NULL)
1093 return tp_contact_get_presence_message (priv->tp_contact);
1099 empathy_contact_get_handle (EmpathyContact *contact)
1101 EmpathyContactPriv *priv;
1103 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1105 priv = GET_PRIV (contact);
1107 if (priv->tp_contact != NULL)
1108 return tp_contact_get_handle (priv->tp_contact);
1110 return priv->handle;
1114 empathy_contact_get_capabilities (EmpathyContact *contact)
1116 EmpathyContactPriv *priv;
1118 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
1120 priv = GET_PRIV (contact);
1122 return priv->capabilities;
1126 empathy_contact_is_user (EmpathyContact *contact)
1128 EmpathyContactPriv *priv;
1130 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1132 priv = GET_PRIV (contact);
1134 return priv->is_user;
1138 empathy_contact_set_is_user (EmpathyContact *contact,
1141 EmpathyContactPriv *priv;
1143 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1145 priv = GET_PRIV (contact);
1147 if (priv->is_user == is_user)
1150 priv->is_user = is_user;
1152 g_object_notify (G_OBJECT (contact), "is-user");
1156 empathy_contact_is_online (EmpathyContact *contact)
1158 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1160 switch (empathy_contact_get_presence (contact))
1162 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
1163 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
1164 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
1166 /* Contacts without presence are considered online so we can display IRC
1167 * contacts in rooms. */
1168 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
1169 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
1170 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
1171 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
1172 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
1173 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
1180 empathy_contact_get_status (EmpathyContact *contact)
1182 const gchar *message;
1184 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
1186 message = empathy_contact_get_presence_message (contact);
1187 if (!EMP_STR_EMPTY (message))
1190 return empathy_presence_get_default_message (
1191 empathy_contact_get_presence (contact));
1195 empathy_contact_can_sms (EmpathyContact *contact)
1197 EmpathyContactPriv *priv;
1199 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1201 priv = GET_PRIV (contact);
1203 return priv->capabilities & EMPATHY_CAPABILITIES_SMS;
1207 empathy_contact_can_voip (EmpathyContact *contact)
1209 EmpathyContactPriv *priv;
1211 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1213 priv = GET_PRIV (contact);
1215 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
1216 EMPATHY_CAPABILITIES_VIDEO);
1220 empathy_contact_can_voip_audio (EmpathyContact *contact)
1222 EmpathyContactPriv *priv;
1224 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1226 priv = GET_PRIV (contact);
1228 return priv->capabilities & EMPATHY_CAPABILITIES_AUDIO;
1232 empathy_contact_can_voip_video (EmpathyContact *contact)
1234 EmpathyContactPriv *priv;
1236 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1238 priv = GET_PRIV (contact);
1240 return priv->capabilities & EMPATHY_CAPABILITIES_VIDEO;
1244 empathy_contact_can_send_files (EmpathyContact *contact)
1246 EmpathyContactPriv *priv;
1248 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1250 priv = GET_PRIV (contact);
1252 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
1256 empathy_contact_can_use_rfb_stream_tube (EmpathyContact *contact)
1258 EmpathyContactPriv *priv;
1260 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1262 priv = GET_PRIV (contact);
1264 return priv->capabilities & EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1268 contact_has_log (EmpathyContact *contact)
1270 TplLogManager *manager;
1274 manager = tpl_log_manager_dup_singleton ();
1275 entity = tpl_entity_new (empathy_contact_get_id (contact),
1276 TPL_ENTITY_CONTACT, NULL, NULL);
1278 have_log = tpl_log_manager_exists (manager,
1279 empathy_contact_get_account (contact), entity, TPL_EVENT_MASK_TEXT);
1281 g_object_unref (entity);
1282 g_object_unref (manager);
1288 empathy_contact_can_do_action (EmpathyContact *self,
1289 EmpathyActionType action_type)
1291 gboolean sensitivity = FALSE;
1293 switch (action_type)
1295 case EMPATHY_ACTION_CHAT:
1298 case EMPATHY_ACTION_SMS:
1299 sensitivity = empathy_contact_can_sms (self);
1301 case EMPATHY_ACTION_AUDIO_CALL:
1302 sensitivity = empathy_contact_can_voip_audio (self);
1304 case EMPATHY_ACTION_VIDEO_CALL:
1305 sensitivity = empathy_contact_can_voip_video (self);
1307 case EMPATHY_ACTION_VIEW_LOGS:
1308 sensitivity = contact_has_log (self);
1310 case EMPATHY_ACTION_SEND_FILE:
1311 sensitivity = empathy_contact_can_send_files (self);
1313 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
1314 sensitivity = empathy_contact_can_use_rfb_stream_tube (self);
1317 g_assert_not_reached ();
1320 return (sensitivity ? TRUE : FALSE);
1324 contact_get_avatar_filename (EmpathyContact *contact,
1330 gchar *token_escaped;
1332 if (EMP_STR_EMPTY (empathy_contact_get_id (contact)))
1335 token_escaped = tp_escape_as_identifier (token);
1336 account = empathy_contact_get_account (contact);
1338 avatar_path = g_build_filename (g_get_user_cache_dir (),
1341 tp_account_get_connection_manager (account),
1342 tp_account_get_protocol (account),
1344 g_mkdir_with_parents (avatar_path, 0700);
1346 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
1348 g_free (token_escaped);
1349 g_free (avatar_path);
1355 contact_load_avatar_cache (EmpathyContact *contact,
1358 EmpathyAvatar *avatar = NULL;
1362 GError *error = NULL;
1364 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
1365 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
1367 /* Load the avatar from file if it exists */
1368 filename = contact_get_avatar_filename (contact, token);
1369 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
1371 if (!g_file_get_contents (filename, &data, &len, &error))
1373 DEBUG ("Failed to load avatar from cache: %s",
1374 error ? error->message : "No error given");
1375 g_clear_error (&error);
1381 DEBUG ("Avatar loaded from %s", filename);
1382 avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
1383 contact_set_avatar (contact, avatar);
1384 empathy_avatar_unref (avatar);
1390 return data != NULL;
1394 empathy_avatar_get_type (void)
1396 static GType type_id = 0;
1400 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1401 (GBoxedCopyFunc) empathy_avatar_ref,
1402 (GBoxedFreeFunc) empathy_avatar_unref);
1409 * empathy_avatar_new:
1410 * @data: the avatar data
1411 * @len: the size of avatar data
1412 * @format: the mime type of the avatar image
1413 * @filename: the filename where the avatar is stored in cache
1415 * Create a #EmpathyAvatar from the provided data.
1417 * Returns: a new #EmpathyAvatar
1420 empathy_avatar_new (const guchar *data,
1422 const gchar *format,
1423 const gchar *filename)
1425 EmpathyAvatar *avatar;
1427 avatar = g_slice_new0 (EmpathyAvatar);
1428 avatar->data = g_memdup (data, len);
1430 avatar->format = g_strdup (format);
1431 avatar->filename = g_strdup (filename);
1432 avatar->refcount = 1;
1438 empathy_avatar_unref (EmpathyAvatar *avatar)
1440 g_return_if_fail (avatar != NULL);
1443 if (avatar->refcount == 0)
1445 g_free (avatar->data);
1446 g_free (avatar->format);
1447 g_free (avatar->filename);
1448 g_slice_free (EmpathyAvatar, avatar);
1453 empathy_avatar_ref (EmpathyAvatar *avatar)
1455 g_return_val_if_fail (avatar != NULL, NULL);
1463 * empathy_avatar_save_to_file:
1464 * @avatar: the avatar
1465 * @filename: name of a file to write avatar to
1466 * @error: return location for a GError, or NULL
1468 * Save the avatar to a file named filename
1470 * Returns: %TRUE on success, %FALSE if an error occurred
1473 empathy_avatar_save_to_file (EmpathyAvatar *self,
1474 const gchar *filename,
1477 return g_file_set_contents (filename, (const gchar *) self->data, self->len,
1482 * empathy_contact_get_location:
1483 * @contact: an #EmpathyContact
1485 * Returns the user's location if available. The keys are defined in
1486 * empathy-location.h. If the contact doesn't have location
1487 * information, the GHashTable will be empthy. Use #g_hash_table_unref when
1488 * you are done with the #GHashTable.
1490 * It is composed of string keys and GValues. Keys are
1491 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1492 * Example: a "city" key would have "Helsinki" as string GValue,
1493 * a "latitude" would have 65.0 as double GValue.
1495 * Returns: a #GHashTable of location values
1498 empathy_contact_get_location (EmpathyContact *contact)
1500 EmpathyContactPriv *priv;
1502 g_return_val_if_fail (EMPATHY_CONTACT (contact), NULL);
1504 priv = GET_PRIV (contact);
1506 return priv->location;
1510 * empathy_contact_set_location:
1511 * @contact: an #EmpathyContact
1512 * @location: a #GHashTable of the location
1514 * Sets the user's location based on the location #GHashTable passed.
1515 * It is composed of string keys and GValues. Keys are
1516 * defined in empathy-location.h such as #EMPATHY_LOCATION_COUNTRY.
1517 * Example: a "city" key would have "Helsinki" as string GValue,
1518 * a "latitude" would have 65.0 as double GValue.
1521 empathy_contact_set_location (EmpathyContact *contact,
1522 GHashTable *location)
1524 EmpathyContactPriv *priv;
1526 g_return_if_fail (EMPATHY_CONTACT (contact));
1527 g_return_if_fail (location != NULL);
1529 priv = GET_PRIV (contact);
1531 if (priv->location != NULL)
1532 g_hash_table_unref (priv->location);
1534 priv->location = g_hash_table_ref (location);
1536 update_geocode (contact);
1538 g_object_notify (G_OBJECT (contact), "location");
1541 const gchar * const *
1542 empathy_contact_get_client_types (EmpathyContact *contact)
1544 EmpathyContactPriv *priv;
1546 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1548 priv = GET_PRIV (contact);
1550 return (const gchar * const *) priv->client_types;
1554 contact_set_client_types (EmpathyContact *contact,
1555 const gchar * const *client_types)
1557 EmpathyContactPriv *priv = GET_PRIV (contact);
1559 if (priv->client_types != NULL)
1560 g_strfreev (priv->client_types);
1562 priv->client_types = g_strdupv ((gchar **) client_types);
1563 g_object_notify (G_OBJECT (contact), "client-types");
1567 * empathy_contact_equal:
1568 * @contact1: an #EmpathyContact
1569 * @contact2: an #EmpathyContact
1571 * Returns FALSE if one of the contacts is NULL but the other is not.
1572 * Otherwise returns TRUE if both pointer are equal or if they bith
1573 * refer to the same id.
1574 * It's only necessary to call this function if your contact objects
1575 * come from logs where contacts are created dynamically and comparing
1576 * pointers is not enough.
1579 empathy_contact_equal (gconstpointer contact1,
1580 gconstpointer contact2)
1587 if ((contact1 == NULL) != (contact2 == NULL)) {
1590 if (contact1 == contact2) {
1593 c1 = EMPATHY_CONTACT (contact1);
1594 c2 = EMPATHY_CONTACT (contact2);
1595 id1 = empathy_contact_get_id (c1);
1596 id2 = empathy_contact_get_id (c2);
1597 if (!tp_strdiff (id1, id2)) {
1604 /* This callback is called by geocode-glib when it found a position
1605 * for the given address. A position is necessary for a contact
1606 * to show up on the map
1609 geocode_cb (GObject *source,
1610 GAsyncResult *result,
1613 EmpathyContact *contact = user_data;
1614 EmpathyContactPriv *priv = GET_PRIV (contact);
1615 GError *error = NULL;
1616 GHashTable *new_location;
1617 GHashTable *resolved = NULL;
1618 gdouble latitude, longitude;
1620 if (priv->location == NULL)
1623 resolved = geocode_object_resolve_finish (GEOCODE_OBJECT (source), result,
1626 if (resolved == NULL)
1628 DEBUG ("Failed to resolve geocode: %s", error->message);
1629 g_error_free (error);
1633 if (!geocode_object_get_coords (resolved, &longitude, &latitude))
1636 new_location = tp_asv_new (
1637 EMPATHY_LOCATION_LAT, G_TYPE_DOUBLE, latitude,
1638 EMPATHY_LOCATION_LON, G_TYPE_DOUBLE, longitude,
1641 DEBUG ("\t - Latitude: %f", latitude);
1642 DEBUG ("\t - Longitude: %f", longitude);
1644 /* Copy remaning fields. LAT and LON were not defined so we won't overwrite
1645 * the values we just set. */
1646 tp_g_hash_table_update (new_location, priv->location,
1647 (GBoxedCopyFunc) g_strdup, (GBoxedCopyFunc) tp_g_value_slice_dup);
1649 /* Don't change the accuracy as we used an address to get this position */
1650 g_hash_table_unref (priv->location);
1651 priv->location = new_location;
1652 g_object_notify ((GObject *) contact, "location");
1655 tp_clear_pointer (&resolved, g_hash_table_unref);
1656 g_object_unref (contact);
1660 update_geocode (EmpathyContact *contact)
1662 GeocodeObject *geocode;
1663 GHashTable *location;
1665 location = empathy_contact_get_location (contact);
1666 if (location == NULL ||
1667 g_hash_table_size (location) == 0)
1670 /* No need to search for position if contact published it */
1671 if (g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL ||
1672 g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL)
1675 geocode = geocode_object_new_for_params (location);
1676 if (geocode == NULL)
1679 geocode_object_resolve_async (geocode, NULL, geocode_cb,
1680 g_object_ref (contact));
1682 g_object_unref (geocode);
1686 static EmpathyCapabilities
1687 tp_caps_to_capabilities (TpCapabilities *caps)
1689 EmpathyCapabilities capabilities = 0;
1693 classes = tp_capabilities_get_channel_classes (caps);
1695 for (i = 0; i < classes->len; i++)
1697 GValueArray *class_struct;
1698 GHashTable *fixed_prop;
1700 TpHandleType handle_type;
1701 const gchar *chan_type;
1703 class_struct = g_ptr_array_index (classes, i);
1704 tp_value_array_unpack (class_struct, 2,
1708 handle_type = tp_asv_get_uint32 (fixed_prop,
1709 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL);
1710 if (handle_type != TP_HANDLE_TYPE_CONTACT)
1713 chan_type = tp_asv_get_string (fixed_prop,
1714 TP_PROP_CHANNEL_CHANNEL_TYPE);
1716 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
1718 capabilities |= EMPATHY_CAPABILITIES_FT;
1720 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
1722 const gchar *service;
1724 service = tp_asv_get_string (fixed_prop,
1725 TP_PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE);
1727 if (!tp_strdiff (service, "rfb"))
1728 capabilities |= EMPATHY_CAPABILITIES_RFB_STREAM_TUBE;
1730 else if (!tp_strdiff (chan_type,
1731 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
1735 for (j = 0; allowed_prop[j] != NULL; j++)
1737 if (!tp_strdiff (allowed_prop[j],
1738 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO))
1739 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1740 else if (!tp_strdiff (allowed_prop[j],
1741 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO))
1742 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1745 if (tp_asv_get_boolean (fixed_prop,
1746 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, NULL))
1747 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1748 if (tp_asv_get_boolean (fixed_prop,
1749 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, NULL))
1750 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1752 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
1754 if (tp_asv_get_boolean (fixed_prop,
1755 TP_PROP_CHANNEL_INTERFACE_SMS_SMS_CHANNEL, NULL))
1756 capabilities |= EMPATHY_CAPABILITIES_SMS;
1758 else if (!tp_strdiff (chan_type,
1759 TPY_IFACE_CHANNEL_TYPE_CALL))
1763 if (tp_asv_get_boolean (fixed_prop,
1764 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, NULL))
1765 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1767 if (tp_asv_get_boolean (fixed_prop,
1768 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, NULL))
1769 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1771 for (j = 0; allowed_prop[j] != NULL; j++)
1773 if (!tp_strdiff (allowed_prop[j],
1774 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO))
1775 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
1776 else if (!tp_strdiff (allowed_prop[j],
1777 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO))
1778 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
1783 return capabilities;
1787 set_capabilities_from_tp_caps (EmpathyContact *self,
1788 TpCapabilities *caps)
1790 EmpathyCapabilities capabilities;
1795 capabilities = tp_caps_to_capabilities (caps);
1796 empathy_contact_set_capabilities (self, capabilities);
1800 contact_set_avatar_from_tp_contact (EmpathyContact *contact)
1802 EmpathyContactPriv *priv = GET_PRIV (contact);
1806 mime = tp_contact_get_avatar_mime_type (priv->tp_contact);
1807 file = tp_contact_get_avatar_file (priv->tp_contact);
1811 EmpathyAvatar *avatar;
1815 GError *error = NULL;
1817 if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
1819 DEBUG ("Failed to load avatar: %s", error->message);
1821 g_error_free (error);
1822 contact_set_avatar (contact, NULL);
1826 path = g_file_get_path (file);
1828 avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
1830 contact_set_avatar (contact, avatar);
1831 empathy_avatar_unref (avatar);
1837 contact_set_avatar (contact, NULL);
1842 empathy_contact_dup_from_tp_contact (TpContact *tp_contact)
1844 EmpathyContact *contact = NULL;
1846 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
1848 if (contacts_table == NULL)
1849 contacts_table = g_hash_table_new (g_direct_hash, g_direct_equal);
1851 contact = g_hash_table_lookup (contacts_table, tp_contact);
1853 if (contact == NULL)
1855 contact = empathy_contact_new (tp_contact);
1857 /* The hash table does not keep any ref.
1858 * contact keeps a ref to tp_contact, and is removed from the table in
1859 * contact_dispose() */
1860 g_hash_table_insert (contacts_table, tp_contact, contact);
1864 g_object_ref (contact);
1871 presence_cmp_func (EmpathyContact *a,
1874 FolksPresenceDetails *presence_a, *presence_b;
1876 presence_a = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (a));
1877 presence_b = FOLKS_PRESENCE_DETAILS (empathy_contact_get_persona (b));
1879 /* We negate the result because we're sorting in reverse order (i.e. such that
1880 * the Personas with the highest presence are at the beginning of the list. */
1881 return -folks_presence_details_typecmp (
1882 folks_presence_details_get_presence_type (presence_a),
1883 folks_presence_details_get_presence_type (presence_b));
1887 voip_cmp_func (EmpathyContact *a,
1890 gboolean has_audio_a, has_audio_b;
1891 gboolean has_video_a, has_video_b;
1893 has_audio_a = empathy_contact_can_voip_audio (a);
1894 has_audio_b = empathy_contact_can_voip_audio (b);
1895 has_video_a = empathy_contact_can_voip_video (a);
1896 has_video_b = empathy_contact_can_voip_video (b);
1898 /* First check video */
1899 if (has_video_a == has_video_b)
1901 /* Use audio to to break tie */
1902 if (has_audio_a == has_audio_b)
1904 else if (has_audio_a)
1909 else if (has_video_a)
1916 ft_cmp_func (EmpathyContact *a,
1919 gboolean can_send_files_a, can_send_files_b;
1921 can_send_files_a = empathy_contact_can_send_files (a);
1922 can_send_files_b = empathy_contact_can_send_files (b);
1924 if (can_send_files_a == can_send_files_b)
1926 else if (can_send_files_a)
1933 rfb_stream_tube_cmp_func (EmpathyContact *a,
1936 gboolean rfb_a, rfb_b;
1938 rfb_a = empathy_contact_can_use_rfb_stream_tube (a);
1939 rfb_b = empathy_contact_can_use_rfb_stream_tube (b);
1949 /* Sort by presence as with presence_cmp_func(), but if the two contacts have
1950 * the same presence, prefer the one which can do both audio *and* video calls,
1951 * over the one which can only do one of the two. */
1953 voip_sort_func (EmpathyContact *a, EmpathyContact *b)
1955 gint presence_sort = presence_cmp_func (a, b);
1957 if (presence_sort != 0)
1958 return presence_sort;
1960 return voip_cmp_func (a, b);
1963 /* Sort by presence as with presence_cmp_func() and then break ties using the
1964 * most "capable" individual. So users will be able to pick more actions on
1965 * the contact in the "Contact" menu of the chat window. */
1967 chat_sort_func (EmpathyContact *a,
1972 result = presence_cmp_func (a, b);
1976 /* Prefer individual supporting file transfer */
1977 result = ft_cmp_func (a, b);
1981 /* Check audio/video capabilities */
1982 result = voip_cmp_func (a, b);
1986 /* Check 'Share my destop' feature */
1987 return rfb_stream_tube_cmp_func (a, b);
1991 get_sort_func_for_action (EmpathyActionType action_type)
1993 switch (action_type)
1995 case EMPATHY_ACTION_AUDIO_CALL:
1996 case EMPATHY_ACTION_VIDEO_CALL:
1997 return (GCompareFunc) voip_sort_func;
1998 case EMPATHY_ACTION_CHAT:
1999 return (GCompareFunc) chat_sort_func;
2000 case EMPATHY_ACTION_VIEW_LOGS:
2001 case EMPATHY_ACTION_SEND_FILE:
2002 case EMPATHY_ACTION_SHARE_MY_DESKTOP:
2004 return (GCompareFunc) presence_cmp_func;
2009 * empathy_contact_dup_best_for_action:
2010 * @individual: a #FolksIndividual
2011 * @action_type: the type of action to be performed on the contact
2013 * Chooses a #FolksPersona from the given @individual which is best-suited for
2014 * the given @action_type. "Best-suited" is determined by choosing the persona
2015 * with the highest presence out of all the personas which can perform the given
2016 * @action_type (e.g. are capable of video calling).
2018 * Return value: an #EmpathyContact for the best persona, or %NULL;
2019 * unref with g_object_unref()
2022 empathy_contact_dup_best_for_action (FolksIndividual *individual,
2023 EmpathyActionType action_type)
2028 EmpathyContact *best_contact = NULL;
2030 /* Build a list of EmpathyContacts that we can sort */
2031 personas = folks_individual_get_personas (individual);
2034 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2035 while (gee_iterator_next (iter))
2037 FolksPersona *persona = gee_iterator_get (iter);
2038 TpContact *tp_contact;
2039 EmpathyContact *contact = NULL;
2041 if (!empathy_folks_persona_is_interesting (persona))
2044 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
2045 if (tp_contact == NULL)
2048 contact = empathy_contact_dup_from_tp_contact (tp_contact);
2049 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
2051 /* Only choose the contact if they're actually capable of the specified
2053 if (empathy_contact_can_do_action (contact, action_type))
2054 contacts = g_list_prepend (contacts, g_object_ref (contact));
2057 g_clear_object (&contact);
2058 g_clear_object (&persona);
2060 g_clear_object (&iter);
2062 /* Sort the contacts by some heuristic based on the action type, then take
2063 * the top contact. */
2064 if (contacts != NULL)
2066 contacts = g_list_sort (contacts, get_sort_func_for_action (action_type));
2067 best_contact = g_object_ref (contacts->data);
2070 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2071 g_list_free (contacts);
2073 return best_contact;