[nautilus/wip/oholy/encrypted-archives: 40/43] compress-dialog: Add support for encrypted .zip




commit 6db771747948e3462cf68fe45b3bbbd0f82ca1d3
Author: Ondrej Holy <oholy redhat com>
Date:   Mon Jun 21 15:07:11 2021 +0200

    compress-dialog: Add support for encrypted .zip
    
    Currently, it is not possible to create encrypted archives over
    Nautilus. Let's add support for encrypted .zip files to not have
    to install a dedicated file manager.
    
    Fixes: https://gitlab.gnome.org/GNOME/nautilus/-/issues/822

 data/org.gnome.nautilus.gschema.xml          |  1 +
 src/nautilus-compress-dialog-controller.c    | 85 +++++++++++++++++++++++++++-
 src/nautilus-compress-dialog-controller.h    |  1 +
 src/nautilus-file-operations.c               |  8 ++-
 src/nautilus-file-operations.h               |  1 +
 src/nautilus-file-undo-operations.c          |  7 ++-
 src/nautilus-file-undo-operations.h          |  3 +-
 src/nautilus-files-view.c                    | 10 ++++
 src/nautilus-global-preferences.h            |  3 +-
 src/resources/ui/nautilus-compress-dialog.ui | 40 ++++++++++++-
 10 files changed, 152 insertions(+), 7 deletions(-)
---
diff --git a/data/org.gnome.nautilus.gschema.xml b/data/org.gnome.nautilus.gschema.xml
index 0289987b8..34af6f87a 100644
--- a/data/org.gnome.nautilus.gschema.xml
+++ b/data/org.gnome.nautilus.gschema.xml
@@ -65,6 +65,7 @@
     <value value="0" nick="zip"/>
     <value value="1" nick="tar.xz"/>
     <value value="2" nick="7z"/>
+    <value value="3" nick="encrypted_zip"/>
   </enum>
 
   <schema path="/org/gnome/nautilus/" id="org.gnome.nautilus" gettext-domain="nautilus">
diff --git a/src/nautilus-compress-dialog-controller.c b/src/nautilus-compress-dialog-controller.c
index 6377ffd17..12c52f10e 100644
--- a/src/nautilus-compress-dialog-controller.c
+++ b/src/nautilus-compress-dialog-controller.c
@@ -33,10 +33,14 @@ struct _NautilusCompressDialogController
     GtkWidget *compress_dialog;
     GtkWidget *name_entry;
     GtkWidget *zip_radio_button;
+    GtkWidget *encrypted_zip_radio_button;
     GtkWidget *tar_xz_radio_button;
     GtkWidget *seven_zip_radio_button;
+    GtkWidget *passphrase_label;
+    GtkWidget *passphrase_entry;
 
     const char *extension;
+    gchar *passphrase;
 
     gulong response_handler_id;
 };
@@ -44,10 +48,11 @@ struct _NautilusCompressDialogController
 G_DEFINE_TYPE (NautilusCompressDialogController, nautilus_compress_dialog_controller, 
NAUTILUS_TYPE_FILE_NAME_WIDGET_CONTROLLER);
 
 static gboolean
