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-idle.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_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_try_migrating_password (EmpathyAccountSettings *self)
461 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
463 const gchar *account_id, *password;
464 const gchar *unset[] = { "password", NULL };
467 if (!priv->supports_sasl || empathy_account_settings_get (
468 self, "password") == NULL)
471 /* mission-control still has our password, although the CM
472 * supports SASL. Let's try migrating it. */
474 account_id = tp_proxy_get_object_path (priv->account)
475 + strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
477 DEBUG ("Trying to migrate password parameter from MC to the "
478 "keyring ourselves for account %s", account_id);
480 v = empathy_account_settings_get (self, "password");
482 /* I can't imagine why this would fail. */
483 if (!G_VALUE_HOLDS_STRING (v))
486 password = g_value_get_string (v);
488 if (EMP_STR_EMPTY (password))
491 empathy_keyring_set_password_async (priv->account, password,
494 /* We don't want to request the password again, we
495 * already know it. */
496 priv->password_requested = TRUE;
498 priv->password = g_strdup (password);
499 priv->password_original = g_strdup (password);
501 /* Now clear the password MC has stored. */
502 empty = tp_asv_new (NULL, NULL);
503 tp_account_update_parameters_async (priv->account,
504 empty, unset, NULL, NULL);
506 g_hash_table_remove (priv->parameters, "password");
508 g_hash_table_unref (empty);
512 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
514 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
515 const TpConnectionManagerProtocol *tp_protocol;
516 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
521 if (priv->account != NULL
522 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
525 if (!empathy_connection_managers_is_ready (priv->managers))
528 if (priv->manager == NULL)
530 priv->manager = empathy_connection_managers_get_cm (
531 priv->managers, priv->cm_name);
534 if (priv->manager == NULL)
537 g_object_ref (priv->manager);
539 if (priv->account != NULL)
541 g_free (priv->display_name);
543 g_strdup (tp_account_get_display_name (priv->account));
545 g_free (priv->icon_name);
547 g_strdup (tp_account_get_icon_name (priv->account));
550 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
553 if (tp_protocol == NULL)
555 priv->manager = NULL;
559 if (priv->required_params == NULL)
561 TpConnectionManagerParam *cur;
563 for (cur = tp_protocol->params; cur->name != NULL; cur++)
565 if (tp_connection_manager_param_is_required (cur))
567 priv->required_params = g_list_append (priv->required_params,
568 g_strdup (cur->name));
573 if (priv->protocol_obj == NULL)
575 priv->protocol_obj = g_object_ref (
576 tp_connection_manager_get_protocol_object (priv->manager,
580 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
581 && !priv->preparing_protocol)
583 priv->preparing_protocol = TRUE;
584 tp_proxy_prepare_async (priv->protocol_obj, features,
585 empathy_account_settings_protocol_obj_prepared_cb, self);
590 if (tp_strv_contains (tp_protocol_get_authentication_types (
592 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
594 priv->supports_sasl = TRUE;
599 empathy_account_settings_try_migrating_password (self);
601 /* priv->account won't be a proper account if it's the account
602 * assistant showing this widget. */
603 if (priv->supports_sasl && !priv->password_requested
604 && priv->account != NULL)
606 priv->password_requested = TRUE;
608 /* Make this call but don't block on its readiness. We'll signal
609 * if it's updated later with ::password-retrieved. */
610 empathy_keyring_get_password_async (priv->account,
611 empathy_account_settings_get_password_cb, self);
615 g_object_notify (G_OBJECT (self), "ready");
619 empathy_account_settings_account_ready_cb (GObject *source_object,
620 GAsyncResult *result,
623 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
624 TpAccount *account = TP_ACCOUNT (source_object);
625 GError *error = NULL;
627 if (!tp_proxy_prepare_finish (account, result, &error))
629 DEBUG ("Failed to prepare account: %s", error->message);
630 g_error_free (error);
634 empathy_account_settings_check_readyness (settings);
638 empathy_account_settings_managers_ready_cb (GObject *object,
642 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
644 empathy_account_settings_check_readyness (settings);
647 EmpathyAccountSettings *
648 empathy_account_settings_new (const gchar *connection_manager,
649 const gchar *protocol,
650 const gchar *service,
651 const char *display_name)
653 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
654 "connection-manager", connection_manager,
655 "protocol", protocol,
657 "display-name", display_name,
661 EmpathyAccountSettings *
662 empathy_account_settings_new_for_account (TpAccount *account)
664 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
669 TpConnectionManagerParam *
670 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
672 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
673 const TpConnectionManagerProtocol *tp_protocol;
675 g_return_val_if_fail (priv->manager != NULL, NULL);
676 g_return_val_if_fail (priv->protocol != NULL, NULL);
678 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
680 if (tp_protocol == NULL)
682 DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
687 return tp_protocol->params;
691 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
693 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
699 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
701 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
703 return priv->cm_name;
707 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
709 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
711 return priv->protocol;
715 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
717 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
719 return priv->service;
723 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
725 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
727 return priv->icon_name;
731 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
733 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
735 return priv->display_name;
739 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
741 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
743 return priv->account;
747 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
750 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
754 a = priv->unset_parameters;
756 for (i = 0; i < a->len; i++)
758 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
765 static TpConnectionManagerParam *
766 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
769 TpConnectionManagerParam *tp_params =
770 empathy_account_settings_get_tp_params (settings);
771 TpConnectionManagerParam *p;
773 for (p = tp_params; p != NULL && p->name != NULL; p++)
775 if (tp_strdiff (p->name, param))
785 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
788 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
792 for (idx = 0; idx < priv->unset_parameters->len; idx++)
794 val = g_array_index (priv->unset_parameters, gchar *, idx);
796 if (!tp_strdiff (val, param))
798 priv->unset_parameters =
799 g_array_remove_index (priv->unset_parameters, idx);
808 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
811 TpConnectionManagerParam *p;
813 p = empathy_account_settings_get_tp_param (settings, param);
815 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
818 return &(p->default_value);
822 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
825 TpConnectionManagerParam *p;
827 p = empathy_account_settings_get_tp_param (settings, param);
832 return p->dbus_signature;
836 empathy_account_settings_get (EmpathyAccountSettings *settings,
839 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
840 const GValue *result = NULL;
842 /* Lookup the update parameters we set */
843 result = tp_asv_lookup (priv->parameters, param);
847 /* If the parameters isn't unset use the accounts setting if any */
848 if (priv->account != NULL
849 && !empathy_account_settings_is_unset (settings, param))
851 const GHashTable *parameters;
853 parameters = tp_account_get_parameters (priv->account);
854 result = tp_asv_lookup (parameters, param);
860 /* fallback to the default */
861 return empathy_account_settings_get_default (settings, param);
865 empathy_account_settings_unset (EmpathyAccountSettings *settings,
868 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
870 if (empathy_account_settings_is_unset (settings, param))
873 if (priv->supports_sasl && !tp_strdiff (param, "password"))
875 g_free (priv->password);
876 priv->password = NULL;
877 priv->password_changed = TRUE;
881 v = g_strdup (param);
883 g_array_append_val (priv->unset_parameters, v);
884 g_hash_table_remove (priv->parameters, param);
888 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
890 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
892 g_hash_table_remove_all (priv->parameters);
893 empathy_account_settings_free_unset_parameters (settings);
895 priv->password_changed = FALSE;
896 g_free (priv->password);
897 priv->password = g_strdup (priv->password_original);
901 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
904 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
907 if (!tp_strdiff (param, "password") && priv->supports_sasl)
909 return priv->password;
912 v = empathy_account_settings_get (settings, param);
914 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
917 return g_value_get_string (v);
920 const gchar * const *
921 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
926 v = empathy_account_settings_get (settings, param);
928 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
931 return g_value_get_boxed (v);
935 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
941 v = empathy_account_settings_get (settings, param);
946 switch G_VALUE_TYPE (v)
949 ret = g_value_get_uchar (v);
952 ret = g_value_get_int (v);
955 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
959 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
962 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
974 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
980 v = empathy_account_settings_get (settings, param);
984 switch G_VALUE_TYPE (v)
987 ret = g_value_get_uchar (v);
990 ret = g_value_get_int (v);
993 ret = g_value_get_uint (v);
996 ret = g_value_get_int64 (v);
999 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
1010 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
1016 v = empathy_account_settings_get (settings, param);
1020 switch G_VALUE_TYPE (v)
1023 ret = g_value_get_uchar (v);
1026 ret = MAX (0, g_value_get_int (v));
1029 ret = g_value_get_uint (v);
1032 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
1035 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
1046 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1052 v = empathy_account_settings_get (settings, param);
1054 if (v == NULL || !G_VALUE_HOLDS_INT (v))
1057 switch G_VALUE_TYPE (v)
1060 ret = g_value_get_uchar (v);
1063 ret = MAX (0, g_value_get_int (v));
1066 ret = g_value_get_uint (v);
1069 ret = MAX (0, g_value_get_int64 (v));
1072 ret = g_value_get_uint64 (v);
1083 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1088 v = empathy_account_settings_get (settings, param);
1090 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1093 return g_value_get_boolean (v);
1097 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1101 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1103 g_return_if_fail (param != NULL);
1104 g_return_if_fail (value != NULL);
1106 if (!tp_strdiff (param, "password") && priv->supports_sasl)
1108 g_free (priv->password);
1109 priv->password = g_strdup (value);
1110 priv->password_changed = TRUE;
1114 tp_asv_set_string (priv->parameters, g_strdup (param), value);
1117 account_settings_remove_from_unset (settings, param);
1121 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1125 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1127 g_return_if_fail (param != NULL);
1128 g_return_if_fail (value != NULL);
1130 tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1132 account_settings_remove_from_unset (settings, param);
1136 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1140 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1142 g_return_if_fail (param != NULL);
1144 tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1146 account_settings_remove_from_unset (settings, param);
1150 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1154 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1156 g_return_if_fail (param != NULL);
1158 tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1160 account_settings_remove_from_unset (settings, param);
1164 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1168 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1170 g_return_if_fail (param != NULL);
1172 tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1174 account_settings_remove_from_unset (settings, param);
1178 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1182 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1184 g_return_if_fail (param != NULL);
1186 tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1188 account_settings_remove_from_unset (settings, param);
1192 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1196 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1198 g_return_if_fail (param != NULL);
1200 tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1202 account_settings_remove_from_unset (settings, param);
1206 account_settings_display_name_set_cb (GObject *src,
1210 GError *error = NULL;
1211 TpAccount *account = TP_ACCOUNT (src);
1212 GSimpleAsyncResult *set_result = user_data;
1214 tp_account_set_display_name_finish (account, res, &error);
1218 g_simple_async_result_set_from_error (set_result, error);
1219 g_error_free (error);
1222 g_simple_async_result_complete (set_result);
1223 g_object_unref (set_result);
1227 empathy_account_settings_set_display_name_async (
1228 EmpathyAccountSettings *settings,
1230 GAsyncReadyCallback callback,
1233 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1234 GSimpleAsyncResult *result;
1236 g_return_if_fail (name != NULL);
1238 result = g_simple_async_result_new (G_OBJECT (settings),
1239 callback, user_data, empathy_account_settings_set_display_name_finish);
1241 if (!tp_strdiff (name, priv->display_name))
1244 g_simple_async_result_complete_in_idle (result);
1248 if (priv->account == NULL)
1250 if (priv->display_name != NULL)
1251 g_free (priv->display_name);
1253 priv->display_name = g_strdup (name);
1255 g_simple_async_result_complete_in_idle (result);
1260 tp_account_set_display_name_async (priv->account, name,
1261 account_settings_display_name_set_cb, result);
1265 empathy_account_settings_set_display_name_finish (
1266 EmpathyAccountSettings *settings,
1267 GAsyncResult *result,
1270 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1274 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1275 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1282 account_settings_icon_name_set_cb (GObject *src,
1286 GError *error = NULL;
1287 TpAccount *account = TP_ACCOUNT (src);
1288 GSimpleAsyncResult *set_result = user_data;
1290 tp_account_set_icon_name_finish (account, res, &error);
1294 g_simple_async_result_set_from_error (set_result, error);
1295 g_error_free (error);
1298 g_simple_async_result_complete (set_result);
1299 g_object_unref (set_result);
1303 empathy_account_settings_set_icon_name_async (
1304 EmpathyAccountSettings *settings,
1306 GAsyncReadyCallback callback,
1309 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1310 GSimpleAsyncResult *result;
1312 g_return_if_fail (name != NULL);
1314 result = g_simple_async_result_new (G_OBJECT (settings),
1315 callback, user_data, empathy_account_settings_set_icon_name_finish);
1317 if (priv->account == NULL)
1319 if (priv->icon_name != NULL)
1320 g_free (priv->icon_name);
1322 priv->icon_name = g_strdup (name);
1324 g_simple_async_result_complete_in_idle (result);
1329 tp_account_set_icon_name_async (priv->account, name,
1330 account_settings_icon_name_set_cb, result);
1334 empathy_account_settings_set_icon_name_finish (
1335 EmpathyAccountSettings *settings,
1336 GAsyncResult *result,
1339 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1343 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1344 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1351 empathy_account_settings_processed_password (GObject *source,
1352 GAsyncResult *result,
1354 gpointer finish_func)
1356 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1357 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1358 GSimpleAsyncResult *r;
1359 GError *error = NULL;
1360 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1363 g_free (priv->password_original);
1364 priv->password_original = g_strdup (priv->password);
1366 if (!func (TP_ACCOUNT (source), result, &error))
1368 g_simple_async_result_set_from_error (priv->apply_result, error);
1369 g_error_free (error);
1372 empathy_account_settings_discard_changes (settings);
1374 r = priv->apply_result;
1375 priv->apply_result = NULL;
1377 g_simple_async_result_complete (r);
1382 empathy_account_settings_set_password_cb (GObject *source,
1383 GAsyncResult *result,
1386 empathy_account_settings_processed_password (source, result, user_data,
1387 empathy_keyring_set_password_finish);
1391 empathy_account_settings_delete_password_cb (GObject *source,
1392 GAsyncResult *result,
1395 empathy_account_settings_processed_password (source, result, user_data,
1396 empathy_keyring_delete_password_finish);
1400 empathy_account_settings_account_updated (GObject *source,
1401 GAsyncResult *result,
1404 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1405 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1406 GSimpleAsyncResult *r;
1407 GError *error = NULL;
1409 if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1410 result, NULL, &error))
1412 g_simple_async_result_set_from_error (priv->apply_result, error);
1413 g_error_free (error);
1417 /* Only set the password in the keyring if the CM supports SASL and
1419 if (priv->supports_sasl && priv->password_changed)
1421 if (priv->password != NULL)
1423 empathy_keyring_set_password_async (priv->account, priv->password,
1424 empathy_account_settings_set_password_cb, settings);
1428 empathy_keyring_delete_password_async (priv->account,
1429 empathy_account_settings_delete_password_cb, settings);
1436 empathy_account_settings_discard_changes (settings);
1438 r = priv->apply_result;
1439 priv->apply_result = NULL;
1441 g_simple_async_result_complete (r);
1446 empathy_account_settings_created_cb (GObject *source,
1447 GAsyncResult *result,
1450 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1451 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1453 GError *error = NULL;
1454 GSimpleAsyncResult *r;
1456 account = tp_account_manager_create_account_finish (
1457 TP_ACCOUNT_MANAGER (source), result, &error);
1459 if (account == NULL)
1461 g_simple_async_result_set_from_error (priv->apply_result, error);
1465 priv->account = g_object_ref (account);
1466 empathy_account_settings_discard_changes (settings);
1469 r = priv->apply_result;
1470 priv->apply_result = NULL;
1472 g_simple_async_result_complete (r);
1478 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1480 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1481 GHashTable *properties;
1482 TpConnectionPresenceType type;
1487 properties = tp_asv_new (NULL, NULL);
1489 idle = empathy_idle_dup_singleton ();
1490 type = empathy_idle_get_requested_presence (idle, &status, &message);
1491 g_object_unref (idle);
1493 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1495 /* Create the account with the requested presence the same as the current
1496 * global requested presence, but don't enable it */
1497 GValueArray *presence;
1498 GValue vtype = { 0, };
1499 GValue vstatus = { 0, };
1500 GValue vmessage = { 0, };
1502 presence = g_value_array_new (3);
1504 g_value_init (&vtype, G_TYPE_UINT);
1505 g_value_set_uint (&vtype, type);
1506 g_value_array_append (presence, &vtype);
1508 g_value_init (&vstatus, G_TYPE_STRING);
1509 g_value_take_string (&vstatus, status);
1510 g_value_array_append (presence, &vstatus);
1512 g_value_init (&vmessage, G_TYPE_STRING);
1513 g_value_take_string (&vmessage, message);
1514 g_value_array_append (presence, &vmessage);
1516 tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1517 TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1520 tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1523 if (priv->service != NULL)
1524 tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1526 tp_account_manager_create_account_async (priv->account_manager,
1527 priv->cm_name, priv->protocol, priv->display_name,
1528 priv->parameters, properties,
1529 empathy_account_settings_created_cb,
1532 g_hash_table_unref (properties);
1536 empathy_account_settings_manager_ready_cb (GObject *source_object,
1537 GAsyncResult *result,
1540 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1541 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1542 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1543 GError *error = NULL;
1545 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1547 DEBUG ("Failed to prepare account manager: %s", error->message);
1548 g_error_free (error);
1552 g_assert (priv->apply_result != NULL && priv->account == NULL);
1553 empathy_account_settings_do_create_account (settings);
1557 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1558 GAsyncReadyCallback callback,
1561 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1563 if (priv->apply_result != NULL)
1565 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1566 callback, user_data,
1567 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1571 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1572 callback, user_data, empathy_account_settings_apply_finish);
1574 if (priv->account == NULL)
1576 tp_account_manager_prepare_async (priv->account_manager, NULL,
1577 empathy_account_settings_manager_ready_cb, settings);
1581 tp_account_update_parameters_async (priv->account,
1582 priv->parameters, (const gchar **)priv->unset_parameters->data,
1583 empathy_account_settings_account_updated, settings);
1588 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1589 GAsyncResult *result,
1592 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1596 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1597 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1603 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1606 EmpathyAccountSettingsPriv *priv;
1607 const gchar *account_path;
1608 const gchar *priv_account_path;
1610 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1611 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1613 priv = GET_PRIV (settings);
1615 if (priv->account == NULL)
1618 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1619 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1621 return (!tp_strdiff (account_path, priv_account_path));
1625 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1627 const gchar *pattern)
1629 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1632 regex = g_regex_new (pattern, 0, 0, NULL);
1633 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1637 empathy_account_settings_parameter_is_valid (
1638 EmpathyAccountSettings *settings,
1641 EmpathyAccountSettingsPriv *priv;
1642 const GRegex *regex;
1645 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1647 priv = GET_PRIV (settings);
1649 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1651 /* first, look if it's set in our own parameters */
1652 if (tp_asv_lookup (priv->parameters, param))
1655 /* if we did not unset the parameter, look if it's in the account */
1656 if (priv->account != NULL &&
1657 !empathy_account_settings_is_unset (settings, param))
1659 const GHashTable *account_params;
1661 account_params = tp_account_get_parameters (priv->account);
1662 if (tp_asv_lookup (account_params, param))
1670 /* test whether parameter value matches its regex */
1671 regex = g_hash_table_lookup (priv->param_regexps, param);
1674 value = empathy_account_settings_get_string (settings, param);
1675 if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1683 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1685 EmpathyAccountSettingsPriv *priv;
1687 GHashTableIter iter;
1690 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1692 priv = GET_PRIV (settings);
1694 for (l = priv->required_params; l; l = l->next)
1696 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1700 g_hash_table_iter_init (&iter, priv->param_regexps);
1701 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1703 if (!empathy_account_settings_parameter_is_valid (settings, param))
1710 const TpConnectionManagerProtocol *
1711 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1713 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1715 return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
1719 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1721 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1723 return priv->supports_sasl;