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 gboolean display_name_overridden;
82 gboolean supports_sasl;
83 gboolean password_changed;
86 gchar *password_original;
88 gboolean password_retrieved;
89 gboolean password_requested;
91 /* Parameter name (gchar *) -> parameter value (GVariant) */
92 GHashTable *parameters;
93 /* Keys are parameter names from the hash above (gchar *).
94 * Values are regular expresions that should match corresponding parameter
95 * values (GRegex *). Possible regexp patterns are defined in
96 * empathy-account-widget.c */
97 GHashTable *param_regexps;
98 GArray *unset_parameters;
99 GList *required_params;
101 gulong managers_ready_id;
102 gboolean preparing_protocol;
104 /* If TRUE, the account should have 'tel' in its
105 * Account.Interface.Addressing.URISchemes property. */
106 gboolean uri_scheme_tel;
107 /* If TRUE, Service property needs to be updated when applying changes */
108 gboolean update_service;
110 GSimpleAsyncResult *apply_result;
114 empathy_account_settings_init (EmpathyAccountSettings *obj)
116 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
117 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
121 /* allocate any data required by the object here */
122 priv->managers = empathy_connection_managers_dup_singleton ();
123 priv->account_manager = tp_account_manager_dup ();
125 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
126 g_free, (GDestroyNotify) g_variant_unref);
128 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
129 g_free, (GDestroyNotify) g_regex_unref);
131 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
133 priv->required_params = NULL;
136 static void empathy_account_settings_dispose (GObject *object);
137 static void empathy_account_settings_finalize (GObject *object);
138 static void empathy_account_settings_account_ready_cb (GObject *source_object,
139 GAsyncResult *result, gpointer user_data);
140 static void empathy_account_settings_managers_ready_cb (GObject *obj,
141 GParamSpec *pspec, gpointer user_data);
142 static void empathy_account_settings_check_readyness (
143 EmpathyAccountSettings *self);
146 empathy_account_settings_set_property (GObject *object,
151 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
152 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
157 priv->account = g_value_dup_object (value);
160 priv->cm_name = g_value_dup_string (value);
163 priv->protocol = g_value_dup_string (value);
166 priv->service = g_value_dup_string (value);
168 case PROP_DISPLAY_NAME:
169 priv->display_name = g_value_dup_string (value);
171 case PROP_DISPLAY_NAME_OVERRIDDEN:
172 priv->display_name_overridden = g_value_get_boolean (value);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 empathy_account_settings_get_property (GObject *object,
186 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
187 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
192 g_value_set_object (value, priv->account);
195 g_value_set_string (value, priv->cm_name);
198 g_value_set_string (value, priv->protocol);
201 g_value_set_string (value, priv->service);
203 case PROP_DISPLAY_NAME:
204 g_value_set_string (value, priv->display_name);
206 case PROP_DISPLAY_NAME_OVERRIDDEN:
207 g_value_set_boolean (value, priv->display_name_overridden);
210 g_value_set_boolean (value, priv->ready);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 empathy_account_settings_constructed (GObject *object)
221 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
222 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
224 if (priv->account != NULL)
226 g_free (priv->cm_name);
227 g_free (priv->protocol);
228 g_free (priv->service);
231 g_strdup (tp_account_get_connection_manager (priv->account));
233 g_strdup (tp_account_get_protocol (priv->account));
235 g_strdup (tp_account_get_service (priv->account));
236 priv->icon_name = g_strdup
237 (tp_account_get_icon_name (priv->account));
241 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
244 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
246 empathy_account_settings_check_readyness (self);
250 GQuark features[] = {
251 TP_ACCOUNT_FEATURE_CORE,
252 TP_ACCOUNT_FEATURE_STORAGE,
253 TP_ACCOUNT_FEATURE_ADDRESSING,
256 if (priv->account != NULL)
258 tp_proxy_prepare_async (priv->account, features,
259 empathy_account_settings_account_ready_cb, self);
262 tp_g_signal_connect_object (priv->managers, "notify::ready",
263 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
267 empathy_account_settings_parent_class)->constructed != NULL)
269 empathy_account_settings_parent_class)->constructed (object);
274 empathy_account_settings_class_init (
275 EmpathyAccountSettingsClass *empathy_account_settings_class)
277 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
279 g_type_class_add_private (empathy_account_settings_class, sizeof
280 (EmpathyAccountSettingsPriv));
282 object_class->dispose = empathy_account_settings_dispose;
283 object_class->finalize = empathy_account_settings_finalize;
284 object_class->set_property = empathy_account_settings_set_property;
285 object_class->get_property = empathy_account_settings_get_property;
286 object_class->constructed = empathy_account_settings_constructed;
288 g_object_class_install_property (object_class, PROP_ACCOUNT,
289 g_param_spec_object ("account",
291 "The TpAccount backing these settings",
293 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
295 g_object_class_install_property (object_class, PROP_CM_NAME,
296 g_param_spec_string ("connection-manager",
297 "connection-manager",
298 "The name of the connection manager this account uses",
300 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
302 g_object_class_install_property (object_class, PROP_PROTOCOL,
303 g_param_spec_string ("protocol",
305 "The name of the protocol this account uses",
307 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
309 g_object_class_install_property (object_class, PROP_SERVICE,
310 g_param_spec_string ("service",
312 "The service of this account, or NULL",
314 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
316 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
317 g_param_spec_string ("display-name",
319 "The display name account these settings belong to",
321 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
323 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
324 g_param_spec_boolean ("display-name-overridden",
325 "display-name-overridden",
326 "Whether the display name for this account has been manually "
329 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
331 g_object_class_install_property (object_class, PROP_READY,
332 g_param_spec_boolean ("ready",
334 "Whether this account is ready to be used",
336 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
338 signals[PASSWORD_RETRIEVED] =
339 g_signal_new ("password-retrieved",
340 G_TYPE_FROM_CLASS (empathy_account_settings_class),
341 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
342 g_cclosure_marshal_generic,
347 empathy_account_settings_dispose (GObject *object)
349 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
350 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
352 if (priv->dispose_has_run)
355 priv->dispose_has_run = TRUE;
357 if (priv->managers_ready_id != 0)
358 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
359 priv->managers_ready_id = 0;
361 tp_clear_object (&priv->managers);
362 tp_clear_object (&priv->manager);
363 tp_clear_object (&priv->account_manager);
364 tp_clear_object (&priv->account);
365 tp_clear_object (&priv->protocol_obj);
367 /* release any references held by the object here */
368 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
369 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
373 empathy_account_settings_free_unset_parameters (
374 EmpathyAccountSettings *settings)
376 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
379 for (i = 0 ; i < priv->unset_parameters->len; i++)
380 g_free (g_array_index (priv->unset_parameters, gchar *, i));
382 g_array_set_size (priv->unset_parameters, 0);
386 empathy_account_settings_finalize (GObject *object)
388 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
389 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
392 /* free any data held directly by the object here */
393 g_free (priv->cm_name);
394 g_free (priv->protocol);
395 g_free (priv->service);
396 g_free (priv->display_name);
397 g_free (priv->icon_name);
398 g_free (priv->password);
399 g_free (priv->password_original);
401 if (priv->required_params != NULL)
403 for (l = priv->required_params; l; l = l->next)
405 g_list_free (priv->required_params);
408 g_hash_table_unref (priv->parameters);
409 g_hash_table_unref (priv->param_regexps);
411 empathy_account_settings_free_unset_parameters (self);
412 g_array_unref (priv->unset_parameters);
414 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
418 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
419 GAsyncResult *result,
422 EmpathyAccountSettings *self = user_data;
423 GError *error = NULL;
425 if (!tp_proxy_prepare_finish (source, result, &error))
427 DEBUG ("Failed to prepare protocol object: %s", error->message);
428 g_clear_error (&error);
432 empathy_account_settings_check_readyness (self);
436 empathy_account_settings_get_password_cb (GObject *source,
437 GAsyncResult *result,
440 EmpathyAccountSettings *self = user_data;
441 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
442 const gchar *password;
443 GError *error = NULL;
445 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
450 DEBUG ("Failed to get password: %s", error->message);
451 g_clear_error (&error);
454 /* It doesn't really matter if getting the password failed; that
455 * just means that it's not there, or let's act like that at
458 g_assert (priv->password == NULL);
460 priv->password = g_strdup (password);
461 priv->password_original = g_strdup (password);
463 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
467 empathy_account_settings_migrate_password_cb (GObject *source,
468 GAsyncResult *result,
471 TpAccount *account = TP_ACCOUNT (source);
472 GError *error = NULL;
473 EmpathyAccountSettings *self = user_data;
474 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
476 const gchar *unset[] = { "password", NULL };
478 if (!empathy_keyring_set_account_password_finish (account, result, &error))
480 DEBUG ("Failed to set password: %s", error->message);
481 g_clear_error (&error);
485 /* Now clear the password MC has stored. */
486 empty = tp_asv_new (NULL, NULL);
487 tp_account_update_parameters_async (priv->account,
488 empty, unset, NULL, NULL);
490 g_hash_table_remove (priv->parameters, "password");
492 g_hash_table_unref (empty);
495 static GVariant * empathy_account_settings_dup (
496 EmpathyAccountSettings *settings,
500 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
502 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
504 const gchar *password;
506 v = empathy_account_settings_dup (self, "password");
510 if (!priv->supports_sasl)
513 /* mission-control still has our password, although the CM
514 * supports SASL. Let's try migrating it. */
516 DEBUG ("Trying to migrate password parameter from MC to the "
517 "keyring ourselves for account %s",
518 tp_account_get_path_suffix (priv->account));
520 /* I can't imagine why this would fail. */
521 if (!g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
524 password = g_variant_get_string (v, NULL);
526 if (EMP_STR_EMPTY (password))
529 empathy_keyring_set_account_password_async (priv->account, password,
530 empathy_account_settings_migrate_password_cb, self);
532 /* We don't want to request the password again, we
533 * already know it. */
534 priv->password_requested = TRUE;
536 priv->password = g_strdup (password);
537 priv->password_original = g_strdup (password);
544 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
546 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
547 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
552 if (priv->account != NULL
553 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
556 if (!empathy_connection_managers_is_ready (priv->managers))
559 if (priv->manager == NULL)
561 priv->manager = empathy_connection_managers_get_cm (
562 priv->managers, priv->cm_name);
565 if (priv->manager == NULL)
568 g_object_ref (priv->manager);
570 if (priv->account != NULL)
572 g_free (priv->display_name);
574 g_strdup (tp_account_get_display_name (priv->account));
576 g_free (priv->icon_name);
578 g_strdup (tp_account_get_icon_name (priv->account));
580 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
583 if (priv->protocol_obj == NULL)
585 priv->protocol_obj = g_object_ref (
586 tp_connection_manager_get_protocol_object (priv->manager,
590 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
591 && !priv->preparing_protocol)
593 priv->preparing_protocol = TRUE;
594 tp_proxy_prepare_async (priv->protocol_obj, features,
595 empathy_account_settings_protocol_obj_prepared_cb, self);
600 if (tp_strv_contains (tp_protocol_get_authentication_types (
602 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
604 priv->supports_sasl = TRUE;
608 if (priv->required_params == NULL)
612 params = tp_protocol_dup_params (priv->protocol_obj);
613 for (l = params; l != NULL; l = g_list_next (l))
615 TpConnectionManagerParam *cur = l->data;
617 if (tp_connection_manager_param_is_required (cur))
619 priv->required_params = g_list_append (priv->required_params,
620 g_strdup (tp_connection_manager_param_get_name (cur)));
624 g_list_free_full (params,
625 (GDestroyNotify) tp_connection_manager_param_free);
628 /* NOTE: When removing MC migration code, remove this call, and the
629 * function it's calling. That's it. */
630 empathy_account_settings_try_migrating_password (self);
632 /* priv->account won't be a proper account if it's the account
633 * assistant showing this widget. */
634 if (priv->supports_sasl && !priv->password_requested
635 && priv->account != NULL)
637 priv->password_requested = TRUE;
639 /* Make this call but don't block on its readiness. We'll signal
640 * if it's updated later with ::password-retrieved. */
641 empathy_keyring_get_account_password_async (priv->account,
642 empathy_account_settings_get_password_cb, self);
646 g_object_notify (G_OBJECT (self), "ready");
650 empathy_account_settings_account_ready_cb (GObject *source_object,
651 GAsyncResult *result,
654 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
655 TpAccount *account = TP_ACCOUNT (source_object);
656 GError *error = NULL;
658 if (!tp_proxy_prepare_finish (account, result, &error))
660 DEBUG ("Failed to prepare account: %s", error->message);
661 g_error_free (error);
665 empathy_account_settings_check_readyness (settings);
669 empathy_account_settings_managers_ready_cb (GObject *object,
673 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
675 empathy_account_settings_check_readyness (settings);
678 EmpathyAccountSettings *
679 empathy_account_settings_new (const gchar *connection_manager,
680 const gchar *protocol,
681 const gchar *service,
682 const char *display_name)
684 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
685 "connection-manager", connection_manager,
686 "protocol", protocol,
688 "display-name", display_name,
692 EmpathyAccountSettings *
693 empathy_account_settings_new_for_account (TpAccount *account)
695 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
701 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
703 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
705 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
707 return tp_protocol_dup_params (priv->protocol_obj);
711 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
713 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
719 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
721 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
723 return priv->cm_name;
727 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
729 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
731 return priv->protocol;
735 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
737 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
739 return priv->service;
743 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
744 const gchar *service)
746 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
748 if (!tp_strdiff (priv->service, service))
751 g_free (priv->service);
752 priv->service = g_strdup (service);
753 g_object_notify (G_OBJECT (settings), "service");
754 priv->update_service = TRUE;
758 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
760 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
762 return priv->icon_name;
766 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
768 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
770 return priv->display_name;
774 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
776 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
778 return priv->account;
782 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
785 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
789 a = priv->unset_parameters;
791 for (i = 0; i < a->len; i++)
793 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
800 static const TpConnectionManagerParam *
801 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
804 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
806 return tp_protocol_get_param (priv->protocol_obj, param);
810 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
813 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
817 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
820 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
824 for (idx = 0; idx < priv->unset_parameters->len; idx++)
826 val = g_array_index (priv->unset_parameters, gchar *, idx);
828 if (!tp_strdiff (val, param))
830 priv->unset_parameters =
831 g_array_remove_index (priv->unset_parameters, idx);
840 empathy_account_settings_dup_default (EmpathyAccountSettings *settings,
843 const TpConnectionManagerParam *p;
845 p = empathy_account_settings_get_tp_param (settings, param);
849 return tp_connection_manager_param_dup_default_variant (p);
853 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
856 const TpConnectionManagerParam *p;
858 p = empathy_account_settings_get_tp_param (settings, param);
863 return tp_connection_manager_param_get_dbus_signature (p);
867 empathy_account_settings_dup (EmpathyAccountSettings *settings,
870 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
873 /* Lookup the update parameters we set */
874 result = g_hash_table_lookup (priv->parameters, param);
876 return g_variant_ref (result);
878 /* If the parameters isn't unset use the accounts setting if any */
879 if (priv->account != NULL
880 && !empathy_account_settings_is_unset (settings, param))
882 GVariant *parameters;
884 parameters = tp_account_dup_parameters_vardict (priv->account);
885 result = g_variant_lookup_value (parameters, param, NULL);
886 g_variant_unref (parameters);
889 /* g_variant_lookup_value() is (transfer full) */
893 /* fallback to the default */
894 return empathy_account_settings_dup_default (settings, param);
898 empathy_account_settings_unset (EmpathyAccountSettings *settings,
901 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
903 if (empathy_account_settings_is_unset (settings, param))
906 if (priv->supports_sasl && !tp_strdiff (param, "password"))
908 g_free (priv->password);
909 priv->password = NULL;
910 priv->password_changed = TRUE;
914 v = g_strdup (param);
916 g_array_append_val (priv->unset_parameters, v);
917 g_hash_table_remove (priv->parameters, param);
921 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
923 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
925 g_hash_table_remove_all (priv->parameters);
926 empathy_account_settings_free_unset_parameters (settings);
928 priv->password_changed = FALSE;
929 g_free (priv->password);
930 priv->password = g_strdup (priv->password_original);
932 if (priv->account != NULL)
933 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
935 priv->uri_scheme_tel = FALSE;
939 empathy_account_settings_dup_string (EmpathyAccountSettings *settings,
942 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
944 gchar *result = NULL;
946 if (!tp_strdiff (param, "password") && priv->supports_sasl)
948 return g_strdup (priv->password);
951 v = empathy_account_settings_dup (settings, param);
955 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
956 result = g_variant_dup_string (v, NULL);
963 empathy_account_settings_dup_strv (EmpathyAccountSettings *settings,
969 v = empathy_account_settings_dup (settings, param);
973 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
974 result = g_variant_dup_strv (v, NULL);
981 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
987 v = empathy_account_settings_dup (settings, param);
991 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
992 ret = g_variant_get_byte (v);
993 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
994 ret = g_variant_get_int32 (v);
995 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
996 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
998 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
999 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
1000 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1001 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
1006 tmp = g_variant_print (v, TRUE);
1007 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1011 g_variant_unref (v);
1016 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
1022 v = empathy_account_settings_dup (settings, param);
1026 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1027 ret = g_variant_get_byte (v);
1028 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1029 ret = g_variant_get_int32 (v);
1030 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1031 ret = g_variant_get_uint32 (v);
1032 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1033 ret = g_variant_get_int64 (v);
1034 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1035 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1040 tmp = g_variant_print (v, TRUE);
1041 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1045 g_variant_unref (v);
1050 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1056 v = empathy_account_settings_dup (settings, param);
1060 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1061 ret = g_variant_get_byte (v);
1062 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1063 ret = MAX (0, g_variant_get_int32 (v));
1064 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1065 ret = g_variant_get_uint32 (v);
1066 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1067 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
1068 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1069 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
1074 tmp = g_variant_print (v, TRUE);
1075 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1079 g_variant_unref (v);
1084 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1090 v = empathy_account_settings_dup (settings, param);
1094 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1095 ret = g_variant_get_byte (v);
1096 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1097 ret = MAX (0, g_variant_get_int32 (v));
1098 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1099 ret = g_variant_get_uint32 (v);
1100 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1101 ret = MAX (0, g_variant_get_int64 (v));
1102 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1103 ret = g_variant_get_uint64 (v);
1108 tmp = g_variant_print (v, TRUE);
1109 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1114 g_variant_unref (v);
1119 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1123 gboolean result = FALSE;
1125 v = empathy_account_settings_dup (settings, param);
1129 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1130 result = g_variant_get_boolean (v);
1136 empathy_account_settings_set (EmpathyAccountSettings *settings,
1140 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1142 g_return_if_fail (param != NULL);
1143 g_return_if_fail (v != NULL);
1145 if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1146 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1148 g_free (priv->password);
1149 priv->password = g_variant_dup_string (v, NULL);
1150 priv->password_changed = TRUE;
1154 g_hash_table_insert (priv->parameters, g_strdup (param),
1155 g_variant_ref_sink (v));
1158 account_settings_remove_from_unset (settings, param);
1162 account_settings_display_name_set_cb (GObject *src,
1166 GError *error = NULL;
1167 TpAccount *account = TP_ACCOUNT (src);
1168 GSimpleAsyncResult *set_result = user_data;
1170 tp_account_set_display_name_finish (account, res, &error);
1174 g_simple_async_result_set_from_error (set_result, error);
1175 g_error_free (error);
1178 g_simple_async_result_complete (set_result);
1179 g_object_unref (set_result);
1183 empathy_account_settings_set_display_name_async (
1184 EmpathyAccountSettings *settings,
1186 GAsyncReadyCallback callback,
1189 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1190 GSimpleAsyncResult *result;
1192 g_return_if_fail (name != NULL);
1194 result = g_simple_async_result_new (G_OBJECT (settings),
1195 callback, user_data, empathy_account_settings_set_display_name_finish);
1197 if (!tp_strdiff (name, priv->display_name))
1200 g_simple_async_result_complete_in_idle (result);
1204 if (priv->account == NULL)
1206 if (priv->display_name != NULL)
1207 g_free (priv->display_name);
1209 priv->display_name = g_strdup (name);
1211 g_simple_async_result_complete_in_idle (result);
1216 tp_account_set_display_name_async (priv->account, name,
1217 account_settings_display_name_set_cb, result);
1221 empathy_account_settings_set_display_name_finish (
1222 EmpathyAccountSettings *settings,
1223 GAsyncResult *result,
1226 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1230 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1231 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1238 account_settings_icon_name_set_cb (GObject *src,
1242 GError *error = NULL;
1243 TpAccount *account = TP_ACCOUNT (src);
1244 GSimpleAsyncResult *set_result = user_data;
1246 tp_account_set_icon_name_finish (account, res, &error);
1250 g_simple_async_result_set_from_error (set_result, error);
1251 g_error_free (error);
1254 g_simple_async_result_complete (set_result);
1255 g_object_unref (set_result);
1259 empathy_account_settings_set_icon_name_async (
1260 EmpathyAccountSettings *settings,
1262 GAsyncReadyCallback callback,
1265 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1266 GSimpleAsyncResult *result;
1268 g_return_if_fail (name != NULL);
1270 result = g_simple_async_result_new (G_OBJECT (settings),
1271 callback, user_data, empathy_account_settings_set_icon_name_finish);
1273 if (priv->account == NULL)
1275 if (priv->icon_name != NULL)
1276 g_free (priv->icon_name);
1278 priv->icon_name = g_strdup (name);
1280 g_simple_async_result_complete_in_idle (result);
1285 tp_account_set_icon_name_async (priv->account, name,
1286 account_settings_icon_name_set_cb, result);
1290 empathy_account_settings_set_icon_name_finish (
1291 EmpathyAccountSettings *settings,
1292 GAsyncResult *result,
1295 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1299 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1300 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1307 empathy_account_settings_processed_password (GObject *source,
1308 GAsyncResult *result,
1310 gpointer finish_func)
1312 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1313 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1314 GSimpleAsyncResult *r;
1315 GError *error = NULL;
1316 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1319 g_free (priv->password_original);
1320 priv->password_original = g_strdup (priv->password);
1322 if (!func (TP_ACCOUNT (source), result, &error))
1324 g_simple_async_result_set_from_error (priv->apply_result, error);
1325 g_error_free (error);
1328 empathy_account_settings_discard_changes (settings);
1330 r = priv->apply_result;
1331 priv->apply_result = NULL;
1333 g_simple_async_result_complete (r);
1338 empathy_account_settings_set_password_cb (GObject *source,
1339 GAsyncResult *result,
1342 empathy_account_settings_processed_password (source, result, user_data,
1343 empathy_keyring_set_account_password_finish);
1347 empathy_account_settings_delete_password_cb (GObject *source,
1348 GAsyncResult *result,
1351 empathy_account_settings_processed_password (source, result, user_data,
1352 empathy_keyring_delete_account_password_finish);
1356 update_account_uri_schemes (EmpathyAccountSettings *self)
1358 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1360 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1364 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1365 priv->uri_scheme_tel, NULL, NULL);
1369 set_service_cb (GObject *source,
1370 GAsyncResult *result,
1373 GError *error = NULL;
1375 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1377 DEBUG ("Failed to set Account.Service: %s", error->message);
1378 g_error_free (error);
1383 update_account_service (EmpathyAccountSettings *self)
1385 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1387 if (!priv->update_service)
1390 tp_account_set_service_async (priv->account,
1391 priv->service != NULL ? priv->service : "", set_service_cb, self);
1395 empathy_account_settings_account_updated (GObject *source,
1396 GAsyncResult *result,
1399 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1400 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1401 GSimpleAsyncResult *r;
1402 GError *error = NULL;
1403 GStrv reconnect_required = NULL;
1405 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1406 result, &reconnect_required, &error))
1408 g_simple_async_result_set_from_error (priv->apply_result, error);
1409 g_error_free (error);
1413 /* Only set the password in the keyring if the CM supports SASL and
1415 if (priv->supports_sasl && priv->password_changed)
1417 if (priv->password != NULL)
1419 /* FIXME: we shouldn't save the password if we
1420 * can't (MaySaveResponse=False) but we don't have API to check that
1421 * at this point (fdo #35382). */
1422 empathy_keyring_set_account_password_async (priv->account, priv->password,
1423 empathy_account_settings_set_password_cb, settings);
1427 empathy_keyring_delete_account_password_async (priv->account,
1428 empathy_account_settings_delete_password_cb, settings);
1434 update_account_uri_schemes (settings);
1435 update_account_service (settings);
1437 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1438 g_strv_length (reconnect_required) > 0);
1441 empathy_account_settings_discard_changes (settings);
1443 r = priv->apply_result;
1444 priv->apply_result = NULL;
1446 g_simple_async_result_complete (r);
1448 g_strfreev (reconnect_required);
1452 empathy_account_settings_created_cb (GObject *source,
1453 GAsyncResult *result,
1456 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1457 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1458 GError *error = NULL;
1459 GSimpleAsyncResult *r;
1461 priv->account = tp_account_request_create_account_finish (
1462 TP_ACCOUNT_REQUEST (source), result, &error);
1464 if (priv->account == NULL)
1466 g_simple_async_result_set_from_error (priv->apply_result, error);
1470 if (priv->supports_sasl && priv->password != NULL)
1472 /* Save the password before connecting */
1473 /* FIXME: we shouldn't save the password if we
1474 * can't (MaySaveResponse=False) but we don't have API to check that
1475 * at this point (fdo #35382). */
1476 empathy_keyring_set_account_password_async (priv->account,
1477 priv->password, empathy_account_settings_set_password_cb,
1482 update_account_uri_schemes (settings);
1484 empathy_account_settings_discard_changes (settings);
1487 r = priv->apply_result;
1488 priv->apply_result = NULL;
1490 g_simple_async_result_complete (r);
1495 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1497 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1498 TpAccountRequest *account_req;
1499 TpConnectionPresenceType type;
1502 EmpathyPresenceManager *presence_mgr;
1503 GHashTableIter iter;
1506 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1507 priv->protocol, "New Account");
1509 presence_mgr = empathy_presence_manager_dup_singleton ();
1510 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1512 g_object_unref (presence_mgr);
1514 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1516 tp_account_request_set_requested_presence (account_req, type,
1520 tp_account_request_set_icon_name (account_req, priv->icon_name);
1522 tp_account_request_set_display_name (account_req, priv->display_name);
1524 if (priv->service != NULL)
1525 tp_account_request_set_service (account_req, priv->service);
1527 g_hash_table_iter_init (&iter, priv->parameters);
1528 while (g_hash_table_iter_next (&iter, &k, &v))
1530 const gchar *key = k;
1531 GVariant *value = v;
1533 tp_account_request_set_parameter (account_req, key, value);
1536 tp_account_request_create_account_async (account_req,
1537 empathy_account_settings_created_cb, self);
1541 build_parameters_variant (EmpathyAccountSettings *self)
1543 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1544 GVariantBuilder *builder;
1545 GHashTableIter iter;
1548 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1550 g_hash_table_iter_init (&iter, priv->parameters);
1551 while (g_hash_table_iter_next (&iter, &k, &v))
1553 const gchar *key = k;
1554 GVariant *value = v;
1557 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1558 g_variant_new_variant (value));
1560 g_variant_builder_add_value (builder, entry);
1563 return g_variant_builder_end (builder);
1567 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1568 GAsyncReadyCallback callback,
1571 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1573 if (priv->apply_result != NULL)
1575 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1576 callback, user_data,
1577 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1581 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1582 callback, user_data, empathy_account_settings_apply_finish);
1584 /* We'll have to reconnect only if we change none DBus_Property on an
1585 * existing account. */
1586 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1588 if (priv->account == NULL)
1590 g_assert (priv->apply_result != NULL && priv->account == NULL);
1592 empathy_account_settings_do_create_account (settings);
1596 tp_account_update_parameters_vardict_async (priv->account,
1597 build_parameters_variant (settings),
1598 (const gchar **) priv->unset_parameters->data,
1599 empathy_account_settings_account_updated, settings);
1604 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1605 GAsyncResult *result,
1606 gboolean *reconnect_required,
1609 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1613 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1614 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1616 if (reconnect_required != NULL)
1617 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1618 G_SIMPLE_ASYNC_RESULT (result));
1624 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1627 EmpathyAccountSettingsPriv *priv;
1628 const gchar *account_path;
1629 const gchar *priv_account_path;
1631 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1632 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1634 priv = GET_PRIV (settings);
1636 if (priv->account == NULL)
1639 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1640 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1642 return (!tp_strdiff (account_path, priv_account_path));
1646 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1648 const gchar *pattern)
1650 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1652 GError *error = NULL;
1654 regex = g_regex_new (pattern, 0, 0, &error);
1657 g_warning ("Failed to create reg exp: %s", error->message);
1658 g_error_free (error);
1662 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1666 empathy_account_settings_parameter_is_valid (
1667 EmpathyAccountSettings *settings,
1670 EmpathyAccountSettingsPriv *priv;
1671 const GRegex *regex;
1673 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1675 priv = GET_PRIV (settings);
1677 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1679 /* first, look if it's set in our own parameters */
1680 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1683 /* if we did not unset the parameter, look if it's in the account */
1684 if (priv->account != NULL &&
1685 !empathy_account_settings_is_unset (settings, param))
1687 const GHashTable *account_params;
1689 account_params = tp_account_get_parameters (priv->account);
1690 if (tp_asv_lookup (account_params, param))
1698 /* test whether parameter value matches its regex */
1699 regex = g_hash_table_lookup (priv->param_regexps, param);
1705 value = empathy_account_settings_dup_string (settings, param);
1709 match = g_regex_match (regex, value, 0, NULL);
1719 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1721 EmpathyAccountSettingsPriv *priv;
1723 GHashTableIter iter;
1726 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1728 priv = GET_PRIV (settings);
1730 for (l = priv->required_params; l; l = l->next)
1732 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1736 g_hash_table_iter_init (&iter, priv->param_regexps);
1737 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1739 if (!empathy_account_settings_parameter_is_valid (settings, param))
1747 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1749 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1751 return priv->protocol_obj;
1755 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1757 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1759 return priv->supports_sasl;
1763 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1766 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1768 return tp_protocol_has_param (priv->protocol_obj, param);
1772 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1775 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1777 priv->uri_scheme_tel = associate;
1781 empathy_account_settings_has_uri_scheme_tel (
1782 EmpathyAccountSettings *self)
1784 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1786 return priv->uri_scheme_tel;