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 gboolean can_request_ft;
56 gboolean can_request_st;
57 /* TRUE if ContactCapabilities is implemented by the Connection */
58 gboolean contact_caps_supported;
59 } EmpathyTpContactFactoryPriv;
61 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
75 static TpContactFeature contact_features[] = {
76 TP_CONTACT_FEATURE_ALIAS,
77 TP_CONTACT_FEATURE_PRESENCE,
80 static EmpathyContact *
81 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
84 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
87 for (l = priv->contacts; l; l = l->next) {
88 if (empathy_contact_get_handle (l->data) == handle) {
96 static EmpathyContact *
97 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
98 TpContact *tp_contact)
100 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
103 for (l = priv->contacts; l; l = l->next) {
104 if (empathy_contact_get_tp_contact (l->data) == tp_contact) {
113 tp_contact_factory_weak_notify (gpointer data,
114 GObject *where_the_object_was)
116 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
118 DEBUG ("Remove finalized contact %p", where_the_object_was);
120 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
124 tp_contact_factory_set_aliases_cb (TpConnection *connection,
130 DEBUG ("Error: %s", error->message);
135 tp_contact_factory_set_location_cb (TpConnection *tp_conn,
138 GObject *weak_object)
141 DEBUG ("Error setting location: %s", error->message);
146 tp_contact_factory_set_avatar_cb (TpConnection *connection,
153 DEBUG ("Error: %s", error->message);
158 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
164 DEBUG ("Error: %s", error->message);
169 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
172 const GArray *avatar_data,
173 const gchar *mime_type,
177 EmpathyContact *contact;
179 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
185 DEBUG ("Avatar retrieved for contact %s (%d)",
186 empathy_contact_get_id (contact),
189 empathy_contact_load_avatar_data (contact,
190 (guchar *) avatar_data->data,
197 tp_contact_factory_request_avatars_cb (TpConnection *connection,
203 DEBUG ("Error: %s", error->message);
208 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
212 EmpathyContact *contact;
213 EmpathyAvatar *avatar;
215 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
220 /* Check if we have an avatar */
221 if (EMP_STR_EMPTY (token)) {
222 empathy_contact_set_avatar (contact, NULL);
226 /* Check if the avatar changed */
227 avatar = empathy_contact_get_avatar (contact);
228 if (avatar && !tp_strdiff (avatar->token, token)) {
232 /* The avatar changed, search the new one in the cache */
233 if (empathy_contact_load_avatar_cache (contact, token)) {
234 /* Got from cache, use it */
238 /* Avatar is not up-to-date, we have to request it. */
243 tp_contact_factory_got_known_avatar_tokens (TpConnection *connection,
247 GObject *weak_object)
249 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
250 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
256 DEBUG ("Error: %s", error->message);
260 handles = g_array_new (FALSE, FALSE, sizeof (guint));
262 g_hash_table_iter_init (&iter, tokens);
263 while (g_hash_table_iter_next (&iter, &key, &value)) {
264 guint handle = GPOINTER_TO_UINT (key);
265 const gchar *token = value;
267 if (!tp_contact_factory_avatar_maybe_update (tp_factory,
269 g_array_append_val (handles, handle);
273 DEBUG ("Got %d tokens, need to request %d avatars",
274 g_hash_table_size (tokens), handles->len);
276 /* Request needed avatars */
277 if (handles->len > 0) {
278 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
281 tp_contact_factory_request_avatars_cb,
283 G_OBJECT (tp_factory));
286 g_array_free (handles, TRUE);
290 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
292 const gchar *new_token,
298 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
299 handle, new_token)) {
300 /* Avatar was cached, nothing to do */
304 DEBUG ("Need to request avatar for token %s", new_token);
306 handles = g_array_new (FALSE, FALSE, sizeof (guint));
307 g_array_append_val (handles, handle);
309 tp_cli_connection_interface_avatars_call_request_avatars (connection,
312 tp_contact_factory_request_avatars_cb,
315 g_array_free (handles, TRUE);
319 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
321 const gchar *channel_type,
325 EmpathyContact *contact;
326 EmpathyCapabilities capabilities;
328 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
333 capabilities = empathy_contact_get_capabilities (contact);
334 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
336 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
337 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
338 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
339 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
340 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
342 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
343 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
347 DEBUG ("Changing capabilities for contact %s (%d) to %d",
348 empathy_contact_get_id (contact),
349 empathy_contact_get_handle (contact),
352 empathy_contact_set_capabilities (contact, capabilities);
356 tp_contact_factory_got_capabilities (TpConnection *connection,
357 const GPtrArray *capabilities,
360 GObject *weak_object)
362 EmpathyTpContactFactory *tp_factory;
365 tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
368 DEBUG ("Error: %s", error->message);
369 /* FIXME Should set the capabilities of the contacts for which this request
370 * originated to NONE */
374 for (i = 0; i < capabilities->len; i++) {
377 const gchar *channel_type;
381 values = g_ptr_array_index (capabilities, i);
382 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
383 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
384 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
385 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
387 tp_contact_factory_update_capabilities (tp_factory,
396 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
397 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
399 /* This callback is called by geoclue when it found a position
400 * for the given address. A position is necessary for a contact
401 * to show up on the map
404 geocode_cb (GeoclueGeocode *geocode,
405 GeocluePositionFields fields,
409 GeoclueAccuracy *accuracy,
414 GHashTable *location;
416 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
419 DEBUG ("Error geocoding location : %s", error->message);
420 g_object_unref (geocode);
421 g_object_unref (contact);
425 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
426 new_value = tp_g_value_slice_new_double (latitude);
427 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
429 DEBUG ("\t - Latitude: %f", latitude);
431 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
432 new_value = tp_g_value_slice_new_double (longitude);
433 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
435 DEBUG ("\t - Longitude: %f", longitude);
437 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
438 new_value = tp_g_value_slice_new_double (altitude);
439 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
441 DEBUG ("\t - Altitude: %f", altitude);
444 /* Don't change the accuracy as we used an address to get this position */
445 g_object_notify (contact, "location");
446 g_object_unref (geocode);
447 g_object_unref (contact);
453 get_dup_string (GHashTable *location,
458 value = g_hash_table_lookup (location, key);
460 return g_value_dup_string (value);
467 tp_contact_factory_geocode (EmpathyContact *contact)
470 static GeoclueGeocode *geocode;
474 GHashTable *location;
476 location = empathy_contact_get_location (contact);
477 if (location == NULL)
480 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
484 if (geocode == NULL) {
485 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
486 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
489 g_object_ref (geocode);
491 address = geoclue_address_details_new ();
493 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
495 g_hash_table_insert (address,
496 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
497 DEBUG ("\t - countrycode: %s", str);
500 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
502 g_hash_table_insert (address,
503 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
504 DEBUG ("\t - country: %s", str);
507 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
509 g_hash_table_insert (address,
510 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
511 DEBUG ("\t - postalcode: %s", str);
514 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
516 g_hash_table_insert (address,
517 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
518 DEBUG ("\t - region: %s", str);
521 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
523 g_hash_table_insert (address,
524 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
525 DEBUG ("\t - locality: %s", str);
528 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
530 g_hash_table_insert (address,
531 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
532 DEBUG ("\t - street: %s", str);
535 g_object_ref (contact);
536 geoclue_geocode_address_to_position_async (geocode, address,
537 geocode_cb, contact);
539 g_hash_table_unref (address);
544 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
546 GHashTable *location)
548 EmpathyContact *contact;
549 GHashTable *new_location;
551 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
556 new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
557 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
558 tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
559 (GBoxedCopyFunc) tp_g_value_slice_dup);
560 empathy_contact_set_location (contact, new_location);
561 g_hash_table_unref (new_location);
563 tp_contact_factory_geocode (contact);
567 tp_contact_factory_got_locations (TpConnection *tp_conn,
568 GHashTable *locations,
571 GObject *weak_object)
575 EmpathyTpContactFactory *tp_factory;
577 tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
579 DEBUG ("Error: %s", error->message);
583 g_hash_table_iter_init (&iter, locations);
584 while (g_hash_table_iter_next (&iter, &key, &value)) {
585 guint handle = GPOINTER_TO_INT (key);
586 GHashTable *location = value;
588 tp_contact_factory_update_location (tp_factory, handle, location);
593 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
594 const GPtrArray *capabilities,
596 GObject *weak_object)
598 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
601 for (i = 0; i < capabilities->len; i++) {
604 const gchar *channel_type;
608 values = g_ptr_array_index (capabilities, i);
609 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
610 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
611 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
612 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
614 tp_contact_factory_update_capabilities (tp_factory,
623 tp_contact_factory_location_updated_cb (TpConnection *tp_conn,
625 GHashTable *location,
627 GObject *weak_object)
629 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
630 tp_contact_factory_update_location (tp_factory, handle, location);
634 get_requestable_channel_classes_cb (TpProxy *connection,
638 GObject *weak_object)
640 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
641 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
647 DEBUG ("Error: %s", error->message);
651 classes = g_value_get_boxed (value);
652 for (i = 0; i < classes->len; i++) {
653 GValueArray *class_struct;
654 GHashTable *fixed_prop;
655 GValue *chan_type, *handle_type;
657 class_struct = g_ptr_array_index (classes, i);
658 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
660 handle_type = g_hash_table_lookup (fixed_prop,
661 TP_IFACE_CHANNEL ".TargetHandleType");
662 if (handle_type == NULL ||
663 g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT)
666 chan_type = g_hash_table_lookup (fixed_prop,
667 TP_IFACE_CHANNEL ".ChannelType");
668 if (chan_type == NULL)
671 if (!tp_strdiff (g_value_get_string (chan_type),
672 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
673 priv->can_request_ft = TRUE;
674 else if (!tp_strdiff (g_value_get_string (chan_type),
675 TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
676 priv->can_request_st = TRUE;
679 if (!priv->can_request_ft && !priv->can_request_st)
682 /* Update the capabilities of all contacts */
683 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
684 EmpathyContact *contact = l->data;
685 EmpathyCapabilities caps;
687 caps = empathy_contact_get_capabilities (contact);
689 if (!priv->contact_caps_supported) {
690 /* ContactCapabilities is not supported; assume all contacts can do file
691 * transfer if it's implemented in the CM */
692 if (priv->can_request_ft)
693 caps |= EMPATHY_CAPABILITIES_FT;
695 if (priv->can_request_st)
696 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
699 empathy_contact_set_capabilities (contact, caps);
704 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
705 const gchar **mime_types,
715 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
718 DEBUG ("Failed to get avatar requirements: %s", error->message);
719 /* We'll just leave avatar_mime_types as NULL; the
720 * avatar-setting code can use this as a signal that you can't
724 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
725 priv->avatar_min_width = min_width;
726 priv->avatar_min_height = min_height;
727 priv->avatar_max_width = max_width;
728 priv->avatar_max_height = max_height;
729 priv->avatar_max_size = max_size;
734 update_contact_capabilities (EmpathyTpContactFactory *self,
740 g_hash_table_iter_init (&iter, caps);
741 while (g_hash_table_iter_next (&iter, &key, &value)) {
742 TpHandle handle = GPOINTER_TO_UINT (key);
743 GPtrArray *classes = value;
745 EmpathyContact *contact;
746 EmpathyCapabilities capabilities;
748 contact = tp_contact_factory_find_by_handle (self, handle);
752 capabilities = empathy_contact_get_capabilities (contact);
753 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
755 for (i = 0; i < classes->len; i++) {
756 GValueArray *class_struct;
757 GHashTable *fixed_prop;
758 TpHandleType handle_type;
759 const gchar *chan_type;
761 class_struct = g_ptr_array_index (classes, i);
762 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
764 handle_type = tp_asv_get_uint32 (fixed_prop,
765 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
766 if (handle_type != TP_HANDLE_TYPE_CONTACT)
769 chan_type = tp_asv_get_string (fixed_prop,
770 TP_IFACE_CHANNEL ".ChannelType");
772 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
773 capabilities |= EMPATHY_CAPABILITIES_FT;
775 if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
776 capabilities |= EMPATHY_CAPABILITIES_STREAM_TUBE;
779 DEBUG ("Changing capabilities for contact %s (%d) to %d",
780 empathy_contact_get_id (contact),
781 empathy_contact_get_handle (contact),
784 empathy_contact_set_capabilities (contact, capabilities);
789 tp_contact_factory_got_contact_capabilities (TpConnection *connection,
793 GObject *weak_object)
795 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
798 DEBUG ("Error: %s", error->message);
802 update_contact_capabilities (self, caps);
806 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
807 EmpathyContact *contact)
809 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
810 TpHandle self_handle;
812 GArray handles = {(gchar *) &handle, 1};
813 EmpathyCapabilities caps;
815 /* Keep a weak ref to that contact */
816 g_object_weak_ref (G_OBJECT (contact),
817 tp_contact_factory_weak_notify,
819 priv->contacts = g_list_prepend (priv->contacts, contact);
821 /* The contact keeps a ref to its factory */
822 g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
823 g_object_ref (tp_factory),
826 caps = empathy_contact_get_capabilities (contact);
828 /* Set the FT capability */
829 if (!priv->contact_caps_supported) {
830 /* ContactCapabilities is not supported; assume all contacts can do file
831 * transfer if it's implemented in the CM */
832 if (priv->can_request_ft) {
833 caps |= EMPATHY_CAPABILITIES_FT;
836 /* Set the Stream Tube capability */
837 if (priv->can_request_st) {
838 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
842 empathy_contact_set_capabilities (contact, caps);
844 /* Set is-user property. Note that it could still be the handle is
845 * different from the connection's self handle, in the case the handle
846 * comes from a group interface. */
847 self_handle = tp_connection_get_self_handle (priv->connection);
848 handle = empathy_contact_get_handle (contact);
849 empathy_contact_set_is_user (contact, self_handle == handle);
851 /* FIXME: This should be done by TpContact */
852 if (tp_proxy_has_interface_by_id (priv->connection,
853 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS)) {
854 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (
855 priv->connection, -1, &handles,
856 tp_contact_factory_got_known_avatar_tokens, NULL, NULL,
857 G_OBJECT (tp_factory));
860 if (tp_proxy_has_interface_by_id (priv->connection,
861 TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES)) {
862 tp_cli_connection_interface_capabilities_call_get_capabilities (
863 priv->connection, -1, &handles,
864 tp_contact_factory_got_capabilities, NULL, NULL,
865 G_OBJECT (tp_factory));
868 if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
869 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
870 tp_cli_connection_interface_location_call_get_locations (priv->connection,
873 tp_contact_factory_got_locations,
879 if (priv->contact_caps_supported) {
880 tp_cli_connection_interface_contact_capabilities_call_get_contact_capabilities (
881 priv->connection, -1, &handles,
882 tp_contact_factory_got_contact_capabilities, NULL, NULL,
883 G_OBJECT (tp_factory));
886 DEBUG ("Contact added: %s (%d)",
887 empathy_contact_get_id (contact),
888 empathy_contact_get_handle (contact));
892 EmpathyTpContactFactoryContactsByIdCb ids_cb;
893 EmpathyTpContactFactoryContactsByHandleCb handles_cb;
894 EmpathyTpContactFactoryContactCb contact_cb;
898 EmpathyTpContactFactory *tp_factory;
899 GetContactsCb callback;
901 GDestroyNotify destroy;
905 get_contacts_data_free (gpointer user_data)
907 GetContactsData *data = user_data;
910 data->destroy (data->user_data);
912 g_object_unref (data->tp_factory);
914 g_slice_free (GetContactsData, data);
917 static EmpathyContact *
918 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
919 TpContact *tp_contact)
921 EmpathyContact *contact;
923 contact = tp_contact_factory_find_by_tp_contact (tp_factory,
926 if (contact != NULL) {
927 g_object_ref (contact);
929 contact = empathy_contact_new (tp_contact);
930 tp_contact_factory_add_contact (tp_factory, contact);
936 static EmpathyContact **
937 contacts_array_new (EmpathyTpContactFactory *tp_factory,
939 TpContact * const * contacts)
941 EmpathyContact **ret;
944 ret = g_new0 (EmpathyContact *, n_contacts);
945 for (i = 0; i < n_contacts; i++) {
946 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
953 contacts_array_free (guint n_contacts,
954 EmpathyContact **contacts)
958 for (i = 0; i < n_contacts; i++) {
959 g_object_unref (contacts[i]);
965 get_contacts_by_id_cb (TpConnection *connection,
967 TpContact * const *contacts,
968 const gchar * const *requested_ids,
969 GHashTable *failed_id_errors,
972 GObject *weak_object)
974 GetContactsData *data = user_data;
975 EmpathyContact **empathy_contacts;
977 empathy_contacts = contacts_array_new (data->tp_factory,
978 n_contacts, contacts);
979 if (data->callback.ids_cb) {
980 data->callback.ids_cb (data->tp_factory,
981 n_contacts, empathy_contacts,
985 data->user_data, weak_object);
988 contacts_array_free (n_contacts, empathy_contacts);
991 /* The callback is NOT given a reference to the EmpathyContact objects */
993 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
995 const gchar * const *ids,
996 EmpathyTpContactFactoryContactsByIdCb callback,
998 GDestroyNotify destroy,
999 GObject *weak_object)
1001 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1002 GetContactsData *data;
1004 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1005 g_return_if_fail (ids != NULL);
1007 data = g_slice_new (GetContactsData);
1008 data->callback.ids_cb = callback;
1009 data->user_data = user_data;
1010 data->destroy = destroy;
1011 data->tp_factory = g_object_ref (tp_factory);
1012 tp_connection_get_contacts_by_id (priv->connection,
1014 G_N_ELEMENTS (contact_features),
1016 get_contacts_by_id_cb,
1018 (GDestroyNotify) get_contacts_data_free,
1023 get_contact_by_id_cb (TpConnection *connection,
1025 TpContact * const *contacts,
1026 const gchar * const *requested_ids,
1027 GHashTable *failed_id_errors,
1028 const GError *error,
1030 GObject *weak_object)
1032 GetContactsData *data = user_data;
1033 EmpathyContact *contact = NULL;
1035 if (n_contacts == 1) {
1036 contact = dup_contact_for_tp_contact (data->tp_factory,
1039 else if (error == NULL) {
1040 GHashTableIter iter;
1043 g_hash_table_iter_init (&iter, failed_id_errors);
1044 while (g_hash_table_iter_next (&iter, NULL, &value)) {
1052 if (data->callback.contact_cb) {
1053 data->callback.contact_cb (data->tp_factory,
1056 data->user_data, weak_object);
1059 if (contact != NULL)
1060 g_object_unref (contact);
1063 /* The callback is NOT given a reference to the EmpathyContact objects */
1065 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
1067 EmpathyTpContactFactoryContactCb callback,
1069 GDestroyNotify destroy,
1070 GObject *weak_object)
1072 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1073 GetContactsData *data;
1075 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1076 g_return_if_fail (id != NULL);
1078 data = g_slice_new (GetContactsData);
1079 data->callback.contact_cb = callback;
1080 data->user_data = user_data;
1081 data->destroy = destroy;
1082 data->tp_factory = g_object_ref (tp_factory);
1083 tp_connection_get_contacts_by_id (priv->connection,
1085 G_N_ELEMENTS (contact_features),
1087 get_contact_by_id_cb,
1089 (GDestroyNotify) get_contacts_data_free,
1094 get_contacts_by_handle_cb (TpConnection *connection,
1096 TpContact * const *contacts,
1098 const TpHandle *failed,
1099 const GError *error,
1101 GObject *weak_object)
1103 GetContactsData *data = user_data;
1104 EmpathyContact **empathy_contacts;
1106 empathy_contacts = contacts_array_new (data->tp_factory,
1107 n_contacts, contacts);
1108 if (data->callback.handles_cb) {
1109 data->callback.handles_cb (data->tp_factory,
1110 n_contacts, empathy_contacts,
1113 data->user_data, weak_object);
1116 contacts_array_free (n_contacts, empathy_contacts);
1119 /* The callback is NOT given a reference to the EmpathyContact objects */
1121 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1123 const TpHandle *handles,
1124 EmpathyTpContactFactoryContactsByHandleCb callback,
1126 GDestroyNotify destroy,
1127 GObject *weak_object)
1129 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1130 GetContactsData *data;
1132 if (n_handles == 0) {
1133 callback (tp_factory, 0, NULL, 0, NULL, NULL, user_data, weak_object);
1137 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1138 g_return_if_fail (handles != NULL);
1140 data = g_slice_new (GetContactsData);
1141 data->callback.handles_cb = callback;
1142 data->user_data = user_data;
1143 data->destroy = destroy;
1144 data->tp_factory = g_object_ref (tp_factory);
1145 tp_connection_get_contacts_by_handle (priv->connection,
1147 G_N_ELEMENTS (contact_features),
1149 get_contacts_by_handle_cb,
1151 (GDestroyNotify) get_contacts_data_free,
1155 /* The callback is NOT given a reference to the EmpathyContact objects */
1157 get_contact_by_handle_cb (TpConnection *connection,
1159 TpContact * const *contacts,
1161 const TpHandle *failed,
1162 const GError *error,
1164 GObject *weak_object)
1166 GetContactsData *data = user_data;
1167 EmpathyContact *contact = NULL;
1170 if (n_contacts == 1) {
1171 contact = dup_contact_for_tp_contact (data->tp_factory,
1175 if (error == NULL) {
1176 /* tp-glib will provide an error only if the whole operation failed,
1177 * but not if, for example, the handle was invalid. We create an error
1178 * so the caller of empathy_tp_contact_factory_get_from_handle can
1179 * rely on the error to check if the operation succeeded or not. */
1181 err = g_error_new_literal (TP_ERRORS, TP_ERROR_INVALID_HANDLE,
1182 "handle is invalid");
1185 err = g_error_copy (error);
1189 if (data->callback.contact_cb) {
1190 data->callback.contact_cb (data->tp_factory,
1193 data->user_data, weak_object);
1196 g_clear_error (&err);
1197 if (contact != NULL)
1198 g_object_unref (contact);
1202 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1204 EmpathyTpContactFactoryContactCb callback,
1206 GDestroyNotify destroy,
1207 GObject *weak_object)
1209 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1210 GetContactsData *data;
1212 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1214 data = g_slice_new (GetContactsData);
1215 data->callback.contact_cb = callback;
1216 data->user_data = user_data;
1217 data->destroy = destroy;
1218 data->tp_factory = g_object_ref (tp_factory);
1219 tp_connection_get_contacts_by_handle (priv->connection,
1221 G_N_ELEMENTS (contact_features),
1223 get_contact_by_handle_cb,
1225 (GDestroyNotify) get_contacts_data_free,
1230 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1231 EmpathyContact *contact,
1234 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1235 GHashTable *new_alias;
1238 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1239 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1241 handle = empathy_contact_get_handle (contact);
1243 DEBUG ("Setting alias for contact %s (%d) to %s",
1244 empathy_contact_get_id (contact),
1247 new_alias = g_hash_table_new_full (g_direct_hash,
1252 g_hash_table_insert (new_alias,
1253 GUINT_TO_POINTER (handle),
1256 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1259 tp_contact_factory_set_aliases_cb,
1261 G_OBJECT (tp_factory));
1263 g_hash_table_destroy (new_alias);
1267 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1270 const gchar *mime_type)
1272 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1274 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1276 if (data && size > 0 && size < G_MAXUINT) {
1279 avatar.data = (gchar *) data;
1282 DEBUG ("Setting avatar on connection %s",
1283 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1285 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1289 tp_contact_factory_set_avatar_cb,
1291 G_OBJECT (tp_factory));
1293 DEBUG ("Clearing avatar on connection %s",
1294 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1296 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1298 tp_contact_factory_clear_avatar_cb,
1300 G_OBJECT (tp_factory));
1305 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1306 GHashTable *location)
1308 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1310 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1312 DEBUG ("Setting location");
1314 tp_cli_connection_interface_location_call_set_location (priv->connection,
1317 tp_contact_factory_set_location_cb,
1319 G_OBJECT (tp_factory));
1323 tp_contact_factory_get_property (GObject *object,
1328 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1331 case PROP_CONNECTION:
1332 g_value_set_object (value, priv->connection);
1334 case PROP_MIME_TYPES:
1335 g_value_set_boxed (value, priv->avatar_mime_types);
1337 case PROP_MIN_WIDTH:
1338 g_value_set_uint (value, priv->avatar_min_width);
1340 case PROP_MIN_HEIGHT:
1341 g_value_set_uint (value, priv->avatar_min_height);
1343 case PROP_MAX_WIDTH:
1344 g_value_set_uint (value, priv->avatar_max_width);
1346 case PROP_MAX_HEIGHT:
1347 g_value_set_uint (value, priv->avatar_max_height);
1350 g_value_set_uint (value, priv->avatar_max_size);
1353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1359 tp_contact_factory_set_property (GObject *object,
1361 const GValue *value,
1364 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1367 case PROP_CONNECTION:
1368 priv->connection = g_value_dup_object (value);
1371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1377 tp_contact_factory_finalize (GObject *object)
1379 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1382 DEBUG ("Finalized: %p", object);
1384 for (l = priv->contacts; l; l = l->next) {
1385 g_object_weak_unref (G_OBJECT (l->data),
1386 tp_contact_factory_weak_notify,
1390 g_list_free (priv->contacts);
1392 g_object_unref (priv->connection);
1394 g_strfreev (priv->avatar_mime_types);
1396 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1400 tp_contact_factory_contact_capabilities_changed_cb (TpConnection *connection,
1403 GObject *weak_object)
1405 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
1407 update_contact_capabilities (self, caps);
1411 connection_ready_cb (TpConnection *connection,
1412 const GError *error,
1415 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
1416 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1421 /* FIXME: This should be moved to TpContact */
1422 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1423 tp_contact_factory_avatar_updated_cb,
1425 G_OBJECT (tp_factory),
1427 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1428 tp_contact_factory_avatar_retrieved_cb,
1430 G_OBJECT (tp_factory),
1432 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1433 tp_contact_factory_capabilities_changed_cb,
1435 G_OBJECT (tp_factory),
1439 tp_cli_connection_interface_location_connect_to_location_updated (priv->connection,
1440 tp_contact_factory_location_updated_cb,
1442 G_OBJECT (tp_factory),
1445 if (tp_proxy_has_interface_by_id (connection,
1446 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_CAPABILITIES)) {
1447 priv->contact_caps_supported = TRUE;
1449 tp_cli_connection_interface_contact_capabilities_connect_to_contact_capabilities_changed (
1450 priv->connection, tp_contact_factory_contact_capabilities_changed_cb,
1451 NULL, NULL, G_OBJECT (tp_factory), NULL);
1454 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1456 tp_contact_factory_got_avatar_requirements_cb,
1458 G_OBJECT (tp_factory));
1460 tp_cli_dbus_properties_call_get (priv->connection, -1,
1461 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1462 "RequestableChannelClasses",
1463 get_requestable_channel_classes_cb, NULL, NULL,
1464 G_OBJECT (tp_factory));
1467 g_object_unref (tp_factory);
1471 tp_contact_factory_constructor (GType type,
1473 GObjectConstructParam *props)
1475 GObject *tp_factory;
1476 EmpathyTpContactFactoryPriv *priv;
1478 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1479 priv = GET_PRIV (tp_factory);
1481 /* Ensure to keep the self object alive while the call_when_ready is
1483 g_object_ref (tp_factory);
1484 tp_connection_call_when_ready (priv->connection, connection_ready_cb,
1491 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1493 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1495 object_class->finalize = tp_contact_factory_finalize;
1496 object_class->constructor = tp_contact_factory_constructor;
1497 object_class->get_property = tp_contact_factory_get_property;
1498 object_class->set_property = tp_contact_factory_set_property;
1500 g_object_class_install_property (object_class,
1502 g_param_spec_object ("connection",
1503 "Factory's Connection",
1504 "The connection associated with the factory",
1507 G_PARAM_CONSTRUCT_ONLY |
1508 G_PARAM_STATIC_STRINGS));
1509 g_object_class_install_property (object_class,
1511 g_param_spec_boxed ("avatar-mime-types",
1512 "Supported MIME types for avatars",
1513 "Types of images that may be set as "
1514 "avatars on this connection.",
1517 G_PARAM_STATIC_STRINGS));
1518 g_object_class_install_property (object_class,
1520 g_param_spec_uint ("avatar-min-width",
1521 "Minimum width for avatars",
1522 "Minimum width of avatar that may be set.",
1527 G_PARAM_STATIC_STRINGS));
1528 g_object_class_install_property (object_class,
1530 g_param_spec_uint ("avatar-min-height",
1531 "Minimum height for avatars",
1532 "Minimum height of avatar that may be set.",
1537 G_PARAM_STATIC_STRINGS));
1538 g_object_class_install_property (object_class,
1540 g_param_spec_uint ("avatar-max-width",
1541 "Maximum width for avatars",
1542 "Maximum width of avatar that may be set "
1543 "or 0 if there is no maximum.",
1548 G_PARAM_STATIC_STRINGS));
1549 g_object_class_install_property (object_class,
1551 g_param_spec_uint ("avatar-max-height",
1552 "Maximum height for avatars",
1553 "Maximum height of avatar that may be set "
1554 "or 0 if there is no maximum.",
1559 G_PARAM_STATIC_STRINGS));
1560 g_object_class_install_property (object_class,
1562 g_param_spec_uint ("avatar-max-size",
1563 "Maximum size for avatars in bytes",
1564 "Maximum file size of avatar that may be "
1565 "set or 0 if there is no maximum.",
1570 G_PARAM_STATIC_STRINGS));
1573 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1577 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1579 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1580 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1582 tp_factory->priv = priv;
1583 priv->can_request_ft = FALSE;
1584 priv->can_request_st = FALSE;
1585 priv->contact_caps_supported = FALSE;
1588 static GHashTable *factories = NULL;
1591 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1597 DEBUG ("Message: %s", message);
1598 g_hash_table_remove (factories, connection);
1602 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1603 GObject *where_the_object_was)
1605 g_hash_table_remove (factories, connection);
1609 tp_contact_factory_remove_connection (gpointer connection)
1611 g_signal_handlers_disconnect_by_func (connection,
1612 tp_contact_factory_connection_invalidated_cb, NULL);
1613 g_object_unref (connection);
1616 EmpathyTpContactFactory *
1617 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1619 EmpathyTpContactFactory *tp_factory;
1621 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1623 if (factories == NULL) {
1624 factories = g_hash_table_new_full (empathy_proxy_hash,
1625 empathy_proxy_equal,
1626 tp_contact_factory_remove_connection,
1630 tp_factory = g_hash_table_lookup (factories, connection);
1631 if (tp_factory == NULL) {
1632 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1633 "connection", connection,
1635 g_hash_table_insert (factories, g_object_ref (connection),
1637 g_object_weak_ref (G_OBJECT (tp_factory),
1638 tp_contact_factory_connection_weak_notify_cb,
1640 g_signal_connect (connection, "invalidated",
1641 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1644 g_object_ref (tp_factory);