2 * tpaw-account-settings.c - Source for TpawAccountSettings
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
22 #include "tpaw-account-settings.h"
24 #include "tpaw-connection-managers.h"
25 #include "tpaw-keyring.h"
26 #include "empathy-presence-manager.h"
27 #include "empathy-utils.h"
28 #include "tpaw-utils.h"
30 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
31 #include "empathy-debug.h"
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, TpawAccountSettings)
35 G_DEFINE_TYPE(TpawAccountSettings, tpaw_account_settings, G_TYPE_OBJECT)
43 PROP_DISPLAY_NAME_OVERRIDDEN,
48 PASSWORD_RETRIEVED = 1,
52 static gulong signals[LAST_SIGNAL] = { 0, };
54 /* private structure */
55 typedef struct _TpawAccountSettingsPriv TpawAccountSettingsPriv;
57 struct _TpawAccountSettingsPriv
59 gboolean dispose_has_run;
60 TpawConnectionManagers *managers;
61 TpAccountManager *account_manager;
63 TpConnectionManager *manager;
64 TpProtocol *protocol_obj;
72 gchar *storage_provider;
73 gboolean display_name_overridden;
76 gboolean supports_sasl;
77 gboolean remember_password;
80 gchar *password_original;
82 gboolean password_retrieved;
83 gboolean password_requested;
85 /* Parameter name (gchar *) -> parameter value (GVariant) */
86 GHashTable *parameters;
87 /* Keys are parameter names from the hash above (gchar *).
88 * Values are regular expresions that should match corresponding parameter
89 * values (GRegex *). Possible regexp patterns are defined in
90 * tpaw-account-widget.c */
91 GHashTable *param_regexps;
92 GArray *unset_parameters;
93 GList *required_params;
95 gulong managers_ready_id;
96 gboolean preparing_protocol;
98 /* If TRUE, the account should have 'tel' in its
99 * Account.Interface.Addressing.URISchemes property. */
100 gboolean uri_scheme_tel;
101 /* If TRUE, Service property needs to be updated when applying changes */
102 gboolean update_service;
104 GSimpleAsyncResult *apply_result;
108 tpaw_account_settings_init (TpawAccountSettings *obj)
110 TpawAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
111 TPAW_TYPE_ACCOUNT_SETTINGS, TpawAccountSettingsPriv);
115 /* allocate any data required by the object here */
116 priv->managers = tpaw_connection_managers_dup_singleton ();
117 priv->account_manager = tp_account_manager_dup ();
119 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
120 g_free, (GDestroyNotify) g_variant_unref);
122 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
123 g_free, (GDestroyNotify) g_regex_unref);
125 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
127 priv->required_params = NULL;
130 static void tpaw_account_settings_dispose (GObject *object);
131 static void tpaw_account_settings_finalize (GObject *object);
132 static void tpaw_account_settings_account_ready_cb (GObject *source_object,
133 GAsyncResult *result, gpointer user_data);
134 static void tpaw_account_settings_managers_ready_cb (GObject *obj,
135 GParamSpec *pspec, gpointer user_data);
136 static void tpaw_account_settings_check_readyness (
137 TpawAccountSettings *self);
140 tpaw_account_settings_set_property (GObject *object,
145 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (object);
146 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
151 priv->account = g_value_dup_object (value);
154 priv->cm_name = g_value_dup_string (value);
157 priv->protocol = g_value_dup_string (value);
160 priv->service = g_value_dup_string (value);
162 case PROP_DISPLAY_NAME:
163 priv->display_name = g_value_dup_string (value);
165 case PROP_DISPLAY_NAME_OVERRIDDEN:
166 priv->display_name_overridden = g_value_get_boolean (value);
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 tpaw_account_settings_get_property (GObject *object,
180 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (object);
181 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
186 g_value_set_object (value, priv->account);
189 g_value_set_string (value, priv->cm_name);
192 g_value_set_string (value, priv->protocol);
195 g_value_set_string (value, priv->service);
197 case PROP_DISPLAY_NAME:
198 g_value_set_string (value, priv->display_name);
200 case PROP_DISPLAY_NAME_OVERRIDDEN:
201 g_value_set_boolean (value, priv->display_name_overridden);
204 g_value_set_boolean (value, priv->ready);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 tpaw_account_settings_constructed (GObject *object)
215 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
216 TpawAccountSettingsPriv *priv = GET_PRIV (self);
218 if (priv->account != NULL)
220 g_free (priv->cm_name);
221 g_free (priv->protocol);
222 g_free (priv->service);
225 g_strdup (tp_account_get_cm_name (priv->account));
227 g_strdup (tp_account_get_protocol_name (priv->account));
229 g_strdup (tp_account_get_service (priv->account));
230 priv->icon_name = g_strdup
231 (tp_account_get_icon_name (priv->account));
235 priv->icon_name = tpaw_protocol_icon_name (priv->protocol);
238 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
240 tpaw_account_settings_check_readyness (self);
244 GQuark features[] = {
245 TP_ACCOUNT_FEATURE_CORE,
246 TP_ACCOUNT_FEATURE_STORAGE,
247 TP_ACCOUNT_FEATURE_ADDRESSING,
250 if (priv->account != NULL)
252 tp_proxy_prepare_async (priv->account, features,
253 tpaw_account_settings_account_ready_cb, self);
256 tp_g_signal_connect_object (priv->managers, "notify::ready",
257 G_CALLBACK (tpaw_account_settings_managers_ready_cb), object, 0);
261 tpaw_account_settings_parent_class)->constructed != NULL)
263 tpaw_account_settings_parent_class)->constructed (object);
268 tpaw_account_settings_class_init (
269 TpawAccountSettingsClass *tpaw_account_settings_class)
271 GObjectClass *object_class = G_OBJECT_CLASS (tpaw_account_settings_class);
273 g_type_class_add_private (tpaw_account_settings_class, sizeof
274 (TpawAccountSettingsPriv));
276 object_class->dispose = tpaw_account_settings_dispose;
277 object_class->finalize = tpaw_account_settings_finalize;
278 object_class->set_property = tpaw_account_settings_set_property;
279 object_class->get_property = tpaw_account_settings_get_property;
280 object_class->constructed = tpaw_account_settings_constructed;
282 g_object_class_install_property (object_class, PROP_ACCOUNT,
283 g_param_spec_object ("account",
285 "The TpAccount backing these settings",
287 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
289 g_object_class_install_property (object_class, PROP_CM_NAME,
290 g_param_spec_string ("connection-manager",
291 "connection-manager",
292 "The name of the connection manager this account uses",
294 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
296 g_object_class_install_property (object_class, PROP_PROTOCOL,
297 g_param_spec_string ("protocol",
299 "The name of the protocol this account uses",
301 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
303 g_object_class_install_property (object_class, PROP_SERVICE,
304 g_param_spec_string ("service",
306 "The service of this account, or NULL",
308 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
310 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
311 g_param_spec_string ("display-name",
313 "The display name account these settings belong to",
315 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
317 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
318 g_param_spec_boolean ("display-name-overridden",
319 "display-name-overridden",
320 "Whether the display name for this account has been manually "
323 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
325 g_object_class_install_property (object_class, PROP_READY,
326 g_param_spec_boolean ("ready",
328 "Whether this account is ready to be used",
330 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
332 signals[PASSWORD_RETRIEVED] =
333 g_signal_new ("password-retrieved",
334 G_TYPE_FROM_CLASS (tpaw_account_settings_class),
335 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
336 g_cclosure_marshal_generic,
341 tpaw_account_settings_dispose (GObject *object)
343 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
344 TpawAccountSettingsPriv *priv = GET_PRIV (self);
346 if (priv->dispose_has_run)
349 priv->dispose_has_run = TRUE;
351 if (priv->managers_ready_id != 0)
352 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
353 priv->managers_ready_id = 0;
355 tp_clear_object (&priv->managers);
356 tp_clear_object (&priv->manager);
357 tp_clear_object (&priv->account_manager);
358 tp_clear_object (&priv->account);
359 tp_clear_object (&priv->protocol_obj);
361 /* release any references held by the object here */
362 if (G_OBJECT_CLASS (tpaw_account_settings_parent_class)->dispose)
363 G_OBJECT_CLASS (tpaw_account_settings_parent_class)->dispose (object);
367 tpaw_account_settings_free_unset_parameters (
368 TpawAccountSettings *settings)
370 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
373 for (i = 0 ; i < priv->unset_parameters->len; i++)
374 g_free (g_array_index (priv->unset_parameters, gchar *, i));
376 g_array_set_size (priv->unset_parameters, 0);
380 tpaw_account_settings_finalize (GObject *object)
382 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
383 TpawAccountSettingsPriv *priv = GET_PRIV (self);
386 /* free any data held directly by the object here */
387 g_free (priv->cm_name);
388 g_free (priv->protocol);
389 g_free (priv->service);
390 g_free (priv->display_name);
391 g_free (priv->icon_name);
392 g_free (priv->password);
393 g_free (priv->password_original);
394 g_free (priv->storage_provider);
396 if (priv->required_params != NULL)
398 for (l = priv->required_params; l; l = l->next)
400 g_list_free (priv->required_params);
403 g_hash_table_unref (priv->parameters);
404 g_hash_table_unref (priv->param_regexps);
406 tpaw_account_settings_free_unset_parameters (self);
407 g_array_unref (priv->unset_parameters);
409 G_OBJECT_CLASS (tpaw_account_settings_parent_class)->finalize (object);
413 tpaw_account_settings_protocol_obj_prepared_cb (GObject *source,
414 GAsyncResult *result,
417 TpawAccountSettings *self = user_data;
418 GError *error = NULL;
420 if (!tp_proxy_prepare_finish (source, result, &error))
422 DEBUG ("Failed to prepare protocol object: %s", error->message);
423 g_clear_error (&error);
427 tpaw_account_settings_check_readyness (self);
431 tpaw_account_settings_get_password_cb (GObject *source,
432 GAsyncResult *result,
435 TpawAccountSettings *self = user_data;
436 TpawAccountSettingsPriv *priv = GET_PRIV (self);
437 const gchar *password;
438 GError *error = NULL;
440 password = tpaw_keyring_get_account_password_finish (TP_ACCOUNT (source),
445 DEBUG ("Failed to get password: %s", error->message);
446 g_clear_error (&error);
449 /* It doesn't really matter if getting the password failed; that
450 * just means that it's not there, or let's act like that at
453 g_assert (priv->password == NULL);
455 priv->password = g_strdup (password);
456 priv->password_original = g_strdup (password);
458 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
461 static GVariant * tpaw_account_settings_dup (
462 TpawAccountSettings *settings,
466 tpaw_account_settings_check_readyness (TpawAccountSettings *self)
468 TpawAccountSettingsPriv *priv = GET_PRIV (self);
469 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
474 if (priv->account != NULL
475 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
478 if (!tpaw_connection_managers_is_ready (priv->managers))
481 if (priv->manager == NULL)
483 priv->manager = tpaw_connection_managers_get_cm (
484 priv->managers, priv->cm_name);
487 if (priv->manager == NULL)
490 g_object_ref (priv->manager);
492 if (priv->account != NULL)
494 g_free (priv->display_name);
496 g_strdup (tp_account_get_display_name (priv->account));
498 g_free (priv->icon_name);
500 g_strdup (tp_account_get_icon_name (priv->account));
502 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
505 if (priv->protocol_obj == NULL)
507 priv->protocol_obj = g_object_ref (
508 tp_connection_manager_get_protocol_object (priv->manager,
512 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
513 && !priv->preparing_protocol)
515 priv->preparing_protocol = TRUE;
516 tp_proxy_prepare_async (priv->protocol_obj, features,
517 tpaw_account_settings_protocol_obj_prepared_cb, self);
522 if (tp_strv_contains (tp_protocol_get_authentication_types (
524 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
526 priv->supports_sasl = TRUE;
530 if (priv->required_params == NULL)
534 params = tp_protocol_dup_params (priv->protocol_obj);
535 for (l = params; l != NULL; l = g_list_next (l))
537 TpConnectionManagerParam *cur = l->data;
539 if (tp_connection_manager_param_is_required (cur))
541 priv->required_params = g_list_append (priv->required_params,
542 g_strdup (tp_connection_manager_param_get_name (cur)));
546 g_list_free_full (params,
547 (GDestroyNotify) tp_connection_manager_param_free);
550 /* priv->account won't be a proper account if it's the account
551 * assistant showing this widget. */
552 if (priv->supports_sasl && !priv->password_requested
553 && priv->account != NULL)
555 priv->password_requested = TRUE;
557 /* Make this call but don't block on its readiness. We'll signal
558 * if it's updated later with ::password-retrieved. */
559 tpaw_keyring_get_account_password_async (priv->account,
560 tpaw_account_settings_get_password_cb, self);
564 g_object_notify (G_OBJECT (self), "ready");
568 tpaw_account_settings_account_ready_cb (GObject *source_object,
569 GAsyncResult *result,
572 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
573 TpAccount *account = TP_ACCOUNT (source_object);
574 GError *error = NULL;
576 if (!tp_proxy_prepare_finish (account, result, &error))
578 DEBUG ("Failed to prepare account: %s", error->message);
579 g_error_free (error);
583 tpaw_account_settings_check_readyness (settings);
587 tpaw_account_settings_managers_ready_cb (GObject *object,
591 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
593 tpaw_account_settings_check_readyness (settings);
596 TpawAccountSettings *
597 tpaw_account_settings_new (const gchar *connection_manager,
598 const gchar *protocol,
599 const gchar *service,
600 const char *display_name)
602 return g_object_new (TPAW_TYPE_ACCOUNT_SETTINGS,
603 "connection-manager", connection_manager,
604 "protocol", protocol,
606 "display-name", display_name,
610 TpawAccountSettings *
611 tpaw_account_settings_new_for_account (TpAccount *account)
613 return g_object_new (TPAW_TYPE_ACCOUNT_SETTINGS,
619 tpaw_account_settings_dup_tp_params (TpawAccountSettings *settings)
621 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
623 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
625 return tp_protocol_dup_params (priv->protocol_obj);
629 tpaw_account_settings_is_ready (TpawAccountSettings *settings)
631 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
637 tpaw_account_settings_get_cm (TpawAccountSettings *settings)
639 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
641 return priv->cm_name;
645 tpaw_account_settings_get_protocol (TpawAccountSettings *settings)
647 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
649 return priv->protocol;
653 tpaw_account_settings_get_service (TpawAccountSettings *settings)
655 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
657 return priv->service;
661 tpaw_account_settings_set_service (TpawAccountSettings *settings,
662 const gchar *service)
664 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
666 if (!tp_strdiff (priv->service, service))
669 g_free (priv->service);
670 priv->service = g_strdup (service);
671 g_object_notify (G_OBJECT (settings), "service");
672 priv->update_service = TRUE;
676 tpaw_account_settings_get_icon_name (TpawAccountSettings *settings)
678 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
680 return priv->icon_name;
684 tpaw_account_settings_get_display_name (TpawAccountSettings *settings)
686 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
688 return priv->display_name;
692 tpaw_account_settings_get_account (TpawAccountSettings *settings)
694 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
696 return priv->account;
700 tpaw_account_settings_is_unset (TpawAccountSettings *settings,
703 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
707 a = priv->unset_parameters;
709 for (i = 0; i < a->len; i++)
711 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
718 static const TpConnectionManagerParam *
719 tpaw_account_settings_get_tp_param (TpawAccountSettings *settings,
722 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
724 return tp_protocol_get_param (priv->protocol_obj, param);
728 tpaw_account_settings_have_tp_param (TpawAccountSettings *settings,
731 return (tpaw_account_settings_get_tp_param (settings, param) != NULL);
735 account_settings_remove_from_unset (TpawAccountSettings *settings,
738 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
742 for (idx = 0; idx < priv->unset_parameters->len; idx++)
744 val = g_array_index (priv->unset_parameters, gchar *, idx);
746 if (!tp_strdiff (val, param))
748 priv->unset_parameters =
749 g_array_remove_index (priv->unset_parameters, idx);
758 tpaw_account_settings_dup_default (TpawAccountSettings *settings,
761 const TpConnectionManagerParam *p;
763 p = tpaw_account_settings_get_tp_param (settings, param);
767 return tp_connection_manager_param_dup_default_variant (p);
771 tpaw_account_settings_get_dbus_signature (TpawAccountSettings *settings,
774 const TpConnectionManagerParam *p;
776 p = tpaw_account_settings_get_tp_param (settings, param);
781 return tp_connection_manager_param_get_dbus_signature (p);
785 tpaw_account_settings_dup (TpawAccountSettings *settings,
788 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
791 /* Lookup the update parameters we set */
792 result = g_hash_table_lookup (priv->parameters, param);
794 return g_variant_ref (result);
796 /* If the parameters isn't unset use the accounts setting if any */
797 if (priv->account != NULL
798 && !tpaw_account_settings_is_unset (settings, param))
800 GVariant *parameters;
802 parameters = tp_account_dup_parameters_vardict (priv->account);
803 result = g_variant_lookup_value (parameters, param, NULL);
804 g_variant_unref (parameters);
807 /* g_variant_lookup_value() is (transfer full) */
811 /* fallback to the default */
812 return tpaw_account_settings_dup_default (settings, param);
816 tpaw_account_settings_unset (TpawAccountSettings *settings,
819 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
821 if (tpaw_account_settings_is_unset (settings, param))
824 if (priv->supports_sasl && !tp_strdiff (param, "password"))
826 g_free (priv->password);
827 priv->password = NULL;
831 v = g_strdup (param);
833 g_array_append_val (priv->unset_parameters, v);
834 g_hash_table_remove (priv->parameters, param);
838 tpaw_account_settings_discard_changes (TpawAccountSettings *settings)
840 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
842 g_hash_table_remove_all (priv->parameters);
843 tpaw_account_settings_free_unset_parameters (settings);
845 g_free (priv->password);
846 priv->password = g_strdup (priv->password_original);
848 if (priv->account != NULL)
849 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
851 priv->uri_scheme_tel = FALSE;
855 tpaw_account_settings_dup_string (TpawAccountSettings *settings,
858 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
860 gchar *result = NULL;
862 if (!tp_strdiff (param, "password") && priv->supports_sasl)
864 return g_strdup (priv->password);
867 v = tpaw_account_settings_dup (settings, param);
871 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
872 result = g_variant_dup_string (v, NULL);
879 tpaw_account_settings_dup_strv (TpawAccountSettings *settings,
885 v = tpaw_account_settings_dup (settings, param);
889 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
890 result = g_variant_dup_strv (v, NULL);
897 tpaw_account_settings_get_int32 (TpawAccountSettings *settings,
903 v = tpaw_account_settings_dup (settings, param);
907 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
908 ret = g_variant_get_byte (v);
909 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
910 ret = g_variant_get_int32 (v);
911 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
912 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
914 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
915 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
916 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
917 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
922 tmp = g_variant_print (v, TRUE);
923 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
932 tpaw_account_settings_get_int64 (TpawAccountSettings *settings,
938 v = tpaw_account_settings_dup (settings, param);
942 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
943 ret = g_variant_get_byte (v);
944 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
945 ret = g_variant_get_int32 (v);
946 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
947 ret = g_variant_get_uint32 (v);
948 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
949 ret = g_variant_get_int64 (v);
950 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
951 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
956 tmp = g_variant_print (v, TRUE);
957 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
966 tpaw_account_settings_get_uint32 (TpawAccountSettings *settings,
972 v = tpaw_account_settings_dup (settings, param);
976 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
977 ret = g_variant_get_byte (v);
978 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
979 ret = MAX (0, g_variant_get_int32 (v));
980 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
981 ret = g_variant_get_uint32 (v);
982 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
983 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
984 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
985 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
990 tmp = g_variant_print (v, TRUE);
991 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1000 tpaw_account_settings_get_uint64 (TpawAccountSettings *settings,
1006 v = tpaw_account_settings_dup (settings, param);
1010 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1011 ret = g_variant_get_byte (v);
1012 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1013 ret = MAX (0, g_variant_get_int32 (v));
1014 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1015 ret = g_variant_get_uint32 (v);
1016 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1017 ret = MAX (0, g_variant_get_int64 (v));
1018 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1019 ret = g_variant_get_uint64 (v);
1024 tmp = g_variant_print (v, TRUE);
1025 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1030 g_variant_unref (v);
1035 tpaw_account_settings_get_boolean (TpawAccountSettings *settings,
1039 gboolean result = FALSE;
1041 v = tpaw_account_settings_dup (settings, param);
1045 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1046 result = g_variant_get_boolean (v);
1052 tpaw_account_settings_set (TpawAccountSettings *settings,
1056 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1058 g_return_if_fail (param != NULL);
1059 g_return_if_fail (v != NULL);
1061 if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1062 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1064 g_free (priv->password);
1065 priv->password = g_variant_dup_string (v, NULL);
1069 g_hash_table_insert (priv->parameters, g_strdup (param),
1070 g_variant_ref_sink (v));
1073 account_settings_remove_from_unset (settings, param);
1077 account_settings_display_name_set_cb (GObject *src,
1081 GError *error = NULL;
1082 TpAccount *account = TP_ACCOUNT (src);
1083 GSimpleAsyncResult *set_result = user_data;
1085 tp_account_set_display_name_finish (account, res, &error);
1089 g_simple_async_result_set_from_error (set_result, error);
1090 g_error_free (error);
1093 g_simple_async_result_complete (set_result);
1094 g_object_unref (set_result);
1098 tpaw_account_settings_set_display_name_async (
1099 TpawAccountSettings *settings,
1101 GAsyncReadyCallback callback,
1104 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1105 GSimpleAsyncResult *result;
1107 g_return_if_fail (name != NULL);
1109 result = g_simple_async_result_new (G_OBJECT (settings),
1110 callback, user_data, tpaw_account_settings_set_display_name_finish);
1112 if (!tp_strdiff (name, priv->display_name))
1115 g_simple_async_result_complete_in_idle (result);
1119 g_free (priv->display_name);
1120 priv->display_name = g_strdup (name);
1122 if (priv->account == NULL)
1124 g_simple_async_result_complete_in_idle (result);
1128 tp_account_set_display_name_async (priv->account, name,
1129 account_settings_display_name_set_cb, result);
1133 tpaw_account_settings_set_display_name_finish (
1134 TpawAccountSettings *settings,
1135 GAsyncResult *result,
1138 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1142 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1143 G_OBJECT (settings), tpaw_account_settings_set_display_name_finish),
1150 account_settings_icon_name_set_cb (GObject *src,
1154 GError *error = NULL;
1155 TpAccount *account = TP_ACCOUNT (src);
1156 GSimpleAsyncResult *set_result = user_data;
1158 tp_account_set_icon_name_finish (account, res, &error);
1162 g_simple_async_result_set_from_error (set_result, error);
1163 g_error_free (error);
1166 g_simple_async_result_complete (set_result);
1167 g_object_unref (set_result);
1171 tpaw_account_settings_set_icon_name_async (
1172 TpawAccountSettings *settings,
1174 GAsyncReadyCallback callback,
1177 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1178 GSimpleAsyncResult *result;
1180 g_return_if_fail (name != NULL);
1182 result = g_simple_async_result_new (G_OBJECT (settings),
1183 callback, user_data, tpaw_account_settings_set_icon_name_finish);
1185 if (priv->account == NULL)
1187 if (priv->icon_name != NULL)
1188 g_free (priv->icon_name);
1190 priv->icon_name = g_strdup (name);
1192 g_simple_async_result_complete_in_idle (result);
1197 tp_account_set_icon_name_async (priv->account, name,
1198 account_settings_icon_name_set_cb, result);
1202 tpaw_account_settings_set_icon_name_finish (
1203 TpawAccountSettings *settings,
1204 GAsyncResult *result,
1207 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1211 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1212 G_OBJECT (settings), tpaw_account_settings_set_icon_name_finish),
1219 tpaw_account_settings_processed_password (GObject *source,
1220 GAsyncResult *result,
1222 gpointer finish_func)
1224 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
1225 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1226 GSimpleAsyncResult *r;
1227 GError *error = NULL;
1228 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1231 g_free (priv->password_original);
1232 priv->password_original = g_strdup (priv->password);
1234 if (!func (TP_ACCOUNT (source), result, &error))
1236 g_simple_async_result_set_from_error (priv->apply_result, error);
1237 g_error_free (error);
1240 tpaw_account_settings_discard_changes (settings);
1242 r = priv->apply_result;
1243 priv->apply_result = NULL;
1245 g_simple_async_result_complete (r);
1250 tpaw_account_settings_set_password_cb (GObject *source,
1251 GAsyncResult *result,
1254 tpaw_account_settings_processed_password (source, result, user_data,
1255 tpaw_keyring_set_account_password_finish);
1259 tpaw_account_settings_delete_password_cb (GObject *source,
1260 GAsyncResult *result,
1263 tpaw_account_settings_processed_password (source, result, user_data,
1264 tpaw_keyring_delete_account_password_finish);
1268 update_account_uri_schemes (TpawAccountSettings *self)
1270 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1272 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1276 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1277 priv->uri_scheme_tel, NULL, NULL);
1281 set_service_cb (GObject *source,
1282 GAsyncResult *result,
1285 GError *error = NULL;
1287 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1289 DEBUG ("Failed to set Account.Service: %s", error->message);
1290 g_error_free (error);
1295 update_account_service (TpawAccountSettings *self)
1297 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1299 if (!priv->update_service)
1302 tp_account_set_service_async (priv->account,
1303 priv->service != NULL ? priv->service : "", set_service_cb, self);
1307 tpaw_account_settings_account_updated (GObject *source,
1308 GAsyncResult *result,
1311 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
1312 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1313 GSimpleAsyncResult *r;
1314 GError *error = NULL;
1315 GStrv reconnect_required = NULL;
1317 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1318 result, &reconnect_required, &error))
1320 g_simple_async_result_set_from_error (priv->apply_result, error);
1321 g_error_free (error);
1325 update_account_uri_schemes (settings);
1326 update_account_service (settings);
1328 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1329 g_strv_length (reconnect_required) > 0);
1331 /* Only set the password in the keyring if the CM supports SASL. */
1332 if (priv->supports_sasl)
1334 if (priv->password != NULL)
1336 /* FIXME: we shouldn't save the password if we
1337 * can't (MaySaveResponse=False) but we don't have API to check that
1338 * at this point (fdo #35382). */
1339 tpaw_keyring_set_account_password_async (priv->account,
1340 priv->password, priv->remember_password,
1341 tpaw_account_settings_set_password_cb, settings);
1345 tpaw_keyring_delete_account_password_async (priv->account,
1346 tpaw_account_settings_delete_password_cb, settings);
1353 tpaw_account_settings_discard_changes (settings);
1355 r = priv->apply_result;
1356 priv->apply_result = NULL;
1358 g_simple_async_result_complete (r);
1360 g_strfreev (reconnect_required);
1364 tpaw_account_settings_created_cb (GObject *source,
1365 GAsyncResult *result,
1368 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
1369 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1370 GError *error = NULL;
1371 GSimpleAsyncResult *r;
1373 priv->account = tp_account_request_create_account_finish (
1374 TP_ACCOUNT_REQUEST (source), result, &error);
1376 if (priv->account == NULL)
1378 g_simple_async_result_set_from_error (priv->apply_result, error);
1382 if (priv->supports_sasl && priv->password != NULL)
1384 /* Save the password before connecting */
1385 /* FIXME: we shouldn't save the password if we
1386 * can't (MaySaveResponse=False) but we don't have API to check that
1387 * at this point (fdo #35382). */
1388 tpaw_keyring_set_account_password_async (priv->account,
1389 priv->password, priv->remember_password,
1390 tpaw_account_settings_set_password_cb,
1395 update_account_uri_schemes (settings);
1397 tpaw_account_settings_discard_changes (settings);
1400 r = priv->apply_result;
1401 priv->apply_result = NULL;
1403 g_simple_async_result_complete (r);
1408 tpaw_account_settings_do_create_account (TpawAccountSettings *self)
1410 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1411 TpAccountRequest *account_req;
1412 TpConnectionPresenceType type;
1415 EmpathyPresenceManager *presence_mgr;
1416 GHashTableIter iter;
1419 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1420 priv->protocol, "New Account");
1422 presence_mgr = empathy_presence_manager_dup_singleton ();
1423 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1425 g_object_unref (presence_mgr);
1427 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1429 tp_account_request_set_requested_presence (account_req, type,
1433 tp_account_request_set_icon_name (account_req, priv->icon_name);
1435 tp_account_request_set_display_name (account_req, priv->display_name);
1437 if (priv->service != NULL)
1438 tp_account_request_set_service (account_req, priv->service);
1440 g_hash_table_iter_init (&iter, priv->parameters);
1441 while (g_hash_table_iter_next (&iter, &k, &v))
1443 const gchar *key = k;
1444 GVariant *value = v;
1446 tp_account_request_set_parameter (account_req, key, value);
1449 if (priv->storage_provider != NULL)
1451 tp_account_request_set_storage_provider (account_req,
1452 priv->storage_provider);
1455 tp_account_request_create_account_async (account_req,
1456 tpaw_account_settings_created_cb, self);
1460 build_parameters_variant (TpawAccountSettings *self)
1462 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1463 GVariantBuilder *builder;
1464 GHashTableIter iter;
1467 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1469 g_hash_table_iter_init (&iter, priv->parameters);
1470 while (g_hash_table_iter_next (&iter, &k, &v))
1472 const gchar *key = k;
1473 GVariant *value = v;
1476 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1477 g_variant_new_variant (value));
1479 g_variant_builder_add_value (builder, entry);
1482 return g_variant_builder_end (builder);
1486 tpaw_account_settings_apply_async (TpawAccountSettings *settings,
1487 GAsyncReadyCallback callback,
1490 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1492 if (priv->apply_result != NULL)
1494 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1495 callback, user_data,
1496 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1500 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1501 callback, user_data, tpaw_account_settings_apply_finish);
1503 /* We'll have to reconnect only if we change none DBus_Property on an
1504 * existing account. */
1505 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1507 if (priv->account == NULL)
1509 g_assert (priv->apply_result != NULL && priv->account == NULL);
1511 tpaw_account_settings_do_create_account (settings);
1515 tp_account_update_parameters_vardict_async (priv->account,
1516 build_parameters_variant (settings),
1517 (const gchar **) priv->unset_parameters->data,
1518 tpaw_account_settings_account_updated, settings);
1523 tpaw_account_settings_apply_finish (TpawAccountSettings *settings,
1524 GAsyncResult *result,
1525 gboolean *reconnect_required,
1528 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1532 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1533 G_OBJECT (settings), tpaw_account_settings_apply_finish), FALSE);
1535 if (reconnect_required != NULL)
1536 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1537 G_SIMPLE_ASYNC_RESULT (result));
1543 tpaw_account_settings_has_account (TpawAccountSettings *settings,
1546 TpawAccountSettingsPriv *priv;
1547 const gchar *account_path;
1548 const gchar *priv_account_path;
1550 g_return_val_if_fail (TPAW_IS_ACCOUNT_SETTINGS (settings), FALSE);
1551 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1553 priv = GET_PRIV (settings);
1555 if (priv->account == NULL)
1558 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1559 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1561 return (!tp_strdiff (account_path, priv_account_path));
1565 tpaw_account_settings_set_regex (TpawAccountSettings *settings,
1567 const gchar *pattern)
1569 TpawAccountSettingsPriv *priv = GET_PRIV (settings);
1571 GError *error = NULL;
1573 regex = g_regex_new (pattern, 0, 0, &error);
1576 g_warning ("Failed to create reg exp: %s", error->message);
1577 g_error_free (error);
1581 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1585 tpaw_account_settings_parameter_is_valid (
1586 TpawAccountSettings *settings,
1589 TpawAccountSettingsPriv *priv;
1590 const GRegex *regex;
1592 g_return_val_if_fail (TPAW_IS_ACCOUNT_SETTINGS (settings), FALSE);
1594 priv = GET_PRIV (settings);
1596 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1598 /* first, look if it's set in our own parameters */
1599 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1602 /* if we did not unset the parameter, look if it's in the account */
1603 if (priv->account != NULL &&
1604 !tpaw_account_settings_is_unset (settings, param))
1606 const GHashTable *account_params;
1608 account_params = tp_account_get_parameters (priv->account);
1609 if (tp_asv_lookup (account_params, param))
1617 /* test whether parameter value matches its regex */
1618 regex = g_hash_table_lookup (priv->param_regexps, param);
1624 value = tpaw_account_settings_dup_string (settings, param);
1628 match = g_regex_match (regex, value, 0, NULL);
1638 tpaw_account_settings_is_valid (TpawAccountSettings *settings)
1640 TpawAccountSettingsPriv *priv;
1642 GHashTableIter iter;
1645 g_return_val_if_fail (TPAW_IS_ACCOUNT_SETTINGS (settings), FALSE);
1647 priv = GET_PRIV (settings);
1649 for (l = priv->required_params; l; l = l->next)
1651 if (!tpaw_account_settings_parameter_is_valid (settings, l->data))
1655 g_hash_table_iter_init (&iter, priv->param_regexps);
1656 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1658 if (!tpaw_account_settings_parameter_is_valid (settings, param))
1666 tpaw_account_settings_get_tp_protocol (TpawAccountSettings *self)
1668 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1670 return priv->protocol_obj;
1674 tpaw_account_settings_supports_sasl (TpawAccountSettings *self)
1676 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1678 return priv->supports_sasl;
1682 tpaw_account_settings_param_is_supported (TpawAccountSettings *self,
1685 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1687 return tp_protocol_has_param (priv->protocol_obj, param);
1691 tpaw_account_settings_set_uri_scheme_tel (TpawAccountSettings *self,
1694 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1696 priv->uri_scheme_tel = associate;
1700 tpaw_account_settings_has_uri_scheme_tel (
1701 TpawAccountSettings *self)
1703 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1705 return priv->uri_scheme_tel;
1709 tpaw_account_settings_set_storage_provider (TpawAccountSettings *self,
1710 const gchar *storage)
1712 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1714 g_free (priv->storage_provider);
1715 priv->storage_provider = g_strdup (storage);
1719 tpaw_account_settings_set_remember_password (TpawAccountSettings *self,
1722 TpawAccountSettingsPriv *priv = GET_PRIV (self);
1724 priv->remember_password = remember;