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-utils.h"
27 #include "tpaw-utils.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
30 #include "empathy-debug.h"
32 G_DEFINE_TYPE(TpawAccountSettings, tpaw_account_settings, G_TYPE_OBJECT)
40 PROP_DISPLAY_NAME_OVERRIDDEN,
45 PASSWORD_RETRIEVED = 1,
49 static gulong signals[LAST_SIGNAL] = { 0, };
51 struct _TpawAccountSettingsPriv
53 gboolean dispose_has_run;
54 TpawConnectionManagers *managers;
55 TpAccountManager *account_manager;
57 TpConnectionManager *manager;
58 TpProtocol *protocol_obj;
66 gchar *storage_provider;
67 gboolean display_name_overridden;
70 gboolean supports_sasl;
71 gboolean remember_password;
74 gchar *password_original;
76 gboolean password_retrieved;
77 gboolean password_requested;
79 /* Parameter name (gchar *) -> parameter value (GVariant) */
80 GHashTable *parameters;
81 /* Keys are parameter names from the hash above (gchar *).
82 * Values are regular expresions that should match corresponding parameter
83 * values (GRegex *). Possible regexp patterns are defined in
84 * tpaw-account-widget.c */
85 GHashTable *param_regexps;
86 GArray *unset_parameters;
87 GList *required_params;
89 gulong managers_ready_id;
90 gboolean preparing_protocol;
92 /* If TRUE, the account should have 'tel' in its
93 * Account.Interface.Addressing.URISchemes property. */
94 gboolean uri_scheme_tel;
95 /* If TRUE, Service property needs to be updated when applying changes */
96 gboolean update_service;
98 GSimpleAsyncResult *apply_result;
102 tpaw_account_settings_init (TpawAccountSettings *obj)
104 obj->priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
105 TPAW_TYPE_ACCOUNT_SETTINGS, TpawAccountSettingsPriv);
107 /* allocate any data required by the object here */
108 obj->priv->managers = tpaw_connection_managers_dup_singleton ();
109 obj->priv->account_manager = tp_account_manager_dup ();
111 obj->priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
112 g_free, (GDestroyNotify) g_variant_unref);
114 obj->priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
115 g_free, (GDestroyNotify) g_regex_unref);
117 obj->priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
119 obj->priv->required_params = NULL;
122 static void tpaw_account_settings_dispose (GObject *object);
123 static void tpaw_account_settings_finalize (GObject *object);
124 static void tpaw_account_settings_account_ready_cb (GObject *source_object,
125 GAsyncResult *result, gpointer user_data);
126 static void tpaw_account_settings_managers_ready_cb (GObject *obj,
127 GParamSpec *pspec, gpointer user_data);
128 static void tpaw_account_settings_check_readyness (
129 TpawAccountSettings *self);
132 tpaw_account_settings_set_property (GObject *object,
137 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
142 self->priv->account = g_value_dup_object (value);
145 self->priv->cm_name = g_value_dup_string (value);
148 self->priv->protocol = g_value_dup_string (value);
151 self->priv->service = g_value_dup_string (value);
153 case PROP_DISPLAY_NAME:
154 self->priv->display_name = g_value_dup_string (value);
156 case PROP_DISPLAY_NAME_OVERRIDDEN:
157 self->priv->display_name_overridden = g_value_get_boolean (value);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 tpaw_account_settings_get_property (GObject *object,
171 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
176 g_value_set_object (value, self->priv->account);
179 g_value_set_string (value, self->priv->cm_name);
182 g_value_set_string (value, self->priv->protocol);
185 g_value_set_string (value, self->priv->service);
187 case PROP_DISPLAY_NAME:
188 g_value_set_string (value, self->priv->display_name);
190 case PROP_DISPLAY_NAME_OVERRIDDEN:
191 g_value_set_boolean (value, self->priv->display_name_overridden);
194 g_value_set_boolean (value, self->priv->ready);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 tpaw_account_settings_constructed (GObject *object)
205 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
207 if (self->priv->account != NULL)
209 g_free (self->priv->cm_name);
210 g_free (self->priv->protocol);
211 g_free (self->priv->service);
213 self->priv->cm_name =
214 g_strdup (tp_account_get_cm_name (self->priv->account));
215 self->priv->protocol =
216 g_strdup (tp_account_get_protocol_name (self->priv->account));
217 self->priv->service =
218 g_strdup (tp_account_get_service (self->priv->account));
219 self->priv->icon_name = g_strdup
220 (tp_account_get_icon_name (self->priv->account));
224 self->priv->icon_name = tpaw_protocol_icon_name (self->priv->protocol);
227 g_assert (self->priv->cm_name != NULL && self->priv->protocol != NULL);
229 tpaw_account_settings_check_readyness (self);
231 if (!self->priv->ready)
233 GQuark features[] = {
234 TP_ACCOUNT_FEATURE_CORE,
235 TP_ACCOUNT_FEATURE_STORAGE,
236 TP_ACCOUNT_FEATURE_ADDRESSING,
239 if (self->priv->account != NULL)
241 tp_proxy_prepare_async (self->priv->account, features,
242 tpaw_account_settings_account_ready_cb, self);
245 tp_g_signal_connect_object (self->priv->managers, "notify::ready",
246 G_CALLBACK (tpaw_account_settings_managers_ready_cb), object, 0);
250 tpaw_account_settings_parent_class)->constructed != NULL)
252 tpaw_account_settings_parent_class)->constructed (object);
257 tpaw_account_settings_class_init (
258 TpawAccountSettingsClass *tpaw_account_settings_class)
260 GObjectClass *object_class = G_OBJECT_CLASS (tpaw_account_settings_class);
262 g_type_class_add_private (tpaw_account_settings_class, sizeof
263 (TpawAccountSettingsPriv));
265 object_class->dispose = tpaw_account_settings_dispose;
266 object_class->finalize = tpaw_account_settings_finalize;
267 object_class->set_property = tpaw_account_settings_set_property;
268 object_class->get_property = tpaw_account_settings_get_property;
269 object_class->constructed = tpaw_account_settings_constructed;
271 g_object_class_install_property (object_class, PROP_ACCOUNT,
272 g_param_spec_object ("account",
274 "The TpAccount backing these settings",
276 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
278 g_object_class_install_property (object_class, PROP_CM_NAME,
279 g_param_spec_string ("connection-manager",
280 "connection-manager",
281 "The name of the connection manager this account uses",
283 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
285 g_object_class_install_property (object_class, PROP_PROTOCOL,
286 g_param_spec_string ("protocol",
288 "The name of the protocol this account uses",
290 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
292 g_object_class_install_property (object_class, PROP_SERVICE,
293 g_param_spec_string ("service",
295 "The service of this account, or NULL",
297 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
299 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
300 g_param_spec_string ("display-name",
302 "The display name account these settings belong to",
304 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
306 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
307 g_param_spec_boolean ("display-name-overridden",
308 "display-name-overridden",
309 "Whether the display name for this account has been manually "
312 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
314 g_object_class_install_property (object_class, PROP_READY,
315 g_param_spec_boolean ("ready",
317 "Whether this account is ready to be used",
319 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
321 signals[PASSWORD_RETRIEVED] =
322 g_signal_new ("password-retrieved",
323 G_TYPE_FROM_CLASS (tpaw_account_settings_class),
324 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
325 g_cclosure_marshal_generic,
330 tpaw_account_settings_dispose (GObject *object)
332 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
334 if (self->priv->dispose_has_run)
337 self->priv->dispose_has_run = TRUE;
339 if (self->priv->managers_ready_id != 0)
340 g_signal_handler_disconnect (self->priv->managers,
341 self->priv->managers_ready_id);
342 self->priv->managers_ready_id = 0;
344 tp_clear_object (&self->priv->managers);
345 tp_clear_object (&self->priv->manager);
346 tp_clear_object (&self->priv->account_manager);
347 tp_clear_object (&self->priv->account);
348 tp_clear_object (&self->priv->protocol_obj);
350 /* release any references held by the object here */
351 if (G_OBJECT_CLASS (tpaw_account_settings_parent_class)->dispose)
352 G_OBJECT_CLASS (tpaw_account_settings_parent_class)->dispose (object);
356 tpaw_account_settings_free_unset_parameters (
357 TpawAccountSettings *settings)
361 for (i = 0 ; i < settings->priv->unset_parameters->len; i++)
362 g_free (g_array_index (settings->priv->unset_parameters, gchar *, i));
364 g_array_set_size (settings->priv->unset_parameters, 0);
368 tpaw_account_settings_finalize (GObject *object)
370 TpawAccountSettings *self = TPAW_ACCOUNT_SETTINGS (object);
373 /* free any data held directly by the object here */
374 g_free (self->priv->cm_name);
375 g_free (self->priv->protocol);
376 g_free (self->priv->service);
377 g_free (self->priv->display_name);
378 g_free (self->priv->icon_name);
379 g_free (self->priv->password);
380 g_free (self->priv->password_original);
381 g_free (self->priv->storage_provider);
383 if (self->priv->required_params != NULL)
385 for (l = self->priv->required_params; l; l = l->next)
387 g_list_free (self->priv->required_params);
390 g_hash_table_unref (self->priv->parameters);
391 g_hash_table_unref (self->priv->param_regexps);
393 tpaw_account_settings_free_unset_parameters (self);
394 g_array_unref (self->priv->unset_parameters);
396 G_OBJECT_CLASS (tpaw_account_settings_parent_class)->finalize (object);
400 tpaw_account_settings_protocol_obj_prepared_cb (GObject *source,
401 GAsyncResult *result,
404 TpawAccountSettings *self = user_data;
405 GError *error = NULL;
407 if (!tp_proxy_prepare_finish (source, result, &error))
409 DEBUG ("Failed to prepare protocol object: %s", error->message);
410 g_clear_error (&error);
414 tpaw_account_settings_check_readyness (self);
418 tpaw_account_settings_get_password_cb (GObject *source,
419 GAsyncResult *result,
422 TpawAccountSettings *self = user_data;
423 const gchar *password;
424 GError *error = NULL;
426 password = tpaw_keyring_get_account_password_finish (TP_ACCOUNT (source),
431 DEBUG ("Failed to get password: %s", error->message);
432 g_clear_error (&error);
435 /* It doesn't really matter if getting the password failed; that
436 * just means that it's not there, or let's act like that at
439 g_assert (self->priv->password == NULL);
441 self->priv->password = g_strdup (password);
442 self->priv->password_original = g_strdup (password);
444 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
448 account_has_uri_scheme_tel (TpAccount *account)
450 return tp_account_associated_with_uri_scheme (account, "tel");
453 static GVariant * tpaw_account_settings_dup (
454 TpawAccountSettings *settings,
458 tpaw_account_settings_check_readyness (TpawAccountSettings *self)
460 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
462 if (self->priv->ready)
465 if (self->priv->account != NULL
466 && !tp_account_is_prepared (self->priv->account,
467 TP_ACCOUNT_FEATURE_CORE))
470 if (!tpaw_connection_managers_is_ready (self->priv->managers))
473 if (self->priv->manager == NULL)
475 self->priv->manager = tpaw_connection_managers_get_cm (
476 self->priv->managers, self->priv->cm_name);
479 if (self->priv->manager == NULL)
482 g_object_ref (self->priv->manager);
484 if (self->priv->account != NULL)
486 g_free (self->priv->display_name);
487 self->priv->display_name =
488 g_strdup (tp_account_get_display_name (self->priv->account));
490 g_free (self->priv->icon_name);
491 self->priv->icon_name =
492 g_strdup (tp_account_get_icon_name (self->priv->account));
494 self->priv->uri_scheme_tel = account_has_uri_scheme_tel (
495 self->priv->account);
498 if (self->priv->protocol_obj == NULL)
500 self->priv->protocol_obj = g_object_ref (
501 tp_connection_manager_get_protocol_object (self->priv->manager,
502 self->priv->protocol));
505 if (!tp_proxy_is_prepared (self->priv->protocol_obj,
506 TP_PROTOCOL_FEATURE_CORE)
507 && !self->priv->preparing_protocol)
509 self->priv->preparing_protocol = TRUE;
510 tp_proxy_prepare_async (self->priv->protocol_obj, features,
511 tpaw_account_settings_protocol_obj_prepared_cb, self);
516 if (tp_strv_contains (tp_protocol_get_authentication_types (
517 self->priv->protocol_obj),
518 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
520 self->priv->supports_sasl = TRUE;
524 if (self->priv->required_params == NULL)
528 params = tp_protocol_dup_params (self->priv->protocol_obj);
529 for (l = params; l != NULL; l = g_list_next (l))
531 TpConnectionManagerParam *cur = l->data;
533 if (tp_connection_manager_param_is_required (cur))
535 self->priv->required_params = g_list_append (
536 self->priv->required_params,
537 g_strdup (tp_connection_manager_param_get_name (cur)));
541 g_list_free_full (params,
542 (GDestroyNotify) tp_connection_manager_param_free);
545 /* self->priv->account won't be a proper account if it's the account
546 * assistant showing this widget. */
547 if (self->priv->supports_sasl && !self->priv->password_requested
548 && self->priv->account != NULL)
550 self->priv->password_requested = TRUE;
552 /* Make this call but don't block on its readiness. We'll signal
553 * if it's updated later with ::password-retrieved. */
554 tpaw_keyring_get_account_password_async (self->priv->account,
555 tpaw_account_settings_get_password_cb, self);
558 self->priv->ready = TRUE;
559 g_object_notify (G_OBJECT (self), "ready");
563 tpaw_account_settings_account_ready_cb (GObject *source_object,
564 GAsyncResult *result,
567 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
568 TpAccount *account = TP_ACCOUNT (source_object);
569 GError *error = NULL;
571 if (!tp_proxy_prepare_finish (account, result, &error))
573 DEBUG ("Failed to prepare account: %s", error->message);
574 g_error_free (error);
578 tpaw_account_settings_check_readyness (settings);
582 tpaw_account_settings_managers_ready_cb (GObject *object,
586 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
588 tpaw_account_settings_check_readyness (settings);
591 TpawAccountSettings *
592 tpaw_account_settings_new (const gchar *connection_manager,
593 const gchar *protocol,
594 const gchar *service,
595 const char *display_name)
597 return g_object_new (TPAW_TYPE_ACCOUNT_SETTINGS,
598 "connection-manager", connection_manager,
599 "protocol", protocol,
601 "display-name", display_name,
605 TpawAccountSettings *
606 tpaw_account_settings_new_for_account (TpAccount *account)
608 return g_object_new (TPAW_TYPE_ACCOUNT_SETTINGS,
614 tpaw_account_settings_dup_tp_params (TpawAccountSettings *settings)
616 g_return_val_if_fail (settings->priv->protocol_obj != NULL, NULL);
618 return tp_protocol_dup_params (settings->priv->protocol_obj);
622 tpaw_account_settings_is_ready (TpawAccountSettings *settings)
624 return settings->priv->ready;
628 tpaw_account_settings_get_cm (TpawAccountSettings *settings)
630 return settings->priv->cm_name;
634 tpaw_account_settings_get_protocol (TpawAccountSettings *settings)
636 return settings->priv->protocol;
640 tpaw_account_settings_get_service (TpawAccountSettings *settings)
642 return settings->priv->service;
646 tpaw_account_settings_set_service (TpawAccountSettings *settings,
647 const gchar *service)
649 if (!tp_strdiff (settings->priv->service, service))
652 g_free (settings->priv->service);
653 settings->priv->service = g_strdup (service);
654 g_object_notify (G_OBJECT (settings), "service");
655 settings->priv->update_service = TRUE;
659 tpaw_account_settings_get_icon_name (TpawAccountSettings *settings)
661 return settings->priv->icon_name;
665 tpaw_account_settings_get_display_name (TpawAccountSettings *settings)
667 return settings->priv->display_name;
671 tpaw_account_settings_get_account (TpawAccountSettings *settings)
673 return settings->priv->account;
677 tpaw_account_settings_is_unset (TpawAccountSettings *settings,
683 a = settings->priv->unset_parameters;
685 for (i = 0; i < a->len; i++)
687 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
694 static const TpConnectionManagerParam *
695 tpaw_account_settings_get_tp_param (TpawAccountSettings *settings,
698 return tp_protocol_get_param (settings->priv->protocol_obj, param);
702 tpaw_account_settings_have_tp_param (TpawAccountSettings *settings,
705 return (tpaw_account_settings_get_tp_param (settings, param) != NULL);
709 account_settings_remove_from_unset (TpawAccountSettings *settings,
715 for (idx = 0; idx < settings->priv->unset_parameters->len; idx++)
717 val = g_array_index (settings->priv->unset_parameters, gchar *, idx);
719 if (!tp_strdiff (val, param))
721 settings->priv->unset_parameters =
722 g_array_remove_index (settings->priv->unset_parameters, idx);
731 tpaw_account_settings_dup_default (TpawAccountSettings *settings,
734 const TpConnectionManagerParam *p;
736 p = tpaw_account_settings_get_tp_param (settings, param);
740 return tp_connection_manager_param_dup_default_variant (p);
744 tpaw_account_settings_get_dbus_signature (TpawAccountSettings *settings,
747 const TpConnectionManagerParam *p;
749 p = tpaw_account_settings_get_tp_param (settings, param);
754 return tp_connection_manager_param_get_dbus_signature (p);
758 tpaw_account_settings_dup (TpawAccountSettings *settings,
763 /* Lookup the update parameters we set */
764 result = g_hash_table_lookup (settings->priv->parameters, param);
766 return g_variant_ref (result);
768 /* If the parameters isn't unset use the accounts setting if any */
769 if (settings->priv->account != NULL
770 && !tpaw_account_settings_is_unset (settings, param))
772 GVariant *parameters;
774 parameters = tp_account_dup_parameters_vardict (
775 settings->priv->account);
776 result = g_variant_lookup_value (parameters, param, NULL);
777 g_variant_unref (parameters);
780 /* g_variant_lookup_value() is (transfer full) */
784 /* fallback to the default */
785 return tpaw_account_settings_dup_default (settings, param);
789 tpaw_account_settings_unset (TpawAccountSettings *settings,
793 if (tpaw_account_settings_is_unset (settings, param))
796 if (settings->priv->supports_sasl && !tp_strdiff (param, "password"))
798 g_free (settings->priv->password);
799 settings->priv->password = NULL;
803 v = g_strdup (param);
805 g_array_append_val (settings->priv->unset_parameters, v);
806 g_hash_table_remove (settings->priv->parameters, param);
810 tpaw_account_settings_discard_changes (TpawAccountSettings *settings)
812 g_hash_table_remove_all (settings->priv->parameters);
813 tpaw_account_settings_free_unset_parameters (settings);
815 g_free (settings->priv->password);
816 settings->priv->password = g_strdup (settings->priv->password_original);
818 if (settings->priv->account != NULL)
819 settings->priv->uri_scheme_tel = account_has_uri_scheme_tel (
820 settings->priv->account);
822 settings->priv->uri_scheme_tel = FALSE;
826 tpaw_account_settings_dup_string (TpawAccountSettings *settings,
830 gchar *result = NULL;
832 if (!tp_strdiff (param, "password") && settings->priv->supports_sasl)
834 return g_strdup (settings->priv->password);
837 v = tpaw_account_settings_dup (settings, param);
841 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
842 result = g_variant_dup_string (v, NULL);
849 tpaw_account_settings_dup_strv (TpawAccountSettings *settings,
855 v = tpaw_account_settings_dup (settings, param);
859 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
860 result = g_variant_dup_strv (v, NULL);
867 tpaw_account_settings_get_int32 (TpawAccountSettings *settings,
873 v = tpaw_account_settings_dup (settings, param);
877 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
878 ret = g_variant_get_byte (v);
879 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
880 ret = g_variant_get_int32 (v);
881 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
882 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
884 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
885 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
886 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
887 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
892 tmp = g_variant_print (v, TRUE);
893 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
902 tpaw_account_settings_get_int64 (TpawAccountSettings *settings,
908 v = tpaw_account_settings_dup (settings, param);
912 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
913 ret = g_variant_get_byte (v);
914 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
915 ret = g_variant_get_int32 (v);
916 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
917 ret = g_variant_get_uint32 (v);
918 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
919 ret = g_variant_get_int64 (v);
920 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
921 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
926 tmp = g_variant_print (v, TRUE);
927 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
936 tpaw_account_settings_get_uint32 (TpawAccountSettings *settings,
942 v = tpaw_account_settings_dup (settings, param);
946 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
947 ret = g_variant_get_byte (v);
948 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
949 ret = MAX (0, g_variant_get_int32 (v));
950 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
951 ret = g_variant_get_uint32 (v);
952 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
953 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
954 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
955 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
960 tmp = g_variant_print (v, TRUE);
961 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
970 tpaw_account_settings_get_uint64 (TpawAccountSettings *settings,
976 v = tpaw_account_settings_dup (settings, param);
980 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
981 ret = g_variant_get_byte (v);
982 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
983 ret = MAX (0, g_variant_get_int32 (v));
984 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
985 ret = g_variant_get_uint32 (v);
986 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
987 ret = MAX (0, g_variant_get_int64 (v));
988 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
989 ret = g_variant_get_uint64 (v);
994 tmp = g_variant_print (v, TRUE);
995 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1000 g_variant_unref (v);
1005 tpaw_account_settings_get_boolean (TpawAccountSettings *settings,
1009 gboolean result = FALSE;
1011 v = tpaw_account_settings_dup (settings, param);
1015 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1016 result = g_variant_get_boolean (v);
1022 tpaw_account_settings_set (TpawAccountSettings *settings,
1026 g_return_if_fail (param != NULL);
1027 g_return_if_fail (v != NULL);
1029 if (!tp_strdiff (param, "password") && settings->priv->supports_sasl &&
1030 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1032 g_free (settings->priv->password);
1033 settings->priv->password = g_variant_dup_string (v, NULL);
1037 g_hash_table_insert (settings->priv->parameters, g_strdup (param),
1038 g_variant_ref_sink (v));
1041 account_settings_remove_from_unset (settings, param);
1045 account_settings_display_name_set_cb (GObject *src,
1049 GError *error = NULL;
1050 TpAccount *account = TP_ACCOUNT (src);
1051 GSimpleAsyncResult *set_result = user_data;
1053 tp_account_set_display_name_finish (account, res, &error);
1057 g_simple_async_result_set_from_error (set_result, error);
1058 g_error_free (error);
1061 g_simple_async_result_complete (set_result);
1062 g_object_unref (set_result);
1066 tpaw_account_settings_set_display_name_async (
1067 TpawAccountSettings *settings,
1069 GAsyncReadyCallback callback,
1072 GSimpleAsyncResult *result;
1074 g_return_if_fail (name != NULL);
1076 result = g_simple_async_result_new (G_OBJECT (settings),
1077 callback, user_data, tpaw_account_settings_set_display_name_finish);
1079 if (!tp_strdiff (name, settings->priv->display_name))
1082 g_simple_async_result_complete_in_idle (result);
1086 g_free (settings->priv->display_name);
1087 settings->priv->display_name = g_strdup (name);
1089 if (settings->priv->account == NULL)
1091 g_simple_async_result_complete_in_idle (result);
1095 tp_account_set_display_name_async (settings->priv->account, name,
1096 account_settings_display_name_set_cb, result);
1100 tpaw_account_settings_set_display_name_finish (
1101 TpawAccountSettings *settings,
1102 GAsyncResult *result,
1105 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1109 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1110 G_OBJECT (settings), tpaw_account_settings_set_display_name_finish),
1117 account_settings_icon_name_set_cb (GObject *src,
1121 GError *error = NULL;
1122 TpAccount *account = TP_ACCOUNT (src);
1123 GSimpleAsyncResult *set_result = user_data;
1125 tp_account_set_icon_name_finish (account, res, &error);
1129 g_simple_async_result_set_from_error (set_result, error);
1130 g_error_free (error);
1133 g_simple_async_result_complete (set_result);
1134 g_object_unref (set_result);
1138 tpaw_account_settings_set_icon_name_async (
1139 TpawAccountSettings *settings,
1141 GAsyncReadyCallback callback,
1144 GSimpleAsyncResult *result;
1146 g_return_if_fail (name != NULL);
1148 result = g_simple_async_result_new (G_OBJECT (settings),
1149 callback, user_data, tpaw_account_settings_set_icon_name_finish);
1151 if (settings->priv->account == NULL)
1153 if (settings->priv->icon_name != NULL)
1154 g_free (settings->priv->icon_name);
1156 settings->priv->icon_name = g_strdup (name);
1158 g_simple_async_result_complete_in_idle (result);
1163 tp_account_set_icon_name_async (settings->priv->account, name,
1164 account_settings_icon_name_set_cb, result);
1168 tpaw_account_settings_set_icon_name_finish (
1169 TpawAccountSettings *settings,
1170 GAsyncResult *result,
1173 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1177 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1178 G_OBJECT (settings), tpaw_account_settings_set_icon_name_finish),
1185 tpaw_account_settings_processed_password (GObject *source,
1186 GAsyncResult *result,
1188 gpointer finish_func)
1190 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
1191 GSimpleAsyncResult *r;
1192 GError *error = NULL;
1193 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1196 g_free (settings->priv->password_original);
1197 settings->priv->password_original = g_strdup (settings->priv->password);
1199 if (!func (TP_ACCOUNT (source), result, &error))
1201 g_simple_async_result_set_from_error (settings->priv->apply_result,
1203 g_error_free (error);
1206 tpaw_account_settings_discard_changes (settings);
1208 r = settings->priv->apply_result;
1209 settings->priv->apply_result = NULL;
1211 g_simple_async_result_complete (r);
1216 tpaw_account_settings_set_password_cb (GObject *source,
1217 GAsyncResult *result,
1220 tpaw_account_settings_processed_password (source, result, user_data,
1221 tpaw_keyring_set_account_password_finish);
1225 tpaw_account_settings_delete_password_cb (GObject *source,
1226 GAsyncResult *result,
1229 tpaw_account_settings_processed_password (source, result, user_data,
1230 tpaw_keyring_delete_account_password_finish);
1234 update_account_uri_schemes (TpawAccountSettings *self)
1236 if (self->priv->uri_scheme_tel == account_has_uri_scheme_tel (
1237 self->priv->account))
1240 tp_account_set_uri_scheme_association_async (self->priv->account, "tel",
1241 self->priv->uri_scheme_tel, NULL, NULL);
1245 set_service_cb (GObject *source,
1246 GAsyncResult *result,
1249 GError *error = NULL;
1251 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1253 DEBUG ("Failed to set Account.Service: %s", error->message);
1254 g_error_free (error);
1259 update_account_service (TpawAccountSettings *self)
1261 if (!self->priv->update_service)
1264 tp_account_set_service_async (self->priv->account,
1265 self->priv->service != NULL ? self->priv->service : "",
1266 set_service_cb, self);
1270 tpaw_account_settings_account_updated (GObject *source,
1271 GAsyncResult *result,
1274 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
1275 GSimpleAsyncResult *r;
1276 GError *error = NULL;
1277 GStrv reconnect_required = NULL;
1279 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1280 result, &reconnect_required, &error))
1282 g_simple_async_result_set_from_error (settings->priv->apply_result,
1284 g_error_free (error);
1288 update_account_uri_schemes (settings);
1289 update_account_service (settings);
1291 g_simple_async_result_set_op_res_gboolean (settings->priv->apply_result,
1292 g_strv_length (reconnect_required) > 0);
1294 /* Only set the password in the keyring if the CM supports SASL. */
1295 if (settings->priv->supports_sasl)
1297 if (settings->priv->password != NULL)
1299 /* FIXME: we shouldn't save the password if we
1300 * can't (MaySaveResponse=False) but we don't have API to check that
1301 * at this point (fdo #35382). */
1302 tpaw_keyring_set_account_password_async (settings->priv->account,
1303 settings->priv->password, settings->priv->remember_password,
1304 tpaw_account_settings_set_password_cb, settings);
1308 tpaw_keyring_delete_account_password_async (
1309 settings->priv->account,
1310 tpaw_account_settings_delete_password_cb, settings);
1317 tpaw_account_settings_discard_changes (settings);
1319 r = settings->priv->apply_result;
1320 settings->priv->apply_result = NULL;
1322 g_simple_async_result_complete (r);
1324 g_strfreev (reconnect_required);
1328 tpaw_account_settings_created_cb (GObject *source,
1329 GAsyncResult *result,
1332 TpawAccountSettings *settings = TPAW_ACCOUNT_SETTINGS (user_data);
1333 GError *error = NULL;
1334 GSimpleAsyncResult *r;
1336 settings->priv->account = tp_account_request_create_account_finish (
1337 TP_ACCOUNT_REQUEST (source), result, &error);
1339 if (settings->priv->account == NULL)
1341 g_simple_async_result_set_from_error (settings->priv->apply_result,
1346 if (settings->priv->supports_sasl && settings->priv->password != NULL)
1348 /* Save the password before connecting */
1349 /* FIXME: we shouldn't save the password if we
1350 * can't (MaySaveResponse=False) but we don't have API to check that
1351 * at this point (fdo #35382). */
1352 tpaw_keyring_set_account_password_async (settings->priv->account,
1353 settings->priv->password, settings->priv->remember_password,
1354 tpaw_account_settings_set_password_cb,
1359 update_account_uri_schemes (settings);
1361 tpaw_account_settings_discard_changes (settings);
1364 r = settings->priv->apply_result;
1365 settings->priv->apply_result = NULL;
1367 g_simple_async_result_complete (r);
1372 tpaw_account_settings_do_create_account (TpawAccountSettings *self)
1374 TpAccountRequest *account_req;
1375 GHashTableIter iter;
1378 account_req = tp_account_request_new (self->priv->account_manager,
1379 self->priv->cm_name, self->priv->protocol, "New Account");
1381 tp_account_request_set_icon_name (account_req, self->priv->icon_name);
1383 tp_account_request_set_display_name (account_req,
1384 self->priv->display_name);
1386 if (self->priv->service != NULL)
1387 tp_account_request_set_service (account_req, self->priv->service);
1389 g_hash_table_iter_init (&iter, self->priv->parameters);
1390 while (g_hash_table_iter_next (&iter, &k, &v))
1392 const gchar *key = k;
1393 GVariant *value = v;
1395 tp_account_request_set_parameter (account_req, key, value);
1398 if (self->priv->storage_provider != NULL)
1400 tp_account_request_set_storage_provider (account_req,
1401 self->priv->storage_provider);
1404 tp_account_request_create_account_async (account_req,
1405 tpaw_account_settings_created_cb, self);
1409 build_parameters_variant (TpawAccountSettings *self)
1411 GVariantBuilder *builder;
1412 GHashTableIter iter;
1415 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1417 g_hash_table_iter_init (&iter, self->priv->parameters);
1418 while (g_hash_table_iter_next (&iter, &k, &v))
1420 const gchar *key = k;
1421 GVariant *value = v;
1424 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1425 g_variant_new_variant (value));
1427 g_variant_builder_add_value (builder, entry);
1430 return g_variant_builder_end (builder);
1434 tpaw_account_settings_apply_async (TpawAccountSettings *settings,
1435 GAsyncReadyCallback callback,
1438 if (settings->priv->apply_result != NULL)
1440 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1441 callback, user_data,
1442 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1446 settings->priv->apply_result = g_simple_async_result_new (
1447 G_OBJECT (settings), callback, user_data,
1448 tpaw_account_settings_apply_finish);
1450 /* We'll have to reconnect only if we change none DBus_Property on an
1451 * existing account. */
1452 g_simple_async_result_set_op_res_gboolean (settings->priv->apply_result,
1455 if (settings->priv->account == NULL)
1457 g_assert (settings->priv->apply_result != NULL &&
1458 settings->priv->account == NULL);
1460 tpaw_account_settings_do_create_account (settings);
1464 tp_account_update_parameters_vardict_async (settings->priv->account,
1465 build_parameters_variant (settings),
1466 (const gchar **) settings->priv->unset_parameters->data,
1467 tpaw_account_settings_account_updated, settings);
1472 tpaw_account_settings_apply_finish (TpawAccountSettings *settings,
1473 GAsyncResult *result,
1474 gboolean *reconnect_required,
1477 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1481 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1482 G_OBJECT (settings), tpaw_account_settings_apply_finish), FALSE);
1484 if (reconnect_required != NULL)
1485 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1486 G_SIMPLE_ASYNC_RESULT (result));
1492 tpaw_account_settings_has_account (TpawAccountSettings *settings,
1495 const gchar *account_path;
1496 const gchar *priv_account_path;
1498 g_return_val_if_fail (TPAW_IS_ACCOUNT_SETTINGS (settings), FALSE);
1499 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1501 if (settings->priv->account == NULL)
1504 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1505 priv_account_path = tp_proxy_get_object_path (
1506 TP_PROXY (settings->priv->account));
1508 return (!tp_strdiff (account_path, priv_account_path));
1512 tpaw_account_settings_set_regex (TpawAccountSettings *settings,
1514 const gchar *pattern)
1517 GError *error = NULL;
1519 regex = g_regex_new (pattern, 0, 0, &error);
1522 g_warning ("Failed to create reg exp: %s", error->message);
1523 g_error_free (error);
1527 g_hash_table_insert (settings->priv->param_regexps, g_strdup (param),
1532 tpaw_account_settings_parameter_is_valid (
1533 TpawAccountSettings *settings,
1536 const GRegex *regex;
1538 g_return_val_if_fail (TPAW_IS_ACCOUNT_SETTINGS (settings), FALSE);
1540 if (g_list_find_custom (settings->priv->required_params, param,
1541 (GCompareFunc) strcmp))
1543 /* first, look if it's set in our own parameters */
1544 if (g_hash_table_lookup (settings->priv->parameters, param) != NULL)
1547 /* if we did not unset the parameter, look if it's in the account */
1548 if (settings->priv->account != NULL &&
1549 !tpaw_account_settings_is_unset (settings, param))
1551 const GHashTable *account_params;
1553 account_params = tp_account_get_parameters (
1554 settings->priv->account);
1555 if (tp_asv_lookup (account_params, param))
1563 /* test whether parameter value matches its regex */
1564 regex = g_hash_table_lookup (settings->priv->param_regexps, param);
1570 value = tpaw_account_settings_dup_string (settings, param);
1574 match = g_regex_match (regex, value, 0, NULL);
1584 tpaw_account_settings_is_valid (TpawAccountSettings *settings)
1587 GHashTableIter iter;
1590 g_return_val_if_fail (TPAW_IS_ACCOUNT_SETTINGS (settings), FALSE);
1592 for (l = settings->priv->required_params; l; l = l->next)
1594 if (!tpaw_account_settings_parameter_is_valid (settings, l->data))
1598 g_hash_table_iter_init (&iter, settings->priv->param_regexps);
1599 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1601 if (!tpaw_account_settings_parameter_is_valid (settings, param))
1609 tpaw_account_settings_get_tp_protocol (TpawAccountSettings *self)
1611 return self->priv->protocol_obj;
1615 tpaw_account_settings_supports_sasl (TpawAccountSettings *self)
1617 return self->priv->supports_sasl;
1621 tpaw_account_settings_param_is_supported (TpawAccountSettings *self,
1624 return tp_protocol_has_param (self->priv->protocol_obj, param);
1628 tpaw_account_settings_set_uri_scheme_tel (TpawAccountSettings *self,
1631 self->priv->uri_scheme_tel = associate;
1635 tpaw_account_settings_has_uri_scheme_tel (
1636 TpawAccountSettings *self)
1638 return self->priv->uri_scheme_tel;
1642 tpaw_account_settings_set_storage_provider (TpawAccountSettings *self,
1643 const gchar *storage)
1645 g_free (self->priv->storage_provider);
1646 self->priv->storage_provider = g_strdup (storage);
1650 tpaw_account_settings_set_remember_password (TpawAccountSettings *self,
1653 self->priv->remember_password = remember;