[geary/gnumdk/fix_quit: 10/10] client: Add support for Background portal




commit 31f79816e89bd631b11c2b1ea0fd91c03ec5a3d1
Author: Cédric Bellegarde <cedric.bellegarde@ædishatz.org>
Date:   Tue Aug 23 16:07:26 2022 +0200

    client: Add support for Background portal

 src/client/application/application-client.vala     |   4 +-
 .../application/application-startup-manager.vala   | 101 ++++++++++++++++-----
 .../components/components-preferences-window.vala  |  11 ---
 3 files changed, 78 insertions(+), 38 deletions(-)
---
diff --git a/src/client/application/application-client.vala b/src/client/application/application-client.vala
index 6f9d857ae..04b73f8d0 100644
--- a/src/client/application/application-client.vala
+++ b/src/client/application/application-client.vala
@@ -375,9 +375,7 @@ public class Application.Client : Gtk.Application {
 
         this.engine = new Geary.Engine(get_resource_directory());
         this.config = new Configuration(SCHEMA_ID);
-        this.autostart = new StartupManager(
-            this.config, this.get_desktop_directory()
-        );
+        this.autostart = new StartupManager(this);
 
         // Ensure all geary windows have an icon
         Gtk.Window.set_default_icon_name(APP_ID);
diff --git a/src/client/application/application-startup-manager.vala 
b/src/client/application/application-startup-manager.vala
index 1d7a3b9c2..1c89fee37 100644
--- a/src/client/application/application-startup-manager.vala
+++ b/src/client/application/application-startup-manager.vala
@@ -1,10 +1,31 @@
 /*
+ * Copyright 2022 Cédric Bellegarde <cedric bellegarde adishatz org>
  * 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.
  */
 
+// Background portal
+namespace portal {
+    [DBus(name = "org.freedesktop.portal.Request")]
+    public interface Request : GLib.Object {
+        [DBus(name = "Response")]
+        public signal void response(
+            uint response,
+            GLib.HashTable<string, GLib.Variant> results);
+    }
+
+    [DBus(name = "org.freedesktop.portal.Background")]
+    public interface Background : GLib.Object {
+        [DBus(name = "RequestBackground")]
+        public abstract GLib.ObjectPath request_background(
+            string parent_window,
+            GLib.HashTable<string, GLib.Variant> options)
+        throws DBusError, IOError;
+    }
+}
+
 /*
  * Manages desktop files in the autostart.
  */
@@ -12,13 +33,17 @@ public class Application.StartupManager : GLib.Object {
 
     private const string AUTOSTART_FOLDER = "autostart";
     private const string AUTOSTART_DESKTOP_FILE = "geary-autostart.desktop";
+    private const string BUS_NAME = "org.freedesktop.portal.Desktop";
+    private const string OBJECT_PATH = "/org/freedesktop/portal/desktop";
 
-    private Configuration config;
+
+    private Application.Client app;
     private GLib.File installed_file;
     private GLib.File startup_file;
 
-    public StartupManager(Configuration config, GLib.File desktop_dir) {
-        this.config = config;
+    public StartupManager(Application.Client app) {
+        GLib.File desktop_dir = app.get_desktop_directory();
+        this.app = app;
         this.installed_file = desktop_dir.get_child(AUTOSTART_DESKTOP_FILE);
         this.startup_file = GLib.File.new_for_path(
             GLib.Environment.get_user_config_dir()
@@ -26,7 +51,7 @@ public class Application.StartupManager : GLib.Object {
         .get_child(AUTOSTART_DESKTOP_FILE);
 
         // Connect run-in-background option callback
-        config.settings.changed[Configuration.RUN_IN_BACKGROUND_KEY].connect(
+        app.config.settings.changed[Configuration.RUN_IN_BACKGROUND_KEY].connect(
             on_run_in_background_change
         );
     }
@@ -34,14 +59,52 @@ public class Application.StartupManager : GLib.Object {
     /**
      * Returns the system-wide autostart desktop file if it exists.
      */
-    public GLib.File? get_installed_desktop_file() {
+    private GLib.File? get_installed_desktop_file() {
         return this.installed_file.query_exists() ? this.installed_file : null;
     }
 
+    /**
+     * Request background mode using Background portal
+     */
+    private async void request_background(bool autostart) {
+        try {
+            GLib.DBusConnection bus = yield Bus.get(BusType.SESSION);
+            string[] cmdline = {"geary", "--gapplication-service"};
+            var background = yield bus.get_proxy<portal.Background>(
+                BUS_NAME, OBJECT_PATH);
+            var options = new GLib.HashTable<string, GLib.Variant>(
+                str_hash, str_equal);
+            options.insert("reason", new GLib.Variant(
+                "s", _("Geary wants to run in background")));
+            options.insert("autostart", new GLib.Variant(
+                "b", autostart));
+            options.insert("commandline", new GLib.Variant.strv(cmdline));
+            var handle = background.request_background(_APP_ID, options);
+            yield bus.get_proxy<portal.Request>(BUS_NAME, handle);
+        } catch (GLib.Error error) {
+            warning("Failed to request to run in background: %s", error.message);
+        }
+    }
+
+    /**
+     * Handle autostart file installation
+     */
+    private async void handle_autostart(bool install) {
+       try {
+            if (install) {
+                install_startup_file();
+            } else {
+                delete_startup_file();
+            }
+        } catch (GLib.Error err) {
+            warning("Failed to update autostart desktop file: %s", err.message);
+        }
+    }
+
     /**
      * Copies the autostart desktop file to the autostart directory.
      */
-    public void install_startup_file() throws GLib.Error {
+    private 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()) {
@@ -59,7 +122,7 @@ public class Application.StartupManager : GLib.Object {
     /**
      * Deletes the desktop file from autostart directory.
      */
-    public void delete_startup_file() throws GLib.Error {
+    private void delete_startup_file() throws GLib.Error {
         try {
             this.startup_file.delete();
         } catch (GLib.IOError.NOT_FOUND err) {
@@ -67,25 +130,15 @@ public class Application.StartupManager : GLib.Object {
         }
     }
 
-    /*
-     * 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).
+    /**
+     * Install background/autostart support depending on current
+     * execution environment
      */
-    public void sync_with_config() {
-        this.config.run_in_background = this.startup_file.query_exists();
-    }
-
     private void on_run_in_background_change() {
-        try {
-            if (this.config.run_in_background) {
-                install_startup_file();
-            } else {
-                delete_startup_file();
-            }
-        } catch (GLib.Error err) {
-            warning("Failed to update autostart desktop file: %s", err.message);
+        if (this.app.is_flatpak_sandboxed) {
+            request_background.begin(this.app.config.run_in_background);
+        } else {
+            handle_autostart.begin(this.app.config.run_in_background);
         }
     }
 
diff --git a/src/client/components/components-preferences-window.vala 
b/src/client/components/components-preferences-window.vala
index 69d6d6d3b..b2f7be9bb 100644
--- a/src/client/components/components-preferences-window.vala
+++ b/src/client/components/components-preferences-window.vala
@@ -228,8 +228,6 @@ public class Components.PreferencesWindow : Hdy.PreferencesWindow {
                 (GLib.SettingsBindSetMappingShared) settings_trust_images_setter
             );
         }
-
-        this.delete_event.connect(on_delete);
     }
 
     private void add_plugin_pane() {
@@ -261,15 +259,6 @@ public class Components.PreferencesWindow : Hdy.PreferencesWindow {
         close();
     }
 
-    private bool on_delete() {
-        // Sync startup notification option with file state
-        Application.Client? application = this.application;
-        if (application != null) {
-            application.autostart.sync_with_config();
-        }
-        return Gdk.EVENT_PROPAGATE;
-    }
-
     private static bool settings_trust_images_getter(GLib.Value value, GLib.Variant variant, void* 
user_data) {
         var domains = variant.get_strv();
         value.set_boolean(domains.length > 0 && domains[0] == "*");


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