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"
32 #include "empathy-debug.h"
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
35 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv))
37 #define DEBUG_DOMAIN "TpContactFactory"
39 struct _EmpathyTpContactFactoryPriv {
42 TpConnection *connection;
49 static void empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass);
50 static void empathy_tp_contact_factory_init (EmpathyTpContactFactory *factory);
52 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
60 static EmpathyContact *
61 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
64 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
67 for (l = priv->contacts; l; l = l->next) {
68 if (empathy_contact_get_handle (l->data) == handle) {
76 static EmpathyContact *
77 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
80 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
83 for (l = priv->contacts; l; l = l->next) {
84 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
93 tp_contact_factory_weak_notify (gpointer data,
94 GObject *where_the_object_was)
96 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
98 empathy_debug (DEBUG_DOMAIN, "Remove finalized contact %p",
99 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 empathy_debug (DEBUG_DOMAIN, "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 empathy_debug (DEBUG_DOMAIN, "Error getting presence: %s",
159 if (error->domain == TP_DBUS_ERRORS &&
160 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
161 guint *handles = user_data;
163 /* We have no presence iface, set default presence
165 while (*handles != 0) {
166 EmpathyContact *contact;
168 contact = tp_contact_factory_find_by_handle (
169 (EmpathyTpContactFactory*) tp_factory,
172 empathy_contact_set_presence (contact,
173 MC_PRESENCE_AVAILABLE);
183 g_hash_table_foreach (handle_table,
184 (GHFunc) tp_contact_factory_parse_presence_foreach,
185 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
189 tp_contact_factory_presence_update_cb (TpConnection *connection,
190 GHashTable *handle_table,
194 g_hash_table_foreach (handle_table,
195 (GHFunc) tp_contact_factory_parse_presence_foreach,
196 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
200 tp_contact_factory_set_aliases_cb (TpConnection *connection,
206 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
212 tp_contact_factory_request_aliases_cb (TpConnection *connection,
213 const gchar **contact_names,
218 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
219 guint *handles = user_data;
224 GArray handles_array;
227 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
230 /* FIXME: Sometimes the dbus call timesout because CM takes
231 * too much time to request all aliases from the server,
232 * that's why we retry. */
233 while (handles[size] != 0) {
236 handles = g_memdup (handles, size * sizeof (guint));
237 handles_array.len = size;
238 handles_array.data = (gchar*) handles;
240 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
243 tp_contact_factory_request_aliases_cb,
245 G_OBJECT (tp_factory));
250 for (name = contact_names; *name; name++) {
251 EmpathyContact *contact;
253 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
259 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
260 empathy_contact_get_id (contact),
261 empathy_contact_get_handle (contact),
264 empathy_contact_set_name (contact, *name);
271 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
272 const GPtrArray *renamed_handlers,
274 GObject *weak_object)
276 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
279 for (i = 0; renamed_handlers->len > i; i++) {
282 GValueArray *renamed_struct;
283 EmpathyContact *contact;
285 renamed_struct = g_ptr_array_index (renamed_handlers, i);
286 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
287 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
288 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
291 /* We don't know this contact, skip */
295 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
296 empathy_contact_get_id (contact),
299 empathy_contact_set_name (contact, alias);
304 tp_contact_factory_set_avatar_cb (TpConnection *connection,
311 empathy_debug (DEBUG_DOMAIN, "Error setting avatar: %s",
317 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
323 empathy_debug (DEBUG_DOMAIN, "Error clearing avatar: %s",
329 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
332 const GArray *avatar_data,
333 const gchar *mime_type,
337 EmpathyContact *contact;
338 EmpathyAvatar *avatar;
340 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
346 empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
347 empathy_contact_get_id (contact),
350 avatar = empathy_avatar_new (avatar_data->data,
355 empathy_contact_set_avatar (contact, avatar);
356 empathy_avatar_unref (avatar);
360 tp_contact_factory_request_avatars_cb (TpConnection *connection,
366 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
372 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
376 EmpathyContact *contact;
377 EmpathyAvatar *avatar;
379 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
384 /* Check if we have an avatar */
385 if (G_STR_EMPTY (token)) {
386 empathy_contact_set_avatar (contact, NULL);
390 /* Check if the avatar changed */
391 avatar = empathy_contact_get_avatar (contact);
392 if (avatar && !tp_strdiff (avatar->token, token)) {
396 /* The avatar changed, search the new one in the cache */
397 avatar = empathy_avatar_new_from_cache (token);
399 /* Got from cache, use it */
400 empathy_contact_set_avatar (contact, avatar);
401 empathy_avatar_unref (avatar);
405 /* Avatar is not up-to-date, we have to request it. */
410 EmpathyTpContactFactory *tp_factory;
415 tp_contact_factory_avatar_tokens_foreach (gpointer key,
419 TokensData *data = user_data;
420 const gchar *token = value;
421 guint handle = GPOINTER_TO_UINT (key);
423 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
425 g_array_append_val (data->handles, handle);
430 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
439 empathy_debug (DEBUG_DOMAIN,
440 "Error getting known avatars tokens: %s",
445 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
446 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
447 g_hash_table_foreach (tokens,
448 tp_contact_factory_avatar_tokens_foreach,
451 empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
452 g_hash_table_size (tokens),
455 /* Request needed avatars */
456 if (data.handles->len > 0) {
457 tp_cli_connection_interface_avatars_call_request_avatars (connection,
460 tp_contact_factory_request_avatars_cb,
465 g_array_free (data.handles, TRUE);
469 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
471 const gchar *new_token,
477 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
478 handle, new_token)) {
479 /* Avatar was cached, nothing to do */
483 empathy_debug (DEBUG_DOMAIN, "Need to request avatar for token %s",
486 handles = g_array_new (FALSE, FALSE, sizeof (guint));
487 g_array_append_val (handles, handle);
489 tp_cli_connection_interface_avatars_call_request_avatars (connection,
492 tp_contact_factory_request_avatars_cb,
495 g_array_free (handles, TRUE);
499 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
501 const gchar *channel_type,
505 EmpathyContact *contact;
506 EmpathyCapabilities capabilities;
508 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
513 capabilities = empathy_contact_get_capabilities (contact);
514 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
516 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
517 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
518 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
519 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
520 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
522 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
523 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
527 empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
528 empathy_contact_get_id (contact),
529 empathy_contact_get_handle (contact),
532 empathy_contact_set_capabilities (contact, capabilities);
536 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
537 const GPtrArray *capabilities,
540 GObject *weak_object)
542 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
546 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
548 /* FIXME Should set the capabilities of the contacts for which this request
549 * originated to NONE */
553 for (i = 0; i < capabilities->len; i++) {
556 const gchar *channel_type;
560 values = g_ptr_array_index (capabilities, i);
561 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
562 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
563 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
564 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
566 tp_contact_factory_update_capabilities (tp_factory,
575 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
576 const GPtrArray *capabilities,
578 GObject *weak_object)
580 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
583 for (i = 0; i < capabilities->len; i++) {
586 const gchar *channel_type;
590 values = g_ptr_array_index (capabilities, i);
591 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
592 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
593 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
594 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
596 tp_contact_factory_update_capabilities (tp_factory,
605 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
606 const GArray *handles)
608 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
615 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
616 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
617 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
620 tp_contact_factory_get_presence_cb,
622 G_OBJECT (tp_factory));
624 dup_handles = g_memdup (handles->data, handles->len * sizeof (guint));
625 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
628 tp_contact_factory_request_aliases_cb,
630 G_OBJECT (tp_factory));
632 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
635 tp_contact_factory_get_known_avatar_tokens_cb,
637 G_OBJECT (tp_factory));
639 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
642 tp_contact_factory_get_capabilities_cb,
644 G_OBJECT (tp_factory));
648 tp_contact_factory_list_free (gpointer data)
652 g_list_foreach (l, (GFunc) g_object_unref, NULL);
657 tp_contact_factory_request_handles_cb (TpConnection *connection,
658 const GArray *handles,
663 GList *contacts = user_data;
668 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
673 for (l = contacts; l; l = l->next) {
676 handle = g_array_index (handles, guint, i);
677 empathy_contact_set_handle (l->data, handle);
682 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
687 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
694 GList *contacts = user_data;
698 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
704 for (l = contacts; l; l = l->next) {
705 empathy_contact_set_id (l->data, *id);
711 tp_contact_factory_disconnect_contact_foreach (gpointer data,
714 EmpathyContact *contact = data;
716 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
717 empathy_contact_set_handle (contact, 0);
721 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
723 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
725 empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
727 g_object_unref (priv->connection);
728 priv->connection = NULL;
730 g_object_notify (G_OBJECT (tp_factory), "ready");
733 g_list_foreach (priv->contacts,
734 tp_contact_factory_disconnect_contact_foreach,
740 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
746 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
748 GArray *handle_needed;
750 GList *handle_needed_contacts = NULL;
751 GList *id_needed_contacts = NULL;
754 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
759 empathy_debug (DEBUG_DOMAIN, "Connection ready");
761 empathy_contact_set_handle (priv->user, handle);
763 g_object_notify (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_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
828 tp_cli_connection_call_inspect_handles (priv->connection,
830 TP_HANDLE_TYPE_CONTACT,
832 tp_contact_factory_inspect_handles_cb,
833 id_needed_contacts, tp_contact_factory_list_free,
834 G_OBJECT (tp_factory));
836 g_array_free (handle_needed, TRUE);
837 g_array_free (id_needed, TRUE);
841 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
843 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
845 /* Get our own handle */
846 tp_cli_connection_call_get_self_handle (priv->connection,
848 tp_contact_factory_got_self_handle_cb,
850 G_OBJECT (tp_factory));
854 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
856 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
858 gboolean connection_ready;
860 if (priv->connection) {
861 /* We already have our connection object */
865 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
870 /* We got a new connection, wait for it to be ready */
871 priv->connection = tp_conn_dup_connection (tp_conn);
872 g_object_unref (tp_conn);
874 g_signal_connect_swapped (priv->connection, "invalidated",
875 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
878 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
879 if (connection_ready) {
880 tp_contact_factory_connection_ready_cb (tp_factory);
882 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
883 G_CALLBACK (tp_contact_factory_connection_ready_cb),
889 tp_contact_factory_status_changed_cb (MissionControl *mc,
890 TpConnectionStatus status,
892 TpConnectionStatusReason reason,
893 const gchar *unique_name,
894 EmpathyTpContactFactory *tp_factory)
896 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
899 account = mc_account_lookup (unique_name);
900 if (account && empathy_account_equal (account, priv->account)) {
901 tp_contact_factory_status_updated (tp_factory);
903 g_object_unref (account);
907 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
908 EmpathyContact *contact)
910 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
912 g_object_weak_ref (G_OBJECT (contact),
913 tp_contact_factory_weak_notify,
915 priv->contacts = g_list_prepend (priv->contacts, contact);
917 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
918 empathy_contact_get_id (contact),
919 empathy_contact_get_handle (contact));
923 tp_contact_factory_hold_handles_cb (TpConnection *connection,
929 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
935 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
937 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
939 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
941 return g_object_ref (priv->user);
945 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
948 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
949 EmpathyContact *contact;
951 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
952 g_return_val_if_fail (id != NULL, NULL);
954 /* Check if the contact already exists */
955 contact = tp_contact_factory_find_by_id (tp_factory, id);
957 return g_object_ref (contact);
960 /* Create new contact */
961 contact = g_object_new (EMPATHY_TYPE_CONTACT,
962 "account", priv->account,
965 tp_contact_factory_add_contact (tp_factory, contact);
968 const gchar *contact_ids[] = {id, NULL};
971 contacts = g_list_prepend (NULL, g_object_ref (contact));
972 tp_cli_connection_call_request_handles (priv->connection,
974 TP_HANDLE_TYPE_CONTACT,
976 tp_contact_factory_request_handles_cb,
977 contacts, tp_contact_factory_list_free,
978 G_OBJECT (tp_factory));
985 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
988 EmpathyContact *contact;
992 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
994 handles = g_array_new (FALSE, FALSE, sizeof (guint));
995 g_array_append_val (handles, handle);
997 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
998 g_array_free (handles, TRUE);
1000 contact = contacts ? contacts->data : NULL;
1001 g_list_free (contacts);
1007 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1008 const GArray *handles)
1010 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1011 GList *contacts = NULL;
1012 GArray *new_handles;
1013 GList *new_contacts = NULL;
1016 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1017 g_return_val_if_fail (handles != NULL, NULL);
1019 /* Search all contacts we already have */
1020 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1021 for (i = 0; i < handles->len; i++) {
1022 EmpathyContact *contact;
1025 handle = g_array_index (handles, guint, i);
1030 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1032 contacts = g_list_prepend (contacts, g_object_ref (contact));
1034 g_array_append_val (new_handles, handle);
1038 if (new_handles->len == 0) {
1039 g_array_free (new_handles, TRUE);
1043 /* Create new contacts */
1044 for (i = 0; i < new_handles->len; i++) {
1045 EmpathyContact *contact;
1048 handle = g_array_index (new_handles, guint, i);
1050 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1051 "account", priv->account,
1054 tp_contact_factory_add_contact (tp_factory, contact);
1055 contacts = g_list_prepend (contacts, contact);
1056 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1058 new_contacts = g_list_reverse (new_contacts);
1061 /* Get the IDs of all new handles */
1062 tp_cli_connection_call_inspect_handles (priv->connection,
1064 TP_HANDLE_TYPE_CONTACT,
1066 tp_contact_factory_inspect_handles_cb,
1067 new_contacts, tp_contact_factory_list_free,
1068 G_OBJECT (tp_factory));
1070 /* Hold all new handles. */
1071 /* FIXME: Should be unholded when removed from the factory */
1072 tp_cli_connection_call_hold_handles (priv->connection,
1074 TP_HANDLE_TYPE_CONTACT,
1076 tp_contact_factory_hold_handles_cb,
1078 G_OBJECT (tp_factory));
1080 tp_contact_factory_request_everything (tp_factory, new_handles);
1087 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1088 EmpathyContact *contact,
1091 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1092 GHashTable *new_alias;
1095 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1096 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1097 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1104 handle = empathy_contact_get_handle (contact);
1106 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1107 empathy_contact_get_id (contact),
1110 new_alias = g_hash_table_new_full (g_direct_hash,
1115 g_hash_table_insert (new_alias,
1116 GUINT_TO_POINTER (handle),
1119 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1122 tp_contact_factory_set_aliases_cb,
1124 G_OBJECT (tp_factory));
1126 g_hash_table_destroy (new_alias);
1130 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1133 const gchar *mime_type)
1135 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1137 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1143 if (data && size > 0 && size < G_MAXUINT) {
1146 avatar.data = (gchar*) data;
1149 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1150 mc_account_get_unique_name (priv->account));
1152 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1156 tp_contact_factory_set_avatar_cb,
1158 G_OBJECT (tp_factory));
1160 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1161 mc_account_get_unique_name (priv->account));
1163 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1165 tp_contact_factory_clear_avatar_cb,
1167 G_OBJECT (tp_factory));
1172 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1174 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1176 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1182 tp_contact_factory_get_property (GObject *object,
1187 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1191 g_value_set_object (value, priv->account);
1194 g_value_set_boolean (value, priv->ready);
1197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1203 tp_contact_factory_set_property (GObject *object,
1205 const GValue *value,
1208 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1212 priv->account = g_object_ref (g_value_get_object (value));
1215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1221 tp_contact_factory_finalize (GObject *object)
1223 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1226 empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1228 mc_account_get_normalized_name (priv->account));
1230 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1231 "AccountStatusChanged",
1232 G_CALLBACK (tp_contact_factory_status_changed_cb),
1235 for (l = priv->contacts; l; l = l->next) {
1236 g_object_weak_unref (G_OBJECT (l->data),
1237 tp_contact_factory_weak_notify,
1241 g_list_free (priv->contacts);
1242 g_object_unref (priv->mc);
1243 g_object_unref (priv->account);
1244 g_object_unref (priv->user);
1246 if (priv->connection) {
1247 g_signal_handlers_disconnect_by_func (priv->connection,
1248 tp_contact_factory_connection_invalidated_cb,
1250 g_object_unref (priv->connection);
1253 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1257 tp_contact_factory_constructor (GType type,
1259 GObjectConstructParam *props)
1261 GObject *tp_factory;
1262 EmpathyTpContactFactoryPriv *priv;
1264 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1265 priv = GET_PRIV (tp_factory);
1267 priv->ready = FALSE;
1268 priv->user = empathy_contact_new (priv->account);
1269 empathy_contact_set_is_user (priv->user, TRUE);
1270 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1271 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1277 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1279 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1281 object_class->finalize = tp_contact_factory_finalize;
1282 object_class->constructor = tp_contact_factory_constructor;
1283 object_class->get_property = tp_contact_factory_get_property;
1284 object_class->set_property = tp_contact_factory_set_property;
1286 g_object_class_install_property (object_class,
1288 g_param_spec_object ("account",
1289 "Factory's Account",
1290 "The account associated with the factory",
1293 G_PARAM_CONSTRUCT_ONLY));
1294 g_object_class_install_property (object_class,
1296 g_param_spec_boolean ("ready",
1297 "Wheter the factor is ready",
1298 "Is the factory ready",
1302 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1306 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1308 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1310 priv->mc = empathy_mission_control_new ();
1311 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1312 "AccountStatusChanged",
1313 G_CALLBACK (tp_contact_factory_status_changed_cb),
1317 EmpathyTpContactFactory *
1318 empathy_tp_contact_factory_new (McAccount *account)
1320 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,