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
26 #include <telepathy-glib/account-manager.h>
27 #include <telepathy-glib/util.h>
28 #include <telepathy-glib/interfaces.h>
29 #include <telepathy-glib/gtypes.h>
31 #include "empathy-account-settings.h"
32 #include "empathy-connection-managers.h"
33 #include "empathy-keyring.h"
34 #include "empathy-utils.h"
35 #include "empathy-presence-manager.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
38 #include <libempathy/empathy-debug.h>
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
42 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
50 PROP_DISPLAY_NAME_OVERRIDDEN,
55 PASSWORD_RETRIEVED = 1,
59 static gulong signals[LAST_SIGNAL] = { 0, };
61 /* private structure */
62 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
64 struct _EmpathyAccountSettingsPriv
66 gboolean dispose_has_run;
67 EmpathyConnectionManagers *managers;
68 TpAccountManager *account_manager;
70 TpConnectionManager *manager;
71 TpProtocol *protocol_obj;
79 gchar *storage_provider;
80 gboolean display_name_overridden;
83 gboolean supports_sasl;
84 gboolean remember_password;
87 gchar *password_original;
89 gboolean password_retrieved;
90 gboolean password_requested;
92 /* Parameter name (gchar *) -> parameter value (GVariant) */
93 GHashTable *parameters;
94 /* Keys are parameter names from the hash above (gchar *).
95 * Values are regular expresions that should match corresponding parameter
96 * values (GRegex *). Possible regexp patterns are defined in
97 * empathy-account-widget.c */
98 GHashTable *param_regexps;
99 GArray *unset_parameters;
100 GList *required_params;
102 gulong managers_ready_id;
103 gboolean preparing_protocol;
105 /* If TRUE, the account should have 'tel' in its
106 * Account.Interface.Addressing.URISchemes property. */
107 gboolean uri_scheme_tel;
108 /* If TRUE, Service property needs to be updated when applying changes */
109 gboolean update_service;
111 GSimpleAsyncResult *apply_result;
115 empathy_account_settings_init (EmpathyAccountSettings *obj)
117 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
118 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
122 /* allocate any data required by the object here */
123 priv->managers = empathy_connection_managers_dup_singleton ();
124 priv->account_manager = tp_account_manager_dup ();
126 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
127 g_free, (GDestroyNotify) g_variant_unref);
129 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
130 g_free, (GDestroyNotify) g_regex_unref);
132 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
134 priv->required_params = NULL;
137 static void empathy_account_settings_dispose (GObject *object);
138 static void empathy_account_settings_finalize (GObject *object);
139 static void empathy_account_settings_account_ready_cb (GObject *source_object,
140 GAsyncResult *result, gpointer user_data);
141 static void empathy_account_settings_managers_ready_cb (GObject *obj,
142 GParamSpec *pspec, gpointer user_data);
143 static void empathy_account_settings_check_readyness (
144 EmpathyAccountSettings *self);
147 empathy_account_settings_set_property (GObject *object,
152 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
153 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
158 priv->account = g_value_dup_object (value);
161 priv->cm_name = g_value_dup_string (value);
164 priv->protocol = g_value_dup_string (value);
167 priv->service = g_value_dup_string (value);
169 case PROP_DISPLAY_NAME:
170 priv->display_name = g_value_dup_string (value);
172 case PROP_DISPLAY_NAME_OVERRIDDEN:
173 priv->display_name_overridden = g_value_get_boolean (value);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 empathy_account_settings_get_property (GObject *object,
187 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
188 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
193 g_value_set_object (value, priv->account);
196 g_value_set_string (value, priv->cm_name);
199 g_value_set_string (value, priv->protocol);
202 g_value_set_string (value, priv->service);
204 case PROP_DISPLAY_NAME:
205 g_value_set_string (value, priv->display_name);
207 case PROP_DISPLAY_NAME_OVERRIDDEN:
208 g_value_set_boolean (value, priv->display_name_overridden);
211 g_value_set_boolean (value, priv->ready);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 empathy_account_settings_constructed (GObject *object)
222 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
223 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
225 if (priv->account != NULL)
227 g_free (priv->cm_name);
228 g_free (priv->protocol);
229 g_free (priv->service);
232 g_strdup (tp_account_get_cm_name (priv->account));
234 g_strdup (tp_account_get_protocol_name (priv->account));
236 g_strdup (tp_account_get_service (priv->account));
237 priv->icon_name = g_strdup
238 (tp_account_get_icon_name (priv->account));
242 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
245 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
247 empathy_account_settings_check_readyness (self);
251 GQuark features[] = {
252 TP_ACCOUNT_FEATURE_CORE,
253 TP_ACCOUNT_FEATURE_STORAGE,
254 TP_ACCOUNT_FEATURE_ADDRESSING,
257 if (priv->account != NULL)
259 tp_proxy_prepare_async (priv->account, features,
260 empathy_account_settings_account_ready_cb, self);
263 tp_g_signal_connect_object (priv->managers, "notify::ready",
264 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
268 empathy_account_settings_parent_class)->constructed != NULL)
270 empathy_account_settings_parent_class)->constructed (object);
275 empathy_account_settings_class_init (
276 EmpathyAccountSettingsClass *empathy_account_settings_class)
278 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
280 g_type_class_add_private (empathy_account_settings_class, sizeof
281 (EmpathyAccountSettingsPriv));
283 object_class->dispose = empathy_account_settings_dispose;
284 object_class->finalize = empathy_account_settings_finalize;
285 object_class->set_property = empathy_account_settings_set_property;
286 object_class->get_property = empathy_account_settings_get_property;
287 object_class->constructed = empathy_account_settings_constructed;
289 g_object_class_install_property (object_class, PROP_ACCOUNT,
290 g_param_spec_object ("account",
292 "The TpAccount backing these settings",
294 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
296 g_object_class_install_property (object_class, PROP_CM_NAME,
297 g_param_spec_string ("connection-manager",
298 "connection-manager",
299 "The name of the connection manager this account uses",
301 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
303 g_object_class_install_property (object_class, PROP_PROTOCOL,
304 g_param_spec_string ("protocol",
306 "The name of the protocol this account uses",
308 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
310 g_object_class_install_property (object_class, PROP_SERVICE,
311 g_param_spec_string ("service",
313 "The service of this account, or NULL",
315 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
317 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
318 g_param_spec_string ("display-name",
320 "The display name account these settings belong to",
322 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
324 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
325 g_param_spec_boolean ("display-name-overridden",
326 "display-name-overridden",
327 "Whether the display name for this account has been manually "
330 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
332 g_object_class_install_property (object_class, PROP_READY,
333 g_param_spec_boolean ("ready",
335 "Whether this account is ready to be used",
337 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
339 signals[PASSWORD_RETRIEVED] =
340 g_signal_new ("password-retrieved",
341 G_TYPE_FROM_CLASS (empathy_account_settings_class),
342 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
343 g_cclosure_marshal_generic,
348 empathy_account_settings_dispose (GObject *object)
350 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
351 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
353 if (priv->dispose_has_run)
356 priv->dispose_has_run = TRUE;
358 if (priv->managers_ready_id != 0)
359 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
360 priv->managers_ready_id = 0;
362 tp_clear_object (&priv->managers);
363 tp_clear_object (&priv->manager);
364 tp_clear_object (&priv->account_manager);
365 tp_clear_object (&priv->account);
366 tp_clear_object (&priv->protocol_obj);
368 /* release any references held by the object here */
369 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
370 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
374 empathy_account_settings_free_unset_parameters (
375 EmpathyAccountSettings *settings)
377 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
380 for (i = 0 ; i < priv->unset_parameters->len; i++)
381 g_free (g_array_index (priv->unset_parameters, gchar *, i));
383 g_array_set_size (priv->unset_parameters, 0);
387 empathy_account_settings_finalize (GObject *object)
389 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
390 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
393 /* free any data held directly by the object here */
394 g_free (priv->cm_name);
395 g_free (priv->protocol);
396 g_free (priv->service);
397 g_free (priv->display_name);
398 g_free (priv->icon_name);
399 g_free (priv->password);
400 g_free (priv->password_original);
401 g_free (priv->storage_provider);
403 if (priv->required_params != NULL)
405 for (l = priv->required_params; l; l = l->next)
407 g_list_free (priv->required_params);
410 g_hash_table_unref (priv->parameters);
411 g_hash_table_unref (priv->param_regexps);
413 empathy_account_settings_free_unset_parameters (self);
414 g_array_unref (priv->unset_parameters);
416 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
420 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
421 GAsyncResult *result,
424 EmpathyAccountSettings *self = user_data;
425 GError *error = NULL;
427 if (!tp_proxy_prepare_finish (source, result, &error))
429 DEBUG ("Failed to prepare protocol object: %s", error->message);
430 g_clear_error (&error);
434 empathy_account_settings_check_readyness (self);
438 empathy_account_settings_get_password_cb (GObject *source,
439 GAsyncResult *result,
442 EmpathyAccountSettings *self = user_data;
443 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
444 const gchar *password;
445 GError *error = NULL;
447 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
452 DEBUG ("Failed to get password: %s", error->message);
453 g_clear_error (&error);
456 /* It doesn't really matter if getting the password failed; that
457 * just means that it's not there, or let's act like that at
460 g_assert (priv->password == NULL);
462 priv->password = g_strdup (password);
463 priv->password_original = g_strdup (password);
465 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
468 static GVariant * empathy_account_settings_dup (
469 EmpathyAccountSettings *settings,
473 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
475 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
476 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
481 if (priv->account != NULL
482 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
485 if (!empathy_connection_managers_is_ready (priv->managers))
488 if (priv->manager == NULL)
490 priv->manager = empathy_connection_managers_get_cm (
491 priv->managers, priv->cm_name);
494 if (priv->manager == NULL)
497 g_object_ref (priv->manager);
499 if (priv->account != NULL)
501 g_free (priv->display_name);
503 g_strdup (tp_account_get_display_name (priv->account));
505 g_free (priv->icon_name);
507 g_strdup (tp_account_get_icon_name (priv->account));
509 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
512 if (priv->protocol_obj == NULL)
514 priv->protocol_obj = g_object_ref (
515 tp_connection_manager_get_protocol_object (priv->manager,
519 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
520 && !priv->preparing_protocol)
522 priv->preparing_protocol = TRUE;
523 tp_proxy_prepare_async (priv->protocol_obj, features,
524 empathy_account_settings_protocol_obj_prepared_cb, self);
529 if (tp_strv_contains (tp_protocol_get_authentication_types (
531 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
533 priv->supports_sasl = TRUE;
537 if (priv->required_params == NULL)
541 params = tp_protocol_dup_params (priv->protocol_obj);
542 for (l = params; l != NULL; l = g_list_next (l))
544 TpConnectionManagerParam *cur = l->data;
546 if (tp_connection_manager_param_is_required (cur))
548 priv->required_params = g_list_append (priv->required_params,
549 g_strdup (tp_connection_manager_param_get_name (cur)));
553 g_list_free_full (params,
554 (GDestroyNotify) tp_connection_manager_param_free);
557 /* priv->account won't be a proper account if it's the account
558 * assistant showing this widget. */
559 if (priv->supports_sasl && !priv->password_requested
560 && priv->account != NULL)
562 priv->password_requested = TRUE;
564 /* Make this call but don't block on its readiness. We'll signal
565 * if it's updated later with ::password-retrieved. */
566 empathy_keyring_get_account_password_async (priv->account,
567 empathy_account_settings_get_password_cb, self);
571 g_object_notify (G_OBJECT (self), "ready");
575 empathy_account_settings_account_ready_cb (GObject *source_object,
576 GAsyncResult *result,
579 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
580 TpAccount *account = TP_ACCOUNT (source_object);
581 GError *error = NULL;
583 if (!tp_proxy_prepare_finish (account, result, &error))
585 DEBUG ("Failed to prepare account: %s", error->message);
586 g_error_free (error);
590 empathy_account_settings_check_readyness (settings);
594 empathy_account_settings_managers_ready_cb (GObject *object,
598 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
600 empathy_account_settings_check_readyness (settings);
603 EmpathyAccountSettings *
604 empathy_account_settings_new (const gchar *connection_manager,
605 const gchar *protocol,
606 const gchar *service,
607 const char *display_name)
609 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
610 "connection-manager", connection_manager,
611 "protocol", protocol,
613 "display-name", display_name,
617 EmpathyAccountSettings *
618 empathy_account_settings_new_for_account (TpAccount *account)
620 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
626 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
628 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
630 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
632 return tp_protocol_dup_params (priv->protocol_obj);
636 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
638 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
644 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
646 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
648 return priv->cm_name;
652 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
654 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
656 return priv->protocol;
660 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
662 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
664 return priv->service;
668 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
669 const gchar *service)
671 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
673 if (!tp_strdiff (priv->service, service))
676 g_free (priv->service);
677 priv->service = g_strdup (service);
678 g_object_notify (G_OBJECT (settings), "service");
679 priv->update_service = TRUE;
683 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
685 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
687 return priv->icon_name;
691 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
693 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
695 return priv->display_name;
699 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
701 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
703 return priv->account;
707 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
710 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
714 a = priv->unset_parameters;
716 for (i = 0; i < a->len; i++)
718 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
725 static const TpConnectionManagerParam *
726 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
729 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
731 return tp_protocol_get_param (priv->protocol_obj, param);
735 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
738 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
742 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
745 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
749 for (idx = 0; idx < priv->unset_parameters->len; idx++)
751 val = g_array_index (priv->unset_parameters, gchar *, idx);
753 if (!tp_strdiff (val, param))
755 priv->unset_parameters =
756 g_array_remove_index (priv->unset_parameters, idx);
765 empathy_account_settings_dup_default (EmpathyAccountSettings *settings,
768 const TpConnectionManagerParam *p;
770 p = empathy_account_settings_get_tp_param (settings, param);
774 return tp_connection_manager_param_dup_default_variant (p);
778 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
781 const TpConnectionManagerParam *p;
783 p = empathy_account_settings_get_tp_param (settings, param);
788 return tp_connection_manager_param_get_dbus_signature (p);
792 empathy_account_settings_dup (EmpathyAccountSettings *settings,
795 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
798 /* Lookup the update parameters we set */
799 result = g_hash_table_lookup (priv->parameters, param);
801 return g_variant_ref (result);
803 /* If the parameters isn't unset use the accounts setting if any */
804 if (priv->account != NULL
805 && !empathy_account_settings_is_unset (settings, param))
807 GVariant *parameters;
809 parameters = tp_account_dup_parameters_vardict (priv->account);
810 result = g_variant_lookup_value (parameters, param, NULL);
811 g_variant_unref (parameters);
814 /* g_variant_lookup_value() is (transfer full) */
818 /* fallback to the default */
819 return empathy_account_settings_dup_default (settings, param);
823 empathy_account_settings_unset (EmpathyAccountSettings *settings,
826 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
828 if (empathy_account_settings_is_unset (settings, param))
831 if (priv->supports_sasl && !tp_strdiff (param, "password"))
833 g_free (priv->password);
834 priv->password = NULL;
838 v = g_strdup (param);
840 g_array_append_val (priv->unset_parameters, v);
841 g_hash_table_remove (priv->parameters, param);
845 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
847 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
849 g_hash_table_remove_all (priv->parameters);
850 empathy_account_settings_free_unset_parameters (settings);
852 g_free (priv->password);
853 priv->password = g_strdup (priv->password_original);
855 if (priv->account != NULL)
856 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
858 priv->uri_scheme_tel = FALSE;
862 empathy_account_settings_dup_string (EmpathyAccountSettings *settings,
865 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
867 gchar *result = NULL;
869 if (!tp_strdiff (param, "password") && priv->supports_sasl)
871 return g_strdup (priv->password);
874 v = empathy_account_settings_dup (settings, param);
878 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
879 result = g_variant_dup_string (v, NULL);
886 empathy_account_settings_dup_strv (EmpathyAccountSettings *settings,
892 v = empathy_account_settings_dup (settings, param);
896 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
897 result = g_variant_dup_strv (v, NULL);
904 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
910 v = empathy_account_settings_dup (settings, param);
914 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
915 ret = g_variant_get_byte (v);
916 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
917 ret = g_variant_get_int32 (v);
918 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
919 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
921 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
922 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
923 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
924 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
929 tmp = g_variant_print (v, TRUE);
930 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
939 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
945 v = empathy_account_settings_dup (settings, param);
949 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
950 ret = g_variant_get_byte (v);
951 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
952 ret = g_variant_get_int32 (v);
953 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
954 ret = g_variant_get_uint32 (v);
955 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
956 ret = g_variant_get_int64 (v);
957 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
958 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
963 tmp = g_variant_print (v, TRUE);
964 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
973 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
979 v = empathy_account_settings_dup (settings, param);
983 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
984 ret = g_variant_get_byte (v);
985 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
986 ret = MAX (0, g_variant_get_int32 (v));
987 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
988 ret = g_variant_get_uint32 (v);
989 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
990 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
991 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
992 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
997 tmp = g_variant_print (v, TRUE);
998 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1002 g_variant_unref (v);
1007 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1013 v = empathy_account_settings_dup (settings, param);
1017 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1018 ret = g_variant_get_byte (v);
1019 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1020 ret = MAX (0, g_variant_get_int32 (v));
1021 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1022 ret = g_variant_get_uint32 (v);
1023 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1024 ret = MAX (0, g_variant_get_int64 (v));
1025 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1026 ret = g_variant_get_uint64 (v);
1031 tmp = g_variant_print (v, TRUE);
1032 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1037 g_variant_unref (v);
1042 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1046 gboolean result = FALSE;
1048 v = empathy_account_settings_dup (settings, param);
1052 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1053 result = g_variant_get_boolean (v);
1059 empathy_account_settings_set (EmpathyAccountSettings *settings,
1063 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1065 g_return_if_fail (param != NULL);
1066 g_return_if_fail (v != NULL);
1068 if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1069 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1071 g_free (priv->password);
1072 priv->password = g_variant_dup_string (v, NULL);
1076 g_hash_table_insert (priv->parameters, g_strdup (param),
1077 g_variant_ref_sink (v));
1080 account_settings_remove_from_unset (settings, param);
1084 account_settings_display_name_set_cb (GObject *src,
1088 GError *error = NULL;
1089 TpAccount *account = TP_ACCOUNT (src);
1090 GSimpleAsyncResult *set_result = user_data;
1092 tp_account_set_display_name_finish (account, res, &error);
1096 g_simple_async_result_set_from_error (set_result, error);
1097 g_error_free (error);
1100 g_simple_async_result_complete (set_result);
1101 g_object_unref (set_result);
1105 empathy_account_settings_set_display_name_async (
1106 EmpathyAccountSettings *settings,
1108 GAsyncReadyCallback callback,
1111 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1112 GSimpleAsyncResult *result;
1114 g_return_if_fail (name != NULL);
1116 result = g_simple_async_result_new (G_OBJECT (settings),
1117 callback, user_data, empathy_account_settings_set_display_name_finish);
1119 if (!tp_strdiff (name, priv->display_name))
1122 g_simple_async_result_complete_in_idle (result);
1126 if (priv->account == NULL)
1128 if (priv->display_name != NULL)
1129 g_free (priv->display_name);
1131 priv->display_name = g_strdup (name);
1133 g_simple_async_result_complete_in_idle (result);
1138 tp_account_set_display_name_async (priv->account, name,
1139 account_settings_display_name_set_cb, result);
1143 empathy_account_settings_set_display_name_finish (
1144 EmpathyAccountSettings *settings,
1145 GAsyncResult *result,
1148 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1152 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1153 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1160 account_settings_icon_name_set_cb (GObject *src,
1164 GError *error = NULL;
1165 TpAccount *account = TP_ACCOUNT (src);
1166 GSimpleAsyncResult *set_result = user_data;
1168 tp_account_set_icon_name_finish (account, res, &error);
1172 g_simple_async_result_set_from_error (set_result, error);
1173 g_error_free (error);
1176 g_simple_async_result_complete (set_result);
1177 g_object_unref (set_result);
1181 empathy_account_settings_set_icon_name_async (
1182 EmpathyAccountSettings *settings,
1184 GAsyncReadyCallback callback,
1187 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1188 GSimpleAsyncResult *result;
1190 g_return_if_fail (name != NULL);
1192 result = g_simple_async_result_new (G_OBJECT (settings),
1193 callback, user_data, empathy_account_settings_set_icon_name_finish);
1195 if (priv->account == NULL)
1197 if (priv->icon_name != NULL)
1198 g_free (priv->icon_name);
1200 priv->icon_name = g_strdup (name);
1202 g_simple_async_result_complete_in_idle (result);
1207 tp_account_set_icon_name_async (priv->account, name,
1208 account_settings_icon_name_set_cb, result);
1212 empathy_account_settings_set_icon_name_finish (
1213 EmpathyAccountSettings *settings,
1214 GAsyncResult *result,
1217 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1221 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1222 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1229 empathy_account_settings_processed_password (GObject *source,
1230 GAsyncResult *result,
1232 gpointer finish_func)
1234 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1235 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1236 GSimpleAsyncResult *r;
1237 GError *error = NULL;
1238 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1241 g_free (priv->password_original);
1242 priv->password_original = g_strdup (priv->password);
1244 if (!func (TP_ACCOUNT (source), result, &error))
1246 g_simple_async_result_set_from_error (priv->apply_result, error);
1247 g_error_free (error);
1250 empathy_account_settings_discard_changes (settings);
1252 r = priv->apply_result;
1253 priv->apply_result = NULL;
1255 g_simple_async_result_complete (r);
1260 empathy_account_settings_set_password_cb (GObject *source,
1261 GAsyncResult *result,
1264 empathy_account_settings_processed_password (source, result, user_data,
1265 empathy_keyring_set_account_password_finish);
1269 empathy_account_settings_delete_password_cb (GObject *source,
1270 GAsyncResult *result,
1273 empathy_account_settings_processed_password (source, result, user_data,
1274 empathy_keyring_delete_account_password_finish);
1278 update_account_uri_schemes (EmpathyAccountSettings *self)
1280 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1282 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1286 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1287 priv->uri_scheme_tel, NULL, NULL);
1291 set_service_cb (GObject *source,
1292 GAsyncResult *result,
1295 GError *error = NULL;
1297 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1299 DEBUG ("Failed to set Account.Service: %s", error->message);
1300 g_error_free (error);
1305 update_account_service (EmpathyAccountSettings *self)
1307 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1309 if (!priv->update_service)
1312 tp_account_set_service_async (priv->account,
1313 priv->service != NULL ? priv->service : "", set_service_cb, self);
1317 empathy_account_settings_account_updated (GObject *source,
1318 GAsyncResult *result,
1321 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1322 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1323 GSimpleAsyncResult *r;
1324 GError *error = NULL;
1325 GStrv reconnect_required = NULL;
1327 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1328 result, &reconnect_required, &error))
1330 g_simple_async_result_set_from_error (priv->apply_result, error);
1331 g_error_free (error);
1335 /* Only set the password in the keyring if the CM supports SASL. */
1336 if (priv->supports_sasl)
1338 if (priv->password != NULL)
1340 /* FIXME: we shouldn't save the password if we
1341 * can't (MaySaveResponse=False) but we don't have API to check that
1342 * at this point (fdo #35382). */
1343 empathy_keyring_set_account_password_async (priv->account,
1344 priv->password, priv->remember_password,
1345 empathy_account_settings_set_password_cb, settings);
1349 empathy_keyring_delete_account_password_async (priv->account,
1350 empathy_account_settings_delete_password_cb, settings);
1356 update_account_uri_schemes (settings);
1357 update_account_service (settings);
1359 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1360 g_strv_length (reconnect_required) > 0);
1363 empathy_account_settings_discard_changes (settings);
1365 r = priv->apply_result;
1366 priv->apply_result = NULL;
1368 g_simple_async_result_complete (r);
1370 g_strfreev (reconnect_required);
1374 empathy_account_settings_created_cb (GObject *source,
1375 GAsyncResult *result,
1378 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1379 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1380 GError *error = NULL;
1381 GSimpleAsyncResult *r;
1383 priv->account = tp_account_request_create_account_finish (
1384 TP_ACCOUNT_REQUEST (source), result, &error);
1386 if (priv->account == NULL)
1388 g_simple_async_result_set_from_error (priv->apply_result, error);
1392 if (priv->supports_sasl && priv->password != NULL)
1394 /* Save the password before connecting */
1395 /* FIXME: we shouldn't save the password if we
1396 * can't (MaySaveResponse=False) but we don't have API to check that
1397 * at this point (fdo #35382). */
1398 empathy_keyring_set_account_password_async (priv->account,
1399 priv->password, priv->remember_password,
1400 empathy_account_settings_set_password_cb,
1405 update_account_uri_schemes (settings);
1407 empathy_account_settings_discard_changes (settings);
1410 r = priv->apply_result;
1411 priv->apply_result = NULL;
1413 g_simple_async_result_complete (r);
1418 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1420 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1421 TpAccountRequest *account_req;
1422 TpConnectionPresenceType type;
1425 EmpathyPresenceManager *presence_mgr;
1426 GHashTableIter iter;
1429 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1430 priv->protocol, "New Account");
1432 presence_mgr = empathy_presence_manager_dup_singleton ();
1433 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1435 g_object_unref (presence_mgr);
1437 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1439 tp_account_request_set_requested_presence (account_req, type,
1443 tp_account_request_set_icon_name (account_req, priv->icon_name);
1445 tp_account_request_set_display_name (account_req, priv->display_name);
1447 if (priv->service != NULL)
1448 tp_account_request_set_service (account_req, priv->service);
1450 g_hash_table_iter_init (&iter, priv->parameters);
1451 while (g_hash_table_iter_next (&iter, &k, &v))
1453 const gchar *key = k;
1454 GVariant *value = v;
1456 tp_account_request_set_parameter (account_req, key, value);
1459 if (priv->storage_provider != NULL)
1461 tp_account_request_set_storage_provider (account_req,
1462 priv->storage_provider);
1465 tp_account_request_create_account_async (account_req,
1466 empathy_account_settings_created_cb, self);
1470 build_parameters_variant (EmpathyAccountSettings *self)
1472 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1473 GVariantBuilder *builder;
1474 GHashTableIter iter;
1477 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1479 g_hash_table_iter_init (&iter, priv->parameters);
1480 while (g_hash_table_iter_next (&iter, &k, &v))
1482 const gchar *key = k;
1483 GVariant *value = v;
1486 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1487 g_variant_new_variant (value));
1489 g_variant_builder_add_value (builder, entry);
1492 return g_variant_builder_end (builder);
1496 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1497 GAsyncReadyCallback callback,
1500 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1502 if (priv->apply_result != NULL)
1504 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1505 callback, user_data,
1506 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1510 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1511 callback, user_data, empathy_account_settings_apply_finish);
1513 /* We'll have to reconnect only if we change none DBus_Property on an
1514 * existing account. */
1515 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1517 if (priv->account == NULL)
1519 g_assert (priv->apply_result != NULL && priv->account == NULL);
1521 empathy_account_settings_do_create_account (settings);
1525 tp_account_update_parameters_vardict_async (priv->account,
1526 build_parameters_variant (settings),
1527 (const gchar **) priv->unset_parameters->data,
1528 empathy_account_settings_account_updated, settings);
1533 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1534 GAsyncResult *result,
1535 gboolean *reconnect_required,
1538 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1542 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1543 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1545 if (reconnect_required != NULL)
1546 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1547 G_SIMPLE_ASYNC_RESULT (result));
1553 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1556 EmpathyAccountSettingsPriv *priv;
1557 const gchar *account_path;
1558 const gchar *priv_account_path;
1560 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1561 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1563 priv = GET_PRIV (settings);
1565 if (priv->account == NULL)
1568 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1569 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1571 return (!tp_strdiff (account_path, priv_account_path));
1575 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1577 const gchar *pattern)
1579 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1581 GError *error = NULL;
1583 regex = g_regex_new (pattern, 0, 0, &error);
1586 g_warning ("Failed to create reg exp: %s", error->message);
1587 g_error_free (error);
1591 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1595 empathy_account_settings_parameter_is_valid (
1596 EmpathyAccountSettings *settings,
1599 EmpathyAccountSettingsPriv *priv;
1600 const GRegex *regex;
1602 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1604 priv = GET_PRIV (settings);
1606 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1608 /* first, look if it's set in our own parameters */
1609 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1612 /* if we did not unset the parameter, look if it's in the account */
1613 if (priv->account != NULL &&
1614 !empathy_account_settings_is_unset (settings, param))
1616 const GHashTable *account_params;
1618 account_params = tp_account_get_parameters (priv->account);
1619 if (tp_asv_lookup (account_params, param))
1627 /* test whether parameter value matches its regex */
1628 regex = g_hash_table_lookup (priv->param_regexps, param);
1634 value = empathy_account_settings_dup_string (settings, param);
1638 match = g_regex_match (regex, value, 0, NULL);
1648 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1650 EmpathyAccountSettingsPriv *priv;
1652 GHashTableIter iter;
1655 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1657 priv = GET_PRIV (settings);
1659 for (l = priv->required_params; l; l = l->next)
1661 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1665 g_hash_table_iter_init (&iter, priv->param_regexps);
1666 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1668 if (!empathy_account_settings_parameter_is_valid (settings, param))
1676 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1678 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1680 return priv->protocol_obj;
1684 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1686 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1688 return priv->supports_sasl;
1692 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1695 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1697 return tp_protocol_has_param (priv->protocol_obj, param);
1701 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1704 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1706 priv->uri_scheme_tel = associate;
1710 empathy_account_settings_has_uri_scheme_tel (
1711 EmpathyAccountSettings *self)
1713 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1715 return priv->uri_scheme_tel;
1719 empathy_account_settings_set_storage_provider (EmpathyAccountSettings *self,
1720 const gchar *storage)
1722 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1724 g_free (priv->storage_provider);
1725 priv->storage_provider = g_strdup (storage);
1729 empathy_account_settings_set_remember_password (EmpathyAccountSettings *self,
1732 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1734 priv->remember_password = remember;