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;
107 GSimpleAsyncResult *apply_result;
111 empathy_account_settings_init (EmpathyAccountSettings *obj)
113 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
114 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
118 /* allocate any data required by the object here */
119 priv->managers = empathy_connection_managers_dup_singleton ();
120 priv->account_manager = tp_account_manager_dup ();
122 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
123 g_free, (GDestroyNotify) tp_g_value_slice_free);
125 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
126 g_free, (GDestroyNotify) g_regex_unref);
128 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
130 priv->required_params = NULL;
133 static void empathy_account_settings_dispose (GObject *object);
134 static void empathy_account_settings_finalize (GObject *object);
135 static void empathy_account_settings_account_ready_cb (GObject *source_object,
136 GAsyncResult *result, gpointer user_data);
137 static void empathy_account_settings_managers_ready_cb (GObject *obj,
138 GParamSpec *pspec, gpointer user_data);
139 static void empathy_account_settings_check_readyness (
140 EmpathyAccountSettings *self);
143 empathy_account_settings_set_property (GObject *object,
148 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
149 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
154 priv->account = g_value_dup_object (value);
157 priv->cm_name = g_value_dup_string (value);
160 priv->protocol = g_value_dup_string (value);
163 priv->service = g_value_dup_string (value);
165 case PROP_DISPLAY_NAME:
166 priv->display_name = g_value_dup_string (value);
168 case PROP_DISPLAY_NAME_OVERRIDDEN:
169 priv->display_name_overridden = g_value_get_boolean (value);
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 empathy_account_settings_get_property (GObject *object,
183 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
184 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
189 g_value_set_object (value, priv->account);
192 g_value_set_string (value, priv->cm_name);
195 g_value_set_string (value, priv->protocol);
198 g_value_set_string (value, priv->service);
200 case PROP_DISPLAY_NAME:
201 g_value_set_string (value, priv->display_name);
203 case PROP_DISPLAY_NAME_OVERRIDDEN:
204 g_value_set_boolean (value, priv->display_name_overridden);
207 g_value_set_boolean (value, priv->ready);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 empathy_account_settings_constructed (GObject *object)
218 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
219 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
221 if (priv->account != NULL)
223 g_free (priv->cm_name);
224 g_free (priv->protocol);
225 g_free (priv->service);
228 g_strdup (tp_account_get_connection_manager (priv->account));
230 g_strdup (tp_account_get_protocol (priv->account));
232 g_strdup (tp_account_get_service (priv->account));
233 priv->icon_name = g_strdup
234 (tp_account_get_icon_name (priv->account));
238 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
241 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
243 empathy_account_settings_check_readyness (self);
247 GQuark features[] = {
248 TP_ACCOUNT_FEATURE_CORE,
249 TP_ACCOUNT_FEATURE_STORAGE,
250 TP_ACCOUNT_FEATURE_ADDRESSING,
253 if (priv->account != NULL)
255 tp_proxy_prepare_async (priv->account, features,
256 empathy_account_settings_account_ready_cb, self);
259 tp_g_signal_connect_object (priv->managers, "notify::ready",
260 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
264 empathy_account_settings_parent_class)->constructed != NULL)
266 empathy_account_settings_parent_class)->constructed (object);
271 empathy_account_settings_class_init (
272 EmpathyAccountSettingsClass *empathy_account_settings_class)
274 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
276 g_type_class_add_private (empathy_account_settings_class, sizeof
277 (EmpathyAccountSettingsPriv));
279 object_class->dispose = empathy_account_settings_dispose;
280 object_class->finalize = empathy_account_settings_finalize;
281 object_class->set_property = empathy_account_settings_set_property;
282 object_class->get_property = empathy_account_settings_get_property;
283 object_class->constructed = empathy_account_settings_constructed;
285 g_object_class_install_property (object_class, PROP_ACCOUNT,
286 g_param_spec_object ("account",
288 "The TpAccount backing these settings",
290 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
292 g_object_class_install_property (object_class, PROP_CM_NAME,
293 g_param_spec_string ("connection-manager",
294 "connection-manager",
295 "The name of the connection manager this account uses",
297 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
299 g_object_class_install_property (object_class, PROP_PROTOCOL,
300 g_param_spec_string ("protocol",
302 "The name of the protocol this account uses",
304 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
306 g_object_class_install_property (object_class, PROP_SERVICE,
307 g_param_spec_string ("service",
309 "The service of this account, or NULL",
311 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
313 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
314 g_param_spec_string ("display-name",
316 "The display name account these settings belong to",
318 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
320 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
321 g_param_spec_boolean ("display-name-overridden",
322 "display-name-overridden",
323 "Whether the display name for this account has been manually "
326 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
328 g_object_class_install_property (object_class, PROP_READY,
329 g_param_spec_boolean ("ready",
331 "Whether this account is ready to be used",
333 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
335 signals[PASSWORD_RETRIEVED] =
336 g_signal_new ("password-retrieved",
337 G_TYPE_FROM_CLASS (empathy_account_settings_class),
338 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
339 g_cclosure_marshal_VOID__VOID,
344 empathy_account_settings_dispose (GObject *object)
346 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
347 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
349 if (priv->dispose_has_run)
352 priv->dispose_has_run = TRUE;
354 if (priv->managers_ready_id != 0)
355 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
356 priv->managers_ready_id = 0;
358 tp_clear_object (&priv->managers);
359 tp_clear_object (&priv->manager);
360 tp_clear_object (&priv->account_manager);
361 tp_clear_object (&priv->account);
362 tp_clear_object (&priv->protocol_obj);
364 /* release any references held by the object here */
365 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
366 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
370 empathy_account_settings_free_unset_parameters (
371 EmpathyAccountSettings *settings)
373 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
376 for (i = 0 ; i < priv->unset_parameters->len; i++)
377 g_free (g_array_index (priv->unset_parameters, gchar *, i));
379 g_array_set_size (priv->unset_parameters, 0);
383 empathy_account_settings_finalize (GObject *object)
385 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
386 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
389 /* free any data held directly by the object here */
390 g_free (priv->cm_name);
391 g_free (priv->protocol);
392 g_free (priv->service);
393 g_free (priv->display_name);
394 g_free (priv->icon_name);
395 g_free (priv->password);
396 g_free (priv->password_original);
398 if (priv->required_params != NULL)
400 for (l = priv->required_params; l; l = l->next)
402 g_list_free (priv->required_params);
405 g_hash_table_destroy (priv->parameters);
406 g_hash_table_destroy (priv->param_regexps);
408 empathy_account_settings_free_unset_parameters (self);
409 g_array_free (priv->unset_parameters, TRUE);
411 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
415 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
416 GAsyncResult *result,
419 EmpathyAccountSettings *self = user_data;
420 GError *error = NULL;
422 if (!tp_proxy_prepare_finish (source, result, &error))
424 DEBUG ("Failed to prepare protocol object: %s", error->message);
425 g_clear_error (&error);
429 empathy_account_settings_check_readyness (self);
433 empathy_account_settings_get_password_cb (GObject *source,
434 GAsyncResult *result,
437 EmpathyAccountSettings *self = user_data;
438 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
439 const gchar *password;
440 GError *error = NULL;
442 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
447 DEBUG ("Failed to get password: %s", error->message);
448 g_clear_error (&error);
451 /* It doesn't really matter if getting the password failed; that
452 * just means that it's not there, or let's act like that at
455 g_assert (priv->password == NULL);
457 priv->password = g_strdup (password);
458 priv->password_original = g_strdup (password);
460 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
464 empathy_account_settings_migrate_password_cb (GObject *source,
465 GAsyncResult *result,
468 TpAccount *account = TP_ACCOUNT (source);
469 GError *error = NULL;
470 EmpathyAccountSettings *self = user_data;
471 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
473 const gchar *unset[] = { "password", NULL };
475 if (!empathy_keyring_set_account_password_finish (account, result, &error))
477 DEBUG ("Failed to set password: %s", error->message);
478 g_clear_error (&error);
482 /* Now clear the password MC has stored. */
483 empty = tp_asv_new (NULL, NULL);
484 tp_account_update_parameters_async (priv->account,
485 empty, unset, NULL, NULL);
487 g_hash_table_remove (priv->parameters, "password");
489 g_hash_table_unref (empty);
493 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
495 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
497 const gchar *password;
499 if (!priv->supports_sasl || empathy_account_settings_get (
500 self, "password") == NULL)
503 /* mission-control still has our password, although the CM
504 * supports SASL. Let's try migrating it. */
506 DEBUG ("Trying to migrate password parameter from MC to the "
507 "keyring ourselves for account %s",
508 tp_account_get_path_suffix (priv->account));
510 v = empathy_account_settings_get (self, "password");
512 /* I can't imagine why this would fail. */
513 if (!G_VALUE_HOLDS_STRING (v))
516 password = g_value_get_string (v);
518 if (EMP_STR_EMPTY (password))
521 empathy_keyring_set_account_password_async (priv->account, password,
522 empathy_account_settings_migrate_password_cb, self);
524 /* We don't want to request the password again, we
525 * already know it. */
526 priv->password_requested = TRUE;
528 priv->password = g_strdup (password);
529 priv->password_original = g_strdup (password);
533 account_has_uri_scheme_tel (TpAccount *account)
535 const gchar * const * uri_schemes;
538 uri_schemes = tp_account_get_uri_schemes (account);
539 if (uri_schemes == NULL)
542 for (i = 0; uri_schemes[i] != NULL; i++)
544 if (!tp_strdiff (uri_schemes[i], "tel"))
552 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
554 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
555 const TpConnectionManagerProtocol *tp_protocol;
556 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
561 if (priv->account != NULL
562 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
565 if (!empathy_connection_managers_is_ready (priv->managers))
568 if (priv->manager == NULL)
570 priv->manager = empathy_connection_managers_get_cm (
571 priv->managers, priv->cm_name);
574 if (priv->manager == NULL)
577 g_object_ref (priv->manager);
579 if (priv->account != NULL)
581 g_free (priv->display_name);
583 g_strdup (tp_account_get_display_name (priv->account));
585 g_free (priv->icon_name);
587 g_strdup (tp_account_get_icon_name (priv->account));
589 priv->uri_scheme_tel = account_has_uri_scheme_tel (priv->account);
592 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
595 if (tp_protocol == NULL)
597 tp_clear_object (&priv->manager);
601 if (priv->required_params == NULL)
603 TpConnectionManagerParam *cur;
605 for (cur = tp_protocol->params; cur->name != NULL; cur++)
607 if (tp_connection_manager_param_is_required (cur))
609 priv->required_params = g_list_append (priv->required_params,
610 g_strdup (cur->name));
615 if (priv->protocol_obj == NULL)
617 priv->protocol_obj = g_object_ref (
618 tp_connection_manager_get_protocol_object (priv->manager,
622 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
623 && !priv->preparing_protocol)
625 priv->preparing_protocol = TRUE;
626 tp_proxy_prepare_async (priv->protocol_obj, features,
627 empathy_account_settings_protocol_obj_prepared_cb, self);
632 if (tp_strv_contains (tp_protocol_get_authentication_types (
634 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
636 priv->supports_sasl = TRUE;
640 /* NOTE: When removing MC migration code, remove this call, and the
641 * function it's calling. That's it. */
642 empathy_account_settings_try_migrating_password (self);
644 /* priv->account won't be a proper account if it's the account
645 * assistant showing this widget. */
646 if (priv->supports_sasl && !priv->password_requested
647 && priv->account != NULL)
649 priv->password_requested = TRUE;
651 /* Make this call but don't block on its readiness. We'll signal
652 * if it's updated later with ::password-retrieved. */
653 empathy_keyring_get_account_password_async (priv->account,
654 empathy_account_settings_get_password_cb, self);
658 g_object_notify (G_OBJECT (self), "ready");
662 empathy_account_settings_account_ready_cb (GObject *source_object,
663 GAsyncResult *result,
666 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
667 TpAccount *account = TP_ACCOUNT (source_object);
668 GError *error = NULL;
670 if (!tp_proxy_prepare_finish (account, result, &error))
672 DEBUG ("Failed to prepare account: %s", error->message);
673 g_error_free (error);
677 empathy_account_settings_check_readyness (settings);
681 empathy_account_settings_managers_ready_cb (GObject *object,
685 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
687 empathy_account_settings_check_readyness (settings);
690 EmpathyAccountSettings *
691 empathy_account_settings_new (const gchar *connection_manager,
692 const gchar *protocol,
693 const gchar *service,
694 const char *display_name)
696 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
697 "connection-manager", connection_manager,
698 "protocol", protocol,
700 "display-name", display_name,
704 EmpathyAccountSettings *
705 empathy_account_settings_new_for_account (TpAccount *account)
707 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
712 TpConnectionManagerParam *
713 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
715 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
716 const TpConnectionManagerProtocol *tp_protocol;
718 g_return_val_if_fail (priv->manager != NULL, NULL);
719 g_return_val_if_fail (priv->protocol != NULL, NULL);
721 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
723 if (tp_protocol == NULL)
725 DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
730 return tp_protocol->params;
734 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
736 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
742 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
744 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
746 return priv->cm_name;
750 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
752 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
754 return priv->protocol;
758 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
760 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
762 return priv->service;
766 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
768 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
770 return priv->icon_name;
774 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
776 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
778 return priv->display_name;
782 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
784 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
786 return priv->account;
790 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
793 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
797 a = priv->unset_parameters;
799 for (i = 0; i < a->len; i++)
801 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
808 static TpConnectionManagerParam *
809 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
812 TpConnectionManagerParam *tp_params =
813 empathy_account_settings_get_tp_params (settings);
814 TpConnectionManagerParam *p;
816 for (p = tp_params; p != NULL && p->name != NULL; p++)
818 if (tp_strdiff (p->name, param))
828 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
831 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
835 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
838 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
842 for (idx = 0; idx < priv->unset_parameters->len; idx++)
844 val = g_array_index (priv->unset_parameters, gchar *, idx);
846 if (!tp_strdiff (val, param))
848 priv->unset_parameters =
849 g_array_remove_index (priv->unset_parameters, idx);
858 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
861 TpConnectionManagerParam *p;
863 p = empathy_account_settings_get_tp_param (settings, param);
865 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
868 return &(p->default_value);
872 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
875 TpConnectionManagerParam *p;
877 p = empathy_account_settings_get_tp_param (settings, param);
882 return p->dbus_signature;
886 empathy_account_settings_get (EmpathyAccountSettings *settings,
889 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
890 const GValue *result = NULL;
892 /* Lookup the update parameters we set */
893 result = tp_asv_lookup (priv->parameters, param);
897 /* If the parameters isn't unset use the accounts setting if any */
898 if (priv->account != NULL
899 && !empathy_account_settings_is_unset (settings, param))
901 const GHashTable *parameters;
903 parameters = tp_account_get_parameters (priv->account);
904 result = tp_asv_lookup (parameters, param);
910 /* fallback to the default */
911 return empathy_account_settings_get_default (settings, param);
915 empathy_account_settings_unset (EmpathyAccountSettings *settings,
918 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
920 if (empathy_account_settings_is_unset (settings, param))
923 if (priv->supports_sasl && !tp_strdiff (param, "password"))
925 g_free (priv->password);
926 priv->password = NULL;
927 priv->password_changed = TRUE;
931 v = g_strdup (param);
933 g_array_append_val (priv->unset_parameters, v);
934 g_hash_table_remove (priv->parameters, param);
938 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
940 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
942 g_hash_table_remove_all (priv->parameters);
943 empathy_account_settings_free_unset_parameters (settings);
945 priv->password_changed = FALSE;
946 g_free (priv->password);
947 priv->password = g_strdup (priv->password_original);
949 if (priv->account != NULL)
950 priv->uri_scheme_tel = account_has_uri_scheme_tel (priv->account);
952 priv->uri_scheme_tel = FALSE;
956 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
959 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
962 if (!tp_strdiff (param, "password") && priv->supports_sasl)
964 return priv->password;
967 v = empathy_account_settings_get (settings, param);
969 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
972 return g_value_get_string (v);
975 const gchar * const *
976 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
981 v = empathy_account_settings_get (settings, param);
983 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
986 return g_value_get_boxed (v);
990 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
996 v = empathy_account_settings_get (settings, param);
1001 switch G_VALUE_TYPE (v)
1004 ret = g_value_get_uchar (v);
1007 ret = g_value_get_int (v);
1010 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
1014 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
1017 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
1029 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
1035 v = empathy_account_settings_get (settings, param);
1039 switch G_VALUE_TYPE (v)
1042 ret = g_value_get_uchar (v);
1045 ret = g_value_get_int (v);
1048 ret = g_value_get_uint (v);
1051 ret = g_value_get_int64 (v);
1054 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1065 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1071 v = empathy_account_settings_get (settings, param);
1075 switch G_VALUE_TYPE (v)
1078 ret = g_value_get_uchar (v);
1081 ret = MAX (0, g_value_get_int (v));
1084 ret = g_value_get_uint (v);
1087 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1090 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1101 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1107 v = empathy_account_settings_get (settings, param);
1109 if (v == NULL || !G_VALUE_HOLDS_INT (v))
1112 switch G_VALUE_TYPE (v)
1115 ret = g_value_get_uchar (v);
1118 ret = MAX (0, g_value_get_int (v));
1121 ret = g_value_get_uint (v);
1124 ret = MAX (0, g_value_get_int64 (v));
1127 ret = g_value_get_uint64 (v);
1138 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1143 v = empathy_account_settings_get (settings, param);
1145 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1148 return g_value_get_boolean (v);
1152 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1156 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1158 g_return_if_fail (param != NULL);
1159 g_return_if_fail (value != NULL);
1161 if (!tp_strdiff (param, "password") && priv->supports_sasl)
1163 g_free (priv->password);
1164 priv->password = g_strdup (value);
1165 priv->password_changed = TRUE;
1169 tp_asv_set_string (priv->parameters, g_strdup (param), value);
1172 account_settings_remove_from_unset (settings, param);
1176 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1180 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1182 g_return_if_fail (param != NULL);
1183 g_return_if_fail (value != NULL);
1185 tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1187 account_settings_remove_from_unset (settings, param);
1191 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1195 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1197 g_return_if_fail (param != NULL);
1199 tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1201 account_settings_remove_from_unset (settings, param);
1205 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1209 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1211 g_return_if_fail (param != NULL);
1213 tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1215 account_settings_remove_from_unset (settings, param);
1219 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1223 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1225 g_return_if_fail (param != NULL);
1227 tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1229 account_settings_remove_from_unset (settings, param);
1233 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1237 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1239 g_return_if_fail (param != NULL);
1241 tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1243 account_settings_remove_from_unset (settings, param);
1247 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1251 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1253 g_return_if_fail (param != NULL);
1255 tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1257 account_settings_remove_from_unset (settings, param);
1261 account_settings_display_name_set_cb (GObject *src,
1265 GError *error = NULL;
1266 TpAccount *account = TP_ACCOUNT (src);
1267 GSimpleAsyncResult *set_result = user_data;
1269 tp_account_set_display_name_finish (account, res, &error);
1273 g_simple_async_result_set_from_error (set_result, error);
1274 g_error_free (error);
1277 g_simple_async_result_complete (set_result);
1278 g_object_unref (set_result);
1282 empathy_account_settings_set_display_name_async (
1283 EmpathyAccountSettings *settings,
1285 GAsyncReadyCallback callback,
1288 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1289 GSimpleAsyncResult *result;
1291 g_return_if_fail (name != NULL);
1293 result = g_simple_async_result_new (G_OBJECT (settings),
1294 callback, user_data, empathy_account_settings_set_display_name_finish);
1296 if (!tp_strdiff (name, priv->display_name))
1299 g_simple_async_result_complete_in_idle (result);
1303 if (priv->account == NULL)
1305 if (priv->display_name != NULL)
1306 g_free (priv->display_name);
1308 priv->display_name = g_strdup (name);
1310 g_simple_async_result_complete_in_idle (result);
1315 tp_account_set_display_name_async (priv->account, name,
1316 account_settings_display_name_set_cb, result);
1320 empathy_account_settings_set_display_name_finish (
1321 EmpathyAccountSettings *settings,
1322 GAsyncResult *result,
1325 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1329 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1330 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1337 account_settings_icon_name_set_cb (GObject *src,
1341 GError *error = NULL;
1342 TpAccount *account = TP_ACCOUNT (src);
1343 GSimpleAsyncResult *set_result = user_data;
1345 tp_account_set_icon_name_finish (account, res, &error);
1349 g_simple_async_result_set_from_error (set_result, error);
1350 g_error_free (error);
1353 g_simple_async_result_complete (set_result);
1354 g_object_unref (set_result);
1358 empathy_account_settings_set_icon_name_async (
1359 EmpathyAccountSettings *settings,
1361 GAsyncReadyCallback callback,
1364 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1365 GSimpleAsyncResult *result;
1367 g_return_if_fail (name != NULL);
1369 result = g_simple_async_result_new (G_OBJECT (settings),
1370 callback, user_data, empathy_account_settings_set_icon_name_finish);
1372 if (priv->account == NULL)
1374 if (priv->icon_name != NULL)
1375 g_free (priv->icon_name);
1377 priv->icon_name = g_strdup (name);
1379 g_simple_async_result_complete_in_idle (result);
1384 tp_account_set_icon_name_async (priv->account, name,
1385 account_settings_icon_name_set_cb, result);
1389 empathy_account_settings_set_icon_name_finish (
1390 EmpathyAccountSettings *settings,
1391 GAsyncResult *result,
1394 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1398 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1399 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1406 empathy_account_settings_processed_password (GObject *source,
1407 GAsyncResult *result,
1409 gpointer finish_func)
1411 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1412 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1413 GSimpleAsyncResult *r;
1414 GError *error = NULL;
1415 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1418 g_free (priv->password_original);
1419 priv->password_original = g_strdup (priv->password);
1421 if (!func (TP_ACCOUNT (source), result, &error))
1423 g_simple_async_result_set_from_error (priv->apply_result, error);
1424 g_error_free (error);
1427 empathy_account_settings_discard_changes (settings);
1429 r = priv->apply_result;
1430 priv->apply_result = NULL;
1432 g_simple_async_result_complete (r);
1437 empathy_account_settings_set_password_cb (GObject *source,
1438 GAsyncResult *result,
1441 empathy_account_settings_processed_password (source, result, user_data,
1442 empathy_keyring_set_account_password_finish);
1446 empathy_account_settings_delete_password_cb (GObject *source,
1447 GAsyncResult *result,
1450 empathy_account_settings_processed_password (source, result, user_data,
1451 empathy_keyring_delete_account_password_finish);
1455 update_account_uri_schemes (EmpathyAccountSettings *self)
1457 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1459 if (priv->uri_scheme_tel == account_has_uri_scheme_tel (priv->account))
1462 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1463 priv->uri_scheme_tel, NULL, NULL);
1467 empathy_account_settings_account_updated (GObject *source,
1468 GAsyncResult *result,
1471 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1472 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1473 GSimpleAsyncResult *r;
1474 GError *error = NULL;
1475 GStrv reconnect_required = NULL;
1477 if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1478 result, &reconnect_required, &error))
1480 g_simple_async_result_set_from_error (priv->apply_result, error);
1481 g_error_free (error);
1485 /* Only set the password in the keyring if the CM supports SASL and
1487 if (priv->supports_sasl && priv->password_changed)
1489 if (priv->password != NULL)
1491 /* FIXME: we shouldn't save the password if we
1492 * can't (MaySaveResponse=False) but we don't have API to check that
1493 * at this point (fdo #35382). */
1494 empathy_keyring_set_account_password_async (priv->account, priv->password,
1495 empathy_account_settings_set_password_cb, settings);
1499 empathy_keyring_delete_account_password_async (priv->account,
1500 empathy_account_settings_delete_password_cb, settings);
1506 update_account_uri_schemes (settings);
1508 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1509 g_strv_length (reconnect_required) > 0);
1512 empathy_account_settings_discard_changes (settings);
1514 r = priv->apply_result;
1515 priv->apply_result = NULL;
1517 g_simple_async_result_complete (r);
1519 g_strfreev (reconnect_required);
1523 empathy_account_settings_created_cb (GObject *source,
1524 GAsyncResult *result,
1527 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1528 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1530 GError *error = NULL;
1531 GSimpleAsyncResult *r;
1533 account = tp_account_manager_create_account_finish (
1534 TP_ACCOUNT_MANAGER (source), result, &error);
1536 if (account == NULL)
1538 g_simple_async_result_set_from_error (priv->apply_result, error);
1542 priv->account = g_object_ref (account);
1544 if (priv->supports_sasl && priv->password != NULL)
1546 /* Save the password before connecting */
1547 /* FIXME: we shouldn't save the password if we
1548 * can't (MaySaveResponse=False) but we don't have API to check that
1549 * at this point (fdo #35382). */
1550 empathy_keyring_set_account_password_async (priv->account,
1551 priv->password, empathy_account_settings_set_password_cb,
1556 update_account_uri_schemes (settings);
1558 empathy_account_settings_discard_changes (settings);
1561 r = priv->apply_result;
1562 priv->apply_result = NULL;
1564 g_simple_async_result_complete (r);
1570 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1572 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1573 GHashTable *properties;
1574 TpConnectionPresenceType type;
1577 EmpathyPresenceManager *presence_mgr;
1579 properties = tp_asv_new (NULL, NULL);
1581 presence_mgr = empathy_presence_manager_dup_singleton ();
1582 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1584 g_object_unref (presence_mgr);
1586 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1588 /* Create the account with the requested presence the same as the current
1589 * global requested presence, but don't enable it */
1590 GValueArray *presence;
1591 GValue vtype = { 0, };
1592 GValue vstatus = { 0, };
1593 GValue vmessage = { 0, };
1595 presence = g_value_array_new (3);
1597 g_value_init (&vtype, G_TYPE_UINT);
1598 g_value_set_uint (&vtype, type);
1599 g_value_array_append (presence, &vtype);
1601 g_value_init (&vstatus, G_TYPE_STRING);
1602 g_value_take_string (&vstatus, status);
1603 g_value_array_append (presence, &vstatus);
1605 g_value_init (&vmessage, G_TYPE_STRING);
1606 g_value_take_string (&vmessage, message);
1607 g_value_array_append (presence, &vmessage);
1609 tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1610 TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1613 tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1616 if (priv->service != NULL)
1617 tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1619 tp_account_manager_create_account_async (priv->account_manager,
1620 priv->cm_name, priv->protocol, priv->display_name,
1621 priv->parameters, properties,
1622 empathy_account_settings_created_cb,
1625 g_hash_table_unref (properties);
1629 empathy_account_settings_manager_ready_cb (GObject *source_object,
1630 GAsyncResult *result,
1633 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1634 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1635 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1636 GError *error = NULL;
1638 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1640 DEBUG ("Failed to prepare account manager: %s", error->message);
1641 g_error_free (error);
1645 g_assert (priv->apply_result != NULL && priv->account == NULL);
1646 empathy_account_settings_do_create_account (settings);
1650 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1651 GAsyncReadyCallback callback,
1654 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1656 if (priv->apply_result != NULL)
1658 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1659 callback, user_data,
1660 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1664 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1665 callback, user_data, empathy_account_settings_apply_finish);
1667 /* We'll have to reconnect only if we change none DBus_Property on an
1668 * existing account. */
1669 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1671 if (priv->account == NULL)
1673 tp_account_manager_prepare_async (priv->account_manager, NULL,
1674 empathy_account_settings_manager_ready_cb, settings);
1678 tp_account_update_parameters_async (priv->account,
1679 priv->parameters, (const gchar **)priv->unset_parameters->data,
1680 empathy_account_settings_account_updated, settings);
1685 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1686 GAsyncResult *result,
1687 gboolean *reconnect_required,
1690 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1694 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1695 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1697 if (reconnect_required != NULL)
1698 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1699 G_SIMPLE_ASYNC_RESULT (result));
1705 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1708 EmpathyAccountSettingsPriv *priv;
1709 const gchar *account_path;
1710 const gchar *priv_account_path;
1712 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1713 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1715 priv = GET_PRIV (settings);
1717 if (priv->account == NULL)
1720 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1721 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1723 return (!tp_strdiff (account_path, priv_account_path));
1727 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1729 const gchar *pattern)
1731 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1733 GError *error = NULL;
1735 regex = g_regex_new (pattern, 0, 0, &error);
1738 g_warning ("Failed to create reg exp: %s", error->message);
1739 g_error_free (error);
1743 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1747 empathy_account_settings_parameter_is_valid (
1748 EmpathyAccountSettings *settings,
1751 EmpathyAccountSettingsPriv *priv;
1752 const GRegex *regex;
1755 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1757 priv = GET_PRIV (settings);
1759 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1761 /* first, look if it's set in our own parameters */
1762 if (tp_asv_lookup (priv->parameters, param))
1765 /* if we did not unset the parameter, look if it's in the account */
1766 if (priv->account != NULL &&
1767 !empathy_account_settings_is_unset (settings, param))
1769 const GHashTable *account_params;
1771 account_params = tp_account_get_parameters (priv->account);
1772 if (tp_asv_lookup (account_params, param))
1780 /* test whether parameter value matches its regex */
1781 regex = g_hash_table_lookup (priv->param_regexps, param);
1784 value = empathy_account_settings_get_string (settings, param);
1785 if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1793 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1795 EmpathyAccountSettingsPriv *priv;
1797 GHashTableIter iter;
1800 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1802 priv = GET_PRIV (settings);
1804 for (l = priv->required_params; l; l = l->next)
1806 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1810 g_hash_table_iter_init (&iter, priv->param_regexps);
1811 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1813 if (!empathy_account_settings_parameter_is_valid (settings, param))
1820 const TpConnectionManagerProtocol *
1821 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1823 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1825 return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1829 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1831 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1833 return priv->supports_sasl;
1837 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1840 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1842 return tp_protocol_has_param (priv->protocol_obj, param);
1846 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1849 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1851 priv->uri_scheme_tel = associate;
1855 empathy_account_settings_has_uri_scheme_tel (
1856 EmpathyAccountSettings *self)
1858 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1860 return priv->uri_scheme_tel;