[geary/wip/775956-dbus-activation: 8/11] Modernise autostart desktop file manager class a bit



commit 8eb9ae4159f73d5b5de7924e4b190bed46a955aa
Author: Michael Gratton <mike vee net>
Date:   Fri Apr 12 20:35:39 2019 +1000

    Modernise autostart desktop file manager class a bit
    
    Move it into the application namespace, give it a slightly more generic
    name, move its instance from the controller to the application, and
    modernise its source code.

 po/POTFILES.in                                     |   2 +-
 .../application/application-startup-manager.vala   | 110 +++++++++++++++++++++
 src/client/application/autostart-manager.vala      |  94 ------------------
 src/client/application/geary-application.vala      |   8 ++
 src/client/application/geary-controller.vala       |   7 --
 src/client/dialogs/preferences-dialog.vala         |   2 +-
 src/client/meson.build                             |   2 +-
 7 files changed, 121 insertions(+), 104 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 9077a63f..69c71b39 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -20,7 +20,7 @@ src/client/application/application-certificate-manager.vala
 src/client/application/application-command.vala
 src/client/application/application-contact-store.vala
 src/client/application/application-contact.vala
-src/client/application/autostart-manager.vala
+src/client/application/application-startup-manager.vala
 src/client/application/geary-application.vala
 src/client/application/geary-controller.vala
 src/client/application/goa-mediator.vala