-nautilus_compress_dialog_controller_name_is_valid (NautilusFileNameWidgetController  *self,
+nautilus_compress_dialog_controller_name_is_valid (NautilusFileNameWidgetController  *controller,
                                                    gchar                             *name,
                                                    gchar                            **error_message)
 {
+    NautilusCompressDialogController *self = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (controller);
     gboolean is_valid;
 
     is_valid = TRUE;
@@ -70,7 +75,7 @@ nautilus_compress_dialog_controller_name_is_valid (NautilusFileNameWidgetControl
         is_valid = FALSE;
         *error_message = _("An archive cannot be called “..”.");
     }
-    else if (nautilus_file_name_widget_controller_is_name_too_long (self, name))
+    else if (nautilus_file_name_widget_controller_is_name_too_long (controller, name))
     {
         is_valid = FALSE;
         *error_message = _("Archive name is too long.");
@@ -82,6 +87,13 @@ nautilus_compress_dialog_controller_name_is_valid (NautilusFileNameWidgetControl
         *error_message = _("Archives with “.” at the beginning of their name are hidden.");
     }
 
+    /* An empty passwords are not allowed. */
+    if (gtk_widget_get_sensitive (self->passphrase_entry) &&
+        (self->passphrase == NULL || self->passphrase[0] == '\0'))
+    {
+        is_valid = FALSE;
+    }
+
     return is_valid;
 }
 
@@ -135,6 +147,7 @@ update_selected_format (NautilusCompressDialogController *self,
 {
     const char *extension;
     GtkWidget *active_button;
+    gboolean passphrase_sensitive = FALSE;
 
     switch (format)
     {
@@ -145,6 +158,14 @@ update_selected_format (NautilusCompressDialogController *self,
         }
         break;
 
+        case NAUTILUS_COMPRESSION_ENCRYPTED_ZIP:
+        {
+            extension = ".zip";
+            active_button = self->encrypted_zip_radio_button;
+            passphrase_sensitive = TRUE;
+        }
+        break;
+
         case NAUTILUS_COMPRESSION_TAR_XZ:
         {
             extension = ".tar.xz";
@@ -168,6 +189,13 @@ update_selected_format (NautilusCompressDialogController *self,
 
     self->extension = extension;
 
+    gtk_widget_set_sensitive (self->passphrase_label, passphrase_sensitive);
+    gtk_widget_set_sensitive (self->passphrase_entry, passphrase_sensitive);
+    if (!passphrase_sensitive)
+    {
+        gtk_entry_set_text (GTK_ENTRY (self->passphrase_entry), "");
+    }
+
     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (active_button),
                                   TRUE);
 
@@ -197,6 +225,23 @@ zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
                             NAUTILUS_COMPRESSION_ZIP);
 }
 
+static void
+encrypted_zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
+                                       gpointer         user_data)
+{
+    NautilusCompressDialogController *controller;
+
+    controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
+
+    if (!gtk_toggle_button_get_active (toggle_button))
+    {
+        return;
+    }
+
+    update_selected_format (controller,
+                            NAUTILUS_COMPRESSION_ENCRYPTED_ZIP);
+}
+
 static void
 tar_xz_radio_button_on_toggled (GtkToggleButton *toggle_button,
                                 gpointer         user_data)
@@ -231,6 +276,21 @@ seven_zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
                             NAUTILUS_COMPRESSION_7ZIP);
 }
 
+static void
+passphrase_entry_on_changed (GtkEditable *editable,
+                             gpointer     user_data)
+{
+    NautilusCompressDialogController *self;
+
+    self = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
+
+    g_free (self->passphrase);
+    self->passphrase = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->passphrase_entry)));
+
+    /* Update activation button sensitivity. */
+    g_signal_emit_by_name (self->name_entry, "changed");
+}
+
 NautilusCompressDialogController *
 nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
                                          NautilusDirectory *destination_directory,
@@ -244,8 +304,11 @@ nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
     GtkWidget *name_entry;
     GtkWidget *activate_button;
     GtkWidget *zip_radio_button;
+    GtkWidget *encrypted_zip_radio_button;
     GtkWidget *tar_xz_radio_button;
     GtkWidget *seven_zip_radio_button;
+    GtkWidget *passphrase_label;
+    GtkWidget *passphrase_entry;
     NautilusCompressionFormat format;
 
     builder = gtk_builder_new_from_resource ("/org/gnome/nautilus/ui/nautilus-compress-dialog.ui");
@@ -255,8 +318,11 @@ nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
     name_entry = GTK_WIDGET (gtk_builder_get_object (builder, "name_entry"));
     activate_button = GTK_WIDGET (gtk_builder_get_object (builder, "activate_button"));
     zip_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "zip_radio_button"));
+    encrypted_zip_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "encrypted_zip_radio_button"));
     tar_xz_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "tar_xz_radio_button"));
     seven_zip_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "seven_zip_radio_button"));
+    passphrase_label = GTK_WIDGET (gtk_builder_get_object (builder, "passphrase_label"));
+    passphrase_entry = GTK_WIDGET (gtk_builder_get_object (builder, "passphrase_entry"));
 
     gtk_window_set_transient_for (GTK_WINDOW (compress_dialog),
                                   parent_window);
