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>
30 #include <geoclue/geoclue-geocode.h>
33 #include <extensions/extensions.h>
35 #include "empathy-tp-contact-factory.h"
36 #include "empathy-utils.h"
37 #include "empathy-location.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
44 TpConnection *connection;
47 gchar **avatar_mime_types;
48 guint avatar_min_width;
49 guint avatar_min_height;
50 guint avatar_max_width;
51 guint avatar_max_height;
52 guint avatar_max_size;
53 gboolean can_request_ft;
54 gboolean can_request_st;
55 } EmpathyTpContactFactoryPriv;
57 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
71 static TpContactFeature contact_features[] = {
72 TP_CONTACT_FEATURE_ALIAS,
73 TP_CONTACT_FEATURE_PRESENCE,
76 static EmpathyContact *
77 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
80 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
83 for (l = priv->contacts; l; l = l->next) {
84 if (empathy_contact_get_handle (l->data) == handle) {
92 static EmpathyContact *
93 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
94 TpContact *tp_contact)
96 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
99 for (l = priv->contacts; l; l = l->next) {
100 if (empathy_contact_get_tp_contact (l->data) == tp_contact) {
109 tp_contact_factory_weak_notify (gpointer data,
110 GObject *where_the_object_was)
112 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
114 DEBUG ("Remove finalized contact %p", where_the_object_was);
116 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
120 tp_contact_factory_set_aliases_cb (TpConnection *connection,
126 DEBUG ("Error: %s", error->message);
131 tp_contact_factory_set_location_cb (TpProxy *proxy,
134 GObject *weak_object)
137 DEBUG ("Error setting location: %s", error->message);
142 tp_contact_factory_set_avatar_cb (TpConnection *connection,
149 DEBUG ("Error: %s", error->message);
154 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
160 DEBUG ("Error: %s", error->message);
165 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
168 const GArray *avatar_data,
169 const gchar *mime_type,
173 EmpathyContact *contact;
175 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
181 DEBUG ("Avatar retrieved for contact %s (%d)",
182 empathy_contact_get_id (contact),
185 empathy_contact_load_avatar_data (contact,
193 tp_contact_factory_request_avatars_cb (TpConnection *connection,
199 DEBUG ("Error: %s", error->message);
204 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
208 EmpathyContact *contact;
209 EmpathyAvatar *avatar;
211 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
216 /* Check if we have an avatar */
217 if (EMP_STR_EMPTY (token)) {
218 empathy_contact_set_avatar (contact, NULL);
222 /* Check if the avatar changed */
223 avatar = empathy_contact_get_avatar (contact);
224 if (avatar && !tp_strdiff (avatar->token, token)) {
228 /* The avatar changed, search the new one in the cache */
229 if (empathy_contact_load_avatar_cache (contact, token)) {
230 /* Got from cache, use it */
234 /* Avatar is not up-to-date, we have to request it. */
239 EmpathyTpContactFactory *tp_factory;
244 tp_contact_factory_avatar_tokens_foreach (gpointer key,
248 TokensData *data = user_data;
249 const gchar *token = value;
250 guint handle = GPOINTER_TO_UINT (key);
252 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
254 g_array_append_val (data->handles, handle);
259 tp_contact_factory_got_known_avatar_tokens (EmpathyTpContactFactory *tp_factory,
263 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
267 DEBUG ("Error: %s", error->message);
271 data.tp_factory = tp_factory;
272 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
273 g_hash_table_foreach (tokens,
274 tp_contact_factory_avatar_tokens_foreach,
277 DEBUG ("Got %d tokens, need to request %d avatars",
278 g_hash_table_size (tokens), data.handles->len);
280 /* Request needed avatars */
281 if (data.handles->len > 0) {
282 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
285 tp_contact_factory_request_avatars_cb,
287 G_OBJECT (tp_factory));
290 g_array_free (data.handles, TRUE);
291 g_hash_table_destroy (tokens);
295 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
297 const gchar *new_token,
303 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
304 handle, new_token)) {
305 /* Avatar was cached, nothing to do */
309 DEBUG ("Need to request avatar for token %s", new_token);
311 handles = g_array_new (FALSE, FALSE, sizeof (guint));
312 g_array_append_val (handles, handle);
314 tp_cli_connection_interface_avatars_call_request_avatars (connection,
317 tp_contact_factory_request_avatars_cb,
320 g_array_free (handles, TRUE);
324 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
326 const gchar *channel_type,
330 EmpathyContact *contact;
331 EmpathyCapabilities capabilities;
333 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
338 capabilities = empathy_contact_get_capabilities (contact);
339 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
341 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
342 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
343 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
344 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
345 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
347 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
348 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
352 DEBUG ("Changing capabilities for contact %s (%d) to %d",
353 empathy_contact_get_id (contact),
354 empathy_contact_get_handle (contact),
357 empathy_contact_set_capabilities (contact, capabilities);
361 tp_contact_factory_got_capabilities (EmpathyTpContactFactory *tp_factory,
362 GPtrArray *capabilities,
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,
393 g_value_array_free (values);
396 g_ptr_array_free (capabilities, TRUE);
400 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
401 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
403 /* This callback is called by geoclue when it found a position
404 * for the given address. A position is necessary for a contact
405 * to show up on the map
408 geocode_cb (GeoclueGeocode *geocode,
409 GeocluePositionFields fields,
413 GeoclueAccuracy *accuracy,
418 GHashTable *location;
420 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
423 DEBUG ("Error geocoding location : %s", error->message);
424 g_object_unref (geocode);
425 g_object_unref (contact);
429 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
430 new_value = tp_g_value_slice_new_double (latitude);
431 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
433 DEBUG ("\t - Latitude: %f", latitude);
435 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
436 new_value = tp_g_value_slice_new_double (longitude);
437 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
439 DEBUG ("\t - Longitude: %f", longitude);
441 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
442 new_value = tp_g_value_slice_new_double (altitude);
443 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
445 DEBUG ("\t - Altitude: %f", altitude);
448 /* Don't change the accuracy as we used an address to get this position */
449 g_object_notify (contact, "location");
450 g_object_unref (geocode);
451 g_object_unref (contact);
457 get_dup_string (GHashTable *location,
462 value = g_hash_table_lookup (location, key);
464 return g_value_dup_string (value);
471 tp_contact_factory_geocode (EmpathyContact *contact)
474 static GeoclueGeocode *geocode;
478 GHashTable *location;
480 location = empathy_contact_get_location (contact);
481 if (location == NULL)
484 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
488 if (geocode == NULL) {
489 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
490 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
493 g_object_ref (geocode);
495 address = geoclue_address_details_new ();
497 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
499 g_hash_table_insert (address,
500 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
501 DEBUG ("\t - countrycode: %s", str);
504 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
506 g_hash_table_insert (address,
507 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
508 DEBUG ("\t - country: %s", str);
511 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
513 g_hash_table_insert (address,
514 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
515 DEBUG ("\t - postalcode: %s", str);
518 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
520 g_hash_table_insert (address,
521 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
522 DEBUG ("\t - region: %s", str);
525 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
527 g_hash_table_insert (address,
528 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
529 DEBUG ("\t - locality: %s", str);
532 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
534 g_hash_table_insert (address,
535 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
536 DEBUG ("\t - street: %s", str);
539 g_object_ref (contact);
540 geoclue_geocode_address_to_position_async (geocode, address,
541 geocode_cb, contact);
543 g_hash_table_unref (address);
548 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
550 GHashTable *location)
552 EmpathyContact *contact;
553 GHashTable *new_location;
555 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
560 new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
561 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
562 tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
563 (GBoxedCopyFunc) tp_g_value_slice_dup);
564 empathy_contact_set_location (contact, new_location);
565 g_hash_table_unref (new_location);
567 tp_contact_factory_geocode (contact);
571 tp_contact_factory_got_locations (TpProxy *tp_proxy,
572 GHashTable *locations,
575 GObject *weak_object)
579 EmpathyTpContactFactory *tp_factory;
581 tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
583 DEBUG ("Error: %s", error->message);
587 g_hash_table_iter_init (&iter, locations);
588 while (g_hash_table_iter_next (&iter, &key, &value)) {
589 guint handle = GPOINTER_TO_INT (key);
590 GHashTable *location = value;
592 tp_contact_factory_update_location (tp_factory, handle, location);
597 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
598 const GPtrArray *capabilities,
600 GObject *weak_object)
602 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
605 for (i = 0; i < capabilities->len; i++) {
608 const gchar *channel_type;
612 values = g_ptr_array_index (capabilities, i);
613 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
614 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
615 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
616 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
618 tp_contact_factory_update_capabilities (tp_factory,
627 tp_contact_factory_location_updated_cb (TpProxy *proxy,
629 GHashTable *location,
631 GObject *weak_object)
633 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
634 tp_contact_factory_update_location (tp_factory, handle, location);
638 get_requestable_channel_classes_cb (TpProxy *connection,
642 GObject *weak_object)
644 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
645 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
651 DEBUG ("Error: %s", error->message);
655 classes = g_value_get_boxed (value);
656 for (i = 0; i < classes->len; i++) {
657 GValueArray *class_struct;
658 GHashTable *fixed_prop;
659 GValue *chan_type, *handle_type;
661 class_struct = g_ptr_array_index (classes, i);
662 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
664 handle_type = g_hash_table_lookup (fixed_prop,
665 TP_IFACE_CHANNEL ".TargetHandleType");
666 if (handle_type == NULL ||
667 g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT)
670 chan_type = g_hash_table_lookup (fixed_prop,
671 TP_IFACE_CHANNEL ".ChannelType");
672 if (chan_type == NULL)
675 if (!tp_strdiff (g_value_get_string (chan_type),
676 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
677 priv->can_request_ft = TRUE;
678 else if (!tp_strdiff (g_value_get_string (chan_type),
679 TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
680 priv->can_request_st = TRUE;
683 if (!priv->can_request_ft && !priv->can_request_st)
686 /* Update the capabilities of all contacts */
687 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
688 EmpathyContact *contact = l->data;
689 EmpathyCapabilities caps;
691 caps = empathy_contact_get_capabilities (contact);
693 if (priv->can_request_ft)
694 caps |= EMPATHY_CAPABILITIES_FT;
696 if (priv->can_request_st)
697 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 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
735 EmpathyContact *contact)
737 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
738 TpHandle self_handle;
740 GArray handles = {(gchar *) &handle, 1};
742 GPtrArray *capabilities;
743 GError *error = NULL;
744 EmpathyCapabilities caps;
746 /* Keep a weak ref to that contact */
747 g_object_weak_ref (G_OBJECT (contact),
748 tp_contact_factory_weak_notify,
750 priv->contacts = g_list_prepend (priv->contacts, contact);
752 /* The contact keeps a ref to its factory */
753 g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
754 g_object_ref (tp_factory),
757 caps = empathy_contact_get_capabilities (contact);
759 /* Set the FT capability */
760 if (priv->can_request_ft) {
761 caps |= EMPATHY_CAPABILITIES_FT;
764 /* Set the Stream Tube capability */
765 if (priv->can_request_st) {
766 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
769 empathy_contact_set_capabilities (contact, caps);
771 /* Set is-user property. Note that it could still be the handle is
772 * different from the connection's self handle, in the case the handle
773 * comes from a group interface. */
774 self_handle = tp_connection_get_self_handle (priv->connection);
775 handle = empathy_contact_get_handle (contact);
776 empathy_contact_set_is_user (contact, self_handle == handle);
778 /* FIXME: This should be done by TpContact */
779 tp_cli_connection_interface_avatars_run_get_known_avatar_tokens (priv->connection,
785 tp_contact_factory_got_known_avatar_tokens (tp_factory, tokens, error);
786 g_clear_error (&error);
788 tp_cli_connection_interface_capabilities_run_get_capabilities (priv->connection,
794 tp_contact_factory_got_capabilities (tp_factory, capabilities, error);
795 g_clear_error (&error);
797 if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
798 EMP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
799 emp_cli_connection_interface_location_call_get_locations (TP_PROXY (priv->connection),
802 tp_contact_factory_got_locations,
808 DEBUG ("Contact added: %s (%d)",
809 empathy_contact_get_id (contact),
810 empathy_contact_get_handle (contact));
814 EmpathyTpContactFactoryContactsByIdCb ids_cb;
815 EmpathyTpContactFactoryContactsByHandleCb handles_cb;
816 EmpathyTpContactFactoryContactCb contact_cb;
820 EmpathyTpContactFactory *tp_factory;
821 GetContactsCb callback;
823 GDestroyNotify destroy;
827 get_contacts_data_free (gpointer user_data)
829 GetContactsData *data = user_data;
832 data->destroy (data->user_data);
834 g_object_unref (data->tp_factory);
836 g_slice_free (GetContactsData, data);
839 static EmpathyContact *
840 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
841 TpContact *tp_contact)
843 EmpathyContact *contact;
845 contact = tp_contact_factory_find_by_tp_contact (tp_factory,
848 if (contact != NULL) {
849 g_object_ref (contact);
851 contact = empathy_contact_new (tp_contact);
852 tp_contact_factory_add_contact (tp_factory, contact);
858 static EmpathyContact **
859 contacts_array_new (EmpathyTpContactFactory *tp_factory,
861 TpContact * const * contacts)
863 EmpathyContact **ret;
866 ret = g_new0 (EmpathyContact *, n_contacts);
867 for (i = 0; i < n_contacts; i++) {
868 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
875 contacts_array_free (guint n_contacts,
876 EmpathyContact **contacts)
880 for (i = 0; i < n_contacts; i++) {
881 g_object_unref (contacts[i]);
887 get_contacts_by_id_cb (TpConnection *connection,
889 TpContact * const *contacts,
890 const gchar * const *requested_ids,
891 GHashTable *failed_id_errors,
894 GObject *weak_object)
896 GetContactsData *data = user_data;
897 EmpathyContact **empathy_contacts;
899 empathy_contacts = contacts_array_new (data->tp_factory,
900 n_contacts, contacts);
901 if (data->callback.ids_cb) {
902 data->callback.ids_cb (data->tp_factory,
903 n_contacts, empathy_contacts,
907 data->user_data, weak_object);
910 contacts_array_free (n_contacts, empathy_contacts);
914 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
916 const gchar * const *ids,
917 EmpathyTpContactFactoryContactsByIdCb callback,
919 GDestroyNotify destroy,
920 GObject *weak_object)
922 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
923 GetContactsData *data;
925 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
926 g_return_if_fail (ids != NULL);
928 data = g_slice_new (GetContactsData);
929 data->callback.ids_cb = callback;
930 data->user_data = user_data;
931 data->destroy = destroy;
932 data->tp_factory = g_object_ref (tp_factory);
933 tp_connection_get_contacts_by_id (priv->connection,
935 G_N_ELEMENTS (contact_features),
937 get_contacts_by_id_cb,
939 (GDestroyNotify) get_contacts_data_free,
944 get_contact_by_id_cb (TpConnection *connection,
946 TpContact * const *contacts,
947 const gchar * const *requested_ids,
948 GHashTable *failed_id_errors,
951 GObject *weak_object)
953 GetContactsData *data = user_data;
954 EmpathyContact *contact = NULL;
956 if (n_contacts == 1) {
957 contact = dup_contact_for_tp_contact (data->tp_factory,
960 else if (error == NULL) {
964 g_hash_table_iter_init (&iter, failed_id_errors);
965 while (g_hash_table_iter_next (&iter, NULL, &value)) {
973 if (data->callback.contact_cb) {
974 data->callback.contact_cb (data->tp_factory,
977 data->user_data, weak_object);
982 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
984 EmpathyTpContactFactoryContactCb callback,
986 GDestroyNotify destroy,
987 GObject *weak_object)
989 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
990 GetContactsData *data;
992 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
993 g_return_if_fail (id != NULL);
995 data = g_slice_new (GetContactsData);
996 data->callback.contact_cb = callback;
997 data->user_data = user_data;
998 data->destroy = destroy;
999 data->tp_factory = g_object_ref (tp_factory);
1000 tp_connection_get_contacts_by_id (priv->connection,
1002 G_N_ELEMENTS (contact_features),
1004 get_contact_by_id_cb,
1006 (GDestroyNotify) get_contacts_data_free,
1011 get_contacts_by_handle_cb (TpConnection *connection,
1013 TpContact * const *contacts,
1015 const TpHandle *failed,
1016 const GError *error,
1018 GObject *weak_object)
1020 GetContactsData *data = user_data;
1021 EmpathyContact **empathy_contacts;
1023 empathy_contacts = contacts_array_new (data->tp_factory,
1024 n_contacts, contacts);
1025 if (data->callback.handles_cb) {
1026 data->callback.handles_cb (data->tp_factory,
1027 n_contacts, empathy_contacts,
1030 data->user_data, weak_object);
1033 contacts_array_free (n_contacts, empathy_contacts);
1037 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1039 const TpHandle *handles,
1040 EmpathyTpContactFactoryContactsByHandleCb callback,
1042 GDestroyNotify destroy,
1043 GObject *weak_object)
1045 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1046 GetContactsData *data;
1048 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1049 g_return_if_fail (handles != NULL);
1051 data = g_slice_new (GetContactsData);
1052 data->callback.handles_cb = callback;
1053 data->user_data = user_data;
1054 data->destroy = destroy;
1055 data->tp_factory = g_object_ref (tp_factory);
1056 tp_connection_get_contacts_by_handle (priv->connection,
1058 G_N_ELEMENTS (contact_features),
1060 get_contacts_by_handle_cb,
1062 (GDestroyNotify) get_contacts_data_free,
1067 get_contact_by_handle_cb (TpConnection *connection,
1069 TpContact * const *contacts,
1071 const TpHandle *failed,
1072 const GError *error,
1074 GObject *weak_object)
1076 GetContactsData *data = user_data;
1077 EmpathyContact *contact = NULL;
1079 if (n_contacts == 1) {
1080 contact = dup_contact_for_tp_contact (data->tp_factory,
1084 if (data->callback.contact_cb) {
1085 data->callback.contact_cb (data->tp_factory,
1088 data->user_data, weak_object);
1093 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1095 EmpathyTpContactFactoryContactCb callback,
1097 GDestroyNotify destroy,
1098 GObject *weak_object)
1100 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1101 GetContactsData *data;
1103 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1105 data = g_slice_new (GetContactsData);
1106 data->callback.contact_cb = callback;
1107 data->user_data = user_data;
1108 data->destroy = destroy;
1109 data->tp_factory = g_object_ref (tp_factory);
1110 tp_connection_get_contacts_by_handle (priv->connection,
1112 G_N_ELEMENTS (contact_features),
1114 get_contact_by_handle_cb,
1116 (GDestroyNotify) get_contacts_data_free,
1121 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1122 EmpathyContact *contact,
1125 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1126 GHashTable *new_alias;
1129 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1130 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1132 handle = empathy_contact_get_handle (contact);
1134 DEBUG ("Setting alias for contact %s (%d) to %s",
1135 empathy_contact_get_id (contact),
1138 new_alias = g_hash_table_new_full (g_direct_hash,
1143 g_hash_table_insert (new_alias,
1144 GUINT_TO_POINTER (handle),
1147 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1150 tp_contact_factory_set_aliases_cb,
1152 G_OBJECT (tp_factory));
1154 g_hash_table_destroy (new_alias);
1158 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1161 const gchar *mime_type)
1163 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1165 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1167 if (data && size > 0 && size < G_MAXUINT) {
1170 avatar.data = (gchar *) data;
1173 DEBUG ("Setting avatar on connection %s",
1174 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1176 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1180 tp_contact_factory_set_avatar_cb,
1182 G_OBJECT (tp_factory));
1184 DEBUG ("Clearing avatar on connection %s",
1185 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1187 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1189 tp_contact_factory_clear_avatar_cb,
1191 G_OBJECT (tp_factory));
1196 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1197 GHashTable *location)
1199 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1201 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1203 DEBUG ("Setting location");
1205 emp_cli_connection_interface_location_call_set_location (TP_PROXY (priv->connection),
1208 tp_contact_factory_set_location_cb,
1210 G_OBJECT (tp_factory));
1214 tp_contact_factory_get_property (GObject *object,
1219 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1222 case PROP_CONNECTION:
1223 g_value_set_object (value, priv->connection);
1225 case PROP_MIME_TYPES:
1226 g_value_set_boxed (value, priv->avatar_mime_types);
1228 case PROP_MIN_WIDTH:
1229 g_value_set_uint (value, priv->avatar_min_width);
1231 case PROP_MIN_HEIGHT:
1232 g_value_set_uint (value, priv->avatar_min_height);
1234 case PROP_MAX_WIDTH:
1235 g_value_set_uint (value, priv->avatar_max_width);
1237 case PROP_MAX_HEIGHT:
1238 g_value_set_uint (value, priv->avatar_max_height);
1241 g_value_set_uint (value, priv->avatar_max_size);
1244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1250 tp_contact_factory_set_property (GObject *object,
1252 const GValue *value,
1255 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1258 case PROP_CONNECTION:
1259 priv->connection = g_value_dup_object (value);
1262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1268 tp_contact_factory_finalize (GObject *object)
1270 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1273 DEBUG ("Finalized: %p", object);
1275 for (l = priv->contacts; l; l = l->next) {
1276 g_object_weak_unref (G_OBJECT (l->data),
1277 tp_contact_factory_weak_notify,
1281 g_list_free (priv->contacts);
1283 g_object_unref (priv->connection);
1285 g_strfreev (priv->avatar_mime_types);
1287 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1291 tp_contact_factory_constructor (GType type,
1293 GObjectConstructParam *props)
1295 GObject *tp_factory;
1296 EmpathyTpContactFactoryPriv *priv;
1298 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1299 priv = GET_PRIV (tp_factory);
1301 /* FIXME: This should be moved to TpContact */
1302 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1303 tp_contact_factory_avatar_updated_cb,
1307 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1308 tp_contact_factory_avatar_retrieved_cb,
1312 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1313 tp_contact_factory_capabilities_changed_cb,
1319 emp_cli_connection_interface_location_connect_to_location_updated (TP_PROXY (priv->connection),
1320 tp_contact_factory_location_updated_cb,
1322 G_OBJECT (tp_factory),
1325 /* FIXME: This should be moved to TpConnection */
1326 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1328 tp_contact_factory_got_avatar_requirements_cb,
1331 tp_cli_dbus_properties_call_get (priv->connection, -1,
1332 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1333 "RequestableChannelClasses",
1334 get_requestable_channel_classes_cb, NULL, NULL,
1335 G_OBJECT (tp_factory));
1341 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1343 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1345 object_class->finalize = tp_contact_factory_finalize;
1346 object_class->constructor = tp_contact_factory_constructor;
1347 object_class->get_property = tp_contact_factory_get_property;
1348 object_class->set_property = tp_contact_factory_set_property;
1350 g_object_class_install_property (object_class,
1352 g_param_spec_object ("connection",
1353 "Factory's Connection",
1354 "The connection associated with the factory",
1357 G_PARAM_CONSTRUCT_ONLY |
1358 G_PARAM_STATIC_STRINGS));
1359 g_object_class_install_property (object_class,
1361 g_param_spec_boxed ("avatar-mime-types",
1362 "Supported MIME types for avatars",
1363 "Types of images that may be set as "
1364 "avatars on this connection.",
1367 G_PARAM_STATIC_STRINGS));
1368 g_object_class_install_property (object_class,
1370 g_param_spec_uint ("avatar-min-width",
1371 "Minimum width for avatars",
1372 "Minimum width of avatar that may be set.",
1377 G_PARAM_STATIC_STRINGS));
1378 g_object_class_install_property (object_class,
1380 g_param_spec_uint ("avatar-min-height",
1381 "Minimum height for avatars",
1382 "Minimum height of avatar that may be set.",
1387 G_PARAM_STATIC_STRINGS));
1388 g_object_class_install_property (object_class,
1390 g_param_spec_uint ("avatar-max-width",
1391 "Maximum width for avatars",
1392 "Maximum width of avatar that may be set "
1393 "or 0 if there is no maximum.",
1398 G_PARAM_STATIC_STRINGS));
1399 g_object_class_install_property (object_class,
1401 g_param_spec_uint ("avatar-max-height",
1402 "Maximum height for avatars",
1403 "Maximum height of avatar that may be set "
1404 "or 0 if there is no maximum.",
1409 G_PARAM_STATIC_STRINGS));
1410 g_object_class_install_property (object_class,
1412 g_param_spec_uint ("avatar-max-size",
1413 "Maximum size for avatars in bytes",
1414 "Maximum file size of avatar that may be "
1415 "set or 0 if there is no maximum.",
1420 G_PARAM_STATIC_STRINGS));
1423 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1427 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1429 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1430 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1432 tp_factory->priv = priv;
1433 priv->can_request_ft = FALSE;
1434 priv->can_request_st = FALSE;
1437 static GHashTable *factories = NULL;
1440 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1446 DEBUG ("Message: %s", message);
1447 g_hash_table_remove (factories, connection);
1451 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1452 GObject *where_the_object_was)
1454 g_hash_table_remove (factories, connection);
1458 tp_contact_factory_remove_connection (gpointer connection)
1460 g_signal_handlers_disconnect_by_func (connection,
1461 tp_contact_factory_connection_invalidated_cb, NULL);
1462 g_object_unref (connection);
1465 EmpathyTpContactFactory *
1466 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1468 EmpathyTpContactFactory *tp_factory;
1470 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1472 if (factories == NULL) {
1473 factories = g_hash_table_new_full (empathy_proxy_hash,
1474 empathy_proxy_equal,
1475 tp_contact_factory_remove_connection,
1479 tp_factory = g_hash_table_lookup (factories, connection);
1480 if (tp_factory == NULL) {
1481 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1482 "connection", connection,
1484 g_hash_table_insert (factories, g_object_ref (connection),
1486 g_object_weak_ref (G_OBJECT (tp_factory),
1487 tp_contact_factory_connection_weak_notify_cb,
1489 g_signal_connect (connection, "invalidated",
1490 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1493 g_object_ref (tp_factory);