1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/connection.h>
28 #include <libmissioncontrol/mission-control.h>
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-utils.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
34 #include "empathy-debug.h"
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
40 TpConnection *connection;
47 gchar **avatar_mime_types;
48 guint avatar_min_width;
49 guint avatar_min_height;
50 guint avatar_max_width;
51 guint avatar_max_height;
52 guint avatar_max_size;
53 } EmpathyTpContactFactoryPriv;
55 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
64 static void tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
65 const gchar **mime_types,
74 static void tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory);
77 static EmpathyContact *
78 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
81 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
84 for (l = priv->contacts; l; l = l->next) {
85 if (empathy_contact_get_handle (l->data) == handle) {
93 static EmpathyContact *
94 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
97 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
100 for (l = priv->contacts; l; l = l->next) {
101 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
110 tp_contact_factory_weak_notify (gpointer data,
111 GObject *where_the_object_was)
113 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
115 DEBUG ("Remove finalized contact %p", where_the_object_was);
117 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
121 tp_contact_factory_presences_table_foreach (const gchar *state_str,
122 GHashTable *presences_table,
123 EmpathyContact *contact)
125 const GValue *message;
126 const gchar *message_str = NULL;
128 empathy_contact_set_presence (contact,
129 empathy_presence_from_str (state_str));
131 message = g_hash_table_lookup (presences_table, "message");
133 message_str = g_value_get_string (message);
136 if (!G_STR_EMPTY (message_str)) {
137 empathy_contact_set_presence_message (contact, message_str);
139 empathy_contact_set_presence_message (contact, NULL);
144 tp_contact_factory_parse_presence_foreach (guint handle,
145 GValueArray *presence_struct,
146 EmpathyTpContactFactory *tp_factory)
148 GHashTable *presences_table;
149 EmpathyContact *contact;
151 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
156 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
158 g_hash_table_foreach (presences_table,
159 (GHFunc) tp_contact_factory_presences_table_foreach,
162 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
163 empathy_contact_get_id (contact),
165 empathy_contact_get_presence_message (contact),
166 empathy_contact_get_presence (contact));
170 tp_contact_factory_get_presence_cb (TpConnection *connection,
171 GHashTable *handle_table,
177 DEBUG ("Error getting presence: %s", error->message);
178 if (error->domain == TP_DBUS_ERRORS &&
179 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
180 guint *handles = user_data;
182 /* We have no presence iface, set default presence
184 while (*handles != 0) {
185 EmpathyContact *contact;
187 contact = tp_contact_factory_find_by_handle (
188 (EmpathyTpContactFactory*) tp_factory,
191 empathy_contact_set_presence (contact,
192 MC_PRESENCE_AVAILABLE);
202 g_hash_table_foreach (handle_table,
203 (GHFunc) tp_contact_factory_parse_presence_foreach,
204 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
208 tp_contact_factory_presence_update_cb (TpConnection *connection,
209 GHashTable *handle_table,
213 g_hash_table_foreach (handle_table,
214 (GHFunc) tp_contact_factory_parse_presence_foreach,
215 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
219 tp_contact_factory_set_aliases_cb (TpConnection *connection,
225 DEBUG ("Error setting alias: %s", error->message);
230 tp_contact_factory_request_aliases_cb (TpConnection *connection,
231 const gchar **contact_names,
236 guint *handles = user_data;
241 DEBUG ("Error requesting aliases: %s", error->message);
243 /* If we failed to get alias set it to NULL, like that if
244 * someone is waiting for the name to be ready it won't wait
246 while (*handles != 0) {
247 EmpathyContact *contact;
249 contact = tp_contact_factory_find_by_handle (
250 (EmpathyTpContactFactory*) tp_factory,
253 empathy_contact_set_name (contact, NULL);
261 for (name = contact_names; *name; name++) {
262 EmpathyContact *contact;
264 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
270 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
271 empathy_contact_get_id (contact),
272 empathy_contact_get_handle (contact),
275 empathy_contact_set_name (contact, *name);
282 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
283 const GPtrArray *renamed_handlers,
285 GObject *weak_object)
287 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
290 for (i = 0; renamed_handlers->len > i; i++) {
293 GValueArray *renamed_struct;
294 EmpathyContact *contact;
296 renamed_struct = g_ptr_array_index (renamed_handlers, i);
297 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
298 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
299 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
302 /* We don't know this contact, skip */
306 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
307 empathy_contact_get_id (contact),
310 empathy_contact_set_name (contact, alias);
315 tp_contact_factory_set_avatar_cb (TpConnection *connection,
322 DEBUG ("Error setting avatar: %s", error->message);
327 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
333 DEBUG ("Error clearing avatar: %s", error->message);
338 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
341 const GArray *avatar_data,
342 const gchar *mime_type,
346 EmpathyContact *contact;
348 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
354 DEBUG ("Avatar retrieved for contact %s (%d)",
355 empathy_contact_get_id (contact),
358 empathy_contact_load_avatar_data (contact,
366 tp_contact_factory_request_avatars_cb (TpConnection *connection,
372 DEBUG ("Error requesting avatars: %s", error->message);
377 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
381 EmpathyContact *contact;
382 EmpathyAvatar *avatar;
384 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
389 /* Check if we have an avatar */
390 if (G_STR_EMPTY (token)) {
391 empathy_contact_set_avatar (contact, NULL);
395 /* Check if the avatar changed */
396 avatar = empathy_contact_get_avatar (contact);
397 if (avatar && !tp_strdiff (avatar->token, token)) {
401 /* The avatar changed, search the new one in the cache */
402 if (empathy_contact_load_avatar_cache (contact, token)) {
403 /* Got from cache, use it */
407 /* Avatar is not up-to-date, we have to request it. */
412 EmpathyTpContactFactory *tp_factory;
417 tp_contact_factory_avatar_tokens_foreach (gpointer key,
421 TokensData *data = user_data;
422 const gchar *token = value;
423 guint handle = GPOINTER_TO_UINT (key);
425 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
427 g_array_append_val (data->handles, handle);
432 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
441 DEBUG ("Error getting known avatars tokens: %s", error->message);
445 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
446 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
447 g_hash_table_foreach (tokens,
448 tp_contact_factory_avatar_tokens_foreach,
451 DEBUG ("Got %d tokens, need to request %d avatars",
452 g_hash_table_size (tokens), data.handles->len);
454 /* Request needed avatars */
455 if (data.handles->len > 0) {
456 tp_cli_connection_interface_avatars_call_request_avatars (connection,
459 tp_contact_factory_request_avatars_cb,
464 g_array_free (data.handles, TRUE);
468 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
470 const gchar *new_token,
476 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
477 handle, new_token)) {
478 /* Avatar was cached, nothing to do */
482 DEBUG ("Need to request avatar for token %s", new_token);
484 handles = g_array_new (FALSE, FALSE, sizeof (guint));
485 g_array_append_val (handles, handle);
487 tp_cli_connection_interface_avatars_call_request_avatars (connection,
490 tp_contact_factory_request_avatars_cb,
493 g_array_free (handles, TRUE);
497 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
499 const gchar *channel_type,
503 EmpathyContact *contact;
504 EmpathyCapabilities capabilities;
506 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
511 capabilities = empathy_contact_get_capabilities (contact);
512 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
514 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
515 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
516 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
517 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
518 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
520 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
521 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
525 DEBUG ("Changing capabilities for contact %s (%d) to %d",
526 empathy_contact_get_id (contact),
527 empathy_contact_get_handle (contact),
530 empathy_contact_set_capabilities (contact, capabilities);
534 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
535 const GPtrArray *capabilities,
538 GObject *weak_object)
540 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
544 DEBUG ("Error getting capabilities: %s", error->message);
545 /* FIXME Should set the capabilities of the contacts for which this request
546 * originated to NONE */
550 for (i = 0; i < capabilities->len; i++) {
553 const gchar *channel_type;
557 values = g_ptr_array_index (capabilities, i);
558 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
559 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
560 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
561 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
563 tp_contact_factory_update_capabilities (tp_factory,
572 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
573 const GPtrArray *capabilities,
575 GObject *weak_object)
577 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
580 for (i = 0; i < capabilities->len; i++) {
583 const gchar *channel_type;
587 values = g_ptr_array_index (capabilities, i);
588 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
589 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
590 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
591 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
593 tp_contact_factory_update_capabilities (tp_factory,
602 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
603 const GArray *handles)
605 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
608 g_return_if_fail (priv->ready);
610 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
611 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
612 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
615 tp_contact_factory_get_presence_cb,
617 G_OBJECT (tp_factory));
619 /* FIXME: Sometimes the dbus call timesout because CM takes
620 * too much time to request all aliases from the server,
621 * that's why we increase the timeout here. See fd.o bug #14795 */
622 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
623 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
624 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
627 tp_contact_factory_request_aliases_cb,
629 G_OBJECT (tp_factory));
631 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
634 tp_contact_factory_get_known_avatar_tokens_cb,
636 G_OBJECT (tp_factory));
638 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
641 tp_contact_factory_get_capabilities_cb,
643 G_OBJECT (tp_factory));
647 tp_contact_factory_list_free (gpointer data)
651 g_list_foreach (l, (GFunc) g_object_unref, NULL);
656 tp_contact_factory_request_handles_cb (TpConnection *connection,
657 const GArray *handles,
662 GList *contacts = user_data;
667 DEBUG ("Failed to request handles: %s", error->message);
671 for (l = contacts; l; l = l->next) {
674 handle = g_array_index (handles, guint, i);
675 empathy_contact_set_handle (l->data, handle);
680 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
685 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
692 GList *contacts = user_data;
696 DEBUG ("Failed to inspect handles: %s", error->message);
701 for (l = contacts; l; l = l->next) {
702 empathy_contact_set_id (l->data, *id);
708 tp_contact_factory_disconnect_contact_foreach (gpointer data,
711 EmpathyContact *contact = data;
713 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
714 empathy_contact_set_handle (contact, 0);
718 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
720 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
722 DEBUG ("Connection invalidated");
724 g_object_unref (priv->connection);
725 priv->connection = NULL;
727 g_object_notify (G_OBJECT (tp_factory), "ready");
730 g_list_foreach (priv->contacts,
731 tp_contact_factory_disconnect_contact_foreach,
737 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
743 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
746 DEBUG ("Failed to get self handles: %s", error->message);
750 empathy_contact_set_handle (priv->user, handle);
752 /* Get avatar requirements for this connection */
753 tp_cli_connection_interface_avatars_call_get_avatar_requirements (
756 tp_contact_factory_got_avatar_requirements_cb,
763 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
764 const gchar **mime_types,
774 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
777 DEBUG ("Failed to get avatar requirements: %s", error->message);
778 /* We'll just leave avatar_mime_types as NULL; the
779 * avatar-setting code can use this as a signal that you can't
783 priv->avatar_mime_types = g_strdupv ((gchar **)mime_types);
784 priv->avatar_min_width = min_width;
785 priv->avatar_min_height = min_height;
786 priv->avatar_max_width = max_width;
787 priv->avatar_max_height = max_height;
788 priv->avatar_max_size = max_size;
791 tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
796 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
798 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
800 GArray *handle_needed;
802 GList *handle_needed_contacts = NULL;
803 GList *id_needed_contacts = NULL;
805 DEBUG ("Connection ready");
808 g_object_notify (G_OBJECT (tp_factory), "ready");
810 /* Connect signals */
811 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
812 tp_contact_factory_aliases_changed_cb,
814 G_OBJECT (tp_factory),
816 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
817 tp_contact_factory_avatar_updated_cb,
819 G_OBJECT (tp_factory),
821 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
822 tp_contact_factory_avatar_retrieved_cb,
824 G_OBJECT (tp_factory),
826 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
827 tp_contact_factory_presence_update_cb,
829 G_OBJECT (tp_factory),
831 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
832 tp_contact_factory_capabilities_changed_cb,
834 G_OBJECT (tp_factory),
837 /* Request needed info for all existing contacts */
838 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
839 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
840 for (l = priv->contacts; l; l = l->next) {
841 EmpathyContact *contact;
846 handle = empathy_contact_get_handle (contact);
847 id = empathy_contact_get_id (contact);
849 g_assert (!G_STR_EMPTY (id));
850 g_array_append_val (handle_needed, id);
851 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
852 g_object_ref (contact));
854 if (G_STR_EMPTY (id)) {
855 g_array_append_val (id_needed, handle);
856 id_needed_contacts = g_list_prepend (id_needed_contacts,
857 g_object_ref (contact));
860 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
861 id_needed_contacts = g_list_reverse (id_needed_contacts);
863 tp_cli_connection_call_request_handles (priv->connection,
865 TP_HANDLE_TYPE_CONTACT,
866 (const gchar**) handle_needed->data,
867 tp_contact_factory_request_handles_cb,
868 handle_needed_contacts, tp_contact_factory_list_free,
869 G_OBJECT (tp_factory));
871 tp_cli_connection_call_inspect_handles (priv->connection,
873 TP_HANDLE_TYPE_CONTACT,
875 tp_contact_factory_inspect_handles_cb,
876 id_needed_contacts, tp_contact_factory_list_free,
877 G_OBJECT (tp_factory));
879 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
882 g_array_free (handle_needed, TRUE);
883 g_array_free (id_needed, TRUE);
887 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
889 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
891 /* Get our own handle */
892 tp_cli_connection_call_get_self_handle (priv->connection,
894 tp_contact_factory_got_self_handle_cb,
896 G_OBJECT (tp_factory));
900 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
902 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
903 gboolean connection_ready;
905 if (priv->connection) {
906 /* We already have our connection object */
910 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
911 if (!priv->connection) {
915 /* We got a new connection, wait for it to be ready */
916 g_signal_connect_swapped (priv->connection, "invalidated",
917 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
920 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
921 if (connection_ready) {
922 tp_contact_factory_connection_ready_cb (tp_factory);
924 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
925 G_CALLBACK (tp_contact_factory_connection_ready_cb),
931 tp_contact_factory_status_changed_cb (MissionControl *mc,
932 TpConnectionStatus status,
934 TpConnectionStatusReason reason,
935 const gchar *unique_name,
936 EmpathyTpContactFactory *tp_factory)
938 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
941 account = mc_account_lookup (unique_name);
942 if (account && empathy_account_equal (account, priv->account)) {
943 tp_contact_factory_status_updated (tp_factory);
945 g_object_unref (account);
949 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
950 EmpathyContact *contact)
952 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
954 g_object_weak_ref (G_OBJECT (contact),
955 tp_contact_factory_weak_notify,
957 priv->contacts = g_list_prepend (priv->contacts, contact);
959 DEBUG ("Contact added: %s (%d)",
960 empathy_contact_get_id (contact),
961 empathy_contact_get_handle (contact));
965 tp_contact_factory_hold_handles_cb (TpConnection *connection,
971 DEBUG ("Failed to hold handles: %s", error->message);
976 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
978 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
980 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
982 return g_object_ref (priv->user);
986 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
989 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
990 EmpathyContact *contact;
992 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
993 g_return_val_if_fail (id != NULL, NULL);
995 /* Check if the contact already exists */
996 contact = tp_contact_factory_find_by_id (tp_factory, id);
998 return g_object_ref (contact);
1001 /* Create new contact */
1002 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1003 "account", priv->account,
1006 tp_contact_factory_add_contact (tp_factory, contact);
1009 const gchar *contact_ids[] = {id, NULL};
1012 contacts = g_list_prepend (NULL, g_object_ref (contact));
1013 tp_cli_connection_call_request_handles (priv->connection,
1015 TP_HANDLE_TYPE_CONTACT,
1017 tp_contact_factory_request_handles_cb,
1018 contacts, tp_contact_factory_list_free,
1019 G_OBJECT (tp_factory));
1026 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1029 EmpathyContact *contact;
1033 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1035 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1036 g_array_append_val (handles, handle);
1038 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1039 g_array_free (handles, TRUE);
1041 contact = contacts ? contacts->data : NULL;
1042 g_list_free (contacts);
1048 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1049 const GArray *handles)
1051 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1052 GList *contacts = NULL;
1053 GArray *new_handles;
1054 GList *new_contacts = NULL;
1057 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1058 g_return_val_if_fail (handles != NULL, NULL);
1060 /* Search all contacts we already have */
1061 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1062 for (i = 0; i < handles->len; i++) {
1063 EmpathyContact *contact;
1066 handle = g_array_index (handles, guint, i);
1071 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1073 contacts = g_list_prepend (contacts, g_object_ref (contact));
1075 g_array_append_val (new_handles, handle);
1079 if (new_handles->len == 0) {
1080 g_array_free (new_handles, TRUE);
1084 /* Create new contacts */
1085 for (i = 0; i < new_handles->len; i++) {
1086 EmpathyContact *contact;
1089 handle = g_array_index (new_handles, guint, i);
1091 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1092 "account", priv->account,
1095 tp_contact_factory_add_contact (tp_factory, contact);
1096 contacts = g_list_prepend (contacts, contact);
1097 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1099 new_contacts = g_list_reverse (new_contacts);
1102 /* Get the IDs of all new handles */
1103 tp_cli_connection_call_inspect_handles (priv->connection,
1105 TP_HANDLE_TYPE_CONTACT,
1107 tp_contact_factory_inspect_handles_cb,
1108 new_contacts, tp_contact_factory_list_free,
1109 G_OBJECT (tp_factory));
1111 /* Hold all new handles. */
1112 /* FIXME: Should be unholded when removed from the factory */
1113 tp_cli_connection_call_hold_handles (priv->connection,
1115 TP_HANDLE_TYPE_CONTACT,
1117 tp_contact_factory_hold_handles_cb,
1119 G_OBJECT (tp_factory));
1121 tp_contact_factory_request_everything (tp_factory, new_handles);
1124 g_array_free (new_handles, TRUE);
1130 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1131 EmpathyContact *contact,
1134 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1135 GHashTable *new_alias;
1138 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1139 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1140 g_return_if_fail (priv->ready);
1141 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1144 handle = empathy_contact_get_handle (contact);
1146 DEBUG ("Setting alias for contact %s (%d) to %s",
1147 empathy_contact_get_id (contact),
1150 new_alias = g_hash_table_new_full (g_direct_hash,
1155 g_hash_table_insert (new_alias,
1156 GUINT_TO_POINTER (handle),
1159 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1162 tp_contact_factory_set_aliases_cb,
1164 G_OBJECT (tp_factory));
1166 g_hash_table_destroy (new_alias);
1170 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1173 const gchar *mime_type)
1175 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1177 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1178 g_return_if_fail (priv->ready);
1180 if (data && size > 0 && size < G_MAXUINT) {
1183 avatar.data = (gchar*) data;
1186 DEBUG ("Setting avatar on account %s",
1187 mc_account_get_unique_name (priv->account));
1189 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1193 tp_contact_factory_set_avatar_cb,
1195 G_OBJECT (tp_factory));
1197 DEBUG ("Clearing avatar on account %s",
1198 mc_account_get_unique_name (priv->account));
1200 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1202 tp_contact_factory_clear_avatar_cb,
1204 G_OBJECT (tp_factory));
1209 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1211 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1213 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1219 tp_contact_factory_get_property (GObject *object,
1224 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1228 g_value_set_object (value, priv->account);
1231 g_value_set_boolean (value, priv->ready);
1234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1240 tp_contact_factory_set_property (GObject *object,
1242 const GValue *value,
1245 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1249 priv->account = g_object_ref (g_value_get_object (value));
1252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1258 tp_contact_factory_finalize (GObject *object)
1260 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1263 DEBUG ("Finalized: %p (%s)", object,
1264 mc_account_get_normalized_name (priv->account));
1266 empathy_disconnect_account_status_changed (priv->token);
1268 for (l = priv->contacts; l; l = l->next) {
1269 g_object_weak_unref (G_OBJECT (l->data),
1270 tp_contact_factory_weak_notify,
1274 g_list_free (priv->contacts);
1275 g_object_unref (priv->mc);
1276 g_object_unref (priv->account);
1277 g_object_unref (priv->user);
1279 if (priv->connection) {
1280 g_signal_handlers_disconnect_by_func (priv->connection,
1281 tp_contact_factory_connection_invalidated_cb,
1283 g_object_unref (priv->connection);
1286 g_strfreev (priv->avatar_mime_types);
1288 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1292 tp_contact_factory_constructor (GType type,
1294 GObjectConstructParam *props)
1296 GObject *tp_factory;
1297 EmpathyTpContactFactoryPriv *priv;
1299 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1300 priv = GET_PRIV (tp_factory);
1302 priv->ready = FALSE;
1303 priv->user = empathy_contact_new (priv->account);
1304 empathy_contact_set_is_user (priv->user, TRUE);
1305 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1306 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1312 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1314 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1316 object_class->finalize = tp_contact_factory_finalize;
1317 object_class->constructor = tp_contact_factory_constructor;
1318 object_class->get_property = tp_contact_factory_get_property;
1319 object_class->set_property = tp_contact_factory_set_property;
1321 g_object_class_install_property (object_class,
1323 g_param_spec_object ("account",
1324 "Factory's Account",
1325 "The account associated with the factory",
1328 G_PARAM_CONSTRUCT_ONLY));
1329 g_object_class_install_property (object_class,
1331 g_param_spec_boolean ("ready",
1332 "Wheter the factor is ready",
1333 "Is the factory ready",
1337 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1341 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1343 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1344 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1346 tp_factory->priv = priv;
1347 priv->mc = empathy_mission_control_new ();
1348 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1349 G_CALLBACK (tp_contact_factory_status_changed_cb),
1353 EmpathyTpContactFactory *
1354 empathy_tp_contact_factory_new (McAccount *account)
1356 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,