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,
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 EmpathyTpContactFactory *tp_factory;
246 tp_contact_factory_avatar_tokens_foreach (gpointer key,
250 TokensData *data = user_data;
251 const gchar *token = value;
252 guint handle = GPOINTER_TO_UINT (key);
254 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
256 g_array_append_val (data->handles, handle);
261 tp_contact_factory_got_known_avatar_tokens (EmpathyTpContactFactory *tp_factory,
265 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
269 DEBUG ("Error: %s", error->message);
273 data.tp_factory = tp_factory;
274 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
275 g_hash_table_foreach (tokens,
276 tp_contact_factory_avatar_tokens_foreach,
279 DEBUG ("Got %d tokens, need to request %d avatars",
280 g_hash_table_size (tokens), data.handles->len);
282 /* Request needed avatars */
283 if (data.handles->len > 0) {
284 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
287 tp_contact_factory_request_avatars_cb,
289 G_OBJECT (tp_factory));
292 g_array_free (data.handles, TRUE);
293 g_hash_table_destroy (tokens);
297 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
299 const gchar *new_token,
305 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
306 handle, new_token)) {
307 /* Avatar was cached, nothing to do */
311 DEBUG ("Need to request avatar for token %s", new_token);
313 handles = g_array_new (FALSE, FALSE, sizeof (guint));
314 g_array_append_val (handles, handle);
316 tp_cli_connection_interface_avatars_call_request_avatars (connection,
319 tp_contact_factory_request_avatars_cb,
322 g_array_free (handles, TRUE);
326 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
328 const gchar *channel_type,
332 EmpathyContact *contact;
333 EmpathyCapabilities capabilities;
335 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
340 capabilities = empathy_contact_get_capabilities (contact);
341 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
343 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
344 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
345 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
346 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
347 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
349 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
350 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
354 DEBUG ("Changing capabilities for contact %s (%d) to %d",
355 empathy_contact_get_id (contact),
356 empathy_contact_get_handle (contact),
359 empathy_contact_set_capabilities (contact, capabilities);
363 tp_contact_factory_got_capabilities (EmpathyTpContactFactory *tp_factory,
364 GPtrArray *capabilities,
370 DEBUG ("Error: %s", error->message);
371 /* FIXME Should set the capabilities of the contacts for which this request
372 * originated to NONE */
376 for (i = 0; i < capabilities->len; i++) {
379 const gchar *channel_type;
383 values = g_ptr_array_index (capabilities, i);
384 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
385 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
386 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
387 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
389 tp_contact_factory_update_capabilities (tp_factory,
395 g_value_array_free (values);
398 g_ptr_array_free (capabilities, TRUE);
402 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
403 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
405 /* This callback is called by geoclue when it found a position
406 * for the given address. A position is necessary for a contact
407 * to show up on the map
410 geocode_cb (GeoclueGeocode *geocode,
411 GeocluePositionFields fields,
415 GeoclueAccuracy *accuracy,
420 GHashTable *location;
422 location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
425 DEBUG ("Error geocoding location : %s", error->message);
426 g_object_unref (geocode);
427 g_object_unref (contact);
431 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
432 new_value = tp_g_value_slice_new_double (latitude);
433 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
435 DEBUG ("\t - Latitude: %f", latitude);
437 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
438 new_value = tp_g_value_slice_new_double (longitude);
439 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
441 DEBUG ("\t - Longitude: %f", longitude);
443 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
444 new_value = tp_g_value_slice_new_double (altitude);
445 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
447 DEBUG ("\t - Altitude: %f", altitude);
450 /* Don't change the accuracy as we used an address to get this position */
451 g_object_notify (contact, "location");
452 g_object_unref (geocode);
453 g_object_unref (contact);
459 get_dup_string (GHashTable *location,
464 value = g_hash_table_lookup (location, key);
466 return g_value_dup_string (value);
473 tp_contact_factory_geocode (EmpathyContact *contact)
476 static GeoclueGeocode *geocode;
480 GHashTable *location;
482 location = empathy_contact_get_location (contact);
483 if (location == NULL)
486 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
490 if (geocode == NULL) {
491 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
492 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
495 g_object_ref (geocode);
497 address = geoclue_address_details_new ();
499 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
501 g_hash_table_insert (address,
502 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
503 DEBUG ("\t - countrycode: %s", str);
506 str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
508 g_hash_table_insert (address,
509 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
510 DEBUG ("\t - country: %s", str);
513 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
515 g_hash_table_insert (address,
516 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
517 DEBUG ("\t - postalcode: %s", str);
520 str = get_dup_string (location, EMPATHY_LOCATION_REGION);
522 g_hash_table_insert (address,
523 g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
524 DEBUG ("\t - region: %s", str);
527 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
529 g_hash_table_insert (address,
530 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
531 DEBUG ("\t - locality: %s", str);
534 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
536 g_hash_table_insert (address,
537 g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
538 DEBUG ("\t - street: %s", str);
541 g_object_ref (contact);
542 geoclue_geocode_address_to_position_async (geocode, address,
543 geocode_cb, contact);
545 g_hash_table_unref (address);
550 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
552 GHashTable *location)
554 EmpathyContact *contact;
555 GHashTable *new_location;
557 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
562 new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
563 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
564 tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
565 (GBoxedCopyFunc) tp_g_value_slice_dup);
566 empathy_contact_set_location (contact, new_location);
567 g_hash_table_unref (new_location);
569 tp_contact_factory_geocode (contact);
573 tp_contact_factory_got_locations (TpConnection *tp_conn,
574 GHashTable *locations,
577 GObject *weak_object)
581 EmpathyTpContactFactory *tp_factory;
583 tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
585 DEBUG ("Error: %s", error->message);
589 g_hash_table_iter_init (&iter, locations);
590 while (g_hash_table_iter_next (&iter, &key, &value)) {
591 guint handle = GPOINTER_TO_INT (key);
592 GHashTable *location = value;
594 tp_contact_factory_update_location (tp_factory, handle, location);
599 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
600 const GPtrArray *capabilities,
602 GObject *weak_object)
604 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
607 for (i = 0; i < capabilities->len; i++) {
610 const gchar *channel_type;
614 values = g_ptr_array_index (capabilities, i);
615 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
616 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
617 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
618 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
620 tp_contact_factory_update_capabilities (tp_factory,
629 tp_contact_factory_location_updated_cb (TpConnection *tp_conn,
631 GHashTable *location,
633 GObject *weak_object)
635 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
636 tp_contact_factory_update_location (tp_factory, handle, location);
640 get_requestable_channel_classes_cb (TpProxy *connection,
644 GObject *weak_object)
646 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
647 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
653 DEBUG ("Error: %s", error->message);
657 classes = g_value_get_boxed (value);
658 for (i = 0; i < classes->len; i++) {
659 GValueArray *class_struct;
660 GHashTable *fixed_prop;
661 GValue *chan_type, *handle_type;
663 class_struct = g_ptr_array_index (classes, i);
664 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
666 handle_type = g_hash_table_lookup (fixed_prop,
667 TP_IFACE_CHANNEL ".TargetHandleType");
668 if (handle_type == NULL ||
669 g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT)
672 chan_type = g_hash_table_lookup (fixed_prop,
673 TP_IFACE_CHANNEL ".ChannelType");
674 if (chan_type == NULL)
677 if (!tp_strdiff (g_value_get_string (chan_type),
678 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
679 priv->can_request_ft = TRUE;
680 else if (!tp_strdiff (g_value_get_string (chan_type),
681 TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
682 priv->can_request_st = TRUE;
685 if (!priv->can_request_ft && !priv->can_request_st)
688 /* Update the capabilities of all contacts */
689 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
690 EmpathyContact *contact = l->data;
691 EmpathyCapabilities caps;
693 caps = empathy_contact_get_capabilities (contact);
695 if (priv->can_request_ft)
696 caps |= EMPATHY_CAPABILITIES_FT;
698 if (priv->can_request_st)
699 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
701 empathy_contact_set_capabilities (contact, caps);
706 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
707 const gchar **mime_types,
717 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
720 DEBUG ("Failed to get avatar requirements: %s", error->message);
721 /* We'll just leave avatar_mime_types as NULL; the
722 * avatar-setting code can use this as a signal that you can't
726 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
727 priv->avatar_min_width = min_width;
728 priv->avatar_min_height = min_height;
729 priv->avatar_max_width = max_width;
730 priv->avatar_max_height = max_height;
731 priv->avatar_max_size = max_size;
736 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
737 EmpathyContact *contact)
739 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
740 TpHandle self_handle;
742 GArray handles = {(gchar *) &handle, 1};
744 GPtrArray *capabilities;
745 GError *error = NULL;
746 EmpathyCapabilities caps;
748 /* Keep a weak ref to that contact */
749 g_object_weak_ref (G_OBJECT (contact),
750 tp_contact_factory_weak_notify,
752 priv->contacts = g_list_prepend (priv->contacts, contact);
754 /* The contact keeps a ref to its factory */
755 g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
756 g_object_ref (tp_factory),
759 caps = empathy_contact_get_capabilities (contact);
761 /* Set the FT capability */
762 if (priv->can_request_ft) {
763 caps |= EMPATHY_CAPABILITIES_FT;
766 /* Set the Stream Tube capability */
767 if (priv->can_request_st) {
768 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
771 empathy_contact_set_capabilities (contact, caps);
773 /* Set is-user property. Note that it could still be the handle is
774 * different from the connection's self handle, in the case the handle
775 * comes from a group interface. */
776 self_handle = tp_connection_get_self_handle (priv->connection);
777 handle = empathy_contact_get_handle (contact);
778 empathy_contact_set_is_user (contact, self_handle == handle);
780 /* FIXME: This should be done by TpContact */
781 tp_cli_connection_interface_avatars_run_get_known_avatar_tokens (priv->connection,
787 tp_contact_factory_got_known_avatar_tokens (tp_factory, tokens, error);
788 g_clear_error (&error);
790 tp_cli_connection_interface_capabilities_run_get_capabilities (priv->connection,
796 tp_contact_factory_got_capabilities (tp_factory, capabilities, error);
797 g_clear_error (&error);
799 if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
800 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
801 tp_cli_connection_interface_location_call_get_locations (priv->connection,
804 tp_contact_factory_got_locations,
810 DEBUG ("Contact added: %s (%d)",
811 empathy_contact_get_id (contact),
812 empathy_contact_get_handle (contact));
816 EmpathyTpContactFactoryContactsByIdCb ids_cb;
817 EmpathyTpContactFactoryContactsByHandleCb handles_cb;
818 EmpathyTpContactFactoryContactCb contact_cb;
822 EmpathyTpContactFactory *tp_factory;
823 GetContactsCb callback;
825 GDestroyNotify destroy;
829 get_contacts_data_free (gpointer user_data)
831 GetContactsData *data = user_data;
834 data->destroy (data->user_data);
836 g_object_unref (data->tp_factory);
838 g_slice_free (GetContactsData, data);
841 static EmpathyContact *
842 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
843 TpContact *tp_contact)
845 EmpathyContact *contact;
847 contact = tp_contact_factory_find_by_tp_contact (tp_factory,
850 if (contact != NULL) {
851 g_object_ref (contact);
853 contact = empathy_contact_new (tp_contact);
854 tp_contact_factory_add_contact (tp_factory, contact);
860 static EmpathyContact **
861 contacts_array_new (EmpathyTpContactFactory *tp_factory,
863 TpContact * const * contacts)
865 EmpathyContact **ret;
868 ret = g_new0 (EmpathyContact *, n_contacts);
869 for (i = 0; i < n_contacts; i++) {
870 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
877 contacts_array_free (guint n_contacts,
878 EmpathyContact **contacts)
882 for (i = 0; i < n_contacts; i++) {
883 g_object_unref (contacts[i]);
889 get_contacts_by_id_cb (TpConnection *connection,
891 TpContact * const *contacts,
892 const gchar * const *requested_ids,
893 GHashTable *failed_id_errors,
896 GObject *weak_object)
898 GetContactsData *data = user_data;
899 EmpathyContact **empathy_contacts;
901 empathy_contacts = contacts_array_new (data->tp_factory,
902 n_contacts, contacts);
903 if (data->callback.ids_cb) {
904 data->callback.ids_cb (data->tp_factory,
905 n_contacts, empathy_contacts,
909 data->user_data, weak_object);
912 contacts_array_free (n_contacts, empathy_contacts);
916 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
918 const gchar * const *ids,
919 EmpathyTpContactFactoryContactsByIdCb callback,
921 GDestroyNotify destroy,
922 GObject *weak_object)
924 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
925 GetContactsData *data;
927 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
928 g_return_if_fail (ids != NULL);
930 data = g_slice_new (GetContactsData);
931 data->callback.ids_cb = callback;
932 data->user_data = user_data;
933 data->destroy = destroy;
934 data->tp_factory = g_object_ref (tp_factory);
935 tp_connection_get_contacts_by_id (priv->connection,
937 G_N_ELEMENTS (contact_features),
939 get_contacts_by_id_cb,
941 (GDestroyNotify) get_contacts_data_free,
946 get_contact_by_id_cb (TpConnection *connection,
948 TpContact * const *contacts,
949 const gchar * const *requested_ids,
950 GHashTable *failed_id_errors,
953 GObject *weak_object)
955 GetContactsData *data = user_data;
956 EmpathyContact *contact = NULL;
958 if (n_contacts == 1) {
959 contact = dup_contact_for_tp_contact (data->tp_factory,
962 else if (error == NULL) {
966 g_hash_table_iter_init (&iter, failed_id_errors);
967 while (g_hash_table_iter_next (&iter, NULL, &value)) {
975 if (data->callback.contact_cb) {
976 data->callback.contact_cb (data->tp_factory,
979 data->user_data, weak_object);
984 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
986 EmpathyTpContactFactoryContactCb callback,
988 GDestroyNotify destroy,
989 GObject *weak_object)
991 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
992 GetContactsData *data;
994 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
995 g_return_if_fail (id != NULL);
997 data = g_slice_new (GetContactsData);
998 data->callback.contact_cb = callback;
999 data->user_data = user_data;
1000 data->destroy = destroy;
1001 data->tp_factory = g_object_ref (tp_factory);
1002 tp_connection_get_contacts_by_id (priv->connection,
1004 G_N_ELEMENTS (contact_features),
1006 get_contact_by_id_cb,
1008 (GDestroyNotify) get_contacts_data_free,
1013 get_contacts_by_handle_cb (TpConnection *connection,
1015 TpContact * const *contacts,
1017 const TpHandle *failed,
1018 const GError *error,
1020 GObject *weak_object)
1022 GetContactsData *data = user_data;
1023 EmpathyContact **empathy_contacts;
1025 empathy_contacts = contacts_array_new (data->tp_factory,
1026 n_contacts, contacts);
1027 if (data->callback.handles_cb) {
1028 data->callback.handles_cb (data->tp_factory,
1029 n_contacts, empathy_contacts,
1032 data->user_data, weak_object);
1035 contacts_array_free (n_contacts, empathy_contacts);
1039 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1041 const TpHandle *handles,
1042 EmpathyTpContactFactoryContactsByHandleCb callback,
1044 GDestroyNotify destroy,
1045 GObject *weak_object)
1047 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1048 GetContactsData *data;
1050 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1051 g_return_if_fail (handles != NULL);
1053 data = g_slice_new (GetContactsData);
1054 data->callback.handles_cb = callback;
1055 data->user_data = user_data;
1056 data->destroy = destroy;
1057 data->tp_factory = g_object_ref (tp_factory);
1058 tp_connection_get_contacts_by_handle (priv->connection,
1060 G_N_ELEMENTS (contact_features),
1062 get_contacts_by_handle_cb,
1064 (GDestroyNotify) get_contacts_data_free,
1069 get_contact_by_handle_cb (TpConnection *connection,
1071 TpContact * const *contacts,
1073 const TpHandle *failed,
1074 const GError *error,
1076 GObject *weak_object)
1078 GetContactsData *data = user_data;
1079 EmpathyContact *contact = NULL;
1081 if (n_contacts == 1) {
1082 contact = dup_contact_for_tp_contact (data->tp_factory,
1086 if (data->callback.contact_cb) {
1087 data->callback.contact_cb (data->tp_factory,
1090 data->user_data, weak_object);
1095 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1097 EmpathyTpContactFactoryContactCb callback,
1099 GDestroyNotify destroy,
1100 GObject *weak_object)
1102 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1103 GetContactsData *data;
1105 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1107 data = g_slice_new (GetContactsData);
1108 data->callback.contact_cb = callback;
1109 data->user_data = user_data;
1110 data->destroy = destroy;
1111 data->tp_factory = g_object_ref (tp_factory);
1112 tp_connection_get_contacts_by_handle (priv->connection,
1114 G_N_ELEMENTS (contact_features),
1116 get_contact_by_handle_cb,
1118 (GDestroyNotify) get_contacts_data_free,
1123 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1124 EmpathyContact *contact,
1127 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1128 GHashTable *new_alias;
1131 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1132 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1134 handle = empathy_contact_get_handle (contact);
1136 DEBUG ("Setting alias for contact %s (%d) to %s",
1137 empathy_contact_get_id (contact),
1140 new_alias = g_hash_table_new_full (g_direct_hash,
1145 g_hash_table_insert (new_alias,
1146 GUINT_TO_POINTER (handle),
1149 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1152 tp_contact_factory_set_aliases_cb,
1154 G_OBJECT (tp_factory));
1156 g_hash_table_destroy (new_alias);
1160 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1163 const gchar *mime_type)
1165 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1167 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1169 if (data && size > 0 && size < G_MAXUINT) {
1172 avatar.data = (gchar *) data;
1175 DEBUG ("Setting avatar on connection %s",
1176 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1178 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1182 tp_contact_factory_set_avatar_cb,
1184 G_OBJECT (tp_factory));
1186 DEBUG ("Clearing avatar on connection %s",
1187 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1189 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1191 tp_contact_factory_clear_avatar_cb,
1193 G_OBJECT (tp_factory));
1198 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1199 GHashTable *location)
1201 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1203 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1205 DEBUG ("Setting location");
1207 tp_cli_connection_interface_location_call_set_location (priv->connection,
1210 tp_contact_factory_set_location_cb,
1212 G_OBJECT (tp_factory));
1216 tp_contact_factory_get_property (GObject *object,
1221 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1224 case PROP_CONNECTION:
1225 g_value_set_object (value, priv->connection);
1227 case PROP_MIME_TYPES:
1228 g_value_set_boxed (value, priv->avatar_mime_types);
1230 case PROP_MIN_WIDTH:
1231 g_value_set_uint (value, priv->avatar_min_width);
1233 case PROP_MIN_HEIGHT:
1234 g_value_set_uint (value, priv->avatar_min_height);
1236 case PROP_MAX_WIDTH:
1237 g_value_set_uint (value, priv->avatar_max_width);
1239 case PROP_MAX_HEIGHT:
1240 g_value_set_uint (value, priv->avatar_max_height);
1243 g_value_set_uint (value, priv->avatar_max_size);
1246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1252 tp_contact_factory_set_property (GObject *object,
1254 const GValue *value,
1257 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1260 case PROP_CONNECTION:
1261 priv->connection = g_value_dup_object (value);
1264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1270 tp_contact_factory_finalize (GObject *object)
1272 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1275 DEBUG ("Finalized: %p", object);
1277 for (l = priv->contacts; l; l = l->next) {
1278 g_object_weak_unref (G_OBJECT (l->data),
1279 tp_contact_factory_weak_notify,
1283 g_list_free (priv->contacts);
1285 g_object_unref (priv->connection);
1287 g_strfreev (priv->avatar_mime_types);
1289 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1293 tp_contact_factory_constructor (GType type,
1295 GObjectConstructParam *props)
1297 GObject *tp_factory;
1298 EmpathyTpContactFactoryPriv *priv;
1300 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1301 priv = GET_PRIV (tp_factory);
1303 /* FIXME: This should be moved to TpContact */
1304 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1305 tp_contact_factory_avatar_updated_cb,
1309 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1310 tp_contact_factory_avatar_retrieved_cb,
1314 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1315 tp_contact_factory_capabilities_changed_cb,
1321 tp_cli_connection_interface_location_connect_to_location_updated (priv->connection,
1322 tp_contact_factory_location_updated_cb,
1324 G_OBJECT (tp_factory),
1327 /* FIXME: This should be moved to TpConnection */
1328 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1330 tp_contact_factory_got_avatar_requirements_cb,
1333 tp_cli_dbus_properties_call_get (priv->connection, -1,
1334 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1335 "RequestableChannelClasses",
1336 get_requestable_channel_classes_cb, NULL, NULL,
1337 G_OBJECT (tp_factory));
1343 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1345 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1347 object_class->finalize = tp_contact_factory_finalize;
1348 object_class->constructor = tp_contact_factory_constructor;
1349 object_class->get_property = tp_contact_factory_get_property;
1350 object_class->set_property = tp_contact_factory_set_property;
1352 g_object_class_install_property (object_class,
1354 g_param_spec_object ("connection",
1355 "Factory's Connection",
1356 "The connection associated with the factory",
1359 G_PARAM_CONSTRUCT_ONLY |
1360 G_PARAM_STATIC_STRINGS));
1361 g_object_class_install_property (object_class,
1363 g_param_spec_boxed ("avatar-mime-types",
1364 "Supported MIME types for avatars",
1365 "Types of images that may be set as "
1366 "avatars on this connection.",
1369 G_PARAM_STATIC_STRINGS));
1370 g_object_class_install_property (object_class,
1372 g_param_spec_uint ("avatar-min-width",
1373 "Minimum width for avatars",
1374 "Minimum width of avatar that may be set.",
1379 G_PARAM_STATIC_STRINGS));
1380 g_object_class_install_property (object_class,
1382 g_param_spec_uint ("avatar-min-height",
1383 "Minimum height for avatars",
1384 "Minimum height of avatar that may be set.",
1389 G_PARAM_STATIC_STRINGS));
1390 g_object_class_install_property (object_class,
1392 g_param_spec_uint ("avatar-max-width",
1393 "Maximum width for avatars",
1394 "Maximum width of avatar that may be set "
1395 "or 0 if there is no maximum.",
1400 G_PARAM_STATIC_STRINGS));
1401 g_object_class_install_property (object_class,
1403 g_param_spec_uint ("avatar-max-height",
1404 "Maximum height for avatars",
1405 "Maximum height of avatar that may be set "
1406 "or 0 if there is no maximum.",
1411 G_PARAM_STATIC_STRINGS));
1412 g_object_class_install_property (object_class,
1414 g_param_spec_uint ("avatar-max-size",
1415 "Maximum size for avatars in bytes",
1416 "Maximum file size of avatar that may be "
1417 "set or 0 if there is no maximum.",
1422 G_PARAM_STATIC_STRINGS));
1425 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1429 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1431 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1432 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1434 tp_factory->priv = priv;
1435 priv->can_request_ft = FALSE;
1436 priv->can_request_st = FALSE;
1439 static GHashTable *factories = NULL;
1442 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1448 DEBUG ("Message: %s", message);
1449 g_hash_table_remove (factories, connection);
1453 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1454 GObject *where_the_object_was)
1456 g_hash_table_remove (factories, connection);
1460 tp_contact_factory_remove_connection (gpointer connection)
1462 g_signal_handlers_disconnect_by_func (connection,
1463 tp_contact_factory_connection_invalidated_cb, NULL);
1464 g_object_unref (connection);
1467 EmpathyTpContactFactory *
1468 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1470 EmpathyTpContactFactory *tp_factory;
1472 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1474 if (factories == NULL) {
1475 factories = g_hash_table_new_full (empathy_proxy_hash,
1476 empathy_proxy_equal,
1477 tp_contact_factory_remove_connection,
1481 tp_factory = g_hash_table_lookup (factories, connection);
1482 if (tp_factory == NULL) {
1483 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1484 "connection", connection,
1486 g_hash_table_insert (factories, g_object_ref (connection),
1488 g_object_weak_ref (G_OBJECT (tp_factory),
1489 tp_contact_factory_connection_weak_notify_cb,
1491 g_signal_connect (connection, "invalidated",
1492 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1495 g_object_ref (tp_factory);