[gnome-online-accounts] Don't export the methods dealing with the keyring as public API



commit ace3e9f480f89dea89f03cd8d5ebc9e282e82f99
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Jun 19 20:43:30 2012 +0200

    Don't export the methods dealing with the keyring as public API
    
    These are only meant to be used from within the backend and the
    daemon.

 doc/goa-sections.txt                 |    2 -
 src/daemon/Makefile.am               |    1 +
 src/daemon/goadaemon.c               |    3 +-
 src/goabackend/goaexchangeprovider.c |   36 +++---
 src/goabackend/goaoauth2provider.c   |   38 +++---
 src/goabackend/goaoauthprovider.c    |   38 +++---
 src/goabackend/goaprovider.c         |  241 ----------------------------------
 src/goabackend/goaprovider.h         |   13 --
 src/goabackend/goautils.c            |  182 +++++++++++++++++++++++++
 src/goabackend/goautils.h            |   16 +++
 10 files changed, 257 insertions(+), 313 deletions(-)
---
diff --git a/doc/goa-sections.txt b/doc/goa-sections.txt
index 33a7a80..1f92e58 100644
--- a/doc/goa-sections.txt
+++ b/doc/goa-sections.txt
@@ -394,8 +394,6 @@ goa_provider_build_object
 goa_provider_add_account
 goa_provider_refresh_account
 goa_provider_show_account
-goa_provider_store_credentials_sync
-goa_provider_lookup_credentials_sync
 goa_provider_ensure_credentials
 goa_provider_ensure_credentials_finish
 goa_provider_ensure_credentials_sync
diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am
index 524c667..236502b 100644
--- a/src/daemon/Makefile.am
+++ b/src/daemon/Makefile.am
@@ -27,6 +27,7 @@ goa_daemon_SOURCES = 						\
 
 goa_daemon_CPPFLAGS =						\
 	-DG_LOG_DOMAIN=\"goa-daemon\"				\
+	-DGOA_BACKEND_COMPILATION				\
 	$(NULL)
 
 goa_daemon_CFLAGS =						\
diff --git a/src/daemon/goadaemon.c b/src/daemon/goadaemon.c
index cf48ae1..6975029 100644
--- a/src/daemon/goadaemon.c
+++ b/src/daemon/goadaemon.c
@@ -29,6 +29,7 @@
 
 #include "goadaemon.h"
 #include "goabackend/goabackend.h"
