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,
744 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
750 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
753 DEBUG ("Failed to get self handles: %s", error->message);
757 empathy_contact_set_handle (priv->user, handle);
759 /* Get avatar requirements for this connection */
760 tp_cli_connection_interface_avatars_call_get_avatar_requirements (
763 tp_contact_factory_got_avatar_requirements_cb,
770 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
771 const gchar **mime_types,
781 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
784 DEBUG ("Failed to get avatar requirements: %s", error->message);
785 /* We'll just leave avatar_mime_types as NULL; the
786 * avatar-setting code can use this as a signal that you can't
790 priv->avatar_mime_types = g_strdupv ((gchar **)mime_types);
791 priv->avatar_min_width = min_width;
792 priv->avatar_min_height = min_height;
793 priv->avatar_max_width = max_width;
794 priv->avatar_max_height = max_height;
795 priv->avatar_max_size = max_size;
798 tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
803 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
805 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
807 GArray *handle_needed;
809 GList *handle_needed_contacts = NULL;
810 GList *id_needed_contacts = NULL;
812 DEBUG ("Connection ready");
815 g_object_notify (G_OBJECT (tp_factory), "ready");
817 /* Connect signals */
818 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
819 tp_contact_factory_aliases_changed_cb,
821 G_OBJECT (tp_factory),
823 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
824 tp_contact_factory_avatar_updated_cb,
826 G_OBJECT (tp_factory),
828 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
829 tp_contact_factory_avatar_retrieved_cb,
831 G_OBJECT (tp_factory),
833 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
834 tp_contact_factory_presence_update_cb,
836 G_OBJECT (tp_factory),
838 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
839 tp_contact_factory_capabilities_changed_cb,
841 G_OBJECT (tp_factory),
844 /* Request needed info for all existing contacts */
845 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
846 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
847 for (l = priv->contacts; l; l = l->next) {
848 EmpathyContact *contact;
853 handle = empathy_contact_get_handle (contact);
854 id = empathy_contact_get_id (contact);
856 g_assert (!G_STR_EMPTY (id));
857 g_array_append_val (handle_needed, id);
858 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
859 g_object_ref (contact));
861 if (G_STR_EMPTY (id)) {
862 g_array_append_val (id_needed, handle);
863 id_needed_contacts = g_list_prepend (id_needed_contacts,
864 g_object_ref (contact));
867 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
868 id_needed_contacts = g_list_reverse (id_needed_contacts);
870 tp_cli_connection_call_request_handles (priv->connection,
872 TP_HANDLE_TYPE_CONTACT,
873 (const gchar**) handle_needed->data,
874 tp_contact_factory_request_handles_cb,
875 handle_needed_contacts, tp_contact_factory_list_free,
876 G_OBJECT (tp_factory));
878 tp_cli_connection_call_inspect_handles (priv->connection,
880 TP_HANDLE_TYPE_CONTACT,
882 tp_contact_factory_inspect_handles_cb,
883 id_needed_contacts, tp_contact_factory_list_free,
884 G_OBJECT (tp_factory));
886 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
889 g_array_free (handle_needed, TRUE);
890 g_array_free (id_needed, TRUE);
894 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
896 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
898 /* Get our own handle */
899 tp_cli_connection_call_get_self_handle (priv->connection,
901 tp_contact_factory_got_self_handle_cb,
903 G_OBJECT (tp_factory));
907 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
909 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
910 gboolean connection_ready;
912 if (priv->connection) {
913 /* We already have our connection object */
917 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
918 if (!priv->connection) {
922 /* We got a new connection, wait for it to be ready */
923 g_signal_connect_swapped (priv->connection, "invalidated",
924 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
927 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
928 if (connection_ready) {
929 tp_contact_factory_connection_ready_cb (tp_factory);
931 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
932 G_CALLBACK (tp_contact_factory_connection_ready_cb),
938 tp_contact_factory_status_changed_cb (MissionControl *mc,
939 TpConnectionStatus status,
941 TpConnectionStatusReason reason,
942 const gchar *unique_name,
943 EmpathyTpContactFactory *tp_factory)
945 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
948 account = mc_account_lookup (unique_name);
949 if (account && empathy_account_equal (account, priv->account)) {
950 tp_contact_factory_status_updated (tp_factory);
952 g_object_unref (account);
956 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
957 EmpathyContact *contact)
959 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
961 g_object_weak_ref (G_OBJECT (contact),
962 tp_contact_factory_weak_notify,
964 priv->contacts = g_list_prepend (priv->contacts, contact);
966 DEBUG ("Contact added: %s (%d)",
967 empathy_contact_get_id (contact),
968 empathy_contact_get_handle (contact));
972 tp_contact_factory_hold_handles_cb (TpConnection *connection,
978 DEBUG ("Failed to hold handles: %s", error->message);
983 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
985 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
987 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
989 return g_object_ref (priv->user);
993 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
996 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
997 EmpathyContact *contact;
999 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1000 g_return_val_if_fail (id != NULL, NULL);
1002 /* Check if the contact already exists */
1003 contact = tp_contact_factory_find_by_id (tp_factory, id);
1005 return g_object_ref (contact);
1008 /* Create new contact */
1009 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1010 "account", priv->account,
1013 tp_contact_factory_add_contact (tp_factory, contact);
1016 const gchar *contact_ids[] = {id, NULL};
1019 contacts = g_list_prepend (NULL, g_object_ref (contact));
1020 tp_cli_connection_call_request_handles (priv->connection,
1022 TP_HANDLE_TYPE_CONTACT,
1024 tp_contact_factory_request_handles_cb,
1025 contacts, tp_contact_factory_list_free,
1026 G_OBJECT (tp_factory));
1033 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1036 EmpathyContact *contact;
1040 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1042 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1043 g_array_append_val (handles, handle);
1045 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1046 g_array_free (handles, TRUE);
1048 contact = contacts ? contacts->data : NULL;
1049 g_list_free (contacts);
1055 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1056 const GArray *handles)
1058 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1059 GList *contacts = NULL;
1060 GArray *new_handles;
1061 GList *new_contacts = NULL;
1064 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1065 g_return_val_if_fail (handles != NULL, NULL);
1067 /* Search all contacts we already have */
1068 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1069 for (i = 0; i < handles->len; i++) {
1070 EmpathyContact *contact;
1073 handle = g_array_index (handles, guint, i);
1078 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1080 contacts = g_list_prepend (contacts, g_object_ref (contact));
1082 g_array_append_val (new_handles, handle);
1086 if (new_handles->len == 0) {
1087 g_array_free (new_handles, TRUE);
1091 /* Create new contacts */
1092 for (i = 0; i < new_handles->len; i++) {
1093 EmpathyContact *contact;
1096 handle = g_array_index (new_handles, guint, i);
1098 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1099 "account", priv->account,
1102 tp_contact_factory_add_contact (tp_factory, contact);
1103 contacts = g_list_prepend (contacts, contact);
1104 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1106 new_contacts = g_list_reverse (new_contacts);
1109 /* Get the IDs of all new handles */
1110 tp_cli_connection_call_inspect_handles (priv->connection,
1112 TP_HANDLE_TYPE_CONTACT,
1114 tp_contact_factory_inspect_handles_cb,
1115 new_contacts, tp_contact_factory_list_free,
1116 G_OBJECT (tp_factory));
1118 /* Hold all new handles. */
1119 /* FIXME: Should be unholded when removed from the factory */
1120 tp_cli_connection_call_hold_handles (priv->connection,
1122 TP_HANDLE_TYPE_CONTACT,
1124 tp_contact_factory_hold_handles_cb,
1126 G_OBJECT (tp_factory));
1128 tp_contact_factory_request_everything (tp_factory, new_handles);
1131 g_array_free (new_handles, TRUE);
1137 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1138 EmpathyContact *contact,
1141 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1142 GHashTable *new_alias;
1145 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1146 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1147 g_return_if_fail (priv->ready);
1148 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1151 handle = empathy_contact_get_handle (contact);
1153 DEBUG ("Setting alias for contact %s (%d) to %s",
1154 empathy_contact_get_id (contact),
1157 new_alias = g_hash_table_new_full (g_direct_hash,
1162 g_hash_table_insert (new_alias,
1163 GUINT_TO_POINTER (handle),
1166 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1169 tp_contact_factory_set_aliases_cb,
1171 G_OBJECT (tp_factory));
1173 g_hash_table_destroy (new_alias);
1177 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1180 const gchar *mime_type)
1182 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1184 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1185 g_return_if_fail (priv->ready);
1187 if (data && size > 0 && size < G_MAXUINT) {
1190 avatar.data = (gchar*) data;
1193 DEBUG ("Setting avatar on account %s",
1194 mc_account_get_unique_name (priv->account));
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 account %s",
1205 mc_account_get_unique_name (priv->account));
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_is_ready (EmpathyTpContactFactory *tp_factory)
1218 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1220 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1226 tp_contact_factory_get_property (GObject *object,
1231 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1235 g_value_set_object (value, priv->account);
1238 g_value_set_boolean (value, priv->ready);
1240 case PROP_MIME_TYPES:
1241 g_value_set_boxed (value, priv->avatar_mime_types);
1243 case PROP_MIN_WIDTH:
1244 g_value_set_uint (value, priv->avatar_min_width);
1246 case PROP_MIN_HEIGHT:
1247 g_value_set_uint (value, priv->avatar_min_height);
1249 case PROP_MAX_WIDTH:
1250 g_value_set_uint (value, priv->avatar_max_width);
1252 case PROP_MAX_HEIGHT:
1253 g_value_set_uint (value, priv->avatar_max_height);
1256 g_value_set_uint (value, priv->avatar_max_size);
1259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1265 tp_contact_factory_set_property (GObject *object,
1267 const GValue *value,
1270 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1274 priv->account = g_object_ref (g_value_get_object (value));
1277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1283 tp_contact_factory_finalize (GObject *object)
1285 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1288 DEBUG ("Finalized: %p (%s)", object,
1289 mc_account_get_normalized_name (priv->account));
1291 empathy_disconnect_account_status_changed (priv->token);
1293 for (l = priv->contacts; l; l = l->next) {
1294 g_object_weak_unref (G_OBJECT (l->data),
1295 tp_contact_factory_weak_notify,
1299 g_list_free (priv->contacts);
1300 g_object_unref (priv->mc);
1301 g_object_unref (priv->account);
1302 g_object_unref (priv->user);
1304 if (priv->connection) {
1305 g_signal_handlers_disconnect_by_func (priv->connection,
1306 tp_contact_factory_connection_invalidated_cb,
1308 g_object_unref (priv->connection);
1311 g_strfreev (priv->avatar_mime_types);
1313 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1317 tp_contact_factory_constructor (GType type,
1319 GObjectConstructParam *props)
1321 GObject *tp_factory;
1322 EmpathyTpContactFactoryPriv *priv;
1324 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1325 priv = GET_PRIV (tp_factory);
1327 priv->ready = FALSE;
1328 priv->user = empathy_contact_new (priv->account);
1329 empathy_contact_set_is_user (priv->user, TRUE);
1330 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1331 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1337 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1339 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1341 object_class->finalize = tp_contact_factory_finalize;
1342 object_class->constructor = tp_contact_factory_constructor;
1343 object_class->get_property = tp_contact_factory_get_property;
1344 object_class->set_property = tp_contact_factory_set_property;
1346 g_object_class_install_property (object_class,
1348 g_param_spec_object ("account",
1349 "Factory's Account",
1350 "The account associated with the factory",
1353 G_PARAM_CONSTRUCT_ONLY));
1354 g_object_class_install_property (object_class,
1356 g_param_spec_boolean ("ready",
1357 "Whether the factory is ready",
1358 "TRUE once the factory is ready to be used",
1361 g_object_class_install_property (object_class,
1363 g_param_spec_boxed ("avatar-mime-types",
1364 "Supported MIME types for avatars",
1365 "Types of images that may be set as "
1366 "avatars on this connection. Only valid "
1367 "once 'ready' becomes TRUE.",
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_object_class_install_property (object_class,
1382 g_param_spec_uint ("avatar-min-height",
1383 "Minimum height for avatars",
1384 "Minimum height of avatar that may be set. "
1385 "Only valid once 'ready' becomes TRUE.",
1390 g_object_class_install_property (object_class,
1392 g_param_spec_uint ("avatar-max-width",
1393 "Maximum width for avatars",
1394 "Maximum width of avatar that may be set "
1395 "or 0 if there is no maximum. "
1396 "Only valid once 'ready' becomes TRUE.",
1401 g_object_class_install_property (object_class,
1403 g_param_spec_uint ("avatar-max-height",
1404 "Maximum height for avatars",
1405 "Maximum height of avatar that may be set "
1406 "or 0 if there is no maximum. "
1407 "Only valid once 'ready' becomes TRUE.",
1412 g_object_class_install_property (object_class,
1414 g_param_spec_uint ("avatar-max-size",
1415 "Maximum size for avatars in bytes",
1416 "Maximum file size of avatar that may be "
1417 "set or 0 if there is no maximum. "
1418 "Only valid once 'ready' becomes TRUE.",
1425 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1429 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1431 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1432 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1434 tp_factory->priv = priv;
1435 priv->mc = empathy_mission_control_new ();
1436 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1437 G_CALLBACK (tp_contact_factory_status_changed_cb),
1441 EmpathyTpContactFactory *
1442 empathy_tp_contact_factory_new (McAccount *account)
1444 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,