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);
499 g_hash_table_insert (address, g_strdup ("country"), str);
501 str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
503 g_hash_table_insert (address, g_strdup ("postalcode"), str);
505 str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
507 g_hash_table_insert (address, g_strdup ("locality"), str);
509 str = get_dup_string (location, EMPATHY_LOCATION_STREET);
511 g_hash_table_insert (address, g_strdup ("street"), str);
513 g_object_ref (contact);
514 geoclue_geocode_address_to_position_async (geocode, address,
515 geocode_cb, contact);
517 g_hash_table_unref (address);
522 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
524 GHashTable *location)
526 EmpathyContact *contact;
527 GHashTable *new_location;
529 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
534 new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
535 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
536 tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
537 (GBoxedCopyFunc) tp_g_value_slice_dup);
538 empathy_contact_set_location (contact, new_location);
539 g_hash_table_unref (new_location);
541 tp_contact_factory_geocode (contact);
545 tp_contact_factory_got_locations (TpProxy *tp_proxy,
546 GHashTable *locations,
549 GObject *weak_object)
553 EmpathyTpContactFactory *tp_factory;
555 tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
557 DEBUG ("Error: %s", error->message);
561 g_hash_table_iter_init (&iter, locations);
562 while (g_hash_table_iter_next (&iter, &key, &value)) {
563 guint handle = GPOINTER_TO_INT (key);
564 GHashTable *location = value;
566 tp_contact_factory_update_location (tp_factory, handle, location);
571 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
572 const GPtrArray *capabilities,
574 GObject *weak_object)
576 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
579 for (i = 0; i < capabilities->len; i++) {
582 const gchar *channel_type;
586 values = g_ptr_array_index (capabilities, i);
587 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
588 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
589 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
590 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
592 tp_contact_factory_update_capabilities (tp_factory,
601 tp_contact_factory_location_updated_cb (TpProxy *proxy,
603 GHashTable *location,
605 GObject *weak_object)
607 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
608 tp_contact_factory_update_location (tp_factory, handle, location);
612 get_requestable_channel_classes_cb (TpProxy *connection,
616 GObject *weak_object)
618 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
619 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
625 DEBUG ("Error: %s", error->message);
629 classes = g_value_get_boxed (value);
630 for (i = 0; i < classes->len; i++) {
631 GValueArray *class_struct;
632 GHashTable *fixed_prop;
633 GValue *chan_type, *handle_type;
635 class_struct = g_ptr_array_index (classes, i);
636 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
638 handle_type = g_hash_table_lookup (fixed_prop,
639 TP_IFACE_CHANNEL ".TargetHandleType");
640 if (handle_type == NULL ||
641 g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT)
644 chan_type = g_hash_table_lookup (fixed_prop,
645 TP_IFACE_CHANNEL ".ChannelType");
646 if (chan_type == NULL)
649 if (!tp_strdiff (g_value_get_string (chan_type),
650 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
651 priv->can_request_ft = TRUE;
652 else if (!tp_strdiff (g_value_get_string (chan_type),
653 TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
654 priv->can_request_st = TRUE;
657 if (!priv->can_request_ft && !priv->can_request_st)
660 /* Update the capabilities of all contacts */
661 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
662 EmpathyContact *contact = l->data;
663 EmpathyCapabilities caps;
665 caps = empathy_contact_get_capabilities (contact);
667 if (priv->can_request_ft)
668 caps |= EMPATHY_CAPABILITIES_FT;
670 if (priv->can_request_st)
671 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
673 empathy_contact_set_capabilities (contact, caps);
678 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
679 const gchar **mime_types,
689 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
692 DEBUG ("Failed to get avatar requirements: %s", error->message);
693 /* We'll just leave avatar_mime_types as NULL; the
694 * avatar-setting code can use this as a signal that you can't
698 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
699 priv->avatar_min_width = min_width;
700 priv->avatar_min_height = min_height;
701 priv->avatar_max_width = max_width;
702 priv->avatar_max_height = max_height;
703 priv->avatar_max_size = max_size;
708 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
709 EmpathyContact *contact)
711 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
712 TpHandle self_handle;
714 GArray handles = {(gchar *) &handle, 1};
716 GPtrArray *capabilities;
717 GError *error = NULL;
718 EmpathyCapabilities caps;
720 /* Keep a weak ref to that contact */
721 g_object_weak_ref (G_OBJECT (contact),
722 tp_contact_factory_weak_notify,
724 priv->contacts = g_list_prepend (priv->contacts, contact);
726 /* The contact keeps a ref to its factory */
727 g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
728 g_object_ref (tp_factory),
731 caps = empathy_contact_get_capabilities (contact);
733 /* Set the FT capability */
734 if (priv->can_request_ft) {
735 caps |= EMPATHY_CAPABILITIES_FT;
738 /* Set the Stream Tube capability */
739 if (priv->can_request_st) {
740 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
743 empathy_contact_set_capabilities (contact, caps);
745 /* Set is-user property. Note that it could still be the handle is
746 * different from the connection's self handle, in the case the handle
747 * comes from a group interface. */
748 self_handle = tp_connection_get_self_handle (priv->connection);
749 handle = empathy_contact_get_handle (contact);
750 empathy_contact_set_is_user (contact, self_handle == handle);
752 /* FIXME: This should be done by TpContact */
753 tp_cli_connection_interface_avatars_run_get_known_avatar_tokens (priv->connection,
759 tp_contact_factory_got_known_avatar_tokens (tp_factory, tokens, error);
760 g_clear_error (&error);
762 tp_cli_connection_interface_capabilities_run_get_capabilities (priv->connection,
768 tp_contact_factory_got_capabilities (tp_factory, capabilities, error);
769 g_clear_error (&error);
771 if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
772 EMP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
773 emp_cli_connection_interface_location_call_get_locations (TP_PROXY (priv->connection),
776 tp_contact_factory_got_locations,
782 DEBUG ("Contact added: %s (%d)",
783 empathy_contact_get_id (contact),
784 empathy_contact_get_handle (contact));
788 EmpathyTpContactFactoryContactsByIdCb ids_cb;
789 EmpathyTpContactFactoryContactsByHandleCb handles_cb;
790 EmpathyTpContactFactoryContactCb contact_cb;
794 EmpathyTpContactFactory *tp_factory;
795 GetContactsCb callback;
797 GDestroyNotify destroy;
801 get_contacts_data_free (gpointer user_data)
803 GetContactsData *data = user_data;
806 data->destroy (data->user_data);
808 g_object_unref (data->tp_factory);
810 g_slice_free (GetContactsData, data);
813 static EmpathyContact *
814 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
815 TpContact *tp_contact)
817 EmpathyContact *contact;
819 contact = tp_contact_factory_find_by_tp_contact (tp_factory,
822 if (contact != NULL) {
823 g_object_ref (contact);
825 contact = empathy_contact_new (tp_contact);
826 tp_contact_factory_add_contact (tp_factory, contact);
832 static EmpathyContact **
833 contacts_array_new (EmpathyTpContactFactory *tp_factory,
835 TpContact * const * contacts)
837 EmpathyContact **ret;
840 ret = g_new0 (EmpathyContact *, n_contacts);
841 for (i = 0; i < n_contacts; i++) {
842 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
849 contacts_array_free (guint n_contacts,
850 EmpathyContact **contacts)
854 for (i = 0; i < n_contacts; i++) {
855 g_object_unref (contacts[i]);
861 get_contacts_by_id_cb (TpConnection *connection,
863 TpContact * const *contacts,
864 const gchar * const *requested_ids,
865 GHashTable *failed_id_errors,
868 GObject *weak_object)
870 GetContactsData *data = user_data;
871 EmpathyContact **empathy_contacts;
873 empathy_contacts = contacts_array_new (data->tp_factory,
874 n_contacts, contacts);
875 if (data->callback.ids_cb) {
876 data->callback.ids_cb (data->tp_factory,
877 n_contacts, empathy_contacts,
881 data->user_data, weak_object);
884 contacts_array_free (n_contacts, empathy_contacts);
888 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
890 const gchar * const *ids,
891 EmpathyTpContactFactoryContactsByIdCb callback,
893 GDestroyNotify destroy,
894 GObject *weak_object)
896 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
897 GetContactsData *data;
899 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
900 g_return_if_fail (ids != NULL);
902 data = g_slice_new (GetContactsData);
903 data->callback.ids_cb = callback;
904 data->user_data = user_data;
905 data->destroy = destroy;
906 data->tp_factory = g_object_ref (tp_factory);
907 tp_connection_get_contacts_by_id (priv->connection,
909 G_N_ELEMENTS (contact_features),
911 get_contacts_by_id_cb,
913 (GDestroyNotify) get_contacts_data_free,
918 get_contact_by_id_cb (TpConnection *connection,
920 TpContact * const *contacts,
921 const gchar * const *requested_ids,
922 GHashTable *failed_id_errors,
925 GObject *weak_object)
927 GetContactsData *data = user_data;
928 EmpathyContact *contact = NULL;
930 if (n_contacts == 1) {
931 contact = dup_contact_for_tp_contact (data->tp_factory,
934 else if (error == NULL) {
938 g_hash_table_iter_init (&iter, failed_id_errors);
939 while (g_hash_table_iter_next (&iter, NULL, &value)) {
947 if (data->callback.contact_cb) {
948 data->callback.contact_cb (data->tp_factory,
951 data->user_data, weak_object);
956 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
958 EmpathyTpContactFactoryContactCb callback,
960 GDestroyNotify destroy,
961 GObject *weak_object)
963 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
964 GetContactsData *data;
966 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
967 g_return_if_fail (id != NULL);
969 data = g_slice_new (GetContactsData);
970 data->callback.contact_cb = callback;
971 data->user_data = user_data;
972 data->destroy = destroy;
973 data->tp_factory = g_object_ref (tp_factory);
974 tp_connection_get_contacts_by_id (priv->connection,
976 G_N_ELEMENTS (contact_features),
978 get_contact_by_id_cb,
980 (GDestroyNotify) get_contacts_data_free,
985 get_contacts_by_handle_cb (TpConnection *connection,
987 TpContact * const *contacts,
989 const TpHandle *failed,
992 GObject *weak_object)
994 GetContactsData *data = user_data;
995 EmpathyContact **empathy_contacts;
997 empathy_contacts = contacts_array_new (data->tp_factory,
998 n_contacts, contacts);
999 if (data->callback.handles_cb) {
1000 data->callback.handles_cb (data->tp_factory,
1001 n_contacts, empathy_contacts,
1004 data->user_data, weak_object);
1007 contacts_array_free (n_contacts, empathy_contacts);
1011 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1013 const TpHandle *handles,
1014 EmpathyTpContactFactoryContactsByHandleCb callback,
1016 GDestroyNotify destroy,
1017 GObject *weak_object)
1019 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1020 GetContactsData *data;
1022 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1023 g_return_if_fail (handles != NULL);
1025 data = g_slice_new (GetContactsData);
1026 data->callback.handles_cb = callback;
1027 data->user_data = user_data;
1028 data->destroy = destroy;
1029 data->tp_factory = g_object_ref (tp_factory);
1030 tp_connection_get_contacts_by_handle (priv->connection,
1032 G_N_ELEMENTS (contact_features),
1034 get_contacts_by_handle_cb,
1036 (GDestroyNotify) get_contacts_data_free,
1041 get_contact_by_handle_cb (TpConnection *connection,
1043 TpContact * const *contacts,
1045 const TpHandle *failed,
1046 const GError *error,
1048 GObject *weak_object)
1050 GetContactsData *data = user_data;
1051 EmpathyContact *contact = NULL;
1053 if (n_contacts == 1) {
1054 contact = dup_contact_for_tp_contact (data->tp_factory,
1058 if (data->callback.contact_cb) {
1059 data->callback.contact_cb (data->tp_factory,
1062 data->user_data, weak_object);
1067 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1069 EmpathyTpContactFactoryContactCb callback,
1071 GDestroyNotify destroy,
1072 GObject *weak_object)
1074 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1075 GetContactsData *data;
1077 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1079 data = g_slice_new (GetContactsData);
1080 data->callback.contact_cb = callback;
1081 data->user_data = user_data;
1082 data->destroy = destroy;
1083 data->tp_factory = g_object_ref (tp_factory);
1084 tp_connection_get_contacts_by_handle (priv->connection,
1086 G_N_ELEMENTS (contact_features),
1088 get_contact_by_handle_cb,
1090 (GDestroyNotify) get_contacts_data_free,
1095 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1096 EmpathyContact *contact,
1099 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1100 GHashTable *new_alias;
1103 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1104 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1106 handle = empathy_contact_get_handle (contact);
1108 DEBUG ("Setting alias for contact %s (%d) to %s",
1109 empathy_contact_get_id (contact),
1112 new_alias = g_hash_table_new_full (g_direct_hash,
1117 g_hash_table_insert (new_alias,
1118 GUINT_TO_POINTER (handle),
1121 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1124 tp_contact_factory_set_aliases_cb,
1126 G_OBJECT (tp_factory));
1128 g_hash_table_destroy (new_alias);
1132 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1135 const gchar *mime_type)
1137 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1139 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1141 if (data && size > 0 && size < G_MAXUINT) {
1144 avatar.data = (gchar *) data;
1147 DEBUG ("Setting avatar on connection %s",
1148 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1150 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1154 tp_contact_factory_set_avatar_cb,
1156 G_OBJECT (tp_factory));
1158 DEBUG ("Clearing avatar on connection %s",
1159 tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1161 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1163 tp_contact_factory_clear_avatar_cb,
1165 G_OBJECT (tp_factory));
1170 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1171 GHashTable *location)
1173 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1175 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1177 DEBUG ("Setting location");
1179 emp_cli_connection_interface_location_call_set_location (TP_PROXY (priv->connection),
1182 tp_contact_factory_set_location_cb,
1184 G_OBJECT (tp_factory));
1188 tp_contact_factory_get_property (GObject *object,
1193 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1196 case PROP_CONNECTION:
1197 g_value_set_object (value, priv->connection);
1199 case PROP_MIME_TYPES:
1200 g_value_set_boxed (value, priv->avatar_mime_types);
1202 case PROP_MIN_WIDTH:
1203 g_value_set_uint (value, priv->avatar_min_width);
1205 case PROP_MIN_HEIGHT:
1206 g_value_set_uint (value, priv->avatar_min_height);
1208 case PROP_MAX_WIDTH:
1209 g_value_set_uint (value, priv->avatar_max_width);
1211 case PROP_MAX_HEIGHT:
1212 g_value_set_uint (value, priv->avatar_max_height);
1215 g_value_set_uint (value, priv->avatar_max_size);
1218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1224 tp_contact_factory_set_property (GObject *object,
1226 const GValue *value,
1229 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1232 case PROP_CONNECTION:
1233 priv->connection = g_value_dup_object (value);
1236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1242 tp_contact_factory_finalize (GObject *object)
1244 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1247 DEBUG ("Finalized: %p", object);
1249 for (l = priv->contacts; l; l = l->next) {
1250 g_object_weak_unref (G_OBJECT (l->data),
1251 tp_contact_factory_weak_notify,
1255 g_list_free (priv->contacts);
1257 g_object_unref (priv->connection);
1259 g_strfreev (priv->avatar_mime_types);
1261 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1265 tp_contact_factory_constructor (GType type,
1267 GObjectConstructParam *props)
1269 GObject *tp_factory;
1270 EmpathyTpContactFactoryPriv *priv;
1272 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1273 priv = GET_PRIV (tp_factory);
1275 /* FIXME: This should be moved to TpContact */
1276 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1277 tp_contact_factory_avatar_updated_cb,
1281 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1282 tp_contact_factory_avatar_retrieved_cb,
1286 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1287 tp_contact_factory_capabilities_changed_cb,
1293 emp_cli_connection_interface_location_connect_to_location_updated (TP_PROXY (priv->connection),
1294 tp_contact_factory_location_updated_cb,
1296 G_OBJECT (tp_factory),
1299 /* FIXME: This should be moved to TpConnection */
1300 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1302 tp_contact_factory_got_avatar_requirements_cb,
1305 tp_cli_dbus_properties_call_get (priv->connection, -1,
1306 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1307 "RequestableChannelClasses",
1308 get_requestable_channel_classes_cb, NULL, NULL,
1309 G_OBJECT (tp_factory));
1315 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1317 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1319 object_class->finalize = tp_contact_factory_finalize;
1320 object_class->constructor = tp_contact_factory_constructor;
1321 object_class->get_property = tp_contact_factory_get_property;
1322 object_class->set_property = tp_contact_factory_set_property;
1324 g_object_class_install_property (object_class,
1326 g_param_spec_object ("connection",
1327 "Factory's Connection",
1328 "The connection associated with the factory",
1331 G_PARAM_CONSTRUCT_ONLY |
1332 G_PARAM_STATIC_STRINGS));
1333 g_object_class_install_property (object_class,
1335 g_param_spec_boxed ("avatar-mime-types",
1336 "Supported MIME types for avatars",
1337 "Types of images that may be set as "
1338 "avatars on this connection.",
1341 G_PARAM_STATIC_STRINGS));
1342 g_object_class_install_property (object_class,
1344 g_param_spec_uint ("avatar-min-width",
1345 "Minimum width for avatars",
1346 "Minimum width of avatar that may be set.",
1351 G_PARAM_STATIC_STRINGS));
1352 g_object_class_install_property (object_class,
1354 g_param_spec_uint ("avatar-min-height",
1355 "Minimum height for avatars",
1356 "Minimum height of avatar that may be set.",
1361 G_PARAM_STATIC_STRINGS));
1362 g_object_class_install_property (object_class,
1364 g_param_spec_uint ("avatar-max-width",
1365 "Maximum width for avatars",
1366 "Maximum width of avatar that may be set "
1367 "or 0 if there is no maximum.",
1372 G_PARAM_STATIC_STRINGS));
1373 g_object_class_install_property (object_class,
1375 g_param_spec_uint ("avatar-max-height",
1376 "Maximum height for avatars",
1377 "Maximum height of avatar that may be set "
1378 "or 0 if there is no maximum.",
1383 G_PARAM_STATIC_STRINGS));
1384 g_object_class_install_property (object_class,
1386 g_param_spec_uint ("avatar-max-size",
1387 "Maximum size for avatars in bytes",
1388 "Maximum file size of avatar that may be "
1389 "set or 0 if there is no maximum.",
1394 G_PARAM_STATIC_STRINGS));
1397 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1401 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1403 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1404 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1406 tp_factory->priv = priv;
1407 priv->can_request_ft = FALSE;
1408 priv->can_request_st = FALSE;
1411 static GHashTable *factories = NULL;
1414 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1420 DEBUG ("Message: %s", message);
1421 g_hash_table_remove (factories, connection);
1425 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1426 GObject *where_the_object_was)
1428 g_hash_table_remove (factories, connection);
1432 tp_contact_factory_remove_connection (gpointer connection)
1434 g_signal_handlers_disconnect_by_func (connection,
1435 tp_contact_factory_connection_invalidated_cb, NULL);
1436 g_object_unref (connection);
1439 EmpathyTpContactFactory *
1440 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1442 EmpathyTpContactFactory *tp_factory;
1444 g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1446 if (factories == NULL) {
1447 factories = g_hash_table_new_full (empathy_proxy_hash,
1448 empathy_proxy_equal,
1449 tp_contact_factory_remove_connection,
1453 tp_factory = g_hash_table_lookup (factories, connection);
1454 if (tp_factory == NULL) {
1455 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1456 "connection", connection,
1458 g_hash_table_insert (factories, g_object_ref (connection),
1460 g_object_weak_ref (G_OBJECT (tp_factory),
1461 tp_contact_factory_connection_weak_notify_cb,
1463 g_signal_connect (connection, "invalidated",
1464 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1467 g_object_ref (tp_factory);