[nautilus/wip/oholy/encrypted-archive-crashes-followup] file-operations: Simplify archive password dialog handling




commit bfce3ba50ded6bd60c8d420d5fbe208f593715af
Author: Ondrej Holy <oholy redhat com>
Date:   Mon Apr 19 16:06:16 2021 +0200

    file-operations: Simplify archive password dialog handling
    
    The previous commit implements invoking the main context for the dialog
    from an operation thread. This duplicates existing code that caters the
    same use case for the file conflict dialog (using the
    `invoke_main_context_sync()` function). Let's move the code handling of
    the password dialog into the `src/nautilus-operations-ui-manager.c` file
    to make use of `invoke_main_context_sync()`.
    
    Fixes: https://gitlab.gnome.org/GNOME/nautilus/-/issues/1829

 src/nautilus-file-operations.c       | 118 +++--------------------------------
 src/nautilus-operations-ui-manager.c |  96 ++++++++++++++++++++++++++++
 src/nautilus-operations-ui-manager.h |   3 +
 3 files changed, 108 insertions(+), 109 deletions(-)
---
diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c
index 59beecd7e..f04a8c47c 100644
--- a/src/nautilus-file-operations.c
+++ b/src/nautilus-file-operations.c
@@ -8364,126 +8364,26 @@ extract_job_on_completed (AutoarExtractor *extractor,
     nautilus_file_changes_queue_file_added (output_file);
 }
 
-typedef struct
-{
-    ExtractJob *extract_job;
-    AutoarExtractor *extractor;
-    gchar *passphrase;
-    GtkWidget *passphrase_entry;
-    GMutex mutex;
-    GCond cond;
-    gboolean completed;
-} PassphraseRequestData;
-
-static void
-on_request_passphrase_cb (GtkDialog *dialog,
-                          gint       response_id,
-                          gpointer   user_data)
-{
-    PassphraseRequestData *data = user_data;
-
-    if (response_id == GTK_RESPONSE_CANCEL ||
-        response_id == GTK_RESPONSE_DELETE_EVENT)
-    {
-        abort_job ((CommonJob *) data->extract_job);
-    }
-    else
-    {
-        data->passphrase = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->passphrase_entry)));
-    }
-
-    data->completed = TRUE;
-
-    gtk_widget_destroy (GTK_WIDGET (dialog));
-
-    g_cond_signal (&data->cond);
-    g_mutex_unlock (&data->mutex);
-}
-
-static gboolean
-run_passphrase_dialog (gpointer user_data)
-{
-    PassphraseRequestData *data = user_data;
-    g_autofree gchar *label_str = NULL;
-    g_autofree gchar *basename = NULL;
-    GtkWidget *dialog;
-    GtkWidget *entry;
-    GtkWidget *label;
-    GtkWidget *box;
-    GFile *source_file;
-
-    g_mutex_lock (&data->mutex);
-
-    dialog = gtk_dialog_new_with_buttons (_("Password Required"),
-                                          data->extract_job->common.parent_window,
-                                          GTK_DIALOG_USE_HEADER_BAR | GTK_DIALOG_MODAL,
-                                          _("Cancel"), GTK_RESPONSE_CANCEL,
-                                          _("Extract"), GTK_RESPONSE_OK,
-                                          NULL);
-    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
-    source_file = autoar_extractor_get_source_file (data->extractor);
-    basename = get_basename (source_file);
-
-    box = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
-    gtk_widget_set_margin_start (box, 20);
-    gtk_widget_set_margin_end (box, 20);
-    gtk_widget_set_margin_top (box, 20);
-    gtk_widget_set_margin_bottom (box, 20);
-
-    label_str = g_strdup_printf (_("“%s” is password-protected."), basename);
-    label = gtk_label_new (label_str);
-    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
-    gtk_label_set_max_width_chars (GTK_LABEL (label), 60);
-    gtk_container_add (GTK_CONTAINER (box), label);
-
-    entry = gtk_entry_new ();
-    gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
-    gtk_widget_set_valign (entry, GTK_ALIGN_END);
-    gtk_widget_set_vexpand (entry, TRUE);
-    gtk_entry_set_placeholder_text (GTK_ENTRY (entry), _("Enter password…"));
-    gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
-    gtk_entry_set_input_purpose (GTK_ENTRY (entry), GTK_INPUT_PURPOSE_PASSWORD);
-    gtk_container_add (GTK_CONTAINER (box), entry);
-
-    data->passphrase_entry = entry;
-    g_signal_connect (dialog, "response", G_CALLBACK (on_request_passphrase_cb), data);
-    gtk_widget_show_all (dialog);
-
-    return G_SOURCE_REMOVE;
-}
-
 static gchar *
 extract_job_on_request_passphrase (AutoarExtractor *extractor,
                                    gpointer         user_data)
 {
-    PassphraseRequestData *data;
     ExtractJob *extract_job = user_data;
+    GtkWindow *parent_window;
+    GFile *source_file;
+    g_autofree gchar *basename = NULL;
     gchar *passphrase;
 
-    data = g_new0 (PassphraseRequestData, 1);
-    g_mutex_init (&data->mutex);
-    g_cond_init (&data->cond);
-    data->extract_job = extract_job;
-    data->extractor = extractor;
-
-    g_mutex_lock (&data->mutex);
-
-    g_main_context_invoke (NULL,
-                           run_passphrase_dialog,
-                           data);
+    parent_window = extract_job->common.parent_window;
+    source_file = autoar_extractor_get_source_file (extractor);
+    basename = get_basename (source_file);
 
-    while (!data->completed)
+    passphrase = extract_ask_passphrase (parent_window, basename);
+    if (passphrase == NULL)
     {
-        g_cond_wait (&data->cond, &data->mutex);
+        abort_job ((CommonJob *) extract_job);
     }
 
-    g_mutex_unlock (&data->mutex);
-    g_mutex_clear (&data->mutex);
-    g_cond_clear (&data->cond);
-
-    passphrase = g_steal_pointer (&data->passphrase);
-    g_free (data);
-
     return passphrase;
 }
 
