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 const 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);
63 static EmpathyContact *
64 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
67 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
70 for (l = priv->contacts; l; l = l->next) {
71 if (empathy_contact_get_handle (l->data) == handle) {
79 static EmpathyContact *
80 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
83 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
86 for (l = priv->contacts; l; l = l->next) {
87 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
96 tp_contact_factory_weak_notify (gpointer data,
97 GObject *where_the_object_was)
99 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
101 DEBUG ("Remove finalized contact %p", where_the_object_was);
103 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
107 tp_contact_factory_presences_table_foreach (const gchar *state_str,
108 GHashTable *presences_table,
109 EmpathyContact *contact)
111 const GValue *message;
112 const gchar *message_str = NULL;
114 empathy_contact_set_presence (contact,
115 empathy_presence_from_str (state_str));
117 message = g_hash_table_lookup (presences_table, "message");
119 message_str = g_value_get_string (message);
122 if (!G_STR_EMPTY (message_str)) {
123 empathy_contact_set_presence_message (contact, message_str);
125 empathy_contact_set_presence_message (contact, NULL);
130 tp_contact_factory_parse_presence_foreach (guint handle,
131 GValueArray *presence_struct,
132 EmpathyTpContactFactory *tp_factory)
134 GHashTable *presences_table;
135 EmpathyContact *contact;
137 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
142 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
144 g_hash_table_foreach (presences_table,
145 (GHFunc) tp_contact_factory_presences_table_foreach,
148 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
149 empathy_contact_get_id (contact),
151 empathy_contact_get_presence_message (contact),
152 empathy_contact_get_presence (contact));
156 tp_contact_factory_get_presence_cb (TpConnection *connection,
157 GHashTable *handle_table,
163 DEBUG ("Error getting presence: %s", error->message);
164 if (error->domain == TP_DBUS_ERRORS &&
165 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
166 guint *handles = user_data;
168 /* We have no presence iface, set default presence
170 while (*handles != 0) {
171 EmpathyContact *contact;
173 contact = tp_contact_factory_find_by_handle (
174 (EmpathyTpContactFactory*) tp_factory,
177 empathy_contact_set_presence (contact,
178 MC_PRESENCE_AVAILABLE);
188 g_hash_table_foreach (handle_table,
189 (GHFunc) tp_contact_factory_parse_presence_foreach,
190 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
194 tp_contact_factory_presence_update_cb (TpConnection *connection,
195 GHashTable *handle_table,
199 g_hash_table_foreach (handle_table,
200 (GHFunc) tp_contact_factory_parse_presence_foreach,
201 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
205 tp_contact_factory_set_aliases_cb (TpConnection *connection,
211 DEBUG ("Error setting alias: %s", error->message);
216 tp_contact_factory_request_aliases_cb (TpConnection *connection,
217 const gchar **contact_names,
222 guint *handles = user_data;
227 DEBUG ("Error requesting aliases: %s", error->message);
229 /* If we failed to get alias set it to NULL, like that if
230 * someone is waiting for the name to be ready it won't wait
232 while (*handles != 0) {
233 EmpathyContact *contact;
235 contact = tp_contact_factory_find_by_handle (
236 (EmpathyTpContactFactory*) tp_factory,
239 empathy_contact_set_name (contact, NULL);
247 for (name = contact_names; *name; name++) {
248 EmpathyContact *contact;
250 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
256 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
257 empathy_contact_get_id (contact),
258 empathy_contact_get_handle (contact),
261 empathy_contact_set_name (contact, *name);
268 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
269 const GPtrArray *renamed_handlers,
271 GObject *weak_object)
273 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
276 for (i = 0; renamed_handlers->len > i; i++) {
279 GValueArray *renamed_struct;
280 EmpathyContact *contact;
282 renamed_struct = g_ptr_array_index (renamed_handlers, i);
283 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
284 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
285 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
288 /* We don't know this contact, skip */
292 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
293 empathy_contact_get_id (contact),
296 empathy_contact_set_name (contact, alias);
301 tp_contact_factory_set_avatar_cb (TpConnection *connection,
308 DEBUG ("Error setting avatar: %s", error->message);
313 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
319 DEBUG ("Error clearing avatar: %s", error->message);
324 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
327 const GArray *avatar_data,
328 const gchar *mime_type,
332 EmpathyContact *contact;
334 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
340 DEBUG ("Avatar retrieved for contact %s (%d)",
341 empathy_contact_get_id (contact),
344 empathy_contact_load_avatar_data (contact,
352 tp_contact_factory_request_avatars_cb (TpConnection *connection,
358 DEBUG ("Error requesting avatars: %s", error->message);
363 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
367 EmpathyContact *contact;
368 EmpathyAvatar *avatar;
370 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
375 /* Check if we have an avatar */
376 if (G_STR_EMPTY (token)) {
377 empathy_contact_set_avatar (contact, NULL);
381 /* Check if the avatar changed */
382 avatar = empathy_contact_get_avatar (contact);
383 if (avatar && !tp_strdiff (avatar->token, token)) {
387 /* The avatar changed, search the new one in the cache */
388 if (empathy_contact_load_avatar_cache (contact, token)) {
389 /* Got from cache, use it */
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_cli_connection_call_inspect_handles (priv->connection,
810 TP_HANDLE_TYPE_CONTACT,
812 tp_contact_factory_inspect_handles_cb,
813 id_needed_contacts, tp_contact_factory_list_free,
814 G_OBJECT (tp_factory));
816 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
819 g_array_free (handle_needed, TRUE);
820 g_array_free (id_needed, TRUE);
824 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
826 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
828 /* Get our own handle */
829 tp_cli_connection_call_get_self_handle (priv->connection,
831 tp_contact_factory_got_self_handle_cb,
833 G_OBJECT (tp_factory));
837 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
839 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
840 gboolean connection_ready;
842 if (priv->connection) {
843 /* We already have our connection object */
847 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
848 if (!priv->connection) {
852 /* We got a new connection, wait for it to be ready */
853 g_signal_connect_swapped (priv->connection, "invalidated",
854 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
857 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
858 if (connection_ready) {
859 tp_contact_factory_connection_ready_cb (tp_factory);
861 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
862 G_CALLBACK (tp_contact_factory_connection_ready_cb),
868 tp_contact_factory_status_changed_cb (MissionControl *mc,
869 TpConnectionStatus status,
871 TpConnectionStatusReason reason,
872 const gchar *unique_name,
873 EmpathyTpContactFactory *tp_factory)
875 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
878 account = mc_account_lookup (unique_name);
879 if (account && empathy_account_equal (account, priv->account)) {
880 tp_contact_factory_status_updated (tp_factory);
882 g_object_unref (account);
886 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
887 EmpathyContact *contact)
889 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
891 g_object_weak_ref (G_OBJECT (contact),
892 tp_contact_factory_weak_notify,
894 priv->contacts = g_list_prepend (priv->contacts, contact);
896 DEBUG ("Contact added: %s (%d)",
897 empathy_contact_get_id (contact),
898 empathy_contact_get_handle (contact));
902 tp_contact_factory_hold_handles_cb (TpConnection *connection,
908 DEBUG ("Failed to hold handles: %s", error->message);
913 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
915 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
917 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
919 return g_object_ref (priv->user);
923 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
926 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
927 EmpathyContact *contact;
929 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
930 g_return_val_if_fail (id != NULL, NULL);
932 /* Check if the contact already exists */
933 contact = tp_contact_factory_find_by_id (tp_factory, id);
935 return g_object_ref (contact);
938 /* Create new contact */
939 contact = g_object_new (EMPATHY_TYPE_CONTACT,
940 "account", priv->account,
943 tp_contact_factory_add_contact (tp_factory, contact);
946 const gchar *contact_ids[] = {id, NULL};
949 contacts = g_list_prepend (NULL, g_object_ref (contact));
950 tp_cli_connection_call_request_handles (priv->connection,
952 TP_HANDLE_TYPE_CONTACT,
954 tp_contact_factory_request_handles_cb,
955 contacts, tp_contact_factory_list_free,
956 G_OBJECT (tp_factory));
963 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
966 EmpathyContact *contact;
970 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
972 handles = g_array_new (FALSE, FALSE, sizeof (guint));
973 g_array_append_val (handles, handle);
975 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
976 g_array_free (handles, TRUE);
978 contact = contacts ? contacts->data : NULL;
979 g_list_free (contacts);
985 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
986 const GArray *handles)
988 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
989 GList *contacts = NULL;
991 GList *new_contacts = NULL;
994 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
995 g_return_val_if_fail (handles != NULL, NULL);
997 /* Search all contacts we already have */
998 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
999 for (i = 0; i < handles->len; i++) {
1000 EmpathyContact *contact;
1003 handle = g_array_index (handles, guint, i);
1008 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1010 contacts = g_list_prepend (contacts, g_object_ref (contact));
1012 g_array_append_val (new_handles, handle);
1016 if (new_handles->len == 0) {
1017 g_array_free (new_handles, TRUE);
1021 /* Create new contacts */
1022 for (i = 0; i < new_handles->len; i++) {
1023 EmpathyContact *contact;
1026 handle = g_array_index (new_handles, guint, i);
1028 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1029 "account", priv->account,
1032 tp_contact_factory_add_contact (tp_factory, contact);
1033 contacts = g_list_prepend (contacts, contact);
1034 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1036 new_contacts = g_list_reverse (new_contacts);
1039 /* Get the IDs of all new handles */
1040 tp_cli_connection_call_inspect_handles (priv->connection,
1042 TP_HANDLE_TYPE_CONTACT,
1044 tp_contact_factory_inspect_handles_cb,
1045 new_contacts, tp_contact_factory_list_free,
1046 G_OBJECT (tp_factory));
1048 /* Hold all new handles. */
1049 /* FIXME: Should be unholded when removed from the factory */
1050 tp_cli_connection_call_hold_handles (priv->connection,
1052 TP_HANDLE_TYPE_CONTACT,
1054 tp_contact_factory_hold_handles_cb,
1056 G_OBJECT (tp_factory));
1058 tp_contact_factory_request_everything (tp_factory, new_handles);
1061 g_array_free (new_handles, TRUE);
1067 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1068 EmpathyContact *contact,
1071 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1072 GHashTable *new_alias;
1075 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1076 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1077 g_return_if_fail (priv->ready);
1078 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1081 handle = empathy_contact_get_handle (contact);
1083 DEBUG ("Setting alias for contact %s (%d) to %s",
1084 empathy_contact_get_id (contact),
1087 new_alias = g_hash_table_new_full (g_direct_hash,
1092 g_hash_table_insert (new_alias,
1093 GUINT_TO_POINTER (handle),
1096 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1099 tp_contact_factory_set_aliases_cb,
1101 G_OBJECT (tp_factory));
1103 g_hash_table_destroy (new_alias);
1107 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1110 const gchar *mime_type)
1112 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1114 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1115 g_return_if_fail (priv->ready);
1117 if (data && size > 0 && size < G_MAXUINT) {
1120 avatar.data = (gchar*) data;
1123 DEBUG ("Setting avatar on account %s",
1124 mc_account_get_unique_name (priv->account));
1126 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1130 tp_contact_factory_set_avatar_cb,
1132 G_OBJECT (tp_factory));
1134 DEBUG ("Clearing avatar on account %s",
1135 mc_account_get_unique_name (priv->account));
1137 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1139 tp_contact_factory_clear_avatar_cb,
1141 G_OBJECT (tp_factory));
1146 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1148 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1150 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1156 tp_contact_factory_get_property (GObject *object,
1161 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1165 g_value_set_object (value, priv->account);
1168 g_value_set_boolean (value, priv->ready);
1171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1177 tp_contact_factory_set_property (GObject *object,
1179 const GValue *value,
1182 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1186 priv->account = g_object_ref (g_value_get_object (value));
1189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1195 tp_contact_factory_finalize (GObject *object)
1197 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1200 DEBUG ("Finalized: %p (%s)", object,
1201 mc_account_get_normalized_name (priv->account));
1203 empathy_disconnect_account_status_changed (priv->token);
1205 for (l = priv->contacts; l; l = l->next) {
1206 g_object_weak_unref (G_OBJECT (l->data),
1207 tp_contact_factory_weak_notify,
1211 g_list_free (priv->contacts);
1212 g_object_unref (priv->mc);
1213 g_object_unref (priv->account);
1214 g_object_unref (priv->user);
1216 if (priv->connection) {
1217 g_signal_handlers_disconnect_by_func (priv->connection,
1218 tp_contact_factory_connection_invalidated_cb,
1220 g_object_unref (priv->connection);
1223 g_strfreev (priv->avatar_mime_types);
1225 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1229 tp_contact_factory_constructor (GType type,
1231 GObjectConstructParam *props)
1233 GObject *tp_factory;
1234 EmpathyTpContactFactoryPriv *priv;
1236 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1237 priv = GET_PRIV (tp_factory);
1239 priv->ready = FALSE;
1240 priv->user = empathy_contact_new (priv->account);
1241 empathy_contact_set_is_user (priv->user, TRUE);
1242 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1243 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1249 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1251 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1253 object_class->finalize = tp_contact_factory_finalize;
1254 object_class->constructor = tp_contact_factory_constructor;
1255 object_class->get_property = tp_contact_factory_get_property;
1256 object_class->set_property = tp_contact_factory_set_property;
1258 g_object_class_install_property (object_class,
1260 g_param_spec_object ("account",
1261 "Factory's Account",
1262 "The account associated with the factory",
1265 G_PARAM_CONSTRUCT_ONLY));
1266 g_object_class_install_property (object_class,
1268 g_param_spec_boolean ("ready",
1269 "Wheter the factor is ready",
1270 "Is the factory ready",
1274 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1278 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1280 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1281 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1283 tp_factory->priv = priv;
1284 priv->mc = empathy_mission_control_new ();
1285 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1286 G_CALLBACK (tp_contact_factory_status_changed_cb),
1290 EmpathyTpContactFactory *
1291 empathy_tp_contact_factory_new (McAccount *account)
1293 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,