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 const GValue * empathy_account_settings_get (
496 EmpathyAccountSettings *settings,
500 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
502 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
504 const gchar *password;
506 if (!priv->supports_sasl || empathy_account_settings_get (
507 self, "password") == NULL)
510 /* mission-control still has our password, although the CM
511 * supports SASL. Let's try migrating it. */
513 DEBUG ("Trying to migrate password parameter from MC to the "
514 "keyring ourselves for account %s",
515 tp_account_get_path_suffix (priv->account));
517 v = empathy_account_settings_get (self, "password");
519 /* I can't imagine why this would fail. */
520 if (!G_VALUE_HOLDS_STRING (v))
523 password = g_value_get_string (v);
525 if (EMP_STR_EMPTY (password))
528 empathy_keyring_set_account_password_async (priv->account, password,
529 empathy_account_settings_migrate_password_cb, self);
531 /* We don't want to request the password again, we
532 * already know it. */
533 priv->password_requested = TRUE;
535 priv->password = g_strdup (password);
536 priv->password_original = g_strdup (password);
540 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
542 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
543 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
548 if (priv->account != NULL
549 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
552 if (!empathy_connection_managers_is_ready (priv->managers))
555 if (priv->manager == NULL)
557 priv->manager = empathy_connection_managers_get_cm (
558 priv->managers, priv->cm_name);
561 if (priv->manager == NULL)
564 g_object_ref (priv->manager);
566 if (priv->account != NULL)
568 g_free (priv->display_name);
570 g_strdup (tp_account_get_display_name (priv->account));
572 g_free (priv->icon_name);
574 g_strdup (tp_account_get_icon_name (priv->account));
576 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
579 if (priv->protocol_obj == NULL)
581 priv->protocol_obj = g_object_ref (
582 tp_connection_manager_get_protocol_object (priv->manager,
586 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
587 && !priv->preparing_protocol)
589 priv->preparing_protocol = TRUE;
590 tp_proxy_prepare_async (priv->protocol_obj, features,
591 empathy_account_settings_protocol_obj_prepared_cb, self);
596 if (tp_strv_contains (tp_protocol_get_authentication_types (
598 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
600 priv->supports_sasl = TRUE;
604 if (priv->required_params == NULL)
608 params = tp_protocol_dup_params (priv->protocol_obj);
609 for (l = params; l != NULL; l = g_list_next (l))
611 TpConnectionManagerParam *cur = l->data;
613 if (tp_connection_manager_param_is_required (cur))
615 priv->required_params = g_list_append (priv->required_params,
616 g_strdup (tp_connection_manager_param_get_name (cur)));
620 g_list_free_full (params,
621 (GDestroyNotify) tp_connection_manager_param_free);
624 /* NOTE: When removing MC migration code, remove this call, and the
625 * function it's calling. That's it. */
626 empathy_account_settings_try_migrating_password (self);
628 /* priv->account won't be a proper account if it's the account
629 * assistant showing this widget. */
630 if (priv->supports_sasl && !priv->password_requested
631 && priv->account != NULL)
633 priv->password_requested = TRUE;
635 /* Make this call but don't block on its readiness. We'll signal
636 * if it's updated later with ::password-retrieved. */
637 empathy_keyring_get_account_password_async (priv->account,
638 empathy_account_settings_get_password_cb, self);
642 g_object_notify (G_OBJECT (self), "ready");
646 empathy_account_settings_account_ready_cb (GObject *source_object,
647 GAsyncResult *result,
650 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
651 TpAccount *account = TP_ACCOUNT (source_object);
652 GError *error = NULL;
654 if (!tp_proxy_prepare_finish (account, result, &error))
656 DEBUG ("Failed to prepare account: %s", error->message);
657 g_error_free (error);
661 empathy_account_settings_check_readyness (settings);
665 empathy_account_settings_managers_ready_cb (GObject *object,
669 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
671 empathy_account_settings_check_readyness (settings);
674 EmpathyAccountSettings *
675 empathy_account_settings_new (const gchar *connection_manager,
676 const gchar *protocol,
677 const gchar *service,
678 const char *display_name)
680 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
681 "connection-manager", connection_manager,
682 "protocol", protocol,
684 "display-name", display_name,
688 EmpathyAccountSettings *
689 empathy_account_settings_new_for_account (TpAccount *account)
691 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
697 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
699 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
701 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
703 return tp_protocol_dup_params (priv->protocol_obj);
707 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
709 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
715 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
717 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
719 return priv->cm_name;
723 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
725 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
727 return priv->protocol;
731 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
733 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
735 return priv->service;
739 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
740 const gchar *service)
742 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
744 if (!tp_strdiff (priv->service, service))
747 g_free (priv->service);
748 priv->service = g_strdup (service);
749 g_object_notify (G_OBJECT (settings), "service");
750 priv->update_service = TRUE;
754 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
756 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
758 return priv->icon_name;
762 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
764 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
766 return priv->display_name;
770 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
772 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
774 return priv->account;
778 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
781 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
785 a = priv->unset_parameters;
787 for (i = 0; i < a->len; i++)
789 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
796 static const TpConnectionManagerParam *
797 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
800 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
802 return tp_protocol_get_param (priv->protocol_obj, param);
806 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
809 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
813 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
816 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
820 for (idx = 0; idx < priv->unset_parameters->len; idx++)
822 val = g_array_index (priv->unset_parameters, gchar *, idx);
824 if (!tp_strdiff (val, param))
826 priv->unset_parameters =
827 g_array_remove_index (priv->unset_parameters, idx);
836 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
839 const TpConnectionManagerParam *p;
841 p = empathy_account_settings_get_tp_param (settings, param);
843 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
846 return &(p->default_value);
850 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
853 const TpConnectionManagerParam *p;
855 p = empathy_account_settings_get_tp_param (settings, param);
860 return tp_connection_manager_param_get_dbus_signature (p);
863 static const GValue *
864 empathy_account_settings_get (EmpathyAccountSettings *settings,
867 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
868 const GValue *result = NULL;
871 /* Lookup the update parameters we set */
872 variant = g_hash_table_lookup (priv->parameters, param);
875 GValue v = G_VALUE_INIT;
877 dbus_g_value_parse_g_variant (variant, &v);
878 result = (const GValue *) tp_g_value_slice_dup (&v);
881 /* FIXME: this GValue is leaked */
885 /* If the parameters isn't unset use the accounts setting if any */
886 if (priv->account != NULL
887 && !empathy_account_settings_is_unset (settings, param))
889 const GHashTable *parameters;
891 parameters = tp_account_get_parameters (priv->account);
892 result = tp_asv_lookup (parameters, param);
898 /* fallback to the default */
899 return empathy_account_settings_get_default (settings, param);
903 empathy_account_settings_unset (EmpathyAccountSettings *settings,
906 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
908 if (empathy_account_settings_is_unset (settings, param))
911 if (priv->supports_sasl && !tp_strdiff (param, "password"))
913 g_free (priv->password);
914 priv->password = NULL;
915 priv->password_changed = TRUE;
919 v = g_strdup (param);
921 g_array_append_val (priv->unset_parameters, v);
922 g_hash_table_remove (priv->parameters, param);
926 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
928 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
930 g_hash_table_remove_all (priv->parameters);
931 empathy_account_settings_free_unset_parameters (settings);
933 priv->password_changed = FALSE;
934 g_free (priv->password);
935 priv->password = g_strdup (priv->password_original);
937 if (priv->account != NULL)
938 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
940 priv->uri_scheme_tel = FALSE;
944 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
947 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
950 if (!tp_strdiff (param, "password") && priv->supports_sasl)
952 return priv->password;
955 v = empathy_account_settings_get (settings, param);
957 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
960 return g_value_get_string (v);
963 const gchar * const *
964 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
969 v = empathy_account_settings_get (settings, param);
971 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
974 return g_value_get_boxed (v);
978 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
984 v = empathy_account_settings_get (settings, param);
989 switch G_VALUE_TYPE (v)
992 ret = g_value_get_uchar (v);
995 ret = g_value_get_int (v);
998 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
1002 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
1005 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
1017 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
1023 v = empathy_account_settings_get (settings, param);
1027 switch G_VALUE_TYPE (v)
1030 ret = g_value_get_uchar (v);
1033 ret = g_value_get_int (v);
1036 ret = g_value_get_uint (v);
1039 ret = g_value_get_int64 (v);
1042 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1053 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1059 v = empathy_account_settings_get (settings, param);
1063 switch G_VALUE_TYPE (v)
1066 ret = g_value_get_uchar (v);
1069 ret = MAX (0, g_value_get_int (v));
1072 ret = g_value_get_uint (v);
1075 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1078 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1089 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1095 v = empathy_account_settings_get (settings, param);
1097 if (v == NULL || !G_VALUE_HOLDS_INT (v))
1100 switch G_VALUE_TYPE (v)
1103 ret = g_value_get_uchar (v);
1106 ret = MAX (0, g_value_get_int (v));
1109 ret = g_value_get_uint (v);
1112 ret = MAX (0, g_value_get_int64 (v));
1115 ret = g_value_get_uint64 (v);
1126 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1131 v = empathy_account_settings_get (settings, param);
1133 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1136 return g_value_get_boolean (v);
1140 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1144 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1146 g_return_if_fail (param != NULL);
1147 g_return_if_fail (value != NULL);
1149 if (!tp_strdiff (param, "password") && priv->supports_sasl)
1151 g_free (priv->password);
1152 priv->password = g_strdup (value);
1153 priv->password_changed = TRUE;
1157 g_hash_table_insert (priv->parameters, g_strdup (param),
1158 g_variant_ref_sink (g_variant_new_string (value)));
1161 account_settings_remove_from_unset (settings, param);
1165 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1167 const gchar * const *value)
1169 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1171 g_return_if_fail (param != NULL);
1172 g_return_if_fail (value != NULL);
1174 g_hash_table_insert (priv->parameters, g_strdup (param),
1175 g_variant_ref_sink (g_variant_new_strv (value, -1)));
1177 account_settings_remove_from_unset (settings, param);
1181 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1185 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1187 g_return_if_fail (param != NULL);
1189 g_hash_table_insert (priv->parameters, g_strdup (param),
1190 g_variant_ref_sink (g_variant_new_int32 (value)));
1192 account_settings_remove_from_unset (settings, param);
1196 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1200 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1202 g_return_if_fail (param != NULL);
1204 g_hash_table_insert (priv->parameters, g_strdup (param),
1205 g_variant_ref_sink (g_variant_new_int64 (value)));
1207 account_settings_remove_from_unset (settings, param);
1211 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1215 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1217 g_return_if_fail (param != NULL);
1219 g_hash_table_insert (priv->parameters, g_strdup (param),
1220 g_variant_ref_sink (g_variant_new_uint32 (value)));
1222 account_settings_remove_from_unset (settings, param);
1226 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1230 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1232 g_return_if_fail (param != NULL);
1234 g_hash_table_insert (priv->parameters, g_strdup (param),
1235 g_variant_ref_sink (g_variant_new_uint64 (value)));
1237 account_settings_remove_from_unset (settings, param);
1241 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1245 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1247 g_return_if_fail (param != NULL);
1249 g_hash_table_insert (priv->parameters, g_strdup (param),
1250 g_variant_ref_sink (g_variant_new_boolean (value)));
1252 account_settings_remove_from_unset (settings, param);
1256 account_settings_display_name_set_cb (GObject *src,
1260 GError *error = NULL;
1261 TpAccount *account = TP_ACCOUNT (src);
1262 GSimpleAsyncResult *set_result = user_data;
1264 tp_account_set_display_name_finish (account, res, &error);
1268 g_simple_async_result_set_from_error (set_result, error);
1269 g_error_free (error);
1272 g_simple_async_result_complete (set_result);
1273 g_object_unref (set_result);
1277 empathy_account_settings_set_display_name_async (
1278 EmpathyAccountSettings *settings,
1280 GAsyncReadyCallback callback,
1283 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1284 GSimpleAsyncResult *result;
1286 g_return_if_fail (name != NULL);
1288 result = g_simple_async_result_new (G_OBJECT (settings),
1289 callback, user_data, empathy_account_settings_set_display_name_finish);
1291 if (!tp_strdiff (name, priv->display_name))
1294 g_simple_async_result_complete_in_idle (result);
1298 if (priv->account == NULL)
1300 if (priv->display_name != NULL)
1301 g_free (priv->display_name);
1303 priv->display_name = g_strdup (name);
1305 g_simple_async_result_complete_in_idle (result);
1310 tp_account_set_display_name_async (priv->account, name,
1311 account_settings_display_name_set_cb, result);
1315 empathy_account_settings_set_display_name_finish (
1316 EmpathyAccountSettings *settings,
1317 GAsyncResult *result,
1320 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1324 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1325 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1332 account_settings_icon_name_set_cb (GObject *src,
1336 GError *error = NULL;
1337 TpAccount *account = TP_ACCOUNT (src);
1338 GSimpleAsyncResult *set_result = user_data;
1340 tp_account_set_icon_name_finish (account, res, &error);
1344 g_simple_async_result_set_from_error (set_result, error);
1345 g_error_free (error);
1348 g_simple_async_result_complete (set_result);
1349 g_object_unref (set_result);
1353 empathy_account_settings_set_icon_name_async (
1354 EmpathyAccountSettings *settings,
1356 GAsyncReadyCallback callback,
1359 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1360 GSimpleAsyncResult *result;
1362 g_return_if_fail (name != NULL);
1364 result = g_simple_async_result_new (G_OBJECT (settings),
1365 callback, user_data, empathy_account_settings_set_icon_name_finish);
1367 if (priv->account == NULL)
1369 if (priv->icon_name != NULL)
1370 g_free (priv->icon_name);
1372 priv->icon_name = g_strdup (name);
1374 g_simple_async_result_complete_in_idle (result);
1379 tp_account_set_icon_name_async (priv->account, name,
1380 account_settings_icon_name_set_cb, result);
1384 empathy_account_settings_set_icon_name_finish (
1385 EmpathyAccountSettings *settings,
1386 GAsyncResult *result,
1389 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1393 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1394 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1401 empathy_account_settings_processed_password (GObject *source,
1402 GAsyncResult *result,
1404 gpointer finish_func)
1406 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1407 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1408 GSimpleAsyncResult *r;
1409 GError *error = NULL;
1410 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1413 g_free (priv->password_original);
1414 priv->password_original = g_strdup (priv->password);
1416 if (!func (TP_ACCOUNT (source), result, &error))
1418 g_simple_async_result_set_from_error (priv->apply_result, error);
1419 g_error_free (error);
1422 empathy_account_settings_discard_changes (settings);
1424 r = priv->apply_result;
1425 priv->apply_result = NULL;
1427 g_simple_async_result_complete (r);
1432 empathy_account_settings_set_password_cb (GObject *source,
1433 GAsyncResult *result,
1436 empathy_account_settings_processed_password (source, result, user_data,
1437 empathy_keyring_set_account_password_finish);
1441 empathy_account_settings_delete_password_cb (GObject *source,
1442 GAsyncResult *result,
1445 empathy_account_settings_processed_password (source, result, user_data,
1446 empathy_keyring_delete_account_password_finish);
1450 update_account_uri_schemes (EmpathyAccountSettings *self)
1452 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1454 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1458 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1459 priv->uri_scheme_tel, NULL, NULL);
1463 set_service_cb (GObject *source,
1464 GAsyncResult *result,
1467 GError *error = NULL;
1469 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1471 DEBUG ("Failed to set Account.Service: %s", error->message);
1472 g_error_free (error);
1477 update_account_service (EmpathyAccountSettings *self)
1479 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1481 if (!priv->update_service)
1484 tp_account_set_service_async (priv->account,
1485 priv->service != NULL ? priv->service : "", set_service_cb, self);
1489 empathy_account_settings_account_updated (GObject *source,
1490 GAsyncResult *result,
1493 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1494 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1495 GSimpleAsyncResult *r;
1496 GError *error = NULL;
1497 GStrv reconnect_required = NULL;
1499 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1500 result, &reconnect_required, &error))
1502 g_simple_async_result_set_from_error (priv->apply_result, error);
1503 g_error_free (error);
1507 /* Only set the password in the keyring if the CM supports SASL and
1509 if (priv->supports_sasl && priv->password_changed)
1511 if (priv->password != NULL)
1513 /* FIXME: we shouldn't save the password if we
1514 * can't (MaySaveResponse=False) but we don't have API to check that
1515 * at this point (fdo #35382). */
1516 empathy_keyring_set_account_password_async (priv->account, priv->password,
1517 empathy_account_settings_set_password_cb, settings);
1521 empathy_keyring_delete_account_password_async (priv->account,
1522 empathy_account_settings_delete_password_cb, settings);
1528 update_account_uri_schemes (settings);
1529 update_account_service (settings);
1531 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1532 g_strv_length (reconnect_required) > 0);
1535 empathy_account_settings_discard_changes (settings);
1537 r = priv->apply_result;
1538 priv->apply_result = NULL;
1540 g_simple_async_result_complete (r);
1542 g_strfreev (reconnect_required);
1546 empathy_account_settings_created_cb (GObject *source,
1547 GAsyncResult *result,
1550 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1551 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1552 GError *error = NULL;
1553 GSimpleAsyncResult *r;
1555 priv->account = tp_account_request_create_account_finish (
1556 TP_ACCOUNT_REQUEST (source), result, &error);
1558 if (priv->account == NULL)
1560 g_simple_async_result_set_from_error (priv->apply_result, error);
1564 if (priv->supports_sasl && priv->password != NULL)
1566 /* Save the password before connecting */
1567 /* FIXME: we shouldn't save the password if we
1568 * can't (MaySaveResponse=False) but we don't have API to check that
1569 * at this point (fdo #35382). */
1570 empathy_keyring_set_account_password_async (priv->account,
1571 priv->password, empathy_account_settings_set_password_cb,
1576 update_account_uri_schemes (settings);
1578 empathy_account_settings_discard_changes (settings);
1581 r = priv->apply_result;
1582 priv->apply_result = NULL;
1584 g_simple_async_result_complete (r);
1589 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1591 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1592 TpAccountRequest *account_req;
1593 TpConnectionPresenceType type;
1596 EmpathyPresenceManager *presence_mgr;
1597 GHashTableIter iter;
1600 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1601 priv->protocol, "New Account");
1603 presence_mgr = empathy_presence_manager_dup_singleton ();
1604 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1606 g_object_unref (presence_mgr);
1608 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1610 tp_account_request_set_requested_presence (account_req, type,
1614 tp_account_request_set_icon_name (account_req, priv->icon_name);
1616 tp_account_request_set_display_name (account_req, priv->display_name);
1618 if (priv->service != NULL)
1619 tp_account_request_set_service (account_req, priv->service);
1621 g_hash_table_iter_init (&iter, priv->parameters);
1622 while (g_hash_table_iter_next (&iter, &k, &v))
1624 const gchar *key = k;
1625 GVariant *value = v;
1627 tp_account_request_set_parameter (account_req, key, value);
1630 tp_account_request_create_account_async (account_req,
1631 empathy_account_settings_created_cb, self);
1635 build_parameters_variant (EmpathyAccountSettings *self)
1637 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1638 GVariantBuilder *builder;
1639 GHashTableIter iter;
1642 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1644 g_hash_table_iter_init (&iter, priv->parameters);
1645 while (g_hash_table_iter_next (&iter, &k, &v))
1647 const gchar *key = k;
1648 GVariant *value = v;
1651 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1652 g_variant_new_variant (value));
1654 g_variant_builder_add_value (builder, entry);
1657 return g_variant_builder_end (builder);
1661 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1662 GAsyncReadyCallback callback,
1665 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1667 if (priv->apply_result != NULL)
1669 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1670 callback, user_data,
1671 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1675 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1676 callback, user_data, empathy_account_settings_apply_finish);
1678 /* We'll have to reconnect only if we change none DBus_Property on an
1679 * existing account. */
1680 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1682 if (priv->account == NULL)
1684 g_assert (priv->apply_result != NULL && priv->account == NULL);
1686 empathy_account_settings_do_create_account (settings);
1690 tp_account_update_parameters_vardict_async (priv->account,
1691 build_parameters_variant (settings),
1692 (const gchar **) priv->unset_parameters->data,
1693 empathy_account_settings_account_updated, settings);
1698 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1699 GAsyncResult *result,
1700 gboolean *reconnect_required,
1703 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1707 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1708 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1710 if (reconnect_required != NULL)
1711 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1712 G_SIMPLE_ASYNC_RESULT (result));
1718 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1721 EmpathyAccountSettingsPriv *priv;
1722 const gchar *account_path;
1723 const gchar *priv_account_path;
1725 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1726 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1728 priv = GET_PRIV (settings);
1730 if (priv->account == NULL)
1733 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1734 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1736 return (!tp_strdiff (account_path, priv_account_path));
1740 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1742 const gchar *pattern)
1744 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1746 GError *error = NULL;
1748 regex = g_regex_new (pattern, 0, 0, &error);
1751 g_warning ("Failed to create reg exp: %s", error->message);
1752 g_error_free (error);
1756 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1760 empathy_account_settings_parameter_is_valid (
1761 EmpathyAccountSettings *settings,
1764 EmpathyAccountSettingsPriv *priv;
1765 const GRegex *regex;
1768 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1770 priv = GET_PRIV (settings);
1772 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1774 /* first, look if it's set in our own parameters */
1775 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1778 /* if we did not unset the parameter, look if it's in the account */
1779 if (priv->account != NULL &&
1780 !empathy_account_settings_is_unset (settings, param))
1782 const GHashTable *account_params;
1784 account_params = tp_account_get_parameters (priv->account);
1785 if (tp_asv_lookup (account_params, param))
1793 /* test whether parameter value matches its regex */
1794 regex = g_hash_table_lookup (priv->param_regexps, param);
1797 value = empathy_account_settings_get_string (settings, param);
1798 if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1806 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1808 EmpathyAccountSettingsPriv *priv;
1810 GHashTableIter iter;
1813 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1815 priv = GET_PRIV (settings);
1817 for (l = priv->required_params; l; l = l->next)
1819 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1823 g_hash_table_iter_init (&iter, priv->param_regexps);
1824 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1826 if (!empathy_account_settings_parameter_is_valid (settings, param))
1834 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1836 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1838 return priv->protocol_obj;
1842 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1844 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1846 return priv->supports_sasl;
1850 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1853 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1855 return tp_protocol_has_param (priv->protocol_obj, param);
1859 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1862 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1864 priv->uri_scheme_tel = associate;
1868 empathy_account_settings_has_uri_scheme_tel (
1869 EmpathyAccountSettings *self)
1871 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1873 return priv->uri_scheme_tel;