[geary/mjog/misc-fixes: 15/17] Fix critical closing composers with drafts



commit fe1226232c7cb049b4400e03d0049ba2c5f0419b
Author: Michael Gratton <mike vee net>
Date:   Wed Jan 8 21:41:23 2020 +1100

    Fix critical closing composers with drafts

 src/client/composer/composer-widget.vala | 94 +++++++++++++++-----------------
 1 file changed, 45 insertions(+), 49 deletions(-)
---
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index 9253eab4..fc6f5494 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -93,6 +93,9 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
 
     private enum AttachPending { ALL, INLINE_ONLY }
 
+    private enum DraftPolicy { DISCARD, KEEP }
+
+
     private class FromAddressMap {
         public Geary.Account account;
         public Geary.RFC822.MailboxAddresses from;
@@ -878,12 +881,14 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
                 this.draft_manager_opening = null;
             }
 
-            if (this.draft_manager != null) {
-                try {
-                    yield close_draft_manager(null);
-                } catch (Error err) {
-                    debug("Error closing draft manager on composer close");
-                }
+            try {
+                yield close_draft_manager(KEEP, null);
+            } catch (GLib.Error error) {
+                this.application.controller.report_problem(
+                    new Geary.AccountProblemReport(
+                        this.account.information, error
+                    )
+                );
             }
 
             destroy();
@@ -1521,11 +1526,7 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
         try {
             yield this.editor.clean_content();
             yield this.application.controller.send_composed_email(this);
-
-            if (this.draft_manager != null) {
-                yield discard_draft();
-                yield close_draft_manager(null);
-            }
+            yield close_draft_manager(DISCARD, null);
 
             if (this.container != null) {
                 this.container.close();
@@ -1591,30 +1592,38 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
      */
     private async void reopen_draft_manager(GLib.Cancellable? cancellable)
         throws GLib.Error {
-        if (this.draft_manager != null) {
-            // Discard the draft, if any, since it may be on a
-            // different account
-            yield discard_draft();
-            yield close_draft_manager(cancellable);
-        }
+        // Discard the draft, if any, since it may be on a different
+        // account
+        yield close_draft_manager(DISCARD, cancellable);
         yield open_draft_manager(null, cancellable);
         yield save_draft();
     }
 
-    private async void close_draft_manager(GLib.Cancellable? cancellable)
+    private async void close_draft_manager(DraftPolicy draft_policy,
+                                           GLib.Cancellable? cancellable)
         throws GLib.Error {
-        Geary.App.DraftManager old_manager = this.draft_manager;
-        this.draft_manager = null;
-        this.draft_status_text = "";
+        var old_manager = this.draft_manager;
+        if (old_manager != null) {
+            this.draft_timer.reset();
+
+            this.draft_manager = null;
+            this.draft_status_text = "";
+            this.current_draft_id = null;
 
-        old_manager.notify[Geary.App.DraftManager.PROP_DRAFT_STATE]
-            .disconnect(on_draft_state_changed);
-        old_manager.notify[Geary.App.DraftManager.PROP_CURRENT_DRAFT_ID]
-            .disconnect(on_draft_id_changed);
-        old_manager.fatal.disconnect(on_draft_manager_fatal);
+            old_manager.notify[Geary.App.DraftManager.PROP_DRAFT_STATE]
+                .disconnect(on_draft_state_changed);
+            old_manager.notify[Geary.App.DraftManager.PROP_CURRENT_DRAFT_ID]
+                .disconnect(on_draft_id_changed);
+            old_manager.fatal.disconnect(on_draft_manager_fatal);
 
-        yield old_manager.close_async(cancellable);
-        debug("Draft manager closed");
+            if (draft_policy == DISCARD) {
+                debug("Discarding draft");
+                yield old_manager.discard(null);
+            }
+
+            yield old_manager.close_async(cancellable);
+            debug("Draft manager closed");
+        }
     }
 
     private void update_draft_state() {
@@ -1672,16 +1681,6 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
         }
     }
 
-    private async void discard_draft() throws GLib.Error {
-        debug("Discarding draft");
-
-        // cancel timer in favor of this operation
-        this.draft_timer.reset();
-
-        yield this.draft_manager.discard(null);
-        this.current_draft_id = null;
-    }
-
     private async void save_and_close() {
         set_enabled(false);
 
@@ -1708,17 +1707,14 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
     private async void discard_and_close() {
         set_enabled(false);
 
-        if (this.draft_manager != null) {
-            try {
-                yield discard_draft();
-                yield close_draft_manager(null);
-            } catch (GLib.Error error) {
-                this.application.controller.report_problem(
-                    new Geary.AccountProblemReport(
-                        this.account.information, error
-                    )
-                );
-            }
+        try {
+            yield close_draft_manager(DISCARD, null);
+        } catch (GLib.Error error) {
+            this.application.controller.report_problem(
+                new Geary.AccountProblemReport(
+                    this.account.information, error
+                )
+            );
         }
 
         // Pass on to the controller so the discarded email can be


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