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_set_avatar_cb (DBusGProxy *proxy,
390 ContactFactoryAccountData *account_data = user_data;
393 empathy_debug (DEBUG_DOMAIN, "Error setting avatar: %s",
397 contact_factory_account_data_unref (account_data);
402 contact_factory_clear_avatar_cb (DBusGProxy *proxy,
406 ContactFactoryAccountData *account_data = user_data;
409 empathy_debug (DEBUG_DOMAIN, "Error clearing avatar: %s",
413 contact_factory_account_data_unref (account_data);
417 contact_factory_avatar_retrieved_cb (DBusGProxy *proxy,
424 ContactFactoryAccountData *account_data = user_data;
425 EmpathyContact *contact;
426 EmpathyAvatar *avatar;
428 contact = contact_factory_account_data_find_by_handle (account_data,
434 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
435 empathy_contact_get_id (contact),
438 avatar = empathy_avatar_new (avatar_data->data,
443 empathy_contact_set_avatar (contact, avatar);
444 empathy_avatar_unref (avatar);
448 contact_factory_request_avatars_cb (DBusGProxy *proxy,
452 ContactFactoryAccountData *account_data = user_data;
455 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
459 contact_factory_account_data_unref (account_data);
463 ContactFactoryAccountData *account_data;
468 contact_factory_avatar_maybe_update (ContactFactoryAccountData *account_data,
472 EmpathyContact *contact;
473 EmpathyAvatar *avatar;
475 contact = contact_factory_account_data_find_by_handle (account_data,
481 /* Check if we have an avatar */
482 if (G_STR_EMPTY (token)) {
483 empathy_contact_set_avatar (contact, NULL);
487 /* Check if the avatar changed */
488 avatar = empathy_contact_get_avatar (contact);
489 if (avatar && !empathy_strdiff (avatar->token, token)) {
493 /* The avatar changed, search the new one in the cache */
494 avatar = empathy_avatar_new_from_cache (token);
496 /* Got from cache, use it */
497 empathy_contact_set_avatar (contact, avatar);
498 empathy_avatar_unref (avatar);
502 /* Avatar is not up-to-date, we have to request it. */
507 contact_factory_avatar_tokens_foreach (gpointer key,
511 TokensData *data = user_data;
512 const gchar *token = value;
513 guint handle = GPOINTER_TO_UINT (key);
515 if (!contact_factory_avatar_maybe_update (data->account_data,
517 g_array_append_val (data->handles, handle);
522 contact_factory_get_known_avatar_tokens_cb (DBusGProxy *proxy,
527 ContactFactoryAccountData *account_data = user_data;
531 empathy_debug (DEBUG_DOMAIN,
532 "Error getting known avatars tokens: %s",
537 data.account_data = account_data;
538 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
539 g_hash_table_foreach (tokens,
540 contact_factory_avatar_tokens_foreach,
543 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
544 g_hash_table_size (tokens),
547 /* Request needed avatars */
548 if (data.handles->len > 0) {
549 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
551 contact_factory_request_avatars_cb,
552 contact_factory_account_data_ref (account_data));
555 g_hash_table_destroy (tokens);
556 g_array_free (data.handles, TRUE);
558 contact_factory_account_data_unref (account_data);
562 contact_factory_avatar_updated_cb (DBusGProxy *proxy,
567 ContactFactoryAccountData *account_data = user_data;
570 if (contact_factory_avatar_maybe_update (account_data, handle, new_token)) {
571 /* Avatar was cached, nothing to do */
575 empathy_debug (DEBUG_DOMAIN, "Need to request avatar for token %s",
578 handles = g_array_new (FALSE, FALSE, sizeof (guint));
579 g_array_append_val (handles, handle);
581 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
583 contact_factory_request_avatars_cb,
584 contact_factory_account_data_ref (account_data));
585 g_array_free (handles, TRUE);
589 contact_factory_update_capabilities (ContactFactoryAccountData *account_data,
591 const gchar *channel_type,
595 EmpathyContact *contact;
596 EmpathyCapabilities capabilities;
598 contact = contact_factory_account_data_find_by_handle (account_data,
604 capabilities = empathy_contact_get_capabilities (contact);
606 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
607 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
608 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
609 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
610 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
612 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
613 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
617 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
618 empathy_contact_get_id (contact),
619 empathy_contact_get_handle (contact),
622 empathy_contact_set_capabilities (contact, capabilities);
626 contact_factory_get_capabilities_cb (DBusGProxy *proxy,
627 GPtrArray *capabilities,
631 ContactFactoryAccountData *account_data = user_data;
635 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
640 for (i = 0; i < capabilities->len; i++) {
643 const gchar *channel_type;
647 values = g_ptr_array_index (capabilities, i);
648 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
649 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
650 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
651 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
653 contact_factory_update_capabilities (account_data,
659 g_value_array_free (values);
662 g_ptr_array_free (capabilities, TRUE);
664 contact_factory_account_data_unref (account_data);
668 contact_factory_capabilities_changed_cb (DBusGProxy *proxy,
669 GPtrArray *capabilities,
672 ContactFactoryAccountData *account_data = user_data;
675 for (i = 0; i < capabilities->len; i++) {
678 const gchar *channel_type;
682 values = g_ptr_array_index (capabilities, i);
683 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
684 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
685 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
686 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
688 contact_factory_update_capabilities (account_data,
697 contact_factory_request_everything (ContactFactoryAccountData *account_data,
700 if (account_data->presence_iface) {
701 tp_conn_iface_presence_get_presence_async (account_data->presence_iface,
703 contact_factory_get_presence_cb,
704 contact_factory_account_data_ref (account_data));
707 if (account_data->aliasing_iface) {
708 RequestAliasesData *data;
710 data = g_slice_new (RequestAliasesData);
711 data->account_data = contact_factory_account_data_ref (account_data);
712 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
714 tp_conn_iface_aliasing_request_aliases_async (account_data->aliasing_iface,
716 contact_factory_request_aliases_cb,
720 if (account_data->avatars_iface) {
721 tp_conn_iface_avatars_get_known_avatar_tokens_async (account_data->avatars_iface,
723 contact_factory_get_known_avatar_tokens_cb,
724 contact_factory_account_data_ref (account_data));
727 if (account_data->capabilities_iface) {
728 tp_conn_iface_capabilities_get_capabilities_async (account_data->capabilities_iface,
730 contact_factory_get_capabilities_cb,
731 contact_factory_account_data_ref (account_data));
736 contact_factory_request_handles_cb (DBusGProxy *proxy,
741 RequestHandlesData *data = user_data;
746 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
751 for (l = data->contacts; l; l = l->next) {
754 handle = g_array_index (handles, guint, i);
755 empathy_contact_set_handle (l->data, handle);
756 if (handle == data->account_data->self_handle) {
757 empathy_contact_set_is_user (l->data, TRUE);
763 contact_factory_request_everything (data->account_data, handles);
764 g_array_free (handles, TRUE);
767 g_list_foreach (data->contacts, (GFunc) g_object_unref, NULL);
768 g_list_free (data->contacts);
769 contact_factory_account_data_unref (data->account_data);
770 g_slice_free (RequestHandlesData, data);
774 contact_factory_disconnect_contact_foreach (gpointer data,
777 EmpathyContact *contact = data;
779 empathy_contact_set_presence (contact, NULL);
780 empathy_contact_set_handle (contact, 0);
784 contact_factory_destroy_cb (TpConn *tp_conn,
785 ContactFactoryAccountData *account_data)
787 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
789 g_object_unref (account_data->tp_conn);
790 account_data->tp_conn = NULL;
791 account_data->aliasing_iface = NULL;
792 account_data->avatars_iface = NULL;
793 account_data->presence_iface = NULL;
794 account_data->capabilities_iface = NULL;
796 g_list_foreach (account_data->contacts,
797 contact_factory_disconnect_contact_foreach,
802 contact_factory_account_data_disconnect (ContactFactoryAccountData *account_data)
804 if (account_data->aliasing_iface) {
805 dbus_g_proxy_disconnect_signal (account_data->aliasing_iface,
807 G_CALLBACK (contact_factory_aliases_changed_cb),
810 if (account_data->avatars_iface) {
811 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
813 G_CALLBACK (contact_factory_avatar_updated_cb),
815 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
817 G_CALLBACK (contact_factory_avatar_retrieved_cb),
820 if (account_data->presence_iface) {
821 dbus_g_proxy_disconnect_signal (account_data->presence_iface,
823 G_CALLBACK (contact_factory_presence_update_cb),
826 if (account_data->capabilities_iface) {
827 dbus_g_proxy_disconnect_signal (account_data->capabilities_iface,
828 "CapabilitiesChanged",
829 G_CALLBACK (contact_factory_capabilities_changed_cb),
832 if (account_data->tp_conn) {
833 g_signal_handlers_disconnect_by_func (account_data->tp_conn,
834 contact_factory_destroy_cb,
840 contact_factory_account_data_update (ContactFactoryAccountData *account_data)
842 EmpathyContactFactory *factory = account_data->factory;
843 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
844 McAccount *account = account_data->account;
845 TpConn *tp_conn = NULL;
846 RequestHandlesData *data;
847 const gchar **contact_ids;
850 GError *error = NULL;
852 if (account_data->account) {
855 /* status == 0 means the status is CONNECTED */
856 status = mission_control_get_connection_status (priv->mc,
859 tp_conn = mission_control_get_connection (priv->mc,
865 /* We are not connected anymore, remove the old connection */
866 contact_factory_account_data_disconnect (account_data);
867 if (account_data->tp_conn) {
868 contact_factory_destroy_cb (account_data->tp_conn,
873 else if (account_data->tp_conn) {
874 /* We were connected and we still are connected, nothing
875 * changed so nothing to do. */
876 g_object_unref (tp_conn);
880 /* We got a new connection */
881 account_data->tp_conn = tp_conn;
882 account_data->aliasing_iface = tp_conn_get_interface (tp_conn,
883 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
884 account_data->avatars_iface = tp_conn_get_interface (tp_conn,
885 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
886 account_data->presence_iface = tp_conn_get_interface (tp_conn,
887 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
888 account_data->capabilities_iface = tp_conn_get_interface (tp_conn,
889 TELEPATHY_CONN_IFACE_CAPABILITIES_QUARK);
891 /* Connect signals */
892 if (account_data->aliasing_iface) {
893 dbus_g_proxy_connect_signal (account_data->aliasing_iface,
895 G_CALLBACK (contact_factory_aliases_changed_cb),
898 if (account_data->avatars_iface) {
899 dbus_g_proxy_connect_signal (account_data->avatars_iface,
901 G_CALLBACK (contact_factory_avatar_updated_cb),
903 dbus_g_proxy_connect_signal (account_data->avatars_iface,
905 G_CALLBACK (contact_factory_avatar_retrieved_cb),
908 if (account_data->presence_iface) {
909 dbus_g_proxy_connect_signal (account_data->presence_iface,
911 G_CALLBACK (contact_factory_presence_update_cb),
914 if (account_data->capabilities_iface) {
915 dbus_g_proxy_connect_signal (account_data->capabilities_iface,
916 "CapabilitiesChanged",
917 G_CALLBACK (contact_factory_capabilities_changed_cb),
920 g_signal_connect (tp_conn, "destroy",
921 G_CALLBACK (contact_factory_destroy_cb),
924 /* Get our own handle */
925 if (!tp_conn_get_self_handle (DBUS_G_PROXY (account_data->tp_conn),
926 &account_data->self_handle,
928 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
929 error ? error->message : "No error given");
930 g_clear_error (&error);
933 /* Request new handles for all contacts */
934 if (account_data->contacts) {
935 data = g_slice_new (RequestHandlesData);
936 data->account_data = contact_factory_account_data_ref (account_data);
937 data->contacts = g_list_copy (account_data->contacts);
938 g_list_foreach (data->contacts, (GFunc) g_object_ref, NULL);
940 i = g_list_length (data->contacts);
941 contact_ids = g_new0 (const gchar*, i + 1);
943 for (l = data->contacts; l; l = l->next) {
944 contact_ids[i] = empathy_contact_get_id (l->data);
948 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
949 TP_HANDLE_TYPE_CONTACT,
951 contact_factory_request_handles_cb,
953 g_free (contact_ids);
957 static ContactFactoryAccountData *
958 contact_factory_account_data_new (EmpathyContactFactory *factory,
961 ContactFactoryAccountData *account_data;
963 account_data = g_slice_new0 (ContactFactoryAccountData);
964 account_data->factory = factory;
965 account_data->account = g_object_ref (account);
966 account_data->refcount = 1;
968 contact_factory_account_data_update (account_data);
974 contact_factory_status_changed_cb (MissionControl *mc,
975 TelepathyConnectionStatus status,
977 TelepathyConnectionStatusReason reason,
978 const gchar *unique_name,
979 EmpathyContactFactory *factory)
981 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
982 ContactFactoryAccountData *account_data;
985 account = mc_account_lookup (unique_name);
986 account_data = g_hash_table_lookup (priv->accounts, account);
988 contact_factory_account_data_update (account_data);
990 g_object_unref (account);
993 static ContactFactoryAccountData *
994 contact_factory_account_data_get (EmpathyContactFactory *factory,
997 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
998 ContactFactoryAccountData *account_data;
1000 account_data = g_hash_table_lookup (priv->accounts, account);
1001 if (!account_data) {
1002 account_data = contact_factory_account_data_new (factory, account);
1003 g_hash_table_insert (priv->accounts,
1004 g_object_ref (account),
1008 return account_data;
1012 contact_factory_account_data_add_contact (ContactFactoryAccountData *account_data,
1013 EmpathyContact *contact)
1015 g_object_weak_ref (G_OBJECT (contact),
1016 contact_factory_weak_notify,
1018 account_data->contacts = g_list_prepend (account_data->contacts, contact);
1020 if (!account_data->presence_iface) {
1021 EmpathyPresence *presence;
1023 /* We have no presence iface, set default presence
1025 presence = empathy_presence_new_full (MC_PRESENCE_AVAILABLE,
1028 empathy_contact_set_presence (contact, presence);
1029 g_object_unref (presence);
1032 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
1033 empathy_contact_get_id (contact),
1034 empathy_contact_get_handle (contact));
1038 contact_factory_hold_handles_cb (DBusGProxy *proxy,
1043 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
1049 empathy_contact_factory_get_user (EmpathyContactFactory *factory,
1052 ContactFactoryAccountData *account_data;
1054 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1055 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1057 account_data = contact_factory_account_data_get (factory, account);
1059 return empathy_contact_factory_get_from_handle (factory, account,
1060 account_data->self_handle);
1064 empathy_contact_factory_get_from_id (EmpathyContactFactory *factory,
1068 ContactFactoryAccountData *account_data;
1069 EmpathyContact *contact;
1071 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1072 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1073 g_return_val_if_fail (id != NULL, NULL);
1075 /* Check if the contact already exists */
1076 account_data = contact_factory_account_data_get (factory, account);
1077 contact = contact_factory_account_data_find_by_id (account_data, id);
1079 return g_object_ref (contact);
1082 /* Create new contact */
1083 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1087 contact_factory_account_data_add_contact (account_data, contact);
1089 /* If the account is connected, request contact's handle */
1090 if (account_data->tp_conn) {
1091 RequestHandlesData *data;
1092 const gchar *contact_ids[] = {id, NULL};
1094 data = g_slice_new (RequestHandlesData);
1095 data->account_data = contact_factory_account_data_ref (account_data);
1096 data->contacts = g_list_prepend (NULL, g_object_ref (contact));
1097 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1098 TP_HANDLE_TYPE_CONTACT,
1100 contact_factory_request_handles_cb,
1108 empathy_contact_factory_get_from_handle (EmpathyContactFactory *factory,
1112 EmpathyContact *contact;
1116 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1117 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1119 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1120 g_array_append_val (handles, handle);
1122 contacts = empathy_contact_factory_get_from_handles (factory, account, handles);
1123 g_array_free (handles, TRUE);
1125 contact = contacts ? contacts->data : NULL;
1126 g_list_free (contacts);
1132 empathy_contact_factory_get_from_handles (EmpathyContactFactory *factory,
1136 ContactFactoryAccountData *account_data;
1137 GList *contacts = NULL;
1138 GArray *new_handles;
1139 gchar **handles_names;
1141 GError *error = NULL;
1143 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1144 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1145 g_return_val_if_fail (handles != NULL, NULL);
1147 /* Search all contacts we already have */
1148 account_data = contact_factory_account_data_get (factory, account);
1149 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1150 for (i = 0; i < handles->len; i++) {
1151 EmpathyContact *contact;
1154 handle = g_array_index (handles, guint, i);
1159 contact = contact_factory_account_data_find_by_handle (account_data, handle);
1161 contacts = g_list_prepend (contacts, g_object_ref (contact));
1163 g_array_append_val (new_handles, handle);
1167 if (new_handles->len == 0) {
1168 g_array_free (new_handles, TRUE);
1172 /* Get the IDs of all new handles */
1173 if (!tp_conn_inspect_handles (DBUS_G_PROXY (account_data->tp_conn),
1174 TP_HANDLE_TYPE_CONTACT,
1178 empathy_debug (DEBUG_DOMAIN,
1179 "Couldn't inspect contact: %s",
1180 error ? error->message : "No error given");
1181 g_clear_error (&error);
1182 g_array_free (new_handles, TRUE);
1186 /* Create new contacts */
1187 for (i = 0; i < new_handles->len; i++) {
1188 EmpathyContact *contact;
1193 id = handles_names[i];
1194 handle = g_array_index (new_handles, guint, i);
1196 is_user = (handle == account_data->self_handle);
1197 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1203 contact_factory_account_data_add_contact (account_data,
1205 contacts = g_list_prepend (contacts, contact);
1208 g_free (handles_names);
1210 /* Hold all new handles. */
1211 tp_conn_hold_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1212 TP_HANDLE_TYPE_CONTACT,
1214 contact_factory_hold_handles_cb,
1217 contact_factory_request_everything (account_data, new_handles);
1219 g_array_free (new_handles, TRUE);
1225 empathy_contact_factory_set_alias (EmpathyContactFactory *factory,
1226 EmpathyContact *contact,
1229 ContactFactoryAccountData *account_data;
1231 GHashTable *new_alias;
1234 g_return_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory));
1235 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1237 account = empathy_contact_get_account (contact);
1238 account_data = contact_factory_account_data_get (factory, account);
1240 if (!account_data->aliasing_iface) {
1244 handle = empathy_contact_get_handle (contact);
1246 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1247 empathy_contact_get_id (contact),
1250 new_alias = g_hash_table_new_full (g_direct_hash,
1255 g_hash_table_insert (new_alias,
1256 GUINT_TO_POINTER (handle),
1259 tp_conn_iface_aliasing_set_aliases_async (account_data->aliasing_iface,
1261 contact_factory_set_aliases_cb,
1262 contact_factory_account_data_ref (account_data));
1264 g_hash_table_destroy (new_alias);
1268 empathy_contact_factory_set_avatar (EmpathyContactFactory *factory,
1272 const gchar *mime_type)
1274 ContactFactoryAccountData *account_data;
1276 g_return_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory));
1277 g_return_if_fail (MC_IS_ACCOUNT (account));
1279 account_data = contact_factory_account_data_get (factory, account);
1281 if (!account_data->avatars_iface) {
1285 if (data && size > 0 && size < G_MAXUINT) {
1288 avatar.data = (gchar*) data;
1291 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1292 mc_account_get_unique_name (account));
1294 tp_conn_iface_avatars_set_avatar_async (account_data->avatars_iface,
1297 contact_factory_set_avatar_cb,
1298 contact_factory_account_data_ref (account_data));
1300 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1301 mc_account_get_unique_name (account));
1302 tp_conn_iface_avatars_clear_avatar_async (account_data->avatars_iface,
1303 contact_factory_clear_avatar_cb,
1304 contact_factory_account_data_ref (account_data));
1309 contact_factory_finalize (GObject *object)
1311 EmpathyContactFactoryPriv *priv;
1313 priv = GET_PRIV (object);
1315 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1316 "AccountStatusChanged",
1317 G_CALLBACK (contact_factory_status_changed_cb),
1320 g_hash_table_destroy (priv->accounts);
1321 g_object_unref (priv->mc);
1323 G_OBJECT_CLASS (empathy_contact_factory_parent_class)->finalize (object);
1327 empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass)
1329 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1331 object_class->finalize = contact_factory_finalize;
1333 g_type_class_add_private (object_class, sizeof (EmpathyContactFactoryPriv));
1337 empathy_contact_factory_init (EmpathyContactFactory *factory)
1339 EmpathyContactFactoryPriv *priv;
1341 priv = GET_PRIV (factory);
1343 priv->mc = empathy_mission_control_new ();
1344 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1345 empathy_account_equal,
1347 (GDestroyNotify) contact_factory_account_data_unref);
1349 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1350 "AccountStatusChanged",
1351 G_CALLBACK (contact_factory_status_changed_cb),
1355 EmpathyContactFactory *
1356 empathy_contact_factory_new (void)
1358 static EmpathyContactFactory *factory = NULL;
1361 factory = g_object_new (EMPATHY_TYPE_CONTACT_FACTORY, NULL);
1362 g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
1364 g_object_ref (factory);