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 "empathy-account-settings.h"
27 #include "empathy-connection-managers.h"
28 #include "empathy-keyring.h"
29 #include "empathy-utils.h"
30 #include "empathy-presence-manager.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
33 #include <libempathy/empathy-debug.h>
35 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
37 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
45 PROP_DISPLAY_NAME_OVERRIDDEN,
50 PASSWORD_RETRIEVED = 1,
54 static gulong signals[LAST_SIGNAL] = { 0, };
56 /* private structure */
57 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
59 struct _EmpathyAccountSettingsPriv
61 gboolean dispose_has_run;
62 EmpathyConnectionManagers *managers;
63 TpAccountManager *account_manager;
65 TpConnectionManager *manager;
66 TpProtocol *protocol_obj;
74 gchar *storage_provider;
75 gboolean display_name_overridden;
78 gboolean supports_sasl;
79 gboolean remember_password;
82 gchar *password_original;
84 gboolean password_retrieved;
85 gboolean password_requested;
87 /* Parameter name (gchar *) -> parameter value (GVariant) */
88 GHashTable *parameters;
89 /* Keys are parameter names from the hash above (gchar *).
90 * Values are regular expresions that should match corresponding parameter
91 * values (GRegex *). Possible regexp patterns are defined in
92 * empathy-account-widget.c */
93 GHashTable *param_regexps;
94 GArray *unset_parameters;
95 GList *required_params;
97 gulong managers_ready_id;
98 gboolean preparing_protocol;
100 /* If TRUE, the account should have 'tel' in its
101 * Account.Interface.Addressing.URISchemes property. */
102 gboolean uri_scheme_tel;
103 /* If TRUE, Service property needs to be updated when applying changes */
104 gboolean update_service;
106 GSimpleAsyncResult *apply_result;
110 empathy_account_settings_init (EmpathyAccountSettings *obj)
112 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
113 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
117 /* allocate any data required by the object here */
118 priv->managers = empathy_connection_managers_dup_singleton ();
119 priv->account_manager = tp_account_manager_dup ();
121 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
122 g_free, (GDestroyNotify) g_variant_unref);
124 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
125 g_free, (GDestroyNotify) g_regex_unref);
127 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
129 priv->required_params = NULL;
132 static void empathy_account_settings_dispose (GObject *object);
133 static void empathy_account_settings_finalize (GObject *object);
134 static void empathy_account_settings_account_ready_cb (GObject *source_object,
135 GAsyncResult *result, gpointer user_data);
136 static void empathy_account_settings_managers_ready_cb (GObject *obj,
137 GParamSpec *pspec, gpointer user_data);
138 static void empathy_account_settings_check_readyness (
139 EmpathyAccountSettings *self);
142 empathy_account_settings_set_property (GObject *object,
147 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
148 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
153 priv->account = g_value_dup_object (value);
156 priv->cm_name = g_value_dup_string (value);
159 priv->protocol = g_value_dup_string (value);
162 priv->service = g_value_dup_string (value);
164 case PROP_DISPLAY_NAME:
165 priv->display_name = g_value_dup_string (value);
167 case PROP_DISPLAY_NAME_OVERRIDDEN:
168 priv->display_name_overridden = g_value_get_boolean (value);
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 empathy_account_settings_get_property (GObject *object,
182 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
183 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
188 g_value_set_object (value, priv->account);
191 g_value_set_string (value, priv->cm_name);
194 g_value_set_string (value, priv->protocol);
197 g_value_set_string (value, priv->service);
199 case PROP_DISPLAY_NAME:
200 g_value_set_string (value, priv->display_name);
202 case PROP_DISPLAY_NAME_OVERRIDDEN:
203 g_value_set_boolean (value, priv->display_name_overridden);
206 g_value_set_boolean (value, priv->ready);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 empathy_account_settings_constructed (GObject *object)
217 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
218 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
220 if (priv->account != NULL)
222 g_free (priv->cm_name);
223 g_free (priv->protocol);
224 g_free (priv->service);
227 g_strdup (tp_account_get_cm_name (priv->account));
229 g_strdup (tp_account_get_protocol_name (priv->account));
231 g_strdup (tp_account_get_service (priv->account));
232 priv->icon_name = g_strdup
233 (tp_account_get_icon_name (priv->account));
237 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
240 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
242 empathy_account_settings_check_readyness (self);
246 GQuark features[] = {
247 TP_ACCOUNT_FEATURE_CORE,
248 TP_ACCOUNT_FEATURE_STORAGE,
249 TP_ACCOUNT_FEATURE_ADDRESSING,
252 if (priv->account != NULL)
254 tp_proxy_prepare_async (priv->account, features,
255 empathy_account_settings_account_ready_cb, self);
258 tp_g_signal_connect_object (priv->managers, "notify::ready",
259 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
263 empathy_account_settings_parent_class)->constructed != NULL)
265 empathy_account_settings_parent_class)->constructed (object);
270 empathy_account_settings_class_init (
271 EmpathyAccountSettingsClass *empathy_account_settings_class)
273 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
275 g_type_class_add_private (empathy_account_settings_class, sizeof
276 (EmpathyAccountSettingsPriv));
278 object_class->dispose = empathy_account_settings_dispose;
279 object_class->finalize = empathy_account_settings_finalize;
280 object_class->set_property = empathy_account_settings_set_property;
281 object_class->get_property = empathy_account_settings_get_property;
282 object_class->constructed = empathy_account_settings_constructed;
284 g_object_class_install_property (object_class, PROP_ACCOUNT,
285 g_param_spec_object ("account",
287 "The TpAccount backing these settings",
289 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
291 g_object_class_install_property (object_class, PROP_CM_NAME,
292 g_param_spec_string ("connection-manager",
293 "connection-manager",
294 "The name of the connection manager this account uses",
296 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
298 g_object_class_install_property (object_class, PROP_PROTOCOL,
299 g_param_spec_string ("protocol",
301 "The name of the protocol this account uses",
303 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
305 g_object_class_install_property (object_class, PROP_SERVICE,
306 g_param_spec_string ("service",
308 "The service of this account, or NULL",
310 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
312 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
313 g_param_spec_string ("display-name",
315 "The display name account these settings belong to",
317 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
319 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
320 g_param_spec_boolean ("display-name-overridden",
321 "display-name-overridden",
322 "Whether the display name for this account has been manually "
325 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
327 g_object_class_install_property (object_class, PROP_READY,
328 g_param_spec_boolean ("ready",
330 "Whether this account is ready to be used",
332 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
334 signals[PASSWORD_RETRIEVED] =
335 g_signal_new ("password-retrieved",
336 G_TYPE_FROM_CLASS (empathy_account_settings_class),
337 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
338 g_cclosure_marshal_generic,
343 empathy_account_settings_dispose (GObject *object)
345 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
346 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
348 if (priv->dispose_has_run)
351 priv->dispose_has_run = TRUE;
353 if (priv->managers_ready_id != 0)
354 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
355 priv->managers_ready_id = 0;
357 tp_clear_object (&priv->managers);
358 tp_clear_object (&priv->manager);
359 tp_clear_object (&priv->account_manager);
360 tp_clear_object (&priv->account);
361 tp_clear_object (&priv->protocol_obj);
363 /* release any references held by the object here */
364 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
365 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
369 empathy_account_settings_free_unset_parameters (
370 EmpathyAccountSettings *settings)
372 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
375 for (i = 0 ; i < priv->unset_parameters->len; i++)
376 g_free (g_array_index (priv->unset_parameters, gchar *, i));
378 g_array_set_size (priv->unset_parameters, 0);
382 empathy_account_settings_finalize (GObject *object)
384 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
385 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
388 /* free any data held directly by the object here */
389 g_free (priv->cm_name);
390 g_free (priv->protocol);
391 g_free (priv->service);
392 g_free (priv->display_name);
393 g_free (priv->icon_name);
394 g_free (priv->password);
395 g_free (priv->password_original);
396 g_free (priv->storage_provider);
398 if (priv->required_params != NULL)
400 for (l = priv->required_params; l; l = l->next)
402 g_list_free (priv->required_params);
405 g_hash_table_unref (priv->parameters);
406 g_hash_table_unref (priv->param_regexps);
408 empathy_account_settings_free_unset_parameters (self);
409 g_array_unref (priv->unset_parameters);
411 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
415 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
416 GAsyncResult *result,
419 EmpathyAccountSettings *self = user_data;
420 GError *error = NULL;
422 if (!tp_proxy_prepare_finish (source, result, &error))
424 DEBUG ("Failed to prepare protocol object: %s", error->message);
425 g_clear_error (&error);
429 empathy_account_settings_check_readyness (self);
433 empathy_account_settings_get_password_cb (GObject *source,
434 GAsyncResult *result,
437 EmpathyAccountSettings *self = user_data;
438 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
439 const gchar *password;
440 GError *error = NULL;
442 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
447 DEBUG ("Failed to get password: %s", error->message);
448 g_clear_error (&error);
451 /* It doesn't really matter if getting the password failed; that
452 * just means that it's not there, or let's act like that at
455 g_assert (priv->password == NULL);
457 priv->password = g_strdup (password);
458 priv->password_original = g_strdup (password);
460 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
463 static GVariant * empathy_account_settings_dup (
464 EmpathyAccountSettings *settings,
468 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
470 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
471 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
476 if (priv->account != NULL
477 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
480 if (!empathy_connection_managers_is_ready (priv->managers))
483 if (priv->manager == NULL)
485 priv->manager = empathy_connection_managers_get_cm (
486 priv->managers, priv->cm_name);
489 if (priv->manager == NULL)
492 g_object_ref (priv->manager);
494 if (priv->account != NULL)
496 g_free (priv->display_name);
498 g_strdup (tp_account_get_display_name (priv->account));
500 g_free (priv->icon_name);
502 g_strdup (tp_account_get_icon_name (priv->account));
504 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
507 if (priv->protocol_obj == NULL)
509 priv->protocol_obj = g_object_ref (
510 tp_connection_manager_get_protocol_object (priv->manager,
514 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
515 && !priv->preparing_protocol)
517 priv->preparing_protocol = TRUE;
518 tp_proxy_prepare_async (priv->protocol_obj, features,
519 empathy_account_settings_protocol_obj_prepared_cb, self);
524 if (tp_strv_contains (tp_protocol_get_authentication_types (
526 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
528 priv->supports_sasl = TRUE;
532 if (priv->required_params == NULL)
536 params = tp_protocol_dup_params (priv->protocol_obj);
537 for (l = params; l != NULL; l = g_list_next (l))
539 TpConnectionManagerParam *cur = l->data;
541 if (tp_connection_manager_param_is_required (cur))
543 priv->required_params = g_list_append (priv->required_params,
544 g_strdup (tp_connection_manager_param_get_name (cur)));
548 g_list_free_full (params,
549 (GDestroyNotify) tp_connection_manager_param_free);
552 /* priv->account won't be a proper account if it's the account
553 * assistant showing this widget. */
554 if (priv->supports_sasl && !priv->password_requested
555 && priv->account != NULL)
557 priv->password_requested = TRUE;
559 /* Make this call but don't block on its readiness. We'll signal
560 * if it's updated later with ::password-retrieved. */
561 empathy_keyring_get_account_password_async (priv->account,
562 empathy_account_settings_get_password_cb, self);
566 g_object_notify (G_OBJECT (self), "ready");
570 empathy_account_settings_account_ready_cb (GObject *source_object,
571 GAsyncResult *result,
574 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
575 TpAccount *account = TP_ACCOUNT (source_object);
576 GError *error = NULL;
578 if (!tp_proxy_prepare_finish (account, result, &error))
580 DEBUG ("Failed to prepare account: %s", error->message);
581 g_error_free (error);
585 empathy_account_settings_check_readyness (settings);
589 empathy_account_settings_managers_ready_cb (GObject *object,
593 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
595 empathy_account_settings_check_readyness (settings);
598 EmpathyAccountSettings *
599 empathy_account_settings_new (const gchar *connection_manager,
600 const gchar *protocol,
601 const gchar *service,
602 const char *display_name)
604 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
605 "connection-manager", connection_manager,
606 "protocol", protocol,
608 "display-name", display_name,
612 EmpathyAccountSettings *
613 empathy_account_settings_new_for_account (TpAccount *account)
615 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
621 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
623 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
625 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
627 return tp_protocol_dup_params (priv->protocol_obj);
631 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
633 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
639 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
641 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
643 return priv->cm_name;
647 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
649 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
651 return priv->protocol;
655 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
657 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
659 return priv->service;
663 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
664 const gchar *service)
666 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
668 if (!tp_strdiff (priv->service, service))
671 g_free (priv->service);
672 priv->service = g_strdup (service);
673 g_object_notify (G_OBJECT (settings), "service");
674 priv->update_service = TRUE;
678 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
680 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
682 return priv->icon_name;
686 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
688 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
690 return priv->display_name;
694 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
696 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
698 return priv->account;
702 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
705 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
709 a = priv->unset_parameters;
711 for (i = 0; i < a->len; i++)
713 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
720 static const TpConnectionManagerParam *
721 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
724 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
726 return tp_protocol_get_param (priv->protocol_obj, param);
730 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
733 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
737 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
740 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
744 for (idx = 0; idx < priv->unset_parameters->len; idx++)
746 val = g_array_index (priv->unset_parameters, gchar *, idx);
748 if (!tp_strdiff (val, param))
750 priv->unset_parameters =
751 g_array_remove_index (priv->unset_parameters, idx);
760 empathy_account_settings_dup_default (EmpathyAccountSettings *settings,
763 const TpConnectionManagerParam *p;
765 p = empathy_account_settings_get_tp_param (settings, param);
769 return tp_connection_manager_param_dup_default_variant (p);
773 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
776 const TpConnectionManagerParam *p;
778 p = empathy_account_settings_get_tp_param (settings, param);
783 return tp_connection_manager_param_get_dbus_signature (p);
787 empathy_account_settings_dup (EmpathyAccountSettings *settings,
790 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
793 /* Lookup the update parameters we set */
794 result = g_hash_table_lookup (priv->parameters, param);
796 return g_variant_ref (result);
798 /* If the parameters isn't unset use the accounts setting if any */
799 if (priv->account != NULL
800 && !empathy_account_settings_is_unset (settings, param))
802 GVariant *parameters;
804 parameters = tp_account_dup_parameters_vardict (priv->account);
805 result = g_variant_lookup_value (parameters, param, NULL);
806 g_variant_unref (parameters);
809 /* g_variant_lookup_value() is (transfer full) */
813 /* fallback to the default */
814 return empathy_account_settings_dup_default (settings, param);
818 empathy_account_settings_unset (EmpathyAccountSettings *settings,
821 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
823 if (empathy_account_settings_is_unset (settings, param))
826 if (priv->supports_sasl && !tp_strdiff (param, "password"))
828 g_free (priv->password);
829 priv->password = NULL;
833 v = g_strdup (param);
835 g_array_append_val (priv->unset_parameters, v);
836 g_hash_table_remove (priv->parameters, param);
840 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
842 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
844 g_hash_table_remove_all (priv->parameters);
845 empathy_account_settings_free_unset_parameters (settings);
847 g_free (priv->password);
848 priv->password = g_strdup (priv->password_original);
850 if (priv->account != NULL)
851 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
853 priv->uri_scheme_tel = FALSE;
857 empathy_account_settings_dup_string (EmpathyAccountSettings *settings,
860 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
862 gchar *result = NULL;
864 if (!tp_strdiff (param, "password") && priv->supports_sasl)
866 return g_strdup (priv->password);
869 v = empathy_account_settings_dup (settings, param);
873 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
874 result = g_variant_dup_string (v, NULL);
881 empathy_account_settings_dup_strv (EmpathyAccountSettings *settings,
887 v = empathy_account_settings_dup (settings, param);
891 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
892 result = g_variant_dup_strv (v, NULL);
899 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
905 v = empathy_account_settings_dup (settings, param);
909 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
910 ret = g_variant_get_byte (v);
911 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
912 ret = g_variant_get_int32 (v);
913 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
914 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
916 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
917 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
918 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
919 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
924 tmp = g_variant_print (v, TRUE);
925 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
934 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
940 v = empathy_account_settings_dup (settings, param);
944 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
945 ret = g_variant_get_byte (v);
946 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
947 ret = g_variant_get_int32 (v);
948 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
949 ret = g_variant_get_uint32 (v);
950 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
951 ret = g_variant_get_int64 (v);
952 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
953 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
958 tmp = g_variant_print (v, TRUE);
959 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
968 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
974 v = empathy_account_settings_dup (settings, param);
978 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
979 ret = g_variant_get_byte (v);
980 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
981 ret = MAX (0, g_variant_get_int32 (v));
982 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
983 ret = g_variant_get_uint32 (v);
984 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
985 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
986 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
987 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
992 tmp = g_variant_print (v, TRUE);
993 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1002 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1008 v = empathy_account_settings_dup (settings, param);
1012 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1013 ret = g_variant_get_byte (v);
1014 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1015 ret = MAX (0, g_variant_get_int32 (v));
1016 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1017 ret = g_variant_get_uint32 (v);
1018 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1019 ret = MAX (0, g_variant_get_int64 (v));
1020 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1021 ret = g_variant_get_uint64 (v);
1026 tmp = g_variant_print (v, TRUE);
1027 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1032 g_variant_unref (v);
1037 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1041 gboolean result = FALSE;
1043 v = empathy_account_settings_dup (settings, param);
1047 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1048 result = g_variant_get_boolean (v);
1054 empathy_account_settings_set (EmpathyAccountSettings *settings,
1058 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1060 g_return_if_fail (param != NULL);
1061 g_return_if_fail (v != NULL);
1063 if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1064 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1066 g_free (priv->password);
1067 priv->password = g_variant_dup_string (v, NULL);
1071 g_hash_table_insert (priv->parameters, g_strdup (param),
1072 g_variant_ref_sink (v));
1075 account_settings_remove_from_unset (settings, param);
1079 account_settings_display_name_set_cb (GObject *src,
1083 GError *error = NULL;
1084 TpAccount *account = TP_ACCOUNT (src);
1085 GSimpleAsyncResult *set_result = user_data;
1087 tp_account_set_display_name_finish (account, res, &error);
1091 g_simple_async_result_set_from_error (set_result, error);
1092 g_error_free (error);
1095 g_simple_async_result_complete (set_result);
1096 g_object_unref (set_result);
1100 empathy_account_settings_set_display_name_async (
1101 EmpathyAccountSettings *settings,
1103 GAsyncReadyCallback callback,
1106 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1107 GSimpleAsyncResult *result;
1109 g_return_if_fail (name != NULL);
1111 result = g_simple_async_result_new (G_OBJECT (settings),
1112 callback, user_data, empathy_account_settings_set_display_name_finish);
1114 if (!tp_strdiff (name, priv->display_name))
1117 g_simple_async_result_complete_in_idle (result);
1121 if (priv->account == NULL)
1123 if (priv->display_name != NULL)
1124 g_free (priv->display_name);
1126 priv->display_name = g_strdup (name);
1128 g_simple_async_result_complete_in_idle (result);
1133 tp_account_set_display_name_async (priv->account, name,
1134 account_settings_display_name_set_cb, result);
1138 empathy_account_settings_set_display_name_finish (
1139 EmpathyAccountSettings *settings,
1140 GAsyncResult *result,
1143 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1147 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1148 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1155 account_settings_icon_name_set_cb (GObject *src,
1159 GError *error = NULL;
1160 TpAccount *account = TP_ACCOUNT (src);
1161 GSimpleAsyncResult *set_result = user_data;
1163 tp_account_set_icon_name_finish (account, res, &error);
1167 g_simple_async_result_set_from_error (set_result, error);
1168 g_error_free (error);
1171 g_simple_async_result_complete (set_result);
1172 g_object_unref (set_result);
1176 empathy_account_settings_set_icon_name_async (
1177 EmpathyAccountSettings *settings,
1179 GAsyncReadyCallback callback,
1182 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1183 GSimpleAsyncResult *result;
1185 g_return_if_fail (name != NULL);
1187 result = g_simple_async_result_new (G_OBJECT (settings),
1188 callback, user_data, empathy_account_settings_set_icon_name_finish);
1190 if (priv->account == NULL)
1192 if (priv->icon_name != NULL)
1193 g_free (priv->icon_name);
1195 priv->icon_name = g_strdup (name);
1197 g_simple_async_result_complete_in_idle (result);
1202 tp_account_set_icon_name_async (priv->account, name,
1203 account_settings_icon_name_set_cb, result);
1207 empathy_account_settings_set_icon_name_finish (
1208 EmpathyAccountSettings *settings,
1209 GAsyncResult *result,
1212 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1216 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1217 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1224 empathy_account_settings_processed_password (GObject *source,
1225 GAsyncResult *result,
1227 gpointer finish_func)
1229 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1230 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1231 GSimpleAsyncResult *r;
1232 GError *error = NULL;
1233 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1236 g_free (priv->password_original);
1237 priv->password_original = g_strdup (priv->password);
1239 if (!func (TP_ACCOUNT (source), result, &error))
1241 g_simple_async_result_set_from_error (priv->apply_result, error);
1242 g_error_free (error);
1245 empathy_account_settings_discard_changes (settings);
1247 r = priv->apply_result;
1248 priv->apply_result = NULL;
1250 g_simple_async_result_complete (r);
1255 empathy_account_settings_set_password_cb (GObject *source,
1256 GAsyncResult *result,
1259 empathy_account_settings_processed_password (source, result, user_data,
1260 empathy_keyring_set_account_password_finish);
1264 empathy_account_settings_delete_password_cb (GObject *source,
1265 GAsyncResult *result,
1268 empathy_account_settings_processed_password (source, result, user_data,
1269 empathy_keyring_delete_account_password_finish);
1273 update_account_uri_schemes (EmpathyAccountSettings *self)
1275 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1277 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1281 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1282 priv->uri_scheme_tel, NULL, NULL);
1286 set_service_cb (GObject *source,
1287 GAsyncResult *result,
1290 GError *error = NULL;
1292 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1294 DEBUG ("Failed to set Account.Service: %s", error->message);
1295 g_error_free (error);
1300 update_account_service (EmpathyAccountSettings *self)
1302 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1304 if (!priv->update_service)
1307 tp_account_set_service_async (priv->account,
1308 priv->service != NULL ? priv->service : "", set_service_cb, self);
1312 empathy_account_settings_account_updated (GObject *source,
1313 GAsyncResult *result,
1316 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1317 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1318 GSimpleAsyncResult *r;
1319 GError *error = NULL;
1320 GStrv reconnect_required = NULL;
1322 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1323 result, &reconnect_required, &error))
1325 g_simple_async_result_set_from_error (priv->apply_result, error);
1326 g_error_free (error);
1330 /* Only set the password in the keyring if the CM supports SASL. */
1331 if (priv->supports_sasl)
1333 if (priv->password != NULL)
1335 /* FIXME: we shouldn't save the password if we
1336 * can't (MaySaveResponse=False) but we don't have API to check that
1337 * at this point (fdo #35382). */
1338 empathy_keyring_set_account_password_async (priv->account,
1339 priv->password, priv->remember_password,
1340 empathy_account_settings_set_password_cb, settings);
1344 empathy_keyring_delete_account_password_async (priv->account,
1345 empathy_account_settings_delete_password_cb, settings);
1351 update_account_uri_schemes (settings);
1352 update_account_service (settings);
1354 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1355 g_strv_length (reconnect_required) > 0);
1358 empathy_account_settings_discard_changes (settings);
1360 r = priv->apply_result;
1361 priv->apply_result = NULL;
1363 g_simple_async_result_complete (r);
1365 g_strfreev (reconnect_required);
1369 empathy_account_settings_created_cb (GObject *source,
1370 GAsyncResult *result,
1373 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1374 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1375 GError *error = NULL;
1376 GSimpleAsyncResult *r;
1378 priv->account = tp_account_request_create_account_finish (
1379 TP_ACCOUNT_REQUEST (source), result, &error);
1381 if (priv->account == NULL)
1383 g_simple_async_result_set_from_error (priv->apply_result, error);
1387 if (priv->supports_sasl && priv->password != NULL)
1389 /* Save the password before connecting */
1390 /* FIXME: we shouldn't save the password if we
1391 * can't (MaySaveResponse=False) but we don't have API to check that
1392 * at this point (fdo #35382). */
1393 empathy_keyring_set_account_password_async (priv->account,
1394 priv->password, priv->remember_password,
1395 empathy_account_settings_set_password_cb,
1400 update_account_uri_schemes (settings);
1402 empathy_account_settings_discard_changes (settings);
1405 r = priv->apply_result;
1406 priv->apply_result = NULL;
1408 g_simple_async_result_complete (r);
1413 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1415 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1416 TpAccountRequest *account_req;
1417 TpConnectionPresenceType type;
1420 EmpathyPresenceManager *presence_mgr;
1421 GHashTableIter iter;
1424 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1425 priv->protocol, "New Account");
1427 presence_mgr = empathy_presence_manager_dup_singleton ();
1428 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1430 g_object_unref (presence_mgr);
1432 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1434 tp_account_request_set_requested_presence (account_req, type,
1438 tp_account_request_set_icon_name (account_req, priv->icon_name);
1440 tp_account_request_set_display_name (account_req, priv->display_name);
1442 if (priv->service != NULL)
1443 tp_account_request_set_service (account_req, priv->service);
1445 g_hash_table_iter_init (&iter, priv->parameters);
1446 while (g_hash_table_iter_next (&iter, &k, &v))
1448 const gchar *key = k;
1449 GVariant *value = v;
1451 tp_account_request_set_parameter (account_req, key, value);
1454 if (priv->storage_provider != NULL)
1456 tp_account_request_set_storage_provider (account_req,
1457 priv->storage_provider);
1460 tp_account_request_create_account_async (account_req,
1461 empathy_account_settings_created_cb, self);
1465 build_parameters_variant (EmpathyAccountSettings *self)
1467 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1468 GVariantBuilder *builder;
1469 GHashTableIter iter;
1472 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1474 g_hash_table_iter_init (&iter, priv->parameters);
1475 while (g_hash_table_iter_next (&iter, &k, &v))
1477 const gchar *key = k;
1478 GVariant *value = v;
1481 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1482 g_variant_new_variant (value));
1484 g_variant_builder_add_value (builder, entry);
1487 return g_variant_builder_end (builder);
1491 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1492 GAsyncReadyCallback callback,
1495 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1497 if (priv->apply_result != NULL)
1499 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1500 callback, user_data,
1501 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1505 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1506 callback, user_data, empathy_account_settings_apply_finish);
1508 /* We'll have to reconnect only if we change none DBus_Property on an
1509 * existing account. */
1510 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1512 if (priv->account == NULL)
1514 g_assert (priv->apply_result != NULL && priv->account == NULL);
1516 empathy_account_settings_do_create_account (settings);
1520 tp_account_update_parameters_vardict_async (priv->account,
1521 build_parameters_variant (settings),
1522 (const gchar **) priv->unset_parameters->data,
1523 empathy_account_settings_account_updated, settings);
1528 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1529 GAsyncResult *result,
1530 gboolean *reconnect_required,
1533 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1537 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1538 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1540 if (reconnect_required != NULL)
1541 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1542 G_SIMPLE_ASYNC_RESULT (result));
1548 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1551 EmpathyAccountSettingsPriv *priv;
1552 const gchar *account_path;
1553 const gchar *priv_account_path;
1555 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1556 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1558 priv = GET_PRIV (settings);
1560 if (priv->account == NULL)
1563 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1564 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1566 return (!tp_strdiff (account_path, priv_account_path));
1570 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1572 const gchar *pattern)
1574 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1576 GError *error = NULL;
1578 regex = g_regex_new (pattern, 0, 0, &error);
1581 g_warning ("Failed to create reg exp: %s", error->message);
1582 g_error_free (error);
1586 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1590 empathy_account_settings_parameter_is_valid (
1591 EmpathyAccountSettings *settings,
1594 EmpathyAccountSettingsPriv *priv;
1595 const GRegex *regex;
1597 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1599 priv = GET_PRIV (settings);
1601 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1603 /* first, look if it's set in our own parameters */
1604 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1607 /* if we did not unset the parameter, look if it's in the account */
1608 if (priv->account != NULL &&
1609 !empathy_account_settings_is_unset (settings, param))
1611 const GHashTable *account_params;
1613 account_params = tp_account_get_parameters (priv->account);
1614 if (tp_asv_lookup (account_params, param))
1622 /* test whether parameter value matches its regex */
1623 regex = g_hash_table_lookup (priv->param_regexps, param);
1629 value = empathy_account_settings_dup_string (settings, param);
1633 match = g_regex_match (regex, value, 0, NULL);
1643 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1645 EmpathyAccountSettingsPriv *priv;
1647 GHashTableIter iter;
1650 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1652 priv = GET_PRIV (settings);
1654 for (l = priv->required_params; l; l = l->next)
1656 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1660 g_hash_table_iter_init (&iter, priv->param_regexps);
1661 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1663 if (!empathy_account_settings_parameter_is_valid (settings, param))
1671 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1673 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1675 return priv->protocol_obj;
1679 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1681 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1683 return priv->supports_sasl;
1687 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1690 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1692 return tp_protocol_has_param (priv->protocol_obj, param);
1696 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1699 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1701 priv->uri_scheme_tel = associate;
1705 empathy_account_settings_has_uri_scheme_tel (
1706 EmpathyAccountSettings *self)
1708 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1710 return priv->uri_scheme_tel;
1714 empathy_account_settings_set_storage_provider (EmpathyAccountSettings *self,
1715 const gchar *storage)
1717 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1719 g_free (priv->storage_provider);
1720 priv->storage_provider = g_strdup (storage);
1724 empathy_account_settings_set_remember_password (EmpathyAccountSettings *self,
1727 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1729 priv->remember_password = remember;