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);
59 static EmpathyContact *
60 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
63 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
66 for (l = priv->contacts; l; l = l->next) {
67 if (empathy_contact_get_handle (l->data) == handle) {
75 static EmpathyContact *
76 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
79 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
82 for (l = priv->contacts; l; l = l->next) {
83 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
92 tp_contact_factory_weak_notify (gpointer data,
93 GObject *where_the_object_was)
95 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
97 empathy_debug (DEBUG_DOMAIN, "Remove finalized contact %p",
98 where_the_object_was);
100 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
104 tp_contact_factory_presences_table_foreach (const gchar *state_str,
105 GHashTable *presences_table,
106 EmpathyContact *contact)
108 const GValue *message;
110 empathy_contact_set_presence (contact,
111 empathy_presence_from_str (state_str));
113 message = g_hash_table_lookup (presences_table, "message");
114 if (message != NULL) {
115 empathy_contact_set_presence_message (contact,
116 g_value_get_string (message));
118 empathy_contact_set_presence_message (contact, NULL);
123 tp_contact_factory_parse_presence_foreach (guint handle,
124 GValueArray *presence_struct,
125 EmpathyTpContactFactory *tp_factory)
127 GHashTable *presences_table;
128 EmpathyContact *contact;
130 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
135 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
137 g_hash_table_foreach (presences_table,
138 (GHFunc) tp_contact_factory_presences_table_foreach,
141 empathy_debug (DEBUG_DOMAIN, "Changing presence for contact %s (%d) to %s (%d)",
142 empathy_contact_get_id (contact),
144 empathy_contact_get_presence_message (contact),
145 empathy_contact_get_presence (contact));
149 tp_contact_factory_get_presence_cb (TpConnection *connection,
150 GHashTable *handle_table,
156 empathy_debug (DEBUG_DOMAIN, "Error getting presence: %s",
161 g_hash_table_foreach (handle_table,
162 (GHFunc) tp_contact_factory_parse_presence_foreach,
163 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
167 tp_contact_factory_presence_update_cb (TpConnection *connection,
168 GHashTable *handle_table,
172 g_hash_table_foreach (handle_table,
173 (GHFunc) tp_contact_factory_parse_presence_foreach,
174 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
178 tp_contact_factory_set_aliases_cb (TpConnection *connection,
184 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
190 tp_contact_factory_request_aliases_cb (TpConnection *connection,
191 const gchar **contact_names,
196 guint *handles = user_data;
201 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
206 for (name = contact_names; *name; name++) {
207 EmpathyContact *contact;
209 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
215 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
216 empathy_contact_get_id (contact),
217 empathy_contact_get_handle (contact),
220 empathy_contact_set_name (contact, *name);
227 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
228 const GPtrArray *renamed_handlers,
230 GObject *weak_object)
232 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
235 for (i = 0; renamed_handlers->len > i; i++) {
238 GValueArray *renamed_struct;
239 EmpathyContact *contact;
241 renamed_struct = g_ptr_array_index (renamed_handlers, i);
242 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
243 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
244 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
247 /* We don't know this contact, skip */
251 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
252 empathy_contact_get_id (contact),
255 empathy_contact_set_name (contact, alias);
260 tp_contact_factory_set_avatar_cb (TpConnection *connection,
267 empathy_debug (DEBUG_DOMAIN, "Error setting avatar: %s",
273 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
279 empathy_debug (DEBUG_DOMAIN, "Error clearing avatar: %s",
285 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
288 const GArray *avatar_data,
289 const gchar *mime_type,
293 EmpathyContact *contact;
294 EmpathyAvatar *avatar;
296 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
302 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
303 empathy_contact_get_id (contact),
306 avatar = empathy_avatar_new (avatar_data->data,
311 empathy_contact_set_avatar (contact, avatar);
312 empathy_avatar_unref (avatar);
316 tp_contact_factory_request_avatars_cb (TpConnection *connection,
322 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
328 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
332 EmpathyContact *contact;
333 EmpathyAvatar *avatar;
335 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
340 /* Check if we have an avatar */
341 if (G_STR_EMPTY (token)) {
342 empathy_contact_set_avatar (contact, NULL);
346 /* Check if the avatar changed */
347 avatar = empathy_contact_get_avatar (contact);
348 if (avatar && !tp_strdiff (avatar->token, token)) {
352 /* The avatar changed, search the new one in the cache */
353 avatar = empathy_avatar_new_from_cache (token);
355 /* Got from cache, use it */
356 empathy_contact_set_avatar (contact, avatar);
357 empathy_avatar_unref (avatar);
361 /* Avatar is not up-to-date, we have to request it. */
366 EmpathyTpContactFactory *tp_factory;
371 tp_contact_factory_avatar_tokens_foreach (gpointer key,
375 TokensData *data = user_data;
376 const gchar *token = value;
377 guint handle = GPOINTER_TO_UINT (key);
379 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
381 g_array_append_val (data->handles, handle);
386 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
395 empathy_debug (DEBUG_DOMAIN,
396 "Error getting known avatars tokens: %s",
401 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
402 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
403 g_hash_table_foreach (tokens,
404 tp_contact_factory_avatar_tokens_foreach,
407 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
408 g_hash_table_size (tokens),
411 /* Request needed avatars */
412 if (data.handles->len > 0) {
413 tp_cli_connection_interface_avatars_call_request_avatars (connection,
416 tp_contact_factory_request_avatars_cb,
421 g_array_free (data.handles, TRUE);
425 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
427 const gchar *new_token,
433 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
434 handle, new_token)) {
435 /* Avatar was cached, nothing to do */
439 empathy_debug (DEBUG_DOMAIN, "Need to request avatar for token %s",
442 handles = g_array_new (FALSE, FALSE, sizeof (guint));
443 g_array_append_val (handles, handle);
445 tp_cli_connection_interface_avatars_call_request_avatars (connection,
448 tp_contact_factory_request_avatars_cb,
451 g_array_free (handles, TRUE);
455 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
457 const gchar *channel_type,
461 EmpathyContact *contact;
462 EmpathyCapabilities capabilities;
464 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
469 capabilities = empathy_contact_get_capabilities (contact);
470 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
472 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
473 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
474 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
475 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
476 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
478 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
479 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
483 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
484 empathy_contact_get_id (contact),
485 empathy_contact_get_handle (contact),
488 empathy_contact_set_capabilities (contact, capabilities);
492 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
493 const GPtrArray *capabilities,
496 GObject *weak_object)
498 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
502 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
504 /* FIXME Should set the capabilities of the contacts for which this request
505 * originated to NONE */
509 for (i = 0; i < capabilities->len; i++) {
512 const gchar *channel_type;
516 values = g_ptr_array_index (capabilities, i);
517 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
518 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
519 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
520 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
522 tp_contact_factory_update_capabilities (tp_factory,
531 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
532 const GPtrArray *capabilities,
534 GObject *weak_object)
536 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
539 for (i = 0; i < capabilities->len; i++) {
542 const gchar *channel_type;
546 values = g_ptr_array_index (capabilities, i);
547 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
548 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
549 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
550 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
552 tp_contact_factory_update_capabilities (tp_factory,
561 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
562 const GArray *handles)
564 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
567 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
570 tp_contact_factory_get_presence_cb,
572 G_OBJECT (tp_factory));
574 dup_handles = g_memdup (handles->data, handles->len * sizeof (guint));
575 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
578 tp_contact_factory_request_aliases_cb,
580 G_OBJECT (tp_factory));
582 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
585 tp_contact_factory_get_known_avatar_tokens_cb,
587 G_OBJECT (tp_factory));
589 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
592 tp_contact_factory_get_capabilities_cb,
594 G_OBJECT (tp_factory));
598 tp_contact_factory_list_free (gpointer data)
602 g_list_foreach (l, (GFunc) g_object_unref, NULL);
607 tp_contact_factory_request_handles_cb (TpConnection *connection,
608 const GArray *handles,
613 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
614 GList *contacts = user_data;
619 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
624 for (l = contacts; l; l = l->next) {
627 handle = g_array_index (handles, guint, i);
628 empathy_contact_set_handle (l->data, handle);
629 if (handle == priv->self_handle) {
630 empathy_contact_set_is_user (l->data, TRUE);
636 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
641 tp_contact_factory_disconnect_contact_foreach (gpointer data,
644 EmpathyContact *contact = data;
646 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
647 empathy_contact_set_handle (contact, 0);
651 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
653 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
655 empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
657 g_object_unref (priv->connection);
658 priv->connection = NULL;
660 priv->self_handle = 0;
662 g_list_foreach (priv->contacts,
663 tp_contact_factory_disconnect_contact_foreach,
669 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
675 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
676 const gchar **contact_ids;
681 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
686 priv->self_handle = handle;
689 /* Connect signals */
690 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
691 tp_contact_factory_aliases_changed_cb,
693 G_OBJECT (tp_factory),
695 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
696 tp_contact_factory_avatar_updated_cb,
698 G_OBJECT (tp_factory),
700 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
701 tp_contact_factory_avatar_retrieved_cb,
703 G_OBJECT (tp_factory),
705 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
706 tp_contact_factory_presence_update_cb,
708 G_OBJECT (tp_factory),
710 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
711 tp_contact_factory_capabilities_changed_cb,
713 G_OBJECT (tp_factory),
716 /* Request new handles for all contacts */
717 if (priv->contacts) {
720 contacts = g_list_copy (priv->contacts);
721 g_list_foreach (contacts, (GFunc) g_object_ref, NULL);
723 i = g_list_length (contacts);
724 contact_ids = g_new0 (const gchar*, i + 1);
726 for (l = contacts; l; l = l->next) {
727 contact_ids[i] = empathy_contact_get_id (l->data);
731 tp_cli_connection_call_request_handles (priv->connection,
733 TP_HANDLE_TYPE_CONTACT,
735 tp_contact_factory_request_handles_cb,
736 contacts, tp_contact_factory_list_free,
737 G_OBJECT (tp_factory));
738 g_free (contact_ids);
743 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
745 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
747 /* Get our own handle */
748 tp_cli_connection_call_get_self_handle (priv->connection,
750 tp_contact_factory_got_self_handle_cb,
752 G_OBJECT (tp_factory));
756 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
758 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
761 if (priv->connection) {
762 /* We already have our connection object */
766 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
771 /* We got a new connection, wait for it to be ready */
772 priv->connection = tp_conn_dup_connection (tp_conn);
773 g_object_unref (tp_conn);
775 g_signal_connect_swapped (priv->connection, "invalidated",
776 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
778 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
779 G_CALLBACK (tp_contact_factory_connection_ready_cb),
784 tp_contact_factory_status_changed_cb (MissionControl *mc,
785 TpConnectionStatus status,
787 TpConnectionStatusReason reason,
788 const gchar *unique_name,
789 EmpathyTpContactFactory *tp_factory)
791 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
794 account = mc_account_lookup (unique_name);
795 if (account && empathy_account_equal (account, priv->account)) {
796 tp_contact_factory_status_updated (tp_factory);
798 g_object_unref (account);
802 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
803 EmpathyContact *contact)
805 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
807 g_object_weak_ref (G_OBJECT (contact),
808 tp_contact_factory_weak_notify,
810 priv->contacts = g_list_prepend (priv->contacts, contact);
812 if (!tp_proxy_has_interface_by_id (priv->connection,
813 TP_IFACE_QUARK_CONNECTION_INTERFACE_PRESENCE)) {
814 /* We have no presence iface, set default presence
816 empathy_contact_set_presence (contact, MC_PRESENCE_AVAILABLE);
819 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
820 empathy_contact_get_id (contact),
821 empathy_contact_get_handle (contact));
825 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
831 guint *handles = user_data;
836 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
841 for (id = ids; *id; id++) {
842 EmpathyContact *contact;
844 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
846 empathy_contact_set_id (contact, *id);
853 tp_contact_factory_hold_handles_cb (TpConnection *connection,
859 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
865 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
867 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
869 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
871 return empathy_tp_contact_factory_get_from_handle (tp_factory,
876 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
879 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
880 EmpathyContact *contact;
882 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
883 g_return_val_if_fail (id != NULL, NULL);
885 /* Check if the contact already exists */
886 contact = tp_contact_factory_find_by_id (tp_factory, id);
888 return g_object_ref (contact);
891 /* Create new contact */
892 contact = g_object_new (EMPATHY_TYPE_CONTACT,
893 "account", priv->account,
896 tp_contact_factory_add_contact (tp_factory, contact);
898 /* If the account is connected, request contact's handle */
899 if (priv->connection) {
900 const gchar *contact_ids[] = {id, NULL};
903 contacts = g_list_prepend (NULL, g_object_ref (contact));
904 tp_cli_connection_call_request_handles (priv->connection,
906 TP_HANDLE_TYPE_CONTACT,
908 tp_contact_factory_request_handles_cb,
909 contacts, tp_contact_factory_list_free,
910 G_OBJECT (tp_factory));
917 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
920 EmpathyContact *contact;
924 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
926 handles = g_array_new (FALSE, FALSE, sizeof (guint));
927 g_array_append_val (handles, handle);
929 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
930 g_array_free (handles, TRUE);
932 contact = contacts ? contacts->data : NULL;
933 g_list_free (contacts);
939 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
940 const GArray *handles)
942 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
943 GList *contacts = NULL;
948 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
949 g_return_val_if_fail (handles != NULL, NULL);
951 /* Search all contacts we already have */
952 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
953 for (i = 0; i < handles->len; i++) {
954 EmpathyContact *contact;
957 handle = g_array_index (handles, guint, i);
962 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
964 contacts = g_list_prepend (contacts, g_object_ref (contact));
966 g_array_append_val (new_handles, handle);
970 if (new_handles->len == 0) {
971 g_array_free (new_handles, TRUE);
975 /* Create new contacts */
976 for (i = 0; i < new_handles->len; i++) {
977 EmpathyContact *contact;
981 handle = g_array_index (new_handles, guint, i);
983 is_user = (handle == priv->self_handle);
984 contact = g_object_new (EMPATHY_TYPE_CONTACT,
985 "account", priv->account,
989 tp_contact_factory_add_contact (tp_factory, contact);
990 contacts = g_list_prepend (contacts, contact);
993 tp_contact_factory_request_everything (tp_factory, new_handles);
995 /* Get the IDs of all new handles */
996 dup_handles = g_memdup (new_handles->data, new_handles->len * sizeof (guint));
997 tp_cli_connection_call_inspect_handles (priv->connection,
999 TP_HANDLE_TYPE_CONTACT,
1001 tp_contact_factory_inspect_handles_cb,
1002 dup_handles, g_free,
1003 G_OBJECT (tp_factory));
1005 /* Hold all new handles. */
1006 /* FIXME: Should be unholded when removed from the factory */
1007 tp_cli_connection_call_hold_handles (priv->connection,
1009 TP_HANDLE_TYPE_CONTACT,
1011 tp_contact_factory_hold_handles_cb,
1013 G_OBJECT (tp_factory));
1019 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1020 EmpathyContact *contact,
1023 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1024 GHashTable *new_alias;
1027 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1028 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1029 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1032 handle = empathy_contact_get_handle (contact);
1034 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1035 empathy_contact_get_id (contact),
1038 new_alias = g_hash_table_new_full (g_direct_hash,
1043 g_hash_table_insert (new_alias,
1044 GUINT_TO_POINTER (handle),
1047 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1050 tp_contact_factory_set_aliases_cb,
1052 G_OBJECT (tp_factory));
1054 g_hash_table_destroy (new_alias);
1058 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1061 const gchar *mime_type)
1063 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1065 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1067 if (data && size > 0 && size < G_MAXUINT) {
1070 avatar.data = (gchar*) data;
1073 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1074 mc_account_get_unique_name (priv->account));
1076 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1080 tp_contact_factory_set_avatar_cb,
1082 G_OBJECT (tp_factory));
1084 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1085 mc_account_get_unique_name (priv->account));
1087 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1089 tp_contact_factory_clear_avatar_cb,
1091 G_OBJECT (tp_factory));
1096 tp_contact_factory_get_property (GObject *object,
1101 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1105 g_value_set_object (value, priv->account);
1108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1114 tp_contact_factory_set_property (GObject *object,
1116 const GValue *value,
1119 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1123 priv->account = g_object_ref (g_value_get_object (value));
1126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1132 tp_contact_factory_finalize (GObject *object)
1134 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1137 empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1139 mc_account_get_normalized_name (priv->account));
1141 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1142 "AccountStatusChanged",
1143 G_CALLBACK (tp_contact_factory_status_changed_cb),
1146 for (l = priv->contacts; l; l = l->next) {
1147 g_object_weak_unref (G_OBJECT (l->data),
1148 tp_contact_factory_weak_notify,
1152 g_list_free (priv->contacts);
1153 g_object_unref (priv->mc);
1154 g_object_unref (priv->account);
1156 if (priv->connection) {
1157 g_object_unref (priv->connection);
1160 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1164 tp_contact_factory_constructor (GType type,
1166 GObjectConstructParam *props)
1168 GObject *tp_factory;
1169 EmpathyTpContactFactoryPriv *priv;
1171 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1172 priv = GET_PRIV (tp_factory);
1174 priv->ready = FALSE;
1175 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1182 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1184 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1186 object_class->finalize = tp_contact_factory_finalize;
1187 object_class->constructor = tp_contact_factory_constructor;
1188 object_class->get_property = tp_contact_factory_get_property;
1189 object_class->set_property = tp_contact_factory_set_property;
1191 /* Construct-only properties */
1192 g_object_class_install_property (object_class,
1194 g_param_spec_object ("account",
1195 "Factory's Account",
1196 "The account associated with the factory",
1199 G_PARAM_CONSTRUCT_ONLY));
1201 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1205 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1207 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1209 priv->mc = empathy_mission_control_new ();
1210 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1211 "AccountStatusChanged",
1212 G_CALLBACK (tp_contact_factory_status_changed_cb),
1216 EmpathyTpContactFactory *
1217 empathy_tp_contact_factory_new (McAccount *account)
1219 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,