[seahorse/wip/nielsdg/gpgme-make-primary] gpgme: Use gpgme_set_uid_flag() to make primary UID



commit f440cff43640164b8b9d75d8930461876bbad603
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Mar 1 17:57:22 2019 +0100

    gpgme: Use gpgme_set_uid_flag() to make primary UID
    
    This gives us more specific erors if something goes wrong, and doesn't
    fall back on the suboptimal gpmg_op_edit workflow.

 pgp/seahorse-gpgme-key-op.c       | 96 +++++++++++++++++++++++++++++----------
 pgp/seahorse-gpgme-key-op.h       |  9 +++-
 pgp/seahorse-pgp-key-properties.c | 30 +++++++++---
 3 files changed, 104 insertions(+), 31 deletions(-)
---
diff --git a/pgp/seahorse-gpgme-key-op.c b/pgp/seahorse-gpgme-key-op.c
index 03ce3a99..6c7b96bd 100644
--- a/pgp/seahorse-gpgme-key-op.c
+++ b/pgp/seahorse-gpgme-key-op.c
@@ -1884,31 +1884,81 @@ primary_transit (guint current_state, gpgme_status_code_t status,
     
     return next_state;
 }
-                
-gpgme_error_t   
-seahorse_gpgme_key_op_primary_uid (SeahorseGpgmeUid *uid)
+
+static gboolean
+on_key_op_make_primary_complete (gpgme_error_t gerr,
+                                 gpointer user_data)
 {
-       PrimaryParm pri_parm;
-       SeahorseEditParm *parms;
-       gpgme_user_id_t userid;
-       gpgme_key_t key;
+    GTask *task = G_TASK (user_data);
+    SeahorseGpgmeUid *uid = SEAHORSE_GPGME_UID (g_task_get_source_object (task));
+    SeahorsePgpKey *parent_key = NULL;
+    g_autoptr(GError) error = NULL;
 
-       g_return_val_if_fail (SEAHORSE_GPGME_IS_UID (uid), GPG_E (GPG_ERR_WRONG_KEY_USAGE));
-    
-       /* Make sure not revoked */
-       userid = seahorse_gpgme_uid_get_userid (uid);
-       g_return_val_if_fail (userid != NULL && !userid->revoked && !userid->invalid, 
-                             GPG_E (GPG_ERR_INV_VALUE));
-    
-       key = seahorse_gpgme_uid_get_pubkey (uid);
-       g_return_val_if_fail (key, GPG_E (GPG_ERR_INV_VALUE));
-    
-       pri_parm.index = seahorse_gpgme_uid_get_actual_index (uid);
-   
-       parms = seahorse_edit_parm_new (PRIMARY_START, primary_action,
-                                       primary_transit, &pri_parm);
- 
-       return edit_refresh_gpgme_key (NULL, key, parms);
+    if (seahorse_gpgme_propagate_error (gerr, &error)) {
+        g_task_return_error (task, g_steal_pointer (&error));
+        return FALSE; /* don't call again */
+    }
+
+    parent_key = seahorse_pgp_uid_get_parent (SEAHORSE_PGP_UID (uid));
+    seahorse_gpgme_key_refresh (SEAHORSE_GPGME_KEY (parent_key));
+
+    g_task_return_boolean (task, TRUE);
+    return FALSE; /* don't call again */
+}
+
+void
+seahorse_gpgme_key_op_make_primary_async (SeahorseGpgmeUid *uid,
+                                          GCancellable *cancellable,
+                                          GAsyncReadyCallback callback,
+                                          gpointer user_data)
+{
+    g_autoptr(GTask) task = NULL;
+    gpgme_ctx_t gctx;
+    gpgme_error_t gerr;
+    g_autoptr(GError) error = NULL;
+    g_autoptr(GSource) gsource = NULL;
+    gpgme_key_t key = NULL;
+    gpgme_user_id_t gpg_uid = NULL;
+
+    g_return_if_fail (SEAHORSE_GPGME_IS_UID (uid));
+
+    gpg_uid = seahorse_gpgme_uid_get_userid (uid);
+    g_return_if_fail (!gpg_uid->revoked && !gpg_uid->invalid);
+
+    key = seahorse_gpgme_uid_get_pubkey (uid);
+    g_return_if_fail (key);
+
+    gctx = seahorse_gpgme_keyring_new_context (&gerr);
+
+    task = g_task_new (uid, cancellable, callback, user_data);
+    gpgme_set_progress_cb (gctx, on_key_op_progress, task);
+    g_task_set_task_data (task, gctx, (GDestroyNotify) gpgme_release);
+
+    seahorse_progress_prep_and_begin (cancellable, task, NULL);
+    gsource = seahorse_gpgme_gsource_new (gctx, cancellable);
+    g_source_set_callback (gsource, G_SOURCE_FUNC (on_key_op_make_primary_complete),
+                           g_object_ref (task), g_object_unref);
+
+    if (gerr == 0)
+        gerr = gpgme_op_set_uid_flag_start (gctx, key, gpg_uid->uid, "primary", NULL);
+
+    if (seahorse_gpgme_propagate_error (gerr, &error)) {
+        g_task_return_error (task, g_steal_pointer (&error));
+        return;
+    }
+
+    g_source_attach (gsource, g_main_context_default ());
+}
+
+gboolean
+seahorse_gpgme_key_op_make_primary_finish (SeahorseGpgmeUid *uid,
+                                           GAsyncResult *result,
+                                           GError **error)
+{
+    g_return_val_if_fail (SEAHORSE_GPGME_IS_UID (uid), FALSE);
+    g_return_val_if_fail (g_task_is_valid (result, uid), FALSE);
+
+    return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 
diff --git a/pgp/seahorse-gpgme-key-op.h b/pgp/seahorse-gpgme-key-op.h
index 22003e71..bcfdfbfb 100644
--- a/pgp/seahorse-gpgme-key-op.h
+++ b/pgp/seahorse-gpgme-key-op.h
@@ -160,7 +160,14 @@ gboolean              seahorse_gpgme_key_op_add_uid_finish  (SeahorseGpgmeKey *p
                                                              GAsyncResult *result,
                                                              GError **error);
 
-gpgme_error_t         seahorse_gpgme_key_op_primary_uid      (SeahorseGpgmeUid *uid);
+void              seahorse_gpgme_key_op_make_primary_async  (SeahorseGpgmeUid *uid,
+                                                             GCancellable *cancellable,
+                                                             GAsyncReadyCallback callback,
+                                                             gpointer user_data);
+
+gboolean          seahorse_gpgme_key_op_make_primary_finish (SeahorseGpgmeUid *uid,
+                                                             GAsyncResult *result,
+                                                             GError **error);
 
 gpgme_error_t         seahorse_gpgme_key_op_del_uid          (SeahorseGpgmeUid *uid);
                              
diff --git a/pgp/seahorse-pgp-key-properties.c b/pgp/seahorse-pgp-key-properties.c
index 05a997da..a5fec103 100644
--- a/pgp/seahorse-pgp-key-properties.c
+++ b/pgp/seahorse-pgp-key-properties.c
@@ -231,20 +231,36 @@ on_uids_add (GSimpleAction *action, GVariant *param, gpointer user_data)
                                        GTK_WINDOW (self));
 }
 
