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 } EmpathyTpContactFactoryPriv;
59 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
73 static TpContactFeature contact_features[] = {
74 TP_CONTACT_FEATURE_ALIAS,
75 TP_CONTACT_FEATURE_PRESENCE,
78 static EmpathyContact *
79 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
82 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
85 for (l = priv->contacts; l; l = l->next) {
86 if (empathy_contact_get_handle (l->data) == handle) {
94 static EmpathyContact *
95 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
96 TpContact *tp_contact)
98 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
101 for (l = priv->contacts; l; l = l->next) {
102 if (empathy_contact_get_tp_contact (l->data) == tp_contact) {
111 tp_contact_factory_weak_notify (gpointer data,
112 GObject *where_the_object_was)
114 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
116 DEBUG ("Remove finalized contact %p", where_the_object_was);
118 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
122 tp_contact_factory_set_aliases_cb (TpConnection *connection,
128 DEBUG ("Error: %s", error->message);
133 tp_contact_factory_set_location_cb (TpConnection *tp_conn,
136 GObject *weak_object)
139 DEBUG ("Error setting location: %s", error->message);
144 tp_contact_factory_set_avatar_cb (TpConnection *connection,
151 DEBUG ("Error: %s", error->message);
156 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
162 DEBUG ("Error: %s", error->message);
167 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
170 const GArray *avatar_data,
171 const gchar *mime_type,
175 EmpathyContact *contact;
177 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
183 DEBUG ("Avatar retrieved for contact %s (%d)",
184 empathy_contact_get_id (contact),
187 empathy_contact_load_avatar_data (contact,
188 (guchar *) avatar_data->data,
195 tp_contact_factory_request_avatars_cb (TpConnection *connection,
201 DEBUG ("Error: %s", error->message);
206 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
210 EmpathyContact *contact;
211 EmpathyAvatar *avatar;
213 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
218 /* Check if we have an avatar */
219 if (EMP_STR_EMPTY (token)) {
220 empathy_contact_set_avatar (contact, NULL);
224 /* Check if the avatar changed */
225 avatar = empathy_contact_get_avatar (contact);
226 if (avatar && !tp_strdiff (avatar->token, token)) {
230 /* The avatar changed, search the new one in the cache */
231 if (empathy_contact_load_avatar_cache (contact, token)) {
232 /* Got from cache, use it */
236 /* Avatar is not up-to-date, we have to request it. */
241 tp_contact_factory_got_known_avatar_tokens (TpConnection *connection,
245 GObject *weak_object)
247 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
248 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
254 DEBUG ("Error: %s", error->message);
258 handles = g_array_new (FALSE, FALSE, sizeof (guint));
260 g_hash_table_iter_init (&iter, tokens);
261 while (g_hash_table_iter_next (&iter, &key, &value)) {
262 guint handle = GPOINTER_TO_UINT (key);
263 const gchar *token = value;
265 if (!tp_contact_factory_avatar_maybe_update (tp_factory,
267 g_array_append_val (handles, handle);
271 DEBUG ("Got %d tokens, need to request %d avatars",
272 g_hash_table_size (tokens), handles->len);
274 /* Request needed avatars */
275 if (handles->len > 0) {
276 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
279 tp_contact_factory_request_avatars_cb,
281 G_OBJECT (tp_factory));
284 g_array_free (handles, TRUE);
288 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
290 const gchar *new_token,
296 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
297 handle, new_token)) {
298 /* Avatar was cached, nothing to do */
302 DEBUG ("Need to request avatar for token %s", new_token);
304 handles = g_array_new (FALSE, FALSE, sizeof (guint));
305 g_array_append_val (handles, handle);
307 tp_cli_connection_interface_avatars_call_request_avatars (connection,
310 tp_contact_factory_request_avatars_cb,
313 g_array_free (handles, TRUE);
317 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
319 const gchar *channel_type,
323 EmpathyContact *contact;
324 EmpathyCapabilities capabilities;
326 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
331 capabilities = empathy_contact_get_capabilities (contact);
332 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
334 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
335 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
336 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
337 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
338 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
340 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
341 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
345 DEBUG ("Changing capabilities for contact %s (%d) to %d",
346 empathy_contact_get_id (contact),
347 empathy_contact_get_handle (contact),
350 empathy_contact_set_capabilities (contact, capabilities);
354 tp_contact_factory_got_capabilities (TpConnection *connection,
355 const GPtrArray *capabilities,
358 GObject *weak_object)
360 EmpathyTpContactFactory *tp_factory;
363 tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
366 DEBUG ("Error: %s", error->message);
367 /* FIXME Should set the capabilities of the contacts for which this request
368 * originated to NONE */
372 for (i = 0; i < capabilities->len; i++) {
375 const gchar *channel_type;
379 values = g_ptr_array_index (capabilities, i);
380 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
381 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
382 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
383 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
385 tp_contact_factory_update_capabilities (tp_factory,
394 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
395 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
397 /* This callback is called by geoclue when it found a position
398 * for the given address. A position is necessary for a contact
399 * to show up on the map
402 geocode_cb (GeoclueGeocode *geocode,
403 GeocluePositionFields fields,
407 GeoclueAccuracy *accuracy,
412 GHashTable *location;
414 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
417 DEBUG ("Error geocoding location : %s", error->message);
418 g_object_unref (geocode);
419 g_object_unref (contact);
423 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
424 new_value = tp_g_value_slice_new_double (latitude);
425 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
427 DEBUG ("\t - Latitude: %f", latitude);
429 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
430 new_value = tp_g_value_slice_new_double (longitude);
431 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
433 DEBUG ("\t - Longitude: %f", longitude);
435 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
436 new_value = tp_g_value_slice_new_double (altitude);
437 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
439 DEBUG ("\t - Altitude: %f", altitude);
442 /* Don't change the accuracy as we used an address to get this position */
443 g_object_notify (contact, "location");
444 g_object_unref (geocode);
445 g_object_unref (contact);
451 get_dup_string (GHashTable *location,
456 value = g_hash_table_lookup (location, key);
458 return g_value_dup_string (value);
465 tp_contact_factory_geocode (EmpathyContact *contact)
468 static GeoclueGeocode *geocode;
472 GHashTable *location;
474 location = empathy_contact_get_location (contact);
475 if (location == NULL)
478 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
482 if (geocode == NULL) {
483 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
484 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
487 g_object_ref (geocode);
489 address = geoclue_address_details_new ();
491 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
493 g_hash_table_insert (address,
494 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
495 DEBUG ("\t - countrycode: %s", str);
498 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
500 g_hash_table_insert (address,
501 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
502 DEBUG ("\t - country: %s", str);
505 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
507 g_hash_table_insert (address,
508 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
509 DEBUG ("\t - postalcode: %s", str);
512 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
514 g_hash_table_insert (address,
515 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
516 DEBUG ("\t - region: %s", str);
519 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
521 g_hash_table_insert (address,
522 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
523 DEBUG ("\t - locality: %s", str);
526 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
528 g_hash_table_insert (address,
529 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
530 DEBUG ("\t - street: %s", str);
533 g_object_ref (contact);
534 geoclue_geocode_address_to_position_async (geocode, address,
535 geocode_cb, contact);
537 g_hash_table_unref (address);
542 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
544 GHashTable *location)
546 EmpathyContact *contact;
547 GHashTable *new_location;
549 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
554 new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
555 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
556 tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
557 (GBoxedCopyFunc) tp_g_value_slice_dup);
558 empathy_contact_set_location (contact, new_location);
559 g_hash_table_unref (new_location);
561 tp_contact_factory_geocode (contact);
565 tp_contact_factory_got_locations (TpConnection *tp_conn,
566 GHashTable *locations,
569 GObject *weak_object)
573 EmpathyTpContactFactory *tp_factory;
575 tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
577 DEBUG ("Error: %s", error->message);
581 g_hash_table_iter_init (&iter, locations);
582 while (g_hash_table_iter_next (&iter, &key, &value)) {
583 guint handle = GPOINTER_TO_INT (key);
584 GHashTable *location = value;
586 tp_contact_factory_update_location (tp_factory, handle, location);
591 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
592 const GPtrArray *capabilities,
594 GObject *weak_object)
596 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
599 for (i = 0; i < capabilities->len; i++) {
602 const gchar *channel_type;
606 values = g_ptr_array_index (capabilities, i);
607 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
608 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
609 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
610 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
612 tp_contact_factory_update_capabilities (tp_factory,
621 tp_contact_factory_location_updated_cb (TpConnection *tp_conn,
623 GHashTable *location,
625 GObject *weak_object)
627 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
628 tp_contact_factory_update_location (tp_factory, handle, location);
632 get_requestable_channel_classes_cb (TpProxy *connection,
636 GObject *weak_object)
638 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
639 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
645 DEBUG ("Error: %s", error->message);
649 classes = g_value_get_boxed (value);
650 for (i = 0; i < classes->len; i++) {
651 GValueArray *class_struct;
652 GHashTable *fixed_prop;
653 GValue *chan_type, *handle_type;
655 class_struct = g_ptr_array_index (classes, i);
656 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
658 handle_type = g_hash_table_lookup (fixed_prop,
659 TP_IFACE_CHANNEL ".TargetHandleType");
660 if (handle_type == NULL ||
661 g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT)
664 chan_type = g_hash_table_lookup (fixed_prop,
665 TP_IFACE_CHANNEL ".ChannelType");
666 if (chan_type == NULL)
669 if (!tp_strdiff (g_value_get_string (chan_type),
670 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
671 priv->can_request_ft = TRUE;
672 else if (!tp_strdiff (g_value_get_string (chan_type),
673 TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
674 priv->can_request_st = TRUE;
677 if (!priv->can_request_ft && !priv->can_request_st)
680 /* Update the capabilities of all contacts */
681 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
682 EmpathyContact *contact = l->data;
683 EmpathyCapabilities caps;
685 caps = empathy_contact_get_capabilities (contact);
687 if (priv->can_request_ft)
688 caps |= EMPATHY_CAPABILITIES_FT;
690 if (priv->can_request_st)
691 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
693 empathy_contact_set_capabilities (contact, caps);
698 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
699 const gchar **mime_types,
709 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
712 DEBUG ("Failed to get avatar requirements: %s", error->message);
713 /* We'll just leave avatar_mime_types as NULL; the
714 * avatar-setting code can use this as a signal that you can't
718 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
719 priv->avatar_min_width = min_width;
720 priv->avatar_min_height = min_height;
721 priv->avatar_max_width = max_width;
722 priv->avatar_max_height = max_height;
723 priv->avatar_max_size = max_size;
728 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
729 EmpathyContact *contact)
731 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
732 TpHandle self_handle;
734 GArray handles = {(gchar *) &handle, 1};
735 EmpathyCapabilities caps;
737 /* Keep a weak ref to that contact */
738 g_object_weak_ref (G_OBJECT (contact),
739 tp_contact_factory_weak_notify,
741 priv->contacts = g_list_prepend (priv->contacts, contact);
743 /* The contact keeps a ref to its factory */
744 g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
745 g_object_ref (tp_factory),
748 caps = empathy_contact_get_capabilities (contact);
750 /* Set the FT capability */
751 if (priv->can_request_ft) {
752 caps |= EMPATHY_CAPABILITIES_FT;
755 /* Set the Stream Tube capability */
756 if (priv->can_request_st) {
757 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
760 empathy_contact_set_capabilities (contact, caps);
762 /* Set is-user property. Note that it could still be the handle is
763 * different from the connection's self handle, in the case the handle
764 * comes from a group interface. */
765 self_handle = tp_connection_get_self_handle (priv->connection);
766 handle = empathy_contact_get_handle (contact);
767 empathy_contact_set_is_user (contact, self_handle == handle);
769 /* FIXME: This should be done by TpContact */
770 if (tp_proxy_has_interface_by_id (priv->connection,
771 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS)) {
772 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (
773 priv->connection, -1, &handles,
774 tp_contact_factory_got_known_avatar_tokens, NULL, NULL,
775 G_OBJECT (tp_factory));
778 if (tp_proxy_has_interface_by_id (priv->connection,
779 TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES)) {
780 tp_cli_connection_interface_capabilities_call_get_capabilities (
781 priv->connection, -1, &handles,
782 tp_contact_factory_got_capabilities, NULL, NULL,
783 G_OBJECT (tp_factory));
786 if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
787 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
788 tp_cli_connection_interface_location_call_get_locations (priv->connection,
791 tp_contact_factory_got_locations,
797 DEBUG ("Contact added: %s (%d)",
798 empathy_contact_get_id (contact),
799 empathy_contact_get_handle (contact));
803 EmpathyTpContactFactoryContactsByIdCb ids_cb;
804 EmpathyTpContactFactoryContactsByHandleCb handles_cb;
805 EmpathyTpContactFactoryContactCb contact_cb;
809 EmpathyTpContactFactory *tp_factory;
810 GetContactsCb callback;
812 GDestroyNotify destroy;
816 get_contacts_data_free (gpointer user_data)
818 GetContactsData *data = user_data;
821 data->destroy (data->user_data);
823 g_object_unref (data->tp_factory);
825 g_slice_free (GetContactsData, data);
828 static EmpathyContact *
829 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
830 TpContact *tp_contact)
832 EmpathyContact *contact;
834 contact = tp_contact_factory_find_by_tp_contact (tp_factory,
837 if (contact != NULL) {
838 g_object_ref (contact);
840 contact = empathy_contact_new (tp_contact);
841 tp_contact_factory_add_contact (tp_factory, contact);
847 static EmpathyContact **
848 contacts_array_new (EmpathyTpContactFactory *tp_factory,
850 TpContact * const * contacts)
852 EmpathyContact **ret;
855 ret = g_new0 (EmpathyContact *, n_contacts);
856 for (i = 0; i < n_contacts; i++) {
857 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
864 contacts_array_free (guint n_contacts,
865 EmpathyContact **contacts)
869 for (i = 0; i < n_contacts; i++) {
870 g_object_unref (contacts[i]);
876 get_contacts_by_id_cb (TpConnection *connection,
878 TpContact * const *contacts,
879 const gchar * const *requested_ids,
880 GHashTable *failed_id_errors,
883 GObject *weak_object)
885 GetContactsData *data = user_data;
886 EmpathyContact **empathy_contacts;
888 empathy_contacts = contacts_array_new (data->tp_factory,
889 n_contacts, contacts);
890 if (data->callback.ids_cb) {
891 data->callback.ids_cb (data->tp_factory,
892 n_contacts, empathy_contacts,
896 data->user_data, weak_object);
899 contacts_array_free (n_contacts, empathy_contacts);
903 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
905 const gchar * const *ids,
906 EmpathyTpContactFactoryContactsByIdCb callback,
908 GDestroyNotify destroy,
909 GObject *weak_object)
911 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
912 GetContactsData *data;
914 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
915 g_return_if_fail (ids != NULL);
917 data = g_slice_new (GetContactsData);
918 data->callback.ids_cb = callback;
919 data->user_data = user_data;
920 data->destroy = destroy;
921 data->tp_factory = g_object_ref (tp_factory);
922 tp_connection_get_contacts_by_id (priv->connection,
924 G_N_ELEMENTS (contact_features),
926 get_contacts_by_id_cb,
928 (GDestroyNotify) get_contacts_data_free,
933 get_contact_by_id_cb (TpConnection *connection,
935 TpContact * const *contacts,
936 const gchar * const *requested_ids,
937 GHashTable *failed_id_errors,
940 GObject *weak_object)
942 GetContactsData *data = user_data;
943 EmpathyContact *contact = NULL;
945 if (n_contacts == 1) {
946 contact = dup_contact_for_tp_contact (data->tp_factory,
949 else if (error == NULL) {
953 g_hash_table_iter_init (&iter, failed_id_errors);
954 while (g_hash_table_iter_next (&iter, NULL, &value)) {
962 if (data->callback.contact_cb) {
963 data->callback.contact_cb (data->tp_factory,
966 data->user_data, weak_object);
971 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
973 EmpathyTpContactFactoryContactCb callback,
975 GDestroyNotify destroy,
976 GObject *weak_object)
978 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
979 GetContactsData *data;
981 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
982 g_return_if_fail (id != NULL);
984 data = g_slice_new (GetContactsData);
985 data->callback.contact_cb = callback;
986 data->user_data = user_data;
987 data->destroy = destroy;
988 data->tp_factory = g_object_ref (tp_factory);
989 tp_connection_get_contacts_by_id (priv->connection,
991 G_N_ELEMENTS (contact_features),
993 get_contact_by_id_cb,
995 (GDestroyNotify) get_contacts_data_free,
1000 get_contacts_by_handle_cb (TpConnection *connection,
1002 TpContact * const *contacts,
1004 const TpHandle *failed,
1005 const GError *error,
1007 GObject *weak_object)
1009 GetContactsData *data = user_data;
1010 EmpathyContact **empathy_contacts;
1012 empathy_contacts = contacts_array_new (data->tp_factory,
1013 n_contacts, contacts);
1014 if (data->callback.handles_cb) {
1015 data->callback.handles_cb (data->tp_factory,
1016 n_contacts, empathy_contacts,
1019 data->user_data, weak_object);
1022 contacts_array_free (n_contacts, empathy_contacts);
1026 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1028 const TpHandle *handles,
1029 EmpathyTpContactFactoryContactsByHandleCb callback,
1031 GDestroyNotify destroy,
1032 GObject *weak_object)
1034 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1035 GetContactsData *data;
1037 if (n_handles == 0) {
1038 callback (tp_factory, 0, NULL, 0, NULL, NULL, user_data, weak_object);
1042 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1043 g_return_if_fail (handles != NULL);
1045 data = g_slice_new (GetContactsData);
1046 data->callback.handles_cb = callback;
1047 data->user_data = user_data;
1048 data->destroy = destroy;
1049 data->tp_factory = g_object_ref (tp_factory);
1050 tp_connection_get_contacts_by_handle (priv->connection,
1052 G_N_ELEMENTS (contact_features),
1054 get_contacts_by_handle_cb,
1056 (GDestroyNotify) get_contacts_data_free,
1061 get_contact_by_handle_cb (TpConnection *connection,
1063 TpContact * const *contacts,
1065 const TpHandle *failed,
1066 const GError *error,
1068 GObject *weak_object)
1070 GetContactsData *data = user_data;
1071 EmpathyContact *contact = NULL;
1073 if (n_contacts == 1) {
1074 contact = dup_contact_for_tp_contact (data->tp_factory,
1078 if (data->callback.contact_cb) {
1079 data->callback.contact_cb (data->tp_factory,
1082 data->user_data, weak_object);
1087 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1089 EmpathyTpContactFactoryContactCb callback,
1091 GDestroyNotify destroy,
1092 GObject *weak_object)
1094 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1095 GetContactsData *data;
1097 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1099 data = g_slice_new (GetContactsData);
1100 data->callback.contact_cb = callback;
1101 data->user_data = user_data;
1102 data->destroy = destroy;
1103 data->tp_factory = g_object_ref (tp_factory);
1104 tp_connection_get_contacts_by_handle (priv->connection,
1106 G_N_ELEMENTS (contact_features),
1108 get_contact_by_handle_cb,
1110 (GDestroyNotify) get_contacts_data_free,
1115 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1116 EmpathyContact *contact,
1119 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1120 GHashTable *new_alias;
1123 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1124 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1126 handle = empathy_contact_get_handle (contact);
1128 DEBUG ("Setting alias for contact %s (%d) to %s",
1129 empathy_contact_get_id (contact),
1132 new_alias = g_hash_table_new_full (g_direct_hash,
1137 g_hash_table_insert (new_alias,
1138 GUINT_TO_POINTER (handle),
1141 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1144 tp_contact_factory_set_aliases_cb,
1146 G_OBJECT (tp_factory));
1148 g_hash_table_destroy (new_alias);
1152 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1155 const gchar *mime_type)
1157 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1159 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1161 if (data && size > 0 && size < G_MAXUINT) {
1164 avatar.data = (gchar *) data;
1167 DEBUG ("Setting avatar on connection %s",
1168 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1170 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1174 tp_contact_factory_set_avatar_cb,
1176 G_OBJECT (tp_factory));
1178 DEBUG ("Clearing avatar on connection %s",
1179 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1181 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1183 tp_contact_factory_clear_avatar_cb,
1185 G_OBJECT (tp_factory));
1190 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1191 GHashTable *location)
1193 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1195 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1197 DEBUG ("Setting location");
1199 tp_cli_connection_interface_location_call_set_location (priv->connection,
1202 tp_contact_factory_set_location_cb,
1204 G_OBJECT (tp_factory));
1208 tp_contact_factory_get_property (GObject *object,
1213 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1216 case PROP_CONNECTION:
1217 g_value_set_object (value, priv->connection);
1219 case PROP_MIME_TYPES:
1220 g_value_set_boxed (value, priv->avatar_mime_types);
1222 case PROP_MIN_WIDTH:
1223 g_value_set_uint (value, priv->avatar_min_width);
1225 case PROP_MIN_HEIGHT:
1226 g_value_set_uint (value, priv->avatar_min_height);
1228 case PROP_MAX_WIDTH:
1229 g_value_set_uint (value, priv->avatar_max_width);
1231 case PROP_MAX_HEIGHT:
1232 g_value_set_uint (value, priv->avatar_max_height);
1235 g_value_set_uint (value, priv->avatar_max_size);
1238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1244 tp_contact_factory_set_property (GObject *object,
1246 const GValue *value,
1249 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1252 case PROP_CONNECTION:
1253 priv->connection = g_value_dup_object (value);
1256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1262 tp_contact_factory_finalize (GObject *object)
1264 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1267 DEBUG ("Finalized: %p", object);
1269 for (l = priv->contacts; l; l = l->next) {
1270 g_object_weak_unref (G_OBJECT (l->data),
1271 tp_contact_factory_weak_notify,
1275 g_list_free (priv->contacts);
1277 g_object_unref (priv->connection);
1279 g_strfreev (priv->avatar_mime_types);
1281 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1285 tp_contact_factory_constructor (GType type,
1287 GObjectConstructParam *props)
1289 GObject *tp_factory;
1290 EmpathyTpContactFactoryPriv *priv;
1292 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1293 priv = GET_PRIV (tp_factory);
1295 /* FIXME: This should be moved to TpContact */
1296 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1297 tp_contact_factory_avatar_updated_cb,
1301 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1302 tp_contact_factory_avatar_retrieved_cb,
1306 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1307 tp_contact_factory_capabilities_changed_cb,
1313 tp_cli_connection_interface_location_connect_to_location_updated (priv->connection,
1314 tp_contact_factory_location_updated_cb,
1316 G_OBJECT (tp_factory),
1319 /* FIXME: This should be moved to TpConnection */
1320 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1322 tp_contact_factory_got_avatar_requirements_cb,
1325 tp_cli_dbus_properties_call_get (priv->connection, -1,
1326 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1327 "RequestableChannelClasses",
1328 get_requestable_channel_classes_cb, NULL, NULL,
1329 G_OBJECT (tp_factory));
1335 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1337 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1339 object_class->finalize = tp_contact_factory_finalize;
1340 object_class->constructor = tp_contact_factory_constructor;
1341 object_class->get_property = tp_contact_factory_get_property;
1342 object_class->set_property = tp_contact_factory_set_property;
1344 g_object_class_install_property (object_class,
1346 g_param_spec_object ("connection",
1347 "Factory's Connection",
1348 "The connection associated with the factory",
1351 G_PARAM_CONSTRUCT_ONLY |
1352 G_PARAM_STATIC_STRINGS));
1353 g_object_class_install_property (object_class,
1355 g_param_spec_boxed ("avatar-mime-types",
1356 "Supported MIME types for avatars",
1357 "Types of images that may be set as "
1358 "avatars on this connection.",
1361 G_PARAM_STATIC_STRINGS));
1362 g_object_class_install_property (object_class,
1364 g_param_spec_uint ("avatar-min-width",
1365 "Minimum width for avatars",
1366 "Minimum width of avatar that may be set.",
1371 G_PARAM_STATIC_STRINGS));
1372 g_object_class_install_property (object_class,
1374 g_param_spec_uint ("avatar-min-height",
1375 "Minimum height for avatars",
1376 "Minimum height of avatar that may be set.",
1381 G_PARAM_STATIC_STRINGS));
1382 g_object_class_install_property (object_class,
1384 g_param_spec_uint ("avatar-max-width",
1385 "Maximum width for avatars",
1386 "Maximum width of avatar that may be set "
1387 "or 0 if there is no maximum.",
1392 G_PARAM_STATIC_STRINGS));
1393 g_object_class_install_property (object_class,
1395 g_param_spec_uint ("avatar-max-height",
1396 "Maximum height for avatars",
1397 "Maximum height of avatar that may be set "
1398 "or 0 if there is no maximum.",
1403 G_PARAM_STATIC_STRINGS));
1404 g_object_class_install_property (object_class,
1406 g_param_spec_uint ("avatar-max-size",
1407 "Maximum size for avatars in bytes",
1408 "Maximum file size of avatar that may be "
1409 "set or 0 if there is no maximum.",
1414 G_PARAM_STATIC_STRINGS));
1417 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1421 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1423 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1424 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1426 tp_factory->priv = priv;
1427 priv->can_request_ft = FALSE;
1428 priv->can_request_st = FALSE;
1431 static GHashTable *factories = NULL;
1434 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1440 DEBUG ("Message: %s", message);
1441 g_hash_table_remove (factories, connection);
1445 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1446 GObject *where_the_object_was)
1448 g_hash_table_remove (factories, connection);
1452 tp_contact_factory_remove_connection (gpointer connection)
1454 g_signal_handlers_disconnect_by_func (connection,
1455 tp_contact_factory_connection_invalidated_cb, NULL);
1456 g_object_unref (connection);
1459 EmpathyTpContactFactory *
1460 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1462 EmpathyTpContactFactory *tp_factory;
1464 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1466 if (factories == NULL) {
1467 factories = g_hash_table_new_full (empathy_proxy_hash,
1468 empathy_proxy_equal,
1469 tp_contact_factory_remove_connection,
1473 tp_factory = g_hash_table_lookup (factories, connection);
1474 if (tp_factory == NULL) {
1475 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1476 "connection", connection,
1478 g_hash_table_insert (factories, g_object_ref (connection),
1480 g_object_weak_ref (G_OBJECT (tp_factory),
1481 tp_contact_factory_connection_weak_notify_cb,
1483 g_signal_connect (connection, "invalidated",
1484 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1487 g_object_ref (tp_factory);