[gnome-break-timer] Use the Background portal to enable autostart



commit 2a22319f248f6433b2979b4eecebe0079845d28d
Author: Dylan McCall <dylan dylanmccall ca>
Date:   Fri Nov 13 20:56:52 2020 -0800

    Use the Background portal to enable autostart

 data/applications/meson.build                      | 22 +-------
 ....desktop.in => org.gnome.BreakTimer.desktop.in} |  2 +-
 data/autostart/meson.build                         | 20 ++++++++
 .../org.gnome.BreakTimer.desktop.in}               |  4 +-
 data/dbus/meson.build                              |  4 +-
 data/dbus/org.gnome.BreakTimer.Daemon.service.in   |  3 --
 data/dbus/org.gnome.BreakTimer.service.in          |  3 ++
 data/metainfo/meson.build                          |  1 -
 meson.build                                        |  5 +-
 src/common/IBackgroundPortal.vala                  | 25 +++++++++
 src/common/IMutterActivityMonitor.vala             | 31 ++++++++++++
 src/common/meson.build                             |  2 +
 .../MutterActivityMonitorBackend.vala              | 11 ----
 src/meson.build                                    |  2 +-
 src/settings/BreakManager.vala                     | 59 ++++++++++++++++++----
 src/settings/MainWindow.vala                       |  1 +
 16 files changed, 142 insertions(+), 53 deletions(-)
---
diff --git a/data/applications/meson.build b/data/applications/meson.build
index d0ce506..ca27708 100644
--- a/data/applications/meson.build
+++ b/data/applications/meson.build
@@ -20,8 +20,8 @@ if desktop_file_validate.found()
 endif
 
 daemon_desktop_file = i18n.merge_file(
-    input: 'org.gnome.BreakTimer.Daemon.desktop.in',
-    output: 'org.gnome.BreakTimer.Daemon.desktop',
+    input: 'org.gnome.BreakTimer.desktop.in',
+    output: 'org.gnome.BreakTimer.desktop',
     type: 'desktop',
     po_dir: po_dir,
     install: true,
@@ -35,21 +35,3 @@ if desktop_file_validate.found()
         args: [daemon_desktop_file]
     )
 endif
-
-
-daemon_autostart_desktop_file = i18n.merge_file(
-    input: 'org.gnome.BreakTimer.Daemon-autostart.desktop.in',
-    output: 'org.gnome.BreakTimer.Daemon-autostart.desktop',
-    type: 'desktop',
-    po_dir: po_dir,
-    install: true,
-    install_dir: applications_dir
-)
-
-if desktop_file_validate.found()
-    test(
-        'Validate desktop file',
-        desktop_file_validate,
-        args: [daemon_autostart_desktop_file]
-    )
-endif
diff --git a/data/applications/org.gnome.BreakTimer.Daemon.desktop.in 
b/data/applications/org.gnome.BreakTimer.desktop.in
similarity index 86%
rename from data/applications/org.gnome.BreakTimer.Daemon.desktop.in
rename to data/applications/org.gnome.BreakTimer.desktop.in
index a2665bb..6dfc144 100644
--- a/data/applications/org.gnome.BreakTimer.Daemon.desktop.in
+++ b/data/applications/org.gnome.BreakTimer.desktop.in
@@ -4,6 +4,6 @@ Name=Break Timer
 Comment=Provides a gentle reminder to take regular breaks
 Icon=org.gnome.BreakTimer
 NoDisplay=true
-Exec=gnome-break-timer-service
+Exec=gnome-break-timer-daemon
 DBusActivatable=true
 X-GNOME-UsesNotifications=true
