[gnome-break-timer] Cosmetic tweaks around dbus connections



commit bc8f4dc006b0e05b64cce149241f13f1078600c1
Author: Dylan McCall <dylan dylanmccall ca>
Date:   Wed Nov 18 21:39:41 2020 -0800

    Cosmetic tweaks around dbus connections

 .../{IScreenSaver.vala => IGnomeScreenSaver.vala}  |  6 ++--
 ...ctivityMonitor.vala => IMutterIdleMonitor.vala} | 12 ++++----
 ...ackgroundPortal.vala => IPortalBackground.vala} |  4 +--
 src/common/NaturalTime.vala                        |  2 +-
 src/common/meson.build                             |  6 ++--
 src/daemon/Application.vala                        | 24 ++++++++++++---
 src/daemon/BreakManager.vala                       | 31 ++++++-------------
 src/daemon/SessionStatus.vala                      | 19 ++++++++----
 src/daemon/activity/ActivityMonitor.vala           |  6 +++-
 src/daemon/activity/ActivityMonitorBackend.vala    |  6 +++-
 .../activity/MutterActivityMonitorBackend.vala     | 23 +++++++++-----
 src/daemon/timerbreak/TimerBreakType.vala          | 35 +++++++---------------
 src/settings/BreakManager.vala                     | 29 +++++++++---------
 src/settings/MainWindow.vala                       |  4 +++
 src/settings/timerbreak/TimerBreakType.vala        | 13 ++++----
 15 files changed, 119 insertions(+), 101 deletions(-)
---
diff --git a/src/common/IScreenSaver.vala b/src/common/IGnomeScreenSaver.vala
similarity index 95%
rename from src/common/IScreenSaver.vala
rename to src/common/IGnomeScreenSaver.vala
index d0e3a3d..a0b8025 100644
--- a/src/common/IScreenSaver.vala
+++ b/src/common/IGnomeScreenSaver.vala
@@ -18,13 +18,13 @@
 namespace BreakTimer.Common {
 
 [DBus (name = "org.gnome.ScreenSaver")]
-public interface IScreenSaver : GLib.Object {
+public interface IGnomeScreenSaver : GLib.Object {
+    public signal void active_changed (bool active);
+
     public abstract bool get_active () throws GLib.DBusError, GLib.IOError;
     public abstract uint32 get_active_time () throws GLib.DBusError, GLib.IOError;
     public abstract void lock () throws GLib.DBusError, GLib.IOError;
     public abstract void set_active (bool active) throws GLib.DBusError, GLib.IOError;
-
-    public signal void active_changed (bool active);
 }
 
 }
diff --git a/src/common/IMutterActivityMonitor.vala b/src/common/IMutterIdleMonitor.vala
similarity index 67%
rename from src/common/IMutterActivityMonitor.vala
rename to src/common/IMutterIdleMonitor.vala
index 215e940..cbdeb3d 100644
--- a/src/common/IMutterActivityMonitor.vala
+++ b/src/common/IMutterIdleMonitor.vala
@@ -19,13 +19,13 @@ namespace BreakTimer.Common {
 
 [DBus (name = "org.gnome.Mutter.IdleMonitor")]
 public interface IMutterIdleMonitor : GLib.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 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;
 }
 
 }
