From 4b3d81be987c759e57ab15c7ee7b6929f03736dd Mon Sep 17 00:00:00 2001 From: ranfdev Date: Sat, 1 Mar 2025 11:56:22 +0100 Subject: [PATCH] Make app dbus-activatable --- .github/workflows/ci.yml | 2 +- build-aux/com.ranfdev.Notify.Devel.json | 6 ++-- data/com.ranfdev.Notify.desktop.in.in | 1 + data/com.ranfdev.Notify.service.in | 3 ++ data/meson.build | 12 ++++++++ meson.build | 1 + ntfy-daemon/src/listener.rs | 3 ++ ntfy-daemon/src/ntfy.rs | 2 ++ ntfy-daemon/src/subscription.rs | 2 +- src/application.rs | 37 +++++++------------------ src/main.rs | 9 +++++- 11 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 data/com.ranfdev.Notify.service.in diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 299cb02..ff00044 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,6 @@ jobs: with: bundle: notify.flatpak manifest-path: build-aux/com.ranfdev.Notify.Devel.json - repository-name: "flathub" + repository-name: "flathub-beta" run-tests: true cache-key: flatpak-builder-${{ github.sha }} diff --git a/build-aux/com.ranfdev.Notify.Devel.json b/build-aux/com.ranfdev.Notify.Devel.json index 5a85fd9..627d518 100644 --- a/build-aux/com.ranfdev.Notify.Devel.json +++ b/build-aux/com.ranfdev.Notify.Devel.json @@ -1,7 +1,7 @@ { "id": "com.ranfdev.Notify.Devel", "runtime": "org.gnome.Platform", - "runtime-version": "47", + "runtime-version": "48beta", "sdk": "org.gnome.Sdk", "sdk-extensions": [ "org.freedesktop.Sdk.Extension.rust-stable", @@ -45,8 +45,8 @@ { "type": "git", "url": "https://gitlab.gnome.org/jwestman/blueprint-compiler", - "tag": "v0.14.0", - "commit": "8e10fcf8692108b9d4ab78f41086c5d7773ef864" + "tag": "v0.16.0", + "commit": "04ef0944db56ab01307a29aaa7303df6067cb3c0" } ] }, diff --git a/data/com.ranfdev.Notify.desktop.in.in b/data/com.ranfdev.Notify.desktop.in.in index 3b53721..6e8fcaa 100644 --- a/data/com.ranfdev.Notify.desktop.in.in +++ b/data/com.ranfdev.Notify.desktop.in.in @@ -4,6 +4,7 @@ Comment=ntfy.sh client application to receive everyday's notifications Type=Application Exec=notify Terminal=false +DBusActivatable=true Categories=GNOME;GTK;Network;Utility; # Translators: Search terms to find this application. Do NOT translate or localize the semicolons! The list MUST also end with a semicolon! Keywords=Gnome;GTK;ntfy; diff --git a/data/com.ranfdev.Notify.service.in b/data/com.ranfdev.Notify.service.in new file mode 100644 index 0000000..183a5eb --- /dev/null +++ b/data/com.ranfdev.Notify.service.in @@ -0,0 +1,3 @@ +[D-BUS Service] +Name=@app-id@ +Exec=@bindir@/notify --gapplication-service \ No newline at end of file diff --git a/data/meson.build b/data/meson.build index 72f5ccf..dcb3008 100644 --- a/data/meson.build +++ b/data/meson.build @@ -48,3 +48,15 @@ if glib_compile_schemas.found() ], ) endif + +# DBus services +service_conf = configuration_data() +service_conf.set('bindir', bindir) +service_conf.set('app-id', application_id) +configure_file( + input: '@0@.service.in'.format(base_id), + output: '@0@.service'.format(application_id), + configuration: service_conf, + install: true, + install_dir: dbusdir +) \ No newline at end of file diff --git a/meson.build b/meson.build index 5a600ff..cb1c5ba 100644 --- a/meson.build +++ b/meson.build @@ -33,6 +33,7 @@ pkgdatadir = datadir / meson.project_name() iconsdir = datadir / 'icons' podir = meson.project_source_root() / 'po' gettext_package = meson.project_name() +dbusdir = datadir / 'dbus-1' / 'services' if get_option('profile') == 'development' profile = 'Devel' diff --git a/ntfy-daemon/src/listener.rs b/ntfy-daemon/src/listener.rs index 49141fa..8af9653 100644 --- a/ntfy-daemon/src/listener.rs +++ b/ntfy-daemon/src/listener.rs @@ -109,6 +109,8 @@ impl ListenerActor { let span = tracing::info_span!("listener_loop", topic = %self.config.topic); async { let mut commands_rx = self.commands_rx.take().unwrap(); + // TODO: Fix + // Each time a ListenerCommand is received, the supervised loop will be dropped and a new one will be started. loop { select! { _ = self.run_supervised_loop() => { @@ -191,6 +193,7 @@ impl ListenerActor { } async fn recv_and_forward_loop(&mut self) -> anyhow::Result<()> { + debug!("starting receive loop"); let span = tracing::info_span!("receive_loop", endpoint = %self.config.endpoint, topic = %self.config.topic, diff --git a/ntfy-daemon/src/ntfy.rs b/ntfy-daemon/src/ntfy.rs index 7407f2f..d5cfc1e 100644 --- a/ntfy-daemon/src/ntfy.rs +++ b/ntfy-daemon/src/ntfy.rs @@ -10,6 +10,7 @@ use tokio::{ sync::{broadcast, mpsc, oneshot, RwLock}, task::{spawn_local, LocalSet}, }; +use tracing::debug; use tracing::{error, info}; use crate::{ @@ -225,6 +226,7 @@ impl NtfyActor { } async fn handle_watch_subscribed(&mut self) -> anyhow::Result<()> { + debug!("Watching previously subscribed topics, restoring all connections"); let f: Vec<_> = self .env .db diff --git a/ntfy-daemon/src/subscription.rs b/ntfy-daemon/src/subscription.rs index 9643443..30e7a73 100644 --- a/ntfy-daemon/src/subscription.rs +++ b/ntfy-daemon/src/subscription.rs @@ -262,7 +262,7 @@ impl SubscriptionActor { actions: msg.actions.clone(), }; - info!(topic=?self.model.topic, "showing notification"); + debug!(topic=?self.model.topic, "sending notification through proxy"); notifier.send(n).unwrap(); } else { debug!(topic=?self.model.topic, "notification muted, skipping"); diff --git a/src/application.rs b/src/application.rs index ee47cc4..00ba35f 100644 --- a/src/application.rs +++ b/src/application.rs @@ -55,12 +55,6 @@ mod imp { app.setup_css(); app.setup_gactions(); app.setup_accels(); - } - fn command_line(&self, command_line: &gio::ApplicationCommandLine) -> glib::ExitCode { - debug!("AdwApplication::command_line"); - let arguments = command_line.arguments(); - let is_daemon = arguments.get(1).map(|x| x.to_str()) == Some(Some("--daemon")); - let app = self.obj(); if self.hold_guard.get().is_none() { app.ensure_rpc_running(); @@ -71,14 +65,6 @@ mod imp { warn!(error = %e, "couldn't request running in background from portal"); } }); - - if is_daemon { - return glib::ExitCode::SUCCESS; - } - - app.ensure_window_present(); - - glib::ExitCode::SUCCESS } } @@ -93,7 +79,13 @@ glib::wrapper! { } impl NotifyApplication { - fn ensure_window_present(&self) { + pub fn new() -> Self { + glib::Object::builder() + .property("application-id", APP_ID) + .property("resource-base-path", "/com/ranfdev/Notify/") + .build() + } + pub fn ensure_window_present(&self) { if let Some(window) = { self.imp().window.borrow().upgrade() } { if window.is_visible() { window.present(); @@ -234,8 +226,8 @@ impl NotifyApplication { let response = ashpd::desktop::background::Background::request() .reason("Listen for coming notifications") .auto_start(true) - .command(&["notify", "--daemon"]) - .dbus_activatable(false) + .command(&["notify", "--gapplication-service"]) + .dbus_activatable(true) .send() .await? .response()?; @@ -279,6 +271,7 @@ impl NotifyApplication { } } + info!(title = %n.title, "showing notification"); app.send_notification(None, &gio_notif); } }); @@ -325,13 +318,3 @@ impl NotifyApplication { *self.imp().window.borrow_mut() = window.downgrade(); } } - -impl Default for NotifyApplication { - fn default() -> Self { - glib::Object::builder() - .property("application-id", APP_ID) - .property("flags", gio::ApplicationFlags::HANDLES_COMMAND_LINE) - .property("resource-base-path", "/com/ranfdev/Notify/") - .build() - } -} diff --git a/src/main.rs b/src/main.rs index 2149993..b7fc68b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,10 @@ pub mod error; mod subscription; pub mod widgets; +use adw::prelude::*; use gettextrs::{gettext, LocaleCategory}; use gtk::{gio, glib}; +use tracing::debug; use self::application::NotifyApplication; use self::config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE}; @@ -26,6 +28,11 @@ fn main() -> glib::ExitCode { let res = gio::Resource::load(RESOURCES_FILE).expect("Could not load gresource file"); gio::resources_register(&res); - let app = NotifyApplication::default(); + let app = NotifyApplication::new(); + app.register(gio::Cancellable::NONE) + .expect("Failed to register application"); + if !app.is_remote() { + debug!("primary instance"); + }; app.run() }