[geary/mjog/engine-api-cleanup: 2/4] Rework application engine convenience methods



commit 8655030745dab213cb61ccede009571330e8f013
Author: Michael Gratton <mike vee net>
Date:   Tue Nov 19 00:46:16 2019 +1100

    Rework application engine convenience methods
    
    Now that Geary.Engine has a more convenient API, remove
    Application.Controller::get_first_account and ::get_num_accounts in
    favour of those.
    
    Provide common Application.MainWwindow methods for getting and selecting
    the first accoutn's inbox, call those to reduce code duplication.

 src/client/application/application-client.vala     | 14 +----
 src/client/application/application-controller.vala | 72 ++++------------------
 .../application/application-main-window.vala       | 38 ++++++++----
 src/client/components/search-bar.vala              |  2 +-
 4 files changed, 41 insertions(+), 85 deletions(-)
---
diff --git a/src/client/application/application-client.vala b/src/client/application/application-client.vala
index a96732a2..4bbccb4b 100644
--- a/src/client/application/application-client.vala
+++ b/src/client/application/application-client.vala
@@ -800,19 +800,7 @@ public class Application.Client : Gtk.Application {
         this.controller.register_window(window);
         window.focus_in_event.connect(on_main_window_focus_in);
         if (select_first_inbox) {
-            try {
-                Geary.Account first = Geary.Collection.get_first(
-                    this.engine.get_accounts()
-                );
-                if (first != null) {
-                    Geary.Folder? inbox = first.get_special_folder(INBOX);
-                    if (inbox != null) {
-                        window.select_folder.begin(inbox, true);
-                    }
-                }
-            } catch (GLib.Error error) {
-                debug("Error getting Inbox for first account");
-            }
+            window.select_first_inbox(true);
         }
         return window;
     }
