1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
27 #include <libtelepathy/tp-conn.h>
28 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
29 #include <libtelepathy/tp-conn-iface-presence-gen.h>
30 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
31 #include <libtelepathy/tp-conn-iface-capabilities-gen.h>
32 #include <libmissioncontrol/mission-control.h>
34 #include "empathy-contact-factory.h"
35 #include "empathy-utils.h"
36 #include "empathy-debug.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_CONTACT_FACTORY, EmpathyContactFactoryPriv))
41 #define DEBUG_DOMAIN "ContactFactory"
43 struct _EmpathyContactFactoryPriv {
49 EmpathyContactFactory *factory;
51 guint nb_pending_calls;
54 DBusGProxy *aliasing_iface;
55 DBusGProxy *avatars_iface;
56 DBusGProxy *presence_iface;
57 DBusGProxy *capabilities_iface;
61 } ContactFactoryAccountData;
64 ContactFactoryAccountData *account_data;
69 ContactFactoryAccountData *account_data;
73 static void empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass);
74 static void empathy_contact_factory_init (EmpathyContactFactory *factory);
76 G_DEFINE_TYPE (EmpathyContactFactory, empathy_contact_factory, G_TYPE_OBJECT);
79 contact_factory_find_by_handle (gconstpointer a,
82 EmpathyContact *contact;
85 contact = EMPATHY_CONTACT (a);
86 handle = GPOINTER_TO_UINT (b);
88 return handle - empathy_contact_get_handle (contact);
91 static EmpathyContact *
92 contact_factory_account_data_find_by_handle (ContactFactoryAccountData *account_data,
97 l = g_list_find_custom (account_data->contacts,
98 GUINT_TO_POINTER (handle),
99 contact_factory_find_by_handle);
101 return l ? l->data : NULL;
105 contact_factory_find_by_id (gconstpointer a,
108 EmpathyContact *contact;
111 contact = EMPATHY_CONTACT (a);
113 return strcmp (id, empathy_contact_get_id (contact));
116 static EmpathyContact *
117 contact_factory_account_data_find_by_id (ContactFactoryAccountData *account_data,
122 l = g_list_find_custom (account_data->contacts,
124 contact_factory_find_by_id);
126 return l ? l->data : NULL;
129 static void contact_factory_account_data_free (gpointer data);
132 contact_factory_account_data_return_call (ContactFactoryAccountData *account_data)
134 if (--account_data->nb_pending_calls == 0 &&
135 account_data->contacts == NULL) {
136 contact_factory_account_data_free (account_data);
141 contact_factory_presences_table_foreach (const gchar *state_str,
142 GHashTable *presences_table,
143 EmpathyPresence **presence)
146 const GValue *message;
148 state = empathy_presence_state_from_str (state_str);
149 if (state == MC_PRESENCE_UNSET) {
154 g_object_unref (*presence);
158 *presence = empathy_presence_new ();
159 empathy_presence_set_state (*presence, state);
161 message = g_hash_table_lookup (presences_table, "message");
162 if (message != NULL) {
163 empathy_presence_set_status (*presence,
164 g_value_get_string (message));
169 contact_factory_parse_presence_foreach (guint handle,
170 GValueArray *presence_struct,
171 ContactFactoryAccountData *account_data)
173 GHashTable *presences_table;
174 EmpathyContact *contact;
175 EmpathyPresence *presence = NULL;
177 contact = contact_factory_account_data_find_by_handle (account_data,
183 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
185 g_hash_table_foreach (presences_table,
186 (GHFunc) contact_factory_presences_table_foreach,
189 empathy_debug (DEBUG_DOMAIN, "Changing presence for contact %s (%d) to %s (%d)",
190 empathy_contact_get_id (contact),
192 presence ? empathy_presence_get_status (presence) : "unset",
193 presence ? empathy_presence_get_state (presence) : MC_PRESENCE_UNSET);
195 empathy_contact_set_presence (contact, presence);
196 g_object_unref (presence);
200 contact_factory_get_presence_cb (DBusGProxy *proxy,
201 GHashTable *handle_table,
205 ContactFactoryAccountData *account_data = user_data;
208 empathy_debug (DEBUG_DOMAIN, "Error getting presence: %s",
213 g_hash_table_foreach (handle_table,
214 (GHFunc) contact_factory_parse_presence_foreach,
217 contact_factory_account_data_return_call (account_data);
221 contact_factory_presence_update_cb (DBusGProxy *proxy,
222 GHashTable *handle_table,
223 ContactFactoryAccountData *account_data)
225 g_hash_table_foreach (handle_table,
226 (GHFunc) contact_factory_parse_presence_foreach,
231 contact_factory_set_aliases_cb (DBusGProxy *proxy,
235 ContactFactoryAccountData *account_data = user_data;
238 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
242 contact_factory_account_data_return_call (account_data);
246 contact_factory_request_aliases_cb (DBusGProxy *proxy,
247 gchar **contact_names,
251 RequestAliasesData *data = user_data;
256 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
261 for (name = contact_names; *name; name++) {
262 EmpathyContact *contact;
264 contact = contact_factory_account_data_find_by_handle (data->account_data,
270 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
271 empathy_contact_get_id (contact),
272 data->handles[i], *name);
274 empathy_contact_set_name (contact, *name);
280 contact_factory_account_data_return_call (data->account_data);
281 g_free (data->handles);
282 g_slice_free (RequestAliasesData, data);
286 contact_factory_aliases_changed_cb (DBusGProxy *proxy,
287 GPtrArray *renamed_handlers,
290 ContactFactoryAccountData *account_data = user_data;
293 for (i = 0; renamed_handlers->len > i; i++) {
296 GValueArray *renamed_struct;
297 EmpathyContact *contact;
299 renamed_struct = g_ptr_array_index (renamed_handlers, i);
300 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
301 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
302 contact = contact_factory_account_data_find_by_handle (account_data, handle);
305 /* We don't know this contact, skip */
309 if (G_STR_EMPTY (alias)) {
313 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
314 empathy_contact_get_id (contact),
317 empathy_contact_set_name (contact, alias);
322 contact_factory_avatar_retrieved_cb (DBusGProxy *proxy,
329 ContactFactoryAccountData *account_data = user_data;
330 EmpathyContact *contact;
331 EmpathyAvatar *avatar;
333 contact = contact_factory_account_data_find_by_handle (account_data,
339 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
340 empathy_contact_get_id (contact),
343 avatar = empathy_avatar_new (avatar_data->data,
348 empathy_contact_set_avatar (contact, avatar);
349 empathy_avatar_unref (avatar);
353 contact_factory_request_avatars_cb (DBusGProxy *proxy,
357 ContactFactoryAccountData *account_data = user_data;
360 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
364 contact_factory_account_data_return_call (account_data);
368 ContactFactoryAccountData *account_data;
373 contact_factory_avatar_maybe_update (ContactFactoryAccountData *account_data,
377 EmpathyContact *contact;
378 EmpathyAvatar *avatar;
380 contact = contact_factory_account_data_find_by_handle (account_data,
386 /* Check if we have an avatar */
387 if (G_STR_EMPTY (token)) {
388 empathy_contact_set_avatar (contact, NULL);
392 /* Check if the avatar changed */
393 avatar = empathy_contact_get_avatar (contact);
394 if (avatar && !empathy_strdiff (avatar->token, token)) {
398 /* The avatar changed, search the new one in the cache */
399 avatar = empathy_avatar_new_from_cache (token);
401 /* Got from cache, use it */
402 empathy_contact_set_avatar (contact, avatar);
403 empathy_avatar_unref (avatar);
407 /* Avatar is not up-to-date, we have to request it. */
412 contact_factory_avatar_tokens_foreach (gpointer key,
416 TokensData *data = user_data;
417 const gchar *token = value;
418 guint handle = GPOINTER_TO_UINT (key);
420 if (!contact_factory_avatar_maybe_update (data->account_data,
422 g_array_append_val (data->handles, handle);
427 contact_factory_get_known_avatar_tokens_cb (DBusGProxy *proxy,
432 ContactFactoryAccountData *account_data = user_data;
436 empathy_debug (DEBUG_DOMAIN,
437 "Error getting known avatars tokens: %s",
442 data.account_data = account_data;
443 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
444 g_hash_table_foreach (tokens,
445 contact_factory_avatar_tokens_foreach,
448 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
449 g_hash_table_size (tokens),
452 /* Request needed avatars */
453 if (data.handles->len > 0) {
454 account_data->nb_pending_calls++;
455 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
457 contact_factory_request_avatars_cb,
461 g_array_free (data.handles, TRUE);
463 contact_factory_account_data_return_call (account_data);
467 contact_factory_avatar_updated_cb (DBusGProxy *proxy,
472 ContactFactoryAccountData *account_data = user_data;
475 if (contact_factory_avatar_maybe_update (account_data, handle, new_token)) {
476 /* Avatar was cached, nothing to do */
480 empathy_debug (DEBUG_DOMAIN, "Need to request one avatar");
482 handles = g_array_new (FALSE, FALSE, sizeof (guint));
483 g_array_append_val (handles, handle);
485 account_data->nb_pending_calls++;
486 tp_conn_iface_avatars_request_avatars_async (account_data->avatars_iface,
488 contact_factory_request_avatars_cb,
490 g_array_free (handles, TRUE);
494 contact_factory_update_capabilities (ContactFactoryAccountData *account_data,
496 const gchar *channel_type,
500 EmpathyContact *contact;
501 EmpathyCapabilities capabilities;
503 contact = contact_factory_account_data_find_by_handle (account_data,
509 capabilities = empathy_contact_get_capabilities (contact);
511 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
512 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
513 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
514 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
515 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
517 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
518 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
522 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
523 empathy_contact_get_id (contact),
524 empathy_contact_get_handle (contact),
527 empathy_contact_set_capabilities (contact, capabilities);
531 contact_factory_get_capabilities_cb (DBusGProxy *proxy,
532 GPtrArray *capabilities,
536 ContactFactoryAccountData *account_data = user_data;
540 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
545 for (i = 0; i < capabilities->len; i++) {
548 const gchar *channel_type;
552 values = g_ptr_array_index (capabilities, i);
553 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
554 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
555 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
556 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
558 contact_factory_update_capabilities (account_data,
567 contact_factory_account_data_return_call (account_data);
571 contact_factory_capabilities_changed_cb (DBusGProxy *proxy,
572 GPtrArray *capabilities,
575 ContactFactoryAccountData *account_data = user_data;
578 for (i = 0; i < capabilities->len; i++) {
581 const gchar *channel_type;
585 values = g_ptr_array_index (capabilities, i);
586 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
587 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
588 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
589 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
591 contact_factory_update_capabilities (account_data,
600 contact_factory_request_everything (ContactFactoryAccountData *account_data,
603 if (account_data->presence_iface) {
604 account_data->nb_pending_calls++;
605 tp_conn_iface_presence_get_presence_async (account_data->presence_iface,
607 contact_factory_get_presence_cb,
611 if (account_data->aliasing_iface) {
612 RequestAliasesData *data;
614 account_data->nb_pending_calls++;
615 data = g_slice_new (RequestAliasesData);
616 data->account_data = account_data;
617 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
619 tp_conn_iface_aliasing_request_aliases_async (account_data->aliasing_iface,
621 contact_factory_request_aliases_cb,
625 if (account_data->avatars_iface) {
626 account_data->nb_pending_calls++;
627 tp_conn_iface_avatars_get_known_avatar_tokens_async (account_data->avatars_iface,
629 contact_factory_get_known_avatar_tokens_cb,
633 if (account_data->capabilities_iface) {
634 account_data->nb_pending_calls++;
635 tp_conn_iface_capabilities_get_capabilities_async (account_data->capabilities_iface,
637 contact_factory_get_capabilities_cb,
643 contact_factory_request_handles_cb (DBusGProxy *proxy,
648 RequestHandlesData *data = user_data;
653 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
658 for (l = data->contacts; l; l = l->next) {
661 handle = g_array_index (handles, guint, i);
662 empathy_contact_set_handle (l->data, handle);
663 if (handle == data->account_data->self_handle) {
664 empathy_contact_set_is_user (l->data, TRUE);
670 contact_factory_request_everything (data->account_data, handles);
673 g_list_foreach (data->contacts, (GFunc) g_object_unref, NULL);
674 g_list_free (data->contacts);
675 contact_factory_account_data_return_call (data->account_data);
676 g_slice_free (RequestHandlesData, data);
680 contact_factory_disconnect_contact_foreach (gpointer data,
683 EmpathyContact *contact = data;
685 empathy_contact_set_presence (contact, NULL);
686 empathy_contact_set_handle (contact, 0);
690 contact_factory_destroy_cb (TpConn *tp_conn,
691 ContactFactoryAccountData *account_data)
693 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
695 g_object_unref (account_data->tp_conn);
696 account_data->tp_conn = NULL;
697 account_data->aliasing_iface = NULL;
698 account_data->avatars_iface = NULL;
699 account_data->presence_iface = NULL;
700 account_data->capabilities_iface = NULL;
702 g_list_foreach (account_data->contacts,
703 contact_factory_disconnect_contact_foreach,
708 contact_factory_account_data_disconnect (ContactFactoryAccountData *account_data)
710 if (account_data->aliasing_iface) {
711 dbus_g_proxy_disconnect_signal (account_data->aliasing_iface,
713 G_CALLBACK (contact_factory_aliases_changed_cb),
716 if (account_data->avatars_iface) {
717 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
719 G_CALLBACK (contact_factory_avatar_updated_cb),
721 dbus_g_proxy_disconnect_signal (account_data->avatars_iface,
723 G_CALLBACK (contact_factory_avatar_retrieved_cb),
726 if (account_data->presence_iface) {
727 dbus_g_proxy_disconnect_signal (account_data->presence_iface,
729 G_CALLBACK (contact_factory_presence_update_cb),
732 if (account_data->capabilities_iface) {
733 dbus_g_proxy_disconnect_signal (account_data->capabilities_iface,
734 "CapabilitiesChanged",
735 G_CALLBACK (contact_factory_capabilities_changed_cb),
738 if (account_data->tp_conn) {
739 g_signal_handlers_disconnect_by_func (account_data->tp_conn,
740 contact_factory_destroy_cb,
746 contact_factory_account_data_update (ContactFactoryAccountData *account_data)
748 EmpathyContactFactory *factory = account_data->factory;
749 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
750 McAccount *account = account_data->account;
751 TpConn *tp_conn = NULL;
752 RequestHandlesData *data;
753 const gchar **contact_ids;
758 if (account_data->account) {
761 /* status == 0 means the status is CONNECTED */
762 status = mission_control_get_connection_status (priv->mc,
765 tp_conn = mission_control_get_connection (priv->mc,
771 /* We are not connected anymore, remove the old connection */
772 contact_factory_account_data_disconnect (account_data);
773 if (account_data->tp_conn) {
774 contact_factory_destroy_cb (account_data->tp_conn,
779 else if (account_data->tp_conn) {
780 /* We were connected and we still are connected, nothing
781 * changed so nothing to do. */
782 g_object_unref (tp_conn);
786 /* We got a new connection */
787 account_data->tp_conn = tp_conn;
788 account_data->aliasing_iface = tp_conn_get_interface (tp_conn,
789 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
790 account_data->avatars_iface = tp_conn_get_interface (tp_conn,
791 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
792 account_data->presence_iface = tp_conn_get_interface (tp_conn,
793 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
794 account_data->capabilities_iface = tp_conn_get_interface (tp_conn,
795 TELEPATHY_CONN_IFACE_CAPABILITIES_QUARK);
797 /* Connect signals */
798 if (account_data->aliasing_iface) {
799 dbus_g_proxy_connect_signal (account_data->aliasing_iface,
801 G_CALLBACK (contact_factory_aliases_changed_cb),
804 if (account_data->avatars_iface) {
805 dbus_g_proxy_connect_signal (account_data->avatars_iface,
807 G_CALLBACK (contact_factory_avatar_updated_cb),
809 dbus_g_proxy_connect_signal (account_data->avatars_iface,
811 G_CALLBACK (contact_factory_avatar_retrieved_cb),
814 if (account_data->presence_iface) {
815 dbus_g_proxy_connect_signal (account_data->presence_iface,
817 G_CALLBACK (contact_factory_presence_update_cb),
820 if (account_data->capabilities_iface) {
821 dbus_g_proxy_connect_signal (account_data->capabilities_iface,
822 "CapabilitiesChanged",
823 G_CALLBACK (contact_factory_capabilities_changed_cb),
826 g_signal_connect (tp_conn, "destroy",
827 G_CALLBACK (contact_factory_destroy_cb),
830 /* Get our own handle */
831 if (!tp_conn_get_self_handle (DBUS_G_PROXY (account_data->tp_conn),
832 &account_data->self_handle,
834 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
835 error ? error->message : "No error given");
836 g_clear_error (&error);
839 /* Request new handles for all contacts */
840 if (account_data->contacts) {
841 data = g_slice_new (RequestHandlesData);
842 data->account_data = account_data;
843 data->contacts = g_list_copy (account_data->contacts);
844 g_list_foreach (data->contacts, (GFunc) g_object_ref, NULL);
846 i = g_list_length (data->contacts);
847 contact_ids = g_new0 (const gchar*, i + 1);
849 for (l = data->contacts; l; l = l->next) {
850 contact_ids[i] = empathy_contact_get_id (l->data);
854 account_data->nb_pending_calls++;
855 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
856 TP_HANDLE_TYPE_CONTACT,
858 contact_factory_request_handles_cb,
860 g_free (contact_ids);
865 contact_factory_weak_notify (gpointer data,
866 GObject *where_the_object_was)
868 ContactFactoryAccountData *account_data = data;
870 empathy_debug (DEBUG_DOMAIN, "Remove finalized contact %p",
871 where_the_object_was);
873 account_data->contacts = g_list_remove (account_data->contacts,
874 where_the_object_was);
875 if (!account_data->contacts) {
876 EmpathyContactFactoryPriv *priv;
878 priv = GET_PRIV (account_data->factory);
880 g_hash_table_remove (priv->accounts, account_data->account);
885 contact_factory_remove_foreach (gpointer data,
888 ContactFactoryAccountData *account_data = user_data;
889 EmpathyContact *contact = data;
891 g_object_weak_unref (G_OBJECT (contact),
892 contact_factory_weak_notify,
896 static ContactFactoryAccountData *
897 contact_factory_account_data_new (EmpathyContactFactory *factory,
900 ContactFactoryAccountData *account_data;
902 account_data = g_slice_new0 (ContactFactoryAccountData);
903 account_data->factory = factory;
904 account_data->account = g_object_ref (account);
906 contact_factory_account_data_update (account_data);
912 contact_factory_account_data_free (gpointer data)
914 ContactFactoryAccountData *account_data = data;
916 contact_factory_account_data_disconnect (account_data);
918 if (account_data->contacts) {
919 g_list_foreach (account_data->contacts,
920 contact_factory_remove_foreach,
922 g_list_free (account_data->contacts);
923 account_data->contacts = NULL;
926 if (account_data->account) {
927 g_object_unref (account_data->account);
928 account_data->account = NULL;
931 if (account_data->tp_conn) {
932 g_object_unref (account_data->tp_conn);
933 account_data->tp_conn = NULL;
934 account_data->aliasing_iface = NULL;
935 account_data->avatars_iface = NULL;
936 account_data->presence_iface = NULL;
937 account_data->capabilities_iface = NULL;
940 /* Keep the struct alive if we have calls in flight, it will be
941 * destroyed once all calls returned. */
942 if (account_data->nb_pending_calls == 0) {
943 g_slice_free (ContactFactoryAccountData, account_data);
948 contact_factory_status_changed_cb (MissionControl *mc,
949 TelepathyConnectionStatus status,
951 TelepathyConnectionStatusReason reason,
952 const gchar *unique_name,
953 EmpathyContactFactory *factory)
955 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
956 ContactFactoryAccountData *account_data;
959 account = mc_account_lookup (unique_name);
960 account_data = g_hash_table_lookup (priv->accounts, account);
962 contact_factory_account_data_update (account_data);
964 g_object_unref (account);
967 static ContactFactoryAccountData *
968 contact_factory_account_data_get (EmpathyContactFactory *factory,
971 EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
972 ContactFactoryAccountData *account_data;
974 account_data = g_hash_table_lookup (priv->accounts, account);
976 account_data = contact_factory_account_data_new (factory, account);
977 g_hash_table_insert (priv->accounts,
978 g_object_ref (account),
986 contact_factory_account_data_add_contact (ContactFactoryAccountData *account_data,
987 EmpathyContact *contact)
989 g_object_weak_ref (G_OBJECT (contact),
990 contact_factory_weak_notify,
992 account_data->contacts = g_list_prepend (account_data->contacts, contact);
994 if (!account_data->presence_iface) {
995 EmpathyPresence *presence;
997 /* We have no presence iface, set default presence
999 presence = empathy_presence_new_full (MC_PRESENCE_AVAILABLE,
1002 empathy_contact_set_presence (contact, presence);
1003 g_object_unref (presence);
1006 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
1007 empathy_contact_get_id (contact),
1008 empathy_contact_get_handle (contact));
1012 contact_factory_hold_handles_cb (DBusGProxy *proxy,
1017 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
1023 empathy_contact_factory_get_user (EmpathyContactFactory *factory,
1026 ContactFactoryAccountData *account_data;
1028 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1029 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1031 account_data = contact_factory_account_data_get (factory, account);
1033 return empathy_contact_factory_get_from_handle (factory, account,
1034 account_data->self_handle);
1038 empathy_contact_factory_get_from_id (EmpathyContactFactory *factory,
1042 ContactFactoryAccountData *account_data;
1043 EmpathyContact *contact;
1045 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1046 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1047 g_return_val_if_fail (id != NULL, NULL);
1049 /* Check if the contact already exists */
1050 account_data = contact_factory_account_data_get (factory, account);
1051 contact = contact_factory_account_data_find_by_id (account_data, id);
1053 return g_object_ref (contact);
1056 /* Create new contact */
1057 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1061 contact_factory_account_data_add_contact (account_data, contact);
1063 /* If the account is connected, request contact's handle */
1064 if (account_data->tp_conn) {
1065 RequestHandlesData *data;
1066 const gchar *contact_ids[] = {id, NULL};
1068 account_data->nb_pending_calls++;
1069 data = g_slice_new (RequestHandlesData);
1070 data->account_data = account_data;
1071 data->contacts = g_list_prepend (NULL, g_object_ref (contact));
1072 tp_conn_request_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1073 TP_HANDLE_TYPE_CONTACT,
1075 contact_factory_request_handles_cb,
1083 empathy_contact_factory_get_from_handle (EmpathyContactFactory *factory,
1087 EmpathyContact *contact;
1091 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1092 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1094 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1095 g_array_append_val (handles, handle);
1097 contacts = empathy_contact_factory_get_from_handles (factory, account, handles);
1098 g_array_free (handles, TRUE);
1100 contact = contacts ? contacts->data : NULL;
1101 g_list_free (contacts);
1107 empathy_contact_factory_get_from_handles (EmpathyContactFactory *factory,
1111 ContactFactoryAccountData *account_data;
1112 GList *contacts = NULL;
1113 GArray *new_handles;
1114 gchar **handles_names;
1116 GError *error = NULL;
1118 g_return_val_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory), NULL);
1119 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1120 g_return_val_if_fail (handles != NULL, NULL);
1122 /* Search all contacts we already have */
1123 account_data = contact_factory_account_data_get (factory, account);
1124 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1125 for (i = 0; i < handles->len; i++) {
1126 EmpathyContact *contact;
1129 handle = g_array_index (handles, guint, i);
1134 contact = contact_factory_account_data_find_by_handle (account_data, handle);
1136 contacts = g_list_prepend (contacts, g_object_ref (contact));
1138 g_array_append_val (new_handles, handle);
1142 if (new_handles->len == 0) {
1143 g_array_free (new_handles, TRUE);
1147 /* Get the IDs of all new handles */
1148 if (!tp_conn_inspect_handles (DBUS_G_PROXY (account_data->tp_conn),
1149 TP_HANDLE_TYPE_CONTACT,
1153 empathy_debug (DEBUG_DOMAIN,
1154 "Couldn't inspect contact: %s",
1155 error ? error->message : "No error given");
1156 g_clear_error (&error);
1157 g_array_free (new_handles, TRUE);
1161 /* Create new contacts */
1162 for (i = 0; i < new_handles->len; i++) {
1163 EmpathyContact *contact;
1168 id = handles_names[i];
1169 handle = g_array_index (new_handles, guint, i);
1171 is_user = (handle == account_data->self_handle);
1172 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1178 contact_factory_account_data_add_contact (account_data,
1180 contacts = g_list_prepend (contacts, contact);
1183 g_free (handles_names);
1185 /* Hold all new handles. */
1186 tp_conn_hold_handles_async (DBUS_G_PROXY (account_data->tp_conn),
1187 TP_HANDLE_TYPE_CONTACT,
1189 contact_factory_hold_handles_cb,
1192 contact_factory_request_everything (account_data, new_handles);
1194 g_array_free (new_handles, TRUE);
1200 empathy_contact_factory_set_name (EmpathyContactFactory *factory,
1201 EmpathyContact *contact,
1204 ContactFactoryAccountData *account_data;
1206 GHashTable *new_alias;
1209 g_return_if_fail (EMPATHY_IS_CONTACT_FACTORY (factory));
1210 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1212 account = empathy_contact_get_account (contact);
1213 account_data = contact_factory_account_data_get (factory, account);
1215 if (!account_data->aliasing_iface) {
1219 handle = empathy_contact_get_handle (contact);
1221 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1222 empathy_contact_get_id (contact),
1225 new_alias = g_hash_table_new_full (g_direct_hash,
1230 g_hash_table_insert (new_alias,
1231 GUINT_TO_POINTER (handle),
1234 account_data->nb_pending_calls++;
1235 tp_conn_iface_aliasing_set_aliases_async (account_data->aliasing_iface,
1237 contact_factory_set_aliases_cb,
1240 g_hash_table_destroy (new_alias);
1244 contact_factory_finalize (GObject *object)
1246 EmpathyContactFactoryPriv *priv;
1248 priv = GET_PRIV (object);
1250 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1251 "AccountStatusChanged",
1252 G_CALLBACK (contact_factory_status_changed_cb),
1255 g_hash_table_destroy (priv->accounts);
1256 g_object_unref (priv->mc);
1258 G_OBJECT_CLASS (empathy_contact_factory_parent_class)->finalize (object);
1262 empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass)
1264 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1266 object_class->finalize = contact_factory_finalize;
1268 g_type_class_add_private (object_class, sizeof (EmpathyContactFactoryPriv));
1272 empathy_contact_factory_init (EmpathyContactFactory *factory)
1274 EmpathyContactFactoryPriv *priv;
1276 priv = GET_PRIV (factory);
1278 priv->mc = empathy_mission_control_new ();
1279 priv->accounts = g_hash_table_new_full (empathy_account_hash,
1280 empathy_account_equal,
1282 contact_factory_account_data_free);
1284 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1285 "AccountStatusChanged",
1286 G_CALLBACK (contact_factory_status_changed_cb),
1290 EmpathyContactFactory *
1291 empathy_contact_factory_new (void)
1293 static EmpathyContactFactory *factory = NULL;
1296 factory = g_object_new (EMPATHY_TYPE_CONTACT_FACTORY, NULL);
1297 g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
1299 g_object_ref (factory);