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
25 #include <telepathy-glib/account-manager.h>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/gtypes.h>
30 #include "empathy-account-settings.h"
31 #include "empathy-connection-managers.h"
32 #include "empathy-keyring.h"
33 #include "empathy-utils.h"
34 #include "empathy-presence-manager.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
37 #include <libempathy/empathy-debug.h>
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
41 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
49 PROP_DISPLAY_NAME_OVERRIDDEN,
54 PASSWORD_RETRIEVED = 1,
58 static gulong signals[LAST_SIGNAL] = { 0, };
60 /* private structure */
61 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
63 struct _EmpathyAccountSettingsPriv
65 gboolean dispose_has_run;
66 EmpathyConnectionManagers *managers;
67 TpAccountManager *account_manager;
69 TpConnectionManager *manager;
70 TpProtocol *protocol_obj;
78 gboolean display_name_overridden;
81 gboolean supports_sasl;
82 gboolean password_changed;
85 gchar *password_original;
87 gboolean password_retrieved;
88 gboolean password_requested;
90 /* Parameter name (gchar *) -> parameter value (GValue) */
91 GHashTable *parameters;
92 /* Keys are parameter names from the hash above (gchar *).
93 * Values are regular expresions that should match corresponding parameter
94 * values (GRegex *). Possible regexp patterns are defined in
95 * empathy-account-widget.c */
96 GHashTable *param_regexps;
97 GArray *unset_parameters;
98 GList *required_params;
100 gulong managers_ready_id;
101 gboolean preparing_protocol;
103 /* If TRUE, the account should have 'tel' in its
104 * Account.Interface.Addressing.URISchemes property. */
105 gboolean uri_scheme_tel;
106 /* If TRUE, Service property needs to be updated when applying changes */
107 gboolean update_service;
109 GSimpleAsyncResult *apply_result;
113 empathy_account_settings_init (EmpathyAccountSettings *obj)
115 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
116 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
120 /* allocate any data required by the object here */
121 priv->managers = empathy_connection_managers_dup_singleton ();
122 priv->account_manager = tp_account_manager_dup ();
124 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
125 g_free, (GDestroyNotify) tp_g_value_slice_free);
127 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
128 g_free, (GDestroyNotify) g_regex_unref);
130 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
132 priv->required_params = NULL;
135 static void empathy_account_settings_dispose (GObject *object);
136 static void empathy_account_settings_finalize (GObject *object);
137 static void empathy_account_settings_account_ready_cb (GObject *source_object,
138 GAsyncResult *result, gpointer user_data);
139 static void empathy_account_settings_managers_ready_cb (GObject *obj,
140 GParamSpec *pspec, gpointer user_data);
141 static void empathy_account_settings_check_readyness (
142 EmpathyAccountSettings *self);
145 empathy_account_settings_set_property (GObject *object,
150 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
151 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
156 priv->account = g_value_dup_object (value);
159 priv->cm_name = g_value_dup_string (value);
162 priv->protocol = g_value_dup_string (value);
165 priv->service = g_value_dup_string (value);
167 case PROP_DISPLAY_NAME:
168 priv->display_name = g_value_dup_string (value);
170 case PROP_DISPLAY_NAME_OVERRIDDEN:
171 priv->display_name_overridden = g_value_get_boolean (value);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 empathy_account_settings_get_property (GObject *object,
185 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
186 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
191 g_value_set_object (value, priv->account);
194 g_value_set_string (value, priv->cm_name);
197 g_value_set_string (value, priv->protocol);
200 g_value_set_string (value, priv->service);
202 case PROP_DISPLAY_NAME:
203 g_value_set_string (value, priv->display_name);
205 case PROP_DISPLAY_NAME_OVERRIDDEN:
206 g_value_set_boolean (value, priv->display_name_overridden);
209 g_value_set_boolean (value, priv->ready);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 empathy_account_settings_constructed (GObject *object)
220 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
221 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
223 if (priv->account != NULL)
225 g_free (priv->cm_name);
226 g_free (priv->protocol);
227 g_free (priv->service);
230 g_strdup (tp_account_get_connection_manager (priv->account));
232 g_strdup (tp_account_get_protocol (priv->account));
234 g_strdup (tp_account_get_service (priv->account));
235 priv->icon_name = g_strdup
236 (tp_account_get_icon_name (priv->account));
240 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
243 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
245 empathy_account_settings_check_readyness (self);
249 GQuark features[] = {
250 TP_ACCOUNT_FEATURE_CORE,
251 TP_ACCOUNT_FEATURE_STORAGE,
252 TP_ACCOUNT_FEATURE_ADDRESSING,
255 if (priv->account != NULL)
257 tp_proxy_prepare_async (priv->account, features,
258 empathy_account_settings_account_ready_cb, self);
261 tp_g_signal_connect_object (priv->managers, "notify::ready",
262 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
266 empathy_account_settings_parent_class)->constructed != NULL)
268 empathy_account_settings_parent_class)->constructed (object);
273 empathy_account_settings_class_init (
274 EmpathyAccountSettingsClass *empathy_account_settings_class)
276 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
278 g_type_class_add_private (empathy_account_settings_class, sizeof
279 (EmpathyAccountSettingsPriv));
281 object_class->dispose = empathy_account_settings_dispose;
282 object_class->finalize = empathy_account_settings_finalize;
283 object_class->set_property = empathy_account_settings_set_property;
284 object_class->get_property = empathy_account_settings_get_property;
285 object_class->constructed = empathy_account_settings_constructed;
287 g_object_class_install_property (object_class, PROP_ACCOUNT,
288 g_param_spec_object ("account",
290 "The TpAccount backing these settings",
292 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
294 g_object_class_install_property (object_class, PROP_CM_NAME,
295 g_param_spec_string ("connection-manager",
296 "connection-manager",
297 "The name of the connection manager this account uses",
299 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
301 g_object_class_install_property (object_class, PROP_PROTOCOL,
302 g_param_spec_string ("protocol",
304 "The name of the protocol this account uses",
306 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
308 g_object_class_install_property (object_class, PROP_SERVICE,
309 g_param_spec_string ("service",
311 "The service of this account, or NULL",
313 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
315 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
316 g_param_spec_string ("display-name",
318 "The display name account these settings belong to",
320 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
322 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
323 g_param_spec_boolean ("display-name-overridden",
324 "display-name-overridden",
325 "Whether the display name for this account has been manually "
328 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
330 g_object_class_install_property (object_class, PROP_READY,
331 g_param_spec_boolean ("ready",
333 "Whether this account is ready to be used",
335 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
337 signals[PASSWORD_RETRIEVED] =
338 g_signal_new ("password-retrieved",
339 G_TYPE_FROM_CLASS (empathy_account_settings_class),
340 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
341 g_cclosure_marshal_generic,
346 empathy_account_settings_dispose (GObject *object)
348 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
349 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
351 if (priv->dispose_has_run)
354 priv->dispose_has_run = TRUE;
356 if (priv->managers_ready_id != 0)
357 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
358 priv->managers_ready_id = 0;
360 tp_clear_object (&priv->managers);
361 tp_clear_object (&priv->manager);
362 tp_clear_object (&priv->account_manager);
363 tp_clear_object (&priv->account);
364 tp_clear_object (&priv->protocol_obj);
366 /* release any references held by the object here */
367 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
368 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
372 empathy_account_settings_free_unset_parameters (
373 EmpathyAccountSettings *settings)
375 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
378 for (i = 0 ; i < priv->unset_parameters->len; i++)
379 g_free (g_array_index (priv->unset_parameters, gchar *, i));
381 g_array_set_size (priv->unset_parameters, 0);
385 empathy_account_settings_finalize (GObject *object)
387 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
388 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
391 /* free any data held directly by the object here */
392 g_free (priv->cm_name);
393 g_free (priv->protocol);
394 g_free (priv->service);
395 g_free (priv->display_name);
396 g_free (priv->icon_name);
397 g_free (priv->password);
398 g_free (priv->password_original);
400 if (priv->required_params != NULL)
402 for (l = priv->required_params; l; l = l->next)
404 g_list_free (priv->required_params);
407 g_hash_table_unref (priv->parameters);
408 g_hash_table_unref (priv->param_regexps);
410 empathy_account_settings_free_unset_parameters (self);
411 g_array_unref (priv->unset_parameters);
413 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
417 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
418 GAsyncResult *result,
421 EmpathyAccountSettings *self = user_data;
422 GError *error = NULL;
424 if (!tp_proxy_prepare_finish (source, result, &error))
426 DEBUG ("Failed to prepare protocol object: %s", error->message);
427 g_clear_error (&error);
431 empathy_account_settings_check_readyness (self);
435 empathy_account_settings_get_password_cb (GObject *source,
436 GAsyncResult *result,
439 EmpathyAccountSettings *self = user_data;
440 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
441 const gchar *password;
442 GError *error = NULL;
444 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
449 DEBUG ("Failed to get password: %s", error->message);
450 g_clear_error (&error);
453 /* It doesn't really matter if getting the password failed; that
454 * just means that it's not there, or let's act like that at
457 g_assert (priv->password == NULL);
459 priv->password = g_strdup (password);
460 priv->password_original = g_strdup (password);
462 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
466 empathy_account_settings_migrate_password_cb (GObject *source,
467 GAsyncResult *result,
470 TpAccount *account = TP_ACCOUNT (source);
471 GError *error = NULL;
472 EmpathyAccountSettings *self = user_data;
473 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
475 const gchar *unset[] = { "password", NULL };
477 if (!empathy_keyring_set_account_password_finish (account, result, &error))
479 DEBUG ("Failed to set password: %s", error->message);
480 g_clear_error (&error);
484 /* Now clear the password MC has stored. */
485 empty = tp_asv_new (NULL, NULL);
486 tp_account_update_parameters_async (priv->account,
487 empty, unset, NULL, NULL);
489 g_hash_table_remove (priv->parameters, "password");
491 g_hash_table_unref (empty);
495 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
497 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
499 const gchar *password;
501 if (!priv->supports_sasl || empathy_account_settings_get (
502 self, "password") == NULL)
505 /* mission-control still has our password, although the CM
506 * supports SASL. Let's try migrating it. */
508 DEBUG ("Trying to migrate password parameter from MC to the "
509 "keyring ourselves for account %s",
510 tp_account_get_path_suffix (priv->account));
512 v = empathy_account_settings_get (self, "password");
514 /* I can't imagine why this would fail. */
515 if (!G_VALUE_HOLDS_STRING (v))
518 password = g_value_get_string (v);
520 if (EMP_STR_EMPTY (password))
523 empathy_keyring_set_account_password_async (priv->account, password,
524 empathy_account_settings_migrate_password_cb, self);
526 /* We don't want to request the password again, we
527 * already know it. */
528 priv->password_requested = TRUE;
530 priv->password = g_strdup (password);
531 priv->password_original = g_strdup (password);
535 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
537 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
538 const TpConnectionManagerProtocol *tp_protocol;
539 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
544 if (priv->account != NULL
545 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
548 if (!empathy_connection_managers_is_ready (priv->managers))
551 if (priv->manager == NULL)
553 priv->manager = empathy_connection_managers_get_cm (
554 priv->managers, priv->cm_name);
557 if (priv->manager == NULL)
560 g_object_ref (priv->manager);
562 if (priv->account != NULL)
564 g_free (priv->display_name);
566 g_strdup (tp_account_get_display_name (priv->account));
568 g_free (priv->icon_name);
570 g_strdup (tp_account_get_icon_name (priv->account));
572 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
575 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
578 if (tp_protocol == NULL)
580 tp_clear_object (&priv->manager);
584 if (priv->required_params == NULL)
586 TpConnectionManagerParam *cur;
588 for (cur = tp_protocol->params; cur->name != NULL; cur++)
590 if (tp_connection_manager_param_is_required (cur))
592 priv->required_params = g_list_append (priv->required_params,
593 g_strdup (cur->name));
598 if (priv->protocol_obj == NULL)
600 priv->protocol_obj = g_object_ref (
601 tp_connection_manager_get_protocol_object (priv->manager,
605 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
606 && !priv->preparing_protocol)
608 priv->preparing_protocol = TRUE;
609 tp_proxy_prepare_async (priv->protocol_obj, features,
610 empathy_account_settings_protocol_obj_prepared_cb, self);
615 if (tp_strv_contains (tp_protocol_get_authentication_types (
617 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
619 priv->supports_sasl = TRUE;
623 /* NOTE: When removing MC migration code, remove this call, and the
624 * function it's calling. That's it. */
625 empathy_account_settings_try_migrating_password (self);
627 /* priv->account won't be a proper account if it's the account
628 * assistant showing this widget. */
629 if (priv->supports_sasl && !priv->password_requested
630 && priv->account != NULL)
632 priv->password_requested = TRUE;
634 /* Make this call but don't block on its readiness. We'll signal
635 * if it's updated later with ::password-retrieved. */
636 empathy_keyring_get_account_password_async (priv->account,
637 empathy_account_settings_get_password_cb, self);
641 g_object_notify (G_OBJECT (self), "ready");
645 empathy_account_settings_account_ready_cb (GObject *source_object,
646 GAsyncResult *result,
649 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
650 TpAccount *account = TP_ACCOUNT (source_object);
651 GError *error = NULL;
653 if (!tp_proxy_prepare_finish (account, result, &error))
655 DEBUG ("Failed to prepare account: %s", error->message);
656 g_error_free (error);
660 empathy_account_settings_check_readyness (settings);
664 empathy_account_settings_managers_ready_cb (GObject *object,
668 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
670 empathy_account_settings_check_readyness (settings);
673 EmpathyAccountSettings *
674 empathy_account_settings_new (const gchar *connection_manager,
675 const gchar *protocol,
676 const gchar *service,
677 const char *display_name)
679 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
680 "connection-manager", connection_manager,
681 "protocol", protocol,
683 "display-name", display_name,
687 EmpathyAccountSettings *
688 empathy_account_settings_new_for_account (TpAccount *account)
690 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
695 TpConnectionManagerParam *
696 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
698 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
699 const TpConnectionManagerProtocol *tp_protocol;
701 g_return_val_if_fail (priv->manager != NULL, NULL);
702 g_return_val_if_fail (priv->protocol != NULL, NULL);
704 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
706 if (tp_protocol == NULL)
708 DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
713 return tp_protocol->params;
717 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
719 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
725 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
727 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
729 return priv->cm_name;
733 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
735 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
737 return priv->protocol;
741 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
743 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
745 return priv->service;
749 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
750 const gchar *service)
752 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
754 if (!tp_strdiff (priv->service, service))
757 g_free (priv->service);
758 priv->service = g_strdup (service);
759 g_object_notify (G_OBJECT (settings), "service");
760 priv->update_service = TRUE;
764 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
766 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
768 return priv->icon_name;
772 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
774 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
776 return priv->display_name;
780 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
782 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
784 return priv->account;
788 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
791 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
795 a = priv->unset_parameters;
797 for (i = 0; i < a->len; i++)
799 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
806 static TpConnectionManagerParam *
807 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
810 TpConnectionManagerParam *tp_params =
811 empathy_account_settings_get_tp_params (settings);
812 TpConnectionManagerParam *p;
814 for (p = tp_params; p != NULL && p->name != NULL; p++)
816 if (tp_strdiff (p->name, param))
826 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
829 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
833 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
836 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
840 for (idx = 0; idx < priv->unset_parameters->len; idx++)
842 val = g_array_index (priv->unset_parameters, gchar *, idx);
844 if (!tp_strdiff (val, param))
846 priv->unset_parameters =
847 g_array_remove_index (priv->unset_parameters, idx);
856 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
859 TpConnectionManagerParam *p;
861 p = empathy_account_settings_get_tp_param (settings, param);
863 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
866 return &(p->default_value);
870 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
873 TpConnectionManagerParam *p;
875 p = empathy_account_settings_get_tp_param (settings, param);
880 return p->dbus_signature;
884 empathy_account_settings_get (EmpathyAccountSettings *settings,
887 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
888 const GValue *result = NULL;
890 /* Lookup the update parameters we set */
891 result = tp_asv_lookup (priv->parameters, param);
895 /* If the parameters isn't unset use the accounts setting if any */
896 if (priv->account != NULL
897 && !empathy_account_settings_is_unset (settings, param))
899 const GHashTable *parameters;
901 parameters = tp_account_get_parameters (priv->account);
902 result = tp_asv_lookup (parameters, param);
908 /* fallback to the default */
909 return empathy_account_settings_get_default (settings, param);
913 empathy_account_settings_unset (EmpathyAccountSettings *settings,
916 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
918 if (empathy_account_settings_is_unset (settings, param))
921 if (priv->supports_sasl && !tp_strdiff (param, "password"))
923 g_free (priv->password);
924 priv->password = NULL;
925 priv->password_changed = TRUE;
929 v = g_strdup (param);
931 g_array_append_val (priv->unset_parameters, v);
932 g_hash_table_remove (priv->parameters, param);
936 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
938 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
940 g_hash_table_remove_all (priv->parameters);
941 empathy_account_settings_free_unset_parameters (settings);
943 priv->password_changed = FALSE;
944 g_free (priv->password);
945 priv->password = g_strdup (priv->password_original);
947 if (priv->account != NULL)
948 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
950 priv->uri_scheme_tel = FALSE;
954 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
957 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
960 if (!tp_strdiff (param, "password") && priv->supports_sasl)
962 return priv->password;
965 v = empathy_account_settings_get (settings, param);
967 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
970 return g_value_get_string (v);
973 const gchar * const *
974 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
979 v = empathy_account_settings_get (settings, param);
981 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
984 return g_value_get_boxed (v);
988 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
994 v = empathy_account_settings_get (settings, param);
999 switch G_VALUE_TYPE (v)
1002 ret = g_value_get_uchar (v);
1005 ret = g_value_get_int (v);
1008 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
1012 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
1015 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
1027 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
1033 v = empathy_account_settings_get (settings, param);
1037 switch G_VALUE_TYPE (v)
1040 ret = g_value_get_uchar (v);
1043 ret = g_value_get_int (v);
1046 ret = g_value_get_uint (v);
1049 ret = g_value_get_int64 (v);
1052 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1063 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1069 v = empathy_account_settings_get (settings, param);
1073 switch G_VALUE_TYPE (v)
1076 ret = g_value_get_uchar (v);
1079 ret = MAX (0, g_value_get_int (v));
1082 ret = g_value_get_uint (v);
1085 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1088 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1099 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1105 v = empathy_account_settings_get (settings, param);
1107 if (v == NULL || !G_VALUE_HOLDS_INT (v))
1110 switch G_VALUE_TYPE (v)
1113 ret = g_value_get_uchar (v);
1116 ret = MAX (0, g_value_get_int (v));
1119 ret = g_value_get_uint (v);
1122 ret = MAX (0, g_value_get_int64 (v));
1125 ret = g_value_get_uint64 (v);
1136 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1141 v = empathy_account_settings_get (settings, param);
1143 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1146 return g_value_get_boolean (v);
1150 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1154 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1156 g_return_if_fail (param != NULL);
1157 g_return_if_fail (value != NULL);
1159 if (!tp_strdiff (param, "password") && priv->supports_sasl)
1161 g_free (priv->password);
1162 priv->password = g_strdup (value);
1163 priv->password_changed = TRUE;
1167 tp_asv_set_string (priv->parameters, g_strdup (param), value);
1170 account_settings_remove_from_unset (settings, param);
1174 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1178 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1180 g_return_if_fail (param != NULL);
1181 g_return_if_fail (value != NULL);
1183 tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1185 account_settings_remove_from_unset (settings, param);
1189 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1193 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1195 g_return_if_fail (param != NULL);
1197 tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1199 account_settings_remove_from_unset (settings, param);
1203 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1207 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1209 g_return_if_fail (param != NULL);
1211 tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1213 account_settings_remove_from_unset (settings, param);
1217 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1221 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1223 g_return_if_fail (param != NULL);
1225 tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1227 account_settings_remove_from_unset (settings, param);
1231 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1235 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1237 g_return_if_fail (param != NULL);
1239 tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1241 account_settings_remove_from_unset (settings, param);
1245 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1249 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1251 g_return_if_fail (param != NULL);
1253 tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1255 account_settings_remove_from_unset (settings, param);
1259 account_settings_display_name_set_cb (GObject *src,
1263 GError *error = NULL;
1264 TpAccount *account = TP_ACCOUNT (src);
1265 GSimpleAsyncResult *set_result = user_data;
1267 tp_account_set_display_name_finish (account, res, &error);
1271 g_simple_async_result_set_from_error (set_result, error);
1272 g_error_free (error);
1275 g_simple_async_result_complete (set_result);
1276 g_object_unref (set_result);
1280 empathy_account_settings_set_display_name_async (
1281 EmpathyAccountSettings *settings,
1283 GAsyncReadyCallback callback,
1286 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1287 GSimpleAsyncResult *result;
1289 g_return_if_fail (name != NULL);
1291 result = g_simple_async_result_new (G_OBJECT (settings),
1292 callback, user_data, empathy_account_settings_set_display_name_finish);
1294 if (!tp_strdiff (name, priv->display_name))
1297 g_simple_async_result_complete_in_idle (result);
1301 if (priv->account == NULL)
1303 if (priv->display_name != NULL)
1304 g_free (priv->display_name);
1306 priv->display_name = g_strdup (name);
1308 g_simple_async_result_complete_in_idle (result);
1313 tp_account_set_display_name_async (priv->account, name,
1314 account_settings_display_name_set_cb, result);
1318 empathy_account_settings_set_display_name_finish (
1319 EmpathyAccountSettings *settings,
1320 GAsyncResult *result,
1323 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1327 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1328 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1335 account_settings_icon_name_set_cb (GObject *src,
1339 GError *error = NULL;
1340 TpAccount *account = TP_ACCOUNT (src);
1341 GSimpleAsyncResult *set_result = user_data;
1343 tp_account_set_icon_name_finish (account, res, &error);
1347 g_simple_async_result_set_from_error (set_result, error);
1348 g_error_free (error);
1351 g_simple_async_result_complete (set_result);
1352 g_object_unref (set_result);
1356 empathy_account_settings_set_icon_name_async (
1357 EmpathyAccountSettings *settings,
1359 GAsyncReadyCallback callback,
1362 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1363 GSimpleAsyncResult *result;
1365 g_return_if_fail (name != NULL);
1367 result = g_simple_async_result_new (G_OBJECT (settings),
1368 callback, user_data, empathy_account_settings_set_icon_name_finish);
1370 if (priv->account == NULL)
1372 if (priv->icon_name != NULL)
1373 g_free (priv->icon_name);
1375 priv->icon_name = g_strdup (name);
1377 g_simple_async_result_complete_in_idle (result);
1382 tp_account_set_icon_name_async (priv->account, name,
1383 account_settings_icon_name_set_cb, result);
1387 empathy_account_settings_set_icon_name_finish (
1388 EmpathyAccountSettings *settings,
1389 GAsyncResult *result,
1392 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1396 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1397 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1404 empathy_account_settings_processed_password (GObject *source,
1405 GAsyncResult *result,
1407 gpointer finish_func)
1409 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1410 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1411 GSimpleAsyncResult *r;
1412 GError *error = NULL;
1413 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1416 g_free (priv->password_original);
1417 priv->password_original = g_strdup (priv->password);
1419 if (!func (TP_ACCOUNT (source), result, &error))
1421 g_simple_async_result_set_from_error (priv->apply_result, error);
1422 g_error_free (error);
1425 empathy_account_settings_discard_changes (settings);
1427 r = priv->apply_result;
1428 priv->apply_result = NULL;
1430 g_simple_async_result_complete (r);
1435 empathy_account_settings_set_password_cb (GObject *source,
1436 GAsyncResult *result,
1439 empathy_account_settings_processed_password (source, result, user_data,
1440 empathy_keyring_set_account_password_finish);
1444 empathy_account_settings_delete_password_cb (GObject *source,
1445 GAsyncResult *result,
1448 empathy_account_settings_processed_password (source, result, user_data,
1449 empathy_keyring_delete_account_password_finish);
1453 update_account_uri_schemes (EmpathyAccountSettings *self)
1455 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1457 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1461 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1462 priv->uri_scheme_tel, NULL, NULL);
1466 set_service_cb (GObject *source,
1467 GAsyncResult *result,
1470 GError *error = NULL;
1472 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1474 DEBUG ("Failed to set Account.Service: %s", error->message);
1475 g_error_free (error);
1480 update_account_service (EmpathyAccountSettings *self)
1482 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1484 if (!priv->update_service)
1487 tp_account_set_service_async (priv->account,
1488 priv->service != NULL ? priv->service : "", set_service_cb, self);
1492 empathy_account_settings_account_updated (GObject *source,
1493 GAsyncResult *result,
1496 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1497 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1498 GSimpleAsyncResult *r;
1499 GError *error = NULL;
1500 GStrv reconnect_required = NULL;
1502 if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1503 result, &reconnect_required, &error))
1505 g_simple_async_result_set_from_error (priv->apply_result, error);
1506 g_error_free (error);
1510 /* Only set the password in the keyring if the CM supports SASL and
1512 if (priv->supports_sasl && priv->password_changed)
1514 if (priv->password != NULL)
1516 /* FIXME: we shouldn't save the password if we
1517 * can't (MaySaveResponse=False) but we don't have API to check that
1518 * at this point (fdo #35382). */
1519 empathy_keyring_set_account_password_async (priv->account, priv->password,
1520 empathy_account_settings_set_password_cb, settings);
1524 empathy_keyring_delete_account_password_async (priv->account,
1525 empathy_account_settings_delete_password_cb, settings);
1531 update_account_uri_schemes (settings);
1532 update_account_service (settings);
1534 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1535 g_strv_length (reconnect_required) > 0);
1538 empathy_account_settings_discard_changes (settings);
1540 r = priv->apply_result;
1541 priv->apply_result = NULL;
1543 g_simple_async_result_complete (r);
1545 g_strfreev (reconnect_required);
1549 empathy_account_settings_created_cb (GObject *source,
1550 GAsyncResult *result,
1553 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1554 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1556 GError *error = NULL;
1557 GSimpleAsyncResult *r;
1559 account = tp_account_manager_create_account_finish (
1560 TP_ACCOUNT_MANAGER (source), result, &error);
1562 if (account == NULL)
1564 g_simple_async_result_set_from_error (priv->apply_result, error);
1568 priv->account = g_object_ref (account);
1570 if (priv->supports_sasl && priv->password != NULL)
1572 /* Save the password before connecting */
1573 /* FIXME: we shouldn't save the password if we
1574 * can't (MaySaveResponse=False) but we don't have API to check that
1575 * at this point (fdo #35382). */
1576 empathy_keyring_set_account_password_async (priv->account,
1577 priv->password, empathy_account_settings_set_password_cb,
1582 update_account_uri_schemes (settings);
1584 empathy_account_settings_discard_changes (settings);
1587 r = priv->apply_result;
1588 priv->apply_result = NULL;
1590 g_simple_async_result_complete (r);
1596 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1598 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1599 GHashTable *properties;
1600 TpConnectionPresenceType type;
1603 EmpathyPresenceManager *presence_mgr;
1605 properties = tp_asv_new (NULL, NULL);
1607 presence_mgr = empathy_presence_manager_dup_singleton ();
1608 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1610 g_object_unref (presence_mgr);
1612 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1614 /* Create the account with the requested presence the same as the current
1615 * global requested presence, but don't enable it */
1616 GValueArray *presence;
1617 GValue vtype = { 0, };
1618 GValue vstatus = { 0, };
1619 GValue vmessage = { 0, };
1621 presence = g_value_array_new (3);
1623 g_value_init (&vtype, G_TYPE_UINT);
1624 g_value_set_uint (&vtype, type);
1625 g_value_array_append (presence, &vtype);
1627 g_value_init (&vstatus, G_TYPE_STRING);
1628 g_value_take_string (&vstatus, status);
1629 g_value_array_append (presence, &vstatus);
1631 g_value_init (&vmessage, G_TYPE_STRING);
1632 g_value_take_string (&vmessage, message);
1633 g_value_array_append (presence, &vmessage);
1635 tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1636 TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1639 tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1642 if (priv->service != NULL)
1643 tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1645 tp_account_manager_create_account_async (priv->account_manager,
1646 priv->cm_name, priv->protocol, priv->display_name,
1647 priv->parameters, properties,
1648 empathy_account_settings_created_cb,
1651 g_hash_table_unref (properties);
1655 empathy_account_settings_manager_ready_cb (GObject *source_object,
1656 GAsyncResult *result,
1659 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1660 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1661 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1662 GError *error = NULL;
1664 if (!tp_proxy_prepare_finish (account_manager, result, &error))
1666 DEBUG ("Failed to prepare account manager: %s", error->message);
1667 g_error_free (error);
1671 g_assert (priv->apply_result != NULL && priv->account == NULL);
1672 empathy_account_settings_do_create_account (settings);
1676 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1677 GAsyncReadyCallback callback,
1680 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1682 if (priv->apply_result != NULL)
1684 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1685 callback, user_data,
1686 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1690 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1691 callback, user_data, empathy_account_settings_apply_finish);
1693 /* We'll have to reconnect only if we change none DBus_Property on an
1694 * existing account. */
1695 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1697 if (priv->account == NULL)
1699 tp_proxy_prepare_async (priv->account_manager, NULL,
1700 empathy_account_settings_manager_ready_cb, settings);
1704 tp_account_update_parameters_async (priv->account,
1705 priv->parameters, (const gchar **)priv->unset_parameters->data,
1706 empathy_account_settings_account_updated, settings);
1711 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1712 GAsyncResult *result,
1713 gboolean *reconnect_required,
1716 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1720 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1721 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1723 if (reconnect_required != NULL)
1724 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1725 G_SIMPLE_ASYNC_RESULT (result));
1731 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1734 EmpathyAccountSettingsPriv *priv;
1735 const gchar *account_path;
1736 const gchar *priv_account_path;
1738 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1739 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1741 priv = GET_PRIV (settings);
1743 if (priv->account == NULL)
1746 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1747 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1749 return (!tp_strdiff (account_path, priv_account_path));
1753 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1755 const gchar *pattern)
1757 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1759 GError *error = NULL;
1761 regex = g_regex_new (pattern, 0, 0, &error);
1764 g_warning ("Failed to create reg exp: %s", error->message);
1765 g_error_free (error);
1769 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1773 empathy_account_settings_parameter_is_valid (
1774 EmpathyAccountSettings *settings,
1777 EmpathyAccountSettingsPriv *priv;
1778 const GRegex *regex;
1781 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1783 priv = GET_PRIV (settings);
1785 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1787 /* first, look if it's set in our own parameters */
1788 if (tp_asv_lookup (priv->parameters, param))
1791 /* if we did not unset the parameter, look if it's in the account */
1792 if (priv->account != NULL &&
1793 !empathy_account_settings_is_unset (settings, param))
1795 const GHashTable *account_params;
1797 account_params = tp_account_get_parameters (priv->account);
1798 if (tp_asv_lookup (account_params, param))
1806 /* test whether parameter value matches its regex */
1807 regex = g_hash_table_lookup (priv->param_regexps, param);
1810 value = empathy_account_settings_get_string (settings, param);
1811 if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1819 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1821 EmpathyAccountSettingsPriv *priv;
1823 GHashTableIter iter;
1826 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1828 priv = GET_PRIV (settings);
1830 for (l = priv->required_params; l; l = l->next)
1832 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1836 g_hash_table_iter_init (&iter, priv->param_regexps);
1837 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1839 if (!empathy_account_settings_parameter_is_valid (settings, param))
1846 const TpConnectionManagerProtocol *
1847 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1849 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1851 return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1855 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1857 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1859 return priv->supports_sasl;
1863 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1866 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1868 return tp_protocol_has_param (priv->protocol_obj, param);
1872 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1875 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1877 priv->uri_scheme_tel = associate;
1881 empathy_account_settings_has_uri_scheme_tel (
1882 EmpathyAccountSettings *self)
1884 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1886 return priv->uri_scheme_tel;