diff --git a/src/client/application/application-controller.vala 
b/src/client/application/application-controller.vala
index 2fc3fae2..a74ccf7d 100644
--- a/src/client/application/application-controller.vala
+++ b/src/client/application/application-controller.vala
@@ -234,23 +234,20 @@ internal class Application.Controller : Geary.BaseObject {
         // main window first opens no folder might be available to be
         // selected. Add look for the inbox and if not found, add a
         // listener here as a once off for when it is loaded.
-        var config = get_first_account();
-        if (config != null) {
-            var first = this.accounts.get(config);
-            if (first != null) {
-                var inbox = first.account.get_special_folder(INBOX);
-                if (inbox != null) {
-                    application.get_active_main_window().select_folder.begin(
-                        inbox, true
-                    );
-
-                } else {
-                    // Connect after so the folder is added to any
-                    // open main windows first.
-                    first.account.folders_available_unavailable.connect_after(
+        if (!application.get_active_main_window().select_first_inbox(true)) {
+            // Connect after so the folder is added to any
+            // open main windows first.
+            try {
+                Geary.Account first = Geary.Collection.get_first(
+                    application.engine.get_accounts()
+                );
+                if (first != null) {
+                    first.folders_available_unavailable.connect_after(
                         on_folders_first_available
                     );
                 }
+            } catch (GLib.Error error) {
+                debug("Error getting Inbox for first account");
             }
         }
 
@@ -924,16 +921,6 @@ internal class Application.Controller : Geary.BaseObject {
         window.retry_service_problem.disconnect(on_retry_service_problem);
     }
 
-    /** Returns the first open account, sorted by ordinal. */
-    internal Geary.AccountInformation? get_first_account() {
-        return this.accounts.keys.iterator().fold<Geary.AccountInformation?>(
-            (next, prev) => {
-                return prev == null || next.ordinal < prev.ordinal ? next : prev;
-            },
-            null
-        );
-    }
-
     /** Expunges removed accounts while the controller remains open. */
     internal async void expunge_accounts() {
         try {
@@ -1312,20 +1299,6 @@ internal class Application.Controller : Geary.BaseObject {
         return retry;
     }
 
-    /**
-     * Returns the number of accounts that exist in Geary.  Note that not all accounts may be
-     * open.  Zero is returned on an error.
-     */
-    public int get_num_accounts() {
-        try {
-            return Geary.Engine.instance.get_accounts().size;
-        } catch (Error e) {
-            debug("Error getting number of accounts: %s", e.message);
-        }
-
-        return 0; // on error
-    }
-
     private bool is_inbox_descendant(Geary.Folder target) {
         bool is_descendent = false;
 
@@ -1428,30 +1401,11 @@ internal class Application.Controller : Geary.BaseObject {
         }
     }
 
-    private void on_folders_first_available(
-        Geary.Account account,
+    private void on_folders_first_available(Geary.Account account,
         Gee.BidirSortedSet<Geary.Folder>? available,
         Gee.BidirSortedSet<Geary.Folder>? unavailable
     ) {
-        debug("XXX folders first loaded");
-        bool inbox_found = false;
-        if (available != null) {
-            foreach (Geary.Folder folder in available) {
-                debug("XXX folder: %s/%s",
-                      folder.get_display_name(),
-                      folder.special_folder_type.to_string());
-                if (Controller.should_add_folder(available, folder) &&
-                    folder.special_folder_type == INBOX) {
-                    debug("XXX found inbox");
-                    inbox_found = true;
-                    this.application.get_active_main_window().select_folder.begin(
-                        folder, true
-                    );
-                }
-            }
-        }
-
-        if (inbox_found) {
+        if (application.get_active_main_window().select_first_inbox(true)) {
             // The handler has done its job, so disconnect it
             account.folders_available_unavailable.disconnect(
                 on_folders_first_available
diff --git a/src/client/application/application-main-window.vala 
b/src/client/application/application-main-window.vala
index 62b2d3b4..fd6337e6 100644
--- a/src/client/application/application-main-window.vala
+++ b/src/client/application/application-main-window.vala
@@ -730,6 +730,16 @@ public class Application.MainWindow :
         }
     }
 
+    internal bool select_first_inbox(bool is_interactive) {
+        bool success = false;
+        Geary.Folder? inbox = get_first_inbox();
+        if (inbox != null) {
+            this.select_folder.begin(inbox, is_interactive);
+            success = true;
+        }
+        return success;
+    }
+
     private void add_account(AccountContext to_add) {
         if (!this.accounts.contains(to_add)) {
             this.folder_list.set_user_folders_root_name(
@@ -845,6 +855,21 @@ public class Application.MainWindow :
         return context;
     }
 
+    private Geary.Folder? get_first_inbox() {
+        Geary.Folder? inbox = null;
+        try {
+            Geary.Account first = Geary.Collection.get_first(
+                this.application.engine.get_accounts()
+            );
+            if (first != null) {
+                inbox = first.get_special_folder(INBOX);
+            }
+        } catch (GLib.Error error) {
+            debug("Error getting inbox for first account");
+        }
+        return inbox;
+    }
+
     private void load_config(Configuration config) {
         // This code both loads AND saves the pane positions with live updating. This is more
         // resilient against crashes because the value in dconf changes *immediately*, and
@@ -1720,19 +1745,8 @@ public class Application.MainWindow :
         // conversation list/viewer.
         Geary.Folder? to_select = null;
         if (!is_shutdown) {
-            Geary.AccountInformation? first_account =
-                this.application.controller.get_first_account();
-            if (first_account != null) {
-                AccountContext? first_context =
-                    this.application.controller.get_context_for_account(
-                        first_account
-                    );
-                if (first_context != null) {
-                    to_select = first_context.inbox;
-                }
-            }
+            to_select = get_first_inbox();
         }
-
         this.remove_account.begin(account, to_select);
     }
 
diff --git a/src/client/components/search-bar.vala b/src/client/components/search-bar.vala
index ad65de5f..2966b652 100644
--- a/src/client/components/search-bar.vala
+++ b/src/client/components/search-bar.vala
@@ -114,7 +114,7 @@ public class SearchBar : Gtk.SearchBar {
         if (main != null) {
             set_search_placeholder_text(
                 current_account == null ||
-                main.application.controller.get_num_accounts() == 1
+                main.application.engine.accounts_count == 1
                 ? DEFAULT_SEARCH_TEXT :
                 _("Search %s account").printf(
                     current_account.information.display_name


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