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);
593 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
594 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
595 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
598 tp_contact_factory_get_presence_cb,
600 G_OBJECT (tp_factory));
602 dup_handles = g_memdup (handles->data, handles->len * sizeof (guint));
603 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
606 tp_contact_factory_request_aliases_cb,
608 G_OBJECT (tp_factory));
610 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
613 tp_contact_factory_get_known_avatar_tokens_cb,
615 G_OBJECT (tp_factory));
617 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
620 tp_contact_factory_get_capabilities_cb,
622 G_OBJECT (tp_factory));
626 tp_contact_factory_list_free (gpointer data)
630 g_list_foreach (l, (GFunc) g_object_unref, NULL);
635 tp_contact_factory_request_handles_cb (TpConnection *connection,
636 const GArray *handles,
641 GList *contacts = user_data;
646 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
651 for (l = contacts; l; l = l->next) {
654 handle = g_array_index (handles, guint, i);
655 empathy_contact_set_handle (l->data, handle);
660 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
665 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
672 GList *contacts = user_data;
676 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
682 for (l = contacts; l; l = l->next) {
683 empathy_contact_set_id (l->data, *id);
689 tp_contact_factory_disconnect_contact_foreach (gpointer data,
692 EmpathyContact *contact = data;
694 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
695 empathy_contact_set_handle (contact, 0);
699 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
701 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
703 empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
705 g_object_unref (priv->connection);
706 priv->connection = NULL;
708 g_object_notify (G_OBJECT (tp_factory), "ready");
711 g_list_foreach (priv->contacts,
712 tp_contact_factory_disconnect_contact_foreach,
718 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
724 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
726 GArray *handle_needed;
728 GList *handle_needed_contacts = NULL;
729 GList *id_needed_contacts = NULL;
732 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
737 empathy_debug (DEBUG_DOMAIN, "Connection ready");
739 empathy_contact_set_handle (priv->user, handle);
741 g_object_notify (tp_factory, "ready");
743 /* Connect signals */
744 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
745 tp_contact_factory_aliases_changed_cb,
747 G_OBJECT (tp_factory),
749 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
750 tp_contact_factory_avatar_updated_cb,
752 G_OBJECT (tp_factory),
754 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
755 tp_contact_factory_avatar_retrieved_cb,
757 G_OBJECT (tp_factory),
759 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
760 tp_contact_factory_presence_update_cb,
762 G_OBJECT (tp_factory),
764 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
765 tp_contact_factory_capabilities_changed_cb,
767 G_OBJECT (tp_factory),
770 /* Request needed info for all existing contacts */
771 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
772 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
773 for (l = priv->contacts; l; l = l->next) {
774 EmpathyContact *contact;
779 handle = empathy_contact_get_handle (contact);
780 id = empathy_contact_get_id (contact);
782 g_assert (!G_STR_EMPTY (id));
783 g_array_append_val (handle_needed, id);
784 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
785 g_object_ref (contact));
787 if (G_STR_EMPTY (id)) {
788 g_array_append_val (id_needed, handle);
789 id_needed_contacts = g_list_prepend (id_needed_contacts,
790 g_object_ref (contact));
793 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
794 id_needed_contacts = g_list_reverse (id_needed_contacts);
796 tp_cli_connection_call_request_handles (priv->connection,
798 TP_HANDLE_TYPE_CONTACT,
799 (const gchar**) handle_needed->data,
800 tp_contact_factory_request_handles_cb,
801 handle_needed_contacts, tp_contact_factory_list_free,
802 G_OBJECT (tp_factory));
804 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
806 tp_cli_connection_call_inspect_handles (priv->connection,
808 TP_HANDLE_TYPE_CONTACT,
810 tp_contact_factory_inspect_handles_cb,
811 id_needed_contacts, tp_contact_factory_list_free,
812 G_OBJECT (tp_factory));
814 g_array_free (handle_needed, TRUE);
815 g_array_free (id_needed, TRUE);
819 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
821 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
823 /* Get our own handle */
824 tp_cli_connection_call_get_self_handle (priv->connection,
826 tp_contact_factory_got_self_handle_cb,
828 G_OBJECT (tp_factory));
832 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
834 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
836 gboolean connection_ready;
838 if (priv->connection) {
839 /* We already have our connection object */
843 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
848 /* We got a new connection, wait for it to be ready */
849 priv->connection = tp_conn_dup_connection (tp_conn);
850 g_object_unref (tp_conn);
852 g_signal_connect_swapped (priv->connection, "invalidated",
853 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
856 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
857 if (connection_ready) {
858 tp_contact_factory_connection_ready_cb (tp_factory);
860 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
861 G_CALLBACK (tp_contact_factory_connection_ready_cb),
867 tp_contact_factory_status_changed_cb (MissionControl *mc,
868 TpConnectionStatus status,
870 TpConnectionStatusReason reason,
871 const gchar *unique_name,
872 EmpathyTpContactFactory *tp_factory)
874 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
877 account = mc_account_lookup (unique_name);
878 if (account && empathy_account_equal (account, priv->account)) {
879 tp_contact_factory_status_updated (tp_factory);
881 g_object_unref (account);
885 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
886 EmpathyContact *contact)
888 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
890 g_object_weak_ref (G_OBJECT (contact),
891 tp_contact_factory_weak_notify,
893 priv->contacts = g_list_prepend (priv->contacts, contact);
895 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
896 empathy_contact_get_id (contact),
897 empathy_contact_get_handle (contact));
901 tp_contact_factory_hold_handles_cb (TpConnection *connection,
907 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
913 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
915 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
917 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
919 return g_object_ref (priv->user);
923 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
926 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
927 EmpathyContact *contact;
929 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
930 g_return_val_if_fail (id != NULL, NULL);
932 /* Check if the contact already exists */
933 contact = tp_contact_factory_find_by_id (tp_factory, id);
935 return g_object_ref (contact);
938 /* Create new contact */
939 contact = g_object_new (EMPATHY_TYPE_CONTACT,
940 "account", priv->account,
943 tp_contact_factory_add_contact (tp_factory, contact);
946 const gchar *contact_ids[] = {id, NULL};
949 contacts = g_list_prepend (NULL, g_object_ref (contact));
950 tp_cli_connection_call_request_handles (priv->connection,
952 TP_HANDLE_TYPE_CONTACT,
954 tp_contact_factory_request_handles_cb,
955 contacts, tp_contact_factory_list_free,
956 G_OBJECT (tp_factory));
963 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
966 EmpathyContact *contact;
970 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
972 handles = g_array_new (FALSE, FALSE, sizeof (guint));
973 g_array_append_val (handles, handle);
975 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
976 g_array_free (handles, TRUE);
978 contact = contacts ? contacts->data : NULL;
979 g_list_free (contacts);
985 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
986 const GArray *handles)
988 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
989 GList *contacts = NULL;
991 GList *new_contacts = NULL;
994 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
995 g_return_val_if_fail (handles != NULL, NULL);
997 /* Search all contacts we already have */
998 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
999 for (i = 0; i < handles->len; i++) {
1000 EmpathyContact *contact;
1003 handle = g_array_index (handles, guint, i);
1008 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1010 contacts = g_list_prepend (contacts, g_object_ref (contact));
1012 g_array_append_val (new_handles, handle);
1016 if (new_handles->len == 0) {
1017 g_array_free (new_handles, TRUE);
1021 /* Create new contacts */
1022 for (i = 0; i < new_handles->len; i++) {
1023 EmpathyContact *contact;
1026 handle = g_array_index (new_handles, guint, i);
1028 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1029 "account", priv->account,
1032 tp_contact_factory_add_contact (tp_factory, contact);
1033 contacts = g_list_prepend (contacts, contact);
1034 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1036 new_contacts = g_list_reverse (new_contacts);
1039 /* Get the IDs of all new handles */
1040 tp_cli_connection_call_inspect_handles (priv->connection,
1042 TP_HANDLE_TYPE_CONTACT,
1044 tp_contact_factory_inspect_handles_cb,
1045 new_contacts, tp_contact_factory_list_free,
1046 G_OBJECT (tp_factory));
1048 /* Hold all new handles. */
1049 /* FIXME: Should be unholded when removed from the factory */
1050 tp_cli_connection_call_hold_handles (priv->connection,
1052 TP_HANDLE_TYPE_CONTACT,
1054 tp_contact_factory_hold_handles_cb,
1056 G_OBJECT (tp_factory));
1058 tp_contact_factory_request_everything (tp_factory, new_handles);
1065 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1066 EmpathyContact *contact,
1069 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1070 GHashTable *new_alias;
1073 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1074 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1075 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1082 handle = empathy_contact_get_handle (contact);
1084 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1085 empathy_contact_get_id (contact),
1088 new_alias = g_hash_table_new_full (g_direct_hash,
1093 g_hash_table_insert (new_alias,
1094 GUINT_TO_POINTER (handle),
1097 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1100 tp_contact_factory_set_aliases_cb,
1102 G_OBJECT (tp_factory));
1104 g_hash_table_destroy (new_alias);
1108 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1111 const gchar *mime_type)
1113 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1115 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1121 if (data && size > 0 && size < G_MAXUINT) {
1124 avatar.data = (gchar*) data;
1127 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1128 mc_account_get_unique_name (priv->account));
1130 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1134 tp_contact_factory_set_avatar_cb,
1136 G_OBJECT (tp_factory));
1138 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1139 mc_account_get_unique_name (priv->account));
1141 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1143 tp_contact_factory_clear_avatar_cb,
1145 G_OBJECT (tp_factory));
1150 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1152 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1154 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1160 tp_contact_factory_get_property (GObject *object,
1165 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1169 g_value_set_object (value, priv->account);
1172 g_value_set_boolean (value, priv->ready);
1175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1181 tp_contact_factory_set_property (GObject *object,
1183 const GValue *value,
1186 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1190 priv->account = g_object_ref (g_value_get_object (value));
1193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1199 tp_contact_factory_finalize (GObject *object)
1201 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1204 empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1206 mc_account_get_normalized_name (priv->account));
1208 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1209 "AccountStatusChanged",
1210 G_CALLBACK (tp_contact_factory_status_changed_cb),
1213 for (l = priv->contacts; l; l = l->next) {
1214 g_object_weak_unref (G_OBJECT (l->data),
1215 tp_contact_factory_weak_notify,
1219 g_list_free (priv->contacts);
1220 g_object_unref (priv->mc);
1221 g_object_unref (priv->account);
1222 g_object_unref (priv->user);
1224 if (priv->connection) {
1225 g_signal_handlers_disconnect_by_func (priv->connection,
1226 tp_contact_factory_connection_invalidated_cb,
1228 g_object_unref (priv->connection);
1231 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1235 tp_contact_factory_constructor (GType type,
1237 GObjectConstructParam *props)
1239 GObject *tp_factory;
1240 EmpathyTpContactFactoryPriv *priv;
1242 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1243 priv = GET_PRIV (tp_factory);
1245 priv->ready = FALSE;
1246 priv->user = empathy_contact_new (priv->account);
1247 empathy_contact_set_is_user (priv->user, TRUE);
1248 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1249 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1255 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1257 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1259 object_class->finalize = tp_contact_factory_finalize;
1260 object_class->constructor = tp_contact_factory_constructor;
1261 object_class->get_property = tp_contact_factory_get_property;
1262 object_class->set_property = tp_contact_factory_set_property;
1264 g_object_class_install_property (object_class,
1266 g_param_spec_object ("account",
1267 "Factory's Account",
1268 "The account associated with the factory",
1271 G_PARAM_CONSTRUCT_ONLY));
1272 g_object_class_install_property (object_class,
1274 g_param_spec_boolean ("ready",
1275 "Wheter the factor is ready",
1276 "Is the factory ready",
1280 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1284 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1286 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1288 priv->mc = empathy_mission_control_new ();
1289 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1290 "AccountStatusChanged",
1291 G_CALLBACK (tp_contact_factory_status_changed_cb),
1295 EmpathyTpContactFactory *
1296 empathy_tp_contact_factory_new (McAccount *account)
1298 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,