From 98811de34af794bcd9c79339a4e14fea5378dfda Mon Sep 17 00:00:00 2001 From: ranfdev Date: Thu, 9 Nov 2023 18:37:11 +0100 Subject: [PATCH] Use custom server from selected subscription --- ntfy-daemon/src/models.rs | 3 ++- src/widgets/add_subscription_dialog.rs | 23 ++++++++++++++--------- src/widgets/window.rs | 5 +++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ntfy-daemon/src/models.rs b/ntfy-daemon/src/models.rs index ba8778d..23ce3ea 100644 --- a/ntfy-daemon/src/models.rs +++ b/ntfy-daemon/src/models.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; use crate::Error; +pub const DEFAULT_SERVER: &str = "https://ntfy.sh"; static EMOJI_MAP: OnceLock> = OnceLock::new(); fn emoji_map() -> &'static HashMap { @@ -188,7 +189,7 @@ pub struct SubscriptionBuilder { impl SubscriptionBuilder { pub fn new(topic: String) -> Self { Self { - server: "https://ntfy.sh".to_string(), + server: DEFAULT_SERVER.to_string(), topic, muted: false, archived: false, diff --git a/src/widgets/add_subscription_dialog.rs b/src/widgets/add_subscription_dialog.rs index 111cc5a..509f74e 100644 --- a/src/widgets/add_subscription_dialog.rs +++ b/src/widgets/add_subscription_dialog.rs @@ -1,3 +1,4 @@ +use std::cell::OnceCell; use std::cell::RefCell; use adw::prelude::*; @@ -20,6 +21,7 @@ mod imp { #[derive(Debug, Default)] pub struct AddSubscriptionDialog { pub widgets: RefCell, + pub init_custom_server: OnceCell, } #[glib::object_subclass] @@ -47,12 +49,6 @@ mod imp { Lazy::new(|| vec![Signal::builder("subscribe-request").build()]); SIGNALS.as_ref() } - - fn constructed(&self) { - self.parent_constructed(); - let obj = self.obj().clone(); - obj.build_ui(); - } } impl WidgetImpl for AddSubscriptionDialog {} impl WindowImpl for AddSubscriptionDialog {} @@ -66,8 +62,15 @@ glib::wrapper! { } impl AddSubscriptionDialog { - pub fn new() -> Self { - glib::Object::builder().build() + pub fn new(custom_server: Option) -> Self { + let this: Self = glib::Object::builder().build(); + if let Some(s) = custom_server { + if s != ntfy_daemon::models::DEFAULT_SERVER { + this.imp().init_custom_server.set(s).unwrap(); + } + } + this.build_ui(); + this } fn build_ui(&self) { let imp = self.imp(); @@ -118,10 +121,12 @@ impl AddSubscriptionDialog { }, append: server_expander = &adw::ExpanderRow { set_title: "Custom server...", - set_enable_expansion: false, + set_enable_expansion: imp.init_custom_server.get().is_some(), + set_expanded: imp.init_custom_server.get().is_some(), set_show_enable_switch: true, add_row: server_entry = &adw::EntryRow { set_title: "Server", + set_text: imp.init_custom_server.get().map(|x| x.as_str()).unwrap_or(""), } } }, diff --git a/src/widgets/window.rs b/src/widgets/window.rs index 38e694b..1889b5a 100644 --- a/src/widgets/window.rs +++ b/src/widgets/window.rs @@ -119,11 +119,12 @@ mod imp { impl NotifyWindow { #[template_callback] fn show_add_topic(&self, _btn: >k::Button) { - let dialog = AddSubscriptionDialog::new(); + let this = self.obj().clone(); + let dialog = + AddSubscriptionDialog::new(this.selected_subscription().map(|x| x.server())); dialog.set_transient_for(Some(&self.obj().clone())); dialog.present(); - let this = self.obj().clone(); let dc = dialog.clone(); dialog.connect_local("subscribe-request", true, move |_| { let sub = match dc.subscription() {