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 <libmissioncontrol/mission-control.h>
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-utils.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
34 #include "empathy-debug.h"
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
40 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 } EmpathyTpContactFactoryPriv;
55 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
71 static void tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
72 const gchar **mime_types,
81 static void tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory);
84 static EmpathyContact *
85 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
88 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
91 for (l = priv->contacts; l; l = l->next) {
92 if (empathy_contact_get_handle (l->data) == handle) {
100 static EmpathyContact *
101 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
104 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
107 for (l = priv->contacts; l; l = l->next) {
108 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
117 tp_contact_factory_weak_notify (gpointer data,
118 GObject *where_the_object_was)
120 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
122 DEBUG ("Remove finalized contact %p", where_the_object_was);
124 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
128 tp_contact_factory_presences_table_foreach (const gchar *state_str,
129 GHashTable *presences_table,
130 EmpathyContact *contact)
132 const GValue *message;
133 const gchar *message_str = NULL;
135 empathy_contact_set_presence (contact,
136 empathy_presence_from_str (state_str));
138 message = g_hash_table_lookup (presences_table, "message");
140 message_str = g_value_get_string (message);
143 if (!G_STR_EMPTY (message_str)) {
144 empathy_contact_set_presence_message (contact, message_str);
146 empathy_contact_set_presence_message (contact, NULL);
151 tp_contact_factory_parse_presence_foreach (guint handle,
152 GValueArray *presence_struct,
153 EmpathyTpContactFactory *tp_factory)
155 GHashTable *presences_table;
156 EmpathyContact *contact;
158 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
163 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
165 g_hash_table_foreach (presences_table,
166 (GHFunc) tp_contact_factory_presences_table_foreach,
169 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
170 empathy_contact_get_id (contact),
172 empathy_contact_get_presence_message (contact),
173 empathy_contact_get_presence (contact));
177 tp_contact_factory_get_presence_cb (TpConnection *connection,
178 GHashTable *handle_table,
184 DEBUG ("Error getting presence: %s", error->message);
185 if (error->domain == TP_DBUS_ERRORS &&
186 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
187 guint *handles = user_data;
189 /* We have no presence iface, set default presence
191 while (*handles != 0) {
192 EmpathyContact *contact;
194 contact = tp_contact_factory_find_by_handle (
195 (EmpathyTpContactFactory*) tp_factory,
198 empathy_contact_set_presence (contact,
199 MC_PRESENCE_AVAILABLE);
209 g_hash_table_foreach (handle_table,
210 (GHFunc) tp_contact_factory_parse_presence_foreach,
211 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
215 tp_contact_factory_presence_update_cb (TpConnection *connection,
216 GHashTable *handle_table,
220 g_hash_table_foreach (handle_table,
221 (GHFunc) tp_contact_factory_parse_presence_foreach,
222 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
226 tp_contact_factory_set_aliases_cb (TpConnection *connection,
232 DEBUG ("Error setting alias: %s", error->message);
237 tp_contact_factory_request_aliases_cb (TpConnection *connection,
238 const gchar **contact_names,
243 guint *handles = user_data;
248 DEBUG ("Error requesting aliases: %s", error->message);
250 /* If we failed to get alias set it to NULL, like that if
251 * someone is waiting for the name to be ready it won't wait
253 while (*handles != 0) {
254 EmpathyContact *contact;
256 contact = tp_contact_factory_find_by_handle (
257 (EmpathyTpContactFactory*) tp_factory,
260 empathy_contact_set_name (contact, NULL);
268 for (name = contact_names; *name; name++) {
269 EmpathyContact *contact;
271 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
277 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
278 empathy_contact_get_id (contact),
279 empathy_contact_get_handle (contact),
282 empathy_contact_set_name (contact, *name);
289 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
290 const GPtrArray *renamed_handlers,
292 GObject *weak_object)
294 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
297 for (i = 0; renamed_handlers->len > i; i++) {
300 GValueArray *renamed_struct;
301 EmpathyContact *contact;
303 renamed_struct = g_ptr_array_index (renamed_handlers, i);
304 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
305 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
306 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
309 /* We don't know this contact, skip */
313 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
314 empathy_contact_get_id (contact),
317 empathy_contact_set_name (contact, alias);
322 tp_contact_factory_set_avatar_cb (TpConnection *connection,
329 DEBUG ("Error setting avatar: %s", error->message);
334 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
340 DEBUG ("Error clearing avatar: %s", error->message);
345 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
348 const GArray *avatar_data,
349 const gchar *mime_type,
353 EmpathyContact *contact;
355 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
361 DEBUG ("Avatar retrieved for contact %s (%d)",
362 empathy_contact_get_id (contact),
365 empathy_contact_load_avatar_data (contact,
373 tp_contact_factory_request_avatars_cb (TpConnection *connection,
379 DEBUG ("Error requesting avatars: %s", error->message);
384 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
388 EmpathyContact *contact;
389 EmpathyAvatar *avatar;
391 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
396 /* Check if we have an avatar */
397 if (G_STR_EMPTY (token)) {
398 empathy_contact_set_avatar (contact, NULL);
402 /* Check if the avatar changed */
403 avatar = empathy_contact_get_avatar (contact);
404 if (avatar && !tp_strdiff (avatar->token, token)) {
408 /* The avatar changed, search the new one in the cache */
409 if (empathy_contact_load_avatar_cache (contact, token)) {
410 /* Got from cache, use it */
414 /* Avatar is not up-to-date, we have to request it. */
419 EmpathyTpContactFactory *tp_factory;
424 tp_contact_factory_avatar_tokens_foreach (gpointer key,
428 TokensData *data = user_data;
429 const gchar *token = value;
430 guint handle = GPOINTER_TO_UINT (key);
432 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
434 g_array_append_val (data->handles, handle);
439 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
448 DEBUG ("Error getting known avatars tokens: %s", error->message);
452 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
453 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
454 g_hash_table_foreach (tokens,
455 tp_contact_factory_avatar_tokens_foreach,
458 DEBUG ("Got %d tokens, need to request %d avatars",
459 g_hash_table_size (tokens), data.handles->len);
461 /* Request needed avatars */
462 if (data.handles->len > 0) {
463 tp_cli_connection_interface_avatars_call_request_avatars (connection,
466 tp_contact_factory_request_avatars_cb,
471 g_array_free (data.handles, TRUE);
475 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
477 const gchar *new_token,
483 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
484 handle, new_token)) {
485 /* Avatar was cached, nothing to do */
489 DEBUG ("Need to request avatar for token %s", new_token);
491 handles = g_array_new (FALSE, FALSE, sizeof (guint));
492 g_array_append_val (handles, handle);
494 tp_cli_connection_interface_avatars_call_request_avatars (connection,
497 tp_contact_factory_request_avatars_cb,
500 g_array_free (handles, TRUE);
504 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
506 const gchar *channel_type,
510 EmpathyContact *contact;
511 EmpathyCapabilities capabilities;
513 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
518 capabilities = empathy_contact_get_capabilities (contact);
519 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
521 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
522 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
523 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
524 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
525 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
527 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
528 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
532 DEBUG ("Changing capabilities for contact %s (%d) to %d",
533 empathy_contact_get_id (contact),
534 empathy_contact_get_handle (contact),
537 empathy_contact_set_capabilities (contact, capabilities);
541 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
542 const GPtrArray *capabilities,
545 GObject *weak_object)
547 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
551 DEBUG ("Error getting capabilities: %s", error->message);
552 /* FIXME Should set the capabilities of the contacts for which this request
553 * originated to NONE */
557 for (i = 0; i < capabilities->len; i++) {
560 const gchar *channel_type;
564 values = g_ptr_array_index (capabilities, i);
565 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
566 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
567 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
568 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
570 tp_contact_factory_update_capabilities (tp_factory,
579 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
580 const GPtrArray *capabilities,
582 GObject *weak_object)
584 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
587 for (i = 0; i < capabilities->len; i++) {
590 const gchar *channel_type;
594 values = g_ptr_array_index (capabilities, i);
595 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
596 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
597 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
598 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
600 tp_contact_factory_update_capabilities (tp_factory,
609 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
610 const GArray *handles)
612 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
615 g_return_if_fail (priv->ready);
617 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
618 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
619 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
622 tp_contact_factory_get_presence_cb,
624 G_OBJECT (tp_factory));
626 /* FIXME: Sometimes the dbus call timesout because CM takes
627 * too much time to request all aliases from the server,
628 * that's why we increase the timeout here. See fd.o bug #14795 */
629 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
630 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
631 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
634 tp_contact_factory_request_aliases_cb,
636 G_OBJECT (tp_factory));
638 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
641 tp_contact_factory_get_known_avatar_tokens_cb,
643 G_OBJECT (tp_factory));
645 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
648 tp_contact_factory_get_capabilities_cb,
650 G_OBJECT (tp_factory));
654 tp_contact_factory_list_free (gpointer data)
658 g_list_foreach (l, (GFunc) g_object_unref, NULL);
663 tp_contact_factory_request_handles_cb (TpConnection *connection,
664 const GArray *handles,
669 GList *contacts = user_data;
674 DEBUG ("Failed to request handles: %s", error->message);
678 for (l = contacts; l; l = l->next) {
681 handle = g_array_index (handles, guint, i);
682 empathy_contact_set_handle (l->data, handle);
687 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
692 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
699 GList *contacts = user_data;
703 DEBUG ("Failed to inspect handles: %s", error->message);
708 for (l = contacts; l; l = l->next) {
709 empathy_contact_set_id (l->data, *id);
715 tp_contact_factory_disconnect_contact_foreach (gpointer data,
718 EmpathyContact *contact = data;
720 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
721 empathy_contact_set_handle (contact, 0);
725 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
727 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
729 DEBUG ("Connection invalidated");
731 g_object_unref (priv->connection);
732 priv->connection = NULL;
734 g_object_notify (G_OBJECT (tp_factory), "ready");
737 g_list_foreach (priv->contacts,
738 tp_contact_factory_disconnect_contact_foreach,
743 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
749 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
752 DEBUG ("Failed to get self handles: %s", error->message);
756 empathy_contact_set_handle (priv->user, handle);
758 /* Get avatar requirements for this connection */
759 tp_cli_connection_interface_avatars_call_get_avatar_requirements (
762 tp_contact_factory_got_avatar_requirements_cb,
768 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
769 const gchar **mime_types,
779 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
782 DEBUG ("Failed to get avatar requirements: %s", error->message);
783 /* We'll just leave avatar_mime_types as NULL; the
784 * avatar-setting code can use this as a signal that you can't
788 priv->avatar_mime_types = g_strdupv ((gchar **)mime_types);
789 priv->avatar_min_width = min_width;
790 priv->avatar_min_height = min_height;
791 priv->avatar_max_width = max_width;
792 priv->avatar_max_height = max_height;
793 priv->avatar_max_size = max_size;
796 tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
800 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
802 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
804 GArray *handle_needed;
806 GList *handle_needed_contacts = NULL;
807 GList *id_needed_contacts = NULL;
809 DEBUG ("Connection ready");
812 g_object_notify (G_OBJECT (tp_factory), "ready");
814 /* Connect signals */
815 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
816 tp_contact_factory_aliases_changed_cb,
818 G_OBJECT (tp_factory),
820 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
821 tp_contact_factory_avatar_updated_cb,
823 G_OBJECT (tp_factory),
825 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
826 tp_contact_factory_avatar_retrieved_cb,
828 G_OBJECT (tp_factory),
830 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
831 tp_contact_factory_presence_update_cb,
833 G_OBJECT (tp_factory),
835 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
836 tp_contact_factory_capabilities_changed_cb,
838 G_OBJECT (tp_factory),
841 /* Request needed info for all existing contacts */
842 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
843 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
844 for (l = priv->contacts; l; l = l->next) {
845 EmpathyContact *contact;
850 handle = empathy_contact_get_handle (contact);
851 id = empathy_contact_get_id (contact);
853 g_assert (!G_STR_EMPTY (id));
854 g_array_append_val (handle_needed, id);
855 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
856 g_object_ref (contact));
858 if (G_STR_EMPTY (id)) {
859 g_array_append_val (id_needed, handle);
860 id_needed_contacts = g_list_prepend (id_needed_contacts,
861 g_object_ref (contact));
864 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
865 id_needed_contacts = g_list_reverse (id_needed_contacts);
867 tp_cli_connection_call_request_handles (priv->connection,
869 TP_HANDLE_TYPE_CONTACT,
870 (const gchar**) handle_needed->data,
871 tp_contact_factory_request_handles_cb,
872 handle_needed_contacts, tp_contact_factory_list_free,
873 G_OBJECT (tp_factory));
875 tp_cli_connection_call_inspect_handles (priv->connection,
877 TP_HANDLE_TYPE_CONTACT,
879 tp_contact_factory_inspect_handles_cb,
880 id_needed_contacts, tp_contact_factory_list_free,
881 G_OBJECT (tp_factory));
883 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
886 g_array_free (handle_needed, TRUE);
887 g_array_free (id_needed, TRUE);
891 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
893 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
895 /* Get our own handle */
896 tp_cli_connection_call_get_self_handle (priv->connection,
898 tp_contact_factory_got_self_handle_cb,
900 G_OBJECT (tp_factory));
904 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
906 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
907 gboolean connection_ready;
909 if (priv->connection) {
910 /* We already have our connection object */
914 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
915 if (!priv->connection) {
919 /* We got a new connection, wait for it to be ready */
920 g_signal_connect_swapped (priv->connection, "invalidated",
921 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
924 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
925 if (connection_ready) {
926 tp_contact_factory_connection_ready_cb (tp_factory);
928 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
929 G_CALLBACK (tp_contact_factory_connection_ready_cb),
935 tp_contact_factory_status_changed_cb (MissionControl *mc,
936 TpConnectionStatus status,
938 TpConnectionStatusReason reason,
939 const gchar *unique_name,
940 EmpathyTpContactFactory *tp_factory)
942 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
945 account = mc_account_lookup (unique_name);
946 if (account && empathy_account_equal (account, priv->account)) {
947 tp_contact_factory_status_updated (tp_factory);
949 g_object_unref (account);
953 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
954 EmpathyContact *contact)
956 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
958 g_object_weak_ref (G_OBJECT (contact),
959 tp_contact_factory_weak_notify,
961 priv->contacts = g_list_prepend (priv->contacts, contact);
963 DEBUG ("Contact added: %s (%d)",
964 empathy_contact_get_id (contact),
965 empathy_contact_get_handle (contact));
969 tp_contact_factory_hold_handles_cb (TpConnection *connection,
975 DEBUG ("Failed to hold handles: %s", error->message);
980 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
982 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
984 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
986 return g_object_ref (priv->user);
990 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
993 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
994 EmpathyContact *contact;
996 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
997 g_return_val_if_fail (id != NULL, NULL);
999 /* Check if the contact already exists */
1000 contact = tp_contact_factory_find_by_id (tp_factory, id);
1002 return g_object_ref (contact);
1005 /* Create new contact */
1006 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1007 "account", priv->account,
1010 tp_contact_factory_add_contact (tp_factory, contact);
1013 const gchar *contact_ids[] = {id, NULL};
1016 contacts = g_list_prepend (NULL, g_object_ref (contact));
1017 tp_cli_connection_call_request_handles (priv->connection,
1019 TP_HANDLE_TYPE_CONTACT,
1021 tp_contact_factory_request_handles_cb,
1022 contacts, tp_contact_factory_list_free,
1023 G_OBJECT (tp_factory));
1030 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1033 EmpathyContact *contact;
1037 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1039 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1040 g_array_append_val (handles, handle);
1042 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1043 g_array_free (handles, TRUE);
1045 contact = contacts ? contacts->data : NULL;
1046 g_list_free (contacts);
1052 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1053 const GArray *handles)
1055 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1056 GList *contacts = NULL;
1057 GArray *new_handles;
1058 GList *new_contacts = NULL;
1061 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1062 g_return_val_if_fail (handles != NULL, NULL);
1064 /* Search all contacts we already have */
1065 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1066 for (i = 0; i < handles->len; i++) {
1067 EmpathyContact *contact;
1070 handle = g_array_index (handles, guint, i);
1075 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1077 contacts = g_list_prepend (contacts, g_object_ref (contact));
1079 g_array_append_val (new_handles, handle);
1083 if (new_handles->len == 0) {
1084 g_array_free (new_handles, TRUE);
1088 /* Create new contacts */
1089 for (i = 0; i < new_handles->len; i++) {
1090 EmpathyContact *contact;
1093 handle = g_array_index (new_handles, guint, i);
1095 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1096 "account", priv->account,
1099 tp_contact_factory_add_contact (tp_factory, contact);
1100 contacts = g_list_prepend (contacts, contact);
1101 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1103 new_contacts = g_list_reverse (new_contacts);
1106 /* Get the IDs of all new handles */
1107 tp_cli_connection_call_inspect_handles (priv->connection,
1109 TP_HANDLE_TYPE_CONTACT,
1111 tp_contact_factory_inspect_handles_cb,
1112 new_contacts, tp_contact_factory_list_free,
1113 G_OBJECT (tp_factory));
1115 /* Hold all new handles. */
1116 /* FIXME: Should be unholded when removed from the factory */
1117 tp_cli_connection_call_hold_handles (priv->connection,
1119 TP_HANDLE_TYPE_CONTACT,
1121 tp_contact_factory_hold_handles_cb,
1123 G_OBJECT (tp_factory));
1125 tp_contact_factory_request_everything (tp_factory, new_handles);
1128 g_array_free (new_handles, TRUE);
1134 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1135 EmpathyContact *contact,
1138 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1139 GHashTable *new_alias;
1142 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1143 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1144 g_return_if_fail (priv->ready);
1145 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1148 handle = empathy_contact_get_handle (contact);
1150 DEBUG ("Setting alias for contact %s (%d) to %s",
1151 empathy_contact_get_id (contact),
1154 new_alias = g_hash_table_new_full (g_direct_hash,
1159 g_hash_table_insert (new_alias,
1160 GUINT_TO_POINTER (handle),
1163 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1166 tp_contact_factory_set_aliases_cb,
1168 G_OBJECT (tp_factory));
1170 g_hash_table_destroy (new_alias);
1174 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1177 const gchar *mime_type)
1179 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1181 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1182 g_return_if_fail (priv->ready);
1184 if (data && size > 0 && size < G_MAXUINT) {
1187 avatar.data = (gchar*) data;
1190 DEBUG ("Setting avatar on account %s",
1191 mc_account_get_unique_name (priv->account));
1193 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1197 tp_contact_factory_set_avatar_cb,
1199 G_OBJECT (tp_factory));
1201 DEBUG ("Clearing avatar on account %s",
1202 mc_account_get_unique_name (priv->account));
1204 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1206 tp_contact_factory_clear_avatar_cb,
1208 G_OBJECT (tp_factory));
1213 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1215 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1217 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1223 tp_contact_factory_get_property (GObject *object,
1228 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1232 g_value_set_object (value, priv->account);
1235 g_value_set_boolean (value, priv->ready);
1237 case PROP_MIME_TYPES:
1238 g_value_set_boxed (value, priv->avatar_mime_types);
1240 case PROP_MIN_WIDTH:
1241 g_value_set_uint (value, priv->avatar_min_width);
1243 case PROP_MIN_HEIGHT:
1244 g_value_set_uint (value, priv->avatar_min_height);
1246 case PROP_MAX_WIDTH:
1247 g_value_set_uint (value, priv->avatar_max_width);
1249 case PROP_MAX_HEIGHT:
1250 g_value_set_uint (value, priv->avatar_max_height);
1253 g_value_set_uint (value, priv->avatar_max_size);
1256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1262 tp_contact_factory_set_property (GObject *object,
1264 const GValue *value,
1267 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1271 priv->account = g_object_ref (g_value_get_object (value));
1274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1280 tp_contact_factory_finalize (GObject *object)
1282 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1285 DEBUG ("Finalized: %p (%s)", object,
1286 mc_account_get_normalized_name (priv->account));
1288 empathy_disconnect_account_status_changed (priv->token);
1290 for (l = priv->contacts; l; l = l->next) {
1291 g_object_weak_unref (G_OBJECT (l->data),
1292 tp_contact_factory_weak_notify,
1296 g_list_free (priv->contacts);
1297 g_object_unref (priv->mc);
1298 g_object_unref (priv->account);
1299 g_object_unref (priv->user);
1301 if (priv->connection) {
1302 g_signal_handlers_disconnect_by_func (priv->connection,
1303 tp_contact_factory_connection_invalidated_cb,
1305 g_object_unref (priv->connection);
1308 g_strfreev (priv->avatar_mime_types);
1310 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1314 tp_contact_factory_constructor (GType type,
1316 GObjectConstructParam *props)
1318 GObject *tp_factory;
1319 EmpathyTpContactFactoryPriv *priv;
1321 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1322 priv = GET_PRIV (tp_factory);
1324 priv->ready = FALSE;
1325 priv->user = empathy_contact_new (priv->account);
1326 empathy_contact_set_is_user (priv->user, TRUE);
1327 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1328 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1334 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1336 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1338 object_class->finalize = tp_contact_factory_finalize;
1339 object_class->constructor = tp_contact_factory_constructor;
1340 object_class->get_property = tp_contact_factory_get_property;
1341 object_class->set_property = tp_contact_factory_set_property;
1343 g_object_class_install_property (object_class,
1345 g_param_spec_object ("account",
1346 "Factory's Account",
1347 "The account associated with the factory",
1350 G_PARAM_CONSTRUCT_ONLY |
1351 G_PARAM_STATIC_STRINGS));
1352 g_object_class_install_property (object_class,
1354 g_param_spec_boolean ("ready",
1355 "Whether the factory is ready",
1356 "TRUE once the factory is ready to be used",
1359 G_PARAM_STATIC_STRINGS));
1360 g_object_class_install_property (object_class,
1362 g_param_spec_boxed ("avatar-mime-types",
1363 "Supported MIME types for avatars",
1364 "Types of images that may be set as "
1365 "avatars on this connection. Only valid "
1366 "once 'ready' becomes TRUE.",
1369 G_PARAM_STATIC_STRINGS));
1370 g_object_class_install_property (object_class,
1372 g_param_spec_uint ("avatar-min-width",
1373 "Minimum width for avatars",
1374 "Minimum width of avatar that may be set. "
1375 "Only valid once 'ready' becomes TRUE.",
1380 G_PARAM_STATIC_STRINGS));
1381 g_object_class_install_property (object_class,
1383 g_param_spec_uint ("avatar-min-height",
1384 "Minimum height for avatars",
1385 "Minimum height of avatar that may be set. "
1386 "Only valid once 'ready' becomes TRUE.",
1391 G_PARAM_STATIC_STRINGS));
1392 g_object_class_install_property (object_class,
1394 g_param_spec_uint ("avatar-max-width",
1395 "Maximum width for avatars",
1396 "Maximum width of avatar that may be set "
1397 "or 0 if there is no maximum. "
1398 "Only valid once 'ready' becomes TRUE.",
1403 G_PARAM_STATIC_STRINGS));
1404 g_object_class_install_property (object_class,
1406 g_param_spec_uint ("avatar-max-height",
1407 "Maximum height for avatars",
1408 "Maximum height of avatar that may be set "
1409 "or 0 if there is no maximum. "
1410 "Only valid once 'ready' becomes TRUE.",
1415 G_PARAM_STATIC_STRINGS));
1416 g_object_class_install_property (object_class,
1418 g_param_spec_uint ("avatar-max-size",
1419 "Maximum size for avatars in bytes",
1420 "Maximum file size of avatar that may be "
1421 "set or 0 if there is no maximum. "
1422 "Only valid once 'ready' becomes TRUE.",
1427 G_PARAM_STATIC_STRINGS));
1430 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1434 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1436 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1437 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1439 tp_factory->priv = priv;
1440 priv->mc = empathy_mission_control_new ();
1441 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1442 G_CALLBACK (tp_contact_factory_status_changed_cb),
1446 EmpathyTpContactFactory *
1447 empathy_tp_contact_factory_new (McAccount *account)
1449 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,