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 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);
70 static EmpathyContact *
71 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
74 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
77 for (l = priv->contacts; l; l = l->next) {
78 if (empathy_contact_get_handle (l->data) == handle) {
86 static EmpathyContact *
87 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
90 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
93 for (l = priv->contacts; l; l = l->next) {
94 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
103 tp_contact_factory_weak_notify (gpointer data,
104 GObject *where_the_object_was)
106 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
108 DEBUG ("Remove finalized contact %p", where_the_object_was);
110 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
114 tp_contact_factory_presences_table_foreach (const gchar *state_str,
115 GHashTable *presences_table,
116 EmpathyContact *contact)
118 const GValue *message;
119 const gchar *message_str = NULL;
121 empathy_contact_set_presence (contact,
122 empathy_presence_from_str (state_str));
124 message = g_hash_table_lookup (presences_table, "message");
126 message_str = g_value_get_string (message);
129 if (!G_STR_EMPTY (message_str)) {
130 empathy_contact_set_presence_message (contact, message_str);
132 empathy_contact_set_presence_message (contact, NULL);
137 tp_contact_factory_parse_presence_foreach (guint handle,
138 GValueArray *presence_struct,
139 EmpathyTpContactFactory *tp_factory)
141 GHashTable *presences_table;
142 EmpathyContact *contact;
144 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
149 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
151 g_hash_table_foreach (presences_table,
152 (GHFunc) tp_contact_factory_presences_table_foreach,
155 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
156 empathy_contact_get_id (contact),
158 empathy_contact_get_presence_message (contact),
159 empathy_contact_get_presence (contact));
163 tp_contact_factory_get_presence_cb (TpConnection *connection,
164 GHashTable *handle_table,
170 DEBUG ("Error getting presence: %s", error->message);
171 if (error->domain == TP_DBUS_ERRORS &&
172 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
173 guint *handles = user_data;
175 /* We have no presence iface, set default presence
177 while (*handles != 0) {
178 EmpathyContact *contact;
180 contact = tp_contact_factory_find_by_handle (
181 (EmpathyTpContactFactory*) tp_factory,
184 empathy_contact_set_presence (contact,
185 MC_PRESENCE_AVAILABLE);
195 g_hash_table_foreach (handle_table,
196 (GHFunc) tp_contact_factory_parse_presence_foreach,
197 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
201 tp_contact_factory_presence_update_cb (TpConnection *connection,
202 GHashTable *handle_table,
206 g_hash_table_foreach (handle_table,
207 (GHFunc) tp_contact_factory_parse_presence_foreach,
208 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
212 tp_contact_factory_set_aliases_cb (TpConnection *connection,
218 DEBUG ("Error setting alias: %s", error->message);
223 tp_contact_factory_request_aliases_cb (TpConnection *connection,
224 const gchar **contact_names,
229 guint *handles = user_data;
234 DEBUG ("Error requesting aliases: %s", error->message);
236 /* If we failed to get alias set it to NULL, like that if
237 * someone is waiting for the name to be ready it won't wait
239 while (*handles != 0) {
240 EmpathyContact *contact;
242 contact = tp_contact_factory_find_by_handle (
243 (EmpathyTpContactFactory*) tp_factory,
246 empathy_contact_set_name (contact, NULL);
254 for (name = contact_names; *name; name++) {
255 EmpathyContact *contact;
257 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
263 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
264 empathy_contact_get_id (contact),
265 empathy_contact_get_handle (contact),
268 empathy_contact_set_name (contact, *name);
275 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
276 const GPtrArray *renamed_handlers,
278 GObject *weak_object)
280 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
283 for (i = 0; renamed_handlers->len > i; i++) {
286 GValueArray *renamed_struct;
287 EmpathyContact *contact;
289 renamed_struct = g_ptr_array_index (renamed_handlers, i);
290 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
291 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
292 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
295 /* We don't know this contact, skip */
299 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
300 empathy_contact_get_id (contact),
303 empathy_contact_set_name (contact, alias);
308 tp_contact_factory_set_avatar_cb (TpConnection *connection,
315 DEBUG ("Error setting avatar: %s", error->message);
320 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
326 DEBUG ("Error clearing avatar: %s", error->message);
331 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
334 const GArray *avatar_data,
335 const gchar *mime_type,
339 EmpathyContact *contact;
341 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
347 DEBUG ("Avatar retrieved for contact %s (%d)",
348 empathy_contact_get_id (contact),
351 empathy_contact_load_avatar_data (contact,
359 tp_contact_factory_request_avatars_cb (TpConnection *connection,
365 DEBUG ("Error requesting avatars: %s", error->message);
370 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
374 EmpathyContact *contact;
375 EmpathyAvatar *avatar;
377 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
382 /* Check if we have an avatar */
383 if (G_STR_EMPTY (token)) {
384 empathy_contact_set_avatar (contact, NULL);
388 /* Check if the avatar changed */
389 avatar = empathy_contact_get_avatar (contact);
390 if (avatar && !tp_strdiff (avatar->token, token)) {
394 /* The avatar changed, search the new one in the cache */
395 if (empathy_contact_load_avatar_cache (contact, token)) {
396 /* Got from cache, use it */
400 /* Avatar is not up-to-date, we have to request it. */
405 EmpathyTpContactFactory *tp_factory;
410 tp_contact_factory_avatar_tokens_foreach (gpointer key,
414 TokensData *data = user_data;
415 const gchar *token = value;
416 guint handle = GPOINTER_TO_UINT (key);
418 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
420 g_array_append_val (data->handles, handle);
425 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
434 DEBUG ("Error getting known avatars tokens: %s", error->message);
438 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
439 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
440 g_hash_table_foreach (tokens,
441 tp_contact_factory_avatar_tokens_foreach,
444 DEBUG ("Got %d tokens, need to request %d avatars",
445 g_hash_table_size (tokens), data.handles->len);
447 /* Request needed avatars */
448 if (data.handles->len > 0) {
449 tp_cli_connection_interface_avatars_call_request_avatars (connection,
452 tp_contact_factory_request_avatars_cb,
457 g_array_free (data.handles, TRUE);
461 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
463 const gchar *new_token,
469 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
470 handle, new_token)) {
471 /* Avatar was cached, nothing to do */
475 DEBUG ("Need to request avatar for token %s", new_token);
477 handles = g_array_new (FALSE, FALSE, sizeof (guint));
478 g_array_append_val (handles, handle);
480 tp_cli_connection_interface_avatars_call_request_avatars (connection,
483 tp_contact_factory_request_avatars_cb,
486 g_array_free (handles, TRUE);
490 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
492 const gchar *channel_type,
496 EmpathyContact *contact;
497 EmpathyCapabilities capabilities;
499 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
504 capabilities = empathy_contact_get_capabilities (contact);
505 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
507 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
508 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
509 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
510 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
511 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
513 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
514 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
518 DEBUG ("Changing capabilities for contact %s (%d) to %d",
519 empathy_contact_get_id (contact),
520 empathy_contact_get_handle (contact),
523 empathy_contact_set_capabilities (contact, capabilities);
527 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
528 const GPtrArray *capabilities,
531 GObject *weak_object)
533 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
537 DEBUG ("Error getting capabilities: %s", error->message);
538 /* FIXME Should set the capabilities of the contacts for which this request
539 * originated to NONE */
543 for (i = 0; i < capabilities->len; i++) {
546 const gchar *channel_type;
550 values = g_ptr_array_index (capabilities, i);
551 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
552 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
553 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
554 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
556 tp_contact_factory_update_capabilities (tp_factory,
565 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
566 const GPtrArray *capabilities,
568 GObject *weak_object)
570 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
573 for (i = 0; i < capabilities->len; i++) {
576 const gchar *channel_type;
580 values = g_ptr_array_index (capabilities, i);
581 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
582 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
583 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
584 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
586 tp_contact_factory_update_capabilities (tp_factory,
595 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
596 const GArray *handles)
598 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
601 g_return_if_fail (priv->ready);
603 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
604 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
605 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
608 tp_contact_factory_get_presence_cb,
610 G_OBJECT (tp_factory));
612 /* FIXME: Sometimes the dbus call timesout because CM takes
613 * too much time to request all aliases from the server,
614 * that's why we increase the timeout here. See fd.o bug #14795 */
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_aliasing_call_request_aliases (priv->connection,
620 tp_contact_factory_request_aliases_cb,
622 G_OBJECT (tp_factory));
624 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
627 tp_contact_factory_get_known_avatar_tokens_cb,
629 G_OBJECT (tp_factory));
631 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
634 tp_contact_factory_get_capabilities_cb,
636 G_OBJECT (tp_factory));
640 tp_contact_factory_list_free (gpointer data)
644 g_list_foreach (l, (GFunc) g_object_unref, NULL);
649 tp_contact_factory_request_handles_cb (TpConnection *connection,
650 const GArray *handles,
655 GList *contacts = user_data;
660 DEBUG ("Failed to request handles: %s", error->message);
664 for (l = contacts; l; l = l->next) {
667 handle = g_array_index (handles, guint, i);
668 empathy_contact_set_handle (l->data, handle);
673 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
678 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
685 GList *contacts = user_data;
689 DEBUG ("Failed to inspect handles: %s", error->message);
694 for (l = contacts; l; l = l->next) {
695 empathy_contact_set_id (l->data, *id);
701 tp_contact_factory_disconnect_contact_foreach (gpointer data,
704 EmpathyContact *contact = data;
706 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
707 empathy_contact_set_handle (contact, 0);
711 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
713 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
715 DEBUG ("Connection invalidated");
717 g_object_unref (priv->connection);
718 priv->connection = NULL;
720 g_object_notify (G_OBJECT (tp_factory), "ready");
723 g_list_foreach (priv->contacts,
724 tp_contact_factory_disconnect_contact_foreach,
729 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
731 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
733 GArray *handle_needed;
735 GList *handle_needed_contacts = NULL;
736 GList *id_needed_contacts = NULL;
738 DEBUG ("Connection ready");
741 g_object_notify (G_OBJECT (tp_factory), "ready");
743 /* Connect signals */
744 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
745 tp_contact_factory_aliases_changed_cb,
747 G_OBJECT (tp_factory),
749 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
750 tp_contact_factory_avatar_updated_cb,
752 G_OBJECT (tp_factory),
754 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
755 tp_contact_factory_avatar_retrieved_cb,
757 G_OBJECT (tp_factory),
759 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
760 tp_contact_factory_presence_update_cb,
762 G_OBJECT (tp_factory),
764 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
765 tp_contact_factory_capabilities_changed_cb,
767 G_OBJECT (tp_factory),
770 /* Request needed info for all existing contacts */
771 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
772 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
773 for (l = priv->contacts; l; l = l->next) {
774 EmpathyContact *contact;
779 handle = empathy_contact_get_handle (contact);
780 id = empathy_contact_get_id (contact);
782 g_assert (!G_STR_EMPTY (id));
783 g_array_append_val (handle_needed, id);
784 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
785 g_object_ref (contact));
787 if (G_STR_EMPTY (id)) {
788 g_array_append_val (id_needed, handle);
789 id_needed_contacts = g_list_prepend (id_needed_contacts,
790 g_object_ref (contact));
793 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
794 id_needed_contacts = g_list_reverse (id_needed_contacts);
796 tp_cli_connection_call_request_handles (priv->connection,
798 TP_HANDLE_TYPE_CONTACT,
799 (const gchar**) handle_needed->data,
800 tp_contact_factory_request_handles_cb,
801 handle_needed_contacts, tp_contact_factory_list_free,
802 G_OBJECT (tp_factory));
804 tp_cli_connection_call_inspect_handles (priv->connection,
806 TP_HANDLE_TYPE_CONTACT,
808 tp_contact_factory_inspect_handles_cb,
809 id_needed_contacts, tp_contact_factory_list_free,
810 G_OBJECT (tp_factory));
812 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
815 g_array_free (handle_needed, TRUE);
816 g_array_free (id_needed, TRUE);
820 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
821 const gchar **mime_types,
831 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
834 DEBUG ("Failed to get avatar requirements: %s", error->message);
835 /* We'll just leave avatar_mime_types as NULL; the
836 * avatar-setting code can use this as a signal that you can't
840 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
841 priv->avatar_min_width = min_width;
842 priv->avatar_min_height = min_height;
843 priv->avatar_max_width = max_width;
844 priv->avatar_max_height = max_height;
845 priv->avatar_max_size = max_size;
848 tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
852 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
858 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
861 DEBUG ("Failed to get self handles: %s", error->message);
865 empathy_contact_set_handle (priv->user, handle);
867 /* Get avatar requirements for this connection */
868 tp_cli_connection_interface_avatars_call_get_avatar_requirements (
871 tp_contact_factory_got_avatar_requirements_cb,
877 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
879 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
881 /* Get our own handle */
882 tp_cli_connection_call_get_self_handle (priv->connection,
884 tp_contact_factory_got_self_handle_cb,
886 G_OBJECT (tp_factory));
890 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
892 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
893 gboolean connection_ready;
895 if (priv->connection) {
896 /* We already have our connection object */
900 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
901 if (!priv->connection) {
905 /* We got a new connection, wait for it to be ready */
906 g_signal_connect_swapped (priv->connection, "invalidated",
907 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
910 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
911 if (connection_ready) {
912 tp_contact_factory_connection_ready_cb (tp_factory);
914 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
915 G_CALLBACK (tp_contact_factory_connection_ready_cb),
921 tp_contact_factory_status_changed_cb (MissionControl *mc,
922 TpConnectionStatus status,
924 TpConnectionStatusReason reason,
925 const gchar *unique_name,
926 EmpathyTpContactFactory *tp_factory)
928 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
931 account = mc_account_lookup (unique_name);
932 if (account && empathy_account_equal (account, priv->account)) {
933 tp_contact_factory_status_updated (tp_factory);
935 g_object_unref (account);
939 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
940 EmpathyContact *contact)
942 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
944 g_object_weak_ref (G_OBJECT (contact),
945 tp_contact_factory_weak_notify,
947 priv->contacts = g_list_prepend (priv->contacts, contact);
949 DEBUG ("Contact added: %s (%d)",
950 empathy_contact_get_id (contact),
951 empathy_contact_get_handle (contact));
955 tp_contact_factory_hold_handles_cb (TpConnection *connection,
961 DEBUG ("Failed to hold handles: %s", error->message);
966 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
968 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
970 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
972 return g_object_ref (priv->user);
976 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
979 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
980 EmpathyContact *contact;
982 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
983 g_return_val_if_fail (id != NULL, NULL);
985 /* Check if the contact already exists */
986 contact = tp_contact_factory_find_by_id (tp_factory, id);
988 return g_object_ref (contact);
991 /* Create new contact */
992 contact = g_object_new (EMPATHY_TYPE_CONTACT,
993 "account", priv->account,
996 tp_contact_factory_add_contact (tp_factory, contact);
999 const gchar *contact_ids[] = {id, NULL};
1002 contacts = g_list_prepend (NULL, g_object_ref (contact));
1003 tp_cli_connection_call_request_handles (priv->connection,
1005 TP_HANDLE_TYPE_CONTACT,
1007 tp_contact_factory_request_handles_cb,
1008 contacts, tp_contact_factory_list_free,
1009 G_OBJECT (tp_factory));
1016 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1019 EmpathyContact *contact;
1023 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1025 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1026 g_array_append_val (handles, handle);
1028 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1029 g_array_free (handles, TRUE);
1031 contact = contacts ? contacts->data : NULL;
1032 g_list_free (contacts);
1038 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1039 const GArray *handles)
1041 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1042 GList *contacts = NULL;
1043 GArray *new_handles;
1044 GList *new_contacts = NULL;
1047 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1048 g_return_val_if_fail (handles != NULL, NULL);
1050 /* Search all contacts we already have */
1051 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1052 for (i = 0; i < handles->len; i++) {
1053 EmpathyContact *contact;
1056 handle = g_array_index (handles, guint, i);
1061 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1063 contacts = g_list_prepend (contacts, g_object_ref (contact));
1065 g_array_append_val (new_handles, handle);
1069 if (new_handles->len == 0) {
1070 g_array_free (new_handles, TRUE);
1074 /* Create new contacts */
1075 for (i = 0; i < new_handles->len; i++) {
1076 EmpathyContact *contact;
1079 handle = g_array_index (new_handles, guint, i);
1081 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1082 "account", priv->account,
1085 tp_contact_factory_add_contact (tp_factory, contact);
1086 contacts = g_list_prepend (contacts, contact);
1087 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1089 new_contacts = g_list_reverse (new_contacts);
1092 /* Get the IDs of all new handles */
1093 tp_cli_connection_call_inspect_handles (priv->connection,
1095 TP_HANDLE_TYPE_CONTACT,
1097 tp_contact_factory_inspect_handles_cb,
1098 new_contacts, tp_contact_factory_list_free,
1099 G_OBJECT (tp_factory));
1101 /* Hold all new handles. */
1102 /* FIXME: Should be unholded when removed from the factory */
1103 tp_cli_connection_call_hold_handles (priv->connection,
1105 TP_HANDLE_TYPE_CONTACT,
1107 tp_contact_factory_hold_handles_cb,
1109 G_OBJECT (tp_factory));
1111 tp_contact_factory_request_everything (tp_factory, new_handles);
1114 g_array_free (new_handles, TRUE);
1120 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1121 EmpathyContact *contact,
1124 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1125 GHashTable *new_alias;
1128 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1129 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1130 g_return_if_fail (priv->ready);
1131 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1134 handle = empathy_contact_get_handle (contact);
1136 DEBUG ("Setting alias for contact %s (%d) to %s",
1137 empathy_contact_get_id (contact),
1140 new_alias = g_hash_table_new_full (g_direct_hash,
1145 g_hash_table_insert (new_alias,
1146 GUINT_TO_POINTER (handle),
1149 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1152 tp_contact_factory_set_aliases_cb,
1154 G_OBJECT (tp_factory));
1156 g_hash_table_destroy (new_alias);
1160 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1163 const gchar *mime_type)
1165 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1167 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1168 g_return_if_fail (priv->ready);
1170 if (data && size > 0 && size < G_MAXUINT) {
1173 avatar.data = (gchar*) data;
1176 DEBUG ("Setting avatar on account %s",
1177 mc_account_get_unique_name (priv->account));
1179 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1183 tp_contact_factory_set_avatar_cb,
1185 G_OBJECT (tp_factory));
1187 DEBUG ("Clearing avatar on account %s",
1188 mc_account_get_unique_name (priv->account));
1190 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1192 tp_contact_factory_clear_avatar_cb,
1194 G_OBJECT (tp_factory));
1199 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1201 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1203 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1209 tp_contact_factory_get_property (GObject *object,
1214 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1218 g_value_set_object (value, priv->account);
1221 g_value_set_boolean (value, priv->ready);
1223 case PROP_MIME_TYPES:
1224 g_value_set_boxed (value, priv->avatar_mime_types);
1226 case PROP_MIN_WIDTH:
1227 g_value_set_uint (value, priv->avatar_min_width);
1229 case PROP_MIN_HEIGHT:
1230 g_value_set_uint (value, priv->avatar_min_height);
1232 case PROP_MAX_WIDTH:
1233 g_value_set_uint (value, priv->avatar_max_width);
1235 case PROP_MAX_HEIGHT:
1236 g_value_set_uint (value, priv->avatar_max_height);
1239 g_value_set_uint (value, priv->avatar_max_size);
1242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1248 tp_contact_factory_set_property (GObject *object,
1250 const GValue *value,
1253 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1257 priv->account = g_object_ref (g_value_get_object (value));
1260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1266 tp_contact_factory_finalize (GObject *object)
1268 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1271 DEBUG ("Finalized: %p (%s)", object,
1272 mc_account_get_normalized_name (priv->account));
1274 empathy_disconnect_account_status_changed (priv->token);
1276 for (l = priv->contacts; l; l = l->next) {
1277 g_object_weak_unref (G_OBJECT (l->data),
1278 tp_contact_factory_weak_notify,
1282 g_list_free (priv->contacts);
1283 g_object_unref (priv->mc);
1284 g_object_unref (priv->account);
1285 g_object_unref (priv->user);
1287 if (priv->connection) {
1288 g_signal_handlers_disconnect_by_func (priv->connection,
1289 tp_contact_factory_connection_invalidated_cb,
1291 g_object_unref (priv->connection);
1294 g_strfreev (priv->avatar_mime_types);
1296 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1300 tp_contact_factory_constructor (GType type,
1302 GObjectConstructParam *props)
1304 GObject *tp_factory;
1305 EmpathyTpContactFactoryPriv *priv;
1307 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1308 priv = GET_PRIV (tp_factory);
1310 priv->ready = FALSE;
1311 priv->user = empathy_contact_new (priv->account);
1312 empathy_contact_set_is_user (priv->user, TRUE);
1313 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1314 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1320 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1322 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1324 object_class->finalize = tp_contact_factory_finalize;
1325 object_class->constructor = tp_contact_factory_constructor;
1326 object_class->get_property = tp_contact_factory_get_property;
1327 object_class->set_property = tp_contact_factory_set_property;
1329 g_object_class_install_property (object_class,
1331 g_param_spec_object ("account",
1332 "Factory's Account",
1333 "The account associated with the factory",
1336 G_PARAM_CONSTRUCT_ONLY |
1337 G_PARAM_STATIC_STRINGS));
1338 g_object_class_install_property (object_class,
1340 g_param_spec_boolean ("ready",
1341 "Whether the factory is ready",
1342 "TRUE once the factory is ready to be used",
1345 G_PARAM_STATIC_STRINGS));
1346 g_object_class_install_property (object_class,
1348 g_param_spec_boxed ("avatar-mime-types",
1349 "Supported MIME types for avatars",
1350 "Types of images that may be set as "
1351 "avatars on this connection. Only valid "
1352 "once 'ready' becomes TRUE.",
1355 G_PARAM_STATIC_STRINGS));
1356 g_object_class_install_property (object_class,
1358 g_param_spec_uint ("avatar-min-width",
1359 "Minimum width for avatars",
1360 "Minimum width of avatar that may be set. "
1361 "Only valid once 'ready' becomes TRUE.",
1366 G_PARAM_STATIC_STRINGS));
1367 g_object_class_install_property (object_class,
1369 g_param_spec_uint ("avatar-min-height",
1370 "Minimum height for avatars",
1371 "Minimum height of avatar that may be set. "
1372 "Only valid once 'ready' becomes TRUE.",
1377 G_PARAM_STATIC_STRINGS));
1378 g_object_class_install_property (object_class,
1380 g_param_spec_uint ("avatar-max-width",
1381 "Maximum width for avatars",
1382 "Maximum width of avatar that may be set "
1383 "or 0 if there is no maximum. "
1384 "Only valid once 'ready' becomes TRUE.",
1389 G_PARAM_STATIC_STRINGS));
1390 g_object_class_install_property (object_class,
1392 g_param_spec_uint ("avatar-max-height",
1393 "Maximum height for avatars",
1394 "Maximum height of avatar that may be set "
1395 "or 0 if there is no maximum. "
1396 "Only valid once 'ready' becomes TRUE.",
1401 G_PARAM_STATIC_STRINGS));
1402 g_object_class_install_property (object_class,
1404 g_param_spec_uint ("avatar-max-size",
1405 "Maximum size for avatars in bytes",
1406 "Maximum file size of avatar that may be "
1407 "set or 0 if there is no maximum. "
1408 "Only valid once 'ready' becomes TRUE.",
1413 G_PARAM_STATIC_STRINGS));
1416 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1420 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1422 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1423 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1425 tp_factory->priv = priv;
1426 priv->mc = empathy_mission_control_new ();
1427 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1428 G_CALLBACK (tp_contact_factory_status_changed_cb),
1432 EmpathyTpContactFactory *
1433 empathy_tp_contact_factory_new (McAccount *account)
1435 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,