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);
64 static void tp_contact_factory_maybe_ready (EmpathyTpContactFactory *tp_factory);
67 static EmpathyContact *
68 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
71 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
74 for (l = priv->contacts; l; l = l->next) {
75 if (empathy_contact_get_handle (l->data) == handle) {
83 static EmpathyContact *
84 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
87 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
90 for (l = priv->contacts; l; l = l->next) {
91 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
100 tp_contact_factory_weak_notify (gpointer data,
101 GObject *where_the_object_was)
103 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
105 DEBUG ("Remove finalized contact %p", where_the_object_was);
107 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
111 tp_contact_factory_presences_table_foreach (const gchar *state_str,
112 GHashTable *presences_table,
113 EmpathyContact *contact)
115 const GValue *message;
116 const gchar *message_str = NULL;
118 empathy_contact_set_presence (contact,
119 empathy_presence_from_str (state_str));
121 message = g_hash_table_lookup (presences_table, "message");
123 message_str = g_value_get_string (message);
126 if (!G_STR_EMPTY (message_str)) {
127 empathy_contact_set_presence_message (contact, message_str);
129 empathy_contact_set_presence_message (contact, NULL);
134 tp_contact_factory_parse_presence_foreach (guint handle,
135 GValueArray *presence_struct,
136 EmpathyTpContactFactory *tp_factory)
138 GHashTable *presences_table;
139 EmpathyContact *contact;
141 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
146 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
148 g_hash_table_foreach (presences_table,
149 (GHFunc) tp_contact_factory_presences_table_foreach,
152 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
153 empathy_contact_get_id (contact),
155 empathy_contact_get_presence_message (contact),
156 empathy_contact_get_presence (contact));
160 tp_contact_factory_get_presence_cb (TpConnection *connection,
161 GHashTable *handle_table,
167 DEBUG ("Error getting presence: %s", error->message);
168 if (error->domain == TP_DBUS_ERRORS &&
169 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
170 guint *handles = user_data;
172 /* We have no presence iface, set default presence
174 while (*handles != 0) {
175 EmpathyContact *contact;
177 contact = tp_contact_factory_find_by_handle (
178 (EmpathyTpContactFactory*) tp_factory,
181 empathy_contact_set_presence (contact,
182 MC_PRESENCE_AVAILABLE);
192 g_hash_table_foreach (handle_table,
193 (GHFunc) tp_contact_factory_parse_presence_foreach,
194 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
198 tp_contact_factory_presence_update_cb (TpConnection *connection,
199 GHashTable *handle_table,
203 g_hash_table_foreach (handle_table,
204 (GHFunc) tp_contact_factory_parse_presence_foreach,
205 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
209 tp_contact_factory_set_aliases_cb (TpConnection *connection,
215 DEBUG ("Error setting alias: %s", error->message);
220 tp_contact_factory_request_aliases_cb (TpConnection *connection,
221 const gchar **contact_names,
226 guint *handles = user_data;
231 DEBUG ("Error requesting aliases: %s", error->message);
233 /* If we failed to get alias set it to NULL, like that if
234 * someone is waiting for the name to be ready it won't wait
236 while (*handles != 0) {
237 EmpathyContact *contact;
239 contact = tp_contact_factory_find_by_handle (
240 (EmpathyTpContactFactory*) tp_factory,
243 empathy_contact_set_name (contact, NULL);
251 for (name = contact_names; *name; name++) {
252 EmpathyContact *contact;
254 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
260 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
261 empathy_contact_get_id (contact),
262 empathy_contact_get_handle (contact),
265 empathy_contact_set_name (contact, *name);
272 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
273 const GPtrArray *renamed_handlers,
275 GObject *weak_object)
277 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
280 for (i = 0; renamed_handlers->len > i; i++) {
283 GValueArray *renamed_struct;
284 EmpathyContact *contact;
286 renamed_struct = g_ptr_array_index (renamed_handlers, i);
287 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
288 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
289 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
292 /* We don't know this contact, skip */
296 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
297 empathy_contact_get_id (contact),
300 empathy_contact_set_name (contact, alias);
305 tp_contact_factory_set_avatar_cb (TpConnection *connection,
312 DEBUG ("Error setting avatar: %s", error->message);
317 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
323 DEBUG ("Error clearing avatar: %s", error->message);
328 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
331 const GArray *avatar_data,
332 const gchar *mime_type,
336 EmpathyContact *contact;
338 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
344 DEBUG ("Avatar retrieved for contact %s (%d)",
345 empathy_contact_get_id (contact),
348 empathy_contact_load_avatar_data (contact,
356 tp_contact_factory_request_avatars_cb (TpConnection *connection,
362 DEBUG ("Error requesting avatars: %s", error->message);
367 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
371 EmpathyContact *contact;
372 EmpathyAvatar *avatar;
374 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
379 /* Check if we have an avatar */
380 if (G_STR_EMPTY (token)) {
381 empathy_contact_set_avatar (contact, NULL);
385 /* Check if the avatar changed */
386 avatar = empathy_contact_get_avatar (contact);
387 if (avatar && !tp_strdiff (avatar->token, token)) {
391 /* The avatar changed, search the new one in the cache */
392 if (empathy_contact_load_avatar_cache (contact, token)) {
393 /* Got from cache, use it */
397 /* Avatar is not up-to-date, we have to request it. */
402 EmpathyTpContactFactory *tp_factory;
407 tp_contact_factory_avatar_tokens_foreach (gpointer key,
411 TokensData *data = user_data;
412 const gchar *token = value;
413 guint handle = GPOINTER_TO_UINT (key);
415 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
417 g_array_append_val (data->handles, handle);
422 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
431 DEBUG ("Error getting known avatars tokens: %s", error->message);
435 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
436 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
437 g_hash_table_foreach (tokens,
438 tp_contact_factory_avatar_tokens_foreach,
441 DEBUG ("Got %d tokens, need to request %d avatars",
442 g_hash_table_size (tokens), data.handles->len);
444 /* Request needed avatars */
445 if (data.handles->len > 0) {
446 tp_cli_connection_interface_avatars_call_request_avatars (connection,
449 tp_contact_factory_request_avatars_cb,
454 g_array_free (data.handles, TRUE);
458 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
460 const gchar *new_token,
466 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
467 handle, new_token)) {
468 /* Avatar was cached, nothing to do */
472 DEBUG ("Need to request avatar for token %s", new_token);
474 handles = g_array_new (FALSE, FALSE, sizeof (guint));
475 g_array_append_val (handles, handle);
477 tp_cli_connection_interface_avatars_call_request_avatars (connection,
480 tp_contact_factory_request_avatars_cb,
483 g_array_free (handles, TRUE);
487 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
489 const gchar *channel_type,
493 EmpathyContact *contact;
494 EmpathyCapabilities capabilities;
496 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
501 capabilities = empathy_contact_get_capabilities (contact);
502 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
504 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
505 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
506 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
507 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
508 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
510 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
511 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
515 DEBUG ("Changing capabilities for contact %s (%d) to %d",
516 empathy_contact_get_id (contact),
517 empathy_contact_get_handle (contact),
520 empathy_contact_set_capabilities (contact, capabilities);
524 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
525 const GPtrArray *capabilities,
528 GObject *weak_object)
530 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
534 DEBUG ("Error getting capabilities: %s", error->message);
535 /* FIXME Should set the capabilities of the contacts for which this request
536 * originated to NONE */
540 for (i = 0; i < capabilities->len; i++) {
543 const gchar *channel_type;
547 values = g_ptr_array_index (capabilities, i);
548 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
549 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
550 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
551 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
553 tp_contact_factory_update_capabilities (tp_factory,
562 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
563 const GPtrArray *capabilities,
565 GObject *weak_object)
567 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
570 for (i = 0; i < capabilities->len; i++) {
573 const gchar *channel_type;
577 values = g_ptr_array_index (capabilities, i);
578 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
579 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
580 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
581 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
583 tp_contact_factory_update_capabilities (tp_factory,
592 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
593 const GArray *handles)
595 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
598 g_return_if_fail (priv->ready);
600 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
601 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
602 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
605 tp_contact_factory_get_presence_cb,
607 G_OBJECT (tp_factory));
609 /* FIXME: Sometimes the dbus call timesout because CM takes
610 * too much time to request all aliases from the server,
611 * that's why we increase the timeout here. See fd.o bug #14795 */
612 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
613 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
614 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
617 tp_contact_factory_request_aliases_cb,
619 G_OBJECT (tp_factory));
621 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
624 tp_contact_factory_get_known_avatar_tokens_cb,
626 G_OBJECT (tp_factory));
628 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
631 tp_contact_factory_get_capabilities_cb,
633 G_OBJECT (tp_factory));
637 tp_contact_factory_list_free (gpointer data)
641 g_list_foreach (l, (GFunc) g_object_unref, NULL);
646 tp_contact_factory_request_handles_cb (TpConnection *connection,
647 const GArray *handles,
652 GList *contacts = user_data;
657 DEBUG ("Failed to request handles: %s", error->message);
661 for (l = contacts; l; l = l->next) {
664 handle = g_array_index (handles, guint, i);
665 empathy_contact_set_handle (l->data, handle);
670 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
675 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
682 GList *contacts = user_data;
686 DEBUG ("Failed to inspect handles: %s", error->message);
691 for (l = contacts; l; l = l->next) {
692 empathy_contact_set_id (l->data, *id);
698 tp_contact_factory_disconnect_contact_foreach (gpointer data,
701 EmpathyContact *contact = data;
703 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
704 empathy_contact_set_handle (contact, 0);
708 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
710 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
712 DEBUG ("Connection invalidated");
714 g_object_unref (priv->connection);
715 priv->connection = NULL;
717 g_object_notify (G_OBJECT (tp_factory), "ready");
720 g_list_foreach (priv->contacts,
721 tp_contact_factory_disconnect_contact_foreach,
727 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
733 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
736 DEBUG ("Failed to get self handles: %s", error->message);
740 empathy_contact_set_handle (priv->user, handle);
742 tp_contact_factory_maybe_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
746 tp_contact_factory_maybe_ready (EmpathyTpContactFactory *tp_factory)
748 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
750 GArray *handle_needed;
752 GList *handle_needed_contacts = NULL;
753 GList *id_needed_contacts = NULL;
755 if (empathy_contact_get_handle (priv->user) == 0) {
756 DEBUG ("Connection not ready: still waiting for self handle");
760 DEBUG ("Connection ready");
763 g_object_notify (G_OBJECT (tp_factory), "ready");
765 /* Connect signals */
766 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
767 tp_contact_factory_aliases_changed_cb,
769 G_OBJECT (tp_factory),
771 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
772 tp_contact_factory_avatar_updated_cb,
774 G_OBJECT (tp_factory),
776 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
777 tp_contact_factory_avatar_retrieved_cb,
779 G_OBJECT (tp_factory),
781 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
782 tp_contact_factory_presence_update_cb,
784 G_OBJECT (tp_factory),
786 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
787 tp_contact_factory_capabilities_changed_cb,
789 G_OBJECT (tp_factory),
792 /* Request needed info for all existing contacts */
793 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
794 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
795 for (l = priv->contacts; l; l = l->next) {
796 EmpathyContact *contact;
801 handle = empathy_contact_get_handle (contact);
802 id = empathy_contact_get_id (contact);
804 g_assert (!G_STR_EMPTY (id));
805 g_array_append_val (handle_needed, id);
806 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
807 g_object_ref (contact));
809 if (G_STR_EMPTY (id)) {
810 g_array_append_val (id_needed, handle);
811 id_needed_contacts = g_list_prepend (id_needed_contacts,
812 g_object_ref (contact));
815 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
816 id_needed_contacts = g_list_reverse (id_needed_contacts);
818 tp_cli_connection_call_request_handles (priv->connection,
820 TP_HANDLE_TYPE_CONTACT,
821 (const gchar**) handle_needed->data,
822 tp_contact_factory_request_handles_cb,
823 handle_needed_contacts, tp_contact_factory_list_free,
824 G_OBJECT (tp_factory));
826 tp_cli_connection_call_inspect_handles (priv->connection,
828 TP_HANDLE_TYPE_CONTACT,
830 tp_contact_factory_inspect_handles_cb,
831 id_needed_contacts, tp_contact_factory_list_free,
832 G_OBJECT (tp_factory));
834 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
837 g_array_free (handle_needed, TRUE);
838 g_array_free (id_needed, TRUE);
842 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
844 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
846 /* Get our own handle */
847 tp_cli_connection_call_get_self_handle (priv->connection,
849 tp_contact_factory_got_self_handle_cb,
851 G_OBJECT (tp_factory));
855 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
857 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
858 gboolean connection_ready;
860 if (priv->connection) {
861 /* We already have our connection object */
865 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
866 if (!priv->connection) {
870 /* We got a new connection, wait for it to be ready */
871 g_signal_connect_swapped (priv->connection, "invalidated",
872 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
875 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
876 if (connection_ready) {
877 tp_contact_factory_connection_ready_cb (tp_factory);
879 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
880 G_CALLBACK (tp_contact_factory_connection_ready_cb),
886 tp_contact_factory_status_changed_cb (MissionControl *mc,
887 TpConnectionStatus status,
889 TpConnectionStatusReason reason,
890 const gchar *unique_name,
891 EmpathyTpContactFactory *tp_factory)
893 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
896 account = mc_account_lookup (unique_name);
897 if (account && empathy_account_equal (account, priv->account)) {
898 tp_contact_factory_status_updated (tp_factory);
900 g_object_unref (account);
904 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
905 EmpathyContact *contact)
907 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
909 g_object_weak_ref (G_OBJECT (contact),
910 tp_contact_factory_weak_notify,
912 priv->contacts = g_list_prepend (priv->contacts, contact);
914 DEBUG ("Contact added: %s (%d)",
915 empathy_contact_get_id (contact),
916 empathy_contact_get_handle (contact));
920 tp_contact_factory_hold_handles_cb (TpConnection *connection,
926 DEBUG ("Failed to hold handles: %s", error->message);
931 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
933 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
935 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
937 return g_object_ref (priv->user);
941 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
944 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
945 EmpathyContact *contact;
947 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
948 g_return_val_if_fail (id != NULL, NULL);
950 /* Check if the contact already exists */
951 contact = tp_contact_factory_find_by_id (tp_factory, id);
953 return g_object_ref (contact);
956 /* Create new contact */
957 contact = g_object_new (EMPATHY_TYPE_CONTACT,
958 "account", priv->account,
961 tp_contact_factory_add_contact (tp_factory, contact);
964 const gchar *contact_ids[] = {id, NULL};
967 contacts = g_list_prepend (NULL, g_object_ref (contact));
968 tp_cli_connection_call_request_handles (priv->connection,
970 TP_HANDLE_TYPE_CONTACT,
972 tp_contact_factory_request_handles_cb,
973 contacts, tp_contact_factory_list_free,
974 G_OBJECT (tp_factory));
981 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
984 EmpathyContact *contact;
988 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
990 handles = g_array_new (FALSE, FALSE, sizeof (guint));
991 g_array_append_val (handles, handle);
993 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
994 g_array_free (handles, TRUE);
996 contact = contacts ? contacts->data : NULL;
997 g_list_free (contacts);
1003 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1004 const GArray *handles)
1006 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1007 GList *contacts = NULL;
1008 GArray *new_handles;
1009 GList *new_contacts = NULL;
1012 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1013 g_return_val_if_fail (handles != NULL, NULL);
1015 /* Search all contacts we already have */
1016 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1017 for (i = 0; i < handles->len; i++) {
1018 EmpathyContact *contact;
1021 handle = g_array_index (handles, guint, i);
1026 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1028 contacts = g_list_prepend (contacts, g_object_ref (contact));
1030 g_array_append_val (new_handles, handle);
1034 if (new_handles->len == 0) {
1035 g_array_free (new_handles, TRUE);
1039 /* Create new contacts */
1040 for (i = 0; i < new_handles->len; i++) {
1041 EmpathyContact *contact;
1044 handle = g_array_index (new_handles, guint, i);
1046 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1047 "account", priv->account,
1050 tp_contact_factory_add_contact (tp_factory, contact);
1051 contacts = g_list_prepend (contacts, contact);
1052 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1054 new_contacts = g_list_reverse (new_contacts);
1057 /* Get the IDs of all new handles */
1058 tp_cli_connection_call_inspect_handles (priv->connection,
1060 TP_HANDLE_TYPE_CONTACT,
1062 tp_contact_factory_inspect_handles_cb,
1063 new_contacts, tp_contact_factory_list_free,
1064 G_OBJECT (tp_factory));
1066 /* Hold all new handles. */
1067 /* FIXME: Should be unholded when removed from the factory */
1068 tp_cli_connection_call_hold_handles (priv->connection,
1070 TP_HANDLE_TYPE_CONTACT,
1072 tp_contact_factory_hold_handles_cb,
1074 G_OBJECT (tp_factory));
1076 tp_contact_factory_request_everything (tp_factory, new_handles);
1079 g_array_free (new_handles, TRUE);
1085 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1086 EmpathyContact *contact,
1089 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1090 GHashTable *new_alias;
1093 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1094 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1095 g_return_if_fail (priv->ready);
1096 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1099 handle = empathy_contact_get_handle (contact);
1101 DEBUG ("Setting alias for contact %s (%d) to %s",
1102 empathy_contact_get_id (contact),
1105 new_alias = g_hash_table_new_full (g_direct_hash,
1110 g_hash_table_insert (new_alias,
1111 GUINT_TO_POINTER (handle),
1114 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1117 tp_contact_factory_set_aliases_cb,
1119 G_OBJECT (tp_factory));
1121 g_hash_table_destroy (new_alias);
1125 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1128 const gchar *mime_type)
1130 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1132 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1133 g_return_if_fail (priv->ready);
1135 if (data && size > 0 && size < G_MAXUINT) {
1138 avatar.data = (gchar*) data;
1141 DEBUG ("Setting avatar on account %s",
1142 mc_account_get_unique_name (priv->account));
1144 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1148 tp_contact_factory_set_avatar_cb,
1150 G_OBJECT (tp_factory));
1152 DEBUG ("Clearing avatar on account %s",
1153 mc_account_get_unique_name (priv->account));
1155 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1157 tp_contact_factory_clear_avatar_cb,
1159 G_OBJECT (tp_factory));
1164 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1166 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1168 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1174 tp_contact_factory_get_property (GObject *object,
1179 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1183 g_value_set_object (value, priv->account);
1186 g_value_set_boolean (value, priv->ready);
1189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1195 tp_contact_factory_set_property (GObject *object,
1197 const GValue *value,
1200 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1204 priv->account = g_object_ref (g_value_get_object (value));
1207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1213 tp_contact_factory_finalize (GObject *object)
1215 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1218 DEBUG ("Finalized: %p (%s)", object,
1219 mc_account_get_normalized_name (priv->account));
1221 empathy_disconnect_account_status_changed (priv->token);
1223 for (l = priv->contacts; l; l = l->next) {
1224 g_object_weak_unref (G_OBJECT (l->data),
1225 tp_contact_factory_weak_notify,
1229 g_list_free (priv->contacts);
1230 g_object_unref (priv->mc);
1231 g_object_unref (priv->account);
1232 g_object_unref (priv->user);
1234 if (priv->connection) {
1235 g_signal_handlers_disconnect_by_func (priv->connection,
1236 tp_contact_factory_connection_invalidated_cb,
1238 g_object_unref (priv->connection);
1241 g_strfreev (priv->avatar_mime_types);
1243 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1247 tp_contact_factory_constructor (GType type,
1249 GObjectConstructParam *props)
1251 GObject *tp_factory;
1252 EmpathyTpContactFactoryPriv *priv;
1254 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1255 priv = GET_PRIV (tp_factory);
1257 priv->ready = FALSE;
1258 priv->user = empathy_contact_new (priv->account);
1259 empathy_contact_set_is_user (priv->user, TRUE);
1260 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1261 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1267 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1269 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1271 object_class->finalize = tp_contact_factory_finalize;
1272 object_class->constructor = tp_contact_factory_constructor;
1273 object_class->get_property = tp_contact_factory_get_property;
1274 object_class->set_property = tp_contact_factory_set_property;
1276 g_object_class_install_property (object_class,
1278 g_param_spec_object ("account",
1279 "Factory's Account",
1280 "The account associated with the factory",
1283 G_PARAM_CONSTRUCT_ONLY));
1284 g_object_class_install_property (object_class,
1286 g_param_spec_boolean ("ready",
1287 "Wheter the factor is ready",
1288 "Is the factory ready",
1292 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1296 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1298 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1299 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1301 tp_factory->priv = priv;
1302 priv->mc = empathy_mission_control_new ();
1303 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1304 G_CALLBACK (tp_contact_factory_status_changed_cb),
1308 EmpathyTpContactFactory *
1309 empathy_tp_contact_factory_new (McAccount *account)
1311 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,