1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/gtypes.h>
28 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/interfaces.h>
32 #include <geoclue/geoclue-geocode.h>
35 #include <extensions/extensions.h>
37 #include "empathy-tp-contact-factory.h"
38 #include "empathy-utils.h"
39 #include "empathy-location.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
46 TpConnection
*connection
;
49 gchar
**avatar_mime_types
;
50 guint avatar_min_width
;
51 guint avatar_min_height
;
52 guint avatar_max_width
;
53 guint avatar_max_height
;
54 guint avatar_max_size
;
55 gboolean can_request_ft
;
56 gboolean can_request_st
;
57 } EmpathyTpContactFactoryPriv
;
59 G_DEFINE_TYPE (EmpathyTpContactFactory
, empathy_tp_contact_factory
, G_TYPE_OBJECT
);
73 static TpContactFeature contact_features
[] = {
74 TP_CONTACT_FEATURE_ALIAS
,
75 TP_CONTACT_FEATURE_PRESENCE
,
78 static EmpathyContact
*
79 tp_contact_factory_find_by_handle (EmpathyTpContactFactory
*tp_factory
,
82 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
85 for (l
= priv
->contacts
; l
; l
= l
->next
) {
86 if (empathy_contact_get_handle (l
->data
) == handle
) {
94 static EmpathyContact
*
95 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory
*tp_factory
,
96 TpContact
*tp_contact
)
98 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
101 for (l
= priv
->contacts
; l
; l
= l
->next
) {
102 if (empathy_contact_get_tp_contact (l
->data
) == tp_contact
) {
111 tp_contact_factory_weak_notify (gpointer data
,
112 GObject
*where_the_object_was
)
114 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (data
);
116 DEBUG ("Remove finalized contact %p", where_the_object_was
);
118 priv
->contacts
= g_list_remove (priv
->contacts
, where_the_object_was
);
122 tp_contact_factory_set_aliases_cb (TpConnection
*connection
,
128 DEBUG ("Error: %s", error
->message
);
133 tp_contact_factory_set_location_cb (TpConnection
*tp_conn
,
136 GObject
*weak_object
)
139 DEBUG ("Error setting location: %s", error
->message
);
144 tp_contact_factory_set_avatar_cb (TpConnection
*connection
,
151 DEBUG ("Error: %s", error
->message
);
156 tp_contact_factory_clear_avatar_cb (TpConnection
*connection
,
162 DEBUG ("Error: %s", error
->message
);
167 tp_contact_factory_avatar_retrieved_cb (TpConnection
*connection
,
170 const GArray
*avatar_data
,
171 const gchar
*mime_type
,
175 EmpathyContact
*contact
;
177 contact
= tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory
),
183 DEBUG ("Avatar retrieved for contact %s (%d)",
184 empathy_contact_get_id (contact
),
187 empathy_contact_load_avatar_data (contact
,
188 (guchar
*) avatar_data
->data
,
195 tp_contact_factory_request_avatars_cb (TpConnection
*connection
,
201 DEBUG ("Error: %s", error
->message
);
206 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory
*tp_factory
,
210 EmpathyContact
*contact
;
211 EmpathyAvatar
*avatar
;
213 contact
= tp_contact_factory_find_by_handle (tp_factory
, handle
);
218 /* Check if we have an avatar */
219 if (EMP_STR_EMPTY (token
)) {
220 empathy_contact_set_avatar (contact
, NULL
);
224 /* Check if the avatar changed */
225 avatar
= empathy_contact_get_avatar (contact
);
226 if (avatar
&& !tp_strdiff (avatar
->token
, token
)) {
230 /* The avatar changed, search the new one in the cache */
231 if (empathy_contact_load_avatar_cache (contact
, token
)) {
232 /* Got from cache, use it */
236 /* Avatar is not up-to-date, we have to request it. */
241 tp_contact_factory_got_known_avatar_tokens (TpConnection
*connection
,
245 GObject
*weak_object
)
247 EmpathyTpContactFactory
*tp_factory
= EMPATHY_TP_CONTACT_FACTORY (weak_object
);
248 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
254 DEBUG ("Error: %s", error
->message
);
258 handles
= g_array_new (FALSE
, FALSE
, sizeof (guint
));
260 g_hash_table_iter_init (&iter
, tokens
);
261 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
262 guint handle
= GPOINTER_TO_UINT (key
);
263 const gchar
*token
= value
;
265 if (!tp_contact_factory_avatar_maybe_update (tp_factory
,
267 g_array_append_val (handles
, handle
);
271 DEBUG ("Got %d tokens, need to request %d avatars",
272 g_hash_table_size (tokens
), handles
->len
);
274 /* Request needed avatars */
275 if (handles
->len
> 0) {
276 tp_cli_connection_interface_avatars_call_request_avatars (priv
->connection
,
279 tp_contact_factory_request_avatars_cb
,
281 G_OBJECT (tp_factory
));
284 g_array_free (handles
, TRUE
);
288 tp_contact_factory_avatar_updated_cb (TpConnection
*connection
,
290 const gchar
*new_token
,
296 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory
),
297 handle
, new_token
)) {
298 /* Avatar was cached, nothing to do */
302 DEBUG ("Need to request avatar for token %s", new_token
);
304 handles
= g_array_new (FALSE
, FALSE
, sizeof (guint
));
305 g_array_append_val (handles
, handle
);
307 tp_cli_connection_interface_avatars_call_request_avatars (connection
,
310 tp_contact_factory_request_avatars_cb
,
313 g_array_free (handles
, TRUE
);
317 tp_contact_factory_update_capabilities (EmpathyTpContactFactory
*tp_factory
,
319 const gchar
*channel_type
,
323 EmpathyContact
*contact
;
324 EmpathyCapabilities capabilities
;
326 contact
= tp_contact_factory_find_by_handle (tp_factory
, handle
);
331 capabilities
= empathy_contact_get_capabilities (contact
);
332 capabilities
&= ~EMPATHY_CAPABILITIES_UNKNOWN
;
334 if (strcmp (channel_type
, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA
) == 0) {
335 capabilities
&= ~EMPATHY_CAPABILITIES_AUDIO
;
336 capabilities
&= ~EMPATHY_CAPABILITIES_VIDEO
;
337 if (specific
& TP_CHANNEL_MEDIA_CAPABILITY_AUDIO
) {
338 capabilities
|= EMPATHY_CAPABILITIES_AUDIO
;
340 if (specific
& TP_CHANNEL_MEDIA_CAPABILITY_VIDEO
) {
341 capabilities
|= EMPATHY_CAPABILITIES_VIDEO
;
345 DEBUG ("Changing capabilities for contact %s (%d) to %d",
346 empathy_contact_get_id (contact
),
347 empathy_contact_get_handle (contact
),
350 empathy_contact_set_capabilities (contact
, capabilities
);
354 tp_contact_factory_got_capabilities (TpConnection
*connection
,
355 const GPtrArray
*capabilities
,
358 GObject
*weak_object
)
360 EmpathyTpContactFactory
*tp_factory
;
363 tp_factory
= EMPATHY_TP_CONTACT_FACTORY (weak_object
);
366 DEBUG ("Error: %s", error
->message
);
367 /* FIXME Should set the capabilities of the contacts for which this request
368 * originated to NONE */
372 for (i
= 0; i
< capabilities
->len
; i
++) {
375 const gchar
*channel_type
;
379 values
= g_ptr_array_index (capabilities
, i
);
380 handle
= g_value_get_uint (g_value_array_get_nth (values
, 0));
381 channel_type
= g_value_get_string (g_value_array_get_nth (values
, 1));
382 generic
= g_value_get_uint (g_value_array_get_nth (values
, 2));
383 specific
= g_value_get_uint (g_value_array_get_nth (values
, 3));
385 tp_contact_factory_update_capabilities (tp_factory
,
394 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
395 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
397 /* This callback is called by geoclue when it found a position
398 * for the given address. A position is necessary for a contact
399 * to show up on the map
402 geocode_cb (GeoclueGeocode
*geocode
,
403 GeocluePositionFields fields
,
407 GeoclueAccuracy
*accuracy
,
412 GHashTable
*location
;
414 location
= empathy_contact_get_location (EMPATHY_CONTACT (contact
));
417 DEBUG ("Error geocoding location : %s", error
->message
);
418 g_object_unref (geocode
);
419 g_object_unref (contact
);
423 if (fields
& GEOCLUE_POSITION_FIELDS_LATITUDE
) {
424 new_value
= tp_g_value_slice_new_double (latitude
);
425 g_hash_table_replace (location
, g_strdup (EMPATHY_LOCATION_LAT
),
427 DEBUG ("\t - Latitude: %f", latitude
);
429 if (fields
& GEOCLUE_POSITION_FIELDS_LONGITUDE
) {
430 new_value
= tp_g_value_slice_new_double (longitude
);
431 g_hash_table_replace (location
, g_strdup (EMPATHY_LOCATION_LON
),
433 DEBUG ("\t - Longitude: %f", longitude
);
435 if (fields
& GEOCLUE_POSITION_FIELDS_ALTITUDE
) {
436 new_value
= tp_g_value_slice_new_double (altitude
);
437 g_hash_table_replace (location
, g_strdup (EMPATHY_LOCATION_ALT
),
439 DEBUG ("\t - Altitude: %f", altitude
);
442 /* Don't change the accuracy as we used an address to get this position */
443 g_object_notify (contact
, "location");
444 g_object_unref (geocode
);
445 g_object_unref (contact
);
451 get_dup_string (GHashTable
*location
,
456 value
= g_hash_table_lookup (location
, key
);
458 return g_value_dup_string (value
);
465 tp_contact_factory_geocode (EmpathyContact
*contact
)
468 static GeoclueGeocode
*geocode
;
472 GHashTable
*location
;
474 location
= empathy_contact_get_location (contact
);
475 if (location
== NULL
)
478 value
= g_hash_table_lookup (location
, EMPATHY_LOCATION_LAT
);
482 if (geocode
== NULL
) {
483 geocode
= geoclue_geocode_new (GEOCODE_SERVICE
, GEOCODE_PATH
);
484 g_object_add_weak_pointer (G_OBJECT (geocode
), (gpointer
*) &geocode
);
487 g_object_ref (geocode
);
489 address
= geoclue_address_details_new ();
491 str
= get_dup_string (location
, EMPATHY_LOCATION_COUNTRY_CODE
);
493 g_hash_table_insert (address
,
494 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE
), str
);
495 DEBUG ("\t - countrycode: %s", str
);
498 str
= get_dup_string (location
, EMPATHY_LOCATION_COUNTRY
);
500 g_hash_table_insert (address
,
501 g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY
), str
);
502 DEBUG ("\t - country: %s", str
);
505 str
= get_dup_string (location
, EMPATHY_LOCATION_POSTAL_CODE
);
507 g_hash_table_insert (address
,
508 g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE
), str
);
509 DEBUG ("\t - postalcode: %s", str
);
512 str
= get_dup_string (location
, EMPATHY_LOCATION_REGION
);
514 g_hash_table_insert (address
,
515 g_strdup (GEOCLUE_ADDRESS_KEY_REGION
), str
);
516 DEBUG ("\t - region: %s", str
);
519 str
= get_dup_string (location
, EMPATHY_LOCATION_LOCALITY
);
521 g_hash_table_insert (address
,
522 g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY
), str
);
523 DEBUG ("\t - locality: %s", str
);
526 str
= get_dup_string (location
, EMPATHY_LOCATION_STREET
);
528 g_hash_table_insert (address
,
529 g_strdup (GEOCLUE_ADDRESS_KEY_STREET
), str
);
530 DEBUG ("\t - street: %s", str
);
533 g_object_ref (contact
);
534 geoclue_geocode_address_to_position_async (geocode
, address
,
535 geocode_cb
, contact
);
537 g_hash_table_unref (address
);
542 tp_contact_factory_update_location (EmpathyTpContactFactory
*tp_factory
,
544 GHashTable
*location
)
546 EmpathyContact
*contact
;
547 GHashTable
*new_location
;
549 contact
= tp_contact_factory_find_by_handle (tp_factory
, handle
);
554 new_location
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
555 (GDestroyNotify
) g_free
, (GDestroyNotify
) tp_g_value_slice_free
);
556 tp_g_hash_table_update (new_location
, location
, (GBoxedCopyFunc
) g_strdup
,
557 (GBoxedCopyFunc
) tp_g_value_slice_dup
);
558 empathy_contact_set_location (contact
, new_location
);
559 g_hash_table_unref (new_location
);
561 tp_contact_factory_geocode (contact
);
565 tp_contact_factory_got_locations (TpConnection
*tp_conn
,
566 GHashTable
*locations
,
569 GObject
*weak_object
)
573 EmpathyTpContactFactory
*tp_factory
;
575 tp_factory
= EMPATHY_TP_CONTACT_FACTORY (user_data
);
577 DEBUG ("Error: %s", error
->message
);
581 g_hash_table_iter_init (&iter
, locations
);
582 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
583 guint handle
= GPOINTER_TO_INT (key
);
584 GHashTable
*location
= value
;
586 tp_contact_factory_update_location (tp_factory
, handle
, location
);
591 tp_contact_factory_capabilities_changed_cb (TpConnection
*connection
,
592 const GPtrArray
*capabilities
,
594 GObject
*weak_object
)
596 EmpathyTpContactFactory
*tp_factory
= EMPATHY_TP_CONTACT_FACTORY (weak_object
);
599 for (i
= 0; i
< capabilities
->len
; i
++) {
602 const gchar
*channel_type
;
606 values
= g_ptr_array_index (capabilities
, i
);
607 handle
= g_value_get_uint (g_value_array_get_nth (values
, 0));
608 channel_type
= g_value_get_string (g_value_array_get_nth (values
, 1));
609 generic
= g_value_get_uint (g_value_array_get_nth (values
, 3));
610 specific
= g_value_get_uint (g_value_array_get_nth (values
, 5));
612 tp_contact_factory_update_capabilities (tp_factory
,
621 tp_contact_factory_location_updated_cb (TpConnection
*tp_conn
,
623 GHashTable
*location
,
625 GObject
*weak_object
)
627 EmpathyTpContactFactory
*tp_factory
= EMPATHY_TP_CONTACT_FACTORY (weak_object
);
628 tp_contact_factory_update_location (tp_factory
, handle
, location
);
632 get_requestable_channel_classes_cb (TpProxy
*connection
,
636 GObject
*weak_object
)
638 EmpathyTpContactFactory
*self
= EMPATHY_TP_CONTACT_FACTORY (weak_object
);
639 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (self
);
645 DEBUG ("Error: %s", error
->message
);
649 classes
= g_value_get_boxed (value
);
650 for (i
= 0; i
< classes
->len
; i
++) {
651 GValueArray
*class_struct
;
652 GHashTable
*fixed_prop
;
653 GValue
*chan_type
, *handle_type
;
655 class_struct
= g_ptr_array_index (classes
, i
);
656 fixed_prop
= g_value_get_boxed (g_value_array_get_nth (class_struct
, 0));
658 handle_type
= g_hash_table_lookup (fixed_prop
,
659 TP_IFACE_CHANNEL
".TargetHandleType");
660 if (handle_type
== NULL
||
661 g_value_get_uint (handle_type
) != TP_HANDLE_TYPE_CONTACT
)
664 chan_type
= g_hash_table_lookup (fixed_prop
,
665 TP_IFACE_CHANNEL
".ChannelType");
666 if (chan_type
== NULL
)
669 if (!tp_strdiff (g_value_get_string (chan_type
),
670 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER
))
671 priv
->can_request_ft
= TRUE
;
672 else if (!tp_strdiff (g_value_get_string (chan_type
),
673 TP_IFACE_CHANNEL_TYPE_STREAM_TUBE
))
674 priv
->can_request_st
= TRUE
;
677 if (!priv
->can_request_ft
&& !priv
->can_request_st
)
680 /* Update the capabilities of all contacts */
681 for (l
= priv
->contacts
; l
!= NULL
; l
= g_list_next (l
)) {
682 EmpathyContact
*contact
= l
->data
;
683 EmpathyCapabilities caps
;
685 caps
= empathy_contact_get_capabilities (contact
);
687 if (priv
->can_request_ft
)
688 caps
|= EMPATHY_CAPABILITIES_FT
;
690 if (priv
->can_request_st
)
691 caps
|= EMPATHY_CAPABILITIES_STREAM_TUBE
;
693 empathy_contact_set_capabilities (contact
, caps
);
698 tp_contact_factory_got_avatar_requirements_cb (TpConnection
*proxy
,
699 const gchar
**mime_types
,
709 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
712 DEBUG ("Failed to get avatar requirements: %s", error
->message
);
713 /* We'll just leave avatar_mime_types as NULL; the
714 * avatar-setting code can use this as a signal that you can't
718 priv
->avatar_mime_types
= g_strdupv ((gchar
**) mime_types
);
719 priv
->avatar_min_width
= min_width
;
720 priv
->avatar_min_height
= min_height
;
721 priv
->avatar_max_width
= max_width
;
722 priv
->avatar_max_height
= max_height
;
723 priv
->avatar_max_size
= max_size
;
728 tp_contact_factory_add_contact (EmpathyTpContactFactory
*tp_factory
,
729 EmpathyContact
*contact
)
731 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
732 TpHandle self_handle
;
734 GArray handles
= {(gchar
*) &handle
, 1};
735 EmpathyCapabilities caps
;
737 /* Keep a weak ref to that contact */
738 g_object_weak_ref (G_OBJECT (contact
),
739 tp_contact_factory_weak_notify
,
741 priv
->contacts
= g_list_prepend (priv
->contacts
, contact
);
743 /* The contact keeps a ref to its factory */
744 g_object_set_data_full (G_OBJECT (contact
), "empathy-factory",
745 g_object_ref (tp_factory
),
748 caps
= empathy_contact_get_capabilities (contact
);
750 /* Set the FT capability */
751 if (priv
->can_request_ft
) {
752 caps
|= EMPATHY_CAPABILITIES_FT
;
755 /* Set the Stream Tube capability */
756 if (priv
->can_request_st
) {
757 caps
|= EMPATHY_CAPABILITIES_STREAM_TUBE
;
760 empathy_contact_set_capabilities (contact
, caps
);
762 /* Set is-user property. Note that it could still be the handle is
763 * different from the connection's self handle, in the case the handle
764 * comes from a group interface. */
765 self_handle
= tp_connection_get_self_handle (priv
->connection
);
766 handle
= empathy_contact_get_handle (contact
);
767 empathy_contact_set_is_user (contact
, self_handle
== handle
);
769 /* FIXME: This should be done by TpContact */
770 if (tp_proxy_has_interface_by_id (priv
->connection
,
771 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS
)) {
772 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (
773 priv
->connection
, -1, &handles
,
774 tp_contact_factory_got_known_avatar_tokens
, NULL
, NULL
,
775 G_OBJECT (tp_factory
));
778 if (tp_proxy_has_interface_by_id (priv
->connection
,
779 TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES
)) {
780 tp_cli_connection_interface_capabilities_call_get_capabilities (
781 priv
->connection
, -1, &handles
,
782 tp_contact_factory_got_capabilities
, NULL
, NULL
,
783 G_OBJECT (tp_factory
));
786 if (tp_proxy_has_interface_by_id (TP_PROXY (priv
->connection
),
787 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION
)) {
788 tp_cli_connection_interface_location_call_get_locations (priv
->connection
,
791 tp_contact_factory_got_locations
,
797 DEBUG ("Contact added: %s (%d)",
798 empathy_contact_get_id (contact
),
799 empathy_contact_get_handle (contact
));
803 EmpathyTpContactFactoryContactsByIdCb ids_cb
;
804 EmpathyTpContactFactoryContactsByHandleCb handles_cb
;
805 EmpathyTpContactFactoryContactCb contact_cb
;
809 EmpathyTpContactFactory
*tp_factory
;
810 GetContactsCb callback
;
812 GDestroyNotify destroy
;
816 get_contacts_data_free (gpointer user_data
)
818 GetContactsData
*data
= user_data
;
821 data
->destroy (data
->user_data
);
823 g_object_unref (data
->tp_factory
);
825 g_slice_free (GetContactsData
, data
);
828 static EmpathyContact
*
829 dup_contact_for_tp_contact (EmpathyTpContactFactory
*tp_factory
,
830 TpContact
*tp_contact
)
832 EmpathyContact
*contact
;
834 contact
= tp_contact_factory_find_by_tp_contact (tp_factory
,
837 if (contact
!= NULL
) {
838 g_object_ref (contact
);
840 contact
= empathy_contact_new (tp_contact
);
841 tp_contact_factory_add_contact (tp_factory
, contact
);
847 static EmpathyContact
**
848 contacts_array_new (EmpathyTpContactFactory
*tp_factory
,
850 TpContact
* const * contacts
)
852 EmpathyContact
**ret
;
855 ret
= g_new0 (EmpathyContact
*, n_contacts
);
856 for (i
= 0; i
< n_contacts
; i
++) {
857 ret
[i
] = dup_contact_for_tp_contact (tp_factory
, contacts
[i
]);
864 contacts_array_free (guint n_contacts
,
865 EmpathyContact
**contacts
)
869 for (i
= 0; i
< n_contacts
; i
++) {
870 g_object_unref (contacts
[i
]);
876 get_contacts_by_id_cb (TpConnection
*connection
,
878 TpContact
* const *contacts
,
879 const gchar
* const *requested_ids
,
880 GHashTable
*failed_id_errors
,
883 GObject
*weak_object
)
885 GetContactsData
*data
= user_data
;
886 EmpathyContact
**empathy_contacts
;
888 empathy_contacts
= contacts_array_new (data
->tp_factory
,
889 n_contacts
, contacts
);
890 if (data
->callback
.ids_cb
) {
891 data
->callback
.ids_cb (data
->tp_factory
,
892 n_contacts
, empathy_contacts
,
896 data
->user_data
, weak_object
);
899 contacts_array_free (n_contacts
, empathy_contacts
);
902 /* The callback is NOT given a reference to the EmpathyContact objects */
904 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory
*tp_factory
,
906 const gchar
* const *ids
,
907 EmpathyTpContactFactoryContactsByIdCb callback
,
909 GDestroyNotify destroy
,
910 GObject
*weak_object
)
912 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
913 GetContactsData
*data
;
915 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory
));
916 g_return_if_fail (ids
!= NULL
);
918 data
= g_slice_new (GetContactsData
);
919 data
->callback
.ids_cb
= callback
;
920 data
->user_data
= user_data
;
921 data
->destroy
= destroy
;
922 data
->tp_factory
= g_object_ref (tp_factory
);
923 tp_connection_get_contacts_by_id (priv
->connection
,
925 G_N_ELEMENTS (contact_features
),
927 get_contacts_by_id_cb
,
929 (GDestroyNotify
) get_contacts_data_free
,
934 get_contact_by_id_cb (TpConnection
*connection
,
936 TpContact
* const *contacts
,
937 const gchar
* const *requested_ids
,
938 GHashTable
*failed_id_errors
,
941 GObject
*weak_object
)
943 GetContactsData
*data
= user_data
;
944 EmpathyContact
*contact
= NULL
;
946 if (n_contacts
== 1) {
947 contact
= dup_contact_for_tp_contact (data
->tp_factory
,
950 else if (error
== NULL
) {
954 g_hash_table_iter_init (&iter
, failed_id_errors
);
955 while (g_hash_table_iter_next (&iter
, NULL
, &value
)) {
963 if (data
->callback
.contact_cb
) {
964 data
->callback
.contact_cb (data
->tp_factory
,
967 data
->user_data
, weak_object
);
971 g_object_unref (contact
);
974 /* The callback is NOT given a reference to the EmpathyContact objects */
976 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory
*tp_factory
,
978 EmpathyTpContactFactoryContactCb callback
,
980 GDestroyNotify destroy
,
981 GObject
*weak_object
)
983 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
984 GetContactsData
*data
;
986 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory
));
987 g_return_if_fail (id
!= NULL
);
989 data
= g_slice_new (GetContactsData
);
990 data
->callback
.contact_cb
= callback
;
991 data
->user_data
= user_data
;
992 data
->destroy
= destroy
;
993 data
->tp_factory
= g_object_ref (tp_factory
);
994 tp_connection_get_contacts_by_id (priv
->connection
,
996 G_N_ELEMENTS (contact_features
),
998 get_contact_by_id_cb
,
1000 (GDestroyNotify
) get_contacts_data_free
,
1005 get_contacts_by_handle_cb (TpConnection
*connection
,
1007 TpContact
* const *contacts
,
1009 const TpHandle
*failed
,
1010 const GError
*error
,
1012 GObject
*weak_object
)
1014 GetContactsData
*data
= user_data
;
1015 EmpathyContact
**empathy_contacts
;
1017 empathy_contacts
= contacts_array_new (data
->tp_factory
,
1018 n_contacts
, contacts
);
1019 if (data
->callback
.handles_cb
) {
1020 data
->callback
.handles_cb (data
->tp_factory
,
1021 n_contacts
, empathy_contacts
,
1024 data
->user_data
, weak_object
);
1027 contacts_array_free (n_contacts
, empathy_contacts
);
1030 /* The callback is NOT given a reference to the EmpathyContact objects */
1032 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory
*tp_factory
,
1034 const TpHandle
*handles
,
1035 EmpathyTpContactFactoryContactsByHandleCb callback
,
1037 GDestroyNotify destroy
,
1038 GObject
*weak_object
)
1040 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
1041 GetContactsData
*data
;
1043 if (n_handles
== 0) {
1044 callback (tp_factory
, 0, NULL
, 0, NULL
, NULL
, user_data
, weak_object
);
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
,
1066 /* The callback is NOT given a reference to the EmpathyContact objects */
1068 get_contact_by_handle_cb (TpConnection
*connection
,
1070 TpContact
* const *contacts
,
1072 const TpHandle
*failed
,
1073 const GError
*error
,
1075 GObject
*weak_object
)
1077 GetContactsData
*data
= user_data
;
1078 EmpathyContact
*contact
= NULL
;
1081 if (n_contacts
== 1) {
1082 contact
= dup_contact_for_tp_contact (data
->tp_factory
,
1086 if (error
== NULL
) {
1087 /* tp-glib will provide an error only if the whole operation failed,
1088 * but not if, for example, the handle was invalid. We create an error
1089 * so the caller of empathy_tp_contact_factory_get_from_handle can
1090 * rely on the error to check if the operation succeeded or not. */
1092 err
= g_error_new_literal (TP_ERRORS
, TP_ERROR_INVALID_HANDLE
,
1093 "handle is invalid");
1096 err
= g_error_copy (error
);
1100 if (data
->callback
.contact_cb
) {
1101 data
->callback
.contact_cb (data
->tp_factory
,
1104 data
->user_data
, weak_object
);
1107 g_clear_error (&err
);
1108 if (contact
!= NULL
)
1109 g_object_unref (contact
);
1113 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory
*tp_factory
,
1115 EmpathyTpContactFactoryContactCb callback
,
1117 GDestroyNotify destroy
,
1118 GObject
*weak_object
)
1120 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
1121 GetContactsData
*data
;
1123 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory
));
1125 data
= g_slice_new (GetContactsData
);
1126 data
->callback
.contact_cb
= callback
;
1127 data
->user_data
= user_data
;
1128 data
->destroy
= destroy
;
1129 data
->tp_factory
= g_object_ref (tp_factory
);
1130 tp_connection_get_contacts_by_handle (priv
->connection
,
1132 G_N_ELEMENTS (contact_features
),
1134 get_contact_by_handle_cb
,
1136 (GDestroyNotify
) get_contacts_data_free
,
1141 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory
*tp_factory
,
1142 EmpathyContact
*contact
,
1145 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
1146 GHashTable
*new_alias
;
1149 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory
));
1150 g_return_if_fail (EMPATHY_IS_CONTACT (contact
));
1152 handle
= empathy_contact_get_handle (contact
);
1154 DEBUG ("Setting alias for contact %s (%d) to %s",
1155 empathy_contact_get_id (contact
),
1158 new_alias
= g_hash_table_new_full (g_direct_hash
,
1163 g_hash_table_insert (new_alias
,
1164 GUINT_TO_POINTER (handle
),
1167 tp_cli_connection_interface_aliasing_call_set_aliases (priv
->connection
,
1170 tp_contact_factory_set_aliases_cb
,
1172 G_OBJECT (tp_factory
));
1174 g_hash_table_destroy (new_alias
);
1178 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory
*tp_factory
,
1181 const gchar
*mime_type
)
1183 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
1185 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory
));
1187 if (data
&& size
> 0 && size
< G_MAXUINT
) {
1190 avatar
.data
= (gchar
*) data
;
1193 DEBUG ("Setting avatar on connection %s",
1194 tp_proxy_get_object_path (TP_PROXY (priv
->connection
)));
1196 tp_cli_connection_interface_avatars_call_set_avatar (priv
->connection
,
1200 tp_contact_factory_set_avatar_cb
,
1202 G_OBJECT (tp_factory
));
1204 DEBUG ("Clearing avatar on connection %s",
1205 tp_proxy_get_object_path (TP_PROXY (priv
->connection
)));
1207 tp_cli_connection_interface_avatars_call_clear_avatar (priv
->connection
,
1209 tp_contact_factory_clear_avatar_cb
,
1211 G_OBJECT (tp_factory
));
1216 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory
*tp_factory
,
1217 GHashTable
*location
)
1219 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
1221 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory
));
1223 DEBUG ("Setting location");
1225 tp_cli_connection_interface_location_call_set_location (priv
->connection
,
1228 tp_contact_factory_set_location_cb
,
1230 G_OBJECT (tp_factory
));
1234 tp_contact_factory_get_property (GObject
*object
,
1239 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (object
);
1242 case PROP_CONNECTION
:
1243 g_value_set_object (value
, priv
->connection
);
1245 case PROP_MIME_TYPES
:
1246 g_value_set_boxed (value
, priv
->avatar_mime_types
);
1248 case PROP_MIN_WIDTH
:
1249 g_value_set_uint (value
, priv
->avatar_min_width
);
1251 case PROP_MIN_HEIGHT
:
1252 g_value_set_uint (value
, priv
->avatar_min_height
);
1254 case PROP_MAX_WIDTH
:
1255 g_value_set_uint (value
, priv
->avatar_max_width
);
1257 case PROP_MAX_HEIGHT
:
1258 g_value_set_uint (value
, priv
->avatar_max_height
);
1261 g_value_set_uint (value
, priv
->avatar_max_size
);
1264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
1270 tp_contact_factory_set_property (GObject
*object
,
1272 const GValue
*value
,
1275 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (object
);
1278 case PROP_CONNECTION
:
1279 priv
->connection
= g_value_dup_object (value
);
1282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
1288 tp_contact_factory_finalize (GObject
*object
)
1290 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (object
);
1293 DEBUG ("Finalized: %p", object
);
1295 for (l
= priv
->contacts
; l
; l
= l
->next
) {
1296 g_object_weak_unref (G_OBJECT (l
->data
),
1297 tp_contact_factory_weak_notify
,
1301 g_list_free (priv
->contacts
);
1303 g_object_unref (priv
->connection
);
1305 g_strfreev (priv
->avatar_mime_types
);
1307 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class
)->finalize (object
);
1311 connection_ready_cb (TpConnection
*connection
,
1312 const GError
*error
,
1315 EmpathyTpContactFactory
*tp_factory
= EMPATHY_TP_CONTACT_FACTORY (user_data
);
1316 EmpathyTpContactFactoryPriv
*priv
= GET_PRIV (tp_factory
);
1321 /* FIXME: This should be moved to TpContact */
1322 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv
->connection
,
1323 tp_contact_factory_avatar_updated_cb
,
1325 G_OBJECT (tp_factory
),
1327 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv
->connection
,
1328 tp_contact_factory_avatar_retrieved_cb
,
1330 G_OBJECT (tp_factory
),
1332 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv
->connection
,
1333 tp_contact_factory_capabilities_changed_cb
,
1335 G_OBJECT (tp_factory
),
1339 tp_cli_connection_interface_location_connect_to_location_updated (priv
->connection
,
1340 tp_contact_factory_location_updated_cb
,
1342 G_OBJECT (tp_factory
),
1346 tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv
->connection
,
1348 tp_contact_factory_got_avatar_requirements_cb
,
1350 G_OBJECT (tp_factory
));
1352 tp_cli_dbus_properties_call_get (priv
->connection
, -1,
1353 TP_IFACE_CONNECTION_INTERFACE_REQUESTS
,
1354 "RequestableChannelClasses",
1355 get_requestable_channel_classes_cb
, NULL
, NULL
,
1356 G_OBJECT (tp_factory
));
1359 g_object_unref (tp_factory
);
1363 tp_contact_factory_constructor (GType type
,
1365 GObjectConstructParam
*props
)
1367 GObject
*tp_factory
;
1368 EmpathyTpContactFactoryPriv
*priv
;
1370 tp_factory
= G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class
)->constructor (type
, n_props
, props
);
1371 priv
= GET_PRIV (tp_factory
);
1373 /* Ensure to keep the self object alive while the call_when_ready is
1375 g_object_ref (tp_factory
);
1376 tp_connection_call_when_ready (priv
->connection
, connection_ready_cb
,
1383 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass
*klass
)
1385 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
1387 object_class
->finalize
= tp_contact_factory_finalize
;
1388 object_class
->constructor
= tp_contact_factory_constructor
;
1389 object_class
->get_property
= tp_contact_factory_get_property
;
1390 object_class
->set_property
= tp_contact_factory_set_property
;
1392 g_object_class_install_property (object_class
,
1394 g_param_spec_object ("connection",
1395 "Factory's Connection",
1396 "The connection associated with the factory",
1399 G_PARAM_CONSTRUCT_ONLY
|
1400 G_PARAM_STATIC_STRINGS
));
1401 g_object_class_install_property (object_class
,
1403 g_param_spec_boxed ("avatar-mime-types",
1404 "Supported MIME types for avatars",
1405 "Types of images that may be set as "
1406 "avatars on this connection.",
1409 G_PARAM_STATIC_STRINGS
));
1410 g_object_class_install_property (object_class
,
1412 g_param_spec_uint ("avatar-min-width",
1413 "Minimum width for avatars",
1414 "Minimum width of avatar that may be set.",
1419 G_PARAM_STATIC_STRINGS
));
1420 g_object_class_install_property (object_class
,
1422 g_param_spec_uint ("avatar-min-height",
1423 "Minimum height for avatars",
1424 "Minimum height of avatar that may be set.",
1429 G_PARAM_STATIC_STRINGS
));
1430 g_object_class_install_property (object_class
,
1432 g_param_spec_uint ("avatar-max-width",
1433 "Maximum width for avatars",
1434 "Maximum width of avatar that may be set "
1435 "or 0 if there is no maximum.",
1440 G_PARAM_STATIC_STRINGS
));
1441 g_object_class_install_property (object_class
,
1443 g_param_spec_uint ("avatar-max-height",
1444 "Maximum height for avatars",
1445 "Maximum height of avatar that may be set "
1446 "or 0 if there is no maximum.",
1451 G_PARAM_STATIC_STRINGS
));
1452 g_object_class_install_property (object_class
,
1454 g_param_spec_uint ("avatar-max-size",
1455 "Maximum size for avatars in bytes",
1456 "Maximum file size of avatar that may be "
1457 "set or 0 if there is no maximum.",
1462 G_PARAM_STATIC_STRINGS
));
1465 g_type_class_add_private (object_class
, sizeof (EmpathyTpContactFactoryPriv
));
1469 empathy_tp_contact_factory_init (EmpathyTpContactFactory
*tp_factory
)
1471 EmpathyTpContactFactoryPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (tp_factory
,
1472 EMPATHY_TYPE_TP_CONTACT_FACTORY
, EmpathyTpContactFactoryPriv
);
1474 tp_factory
->priv
= priv
;
1475 priv
->can_request_ft
= FALSE
;
1476 priv
->can_request_st
= FALSE
;
1479 static GHashTable
*factories
= NULL
;
1482 tp_contact_factory_connection_invalidated_cb (TpProxy
*connection
,
1488 DEBUG ("Message: %s", message
);
1489 g_hash_table_remove (factories
, connection
);
1493 tp_contact_factory_connection_weak_notify_cb (gpointer connection
,
1494 GObject
*where_the_object_was
)
1496 g_hash_table_remove (factories
, connection
);
1500 tp_contact_factory_remove_connection (gpointer connection
)
1502 g_signal_handlers_disconnect_by_func (connection
,
1503 tp_contact_factory_connection_invalidated_cb
, NULL
);
1504 g_object_unref (connection
);
1507 EmpathyTpContactFactory
*
1508 empathy_tp_contact_factory_dup_singleton (TpConnection
*connection
)
1510 EmpathyTpContactFactory
*tp_factory
;
1512 g_return_val_if_fail (TP_IS_CONNECTION (connection
), NULL
);
1514 if (factories
== NULL
) {
1515 factories
= g_hash_table_new_full (empathy_proxy_hash
,
1516 empathy_proxy_equal
,
1517 tp_contact_factory_remove_connection
,
1521 tp_factory
= g_hash_table_lookup (factories
, connection
);
1522 if (tp_factory
== NULL
) {
1523 tp_factory
= g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY
,
1524 "connection", connection
,
1526 g_hash_table_insert (factories
, g_object_ref (connection
),
1528 g_object_weak_ref (G_OBJECT (tp_factory
),
1529 tp_contact_factory_connection_weak_notify_cb
,
1531 g_signal_connect (connection
, "invalidated",
1532 G_CALLBACK (tp_contact_factory_connection_invalidated_cb
),
1535 g_object_ref (tp_factory
);