diff --git a/src/nautilus-operations-ui-manager.c b/src/nautilus-operations-ui-manager.c
index 170ee7f5a..c58844186 100644
--- a/src/nautilus-operations-ui-manager.c
+++ b/src/nautilus-operations-ui-manager.c
@@ -581,3 +581,99 @@ handle_unsupported_compressed_file (GtkWindow *parent_window,
 
     return;
 }
+
+typedef struct
+{
+    GtkWindow *parent_window;
+    const gchar *basename;
+    gchar *passphrase;
+    GtkWidget *passphrase_entry;
+    gboolean completed;
+} PassphraseRequestData;
+
+static void
+on_request_passphrase_cb (GtkDialog *dialog,
+                          gint       response_id,
+                          gpointer   user_data)
+{
+    PassphraseRequestData *data = user_data;
+
+    if (response_id != GTK_RESPONSE_CANCEL &&
+        response_id != GTK_RESPONSE_DELETE_EVENT)
+    {
+        data->passphrase = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->passphrase_entry)));
+    }
+
+    data->completed = TRUE;
+}
+
+static gboolean
+run_passphrase_dialog (gpointer user_data)
+{
+    PassphraseRequestData *data = user_data;
+    g_autofree gchar *label_str = NULL;
+    GtkWidget *dialog;
+    GtkWidget *entry;
+    GtkWidget *label;
+    GtkWidget *box;
+
+    dialog = gtk_dialog_new_with_buttons (_("Password Required"),
+                                          data->parent_window,
+                                          GTK_DIALOG_USE_HEADER_BAR | GTK_DIALOG_MODAL,
+                                          _("Cancel"), GTK_RESPONSE_CANCEL,
+                                          _("Extract"), GTK_RESPONSE_OK,
+                                          NULL);
+    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+
+    box = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+    gtk_widget_set_margin_start (box, 20);
+    gtk_widget_set_margin_end (box, 20);
+    gtk_widget_set_margin_top (box, 20);
+    gtk_widget_set_margin_bottom (box, 20);
+
+    label_str = g_strdup_printf (_("“%s” is password-protected."), data->basename);
+    label = gtk_label_new (label_str);
+    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+    gtk_label_set_max_width_chars (GTK_LABEL (label), 60);
+    gtk_container_add (GTK_CONTAINER (box), label);
+
+    entry = gtk_entry_new ();
+    gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
+    gtk_widget_set_valign (entry, GTK_ALIGN_END);
+    gtk_widget_set_vexpand (entry, TRUE);
+    gtk_entry_set_placeholder_text (GTK_ENTRY (entry), _("Enter password…"));
+    gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
+    gtk_entry_set_input_purpose (GTK_ENTRY (entry), GTK_INPUT_PURPOSE_PASSWORD);
+    gtk_container_add (GTK_CONTAINER (box), entry);
+
+    data->passphrase_entry = entry;
+    g_signal_connect (dialog, "response", G_CALLBACK (on_request_passphrase_cb), data);
+    gtk_widget_show_all (dialog);
+
+    while (!data->completed)
+    {
+        g_main_context_iteration (NULL, TRUE);
+    }
+
+    gtk_widget_destroy (GTK_WIDGET (dialog));
+
+    return G_SOURCE_REMOVE;
+}
+
+gchar *
+extract_ask_passphrase (GtkWindow   *parent_window,
+                        const gchar *archive_basename)
+{
+    PassphraseRequestData *data;
+    gchar *passphrase;
+
+    data = g_new0 (PassphraseRequestData, 1);
+    data->parent_window = parent_window;
+    data->basename = archive_basename;
+    invoke_main_context_sync (NULL, run_passphrase_dialog, data);
+
+    passphrase = g_steal_pointer (&data->passphrase);
+    g_free (data);
+
+    return passphrase;
+}
diff --git a/src/nautilus-operations-ui-manager.h b/src/nautilus-operations-ui-manager.h
index 4321c824f..06fa3ba3d 100644
--- a/src/nautilus-operations-ui-manager.h
+++ b/src/nautilus-operations-ui-manager.h
@@ -26,3 +26,6 @@ enum
 
 void handle_unsupported_compressed_file (GtkWindow *parent_window,
                                          GFile     *compressed_file);
+
+gchar *extract_ask_passphrase (GtkWindow   *parent_window,
+                               const gchar *archive_basename);


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