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




commit 814b38a703e0dc38bf2e83873ffc193addc49b54
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   | 69 ++++++++++++++++++----
 .../components/components-preferences-window.vala  | 11 ----
 3 files changed, 60 insertions(+), 24 deletions(-)
---
diff --git a/src/client/application/application-client.vala b/src/client/application/application-client.vala
index 6f9d857ae..6dce446b0 100644
--- a/src/client/application/application-client.vala
+++ b/src/client/application/application-client.vala
@@ -78,6 +78,7 @@ public class Application.Client : Gtk.Application {
     private const string OPTION_NEW_WINDOW = "new-window";
     private const string OPTION_QUIT = "quit";
     private const string OPTION_REVOKE_CERTS = "revoke-certs";
+    private const string OPTION_SERVICE = "gapplication-service";
 
     private const ActionEntry[] ACTION_ENTRIES = {
         { Action.Application.ABOUT, on_activate_about},
@@ -1049,6 +1050,7 @@ public class Application.Client : Gtk.Application {
             return 0;
         }
 
+        // As a service, mark us as already activated
         bool activated = false;
 
         // Suppress some noisy domains from third-party libraries
@@ -1122,7 +1124,7 @@ public class Application.Client : Gtk.Application {
         this.config.revoke_certs = options.contains(OPTION_REVOKE_CERTS);
 
         if (!activated) {
-            activate();
+            //activate();
         }
 
         return -1;
diff --git a/src/client/application/application-startup-manager.vala 
b/src/client/application/application-startup-manager.vala
index 1d7a3b9c2..410342098 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,6 +33,9 @@ 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 GLib.File installed_file;
@@ -34,14 +58,36 @@ 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);
+            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 ("dbus-activatable", new GLib.Variant(
+                "b", true));
+            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);
+        }
+    }
+
     /**
      * 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 +105,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,16 +113,10 @@ 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).
+    /**
+     * Background request isn't supported by non flatpak apps for now.
+     * Keep desktop file support until it is.
      */
-    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) {
@@ -87,6 +127,11 @@ public class Application.StartupManager : GLib.Object {
         } catch (GLib.Error err) {
             warning("Failed to update autostart desktop file: %s", err.message);
         }
+        if (this.config.run_in_background) {
+            request_background.begin(true);
+        } else {
+            request_background.begin(false);
+        }
     }
 
 }
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]