[geary/mjog/misc-fixes: 22/27] Fix occasional Application.MainWindow criticals on shutdown



commit 4614a89db81c9b1409b8de98f4b06ac34c6a1482
Author: Michael Gratton <mike vee net>
Date:   Wed Jan 22 08:44:28 2020 +1100

    Fix occasional Application.MainWindow criticals on shutdown
    
    Keep a reference to Application.Controller in MainWindow so that when
    being shutdown, async method's end can still be successfully called.

 .../application/application-main-window.vala       | 118 +++++++++++----------
 1 file changed, 62 insertions(+), 56 deletions(-)
---
diff --git a/src/client/application/application-main-window.vala 
b/src/client/application/application-main-window.vala
index 3c138185..285d5224 100644
--- a/src/client/application/application-main-window.vala
+++ b/src/client/application/application-main-window.vala
@@ -287,6 +287,8 @@ public class Application.MainWindow :
     public ConversationViewer conversation_viewer { get; private set; }
     public StatusBar status_bar { get; private set; default = new StatusBar(); }
 
+    private Controller controller;
+
     private MonitoredSpinner spinner = new MonitoredSpinner();
 
     private Gee.Set<AccountContext> accounts = new Gee.HashSet<AccountContext>();
@@ -482,6 +484,12 @@ public class Application.MainWindow :
         );
         base_ref();
 
+        // Keep a ref on the current controller so that even if the
+        // application has been shut down, any async tasks that are
+        // running (and presumably being cancelled) can still complete
+        // without a warning.
+        this.controller = application.controller;
+
         load_config(application.config);
         restore_saved_window_state();
 
@@ -510,14 +518,14 @@ public class Application.MainWindow :
         this.update_ui_timeout.repetition = FOREVER;
 
         // Add future and existing accounts to the main window
-        this.application.controller.account_available.connect(
+        this.controller.account_available.connect(
             on_account_available
         );
-        this.application.controller.account_unavailable.connect(
+        this.controller.account_unavailable.connect(
             on_account_unavailable
         );
         foreach (AccountContext context in
-                 this.application.controller.get_account_contexts()) {
+                 this.controller.get_account_contexts()) {
             add_account(context);
         }
 
@@ -531,10 +539,10 @@ public class Application.MainWindow :
     /** {@inheritDoc} */
     public override void destroy() {
         if (this.application != null) {
-            this.application.controller.account_available.disconnect(
+            this.controller.account_available.disconnect(
                 on_account_available
             );
-            this.application.controller.account_unavailable.disconnect(
+            this.controller.account_unavailable.disconnect(
                 on_account_unavailable
             );
         }
@@ -733,11 +741,9 @@ public class Application.MainWindow :
                 );
 
                 yield open_conversation_monitor(this.conversations, cancellable);
-                this.application.controller.clear_new_messages(
-                    GLib.Log.METHOD, null
-                );
+                this.controller.clear_new_messages(GLib.Log.METHOD, null);
 
-                this.application.controller.process_pending_composers();
+                this.controller.process_pending_composers();
             }
         }
 
@@ -839,7 +845,7 @@ public class Application.MainWindow :
         var composer = new Composer.Widget.from_mailbox(
             this.application, this.selected_folder.account, to
         );
-        this.application.controller.add_composer(composer);
+        this.controller.add_composer(composer);
         show_composer(composer, null);
         composer.load.begin(null, false, null, null);
     }
@@ -940,7 +946,7 @@ public class Application.MainWindow :
         }
         this.folder_list.remove_search();
 
-        foreach (var context in this.application.controller.get_account_contexts()) {
+        foreach (var context in this.controller.get_account_contexts()) {
             context.search.clear();
         }
     }
