2 * empathy-account-settings.c - Source for EmpathyAccountSettings
3 * Copyright (C) 2009 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <telepathy-glib/account-manager.h>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/gtypes.h>
30 #include "empathy-account-settings.h"
31 #include "empathy-connection-managers.h"
32 #include "empathy-utils.h"
33 #include "empathy-idle.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
36 #include <libempathy/empathy-debug.h>
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
40 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
48 PROP_DISPLAY_NAME_OVERRIDDEN,
52 /* private structure */
53 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
55 struct _EmpathyAccountSettingsPriv
57 gboolean dispose_has_run;
58 EmpathyConnectionManagers *managers;
59 TpAccountManager *account_manager;
61 TpConnectionManager *manager;
69 gboolean display_name_overridden;
72 GHashTable *parameters;
73 GArray *unset_parameters;
74 GArray *required_params;
76 gulong managers_ready_id;
78 GSimpleAsyncResult *apply_result;
82 empathy_account_settings_init (EmpathyAccountSettings *obj)
84 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
85 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
89 /* allocate any data required by the object here */
90 priv->managers = empathy_connection_managers_dup_singleton ();
91 priv->account_manager = tp_account_manager_dup ();
93 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
94 g_free, (GDestroyNotify) tp_g_value_slice_free);
96 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
99 static void empathy_account_settings_dispose (GObject *object);
100 static void empathy_account_settings_finalize (GObject *object);
101 static void empathy_account_settings_account_ready_cb (GObject *source_object,
102 GAsyncResult *result, gpointer user_data);
103 static void empathy_account_settings_managers_ready_cb (GObject *obj,
104 GParamSpec *pspec, gpointer user_data);
105 static void empathy_account_settings_check_readyness (
106 EmpathyAccountSettings *self);
109 empathy_account_settings_set_property (GObject *object,
114 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
115 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
120 priv->account = g_value_dup_object (value);
123 priv->cm_name = g_value_dup_string (value);
126 priv->protocol = g_value_dup_string (value);
129 priv->service = g_value_dup_string (value);
131 case PROP_DISPLAY_NAME:
132 priv->display_name = g_value_dup_string (value);
134 case PROP_DISPLAY_NAME_OVERRIDDEN:
135 priv->display_name_overridden = g_value_get_boolean (value);
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 empathy_account_settings_get_property (GObject *object,
149 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
150 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
155 g_value_set_object (value, priv->account);
158 g_value_set_string (value, priv->cm_name);
161 g_value_set_string (value, priv->protocol);
164 g_value_set_string (value, priv->service);
166 case PROP_DISPLAY_NAME:
167 g_value_set_string (value, priv->display_name);
169 case PROP_DISPLAY_NAME_OVERRIDDEN:
170 g_value_set_boolean (value, priv->display_name_overridden);
173 g_value_set_boolean (value, priv->ready);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 empathy_account_settings_constructed (GObject *object)
184 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
185 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
187 if (priv->account != NULL)
189 g_free (priv->cm_name);
190 g_free (priv->protocol);
191 g_free (priv->service);
194 g_strdup (tp_account_get_connection_manager (priv->account));
196 g_strdup (tp_account_get_protocol (priv->account));
198 g_strdup (tp_account_get_service (priv->account));
199 priv->icon_name = g_strdup
200 (tp_account_get_icon_name (priv->account));
204 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
207 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
209 empathy_account_settings_check_readyness (self);
213 tp_account_prepare_async (priv->account, NULL,
214 empathy_account_settings_account_ready_cb, self);
215 tp_g_signal_connect_object (priv->managers, "notify::ready",
216 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
220 empathy_account_settings_parent_class)->constructed != NULL)
222 empathy_account_settings_parent_class)->constructed (object);
227 empathy_account_settings_class_init (
228 EmpathyAccountSettingsClass *empathy_account_settings_class)
230 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
232 g_type_class_add_private (empathy_account_settings_class, sizeof
233 (EmpathyAccountSettingsPriv));
235 object_class->dispose = empathy_account_settings_dispose;
236 object_class->finalize = empathy_account_settings_finalize;
237 object_class->set_property = empathy_account_settings_set_property;
238 object_class->get_property = empathy_account_settings_get_property;
239 object_class->constructed = empathy_account_settings_constructed;
241 g_object_class_install_property (object_class, PROP_ACCOUNT,
242 g_param_spec_object ("account",
244 "The TpAccount backing these settings",
246 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
248 g_object_class_install_property (object_class, PROP_CM_NAME,
249 g_param_spec_string ("connection-manager",
250 "connection-manager",
251 "The name of the connection manager this account uses",
253 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
255 g_object_class_install_property (object_class, PROP_PROTOCOL,
256 g_param_spec_string ("protocol",
258 "The name of the protocol this account uses",
260 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
262 g_object_class_install_property (object_class, PROP_SERVICE,
263 g_param_spec_string ("service",
265 "The service of this account, or NULL",
267 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
269 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
270 g_param_spec_string ("display-name",
272 "The display name account these settings belong to",
274 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
276 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
277 g_param_spec_boolean ("display-name-overridden",
278 "display-name-overridden",
279 "Whether the display name for this account has been manually "
282 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
284 g_object_class_install_property (object_class, PROP_READY,
285 g_param_spec_boolean ("ready",
287 "Whether this account is ready to be used",
289 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
293 empathy_account_settings_dispose (GObject *object)
295 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
296 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
298 if (priv->dispose_has_run)
301 priv->dispose_has_run = TRUE;
303 if (priv->managers_ready_id != 0)
304 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
305 priv->managers_ready_id = 0;
307 if (priv->managers != NULL)
308 g_object_unref (priv->managers);
309 priv->managers = NULL;
311 if (priv->manager != NULL)
312 g_object_unref (priv->manager);
313 priv->manager = NULL;
315 if (priv->account_manager != NULL)
316 g_object_unref (priv->account_manager);
317 priv->account_manager = NULL;
319 if (priv->account != NULL)
320 g_object_unref (priv->account);
321 priv->account = NULL;
323 /* release any references held by the object here */
324 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
325 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
329 empathy_account_settings_free_unset_parameters (
330 EmpathyAccountSettings *settings)
332 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
335 for (i = 0 ; i < priv->unset_parameters->len; i++)
336 g_free (g_array_index (priv->unset_parameters, gchar *, i));
338 g_array_set_size (priv->unset_parameters, 0);
342 empathy_account_settings_finalize (GObject *object)
344 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
345 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
347 /* free any data held directly by the object here */
348 g_free (priv->cm_name);
349 g_free (priv->protocol);
350 g_free (priv->service);
351 g_free (priv->display_name);
352 g_free (priv->icon_name);
354 if (priv->required_params != NULL)
355 g_array_free (priv->required_params, TRUE);
357 g_hash_table_destroy (priv->parameters);
359 empathy_account_settings_free_unset_parameters (self);
360 g_array_free (priv->unset_parameters, TRUE);
362 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
366 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
368 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
369 const TpConnectionManagerProtocol *tp_protocol;
374 if (priv->account != NULL
375 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
378 if (!empathy_connection_managers_is_ready (priv->managers))
381 priv->manager = empathy_connection_managers_get_cm (
382 priv->managers, priv->cm_name);
384 if (priv->manager == NULL)
387 if (priv->account != NULL)
389 g_free (priv->display_name);
391 g_strdup (tp_account_get_display_name (priv->account));
393 g_free (priv->icon_name);
395 g_strdup (tp_account_get_icon_name (priv->account));
398 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
401 if (tp_protocol == NULL)
403 priv->manager = NULL;
407 if (priv->required_params == NULL)
409 TpConnectionManagerParam *cur;
412 priv->required_params = g_array_new (TRUE, FALSE, sizeof (gchar *));
414 for (cur = tp_protocol->params; cur->name != NULL; cur++)
416 if (tp_connection_manager_param_is_required (cur))
418 val = g_strdup (cur->name);
419 g_array_append_val (priv->required_params, val);
424 g_object_ref (priv->manager);
427 g_object_notify (G_OBJECT (self), "ready");
431 empathy_account_settings_account_ready_cb (GObject *source_object,
432 GAsyncResult *result,
435 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
436 TpAccount *account = TP_ACCOUNT (source_object);
437 GError *error = NULL;
439 if (!tp_account_prepare_finish (account, result, &error))
441 DEBUG ("Failed to prepare account: %s", error->message);
442 g_error_free (error);
446 empathy_account_settings_check_readyness (settings);
450 empathy_account_settings_managers_ready_cb (GObject *object,
454 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
456 empathy_account_settings_check_readyness (settings);
459 EmpathyAccountSettings *
460 empathy_account_settings_new (const gchar *connection_manager,
461 const gchar *protocol,
462 const gchar *service,
463 const char *display_name)
465 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
466 "connection-manager", connection_manager,
467 "protocol", protocol,
469 "display-name", display_name,
473 EmpathyAccountSettings *
474 empathy_account_settings_new_for_account (TpAccount *account)
476 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
481 TpConnectionManagerParam *
482 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
484 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
485 const TpConnectionManagerProtocol *tp_protocol;
487 g_return_val_if_fail (priv->manager != NULL, NULL);
488 g_return_val_if_fail (priv->protocol != NULL, NULL);
490 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
492 if (tp_protocol == NULL)
494 DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
499 return tp_protocol->params;
503 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
505 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
511 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
513 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
515 return priv->cm_name;
519 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
521 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
523 return priv->protocol;
527 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
529 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
531 return priv->service;
535 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
537 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
539 return priv->icon_name;
543 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
545 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
547 return priv->display_name;
551 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
553 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
555 return priv->account;
559 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
562 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
566 a = priv->unset_parameters;
568 for (i = 0; i < a->len; i++)
570 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
577 static TpConnectionManagerParam *
578 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
581 TpConnectionManagerParam *tp_params =
582 empathy_account_settings_get_tp_params (settings);
583 TpConnectionManagerParam *p;
585 for (p = tp_params; p != NULL && p->name != NULL; p++)
587 if (tp_strdiff (p->name, param))
597 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
600 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
604 for (idx = 0; idx < priv->unset_parameters->len; idx++)
606 val = g_array_index (priv->unset_parameters, gchar *, idx);
608 if (!tp_strdiff (val, param))
610 priv->unset_parameters =
611 g_array_remove_index (priv->unset_parameters, idx);
620 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
623 TpConnectionManagerParam *p;
625 p = empathy_account_settings_get_tp_param (settings, param);
627 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
630 return &(p->default_value);
634 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
637 TpConnectionManagerParam *p;
639 p = empathy_account_settings_get_tp_param (settings, param);
644 return p->dbus_signature;
648 empathy_account_settings_get (EmpathyAccountSettings *settings,
651 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
652 const GValue *result = NULL;
654 /* Lookup the update parameters we set */
655 result = tp_asv_lookup (priv->parameters, param);
659 /* If the parameters isn't unset use the accounts setting if any */
660 if (priv->account != NULL
661 && !empathy_account_settings_is_unset (settings, param))
663 const GHashTable *parameters;
665 parameters = tp_account_get_parameters (priv->account);
666 result = tp_asv_lookup (parameters, param);
672 /* fallback to the default */
673 return empathy_account_settings_get_default (settings, param);
677 empathy_account_settings_unset (EmpathyAccountSettings *settings,
680 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
682 if (empathy_account_settings_is_unset (settings, param))
685 v = g_strdup (param);
687 g_array_append_val (priv->unset_parameters, v);
688 g_hash_table_remove (priv->parameters, param);
692 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
694 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
696 g_hash_table_remove_all (priv->parameters);
697 empathy_account_settings_free_unset_parameters (settings);
701 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
706 v = empathy_account_settings_get (settings, param);
708 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
711 return g_value_get_string (v);
714 const gchar * const *
715 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
720 v = empathy_account_settings_get (settings, param);
722 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
725 return g_value_get_boxed (v);
729 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
735 v = empathy_account_settings_get (settings, param);
740 switch G_VALUE_TYPE (v)
743 ret = g_value_get_uchar (v);
746 ret = g_value_get_int (v);
749 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
753 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
756 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
768 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
774 v = empathy_account_settings_get (settings, param);
778 switch G_VALUE_TYPE (v)
781 ret = g_value_get_uchar (v);
784 ret = g_value_get_int (v);
787 ret = g_value_get_uint (v);
790 ret = g_value_get_int64 (v);
793 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
804 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
810 v = empathy_account_settings_get (settings, param);
814 switch G_VALUE_TYPE (v)
817 ret = g_value_get_uchar (v);
820 ret = MAX (0, g_value_get_int (v));
823 ret = g_value_get_uint (v);
826 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
829 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
840 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
846 v = empathy_account_settings_get (settings, param);
848 if (v == NULL || !G_VALUE_HOLDS_INT (v))
851 switch G_VALUE_TYPE (v)
854 ret = g_value_get_uchar (v);
857 ret = MAX (0, g_value_get_int (v));
860 ret = g_value_get_uint (v);
863 ret = MAX (0, g_value_get_int64 (v));
866 ret = g_value_get_uint64 (v);
877 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
882 v = empathy_account_settings_get (settings, param);
884 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
887 return g_value_get_boolean (v);
891 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
895 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
897 g_return_if_fail (param != NULL);
898 g_return_if_fail (value != NULL);
900 tp_asv_set_string (priv->parameters, g_strdup (param), value);
902 account_settings_remove_from_unset (settings, param);
906 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
910 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
912 g_return_if_fail (param != NULL);
913 g_return_if_fail (value != NULL);
915 tp_asv_set_strv (priv->parameters, g_strdup (param), value);
917 account_settings_remove_from_unset (settings, param);
921 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
925 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
927 g_return_if_fail (param != NULL);
929 tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
931 account_settings_remove_from_unset (settings, param);
935 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
939 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
941 g_return_if_fail (param != NULL);
943 tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
945 account_settings_remove_from_unset (settings, param);
949 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
953 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
955 g_return_if_fail (param != NULL);
957 tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
959 account_settings_remove_from_unset (settings, param);
963 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
967 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
969 g_return_if_fail (param != NULL);
971 tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
973 account_settings_remove_from_unset (settings, param);
977 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
981 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
983 g_return_if_fail (param != NULL);
985 tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
987 account_settings_remove_from_unset (settings, param);
991 account_settings_display_name_set_cb (GObject *src,
995 GError *error = NULL;
996 TpAccount *account = TP_ACCOUNT (src);
997 GSimpleAsyncResult *set_result = user_data;
999 tp_account_set_display_name_finish (account, res, &error);
1003 g_simple_async_result_set_from_error (set_result, error);
1004 g_error_free (error);
1007 g_simple_async_result_complete (set_result);
1008 g_object_unref (set_result);
1012 empathy_account_settings_set_display_name_async (
1013 EmpathyAccountSettings *settings,
1015 GAsyncReadyCallback callback,
1018 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1019 GSimpleAsyncResult *result;
1021 g_return_if_fail (name != NULL);
1023 result = g_simple_async_result_new (G_OBJECT (settings),
1024 callback, user_data, empathy_account_settings_set_display_name_finish);
1026 if (!tp_strdiff (name, priv->display_name))
1029 g_simple_async_result_complete_in_idle (result);
1033 if (priv->account == NULL)
1035 if (priv->display_name != NULL)
1036 g_free (priv->display_name);
1038 priv->display_name = g_strdup (name);
1040 g_simple_async_result_complete_in_idle (result);
1045 tp_account_set_display_name_async (priv->account, name,
1046 account_settings_display_name_set_cb, result);
1050 empathy_account_settings_set_display_name_finish (
1051 EmpathyAccountSettings *settings,
1052 GAsyncResult *result,
1055 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1059 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1060 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1067 account_settings_icon_name_set_cb (GObject *src,
1071 GError *error = NULL;
1072 TpAccount *account = TP_ACCOUNT (src);
1073 GSimpleAsyncResult *set_result = user_data;
1075 tp_account_set_icon_name_finish (account, res, &error);
1079 g_simple_async_result_set_from_error (set_result, error);
1080 g_error_free (error);
1083 g_simple_async_result_complete (set_result);
1084 g_object_unref (set_result);
1088 empathy_account_settings_set_icon_name_async (
1089 EmpathyAccountSettings *settings,
1091 GAsyncReadyCallback callback,
1094 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1095 GSimpleAsyncResult *result;
1097 g_return_if_fail (name != NULL);
1099 result = g_simple_async_result_new (G_OBJECT (settings),
1100 callback, user_data, empathy_account_settings_set_icon_name_finish);
1102 if (priv->account == NULL)
1104 if (priv->icon_name != NULL)
1105 g_free (priv->icon_name);
1107 priv->icon_name = g_strdup (name);
1109 g_simple_async_result_complete_in_idle (result);
1114 tp_account_set_icon_name_async (priv->account, name,
1115 account_settings_icon_name_set_cb, result);
1119 empathy_account_settings_set_icon_name_finish (
1120 EmpathyAccountSettings *settings,
1121 GAsyncResult *result,
1124 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1128 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1129 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1136 empathy_account_settings_account_updated (GObject *source,
1137 GAsyncResult *result,
1140 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1141 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1142 GSimpleAsyncResult *r;
1143 GError *error = NULL;
1145 if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1146 result, NULL, &error))
1148 g_simple_async_result_set_from_error (priv->apply_result, error);
1149 g_error_free (error);
1153 empathy_account_settings_discard_changes (settings);
1156 r = priv->apply_result;
1157 priv->apply_result = NULL;
1159 g_simple_async_result_complete (r);
1164 empathy_account_settings_created_cb (GObject *source,
1165 GAsyncResult *result,
1168 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1169 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1171 GError *error = NULL;
1172 GSimpleAsyncResult *r;
1174 account = tp_account_manager_create_account_finish (
1175 TP_ACCOUNT_MANAGER (source), result, &error);
1177 if (account == NULL)
1179 g_simple_async_result_set_from_error (priv->apply_result, error);
1183 priv->account = g_object_ref (account);
1184 empathy_account_settings_discard_changes (settings);
1187 r = priv->apply_result;
1188 priv->apply_result = NULL;
1190 g_simple_async_result_complete (r);
1196 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1198 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1199 GHashTable *properties;
1200 TpConnectionPresenceType type;
1205 properties = tp_asv_new (NULL, NULL);
1207 idle = empathy_idle_dup_singleton ();
1208 type = empathy_idle_get_requested_presence (idle, &status, &message);
1209 g_object_unref (idle);
1211 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1213 /* Create the account with the requested presence the same as the current
1214 * global requested presence, but don't enable it */
1215 GValueArray *presence;
1216 GValue vtype = { 0, };
1217 GValue vstatus = { 0, };
1218 GValue vmessage = { 0, };
1220 presence = g_value_array_new (3);
1222 g_value_init (&vtype, G_TYPE_UINT);
1223 g_value_set_uint (&vtype, type);
1224 g_value_array_append (presence, &vtype);
1226 g_value_init (&vstatus, G_TYPE_STRING);
1227 g_value_take_string (&vstatus, status);
1228 g_value_array_append (presence, &vstatus);
1230 g_value_init (&vmessage, G_TYPE_STRING);
1231 g_value_take_string (&vmessage, message);
1232 g_value_array_append (presence, &vmessage);
1234 tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1235 TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1238 tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1241 if (priv->service != NULL)
1242 tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1244 tp_account_manager_create_account_async (priv->account_manager,
1245 priv->cm_name, priv->protocol, priv->display_name,
1246 priv->parameters, properties,
1247 empathy_account_settings_created_cb,
1250 g_hash_table_unref (properties);
1254 empathy_account_settings_manager_ready_cb (GObject *source_object,
1255 GAsyncResult *result,
1258 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1259 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1260 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1261 GError *error = NULL;
1263 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1265 DEBUG ("Failed to prepare account manager: %s", error->message);
1266 g_error_free (error);
1270 g_assert (priv->apply_result != NULL && priv->account == NULL);
1271 empathy_account_settings_do_create_account (settings);
1275 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1276 GAsyncReadyCallback callback,
1279 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1281 if (priv->apply_result != NULL)
1283 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1284 callback, user_data,
1285 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1289 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1290 callback, user_data, empathy_account_settings_apply_finish);
1292 if (priv->account == NULL)
1294 tp_account_manager_prepare_async (priv->account_manager, NULL,
1295 empathy_account_settings_manager_ready_cb, settings);
1299 tp_account_update_parameters_async (priv->account,
1300 priv->parameters, (const gchar **)priv->unset_parameters->data,
1301 empathy_account_settings_account_updated, settings);
1306 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1307 GAsyncResult *result,
1310 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1314 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1315 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1321 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1324 EmpathyAccountSettingsPriv *priv;
1325 const gchar *account_path;
1326 const gchar *priv_account_path;
1328 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1329 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1331 priv = GET_PRIV (settings);
1333 if (priv->account == NULL)
1336 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1337 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1339 return (!tp_strdiff (account_path, priv_account_path));
1343 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1345 EmpathyAccountSettingsPriv *priv;
1348 gboolean missed = FALSE;
1350 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1352 priv = GET_PRIV (settings);
1354 for (idx = 0; idx < priv->required_params->len; idx++)
1356 current = g_array_index (priv->required_params, gchar *, idx);
1358 /* first, look if it's set in our own parameters */
1359 if (tp_asv_lookup (priv->parameters, current))
1362 /* if we did not unset the parameter, look if it's in the account */
1363 if (priv->account != NULL &&
1364 !empathy_account_settings_is_unset (settings, current))
1366 const GHashTable *account_params;
1368 account_params = tp_account_get_parameters (priv->account);
1369 if (tp_asv_lookup (account_params, current))
1380 const TpConnectionManagerProtocol *
1381 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1383 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1385 return tp_connection_manager_get_protocol (priv->manager, priv->protocol);