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"
32 #include "empathy-debug.h"
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
35 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv))
37 #define DEBUG_DOMAIN "TpContactFactory"
39 struct _EmpathyTpContactFactoryPriv {
42 TpConnection *connection;
49 static void empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass);
50 static void empathy_tp_contact_factory_init (EmpathyTpContactFactory *factory);
52 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
60 static EmpathyContact *
61 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
64 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
67 for (l = priv->contacts; l; l = l->next) {
68 if (empathy_contact_get_handle (l->data) == handle) {
76 static EmpathyContact *
77 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
80 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
83 for (l = priv->contacts; l; l = l->next) {
84 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
93 tp_contact_factory_weak_notify (gpointer data,
94 GObject *where_the_object_was)
96 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
98 empathy_debug (DEBUG_DOMAIN, "Remove finalized contact %p",
99 where_the_object_was);
101 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
105 tp_contact_factory_presences_table_foreach (const gchar *state_str,
106 GHashTable *presences_table,
107 EmpathyContact *contact)
109 const GValue *message;
111 empathy_contact_set_presence (contact,
112 empathy_presence_from_str (state_str));
114 message = g_hash_table_lookup (presences_table, "message");
115 if (message != NULL) {
116 empathy_contact_set_presence_message (contact,
117 g_value_get_string (message));
119 empathy_contact_set_presence_message (contact, NULL);
124 tp_contact_factory_parse_presence_foreach (guint handle,
125 GValueArray *presence_struct,
126 EmpathyTpContactFactory *tp_factory)
128 GHashTable *presences_table;
129 EmpathyContact *contact;
131 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
136 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
138 g_hash_table_foreach (presences_table,
139 (GHFunc) tp_contact_factory_presences_table_foreach,
142 empathy_debug (DEBUG_DOMAIN, "Changing presence for contact %s (%d) to %s (%d)",
143 empathy_contact_get_id (contact),
145 empathy_contact_get_presence_message (contact),
146 empathy_contact_get_presence (contact));
150 tp_contact_factory_get_presence_cb (TpConnection *connection,
151 GHashTable *handle_table,
157 empathy_debug (DEBUG_DOMAIN, "Error getting presence: %s",
159 if (error->domain == TP_DBUS_ERRORS &&
160 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
161 guint *handles = user_data;
163 /* We have no presence iface, set default presence
165 while (*handles != 0) {
166 EmpathyContact *contact;
168 contact = tp_contact_factory_find_by_handle (
169 (EmpathyTpContactFactory*) tp_factory,
172 empathy_contact_set_presence (contact,
173 MC_PRESENCE_AVAILABLE);
183 g_hash_table_foreach (handle_table,
184 (GHFunc) tp_contact_factory_parse_presence_foreach,
185 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
189 tp_contact_factory_presence_update_cb (TpConnection *connection,
190 GHashTable *handle_table,
194 g_hash_table_foreach (handle_table,
195 (GHFunc) tp_contact_factory_parse_presence_foreach,
196 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
200 tp_contact_factory_set_aliases_cb (TpConnection *connection,
206 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
212 tp_contact_factory_request_aliases_cb (TpConnection *connection,
213 const gchar **contact_names,
218 guint *handles = user_data;
223 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
228 for (name = contact_names; *name; name++) {
229 EmpathyContact *contact;
231 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
237 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
238 empathy_contact_get_id (contact),
239 empathy_contact_get_handle (contact),
242 empathy_contact_set_name (contact, *name);
249 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
250 const GPtrArray *renamed_handlers,
252 GObject *weak_object)
254 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
257 for (i = 0; renamed_handlers->len > i; i++) {
260 GValueArray *renamed_struct;
261 EmpathyContact *contact;
263 renamed_struct = g_ptr_array_index (renamed_handlers, i);
264 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
265 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
266 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
269 /* We don't know this contact, skip */
273 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
274 empathy_contact_get_id (contact),
277 empathy_contact_set_name (contact, alias);
282 tp_contact_factory_set_avatar_cb (TpConnection *connection,
289 empathy_debug (DEBUG_DOMAIN, "Error setting avatar: %s",
295 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
301 empathy_debug (DEBUG_DOMAIN, "Error clearing avatar: %s",
307 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
310 const GArray *avatar_data,
311 const gchar *mime_type,
315 EmpathyContact *contact;
316 EmpathyAvatar *avatar;
318 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
324 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
325 empathy_contact_get_id (contact),
328 avatar = empathy_avatar_new (avatar_data->data,
333 empathy_contact_set_avatar (contact, avatar);
334 empathy_avatar_unref (avatar);
338 tp_contact_factory_request_avatars_cb (TpConnection *connection,
344 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
350 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
354 EmpathyContact *contact;
355 EmpathyAvatar *avatar;
357 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
362 /* Check if we have an avatar */
363 if (G_STR_EMPTY (token)) {
364 empathy_contact_set_avatar (contact, NULL);
368 /* Check if the avatar changed */
369 avatar = empathy_contact_get_avatar (contact);
370 if (avatar && !tp_strdiff (avatar->token, token)) {
374 /* The avatar changed, search the new one in the cache */
375 avatar = empathy_avatar_new_from_cache (token);
377 /* Got from cache, use it */
378 empathy_contact_set_avatar (contact, avatar);
379 empathy_avatar_unref (avatar);
383 /* Avatar is not up-to-date, we have to request it. */
388 EmpathyTpContactFactory *tp_factory;
393 tp_contact_factory_avatar_tokens_foreach (gpointer key,
397 TokensData *data = user_data;
398 const gchar *token = value;
399 guint handle = GPOINTER_TO_UINT (key);
401 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
403 g_array_append_val (data->handles, handle);
408 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
417 empathy_debug (DEBUG_DOMAIN,
418 "Error getting known avatars tokens: %s",
423 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
424 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
425 g_hash_table_foreach (tokens,
426 tp_contact_factory_avatar_tokens_foreach,
429 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
430 g_hash_table_size (tokens),
433 /* Request needed avatars */
434 if (data.handles->len > 0) {
435 tp_cli_connection_interface_avatars_call_request_avatars (connection,
438 tp_contact_factory_request_avatars_cb,
443 g_array_free (data.handles, TRUE);
447 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
449 const gchar *new_token,
455 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
456 handle, new_token)) {
457 /* Avatar was cached, nothing to do */
461 empathy_debug (DEBUG_DOMAIN, "Need to request avatar for token %s",
464 handles = g_array_new (FALSE, FALSE, sizeof (guint));
465 g_array_append_val (handles, handle);
467 tp_cli_connection_interface_avatars_call_request_avatars (connection,
470 tp_contact_factory_request_avatars_cb,
473 g_array_free (handles, TRUE);
477 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
479 const gchar *channel_type,
483 EmpathyContact *contact;
484 EmpathyCapabilities capabilities;
486 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
491 capabilities = empathy_contact_get_capabilities (contact);
492 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
494 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
495 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
496 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
497 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
498 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
500 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
501 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
505 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
506 empathy_contact_get_id (contact),
507 empathy_contact_get_handle (contact),
510 empathy_contact_set_capabilities (contact, capabilities);
514 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
515 const GPtrArray *capabilities,
518 GObject *weak_object)
520 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
524 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
526 /* FIXME Should set the capabilities of the contacts for which this request
527 * originated to NONE */
531 for (i = 0; i < capabilities->len; i++) {
534 const gchar *channel_type;
538 values = g_ptr_array_index (capabilities, i);
539 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
540 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
541 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
542 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
544 tp_contact_factory_update_capabilities (tp_factory,
553 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
554 const GPtrArray *capabilities,
556 GObject *weak_object)
558 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
561 for (i = 0; i < capabilities->len; i++) {
564 const gchar *channel_type;
568 values = g_ptr_array_index (capabilities, i);
569 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
570 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
571 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
572 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
574 tp_contact_factory_update_capabilities (tp_factory,
583 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
584 const GArray *handles)
586 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
589 g_return_if_fail (priv->ready);
591 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
592 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
593 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
596 tp_contact_factory_get_presence_cb,
598 G_OBJECT (tp_factory));
600 /* FIXME: Sometimes the dbus call timesout because CM takes
601 * too much time to request all aliases from the server,
602 * that's why we increase the timeout here. See fd.o bug #14795 */
603 dup_handles = g_memdup (handles->data, handles->len * sizeof (guint));
604 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
607 tp_contact_factory_request_aliases_cb,
609 G_OBJECT (tp_factory));
611 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
614 tp_contact_factory_get_known_avatar_tokens_cb,
616 G_OBJECT (tp_factory));
618 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
621 tp_contact_factory_get_capabilities_cb,
623 G_OBJECT (tp_factory));
627 tp_contact_factory_list_free (gpointer data)
631 g_list_foreach (l, (GFunc) g_object_unref, NULL);
636 tp_contact_factory_request_handles_cb (TpConnection *connection,
637 const GArray *handles,
642 GList *contacts = user_data;
647 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
652 for (l = contacts; l; l = l->next) {
655 handle = g_array_index (handles, guint, i);
656 empathy_contact_set_handle (l->data, handle);
661 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
666 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
673 GList *contacts = user_data;
677 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
683 for (l = contacts; l; l = l->next) {
684 empathy_contact_set_id (l->data, *id);
690 tp_contact_factory_disconnect_contact_foreach (gpointer data,
693 EmpathyContact *contact = data;
695 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
696 empathy_contact_set_handle (contact, 0);
700 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
702 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
704 empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
706 g_object_unref (priv->connection);
707 priv->connection = NULL;
709 g_object_notify (G_OBJECT (tp_factory), "ready");
712 g_list_foreach (priv->contacts,
713 tp_contact_factory_disconnect_contact_foreach,
719 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
725 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
727 GArray *handle_needed;
729 GList *handle_needed_contacts = NULL;
730 GList *id_needed_contacts = NULL;
733 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
738 empathy_debug (DEBUG_DOMAIN, "Connection ready");
740 empathy_contact_set_handle (priv->user, handle);
742 g_object_notify (tp_factory, "ready");
744 /* Connect signals */
745 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
746 tp_contact_factory_aliases_changed_cb,
748 G_OBJECT (tp_factory),
750 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
751 tp_contact_factory_avatar_updated_cb,
753 G_OBJECT (tp_factory),
755 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
756 tp_contact_factory_avatar_retrieved_cb,
758 G_OBJECT (tp_factory),
760 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
761 tp_contact_factory_presence_update_cb,
763 G_OBJECT (tp_factory),
765 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
766 tp_contact_factory_capabilities_changed_cb,
768 G_OBJECT (tp_factory),
771 /* Request needed info for all existing contacts */
772 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
773 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
774 for (l = priv->contacts; l; l = l->next) {
775 EmpathyContact *contact;
780 handle = empathy_contact_get_handle (contact);
781 id = empathy_contact_get_id (contact);
783 g_assert (!G_STR_EMPTY (id));
784 g_array_append_val (handle_needed, id);
785 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
786 g_object_ref (contact));
788 if (G_STR_EMPTY (id)) {
789 g_array_append_val (id_needed, handle);
790 id_needed_contacts = g_list_prepend (id_needed_contacts,
791 g_object_ref (contact));
794 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
795 id_needed_contacts = g_list_reverse (id_needed_contacts);
797 tp_cli_connection_call_request_handles (priv->connection,
799 TP_HANDLE_TYPE_CONTACT,
800 (const gchar**) handle_needed->data,
801 tp_contact_factory_request_handles_cb,
802 handle_needed_contacts, tp_contact_factory_list_free,
803 G_OBJECT (tp_factory));
805 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
807 tp_cli_connection_call_inspect_handles (priv->connection,
809 TP_HANDLE_TYPE_CONTACT,
811 tp_contact_factory_inspect_handles_cb,
812 id_needed_contacts, tp_contact_factory_list_free,
813 G_OBJECT (tp_factory));
815 g_array_free (handle_needed, TRUE);
816 g_array_free (id_needed, TRUE);
820 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
822 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
824 /* Get our own handle */
825 tp_cli_connection_call_get_self_handle (priv->connection,
827 tp_contact_factory_got_self_handle_cb,
829 G_OBJECT (tp_factory));
833 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
835 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
836 gboolean connection_ready;
838 if (priv->connection) {
839 /* We already have our connection object */
843 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
844 if (!priv->connection) {
848 /* We got a new connection, wait for it to be ready */
849 g_signal_connect_swapped (priv->connection, "invalidated",
850 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
853 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
854 if (connection_ready) {
855 tp_contact_factory_connection_ready_cb (tp_factory);
857 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
858 G_CALLBACK (tp_contact_factory_connection_ready_cb),
864 tp_contact_factory_status_changed_cb (MissionControl *mc,
865 TpConnectionStatus status,
867 TpConnectionStatusReason reason,
868 const gchar *unique_name,
869 EmpathyTpContactFactory *tp_factory)
871 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
874 account = mc_account_lookup (unique_name);
875 if (account && empathy_account_equal (account, priv->account)) {
876 tp_contact_factory_status_updated (tp_factory);
878 g_object_unref (account);
882 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
883 EmpathyContact *contact)
885 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
887 g_object_weak_ref (G_OBJECT (contact),
888 tp_contact_factory_weak_notify,
890 priv->contacts = g_list_prepend (priv->contacts, contact);
892 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
893 empathy_contact_get_id (contact),
894 empathy_contact_get_handle (contact));
898 tp_contact_factory_hold_handles_cb (TpConnection *connection,
904 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
910 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
912 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
914 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
916 return g_object_ref (priv->user);
920 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
923 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
924 EmpathyContact *contact;
926 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
927 g_return_val_if_fail (id != NULL, NULL);
929 /* Check if the contact already exists */
930 contact = tp_contact_factory_find_by_id (tp_factory, id);
932 return g_object_ref (contact);
935 /* Create new contact */
936 contact = g_object_new (EMPATHY_TYPE_CONTACT,
937 "account", priv->account,
940 tp_contact_factory_add_contact (tp_factory, contact);
943 const gchar *contact_ids[] = {id, NULL};
946 contacts = g_list_prepend (NULL, g_object_ref (contact));
947 tp_cli_connection_call_request_handles (priv->connection,
949 TP_HANDLE_TYPE_CONTACT,
951 tp_contact_factory_request_handles_cb,
952 contacts, tp_contact_factory_list_free,
953 G_OBJECT (tp_factory));
960 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
963 EmpathyContact *contact;
967 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
969 handles = g_array_new (FALSE, FALSE, sizeof (guint));
970 g_array_append_val (handles, handle);
972 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
973 g_array_free (handles, TRUE);
975 contact = contacts ? contacts->data : NULL;
976 g_list_free (contacts);
982 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
983 const GArray *handles)
985 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
986 GList *contacts = NULL;
988 GList *new_contacts = NULL;
991 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
992 g_return_val_if_fail (handles != NULL, NULL);
994 /* Search all contacts we already have */
995 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
996 for (i = 0; i < handles->len; i++) {
997 EmpathyContact *contact;
1000 handle = g_array_index (handles, guint, i);
1005 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1007 contacts = g_list_prepend (contacts, g_object_ref (contact));
1009 g_array_append_val (new_handles, handle);
1013 if (new_handles->len == 0) {
1014 g_array_free (new_handles, TRUE);
1018 /* Create new contacts */
1019 for (i = 0; i < new_handles->len; i++) {
1020 EmpathyContact *contact;
1023 handle = g_array_index (new_handles, guint, i);
1025 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1026 "account", priv->account,
1029 tp_contact_factory_add_contact (tp_factory, contact);
1030 contacts = g_list_prepend (contacts, contact);
1031 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1033 new_contacts = g_list_reverse (new_contacts);
1036 /* Get the IDs of all new handles */
1037 tp_cli_connection_call_inspect_handles (priv->connection,
1039 TP_HANDLE_TYPE_CONTACT,
1041 tp_contact_factory_inspect_handles_cb,
1042 new_contacts, tp_contact_factory_list_free,
1043 G_OBJECT (tp_factory));
1045 /* Hold all new handles. */
1046 /* FIXME: Should be unholded when removed from the factory */
1047 tp_cli_connection_call_hold_handles (priv->connection,
1049 TP_HANDLE_TYPE_CONTACT,
1051 tp_contact_factory_hold_handles_cb,
1053 G_OBJECT (tp_factory));
1055 tp_contact_factory_request_everything (tp_factory, new_handles);
1062 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1063 EmpathyContact *contact,
1066 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1067 GHashTable *new_alias;
1070 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1071 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1072 g_return_if_fail (priv->ready);
1073 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1076 handle = empathy_contact_get_handle (contact);
1078 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1079 empathy_contact_get_id (contact),
1082 new_alias = g_hash_table_new_full (g_direct_hash,
1087 g_hash_table_insert (new_alias,
1088 GUINT_TO_POINTER (handle),
1091 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1094 tp_contact_factory_set_aliases_cb,
1096 G_OBJECT (tp_factory));
1098 g_hash_table_destroy (new_alias);
1102 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1105 const gchar *mime_type)
1107 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1109 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1110 g_return_if_fail (priv->ready);
1112 if (data && size > 0 && size < G_MAXUINT) {
1115 avatar.data = (gchar*) data;
1118 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1119 mc_account_get_unique_name (priv->account));
1121 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1125 tp_contact_factory_set_avatar_cb,
1127 G_OBJECT (tp_factory));
1129 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1130 mc_account_get_unique_name (priv->account));
1132 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1134 tp_contact_factory_clear_avatar_cb,
1136 G_OBJECT (tp_factory));
1141 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1143 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1145 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1151 tp_contact_factory_get_property (GObject *object,
1156 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1160 g_value_set_object (value, priv->account);
1163 g_value_set_boolean (value, priv->ready);
1166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1172 tp_contact_factory_set_property (GObject *object,
1174 const GValue *value,
1177 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1181 priv->account = g_object_ref (g_value_get_object (value));
1184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1190 tp_contact_factory_finalize (GObject *object)
1192 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1195 empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1197 mc_account_get_normalized_name (priv->account));
1199 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1200 "AccountStatusChanged",
1201 G_CALLBACK (tp_contact_factory_status_changed_cb),
1204 for (l = priv->contacts; l; l = l->next) {
1205 g_object_weak_unref (G_OBJECT (l->data),
1206 tp_contact_factory_weak_notify,
1210 g_list_free (priv->contacts);
1211 g_object_unref (priv->mc);
1212 g_object_unref (priv->account);
1213 g_object_unref (priv->user);
1215 if (priv->connection) {
1216 g_signal_handlers_disconnect_by_func (priv->connection,
1217 tp_contact_factory_connection_invalidated_cb,
1219 g_object_unref (priv->connection);
1222 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1226 tp_contact_factory_constructor (GType type,
1228 GObjectConstructParam *props)
1230 GObject *tp_factory;
1231 EmpathyTpContactFactoryPriv *priv;
1233 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1234 priv = GET_PRIV (tp_factory);
1236 priv->ready = FALSE;
1237 priv->user = empathy_contact_new (priv->account);
1238 empathy_contact_set_is_user (priv->user, TRUE);
1239 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1240 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1246 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1248 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1250 object_class->finalize = tp_contact_factory_finalize;
1251 object_class->constructor = tp_contact_factory_constructor;
1252 object_class->get_property = tp_contact_factory_get_property;
1253 object_class->set_property = tp_contact_factory_set_property;
1255 g_object_class_install_property (object_class,
1257 g_param_spec_object ("account",
1258 "Factory's Account",
1259 "The account associated with the factory",
1262 G_PARAM_CONSTRUCT_ONLY));
1263 g_object_class_install_property (object_class,
1265 g_param_spec_boolean ("ready",
1266 "Wheter the factor is ready",
1267 "Is the factory ready",
1271 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1275 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1277 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1279 priv->mc = empathy_mission_control_new ();
1280 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1281 "AccountStatusChanged",
1282 G_CALLBACK (tp_contact_factory_status_changed_cb),
1286 EmpathyTpContactFactory *
1287 empathy_tp_contact_factory_new (McAccount *account)
1289 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,