@@ -1066,7 +1072,7 @@ public class Application.MainWindow :
     private AccountContext? get_selected_account_context() {
         AccountContext? context = null;
         if (this.selected_account != null) {
-            context = this.application.controller.get_context_for_account(
+            context = this.controller.get_context_for_account(
                 this.selected_account.information
             );
         }
@@ -1379,7 +1385,7 @@ public class Application.MainWindow :
         Geary.ProblemReport? report = (account != null)
             ? new Geary.AccountProblemReport(account, error)
             : new Geary.ProblemReport(error);
-        this.application.controller.report_problem(report);
+        this.controller.report_problem(report);
     }
 
     private void update_ui() {
@@ -1555,7 +1561,7 @@ public class Application.MainWindow :
         if (account != null && email_view != null) {
             email_view.get_selection_for_quoting.begin((obj, res) => {
                     string? quote = email_view.get_selection_for_quoting.end(res);
-                    this.application.controller.compose_with_context_email(
+                    this.controller.compose_with_context_email(
                         this,
                         account,
                         compose_type,
@@ -1729,7 +1735,7 @@ public class Application.MainWindow :
         Gee.MultiMap<Geary.EmailIdentifier, Type>? selected_operations = null;
         if (this.selected_folder != null) {
             AccountContext? context =
-                this.application.controller.get_context_for_account(
+                this.controller.get_context_for_account(
                     this.selected_folder.account.information
                 );
             if (context != null) {
@@ -1872,7 +1878,7 @@ public class Application.MainWindow :
                                GLib.Error err) {
         Geary.AccountInformation account =
             monitor.base_folder.account.information;
-        this.application.controller.report_problem(
+        this.controller.report_problem(
             new Geary.ServiceProblemReport(account, account.incoming, err)
         );
     }
@@ -2072,7 +2078,7 @@ public class Application.MainWindow :
     // this signal does not necessarily indicate that the application
     // previously didn't have focus and now it does
     private void on_has_toplevel_focus() {
-        this.application.controller.clear_new_messages(GLib.Log.METHOD, null);
+        this.controller.clear_new_messages(GLib.Log.METHOD, null);
     }
 
     private void on_folder_selected(Geary.Folder? folder) {
@@ -2088,7 +2094,7 @@ public class Application.MainWindow :
     }
 
     private void on_visible_conversations_changed(Gee.Set<Geary.App.Conversation> visible) {
-        this.application.controller.clear_new_messages(GLib.Log.METHOD, visible);
+        this.controller.clear_new_messages(GLib.Log.METHOD, visible);
     }
 
     private void on_conversation_activated(Geary.App.Conversation activated) {
@@ -2107,7 +2113,7 @@ public class Application.MainWindow :
                 // open in a detached composer
                 bool already_open = false;
                 foreach (Composer.Widget composer
-                         in this.application.controller.get_composers()) {
+                         in this.controller.get_composers()) {
                     if (composer.current_draft_id != null &&
                         composer.current_draft_id.equal_to(draft.id)) {
                         already_open = true;
@@ -2118,7 +2124,7 @@ public class Application.MainWindow :
                 }
 
                 if (!already_open) {
-                    this.application.controller.compose_with_context_email(
+                    this.controller.compose_with_context_email(
                         this,
                         activated.base_folder.account,
                         NEW_MESSAGE,
@@ -2222,14 +2228,14 @@ public class Application.MainWindow :
                                        Geary.NamedFlag flag) {
         Geary.Folder? location = this.selected_folder;
         if (location != null) {
-            this.application.controller.mark_conversations.begin(
+            this.controller.mark_conversations.begin(
                 location,
                 conversations,
                 flag,
                 true,
                 (obj, res) => {
                     try {
-                        this.application.controller.mark_conversations.end(res);
+                        this.controller.mark_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(location.account.information, err);
                     }
@@ -2241,14 +2247,14 @@ public class Application.MainWindow :
     private void on_mark_as_read() {
         Geary.Folder? location = this.selected_folder;
         if (location != null) {
-            this.application.controller.mark_conversations.begin(
+            this.controller.mark_conversations.begin(
                 location,
                 this.conversation_list_view.copy_selected(),
                 Geary.EmailFlags.UNREAD,
                 false,
                 (obj, res) => {
                     try {
-                        this.application.controller.mark_conversations.end(res);
+                        this.controller.mark_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(location.account.information, err);
                     }
@@ -2260,14 +2266,14 @@ public class Application.MainWindow :
     private void on_mark_as_unread() {
         Geary.Folder? location = this.selected_folder;
         if (location != null) {
-            this.application.controller.mark_conversations.begin(
+            this.controller.mark_conversations.begin(
                 location,
                 this.conversation_list_view.copy_selected(),
                 Geary.EmailFlags.UNREAD,
                 true,
                 (obj, res) => {
                     try {
-                        this.application.controller.mark_conversations.end(res);
+                        this.controller.mark_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(location.account.information, err);
                     }
@@ -2279,14 +2285,14 @@ public class Application.MainWindow :
     private void on_mark_as_starred() {
         Geary.Folder? location = this.selected_folder;
         if (location != null) {
-            this.application.controller.mark_conversations.begin(
+            this.controller.mark_conversations.begin(
                 location,
                 this.conversation_list_view.copy_selected(),
                 Geary.EmailFlags.FLAGGED,
                 true,
                 (obj, res) => {
                     try {
-                        this.application.controller.mark_conversations.end(res);
+                        this.controller.mark_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(location.account.information, err);
                     }
@@ -2298,14 +2304,14 @@ public class Application.MainWindow :
     private void on_mark_as_unstarred() {
         Geary.Folder? location = this.selected_folder;
         if (location != null) {
-            this.application.controller.mark_conversations.begin(
+            this.controller.mark_conversations.begin(
                 location,
                 this.conversation_list_view.copy_selected(),
                 Geary.EmailFlags.FLAGGED,
                 false,
                 (obj, res) => {
                     try {
-                        this.application.controller.mark_conversations.end(res);
+                        this.controller.mark_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(location.account.information, err);
                     }
@@ -2321,13 +2327,13 @@ public class Application.MainWindow :
                 (source.special_folder_type != SPAM)
                 ? Geary.SpecialFolderType.SPAM
                 : Geary.SpecialFolderType.INBOX;
-            this.application.controller.move_conversations_special.begin(
+            this.controller.move_conversations_special.begin(
                 source,
                 destination,
                 this.conversation_list_view.copy_selected(),
                 (obj, res) => {
                     try {
-                        this.application.controller.move_conversations_special.end(res);
+                        this.controller.move_conversations_special.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }
@@ -2340,13 +2346,13 @@ public class Application.MainWindow :
         Geary.FolderSupport.Move source =
             this.selected_folder as Geary.FolderSupport.Move;
         if (source != null) {
-            this.application.controller.move_conversations.begin(
+            this.controller.move_conversations.begin(
                 source,
                 destination,
                 this.conversation_list_view.copy_selected(),
                 (obj, res) => {
                     try {
-                        this.application.controller.move_conversations.end(res);
+                        this.controller.move_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }
@@ -2360,13 +2366,13 @@ public class Application.MainWindow :
         Geary.FolderSupport.Copy source =
             this.selected_folder as Geary.FolderSupport.Copy;
         if (source != null) {
-            this.application.controller.copy_conversations.begin(
+            this.controller.copy_conversations.begin(
                 source,
                 destination,
                 this.conversation_list_view.copy_selected(),
                 (obj, res) => {
                     try {
-                        this.application.controller.copy_conversations.end(res);
+                        this.controller.copy_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }
@@ -2379,13 +2385,13 @@ public class Application.MainWindow :
     private void on_archive_conversation() {
         Geary.Folder source = this.selected_folder;
         if (source != null) {
-            this.application.controller.move_conversations_special.begin(
+            this.controller.move_conversations_special.begin(
                 source,
                 ARCHIVE,
                 this.conversation_list_view.copy_selected(),
                 (obj, res) => {
                     try {
-                        this.application.controller.move_conversations_special.end(res);
+                        this.controller.move_conversations_special.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }
@@ -2397,13 +2403,13 @@ public class Application.MainWindow :
     private void on_trash_conversation() {
         Geary.Folder source = this.selected_folder;
         if (source != null) {
-            this.application.controller.move_conversations_special.begin(
+            this.controller.move_conversations_special.begin(
                 source,
                 Geary.SpecialFolderType.TRASH,
                 this.conversation_list_view.copy_selected(),
                 (obj, res) => {
                     try {
-                        this.application.controller.move_conversations_special.end(res);
+                        this.controller.move_conversations_special.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }
@@ -2418,12 +2424,12 @@ public class Application.MainWindow :
         Gee.Collection<Geary.App.Conversation> conversations =
             this.conversation_list_view.copy_selected();
         if (target != null && this.prompt_delete_conversations(conversations.size)) {
-            this.application.controller.delete_conversations.begin(
+            this.controller.delete_conversations.begin(
                 target,
                 conversations,
                 (obj, res) => {
                     try {
-                        this.application.controller.delete_conversations.end(res);
+                        this.controller.delete_conversations.end(res);
                     } catch (GLib.Error err) {
                         handle_error(target.account.information, err);
                     }
@@ -2436,12 +2442,12 @@ public class Application.MainWindow :
         Geary.Account? account = this.selected_account;
         if (account != null &&
             prompt_empty_folder(Geary.SpecialFolderType.SPAM)) {
-            this.application.controller.empty_folder_special.begin(
+            this.controller.empty_folder_special.begin(
                 account,
                 Geary.SpecialFolderType.SPAM,
                 (obj, res) => {
                     try {
-                        this.application.controller.empty_folder_special.end(res);
+                        this.controller.empty_folder_special.end(res);
                 } catch (GLib.Error err) {
                         handle_error(account.information, err);
                     }
@@ -2454,12 +2460,12 @@ public class Application.MainWindow :
         Geary.Account? account = this.selected_account;
         if (account != null &&
             prompt_empty_folder(Geary.SpecialFolderType.TRASH)) {
-            this.application.controller.empty_folder_special.begin(
+            this.controller.empty_folder_special.begin(
                 account,
                 Geary.SpecialFolderType.TRASH,
                 (obj, res) => {
                     try {
-                        this.application.controller.empty_folder_special.end(res);
+                        this.controller.empty_folder_special.end(res);
                     } catch (GLib.Error err) {
                         handle_error(account.information, err);
                     }
@@ -2486,7 +2492,7 @@ public class Application.MainWindow :
                 remove_flags = new Geary.EmailFlags();
                 remove_flags.add(to_remove);
             }
-            this.application.controller.mark_messages.begin(
+            this.controller.mark_messages.begin(
                 location,
                 Geary.Collection.single(view.conversation),
                 messages,
@@ -2494,7 +2500,7 @@ public class Application.MainWindow :
                 remove_flags,
                 (obj, res) => {
                     try {
-                        this.application.controller.mark_messages.end(res);
+                        this.controller.mark_messages.end(res);
                     } catch (GLib.Error err) {
                         handle_error(location.account.information, err);
                     }
@@ -2506,7 +2512,7 @@ public class Application.MainWindow :
     private void on_email_reply_to_sender(Geary.Email target, string? quote) {
         Geary.Account? account = this.selected_account;
         if (account != null) {
-            this.application.controller.compose_with_context_email(
+            this.controller.compose_with_context_email(
                 this, account, REPLY, target, quote, false
             );
         }
@@ -2515,7 +2521,7 @@ public class Application.MainWindow :
     private void on_email_reply_to_all(Geary.Email target, string? quote) {
         Geary.Account? account = this.selected_account;
         if (account != null) {
-            this.application.controller.compose_with_context_email(
+            this.controller.compose_with_context_email(
                 this, account, REPLY_ALL, target, quote, false
             );
         }
@@ -2524,7 +2530,7 @@ public class Application.MainWindow :
     private void on_email_forward(Geary.Email target, string? quote) {
         Geary.Account? account = this.selected_account;
         if (account != null) {
-            this.application.controller.compose_with_context_email(
+            this.controller.compose_with_context_email(
                 this, account, FORWARD, target, quote, false
             );
         }
@@ -2533,7 +2539,7 @@ public class Application.MainWindow :
     private void on_email_edit(Geary.Email target) {
         Geary.Account? account = this.selected_account;
         if (account != null) {
-            this.application.controller.compose_with_context_email(
+            this.controller.compose_with_context_email(
                 this, account, NEW_MESSAGE, target, null, true
             );
         }
@@ -2542,14 +2548,14 @@ public class Application.MainWindow :
     private void on_email_trash(ConversationListBox view, Geary.Email target) {
         Geary.Folder? source = this.selected_folder;
         if (source != null) {
-            this.application.controller.move_messages_special.begin(
+            this.controller.move_messages_special.begin(
                 source,
                 TRASH,
                 Geary.Collection.single(view.conversation),
                 Geary.Collection.single(target.id),
                 (obj, res) => {
                     try {
-                        this.application.controller.move_messages_special.end(res);
+                        this.controller.move_messages_special.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }
@@ -2562,13 +2568,13 @@ public class Application.MainWindow :
         Geary.FolderSupport.Remove? source =
             this.selected_folder as Geary.FolderSupport.Remove;
         if (source != null && prompt_delete_messages(1)) {
-            this.application.controller.delete_messages.begin(
+            this.controller.delete_messages.begin(
                 source,
                 Geary.Collection.single(view.conversation),
                 Geary.Collection.single(target.id),
                 (obj, res) => {
                     try {
-                        this.application.controller.delete_messages.end(res);
+                        this.controller.delete_messages.end(res);
                     } catch (GLib.Error err) {
                         handle_error(source.account.information, err);
                     }


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