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,
53 /* private structure */
54 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
56 struct _EmpathyAccountSettingsPriv
58 gboolean dispose_has_run;
59 EmpathyConnectionManagers *managers;
60 TpAccountManager *account_manager;
62 TpConnectionManager *manager;
63 TpProtocol *protocol_obj;
71 gboolean display_name_overridden;
74 gboolean supports_sasl;
75 gboolean password_changed;
78 gchar *password_original;
80 gboolean password_retrieved;
81 gboolean password_requested;
83 /* Parameter name (gchar *) -> parameter value (GValue) */
84 GHashTable *parameters;
85 /* Keys are parameter names from the hash above (gchar *).
86 * Values are regular expresions that should match corresponding parameter
87 * values (GRegex *). Possible regexp patterns are defined in
88 * empathy-account-widget.c */
89 GHashTable *param_regexps;
90 GArray *unset_parameters;
91 GList *required_params;
93 gulong managers_ready_id;
94 gboolean preparing_protocol;
96 GSimpleAsyncResult *apply_result;
100 empathy_account_settings_init (EmpathyAccountSettings *obj)
102 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
103 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
107 /* allocate any data required by the object here */
108 priv->managers = empathy_connection_managers_dup_singleton ();
109 priv->account_manager = tp_account_manager_dup ();
111 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
112 g_free, (GDestroyNotify) tp_g_value_slice_free);
114 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
115 g_free, (GDestroyNotify) g_regex_unref);
117 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
119 priv->required_params = NULL;
122 static void empathy_account_settings_dispose (GObject *object);
123 static void empathy_account_settings_finalize (GObject *object);
124 static void empathy_account_settings_account_ready_cb (GObject *source_object,
125 GAsyncResult *result, gpointer user_data);
126 static void empathy_account_settings_managers_ready_cb (GObject *obj,
127 GParamSpec *pspec, gpointer user_data);
128 static void empathy_account_settings_check_readyness (
129 EmpathyAccountSettings *self);
132 empathy_account_settings_set_property (GObject *object,
137 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
138 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
143 priv->account = g_value_dup_object (value);
146 priv->cm_name = g_value_dup_string (value);
149 priv->protocol = g_value_dup_string (value);
152 priv->service = g_value_dup_string (value);
154 case PROP_DISPLAY_NAME:
155 priv->display_name = g_value_dup_string (value);
157 case PROP_DISPLAY_NAME_OVERRIDDEN:
158 priv->display_name_overridden = g_value_get_boolean (value);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 empathy_account_settings_get_property (GObject *object,
172 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
173 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
178 g_value_set_object (value, priv->account);
181 g_value_set_string (value, priv->cm_name);
184 g_value_set_string (value, priv->protocol);
187 g_value_set_string (value, priv->service);
189 case PROP_DISPLAY_NAME:
190 g_value_set_string (value, priv->display_name);
192 case PROP_DISPLAY_NAME_OVERRIDDEN:
193 g_value_set_boolean (value, priv->display_name_overridden);
196 g_value_set_boolean (value, priv->ready);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 empathy_account_settings_constructed (GObject *object)
207 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
208 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
210 if (priv->account != NULL)
212 g_free (priv->cm_name);
213 g_free (priv->protocol);
214 g_free (priv->service);
217 g_strdup (tp_account_get_connection_manager (priv->account));
219 g_strdup (tp_account_get_protocol (priv->account));
221 g_strdup (tp_account_get_service (priv->account));
222 priv->icon_name = g_strdup
223 (tp_account_get_icon_name (priv->account));
227 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
230 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
232 empathy_account_settings_check_readyness (self);
236 GQuark features[] = {
237 TP_ACCOUNT_FEATURE_CORE,
238 TP_ACCOUNT_FEATURE_STORAGE,
241 if (priv->account != NULL)
243 tp_proxy_prepare_async (priv->account, features,
244 empathy_account_settings_account_ready_cb, self);
247 tp_g_signal_connect_object (priv->managers, "notify::ready",
248 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
252 empathy_account_settings_parent_class)->constructed != NULL)
254 empathy_account_settings_parent_class)->constructed (object);
259 empathy_account_settings_class_init (
260 EmpathyAccountSettingsClass *empathy_account_settings_class)
262 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
264 g_type_class_add_private (empathy_account_settings_class, sizeof
265 (EmpathyAccountSettingsPriv));
267 object_class->dispose = empathy_account_settings_dispose;
268 object_class->finalize = empathy_account_settings_finalize;
269 object_class->set_property = empathy_account_settings_set_property;
270 object_class->get_property = empathy_account_settings_get_property;
271 object_class->constructed = empathy_account_settings_constructed;
273 g_object_class_install_property (object_class, PROP_ACCOUNT,
274 g_param_spec_object ("account",
276 "The TpAccount backing these settings",
278 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
280 g_object_class_install_property (object_class, PROP_CM_NAME,
281 g_param_spec_string ("connection-manager",
282 "connection-manager",
283 "The name of the connection manager this account uses",
285 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
287 g_object_class_install_property (object_class, PROP_PROTOCOL,
288 g_param_spec_string ("protocol",
290 "The name of the protocol this account uses",
292 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
294 g_object_class_install_property (object_class, PROP_SERVICE,
295 g_param_spec_string ("service",
297 "The service of this account, or NULL",
299 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
301 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
302 g_param_spec_string ("display-name",
304 "The display name account these settings belong to",
306 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
308 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
309 g_param_spec_boolean ("display-name-overridden",
310 "display-name-overridden",
311 "Whether the display name for this account has been manually "
314 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
316 g_object_class_install_property (object_class, PROP_READY,
317 g_param_spec_boolean ("ready",
319 "Whether this account is ready to be used",
321 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
325 empathy_account_settings_dispose (GObject *object)
327 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
328 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
330 if (priv->dispose_has_run)
333 priv->dispose_has_run = TRUE;
335 if (priv->managers_ready_id != 0)
336 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
337 priv->managers_ready_id = 0;
339 if (priv->managers != NULL)
340 g_object_unref (priv->managers);
341 priv->managers = NULL;
343 if (priv->manager != NULL)
344 g_object_unref (priv->manager);
345 priv->manager = NULL;
347 if (priv->account_manager != NULL)
348 g_object_unref (priv->account_manager);
349 priv->account_manager = NULL;
351 if (priv->account != NULL)
352 g_object_unref (priv->account);
353 priv->account = NULL;
355 if (priv->protocol_obj != NULL)
356 g_object_unref (priv->protocol_obj);
357 priv->protocol_obj = NULL;
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 priv->password_retrieved = TRUE;
457 empathy_account_settings_check_readyness (self);
461 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
463 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
464 const TpConnectionManagerProtocol *tp_protocol;
465 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
470 if (priv->account != NULL
471 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
474 if (!empathy_connection_managers_is_ready (priv->managers))
477 if (priv->manager == NULL)
479 priv->manager = empathy_connection_managers_get_cm (
480 priv->managers, priv->cm_name);
483 if (priv->manager == NULL)
486 g_object_ref (priv->manager);
488 if (priv->account != NULL)
490 g_free (priv->display_name);
492 g_strdup (tp_account_get_display_name (priv->account));
494 g_free (priv->icon_name);
496 g_strdup (tp_account_get_icon_name (priv->account));
499 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
502 if (tp_protocol == NULL)
504 priv->manager = NULL;
508 if (priv->required_params == NULL)
510 TpConnectionManagerParam *cur;
512 for (cur = tp_protocol->params; cur->name != NULL; cur++)
514 if (tp_connection_manager_param_is_required (cur))
516 priv->required_params = g_list_append (priv->required_params,
517 g_strdup (cur->name));
522 if (priv->protocol_obj == NULL)
524 priv->protocol_obj = g_object_ref (
525 tp_connection_manager_get_protocol_object (priv->manager,
529 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
530 && !priv->preparing_protocol)
532 priv->preparing_protocol = TRUE;
533 tp_proxy_prepare_async (priv->protocol_obj, features,
534 empathy_account_settings_protocol_obj_prepared_cb, self);
539 if (tp_strv_contains (tp_protocol_get_authentication_types (
541 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
543 priv->supports_sasl = TRUE;
547 /* priv->account won't be a proper account if it's the account
548 * assistant showing this widget. */
549 if (priv->supports_sasl && !priv->password_retrieved
550 && !priv->password_requested && priv->account != NULL)
552 priv->password_requested = TRUE;
554 empathy_keyring_get_password_async (priv->account,
555 empathy_account_settings_get_password_cb, self);
560 g_object_notify (G_OBJECT (self), "ready");
564 empathy_account_settings_account_ready_cb (GObject *source_object,
565 GAsyncResult *result,
568 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
569 TpAccount *account = TP_ACCOUNT (source_object);
570 GError *error = NULL;
572 if (!tp_proxy_prepare_finish (account, result, &error))
574 DEBUG ("Failed to prepare account: %s", error->message);
575 g_error_free (error);
579 empathy_account_settings_check_readyness (settings);
583 empathy_account_settings_managers_ready_cb (GObject *object,
587 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
589 empathy_account_settings_check_readyness (settings);
592 EmpathyAccountSettings *
593 empathy_account_settings_new (const gchar *connection_manager,
594 const gchar *protocol,
595 const gchar *service,
596 const char *display_name)
598 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
599 "connection-manager", connection_manager,
600 "protocol", protocol,
602 "display-name", display_name,
606 EmpathyAccountSettings *
607 empathy_account_settings_new_for_account (TpAccount *account)
609 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
614 TpConnectionManagerParam *
615 empathy_account_settings_get_tp_params (EmpathyAccountSettings *settings)
617 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
618 const TpConnectionManagerProtocol *tp_protocol;
620 g_return_val_if_fail (priv->manager != NULL, NULL);
621 g_return_val_if_fail (priv->protocol != NULL, NULL);
623 tp_protocol = tp_connection_manager_get_protocol (priv->manager,
625 if (tp_protocol == NULL)
627 DEBUG ("Can't retrieve TpConnectionManagerProtocol for protocol '%s'",
632 return tp_protocol->params;
636 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
638 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
644 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
646 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
648 return priv->cm_name;
652 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
654 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
656 return priv->protocol;
660 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
662 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
664 return priv->service;
668 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
670 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
672 return priv->icon_name;
676 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
678 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
680 return priv->display_name;
684 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
686 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
688 return priv->account;
692 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
695 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
699 a = priv->unset_parameters;
701 for (i = 0; i < a->len; i++)
703 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
710 static TpConnectionManagerParam *
711 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
714 TpConnectionManagerParam *tp_params =
715 empathy_account_settings_get_tp_params (settings);
716 TpConnectionManagerParam *p;
718 for (p = tp_params; p != NULL && p->name != NULL; p++)
720 if (tp_strdiff (p->name, param))
730 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
733 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
737 for (idx = 0; idx < priv->unset_parameters->len; idx++)
739 val = g_array_index (priv->unset_parameters, gchar *, idx);
741 if (!tp_strdiff (val, param))
743 priv->unset_parameters =
744 g_array_remove_index (priv->unset_parameters, idx);
753 empathy_account_settings_get_default (EmpathyAccountSettings *settings,
756 TpConnectionManagerParam *p;
758 p = empathy_account_settings_get_tp_param (settings, param);
760 if (p == NULL || !(p->flags & TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT))
763 return &(p->default_value);
767 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
770 TpConnectionManagerParam *p;
772 p = empathy_account_settings_get_tp_param (settings, param);
777 return p->dbus_signature;
781 empathy_account_settings_get (EmpathyAccountSettings *settings,
784 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
785 const GValue *result = NULL;
787 /* Lookup the update parameters we set */
788 result = tp_asv_lookup (priv->parameters, param);
792 /* If the parameters isn't unset use the accounts setting if any */
793 if (priv->account != NULL
794 && !empathy_account_settings_is_unset (settings, param))
796 const GHashTable *parameters;
798 parameters = tp_account_get_parameters (priv->account);
799 result = tp_asv_lookup (parameters, param);
805 /* fallback to the default */
806 return empathy_account_settings_get_default (settings, param);
810 empathy_account_settings_unset (EmpathyAccountSettings *settings,
813 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
815 if (empathy_account_settings_is_unset (settings, param))
818 if (priv->supports_sasl && !tp_strdiff (param, "password"))
820 g_free (priv->password);
821 priv->password = NULL;
822 priv->password_changed = TRUE;
826 v = g_strdup (param);
828 g_array_append_val (priv->unset_parameters, v);
829 g_hash_table_remove (priv->parameters, param);
833 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
835 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
837 g_hash_table_remove_all (priv->parameters);
838 empathy_account_settings_free_unset_parameters (settings);
840 priv->password_changed = FALSE;
841 g_free (priv->password);
842 priv->password = g_strdup (priv->password_original);
846 empathy_account_settings_get_string (EmpathyAccountSettings *settings,
849 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
852 if (!tp_strdiff (param, "password") && priv->supports_sasl)
854 return priv->password;
857 v = empathy_account_settings_get (settings, param);
859 if (v == NULL || !G_VALUE_HOLDS_STRING (v))
862 return g_value_get_string (v);
865 const gchar * const *
866 empathy_account_settings_get_strv (EmpathyAccountSettings *settings,
871 v = empathy_account_settings_get (settings, param);
873 if (v == NULL || !G_VALUE_HOLDS (v, G_TYPE_STRV))
876 return g_value_get_boxed (v);
880 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
886 v = empathy_account_settings_get (settings, param);
891 switch G_VALUE_TYPE (v)
894 ret = g_value_get_uchar (v);
897 ret = g_value_get_int (v);
900 ret = CLAMP (g_value_get_uint (v), (guint) G_MININT32,
904 ret = CLAMP (g_value_get_int64 (v), G_MININT32, G_MAXINT32);
907 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT32,
919 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
925 v = empathy_account_settings_get (settings, param);
929 switch G_VALUE_TYPE (v)
932 ret = g_value_get_uchar (v);
935 ret = g_value_get_int (v);
938 ret = g_value_get_uint (v);
941 ret = g_value_get_int64 (v);
944 ret = CLAMP (g_value_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
955 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
961 v = empathy_account_settings_get (settings, param);
965 switch G_VALUE_TYPE (v)
968 ret = g_value_get_uchar (v);
971 ret = MAX (0, g_value_get_int (v));
974 ret = g_value_get_uint (v);
977 ret = CLAMP (g_value_get_int64 (v), 0, G_MAXUINT32);
980 ret = MIN (g_value_get_uint64 (v), G_MAXUINT32);
991 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
997 v = empathy_account_settings_get (settings, param);
999 if (v == NULL || !G_VALUE_HOLDS_INT (v))
1002 switch G_VALUE_TYPE (v)
1005 ret = g_value_get_uchar (v);
1008 ret = MAX (0, g_value_get_int (v));
1011 ret = g_value_get_uint (v);
1014 ret = MAX (0, g_value_get_int64 (v));
1017 ret = g_value_get_uint64 (v);
1028 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1033 v = empathy_account_settings_get (settings, param);
1035 if (v == NULL || !G_VALUE_HOLDS_BOOLEAN (v))
1038 return g_value_get_boolean (v);
1042 empathy_account_settings_set_string (EmpathyAccountSettings *settings,
1046 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1048 g_return_if_fail (param != NULL);
1049 g_return_if_fail (value != NULL);
1051 if (!tp_strdiff (param, "password") && priv->supports_sasl)
1053 g_free (priv->password);
1054 priv->password = g_strdup (value);
1055 priv->password_changed = TRUE;
1059 tp_asv_set_string (priv->parameters, g_strdup (param), value);
1062 account_settings_remove_from_unset (settings, param);
1066 empathy_account_settings_set_strv (EmpathyAccountSettings *settings,
1070 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1072 g_return_if_fail (param != NULL);
1073 g_return_if_fail (value != NULL);
1075 tp_asv_set_strv (priv->parameters, g_strdup (param), value);
1077 account_settings_remove_from_unset (settings, param);
1081 empathy_account_settings_set_int32 (EmpathyAccountSettings *settings,
1085 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1087 g_return_if_fail (param != NULL);
1089 tp_asv_set_int32 (priv->parameters, g_strdup (param), value);
1091 account_settings_remove_from_unset (settings, param);
1095 empathy_account_settings_set_int64 (EmpathyAccountSettings *settings,
1099 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1101 g_return_if_fail (param != NULL);
1103 tp_asv_set_int64 (priv->parameters, g_strdup (param), value);
1105 account_settings_remove_from_unset (settings, param);
1109 empathy_account_settings_set_uint32 (EmpathyAccountSettings *settings,
1113 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1115 g_return_if_fail (param != NULL);
1117 tp_asv_set_uint32 (priv->parameters, g_strdup (param), value);
1119 account_settings_remove_from_unset (settings, param);
1123 empathy_account_settings_set_uint64 (EmpathyAccountSettings *settings,
1127 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1129 g_return_if_fail (param != NULL);
1131 tp_asv_set_uint64 (priv->parameters, g_strdup (param), value);
1133 account_settings_remove_from_unset (settings, param);
1137 empathy_account_settings_set_boolean (EmpathyAccountSettings *settings,
1141 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1143 g_return_if_fail (param != NULL);
1145 tp_asv_set_boolean (priv->parameters, g_strdup (param), value);
1147 account_settings_remove_from_unset (settings, param);
1151 account_settings_display_name_set_cb (GObject *src,
1155 GError *error = NULL;
1156 TpAccount *account = TP_ACCOUNT (src);
1157 GSimpleAsyncResult *set_result = user_data;
1159 tp_account_set_display_name_finish (account, res, &error);
1163 g_simple_async_result_set_from_error (set_result, error);
1164 g_error_free (error);
1167 g_simple_async_result_complete (set_result);
1168 g_object_unref (set_result);
1172 empathy_account_settings_set_display_name_async (
1173 EmpathyAccountSettings *settings,
1175 GAsyncReadyCallback callback,
1178 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1179 GSimpleAsyncResult *result;
1181 g_return_if_fail (name != NULL);
1183 result = g_simple_async_result_new (G_OBJECT (settings),
1184 callback, user_data, empathy_account_settings_set_display_name_finish);
1186 if (!tp_strdiff (name, priv->display_name))
1189 g_simple_async_result_complete_in_idle (result);
1193 if (priv->account == NULL)
1195 if (priv->display_name != NULL)
1196 g_free (priv->display_name);
1198 priv->display_name = g_strdup (name);
1200 g_simple_async_result_complete_in_idle (result);
1205 tp_account_set_display_name_async (priv->account, name,
1206 account_settings_display_name_set_cb, result);
1210 empathy_account_settings_set_display_name_finish (
1211 EmpathyAccountSettings *settings,
1212 GAsyncResult *result,
1215 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1219 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1220 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1227 account_settings_icon_name_set_cb (GObject *src,
1231 GError *error = NULL;
1232 TpAccount *account = TP_ACCOUNT (src);
1233 GSimpleAsyncResult *set_result = user_data;
1235 tp_account_set_icon_name_finish (account, res, &error);
1239 g_simple_async_result_set_from_error (set_result, error);
1240 g_error_free (error);
1243 g_simple_async_result_complete (set_result);
1244 g_object_unref (set_result);
1248 empathy_account_settings_set_icon_name_async (
1249 EmpathyAccountSettings *settings,
1251 GAsyncReadyCallback callback,
1254 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1255 GSimpleAsyncResult *result;
1257 g_return_if_fail (name != NULL);
1259 result = g_simple_async_result_new (G_OBJECT (settings),
1260 callback, user_data, empathy_account_settings_set_icon_name_finish);
1262 if (priv->account == NULL)
1264 if (priv->icon_name != NULL)
1265 g_free (priv->icon_name);
1267 priv->icon_name = g_strdup (name);
1269 g_simple_async_result_complete_in_idle (result);
1274 tp_account_set_icon_name_async (priv->account, name,
1275 account_settings_icon_name_set_cb, result);
1279 empathy_account_settings_set_icon_name_finish (
1280 EmpathyAccountSettings *settings,
1281 GAsyncResult *result,
1284 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1288 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1289 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1296 empathy_account_settings_processed_password (GObject *source,
1297 GAsyncResult *result,
1299 gpointer finish_func)
1301 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1302 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1303 GSimpleAsyncResult *r;
1304 GError *error = NULL;
1305 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1308 g_free (priv->password_original);
1309 priv->password_original = g_strdup (priv->password);
1311 if (!func (TP_ACCOUNT (source), result, &error))
1313 g_simple_async_result_set_from_error (priv->apply_result, error);
1314 g_error_free (error);
1317 empathy_account_settings_discard_changes (settings);
1319 r = priv->apply_result;
1320 priv->apply_result = NULL;
1322 g_simple_async_result_complete (r);
1327 empathy_account_settings_set_password_cb (GObject *source,
1328 GAsyncResult *result,
1331 empathy_account_settings_processed_password (source, result, user_data,
1332 empathy_keyring_set_password_finish);
1336 empathy_account_settings_delete_password_cb (GObject *source,
1337 GAsyncResult *result,
1340 empathy_account_settings_processed_password (source, result, user_data,
1341 empathy_keyring_delete_password_finish);
1345 empathy_account_settings_account_updated (GObject *source,
1346 GAsyncResult *result,
1349 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1350 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1351 GSimpleAsyncResult *r;
1352 GError *error = NULL;
1354 if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
1355 result, NULL, &error))
1357 g_simple_async_result_set_from_error (priv->apply_result, error);
1358 g_error_free (error);
1362 /* Only set the password in the keyring if the CM supports SASL and
1364 if (priv->supports_sasl && priv->password_changed)
1366 if (priv->password != NULL)
1368 empathy_keyring_set_password_async (priv->account, priv->password,
1369 empathy_account_settings_set_password_cb, settings);
1373 empathy_keyring_delete_password_async (priv->account,
1374 empathy_account_settings_delete_password_cb, settings);
1381 empathy_account_settings_discard_changes (settings);
1383 r = priv->apply_result;
1384 priv->apply_result = NULL;
1386 g_simple_async_result_complete (r);
1391 empathy_account_settings_created_cb (GObject *source,
1392 GAsyncResult *result,
1395 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1396 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1398 GError *error = NULL;
1399 GSimpleAsyncResult *r;
1401 account = tp_account_manager_create_account_finish (
1402 TP_ACCOUNT_MANAGER (source), result, &error);
1404 if (account == NULL)
1406 g_simple_async_result_set_from_error (priv->apply_result, error);
1410 priv->account = g_object_ref (account);
1411 empathy_account_settings_discard_changes (settings);
1414 r = priv->apply_result;
1415 priv->apply_result = NULL;
1417 g_simple_async_result_complete (r);
1423 empathy_account_settings_do_create_account (EmpathyAccountSettings *settings)
1425 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1426 GHashTable *properties;
1427 TpConnectionPresenceType type;
1432 properties = tp_asv_new (NULL, NULL);
1434 idle = empathy_idle_dup_singleton ();
1435 type = empathy_idle_get_requested_presence (idle, &status, &message);
1436 g_object_unref (idle);
1438 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1440 /* Create the account with the requested presence the same as the current
1441 * global requested presence, but don't enable it */
1442 GValueArray *presence;
1443 GValue vtype = { 0, };
1444 GValue vstatus = { 0, };
1445 GValue vmessage = { 0, };
1447 presence = g_value_array_new (3);
1449 g_value_init (&vtype, G_TYPE_UINT);
1450 g_value_set_uint (&vtype, type);
1451 g_value_array_append (presence, &vtype);
1453 g_value_init (&vstatus, G_TYPE_STRING);
1454 g_value_take_string (&vstatus, status);
1455 g_value_array_append (presence, &vstatus);
1457 g_value_init (&vmessage, G_TYPE_STRING);
1458 g_value_take_string (&vmessage, message);
1459 g_value_array_append (presence, &vmessage);
1461 tp_asv_take_boxed (properties, TP_IFACE_ACCOUNT ".RequestedPresence",
1462 TP_STRUCT_TYPE_SIMPLE_PRESENCE, presence);
1465 tp_asv_set_string (properties, TP_IFACE_ACCOUNT ".Icon",
1468 if (priv->service != NULL)
1469 tp_asv_set_string (properties, TP_PROP_ACCOUNT_SERVICE, priv->service);
1471 tp_account_manager_create_account_async (priv->account_manager,
1472 priv->cm_name, priv->protocol, priv->display_name,
1473 priv->parameters, properties,
1474 empathy_account_settings_created_cb,
1477 g_hash_table_unref (properties);
1481 empathy_account_settings_manager_ready_cb (GObject *source_object,
1482 GAsyncResult *result,
1485 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1486 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1487 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1488 GError *error = NULL;
1490 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1492 DEBUG ("Failed to prepare account manager: %s", error->message);
1493 g_error_free (error);
1497 g_assert (priv->apply_result != NULL && priv->account == NULL);
1498 empathy_account_settings_do_create_account (settings);
1502 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1503 GAsyncReadyCallback callback,
1506 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1508 if (priv->apply_result != NULL)
1510 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1511 callback, user_data,
1512 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1516 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1517 callback, user_data, empathy_account_settings_apply_finish);
1519 if (priv->account == NULL)
1521 tp_account_manager_prepare_async (priv->account_manager, NULL,
1522 empathy_account_settings_manager_ready_cb, settings);
1526 tp_account_update_parameters_async (priv->account,
1527 priv->parameters, (const gchar **)priv->unset_parameters->data,
1528 empathy_account_settings_account_updated, settings);
1533 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1534 GAsyncResult *result,
1537 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1541 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1542 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1548 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1551 EmpathyAccountSettingsPriv *priv;
1552 const gchar *account_path;
1553 const gchar *priv_account_path;
1555 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1556 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1558 priv = GET_PRIV (settings);
1560 if (priv->account == NULL)
1563 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1564 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1566 return (!tp_strdiff (account_path, priv_account_path));
1570 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1572 const gchar *pattern)
1574 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1577 regex = g_regex_new (pattern, 0, 0, NULL);
1578 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1582 empathy_account_settings_parameter_is_valid (
1583 EmpathyAccountSettings *settings,
1586 EmpathyAccountSettingsPriv *priv;
1587 const GRegex *regex;
1590 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1592 priv = GET_PRIV (settings);
1594 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1596 /* first, look if it's set in our own parameters */
1597 if (tp_asv_lookup (priv->parameters, param))
1600 /* if we did not unset the parameter, look if it's in the account */
1601 if (priv->account != NULL &&
1602 !empathy_account_settings_is_unset (settings, param))
1604 const GHashTable *account_params;
1606 account_params = tp_account_get_parameters (priv->account);
1607 if (tp_asv_lookup (account_params, param))
1615 /* test whether parameter value matches its regex */
1616 regex = g_hash_table_lookup (priv->param_regexps, param);
1619 value = empathy_account_settings_get_string (settings, param);
1620 if (value != NULL && !g_regex_match (regex, value, 0, NULL))
1628 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1630 EmpathyAccountSettingsPriv *priv;
1632 GHashTableIter iter;
1635 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1637 priv = GET_PRIV (settings);
1639 for (l = priv->required_params; l; l = l->next)
1641 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1645 g_hash_table_iter_init (&iter, priv->param_regexps);
1646 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1648 if (!empathy_account_settings_parameter_is_valid (settings, param))
1655 const TpConnectionManagerProtocol *
1656 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1658 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1660 return tp_connection_manager_get_protocol (priv->manager, priv->protocol);