[geary/mjog/account-command-stacks: 1/25] Update Application.CommandSequence



commit a8849c99256bd745b0b89471cf5e93ee3c22bc7b
Author: Michael Gratton <mike vee net>
Date:   Wed Oct 23 11:56:18 2019 +1100

    Update Application.CommandSequence
    
    Make its list of commands private, implement firing state signals for
    commands in its sequence.

 .../accounts/accounts-editor-servers-pane.vala     | 26 +++++-----
 src/client/application/application-command.vala    | 58 +++++++++++++++++++---
 2 files changed, 64 insertions(+), 20 deletions(-)
---
diff --git a/src/client/accounts/accounts-editor-servers-pane.vala 
b/src/client/accounts/accounts-editor-servers-pane.vala
index 328d2aa6..1eb402c9 100644
--- a/src/client/accounts/accounts-editor-servers-pane.vala
+++ b/src/client/accounts/accounts-editor-servers-pane.vala
@@ -1079,14 +1079,14 @@ private class Accounts.ServiceOutgoingAuthRow :
                 );
             }
 
-            Application.CommandSequence seq = new Application.CommandSequence({
-                    new Application.PropertyCommand<Geary.Credentials?>(
-                        this.service, "credentials", new_creds
-                    ),
-                    new Application.PropertyCommand<uint>(
-                        this.service, "credentials-requirement", this.value.source
-                    )
-                });
+            Application.Command[] commands = {
+                new Application.PropertyCommand<Geary.Credentials?>(
+                    this.service, "credentials", new_creds
+                ),
+                new Application.PropertyCommand<uint>(
+                    this.service, "credentials-requirement", this.value.source
+                )
+            };
 
             // The default SMTP port also depends on the auth method
             // used, so also update the port here if we're currently
@@ -1099,14 +1099,14 @@ private class Accounts.ServiceOutgoingAuthRow :
                 Geary.ServiceInformation copy =
                     new Geary.ServiceInformation.copy(this.service);
                 copy.credentials_requirement = this.value.source;
-                seq.commands.add(
-                    new Application.PropertyCommand<uint>(
-                        this.service, "port", copy.get_default_port()
-                    )
+                commands += new Application.PropertyCommand<uint>(
+                    this.service, "port", copy.get_default_port()
                 );
             }
 
-            this.commands.execute.begin(seq, this.cancellable);
+            this.commands.execute.begin(
+                new Application.CommandSequence(commands), this.cancellable
+            );
         }
     }
 
diff --git a/src/client/application/application-command.vala b/src/client/application/application-command.vala
index 5a781a21..d04d3141 100644
--- a/src/client/application/application-command.vala
+++ b/src/client/application/application-command.vala
@@ -155,14 +155,54 @@ public abstract class Application.Command : GLib.Object {
 
 /**
  * A command that executes a sequence of other commands.
+ *
+ * When initially executed or redone, commands will be processed
+ * individually in the given order. When undone, commands will be
+ * processed individually in reverse order.
  */
 public class Application.CommandSequence : Command {
 
 
-    public Gee.List<Command> commands {
-        get; private set; default = new Gee.LinkedList<Command>();
+    /**
+     * Emitted when the command was successfully executed.
+     *
+     * Ensures the same signal is emitted on all commands in the
+     * sequence, in order.
+     */
+    public override void executed() {
+       foreach (Command command in this.commands) {
+           command.executed();
+       }
+    }
+
+    /**
+     * Emitted when the command was successfully undone.
+     *
+     * Ensures the same signal is emitted on all commands in the
+     * sequence, in reverse order.
+     */
+    public override void undone() {
+       foreach (Command command in reversed_commands()) {
+           command.undone();
+       }
+    }
+
+    /**
+     * Emitted when the command was successfully redone.
+     *
+     * Ensures the same signal is emitted on all commands in the
+     * sequence, in order.
+     */
+    public override void redone() {
+       foreach (Command command in this.commands) {
+           command.redone();
+       }
     }
 
+
+    private Gee.List<Command> commands = new Gee.LinkedList<Command>();
+
+
     public CommandSequence(Command[]? commands = null) {
         if (commands != null) {
             this.commands.add_all_array(commands);
@@ -185,11 +225,7 @@ public class Application.CommandSequence : Command {
      */
     public override async void undo(GLib.Cancellable? cancellable)
        throws GLib.Error {
-       Gee.LinkedList<Command> reversed = new Gee.LinkedList<Command>();
-       foreach (Command command in this.commands) {
-           reversed.insert(0, command);
-       }
-       foreach (Command command in this.commands) {
+       foreach (Command command in reversed_commands()) {
            yield command.undo(cancellable);
        }
     }
@@ -204,6 +240,14 @@ public class Application.CommandSequence : Command {
        }
     }
 
+    private Gee.List<Command> reversed_commands() {
+       var reversed = new Gee.LinkedList<Command>();
+       foreach (Command command in this.commands) {
+           reversed.insert(0, command);
+       }
+       return reversed;
+    }
+
 }
 
 


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