diff --git a/data/autostart/meson.build b/data/autostart/meson.build
new file mode 100644
index 0000000..443e848
--- /dev/null
+++ b/data/autostart/meson.build
@@ -0,0 +1,20 @@
+autostart_dir = join_paths(sysconf_dir, 'xdg', 'autostart')
+
+desktop_file_validate = find_program('desktop-file-validate', required: false)
+
+daemon_autostart_desktop_file = i18n.merge_file(
+    input: 'org.gnome.BreakTimer.desktop.in',
+    output: 'org.gnome.BreakTimer.desktop',
+    type: 'desktop',
+    po_dir: po_dir,
+    install: true,
+    install_dir: applications_dir
+)
+
+if desktop_file_validate.found()
+    test(
+        'Validate desktop file',
+        desktop_file_validate,
+        args: [daemon_autostart_desktop_file]
+    )
+endif
diff --git a/data/applications/org.gnome.BreakTimer.Daemon-autostart.desktop.in 
b/data/autostart/org.gnome.BreakTimer.desktop.in
similarity index 80%
rename from data/applications/org.gnome.BreakTimer.Daemon-autostart.desktop.in
rename to data/autostart/org.gnome.BreakTimer.desktop.in
index be11258..050bd02 100644
--- a/data/applications/org.gnome.BreakTimer.Daemon-autostart.desktop.in
+++ b/data/autostart/org.gnome.BreakTimer.desktop.in
@@ -4,8 +4,8 @@ Name=Break Timer
 Comment=Provides a gentle reminder to take regular breaks
 Icon=org.gnome.BreakTimer
 NoDisplay=true
-Exec=@BINDIR@/gnome-break-timer-service
+Exec=gnome-break-timer-daemon
 AutostartCondition=GSettings org.gnome.BreakTimer enabled
 X-GNOME-Autostart-Phase=Applications
 X-GNOME-Autostart-Delay=30
-X-GNOME-AutoRestart=true
\ No newline at end of file
+X-GNOME-AutoRestart=true
diff --git a/data/dbus/meson.build b/data/dbus/meson.build
index 687e2db..c703630 100644
--- a/data/dbus/meson.build
+++ b/data/dbus/meson.build
@@ -8,8 +8,8 @@ configure_file(
 )
 
 configure_file(
-    input: 'org.gnome.BreakTimer.Daemon.service.in',
-    output: 'org.gnome.BreakTimer.Daemon.service',
+    input: 'org.gnome.BreakTimer.service.in',
+    output: 'org.gnome.BreakTimer.service',
     configuration: build_conf,
     install_dir: dbus_session_bus_services_dir
 )
diff --git a/data/dbus/org.gnome.BreakTimer.service.in b/data/dbus/org.gnome.BreakTimer.service.in
new file mode 100644
index 0000000..c120283
--- /dev/null
+++ b/data/dbus/org.gnome.BreakTimer.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.gnome.BreakTimer
+Exec=gnome-break-timer-daemon
diff --git a/data/metainfo/meson.build b/data/metainfo/meson.build
index 84dbfd5..231bdeb 100644
--- a/data/metainfo/meson.build
+++ b/data/metainfo/meson.build
@@ -5,7 +5,6 @@ metainfo_file = i18n.merge_file(
     output: 'org.gnome.BreakTimer.metainfo.xml',
     type: 'xml',
     po_dir: po_dir,
-    configuration: build_conf,
     install: true,
     install_dir: metainfo_dir
 )
diff --git a/meson.build b/meson.build
index 7d1f012..d9adefb 100644
--- a/meson.build
+++ b/meson.build
@@ -33,12 +33,13 @@ libdir = join_paths(prefix, get_option('libdir'))
 datadir = join_paths(prefix, get_option('datadir'))
 libexecdir = join_paths(prefix, get_option('libexecdir'))
 locale_dir = join_paths(prefix, get_option('localedir'))
+sysconf_dir = get_option('sysconfdir')
 
 application_icon = 'org.gnome.BreakTimer'
 application_url = 'https://wiki.gnome.org/Apps/BreakTimer'
 settings_application_id = 'org.gnome.BreakTimer.Settings'
-daemon_application_id = 'org.gnome.BreakTimer.Daemon'
-daemon_bus_name = 'org.gnome.BreakTimer.Daemon'
+daemon_application_id = 'org.gnome.BreakTimer'
+daemon_bus_name = 'org.gnome.BreakTimer'
 daemon_object_path = '/org/gnome/BreakTimer'
 daemon_break_object_base_path = '/org/gnome/BreakTimer/'
 
