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_avatar_retrieved_cb (DBusGProxy *proxy,
409 ContactFactoryAccountData *account_data = user_data;
410 EmpathyContact *contact;
411 EmpathyAvatar *avatar;
413 contact = contact_factory_account_data_find_by_handle (account_data,
419 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
420 empathy_contact_get_id (contact),
423 avatar = empathy_avatar_new (avatar_data->data,
428 empathy_contact_set_avatar (contact, avatar);
429 empathy_avatar_unref (avatar);
433 contact_factory_request_avatars_cb (DBusGProxy *proxy,
437 ContactFactoryAccountData *account_data = user_data;
440 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
444 contact_factory_account_data_unref (account_data);
448 ContactFactoryAccountData *account_data;
453 contact_factory_avatar_maybe_update (ContactFactoryAccountData *account_data,
457 EmpathyContact *contact;
458 EmpathyAvatar *avatar;
460 contact = contact_factory_account_data_find_by_handle (account_data,
466 /* Check if we have an avatar */
467 if (G_STR_EMPTY (token)) {
468 empathy_contact_set_avatar (contact, NULL);
472 /* Check if the avatar changed */
473 avatar = empathy_contact_get_avatar (contact);
474 if (avatar && !empathy_strdiff (avatar->token, token)) {
478 /* The avatar changed, search the new one in the cache */
479 avatar = empathy_avatar_new_from_cache (token);
481 /* Got from cache, use it */
482 empathy_contact_set_avatar (contact, avatar);
483 empathy_avatar_unref (avatar);
487 /* Avatar is not up-to-date, we have to request it. */
492 contact_factory_avatar_tokens_foreach (gpointer key,
496 TokensData *data = user_data;
497 const gchar *token = value;
498 guint handle = GPOINTER_TO_UINT (key);
500 if (!contact_factory_avatar_maybe_update (data->account_data,
502 g_array_append_val (data->handles, handle);
507 contact_factory_get_known_avatar_tokens_cb (DBusGProxy *proxy,
512 ContactFactoryAccountData *account_data = user_data;
516 empathy_debug (DEBUG_DOMAIN,
517 "Error getting known avatars tokens: %s",
522 data.account_data = account_data;
523 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
524 g_hash_table_foreach (tokens,
525 contact_factory_avatar_tokens_foreach,
528 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
529 g_hash_table_size (tokens),
532 /* Request needed avatars */
533 if (data.handles->len > 0) {
534 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
536 contact_factory_request_avatars_cb,
537 contact_factory_account_data_ref (account_data));
540 g_hash_table_destroy (tokens);
541 g_array_free (data.handles, TRUE);
543 contact_factory_account_data_unref (account_data);
547 contact_factory_avatar_updated_cb (DBusGProxy *proxy,
552 ContactFactoryAccountData *account_data = user_data;
555 if (contact_factory_avatar_maybe_update (account_data, handle, new_token)) {
556 /* Avatar was cached, nothing to do */
560 empathy_debug (DEBUG_DOMAIN, "Need to request one avatar");
562 handles = g_array_new (FALSE, FALSE, sizeof (guint));
563 g_array_append_val (handles, handle);
565 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
567 contact_factory_request_avatars_cb,
568 contact_factory_account_data_ref (account_data));
569 g_array_free (handles, TRUE);
573 contact_factory_update_capabilities (ContactFactoryAccountData *account_data,
575 const gchar *channel_type,
579 EmpathyContact *contact;
580 EmpathyCapabilities capabilities;
582 contact = contact_factory_account_data_find_by_handle (account_data,
588 capabilities = empathy_contact_get_capabilities (contact);
590 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
591 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
592 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
593 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
594 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
596 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
597 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
601 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
602 empathy_contact_get_id (contact),
603 empathy_contact_get_handle (contact),
606 empathy_contact_set_capabilities (contact, capabilities);
610 contact_factory_get_capabilities_cb (DBusGProxy *proxy,
611 GPtrArray *capabilities,
615 ContactFactoryAccountData *account_data = user_data;
619 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
624 for (i = 0; i < capabilities->len; i++) {
627 const gchar *channel_type;
631 values = g_ptr_array_index (capabilities, i);
632 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
633 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
634 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
635 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
637 contact_factory_update_capabilities (account_data,
643 g_value_array_free (values);
646 g_ptr_array_free (capabilities, TRUE);
648 contact_factory_account_data_unref (account_data);
652 contact_factory_capabilities_changed_cb (DBusGProxy *proxy,
653 GPtrArray *capabilities,
656 ContactFactoryAccountData *account_data = user_data;
659 for (i = 0; i < capabilities->len; i++) {
662 const gchar *channel_type;
666 values = g_ptr_array_index (capabilities, i);
667 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
668 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
669 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
670 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
672 contact_factory_update_capabilities (account_data,
681 contact_factory_request_everything (ContactFactoryAccountData *account_data,
684 if (account_data->presence_iface) {
685 tp_conn_iface_presence_get_presence_async (account_data->presence_iface,
687 contact_factory_get_presence_cb,
688 contact_factory_account_data_ref (account_data));
691 if (account_data->aliasing_iface) {
692 RequestAliasesData *data;
694 data = g_slice_new (RequestAliasesData);
695 data->account_data = contact_factory_account_data_ref (account_data);
696 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
698 tp_conn_iface_aliasing_request_aliases_async (account_data->aliasing_iface,
700 contact_factory_request_aliases_cb,
704 if (account_data->avatars_iface) {
705 tp_conn_iface_avatars_get_known_avatar_tokens_async (account_data->avatars_iface,
707 contact_factory_get_known_avatar_tokens_cb,
708 contact_factory_account_data_ref (account_data));
711 if (account_data->capabilities_iface) {
712 tp_conn_iface_capabilities_get_capabilities_async (account_data->capabilities_iface,
714 contact_factory_get_capabilities_cb,
715 contact_factory_account_data_ref (account_data));
720 contact_factory_request_handles_cb (DBusGProxy *proxy,
725 RequestHandlesData *data = user_data;
730 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
735 for (l = data->contacts; l; l = l->next) {
738 handle = g_array_index (handles, guint, i);
739 empathy_contact_set_handle (l->data, handle);
740 if (handle == data->account_data->self_handle) {
741 empathy_contact_set_is_user (l->data, TRUE);
747 contact_factory_request_everything (data->account_data, handles);
748 g_array_free (handles, TRUE);
751 g_list_foreach (data->contacts, (GFunc) g_object_unref, NULL);
752 g_list_free (data->contacts);
753 contact_factory_account_data_unref (data->account_data);
754 g_slice_free (RequestHandlesData, data);
758 contact_factory_disconnect_contact_foreach (gpointer data,
761 EmpathyContact *contact = data;
763 empathy_contact_set_presence (contact, NULL);
764 empathy_contact_set_handle (contact, 0);
768 contact_factory_destroy_cb (TpConn *tp_conn,
769 ContactFactoryAccountData *account_data)
771 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
773 g_object_unref (account_data->tp_conn);
774 account_data->tp_conn = NULL;
775 account_data->aliasing_iface = NULL;
776 account_data->avatars_iface = NULL;
777 account_data->presence_iface = NULL;
778 account_data->capabilities_iface = NULL;
780 g_list_foreach (account_data->contacts,
781 contact_factory_disconnect_contact_foreach,
786 contact_factory_account_data_disconnect (ContactFactoryAccountData *account_data)
788 if (account_data->aliasing_iface) {
789 dbus_g_proxy_disconnect_signal (account_data->aliasing_iface,
791 G_CALLBACK (contact_factory_aliases_changed_cb),
794 if (account_data->avatars_iface) {
795 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
797 G_CALLBACK (contact_factory_avatar_updated_cb),
799 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
801 G_CALLBACK (contact_factory_avatar_retrieved_cb),
804 if (account_data->presence_iface) {
805 dbus_g_proxy_disconnect_signal (account_data->presence_iface,
807 G_CALLBACK (contact_factory_presence_update_cb),
810 if (account_data->capabilities_iface) {
811 dbus_g_proxy_disconnect_signal (account_data->capabilities_iface,
812 "CapabilitiesChanged",
813 G_CALLBACK (contact_factory_capabilities_changed_cb),
816 if (account_data->tp_conn) {
817 g_signal_handlers_disconnect_by_func (account_data->tp_conn,
818 contact_factory_destroy_cb,
824 contact_factory_account_data_update (ContactFactoryAccountData *account_data)
826 EmpathyContactFactory *factory = account_data->factory;
827 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
828 McAccount *account = account_data->account;
829 TpConn *tp_conn = NULL;
830 RequestHandlesData *data;
831 const gchar **contact_ids;
834 GError *error = NULL;
836 if (account_data->account) {
839 /* status == 0 means the status is CONNECTED */
840 status = mission_control_get_connection_status (priv->mc,
843 tp_conn = mission_control_get_connection (priv->mc,
849 /* We are not connected anymore, remove the old connection */
850 contact_factory_account_data_disconnect (account_data);
851 if (account_data->tp_conn) {
852 contact_factory_destroy_cb (account_data->tp_conn,
857 else if (account_data->tp_conn) {
858 /* We were connected and we still are connected, nothing
859 * changed so nothing to do. */
860 g_object_unref (tp_conn);
864 /* We got a new connection */
865 account_data->tp_conn = tp_conn;
866 account_data->aliasing_iface = tp_conn_get_interface (tp_conn,
867 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
868 account_data->avatars_iface = tp_conn_get_interface (tp_conn,
869 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
870 account_data->presence_iface = tp_conn_get_interface (tp_conn,
871 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
872 account_data->capabilities_iface = tp_conn_get_interface (tp_conn,
873 TELEPATHY_CONN_IFACE_CAPABILITIES_QUARK);
875 /* Connect signals */
876 if (account_data->aliasing_iface) {
877 dbus_g_proxy_connect_signal (account_data->aliasing_iface,
879 G_CALLBACK (contact_factory_aliases_changed_cb),
882 if (account_data->avatars_iface) {
883 dbus_g_proxy_connect_signal (account_data->avatars_iface,
885 G_CALLBACK (contact_factory_avatar_updated_cb),
887 dbus_g_proxy_connect_signal (account_data->avatars_iface,
889 G_CALLBACK (contact_factory_avatar_retrieved_cb),
892 if (account_data->presence_iface) {
893 dbus_g_proxy_connect_signal (account_data->presence_iface,
895 G_CALLBACK (contact_factory_presence_update_cb),
898 if (account_data->capabilities_iface) {
899 dbus_g_proxy_connect_signal (account_data->capabilities_iface,
900 "CapabilitiesChanged",
901 G_CALLBACK (contact_factory_capabilities_changed_cb),
904 g_signal_connect (tp_conn, "destroy",
905 G_CALLBACK (contact_factory_destroy_cb),
908 /* Get our own handle */
909 if (!tp_conn_get_self_handle (DBUS_G_PROXY (account_data->tp_conn),
910 &account_data->self_handle,
912 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
913 error ? error->message : "No error given");
914 g_clear_error (&error);
917 /* Request new handles for all contacts */
918 if (account_data->contacts) {
919 data = g_slice_new (RequestHandlesData);
920 data->account_data = contact_factory_account_data_ref (account_data);
921 data->contacts = g_list_copy (account_data->contacts);
922 g_list_foreach (data->contacts, (GFunc) g_object_ref, NULL);
924 i = g_list_length (data->contacts);
925 contact_ids = g_new0 (const gchar*, i + 1);
927 for (l = data->contacts; l; l = l->next) {
928 contact_ids[i] = empathy_contact_get_id (l->data);
932 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
933 TP_HANDLE_TYPE_CONTACT,
935 contact_factory_request_handles_cb,
937 g_free (contact_ids);
941 static ContactFactoryAccountData *
942 contact_factory_account_data_new (EmpathyContactFactory *factory,
945 ContactFactoryAccountData *account_data;
947 account_data = g_slice_new0 (ContactFactoryAccountData);
948 account_data->factory = factory;
949 account_data->account = g_object_ref (account);
950 account_data->refcount = 1;
952 contact_factory_account_data_update (account_data);
958 contact_factory_status_changed_cb (MissionControl *mc,
959 TelepathyConnectionStatus status,
961 TelepathyConnectionStatusReason reason,
962 const gchar *unique_name,
963 EmpathyContactFactory *factory)
965 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
966 ContactFactoryAccountData *account_data;
969 account = mc_account_lookup (unique_name);
970 account_data = g_hash_table_lookup (priv->accounts, account);
972 contact_factory_account_data_update (account_data);
974 g_object_unref (account);
977 static ContactFactoryAccountData *
978 contact_factory_account_data_get (EmpathyContactFactory *factory,
981 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
982 ContactFactoryAccountData *account_data;
984 account_data = g_hash_table_lookup (priv->accounts, account);
986 account_data = contact_factory_account_data_new (factory, account);
987 g_hash_table_insert (priv->accounts,
988 g_object_ref (account),
996 contact_factory_account_data_add_contact (ContactFactoryAccountData *account_data,
997 EmpathyContact *contact)
999 g_object_weak_ref (G_OBJECT (contact),
1000 contact_factory_weak_notify,
1002 account_data->contacts = g_list_prepend (account_data->contacts, contact);
1004 if (!account_data->presence_iface) {
1005 EmpathyPresence *presence;
1007 /* We have no presence iface, set default presence
1009 presence = empathy_presence_new_full (MC_PRESENCE_AVAILABLE,
1012 empathy_contact_set_presence (contact, presence);
1013 g_object_unref (presence);
1016 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
1017 empathy_contact_get_id (contact),
1018 empathy_contact_get_handle (contact));
1022 contact_factory_hold_handles_cb (DBusGProxy *proxy,
1027 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
1033 empathy_contact_factory_get_user (EmpathyContactFactory *factory,
1036 ContactFactoryAccountData *account_data;
1038 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1039 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1041 account_data = contact_factory_account_data_get (factory, account);
1043 return empathy_contact_factory_get_from_handle (factory, account,
1044 account_data->self_handle);
1048 empathy_contact_factory_get_from_id (EmpathyContactFactory *factory,
1052 ContactFactoryAccountData *account_data;
1053 EmpathyContact *contact;
1055 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1056 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1057 g_return_val_if_fail (id != NULL, NULL);
1059 /* Check if the contact already exists */
1060 account_data = contact_factory_account_data_get (factory, account);
1061 contact = contact_factory_account_data_find_by_id (account_data, id);
1063 return g_object_ref (contact);
1066 /* Create new contact */
1067 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1071 contact_factory_account_data_add_contact (account_data, contact);
1073 /* If the account is connected, request contact's handle */
1074 if (account_data->tp_conn) {
1075 RequestHandlesData *data;
1076 const gchar *contact_ids[] = {id, NULL};
1078 data = g_slice_new (RequestHandlesData);
1079 data->account_data = contact_factory_account_data_ref (account_data);
1080 data->contacts = g_list_prepend (NULL, g_object_ref (contact));
1081 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1082 TP_HANDLE_TYPE_CONTACT,
1084 contact_factory_request_handles_cb,
1092 empathy_contact_factory_get_from_handle (EmpathyContactFactory *factory,
1096 EmpathyContact *contact;
1100 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1101 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1103 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1104 g_array_append_val (handles, handle);
1106 contacts = empathy_contact_factory_get_from_handles (factory, account, handles);
1107 g_array_free (handles, TRUE);
1109 contact = contacts ? contacts->data : NULL;
1110 g_list_free (contacts);
1116 empathy_contact_factory_get_from_handles (EmpathyContactFactory *factory,
1120 ContactFactoryAccountData *account_data;
1121 GList *contacts = NULL;
1122 GArray *new_handles;
1123 gchar **handles_names;
1125 GError *error = NULL;
1127 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1128 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1129 g_return_val_if_fail (handles != NULL, NULL);
1131 /* Search all contacts we already have */
1132 account_data = contact_factory_account_data_get (factory, account);
1133 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1134 for (i = 0; i < handles->len; i++) {
1135 EmpathyContact *contact;
1138 handle = g_array_index (handles, guint, i);
1143 contact = contact_factory_account_data_find_by_handle (account_data, handle);
1145 contacts = g_list_prepend (contacts, g_object_ref (contact));
1147 g_array_append_val (new_handles, handle);
1151 if (new_handles->len == 0) {
1152 g_array_free (new_handles, TRUE);
1156 /* Get the IDs of all new handles */
1157 if (!tp_conn_inspect_handles (DBUS_G_PROXY (account_data->tp_conn),
1158 TP_HANDLE_TYPE_CONTACT,
1162 empathy_debug (DEBUG_DOMAIN,
1163 "Couldn't inspect contact: %s",
1164 error ? error->message : "No error given");
1165 g_clear_error (&error);
1166 g_array_free (new_handles, TRUE);
1170 /* Create new contacts */
1171 for (i = 0; i < new_handles->len; i++) {
1172 EmpathyContact *contact;
1177 id = handles_names[i];
1178 handle = g_array_index (new_handles, guint, i);
1180 is_user = (handle == account_data->self_handle);
1181 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1187 contact_factory_account_data_add_contact (account_data,
1189 contacts = g_list_prepend (contacts, contact);
1192 g_free (handles_names);
1194 /* Hold all new handles. */
1195 tp_conn_hold_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1196 TP_HANDLE_TYPE_CONTACT,
1198 contact_factory_hold_handles_cb,
1201 contact_factory_request_everything (account_data, new_handles);
1203 g_array_free (new_handles, TRUE);
1209 empathy_contact_factory_set_alias (EmpathyContactFactory *factory,
1210 EmpathyContact *contact,
1213 ContactFactoryAccountData *account_data;
1215 GHashTable *new_alias;
1218 g_return_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory));
1219 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1221 account = empathy_contact_get_account (contact);
1222 account_data = contact_factory_account_data_get (factory, account);
1224 if (!account_data->aliasing_iface) {
1228 handle = empathy_contact_get_handle (contact);
1230 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1231 empathy_contact_get_id (contact),
1234 new_alias = g_hash_table_new_full (g_direct_hash,
1239 g_hash_table_insert (new_alias,
1240 GUINT_TO_POINTER (handle),
1243 tp_conn_iface_aliasing_set_aliases_async (account_data->aliasing_iface,
1245 contact_factory_set_aliases_cb,
1246 contact_factory_account_data_ref (account_data));
1248 g_hash_table_destroy (new_alias);
1252 empathy_contact_factory_set_avatar (EmpathyContactFactory *factory,
1256 const gchar *mime_type)
1258 ContactFactoryAccountData *account_data;
1261 g_return_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory));
1262 g_return_if_fail (MC_IS_ACCOUNT (account));
1264 account_data = contact_factory_account_data_get (factory, account);
1266 if (!account_data->avatars_iface) {
1270 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1271 mc_account_get_unique_name (account));
1273 avatar.data = (gchar*) data;
1275 tp_conn_iface_avatars_set_avatar_async (account_data->avatars_iface,
1278 contact_factory_set_avatar_cb,
1279 contact_factory_account_data_ref (account_data));
1283 contact_factory_finalize (GObject *object)
1285 EmpathyContactFactoryPriv *priv;
1287 priv = GET_PRIV (object);
1289 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1290 "AccountStatusChanged",
1291 G_CALLBACK (contact_factory_status_changed_cb),
1294 g_hash_table_destroy (priv->accounts);
1295 g_object_unref (priv->mc);
1297 G_OBJECT_CLASS (empathy_contact_factory_parent_class)->finalize (object);
1301 empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass)
1303 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1305 object_class->finalize = contact_factory_finalize;
1307 g_type_class_add_private (object_class, sizeof (EmpathyContactFactoryPriv));
1311 empathy_contact_factory_init (EmpathyContactFactory *factory)
1313 EmpathyContactFactoryPriv *priv;
1315 priv = GET_PRIV (factory);
1317 priv->mc = empathy_mission_control_new ();
1318 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1319 empathy_account_equal,
1321 (GDestroyNotify) contact_factory_account_data_unref);
1323 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1324 "AccountStatusChanged",
1325 G_CALLBACK (contact_factory_status_changed_cb),
1329 EmpathyContactFactory *
1330 empathy_contact_factory_new (void)
1332 static EmpathyContactFactory *factory = NULL;
1335 factory = g_object_new (EMPATHY_TYPE_CONTACT_FACTORY, NULL);
1336 g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
1338 g_object_ref (factory);