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



commit a44771999ee3e3590c9827dae640b1a1462d2b9a
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 979730b9..8d080e03 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 {
@@ -2516,3 +2538,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]