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;
46 } EmpathyTpContactFactoryPriv;
48 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
56 static EmpathyContact *
57 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
60 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
63 for (l = priv->contacts; l; l = l->next) {
64 if (empathy_contact_get_handle (l->data) == handle) {
72 static EmpathyContact *
73 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
76 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
79 for (l = priv->contacts; l; l = l->next) {
80 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
89 tp_contact_factory_weak_notify (gpointer data,
90 GObject *where_the_object_was)
92 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
94 DEBUG ("Remove finalized contact %p", where_the_object_was);
96 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
100 tp_contact_factory_presences_table_foreach (const gchar *state_str,
101 GHashTable *presences_table,
102 EmpathyContact *contact)
104 const GValue *message;
105 const gchar *message_str = NULL;
107 empathy_contact_set_presence (contact,
108 empathy_presence_from_str (state_str));
110 message = g_hash_table_lookup (presences_table, "message");
112 message_str = g_value_get_string (message);
115 if (!G_STR_EMPTY (message_str)) {
116 empathy_contact_set_presence_message (contact, message_str);
118 empathy_contact_set_presence_message (contact, NULL);
123 tp_contact_factory_parse_presence_foreach (guint handle,
124 GValueArray *presence_struct,
125 EmpathyTpContactFactory *tp_factory)
127 GHashTable *presences_table;
128 EmpathyContact *contact;
130 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
135 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
137 g_hash_table_foreach (presences_table,
138 (GHFunc) tp_contact_factory_presences_table_foreach,
141 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
142 empathy_contact_get_id (contact),
144 empathy_contact_get_presence_message (contact),
145 empathy_contact_get_presence (contact));
149 tp_contact_factory_get_presence_cb (TpConnection *connection,
150 GHashTable *handle_table,
156 DEBUG ("Error getting presence: %s", error->message);
157 if (error->domain == TP_DBUS_ERRORS &&
158 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
159 guint *handles = user_data;
161 /* We have no presence iface, set default presence
163 while (*handles != 0) {
164 EmpathyContact *contact;
166 contact = tp_contact_factory_find_by_handle (
167 (EmpathyTpContactFactory*) tp_factory,
170 empathy_contact_set_presence (contact,
171 MC_PRESENCE_AVAILABLE);
181 g_hash_table_foreach (handle_table,
182 (GHFunc) tp_contact_factory_parse_presence_foreach,
183 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
187 tp_contact_factory_presence_update_cb (TpConnection *connection,
188 GHashTable *handle_table,
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_set_aliases_cb (TpConnection *connection,
204 DEBUG ("Error setting alias: %s", error->message);
209 tp_contact_factory_request_aliases_cb (TpConnection *connection,
210 const gchar **contact_names,
215 guint *handles = user_data;
220 DEBUG ("Error requesting aliases: %s", error->message);
222 /* If we failed to get alias set it to NULL, like that if
223 * someone is waiting for the name to be ready it won't wait
225 while (*handles != 0) {
226 EmpathyContact *contact;
228 contact = tp_contact_factory_find_by_handle (
229 (EmpathyTpContactFactory*) tp_factory,
232 empathy_contact_set_name (contact, NULL);
240 for (name = contact_names; *name; name++) {
241 EmpathyContact *contact;
243 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
249 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
250 empathy_contact_get_id (contact),
251 empathy_contact_get_handle (contact),
254 empathy_contact_set_name (contact, *name);
261 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
262 const GPtrArray *renamed_handlers,
264 GObject *weak_object)
266 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
269 for (i = 0; renamed_handlers->len > i; i++) {
272 GValueArray *renamed_struct;
273 EmpathyContact *contact;
275 renamed_struct = g_ptr_array_index (renamed_handlers, i);
276 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
277 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
278 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
281 /* We don't know this contact, skip */
285 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
286 empathy_contact_get_id (contact),
289 empathy_contact_set_name (contact, alias);
294 tp_contact_factory_set_avatar_cb (TpConnection *connection,
301 DEBUG ("Error setting avatar: %s", error->message);
306 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
312 DEBUG ("Error clearing avatar: %s", error->message);
317 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
320 const GArray *avatar_data,
321 const gchar *mime_type,
325 EmpathyContact *contact;
327 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
333 DEBUG ("Avatar retrieved for contact %s (%d)",
334 empathy_contact_get_id (contact),
337 empathy_contact_load_avatar_data (contact,
345 tp_contact_factory_request_avatars_cb (TpConnection *connection,
351 DEBUG ("Error requesting avatars: %s", error->message);
356 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
360 EmpathyContact *contact;
361 EmpathyAvatar *avatar;
363 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
368 /* Check if we have an avatar */
369 if (G_STR_EMPTY (token)) {
370 empathy_contact_set_avatar (contact, NULL);
374 /* Check if the avatar changed */
375 avatar = empathy_contact_get_avatar (contact);
376 if (avatar && !tp_strdiff (avatar->token, token)) {
380 /* The avatar changed, search the new one in the cache */
381 if (empathy_contact_load_avatar_cache (contact, token)) {
382 /* Got from cache, use it */
386 /* Avatar is not up-to-date, we have to request it. */
391 EmpathyTpContactFactory *tp_factory;
396 tp_contact_factory_avatar_tokens_foreach (gpointer key,
400 TokensData *data = user_data;
401 const gchar *token = value;
402 guint handle = GPOINTER_TO_UINT (key);
404 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
406 g_array_append_val (data->handles, handle);
411 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
420 DEBUG ("Error getting known avatars tokens: %s", error->message);
424 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
425 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
426 g_hash_table_foreach (tokens,
427 tp_contact_factory_avatar_tokens_foreach,
430 DEBUG ("Got %d tokens, need to request %d avatars",
431 g_hash_table_size (tokens), data.handles->len);
433 /* Request needed avatars */
434 if (data.handles->len > 0) {
435 tp_cli_connection_interface_avatars_call_request_avatars (connection,
438 tp_contact_factory_request_avatars_cb,
443 g_array_free (data.handles, TRUE);
447 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
449 const gchar *new_token,
455 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
456 handle, new_token)) {
457 /* Avatar was cached, nothing to do */
461 DEBUG ("Need to request avatar for token %s", new_token);
463 handles = g_array_new (FALSE, FALSE, sizeof (guint));
464 g_array_append_val (handles, handle);
466 tp_cli_connection_interface_avatars_call_request_avatars (connection,
469 tp_contact_factory_request_avatars_cb,
472 g_array_free (handles, TRUE);
476 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
478 const gchar *channel_type,
482 EmpathyContact *contact;
483 EmpathyCapabilities capabilities;
485 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
490 capabilities = empathy_contact_get_capabilities (contact);
491 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
493 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
494 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
495 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
496 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
497 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
499 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
500 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
504 DEBUG ("Changing capabilities for contact %s (%d) to %d",
505 empathy_contact_get_id (contact),
506 empathy_contact_get_handle (contact),
509 empathy_contact_set_capabilities (contact, capabilities);
513 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
514 const GPtrArray *capabilities,
517 GObject *weak_object)
519 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
523 DEBUG ("Error getting capabilities: %s", error->message);
524 /* FIXME Should set the capabilities of the contacts for which this request
525 * originated to NONE */
529 for (i = 0; i < capabilities->len; i++) {
532 const gchar *channel_type;
536 values = g_ptr_array_index (capabilities, i);
537 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
538 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
539 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
540 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
542 tp_contact_factory_update_capabilities (tp_factory,
551 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
552 const GPtrArray *capabilities,
554 GObject *weak_object)
556 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
559 for (i = 0; i < capabilities->len; i++) {
562 const gchar *channel_type;
566 values = g_ptr_array_index (capabilities, i);
567 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
568 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
569 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
570 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
572 tp_contact_factory_update_capabilities (tp_factory,
581 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
582 const GArray *handles)
584 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
587 g_return_if_fail (priv->ready);
589 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
590 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
591 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
594 tp_contact_factory_get_presence_cb,
596 G_OBJECT (tp_factory));
598 /* FIXME: Sometimes the dbus call timesout because CM takes
599 * too much time to request all aliases from the server,
600 * that's why we increase the timeout here. See fd.o bug #14795 */
601 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
602 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
603 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
606 tp_contact_factory_request_aliases_cb,
608 G_OBJECT (tp_factory));
610 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
613 tp_contact_factory_get_known_avatar_tokens_cb,
615 G_OBJECT (tp_factory));
617 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
620 tp_contact_factory_get_capabilities_cb,
622 G_OBJECT (tp_factory));
626 tp_contact_factory_list_free (gpointer data)
630 g_list_foreach (l, (GFunc) g_object_unref, NULL);
635 tp_contact_factory_request_handles_cb (TpConnection *connection,
636 const GArray *handles,
641 GList *contacts = user_data;
646 DEBUG ("Failed to request handles: %s", error->message);
650 for (l = contacts; l; l = l->next) {
653 handle = g_array_index (handles, guint, i);
654 empathy_contact_set_handle (l->data, handle);
659 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
664 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
671 GList *contacts = user_data;
675 DEBUG ("Failed to inspect handles: %s", error->message);
680 for (l = contacts; l; l = l->next) {
681 empathy_contact_set_id (l->data, *id);
687 tp_contact_factory_disconnect_contact_foreach (gpointer data,
690 EmpathyContact *contact = data;
692 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
693 empathy_contact_set_handle (contact, 0);
697 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
699 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
701 DEBUG ("Connection invalidated");
703 g_object_unref (priv->connection);
704 priv->connection = NULL;
706 g_object_notify (G_OBJECT (tp_factory), "ready");
709 g_list_foreach (priv->contacts,
710 tp_contact_factory_disconnect_contact_foreach,
716 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
722 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
724 GArray *handle_needed;
726 GList *handle_needed_contacts = NULL;
727 GList *id_needed_contacts = NULL;
730 DEBUG ("Failed to get self handles: %s", error->message);
734 DEBUG ("Connection ready");
736 empathy_contact_set_handle (priv->user, handle);
738 g_object_notify (tp_factory, "ready");
740 /* Connect signals */
741 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
742 tp_contact_factory_aliases_changed_cb,
744 G_OBJECT (tp_factory),
746 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
747 tp_contact_factory_avatar_updated_cb,
749 G_OBJECT (tp_factory),
751 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
752 tp_contact_factory_avatar_retrieved_cb,
754 G_OBJECT (tp_factory),
756 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
757 tp_contact_factory_presence_update_cb,
759 G_OBJECT (tp_factory),
761 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
762 tp_contact_factory_capabilities_changed_cb,
764 G_OBJECT (tp_factory),
767 /* Request needed info for all existing contacts */
768 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
769 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
770 for (l = priv->contacts; l; l = l->next) {
771 EmpathyContact *contact;
776 handle = empathy_contact_get_handle (contact);
777 id = empathy_contact_get_id (contact);
779 g_assert (!G_STR_EMPTY (id));
780 g_array_append_val (handle_needed, id);
781 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
782 g_object_ref (contact));
784 if (G_STR_EMPTY (id)) {
785 g_array_append_val (id_needed, handle);
786 id_needed_contacts = g_list_prepend (id_needed_contacts,
787 g_object_ref (contact));
790 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
791 id_needed_contacts = g_list_reverse (id_needed_contacts);
793 tp_cli_connection_call_request_handles (priv->connection,
795 TP_HANDLE_TYPE_CONTACT,
796 (const gchar**) handle_needed->data,
797 tp_contact_factory_request_handles_cb,
798 handle_needed_contacts, tp_contact_factory_list_free,
799 G_OBJECT (tp_factory));
801 tp_cli_connection_call_inspect_handles (priv->connection,
803 TP_HANDLE_TYPE_CONTACT,
805 tp_contact_factory_inspect_handles_cb,
806 id_needed_contacts, tp_contact_factory_list_free,
807 G_OBJECT (tp_factory));
809 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
812 g_array_free (handle_needed, TRUE);
813 g_array_free (id_needed, TRUE);
817 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
819 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
821 /* Get our own handle */
822 tp_cli_connection_call_get_self_handle (priv->connection,
824 tp_contact_factory_got_self_handle_cb,
826 G_OBJECT (tp_factory));
830 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
832 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
833 gboolean connection_ready;
835 if (priv->connection) {
836 /* We already have our connection object */
840 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
841 if (!priv->connection) {
845 /* We got a new connection, wait for it to be ready */
846 g_signal_connect_swapped (priv->connection, "invalidated",
847 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
850 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
851 if (connection_ready) {
852 tp_contact_factory_connection_ready_cb (tp_factory);
854 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
855 G_CALLBACK (tp_contact_factory_connection_ready_cb),
861 tp_contact_factory_status_changed_cb (MissionControl *mc,
862 TpConnectionStatus status,
864 TpConnectionStatusReason reason,
865 const gchar *unique_name,
866 EmpathyTpContactFactory *tp_factory)
868 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
871 account = mc_account_lookup (unique_name);
872 if (account && empathy_account_equal (account, priv->account)) {
873 tp_contact_factory_status_updated (tp_factory);
875 g_object_unref (account);
879 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
880 EmpathyContact *contact)
882 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
884 g_object_weak_ref (G_OBJECT (contact),
885 tp_contact_factory_weak_notify,
887 priv->contacts = g_list_prepend (priv->contacts, contact);
889 DEBUG ("Contact added: %s (%d)",
890 empathy_contact_get_id (contact),
891 empathy_contact_get_handle (contact));
895 tp_contact_factory_hold_handles_cb (TpConnection *connection,
901 DEBUG ("Failed to hold handles: %s", error->message);
906 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
908 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
910 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
912 return g_object_ref (priv->user);
916 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
919 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
920 EmpathyContact *contact;
922 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
923 g_return_val_if_fail (id != NULL, NULL);
925 /* Check if the contact already exists */
926 contact = tp_contact_factory_find_by_id (tp_factory, id);
928 return g_object_ref (contact);
931 /* Create new contact */
932 contact = g_object_new (EMPATHY_TYPE_CONTACT,
933 "account", priv->account,
936 tp_contact_factory_add_contact (tp_factory, contact);
939 const gchar *contact_ids[] = {id, NULL};
942 contacts = g_list_prepend (NULL, g_object_ref (contact));
943 tp_cli_connection_call_request_handles (priv->connection,
945 TP_HANDLE_TYPE_CONTACT,
947 tp_contact_factory_request_handles_cb,
948 contacts, tp_contact_factory_list_free,
949 G_OBJECT (tp_factory));
956 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
959 EmpathyContact *contact;
963 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
965 handles = g_array_new (FALSE, FALSE, sizeof (guint));
966 g_array_append_val (handles, handle);
968 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
969 g_array_free (handles, TRUE);
971 contact = contacts ? contacts->data : NULL;
972 g_list_free (contacts);
978 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
979 const GArray *handles)
981 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
982 GList *contacts = NULL;
984 GList *new_contacts = NULL;
987 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
988 g_return_val_if_fail (handles != NULL, NULL);
990 /* Search all contacts we already have */
991 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
992 for (i = 0; i < handles->len; i++) {
993 EmpathyContact *contact;
996 handle = g_array_index (handles, guint, i);
1001 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1003 contacts = g_list_prepend (contacts, g_object_ref (contact));
1005 g_array_append_val (new_handles, handle);
1009 if (new_handles->len == 0) {
1010 g_array_free (new_handles, TRUE);
1014 /* Create new contacts */
1015 for (i = 0; i < new_handles->len; i++) {
1016 EmpathyContact *contact;
1019 handle = g_array_index (new_handles, guint, i);
1021 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1022 "account", priv->account,
1025 tp_contact_factory_add_contact (tp_factory, contact);
1026 contacts = g_list_prepend (contacts, contact);
1027 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1029 new_contacts = g_list_reverse (new_contacts);
1032 /* Get the IDs of all new handles */
1033 tp_cli_connection_call_inspect_handles (priv->connection,
1035 TP_HANDLE_TYPE_CONTACT,
1037 tp_contact_factory_inspect_handles_cb,
1038 new_contacts, tp_contact_factory_list_free,
1039 G_OBJECT (tp_factory));
1041 /* Hold all new handles. */
1042 /* FIXME: Should be unholded when removed from the factory */
1043 tp_cli_connection_call_hold_handles (priv->connection,
1045 TP_HANDLE_TYPE_CONTACT,
1047 tp_contact_factory_hold_handles_cb,
1049 G_OBJECT (tp_factory));
1051 tp_contact_factory_request_everything (tp_factory, new_handles);
1058 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1059 EmpathyContact *contact,
1062 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1063 GHashTable *new_alias;
1066 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1067 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1068 g_return_if_fail (priv->ready);
1069 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1072 handle = empathy_contact_get_handle (contact);
1074 DEBUG ("Setting alias for contact %s (%d) to %s",
1075 empathy_contact_get_id (contact),
1078 new_alias = g_hash_table_new_full (g_direct_hash,
1083 g_hash_table_insert (new_alias,
1084 GUINT_TO_POINTER (handle),
1087 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1090 tp_contact_factory_set_aliases_cb,
1092 G_OBJECT (tp_factory));
1094 g_hash_table_destroy (new_alias);
1098 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1101 const gchar *mime_type)
1103 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1105 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1106 g_return_if_fail (priv->ready);
1108 if (data && size > 0 && size < G_MAXUINT) {
1111 avatar.data = (gchar*) data;
1114 DEBUG ("Setting avatar on account %s",
1115 mc_account_get_unique_name (priv->account));
1117 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1121 tp_contact_factory_set_avatar_cb,
1123 G_OBJECT (tp_factory));
1125 DEBUG ("Clearing avatar on account %s",
1126 mc_account_get_unique_name (priv->account));
1128 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1130 tp_contact_factory_clear_avatar_cb,
1132 G_OBJECT (tp_factory));
1137 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1139 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1141 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1147 tp_contact_factory_get_property (GObject *object,
1152 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1156 g_value_set_object (value, priv->account);
1159 g_value_set_boolean (value, priv->ready);
1162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1168 tp_contact_factory_set_property (GObject *object,
1170 const GValue *value,
1173 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1177 priv->account = g_object_ref (g_value_get_object (value));
1180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1186 tp_contact_factory_finalize (GObject *object)
1188 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1191 DEBUG ("Finalized: %p (%s)", object,
1192 mc_account_get_normalized_name (priv->account));
1194 empathy_disconnect_account_status_changed (priv->token);
1196 for (l = priv->contacts; l; l = l->next) {
1197 g_object_weak_unref (G_OBJECT (l->data),
1198 tp_contact_factory_weak_notify,
1202 g_list_free (priv->contacts);
1203 g_object_unref (priv->mc);
1204 g_object_unref (priv->account);
1205 g_object_unref (priv->user);
1207 if (priv->connection) {
1208 g_signal_handlers_disconnect_by_func (priv->connection,
1209 tp_contact_factory_connection_invalidated_cb,
1211 g_object_unref (priv->connection);
1214 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1218 tp_contact_factory_constructor (GType type,
1220 GObjectConstructParam *props)
1222 GObject *tp_factory;
1223 EmpathyTpContactFactoryPriv *priv;
1225 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1226 priv = GET_PRIV (tp_factory);
1228 priv->ready = FALSE;
1229 priv->user = empathy_contact_new (priv->account);
1230 empathy_contact_set_is_user (priv->user, TRUE);
1231 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1232 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1238 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1240 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1242 object_class->finalize = tp_contact_factory_finalize;
1243 object_class->constructor = tp_contact_factory_constructor;
1244 object_class->get_property = tp_contact_factory_get_property;
1245 object_class->set_property = tp_contact_factory_set_property;
1247 g_object_class_install_property (object_class,
1249 g_param_spec_object ("account",
1250 "Factory's Account",
1251 "The account associated with the factory",
1254 G_PARAM_CONSTRUCT_ONLY));
1255 g_object_class_install_property (object_class,
1257 g_param_spec_boolean ("ready",
1258 "Wheter the factor is ready",
1259 "Is the factory ready",
1263 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1267 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1269 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1270 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1272 tp_factory->priv = priv;
1273 priv->mc = empathy_mission_control_new ();
1274 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1275 G_CALLBACK (tp_contact_factory_status_changed_cb),
1279 EmpathyTpContactFactory *
1280 empathy_tp_contact_factory_new (McAccount *account)
1282 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,