[geary/mjog/account-command-stacks: 45/77] Implement emptying spam/trash as non-undoable commands



commit 08710390694ed38639616f86e2c461c76e0782e0
Author: Michael Gratton <mike vee net>
Date:   Sun Oct 6 10:56:09 2019 +1100

    Implement emptying spam/trash as non-undoable commands

 src/client/application/application-controller.vala | 65 ++++++++++++++++++++++
 src/client/components/main-window.vala             | 48 ++++++++++++++++
 2 files changed, 113 insertions(+)
---
diff --git a/src/client/application/application-controller.vala 
b/src/client/application/application-controller.vala
index b9a6dd2a..4ce9284a 100644
--- a/src/client/application/application-controller.vala
+++ b/src/client/application/application-controller.vala
@@ -823,6 +823,28 @@ public class Application.Controller : Geary.BaseObject {
         }
     }
 
+    public async void empty_folder_special(Geary.Account source,
+                                           Geary.SpecialFolderType type)
+        throws GLib.Error {
+        AccountContext? context = this.accounts.get(source.information);
+        if (context != null) {
+            Geary.FolderSupport.Empty? emptyable = (
+                source.get_special_folder(type)
+                as Geary.FolderSupport.Empty
+            );
+            if (emptyable == null) {
+                throw new Geary.EngineError.UNSUPPORTED(
+                    "Special folder type not supported %s", type.to_string()
+                );
+            }
+
+            yield this.commands.execute(
+                new EmptyFolderCommand(emptyable),
+                context.cancellable
+            );
+        }
+    }
+
     /** Expunges removed accounts while the controller remains open. */
     internal async void expunge_accounts() {
         try {
@@ -2521,3 +2543,46 @@ private class Application.DeleteEmailCommand : Command {
 }
 
 
+private class Application.EmptyFolderCommand : Command {
+
+
+    public override bool can_undo {
+        get { return false; }
+    }
+
+    private Geary.FolderSupport.Empty target;
+
+
+    public EmptyFolderCommand(Geary.FolderSupport.Empty target) {
+        this.target = target;
+    }
+
+    public override async void execute(GLib.Cancellable? cancellable)
+        throws GLib.Error {
+        bool open = false;
+        try {
+            yield this.target.open_async(
+                Geary.Folder.OpenFlags.NO_DELAY, cancellable
+            );
+            open = true;
+            yield this.target.empty_folder_async(cancellable);
+        } finally {
+            if (open) {
+                try {
+                    yield this.target.close_async(null);
+                } catch (GLib.Error err) {
+                    // ignored
+                }
+            }
+        }
+    }
+
+    public override async void undo(GLib.Cancellable? cancellable)
+        throws GLib.Error {
+        throw new Geary.EngineError.UNSUPPORTED(
+            "Cannot undo emptying a folder: %s",
+            this.target.path.to_string()
+        );
+    }
+
+}
diff --git a/src/client/components/main-window.vala b/src/client/components/main-window.vala
index 27b941a1..7e7fe2bf 100644
--- a/src/client/components/main-window.vala
+++ b/src/client/components/main-window.vala
@@ -744,6 +744,22 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
         return (dialog.run() == Gtk.ResponseType.OK);
     }
 
+    private bool prompt_empty_folder(Geary.SpecialFolderType type) {
+        ConfirmationDialog dialog = new ConfirmationDialog(
+            this,
+            _("Empty all email from your %s folder?").printf(
+                type.get_display_name()
+            ),
+            _("This removes the email from Geary and your email server.") +
+            "  <b>" + _("This cannot be undone.") + "</b>",
+            _("Empty %s").printf(type.get_display_name()),
+            "destructive-action"
+        );
+        dialog.use_secondary_markup(true);
+        dialog.set_focus_response(Gtk.ResponseType.CANCEL);
+        return (dialog.run() == Gtk.ResponseType.OK);
+    }
+
     private inline void handle_error(Geary.AccountInformation? account,
                                      GLib.Error error) {
         Geary.ProblemReport? report = (account != null)
@@ -1650,9 +1666,41 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
     }
 
     private void on_empty_spam() {
+        Geary.Account? account = this.current_account;
+        if (account != null &&
+            prompt_empty_folder(Geary.SpecialFolderType.SPAM)) {
+            this.application.controller.empty_folder_special.begin(
+                account,
+                Geary.SpecialFolderType.SPAM,
+                (obj, res) => {
+                    try {
+                        this.application.controller.empty_folder_special.end(res);
+                } catch (GLib.Error err) {
+                        handle_error(account.information, err);
+                    }
+                }
+            );
+        }
     }
 
     private void on_empty_trash() {
+        Geary.Account? account = this.current_account;
+        if (account != null &&
+            prompt_empty_folder(Geary.SpecialFolderType.TRASH)) {
+            this.application.controller.empty_folder_special.begin(
+                account,
+                Geary.SpecialFolderType.TRASH,
+                (obj, res) => {
+                    try {
+                        this.application.controller.empty_folder_special.end(res);
+                    } catch (GLib.Error err) {
+                        handle_error(account.information, err);
+                    }
+                }
+            );
+        }
+    }
+
     // Individual message view action callbacks
 
     private void on_conversation_viewer_mark_emails(Gee.Collection<Geary.EmailIdentifier> messages,


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