1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 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 <libtelepathy/tp-conn.h>
27 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
28 #include <libtelepathy/tp-conn-iface-presence-gen.h>
29 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
30 #include <libtelepathy/tp-conn-iface-capabilities-gen.h>
31 #include <libmissioncontrol/mission-control.h>
33 #include "empathy-contact-factory.h"
34 #include "empathy-utils.h"
35 #include "empathy-debug.h"
37 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
38 EMPATHY_TYPE_CONTACT_FACTORY, EmpathyContactFactoryPriv))
40 #define DEBUG_DOMAIN "ContactFactory"
42 struct _EmpathyContactFactoryPriv {
48 EmpathyContactFactory *factory;
53 DBusGProxy *aliasing_iface;
54 DBusGProxy *avatars_iface;
55 DBusGProxy *presence_iface;
56 DBusGProxy *capabilities_iface;
60 } ContactFactoryAccountData;
63 ContactFactoryAccountData *account_data;
68 ContactFactoryAccountData *account_data;
72 static void empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass);
73 static void empathy_contact_factory_init (EmpathyContactFactory *factory);
75 G_DEFINE_TYPE (EmpathyContactFactory, empathy_contact_factory, G_TYPE_OBJECT);
78 contact_factory_find_by_handle (gconstpointer a,
81 EmpathyContact *contact;
84 contact = EMPATHY_CONTACT (a);
85 handle = GPOINTER_TO_UINT (b);
87 return handle - empathy_contact_get_handle (contact);
90 static EmpathyContact *
91 contact_factory_account_data_find_by_handle (ContactFactoryAccountData *account_data,
96 l = g_list_find_custom (account_data->contacts,
97 GUINT_TO_POINTER (handle),
98 contact_factory_find_by_handle);
100 return l ? l->data : NULL;
104 contact_factory_find_by_id (gconstpointer a,
107 EmpathyContact *contact;
110 contact = EMPATHY_CONTACT (a);
112 return strcmp (id, empathy_contact_get_id (contact));
115 static EmpathyContact *
116 contact_factory_account_data_find_by_id (ContactFactoryAccountData *account_data,
121 l = g_list_find_custom (account_data->contacts,
123 contact_factory_find_by_id);
125 return l ? l->data : NULL;
128 static void contact_factory_account_data_disconnect (ContactFactoryAccountData *account_data);
131 contact_factory_weak_notify (gpointer data,
132 GObject *where_the_object_was)
134 ContactFactoryAccountData *account_data = data;
136 empathy_debug (DEBUG_DOMAIN, "Remove finalized contact %p",
137 where_the_object_was);
139 account_data->contacts = g_list_remove (account_data->contacts,
140 where_the_object_was);
141 if (!account_data->contacts) {
142 EmpathyContactFactoryPriv *priv = GET_PRIV (account_data->factory);
144 g_hash_table_remove (priv->accounts, account_data->account);
149 contact_factory_remove_foreach (gpointer data,
152 ContactFactoryAccountData *account_data = user_data;
153 EmpathyContact *contact = data;
155 g_object_weak_unref (G_OBJECT (contact),
156 contact_factory_weak_notify,
160 static ContactFactoryAccountData *
161 contact_factory_account_data_ref (ContactFactoryAccountData *account_data)
163 account_data->refcount++;
169 contact_factory_account_data_unref (ContactFactoryAccountData *account_data)
171 account_data->refcount--;
172 if (account_data->refcount > 0) {
176 empathy_debug (DEBUG_DOMAIN, "Account data finalized: %p (%s)",
178 mc_account_get_normalized_name (account_data->account));
180 contact_factory_account_data_disconnect (account_data);
182 if (account_data->contacts) {
183 g_list_foreach (account_data->contacts,
184 contact_factory_remove_foreach,
186 g_list_free (account_data->contacts);
189 if (account_data->account) {
190 g_object_unref (account_data->account);
193 if (account_data->tp_conn) {
194 g_object_unref (account_data->tp_conn);
197 g_slice_free (ContactFactoryAccountData, account_data);
201 contact_factory_presences_table_foreach (const gchar *state_str,
202 GHashTable *presences_table,
203 EmpathyPresence **presence)
206 const GValue *message;
208 state = empathy_presence_state_from_str (state_str);
209 if (state == MC_PRESENCE_UNSET) {
214 g_object_unref (*presence);
218 *presence = empathy_presence_new ();
219 empathy_presence_set_state (*presence, state);
221 message = g_hash_table_lookup (presences_table, "message");
222 if (message != NULL) {
223 empathy_presence_set_status (*presence,
224 g_value_get_string (message));
229 contact_factory_parse_presence_foreach (guint handle,
230 GValueArray *presence_struct,
231 ContactFactoryAccountData *account_data)
233 GHashTable *presences_table;
234 EmpathyContact *contact;
235 EmpathyPresence *presence = NULL;
237 contact = contact_factory_account_data_find_by_handle (account_data,
243 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
245 g_hash_table_foreach (presences_table,
246 (GHFunc) contact_factory_presences_table_foreach,
249 empathy_debug (DEBUG_DOMAIN, "Changing presence for contact %s (%d) to %s (%d)",
250 empathy_contact_get_id (contact),
252 presence ? empathy_presence_get_status (presence) : "unset",
253 presence ? empathy_presence_get_state (presence) : MC_PRESENCE_UNSET);
255 empathy_contact_set_presence (contact, presence);
256 g_object_unref (presence);
260 contact_factory_get_presence_cb (DBusGProxy *proxy,
261 GHashTable *handle_table,
265 ContactFactoryAccountData *account_data = user_data;
268 empathy_debug (DEBUG_DOMAIN, "Error getting presence: %s",
273 g_hash_table_foreach (handle_table,
274 (GHFunc) contact_factory_parse_presence_foreach,
277 g_hash_table_destroy (handle_table);
279 contact_factory_account_data_unref (account_data);
283 contact_factory_presence_update_cb (DBusGProxy *proxy,
284 GHashTable *handle_table,
285 ContactFactoryAccountData *account_data)
287 g_hash_table_foreach (handle_table,
288 (GHFunc) contact_factory_parse_presence_foreach,
293 contact_factory_set_aliases_cb (DBusGProxy *proxy,
297 ContactFactoryAccountData *account_data = user_data;
300 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
304 contact_factory_account_data_unref (account_data);
308 contact_factory_request_aliases_cb (DBusGProxy *proxy,
309 gchar **contact_names,
313 RequestAliasesData *data = user_data;
318 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
323 for (name = contact_names; *name; name++) {
324 EmpathyContact *contact;
326 contact = contact_factory_account_data_find_by_handle (data->account_data,
332 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
333 empathy_contact_get_id (contact),
334 data->handles[i], *name);
336 empathy_contact_set_name (contact, *name);
341 g_strfreev (contact_names);
343 contact_factory_account_data_unref (data->account_data);
344 g_free (data->handles);
345 g_slice_free (RequestAliasesData, data);
349 contact_factory_aliases_changed_cb (DBusGProxy *proxy,
350 GPtrArray *renamed_handlers,
353 ContactFactoryAccountData *account_data = user_data;
356 for (i = 0; renamed_handlers->len > i; i++) {
359 GValueArray *renamed_struct;
360 EmpathyContact *contact;
362 renamed_struct = g_ptr_array_index (renamed_handlers, i);
363 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
364 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
365 contact = contact_factory_account_data_find_by_handle (account_data, handle);
368 /* We don't know this contact, skip */
372 if (G_STR_EMPTY (alias)) {
376 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
377 empathy_contact_get_id (contact),
380 empathy_contact_set_name (contact, alias);
385 contact_factory_avatar_retrieved_cb (DBusGProxy *proxy,
392 ContactFactoryAccountData *account_data = user_data;
393 EmpathyContact *contact;
394 EmpathyAvatar *avatar;
396 contact = contact_factory_account_data_find_by_handle (account_data,
402 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
403 empathy_contact_get_id (contact),
406 avatar = empathy_avatar_new (avatar_data->data,
411 empathy_contact_set_avatar (contact, avatar);
412 empathy_avatar_unref (avatar);
416 contact_factory_request_avatars_cb (DBusGProxy *proxy,
420 ContactFactoryAccountData *account_data = user_data;
423 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
427 contact_factory_account_data_unref (account_data);
431 ContactFactoryAccountData *account_data;
436 contact_factory_avatar_maybe_update (ContactFactoryAccountData *account_data,
440 EmpathyContact *contact;
441 EmpathyAvatar *avatar;
443 contact = contact_factory_account_data_find_by_handle (account_data,
449 /* Check if we have an avatar */
450 if (G_STR_EMPTY (token)) {
451 empathy_contact_set_avatar (contact, NULL);
455 /* Check if the avatar changed */
456 avatar = empathy_contact_get_avatar (contact);
457 if (avatar && !empathy_strdiff (avatar->token, token)) {
461 /* The avatar changed, search the new one in the cache */
462 avatar = empathy_avatar_new_from_cache (token);
464 /* Got from cache, use it */
465 empathy_contact_set_avatar (contact, avatar);
466 empathy_avatar_unref (avatar);
470 /* Avatar is not up-to-date, we have to request it. */
475 contact_factory_avatar_tokens_foreach (gpointer key,
479 TokensData *data = user_data;
480 const gchar *token = value;
481 guint handle = GPOINTER_TO_UINT (key);
483 if (!contact_factory_avatar_maybe_update (data->account_data,
485 g_array_append_val (data->handles, handle);
490 contact_factory_get_known_avatar_tokens_cb (DBusGProxy *proxy,
495 ContactFactoryAccountData *account_data = user_data;
499 empathy_debug (DEBUG_DOMAIN,
500 "Error getting known avatars tokens: %s",
505 data.account_data = account_data;
506 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
507 g_hash_table_foreach (tokens,
508 contact_factory_avatar_tokens_foreach,
511 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
512 g_hash_table_size (tokens),
515 /* Request needed avatars */
516 if (data.handles->len > 0) {
517 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
519 contact_factory_request_avatars_cb,
520 contact_factory_account_data_ref (account_data));
523 g_hash_table_destroy (tokens);
524 g_array_free (data.handles, TRUE);
526 contact_factory_account_data_unref (account_data);
530 contact_factory_avatar_updated_cb (DBusGProxy *proxy,
535 ContactFactoryAccountData *account_data = user_data;
538 if (contact_factory_avatar_maybe_update (account_data, handle, new_token)) {
539 /* Avatar was cached, nothing to do */
543 empathy_debug (DEBUG_DOMAIN, "Need to request one avatar");
545 handles = g_array_new (FALSE, FALSE, sizeof (guint));
546 g_array_append_val (handles, handle);
548 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
550 contact_factory_request_avatars_cb,
551 contact_factory_account_data_ref (account_data));
552 g_array_free (handles, TRUE);
556 contact_factory_update_capabilities (ContactFactoryAccountData *account_data,
558 const gchar *channel_type,
562 EmpathyContact *contact;
563 EmpathyCapabilities capabilities;
565 contact = contact_factory_account_data_find_by_handle (account_data,
571 capabilities = empathy_contact_get_capabilities (contact);
573 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
574 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
575 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
576 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
577 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
579 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
580 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
584 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
585 empathy_contact_get_id (contact),
586 empathy_contact_get_handle (contact),
589 empathy_contact_set_capabilities (contact, capabilities);
593 contact_factory_get_capabilities_cb (DBusGProxy *proxy,
594 GPtrArray *capabilities,
598 ContactFactoryAccountData *account_data = user_data;
602 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
607 for (i = 0; i < capabilities->len; i++) {
610 const gchar *channel_type;
614 values = g_ptr_array_index (capabilities, i);
615 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
616 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
617 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
618 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
620 contact_factory_update_capabilities (account_data,
626 g_value_array_free (values);
629 g_ptr_array_free (capabilities, TRUE);
631 contact_factory_account_data_unref (account_data);
635 contact_factory_capabilities_changed_cb (DBusGProxy *proxy,
636 GPtrArray *capabilities,
639 ContactFactoryAccountData *account_data = user_data;
642 for (i = 0; i < capabilities->len; i++) {
645 const gchar *channel_type;
649 values = g_ptr_array_index (capabilities, i);
650 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
651 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
652 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
653 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
655 contact_factory_update_capabilities (account_data,
664 contact_factory_request_everything (ContactFactoryAccountData *account_data,
667 if (account_data->presence_iface) {
668 tp_conn_iface_presence_get_presence_async (account_data->presence_iface,
670 contact_factory_get_presence_cb,
671 contact_factory_account_data_ref (account_data));
674 if (account_data->aliasing_iface) {
675 RequestAliasesData *data;
677 data = g_slice_new (RequestAliasesData);
678 data->account_data = contact_factory_account_data_ref (account_data);
679 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
681 tp_conn_iface_aliasing_request_aliases_async (account_data->aliasing_iface,
683 contact_factory_request_aliases_cb,
687 if (account_data->avatars_iface) {
688 tp_conn_iface_avatars_get_known_avatar_tokens_async (account_data->avatars_iface,
690 contact_factory_get_known_avatar_tokens_cb,
691 contact_factory_account_data_ref (account_data));
694 if (account_data->capabilities_iface) {
695 tp_conn_iface_capabilities_get_capabilities_async (account_data->capabilities_iface,
697 contact_factory_get_capabilities_cb,
698 contact_factory_account_data_ref (account_data));
703 contact_factory_request_handles_cb (DBusGProxy *proxy,
708 RequestHandlesData *data = user_data;
713 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
718 for (l = data->contacts; l; l = l->next) {
721 handle = g_array_index (handles, guint, i);
722 empathy_contact_set_handle (l->data, handle);
723 if (handle == data->account_data->self_handle) {
724 empathy_contact_set_is_user (l->data, TRUE);
730 contact_factory_request_everything (data->account_data, handles);
731 g_array_free (handles, TRUE);
734 g_list_foreach (data->contacts, (GFunc) g_object_unref, NULL);
735 g_list_free (data->contacts);
736 contact_factory_account_data_unref (data->account_data);
737 g_slice_free (RequestHandlesData, data);
741 contact_factory_disconnect_contact_foreach (gpointer data,
744 EmpathyContact *contact = data;
746 empathy_contact_set_presence (contact, NULL);
747 empathy_contact_set_handle (contact, 0);
751 contact_factory_destroy_cb (TpConn *tp_conn,
752 ContactFactoryAccountData *account_data)
754 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
756 g_object_unref (account_data->tp_conn);
757 account_data->tp_conn = NULL;
758 account_data->aliasing_iface = NULL;
759 account_data->avatars_iface = NULL;
760 account_data->presence_iface = NULL;
761 account_data->capabilities_iface = NULL;
763 g_list_foreach (account_data->contacts,
764 contact_factory_disconnect_contact_foreach,
769 contact_factory_account_data_disconnect (ContactFactoryAccountData *account_data)
771 if (account_data->aliasing_iface) {
772 dbus_g_proxy_disconnect_signal (account_data->aliasing_iface,
774 G_CALLBACK (contact_factory_aliases_changed_cb),
777 if (account_data->avatars_iface) {
778 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
780 G_CALLBACK (contact_factory_avatar_updated_cb),
782 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
784 G_CALLBACK (contact_factory_avatar_retrieved_cb),
787 if (account_data->presence_iface) {
788 dbus_g_proxy_disconnect_signal (account_data->presence_iface,
790 G_CALLBACK (contact_factory_presence_update_cb),
793 if (account_data->capabilities_iface) {
794 dbus_g_proxy_disconnect_signal (account_data->capabilities_iface,
795 "CapabilitiesChanged",
796 G_CALLBACK (contact_factory_capabilities_changed_cb),
799 if (account_data->tp_conn) {
800 g_signal_handlers_disconnect_by_func (account_data->tp_conn,
801 contact_factory_destroy_cb,
807 contact_factory_account_data_update (ContactFactoryAccountData *account_data)
809 EmpathyContactFactory *factory = account_data->factory;
810 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
811 McAccount *account = account_data->account;
812 TpConn *tp_conn = NULL;
813 RequestHandlesData *data;
814 const gchar **contact_ids;
817 GError *error = NULL;
819 if (account_data->account) {
822 /* status == 0 means the status is CONNECTED */
823 status = mission_control_get_connection_status (priv->mc,
826 tp_conn = mission_control_get_connection (priv->mc,
832 /* We are not connected anymore, remove the old connection */
833 contact_factory_account_data_disconnect (account_data);
834 if (account_data->tp_conn) {
835 contact_factory_destroy_cb (account_data->tp_conn,
840 else if (account_data->tp_conn) {
841 /* We were connected and we still are connected, nothing
842 * changed so nothing to do. */
843 g_object_unref (tp_conn);
847 /* We got a new connection */
848 account_data->tp_conn = tp_conn;
849 account_data->aliasing_iface = tp_conn_get_interface (tp_conn,
850 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
851 account_data->avatars_iface = tp_conn_get_interface (tp_conn,
852 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
853 account_data->presence_iface = tp_conn_get_interface (tp_conn,
854 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
855 account_data->capabilities_iface = tp_conn_get_interface (tp_conn,
856 TELEPATHY_CONN_IFACE_CAPABILITIES_QUARK);
858 /* Connect signals */
859 if (account_data->aliasing_iface) {
860 dbus_g_proxy_connect_signal (account_data->aliasing_iface,
862 G_CALLBACK (contact_factory_aliases_changed_cb),
865 if (account_data->avatars_iface) {
866 dbus_g_proxy_connect_signal (account_data->avatars_iface,
868 G_CALLBACK (contact_factory_avatar_updated_cb),
870 dbus_g_proxy_connect_signal (account_data->avatars_iface,
872 G_CALLBACK (contact_factory_avatar_retrieved_cb),
875 if (account_data->presence_iface) {
876 dbus_g_proxy_connect_signal (account_data->presence_iface,
878 G_CALLBACK (contact_factory_presence_update_cb),
881 if (account_data->capabilities_iface) {
882 dbus_g_proxy_connect_signal (account_data->capabilities_iface,
883 "CapabilitiesChanged",
884 G_CALLBACK (contact_factory_capabilities_changed_cb),
887 g_signal_connect (tp_conn, "destroy",
888 G_CALLBACK (contact_factory_destroy_cb),
891 /* Get our own handle */
892 if (!tp_conn_get_self_handle (DBUS_G_PROXY (account_data->tp_conn),
893 &account_data->self_handle,
895 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
896 error ? error->message : "No error given");
897 g_clear_error (&error);
900 /* Request new handles for all contacts */
901 if (account_data->contacts) {
902 data = g_slice_new (RequestHandlesData);
903 data->account_data = contact_factory_account_data_ref (account_data);
904 data->contacts = g_list_copy (account_data->contacts);
905 g_list_foreach (data->contacts, (GFunc) g_object_ref, NULL);
907 i = g_list_length (data->contacts);
908 contact_ids = g_new0 (const gchar*, i + 1);
910 for (l = data->contacts; l; l = l->next) {
911 contact_ids[i] = empathy_contact_get_id (l->data);
915 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
916 TP_HANDLE_TYPE_CONTACT,
918 contact_factory_request_handles_cb,
920 g_free (contact_ids);
924 static ContactFactoryAccountData *
925 contact_factory_account_data_new (EmpathyContactFactory *factory,
928 ContactFactoryAccountData *account_data;
930 account_data = g_slice_new0 (ContactFactoryAccountData);
931 account_data->factory = factory;
932 account_data->account = g_object_ref (account);
933 account_data->refcount = 1;
935 contact_factory_account_data_update (account_data);
941 contact_factory_status_changed_cb (MissionControl *mc,
942 TelepathyConnectionStatus status,
944 TelepathyConnectionStatusReason reason,
945 const gchar *unique_name,
946 EmpathyContactFactory *factory)
948 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
949 ContactFactoryAccountData *account_data;
952 account = mc_account_lookup (unique_name);
953 account_data = g_hash_table_lookup (priv->accounts, account);
955 contact_factory_account_data_update (account_data);
957 g_object_unref (account);
960 static ContactFactoryAccountData *
961 contact_factory_account_data_get (EmpathyContactFactory *factory,
964 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
965 ContactFactoryAccountData *account_data;
967 account_data = g_hash_table_lookup (priv->accounts, account);
969 account_data = contact_factory_account_data_new (factory, account);
970 g_hash_table_insert (priv->accounts,
971 g_object_ref (account),
979 contact_factory_account_data_add_contact (ContactFactoryAccountData *account_data,
980 EmpathyContact *contact)
982 g_object_weak_ref (G_OBJECT (contact),
983 contact_factory_weak_notify,
985 account_data->contacts = g_list_prepend (account_data->contacts, contact);
987 if (!account_data->presence_iface) {
988 EmpathyPresence *presence;
990 /* We have no presence iface, set default presence
992 presence = empathy_presence_new_full (MC_PRESENCE_AVAILABLE,
995 empathy_contact_set_presence (contact, presence);
996 g_object_unref (presence);
999 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
1000 empathy_contact_get_id (contact),
1001 empathy_contact_get_handle (contact));
1005 contact_factory_hold_handles_cb (DBusGProxy *proxy,
1010 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
1016 empathy_contact_factory_get_user (EmpathyContactFactory *factory,
1019 ContactFactoryAccountData *account_data;
1021 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1022 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1024 account_data = contact_factory_account_data_get (factory, account);
1026 return empathy_contact_factory_get_from_handle (factory, account,
1027 account_data->self_handle);
1031 empathy_contact_factory_get_from_id (EmpathyContactFactory *factory,
1035 ContactFactoryAccountData *account_data;
1036 EmpathyContact *contact;
1038 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1039 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1040 g_return_val_if_fail (id != NULL, NULL);
1042 /* Check if the contact already exists */
1043 account_data = contact_factory_account_data_get (factory, account);
1044 contact = contact_factory_account_data_find_by_id (account_data, id);
1046 return g_object_ref (contact);
1049 /* Create new contact */
1050 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1054 contact_factory_account_data_add_contact (account_data, contact);
1056 /* If the account is connected, request contact's handle */
1057 if (account_data->tp_conn) {
1058 RequestHandlesData *data;
1059 const gchar *contact_ids[] = {id, NULL};
1061 data = g_slice_new (RequestHandlesData);
1062 data->account_data = contact_factory_account_data_ref (account_data);
1063 data->contacts = g_list_prepend (NULL, g_object_ref (contact));
1064 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1065 TP_HANDLE_TYPE_CONTACT,
1067 contact_factory_request_handles_cb,
1075 empathy_contact_factory_get_from_handle (EmpathyContactFactory *factory,
1079 EmpathyContact *contact;
1083 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1084 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1086 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1087 g_array_append_val (handles, handle);
1089 contacts = empathy_contact_factory_get_from_handles (factory, account, handles);
1090 g_array_free (handles, TRUE);
1092 contact = contacts ? contacts->data : NULL;
1093 g_list_free (contacts);
1099 empathy_contact_factory_get_from_handles (EmpathyContactFactory *factory,
1103 ContactFactoryAccountData *account_data;
1104 GList *contacts = NULL;
1105 GArray *new_handles;
1106 gchar **handles_names;
1108 GError *error = NULL;
1110 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1111 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1112 g_return_val_if_fail (handles != NULL, NULL);
1114 /* Search all contacts we already have */
1115 account_data = contact_factory_account_data_get (factory, account);
1116 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1117 for (i = 0; i < handles->len; i++) {
1118 EmpathyContact *contact;
1121 handle = g_array_index (handles, guint, i);
1126 contact = contact_factory_account_data_find_by_handle (account_data, handle);
1128 contacts = g_list_prepend (contacts, g_object_ref (contact));
1130 g_array_append_val (new_handles, handle);
1134 if (new_handles->len == 0) {
1135 g_array_free (new_handles, TRUE);
1139 /* Get the IDs of all new handles */
1140 if (!tp_conn_inspect_handles (DBUS_G_PROXY (account_data->tp_conn),
1141 TP_HANDLE_TYPE_CONTACT,
1145 empathy_debug (DEBUG_DOMAIN,
1146 "Couldn't inspect contact: %s",
1147 error ? error->message : "No error given");
1148 g_clear_error (&error);
1149 g_array_free (new_handles, TRUE);
1153 /* Create new contacts */
1154 for (i = 0; i < new_handles->len; i++) {
1155 EmpathyContact *contact;
1160 id = handles_names[i];
1161 handle = g_array_index (new_handles, guint, i);
1163 is_user = (handle == account_data->self_handle);
1164 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1170 contact_factory_account_data_add_contact (account_data,
1172 contacts = g_list_prepend (contacts, contact);
1175 g_free (handles_names);
1177 /* Hold all new handles. */
1178 tp_conn_hold_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1179 TP_HANDLE_TYPE_CONTACT,
1181 contact_factory_hold_handles_cb,
1184 contact_factory_request_everything (account_data, new_handles);
1186 g_array_free (new_handles, TRUE);
1192 empathy_contact_factory_set_name (EmpathyContactFactory *factory,
1193 EmpathyContact *contact,
1196 ContactFactoryAccountData *account_data;
1198 GHashTable *new_alias;
1201 g_return_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory));
1202 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1204 account = empathy_contact_get_account (contact);
1205 account_data = contact_factory_account_data_get (factory, account);
1207 if (!account_data->aliasing_iface) {
1211 handle = empathy_contact_get_handle (contact);
1213 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1214 empathy_contact_get_id (contact),
1217 new_alias = g_hash_table_new_full (g_direct_hash,
1222 g_hash_table_insert (new_alias,
1223 GUINT_TO_POINTER (handle),
1226 tp_conn_iface_aliasing_set_aliases_async (account_data->aliasing_iface,
1228 contact_factory_set_aliases_cb,
1229 contact_factory_account_data_ref (account_data));
1231 g_hash_table_destroy (new_alias);
1235 contact_factory_finalize (GObject *object)
1237 EmpathyContactFactoryPriv *priv;
1239 priv = GET_PRIV (object);
1241 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1242 "AccountStatusChanged",
1243 G_CALLBACK (contact_factory_status_changed_cb),
1246 g_hash_table_destroy (priv->accounts);
1247 g_object_unref (priv->mc);
1249 G_OBJECT_CLASS (empathy_contact_factory_parent_class)->finalize (object);
1253 empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass)
1255 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1257 object_class->finalize = contact_factory_finalize;
1259 g_type_class_add_private (object_class, sizeof (EmpathyContactFactoryPriv));
1263 empathy_contact_factory_init (EmpathyContactFactory *factory)
1265 EmpathyContactFactoryPriv *priv;
1267 priv = GET_PRIV (factory);
1269 priv->mc = empathy_mission_control_new ();
1270 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1271 empathy_account_equal,
1273 (GDestroyNotify) contact_factory_account_data_unref);
1275 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1276 "AccountStatusChanged",
1277 G_CALLBACK (contact_factory_status_changed_cb),
1281 EmpathyContactFactory *
1282 empathy_contact_factory_new (void)
1284 static EmpathyContactFactory *factory = NULL;
1287 factory = g_object_new (EMPATHY_TYPE_CONTACT_FACTORY, NULL);
1288 g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
1290 g_object_ref (factory);