#include "empathy-account-settings.h"
#include "empathy-connection-managers.h"
+#include "empathy-keyring.h"
#include "empathy-utils.h"
-#include "empathy-idle.h"
+#include "empathy-presence-manager.h"
#define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
#include <libempathy/empathy-debug.h>
PROP_READY
};
+enum {
+ PASSWORD_RETRIEVED = 1,
+ LAST_SIGNAL
+};
+
+static gulong signals[LAST_SIGNAL] = { 0, };
+
/* private structure */
typedef struct _EmpathyAccountSettingsPriv EmpathyAccountSettingsPriv;
TpAccountManager *account_manager;
TpConnectionManager *manager;
+ TpProtocol *protocol_obj;
TpAccount *account;
gchar *cm_name;
gboolean display_name_overridden;
gboolean ready;
+ gboolean supports_sasl;
+ gboolean password_changed;
+
+ gchar *password;
+ gchar *password_original;
+
+ gboolean password_retrieved;
+ gboolean password_requested;
+
/* Parameter name (gchar *) -> parameter value (GValue) */
GHashTable *parameters;
/* Keys are parameter names from the hash above (gchar *).
GList *required_params;
gulong managers_ready_id;
+ gboolean preparing_protocol;
+
+ /* If TRUE, the account should have 'tel' in its
+ * Account.Interface.Addressing.URISchemes property. */
+ gboolean uri_scheme_tel;
+ /* If TRUE, Service property needs to be updated when applying changes */
+ gboolean update_service;
GSimpleAsyncResult *apply_result;
};
if (!priv->ready)
{
- tp_account_prepare_async (priv->account, NULL,
- empathy_account_settings_account_ready_cb, self);
+ GQuark features[] = {
+ TP_ACCOUNT_FEATURE_CORE,
+ TP_ACCOUNT_FEATURE_STORAGE,
+ TP_ACCOUNT_FEATURE_ADDRESSING,
+ 0 };
+
+ if (priv->account != NULL)
+ {
+ tp_proxy_prepare_async (priv->account, features,
+ empathy_account_settings_account_ready_cb, self);
+ }
+
tp_g_signal_connect_object (priv->managers, "notify::ready",
G_CALLBACK (empathy_account_settings_managers_ready_cb), object, 0);
}
"Whether this account is ready to be used",
FALSE,
G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
+
+ signals[PASSWORD_RETRIEVED] =
+ g_signal_new ("password-retrieved",
+ G_TYPE_FROM_CLASS (empathy_account_settings_class),
+ G_SIGNAL_RUN_LAST, 0, NULL, NULL,
+ g_cclosure_marshal_generic,
+ G_TYPE_NONE, 0);
}
static void
g_signal_handler_disconnect (priv->managers, priv->managers_ready_id);
priv->managers_ready_id = 0;
- if (priv->managers != NULL)
- g_object_unref (priv->managers);
- priv->managers = NULL;
-
- if (priv->manager != NULL)
- g_object_unref (priv->manager);
- priv->manager = NULL;
-
- if (priv->account_manager != NULL)
- g_object_unref (priv->account_manager);
- priv->account_manager = NULL;
-
- if (priv->account != NULL)
- g_object_unref (priv->account);
- priv->account = NULL;
+ tp_clear_object (&priv->managers);
+ tp_clear_object (&priv->manager);
+ tp_clear_object (&priv->account_manager);
+ tp_clear_object (&priv->account);
+ tp_clear_object (&priv->protocol_obj);
/* release any references held by the object here */
if (G_OBJECT_CLASS (empathy_account_settings_parent_class)->dispose)
g_free (priv->service);
g_free (priv->display_name);
g_free (priv->icon_name);
+ g_free (priv->password);
+ g_free (priv->password_original);
if (priv->required_params != NULL)
{
g_list_free (priv->required_params);
}
- g_hash_table_destroy (priv->parameters);
- g_hash_table_destroy (priv->param_regexps);
+ g_hash_table_unref (priv->parameters);
+ g_hash_table_unref (priv->param_regexps);
empathy_account_settings_free_unset_parameters (self);
- g_array_free (priv->unset_parameters, TRUE);
+ g_array_unref (priv->unset_parameters);
G_OBJECT_CLASS (empathy_account_settings_parent_class)->finalize (object);
}
+static void
+empathy_account_settings_protocol_obj_prepared_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ EmpathyAccountSettings *self = user_data;
+ GError *error = NULL;
+
+ if (!tp_proxy_prepare_finish (source, result, &error))
+ {
+ DEBUG ("Failed to prepare protocol object: %s", error->message);
+ g_clear_error (&error);
+ return;
+ }
+
+ empathy_account_settings_check_readyness (self);
+}
+
+static void
+empathy_account_settings_get_password_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ EmpathyAccountSettings *self = user_data;
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+ const gchar *password;
+ GError *error = NULL;
+
+ password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
+ result, &error);
+
+ if (error != NULL)
+ {
+ DEBUG ("Failed to get password: %s", error->message);
+ g_clear_error (&error);
+ }
+
+ /* It doesn't really matter if getting the password failed; that
+ * just means that it's not there, or let's act like that at
+ * least. */
+
+ g_assert (priv->password == NULL);
+
+ priv->password = g_strdup (password);
+ priv->password_original = g_strdup (password);
+
+ g_signal_emit (self, signals[PASSWORD_RETRIEVED], 0);
+}
+
+static void
+empathy_account_settings_migrate_password_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TpAccount *account = TP_ACCOUNT (source);
+ GError *error = NULL;
+ EmpathyAccountSettings *self = user_data;
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+ GHashTable *empty;
+ const gchar *unset[] = { "password", NULL };
+
+ if (!empathy_keyring_set_account_password_finish (account, result, &error))
+ {
+ DEBUG ("Failed to set password: %s", error->message);
+ g_clear_error (&error);
+ return;
+ }
+
+ /* Now clear the password MC has stored. */
+ empty = tp_asv_new (NULL, NULL);
+ tp_account_update_parameters_async (priv->account,
+ empty, unset, NULL, NULL);
+
+ g_hash_table_remove (priv->parameters, "password");
+
+ g_hash_table_unref (empty);
+}
+
+static void
+empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+ const GValue *v;
+ const gchar *password;
+
+ if (!priv->supports_sasl || empathy_account_settings_get (
+ self, "password") == NULL)
+ return;
+
+ /* mission-control still has our password, although the CM
+ * supports SASL. Let's try migrating it. */
+
+ DEBUG ("Trying to migrate password parameter from MC to the "
+ "keyring ourselves for account %s",
+ tp_account_get_path_suffix (priv->account));
+
+ v = empathy_account_settings_get (self, "password");
+
+ /* I can't imagine why this would fail. */
+ if (!G_VALUE_HOLDS_STRING (v))
+ return;
+
+ password = g_value_get_string (v);
+
+ if (EMP_STR_EMPTY (password))
+ return;
+
+ empathy_keyring_set_account_password_async (priv->account, password,
+ empathy_account_settings_migrate_password_cb, self);
+
+ /* We don't want to request the password again, we
+ * already know it. */
+ priv->password_requested = TRUE;
+
+ priv->password = g_strdup (password);
+ priv->password_original = g_strdup (password);
+}
+
static void
empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
{
EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
const TpConnectionManagerProtocol *tp_protocol;
+ GQuark features[] = { TP_PROTOCOL_FEATURE_CORE, 0 };
if (priv->ready)
return;
if (!empathy_connection_managers_is_ready (priv->managers))
return;
- priv->manager = empathy_connection_managers_get_cm (
- priv->managers, priv->cm_name);
+ if (priv->manager == NULL)
+ {
+ priv->manager = empathy_connection_managers_get_cm (
+ priv->managers, priv->cm_name);
+ }
if (priv->manager == NULL)
return;
+ g_object_ref (priv->manager);
+
if (priv->account != NULL)
{
g_free (priv->display_name);
g_free (priv->icon_name);
priv->icon_name =
g_strdup (tp_account_get_icon_name (priv->account));
+
+ priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
}
tp_protocol = tp_connection_manager_get_protocol (priv->manager,
if (tp_protocol == NULL)
{
- priv->manager = NULL;
+ tp_clear_object (&priv->manager);
return;
}
}
}
- g_object_ref (priv->manager);
+ if (priv->protocol_obj == NULL)
+ {
+ priv->protocol_obj = g_object_ref (
+ tp_connection_manager_get_protocol_object (priv->manager,
+ priv->protocol));
+ }
+
+ if (!tp_proxy_is_prepared (priv->protocol_obj, TP_PROTOCOL_FEATURE_CORE)
+ && !priv->preparing_protocol)
+ {
+ priv->preparing_protocol = TRUE;
+ tp_proxy_prepare_async (priv->protocol_obj, features,
+ empathy_account_settings_protocol_obj_prepared_cb, self);
+ return;
+ }
+ else
+ {
+ if (tp_strv_contains (tp_protocol_get_authentication_types (
+ priv->protocol_obj),
+ TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION))
+ {
+ priv->supports_sasl = TRUE;
+ }
+ }
+
+ /* NOTE: When removing MC migration code, remove this call, and the
+ * function it's calling. That's it. */
+ empathy_account_settings_try_migrating_password (self);
+
+ /* priv->account won't be a proper account if it's the account
+ * assistant showing this widget. */
+ if (priv->supports_sasl && !priv->password_requested
+ && priv->account != NULL)
+ {
+ priv->password_requested = TRUE;
+
+ /* Make this call but don't block on its readiness. We'll signal
+ * if it's updated later with ::password-retrieved. */
+ empathy_keyring_get_account_password_async (priv->account,
+ empathy_account_settings_get_password_cb, self);
+ }
priv->ready = TRUE;
g_object_notify (G_OBJECT (self), "ready");
TpAccount *account = TP_ACCOUNT (source_object);
GError *error = NULL;
- if (!tp_account_prepare_finish (account, result, &error))
+ if (!tp_proxy_prepare_finish (account, result, &error))
{
DEBUG ("Failed to prepare account: %s", error->message);
g_error_free (error);
return priv->service;
}
+void
+empathy_account_settings_set_service (EmpathyAccountSettings *settings,
+ const gchar *service)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
+
+ if (!tp_strdiff (priv->service, service))
+ return;
+
+ g_free (priv->service);
+ priv->service = g_strdup (service);
+ g_object_notify (G_OBJECT (settings), "service");
+ priv->update_service = TRUE;
+}
+
gchar *
empathy_account_settings_get_icon_name (EmpathyAccountSettings *settings)
{
return NULL;
}
+gboolean
+empathy_account_settings_have_tp_param (EmpathyAccountSettings *settings,
+ const gchar *param)
+{
+ return (empathy_account_settings_get_tp_param (settings, param) != NULL);
+}
+
static void
account_settings_remove_from_unset (EmpathyAccountSettings *settings,
const gchar *param)
if (empathy_account_settings_is_unset (settings, param))
return;
+ if (priv->supports_sasl && !tp_strdiff (param, "password"))
+ {
+ g_free (priv->password);
+ priv->password = NULL;
+ priv->password_changed = TRUE;
+ return;
+ }
+
v = g_strdup (param);
g_array_append_val (priv->unset_parameters, v);
g_hash_table_remove_all (priv->parameters);
empathy_account_settings_free_unset_parameters (settings);
+
+ priv->password_changed = FALSE;
+ g_free (priv->password);
+ priv->password = g_strdup (priv->password_original);
+
+ if (priv->account != NULL)
+ priv->uri_scheme_tel = empathy_account_has_uri_scheme_tel (priv->account);
+ else
+ priv->uri_scheme_tel = FALSE;
}
const gchar *
empathy_account_settings_get_string (EmpathyAccountSettings *settings,
const gchar *param)
{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
const GValue *v;
+ if (!tp_strdiff (param, "password") && priv->supports_sasl)
+ {
+ return priv->password;
+ }
+
v = empathy_account_settings_get (settings, param);
if (v == NULL || !G_VALUE_HOLDS_STRING (v))
g_return_if_fail (param != NULL);
g_return_if_fail (value != NULL);
- tp_asv_set_string (priv->parameters, g_strdup (param), value);
+ if (!tp_strdiff (param, "password") && priv->supports_sasl)
+ {
+ g_free (priv->password);
+ priv->password = g_strdup (value);
+ priv->password_changed = TRUE;
+ }
+ else
+ {
+ tp_asv_set_string (priv->parameters, g_strdup (param), value);
+ }
account_settings_remove_from_unset (settings, param);
}
return TRUE;
}
+static void
+empathy_account_settings_processed_password (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data,
+ gpointer finish_func)
+{
+ EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (user_data);
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
+ GSimpleAsyncResult *r;
+ GError *error = NULL;
+ gboolean (*func) (TpAccount *source, GAsyncResult *result, GError **error) =
+ finish_func;
+
+ g_free (priv->password_original);
+ priv->password_original = g_strdup (priv->password);
+
+ if (!func (TP_ACCOUNT (source), result, &error))
+ {
+ g_simple_async_result_set_from_error (priv->apply_result, error);
+ g_error_free (error);
+ }
+
+ empathy_account_settings_discard_changes (settings);
+
+ r = priv->apply_result;
+ priv->apply_result = NULL;
+
+ g_simple_async_result_complete (r);
+ g_object_unref (r);
+}
+
+static void
+empathy_account_settings_set_password_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ empathy_account_settings_processed_password (source, result, user_data,
+ empathy_keyring_set_account_password_finish);
+}
+
+static void
+empathy_account_settings_delete_password_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ empathy_account_settings_processed_password (source, result, user_data,
+ empathy_keyring_delete_account_password_finish);
+}
+
+static void
+update_account_uri_schemes (EmpathyAccountSettings *self)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+
+ if (priv->uri_scheme_tel == empathy_account_has_uri_scheme_tel (
+ priv->account))
+ return;
+
+ tp_account_set_uri_scheme_association_async (priv->account, "tel",
+ priv->uri_scheme_tel, NULL, NULL);
+}
+
+static void
+set_service_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GError *error = NULL;
+
+ if (!tp_account_set_service_finish (TP_ACCOUNT (source), result, &error))
+ {
+ DEBUG ("Failed to set Account.Service: %s", error->message);
+ g_error_free (error);
+ }
+}
+
+static void
+update_account_service (EmpathyAccountSettings *self)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+
+ if (!priv->update_service)
+ return;
+
+ tp_account_set_service_async (priv->account,
+ priv->service != NULL ? priv->service : "", set_service_cb, self);
+}
+
static void
empathy_account_settings_account_updated (GObject *source,
GAsyncResult *result,
EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
GSimpleAsyncResult *r;
GError *error = NULL;
+ GStrv reconnect_required = NULL;
if (!tp_account_update_parameters_finish (TP_ACCOUNT (source),
- result, NULL, &error))
+ result, &reconnect_required, &error))
{
g_simple_async_result_set_from_error (priv->apply_result, error);
g_error_free (error);
+ goto out;
}
- else
+
+ /* Only set the password in the keyring if the CM supports SASL and
+ * it's changed. */
+ if (priv->supports_sasl && priv->password_changed)
{
- empathy_account_settings_discard_changes (settings);
+ if (priv->password != NULL)
+ {
+ /* FIXME: we shouldn't save the password if we
+ * can't (MaySaveResponse=False) but we don't have API to check that
+ * at this point (fdo #35382). */
+ empathy_keyring_set_account_password_async (priv->account, priv->password,
+ empathy_account_settings_set_password_cb, settings);
+ }
+ else
+ {
+ empathy_keyring_delete_account_password_async (priv->account,
+ empathy_account_settings_delete_password_cb, settings);
+ }
+
+ return;
}
+ update_account_uri_schemes (settings);
+ update_account_service (settings);
+
+ g_simple_async_result_set_op_res_gboolean (priv->apply_result,
+ g_strv_length (reconnect_required) > 0);
+
+out:
+ empathy_account_settings_discard_changes (settings);
+
r = priv->apply_result;
priv->apply_result = NULL;
g_simple_async_result_complete (r);
g_object_unref (r);
+ g_strfreev (reconnect_required);
}
static void
else
{
priv->account = g_object_ref (account);
+
+ if (priv->supports_sasl && priv->password != NULL)
+ {
+ /* Save the password before connecting */
+ /* FIXME: we shouldn't save the password if we
+ * can't (MaySaveResponse=False) but we don't have API to check that
+ * at this point (fdo #35382). */
+ empathy_keyring_set_account_password_async (priv->account,
+ priv->password, empathy_account_settings_set_password_cb,
+ settings);
+ return;
+ }
+
+ update_account_uri_schemes (settings);
+
empathy_account_settings_discard_changes (settings);
}
TpConnectionPresenceType type;
gchar *status;
gchar *message;
- EmpathyIdle *idle;
+ EmpathyPresenceManager *presence_mgr;
properties = tp_asv_new (NULL, NULL);
- idle = empathy_idle_dup_singleton ();
- type = empathy_idle_get_requested_presence (idle, &status, &message);
- g_object_unref (idle);
+ presence_mgr = empathy_presence_manager_dup_singleton ();
+ type = empathy_presence_manager_get_requested_presence (presence_mgr, &status,
+ &message);
+ g_object_unref (presence_mgr);
if (type != TP_CONNECTION_PRESENCE_TYPE_UNSET)
{
TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
GError *error = NULL;
- if (!tp_account_manager_prepare_finish (account_manager, result, &error))
+ if (!tp_proxy_prepare_finish (account_manager, result, &error))
{
DEBUG ("Failed to prepare account manager: %s", error->message);
g_error_free (error);
priv->apply_result = g_simple_async_result_new (G_OBJECT (settings),
callback, user_data, empathy_account_settings_apply_finish);
+ /* We'll have to reconnect only if we change none DBus_Property on an
+ * existing account. */
+ g_simple_async_result_set_op_res_gboolean (priv->apply_result, FALSE);
+
if (priv->account == NULL)
{
- tp_account_manager_prepare_async (priv->account_manager, NULL,
+ tp_proxy_prepare_async (priv->account_manager, NULL,
empathy_account_settings_manager_ready_cb, settings);
}
else
gboolean
empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
GAsyncResult *result,
+ gboolean *reconnect_required,
GError **error)
{
if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
g_return_val_if_fail (g_simple_async_result_is_valid (result,
G_OBJECT (settings), empathy_account_settings_apply_finish), FALSE);
+ if (reconnect_required != NULL)
+ *reconnect_required = g_simple_async_result_get_op_res_gboolean (
+ G_SIMPLE_ASYNC_RESULT (result));
+
return TRUE;
}
{
EmpathyAccountSettingsPriv *priv = GET_PRIV (settings);
GRegex *regex;
+ GError *error = NULL;
+
+ regex = g_regex_new (pattern, 0, 0, &error);
+ if (regex == NULL)
+ {
+ g_warning ("Failed to create reg exp: %s", error->message);
+ g_error_free (error);
+ return;
+ }
- regex = g_regex_new (pattern, 0, 0, NULL);
g_hash_table_insert (priv->param_regexps, g_strdup (param), regex);
}
return tp_connection_manager_get_protocol (priv->manager, priv->protocol);
}
+
+gboolean
+empathy_account_settings_supports_sasl (EmpathyAccountSettings *self)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+
+ return priv->supports_sasl;
+}
+
+gboolean
+empathy_account_settings_param_is_supported (EmpathyAccountSettings *self,
+ const gchar *param)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+
+ return tp_protocol_has_param (priv->protocol_obj, param);
+}
+
+void
+empathy_account_settings_set_uri_scheme_tel (EmpathyAccountSettings *self,
+ gboolean associate)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+
+ priv->uri_scheme_tel = associate;
+}
+
+gboolean
+empathy_account_settings_has_uri_scheme_tel (
+ EmpathyAccountSettings *self)
+{
+ EmpathyAccountSettingsPriv *priv = GET_PRIV (self);
+
+ return priv->uri_scheme_tel;
+}