2 * empathy-account-settings.c - Source for EmpathyAccountSettings
3 * Copyright (C) 2009 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <telepathy-glib/account-manager.h>
27 #include <telepathy-glib/util.h>
28 #include <telepathy-glib/interfaces.h>
29 #include <telepathy-glib/gtypes.h>
31 #include "empathy-account-settings.h"
32 #include "empathy-connection-managers.h"
33 #include "empathy-keyring.h"
34 #include "empathy-utils.h"
35 #include "empathy-presence-manager.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
38 #include <libempathy/empathy-debug.h>
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountSettings)
42 G_DEFINE_TYPE(EmpathyAccountSettings, empathy_account_settings, G_TYPE_OBJECT)
50 PROP_DISPLAY_NAME_OVERRIDDEN,
55 PASSWORD_RETRIEVED = 1,
59 static gulong signals[LAST_SIGNAL] = { 0, };
61 /* private structure */
62 typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
64 struct _EmpathyAccountSettingsPriv
66 gboolean dispose_has_run;
67 EmpathyConnectionManagers *managers;
68 TpAccountManager *account_manager;
70 TpConnectionManager *manager;
71 TpProtocol *protocol_obj;
79 gchar *storage_provider;
80 gboolean display_name_overridden;
83 gboolean supports_sasl;
84 gboolean password_changed;
85 gboolean remember_password;
88 gchar *password_original;
90 gboolean password_retrieved;
91 gboolean password_requested;
93 /* Parameter name (gchar *) -> parameter value (GVariant) */
94 GHashTable *parameters;
95 /* Keys are parameter names from the hash above (gchar *).
96 * Values are regular expresions that should match corresponding parameter
97 * values (GRegex *). Possible regexp patterns are defined in
98 * empathy-account-widget.c */
99 GHashTable *param_regexps;
100 GArray *unset_parameters;
101 GList *required_params;
103 gulong managers_ready_id;
104 gboolean preparing_protocol;
106 /* If TRUE, the account should have 'tel' in its
107 * Account.Interface.Addressing.URISchemes property. */
108 gboolean uri_scheme_tel;
109 /* If TRUE, Service property needs to be updated when applying changes */
110 gboolean update_service;
112 GSimpleAsyncResult *apply_result;
116 empathy_account_settings_init (EmpathyAccountSettings *obj)
118 EmpathyAccountSettingsPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE ((obj),
119 EMPATHY_TYPE_ACCOUNT_SETTINGS, EmpathyAccountSettingsPriv);
123 /* allocate any data required by the object here */
124 priv->managers = empathy_connection_managers_dup_singleton ();
125 priv->account_manager = tp_account_manager_dup ();
127 priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
128 g_free, (GDestroyNotify) g_variant_unref);
130 priv->param_regexps = g_hash_table_new_full (g_str_hash, g_str_equal,
131 g_free, (GDestroyNotify) g_regex_unref);
133 priv->unset_parameters = g_array_new (TRUE, FALSE, sizeof (gchar *));
135 priv->required_params = NULL;
138 static void empathy_account_settings_dispose (GObject *object);
139 static void empathy_account_settings_finalize (GObject *object);
140 static void empathy_account_settings_account_ready_cb (GObject *source_object,
141 GAsyncResult *result, gpointer user_data);
142 static void empathy_account_settings_managers_ready_cb (GObject *obj,
143 GParamSpec *pspec, gpointer user_data);
144 static void empathy_account_settings_check_readyness (
145 EmpathyAccountSettings *self);
148 empathy_account_settings_set_property (GObject *object,
153 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
154 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
159 priv->account = g_value_dup_object (value);
162 priv->cm_name = g_value_dup_string (value);
165 priv->protocol = g_value_dup_string (value);
168 priv->service = g_value_dup_string (value);
170 case PROP_DISPLAY_NAME:
171 priv->display_name = g_value_dup_string (value);
173 case PROP_DISPLAY_NAME_OVERRIDDEN:
174 priv->display_name_overridden = g_value_get_boolean (value);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 empathy_account_settings_get_property (GObject *object,
188 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (object);
189 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
194 g_value_set_object (value, priv->account);
197 g_value_set_string (value, priv->cm_name);
200 g_value_set_string (value, priv->protocol);
203 g_value_set_string (value, priv->service);
205 case PROP_DISPLAY_NAME:
206 g_value_set_string (value, priv->display_name);
208 case PROP_DISPLAY_NAME_OVERRIDDEN:
209 g_value_set_boolean (value, priv->display_name_overridden);
212 g_value_set_boolean (value, priv->ready);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 empathy_account_settings_constructed (GObject *object)
223 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
224 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
226 if (priv->account != NULL)
228 g_free (priv->cm_name);
229 g_free (priv->protocol);
230 g_free (priv->service);
233 g_strdup (tp_account_get_cm_name (priv->account));
235 g_strdup (tp_account_get_protocol_name (priv->account));
237 g_strdup (tp_account_get_service (priv->account));
238 priv->icon_name = g_strdup
239 (tp_account_get_icon_name (priv->account));
243 priv->icon_name = empathy_protocol_icon_name (priv->protocol);
246 g_assert (priv->cm_name != NULL && priv->protocol != NULL);
248 empathy_account_settings_check_readyness (self);
252 GQuark features[] = {
253 TP_ACCOUNT_FEATURE_CORE,
254 TP_ACCOUNT_FEATURE_STORAGE,
255 TP_ACCOUNT_FEATURE_ADDRESSING,
258 if (priv->account != NULL)
260 tp_proxy_prepare_async (priv->account, features,
261 empathy_account_settings_account_ready_cb, self);
264 tp_g_signal_connect_object (priv->managers, "notify::ready",
265 G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
269 empathy_account_settings_parent_class)->constructed != NULL)
271 empathy_account_settings_parent_class)->constructed (object);
276 empathy_account_settings_class_init (
277 EmpathyAccountSettingsClass *empathy_account_settings_class)
279 GObjectClass *object_class = G_OBJECT_CLASS (empathy_account_settings_class);
281 g_type_class_add_private (empathy_account_settings_class, sizeof
282 (EmpathyAccountSettingsPriv));
284 object_class->dispose = empathy_account_settings_dispose;
285 object_class->finalize = empathy_account_settings_finalize;
286 object_class->set_property = empathy_account_settings_set_property;
287 object_class->get_property = empathy_account_settings_get_property;
288 object_class->constructed = empathy_account_settings_constructed;
290 g_object_class_install_property (object_class, PROP_ACCOUNT,
291 g_param_spec_object ("account",
293 "The TpAccount backing these settings",
295 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
297 g_object_class_install_property (object_class, PROP_CM_NAME,
298 g_param_spec_string ("connection-manager",
299 "connection-manager",
300 "The name of the connection manager this account uses",
302 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
304 g_object_class_install_property (object_class, PROP_PROTOCOL,
305 g_param_spec_string ("protocol",
307 "The name of the protocol this account uses",
309 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
311 g_object_class_install_property (object_class, PROP_SERVICE,
312 g_param_spec_string ("service",
314 "The service of this account, or NULL",
316 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
318 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
319 g_param_spec_string ("display-name",
321 "The display name account these settings belong to",
323 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
325 g_object_class_install_property (object_class, PROP_DISPLAY_NAME_OVERRIDDEN,
326 g_param_spec_boolean ("display-name-overridden",
327 "display-name-overridden",
328 "Whether the display name for this account has been manually "
331 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
333 g_object_class_install_property (object_class, PROP_READY,
334 g_param_spec_boolean ("ready",
336 "Whether this account is ready to be used",
338 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
340 signals[PASSWORD_RETRIEVED] =
341 g_signal_new ("password-retrieved",
342 G_TYPE_FROM_CLASS (empathy_account_settings_class),
343 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
344 g_cclosure_marshal_generic,
349 empathy_account_settings_dispose (GObject *object)
351 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
352 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
354 if (priv->dispose_has_run)
357 priv->dispose_has_run = TRUE;
359 if (priv->managers_ready_id != 0)
360 g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
361 priv->managers_ready_id = 0;
363 tp_clear_object (&priv->managers);
364 tp_clear_object (&priv->manager);
365 tp_clear_object (&priv->account_manager);
366 tp_clear_object (&priv->account);
367 tp_clear_object (&priv->protocol_obj);
369 /* release any references held by the object here */
370 if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
371 G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose (object);
375 empathy_account_settings_free_unset_parameters (
376 EmpathyAccountSettings *settings)
378 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
381 for (i = 0 ; i < priv->unset_parameters->len; i++)
382 g_free (g_array_index (priv->unset_parameters, gchar *, i));
384 g_array_set_size (priv->unset_parameters, 0);
388 empathy_account_settings_finalize (GObject *object)
390 EmpathyAccountSettings *self = EMPATHY_ACCOUNT_SETTINGS (object);
391 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
394 /* free any data held directly by the object here */
395 g_free (priv->cm_name);
396 g_free (priv->protocol);
397 g_free (priv->service);
398 g_free (priv->display_name);
399 g_free (priv->icon_name);
400 g_free (priv->password);
401 g_free (priv->password_original);
402 g_free (priv->storage_provider);
404 if (priv->required_params != NULL)
406 for (l = priv->required_params; l; l = l->next)
408 g_list_free (priv->required_params);
411 g_hash_table_unref (priv->parameters);
412 g_hash_table_unref (priv->param_regexps);
414 empathy_account_settings_free_unset_parameters (self);
415 g_array_unref (priv->unset_parameters);
417 G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
421 empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
422 GAsyncResult *result,
425 EmpathyAccountSettings *self = user_data;
426 GError *error = NULL;
428 if (!tp_proxy_prepare_finish (source, result, &error))
430 DEBUG ("Failed to prepare protocol object: %s", error->message);
431 g_clear_error (&error);
435 empathy_account_settings_check_readyness (self);
439 empathy_account_settings_get_password_cb (GObject *source,
440 GAsyncResult *result,
443 EmpathyAccountSettings *self = user_data;
444 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
445 const gchar *password;
446 GError *error = NULL;
448 password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
453 DEBUG ("Failed to get password: %s", error->message);
454 g_clear_error (&error);
457 /* It doesn't really matter if getting the password failed; that
458 * just means that it's not there, or let's act like that at
461 g_assert (priv->password == NULL);
463 priv->password = g_strdup (password);
464 priv->password_original = g_strdup (password);
466 g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
469 static GVariant * empathy_account_settings_dup (
470 EmpathyAccountSettings *settings,
474 empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
476 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
477 GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
482 if (priv->account != NULL
483 && !tp_account_is_prepared (priv->account, TP_ACCOUNT_FEATURE_CORE))
486 if (!empathy_connection_managers_is_ready (priv->managers))
489 if (priv->manager == NULL)
491 priv->manager = empathy_connection_managers_get_cm (
492 priv->managers, priv->cm_name);
495 if (priv->manager == NULL)
498 g_object_ref (priv->manager);
500 if (priv->account != NULL)
502 g_free (priv->display_name);
504 g_strdup (tp_account_get_display_name (priv->account));
506 g_free (priv->icon_name);
508 g_strdup (tp_account_get_icon_name (priv->account));
510 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
513 if (priv->protocol_obj == NULL)
515 priv->protocol_obj = g_object_ref (
516 tp_connection_manager_get_protocol_object (priv->manager,
520 if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
521 && !priv->preparing_protocol)
523 priv->preparing_protocol = TRUE;
524 tp_proxy_prepare_async (priv->protocol_obj, features,
525 empathy_account_settings_protocol_obj_prepared_cb, self);
530 if (tp_strv_contains (tp_protocol_get_authentication_types (
532 TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
534 priv->supports_sasl = TRUE;
538 if (priv->required_params == NULL)
542 params = tp_protocol_dup_params (priv->protocol_obj);
543 for (l = params; l != NULL; l = g_list_next (l))
545 TpConnectionManagerParam *cur = l->data;
547 if (tp_connection_manager_param_is_required (cur))
549 priv->required_params = g_list_append (priv->required_params,
550 g_strdup (tp_connection_manager_param_get_name (cur)));
554 g_list_free_full (params,
555 (GDestroyNotify) tp_connection_manager_param_free);
558 /* priv->account won't be a proper account if it's the account
559 * assistant showing this widget. */
560 if (priv->supports_sasl && !priv->password_requested
561 && priv->account != NULL)
563 priv->password_requested = TRUE;
565 /* Make this call but don't block on its readiness. We'll signal
566 * if it's updated later with ::password-retrieved. */
567 empathy_keyring_get_account_password_async (priv->account,
568 empathy_account_settings_get_password_cb, self);
572 g_object_notify (G_OBJECT (self), "ready");
576 empathy_account_settings_account_ready_cb (GObject *source_object,
577 GAsyncResult *result,
580 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
581 TpAccount *account = TP_ACCOUNT (source_object);
582 GError *error = NULL;
584 if (!tp_proxy_prepare_finish (account, result, &error))
586 DEBUG ("Failed to prepare account: %s", error->message);
587 g_error_free (error);
591 empathy_account_settings_check_readyness (settings);
595 empathy_account_settings_managers_ready_cb (GObject *object,
599 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
601 empathy_account_settings_check_readyness (settings);
604 EmpathyAccountSettings *
605 empathy_account_settings_new (const gchar *connection_manager,
606 const gchar *protocol,
607 const gchar *service,
608 const char *display_name)
610 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
611 "connection-manager", connection_manager,
612 "protocol", protocol,
614 "display-name", display_name,
618 EmpathyAccountSettings *
619 empathy_account_settings_new_for_account (TpAccount *account)
621 return g_object_new (EMPATHY_TYPE_ACCOUNT_SETTINGS,
627 empathy_account_settings_dup_tp_params (EmpathyAccountSettings *settings)
629 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
631 g_return_val_if_fail (priv->protocol_obj != NULL, NULL);
633 return tp_protocol_dup_params (priv->protocol_obj);
637 empathy_account_settings_is_ready (EmpathyAccountSettings *settings)
639 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
645 empathy_account_settings_get_cm (EmpathyAccountSettings *settings)
647 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
649 return priv->cm_name;
653 empathy_account_settings_get_protocol (EmpathyAccountSettings *settings)
655 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
657 return priv->protocol;
661 empathy_account_settings_get_service (EmpathyAccountSettings *settings)
663 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
665 return priv->service;
669 empathy_account_settings_set_service (EmpathyAccountSettings *settings,
670 const gchar *service)
672 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
674 if (!tp_strdiff (priv->service, service))
677 g_free (priv->service);
678 priv->service = g_strdup (service);
679 g_object_notify (G_OBJECT (settings), "service");
680 priv->update_service = TRUE;
684 empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
686 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
688 return priv->icon_name;
692 empathy_account_settings_get_display_name (EmpathyAccountSettings *settings)
694 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
696 return priv->display_name;
700 empathy_account_settings_get_account (EmpathyAccountSettings *settings)
702 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
704 return priv->account;
708 empathy_account_settings_is_unset (EmpathyAccountSettings *settings,
711 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
715 a = priv->unset_parameters;
717 for (i = 0; i < a->len; i++)
719 if (!tp_strdiff (g_array_index (a, gchar *, i), param))
726 static const TpConnectionManagerParam *
727 empathy_account_settings_get_tp_param (EmpathyAccountSettings *settings,
730 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
732 return tp_protocol_get_param (priv->protocol_obj, param);
736 empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
739 return (empathy_account_settings_get_tp_param (settings, param) != NULL);
743 account_settings_remove_from_unset (EmpathyAccountSettings *settings,
746 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
750 for (idx = 0; idx < priv->unset_parameters->len; idx++)
752 val = g_array_index (priv->unset_parameters, gchar *, idx);
754 if (!tp_strdiff (val, param))
756 priv->unset_parameters =
757 g_array_remove_index (priv->unset_parameters, idx);
766 empathy_account_settings_dup_default (EmpathyAccountSettings *settings,
769 const TpConnectionManagerParam *p;
771 p = empathy_account_settings_get_tp_param (settings, param);
775 return tp_connection_manager_param_dup_default_variant (p);
779 empathy_account_settings_get_dbus_signature (EmpathyAccountSettings *settings,
782 const TpConnectionManagerParam *p;
784 p = empathy_account_settings_get_tp_param (settings, param);
789 return tp_connection_manager_param_get_dbus_signature (p);
793 empathy_account_settings_dup (EmpathyAccountSettings *settings,
796 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
799 /* Lookup the update parameters we set */
800 result = g_hash_table_lookup (priv->parameters, param);
802 return g_variant_ref (result);
804 /* If the parameters isn't unset use the accounts setting if any */
805 if (priv->account != NULL
806 && !empathy_account_settings_is_unset (settings, param))
808 GVariant *parameters;
810 parameters = tp_account_dup_parameters_vardict (priv->account);
811 result = g_variant_lookup_value (parameters, param, NULL);
812 g_variant_unref (parameters);
815 /* g_variant_lookup_value() is (transfer full) */
819 /* fallback to the default */
820 return empathy_account_settings_dup_default (settings, param);
824 empathy_account_settings_unset (EmpathyAccountSettings *settings,
827 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
829 if (empathy_account_settings_is_unset (settings, param))
832 if (priv->supports_sasl && !tp_strdiff (param, "password"))
834 g_free (priv->password);
835 priv->password = NULL;
836 priv->password_changed = TRUE;
840 v = g_strdup (param);
842 g_array_append_val (priv->unset_parameters, v);
843 g_hash_table_remove (priv->parameters, param);
847 empathy_account_settings_discard_changes (EmpathyAccountSettings *settings)
849 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
851 g_hash_table_remove_all (priv->parameters);
852 empathy_account_settings_free_unset_parameters (settings);
854 priv->password_changed = FALSE;
855 g_free (priv->password);
856 priv->password = g_strdup (priv->password_original);
858 if (priv->account != NULL)
859 priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
861 priv->uri_scheme_tel = FALSE;
865 empathy_account_settings_dup_string (EmpathyAccountSettings *settings,
868 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
870 gchar *result = NULL;
872 if (!tp_strdiff (param, "password") && priv->supports_sasl)
874 return g_strdup (priv->password);
877 v = empathy_account_settings_dup (settings, param);
881 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
882 result = g_variant_dup_string (v, NULL);
889 empathy_account_settings_dup_strv (EmpathyAccountSettings *settings,
895 v = empathy_account_settings_dup (settings, param);
899 if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING_ARRAY))
900 result = g_variant_dup_strv (v, NULL);
907 empathy_account_settings_get_int32 (EmpathyAccountSettings *settings,
913 v = empathy_account_settings_dup (settings, param);
917 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
918 ret = g_variant_get_byte (v);
919 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
920 ret = g_variant_get_int32 (v);
921 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
922 ret = CLAMP (g_variant_get_uint32 (v), (guint) G_MININT32,
924 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
925 ret = CLAMP (g_variant_get_int64 (v), G_MININT32, G_MAXINT32);
926 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
927 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT32, G_MAXINT32);
932 tmp = g_variant_print (v, TRUE);
933 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
942 empathy_account_settings_get_int64 (EmpathyAccountSettings *settings,
948 v = empathy_account_settings_dup (settings, param);
952 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
953 ret = g_variant_get_byte (v);
954 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
955 ret = g_variant_get_int32 (v);
956 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
957 ret = g_variant_get_uint32 (v);
958 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
959 ret = g_variant_get_int64 (v);
960 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
961 ret = CLAMP (g_variant_get_uint64 (v), (guint64) G_MININT64, G_MAXINT64);
966 tmp = g_variant_print (v, TRUE);
967 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
976 empathy_account_settings_get_uint32 (EmpathyAccountSettings *settings,
982 v = empathy_account_settings_dup (settings, param);
986 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
987 ret = g_variant_get_byte (v);
988 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
989 ret = MAX (0, g_variant_get_int32 (v));
990 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
991 ret = g_variant_get_uint32 (v);
992 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
993 ret = CLAMP (g_variant_get_int64 (v), 0, G_MAXUINT32);
994 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
995 ret = MIN (g_variant_get_uint64 (v), G_MAXUINT32);
1000 tmp = g_variant_print (v, TRUE);
1001 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1005 g_variant_unref (v);
1010 empathy_account_settings_get_uint64 (EmpathyAccountSettings *settings,
1016 v = empathy_account_settings_dup (settings, param);
1020 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BYTE))
1021 ret = g_variant_get_byte (v);
1022 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT32))
1023 ret = MAX (0, g_variant_get_int32 (v));
1024 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
1025 ret = g_variant_get_uint32 (v);
1026 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_INT64))
1027 ret = MAX (0, g_variant_get_int64 (v));
1028 else if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT64))
1029 ret = g_variant_get_uint64 (v);
1034 tmp = g_variant_print (v, TRUE);
1035 DEBUG ("Unsupported type for param '%s': %s'", param, tmp);
1040 g_variant_unref (v);
1045 empathy_account_settings_get_boolean (EmpathyAccountSettings *settings,
1049 gboolean result = FALSE;
1051 v = empathy_account_settings_dup (settings, param);
1055 if (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN))
1056 result = g_variant_get_boolean (v);
1062 empathy_account_settings_set (EmpathyAccountSettings *settings,
1066 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1068 g_return_if_fail (param != NULL);
1069 g_return_if_fail (v != NULL);
1071 if (!tp_strdiff (param, "password") && priv->supports_sasl &&
1072 g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
1074 g_free (priv->password);
1075 priv->password = g_variant_dup_string (v, NULL);
1076 priv->password_changed = TRUE;
1080 g_hash_table_insert (priv->parameters, g_strdup (param),
1081 g_variant_ref_sink (v));
1084 account_settings_remove_from_unset (settings, param);
1088 account_settings_display_name_set_cb (GObject *src,
1092 GError *error = NULL;
1093 TpAccount *account = TP_ACCOUNT (src);
1094 GSimpleAsyncResult *set_result = user_data;
1096 tp_account_set_display_name_finish (account, res, &error);
1100 g_simple_async_result_set_from_error (set_result, error);
1101 g_error_free (error);
1104 g_simple_async_result_complete (set_result);
1105 g_object_unref (set_result);
1109 empathy_account_settings_set_display_name_async (
1110 EmpathyAccountSettings *settings,
1112 GAsyncReadyCallback callback,
1115 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1116 GSimpleAsyncResult *result;
1118 g_return_if_fail (name != NULL);
1120 result = g_simple_async_result_new (G_OBJECT (settings),
1121 callback, user_data, empathy_account_settings_set_display_name_finish);
1123 if (!tp_strdiff (name, priv->display_name))
1126 g_simple_async_result_complete_in_idle (result);
1130 if (priv->account == NULL)
1132 if (priv->display_name != NULL)
1133 g_free (priv->display_name);
1135 priv->display_name = g_strdup (name);
1137 g_simple_async_result_complete_in_idle (result);
1142 tp_account_set_display_name_async (priv->account, name,
1143 account_settings_display_name_set_cb, result);
1147 empathy_account_settings_set_display_name_finish (
1148 EmpathyAccountSettings *settings,
1149 GAsyncResult *result,
1152 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1156 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1157 G_OBJECT (settings), empathy_account_settings_set_display_name_finish),
1164 account_settings_icon_name_set_cb (GObject *src,
1168 GError *error = NULL;
1169 TpAccount *account = TP_ACCOUNT (src);
1170 GSimpleAsyncResult *set_result = user_data;
1172 tp_account_set_icon_name_finish (account, res, &error);
1176 g_simple_async_result_set_from_error (set_result, error);
1177 g_error_free (error);
1180 g_simple_async_result_complete (set_result);
1181 g_object_unref (set_result);
1185 empathy_account_settings_set_icon_name_async (
1186 EmpathyAccountSettings *settings,
1188 GAsyncReadyCallback callback,
1191 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1192 GSimpleAsyncResult *result;
1194 g_return_if_fail (name != NULL);
1196 result = g_simple_async_result_new (G_OBJECT (settings),
1197 callback, user_data, empathy_account_settings_set_icon_name_finish);
1199 if (priv->account == NULL)
1201 if (priv->icon_name != NULL)
1202 g_free (priv->icon_name);
1204 priv->icon_name = g_strdup (name);
1206 g_simple_async_result_complete_in_idle (result);
1211 tp_account_set_icon_name_async (priv->account, name,
1212 account_settings_icon_name_set_cb, result);
1216 empathy_account_settings_set_icon_name_finish (
1217 EmpathyAccountSettings *settings,
1218 GAsyncResult *result,
1221 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1225 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1226 G_OBJECT (settings), empathy_account_settings_set_icon_name_finish),
1233 empathy_account_settings_processed_password (GObject *source,
1234 GAsyncResult *result,
1236 gpointer finish_func)
1238 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1239 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1240 GSimpleAsyncResult *r;
1241 GError *error = NULL;
1242 gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
1245 g_free (priv->password_original);
1246 priv->password_original = g_strdup (priv->password);
1248 if (!func (TP_ACCOUNT (source), result, &error))
1250 g_simple_async_result_set_from_error (priv->apply_result, error);
1251 g_error_free (error);
1254 empathy_account_settings_discard_changes (settings);
1256 r = priv->apply_result;
1257 priv->apply_result = NULL;
1259 g_simple_async_result_complete (r);
1264 empathy_account_settings_set_password_cb (GObject *source,
1265 GAsyncResult *result,
1268 empathy_account_settings_processed_password (source, result, user_data,
1269 empathy_keyring_set_account_password_finish);
1273 empathy_account_settings_delete_password_cb (GObject *source,
1274 GAsyncResult *result,
1277 empathy_account_settings_processed_password (source, result, user_data,
1278 empathy_keyring_delete_account_password_finish);
1282 update_account_uri_schemes (EmpathyAccountSettings *self)
1284 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1286 if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
1290 tp_account_set_uri_scheme_association_async (priv->account, "tel",
1291 priv->uri_scheme_tel, NULL, NULL);
1295 set_service_cb (GObject *source,
1296 GAsyncResult *result,
1299 GError *error = NULL;
1301 if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
1303 DEBUG ("Failed to set Account.Service: %s", error->message);
1304 g_error_free (error);
1309 update_account_service (EmpathyAccountSettings *self)
1311 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1313 if (!priv->update_service)
1316 tp_account_set_service_async (priv->account,
1317 priv->service != NULL ? priv->service : "", set_service_cb, self);
1321 empathy_account_settings_account_updated (GObject *source,
1322 GAsyncResult *result,
1325 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1326 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1327 GSimpleAsyncResult *r;
1328 GError *error = NULL;
1329 GStrv reconnect_required = NULL;
1331 if (!tp_account_update_parameters_vardict_finish (TP_ACCOUNT (source),
1332 result, &reconnect_required, &error))
1334 g_simple_async_result_set_from_error (priv->apply_result, error);
1335 g_error_free (error);
1339 /* Only set the password in the keyring if the CM supports SASL and
1341 if (priv->supports_sasl && priv->password_changed)
1343 if (priv->password != NULL)
1345 /* FIXME: we shouldn't save the password if we
1346 * can't (MaySaveResponse=False) but we don't have API to check that
1347 * at this point (fdo #35382). */
1348 empathy_keyring_set_account_password_async (priv->account,
1349 priv->password, priv->remember_password,
1350 empathy_account_settings_set_password_cb, settings);
1354 empathy_keyring_delete_account_password_async (priv->account,
1355 empathy_account_settings_delete_password_cb, settings);
1361 update_account_uri_schemes (settings);
1362 update_account_service (settings);
1364 g_simple_async_result_set_op_res_gboolean (priv->apply_result,
1365 g_strv_length (reconnect_required) > 0);
1368 empathy_account_settings_discard_changes (settings);
1370 r = priv->apply_result;
1371 priv->apply_result = NULL;
1373 g_simple_async_result_complete (r);
1375 g_strfreev (reconnect_required);
1379 empathy_account_settings_created_cb (GObject *source,
1380 GAsyncResult *result,
1383 EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
1384 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1385 GError *error = NULL;
1386 GSimpleAsyncResult *r;
1388 priv->account = tp_account_request_create_account_finish (
1389 TP_ACCOUNT_REQUEST (source), result, &error);
1391 if (priv->account == NULL)
1393 g_simple_async_result_set_from_error (priv->apply_result, error);
1397 if (priv->supports_sasl && priv->password != NULL)
1399 /* Save the password before connecting */
1400 /* FIXME: we shouldn't save the password if we
1401 * can't (MaySaveResponse=False) but we don't have API to check that
1402 * at this point (fdo #35382). */
1403 empathy_keyring_set_account_password_async (priv->account,
1404 priv->password, priv->remember_password,
1405 empathy_account_settings_set_password_cb,
1410 update_account_uri_schemes (settings);
1412 empathy_account_settings_discard_changes (settings);
1415 r = priv->apply_result;
1416 priv->apply_result = NULL;
1418 g_simple_async_result_complete (r);
1423 empathy_account_settings_do_create_account (EmpathyAccountSettings *self)
1425 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1426 TpAccountRequest *account_req;
1427 TpConnectionPresenceType type;
1430 EmpathyPresenceManager *presence_mgr;
1431 GHashTableIter iter;
1434 account_req = tp_account_request_new (priv->account_manager, priv->cm_name,
1435 priv->protocol, "New Account");
1437 presence_mgr = empathy_presence_manager_dup_singleton ();
1438 type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
1440 g_object_unref (presence_mgr);
1442 if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
1444 tp_account_request_set_requested_presence (account_req, type,
1448 tp_account_request_set_icon_name (account_req, priv->icon_name);
1450 tp_account_request_set_display_name (account_req, priv->display_name);
1452 if (priv->service != NULL)
1453 tp_account_request_set_service (account_req, priv->service);
1455 g_hash_table_iter_init (&iter, priv->parameters);
1456 while (g_hash_table_iter_next (&iter, &k, &v))
1458 const gchar *key = k;
1459 GVariant *value = v;
1461 tp_account_request_set_parameter (account_req, key, value);
1464 if (priv->storage_provider != NULL)
1466 tp_account_request_set_storage_provider (account_req,
1467 priv->storage_provider);
1470 tp_account_request_create_account_async (account_req,
1471 empathy_account_settings_created_cb, self);
1475 build_parameters_variant (EmpathyAccountSettings *self)
1477 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1478 GVariantBuilder *builder;
1479 GHashTableIter iter;
1482 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
1484 g_hash_table_iter_init (&iter, priv->parameters);
1485 while (g_hash_table_iter_next (&iter, &k, &v))
1487 const gchar *key = k;
1488 GVariant *value = v;
1491 entry = g_variant_new_dict_entry (g_variant_new_string (key),
1492 g_variant_new_variant (value));
1494 g_variant_builder_add_value (builder, entry);
1497 return g_variant_builder_end (builder);
1501 empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
1502 GAsyncReadyCallback callback,
1505 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1507 if (priv->apply_result != NULL)
1509 g_simple_async_report_error_in_idle (G_OBJECT (settings),
1510 callback, user_data,
1511 G_IO_ERROR, G_IO_ERROR_PENDING, "Applying already in progress");
1515 priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
1516 callback, user_data, empathy_account_settings_apply_finish);
1518 /* We'll have to reconnect only if we change none DBus_Property on an
1519 * existing account. */
1520 g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
1522 if (priv->account == NULL)
1524 g_assert (priv->apply_result != NULL && priv->account == NULL);
1526 empathy_account_settings_do_create_account (settings);
1530 tp_account_update_parameters_vardict_async (priv->account,
1531 build_parameters_variant (settings),
1532 (const gchar **) priv->unset_parameters->data,
1533 empathy_account_settings_account_updated, settings);
1538 empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
1539 GAsyncResult *result,
1540 gboolean *reconnect_required,
1543 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
1547 g_return_val_if_fail (g_simple_async_result_is_valid (result,
1548 G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
1550 if (reconnect_required != NULL)
1551 *reconnect_required = g_simple_async_result_get_op_res_gboolean (
1552 G_SIMPLE_ASYNC_RESULT (result));
1558 empathy_account_settings_has_account (EmpathyAccountSettings *settings,
1561 EmpathyAccountSettingsPriv *priv;
1562 const gchar *account_path;
1563 const gchar *priv_account_path;
1565 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1566 g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
1568 priv = GET_PRIV (settings);
1570 if (priv->account == NULL)
1573 account_path = tp_proxy_get_object_path (TP_PROXY (account));
1574 priv_account_path = tp_proxy_get_object_path (TP_PROXY (priv->account));
1576 return (!tp_strdiff (account_path, priv_account_path));
1580 empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
1582 const gchar *pattern)
1584 EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
1586 GError *error = NULL;
1588 regex = g_regex_new (pattern, 0, 0, &error);
1591 g_warning ("Failed to create reg exp: %s", error->message);
1592 g_error_free (error);
1596 g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
1600 empathy_account_settings_parameter_is_valid (
1601 EmpathyAccountSettings *settings,
1604 EmpathyAccountSettingsPriv *priv;
1605 const GRegex *regex;
1607 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1609 priv = GET_PRIV (settings);
1611 if (g_list_find_custom (priv->required_params, param, (GCompareFunc) strcmp))
1613 /* first, look if it's set in our own parameters */
1614 if (g_hash_table_lookup (priv->parameters, param) != NULL)
1617 /* if we did not unset the parameter, look if it's in the account */
1618 if (priv->account != NULL &&
1619 !empathy_account_settings_is_unset (settings, param))
1621 const GHashTable *account_params;
1623 account_params = tp_account_get_parameters (priv->account);
1624 if (tp_asv_lookup (account_params, param))
1632 /* test whether parameter value matches its regex */
1633 regex = g_hash_table_lookup (priv->param_regexps, param);
1639 value = empathy_account_settings_dup_string (settings, param);
1643 match = g_regex_match (regex, value, 0, NULL);
1653 empathy_account_settings_is_valid (EmpathyAccountSettings *settings)
1655 EmpathyAccountSettingsPriv *priv;
1657 GHashTableIter iter;
1660 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_SETTINGS (settings), FALSE);
1662 priv = GET_PRIV (settings);
1664 for (l = priv->required_params; l; l = l->next)
1666 if (!empathy_account_settings_parameter_is_valid (settings, l->data))
1670 g_hash_table_iter_init (&iter, priv->param_regexps);
1671 while (g_hash_table_iter_next (&iter, (gpointer *) ¶m, NULL))
1673 if (!empathy_account_settings_parameter_is_valid (settings, param))
1681 empathy_account_settings_get_tp_protocol (EmpathyAccountSettings *self)
1683 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1685 return priv->protocol_obj;
1689 empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
1691 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1693 return priv->supports_sasl;
1697 empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
1700 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1702 return tp_protocol_has_param (priv->protocol_obj, param);
1706 empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
1709 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1711 priv->uri_scheme_tel = associate;
1715 empathy_account_settings_has_uri_scheme_tel (
1716 EmpathyAccountSettings *self)
1718 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1720 return priv->uri_scheme_tel;
1724 empathy_account_settings_set_storage_provider (EmpathyAccountSettings *self,
1725 const gchar *storage)
1727 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1729 g_free (priv->storage_provider);
1730 priv->storage_provider = g_strdup (storage);
1734 empathy_account_settings_set_remember_password (EmpathyAccountSettings *self,
1737 EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
1739 priv->remember_password = remember;