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",
158 if (error->domain == TP_DBUS_ERRORS &&
159 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
160 guint *handles = user_data;
162 /* We have no presence iface, set default presence
164 while (*handles != 0) {
165 EmpathyContact *contact;
167 contact = tp_contact_factory_find_by_handle (
168 (EmpathyTpContactFactory*) tp_factory,
171 empathy_contact_set_presence (contact,
172 MC_PRESENCE_AVAILABLE);
182 g_hash_table_foreach (handle_table,
183 (GHFunc) tp_contact_factory_parse_presence_foreach,
184 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
188 tp_contact_factory_presence_update_cb (TpConnection *connection,
189 GHashTable *handle_table,
193 g_hash_table_foreach (handle_table,
194 (GHFunc) tp_contact_factory_parse_presence_foreach,
195 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
199 tp_contact_factory_set_aliases_cb (TpConnection *connection,
205 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
211 tp_contact_factory_request_aliases_cb (TpConnection *connection,
212 const gchar **contact_names,
217 guint *handles = user_data;
222 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
227 for (name = contact_names; *name; name++) {
228 EmpathyContact *contact;
230 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
236 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
237 empathy_contact_get_id (contact),
238 empathy_contact_get_handle (contact),
241 empathy_contact_set_name (contact, *name);
248 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
249 const GPtrArray *renamed_handlers,
251 GObject *weak_object)
253 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
256 for (i = 0; renamed_handlers->len > i; i++) {
259 GValueArray *renamed_struct;
260 EmpathyContact *contact;
262 renamed_struct = g_ptr_array_index (renamed_handlers, i);
263 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
264 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
265 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
268 /* We don't know this contact, skip */
272 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
273 empathy_contact_get_id (contact),
276 empathy_contact_set_name (contact, alias);
281 tp_contact_factory_set_avatar_cb (TpConnection *connection,
288 empathy_debug (DEBUG_DOMAIN, "Error setting avatar: %s",
294 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
300 empathy_debug (DEBUG_DOMAIN, "Error clearing avatar: %s",
306 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
309 const GArray *avatar_data,
310 const gchar *mime_type,
314 EmpathyContact *contact;
315 EmpathyAvatar *avatar;
317 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
323 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
324 empathy_contact_get_id (contact),
327 avatar = empathy_avatar_new (avatar_data->data,
332 empathy_contact_set_avatar (contact, avatar);
333 empathy_avatar_unref (avatar);
337 tp_contact_factory_request_avatars_cb (TpConnection *connection,
343 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
349 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
353 EmpathyContact *contact;
354 EmpathyAvatar *avatar;
356 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
361 /* Check if we have an avatar */
362 if (G_STR_EMPTY (token)) {
363 empathy_contact_set_avatar (contact, NULL);
367 /* Check if the avatar changed */
368 avatar = empathy_contact_get_avatar (contact);
369 if (avatar && !tp_strdiff (avatar->token, token)) {
373 /* The avatar changed, search the new one in the cache */
374 avatar = empathy_avatar_new_from_cache (token);
376 /* Got from cache, use it */
377 empathy_contact_set_avatar (contact, avatar);
378 empathy_avatar_unref (avatar);
382 /* Avatar is not up-to-date, we have to request it. */
387 EmpathyTpContactFactory *tp_factory;
392 tp_contact_factory_avatar_tokens_foreach (gpointer key,
396 TokensData *data = user_data;
397 const gchar *token = value;
398 guint handle = GPOINTER_TO_UINT (key);
400 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
402 g_array_append_val (data->handles, handle);
407 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
416 empathy_debug (DEBUG_DOMAIN,
417 "Error getting known avatars tokens: %s",
422 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
423 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
424 g_hash_table_foreach (tokens,
425 tp_contact_factory_avatar_tokens_foreach,
428 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
429 g_hash_table_size (tokens),
432 /* Request needed avatars */
433 if (data.handles->len > 0) {
434 tp_cli_connection_interface_avatars_call_request_avatars (connection,
437 tp_contact_factory_request_avatars_cb,
442 g_array_free (data.handles, TRUE);
446 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
448 const gchar *new_token,
454 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
455 handle, new_token)) {
456 /* Avatar was cached, nothing to do */
460 empathy_debug (DEBUG_DOMAIN, "Need to request avatar for token %s",
463 handles = g_array_new (FALSE, FALSE, sizeof (guint));
464 g_array_append_val (handles, handle);
466 tp_cli_connection_interface_avatars_call_request_avatars (connection,
469 tp_contact_factory_request_avatars_cb,
472 g_array_free (handles, TRUE);
476 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
478 const gchar *channel_type,
482 EmpathyContact *contact;
483 EmpathyCapabilities capabilities;
485 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
490 capabilities = empathy_contact_get_capabilities (contact);
491 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
493 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
494 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
495 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
496 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
497 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
499 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
500 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
504 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
505 empathy_contact_get_id (contact),
506 empathy_contact_get_handle (contact),
509 empathy_contact_set_capabilities (contact, capabilities);
513 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
514 const GPtrArray *capabilities,
517 GObject *weak_object)
519 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
523 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
525 /* FIXME Should set the capabilities of the contacts for which this request
526 * originated to NONE */
530 for (i = 0; i < capabilities->len; i++) {
533 const gchar *channel_type;
537 values = g_ptr_array_index (capabilities, i);
538 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
539 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
540 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
541 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
543 tp_contact_factory_update_capabilities (tp_factory,
552 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
553 const GPtrArray *capabilities,
555 GObject *weak_object)
557 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
560 for (i = 0; i < capabilities->len; i++) {
563 const gchar *channel_type;
567 values = g_ptr_array_index (capabilities, i);
568 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
569 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
570 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
571 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
573 tp_contact_factory_update_capabilities (tp_factory,
582 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
583 const GArray *handles)
585 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
592 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
593 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
594 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
597 tp_contact_factory_get_presence_cb,
599 G_OBJECT (tp_factory));
601 dup_handles = g_memdup (handles->data, handles->len * sizeof (guint));
602 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
605 tp_contact_factory_request_aliases_cb,
607 G_OBJECT (tp_factory));
609 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
612 tp_contact_factory_get_known_avatar_tokens_cb,
614 G_OBJECT (tp_factory));
616 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
619 tp_contact_factory_get_capabilities_cb,
621 G_OBJECT (tp_factory));
625 tp_contact_factory_list_free (gpointer data)
629 g_list_foreach (l, (GFunc) g_object_unref, NULL);
634 tp_contact_factory_request_handles_cb (TpConnection *connection,
635 const GArray *handles,
640 GList *contacts = user_data;
645 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
650 for (l = contacts; l; l = l->next) {
653 handle = g_array_index (handles, guint, i);
654 empathy_contact_set_handle (l->data, handle);
659 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
664 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
671 GList *contacts = user_data;
675 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
681 for (l = contacts; l; l = l->next) {
682 empathy_contact_set_id (l->data, *id);
688 tp_contact_factory_disconnect_contact_foreach (gpointer data,
691 EmpathyContact *contact = data;
693 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
694 empathy_contact_set_handle (contact, 0);
698 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
700 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
702 empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
704 g_object_unref (priv->connection);
705 priv->connection = NULL;
708 g_list_foreach (priv->contacts,
709 tp_contact_factory_disconnect_contact_foreach,
715 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
721 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
723 GArray *handle_needed;
725 GList *handle_needed_contacts = NULL;
726 GList *id_needed_contacts = NULL;
729 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
734 empathy_debug (DEBUG_DOMAIN, "Connection ready");
736 empathy_contact_set_handle (priv->user, handle);
739 /* Connect signals */
740 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
741 tp_contact_factory_aliases_changed_cb,
743 G_OBJECT (tp_factory),
745 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
746 tp_contact_factory_avatar_updated_cb,
748 G_OBJECT (tp_factory),
750 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
751 tp_contact_factory_avatar_retrieved_cb,
753 G_OBJECT (tp_factory),
755 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
756 tp_contact_factory_presence_update_cb,
758 G_OBJECT (tp_factory),
760 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
761 tp_contact_factory_capabilities_changed_cb,
763 G_OBJECT (tp_factory),
766 /* Request needed info for all existing contacts */
767 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
768 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
769 for (l = priv->contacts; l; l = l->next) {
770 EmpathyContact *contact;
775 handle = empathy_contact_get_handle (contact);
776 id = empathy_contact_get_id (contact);
778 g_assert (!G_STR_EMPTY (id));
779 g_array_append_val (handle_needed, id);
780 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
781 g_object_ref (contact));
783 if (G_STR_EMPTY (id)) {
784 g_array_append_val (id_needed, handle);
785 id_needed_contacts = g_list_prepend (id_needed_contacts,
786 g_object_ref (contact));
789 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
790 id_needed_contacts = g_list_reverse (id_needed_contacts);
792 tp_cli_connection_call_request_handles (priv->connection,
794 TP_HANDLE_TYPE_CONTACT,
795 (const gchar**) handle_needed->data,
796 tp_contact_factory_request_handles_cb,
797 handle_needed_contacts, tp_contact_factory_list_free,
798 G_OBJECT (tp_factory));
800 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
802 tp_cli_connection_call_inspect_handles (priv->connection,
804 TP_HANDLE_TYPE_CONTACT,
806 tp_contact_factory_inspect_handles_cb,
807 id_needed_contacts, tp_contact_factory_list_free,
808 G_OBJECT (tp_factory));
810 g_array_free (handle_needed, TRUE);
811 g_array_free (id_needed, TRUE);
815 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
817 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
819 /* Get our own handle */
820 tp_cli_connection_call_get_self_handle (priv->connection,
822 tp_contact_factory_got_self_handle_cb,
824 G_OBJECT (tp_factory));
828 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
830 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
832 gboolean connection_ready;
834 if (priv->connection) {
835 /* We already have our connection object */
839 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
844 /* We got a new connection, wait for it to be ready */
845 priv->connection = tp_conn_dup_connection (tp_conn);
846 g_object_unref (tp_conn);
848 g_signal_connect_swapped (priv->connection, "invalidated",
849 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
852 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
853 if (connection_ready) {
854 tp_contact_factory_connection_ready_cb (tp_factory);
856 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
857 G_CALLBACK (tp_contact_factory_connection_ready_cb),
863 tp_contact_factory_status_changed_cb (MissionControl *mc,
864 TpConnectionStatus status,
866 TpConnectionStatusReason reason,
867 const gchar *unique_name,
868 EmpathyTpContactFactory *tp_factory)
870 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
873 account = mc_account_lookup (unique_name);
874 if (account && empathy_account_equal (account, priv->account)) {
875 tp_contact_factory_status_updated (tp_factory);
877 g_object_unref (account);
881 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
882 EmpathyContact *contact)
884 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
886 g_object_weak_ref (G_OBJECT (contact),
887 tp_contact_factory_weak_notify,
889 priv->contacts = g_list_prepend (priv->contacts, contact);
891 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
892 empathy_contact_get_id (contact),
893 empathy_contact_get_handle (contact));
897 tp_contact_factory_hold_handles_cb (TpConnection *connection,
903 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
909 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
911 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
913 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
915 return g_object_ref (priv->user);
919 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
922 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
923 EmpathyContact *contact;
925 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
926 g_return_val_if_fail (id != NULL, NULL);
928 /* Check if the contact already exists */
929 contact = tp_contact_factory_find_by_id (tp_factory, id);
931 return g_object_ref (contact);
934 /* Create new contact */
935 contact = g_object_new (EMPATHY_TYPE_CONTACT,
936 "account", priv->account,
939 tp_contact_factory_add_contact (tp_factory, contact);
942 const gchar *contact_ids[] = {id, NULL};
945 contacts = g_list_prepend (NULL, g_object_ref (contact));
946 tp_cli_connection_call_request_handles (priv->connection,
948 TP_HANDLE_TYPE_CONTACT,
950 tp_contact_factory_request_handles_cb,
951 contacts, tp_contact_factory_list_free,
952 G_OBJECT (tp_factory));
959 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
962 EmpathyContact *contact;
966 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
968 handles = g_array_new (FALSE, FALSE, sizeof (guint));
969 g_array_append_val (handles, handle);
971 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
972 g_array_free (handles, TRUE);
974 contact = contacts ? contacts->data : NULL;
975 g_list_free (contacts);
981 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
982 const GArray *handles)
984 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
985 GList *contacts = NULL;
987 GList *new_contacts = NULL;
990 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
991 g_return_val_if_fail (handles != NULL, NULL);
993 /* Search all contacts we already have */
994 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
995 for (i = 0; i < handles->len; i++) {
996 EmpathyContact *contact;
999 handle = g_array_index (handles, guint, i);
1004 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1006 contacts = g_list_prepend (contacts, g_object_ref (contact));
1008 g_array_append_val (new_handles, handle);
1012 if (new_handles->len == 0) {
1013 g_array_free (new_handles, TRUE);
1017 /* Create new contacts */
1018 for (i = 0; i < new_handles->len; i++) {
1019 EmpathyContact *contact;
1022 handle = g_array_index (new_handles, guint, i);
1024 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1025 "account", priv->account,
1028 tp_contact_factory_add_contact (tp_factory, contact);
1029 contacts = g_list_prepend (contacts, contact);
1030 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1032 new_contacts = g_list_reverse (new_contacts);
1035 /* Get the IDs of all new handles */
1036 tp_cli_connection_call_inspect_handles (priv->connection,
1038 TP_HANDLE_TYPE_CONTACT,
1040 tp_contact_factory_inspect_handles_cb,
1041 new_contacts, tp_contact_factory_list_free,
1042 G_OBJECT (tp_factory));
1044 /* Hold all new handles. */
1045 /* FIXME: Should be unholded when removed from the factory */
1046 tp_cli_connection_call_hold_handles (priv->connection,
1048 TP_HANDLE_TYPE_CONTACT,
1050 tp_contact_factory_hold_handles_cb,
1052 G_OBJECT (tp_factory));
1054 tp_contact_factory_request_everything (tp_factory, new_handles);
1061 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1062 EmpathyContact *contact,
1065 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1066 GHashTable *new_alias;
1069 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1070 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1071 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1078 handle = empathy_contact_get_handle (contact);
1080 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1081 empathy_contact_get_id (contact),
1084 new_alias = g_hash_table_new_full (g_direct_hash,
1089 g_hash_table_insert (new_alias,
1090 GUINT_TO_POINTER (handle),
1093 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1096 tp_contact_factory_set_aliases_cb,
1098 G_OBJECT (tp_factory));
1100 g_hash_table_destroy (new_alias);
1104 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1107 const gchar *mime_type)
1109 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1111 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1117 if (data && size > 0 && size < G_MAXUINT) {
1120 avatar.data = (gchar*) data;
1123 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1124 mc_account_get_unique_name (priv->account));
1126 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1130 tp_contact_factory_set_avatar_cb,
1132 G_OBJECT (tp_factory));
1134 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1135 mc_account_get_unique_name (priv->account));
1137 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1139 tp_contact_factory_clear_avatar_cb,
1141 G_OBJECT (tp_factory));
1146 tp_contact_factory_get_property (GObject *object,
1151 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1155 g_value_set_object (value, priv->account);
1158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1164 tp_contact_factory_set_property (GObject *object,
1166 const GValue *value,
1169 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1173 priv->account = g_object_ref (g_value_get_object (value));
1176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1182 tp_contact_factory_finalize (GObject *object)
1184 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1187 empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1189 mc_account_get_normalized_name (priv->account));
1191 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1192 "AccountStatusChanged",
1193 G_CALLBACK (tp_contact_factory_status_changed_cb),
1196 for (l = priv->contacts; l; l = l->next) {
1197 g_object_weak_unref (G_OBJECT (l->data),
1198 tp_contact_factory_weak_notify,
1202 g_list_free (priv->contacts);
1203 g_object_unref (priv->mc);
1204 g_object_unref (priv->account);
1205 g_object_unref (priv->user);
1207 if (priv->connection) {
1208 g_signal_handlers_disconnect_by_func (priv->connection,
1209 tp_contact_factory_connection_invalidated_cb,
1211 g_object_unref (priv->connection);
1214 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1218 tp_contact_factory_constructor (GType type,
1220 GObjectConstructParam *props)
1222 GObject *tp_factory;
1223 EmpathyTpContactFactoryPriv *priv;
1225 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1226 priv = GET_PRIV (tp_factory);
1228 priv->ready = FALSE;
1229 priv->user = empathy_contact_new (priv->account);
1230 empathy_contact_set_is_user (priv->user, TRUE);
1231 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1232 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1238 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1240 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1242 object_class->finalize = tp_contact_factory_finalize;
1243 object_class->constructor = tp_contact_factory_constructor;
1244 object_class->get_property = tp_contact_factory_get_property;
1245 object_class->set_property = tp_contact_factory_set_property;
1247 /* Construct-only properties */
1248 g_object_class_install_property (object_class,
1250 g_param_spec_object ("account",
1251 "Factory's Account",
1252 "The account associated with the factory",
1255 G_PARAM_CONSTRUCT_ONLY));
1257 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1261 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1263 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1265 priv->mc = empathy_mission_control_new ();
1266 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1267 "AccountStatusChanged",
1268 G_CALLBACK (tp_contact_factory_status_changed_cb),
1272 EmpathyTpContactFactory *
1273 empathy_tp_contact_factory_new (McAccount *account)
1275 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,