1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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 <telepathy-glib/util.h>
27 #include <telepathy-glib/gtypes.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/interfaces.h>
32 #include <geoclue/geoclue-geocode.h>
35 #include <extensions/extensions.h>
37 #include "empathy-tp-contact-factory.h"
38 #include "empathy-utils.h"
39 #include "empathy-location.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
46 TpConnection *connection;
49 gchar **avatar_mime_types;
50 guint avatar_min_width;
51 guint avatar_min_height;
52 guint avatar_max_width;
53 guint avatar_max_height;
54 guint avatar_max_size;
55 /* can_request_ft and can_request_st are meaningful only if the connection
56 * doesn't implement ContactCapabilities. If it's implemented, we use it to
57 * check if contacts support file transfer and stream tubes. */
58 gboolean can_request_ft;
59 gboolean can_request_st;
60 /* TRUE if ContactCapabilities is implemented by the Connection */
61 gboolean contact_caps_supported;
62 } EmpathyTpContactFactoryPriv;
64 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
78 static TpContactFeature contact_features[] = {
79 TP_CONTACT_FEATURE_ALIAS,
80 TP_CONTACT_FEATURE_PRESENCE,
83 static EmpathyContact *
84 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
87 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
90 for (l = priv->contacts; l; l = l->next) {
91 if (empathy_contact_get_handle (l->data) == handle) {
99 static EmpathyContact *
100 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
101 TpContact *tp_contact)
103 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
106 for (l = priv->contacts; l; l = l->next) {
107 if (empathy_contact_get_tp_contact (l->data) == tp_contact) {
116 tp_contact_factory_weak_notify (gpointer data,
117 GObject *where_the_object_was)
119 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
121 DEBUG ("Remove finalized contact %p", where_the_object_was);
123 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
127 tp_contact_factory_set_aliases_cb (TpConnection *connection,
133 DEBUG ("Error: %s", error->message);
138 tp_contact_factory_set_location_cb (TpConnection *tp_conn,
141 GObject *weak_object)
144 DEBUG ("Error setting location: %s", error->message);
149 tp_contact_factory_set_avatar_cb (TpConnection *connection,
156 DEBUG ("Error: %s", error->message);
161 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
167 DEBUG ("Error: %s", error->message);
172 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
175 const GArray *avatar_data,
176 const gchar *mime_type,
180 EmpathyContact *contact;
182 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
188 DEBUG ("Avatar retrieved for contact %s (%d)",
189 empathy_contact_get_id (contact),
192 empathy_contact_load_avatar_data (contact,
193 (guchar *) avatar_data->data,
200 tp_contact_factory_request_avatars_cb (TpConnection *connection,
206 DEBUG ("Error: %s", error->message);
211 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
215 EmpathyContact *contact;
216 EmpathyAvatar *avatar;
218 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
223 /* Check if we have an avatar */
224 if (EMP_STR_EMPTY (token)) {
225 empathy_contact_set_avatar (contact, NULL);
229 /* Check if the avatar changed */
230 avatar = empathy_contact_get_avatar (contact);
231 if (avatar && !tp_strdiff (avatar->token, token)) {
235 /* The avatar changed, search the new one in the cache */
236 if (empathy_contact_load_avatar_cache (contact, token)) {
237 /* Got from cache, use it */
241 /* Avatar is not up-to-date, we have to request it. */
246 tp_contact_factory_got_known_avatar_tokens (TpConnection *connection,
250 GObject *weak_object)
252 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
253 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
259 DEBUG ("Error: %s", error->message);
263 handles = g_array_new (FALSE, FALSE, sizeof (guint));
265 g_hash_table_iter_init (&iter, tokens);
266 while (g_hash_table_iter_next (&iter, &key, &value)) {
267 guint handle = GPOINTER_TO_UINT (key);
268 const gchar *token = value;
270 if (!tp_contact_factory_avatar_maybe_update (tp_factory,
272 g_array_append_val (handles, handle);
276 DEBUG ("Got %d tokens, need to request %d avatars",
277 g_hash_table_size (tokens), handles->len);
279 /* Request needed avatars */
280 if (handles->len > 0) {
281 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
284 tp_contact_factory_request_avatars_cb,
286 G_OBJECT (tp_factory));
289 g_array_free (handles, TRUE);
293 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
295 const gchar *new_token,
301 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
302 handle, new_token)) {
303 /* Avatar was cached, nothing to do */
307 DEBUG ("Need to request avatar for token %s", new_token);
309 handles = g_array_new (FALSE, FALSE, sizeof (guint));
310 g_array_append_val (handles, handle);
312 tp_cli_connection_interface_avatars_call_request_avatars (connection,
315 tp_contact_factory_request_avatars_cb,
318 g_array_free (handles, TRUE);
322 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
324 const gchar *channel_type,
328 EmpathyContact *contact;
329 EmpathyCapabilities capabilities;
331 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
336 capabilities = empathy_contact_get_capabilities (contact);
337 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
339 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
340 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
341 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
342 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
343 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
345 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
346 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
350 DEBUG ("Changing capabilities for contact %s (%d) to %d",
351 empathy_contact_get_id (contact),
352 empathy_contact_get_handle (contact),
355 empathy_contact_set_capabilities (contact, capabilities);
359 tp_contact_factory_got_capabilities (TpConnection *connection,
360 const GPtrArray *capabilities,
363 GObject *weak_object)
365 EmpathyTpContactFactory *tp_factory;
368 tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
371 DEBUG ("Error: %s", error->message);
372 /* FIXME Should set the capabilities of the contacts for which this request
373 * originated to NONE */
377 for (i = 0; i < capabilities->len; i++) {
380 const gchar *channel_type;
384 values = g_ptr_array_index (capabilities, i);
385 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
386 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
387 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
388 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
390 tp_contact_factory_update_capabilities (tp_factory,
399 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
400 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
402 /* This callback is called by geoclue when it found a position
403 * for the given address. A position is necessary for a contact
404 * to show up on the map
407 geocode_cb (GeoclueGeocode *geocode,
408 GeocluePositionFields fields,
412 GeoclueAccuracy *accuracy,
417 GHashTable *location;
419 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
422 DEBUG ("Error geocoding location : %s", error->message);
423 g_object_unref (geocode);
424 g_object_unref (contact);
428 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
429 new_value = tp_g_value_slice_new_double (latitude);
430 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
432 DEBUG ("\t - Latitude: %f", latitude);
434 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
435 new_value = tp_g_value_slice_new_double (longitude);
436 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
438 DEBUG ("\t - Longitude: %f", longitude);
440 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
441 new_value = tp_g_value_slice_new_double (altitude);
442 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
444 DEBUG ("\t - Altitude: %f", altitude);
447 /* Don't change the accuracy as we used an address to get this position */
448 g_object_notify (contact, "location");
449 g_object_unref (geocode);
450 g_object_unref (contact);
456 get_dup_string (GHashTable *location,
461 value = g_hash_table_lookup (location, key);
463 return g_value_dup_string (value);
470 tp_contact_factory_geocode (EmpathyContact *contact)
473 static GeoclueGeocode *geocode;
477 GHashTable *location;
479 location = empathy_contact_get_location (contact);
480 if (location == NULL)
483 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
487 if (geocode == NULL) {
488 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
489 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
492 g_object_ref (geocode);
494 address = geoclue_address_details_new ();
496 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
498 g_hash_table_insert (address,
499 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
500 DEBUG ("\t - countrycode: %s", str);
503 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
505 g_hash_table_insert (address,
506 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
507 DEBUG ("\t - country: %s", str);
510 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
512 g_hash_table_insert (address,
513 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
514 DEBUG ("\t - postalcode: %s", str);
517 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
519 g_hash_table_insert (address,
520 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
521 DEBUG ("\t - region: %s", str);
524 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
526 g_hash_table_insert (address,
527 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
528 DEBUG ("\t - locality: %s", str);
531 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
533 g_hash_table_insert (address,
534 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
535 DEBUG ("\t - street: %s", str);
538 g_object_ref (contact);
539 geoclue_geocode_address_to_position_async (geocode, address,
540 geocode_cb, contact);
542 g_hash_table_unref (address);
547 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
549 GHashTable *location)
551 EmpathyContact *contact;
552 GHashTable *new_location;
554 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
559 new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
560 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
561 tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
562 (GBoxedCopyFunc) tp_g_value_slice_dup);
563 empathy_contact_set_location (contact, new_location);
564 g_hash_table_unref (new_location);
566 tp_contact_factory_geocode (contact);
570 tp_contact_factory_got_locations (TpConnection *tp_conn,
571 GHashTable *locations,
574 GObject *weak_object)
578 EmpathyTpContactFactory *tp_factory;
580 tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
582 DEBUG ("Error: %s", error->message);
586 g_hash_table_iter_init (&iter, locations);
587 while (g_hash_table_iter_next (&iter, &key, &value)) {
588 guint handle = GPOINTER_TO_INT (key);
589 GHashTable *location = value;
591 tp_contact_factory_update_location (tp_factory, handle, location);
596 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
597 const GPtrArray *capabilities,
599 GObject *weak_object)
601 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
604 for (i = 0; i < capabilities->len; i++) {
607 const gchar *channel_type;
611 values = g_ptr_array_index (capabilities, i);
612 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
613 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
614 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
615 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
617 tp_contact_factory_update_capabilities (tp_factory,
626 tp_contact_factory_location_updated_cb (TpConnection *tp_conn,
628 GHashTable *location,
630 GObject *weak_object)
632 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
633 tp_contact_factory_update_location (tp_factory, handle, location);
636 static EmpathyCapabilities
637 channel_classes_to_capabilities (GPtrArray *classes,
638 gboolean audio_video)
640 EmpathyCapabilities capabilities = 0;
643 for (i = 0; i < classes->len; i++) {
644 GValueArray *class_struct;
645 GHashTable *fixed_prop;
647 TpHandleType handle_type;
648 const gchar *chan_type;
650 class_struct = g_ptr_array_index (classes, i);
651 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
652 allowed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 1));
654 handle_type = tp_asv_get_uint32 (fixed_prop,
655 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
656 if (handle_type != TP_HANDLE_TYPE_CONTACT)
659 chan_type = tp_asv_get_string (fixed_prop,
660 TP_IFACE_CHANNEL ".ChannelType");
662 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) {
663 capabilities |= EMPATHY_CAPABILITIES_FT;
666 else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE)) {
667 capabilities |= EMPATHY_CAPABILITIES_STREAM_TUBE;
669 else if (audio_video && !tp_strdiff (chan_type,
670 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
673 for (j = 0; allowed_prop[j] != NULL; j++) {
674 if (!tp_strdiff (allowed_prop[j],
675 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialAudio"))
676 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
677 else if (!tp_strdiff (allowed_prop[j],
678 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo"))
679 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
688 get_requestable_channel_classes_cb (TpProxy *connection,
692 GObject *weak_object)
694 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
695 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
698 EmpathyCapabilities capabilities;
701 DEBUG ("Error: %s", error->message);
705 classes = g_value_get_boxed (value);
707 DEBUG ("ContactCapabilities is not implemented; use RCC");
708 capabilities = channel_classes_to_capabilities (classes, FALSE);
709 if ((capabilities & EMPATHY_CAPABILITIES_FT) != 0) {
710 DEBUG ("Assume all contacts support FT as CM implements it");
711 priv->can_request_ft = TRUE;
714 if ((capabilities & EMPATHY_CAPABILITIES_STREAM_TUBE) != 0) {
715 DEBUG ("Assume all contacts support stream tubes as CM implements them");
716 priv->can_request_st = TRUE;
719 if (!priv->can_request_ft && !priv->can_request_st)
722 /* Update the capabilities of all contacts */
723 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
724 EmpathyContact *contact = l->data;
725 EmpathyCapabilities caps;
727 caps = empathy_contact_get_capabilities (contact);
729 /* ContactCapabilities is not supported; assume all contacts can do file
730 * transfer if it's implemented in the CM */
731 if (priv->can_request_ft)
732 caps |= EMPATHY_CAPABILITIES_FT;
734 if (priv->can_request_st)
735 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
737 empathy_contact_set_capabilities (contact, caps);
742 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
743 const gchar **mime_types,
753 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
756 DEBUG ("Failed to get avatar requirements: %s", error->message);
757 /* We'll just leave avatar_mime_types as NULL; the
758 * avatar-setting code can use this as a signal that you can't
762 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
763 priv->avatar_min_width = min_width;
764 priv->avatar_min_height = min_height;
765 priv->avatar_max_width = max_width;
766 priv->avatar_max_height = max_height;
767 priv->avatar_max_size = max_size;
772 update_contact_capabilities (EmpathyTpContactFactory *self,
778 g_hash_table_iter_init (&iter, caps);
779 while (g_hash_table_iter_next (&iter, &key, &value)) {
780 TpHandle handle = GPOINTER_TO_UINT (key);
781 GPtrArray *classes = value;
782 EmpathyContact *contact;
783 EmpathyCapabilities capabilities;
785 contact = tp_contact_factory_find_by_handle (self, handle);
789 capabilities = empathy_contact_get_capabilities (contact);
790 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
792 capabilities |= channel_classes_to_capabilities (classes, TRUE);
794 DEBUG ("Changing capabilities for contact %s (%d) to %d",
795 empathy_contact_get_id (contact),
796 empathy_contact_get_handle (contact),
799 empathy_contact_set_capabilities (contact, capabilities);
804 tp_contact_factory_got_contact_capabilities (TpConnection *connection,
808 GObject *weak_object)
810 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
813 DEBUG ("Error: %s", error->message);
817 update_contact_capabilities (self, caps);
821 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
822 EmpathyContact *contact)
824 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
825 TpHandle self_handle;
827 GArray handles = {(gchar *) &handle, 1};
828 EmpathyCapabilities caps;
830 /* Keep a weak ref to that contact */
831 g_object_weak_ref (G_OBJECT (contact),
832 tp_contact_factory_weak_notify,
834 priv->contacts = g_list_prepend (priv->contacts, contact);
836 /* The contact keeps a ref to its factory */
837 g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
838 g_object_ref (tp_factory),
841 caps = empathy_contact_get_capabilities (contact);
843 /* Set the FT capability */
844 if (!priv->contact_caps_supported) {
845 /* ContactCapabilities is not supported; assume all contacts can do file
846 * transfer if it's implemented in the CM */
847 if (priv->can_request_ft) {
848 caps |= EMPATHY_CAPABILITIES_FT;
851 /* Set the Stream Tube capability */
852 if (priv->can_request_st) {
853 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
857 empathy_contact_set_capabilities (contact, caps);
859 /* Set is-user property. Note that it could still be the handle is
860 * different from the connection's self handle, in the case the handle
861 * comes from a group interface. */
862 self_handle = tp_connection_get_self_handle (priv->connection);
863 handle = empathy_contact_get_handle (contact);
864 empathy_contact_set_is_user (contact, self_handle == handle);
866 /* FIXME: This should be done by TpContact */
867 if (tp_proxy_has_interface_by_id (priv->connection,
868 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS)) {
869 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (
870 priv->connection, -1, &handles,
871 tp_contact_factory_got_known_avatar_tokens, NULL, NULL,
872 G_OBJECT (tp_factory));
875 if (priv->contact_caps_supported) {
876 tp_cli_connection_interface_contact_capabilities_call_get_contact_capabilities (
877 priv->connection, -1, &handles,
878 tp_contact_factory_got_contact_capabilities, NULL, NULL,
879 G_OBJECT (tp_factory));
881 else if (tp_proxy_has_interface_by_id (priv->connection,
882 TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES)) {
883 tp_cli_connection_interface_capabilities_call_get_capabilities (
884 priv->connection, -1, &handles,
885 tp_contact_factory_got_capabilities, NULL, NULL,
886 G_OBJECT (tp_factory));
889 if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
890 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
891 tp_cli_connection_interface_location_call_get_locations (priv->connection,
894 tp_contact_factory_got_locations,
900 DEBUG ("Contact added: %s (%d)",
901 empathy_contact_get_id (contact),
902 empathy_contact_get_handle (contact));
906 EmpathyTpContactFactoryContactsByIdCb ids_cb;
907 EmpathyTpContactFactoryContactsByHandleCb handles_cb;
908 EmpathyTpContactFactoryContactCb contact_cb;
912 EmpathyTpContactFactory *tp_factory;
913 GetContactsCb callback;
915 GDestroyNotify destroy;
919 get_contacts_data_free (gpointer user_data)
921 GetContactsData *data = user_data;
924 data->destroy (data->user_data);
926 g_object_unref (data->tp_factory);
928 g_slice_free (GetContactsData, data);
931 static EmpathyContact *
932 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
933 TpContact *tp_contact)
935 EmpathyContact *contact;
937 contact = tp_contact_factory_find_by_tp_contact (tp_factory,
940 if (contact != NULL) {
941 g_object_ref (contact);
943 contact = empathy_contact_new (tp_contact);
944 tp_contact_factory_add_contact (tp_factory, contact);
950 static EmpathyContact **
951 contacts_array_new (EmpathyTpContactFactory *tp_factory,
953 TpContact * const * contacts)
955 EmpathyContact **ret;
958 ret = g_new0 (EmpathyContact *, n_contacts);
959 for (i = 0; i < n_contacts; i++) {
960 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
967 contacts_array_free (guint n_contacts,
968 EmpathyContact **contacts)
972 for (i = 0; i < n_contacts; i++) {
973 g_object_unref (contacts[i]);
979 get_contacts_by_id_cb (TpConnection *connection,
981 TpContact * const *contacts,
982 const gchar * const *requested_ids,
983 GHashTable *failed_id_errors,
986 GObject *weak_object)
988 GetContactsData *data = user_data;
989 EmpathyContact **empathy_contacts;
991 empathy_contacts = contacts_array_new (data->tp_factory,
992 n_contacts, contacts);
993 if (data->callback.ids_cb) {
994 data->callback.ids_cb (data->tp_factory,
995 n_contacts, empathy_contacts,
999 data->user_data, weak_object);
1002 contacts_array_free (n_contacts, empathy_contacts);
1005 /* The callback is NOT given a reference to the EmpathyContact objects */
1007 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
1009 const gchar * const *ids,
1010 EmpathyTpContactFactoryContactsByIdCb callback,
1012 GDestroyNotify destroy,
1013 GObject *weak_object)
1015 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1016 GetContactsData *data;
1018 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1019 g_return_if_fail (ids != NULL);
1021 data = g_slice_new (GetContactsData);
1022 data->callback.ids_cb = callback;
1023 data->user_data = user_data;
1024 data->destroy = destroy;
1025 data->tp_factory = g_object_ref (tp_factory);
1026 tp_connection_get_contacts_by_id (priv->connection,
1028 G_N_ELEMENTS (contact_features),
1030 get_contacts_by_id_cb,
1032 (GDestroyNotify) get_contacts_data_free,
1037 get_contact_by_id_cb (TpConnection *connection,
1039 TpContact * const *contacts,
1040 const gchar * const *requested_ids,
1041 GHashTable *failed_id_errors,
1042 const GError *error,
1044 GObject *weak_object)
1046 GetContactsData *data = user_data;
1047 EmpathyContact *contact = NULL;
1049 if (n_contacts == 1) {
1050 contact = dup_contact_for_tp_contact (data->tp_factory,
1053 else if (error == NULL) {
1054 GHashTableIter iter;
1057 g_hash_table_iter_init (&iter, failed_id_errors);
1058 while (g_hash_table_iter_next (&iter, NULL, &value)) {
1066 if (data->callback.contact_cb) {
1067 data->callback.contact_cb (data->tp_factory,
1070 data->user_data, weak_object);
1073 if (contact != NULL)
1074 g_object_unref (contact);
1077 /* The callback is NOT given a reference to the EmpathyContact objects */
1079 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
1081 EmpathyTpContactFactoryContactCb callback,
1083 GDestroyNotify destroy,
1084 GObject *weak_object)
1086 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1087 GetContactsData *data;
1089 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1090 g_return_if_fail (id != NULL);
1092 data = g_slice_new (GetContactsData);
1093 data->callback.contact_cb = callback;
1094 data->user_data = user_data;
1095 data->destroy = destroy;
1096 data->tp_factory = g_object_ref (tp_factory);
1097 tp_connection_get_contacts_by_id (priv->connection,
1099 G_N_ELEMENTS (contact_features),
1101 get_contact_by_id_cb,
1103 (GDestroyNotify) get_contacts_data_free,
1108 get_contacts_by_handle_cb (TpConnection *connection,
1110 TpContact * const *contacts,
1112 const TpHandle *failed,
1113 const GError *error,
1115 GObject *weak_object)
1117 GetContactsData *data = user_data;
1118 EmpathyContact **empathy_contacts;
1120 empathy_contacts = contacts_array_new (data->tp_factory,
1121 n_contacts, contacts);
1122 if (data->callback.handles_cb) {
1123 data->callback.handles_cb (data->tp_factory,
1124 n_contacts, empathy_contacts,
1127 data->user_data, weak_object);
1130 contacts_array_free (n_contacts, empathy_contacts);
1133 /* The callback is NOT given a reference to the EmpathyContact objects */
1135 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1137 const TpHandle *handles,
1138 EmpathyTpContactFactoryContactsByHandleCb callback,
1140 GDestroyNotify destroy,
1141 GObject *weak_object)
1143 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1144 GetContactsData *data;
1146 if (n_handles == 0) {
1147 callback (tp_factory, 0, NULL, 0, NULL, NULL, user_data, weak_object);
1151 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1152 g_return_if_fail (handles != NULL);
1154 data = g_slice_new (GetContactsData);
1155 data->callback.handles_cb = callback;
1156 data->user_data = user_data;
1157 data->destroy = destroy;
1158 data->tp_factory = g_object_ref (tp_factory);
1159 tp_connection_get_contacts_by_handle (priv->connection,
1161 G_N_ELEMENTS (contact_features),
1163 get_contacts_by_handle_cb,
1165 (GDestroyNotify) get_contacts_data_free,
1169 /* The callback is NOT given a reference to the EmpathyContact objects */
1171 get_contact_by_handle_cb (TpConnection *connection,
1173 TpContact * const *contacts,
1175 const TpHandle *failed,
1176 const GError *error,
1178 GObject *weak_object)
1180 GetContactsData *data = user_data;
1181 EmpathyContact *contact = NULL;
1184 if (n_contacts == 1) {
1185 contact = dup_contact_for_tp_contact (data->tp_factory,
1189 if (error == NULL) {
1190 /* tp-glib will provide an error only if the whole operation failed,
1191 * but not if, for example, the handle was invalid. We create an error
1192 * so the caller of empathy_tp_contact_factory_get_from_handle can
1193 * rely on the error to check if the operation succeeded or not. */
1195 err = g_error_new_literal (TP_ERRORS, TP_ERROR_INVALID_HANDLE,
1196 "handle is invalid");
1199 err = g_error_copy (error);
1203 if (data->callback.contact_cb) {
1204 data->callback.contact_cb (data->tp_factory,
1207 data->user_data, weak_object);
1210 g_clear_error (&err);
1211 if (contact != NULL)
1212 g_object_unref (contact);
1216 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1218 EmpathyTpContactFactoryContactCb callback,
1220 GDestroyNotify destroy,
1221 GObject *weak_object)
1223 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1224 GetContactsData *data;
1226 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1228 data = g_slice_new (GetContactsData);
1229 data->callback.contact_cb = callback;
1230 data->user_data = user_data;
1231 data->destroy = destroy;
1232 data->tp_factory = g_object_ref (tp_factory);
1233 tp_connection_get_contacts_by_handle (priv->connection,
1235 G_N_ELEMENTS (contact_features),
1237 get_contact_by_handle_cb,
1239 (GDestroyNotify) get_contacts_data_free,
1244 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1245 EmpathyContact *contact,
1248 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1249 GHashTable *new_alias;
1252 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1253 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1255 handle = empathy_contact_get_handle (contact);
1257 DEBUG ("Setting alias for contact %s (%d) to %s",
1258 empathy_contact_get_id (contact),
1261 new_alias = g_hash_table_new_full (g_direct_hash,
1266 g_hash_table_insert (new_alias,
1267 GUINT_TO_POINTER (handle),
1270 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1273 tp_contact_factory_set_aliases_cb,
1275 G_OBJECT (tp_factory));
1277 g_hash_table_destroy (new_alias);
1281 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1284 const gchar *mime_type)
1286 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1288 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1290 if (data && size > 0 && size < G_MAXUINT) {
1293 avatar.data = (gchar *) data;
1296 DEBUG ("Setting avatar on connection %s",
1297 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1299 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1303 tp_contact_factory_set_avatar_cb,
1305 G_OBJECT (tp_factory));
1307 DEBUG ("Clearing avatar on connection %s",
1308 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1310 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1312 tp_contact_factory_clear_avatar_cb,
1314 G_OBJECT (tp_factory));
1319 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1320 GHashTable *location)
1322 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1324 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1326 DEBUG ("Setting location");
1328 tp_cli_connection_interface_location_call_set_location (priv->connection,
1331 tp_contact_factory_set_location_cb,
1333 G_OBJECT (tp_factory));
1337 tp_contact_factory_get_property (GObject *object,
1342 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1345 case PROP_CONNECTION:
1346 g_value_set_object (value, priv->connection);
1348 case PROP_MIME_TYPES:
1349 g_value_set_boxed (value, priv->avatar_mime_types);
1351 case PROP_MIN_WIDTH:
1352 g_value_set_uint (value, priv->avatar_min_width);
1354 case PROP_MIN_HEIGHT:
1355 g_value_set_uint (value, priv->avatar_min_height);
1357 case PROP_MAX_WIDTH:
1358 g_value_set_uint (value, priv->avatar_max_width);
1360 case PROP_MAX_HEIGHT:
1361 g_value_set_uint (value, priv->avatar_max_height);
1364 g_value_set_uint (value, priv->avatar_max_size);
1367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1373 tp_contact_factory_set_property (GObject *object,
1375 const GValue *value,
1378 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1381 case PROP_CONNECTION:
1382 priv->connection = g_value_dup_object (value);
1385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1391 tp_contact_factory_finalize (GObject *object)
1393 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1396 DEBUG ("Finalized: %p", object);
1398 for (l = priv->contacts; l; l = l->next) {
1399 g_object_weak_unref (G_OBJECT (l->data),
1400 tp_contact_factory_weak_notify,
1404 g_list_free (priv->contacts);
1406 g_object_unref (priv->connection);
1408 g_strfreev (priv->avatar_mime_types);
1410 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1414 tp_contact_factory_contact_capabilities_changed_cb (TpConnection *connection,
1417 GObject *weak_object)
1419 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
1421 update_contact_capabilities (self, caps);
1425 connection_ready_cb (TpConnection *connection,
1426 const GError *error,
1429 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
1430 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1435 /* FIXME: This should be moved to TpContact */
1436 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1437 tp_contact_factory_avatar_updated_cb,
1439 G_OBJECT (tp_factory),
1441 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1442 tp_contact_factory_avatar_retrieved_cb,
1444 G_OBJECT (tp_factory),
1447 if (tp_proxy_has_interface_by_id (connection,
1448 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_CAPABILITIES)) {
1449 priv->contact_caps_supported = TRUE;
1451 tp_cli_connection_interface_contact_capabilities_connect_to_contact_capabilities_changed (
1452 priv->connection, tp_contact_factory_contact_capabilities_changed_cb,
1453 NULL, NULL, G_OBJECT (tp_factory), NULL);
1456 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1457 tp_contact_factory_capabilities_changed_cb,
1459 G_OBJECT (tp_factory),
1463 tp_cli_connection_interface_location_connect_to_location_updated (priv->connection,
1464 tp_contact_factory_location_updated_cb,
1466 G_OBJECT (tp_factory),
1469 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1471 tp_contact_factory_got_avatar_requirements_cb,
1473 G_OBJECT (tp_factory));
1475 if (!priv->contact_caps_supported) {
1476 tp_cli_dbus_properties_call_get (priv->connection, -1,
1477 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1478 "RequestableChannelClasses",
1479 get_requestable_channel_classes_cb, NULL, NULL,
1480 G_OBJECT (tp_factory));
1484 g_object_unref (tp_factory);
1488 tp_contact_factory_constructor (GType type,
1490 GObjectConstructParam *props)
1492 GObject *tp_factory;
1493 EmpathyTpContactFactoryPriv *priv;
1495 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1496 priv = GET_PRIV (tp_factory);
1498 /* Ensure to keep the self object alive while the call_when_ready is
1500 g_object_ref (tp_factory);
1501 tp_connection_call_when_ready (priv->connection, connection_ready_cb,
1508 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1510 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1512 object_class->finalize = tp_contact_factory_finalize;
1513 object_class->constructor = tp_contact_factory_constructor;
1514 object_class->get_property = tp_contact_factory_get_property;
1515 object_class->set_property = tp_contact_factory_set_property;
1517 g_object_class_install_property (object_class,
1519 g_param_spec_object ("connection",
1520 "Factory's Connection",
1521 "The connection associated with the factory",
1524 G_PARAM_CONSTRUCT_ONLY |
1525 G_PARAM_STATIC_STRINGS));
1526 g_object_class_install_property (object_class,
1528 g_param_spec_boxed ("avatar-mime-types",
1529 "Supported MIME types for avatars",
1530 "Types of images that may be set as "
1531 "avatars on this connection.",
1534 G_PARAM_STATIC_STRINGS));
1535 g_object_class_install_property (object_class,
1537 g_param_spec_uint ("avatar-min-width",
1538 "Minimum width for avatars",
1539 "Minimum width of avatar that may be set.",
1544 G_PARAM_STATIC_STRINGS));
1545 g_object_class_install_property (object_class,
1547 g_param_spec_uint ("avatar-min-height",
1548 "Minimum height for avatars",
1549 "Minimum height of avatar that may be set.",
1554 G_PARAM_STATIC_STRINGS));
1555 g_object_class_install_property (object_class,
1557 g_param_spec_uint ("avatar-max-width",
1558 "Maximum width for avatars",
1559 "Maximum width of avatar that may be set "
1560 "or 0 if there is no maximum.",
1565 G_PARAM_STATIC_STRINGS));
1566 g_object_class_install_property (object_class,
1568 g_param_spec_uint ("avatar-max-height",
1569 "Maximum height for avatars",
1570 "Maximum height of avatar that may be set "
1571 "or 0 if there is no maximum.",
1576 G_PARAM_STATIC_STRINGS));
1577 g_object_class_install_property (object_class,
1579 g_param_spec_uint ("avatar-max-size",
1580 "Maximum size for avatars in bytes",
1581 "Maximum file size of avatar that may be "
1582 "set or 0 if there is no maximum.",
1587 G_PARAM_STATIC_STRINGS));
1590 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1594 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1596 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1597 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1599 tp_factory->priv = priv;
1600 priv->can_request_ft = FALSE;
1601 priv->can_request_st = FALSE;
1602 priv->contact_caps_supported = FALSE;
1605 static GHashTable *factories = NULL;
1608 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1614 DEBUG ("Message: %s", message);
1615 g_hash_table_remove (factories, connection);
1619 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1620 GObject *where_the_object_was)
1622 g_hash_table_remove (factories, connection);
1626 tp_contact_factory_remove_connection (gpointer connection)
1628 g_signal_handlers_disconnect_by_func (connection,
1629 tp_contact_factory_connection_invalidated_cb, NULL);
1630 g_object_unref (connection);
1633 EmpathyTpContactFactory *
1634 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1636 EmpathyTpContactFactory *tp_factory;
1638 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1640 if (factories == NULL) {
1641 factories = g_hash_table_new_full (empathy_proxy_hash,
1642 empathy_proxy_equal,
1643 tp_contact_factory_remove_connection,
1647 tp_factory = g_hash_table_lookup (factories, connection);
1648 if (tp_factory == NULL) {
1649 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1650 "connection", connection,
1652 g_hash_table_insert (factories, g_object_ref (connection),
1654 g_object_weak_ref (G_OBJECT (tp_factory),
1655 tp_contact_factory_connection_weak_notify_cb,
1657 g_signal_connect (connection, "invalidated",
1658 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1661 g_object_ref (tp_factory);