+#include "goabackend/goautils.h"
 
 struct _GoaDaemon
 {
@@ -1031,7 +1032,7 @@ on_account_handle_remove (GoaAccount            *account,
     }
 
   error = NULL;
-  if (!goa_provider_delete_credentials_sync (provider, account, NULL, &error))
+  if (!goa_utils_delete_credentials_sync (provider, account, NULL, &error))
     {
       g_dbus_method_invocation_return_gerror (invocation, error);
       goto out;
diff --git a/src/goabackend/goaexchangeprovider.c b/src/goabackend/goaexchangeprovider.c
index d53ebe5..963d0e8 100644
--- a/src/goabackend/goaexchangeprovider.c
+++ b/src/goabackend/goaexchangeprovider.c
@@ -265,10 +265,10 @@ ensure_credentials_sync (GoaProvider         *provider,
 
   ret = FALSE;
 
-  credentials = goa_provider_lookup_credentials_sync (provider,
-                                                      object,
-                                                      cancellable,
-                                                      error);
+  credentials = goa_utils_lookup_credentials_sync (provider,
+                                                   object,
+                                                   cancellable,
+                                                   error);
   if (credentials == NULL)
     {
       if (error != NULL)
@@ -678,11 +678,11 @@ add_account (GoaProvider    *provider,
   g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
   g_variant_builder_add (&builder, "{sv}", "password", g_variant_new_string (password));
 
-  if (!goa_provider_store_credentials_sync (provider,
-                                            ret,
-                                            g_variant_builder_end (&builder),
-                                            NULL, /* GCancellable */
-                                            &data.error))
+  if (!goa_utils_store_credentials_sync (provider,
+                                         ret,
+                                         g_variant_builder_end (&builder),
+                                         NULL, /* GCancellable */
+                                         &data.error))
     goto out;
 
  out:
@@ -815,11 +815,11 @@ refresh_account (GoaProvider    *provider,
   g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
   g_variant_builder_add (&builder, "{sv}", "password", g_variant_new_string (password));
 
-  if (!goa_provider_store_credentials_sync (provider,
-                                            object,
-                                            g_variant_builder_end (&builder),
-                                            NULL, /* GCancellable */
-                                            error))
+  if (!goa_utils_store_credentials_sync (provider,
+                                         object,
+                                         g_variant_builder_end (&builder),
+                                         NULL, /* GCancellable */
+                                         error))
     goto out;
 
   goa_account_call_ensure_credentials (account,
@@ -918,10 +918,10 @@ on_handle_get_password (GoaPasswordBased      *interface,
   provider = goa_provider_get_for_provider_type (goa_account_get_provider_type (account));
 
   error = NULL;
-  credentials = goa_provider_lookup_credentials_sync (provider,
-                                                      object,
-                                                      NULL, /* GCancellable* */
-                                                      &error);
+  credentials = goa_utils_lookup_credentials_sync (provider,
+                                                   object,
+                                                   NULL, /* GCancellable* */
+                                                   &error);
   if (credentials == NULL)
     {
       g_dbus_method_invocation_take_error (invocation, error);
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index fbed7a3..fbf998f 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -1108,11 +1108,11 @@ goa_oauth2_provider_add_account (GoaProvider *_provider,
   if (refresh_token != NULL)
     g_variant_builder_add (&builder, "{sv}", "refresh_token", g_variant_new_string (refresh_token));
   /* TODO: run in worker thread */
-  if (!goa_provider_store_credentials_sync (GOA_PROVIDER (provider),
-                                            ret,
-                                            g_variant_builder_end (&builder),
-                                            NULL, /* GCancellable */
-                                            &data.error))
+  if (!goa_utils_store_credentials_sync (GOA_PROVIDER (provider),
+                                         ret,
+                                         g_variant_builder_end (&builder),
+                                         NULL, /* GCancellable */
+                                         &data.error))
     goto out;
 
  out:
@@ -1209,11 +1209,11 @@ goa_oauth2_provider_refresh_account (GoaProvider  *_provider,
                            g_variant_new_int64 (duration_to_abs_usec (access_token_expires_in)));
   if (refresh_token != NULL)
     g_variant_builder_add (&builder, "{sv}", "refresh_token", g_variant_new_string (refresh_token));
-  if (!goa_provider_store_credentials_sync (GOA_PROVIDER (provider),
-                                            object,
-                                            g_variant_builder_end (&builder),
-                                            NULL, /* GCancellable */
-                                            error))
+  if (!goa_utils_store_credentials_sync (GOA_PROVIDER (provider),
+                                         object,
+                                         g_variant_builder_end (&builder),
+                                         NULL, /* GCancellable */
+                                         error))
     goto out;
 
   goa_account_call_ensure_credentials (goa_object_peek_account (object),
@@ -1325,10 +1325,10 @@ goa_oauth2_provider_get_access_token_sync (GoaOAuth2Provider  *provider,
   g_mutex_lock (lock);
 
   /* First, get the credentials from the keyring */
-  credentials = goa_provider_lookup_credentials_sync (GOA_PROVIDER (provider),
-                                                      object,
-                                                      cancellable,
-                                                      error);
+  credentials = goa_utils_lookup_credentials_sync (GOA_PROVIDER (provider),
+                                                   object,
+                                                   cancellable,
+                                                   error);
   if (credentials == NULL)
     {
       if (error != NULL)
@@ -1426,11 +1426,11 @@ goa_oauth2_provider_get_access_token_sync (GoaOAuth2Provider  *provider,
   if (refresh_token != NULL)
     g_variant_builder_add (&builder, "{sv}", "refresh_token", g_variant_new_string (refresh_token));
 
-  if (!goa_provider_store_credentials_sync (GOA_PROVIDER (provider),
-                                            object,
-                                            g_variant_builder_end (&builder),
-                                            cancellable,
-                                            error))
+  if (!goa_utils_store_credentials_sync (GOA_PROVIDER (provider),
+                                         object,
+                                         g_variant_builder_end (&builder),
+                                         cancellable,
+                                         error))
     {
       if (error != NULL)
         {
diff --git a/src/goabackend/goaoauthprovider.c b/src/goabackend/goaoauthprovider.c
index 5348a72..a33d6a2 100644
--- a/src/goabackend/goaoauthprovider.c
+++ b/src/goabackend/goaoauthprovider.c
@@ -1113,11 +1113,11 @@ goa_oauth_provider_add_account (GoaProvider *_provider,
     g_variant_builder_add (&builder, "{sv}", "session_handle_expires_at",
                            g_variant_new_int64 (duration_to_abs_usec (session_handle_expires_in)));
   /* TODO: run in worker thread */
-  if (!goa_provider_store_credentials_sync (GOA_PROVIDER (provider),
-                                            ret,
-                                            g_variant_builder_end (&builder),
-                                            NULL, /* GCancellable */
-                                            &data.error))
+  if (!goa_utils_store_credentials_sync (GOA_PROVIDER (provider),
+                                         ret,
+                                         g_variant_builder_end (&builder),
+                                         NULL, /* GCancellable */
+                                         &data.error))
     goto out;
 
  out:
@@ -1219,11 +1219,11 @@ goa_oauth_provider_refresh_account (GoaProvider  *_provider,
     g_variant_builder_add (&builder, "{sv}", "session_handle_expires_at",
                            g_variant_new_int64 (duration_to_abs_usec (session_handle_expires_in)));
   /* TODO: run in worker thread */
-  if (!goa_provider_store_credentials_sync (GOA_PROVIDER (provider),
-                                            object,
-                                            g_variant_builder_end (&builder),
-                                            NULL, /* GCancellable */
-                                            error))
+  if (!goa_utils_store_credentials_sync (GOA_PROVIDER (provider),
+                                         object,
+                                         g_variant_builder_end (&builder),
+                                         NULL, /* GCancellable */
+                                         error))
     goto out;
 
   goa_account_call_ensure_credentials (goa_object_peek_account (object),
@@ -1343,10 +1343,10 @@ goa_oauth_provider_get_access_token_sync (GoaOAuthProvider   *provider,
   g_mutex_lock (lock);
 
   /* First, get the credentials from the keyring */
-  credentials = goa_provider_lookup_credentials_sync (GOA_PROVIDER (provider),
-                                                      object,
-                                                      cancellable,
-                                                      error);
+  credentials = goa_utils_lookup_credentials_sync (GOA_PROVIDER (provider),
+                                                   object,
+                                                   cancellable,
+                                                   error);
   if (credentials == NULL)
     {
       if (error != NULL)
@@ -1446,11 +1446,11 @@ goa_oauth_provider_get_access_token_sync (GoaOAuthProvider   *provider,
                            g_variant_new_int64 (duration_to_abs_usec (session_handle_expires_in)));
 
   /* TODO: run in worker thread */
-  if (!goa_provider_store_credentials_sync (GOA_PROVIDER (provider),
-                                            object,
-                                            g_variant_builder_end (&builder),
-                                            cancellable,
-                                            error))
+  if (!goa_utils_store_credentials_sync (GOA_PROVIDER (provider),
+                                         object,
+                                         g_variant_builder_end (&builder),
+                                         cancellable,
+                                         error))
     {
       if (error != NULL)
         {
diff --git a/src/goabackend/goaprovider.c b/src/goabackend/goaprovider.c
index 26f4853..3538e80 100644
--- a/src/goabackend/goaprovider.c
+++ b/src/goabackend/goaprovider.c
@@ -24,7 +24,6 @@
 #include "config.h"
 
 #include <glib/gi18n-lib.h>
-#include <gnome-keyring.h>
 
 #include "goalogging.h"
 #include "goaprovider.h"
@@ -694,246 +693,6 @@ goa_provider_get_all (void)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-static const GnomeKeyringPasswordSchema keyring_password_schema =
-{
-  GNOME_KEYRING_ITEM_GENERIC_SECRET,
-  {
-    { "goa-identity", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
-    { NULL, 0 }
-  }
-};
-
-/**
- * goa_provider_store_credentials_sync:
- * @provider: A #GoaProvider.
- * @object: The account to store credentials for.
- * @credentials: The credentials to store.
- * @cancellable: (allow-none): A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
- *
- * Stores @credentials for @identity in the key-ring. If @credentials
- * is floating, it is consumed.
- *
- * The calling thread is blocked while waiting for a reply.
- *
- * This is a convenience method (not virtual) that subclasses can use.
- *
- * Returns: %TRUE if the credentials was successfully stored, %FALSE
- * if @error is set.
- */
-gboolean
-goa_provider_store_credentials_sync (GoaProvider   *provider,
-                                     GoaObject     *object,
-                                     GVariant      *credentials,
-                                     GCancellable  *cancellable,
-                                     GError       **error)
-{
-  gboolean ret;
-  gchar *credentials_str;
-  gchar *password_description;
-  gchar *password_key;
-  GnomeKeyringResult result;
-  const gchar *identity;
-
-  g_return_val_if_fail (GOA_IS_PROVIDER (provider), FALSE);
-  g_return_val_if_fail (GOA_IS_OBJECT (object) && goa_object_peek_account (object) != NULL, FALSE);
-  g_return_val_if_fail (credentials != NULL, FALSE);
-  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
-  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
-  /* TODO: use GCancellable */
-  ret = FALSE;
-
-  identity = goa_account_get_id (goa_object_peek_account (object));
-
-  credentials_str = g_variant_print (credentials, TRUE);
-  g_variant_ref_sink (credentials);
-  g_variant_unref (credentials);
-
-  password_key = g_strdup_printf ("%s:gen%d:%s",
-                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
-                                  goa_provider_get_credentials_generation (GOA_PROVIDER (provider)),
-                                  identity);
-  /* Translators: The %s is the type of the provider, e.g. 'google' or 'yahoo' */
-  password_description = g_strdup_printf (_("GOA %s credentials for identity %s"),
-                                          goa_provider_get_provider_type (GOA_PROVIDER (provider)),
-                                          identity);
-  result = gnome_keyring_store_password_sync (&keyring_password_schema,
-                                              NULL, /* default keyring */
-                                              password_description,
-                                              credentials_str,
-                                              "goa-identity", password_key,
-                                              NULL);
-  if (result != GNOME_KEYRING_RESULT_OK)
-    {
-      g_set_error (error,
-                   GOA_ERROR,
-                   GOA_ERROR_FAILED, /* TODO: more specific */
-                   _("Failed to store credentials in the keyring: %s"),
-                   gnome_keyring_result_to_message (result));
-      goto out;
-    }
-
-  ret = TRUE;
-
- out:
-  g_free (credentials_str);
-  g_free (password_key);
-  g_free (password_description);
-  return ret;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
-
-/**
- * goa_provider_lookup_credentials_sync:
- * @provider: A #GoaProvider.
- * @object: The account to store credentials for.
- * @cancellable: (allow-none): A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
- *
- * Looks up credentials in the keyring for @identity previously stored
- * with goa_provider_store_credentials().
- *
- * The calling thread is blocked while waiting for a reply.
- *
- * This is a convenience method (not virtual) that subclasses can use.
- *
- * Returns: (transfer full): A #GVariant (never floating)
- * with credentials or %NULL if @error is set. Free with
- * g_variant_unref().
- */
-GVariant *
-goa_provider_lookup_credentials_sync (GoaProvider   *provider,
-                                      GoaObject     *object,
-                                      GCancellable  *cancellable,
-                                      GError       **error)
-{
-  gchar *password_key;
-  GVariant *ret;
-  GnomeKeyringResult result;
-  gchar *returned_password;
-  const gchar *identity;
-
-  g_return_val_if_fail (GOA_IS_PROVIDER (provider), NULL);
-  g_return_val_if_fail (GOA_IS_OBJECT (object) && goa_object_peek_account (object) != NULL, FALSE);
-  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
-  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
-
-  ret = NULL;
-  password_key = NULL;
-  returned_password = NULL;
-
-  identity = goa_account_get_id (goa_object_peek_account (object));
-
-  password_key = g_strdup_printf ("%s:gen%d:%s",
-                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
-                                  goa_provider_get_credentials_generation (GOA_PROVIDER (provider)),
-                                  identity);
-
-  result = gnome_keyring_find_password_sync (&keyring_password_schema,
-                                             &returned_password,
-                                             "goa-identity", password_key,
-                                             NULL);
-  if (result != GNOME_KEYRING_RESULT_OK)
-    {
-      g_set_error (error,
-                   GOA_ERROR,
-                   GOA_ERROR_FAILED, /* TODO: more specific */
-                   _("Failed to retrieve credentials from the keyring: %s"),
-                   gnome_keyring_result_to_message (result));
-      goto out;
-    }
-
-  ret = g_variant_parse (NULL, /* GVariantType */
-                         returned_password,
-                         NULL, /* limit */
-                         NULL, /* endptr */
-                         error);
-  if (ret == NULL)
-    {
-      g_prefix_error (error, _("Error parsing result obtained from the keyring: "));
-      goto out;
-    }
-
-  if (g_variant_is_floating (ret))
-    g_variant_ref_sink (ret);
-
- out:
-  gnome_keyring_free_password (returned_password);
-  g_free (password_key);
-  return ret;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
-
-/**
- * goa_provider_delete_credentials_sync:
- * @provider: A #GoaProvider.
- * @object: The #GoaAccount to delete credentials for.
- * @cancellable: (allow-none): A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
- *
- * Deletes the credentials in the keyring for @object previously stored
- * with goa_provider_store_credentials_sync().
- *
- * The calling thread is blocked while waiting for a reply.
- *
- * This is a convenience method (not virtual) that subclasses can use.
- *
- * Returns: %TRUE if the credentials was successfully deleted, %FALSE
- * if @error is set.
- */
-gboolean
-goa_provider_delete_credentials_sync (GoaProvider   *provider,
-                                      GoaAccount    *object,
-                                      GCancellable  *cancellable,
-                                      GError       **error)
-{
-  gboolean ret;
-  gchar *password_key;
-  GnomeKeyringResult result;
-  const gchar *identity;
-
-  g_return_val_if_fail (GOA_IS_PROVIDER (provider), FALSE);
-  g_return_val_if_fail (GOA_IS_ACCOUNT (object), FALSE);
-  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
-  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
-  /* TODO: use GCancellable */
-  ret = FALSE;
-
-  password_key = NULL;
-
-  identity = goa_account_get_id (object);
-
-  password_key = g_strdup_printf ("%s:gen%d:%s",
-                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
-                                  goa_provider_get_credentials_generation (GOA_PROVIDER (provider)),
-                                  identity);
-
-  result = gnome_keyring_delete_password_sync (&keyring_password_schema,
-                                               "goa-identity", password_key,
-                                               NULL);
-  if (result != GNOME_KEYRING_RESULT_OK)
-    {
-      g_set_error (error,
-                   GOA_ERROR,
-                   GOA_ERROR_FAILED, /* TODO: more specific */
-                   _("Failed to delete credentials from the keyring: %s"),
-                   gnome_keyring_result_to_message (result));
-      goto out;
-    }
-
-  ret = TRUE;
-
- out:
-  g_free (password_key);
-  return ret;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
-
 /**
  * SECTION:goautil
  * @title: Utilities
diff --git a/src/goabackend/goaprovider.h b/src/goabackend/goaprovider.h
index 06fcadb..b5e186e 100644
--- a/src/goabackend/goaprovider.h
+++ b/src/goabackend/goaprovider.h
@@ -143,19 +143,6 @@ gboolean     goa_provider_build_object              (GoaProvider         *provid
                                                      const gchar         *group,
                                                      gboolean             just_added,
                                                      GError             **error);
-gboolean     goa_provider_store_credentials_sync    (GoaProvider         *provider,
-                                                     GoaObject           *object,
-                                                     GVariant            *credentials,
-                                                     GCancellable        *cancellable,
-                                                     GError             **error);
-GVariant    *goa_provider_lookup_credentials_sync   (GoaProvider         *provider,
-                                                     GoaObject           *object,
-                                                     GCancellable        *cancellable,
-                                                     GError             **error);
-gboolean     goa_provider_delete_credentials_sync   (GoaProvider         *provider,
-                                                     GoaAccount          *account,
-                                                     GCancellable        *cancellable,
-                                                     GError             **error);
 void         goa_provider_ensure_credentials        (GoaProvider         *provider,
                                                      GoaObject           *object,
                                                      GCancellable        *cancellable,
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
index 0be7fba..9d00130 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -23,10 +23,20 @@
 #include "config.h"
 
 #include <glib/gi18n-lib.h>
+#include <gnome-keyring.h>
 
 #include "goaprovider.h"
 #include "goautils.h"
 
+static const GnomeKeyringPasswordSchema keyring_password_schema =
+{
+  GNOME_KEYRING_ITEM_GENERIC_SECRET,
+  {
+    { "goa-identity", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
+    { NULL, 0 }
+  }
+};
+
 gboolean
 goa_utils_check_duplicate (GoaClient              *client,
                            const gchar            *identity,
@@ -104,3 +114,175 @@ goa_utils_create_add_refresh_label (GoaProvider *provider, gboolean add_account)
 
   return label;
 }
+
+gboolean
+goa_utils_delete_credentials_sync (GoaProvider   *provider,
+                                   GoaAccount    *object,
+                                   GCancellable  *cancellable,
+                                   GError       **error)
+{
+  gboolean ret;
+  gchar *password_key;
+  GnomeKeyringResult result;
+  const gchar *identity;
+
+  g_return_val_if_fail (GOA_IS_PROVIDER (provider), FALSE);
+  g_return_val_if_fail (GOA_IS_ACCOUNT (object), FALSE);
+  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  /* TODO: use GCancellable */
+  ret = FALSE;
+
+  password_key = NULL;
+
+  identity = goa_account_get_id (object);
+
+  password_key = g_strdup_printf ("%s:gen%d:%s",
+                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
+                                  goa_provider_get_credentials_generation (GOA_PROVIDER (provider)),
+                                  identity);
+
+  result = gnome_keyring_delete_password_sync (&keyring_password_schema,
+                                               "goa-identity", password_key,
+                                               NULL);
+  if (result != GNOME_KEYRING_RESULT_OK)
+    {
+      g_set_error (error,
+                   GOA_ERROR,
+                   GOA_ERROR_FAILED, /* TODO: more specific */
+                   _("Failed to delete credentials from the keyring: %s"),
+                   gnome_keyring_result_to_message (result));
+      goto out;
+    }
+
+  ret = TRUE;
+
+ out:
+  g_free (password_key);
+  return ret;
+}
+
+GVariant *
+goa_utils_lookup_credentials_sync (GoaProvider   *provider,
+                                   GoaObject     *object,
+                                   GCancellable  *cancellable,
+                                   GError       **error)
+{
+  gchar *password_key;
+  GVariant *ret;
+  GnomeKeyringResult result;
+  gchar *returned_password;
+  const gchar *identity;
+
+  g_return_val_if_fail (GOA_IS_PROVIDER (provider), NULL);
+  g_return_val_if_fail (GOA_IS_OBJECT (object) && goa_object_peek_account (object) != NULL, FALSE);
+  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  ret = NULL;
+  password_key = NULL;
+  returned_password = NULL;
+
+  identity = goa_account_get_id (goa_object_peek_account (object));
+
+  password_key = g_strdup_printf ("%s:gen%d:%s",
+                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
+                                  goa_provider_get_credentials_generation (GOA_PROVIDER (provider)),
+                                  identity);
+
+  result = gnome_keyring_find_password_sync (&keyring_password_schema,
+                                             &returned_password,
+                                             "goa-identity", password_key,
+                                             NULL);
+  if (result != GNOME_KEYRING_RESULT_OK)
+    {
+      g_set_error (error,
+                   GOA_ERROR,
+                   GOA_ERROR_FAILED, /* TODO: more specific */
+                   _("Failed to retrieve credentials from the keyring: %s"),
+                   gnome_keyring_result_to_message (result));
+      goto out;
+    }
+
+  ret = g_variant_parse (NULL, /* GVariantType */
+                         returned_password,
+                         NULL, /* limit */
+                         NULL, /* endptr */
+                         error);
+  if (ret == NULL)
+    {
+      g_prefix_error (error, _("Error parsing result obtained from the keyring: "));
+      goto out;
+    }
+
+  if (g_variant_is_floating (ret))
+    g_variant_ref_sink (ret);
+
+ out:
+  gnome_keyring_free_password (returned_password);
+  g_free (password_key);
+  return ret;
+}
+
+gboolean
+goa_utils_store_credentials_sync (GoaProvider   *provider,
+                                  GoaObject     *object,
+                                  GVariant      *credentials,
+                                  GCancellable  *cancellable,
+                                  GError       **error)
+{
+  gboolean ret;
+  gchar *credentials_str;
+  gchar *password_description;
+  gchar *password_key;
+  GnomeKeyringResult result;
+  const gchar *identity;
+
+  g_return_val_if_fail (GOA_IS_PROVIDER (provider), FALSE);
+  g_return_val_if_fail (GOA_IS_OBJECT (object) && goa_object_peek_account (object) != NULL, FALSE);
+  g_return_val_if_fail (credentials != NULL, FALSE);
+  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  /* TODO: use GCancellable */
+  ret = FALSE;
+
+  identity = goa_account_get_id (goa_object_peek_account (object));
+
+  credentials_str = g_variant_print (credentials, TRUE);
+  g_variant_ref_sink (credentials);
+  g_variant_unref (credentials);
+
+  password_key = g_strdup_printf ("%s:gen%d:%s",
+                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
+                                  goa_provider_get_credentials_generation (GOA_PROVIDER (provider)),
+                                  identity);
+  /* Translators: The %s is the type of the provider, e.g. 'google' or 'yahoo' */
+  password_description = g_strdup_printf (_("GOA %s credentials for identity %s"),
+                                          goa_provider_get_provider_type (GOA_PROVIDER (provider)),
+                                          identity);
+  result = gnome_keyring_store_password_sync (&keyring_password_schema,
+                                              NULL, /* default keyring */
+                                              password_description,
+                                              credentials_str,
+                                              "goa-identity", password_key,
+                                              NULL);
+  if (result != GNOME_KEYRING_RESULT_OK)
+    {
+      g_set_error (error,
+                   GOA_ERROR,
+                   GOA_ERROR_FAILED, /* TODO: more specific */
+                   _("Failed to store credentials in the keyring: %s"),
+                   gnome_keyring_result_to_message (result));
+      goto out;
+    }
+
+  ret = TRUE;
+
+ out:
+  g_free (credentials_str);
+  g_free (password_key);
+  g_free (password_description);
+  return ret;
+}
diff --git a/src/goabackend/goautils.h b/src/goabackend/goautils.h
index b306ecd..343c8f4 100644
--- a/src/goabackend/goautils.h
+++ b/src/goabackend/goautils.h
@@ -43,6 +43,22 @@ gboolean         goa_utils_check_duplicate (GoaClient              *client,
 
 GtkWidget       *goa_utils_create_add_refresh_label (GoaProvider *provider, gboolean add_account);
 
+gboolean         goa_utils_delete_credentials_sync (GoaProvider    *provider,
+                                                    GoaAccount     *account,
+                                                    GCancellable   *cancellable,
+                                                    GError        **error);
+
+GVariant        *goa_utils_lookup_credentials_sync (GoaProvider    *provider,
+                                                    GoaObject      *object,
+                                                    GCancellable   *cancellable,
+                                                    GError        **error);
+
+gboolean         goa_utils_store_credentials_sync (GoaProvider    *provider,
+                                                   GoaObject      *object,
+                                                   GVariant       *credentials,
+                                                   GCancellable   *cancellable,
+                                                   GError        **error);
+
 G_END_DECLS
 
 #endif /* __GOA_UTILS_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]