diff --git a/src/common/IBackgroundPortal.vala b/src/common/IPortalBackground.vala
similarity index 79%
rename from src/common/IBackgroundPortal.vala
rename to src/common/IPortalBackground.vala
index 9261356..049a18b 100644
--- a/src/common/IBackgroundPortal.vala
+++ b/src/common/IPortalBackground.vala
@@ -18,8 +18,8 @@
 namespace BreakTimer.Common {
 
 [DBus (name = "org.freedesktop.portal.Background")]
-public interface IBackgroundPortal : GLib.Object {
-    public abstract GLib.ObjectPath request_background(string parent_window, GLib.HashTable<string, Variant> 
options) throws GLib.DBusError, GLib.IOError;
+public interface IPortalBackground : GLib.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/NaturalTime.vala b/src/common/NaturalTime.vala
index 4bf0d7f..a08685d 100644
--- a/src/common/NaturalTime.vala
+++ b/src/common/NaturalTime.vala
@@ -31,7 +31,7 @@ public class NaturalTime : GLib.Object {
 
         public string format_seconds (int seconds, out int output_value) {
             output_value = seconds / this.seconds;
-            return this.format_time(output_value);
+            return this.format_time (output_value);
         }
     }
 
diff --git a/src/common/meson.build b/src/common/meson.build
index 520d655..acd652b 100644
--- a/src/common/meson.build
+++ b/src/common/meson.build
@@ -1,9 +1,9 @@
 common_sources = files(
-    'IBackgroundPortal.vala',
     'IBreakTimer_TimerBreak.vala',
     'IBreakTimer.vala',
-    'IMutterActivityMonitor.vala',
-    'IScreenSaver.vala',
+    'IGnomeScreenSaver.vala',
+    'IMutterIdleMonitor.vala',
+    'IPortalBackground.vala',
     'ISessionStatus.vala',
     'NaturalTime.vala'
 )
diff --git a/src/daemon/Application.vala b/src/daemon/Application.vala
index 8cbf723..040ab98 100644
--- a/src/daemon/Application.vala
+++ b/src/daemon/Application.vala
@@ -28,7 +28,7 @@ public class Application : Gtk.Application {
     private const int ACTIVITY_TIMEOUT_MS = 60000;
 
     private BreakManager break_manager;
-    private ISessionStatus session_status;
+    private SessionStatus session_status;
     private ActivityMonitorBackend activity_monitor_backend;
     private ActivityMonitor activity_monitor;
     private UIManager ui_manager;
@@ -69,22 +69,38 @@ public class Application : Gtk.Application {
         );
 
         this.session_status = new SessionStatus (this);
+        try {
+            this.session_status.init (null);
+        } catch (GLib.Error error) {
+            GLib.error ("Error initializing session_status: %s", error.message);
+        }
 
         this.activity_monitor_backend = new MutterActivityMonitorBackend ();
+        try {
+            this.activity_monitor_backend.init (null);
+        } catch (GLib.Error error) {
+            GLib.error ("Error initializing activity_monitor_backend: %s", error.message);
+        }
+
         this.activity_monitor = new ActivityMonitor (session_status, activity_monitor_backend);
+        try {
+            this.activity_monitor.init (null);
+        } catch (GLib.Error error) {
+            GLib.error ("Error initializing activity_monitor: %s", error.message);
+        }
 
         this.ui_manager = new UIManager (this, session_status);
         try {
-            this.ui_manager.init ();
+            this.ui_manager.init (null);
         } catch (GLib.Error error) {
-            GLib.error("Error initializing ui_manager: %s", error.message);
+            GLib.error ("Error initializing ui_manager: %s", error.message);
         }
 
         this.break_manager = new BreakManager (ui_manager, activity_monitor);
         try {
             this.break_manager.init (null);
         } catch (GLib.Error error) {
-            GLib.error("Error initializing break_manager: %s", error.message);
+            GLib.error ("Error initializing break_manager: %s", error.message);
         }
 
         this.restore_state ();
diff --git a/src/daemon/BreakManager.vala b/src/daemon/BreakManager.vala
index 6df7e38..3d76803 100644
--- a/src/daemon/BreakManager.vala
+++ b/src/daemon/BreakManager.vala
@@ -23,11 +23,13 @@ using BreakTimer.Daemon.RestBreak;
 namespace BreakTimer.Daemon {
 
 public class BreakManager : GLib.Object, GLib.Initable {
-    private GLib.Settings settings;
-    private GLib.HashTable<string, BreakType> breaks;
     public bool master_enabled { get; set; }
     public string[] selected_break_ids { get; set; }
 
+    private GLib.DBusConnection dbus_connection;
+    private GLib.Settings settings;
+    private GLib.HashTable<string, BreakType> breaks;
+
     public BreakManager (UIManager ui_manager, ActivityMonitor activity_monitor) {
         this.settings = new GLib.Settings ("org.gnome.BreakTimer");
 
@@ -44,27 +46,12 @@ public class BreakManager : GLib.Object, GLib.Initable {
     }
 
     public bool init (GLib.Cancellable? cancellable) throws GLib.Error {
-        GLib.DBusConnection connection;
-
-        try {
-            connection = GLib.Bus.get_sync (
-                GLib.BusType.SESSION,
-                null
-            );
-        } catch (GLib.IOError error) {
-            GLib.warning ("Error connecting to the session bus: %s", error.message);
-            throw error;
-        }
+        this.dbus_connection = GLib.Bus.get_sync(GLib.BusType.SESSION, cancellable);
 
-        try {
-            connection.register_object (
-                Config.DAEMON_OBJECT_PATH,
-                new BreakManagerDBusObject (this)
-            );
-        } catch (GLib.IOError error) {
-            GLib.warning ("Error registering daemon on the session bus: %s", error.message);
-            throw error;
-        }
+        this.dbus_connection.register_object (
+            Config.DAEMON_OBJECT_PATH,
+            new BreakManagerDBusObject (this)
+        );
 
         foreach (BreakType break_type in this.all_breaks ()) {
             break_type.init (cancellable);
diff --git a/src/daemon/SessionStatus.vala b/src/daemon/SessionStatus.vala
index b2f8cce..a927384 100644
--- a/src/daemon/SessionStatus.vala
+++ b/src/daemon/SessionStatus.vala
@@ -23,26 +23,33 @@ namespace BreakTimer.Daemon {
  * Abstraction of GNOME Screensaver's dbus interface with gentle defaults in
  * case we are unable to connect.
  */
-public class SessionStatus : GLib.Object, ISessionStatus {
+public class SessionStatus : GLib.Object, ISessionStatus, GLib.Initable {
     private Gtk.Application application;
-    private IScreenSaver? screensaver;
+    private GLib.DBusConnection dbus_connection;
+    private IGnomeScreenSaver? screensaver;
     private bool screensaver_is_active = false;
 
     public SessionStatus (Gtk.Application application) {
         this.application = application;
-        GLib.Bus.watch_name (
-            GLib.BusType.SESSION,
+    }
+
+    public bool init (GLib.Cancellable? cancellable) throws GLib.Error {
+        this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+
+        GLib.Bus.watch_name_on_connection (
+            this.dbus_connection,
             "org.gnome.ScreenSaver",
             GLib.BusNameWatcherFlags.NONE,
             this.screensaver_appeared,
             this.screensaver_disappeared
         );
+
+        return true;
     }
 
     private void screensaver_appeared () {
         try {
-            this.screensaver = GLib.Bus.get_proxy_sync (
-                GLib.BusType.SESSION,
+            this.screensaver = this.dbus_connection.get_proxy_sync (
                 "org.gnome.ScreenSaver",
                 "/org/gnome/ScreenSaver"
             );
diff --git a/src/daemon/activity/ActivityMonitor.vala b/src/daemon/activity/ActivityMonitor.vala
index c2bac3e..b368f0b 100644
--- a/src/daemon/activity/ActivityMonitor.vala
+++ b/src/daemon/activity/ActivityMonitor.vala
@@ -20,7 +20,7 @@ using BreakTimer.Daemon.Util;
 
 namespace BreakTimer.Daemon.Activity {
 
-public class ActivityMonitor : GLib.Object {
+public class ActivityMonitor : GLib.Object, GLib.Initable {
     private PausableTimeout poll_activity_timeout;
     private UserActivity last_activity;
     private int64 last_active_timestamp;
@@ -41,6 +41,10 @@ public class ActivityMonitor : GLib.Object {
         this.last_activity = UserActivity ();
     }
 
+    public bool init (GLib.Cancellable? cancellable) throws GLib.Error {
+        return true;
+    }
+
     public Json.Object serialize () {
         Json.Object json_root = new Json.Object ();
         json_root.set_int_member ("last_active_timestamp", this.last_active_timestamp);
diff --git a/src/daemon/activity/ActivityMonitorBackend.vala b/src/daemon/activity/ActivityMonitorBackend.vala
index 26740b6..208965d 100644
--- a/src/daemon/activity/ActivityMonitorBackend.vala
+++ b/src/daemon/activity/ActivityMonitorBackend.vala
@@ -20,10 +20,14 @@ using BreakTimer.Daemon.Util;
 
 namespace BreakTimer.Daemon.Activity {
 
-public abstract class ActivityMonitorBackend : GLib.Object {
+public abstract class ActivityMonitorBackend : GLib.Object, GLib.Initable {
     private int64 last_real_time = 0;
     private int64 last_monotonic_time = 0;
 
+    public virtual bool init (GLib.Cancellable? cancellable) throws GLib.Error {
+        return true;
+    }
+
     public virtual Json.Object serialize () {
         Json.Object json_root = new Json.Object ();
         json_root.set_int_member ("last_real_time", this.last_real_time);
diff --git a/src/daemon/activity/MutterActivityMonitorBackend.vala 
b/src/daemon/activity/MutterActivityMonitorBackend.vala
index b58bac2..2a12e74 100644
--- a/src/daemon/activity/MutterActivityMonitorBackend.vala
+++ b/src/daemon/activity/MutterActivityMonitorBackend.vala
@@ -20,7 +20,9 @@ using BreakTimer.Daemon.Util;
 
 namespace BreakTimer.Daemon.Activity {
 
-public class MutterActivityMonitorBackend : ActivityMonitorBackend {
+public class MutterActivityMonitorBackend : ActivityMonitorBackend, GLib.Initable {
+    private GLib.DBusConnection dbus_connection;
+
     private IMutterIdleMonitor? mutter_idle_monitor;
     private uint32 idle_watch_id;
     private uint32 user_active_watch_id;
@@ -33,13 +35,6 @@ public class MutterActivityMonitorBackend : ActivityMonitorBackend {
 
     public MutterActivityMonitorBackend () {
         this.user_is_active = false;
-        GLib.Bus.watch_name (
-            GLib.BusType.SESSION,
-            "org.gnome.Mutter.IdleMonitor",
-            GLib.BusNameWatcherFlags.NONE,
-            this.mutter_idle_monitor_appeared,
-            this.mutter_idle_monitor_disappeared
-        );
     }
 
     ~MutterActivityMonitorBackend() {
@@ -48,6 +43,18 @@ public class MutterActivityMonitorBackend : ActivityMonitorBackend {
         }
     }
 
+    public override bool init (GLib.Cancellable? cancellable) throws GLib.Error {
+        this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+        GLib.Bus.watch_name_on_connection (
+            this.dbus_connection,
+            "org.gnome.Mutter.IdleMonitor",
+            GLib.BusNameWatcherFlags.NONE,
+            this.mutter_idle_monitor_appeared,
+            this.mutter_idle_monitor_disappeared
+        );
+        return true;
+    }
+
     private void mutter_idle_monitor_appeared () {
         try {
             this.mutter_idle_monitor = GLib.Bus.get_proxy_sync (
diff --git a/src/daemon/timerbreak/TimerBreakType.vala b/src/daemon/timerbreak/TimerBreakType.vala
index ec5287c..b0f29b0 100644
--- a/src/daemon/timerbreak/TimerBreakType.vala
+++ b/src/daemon/timerbreak/TimerBreakType.vala
@@ -20,35 +20,22 @@ using BreakTimer.Daemon.Break;
 namespace BreakTimer.Daemon.TimerBreak {
 
 public abstract class TimerBreakType : BreakType {
+    private GLib.DBusConnection dbus_connection;
+
     protected TimerBreakType (string id, BreakController break_controller, BreakView break_view) {
         base (id, break_controller, break_view);
     }
 
     public override bool init (GLib.Cancellable? cancellable) throws GLib.Error {
-        GLib.DBusConnection connection;
-
-        try {
-            connection = GLib.Bus.get_sync (
-                GLib.BusType.SESSION,
-                null
-            );
-        } catch (GLib.IOError error) {
-            GLib.warning ("Error connecting to the session bus: %s", error.message);
-            throw error;
-        }
-
-        try {
-            connection.register_object (
-                Config.DAEMON_BREAK_OBJECT_BASE_PATH+this.id,
-                new TimerBreakDBusObject (
-                    (TimerBreakController) this.break_controller,
-                    (TimerBreakView) this.break_view
-                )
-            );
-        } catch (GLib.IOError error) {
-            GLib.warning ("Error registering break type on the session bus: %s", error.message);
-            throw error;
-        }
+        this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+
+        this.dbus_connection.register_object (
+            Config.DAEMON_BREAK_OBJECT_BASE_PATH+this.id,
+            new TimerBreakDBusObject (
+                (TimerBreakController) this.break_controller,
+                (TimerBreakView) this.break_view
+            )
+        );
 
         return base.init (cancellable);
     }
diff --git a/src/settings/BreakManager.vala b/src/settings/BreakManager.vala
index 6422f7f..f5ce425 100644
--- a/src/settings/BreakManager.vala
+++ b/src/settings/BreakManager.vala
@@ -37,7 +37,9 @@ public class BreakManager : GLib.Object {
     public string[] selected_break_ids { get; set; }
     public BreakType? foreground_break { get; private set; }
 
-    IBackgroundPortal? background_portal = null;
+    private GLib.DBusConnection dbus_connection;
+
+    private IPortalBackground? background_portal = null;
 
     public signal void break_status_available ();
     public signal void status_changed ();
@@ -58,23 +60,21 @@ public class BreakManager : GLib.Object {
     }
 
     public bool init (GLib.Cancellable? cancellable) throws GLib.Error {
+        this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+
         if (this.get_is_in_flatpak ()) {
             // TODO: Does this work outside of a flatpak? We could remove the
             // extra file we install in data/autostart, which would be nice.
-            try {
-                this.background_portal = GLib.Bus.get_proxy_sync (
-                    GLib.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);
-                throw error;
-            }
+            this.background_portal = this.dbus_connection.get_proxy_sync (
+                "org.freedesktop.portal.Desktop",
+                "/org/freedesktop/portal/desktop",
+                GLib.DBusProxyFlags.NONE,
+                cancellable
+            );
         }
 
-        GLib.Bus.watch_name (
-            GLib.BusType.SESSION,
+        GLib.Bus.watch_name_on_connection (
+            this.dbus_connection,
             Config.DAEMON_APPLICATION_ID,
             GLib.BusNameWatcherFlags.NONE,
             this.break_daemon_appeared,
@@ -156,8 +156,7 @@ public class BreakManager : GLib.Object {
 
     private void break_daemon_appeared () {
         try {
-            this.break_daemon = GLib.Bus.get_proxy_sync (
-                GLib.BusType.SESSION,
+            this.break_daemon = this.dbus_connection.get_proxy_sync (
                 Config.DAEMON_APPLICATION_ID,
                 Config.DAEMON_OBJECT_PATH,
                 GLib.DBusProxyFlags.DO_NOT_AUTO_START
diff --git a/src/settings/MainWindow.vala b/src/settings/MainWindow.vala
index 4a27c65..a14a23c 100644
--- a/src/settings/MainWindow.vala
+++ b/src/settings/MainWindow.vala
@@ -23,6 +23,8 @@ namespace BreakTimer.Settings {
 public class MainWindow : Gtk.ApplicationWindow, GLib.Initable {
     private BreakManager break_manager;
 
+    private GLib.DBusConnection dbus_connection;
+
     private GLib.Menu app_menu;
 
     private Gtk.HeaderBar header;
@@ -112,6 +114,8 @@ public class MainWindow : Gtk.ApplicationWindow, GLib.Initable {
     }
 
     public bool init (GLib.Cancellable? cancellable) throws GLib.Error {
+        this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+
         foreach (BreakType break_type in this.break_manager.all_breaks ()) {
             var info_widget = break_type.info_widget;
             this.main_stack.add_named (info_widget, break_type.id);
diff --git a/src/settings/timerbreak/TimerBreakType.vala b/src/settings/timerbreak/TimerBreakType.vala
index 0bd6eb4..43c4bfe 100644
--- a/src/settings/timerbreak/TimerBreakType.vala
+++ b/src/settings/timerbreak/TimerBreakType.vala
@@ -27,7 +27,8 @@ public abstract class TimerBreakType : BreakType {
     public int[] interval_options;
     public int[] duration_options;
 
-    public IBreakTimer_TimerBreak? break_server;
+    private GLib.DBusConnection dbus_connection;
+    private IBreakTimer_TimerBreak? break_server;
 
     public signal void timer_status_changed (TimerBreakStatus? status);
 
@@ -38,13 +39,16 @@ public abstract class TimerBreakType : BreakType {
     }
 
     public override bool init (GLib.Cancellable? cancellable) throws GLib.Error {
-        GLib.Bus.watch_name (
-            GLib.BusType.SESSION,
+        this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+
+        GLib.Bus.watch_name_on_connection (
+            this.dbus_connection,
             Config.DAEMON_APPLICATION_ID,
             GLib.BusNameWatcherFlags.NONE,
             this.breakdaemon_appeared,
             this.breakdaemon_disappeared
         );
+
         return base.init (cancellable);
     }
 
@@ -90,8 +94,7 @@ public abstract class TimerBreakType : BreakType {
 
     private void breakdaemon_appeared () {
         try {
-            this.break_server = GLib.Bus.get_proxy_sync (
-                GLib.BusType.SESSION,
+            this.break_server = this.dbus_connection.get_proxy_sync (
                 Config.DAEMON_APPLICATION_ID,
                 Config.DAEMON_BREAK_OBJECT_BASE_PATH+this.id,
                 GLib.DBusProxyFlags.DO_NOT_AUTO_START


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