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
22 #include "empathy-account-settings.h"
24 #include "empathy-connection-managers.h"
25 #include "empathy-keyring.h"
26 #include "empathy-presence-manager.h"
27 #include "empathy-utils.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
30 #include "empathy-debug.h"
32 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
34 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
42 PROP_DISPLAY_NAME_OVERRIDDEN,
47 PASSWORD_RETRIEVED = 1,
51 static gulong signals[LAST_SIGNAL] = { 0, };
53 /* private structure */
54 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
56 struct _EmpathyAccountSettingsPriv
58 gboolean dispose_has_run;
59 EmpathyConnectionManagers *managers;
60 TpAccountManager *account_manager;
62 TpConnectionManager *manager;
63 TpProtocol *protocol_obj;
71 gchar *storage_provider;
72 gboolean display_name_overridden;
75 gboolean supports_sasl;
76 gboolean remember_password;
79 gchar *password_original;
81 gboolean password_retrieved;
82 gboolean password_requested;
84 /* Parameter name (gchar *) -> parameter value (GVariant) */
85 GHashTable *parameters;
86 /* Keys are parameter names from the hash above (gchar *).
87 * Values are regular expresions that should match corresponding parameter
88 * values (GRegex *). Possible regexp patterns are defined in
89 * empathy-account-widget.c */
90 GHashTable *param_regexps;
91 GArray *unset_parameters;
92 GList *required_params;
94 gulong managers_ready_id;
95 gboolean preparing_protocol;
97 /* If TRUE, the account should have 'tel' in its
98 * Account.Interface.Addressing.URISchemes property. */
99 gboolean uri_scheme_tel;
100 /* If TRUE, Service property needs to be updated when applying changes */
101 gboolean update_service;
103 GSimpleAsyncResult *apply_result;
107 empathy_account_settings_init (EmpathyAccountSettings *obj)
109 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
110 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
114 /* allocate any data required by the object here */
115 priv->managers = empathy_connection_managers_dup_singleton ();
116 priv->account_manager = tp_account_manager_dup ();
118 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
119 g_free, (GDestroyNotify) g_variant_unref);
121 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
122 g_free, (GDestroyNotify) g_regex_unref);
124 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
126 priv->required_params = NULL;
129 static void empathy_account_settings_dispose (GObject *object);
130 static void empathy_account_settings_finalize (GObject *object);
131 static void empathy_account_settings_account_ready_cb (GObject *source_object,
132 GAsyncResult *result, gpointer user_data);
133 static void empathy_account_settings_managers_ready_cb (GObject *obj,
134 GParamSpec *pspec, gpointer user_data);
135 static void empathy_account_settings_check_readyness (
136 EmpathyAccountSettings *self);
139 empathy_account_settings_set_property (GObject *object,
144 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
145 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
150 priv->account = g_value_dup_object (value);
153 priv->cm_name = g_value_dup_string (value);
156 priv->protocol = g_value_dup_string (value);
159 priv->service = g_value_dup_string (value);
161 case PROP_DISPLAY_NAME:
162 priv->display_name = g_value_dup_string (value);
164 case PROP_DISPLAY_NAME_OVERRIDDEN:
165 priv->display_name_overridden = g_value_get_boolean (value);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 empathy_account_settings_get_property (GObject *object,
179 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
180 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
185 g_value_set_object (value, priv->account);
188 g_value_set_string (value, priv->cm_name);
191 g_value_set_string (value, priv->protocol);
194 g_value_set_string (value, priv->service);
196 case PROP_DISPLAY_NAME:
197 g_value_set_string (value, priv->display_name);
199 case PROP_DISPLAY_NAME_OVERRIDDEN:
200 g_value_set_boolean (value, priv->display_name_overridden);
203 g_value_set_boolean (value, priv->ready);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 empathy_account_settings_constructed (GObject *object)
214 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
215 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
217 if (priv->account != NULL)
219 g_free (priv->cm_name);
220 g_free (priv->protocol);
221 g_free (priv->service);
224 g_strdup (tp_account_get_cm_name (priv->account));
226 g_strdup (tp_account_get_protocol_name (priv->account));
228 g_strdup (tp_account_get_service (priv->account));
229 priv->icon_name = g_strdup
230 (tp_account_get_icon_name (priv->account));
234 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
237 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
239 empathy_account_settings_check_readyness (self);
243 GQuark features[] = {
244 TP_ACCOUNT_FEATURE_CORE,
245 TP_ACCOUNT_FEATURE_STORAGE,
246 TP_ACCOUNT_FEATURE_ADDRESSING,
249 if (priv->account != NULL)
251 tp_proxy_prepare_async (priv->account, features,
252 empathy_account_settings_account_ready_cb, self);
255 tp_g_signal_connect_object (priv->managers, "notify::ready",
256 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
260 empathy_account_settings_parent_class)->constructed != NULL)
262 empathy_account_settings_parent_class)->constructed (object);
267 empathy_account_settings_class_init (
268 EmpathyAccountSettingsClass *empathy_account_settings_class)
270 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
272 g_type_class_add_private (empathy_account_settings_class, sizeof
273 (EmpathyAccountSettingsPriv));
275 object_class->dispose = empathy_account_settings_dispose;
276 object_class->finalize = empathy_account_settings_finalize;
277 object_class->set_property = empathy_account_settings_set_property;
278 object_class->get_property = empathy_account_settings_get_property;
279 object_class->constructed = empathy_account_settings_constructed;
281 g_object_class_install_property (object_class, PROP_ACCOUNT,
282 g_param_spec_object ("account",
284 "The TpAccount backing these settings",
286 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
288 g_object_class_install_property (object_class, PROP_CM_NAME,
289 g_param_spec_string ("connection-manager",
290 "connection-manager",
291 "The name of the connection manager this account uses",
293 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
295 g_object_class_install_property (object_class, PROP_PROTOCOL,
296 g_param_spec_string ("protocol",
298 "The name of the protocol this account uses",
300 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
302 g_object_class_install_property (object_class, PROP_SERVICE,
303 g_param_spec_string ("service",
305 "The service of this account, or NULL",
307 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
309 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
310 g_param_spec_string ("display-name",
312 "The display name account these settings belong to",
314 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
316 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
317 g_param_spec_boolean ("display-name-overridden",
318 "display-name-overridden",
319 "Whether the display name for this account has been manually "
322 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
324 g_object_class_install_property (object_class, PROP_READY,
325 g_param_spec_boolean ("ready",
327 "Whether this account is ready to be used",
329 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
331 signals[PASSWORD_RETRIEVED] =
332 g_signal_new ("password-retrieved",
333 G_TYPE_FROM_CLASS (empathy_account_settings_class),
334 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
335 g_cclosure_marshal_generic,
340 empathy_account_settings_dispose (GObject *object)
342 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
343 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
345 if (priv->dispose_has_run)
348 priv->dispose_has_run = TRUE;
350 if (priv->managers_ready_id != 0)
351 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
352 priv->managers_ready_id = 0;
354 tp_clear_object (&priv->managers);
355 tp_clear_object (&priv->manager);
356 tp_clear_object (&priv->account_manager);
357 tp_clear_object (&priv->account);
358 tp_clear_object (&priv->protocol_obj);
360 /* release any references held by the object here */
361 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
362 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
366 empathy_account_settings_free_unset_parameters (
367 EmpathyAccountSettings *settings)
369 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
372 for (i = 0 ; i < priv->unset_parameters->len; i++)
373 g_free (g_array_index (priv->unset_parameters, gchar *, i));
375 g_array_set_size (priv->unset_parameters, 0);
379 empathy_account_settings_finalize (GObject *object)
381 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
382 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
385 /* free any data held directly by the object here */
386 g_free (priv->cm_name);
387 g_free (priv->protocol);
388 g_free (priv->service);
389 g_free (priv->display_name);
390 g_free (priv->icon_name);
391 g_free (priv->password);
392 g_free (priv->password_original);
393 g_free (priv->storage_provider);
395 if (priv->required_params != NULL)
397 for (l = priv->required_params; l; l = l->next)
399 g_list_free (priv->required_params);
402 g_hash_table_unref (priv->parameters);
403 g_hash_table_unref (priv->param_regexps);
405 empathy_account_settings_free_unset_parameters (self);
406 g_array_unref (priv->unset_parameters);
408 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
412 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
413 GAsyncResult *result,
416 EmpathyAccountSettings *self = user_data;
417 GError *error = NULL;
419 if (!tp_proxy_prepare_finish (source, result, &error))
421 DEBUG ("Failed to prepare protocol object: %s", error->message);
422 g_clear_error (&error);
426 empathy_account_settings_check_readyness (self);
430 empathy_account_settings_get_password_cb (GObject *source,
431 GAsyncResult *result,
434 EmpathyAccountSettings *self = user_data;
435 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
436 const gchar *password;
437 GError *error = NULL;
439 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
444 DEBUG ("Failed to get password: %s", error->message);
445 g_clear_error (&error);
448 /* It doesn't really matter if getting the password failed; that
449 * just means that it's not there, or let's act like that at
452 g_assert (priv->password == NULL);
454 priv->password = g_strdup (password);
455 priv->password_original = g_strdup (password);
457 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
460 static GVariant * empathy_account_settings_dup (
461 EmpathyAccountSettings *settings,
465 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
467 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
468 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
473 if (priv->account != NULL
474 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
477 if (!empathy_connection_managers_is_ready (priv->managers))
480 if (priv->manager == NULL)
482 priv->manager = empathy_connection_managers_get_cm (
483 priv->managers, priv->cm_name);
486 if (priv->manager == NULL)
489 g_object_ref (priv->manager);
491 if (priv->account != NULL)
493 g_free (priv->display_name);
495 g_strdup (tp_account_get_display_name (priv->account));
497 g_free (priv->icon_name);
499 g_strdup (tp_account_get_icon_name (priv->account));
501 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
504 if (priv->protocol_obj == NULL)
506 priv->protocol_obj = g_object_ref (
507 tp_connection_manager_get_protocol_object (priv->manager,
511 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
512 && !priv->preparing_protocol)
514 priv->preparing_protocol = TRUE;
515 tp_proxy_prepare_async (priv->protocol_obj, features,
516 empathy_account_settings_protocol_obj_prepared_cb, self);
521 if (tp_strv_contains (tp_protocol_get_authentication_types (
523 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
525 priv->supports_sasl = TRUE;
529 if (priv->required_params == NULL)
533 params = tp_protocol_dup_params (priv->protocol_obj);
534 for (l = params; l != NULL; l = g_list_next (l))
536 TpConnectionManagerParam *cur = l->data;
538 if (tp_connection_manager_param_is_required (cur))
540 priv->required_params = g_list_append (priv->required_params,
541 g_strdup (tp_connection_manager_param_get_name (cur)));
545 g_list_free_full (params,
546 (GDestroyNotify) tp_connection_manager_param_free);
549 /* priv->account won't be a proper account if it's the account
550 * assistant showing this widget. */
551 if (priv->supports_sasl && !priv->password_requested
552 && priv->account != NULL)
554 priv->password_requested = TRUE;
556 /* Make this call but don't block on its readiness. We'll signal
557 * if it's updated later with ::password-retrieved. */
558 empathy_keyring_get_account_password_async (priv->account,
559 empathy_account_settings_get_password_cb, self);
563 g_object_notify (G_OBJECT (self), "ready");
567 empathy_account_settings_account_ready_cb (GObject *source_object,
568 GAsyncResult *result,
571 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
572 TpAccount *account = TP_ACCOUNT (source_object);
573 GError *error = NULL;
575 if (!tp_proxy_prepare_finish (account, result, &error))
577 DEBUG ("Failed to prepare account: %s", error->message);
578 g_error_free (error);
582 empathy_account_settings_check_readyness (settings);
586 empathy_account_settings_managers_ready_cb (GObject *object,
590 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
592 empathy_account_settings_check_readyness (settings);
595 EmpathyAccountSettings *
596 empathy_account_settings_new (const gchar *connection_manager,
597 const gchar *protocol,
598 const gchar *service,
599 const char *display_name)
601 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
602 "connection-manager", connection_manager,
603 "protocol", protocol,
605 "display-name", display_name,
609 EmpathyAccountSettings *
610 empathy_account_settings_new_for_account (TpAccount *account)
612 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
618 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
620 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
622 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
624 return tp_protocol_dup_params (priv->protocol_obj);
628 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
630 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
636 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
638 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
640 return priv->cm_name;
644 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
646 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
648 return priv->protocol;
652 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
654 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
656 return priv->service;
660 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
661 const gchar *service)
663 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
665 if (!tp_strdiff (priv->service, service))
668 g_free (priv->service);
669 priv->service = g_strdup (service);
670 g_object_notify (G_OBJECT (settings), "service");
671 priv->update_service = TRUE;
675 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
677 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
679 return priv->icon_name;
683 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
685 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
687 return priv->display_name;
691 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
693 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
695 return priv->account;
699 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
702 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
706 a = priv->unset_parameters;
708 for (i = 0; i < a->len; i++)
710 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
717 static const TpConnectionManagerParam *
718 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
721 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
723 return tp_protocol_get_param (priv->protocol_obj, param);
727 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
730 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
734 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
737 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
741 for (idx = 0; idx < priv->unset_parameters->len; idx++)
743 val = g_array_index (priv->unset_parameters, gchar *, idx);
745 if (!tp_strdiff (val, param))
747 priv->unset_parameters =
748 g_array_remove_index (priv->unset_parameters, idx);
757 empathy_account_settings_dup_default (EmpathyAccountSettings *settings,
760 const TpConnectionManagerParam *p;
762 p = empathy_account_settings_get_tp_param (settings, param);
766 return tp_connection_manager_param_dup_default_variant (p);
770 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
773 const TpConnectionManagerParam *p;
775 p = empathy_account_settings_get_tp_param (settings, param);
780 return tp_connection_manager_param_get_dbus_signature (p);
784 empathy_account_settings_dup (EmpathyAccountSettings *settings,
787 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
790 /* Lookup the update parameters we set */
791 result = g_hash_table_lookup (priv->parameters, param);
793 return g_variant_ref (result);
795 /* If the parameters isn't unset use the accounts setting if any */
796 if (priv->account != NULL
797 && !empathy_account_settings_is_unset (settings, param))
799 GVariant *parameters;
801 parameters = tp_account_dup_parameters_vardict (priv->account);
802 result = g_variant_lookup_value (parameters, param, NULL);
803 g_variant_unref (parameters);
806 /* g_variant_lookup_value() is (transfer full) */
810 /* fallback to the default */
811 return empathy_account_settings_dup_default (settings, param);
815 empathy_account_settings_unset (EmpathyAccountSettings *settings,
818 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
820 if (empathy_account_settings_is_unset (settings, param))
823 if (priv->supports_sasl && !tp_strdiff (param, "password"))
825 g_free (priv->password);
826 priv->password = NULL;
830 v = g_strdup (param);
832 g_array_append_val (priv->unset_parameters, v);
833 g_hash_table_remove (priv->parameters, param);
837 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
839 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
841 g_hash_table_remove_all (priv->parameters);
842 empathy_account_settings_free_unset_parameters (settings);
844 g_free (priv->password);
845 priv->password = g_strdup (priv->password_original);
847 if (priv->account != NULL)
848 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
850 priv->uri_scheme_tel = FALSE;
854 empathy_account_settings_dup_string (EmpathyAccountSettings *settings,
857 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
859 gchar *result = NULL;
861 if (!tp_strdiff (param, "password") && priv->supports_sasl)
863 return g_strdup (priv->password);
866 v = empathy_account_settings_dup (settings, param);
870 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
871 result = g_variant_dup_string (v, NULL);
878 empathy_account_settings_dup_strv (EmpathyAccountSettings *settings,
884 v = empathy_account_settings_dup (settings, param);
888 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
889 result = g_variant_dup_strv (v, NULL);
896 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
902 v = empathy_account_settings_dup (settings, param);
906 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
907 ret = g_variant_get_byte (v);
908 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
909 ret = g_variant_get_int32 (v);
910 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
911 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
913 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
914 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
915 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
916 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
921 tmp = g_variant_print (v, TRUE);
922 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
931 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
937 v = empathy_account_settings_dup (settings, param);
941 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
942 ret = g_variant_get_byte (v);
943 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
944 ret = g_variant_get_int32 (v);
945 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
946 ret = g_variant_get_uint32 (v);
947 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
948 ret = g_variant_get_int64 (v);
949 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
950 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
955 tmp = g_variant_print (v, TRUE);
956 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
965 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
971 v = empathy_account_settings_dup (settings, param);
975 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
976 ret = g_variant_get_byte (v);
977 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
978 ret = MAX (0, g_variant_get_int32 (v));
979 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
980 ret = g_variant_get_uint32 (v);
981 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
982 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
983 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
984 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
989 tmp = g_variant_print (v, TRUE);
990 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
999 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1005 v = empathy_account_settings_dup (settings, param);
1009 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1010 ret = g_variant_get_byte (v);
1011 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1012 ret = MAX (0, g_variant_get_int32 (v));
1013 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1014 ret = g_variant_get_uint32 (v);
1015 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1016 ret = MAX (0, g_variant_get_int64 (v));
1017 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1018 ret = g_variant_get_uint64 (v);
1023 tmp = g_variant_print (v, TRUE);
1024 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1029 g_variant_unref (v);
1034 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1038 gboolean result = FALSE;
1040 v = empathy_account_settings_dup (settings, param);
1044 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1045 result = g_variant_get_boolean (v);
1051 empathy_account_settings_set (EmpathyAccountSettings *settings,
1055 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1057 g_return_if_fail (param != NULL);
1058 g_return_if_fail (v != NULL);
1060 if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1061 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1063 g_free (priv->password);
1064 priv->password = g_variant_dup_string (v, NULL);
1068 g_hash_table_insert (priv->parameters, g_strdup (param),
1069 g_variant_ref_sink (v));
1072 account_settings_remove_from_unset (settings, param);
1076 account_settings_display_name_set_cb (GObject *src,
1080 GError *error = NULL;
1081 TpAccount *account = TP_ACCOUNT (src);
1082 GSimpleAsyncResult *set_result = user_data;
1084 tp_account_set_display_name_finish (account, res, &error);
1088 g_simple_async_result_set_from_error (set_result, error);
1089 g_error_free (error);
1092 g_simple_async_result_complete (set_result);
1093 g_object_unref (set_result);
1097 empathy_account_settings_set_display_name_async (
1098 EmpathyAccountSettings *settings,
1100 GAsyncReadyCallback callback,
1103 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1104 GSimpleAsyncResult *result;
1106 g_return_if_fail (name != NULL);
1108 result = g_simple_async_result_new (G_OBJECT (settings),
1109 callback, user_data, empathy_account_settings_set_display_name_finish);
1111 if (!tp_strdiff (name, priv->display_name))
1114 g_simple_async_result_complete_in_idle (result);
1118 if (priv->account == NULL)
1120 if (priv->display_name != NULL)
1121 g_free (priv->display_name);
1123 priv->display_name = g_strdup (name);
1125 g_simple_async_result_complete_in_idle (result);
1130 tp_account_set_display_name_async (priv->account, name,
1131 account_settings_display_name_set_cb, result);
1135 empathy_account_settings_set_display_name_finish (
1136 EmpathyAccountSettings *settings,
1137 GAsyncResult *result,
1140 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1144 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1145 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1152 account_settings_icon_name_set_cb (GObject *src,
1156 GError *error = NULL;
1157 TpAccount *account = TP_ACCOUNT (src);
1158 GSimpleAsyncResult *set_result = user_data;
1160 tp_account_set_icon_name_finish (account, res, &error);
1164 g_simple_async_result_set_from_error (set_result, error);
1165 g_error_free (error);
1168 g_simple_async_result_complete (set_result);
1169 g_object_unref (set_result);
1173 empathy_account_settings_set_icon_name_async (
1174 EmpathyAccountSettings *settings,
1176 GAsyncReadyCallback callback,
1179 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1180 GSimpleAsyncResult *result;
1182 g_return_if_fail (name != NULL);
1184 result = g_simple_async_result_new (G_OBJECT (settings),
1185 callback, user_data, empathy_account_settings_set_icon_name_finish);
1187 if (priv->account == NULL)
1189 if (priv->icon_name != NULL)
1190 g_free (priv->icon_name);
1192 priv->icon_name = g_strdup (name);
1194 g_simple_async_result_complete_in_idle (result);
1199 tp_account_set_icon_name_async (priv->account, name,
1200 account_settings_icon_name_set_cb, result);
1204 empathy_account_settings_set_icon_name_finish (
1205 EmpathyAccountSettings *settings,
1206 GAsyncResult *result,
1209 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1213 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1214 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1221 empathy_account_settings_processed_password (GObject *source,
1222 GAsyncResult *result,
1224 gpointer finish_func)
1226 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1227 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1228 GSimpleAsyncResult *r;
1229 GError *error = NULL;
1230 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1233 g_free (priv->password_original);
1234 priv->password_original = g_strdup (priv->password);
1236 if (!func (TP_ACCOUNT (source), result, &error))
1238 g_simple_async_result_set_from_error (priv->apply_result, error);
1239 g_error_free (error);
1242 empathy_account_settings_discard_changes (settings);
1244 r = priv->apply_result;
1245 priv->apply_result = NULL;
1247 g_simple_async_result_complete (r);
1252 empathy_account_settings_set_password_cb (GObject *source,
1253 GAsyncResult *result,
1256 empathy_account_settings_processed_password (source, result, user_data,
1257 empathy_keyring_set_account_password_finish);
1261 empathy_account_settings_delete_password_cb (GObject *source,
1262 GAsyncResult *result,
1265 empathy_account_settings_processed_password (source, result, user_data,
1266 empathy_keyring_delete_account_password_finish);
1270 update_account_uri_schemes (EmpathyAccountSettings *self)
1272 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1274 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1278 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1279 priv->uri_scheme_tel, NULL, NULL);
1283 set_service_cb (GObject *source,
1284 GAsyncResult *result,
1287 GError *error = NULL;
1289 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1291 DEBUG ("Failed to set Account.Service: %s", error->message);
1292 g_error_free (error);
1297 update_account_service (EmpathyAccountSettings *self)
1299 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1301 if (!priv->update_service)
1304 tp_account_set_service_async (priv->account,
1305 priv->service != NULL ? priv->service : "", set_service_cb, self);
1309 empathy_account_settings_account_updated (GObject *source,
1310 GAsyncResult *result,
1313 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1314 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1315 GSimpleAsyncResult *r;
1316 GError *error = NULL;
1317 GStrv reconnect_required = NULL;
1319 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1320 result, &reconnect_required, &error))
1322 g_simple_async_result_set_from_error (priv->apply_result, error);
1323 g_error_free (error);
1327 update_account_uri_schemes (settings);
1328 update_account_service (settings);
1330 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1331 g_strv_length (reconnect_required) > 0);
1333 /* Only set the password in the keyring if the CM supports SASL. */
1334 if (priv->supports_sasl)
1336 if (priv->password != NULL)
1338 /* FIXME: we shouldn't save the password if we
1339 * can't (MaySaveResponse=False) but we don't have API to check that
1340 * at this point (fdo #35382). */
1341 empathy_keyring_set_account_password_async (priv->account,
1342 priv->password, priv->remember_password,
1343 empathy_account_settings_set_password_cb, settings);
1347 empathy_keyring_delete_account_password_async (priv->account,
1348 empathy_account_settings_delete_password_cb, settings);
1355 empathy_account_settings_discard_changes (settings);
1357 r = priv->apply_result;
1358 priv->apply_result = NULL;
1360 g_simple_async_result_complete (r);
1362 g_strfreev (reconnect_required);
1366 empathy_account_settings_created_cb (GObject *source,
1367 GAsyncResult *result,
1370 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1371 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1372 GError *error = NULL;
1373 GSimpleAsyncResult *r;
1375 priv->account = tp_account_request_create_account_finish (
1376 TP_ACCOUNT_REQUEST (source), result, &error);
1378 if (priv->account == NULL)
1380 g_simple_async_result_set_from_error (priv->apply_result, error);
1384 if (priv->supports_sasl && priv->password != NULL)
1386 /* Save the password before connecting */
1387 /* FIXME: we shouldn't save the password if we
1388 * can't (MaySaveResponse=False) but we don't have API to check that
1389 * at this point (fdo #35382). */
1390 empathy_keyring_set_account_password_async (priv->account,
1391 priv->password, priv->remember_password,
1392 empathy_account_settings_set_password_cb,
1397 update_account_uri_schemes (settings);
1399 empathy_account_settings_discard_changes (settings);
1402 r = priv->apply_result;
1403 priv->apply_result = NULL;
1405 g_simple_async_result_complete (r);
1410 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1412 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1413 TpAccountRequest *account_req;
1414 TpConnectionPresenceType type;
1417 EmpathyPresenceManager *presence_mgr;
1418 GHashTableIter iter;
1421 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1422 priv->protocol, "New Account");
1424 presence_mgr = empathy_presence_manager_dup_singleton ();
1425 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1427 g_object_unref (presence_mgr);
1429 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1431 tp_account_request_set_requested_presence (account_req, type,
1435 tp_account_request_set_icon_name (account_req, priv->icon_name);
1437 tp_account_request_set_display_name (account_req, priv->display_name);
1439 if (priv->service != NULL)
1440 tp_account_request_set_service (account_req, priv->service);
1442 g_hash_table_iter_init (&iter, priv->parameters);
1443 while (g_hash_table_iter_next (&iter, &k, &v))
1445 const gchar *key = k;
1446 GVariant *value = v;
1448 tp_account_request_set_parameter (account_req, key, value);
1451 if (priv->storage_provider != NULL)
1453 tp_account_request_set_storage_provider (account_req,
1454 priv->storage_provider);
1457 tp_account_request_create_account_async (account_req,
1458 empathy_account_settings_created_cb, self);
1462 build_parameters_variant (EmpathyAccountSettings *self)
1464 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1465 GVariantBuilder *builder;
1466 GHashTableIter iter;
1469 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1471 g_hash_table_iter_init (&iter, priv->parameters);
1472 while (g_hash_table_iter_next (&iter, &k, &v))
1474 const gchar *key = k;
1475 GVariant *value = v;
1478 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1479 g_variant_new_variant (value));
1481 g_variant_builder_add_value (builder, entry);
1484 return g_variant_builder_end (builder);
1488 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1489 GAsyncReadyCallback callback,
1492 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1494 if (priv->apply_result != NULL)
1496 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1497 callback, user_data,
1498 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1502 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1503 callback, user_data, empathy_account_settings_apply_finish);
1505 /* We'll have to reconnect only if we change none DBus_Property on an
1506 * existing account. */
1507 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1509 if (priv->account == NULL)
1511 g_assert (priv->apply_result != NULL && priv->account == NULL);
1513 empathy_account_settings_do_create_account (settings);
1517 tp_account_update_parameters_vardict_async (priv->account,
1518 build_parameters_variant (settings),
1519 (const gchar **) priv->unset_parameters->data,
1520 empathy_account_settings_account_updated, settings);
1525 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1526 GAsyncResult *result,
1527 gboolean *reconnect_required,
1530 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1534 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1535 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1537 if (reconnect_required != NULL)
1538 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1539 G_SIMPLE_ASYNC_RESULT (result));
1545 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1548 EmpathyAccountSettingsPriv *priv;
1549 const gchar *account_path;
1550 const gchar *priv_account_path;
1552 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1553 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1555 priv = GET_PRIV (settings);
1557 if (priv->account == NULL)
1560 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1561 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1563 return (!tp_strdiff (account_path, priv_account_path));
1567 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1569 const gchar *pattern)
1571 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1573 GError *error = NULL;
1575 regex = g_regex_new (pattern, 0, 0, &error);
1578 g_warning ("Failed to create reg exp: %s", error->message);
1579 g_error_free (error);
1583 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1587 empathy_account_settings_parameter_is_valid (
1588 EmpathyAccountSettings *settings,
1591 EmpathyAccountSettingsPriv *priv;
1592 const GRegex *regex;
1594 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1596 priv = GET_PRIV (settings);
1598 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1600 /* first, look if it's set in our own parameters */
1601 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1604 /* if we did not unset the parameter, look if it's in the account */
1605 if (priv->account != NULL &&
1606 !empathy_account_settings_is_unset (settings, param))
1608 const GHashTable *account_params;
1610 account_params = tp_account_get_parameters (priv->account);
1611 if (tp_asv_lookup (account_params, param))
1619 /* test whether parameter value matches its regex */
1620 regex = g_hash_table_lookup (priv->param_regexps, param);
1626 value = empathy_account_settings_dup_string (settings, param);
1630 match = g_regex_match (regex, value, 0, NULL);
1640 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1642 EmpathyAccountSettingsPriv *priv;
1644 GHashTableIter iter;
1647 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1649 priv = GET_PRIV (settings);
1651 for (l = priv->required_params; l; l = l->next)
1653 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1657 g_hash_table_iter_init (&iter, priv->param_regexps);
1658 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1660 if (!empathy_account_settings_parameter_is_valid (settings, param))
1668 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1670 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1672 return priv->protocol_obj;
1676 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1678 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1680 return priv->supports_sasl;
1684 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1687 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1689 return tp_protocol_has_param (priv->protocol_obj, param);
1693 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1696 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1698 priv->uri_scheme_tel = associate;
1702 empathy_account_settings_has_uri_scheme_tel (
1703 EmpathyAccountSettings *self)
1705 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1707 return priv->uri_scheme_tel;
1711 empathy_account_settings_set_storage_provider (EmpathyAccountSettings *self,
1712 const gchar *storage)
1714 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1716 g_free (priv->storage_provider);
1717 priv->storage_provider = g_strdup (storage);
1721 empathy_account_settings_set_remember_password (EmpathyAccountSettings *self,
1724 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1726 priv->remember_password = remember;