[seahorse/wip/nielsdg/gtask] gpg: key-op: Use GTask for key creation



commit 36f233b6865e26ef1c4953835e72f010a5bcc4d5
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Wed Jan 30 13:51:54 2019 +0100

    gpg: key-op: Use GTask for key creation

 pgp/seahorse-gpgme-key-op.c | 85 ++++++++++++++-------------------------------
 1 file changed, 27 insertions(+), 58 deletions(-)
---
diff --git a/pgp/seahorse-gpgme-key-op.c b/pgp/seahorse-gpgme-key-op.c
index 3f81850d..ab7b0630 100644
--- a/pgp/seahorse-gpgme-key-op.c
+++ b/pgp/seahorse-gpgme-key-op.c
@@ -55,21 +55,6 @@
 #define GPG_FULL        4
 #define GPG_ULTIMATE    5
 
-typedef struct {
-       GCancellable *cancellable;
-       gpgme_ctx_t gctx;
-} key_op_generate_closure;
-
-static void
-key_op_generate_free (gpointer data)
-{
-       key_op_generate_closure *closure = data;
-       g_clear_object (&closure->cancellable);
-       if (closure->gctx)
-               gpgme_release (closure->gctx);
-       g_free (closure);
-}
-
 static void
 on_key_op_generate_progress (void *opaque,
                              const char *what,
@@ -77,25 +62,24 @@ on_key_op_generate_progress (void *opaque,
                              int current,
                              int total)
 {
-       GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (opaque);
-       key_op_generate_closure *closure = g_simple_async_result_get_op_res_gpointer (res);
-       seahorse_progress_update (closure->cancellable, res, "%s", what);
+       GTask *task = G_TASK (opaque);
+       seahorse_progress_update (g_task_get_cancellable (task), task, "%s", what);
 }
 
 static gboolean
 on_key_op_generate_complete (gpgme_error_t gerr,
                              gpointer user_data)
 {
-       GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
-       key_op_generate_closure *closure = g_simple_async_result_get_op_res_gpointer (res);
-       GError *error = NULL;
+       GTask *task = G_TASK (user_data);
+       g_autoptr(GError) error = NULL;
 
        if (seahorse_gpgme_propagate_error (gerr, &error)) {
-               g_simple_async_result_take_error (res, error);
+               g_task_return_error (task, g_steal_pointer (&error));
+        return FALSE; /* don't call again */
        }
 
-       seahorse_progress_end (closure->cancellable, res);
-       g_simple_async_result_complete (res);
+       seahorse_progress_end (g_task_get_cancellable (task), task);
+       g_task_return_boolean (task, TRUE);
        return FALSE; /* don't call again */
 }
 
@@ -126,13 +110,14 @@ seahorse_gpgme_key_op_generate_async (SeahorseGpgmeKeyring *keyring,
                                       GAsyncReadyCallback callback,
                                       gpointer user_data)
 {
-       gchar *common, *key_type, *start, *expires_date;
-       key_op_generate_closure *closure;
-       GSimpleAsyncResult *res;
-       GError *error = NULL;
+    const gchar* key_type;
+       g_autofree gchar *common = NULL, *start = NULL, *expires_date = NULL;
+       gpgme_ctx_t gctx;
+       g_autoptr(GTask) task = NULL;
+       g_autoptr(GError) error = NULL;
        const gchar *parms;
        gpgme_error_t gerr = 0;
-       GSource *gsource;
+       g_autoptr(GSource) gsource = NULL;
 
        g_return_if_fail (SEAHORSE_IS_GPGME_KEYRING (keyring));
        g_return_if_fail (name);
@@ -186,38 +171,26 @@ seahorse_gpgme_key_op_generate_async (SeahorseGpgmeKeyring *keyring,
        else
                parms = g_strdup_printf ("%s%d\n%s", start, length, common);
 
-       res = g_simple_async_result_new (G_OBJECT (keyring), callback, user_data,
-                                        seahorse_gpgme_key_op_generate_async);
+       gctx = seahorse_gpgme_keyring_new_context (&gerr);
 
-       closure = g_new0 (key_op_generate_closure, 1);
-       closure->gctx = seahorse_gpgme_keyring_new_context (&gerr);
-       closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
-       gpgme_set_progress_cb (closure->gctx, on_key_op_generate_progress, res);
-       g_simple_async_result_set_op_res_gpointer (res, closure, key_op_generate_free);
+       task = g_task_new (keyring, cancellable, callback, user_data);
+       gpgme_set_progress_cb (gctx, on_key_op_generate_progress, task);
+       g_task_set_task_data (task, gctx, (GDestroyNotify) gpgme_release);
 
-       seahorse_progress_prep_and_begin (cancellable, res, NULL);
-       gsource = seahorse_gpgme_gsource_new (closure->gctx, cancellable);
+       seahorse_progress_prep_and_begin (cancellable, task, NULL);
+       gsource = seahorse_gpgme_gsource_new (gctx, cancellable);
        g_source_set_callback (gsource, (GSourceFunc)on_key_op_generate_complete,
-                              g_object_ref (res), g_object_unref);
+                              g_object_ref (task), g_object_unref);
 
        if (gerr == 0)
-               gerr = gpgme_op_genkey_start (closure->gctx, parms, NULL, NULL);
+               gerr = gpgme_op_genkey_start (gctx, parms, NULL, NULL);
 
        if (seahorse_gpgme_propagate_error (gerr, &error)) {
-               g_simple_async_result_take_error (res, error);
-               g_simple_async_result_complete_in_idle (res);
-       } else {
-               g_source_attach (gsource, g_main_context_default ());
+               g_task_return_error (task, g_steal_pointer (&error));
+        return;
        }
 
-       g_source_unref (gsource);
-
-       /* Free xmls */
-       g_free (start);
-       g_free (common);
-       g_free (expires_date);
-
-       g_object_unref (res);
+       g_source_attach (gsource, g_main_context_default ());
 }
 
 gboolean
@@ -225,13 +198,9 @@ seahorse_gpgme_key_op_generate_finish (SeahorseGpgmeKeyring *keyring,
                                        GAsyncResult *result,
                                        GError **error)
 {
-       g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (keyring),
-                             seahorse_gpgme_key_op_generate_async), FALSE);
-
-       if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
-               return FALSE;
+       g_return_val_if_fail (g_task_is_valid (result, keyring), FALSE);
 
-       return TRUE;
+       return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 /* helper function for deleting @skey */


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