@@ -270,9 +336,12 @@ nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
 
     self->compress_dialog = compress_dialog;
     self->zip_radio_button = zip_radio_button;
+    self->encrypted_zip_radio_button = encrypted_zip_radio_button;
     self->tar_xz_radio_button = tar_xz_radio_button;
     self->seven_zip_radio_button = seven_zip_radio_button;
     self->name_entry = name_entry;
+    self->passphrase_label = passphrase_label;
+    self->passphrase_entry = passphrase_entry;
 
     self->response_handler_id = g_signal_connect (compress_dialog,
                                                   "response",
@@ -282,10 +351,14 @@ nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
     gtk_builder_add_callback_symbols (builder,
                                       "zip_radio_button_on_toggled",
                                       G_CALLBACK (zip_radio_button_on_toggled),
+                                      "encrypted_zip_radio_button_on_toggled",
+                                      G_CALLBACK (encrypted_zip_radio_button_on_toggled),
                                       "tar_xz_radio_button_on_toggled",
                                       G_CALLBACK (tar_xz_radio_button_on_toggled),
                                       "seven_zip_radio_button_on_toggled",
                                       G_CALLBACK (seven_zip_radio_button_on_toggled),
+                                      "passphrase_entry_on_changed",
+                                      G_CALLBACK (passphrase_entry_on_changed),
                                       NULL);
     gtk_builder_connect_signals (builder, self);
 
@@ -323,6 +396,8 @@ nautilus_compress_dialog_controller_finalize (GObject *object)
         self->compress_dialog = NULL;
     }
 
+    g_free (self->passphrase);
+
     G_OBJECT_CLASS (nautilus_compress_dialog_controller_parent_class)->finalize (object);
 }
 
@@ -337,3 +412,9 @@ nautilus_compress_dialog_controller_class_init (NautilusCompressDialogController
     parent_class->get_new_name = nautilus_compress_dialog_controller_get_new_name;
     parent_class->name_is_valid = nautilus_compress_dialog_controller_name_is_valid;
 }
+
+const gchar *
+nautilus_compress_dialog_controller_get_passphrase (NautilusCompressDialogController *self)
+{
+    return self->passphrase;
+}
diff --git a/src/nautilus-compress-dialog-controller.h b/src/nautilus-compress-dialog-controller.h
index 2421b8115..6c96d68fa 100644
--- a/src/nautilus-compress-dialog-controller.h
+++ b/src/nautilus-compress-dialog-controller.h
@@ -31,3 +31,4 @@ G_DECLARE_FINAL_TYPE (NautilusCompressDialogController, nautilus_compress_dialog
 NautilusCompressDialogController * nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
                                                                             NautilusDirectory 
*destination_directory,
                                                                             gchar             *initial_name);
+const gchar * nautilus_compress_dialog_controller_get_passphrase (NautilusCompressDialogController 
*controller);
diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c
index 0c0fa43e8..7927bd504 100644
--- a/src/nautilus-file-operations.c
+++ b/src/nautilus-file-operations.c
@@ -222,6 +222,7 @@ typedef struct
 
     AutoarFormat format;
     AutoarFilter filter;
+    gchar *passphrase;
 
     guint64 total_size;
     guint total_files;
