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_new (guint, handles->len + 1);
625 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
626 dup_handles[handles->len] = 0;
627 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
630 tp_contact_factory_request_aliases_cb,
632 G_OBJECT (tp_factory));
634 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
637 tp_contact_factory_get_known_avatar_tokens_cb,
639 G_OBJECT (tp_factory));
641 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
644 tp_contact_factory_get_capabilities_cb,
646 G_OBJECT (tp_factory));
650 tp_contact_factory_list_free (gpointer data)
654 g_list_foreach (l, (GFunc) g_object_unref, NULL);
659 tp_contact_factory_request_handles_cb (TpConnection *connection,
660 const GArray *handles,
665 GList *contacts = user_data;
670 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
675 for (l = contacts; l; l = l->next) {
678 handle = g_array_index (handles, guint, i);
679 empathy_contact_set_handle (l->data, handle);
684 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
689 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
696 GList *contacts = user_data;
700 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
706 for (l = contacts; l; l = l->next) {
707 empathy_contact_set_id (l->data, *id);
713 tp_contact_factory_disconnect_contact_foreach (gpointer data,
716 EmpathyContact *contact = data;
718 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
719 empathy_contact_set_handle (contact, 0);
723 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
725 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
727 empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
729 g_object_unref (priv->connection);
730 priv->connection = NULL;
732 g_object_notify (G_OBJECT (tp_factory), "ready");
735 g_list_foreach (priv->contacts,
736 tp_contact_factory_disconnect_contact_foreach,
742 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
748 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
750 GArray *handle_needed;
752 GList *handle_needed_contacts = NULL;
753 GList *id_needed_contacts = NULL;
756 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
761 empathy_debug (DEBUG_DOMAIN, "Connection ready");
763 empathy_contact_set_handle (priv->user, handle);
765 g_object_notify (tp_factory, "ready");
767 /* Connect signals */
768 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
769 tp_contact_factory_aliases_changed_cb,
771 G_OBJECT (tp_factory),
773 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
774 tp_contact_factory_avatar_updated_cb,
776 G_OBJECT (tp_factory),
778 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
779 tp_contact_factory_avatar_retrieved_cb,
781 G_OBJECT (tp_factory),
783 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
784 tp_contact_factory_presence_update_cb,
786 G_OBJECT (tp_factory),
788 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
789 tp_contact_factory_capabilities_changed_cb,
791 G_OBJECT (tp_factory),
794 /* Request needed info for all existing contacts */
795 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
796 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
797 for (l = priv->contacts; l; l = l->next) {
798 EmpathyContact *contact;
803 handle = empathy_contact_get_handle (contact);
804 id = empathy_contact_get_id (contact);
806 g_assert (!G_STR_EMPTY (id));
807 g_array_append_val (handle_needed, id);
808 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
809 g_object_ref (contact));
811 if (G_STR_EMPTY (id)) {
812 g_array_append_val (id_needed, handle);
813 id_needed_contacts = g_list_prepend (id_needed_contacts,
814 g_object_ref (contact));
817 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
818 id_needed_contacts = g_list_reverse (id_needed_contacts);
820 tp_cli_connection_call_request_handles (priv->connection,
822 TP_HANDLE_TYPE_CONTACT,
823 (const gchar**) handle_needed->data,
824 tp_contact_factory_request_handles_cb,
825 handle_needed_contacts, tp_contact_factory_list_free,
826 G_OBJECT (tp_factory));
828 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
830 tp_cli_connection_call_inspect_handles (priv->connection,
832 TP_HANDLE_TYPE_CONTACT,
834 tp_contact_factory_inspect_handles_cb,
835 id_needed_contacts, tp_contact_factory_list_free,
836 G_OBJECT (tp_factory));
838 g_array_free (handle_needed, TRUE);
839 g_array_free (id_needed, TRUE);
843 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
845 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
847 /* Get our own handle */
848 tp_cli_connection_call_get_self_handle (priv->connection,
850 tp_contact_factory_got_self_handle_cb,
852 G_OBJECT (tp_factory));
856 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
858 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
860 gboolean connection_ready;
862 if (priv->connection) {
863 /* We already have our connection object */
867 tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
872 /* We got a new connection, wait for it to be ready */
873 priv->connection = tp_conn_dup_connection (tp_conn);
874 g_object_unref (tp_conn);
876 g_signal_connect_swapped (priv->connection, "invalidated",
877 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
880 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
881 if (connection_ready) {
882 tp_contact_factory_connection_ready_cb (tp_factory);
884 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
885 G_CALLBACK (tp_contact_factory_connection_ready_cb),
891 tp_contact_factory_status_changed_cb (MissionControl *mc,
892 TpConnectionStatus status,
894 TpConnectionStatusReason reason,
895 const gchar *unique_name,
896 EmpathyTpContactFactory *tp_factory)
898 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
901 account = mc_account_lookup (unique_name);
902 if (account && empathy_account_equal (account, priv->account)) {
903 tp_contact_factory_status_updated (tp_factory);
905 g_object_unref (account);
909 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
910 EmpathyContact *contact)
912 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
914 g_object_weak_ref (G_OBJECT (contact),
915 tp_contact_factory_weak_notify,
917 priv->contacts = g_list_prepend (priv->contacts, contact);
919 empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
920 empathy_contact_get_id (contact),
921 empathy_contact_get_handle (contact));
925 tp_contact_factory_hold_handles_cb (TpConnection *connection,
931 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
937 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
939 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
941 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
943 return g_object_ref (priv->user);
947 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
950 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
951 EmpathyContact *contact;
953 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
954 g_return_val_if_fail (id != NULL, NULL);
956 /* Check if the contact already exists */
957 contact = tp_contact_factory_find_by_id (tp_factory, id);
959 return g_object_ref (contact);
962 /* Create new contact */
963 contact = g_object_new (EMPATHY_TYPE_CONTACT,
964 "account", priv->account,
967 tp_contact_factory_add_contact (tp_factory, contact);
970 const gchar *contact_ids[] = {id, NULL};
973 contacts = g_list_prepend (NULL, g_object_ref (contact));
974 tp_cli_connection_call_request_handles (priv->connection,
976 TP_HANDLE_TYPE_CONTACT,
978 tp_contact_factory_request_handles_cb,
979 contacts, tp_contact_factory_list_free,
980 G_OBJECT (tp_factory));
987 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
990 EmpathyContact *contact;
994 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
996 handles = g_array_new (FALSE, FALSE, sizeof (guint));
997 g_array_append_val (handles, handle);
999 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1000 g_array_free (handles, TRUE);
1002 contact = contacts ? contacts->data : NULL;
1003 g_list_free (contacts);
1009 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1010 const GArray *handles)
1012 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1013 GList *contacts = NULL;
1014 GArray *new_handles;
1015 GList *new_contacts = NULL;
1018 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1019 g_return_val_if_fail (handles != NULL, NULL);
1021 /* Search all contacts we already have */
1022 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1023 for (i = 0; i < handles->len; i++) {
1024 EmpathyContact *contact;
1027 handle = g_array_index (handles, guint, i);
1032 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1034 contacts = g_list_prepend (contacts, g_object_ref (contact));
1036 g_array_append_val (new_handles, handle);
1040 if (new_handles->len == 0) {
1041 g_array_free (new_handles, TRUE);
1045 /* Create new contacts */
1046 for (i = 0; i < new_handles->len; i++) {
1047 EmpathyContact *contact;
1050 handle = g_array_index (new_handles, guint, i);
1052 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1053 "account", priv->account,
1056 tp_contact_factory_add_contact (tp_factory, contact);
1057 contacts = g_list_prepend (contacts, contact);
1058 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1060 new_contacts = g_list_reverse (new_contacts);
1063 /* Get the IDs of all new handles */
1064 tp_cli_connection_call_inspect_handles (priv->connection,
1066 TP_HANDLE_TYPE_CONTACT,
1068 tp_contact_factory_inspect_handles_cb,
1069 new_contacts, tp_contact_factory_list_free,
1070 G_OBJECT (tp_factory));
1072 /* Hold all new handles. */
1073 /* FIXME: Should be unholded when removed from the factory */
1074 tp_cli_connection_call_hold_handles (priv->connection,
1076 TP_HANDLE_TYPE_CONTACT,
1078 tp_contact_factory_hold_handles_cb,
1080 G_OBJECT (tp_factory));
1082 tp_contact_factory_request_everything (tp_factory, new_handles);
1089 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1090 EmpathyContact *contact,
1093 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1094 GHashTable *new_alias;
1097 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1098 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1099 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1106 handle = empathy_contact_get_handle (contact);
1108 empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1109 empathy_contact_get_id (contact),
1112 new_alias = g_hash_table_new_full (g_direct_hash,
1117 g_hash_table_insert (new_alias,
1118 GUINT_TO_POINTER (handle),
1121 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1124 tp_contact_factory_set_aliases_cb,
1126 G_OBJECT (tp_factory));
1128 g_hash_table_destroy (new_alias);
1132 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1135 const gchar *mime_type)
1137 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1139 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1145 if (data && size > 0 && size < G_MAXUINT) {
1148 avatar.data = (gchar*) data;
1151 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1152 mc_account_get_unique_name (priv->account));
1154 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1158 tp_contact_factory_set_avatar_cb,
1160 G_OBJECT (tp_factory));
1162 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1163 mc_account_get_unique_name (priv->account));
1165 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1167 tp_contact_factory_clear_avatar_cb,
1169 G_OBJECT (tp_factory));
1174 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1176 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1178 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1184 tp_contact_factory_get_property (GObject *object,
1189 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1193 g_value_set_object (value, priv->account);
1196 g_value_set_boolean (value, priv->ready);
1199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1205 tp_contact_factory_set_property (GObject *object,
1207 const GValue *value,
1210 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1214 priv->account = g_object_ref (g_value_get_object (value));
1217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1223 tp_contact_factory_finalize (GObject *object)
1225 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1228 empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1230 mc_account_get_normalized_name (priv->account));
1232 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1233 "AccountStatusChanged",
1234 G_CALLBACK (tp_contact_factory_status_changed_cb),
1237 for (l = priv->contacts; l; l = l->next) {
1238 g_object_weak_unref (G_OBJECT (l->data),
1239 tp_contact_factory_weak_notify,
1243 g_list_free (priv->contacts);
1244 g_object_unref (priv->mc);
1245 g_object_unref (priv->account);
1246 g_object_unref (priv->user);
1248 if (priv->connection) {
1249 g_signal_handlers_disconnect_by_func (priv->connection,
1250 tp_contact_factory_connection_invalidated_cb,
1252 g_object_unref (priv->connection);
1255 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1259 tp_contact_factory_constructor (GType type,
1261 GObjectConstructParam *props)
1263 GObject *tp_factory;
1264 EmpathyTpContactFactoryPriv *priv;
1266 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1267 priv = GET_PRIV (tp_factory);
1269 priv->ready = FALSE;
1270 priv->user = empathy_contact_new (priv->account);
1271 empathy_contact_set_is_user (priv->user, TRUE);
1272 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1273 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1279 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1281 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1283 object_class->finalize = tp_contact_factory_finalize;
1284 object_class->constructor = tp_contact_factory_constructor;
1285 object_class->get_property = tp_contact_factory_get_property;
1286 object_class->set_property = tp_contact_factory_set_property;
1288 g_object_class_install_property (object_class,
1290 g_param_spec_object ("account",
1291 "Factory's Account",
1292 "The account associated with the factory",
1295 G_PARAM_CONSTRUCT_ONLY));
1296 g_object_class_install_property (object_class,
1298 g_param_spec_boolean ("ready",
1299 "Wheter the factor is ready",
1300 "Is the factory ready",
1304 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1308 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1310 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1312 priv->mc = empathy_mission_control_new ();
1313 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1314 "AccountStatusChanged",
1315 G_CALLBACK (tp_contact_factory_status_changed_cb),
1319 EmpathyTpContactFactory *
1320 empathy_tp_contact_factory_new (McAccount *account)
1322 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,