1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 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/connection.h>
28 #include <telepathy-glib/gtypes.h>
29 #include <libmissioncontrol/mission-control.h>
31 #include <extensions/extensions.h>
33 #include "empathy-tp-contact-factory.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
37 #include "empathy-debug.h"
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
43 TpConnection *connection;
50 gchar **avatar_mime_types;
51 guint avatar_min_width;
52 guint avatar_min_height;
53 guint avatar_max_width;
54 guint avatar_max_height;
55 guint avatar_max_size;
56 gboolean can_request_ft;
57 } EmpathyTpContactFactoryPriv;
59 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
74 static EmpathyContact *
75 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
78 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
81 for (l = priv->contacts; l; l = l->next) {
82 if (empathy_contact_get_handle (l->data) == handle) {
90 static EmpathyContact *
91 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
94 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
97 for (l = priv->contacts; l; l = l->next) {
98 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
107 tp_contact_factory_weak_notify (gpointer data,
108 GObject *where_the_object_was)
110 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
112 DEBUG ("Remove finalized contact %p", where_the_object_was);
114 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
118 tp_contact_factory_presences_table_foreach (const gchar *state_str,
119 GHashTable *presences_table,
120 EmpathyContact *contact)
122 const GValue *message;
123 const gchar *message_str = NULL;
125 empathy_contact_set_presence (contact,
126 empathy_presence_from_str (state_str));
128 message = g_hash_table_lookup (presences_table, "message");
130 message_str = g_value_get_string (message);
133 if (!G_STR_EMPTY (message_str)) {
134 empathy_contact_set_presence_message (contact, message_str);
136 empathy_contact_set_presence_message (contact, NULL);
141 tp_contact_factory_parse_presence_foreach (guint handle,
142 GValueArray *presence_struct,
143 EmpathyTpContactFactory *tp_factory)
145 GHashTable *presences_table;
146 EmpathyContact *contact;
148 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
153 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
155 g_hash_table_foreach (presences_table,
156 (GHFunc) tp_contact_factory_presences_table_foreach,
159 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
160 empathy_contact_get_id (contact),
162 empathy_contact_get_presence_message (contact),
163 empathy_contact_get_presence (contact));
167 tp_contact_factory_get_presence_cb (TpConnection *connection,
168 GHashTable *handle_table,
174 DEBUG ("Error getting presence: %s", error->message);
175 if (error->domain == TP_DBUS_ERRORS &&
176 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
177 guint *handles = user_data;
179 /* We have no presence iface, set default presence
181 while (*handles != 0) {
182 EmpathyContact *contact;
184 contact = tp_contact_factory_find_by_handle (
185 (EmpathyTpContactFactory*) tp_factory,
188 empathy_contact_set_presence (contact,
189 MC_PRESENCE_AVAILABLE);
199 g_hash_table_foreach (handle_table,
200 (GHFunc) tp_contact_factory_parse_presence_foreach,
201 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
205 tp_contact_factory_presence_update_cb (TpConnection *connection,
206 GHashTable *handle_table,
210 g_hash_table_foreach (handle_table,
211 (GHFunc) tp_contact_factory_parse_presence_foreach,
212 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
216 tp_contact_factory_set_aliases_cb (TpConnection *connection,
222 DEBUG ("Error setting alias: %s", error->message);
227 tp_contact_factory_request_aliases_cb (TpConnection *connection,
228 const gchar **contact_names,
233 guint *handles = user_data;
238 DEBUG ("Error requesting aliases: %s", error->message);
240 /* If we failed to get alias set it to NULL, like that if
241 * someone is waiting for the name to be ready it won't wait
243 while (*handles != 0) {
244 EmpathyContact *contact;
246 contact = tp_contact_factory_find_by_handle (
247 (EmpathyTpContactFactory*) tp_factory,
250 empathy_contact_set_name (contact, NULL);
258 for (name = contact_names; *name; name++) {
259 EmpathyContact *contact;
261 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
267 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
268 empathy_contact_get_id (contact),
269 empathy_contact_get_handle (contact),
272 empathy_contact_set_name (contact, *name);
279 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
280 const GPtrArray *renamed_handlers,
282 GObject *weak_object)
284 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
287 for (i = 0; renamed_handlers->len > i; i++) {
290 GValueArray *renamed_struct;
291 EmpathyContact *contact;
293 renamed_struct = g_ptr_array_index (renamed_handlers, i);
294 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
295 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
296 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
299 /* We don't know this contact, skip */
303 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
304 empathy_contact_get_id (contact),
307 empathy_contact_set_name (contact, alias);
312 tp_contact_factory_set_avatar_cb (TpConnection *connection,
319 DEBUG ("Error setting avatar: %s", error->message);
324 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
330 DEBUG ("Error clearing avatar: %s", error->message);
335 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
338 const GArray *avatar_data,
339 const gchar *mime_type,
343 EmpathyContact *contact;
345 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
351 DEBUG ("Avatar retrieved for contact %s (%d)",
352 empathy_contact_get_id (contact),
355 empathy_contact_load_avatar_data (contact,
363 tp_contact_factory_request_avatars_cb (TpConnection *connection,
369 DEBUG ("Error requesting avatars: %s", error->message);
374 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
378 EmpathyContact *contact;
379 EmpathyAvatar *avatar;
381 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
386 /* Check if we have an avatar */
387 if (G_STR_EMPTY (token)) {
388 empathy_contact_set_avatar (contact, NULL);
392 /* Check if the avatar changed */
393 avatar = empathy_contact_get_avatar (contact);
394 if (avatar && !tp_strdiff (avatar->token, token)) {
398 /* The avatar changed, search the new one in the cache */
399 if (empathy_contact_load_avatar_cache (contact, token)) {
400 /* Got from cache, use it */
404 /* Avatar is not up-to-date, we have to request it. */
409 EmpathyTpContactFactory *tp_factory;
414 tp_contact_factory_avatar_tokens_foreach (gpointer key,
418 TokensData *data = user_data;
419 const gchar *token = value;
420 guint handle = GPOINTER_TO_UINT (key);
422 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
424 g_array_append_val (data->handles, handle);
429 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
438 DEBUG ("Error getting known avatars tokens: %s", error->message);
442 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
443 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
444 g_hash_table_foreach (tokens,
445 tp_contact_factory_avatar_tokens_foreach,
448 DEBUG ("Got %d tokens, need to request %d avatars",
449 g_hash_table_size (tokens), data.handles->len);
451 /* Request needed avatars */
452 if (data.handles->len > 0) {
453 tp_cli_connection_interface_avatars_call_request_avatars (connection,
456 tp_contact_factory_request_avatars_cb,
461 g_array_free (data.handles, TRUE);
465 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
467 const gchar *new_token,
473 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
474 handle, new_token)) {
475 /* Avatar was cached, nothing to do */
479 DEBUG ("Need to request avatar for token %s", new_token);
481 handles = g_array_new (FALSE, FALSE, sizeof (guint));
482 g_array_append_val (handles, handle);
484 tp_cli_connection_interface_avatars_call_request_avatars (connection,
487 tp_contact_factory_request_avatars_cb,
490 g_array_free (handles, TRUE);
494 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
496 const gchar *channel_type,
500 EmpathyContact *contact;
501 EmpathyCapabilities capabilities;
503 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
508 capabilities = empathy_contact_get_capabilities (contact);
509 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
511 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
512 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
513 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
514 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
515 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
517 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
518 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
522 DEBUG ("Changing capabilities for contact %s (%d) to %d",
523 empathy_contact_get_id (contact),
524 empathy_contact_get_handle (contact),
527 empathy_contact_set_capabilities (contact, capabilities);
531 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
532 const GPtrArray *capabilities,
535 GObject *weak_object)
537 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
541 DEBUG ("Error getting capabilities: %s", error->message);
542 /* FIXME Should set the capabilities of the contacts for which this request
543 * originated to NONE */
547 for (i = 0; i < capabilities->len; i++) {
550 const gchar *channel_type;
554 values = g_ptr_array_index (capabilities, i);
555 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
556 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
557 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
558 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
560 tp_contact_factory_update_capabilities (tp_factory,
569 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
570 const GPtrArray *capabilities,
572 GObject *weak_object)
574 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
577 for (i = 0; i < capabilities->len; i++) {
580 const gchar *channel_type;
584 values = g_ptr_array_index (capabilities, i);
585 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
586 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
587 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
588 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
590 tp_contact_factory_update_capabilities (tp_factory,
599 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
600 const GArray *handles)
602 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
605 g_return_if_fail (priv->ready);
607 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
608 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
609 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
612 tp_contact_factory_get_presence_cb,
614 G_OBJECT (tp_factory));
616 /* FIXME: Sometimes the dbus call timesout because CM takes
617 * too much time to request all aliases from the server,
618 * that's why we increase the timeout here. See fd.o bug #14795 */
619 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
620 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
621 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
624 tp_contact_factory_request_aliases_cb,
626 G_OBJECT (tp_factory));
628 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
631 tp_contact_factory_get_known_avatar_tokens_cb,
633 G_OBJECT (tp_factory));
635 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
638 tp_contact_factory_get_capabilities_cb,
640 G_OBJECT (tp_factory));
644 tp_contact_factory_list_free (gpointer data)
648 g_list_foreach (l, (GFunc) g_object_unref, NULL);
653 tp_contact_factory_request_handles_cb (TpConnection *connection,
654 const GArray *handles,
659 GList *contacts = user_data;
664 DEBUG ("Failed to request handles: %s", error->message);
668 for (l = contacts; l; l = l->next) {
671 handle = g_array_index (handles, guint, i);
672 empathy_contact_set_handle (l->data, handle);
677 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
682 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
689 GList *contacts = user_data;
693 DEBUG ("Failed to inspect handles: %s", error->message);
698 for (l = contacts; l; l = l->next) {
699 empathy_contact_set_id (l->data, *id);
705 tp_contact_factory_disconnect_contact_foreach (gpointer data,
708 EmpathyContact *contact = data;
710 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
711 empathy_contact_set_handle (contact, 0);
715 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
717 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
719 DEBUG ("Connection invalidated");
721 g_object_unref (priv->connection);
722 priv->connection = NULL;
724 g_object_notify (G_OBJECT (tp_factory), "ready");
727 g_list_foreach (priv->contacts,
728 tp_contact_factory_disconnect_contact_foreach,
733 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
735 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
737 GArray *handle_needed;
739 GList *handle_needed_contacts = NULL;
740 GList *id_needed_contacts = NULL;
742 DEBUG ("Connection ready");
745 g_object_notify (G_OBJECT (tp_factory), "ready");
747 /* Connect signals */
748 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
749 tp_contact_factory_aliases_changed_cb,
751 G_OBJECT (tp_factory),
753 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
754 tp_contact_factory_avatar_updated_cb,
756 G_OBJECT (tp_factory),
758 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
759 tp_contact_factory_avatar_retrieved_cb,
761 G_OBJECT (tp_factory),
763 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
764 tp_contact_factory_presence_update_cb,
766 G_OBJECT (tp_factory),
768 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
769 tp_contact_factory_capabilities_changed_cb,
771 G_OBJECT (tp_factory),
774 /* Request needed info for all existing contacts */
775 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
776 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
777 for (l = priv->contacts; l; l = l->next) {
778 EmpathyContact *contact;
783 handle = empathy_contact_get_handle (contact);
784 id = empathy_contact_get_id (contact);
786 g_assert (!G_STR_EMPTY (id));
787 g_array_append_val (handle_needed, id);
788 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
789 g_object_ref (contact));
791 if (G_STR_EMPTY (id)) {
792 g_array_append_val (id_needed, handle);
793 id_needed_contacts = g_list_prepend (id_needed_contacts,
794 g_object_ref (contact));
797 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
798 id_needed_contacts = g_list_reverse (id_needed_contacts);
800 tp_cli_connection_call_request_handles (priv->connection,
802 TP_HANDLE_TYPE_CONTACT,
803 (const gchar**) handle_needed->data,
804 tp_contact_factory_request_handles_cb,
805 handle_needed_contacts, tp_contact_factory_list_free,
806 G_OBJECT (tp_factory));
808 tp_cli_connection_call_inspect_handles (priv->connection,
810 TP_HANDLE_TYPE_CONTACT,
812 tp_contact_factory_inspect_handles_cb,
813 id_needed_contacts, tp_contact_factory_list_free,
814 G_OBJECT (tp_factory));
816 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
819 g_array_free (handle_needed, TRUE);
820 g_array_free (id_needed, TRUE);
824 get_requestable_channel_classes_cb (TpProxy *connection,
828 GObject *weak_object)
830 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (user_data);
831 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
837 DEBUG ("Error: %s", error->message);
838 tp_contact_factory_ready (self);
842 classes = g_value_get_boxed (value);
844 for (i = 0; i < classes->len; i++)
847 GValue *chan_type, *handle_type;
848 GHashTable *fixed_prop;
850 g_value_init (&class, TP_STRUCT_TYPE_REQUESTABLE_CHANNEL_CLASS);
851 g_value_set_static_boxed (&class, g_ptr_array_index (classes, i));
853 dbus_g_type_struct_get (&class,
857 chan_type = g_hash_table_lookup (fixed_prop,
858 TP_IFACE_CHANNEL ".ChannelType");
859 if (chan_type == NULL || tp_strdiff (g_value_get_string (chan_type),
860 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
863 handle_type = g_hash_table_lookup (fixed_prop,
864 TP_IFACE_CHANNEL ".TargetHandleType");
865 if (handle_type == NULL || g_value_get_uint (handle_type)
866 != TP_HANDLE_TYPE_CONTACT)
869 /* We can request file transfer channel to contacts. */
870 priv->can_request_ft = TRUE;
875 tp_contact_factory_ready (self);
879 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
880 const gchar **mime_types,
890 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
893 DEBUG ("Failed to get avatar requirements: %s", error->message);
894 /* We'll just leave avatar_mime_types as NULL; the
895 * avatar-setting code can use this as a signal that you can't
899 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
900 priv->avatar_min_width = min_width;
901 priv->avatar_min_height = min_height;
902 priv->avatar_max_width = max_width;
903 priv->avatar_max_height = max_height;
904 priv->avatar_max_size = max_size;
907 /* Can we request file transfer channels? */
908 tp_cli_dbus_properties_call_get (priv->connection, -1,
909 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "RequestableChannelClasses",
910 get_requestable_channel_classes_cb, tp_factory, NULL,
911 G_OBJECT (tp_factory));
915 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
921 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
924 DEBUG ("Failed to get self handles: %s", error->message);
928 empathy_contact_set_handle (priv->user, handle);
930 /* Get avatar requirements for this connection */
931 tp_cli_connection_interface_avatars_call_get_avatar_requirements (
934 tp_contact_factory_got_avatar_requirements_cb,
940 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
942 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
944 /* Get our own handle */
945 tp_cli_connection_call_get_self_handle (priv->connection,
947 tp_contact_factory_got_self_handle_cb,
949 G_OBJECT (tp_factory));
953 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
955 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
956 gboolean connection_ready;
958 if (priv->connection) {
959 /* We already have our connection object */
963 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
964 if (!priv->connection) {
968 /* We got a new connection, wait for it to be ready */
969 g_signal_connect_swapped (priv->connection, "invalidated",
970 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
973 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
974 if (connection_ready) {
975 tp_contact_factory_connection_ready_cb (tp_factory);
977 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
978 G_CALLBACK (tp_contact_factory_connection_ready_cb),
984 tp_contact_factory_status_changed_cb (MissionControl *mc,
985 TpConnectionStatus status,
987 TpConnectionStatusReason reason,
988 const gchar *unique_name,
989 EmpathyTpContactFactory *tp_factory)
991 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
994 account = mc_account_lookup (unique_name);
995 if (account && empathy_account_equal (account, priv->account)) {
996 tp_contact_factory_status_updated (tp_factory);
998 g_object_unref (account);
1002 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
1003 EmpathyContact *contact)
1005 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1007 g_object_weak_ref (G_OBJECT (contact),
1008 tp_contact_factory_weak_notify,
1010 priv->contacts = g_list_prepend (priv->contacts, contact);
1012 DEBUG ("Contact added: %s (%d)",
1013 empathy_contact_get_id (contact),
1014 empathy_contact_get_handle (contact));
1018 tp_contact_factory_hold_handles_cb (TpConnection *connection,
1019 const GError *error,
1021 GObject *tp_factory)
1024 DEBUG ("Failed to hold handles: %s", error->message);
1029 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
1031 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1033 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1035 return g_object_ref (priv->user);
1039 contact_created (EmpathyTpContactFactory *self,
1040 EmpathyContact *contact)
1042 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
1044 if (priv->can_request_ft)
1046 /* Set the FT capability */
1047 /* FIXME: We should use the futur ContactCapabilities interface */
1048 EmpathyCapabilities caps;
1050 caps = empathy_contact_get_capabilities (contact);
1051 caps |= EMPATHY_CAPABILITIES_FT;
1053 empathy_contact_set_capabilities (contact, caps);
1056 tp_contact_factory_add_contact (self, contact);
1060 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
1063 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1064 EmpathyContact *contact;
1066 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1067 g_return_val_if_fail (id != NULL, NULL);
1069 /* Check if the contact already exists */
1070 contact = tp_contact_factory_find_by_id (tp_factory, id);
1072 return g_object_ref (contact);
1075 /* Create new contact */
1076 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1077 "account", priv->account,
1080 contact_created (tp_factory, contact);
1083 const gchar *contact_ids[] = {id, NULL};
1086 contacts = g_list_prepend (NULL, g_object_ref (contact));
1087 tp_cli_connection_call_request_handles (priv->connection,
1089 TP_HANDLE_TYPE_CONTACT,
1091 tp_contact_factory_request_handles_cb,
1092 contacts, tp_contact_factory_list_free,
1093 G_OBJECT (tp_factory));
1100 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1103 EmpathyContact *contact;
1107 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1109 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1110 g_array_append_val (handles, handle);
1112 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1113 g_array_free (handles, TRUE);
1115 contact = contacts ? contacts->data : NULL;
1116 g_list_free (contacts);
1122 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1123 const GArray *handles)
1125 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1126 GList *contacts = NULL;
1127 GArray *new_handles;
1128 GList *new_contacts = NULL;
1131 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1132 g_return_val_if_fail (handles != NULL, NULL);
1134 /* Search all contacts we already have */
1135 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1136 for (i = 0; i < handles->len; i++) {
1137 EmpathyContact *contact;
1140 handle = g_array_index (handles, guint, i);
1145 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1147 contacts = g_list_prepend (contacts, g_object_ref (contact));
1149 g_array_append_val (new_handles, handle);
1153 if (new_handles->len == 0) {
1154 g_array_free (new_handles, TRUE);
1158 /* Create new contacts */
1159 for (i = 0; i < new_handles->len; i++) {
1160 EmpathyContact *contact;
1163 handle = g_array_index (new_handles, guint, i);
1165 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1166 "account", priv->account,
1169 contact_created (tp_factory, contact);
1170 contacts = g_list_prepend (contacts, contact);
1171 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1173 new_contacts = g_list_reverse (new_contacts);
1176 /* Get the IDs of all new handles */
1177 tp_cli_connection_call_inspect_handles (priv->connection,
1179 TP_HANDLE_TYPE_CONTACT,
1181 tp_contact_factory_inspect_handles_cb,
1182 new_contacts, tp_contact_factory_list_free,
1183 G_OBJECT (tp_factory));
1185 /* Hold all new handles. */
1186 /* FIXME: Should be unholded when removed from the factory */
1187 tp_cli_connection_call_hold_handles (priv->connection,
1189 TP_HANDLE_TYPE_CONTACT,
1191 tp_contact_factory_hold_handles_cb,
1193 G_OBJECT (tp_factory));
1195 tp_contact_factory_request_everything (tp_factory, new_handles);
1198 g_array_free (new_handles, TRUE);
1204 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1205 EmpathyContact *contact,
1208 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1209 GHashTable *new_alias;
1212 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1213 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1214 g_return_if_fail (priv->ready);
1215 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1218 handle = empathy_contact_get_handle (contact);
1220 DEBUG ("Setting alias for contact %s (%d) to %s",
1221 empathy_contact_get_id (contact),
1224 new_alias = g_hash_table_new_full (g_direct_hash,
1229 g_hash_table_insert (new_alias,
1230 GUINT_TO_POINTER (handle),
1233 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1236 tp_contact_factory_set_aliases_cb,
1238 G_OBJECT (tp_factory));
1240 g_hash_table_destroy (new_alias);
1244 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1247 const gchar *mime_type)
1249 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1251 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1252 g_return_if_fail (priv->ready);
1254 if (data && size > 0 && size < G_MAXUINT) {
1257 avatar.data = (gchar*) data;
1260 DEBUG ("Setting avatar on account %s",
1261 mc_account_get_unique_name (priv->account));
1263 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1267 tp_contact_factory_set_avatar_cb,
1269 G_OBJECT (tp_factory));
1271 DEBUG ("Clearing avatar on account %s",
1272 mc_account_get_unique_name (priv->account));
1274 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1276 tp_contact_factory_clear_avatar_cb,
1278 G_OBJECT (tp_factory));
1283 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1285 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1287 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1293 tp_contact_factory_get_property (GObject *object,
1298 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1302 g_value_set_object (value, priv->account);
1305 g_value_set_boolean (value, priv->ready);
1307 case PROP_MIME_TYPES:
1308 g_value_set_boxed (value, priv->avatar_mime_types);
1310 case PROP_MIN_WIDTH:
1311 g_value_set_uint (value, priv->avatar_min_width);
1313 case PROP_MIN_HEIGHT:
1314 g_value_set_uint (value, priv->avatar_min_height);
1316 case PROP_MAX_WIDTH:
1317 g_value_set_uint (value, priv->avatar_max_width);
1319 case PROP_MAX_HEIGHT:
1320 g_value_set_uint (value, priv->avatar_max_height);
1323 g_value_set_uint (value, priv->avatar_max_size);
1326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1332 tp_contact_factory_set_property (GObject *object,
1334 const GValue *value,
1337 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1341 priv->account = g_object_ref (g_value_get_object (value));
1344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1350 tp_contact_factory_finalize (GObject *object)
1352 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1355 DEBUG ("Finalized: %p (%s)", object,
1356 mc_account_get_normalized_name (priv->account));
1358 empathy_disconnect_account_status_changed (priv->token);
1360 for (l = priv->contacts; l; l = l->next) {
1361 g_object_weak_unref (G_OBJECT (l->data),
1362 tp_contact_factory_weak_notify,
1366 g_list_free (priv->contacts);
1367 g_object_unref (priv->mc);
1368 g_object_unref (priv->account);
1369 g_object_unref (priv->user);
1371 if (priv->connection) {
1372 g_signal_handlers_disconnect_by_func (priv->connection,
1373 tp_contact_factory_connection_invalidated_cb,
1375 g_object_unref (priv->connection);
1378 g_strfreev (priv->avatar_mime_types);
1380 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1384 tp_contact_factory_constructor (GType type,
1386 GObjectConstructParam *props)
1388 GObject *tp_factory;
1389 EmpathyTpContactFactoryPriv *priv;
1391 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1392 priv = GET_PRIV (tp_factory);
1394 priv->ready = FALSE;
1395 priv->user = empathy_contact_new (priv->account);
1396 empathy_contact_set_is_user (priv->user, TRUE);
1397 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1398 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1404 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1406 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1408 object_class->finalize = tp_contact_factory_finalize;
1409 object_class->constructor = tp_contact_factory_constructor;
1410 object_class->get_property = tp_contact_factory_get_property;
1411 object_class->set_property = tp_contact_factory_set_property;
1413 g_object_class_install_property (object_class,
1415 g_param_spec_object ("account",
1416 "Factory's Account",
1417 "The account associated with the factory",
1420 G_PARAM_CONSTRUCT_ONLY |
1421 G_PARAM_STATIC_STRINGS));
1422 g_object_class_install_property (object_class,
1424 g_param_spec_boolean ("ready",
1425 "Whether the factory is ready",
1426 "TRUE once the factory is ready to be used",
1429 G_PARAM_STATIC_STRINGS));
1430 g_object_class_install_property (object_class,
1432 g_param_spec_boxed ("avatar-mime-types",
1433 "Supported MIME types for avatars",
1434 "Types of images that may be set as "
1435 "avatars on this connection. Only valid "
1436 "once 'ready' becomes TRUE.",
1439 G_PARAM_STATIC_STRINGS));
1440 g_object_class_install_property (object_class,
1442 g_param_spec_uint ("avatar-min-width",
1443 "Minimum width for avatars",
1444 "Minimum width of avatar that may be set. "
1445 "Only valid once 'ready' becomes TRUE.",
1450 G_PARAM_STATIC_STRINGS));
1451 g_object_class_install_property (object_class,
1453 g_param_spec_uint ("avatar-min-height",
1454 "Minimum height for avatars",
1455 "Minimum height of avatar that may be set. "
1456 "Only valid once 'ready' becomes TRUE.",
1461 G_PARAM_STATIC_STRINGS));
1462 g_object_class_install_property (object_class,
1464 g_param_spec_uint ("avatar-max-width",
1465 "Maximum width for avatars",
1466 "Maximum width of avatar that may be set "
1467 "or 0 if there is no maximum. "
1468 "Only valid once 'ready' becomes TRUE.",
1473 G_PARAM_STATIC_STRINGS));
1474 g_object_class_install_property (object_class,
1476 g_param_spec_uint ("avatar-max-height",
1477 "Maximum height for avatars",
1478 "Maximum height of avatar that may be set "
1479 "or 0 if there is no maximum. "
1480 "Only valid once 'ready' becomes TRUE.",
1485 G_PARAM_STATIC_STRINGS));
1486 g_object_class_install_property (object_class,
1488 g_param_spec_uint ("avatar-max-size",
1489 "Maximum size for avatars in bytes",
1490 "Maximum file size of avatar that may be "
1491 "set or 0 if there is no maximum. "
1492 "Only valid once 'ready' becomes TRUE.",
1497 G_PARAM_STATIC_STRINGS));
1500 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1504 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1506 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1507 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1509 tp_factory->priv = priv;
1510 priv->mc = empathy_mission_control_new ();
1511 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1512 G_CALLBACK (tp_contact_factory_status_changed_cb),
1515 priv->can_request_ft = FALSE;
1518 EmpathyTpContactFactory *
1519 empathy_tp_contact_factory_new (McAccount *account)
1521 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,