@@ -8757,6 +8758,7 @@ compress_task_done (GObject      *source_object,
 
     g_object_unref (compress_job->output_file);
     g_list_free_full (compress_job->source_files, g_object_unref);
+    g_free (compress_job->passphrase);
 
     finalize_common ((CommonJob *) compress_job);
 
@@ -9031,6 +9033,7 @@ compress_task_thread_func (GTask        *task,
                                         compress_job->format,
                                         compress_job->filter,
                                         FALSE);
+    autoar_compressor_set_passphrase (compressor, compress_job->passphrase);
 
     autoar_compressor_set_output_is_dest (compressor, TRUE);
 
@@ -9061,6 +9064,7 @@ nautilus_file_operations_compress (GList                          *files,
                                    GFile                          *output,
                                    AutoarFormat                    format,
                                    AutoarFilter                    filter,
+                                   const gchar                    *passphrase,
                                    GtkWindow                      *parent_window,
                                    NautilusFileOperationsDBusData *dbus_data,
                                    NautilusCreateCallback          done_callback,
@@ -9076,6 +9080,7 @@ nautilus_file_operations_compress (GList                          *files,
     compress_job->output_file = g_object_ref (output);
     compress_job->format = format;
     compress_job->filter = filter;
+    compress_job->passphrase = g_strdup (passphrase);
     compress_job->done_callback = done_callback;
     compress_job->done_callback_data = done_callback_data;
 
@@ -9086,7 +9091,8 @@ nautilus_file_operations_compress (GList                          *files,
         compress_job->common.undo_info = nautilus_file_undo_info_compress_new (files,
                                                                                output,
                                                                                format,
-                                                                               filter);
+                                                                               filter,
+                                                                               passphrase);
     }
 
     task = g_task_new (NULL, compress_job->common.cancellable,
diff --git a/src/nautilus-file-operations.h b/src/nautilus-file-operations.h
index 8236e0e06..14d664f80 100644
--- a/src/nautilus-file-operations.h
+++ b/src/nautilus-file-operations.h
@@ -159,6 +159,7 @@ void nautilus_file_operations_compress (GList                          *files,
                                         GFile                          *output,
                                         AutoarFormat                    format,
                                         AutoarFilter                    filter,
+                                        const gchar                    *passphrase,
                                         GtkWindow                      *parent_window,
                                         NautilusFileOperationsDBusData *dbus_data,
                                         NautilusCreateCallback          done_callback,
diff --git a/src/nautilus-file-undo-operations.c b/src/nautilus-file-undo-operations.c
index a6a3b2025..64f9ce76c 100644
--- a/src/nautilus-file-undo-operations.c
+++ b/src/nautilus-file-undo-operations.c
@@ -2495,6 +2495,7 @@ struct _NautilusFileUndoInfoCompress
     GFile *output;
     AutoarFormat format;
     AutoarFilter filter;
+    gchar *passphrase;
 };
 
 G_DEFINE_TYPE (NautilusFileUndoInfoCompress, nautilus_file_undo_info_compress, NAUTILUS_TYPE_FILE_UNDO_INFO)
@@ -2562,6 +2563,7 @@ compress_redo_func (NautilusFileUndoInfo           *info,
                                        self->output,
                                        self->format,
                                        self->filter,
+                                       self->passphrase,
                                        parent_window,
                                        dbus_data,
                                        compress_callback,
@@ -2597,6 +2599,7 @@ nautilus_file_undo_info_compress_finalize (GObject *obj)
 
     g_list_free_full (self->sources, g_object_unref);
     g_clear_object (&self->output);
+    g_free (self->passphrase);
 
     G_OBJECT_CLASS (nautilus_file_undo_info_compress_parent_class)->finalize (obj);
 }
@@ -2618,7 +2621,8 @@ NautilusFileUndoInfo *
 nautilus_file_undo_info_compress_new (GList        *sources,
                                       GFile        *output,
                                       AutoarFormat  format,
-                                      AutoarFilter  filter)
+                                      AutoarFilter  filter,
+                                      const gchar  *passphrase)
 {
     NautilusFileUndoInfoCompress *self;
 
@@ -2631,6 +2635,7 @@ nautilus_file_undo_info_compress_new (GList        *sources,
     self->output = g_object_ref (output);
     self->format = format;
     self->filter = filter;
+    self->passphrase = g_strdup (passphrase);
 
     return NAUTILUS_FILE_UNDO_INFO (self);
 }
diff --git a/src/nautilus-file-undo-operations.h b/src/nautilus-file-undo-operations.h
index f96f2fe69..09ae17cef 100644
--- a/src/nautilus-file-undo-operations.h
+++ b/src/nautilus-file-undo-operations.h
@@ -226,4 +226,5 @@ G_DECLARE_FINAL_TYPE (NautilusFileUndoInfoCompress, nautilus_file_undo_info_comp
 NautilusFileUndoInfo * nautilus_file_undo_info_compress_new (GList        *sources,
                                                              GFile        *output,
                                                              AutoarFormat  format,
-                                                             AutoarFilter  filter);
+                                                             AutoarFilter  filter,
+                                                             const gchar  *passphrase);
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index c8863bee6..af187fdd0 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -2248,6 +2248,7 @@ compress_dialog_controller_on_name_accepted (NautilusFileNameWidgetController *c
     NautilusFilesViewPrivate *priv;
     AutoarFormat format;
     AutoarFilter filter;
+    const gchar *passphrase = NULL;
 
     view = NAUTILUS_FILES_VIEW (user_data);
     priv = nautilus_files_view_get_instance_private (view);
@@ -2295,6 +2296,14 @@ compress_dialog_controller_on_name_accepted (NautilusFileNameWidgetController *c
         }
         break;
 
+        case NAUTILUS_COMPRESSION_ENCRYPTED_ZIP:
+        {
+            format = AUTOAR_FORMAT_ZIP;
+            filter = AUTOAR_FILTER_NONE;
+            passphrase = nautilus_compress_dialog_controller_get_passphrase (priv->compress_controller);
+        }
+        break;
+
         case NAUTILUS_COMPRESSION_TAR_XZ:
         {
             format = AUTOAR_FORMAT_TAR;
@@ -2318,6 +2327,7 @@ compress_dialog_controller_on_name_accepted (NautilusFileNameWidgetController *c
     nautilus_file_operations_compress (source_files, output,
                                        format,
                                        filter,
+                                       passphrase,
                                        nautilus_files_view_get_containing_window (view),
                                        NULL,
                                        compress_done,
diff --git a/src/nautilus-global-preferences.h b/src/nautilus-global-preferences.h
index 8c482f7ce..2e8753b3c 100644
--- a/src/nautilus-global-preferences.h
+++ b/src/nautilus-global-preferences.h
@@ -77,7 +77,8 @@ typedef enum
 {
         NAUTILUS_COMPRESSION_ZIP = 0,
         NAUTILUS_COMPRESSION_TAR_XZ,
-        NAUTILUS_COMPRESSION_7ZIP
+        NAUTILUS_COMPRESSION_7ZIP,
+        NAUTILUS_COMPRESSION_ENCRYPTED_ZIP
 } NautilusCompressionFormat;
 
 /* Icon View */
diff --git a/src/resources/ui/nautilus-compress-dialog.ui b/src/resources/ui/nautilus-compress-dialog.ui
index f8dbc9a44..8410bd3ab 100644
--- a/src/resources/ui/nautilus-compress-dialog.ui
+++ b/src/resources/ui/nautilus-compress-dialog.ui
@@ -13,7 +13,7 @@
       <object class="GtkBox" id="vbox">
         <property name="orientation">vertical</property>
         <property name="margin_top">18</property>
-        <property name="margin_bottom">12</property>
+        <property name="margin_bottom">18</property>
         <property name="margin_start">84</property>
         <property name="margin_end">84</property>
         <property name="spacing">6</property>
@@ -78,6 +78,21 @@
                 </child>
               </object>
             </child>
+            <child>
+              <object class="HdyActionRow">
+                <property name="title" translatable="yes" comments="Translators: Keep '.zip' 
unmodified.">.zip (Password Protected)</property>
+                <property name="subtitle" translatable="yes">Encrypted .zip format.</property>
+                <property name="activatable-widget">encrypted_zip_radio_button</property>
+                <child type="prefix">
+                  <object class="GtkRadioButton" id="encrypted_zip_radio_button">
+                    <property name="draw-indicator">True</property>
+                    <property name="can-focus">False</property>
+                    <property name="group">zip_radio_button</property>
+                    <signal name="toggled" handler="encrypted_zip_radio_button_on_toggled"/>
+                  </object>
+                </child>
+              </object>
+            </child>
             <child>
               <object class="HdyActionRow">
                 <property name="title" translatable="no">.tar.xz</property>
@@ -115,6 +130,29 @@
             <property name="position">4</property>
           </packing>
         </child>
+        <child>
+          <object class="GtkLabel" id="passphrase_label">
+            <property name="label" translatable="yes">Password</property>
+            <property name="xalign">0</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">5</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkEntry" id="passphrase_entry">
+            <property name="input-purpose">GTK_INPUT_PURPOSE_PASSWORD</property>
+            <property name="visibility">False</property>
+            <signal name="changed" handler="passphrase_entry_on_changed"/>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">6</property>
+          </packing>
+        </child>
       </object>
     </child>
     <child type="action">


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