]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-account-settings.c
account-settings: allow to change the service
[empathy.git] / libempathy / empathy-account-settings.c
index 8c673dfcf4a204e2422c034224fdd69ff82fccab..cf269bdc84e997168a8fec4c287a54e4b283763a 100644 (file)
@@ -100,6 +100,12 @@ struct _EmpathyAccountSettingsPriv
   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;
 };
 
@@ -243,6 +249,7 @@ empathy_account_settings_constructed (GObject *object)
       GQuark features[] = {
           TP_ACCOUNT_FEATURE_CORE,
           TP_ACCOUNT_FEATURE_STORAGE,
+          TP_ACCOUNT_FEATURE_ADDRESSING,
           0 };
 
       if (priv->account != NULL)
@@ -331,7 +338,7 @@ empathy_account_settings_class_init (
       g_signal_new ("password-retrieved",
           G_TYPE_FROM_CLASS (empathy_account_settings_class),
           G_SIGNAL_RUN_LAST, 0, NULL, NULL,
-          g_cclosure_marshal_VOID__VOID,
+          g_cclosure_marshal_generic,
           G_TYPE_NONE, 0);
 }
 
@@ -397,11 +404,11 @@ empathy_account_settings_finalize (GObject *object)
       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);
 }
@@ -434,7 +441,7 @@ empathy_account_settings_get_password_cb (GObject *source,
   const gchar *password;
   GError *error = NULL;
 
-  password = empathy_keyring_get_password_finish (TP_ACCOUNT (source),
+  password = empathy_keyring_get_account_password_finish (TP_ACCOUNT (source),
       result, &error);
 
   if (error != NULL)
@@ -467,7 +474,7 @@ empathy_account_settings_migrate_password_cb (GObject *source,
   GHashTable *empty;
   const gchar *unset[] = { "password", NULL };
 
-  if (!empathy_keyring_set_password_finish (account, result, &error))
+  if (!empathy_keyring_set_account_password_finish (account, result, &error))
     {
       DEBUG ("Failed to set password: %s", error->message);
       g_clear_error (&error);
@@ -513,7 +520,7 @@ empathy_account_settings_try_migrating_password (EmpathyAccountSettings *self)
   if (EMP_STR_EMPTY (password))
     return;
 
-  empathy_keyring_set_password_async (priv->account, password,
+  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
@@ -561,6 +568,8 @@ empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
       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,
@@ -568,7 +577,7 @@ empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
 
   if (tp_protocol == NULL)
     {
-      priv->manager = NULL;
+      tp_clear_object (&priv->manager);
       return;
     }
 
@@ -624,7 +633,7 @@ empathy_account_settings_check_readyness (EmpathyAccountSettings *self)
 
       /* Make this call but don't block on its readiness. We'll signal
        * if it's updated later with ::password-retrieved. */
-      empathy_keyring_get_password_async (priv->account,
+      empathy_keyring_get_account_password_async (priv->account,
           empathy_account_settings_get_password_cb, self);
     }
 
@@ -736,6 +745,21 @@ empathy_account_settings_get_service (EmpathyAccountSettings *settings)
   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)
 {
@@ -798,6 +822,13 @@ empathy_account_settings_get_tp_param (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)
@@ -912,6 +943,11 @@ empathy_account_settings_discard_changes (EmpathyAccountSettings *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 *
@@ -1401,7 +1437,7 @@ empathy_account_settings_set_password_cb (GObject *source,
     gpointer user_data)
 {
   empathy_account_settings_processed_password (source, result, user_data,
-      empathy_keyring_set_password_finish);
+      empathy_keyring_set_account_password_finish);
 }
 
 static void
@@ -1410,7 +1446,46 @@ empathy_account_settings_delete_password_cb (GObject *source,
     gpointer user_data)
 {
   empathy_account_settings_processed_password (source, result, user_data,
-      empathy_keyring_delete_password_finish);
+      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
@@ -1422,9 +1497,10 @@ empathy_account_settings_account_updated (GObject *source,
   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);
@@ -1437,18 +1513,27 @@ empathy_account_settings_account_updated (GObject *source,
     {
       if (priv->password != NULL)
         {
-          empathy_keyring_set_password_async (priv->account, priv->password,
+          /* 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_password_async (priv->account,
+          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);
 
@@ -1457,6 +1542,7 @@ out:
 
   g_simple_async_result_complete (r);
   g_object_unref (r);
+  g_strfreev (reconnect_required);
 }
 
 static void
@@ -1480,6 +1566,21 @@ empathy_account_settings_created_cb (GObject *source,
   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);
     }
 
@@ -1560,7 +1661,7 @@ empathy_account_settings_manager_ready_cb (GObject *source_object,
   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);
@@ -1589,9 +1690,13 @@ empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
   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
@@ -1605,6 +1710,7 @@ empathy_account_settings_apply_async (EmpathyAccountSettings *settings,
 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),
@@ -1614,6 +1720,10 @@ empathy_account_settings_apply_finish (EmpathyAccountSettings *settings,
   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;
 }
 
@@ -1646,8 +1756,16 @@ empathy_account_settings_set_regex (EmpathyAccountSettings *settings,
 {
   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);
 }
 
@@ -1740,3 +1858,30 @@ empathy_account_settings_supports_sasl (EmpathyAccountSettings *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;
+}