diff --git a/src/common/IBackgroundPortal.vala b/src/common/IBackgroundPortal.vala
new file mode 100644
index 0000000..e43a64a
--- /dev/null
+++ b/src/common/IBackgroundPortal.vala
@@ -0,0 +1,25 @@
+/*
+ * This file is part of GNOME Break Timer.
+ *
+ * GNOME Break Timer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GNOME Break Timer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNOME Break Timer.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace BreakTimer {
+
+[DBus (name = "org.freedesktop.portal.Background")]
+public interface IBackgroundPortal : Object {
+    public abstract GLib.ObjectPath request_background(string parent_window, GLib.HashTable<string, Variant> 
options) throws GLib.DBusError, GLib.IOError;
+}
+
+}
diff --git a/src/common/IMutterActivityMonitor.vala b/src/common/IMutterActivityMonitor.vala
new file mode 100644
index 0000000..0e2301a
--- /dev/null
+++ b/src/common/IMutterActivityMonitor.vala
@@ -0,0 +1,31 @@
+/*
+ * This file is part of GNOME Break Timer.
+ *
+ * GNOME Break Timer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GNOME Break Timer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNOME Break Timer.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace BreakTimer {
+
+[DBus (name = "org.gnome.Mutter.IdleMonitor")]
+public interface IMutterIdleMonitor : Object {
+    public abstract uint32 add_idle_watch(uint64 interval_ms) throws GLib.DBusError, GLib.IOError;
+    public abstract uint32 add_user_active_watch() throws GLib.DBusError, GLib.IOError;
+    public abstract uint64 get_idletime() throws GLib.DBusError, GLib.IOError;
+    public abstract void remove_watch(uint32 id) throws GLib.DBusError, GLib.IOError;
+    public abstract void reset_idletime() throws GLib.DBusError, GLib.IOError;
+
+    public signal void watch_fired (uint32 id);
+}
+
+}
diff --git a/src/common/meson.build b/src/common/meson.build
index 2dc2124..615d76f 100644
--- a/src/common/meson.build
+++ b/src/common/meson.build
@@ -1,5 +1,7 @@
 common_sources = files(
+    'IBackgroundPortal.vala',
     'IBreakDaemon.vala',
+    'IMutterActivityMonitor.vala',
     'ISessionStatus.vala',
     'NaturalTime.vala'
 )
diff --git a/src/daemon/activity-monitor/MutterActivityMonitorBackend.vala 
b/src/daemon/activity-monitor/MutterActivityMonitorBackend.vala
index 7134d89..3eec160 100644
--- a/src/daemon/activity-monitor/MutterActivityMonitorBackend.vala
+++ b/src/daemon/activity-monitor/MutterActivityMonitorBackend.vala
@@ -17,17 +17,6 @@
 
 namespace BreakTimer.Daemon {
 
-[DBus (name = "org.gnome.Mutter.IdleMonitor")]
-public interface IMutterIdleMonitor : Object {
-    public abstract uint32 add_idle_watch(uint64 interval_ms) throws GLib.DBusError, GLib.IOError;
-    public abstract uint32 add_user_active_watch() throws GLib.DBusError, GLib.IOError;
-    public abstract uint64 get_idletime() throws GLib.DBusError, GLib.IOError;
-    public abstract void remove_watch(uint32 id) throws GLib.DBusError, GLib.IOError;
-    public abstract void reset_idletime() throws GLib.DBusError, GLib.IOError;
-
-    public signal void watch_fired (uint32 id);
-}
-
 public class MutterActivityMonitorBackend : ActivityMonitorBackend {
     private IMutterIdleMonitor? mutter_idle_monitor;
     private uint32 idle_watch_id;
diff --git a/src/meson.build b/src/meson.build
index 9940263..b871826 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -45,7 +45,7 @@ executable(
 )
 
 executable(
-    'gnome-break-timer-service',
+    'gnome-break-timer-daemon',
     sources : [
         daemon_sources
     ],
diff --git a/src/settings/BreakManager.vala b/src/settings/BreakManager.vala
index 9f0c808..8352c60 100644
--- a/src/settings/BreakManager.vala
+++ b/src/settings/BreakManager.vala
@@ -33,31 +33,73 @@ public class BreakManager : Object {
     public string[] selected_break_ids { get; set; }
     public BreakType? foreground_break { get; private set; }
 
+    IBackgroundPortal? background_portal = null;
+
     public BreakManager (SettingsApplication application) {
         this.application = application;
         this.breaks = new Gee.HashMap<string, BreakType> ();
         this.breaks_ordered = new GLib.List<BreakType> ();
 
+        if (this.get_is_in_flatpak ()) {
+            try {
+                this.background_portal = Bus.get_proxy_sync (
+                    BusType.SESSION,
+                    "org.freedesktop.portal.Desktop",
+                    "/org/freedesktop/portal/desktop"
+                );
+            } catch (GLib.IOError error) {
+                GLib.warning ("Error connecting to xdg desktop portal: %s", error.message);
+            }
+        }
+
         this.settings = new GLib.Settings ("org.gnome.BreakTimer");
         this.settings.bind ("enabled", this, "master-enabled", SettingsBindFlags.DEFAULT);
         this.settings.bind ("selected-breaks", this, "selected-break-ids", SettingsBindFlags.DEFAULT);
 
-        // We choose not too send a signal when master_enabled changes because
+        // We choose not to send a signal when master_enabled changes because
         // we might be starting the break daemon at the same time, so the
         // value of is_working () could fluctuate unpleasantly.
         //this.notify["master-enabled"].connect ( () => { this.status_changed (); });
-        this.notify["master-enabled"].connect ( () => {
-            // Launch the break timer service if the break manager is enabled
-            // TODO: this is redundant, because gnome-session autostarts the
-            // service. However, it is unclear if we should rely on it.
-            if (this.master_enabled) this.launch_break_timer_service ();
-        });
+        this.notify["master-enabled"].connect ( this.on_master_enabled_changed );
     }
 
     public signal void break_status_available ();
     public signal void break_added (BreakType break_type);
     public signal void status_changed ();
 
+    private void on_master_enabled_changed () {
+        // Launch the break timer service if the break manager is enabled
+        if (this.master_enabled) {
+            this.launch_break_timer_service ();
+        }
+
+        if (this.background_portal != null) {
+            HashTable<string, Variant> options = new HashTable<string, Variant> (str_hash, str_equal);
+            GLib.Variant commandline_variant = new GLib.Variant.strv ({"gnome-break-timer-daemon"});
+            options.insert ("autostart", this.master_enabled);
+            options.insert ("commandline", commandline_variant);
+            // RequestBackground creates a desktop file with the same name as
+            // the flatpak, which happens to be the dbus name of the daemon
+            // (although it is not the dbus name of the settings application).
+            options.insert ("dbus-activatable", true);
+
+            try {
+                // We don't have a nice way to generate a window handle, but the
+                // background portal isn't using it at the moment.
+                this.background_portal.request_background("", options);
+            } catch (IOError error) {
+                GLib.warning ("Error connecting to xdg desktop portal: %s", error.message);
+            } catch (GLib.DBusError error) {
+                GLib.warning ("Error enabling autostart: %s", error.message);
+            }
+        }
+    }
+
+    private bool get_is_in_flatpak () {
+        string flatpak_info_path = GLib.Path.build_filename (GLib.Environment.get_user_runtime_dir (), 
"flatpak-info");
+        return GLib.FileUtils.test (flatpak_info_path, GLib.FileTest.EXISTS);
+    }
+
     public void load_breaks () {
         this.add_break (new MicroBreakType ());
         this.add_break (new RestBreakType ());
@@ -139,10 +181,7 @@ public class BreakManager : Object {
     }
 
     private void launch_break_timer_service () {
-        stdout.printf ("Trying to launch: %s\n", Config.DAEMON_DESKTOP_FILE_ID);
         AppInfo daemon_app_info = new DesktopAppInfo (Config.DAEMON_DESKTOP_FILE_ID);
-        stdout.printf ("daemon_app_info: %s\n", daemon_app_info.get_name());
-        stdout.printf ("daemon_app_info: %s\n", daemon_app_info.get_commandline());
         AppLaunchContext app_launch_context = new AppLaunchContext ();
         try {
             daemon_app_info.launch (null, app_launch_context);
diff --git a/src/settings/MainWindow.vala b/src/settings/MainWindow.vala
index ff4f808..7ba8768 100644
--- a/src/settings/MainWindow.vala
+++ b/src/settings/MainWindow.vala
@@ -39,6 +39,7 @@ public class MainWindow : Gtk.ApplicationWindow {
         this.break_manager = break_manager;
 
         this.set_title ( _("Break Timer"));
+        this.set_default_size (850, 400);
 
         Gtk.Builder builder = new Gtk.Builder ();
         try {


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