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) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
37 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv))
39 struct _EmpathyTpContactFactoryPriv {
42 TpConnection *connection;
50 static void empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass);
51 static void empathy_tp_contact_factory_init (EmpathyTpContactFactory *factory);
53 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
61 static EmpathyContact *
62 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
65 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
68 for (l = priv->contacts; l; l = l->next) {
69 if (empathy_contact_get_handle (l->data) == handle) {
77 static EmpathyContact *
78 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
81 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
84 for (l = priv->contacts; l; l = l->next) {
85 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
94 tp_contact_factory_weak_notify (gpointer data,
95 GObject *where_the_object_was)
97 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
99 DEBUG ("Remove finalized contact %p", where_the_object_was);
101 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
105 tp_contact_factory_presences_table_foreach (const gchar *state_str,
106 GHashTable *presences_table,
107 EmpathyContact *contact)
109 const GValue *message;
111 empathy_contact_set_presence (contact,
112 empathy_presence_from_str (state_str));
114 message = g_hash_table_lookup (presences_table, "message");
115 if (message != NULL) {
116 empathy_contact_set_presence_message (contact,
117 g_value_get_string (message));
119 empathy_contact_set_presence_message (contact, NULL);
124 tp_contact_factory_parse_presence_foreach (guint handle,
125 GValueArray *presence_struct,
126 EmpathyTpContactFactory *tp_factory)
128 GHashTable *presences_table;
129 EmpathyContact *contact;
131 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
136 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
138 g_hash_table_foreach (presences_table,
139 (GHFunc) tp_contact_factory_presences_table_foreach,
142 DEBUG ("Changing presence for contact %s (%d) to %s (%d)",
143 empathy_contact_get_id (contact),
145 empathy_contact_get_presence_message (contact),
146 empathy_contact_get_presence (contact));
150 tp_contact_factory_get_presence_cb (TpConnection *connection,
151 GHashTable *handle_table,
157 DEBUG ("Error getting presence: %s", error->message);
158 if (error->domain == TP_DBUS_ERRORS &&
159 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
160 guint *handles = user_data;
162 /* We have no presence iface, set default presence
164 while (*handles != 0) {
165 EmpathyContact *contact;
167 contact = tp_contact_factory_find_by_handle (
168 (EmpathyTpContactFactory*) tp_factory,
171 empathy_contact_set_presence (contact,
172 MC_PRESENCE_AVAILABLE);
182 g_hash_table_foreach (handle_table,
183 (GHFunc) tp_contact_factory_parse_presence_foreach,
184 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
188 tp_contact_factory_presence_update_cb (TpConnection *connection,
189 GHashTable *handle_table,
193 g_hash_table_foreach (handle_table,
194 (GHFunc) tp_contact_factory_parse_presence_foreach,
195 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
199 tp_contact_factory_set_aliases_cb (TpConnection *connection,
205 DEBUG ("Error setting alias: %s", error->message);
210 tp_contact_factory_request_aliases_cb (TpConnection *connection,
211 const gchar **contact_names,
216 guint *handles = user_data;
221 DEBUG ("Error requesting aliases: %s", error->message);
223 /* If we failed to get alias set it to NULL, like that if
224 * someone is waiting for the name to be ready it won't wait
226 while (*handles != 0) {
227 EmpathyContact *contact;
229 contact = tp_contact_factory_find_by_handle (
230 (EmpathyTpContactFactory*) tp_factory,
233 empathy_contact_set_name (contact, NULL);
241 for (name = contact_names; *name; name++) {
242 EmpathyContact *contact;
244 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
250 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
251 empathy_contact_get_id (contact),
252 empathy_contact_get_handle (contact),
255 empathy_contact_set_name (contact, *name);
262 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
263 const GPtrArray *renamed_handlers,
265 GObject *weak_object)
267 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
270 for (i = 0; renamed_handlers->len > i; i++) {
273 GValueArray *renamed_struct;
274 EmpathyContact *contact;
276 renamed_struct = g_ptr_array_index (renamed_handlers, i);
277 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
278 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
279 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
282 /* We don't know this contact, skip */
286 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
287 empathy_contact_get_id (contact),
290 empathy_contact_set_name (contact, alias);
295 tp_contact_factory_set_avatar_cb (TpConnection *connection,
302 DEBUG ("Error setting avatar: %s", error->message);
307 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
313 DEBUG ("Error clearing avatar: %s", error->message);
318 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
321 const GArray *avatar_data,
322 const gchar *mime_type,
326 EmpathyContact *contact;
327 EmpathyAvatar *avatar;
329 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
335 DEBUG ("Avatar retrieved for contact %s (%d)",
336 empathy_contact_get_id (contact),
339 avatar = empathy_avatar_new (avatar_data->data,
344 empathy_contact_set_avatar (contact, avatar);
345 empathy_avatar_unref (avatar);
349 tp_contact_factory_request_avatars_cb (TpConnection *connection,
355 DEBUG ("Error requesting avatars: %s", error->message);
360 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
364 EmpathyContact *contact;
365 EmpathyAvatar *avatar;
367 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
372 /* Check if we have an avatar */
373 if (G_STR_EMPTY (token)) {
374 empathy_contact_set_avatar (contact, NULL);
378 /* Check if the avatar changed */
379 avatar = empathy_contact_get_avatar (contact);
380 if (avatar && !tp_strdiff (avatar->token, token)) {
384 /* The avatar changed, search the new one in the cache */
385 avatar = empathy_avatar_new_from_cache (token);
387 /* Got from cache, use it */
388 empathy_contact_set_avatar (contact, avatar);
389 empathy_avatar_unref (avatar);
393 /* Avatar is not up-to-date, we have to request it. */
398 EmpathyTpContactFactory *tp_factory;
403 tp_contact_factory_avatar_tokens_foreach (gpointer key,
407 TokensData *data = user_data;
408 const gchar *token = value;
409 guint handle = GPOINTER_TO_UINT (key);
411 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
413 g_array_append_val (data->handles, handle);
418 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
427 DEBUG ("Error getting known avatars tokens: %s", error->message);
431 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
432 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
433 g_hash_table_foreach (tokens,
434 tp_contact_factory_avatar_tokens_foreach,
437 DEBUG ("Got %d tokens, need to request %d avatars",
438 g_hash_table_size (tokens), data.handles->len);
440 /* Request needed avatars */
441 if (data.handles->len > 0) {
442 tp_cli_connection_interface_avatars_call_request_avatars (connection,
445 tp_contact_factory_request_avatars_cb,
450 g_array_free (data.handles, TRUE);
454 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
456 const gchar *new_token,
462 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
463 handle, new_token)) {
464 /* Avatar was cached, nothing to do */
468 DEBUG ("Need to request avatar for token %s", new_token);
470 handles = g_array_new (FALSE, FALSE, sizeof (guint));
471 g_array_append_val (handles, handle);
473 tp_cli_connection_interface_avatars_call_request_avatars (connection,
476 tp_contact_factory_request_avatars_cb,
479 g_array_free (handles, TRUE);
483 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
485 const gchar *channel_type,
489 EmpathyContact *contact;
490 EmpathyCapabilities capabilities;
492 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
497 capabilities = empathy_contact_get_capabilities (contact);
498 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
500 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
501 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
502 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
503 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
504 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
506 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
507 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
511 DEBUG ("Changing capabilities for contact %s (%d) to %d",
512 empathy_contact_get_id (contact),
513 empathy_contact_get_handle (contact),
516 empathy_contact_set_capabilities (contact, capabilities);
520 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
521 const GPtrArray *capabilities,
524 GObject *weak_object)
526 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
530 DEBUG ("Error getting capabilities: %s", error->message);
531 /* FIXME Should set the capabilities of the contacts for which this request
532 * originated to NONE */
536 for (i = 0; i < capabilities->len; i++) {
539 const gchar *channel_type;
543 values = g_ptr_array_index (capabilities, i);
544 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
545 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
546 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
547 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
549 tp_contact_factory_update_capabilities (tp_factory,
558 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
559 const GPtrArray *capabilities,
561 GObject *weak_object)
563 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
566 for (i = 0; i < capabilities->len; i++) {
569 const gchar *channel_type;
573 values = g_ptr_array_index (capabilities, i);
574 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
575 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
576 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
577 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
579 tp_contact_factory_update_capabilities (tp_factory,
588 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
589 const GArray *handles)
591 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
594 g_return_if_fail (priv->ready);
596 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
597 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
598 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
601 tp_contact_factory_get_presence_cb,
603 G_OBJECT (tp_factory));
605 /* FIXME: Sometimes the dbus call timesout because CM takes
606 * too much time to request all aliases from the server,
607 * that's why we increase the timeout here. See fd.o bug #14795 */
608 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
609 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
610 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
613 tp_contact_factory_request_aliases_cb,
615 G_OBJECT (tp_factory));
617 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
620 tp_contact_factory_get_known_avatar_tokens_cb,
622 G_OBJECT (tp_factory));
624 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
627 tp_contact_factory_get_capabilities_cb,
629 G_OBJECT (tp_factory));
633 tp_contact_factory_list_free (gpointer data)
637 g_list_foreach (l, (GFunc) g_object_unref, NULL);
642 tp_contact_factory_request_handles_cb (TpConnection *connection,
643 const GArray *handles,
648 GList *contacts = user_data;
653 DEBUG ("Failed to request handles: %s", error->message);
657 for (l = contacts; l; l = l->next) {
660 handle = g_array_index (handles, guint, i);
661 empathy_contact_set_handle (l->data, handle);
666 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
671 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
678 GList *contacts = user_data;
682 DEBUG ("Failed to inspect handles: %s", error->message);
687 for (l = contacts; l; l = l->next) {
688 empathy_contact_set_id (l->data, *id);
694 tp_contact_factory_disconnect_contact_foreach (gpointer data,
697 EmpathyContact *contact = data;
699 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
700 empathy_contact_set_handle (contact, 0);
704 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
706 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
708 DEBUG ("Connection invalidated");
710 g_object_unref (priv->connection);
711 priv->connection = NULL;
713 g_object_notify (G_OBJECT (tp_factory), "ready");
716 g_list_foreach (priv->contacts,
717 tp_contact_factory_disconnect_contact_foreach,
723 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
729 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
731 GArray *handle_needed;
733 GList *handle_needed_contacts = NULL;
734 GList *id_needed_contacts = NULL;
737 DEBUG ("Failed to get self handles: %s", error->message);
741 DEBUG ("Connection ready");
743 empathy_contact_set_handle (priv->user, handle);
745 g_object_notify (tp_factory, "ready");
747 /* Connect signals */
748 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
749 tp_contact_factory_aliases_changed_cb,
751 G_OBJECT (tp_factory),
753 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
754 tp_contact_factory_avatar_updated_cb,
756 G_OBJECT (tp_factory),
758 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
759 tp_contact_factory_avatar_retrieved_cb,
761 G_OBJECT (tp_factory),
763 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
764 tp_contact_factory_presence_update_cb,
766 G_OBJECT (tp_factory),
768 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
769 tp_contact_factory_capabilities_changed_cb,
771 G_OBJECT (tp_factory),
774 /* Request needed info for all existing contacts */
775 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
776 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
777 for (l = priv->contacts; l; l = l->next) {
778 EmpathyContact *contact;
783 handle = empathy_contact_get_handle (contact);
784 id = empathy_contact_get_id (contact);
786 g_assert (!G_STR_EMPTY (id));
787 g_array_append_val (handle_needed, id);
788 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
789 g_object_ref (contact));
791 if (G_STR_EMPTY (id)) {
792 g_array_append_val (id_needed, handle);
793 id_needed_contacts = g_list_prepend (id_needed_contacts,
794 g_object_ref (contact));
797 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
798 id_needed_contacts = g_list_reverse (id_needed_contacts);
800 tp_cli_connection_call_request_handles (priv->connection,
802 TP_HANDLE_TYPE_CONTACT,
803 (const gchar**) handle_needed->data,
804 tp_contact_factory_request_handles_cb,
805 handle_needed_contacts, tp_contact_factory_list_free,
806 G_OBJECT (tp_factory));
808 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
810 tp_cli_connection_call_inspect_handles (priv->connection,
812 TP_HANDLE_TYPE_CONTACT,
814 tp_contact_factory_inspect_handles_cb,
815 id_needed_contacts, tp_contact_factory_list_free,
816 G_OBJECT (tp_factory));
818 g_array_free (handle_needed, TRUE);
819 g_array_free (id_needed, TRUE);
823 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
825 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
827 /* Get our own handle */
828 tp_cli_connection_call_get_self_handle (priv->connection,
830 tp_contact_factory_got_self_handle_cb,
832 G_OBJECT (tp_factory));
836 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
838 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
839 gboolean connection_ready;
841 if (priv->connection) {
842 /* We already have our connection object */
846 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
847 if (!priv->connection) {
851 /* We got a new connection, wait for it to be ready */
852 g_signal_connect_swapped (priv->connection, "invalidated",
853 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
856 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
857 if (connection_ready) {
858 tp_contact_factory_connection_ready_cb (tp_factory);
860 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
861 G_CALLBACK (tp_contact_factory_connection_ready_cb),
867 tp_contact_factory_status_changed_cb (MissionControl *mc,
868 TpConnectionStatus status,
870 TpConnectionStatusReason reason,
871 const gchar *unique_name,
872 EmpathyTpContactFactory *tp_factory)
874 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
877 account = mc_account_lookup (unique_name);
878 if (account && empathy_account_equal (account, priv->account)) {
879 tp_contact_factory_status_updated (tp_factory);
881 g_object_unref (account);
885 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
886 EmpathyContact *contact)
888 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
890 g_object_weak_ref (G_OBJECT (contact),
891 tp_contact_factory_weak_notify,
893 priv->contacts = g_list_prepend (priv->contacts, contact);
895 DEBUG ("Contact added: %s (%d)",
896 empathy_contact_get_id (contact),
897 empathy_contact_get_handle (contact));
901 tp_contact_factory_hold_handles_cb (TpConnection *connection,
907 DEBUG ("Failed to hold handles: %s", error->message);
912 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
914 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
916 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
918 return g_object_ref (priv->user);
922 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
925 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
926 EmpathyContact *contact;
928 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
929 g_return_val_if_fail (id != NULL, NULL);
931 /* Check if the contact already exists */
932 contact = tp_contact_factory_find_by_id (tp_factory, id);
934 return g_object_ref (contact);
937 /* Create new contact */
938 contact = g_object_new (EMPATHY_TYPE_CONTACT,
939 "account", priv->account,
942 tp_contact_factory_add_contact (tp_factory, contact);
945 const gchar *contact_ids[] = {id, NULL};
948 contacts = g_list_prepend (NULL, g_object_ref (contact));
949 tp_cli_connection_call_request_handles (priv->connection,
951 TP_HANDLE_TYPE_CONTACT,
953 tp_contact_factory_request_handles_cb,
954 contacts, tp_contact_factory_list_free,
955 G_OBJECT (tp_factory));
962 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
965 EmpathyContact *contact;
969 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
971 handles = g_array_new (FALSE, FALSE, sizeof (guint));
972 g_array_append_val (handles, handle);
974 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
975 g_array_free (handles, TRUE);
977 contact = contacts ? contacts->data : NULL;
978 g_list_free (contacts);
984 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
985 const GArray *handles)
987 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
988 GList *contacts = NULL;
990 GList *new_contacts = NULL;
993 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
994 g_return_val_if_fail (handles != NULL, NULL);
996 /* Search all contacts we already have */
997 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
998 for (i = 0; i < handles->len; i++) {
999 EmpathyContact *contact;
1002 handle = g_array_index (handles, guint, i);
1007 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1009 contacts = g_list_prepend (contacts, g_object_ref (contact));
1011 g_array_append_val (new_handles, handle);
1015 if (new_handles->len == 0) {
1016 g_array_free (new_handles, TRUE);
1020 /* Create new contacts */
1021 for (i = 0; i < new_handles->len; i++) {
1022 EmpathyContact *contact;
1025 handle = g_array_index (new_handles, guint, i);
1027 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1028 "account", priv->account,
1031 tp_contact_factory_add_contact (tp_factory, contact);
1032 contacts = g_list_prepend (contacts, contact);
1033 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1035 new_contacts = g_list_reverse (new_contacts);
1038 /* Get the IDs of all new handles */
1039 tp_cli_connection_call_inspect_handles (priv->connection,
1041 TP_HANDLE_TYPE_CONTACT,
1043 tp_contact_factory_inspect_handles_cb,
1044 new_contacts, tp_contact_factory_list_free,
1045 G_OBJECT (tp_factory));
1047 /* Hold all new handles. */
1048 /* FIXME: Should be unholded when removed from the factory */
1049 tp_cli_connection_call_hold_handles (priv->connection,
1051 TP_HANDLE_TYPE_CONTACT,
1053 tp_contact_factory_hold_handles_cb,
1055 G_OBJECT (tp_factory));
1057 tp_contact_factory_request_everything (tp_factory, new_handles);
1064 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1065 EmpathyContact *contact,
1068 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1069 GHashTable *new_alias;
1072 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1073 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1074 g_return_if_fail (priv->ready);
1075 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1078 handle = empathy_contact_get_handle (contact);
1080 DEBUG ("Setting alias for contact %s (%d) to %s",
1081 empathy_contact_get_id (contact),
1084 new_alias = g_hash_table_new_full (g_direct_hash,
1089 g_hash_table_insert (new_alias,
1090 GUINT_TO_POINTER (handle),
1093 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1096 tp_contact_factory_set_aliases_cb,
1098 G_OBJECT (tp_factory));
1100 g_hash_table_destroy (new_alias);
1104 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1107 const gchar *mime_type)
1109 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1111 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1112 g_return_if_fail (priv->ready);
1114 if (data && size > 0 && size < G_MAXUINT) {
1117 avatar.data = (gchar*) data;
1120 DEBUG ("Setting avatar on account %s",
1121 mc_account_get_unique_name (priv->account));
1123 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1127 tp_contact_factory_set_avatar_cb,
1129 G_OBJECT (tp_factory));
1131 DEBUG ("Clearing avatar on account %s",
1132 mc_account_get_unique_name (priv->account));
1134 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1136 tp_contact_factory_clear_avatar_cb,
1138 G_OBJECT (tp_factory));
1143 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1145 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1147 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1153 tp_contact_factory_get_property (GObject *object,
1158 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1162 g_value_set_object (value, priv->account);
1165 g_value_set_boolean (value, priv->ready);
1168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1174 tp_contact_factory_set_property (GObject *object,
1176 const GValue *value,
1179 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1183 priv->account = g_object_ref (g_value_get_object (value));
1186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1192 tp_contact_factory_finalize (GObject *object)
1194 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1197 DEBUG ("Finalized: %p (%s)", object,
1198 mc_account_get_normalized_name (priv->account));
1200 empathy_disconnect_account_status_changed (priv->token);
1202 for (l = priv->contacts; l; l = l->next) {
1203 g_object_weak_unref (G_OBJECT (l->data),
1204 tp_contact_factory_weak_notify,
1208 g_list_free (priv->contacts);
1209 g_object_unref (priv->mc);
1210 g_object_unref (priv->account);
1211 g_object_unref (priv->user);
1213 if (priv->connection) {
1214 g_signal_handlers_disconnect_by_func (priv->connection,
1215 tp_contact_factory_connection_invalidated_cb,
1217 g_object_unref (priv->connection);
1220 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1224 tp_contact_factory_constructor (GType type,
1226 GObjectConstructParam *props)
1228 GObject *tp_factory;
1229 EmpathyTpContactFactoryPriv *priv;
1231 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1232 priv = GET_PRIV (tp_factory);
1234 priv->ready = FALSE;
1235 priv->user = empathy_contact_new (priv->account);
1236 empathy_contact_set_is_user (priv->user, TRUE);
1237 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1238 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1244 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1246 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1248 object_class->finalize = tp_contact_factory_finalize;
1249 object_class->constructor = tp_contact_factory_constructor;
1250 object_class->get_property = tp_contact_factory_get_property;
1251 object_class->set_property = tp_contact_factory_set_property;
1253 g_object_class_install_property (object_class,
1255 g_param_spec_object ("account",
1256 "Factory's Account",
1257 "The account associated with the factory",
1260 G_PARAM_CONSTRUCT_ONLY));
1261 g_object_class_install_property (object_class,
1263 g_param_spec_boolean ("ready",
1264 "Wheter the factor is ready",
1265 "Is the factory ready",
1269 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1273 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1275 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1277 priv->mc = empathy_mission_control_new ();
1278 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1279 G_CALLBACK (tp_contact_factory_status_changed_cb),
1283 EmpathyTpContactFactory *
1284 empathy_tp_contact_factory_new (McAccount *account)
1286 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,