+static void
+on_uids_make_primary_cb (GObject *source, GAsyncResult *res, gpointer user_data)
+{
+    SeahorsePgpKeyProperties *self = SEAHORSE_PGP_KEY_PROPERTIES (user_data);
+    SeahorseGpgmeUid *uid = SEAHORSE_GPGME_UID (source);
+    g_autoptr(GError) error = NULL;
+
+    if (!seahorse_gpgme_key_op_make_primary_finish (uid, res, &error)) {
+        GtkWindow *window;
+        window = gtk_window_get_transient_for (GTK_WINDOW (self));
+        seahorse_util_show_error (GTK_WIDGET (window),
+                                  _("Couldn’t change primary user ID"),
+                                  error->message);
+    }
+}
+
 static void
 on_uids_make_primary (GSimpleAction *action, GVariant *param, gpointer user_data)
 {
     SeahorsePgpKeyProperties *self = SEAHORSE_PGP_KEY_PROPERTIES (user_data);
     SeahorsePgpUid *uid;
-    gpgme_error_t err;
 
     uid = names_get_selected_uid (self);
-    if (uid) {
-        g_return_if_fail (SEAHORSE_GPGME_IS_UID (uid));
-        err = seahorse_gpgme_key_op_primary_uid (SEAHORSE_GPGME_UID (uid));
-        if (!GPG_IS_OK (err))
-            seahorse_gpgme_handle_error (err, _("Couldn’t change primary user ID"));
-    }
+    if (!uid)
+        return;
+
+    g_return_if_fail (SEAHORSE_GPGME_IS_UID (uid));
+    seahorse_gpgme_key_op_make_primary_async (SEAHORSE_GPGME_UID (uid),
+                                              NULL,
+                                              on_uids_make_primary_cb, self);
 }
 
 static void


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