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 GSimpleAsyncResult *apply_result;
107 empathy_account_settings_init (EmpathyAccountSettings *obj)
109 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
110 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
114 /* allocate any data required by the object here */
115 priv->managers = empathy_connection_managers_dup_singleton ();
116 priv->account_manager = tp_account_manager_dup ();
118 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
119 g_free, (GDestroyNotify) tp_g_value_slice_free);
121 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
122 g_free, (GDestroyNotify) g_regex_unref);
124 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
126 priv->required_params = NULL;
129 static void empathy_account_settings_dispose (GObject *object);
130 static void empathy_account_settings_finalize (GObject *object);
131 static void empathy_account_settings_account_ready_cb (GObject *source_object,
132 GAsyncResult *result, gpointer user_data);
133 static void empathy_account_settings_managers_ready_cb (GObject *obj,
134 GParamSpec *pspec, gpointer user_data);
135 static void empathy_account_settings_check_readyness (
136 EmpathyAccountSettings *self);
139 empathy_account_settings_set_property (GObject *object,
144 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
145 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
150 priv->account = g_value_dup_object (value);
153 priv->cm_name = g_value_dup_string (value);
156 priv->protocol = g_value_dup_string (value);
159 priv->service = g_value_dup_string (value);
161 case PROP_DISPLAY_NAME:
162 priv->display_name = g_value_dup_string (value);
164 case PROP_DISPLAY_NAME_OVERRIDDEN:
165 priv->display_name_overridden = g_value_get_boolean (value);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 empathy_account_settings_get_property (GObject *object,
179 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
180 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
185 g_value_set_object (value, priv->account);
188 g_value_set_string (value, priv->cm_name);
191 g_value_set_string (value, priv->protocol);
194 g_value_set_string (value, priv->service);
196 case PROP_DISPLAY_NAME:
197 g_value_set_string (value, priv->display_name);
199 case PROP_DISPLAY_NAME_OVERRIDDEN:
200 g_value_set_boolean (value, priv->display_name_overridden);
203 g_value_set_boolean (value, priv->ready);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 empathy_account_settings_constructed (GObject *object)
214 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
215 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
217 if (priv->account != NULL)
219 g_free (priv->cm_name);
220 g_free (priv->protocol);
221 g_free (priv->service);
224 g_strdup (tp_account_get_connection_manager (priv->account));
226 g_strdup (tp_account_get_protocol (priv->account));
228 g_strdup (tp_account_get_service (priv->account));
229 priv->icon_name = g_strdup
230 (tp_account_get_icon_name (priv->account));
234 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
237 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
239 empathy_account_settings_check_readyness (self);
243 GQuark features[] = {
244 TP_ACCOUNT_FEATURE_CORE,
245 TP_ACCOUNT_FEATURE_STORAGE,
248 if (priv->account != NULL)
250 tp_proxy_prepare_async (priv->account, features,
251 empathy_account_settings_account_ready_cb, self);
254 tp_g_signal_connect_object (priv->managers, "notify::ready",
255 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
259 empathy_account_settings_parent_class)->constructed != NULL)
261 empathy_account_settings_parent_class)->constructed (object);
266 empathy_account_settings_class_init (
267 EmpathyAccountSettingsClass *empathy_account_settings_class)
269 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
271 g_type_class_add_private (empathy_account_settings_class, sizeof
272 (EmpathyAccountSettingsPriv));
274 object_class->dispose = empathy_account_settings_dispose;
275 object_class->finalize = empathy_account_settings_finalize;
276 object_class->set_property = empathy_account_settings_set_property;
277 object_class->get_property = empathy_account_settings_get_property;
278 object_class->constructed = empathy_account_settings_constructed;
280 g_object_class_install_property (object_class, PROP_ACCOUNT,
281 g_param_spec_object ("account",
283 "The TpAccount backing these settings",
285 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
287 g_object_class_install_property (object_class, PROP_CM_NAME,
288 g_param_spec_string ("connection-manager",
289 "connection-manager",
290 "The name of the connection manager this account uses",
292 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
294 g_object_class_install_property (object_class, PROP_PROTOCOL,
295 g_param_spec_string ("protocol",
297 "The name of the protocol this account uses",
299 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
301 g_object_class_install_property (object_class, PROP_SERVICE,
302 g_param_spec_string ("service",
304 "The service of this account, or NULL",
306 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
308 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
309 g_param_spec_string ("display-name",
311 "The display name account these settings belong to",
313 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
315 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
316 g_param_spec_boolean ("display-name-overridden",
317 "display-name-overridden",
318 "Whether the display name for this account has been manually "
321 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
323 g_object_class_install_property (object_class, PROP_READY,
324 g_param_spec_boolean ("ready",
326 "Whether this account is ready to be used",
328 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
330 signals[PASSWORD_RETRIEVED] =
331 g_signal_new ("password-retrieved",
332 G_TYPE_FROM_CLASS (empathy_account_settings_class),
333 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
334 g_cclosure_marshal_VOID__VOID,
339 empathy_account_settings_dispose (GObject *object)
341 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
342 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
344 if (priv->dispose_has_run)
347 priv->dispose_has_run = TRUE;
349 if (priv->managers_ready_id != 0)
350 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
351 priv->managers_ready_id = 0;
353 tp_clear_object (&priv->managers);
354 tp_clear_object (&priv->manager);
355 tp_clear_object (&priv->account_manager);
356 tp_clear_object (&priv->account);
357 tp_clear_object (&priv->protocol_obj);
359 /* release any references held by the object here */
360 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
361 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
365 empathy_account_settings_free_unset_parameters (
366 EmpathyAccountSettings *settings)
368 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
371 for (i = 0 ; i < priv->unset_parameters->len; i++)
372 g_free (g_array_index (priv->unset_parameters, gchar *, i));
374 g_array_set_size (priv->unset_parameters, 0);
378 empathy_account_settings_finalize (GObject *object)
380 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
381 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
384 /* free any data held directly by the object here */
385 g_free (priv->cm_name);
386 g_free (priv->protocol);
387 g_free (priv->service);
388 g_free (priv->display_name);
389 g_free (priv->icon_name);
390 g_free (priv->password);
391 g_free (priv->password_original);
393 if (priv->required_params != NULL)
395 for (l = priv->required_params; l; l = l->next)
397 g_list_free (priv->required_params);
400 g_hash_table_destroy (priv->parameters);
401 g_hash_table_destroy (priv->param_regexps);
403 empathy_account_settings_free_unset_parameters (self);
404 g_array_free (priv->unset_parameters, TRUE);
406 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
410 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
411 GAsyncResult *result,
414 EmpathyAccountSettings *self = user_data;
415 GError *error = NULL;
417 if (!tp_proxy_prepare_finish (source, result, &error))
419 DEBUG ("Failed to prepare protocol object: %s", error->message);
420 g_clear_error (&error);
424 empathy_account_settings_check_readyness (self);
428 empathy_account_settings_get_password_cb (GObject *source,
429 GAsyncResult *result,
432 EmpathyAccountSettings *self = user_data;
433 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
434 const gchar *password;
435 GError *error = NULL;
437 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
442 DEBUG ("Failed to get password: %s", error->message);
443 g_clear_error (&error);
446 /* It doesn't really matter if getting the password failed; that
447 * just means that it's not there, or let's act like that at
450 g_assert (priv->password == NULL);
452 priv->password = g_strdup (password);
453 priv->password_original = g_strdup (password);
455 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
459 empathy_account_settings_migrate_password_cb (GObject *source,
460 GAsyncResult *result,
463 TpAccount *account = TP_ACCOUNT (source);
464 GError *error = NULL;
465 EmpathyAccountSettings *self = user_data;
466 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
468 const gchar *unset[] = { "password", NULL };
470 if (!empathy_keyring_set_account_password_finish (account, result, &error))
472 DEBUG ("Failed to set password: %s", error->message);
473 g_clear_error (&error);
477 /* Now clear the password MC has stored. */
478 empty = tp_asv_new (NULL, NULL);
479 tp_account_update_parameters_async (priv->account,
480 empty, unset, NULL, NULL);
482 g_hash_table_remove (priv->parameters, "password");
484 g_hash_table_unref (empty);
488 empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
490 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
492 const gchar *password;
494 if (!priv->supports_sasl || empathy_account_settings_get (
495 self, "password") == NULL)
498 /* mission-control still has our password, although the CM
499 * supports SASL. Let's try migrating it. */
501 DEBUG ("Trying to migrate password parameter from MC to the "
502 "keyring ourselves for account %s",
503 tp_account_get_path_suffix (priv->account));
505 v = empathy_account_settings_get (self, "password");
507 /* I can't imagine why this would fail. */
508 if (!G_VALUE_HOLDS_STRING (v))
511 password = g_value_get_string (v);
513 if (EMP_STR_EMPTY (password))
516 empathy_keyring_set_account_password_async (priv->account, password,
517 empathy_account_settings_migrate_password_cb, self);
519 /* We don't want to request the password again, we
520 * already know it. */
521 priv->password_requested = TRUE;
523 priv->password = g_strdup (password);
524 priv->password_original = g_strdup (password);
528 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
530 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
531 const TpConnectionManagerProtocol *tp_protocol;
532 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
537 if (priv->account != NULL
538 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
541 if (!empathy_connection_managers_is_ready (priv->managers))
544 if (priv->manager == NULL)
546 priv->manager = empathy_connection_managers_get_cm (
547 priv->managers, priv->cm_name);
550 if (priv->manager == NULL)
553 g_object_ref (priv->manager);
555 if (priv->account != NULL)
557 g_free (priv->display_name);
559 g_strdup (tp_account_get_display_name (priv->account));
561 g_free (priv->icon_name);
563 g_strdup (tp_account_get_icon_name (priv->account));
566 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
569 if (tp_protocol == NULL)
571 tp_clear_object (&priv->manager);
575 if (priv->required_params == NULL)
577 TpConnectionManagerParam *cur;
579 for (cur = tp_protocol->params; cur->name != NULL; cur++)
581 if (tp_connection_manager_param_is_required (cur))
583 priv->required_params = g_list_append (priv->required_params,
584 g_strdup (cur->name));
589 if (priv->protocol_obj == NULL)
591 priv->protocol_obj = g_object_ref (
592 tp_connection_manager_get_protocol_object (priv->manager,
596 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
597 && !priv->preparing_protocol)
599 priv->preparing_protocol = TRUE;
600 tp_proxy_prepare_async (priv->protocol_obj, features,
601 empathy_account_settings_protocol_obj_prepared_cb, self);
606 if (tp_strv_contains (tp_protocol_get_authentication_types (
608 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
610 priv->supports_sasl = TRUE;
614 /* NOTE: When removing MC migration code, remove this call, and the
615 * function it's calling. That's it. */
616 empathy_account_settings_try_migrating_password (self);
618 /* priv->account won't be a proper account if it's the account
619 * assistant showing this widget. */
620 if (priv->supports_sasl && !priv->password_requested
621 && priv->account != NULL)
623 priv->password_requested = TRUE;
625 /* Make this call but don't block on its readiness. We'll signal
626 * if it's updated later with ::password-retrieved. */
627 empathy_keyring_get_account_password_async (priv->account,
628 empathy_account_settings_get_password_cb, self);
632 g_object_notify (G_OBJECT (self), "ready");
636 empathy_account_settings_account_ready_cb (GObject *source_object,
637 GAsyncResult *result,
640 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
641 TpAccount *account = TP_ACCOUNT (source_object);
642 GError *error = NULL;
644 if (!tp_proxy_prepare_finish (account, result, &error))
646 DEBUG ("Failed to prepare account: %s", error->message);
647 g_error_free (error);
651 empathy_account_settings_check_readyness (settings);
655 empathy_account_settings_managers_ready_cb (GObject *object,
659 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
661 empathy_account_settings_check_readyness (settings);
664 EmpathyAccountSettings *
665 empathy_account_settings_new (const gchar *connection_manager,
666 const gchar *protocol,
667 const gchar *service,
668 const char *display_name)
670 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
671 "connection-manager", connection_manager,
672 "protocol", protocol,
674 "display-name", display_name,
678 EmpathyAccountSettings *
679 empathy_account_settings_new_for_account (TpAccount *account)
681 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
686 TpConnectionManagerParam *
687 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
689 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
690 const TpConnectionManagerProtocol *tp_protocol;
692 g_return_val_if_fail (priv->manager != NULL, NULL);
693 g_return_val_if_fail (priv->protocol != NULL, NULL);
695 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
697 if (tp_protocol == NULL)
699 DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
704 return tp_protocol->params;
708 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
710 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
716 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
718 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
720 return priv->cm_name;
724 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
726 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
728 return priv->protocol;
732 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
734 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
736 return priv->service;
740 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
742 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
744 return priv->icon_name;
748 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
750 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
752 return priv->display_name;
756 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
758 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
760 return priv->account;
764 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
767 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
771 a = priv->unset_parameters;
773 for (i = 0; i < a->len; i++)
775 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
782 static TpConnectionManagerParam *
783 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
786 TpConnectionManagerParam *tp_params =
787 empathy_account_settings_get_tp_params (settings);
788 TpConnectionManagerParam *p;
790 for (p = tp_params; p != NULL && p->name != NULL; p++)
792 if (tp_strdiff (p->name, param))
802 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
805 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
809 for (idx = 0; idx < priv->unset_parameters->len; idx++)
811 val = g_array_index (priv->unset_parameters, gchar *, idx);
813 if (!tp_strdiff (val, param))
815 priv->unset_parameters =
816 g_array_remove_index (priv->unset_parameters, idx);
825 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
828 TpConnectionManagerParam *p;
830 p = empathy_account_settings_get_tp_param (settings, param);
832 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
835 return &(p->default_value);
839 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
842 TpConnectionManagerParam *p;
844 p = empathy_account_settings_get_tp_param (settings, param);
849 return p->dbus_signature;
853 empathy_account_settings_get (EmpathyAccountSettings *settings,
856 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
857 const GValue *result = NULL;
859 /* Lookup the update parameters we set */
860 result = tp_asv_lookup (priv->parameters, param);
864 /* If the parameters isn't unset use the accounts setting if any */
865 if (priv->account != NULL
866 && !empathy_account_settings_is_unset (settings, param))
868 const GHashTable *parameters;
870 parameters = tp_account_get_parameters (priv->account);
871 result = tp_asv_lookup (parameters, param);
877 /* fallback to the default */
878 return empathy_account_settings_get_default (settings, param);
882 empathy_account_settings_unset (EmpathyAccountSettings *settings,
885 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
887 if (empathy_account_settings_is_unset (settings, param))
890 if (priv->supports_sasl && !tp_strdiff (param, "password"))
892 g_free (priv->password);
893 priv->password = NULL;
894 priv->password_changed = TRUE;
898 v = g_strdup (param);
900 g_array_append_val (priv->unset_parameters, v);
901 g_hash_table_remove (priv->parameters, param);
905 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
907 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
909 g_hash_table_remove_all (priv->parameters);
910 empathy_account_settings_free_unset_parameters (settings);
912 priv->password_changed = FALSE;
913 g_free (priv->password);
914 priv->password = g_strdup (priv->password_original);
918 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
921 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
924 if (!tp_strdiff (param, "password") && priv->supports_sasl)
926 return priv->password;
929 v = empathy_account_settings_get (settings, param);
931 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
934 return g_value_get_string (v);
937 const gchar * const *
938 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
943 v = empathy_account_settings_get (settings, param);
945 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
948 return g_value_get_boxed (v);
952 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
958 v = empathy_account_settings_get (settings, param);
963 switch G_VALUE_TYPE (v)
966 ret = g_value_get_uchar (v);
969 ret = g_value_get_int (v);
972 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
976 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
979 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
991 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
997 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 = g_value_get_uint (v);
1013 ret = g_value_get_int64 (v);
1016 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1027 empathy_account_settings_get_uint32 (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 = MAX (0, g_value_get_int (v));
1046 ret = g_value_get_uint (v);
1049 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1052 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1063 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1069 v = empathy_account_settings_get (settings, param);
1071 if (v == NULL || !G_VALUE_HOLDS_INT (v))
1074 switch G_VALUE_TYPE (v)
1077 ret = g_value_get_uchar (v);
1080 ret = MAX (0, g_value_get_int (v));
1083 ret = g_value_get_uint (v);
1086 ret = MAX (0, g_value_get_int64 (v));
1089 ret = g_value_get_uint64 (v);
1100 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1105 v = empathy_account_settings_get (settings, param);
1107 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1110 return g_value_get_boolean (v);
1114 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1118 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1120 g_return_if_fail (param != NULL);
1121 g_return_if_fail (value != NULL);
1123 if (!tp_strdiff (param, "password") && priv->supports_sasl)
1125 g_free (priv->password);
1126 priv->password = g_strdup (value);
1127 priv->password_changed = TRUE;
1131 tp_asv_set_string (priv->parameters, g_strdup (param), value);
1134 account_settings_remove_from_unset (settings, param);
1138 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1142 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1144 g_return_if_fail (param != NULL);
1145 g_return_if_fail (value != NULL);
1147 tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1149 account_settings_remove_from_unset (settings, param);
1153 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1157 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1159 g_return_if_fail (param != NULL);
1161 tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1163 account_settings_remove_from_unset (settings, param);
1167 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1171 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1173 g_return_if_fail (param != NULL);
1175 tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1177 account_settings_remove_from_unset (settings, param);
1181 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1185 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1187 g_return_if_fail (param != NULL);
1189 tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1191 account_settings_remove_from_unset (settings, param);
1195 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1199 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1201 g_return_if_fail (param != NULL);
1203 tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1205 account_settings_remove_from_unset (settings, param);
1209 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1213 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1215 g_return_if_fail (param != NULL);
1217 tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1219 account_settings_remove_from_unset (settings, param);
1223 account_settings_display_name_set_cb (GObject *src,
1227 GError *error = NULL;
1228 TpAccount *account = TP_ACCOUNT (src);
1229 GSimpleAsyncResult *set_result = user_data;
1231 tp_account_set_display_name_finish (account, res, &error);
1235 g_simple_async_result_set_from_error (set_result, error);
1236 g_error_free (error);
1239 g_simple_async_result_complete (set_result);
1240 g_object_unref (set_result);
1244 empathy_account_settings_set_display_name_async (
1245 EmpathyAccountSettings *settings,
1247 GAsyncReadyCallback callback,
1250 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1251 GSimpleAsyncResult *result;
1253 g_return_if_fail (name != NULL);
1255 result = g_simple_async_result_new (G_OBJECT (settings),
1256 callback, user_data, empathy_account_settings_set_display_name_finish);
1258 if (!tp_strdiff (name, priv->display_name))
1261 g_simple_async_result_complete_in_idle (result);
1265 if (priv->account == NULL)
1267 if (priv->display_name != NULL)
1268 g_free (priv->display_name);
1270 priv->display_name = g_strdup (name);
1272 g_simple_async_result_complete_in_idle (result);
1277 tp_account_set_display_name_async (priv->account, name,
1278 account_settings_display_name_set_cb, result);
1282 empathy_account_settings_set_display_name_finish (
1283 EmpathyAccountSettings *settings,
1284 GAsyncResult *result,
1287 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1291 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1292 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1299 account_settings_icon_name_set_cb (GObject *src,
1303 GError *error = NULL;
1304 TpAccount *account = TP_ACCOUNT (src);
1305 GSimpleAsyncResult *set_result = user_data;
1307 tp_account_set_icon_name_finish (account, res, &error);
1311 g_simple_async_result_set_from_error (set_result, error);
1312 g_error_free (error);
1315 g_simple_async_result_complete (set_result);
1316 g_object_unref (set_result);
1320 empathy_account_settings_set_icon_name_async (
1321 EmpathyAccountSettings *settings,
1323 GAsyncReadyCallback callback,
1326 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1327 GSimpleAsyncResult *result;
1329 g_return_if_fail (name != NULL);
1331 result = g_simple_async_result_new (G_OBJECT (settings),
1332 callback, user_data, empathy_account_settings_set_icon_name_finish);
1334 if (priv->account == NULL)
1336 if (priv->icon_name != NULL)
1337 g_free (priv->icon_name);
1339 priv->icon_name = g_strdup (name);
1341 g_simple_async_result_complete_in_idle (result);
1346 tp_account_set_icon_name_async (priv->account, name,
1347 account_settings_icon_name_set_cb, result);
1351 empathy_account_settings_set_icon_name_finish (
1352 EmpathyAccountSettings *settings,
1353 GAsyncResult *result,
1356 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1360 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1361 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1368 empathy_account_settings_processed_password (GObject *source,
1369 GAsyncResult *result,
1371 gpointer finish_func)
1373 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1374 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1375 GSimpleAsyncResult *r;
1376 GError *error = NULL;
1377 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1380 g_free (priv->password_original);
1381 priv->password_original = g_strdup (priv->password);
1383 if (!func (TP_ACCOUNT (source), result, &error))
1385 g_simple_async_result_set_from_error (priv->apply_result, error);
1386 g_error_free (error);
1389 empathy_account_settings_discard_changes (settings);
1391 r = priv->apply_result;
1392 priv->apply_result = NULL;
1394 g_simple_async_result_complete (r);
1399 empathy_account_settings_set_password_cb (GObject *source,
1400 GAsyncResult *result,
1403 empathy_account_settings_processed_password (source, result, user_data,
1404 empathy_keyring_set_account_password_finish);
1408 empathy_account_settings_delete_password_cb (GObject *source,
1409 GAsyncResult *result,
1412 empathy_account_settings_processed_password (source, result, user_data,
1413 empathy_keyring_delete_account_password_finish);
1417 empathy_account_settings_account_updated (GObject *source,
1418 GAsyncResult *result,
1421 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1422 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1423 GSimpleAsyncResult *r;
1424 GError *error = NULL;
1425 GStrv reconnect_required = NULL;
1427 if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1428 result, &reconnect_required, &error))
1430 g_simple_async_result_set_from_error (priv->apply_result, error);
1431 g_error_free (error);
1435 /* Only set the password in the keyring if the CM supports SASL and
1437 if (priv->supports_sasl && priv->password_changed)
1439 if (priv->password != NULL)
1441 /* FIXME: we shouldn't save the password if we
1442 * can't (MaySaveResponse=False) but we don't have API to check that
1443 * at this point (fdo #35382). */
1444 empathy_keyring_set_account_password_async (priv->account, priv->password,
1445 empathy_account_settings_set_password_cb, settings);
1449 empathy_keyring_delete_account_password_async (priv->account,
1450 empathy_account_settings_delete_password_cb, settings);
1456 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1457 g_strv_length (reconnect_required) > 0);
1460 empathy_account_settings_discard_changes (settings);
1462 r = priv->apply_result;
1463 priv->apply_result = NULL;
1465 g_simple_async_result_complete (r);
1467 g_strfreev (reconnect_required);
1471 empathy_account_settings_created_cb (GObject *source,
1472 GAsyncResult *result,
1475 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1476 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1478 GError *error = NULL;
1479 GSimpleAsyncResult *r;
1481 account = tp_account_manager_create_account_finish (
1482 TP_ACCOUNT_MANAGER (source), result, &error);
1484 if (account == NULL)
1486 g_simple_async_result_set_from_error (priv->apply_result, error);
1490 priv->account = g_object_ref (account);
1492 if (priv->supports_sasl && priv->password != NULL)
1494 /* Save the password before connecting */
1495 /* FIXME: we shouldn't save the password if we
1496 * can't (MaySaveResponse=False) but we don't have API to check that
1497 * at this point (fdo #35382). */
1498 empathy_keyring_set_account_password_async (priv->account,
1499 priv->password, empathy_account_settings_set_password_cb,
1504 empathy_account_settings_discard_changes (settings);
1507 r = priv->apply_result;
1508 priv->apply_result = NULL;
1510 g_simple_async_result_complete (r);
1516 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1518 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1519 GHashTable *properties;
1520 TpConnectionPresenceType type;
1523 EmpathyPresenceManager *presence_mgr;
1525 properties = tp_asv_new (NULL, NULL);
1527 presence_mgr = empathy_presence_manager_dup_singleton ();
1528 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1530 g_object_unref (presence_mgr);
1532 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1534 /* Create the account with the requested presence the same as the current
1535 * global requested presence, but don't enable it */
1536 GValueArray *presence;
1537 GValue vtype = { 0, };
1538 GValue vstatus = { 0, };
1539 GValue vmessage = { 0, };
1541 presence = g_value_array_new (3);
1543 g_value_init (&vtype, G_TYPE_UINT);
1544 g_value_set_uint (&vtype, type);
1545 g_value_array_append (presence, &vtype);
1547 g_value_init (&vstatus, G_TYPE_STRING);
1548 g_value_take_string (&vstatus, status);
1549 g_value_array_append (presence, &vstatus);
1551 g_value_init (&vmessage, G_TYPE_STRING);
1552 g_value_take_string (&vmessage, message);
1553 g_value_array_append (presence, &vmessage);
1555 tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1556 TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1559 tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1562 if (priv->service != NULL)
1563 tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1565 tp_account_manager_create_account_async (priv->account_manager,
1566 priv->cm_name, priv->protocol, priv->display_name,
1567 priv->parameters, properties,
1568 empathy_account_settings_created_cb,
1571 g_hash_table_unref (properties);
1575 empathy_account_settings_manager_ready_cb (GObject *source_object,
1576 GAsyncResult *result,
1579 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1580 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1581 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1582 GError *error = NULL;
1584 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1586 DEBUG ("Failed to prepare account manager: %s", error->message);
1587 g_error_free (error);
1591 g_assert (priv->apply_result != NULL && priv->account == NULL);
1592 empathy_account_settings_do_create_account (settings);
1596 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1597 GAsyncReadyCallback callback,
1600 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1602 if (priv->apply_result != NULL)
1604 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1605 callback, user_data,
1606 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1610 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1611 callback, user_data, empathy_account_settings_apply_finish);
1613 /* We'll have to reconnect only if we change none DBus_Property on an
1614 * existing account. */
1615 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1617 if (priv->account == NULL)
1619 tp_account_manager_prepare_async (priv->account_manager, NULL,
1620 empathy_account_settings_manager_ready_cb, settings);
1624 tp_account_update_parameters_async (priv->account,
1625 priv->parameters, (const gchar **)priv->unset_parameters->data,
1626 empathy_account_settings_account_updated, settings);
1631 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1632 GAsyncResult *result,
1633 gboolean *reconnect_required,
1636 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1640 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1641 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1643 if (reconnect_required != NULL)
1644 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1645 G_SIMPLE_ASYNC_RESULT (result));
1651 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1654 EmpathyAccountSettingsPriv *priv;
1655 const gchar *account_path;
1656 const gchar *priv_account_path;
1658 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1659 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1661 priv = GET_PRIV (settings);
1663 if (priv->account == NULL)
1666 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1667 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1669 return (!tp_strdiff (account_path, priv_account_path));
1673 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1675 const gchar *pattern)
1677 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1679 GError *error = NULL;
1681 regex = g_regex_new (pattern, 0, 0, &error);
1684 g_warning ("Failed to create reg exp: %s", error->message);
1685 g_error_free (error);
1689 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1693 empathy_account_settings_parameter_is_valid (
1694 EmpathyAccountSettings *settings,
1697 EmpathyAccountSettingsPriv *priv;
1698 const GRegex *regex;
1701 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1703 priv = GET_PRIV (settings);
1705 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1707 /* first, look if it's set in our own parameters */
1708 if (tp_asv_lookup (priv->parameters, param))
1711 /* if we did not unset the parameter, look if it's in the account */
1712 if (priv->account != NULL &&
1713 !empathy_account_settings_is_unset (settings, param))
1715 const GHashTable *account_params;
1717 account_params = tp_account_get_parameters (priv->account);
1718 if (tp_asv_lookup (account_params, param))
1726 /* test whether parameter value matches its regex */
1727 regex = g_hash_table_lookup (priv->param_regexps, param);
1730 value = empathy_account_settings_get_string (settings, param);
1731 if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1739 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1741 EmpathyAccountSettingsPriv *priv;
1743 GHashTableIter iter;
1746 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1748 priv = GET_PRIV (settings);
1750 for (l = priv->required_params; l; l = l->next)
1752 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1756 g_hash_table_iter_init (&iter, priv->param_regexps);
1757 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1759 if (!empathy_account_settings_parameter_is_valid (settings, param))
1766 const TpConnectionManagerProtocol *
1767 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1769 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1771 return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1775 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1777 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1779 return priv->supports_sasl;