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 contact_created (EmpathyTpContactFactory *self,
977 EmpathyContact *contact)
979 tp_contact_factory_add_contact (self, contact);
983 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
986 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
987 EmpathyContact *contact;
989 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
990 g_return_val_if_fail (id != NULL, NULL);
992 /* Check if the contact already exists */
993 contact = tp_contact_factory_find_by_id (tp_factory, id);
995 return g_object_ref (contact);
998 /* Create new contact */
999 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1000 "account", priv->account,
1003 contact_created (tp_factory, contact);
1006 const gchar *contact_ids[] = {id, NULL};
1009 contacts = g_list_prepend (NULL, g_object_ref (contact));
1010 tp_cli_connection_call_request_handles (priv->connection,
1012 TP_HANDLE_TYPE_CONTACT,
1014 tp_contact_factory_request_handles_cb,
1015 contacts, tp_contact_factory_list_free,
1016 G_OBJECT (tp_factory));
1023 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1026 EmpathyContact *contact;
1030 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1032 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1033 g_array_append_val (handles, handle);
1035 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1036 g_array_free (handles, TRUE);
1038 contact = contacts ? contacts->data : NULL;
1039 g_list_free (contacts);
1045 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1046 const GArray *handles)
1048 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1049 GList *contacts = NULL;
1050 GArray *new_handles;
1051 GList *new_contacts = NULL;
1054 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1055 g_return_val_if_fail (handles != NULL, NULL);
1057 /* Search all contacts we already have */
1058 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1059 for (i = 0; i < handles->len; i++) {
1060 EmpathyContact *contact;
1063 handle = g_array_index (handles, guint, i);
1068 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1070 contacts = g_list_prepend (contacts, g_object_ref (contact));
1072 g_array_append_val (new_handles, handle);
1076 if (new_handles->len == 0) {
1077 g_array_free (new_handles, TRUE);
1081 /* Create new contacts */
1082 for (i = 0; i < new_handles->len; i++) {
1083 EmpathyContact *contact;
1086 handle = g_array_index (new_handles, guint, i);
1088 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1089 "account", priv->account,
1092 contact_created (tp_factory, contact);
1093 contacts = g_list_prepend (contacts, contact);
1094 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1096 new_contacts = g_list_reverse (new_contacts);
1099 /* Get the IDs of all new handles */
1100 tp_cli_connection_call_inspect_handles (priv->connection,
1102 TP_HANDLE_TYPE_CONTACT,
1104 tp_contact_factory_inspect_handles_cb,
1105 new_contacts, tp_contact_factory_list_free,
1106 G_OBJECT (tp_factory));
1108 /* Hold all new handles. */
1109 /* FIXME: Should be unholded when removed from the factory */
1110 tp_cli_connection_call_hold_handles (priv->connection,
1112 TP_HANDLE_TYPE_CONTACT,
1114 tp_contact_factory_hold_handles_cb,
1116 G_OBJECT (tp_factory));
1118 tp_contact_factory_request_everything (tp_factory, new_handles);
1121 g_array_free (new_handles, TRUE);
1127 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1128 EmpathyContact *contact,
1131 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1132 GHashTable *new_alias;
1135 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1136 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1137 g_return_if_fail (priv->ready);
1138 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1141 handle = empathy_contact_get_handle (contact);
1143 DEBUG ("Setting alias for contact %s (%d) to %s",
1144 empathy_contact_get_id (contact),
1147 new_alias = g_hash_table_new_full (g_direct_hash,
1152 g_hash_table_insert (new_alias,
1153 GUINT_TO_POINTER (handle),
1156 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1159 tp_contact_factory_set_aliases_cb,
1161 G_OBJECT (tp_factory));
1163 g_hash_table_destroy (new_alias);
1167 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1170 const gchar *mime_type)
1172 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1174 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1175 g_return_if_fail (priv->ready);
1177 if (data && size > 0 && size < G_MAXUINT) {
1180 avatar.data = (gchar*) data;
1183 DEBUG ("Setting avatar on account %s",
1184 mc_account_get_unique_name (priv->account));
1186 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1190 tp_contact_factory_set_avatar_cb,
1192 G_OBJECT (tp_factory));
1194 DEBUG ("Clearing avatar on account %s",
1195 mc_account_get_unique_name (priv->account));
1197 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1199 tp_contact_factory_clear_avatar_cb,
1201 G_OBJECT (tp_factory));
1206 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1208 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1210 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1216 tp_contact_factory_get_property (GObject *object,
1221 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1225 g_value_set_object (value, priv->account);
1228 g_value_set_boolean (value, priv->ready);
1230 case PROP_MIME_TYPES:
1231 g_value_set_boxed (value, priv->avatar_mime_types);
1233 case PROP_MIN_WIDTH:
1234 g_value_set_uint (value, priv->avatar_min_width);
1236 case PROP_MIN_HEIGHT:
1237 g_value_set_uint (value, priv->avatar_min_height);
1239 case PROP_MAX_WIDTH:
1240 g_value_set_uint (value, priv->avatar_max_width);
1242 case PROP_MAX_HEIGHT:
1243 g_value_set_uint (value, priv->avatar_max_height);
1246 g_value_set_uint (value, priv->avatar_max_size);
1249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1255 tp_contact_factory_set_property (GObject *object,
1257 const GValue *value,
1260 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1264 priv->account = g_object_ref (g_value_get_object (value));
1267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1273 tp_contact_factory_finalize (GObject *object)
1275 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1278 DEBUG ("Finalized: %p (%s)", object,
1279 mc_account_get_normalized_name (priv->account));
1281 empathy_disconnect_account_status_changed (priv->token);
1283 for (l = priv->contacts; l; l = l->next) {
1284 g_object_weak_unref (G_OBJECT (l->data),
1285 tp_contact_factory_weak_notify,
1289 g_list_free (priv->contacts);
1290 g_object_unref (priv->mc);
1291 g_object_unref (priv->account);
1292 g_object_unref (priv->user);
1294 if (priv->connection) {
1295 g_signal_handlers_disconnect_by_func (priv->connection,
1296 tp_contact_factory_connection_invalidated_cb,
1298 g_object_unref (priv->connection);
1301 g_strfreev (priv->avatar_mime_types);
1303 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1307 tp_contact_factory_constructor (GType type,
1309 GObjectConstructParam *props)
1311 GObject *tp_factory;
1312 EmpathyTpContactFactoryPriv *priv;
1314 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1315 priv = GET_PRIV (tp_factory);
1317 priv->ready = FALSE;
1318 priv->user = empathy_contact_new (priv->account);
1319 empathy_contact_set_is_user (priv->user, TRUE);
1320 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1321 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1327 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1329 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1331 object_class->finalize = tp_contact_factory_finalize;
1332 object_class->constructor = tp_contact_factory_constructor;
1333 object_class->get_property = tp_contact_factory_get_property;
1334 object_class->set_property = tp_contact_factory_set_property;
1336 g_object_class_install_property (object_class,
1338 g_param_spec_object ("account",
1339 "Factory's Account",
1340 "The account associated with the factory",
1343 G_PARAM_CONSTRUCT_ONLY |
1344 G_PARAM_STATIC_STRINGS));
1345 g_object_class_install_property (object_class,
1347 g_param_spec_boolean ("ready",
1348 "Whether the factory is ready",
1349 "TRUE once the factory is ready to be used",
1352 G_PARAM_STATIC_STRINGS));
1353 g_object_class_install_property (object_class,
1355 g_param_spec_boxed ("avatar-mime-types",
1356 "Supported MIME types for avatars",
1357 "Types of images that may be set as "
1358 "avatars on this connection. Only valid "
1359 "once 'ready' becomes TRUE.",
1362 G_PARAM_STATIC_STRINGS));
1363 g_object_class_install_property (object_class,
1365 g_param_spec_uint ("avatar-min-width",
1366 "Minimum width for avatars",
1367 "Minimum width of avatar that may be set. "
1368 "Only valid once 'ready' becomes TRUE.",
1373 G_PARAM_STATIC_STRINGS));
1374 g_object_class_install_property (object_class,
1376 g_param_spec_uint ("avatar-min-height",
1377 "Minimum height for avatars",
1378 "Minimum height of avatar that may be set. "
1379 "Only valid once 'ready' becomes TRUE.",
1384 G_PARAM_STATIC_STRINGS));
1385 g_object_class_install_property (object_class,
1387 g_param_spec_uint ("avatar-max-width",
1388 "Maximum width for avatars",
1389 "Maximum width of avatar that may be set "
1390 "or 0 if there is no maximum. "
1391 "Only valid once 'ready' becomes TRUE.",
1396 G_PARAM_STATIC_STRINGS));
1397 g_object_class_install_property (object_class,
1399 g_param_spec_uint ("avatar-max-height",
1400 "Maximum height for avatars",
1401 "Maximum height of avatar that may be set "
1402 "or 0 if there is no maximum. "
1403 "Only valid once 'ready' becomes TRUE.",
1408 G_PARAM_STATIC_STRINGS));
1409 g_object_class_install_property (object_class,
1411 g_param_spec_uint ("avatar-max-size",
1412 "Maximum size for avatars in bytes",
1413 "Maximum file size of avatar that may be "
1414 "set or 0 if there is no maximum. "
1415 "Only valid once 'ready' becomes TRUE.",
1420 G_PARAM_STATIC_STRINGS));
1423 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1427 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1429 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1430 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1432 tp_factory->priv = priv;
1433 priv->mc = empathy_mission_control_new ();
1434 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1435 G_CALLBACK (tp_contact_factory_status_changed_cb),
1439 EmpathyTpContactFactory *
1440 empathy_tp_contact_factory_new (McAccount *account)
1442 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,