[libsecret/wip/dueno/simple-item: 3/4] secret-item: Implement SecretRetrievableInterface



commit db4f22dec3a92ed7ea890de736a60da73678f4c2
Author: Daiki Ueno <dueno src gnome org>
Date:   Mon Jun 17 15:30:00 2019 +0200

    secret-item: Implement SecretRetrievableInterface

 libsecret/secret-item.c | 67 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 58 insertions(+), 9 deletions(-)
---
diff --git a/libsecret/secret-item.c b/libsecret/secret-item.c
index d447bcd..36838d0 100644
--- a/libsecret/secret-item.c
+++ b/libsecret/secret-item.c
@@ -19,6 +19,7 @@
 #include "secret-item.h"
 #include "secret-paths.h"
 #include "secret-private.h"
+#include "secret-retrievable.h"
 #include "secret-service.h"
 #include "secret-types.h"
 #include "secret-value.h"
@@ -105,16 +106,21 @@ struct _SecretItemPrivate {
        gint disposed;
 };
 
+static SecretRetrievableInterface *secret_item_retrievable_parent_iface = NULL;
+
 static GInitableIface *secret_item_initable_parent_iface = NULL;
 
 static GAsyncInitableIface *secret_item_async_initable_parent_iface = NULL;
 
+static void   secret_item_retrievable_iface      (SecretRetrievableInterface *iface);
+
 static void   secret_item_initable_iface         (GInitableIface *iface);
 
 static void   secret_item_async_initable_iface   (GAsyncInitableIface *iface);
 
 G_DEFINE_TYPE_WITH_CODE (SecretItem, secret_item, G_TYPE_DBUS_PROXY,
                          G_ADD_PRIVATE (SecretItem)
+                        G_IMPLEMENT_INTERFACE (SECRET_TYPE_RETRIEVABLE, secret_item_retrievable_iface);
                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, secret_item_initable_iface);
                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, secret_item_async_initable_iface);
 );
@@ -355,9 +361,7 @@ secret_item_class_init (SecretItemClass *klass)
         * The attributes set on this item. Attributes are used to locate an
         * item. They are not guaranteed to be stored or transferred securely.
         */
-       g_object_class_install_property (gobject_class, PROP_ATTRIBUTES,
-                    g_param_spec_boxed ("attributes", "Attributes", "Item attributes",
-                                        G_TYPE_HASH_TABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+       g_object_class_override_property (gobject_class, PROP_ATTRIBUTES, "attributes");
 
        /**
         * SecretItem:label:
@@ -391,9 +395,7 @@ secret_item_class_init (SecretItemClass *klass)
         * The date and time (in seconds since the UNIX epoch) that this
         * item was created.
         */
-       g_object_class_install_property (gobject_class, PROP_CREATED,
-                   g_param_spec_uint64 ("created", "Created", "Item creation date",
-                                        0UL, G_MAXUINT64, 0UL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+       g_object_class_override_property (gobject_class, PROP_CREATED, "created");
 
        /**
         * SecretItem:modified:
@@ -401,9 +403,7 @@ secret_item_class_init (SecretItemClass *klass)
         * The date and time (in seconds since the UNIX epoch) that this
         * item was last modified.
         */
-       g_object_class_install_property (gobject_class, PROP_MODIFIED,
-                   g_param_spec_uint64 ("modified", "Modified", "Item modified date",
-                                        0UL, G_MAXUINT64, 0UL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+       g_object_class_override_property (gobject_class, PROP_MODIFIED, "modified");
 }
 
 typedef struct {
@@ -1320,6 +1320,55 @@ secret_item_load_secret_sync (SecretItem *self,
        return result;
 }
 
+static void
+on_retrieve_load (GObject *source_object,
+                 GAsyncResult *res,
+                 gpointer user_data)
+{
+       SecretItem *self = SECRET_ITEM (source_object);
+       GTask *task = G_TASK (user_data);
+       GError *error = NULL;
+
+       if (secret_item_load_secret_finish (self, res, &error)) {
+               g_task_return_pointer (task,
+                                      secret_item_get_secret (self),
+                                      secret_value_unref);
+               g_object_unref (task);
+       } else {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+       }
+}
+
+static void
+secret_item_retrieve_secret (SecretItem *self,
+                            GCancellable *cancellable,
+                            GAsyncReadyCallback callback,
+                            gpointer user_data)
+{
+       GTask *task = g_task_new (self, cancellable, callback, user_data);
+
+       secret_item_load_secret (self, cancellable, on_retrieve_load, task);
+}
+
+static SecretValue *
+secret_item_retrieve_secret_finish (SecretItem *self,
+                                   GAsyncResult *result,
+                                   GError **error)
+{
+       g_return_val_if_fail (g_task_is_valid (result, self), NULL);
+
+       return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+static void
+secret_item_retrievable_iface (SecretRetrievableInterface *iface)
+{
+       secret_item_retrievable_parent_iface = g_type_interface_peek_parent (iface);
+       iface->retrieve_secret = secret_item_retrieve_secret;
+       iface->retrieve_secret_finish = secret_item_retrieve_secret_finish;
+}
+
 typedef struct {
        SecretService *service;
        GCancellable *cancellable;


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