diff --git a/src/client/application/application-startup-manager.vala 
b/src/client/application/application-startup-manager.vala
new file mode 100644
index 00000000..2e62d7a1
--- /dev/null
+++ b/src/client/application/application-startup-manager.vala
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2016 Software Freedom Conservancy Inc.
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+/*
+ * Manages desktop files in the autostart.
+ */
+public class Application.StartupManager : GLib.Object {
+
+    private const string AUTOSTART_FOLDER = "autostart";
+    private const string AUTOSTART_DESKTOP_FILE = "geary-autostart.desktop";
+
+    private Configuration config;
+    private GLib.File? install_dir;
+    private GLib.File startup_file; // Startup '.desktop' file
+
+    public StartupManager(Configuration config, GLib.File? install_dir) {
+        this.config = config;
+        this.install_dir = install_dir;
+        this.startup_file = GLib.File.new_for_path(
+            GLib.Environment.get_user_config_dir()
+        ).get_child(AUTOSTART_FOLDER)
+        .get_child(AUTOSTART_DESKTOP_FILE);
+
+        // Connect startup-notifications option callback
+        config.settings.changed[Configuration.STARTUP_NOTIFICATIONS_KEY].connect(
+            on_startup_notification_change
+        );
+    }
+
+    /**
+     * Returns the system-wide autostart desktop file
+     */
+    public GLib.File? get_autostart_desktop_file() {
+        GLib.File? parent = null;
+        if (this.install_dir != null) {
+            // Running from the installation directory
+            parent = (
+                this.install_dir
+                .get_child("share")
+                .get_child("applications")
+            );
+        } else {
+            // Running from the source build directory
+            parent = (
+                GLib.File.new_for_path(GearyApplication.SOURCE_ROOT_DIR)
+                .get_child("build")
+                .get_child("desktop")
+            );
+        }
+
+        GLib.File desktop_file = parent.get_child(AUTOSTART_DESKTOP_FILE);
+        return desktop_file.query_exists() ? desktop_file : null;
+    }
+
+    /**
+     * Copies the autostart desktop file to the autostart directory.
+     */
+    public void install_startup_file() throws GLib.Error {
+        if (!this.startup_file.query_exists()) {
+            GLib.File autostart_dir = this.startup_file.get_parent();
+            if (!autostart_dir.query_exists()) {
+                autostart_dir.make_directory_with_parents();
+            }
+            GLib.File? autostart = get_autostart_desktop_file();
+            if (autostart == null) {
+                warning("Autostart file is not installed!");
+            } else {
+                autostart.copy(this.startup_file, 0);
+            }
+        }
+    }
+
+    /**
+     * Deletes the desktop file from autostart directory.
+     */
+    public void delete_startup_file() throws GLib.Error {
+        try {
+            this.startup_file.delete();
+        } catch (GLib.IOError.NOT_FOUND err) {
+            // All good
+        }
+    }
+
+    /*
+     * Synchronises the config with the actual state of the autostart file.
+     *
+     * Ensures it's not misleading (i.e. the option is checked while
+     * the file doesn't exist).
+     */
+    public void sync_with_config() {
+        this.config.startup_notifications = this.startup_file.query_exists();
+    }
+
+    private void on_startup_notification_change() {
+        try {
+            if (this.config.startup_notifications) {
+                install_startup_file();
+            } else {
+                delete_startup_file();
+            }
+        } catch (GLib.Error err) {
+            warning("Failed to update autostart desktop file: %s", err.message);
+        }
+    }
+
+}
diff --git a/src/client/application/geary-application.vala b/src/client/application/geary-application.vala
index bca95f13..09bf2ddc 100644
--- a/src/client/application/geary-application.vala
+++ b/src/client/application/geary-application.vala
@@ -178,6 +178,11 @@ public class GearyApplication : Gtk.Application {
      */
     public Configuration config { get; private set; }
 
+    /** Manages the autostart desktop file. */
+    public Application.StartupManager? autostart {
+        get; private set; default = null;
+    }
+
     /**
      * Determines if Geary configured to run as as a background service.
      *
@@ -339,6 +344,9 @@ public class GearyApplication : Gtk.Application {
         Gtk.Window.set_default_icon_name(APP_ID);
 
         this.config = new Configuration(APP_ID);
+        this.autostart = new Application.StartupManager(
+            this.config, get_install_dir()
+        );
 
         add_action_entries(action_entries, this);
 
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index f3e524f8..e9104451 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -129,8 +129,6 @@ public class GearyController : Geary.BaseObject {
 
     public Geary.App.ConversationMonitor? current_conversations { get; private set; default = null; }
 
-    public AutostartManager? autostart_manager { get; private set; default = null; }
-
     public Application.AvatarStore? avatars {
         get; private set; default = new Application.AvatarStore();
     }
@@ -335,9 +333,6 @@ public class GearyController : Geary.BaseObject {
 
         this.main_window.conversation_list_view.grab_focus();
 
-        // instantiate here to ensure that Config is initialized and ready
-        this.autostart_manager = new AutostartManager(this.application);
-
         // initialize revokable
         save_revokable(null, null);
 
@@ -562,8 +557,6 @@ public class GearyController : Geary.BaseObject {
         this.composer_widgets.clear();
         this.waiting_to_close.clear();
 
-        this.autostart_manager = null;
-
         this.avatars.close();
         this.avatars = null;
 
diff --git a/src/client/dialogs/preferences-dialog.vala b/src/client/dialogs/preferences-dialog.vala
index 1af91a09..41c2a8a8 100644
--- a/src/client/dialogs/preferences-dialog.vala
+++ b/src/client/dialogs/preferences-dialog.vala
@@ -46,7 +46,7 @@ public class PreferencesDialog : Gtk.Dialog {
 
     public new void run() {
         // Sync startup notification option with file state
-        this.app.controller.autostart_manager.sync_with_config();
+        this.app.autostart.sync_with_config();
 
         base.run();
         destroy();
diff --git a/src/client/meson.build b/src/client/meson.build
index f362f7b1..0770c20e 100644
--- a/src/client/meson.build
+++ b/src/client/meson.build
@@ -5,7 +5,7 @@ geary_client_vala_sources = files(
   'application/application-command.vala',
   'application/application-contact-store.vala',
   'application/application-contact.vala',
-  'application/autostart-manager.vala',
+  'application/application-startup-manager.vala',
   'application/geary-application.vala',
   'application/geary-config.vala',
   'application/geary-controller.vala',


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