[geary/mjog/account-command-stacks: 50/77] Remove Application.Controller::current_account



commit 12e73b3d723920a96651447a633b89ad2d2d8f17
Author: Michael Gratton <mike vee net>
Date:   Wed Oct 23 01:00:38 2019 +1100

    Remove Application.Controller::current_account
    
    Property is per-window state and hence should not be present in the
    app controller.

 src/client/application/application-controller.vala |  66 ++++++-------
 src/client/components/main-window.vala             | 102 ++++++++++++++-------
 2 files changed, 96 insertions(+), 72 deletions(-)
---
diff --git a/src/client/application/application-controller.vala 
b/src/client/application/application-controller.vala
index 8ad79db2..5e2f9889 100644
--- a/src/client/application/application-controller.vala
+++ b/src/client/application/application-controller.vala
@@ -154,9 +154,6 @@ public class Application.Controller : Geary.BaseObject {
     // Null if none selected
     private Geary.Folder? current_folder = null;
 
-    // Null if no folder ever selected
-    private Geary.Account? current_account = null;
-
     private Application.CommandStack commands { get; protected set; }
 
     private Cancellable cancellable_folder = new Cancellable();
@@ -451,8 +448,6 @@ public class Application.Controller : Geary.BaseObject {
         this.current_folder = null;
         this.previous_non_search_folder = null;
 
-        this.current_account = null;
-
         this.pending_mailtos.clear();
         this.composer_widgets.clear();
         this.waiting_to_close.clear();
@@ -466,21 +461,23 @@ public class Application.Controller : Geary.BaseObject {
      * Opens or queues a new composer addressed to a specific email address.
      */
     public void compose(string? mailto = null) {
-        if (current_account == null) {
+        Geary.Account? selected = this.main_window.selected_account;
+        if (selected == null) {
             // Schedule the send for after we have an account open.
             pending_mailtos.add(mailto);
         } else {
-            create_compose_widget(ComposerWidget.ComposeType.NEW_MESSAGE, null, null, mailto);
+            create_compose_widget(selected, NEW_MESSAGE, null, null, mailto);
         }
     }
 
     /**
      * Opens new composer with an existing message as context.
      */
-    public void compose_with_context_email(ComposerWidget.ComposeType type,
+    public void compose_with_context_email(Geary.Account account,
+                                           ComposerWidget.ComposeType type,
                                            Geary.Email context,
                                            string? quote) {
-        create_compose_widget(type, context, quote);
+        create_compose_widget(account, type, context, quote);
     }
 
     /** Adds a new composer to be kept track of. */
@@ -867,13 +864,10 @@ public class Application.Controller : Geary.BaseObject {
         AccountContext? context = this.accounts.get(config);
         if (context != null) {
             Geary.Account account = context.account;
-            if (this.current_account == account) {
-                this.current_account = null;
-
-                previous_non_search_folder = null;
-                main_window.search_bar.set_search_text(""); // Reset search.
-
+            if (this.main_window.selected_account == account) {
+                this.main_window.deselect_account();
                 cancel_folder();
+                this.previous_non_search_folder = null;
             }
 
             // Stop updating status and showing errors when closing
@@ -1333,24 +1327,15 @@ public class Application.Controller : Geary.BaseObject {
 
         this.current_folder = folder;
 
-        if (this.current_account != folder.account) {
-            this.current_account = folder.account;
-            this.main_window.search_bar.set_account(this.current_account);
-
-            // If we were waiting for an account to be selected before issuing mailtos, do that now.
+        if (this.main_window.selected_account != folder.account) {
+            // If we were waiting for an account to be selected before
+            // issuing mailtos, do that now.
             if (pending_mailtos.size > 0) {
                 foreach(string? mailto in pending_mailtos)
                     compose(mailto);
 
                 pending_mailtos.clear();
             }
-
-            main_window.main_toolbar.copy_folder_menu.clear();
-            main_window.main_toolbar.move_folder_menu.clear();
-            foreach(Geary.Folder f in current_folder.account.list_folders()) {
-                main_window.main_toolbar.copy_folder_menu.add_folder(f);
-                main_window.main_toolbar.move_folder_menu.add_folder(f);
-            }
         }
 
         if (!(current_folder is Geary.SearchFolder))
@@ -1403,7 +1388,12 @@ public class Application.Controller : Geary.BaseObject {
 
         if (!already_open) {
             create_compose_widget(
-                ComposerWidget.ComposeType.NEW_MESSAGE, draft, null, null, true
+                activated.base_folder.account,
+                NEW_MESSAGE,
+                draft,
+                null,
+                null,
+                true
             );
         }
     }
@@ -1452,7 +1442,7 @@ public class Application.Controller : Geary.BaseObject {
                 }
 
                 main_window.folder_list.add_folder(folder);
-                if (folder.account == current_account) {
+                if (folder.account == main_window.selected_account) {
                     if (!main_window.main_toolbar.copy_folder_menu.has_folder(folder))
                         main_window.main_toolbar.copy_folder_menu.add_folder(folder);
                     if (!main_window.main_toolbar.move_folder_menu.has_folder(folder))
@@ -1518,7 +1508,7 @@ public class Application.Controller : Geary.BaseObject {
                 Geary.Folder folder = unavailable_iterator.get();
 
                 main_window.folder_list.remove_folder(folder);
-                if (folder.account == current_account) {
+                if (folder.account == this.main_window.selected_account) {
                     if (main_window.main_toolbar.copy_folder_menu.has_folder(folder))
                         main_window.main_toolbar.copy_folder_menu.remove_folder(folder);
                     if (main_window.main_toolbar.move_folder_menu.has_folder(folder))
@@ -1910,14 +1900,12 @@ public class Application.Controller : Geary.BaseObject {
      * @param is_draft - Whether we're starting from a draft (true) or
      * a new mail (false)
      */
-    private void create_compose_widget(ComposerWidget.ComposeType compose_type,
+    private void create_compose_widget(Geary.Account account,
+                                       ComposerWidget.ComposeType compose_type,
                                        Geary.Email? referred = null,
                                        string? quote = null,
                                        string? mailto = null,
                                        bool is_draft = false) {
-        if (current_account == null)
-            return;
-
         // There's a few situations where we can re-use an existing
         // composer, check for these first.
 
@@ -1957,12 +1945,12 @@ public class Application.Controller : Geary.BaseObject {
         ComposerWidget widget;
         if (mailto != null) {
             widget = new ComposerWidget.from_mailto(
-                this.application, current_account, mailto
+                this.application, account, mailto
             );
         } else {
             widget = new ComposerWidget(
                 this.application,
-                current_account,
+                account,
                 is_draft ? referred.id : null,
                 compose_type
             );
@@ -1980,7 +1968,7 @@ public class Application.Controller : Geary.BaseObject {
         }
 
         this.load_composer.begin(
-            this.current_account,
+            account,
             widget,
             referred,
             quote,
@@ -2053,8 +2041,8 @@ public class Application.Controller : Geary.BaseObject {
 
     private void do_search(string search_text) {
         Geary.SearchFolder? search_folder = null;
-        if (this.current_account != null) {
-            search_folder = this.current_account.get_special_folder(
+        if (this.main_window.selected_account != null) {
+            search_folder = this.main_window.selected_account.get_special_folder(
                 Geary.SpecialFolderType.SEARCH
             ) as Geary.SearchFolder;
         }
diff --git a/src/client/components/main-window.vala b/src/client/components/main-window.vala
index 5b0c531c..19b583d2 100644
--- a/src/client/components/main-window.vala
+++ b/src/client/components/main-window.vala
@@ -161,15 +161,7 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
     }
 
     /** Currently selected account, null if none selected */
-    public Geary.Account? selected_account {
-        owned get {
-            Geary.Account? account = null;
-            if (this.selected_folder != null) {
-                account = this.selected_folder.account;
-            }
-            return account;
-        }
-    }
+    public Geary.Account? selected_account { get; private set; default = null; }
 
     /** Currently selected folder, null if none selected */
     public Geary.Folder? selected_folder { get; private set; default = null; }
@@ -383,6 +375,12 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
         this.info_bar_frame.show();
     }
 
+    /** Deselected the currently selected account, if any. */
+    public void deselect_account() {
+        this.search_bar.set_search_text(""); // Reset search.
+        // XXX do other things
+    }
+
     /** Displays a composer addressed to a specific email address. */
     public void open_composer_for_mailbox(Geary.RFC822.MailboxAddress to) {
         Application.Controller controller = this.application.controller;
@@ -680,16 +678,19 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
     public void folder_selected(Geary.Folder? folder,
                                 GLib.Cancellable? cancellable) {
         if (this.selected_folder != folder) {
+            Geary.Account? previous_account = this.selected_account;
             if (this.selected_folder != null) {
+                previous_account = this.selected_folder.account;
                 this.progress_monitor.remove(this.selected_folder.opening_monitor);
                 this.selected_folder.properties.notify.disconnect(update_headerbar);
                 close_conversation_monitor();
             }
 
+            account_selected(folder != null ? folder.account : null);
             this.selected_folder = folder;
 
-            this.conversation_viewer.show_loading();
             update_conversation_actions(NONE);
+            this.conversation_viewer.show_loading();
             this.main_toolbar.update_trash_button(
                 !this.is_shift_down && selected_folder_supports_trash()
             );
@@ -785,6 +786,25 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
         }
     }
 
+    private void account_selected(Geary.Account? account) {
+        if (this.selected_account != account) {
+            if (this.selected_account != null) {
+                this.main_toolbar.copy_folder_menu.clear();
+                this.main_toolbar.move_folder_menu.clear();
+            }
+
+            this.selected_account = account;
+            this.search_bar.set_account(account);
+
+            if (account != null) {
+                foreach (Geary.Folder folder in account.list_folders()) {
+                    this.main_toolbar.copy_folder_menu.add_folder(folder);
+                    this.main_toolbar.move_folder_menu.add_folder(folder);
+                }
+            }
+        }
+    }
+
     private async void open_conversation_monitor(GLib.Cancellable cancellable) {
         this.conversations = new Geary.App.ConversationMonitor(
             this.selected_folder,
@@ -880,16 +900,20 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
     }
 
     private void create_composer_from_viewer(ComposerWidget.ComposeType compose_type) {
+        Geary.Account? account = this.selected_account;
         ConversationEmail? email_view = null;
         ConversationListBox? list_view = this.conversation_viewer.current_list;
         if (list_view != null) {
             email_view = list_view.get_reply_target();
         }
-        if (email_view != null) {
+        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(
-                        compose_type, email_view.email, quote
+                        account,
+                        compose_type,
+                        email_view.email,
+                        quote
                     );
                 });
         }
@@ -1743,36 +1767,48 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
     }
 
     private void on_reply_to_message(ConversationEmail target_view) {
-        target_view.get_selection_for_quoting.begin((obj, res) => {
-                string? quote = target_view.get_selection_for_quoting.end(res);
-                this.application.controller.compose_with_context_email(
-                    REPLY, target_view.email, quote
-                );
-            });
+        Geary.Account? account = this.selected_account;
+        if (account != null) {
+            target_view.get_selection_for_quoting.begin((obj, res) => {
+                    string? quote = target_view.get_selection_for_quoting.end(res);
+                    this.application.controller.compose_with_context_email(
+                        account, REPLY, target_view.email, quote
+                    );
+                });
+        }
     }
 
     private void on_reply_all_message(ConversationEmail target_view) {
-        target_view.get_selection_for_quoting.begin((obj, res) => {
-                string? quote = target_view.get_selection_for_quoting.end(res);
-                this.application.controller.compose_with_context_email(
-                    REPLY_ALL, target_view.email, quote
-                );
-            });
+        Geary.Account? account = this.selected_account;
+        if (account != null) {
+            target_view.get_selection_for_quoting.begin((obj, res) => {
+                    string? quote = target_view.get_selection_for_quoting.end(res);
+                    this.application.controller.compose_with_context_email(
+                        account, REPLY_ALL, target_view.email, quote
+                    );
+                });
+        }
     }
 
     private void on_forward_message(ConversationEmail target_view) {
-        target_view.get_selection_for_quoting.begin((obj, res) => {
-                string? quote = target_view.get_selection_for_quoting.end(res);
-                this.application.controller.compose_with_context_email(
-                    FORWARD, target_view.email, quote
-                );
-            });
+        Geary.Account? account = this.selected_account;
+        if (account != null) {
+            target_view.get_selection_for_quoting.begin((obj, res) => {
+                    string? quote = target_view.get_selection_for_quoting.end(res);
+                    this.application.controller.compose_with_context_email(
+                        account, FORWARD, target_view.email, quote
+                    );
+                });
+        }
     }
 
     private void on_edit_draft(ConversationEmail target_view) {
-        this.application.controller.compose_with_context_email(
-            NEW_MESSAGE, target_view.email, null
-        );
+        Geary.Account? account = this.selected_account;
+        if (account != null) {
+            this.application.controller.compose_with_context_email(
+                account, NEW_MESSAGE, target_view.email, null
+            );
+        }
     }
 
     private void on_attachments_activated(Gee.Collection<Geary.Attachment> attachments) {


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