From 6eae0e69729d4bba97cb3304718d22c68921d6a8 Mon Sep 17 00:00:00 2001 From: ranfdev Date: Wed, 20 Nov 2024 20:35:34 +0100 Subject: [PATCH] Add missing calls --- ntfy-daemon/src/ntfy.rs | 53 ++++++++++++++++++++++++++++++++++++++ src/application.rs | 8 +++--- src/widgets/preferences.rs | 44 +++++++++---------------------- src/widgets/window.rs | 33 +++++++++++------------- 4 files changed, 84 insertions(+), 54 deletions(-) diff --git a/ntfy-daemon/src/ntfy.rs b/ntfy-daemon/src/ntfy.rs index 3fed941..c7d9699 100644 --- a/ntfy-daemon/src/ntfy.rs +++ b/ntfy-daemon/src/ntfy.rs @@ -42,6 +42,16 @@ pub enum NtfyMessage { WatchSubscribed { respond_to: oneshot::Sender>, }, + AddAccount { + server: String, + username: String, + password: String, + respond_to: oneshot::Sender>, + }, + RemoveAccount { + server: String, + respond_to: oneshot::Sender>, + }, Shutdown, } @@ -173,6 +183,21 @@ impl NtfyActor { let _ = respond_to.send(result); } + NtfyMessage::AddAccount { + server, + username, + password, + respond_to, + } => { + let result = self.env.credentials.insert(&server, &username, &password).await; + let _ = respond_to.send(result); + } + + NtfyMessage::RemoveAccount { server, respond_to } => { + let result = self.env.credentials.delete(&server).await; + let _ = respond_to.send(result); + } + NtfyMessage::Shutdown => break, } } @@ -296,6 +321,34 @@ impl NtfyHandle { rx.await.map_err(|_| anyhow!("Actor response error"))? } + + pub async fn add_account(&self, server: &str, username: &str, password: &str) -> anyhow::Result<()> { + let (tx, rx) = oneshot::channel(); + self.command_tx + .send(NtfyMessage::AddAccount { + server: server.to_string(), + username: username.to_string(), + password: password.to_string(), + respond_to: tx, + }) + .await + .map_err(|_| anyhow!("Actor mailbox error"))?; + + rx.await.map_err(|_| anyhow!("Actor response error"))? + } + + pub async fn remove_account(&self, server: &str) -> anyhow::Result<()> { + let (tx, rx) = oneshot::channel(); + self.command_tx + .send(NtfyMessage::RemoveAccount { + server: server.to_string(), + respond_to: tx, + }) + .await + .map_err(|_| anyhow!("Actor mailbox error"))?; + + rx.await.map_err(|_| anyhow!("Actor response error"))? + } } pub fn start( diff --git a/src/application.rs b/src/application.rs index 89cbafb..fd246b0 100644 --- a/src/application.rs +++ b/src/application.rs @@ -229,10 +229,10 @@ impl NotifyApplication { } fn show_preferences(&self) { - // let win = crate::widgets::NotifyPreferences::new( - // self.main_window().imp().notifier.get().unwrap().clone(), - // ); - // win.present(Some(&self.main_window())); + let win = crate::widgets::NotifyPreferences::new( + self.main_window().imp().notifier.get().unwrap().clone(), + ); + win.present(Some(&self.main_window())); } pub fn run(&self) -> glib::ExitCode { diff --git a/src/widgets/preferences.rs b/src/widgets/preferences.rs index 2cd9540..d10cf6e 100644 --- a/src/widgets/preferences.rs +++ b/src/widgets/preferences.rs @@ -3,11 +3,12 @@ use std::cell::OnceCell; use adw::prelude::*; use adw::subclass::prelude::*; use gtk::{gio, glib}; -use ntfy_daemon::ntfy_capnp::system_notifier; use crate::error::*; mod imp { + use ntfy_daemon::NtfyHandle; + use super::*; #[derive(gtk::CompositeTemplate)] @@ -25,7 +26,7 @@ mod imp { pub added_accounts: TemplateChild, #[template_child] pub added_accounts_group: TemplateChild, - pub notifier: OnceCell, + pub notifier: OnceCell, } impl Default for NotifyPreferences { @@ -77,7 +78,7 @@ glib::wrapper! { } impl NotifyPreferences { - pub fn new(notifier: system_notifier::Client) -> Self { + pub fn new(notifier: ntfy_daemon::NtfyHandle ) -> Self { let obj: Self = glib::Object::builder().build(); obj.imp() .notifier @@ -100,21 +101,16 @@ impl NotifyPreferences { pub async fn show_accounts(&self) -> anyhow::Result<()> { let imp = self.imp(); - let req = imp.notifier.get().unwrap().list_accounts_request(); - let res = req.send().promise.await?; - - let accounts = res.get()?.get_list()?; + let accounts = imp.notifier.get().unwrap().list_accounts().await?; imp.added_accounts_group.set_visible(!accounts.is_empty()); imp.added_accounts.remove_all(); for a in accounts { - let server = a.get_server()?.to_string()?; - let username = a.get_username()?.to_string()?; let row = adw::ActionRow::builder() - .title(&server) - .subtitle(&username) + .title(&a.server) + .subtitle(&a.username) .build(); row.add_css_class("property"); row.add_suffix(&{ @@ -125,10 +121,9 @@ impl NotifyPreferences { let this = self.clone(); btn.connect_clicked(move |btn| { let this = this.clone(); - let username = username.clone(); - let server = server.clone(); + let a = a.clone(); btn.error_boundary() - .spawn(async move { this.remove_account(&server, &username).await }); + .spawn(async move { this.remove_account(&a.server).await }); }); btn }); @@ -142,29 +137,14 @@ impl NotifyPreferences { let server = imp.server_entry.text(); let username = imp.username_entry.text(); - let mut req = imp.notifier.get().unwrap().add_account_request(); - let mut acc = req.get().get_account()?; - acc.set_username(username[..].into()); - acc.set_server(server[..].into()); - req.get().set_password(password[..].into()); - - req.send().promise.await?; - + imp.notifier.get().unwrap().add_account(&server, &username, &password).await?; self.show_accounts().await?; Ok(()) } - pub async fn remove_account(&self, server: &str, username: &str) -> anyhow::Result<()> { - let mut req = self.imp().notifier.get().unwrap().remove_account_request(); - let mut acc = req.get().get_account()?; - - acc.set_username(username[..].into()); - acc.set_server(server[..].into()); - - req.send().promise.await?; - + pub async fn remove_account(&self, server: &str) -> anyhow::Result<()> { + self.imp().notifier.get().unwrap().remove_account(server).await?; self.show_accounts().await?; - Ok(()) } } diff --git a/src/widgets/window.rs b/src/widgets/window.rs index 1e9dbaa..01bdc1c 100644 --- a/src/widgets/window.rs +++ b/src/widgets/window.rs @@ -141,7 +141,8 @@ mod imp { }); klass.install_action("win.clear-notifications", None, |this, _, _| { this.selected_subscription().map(|sub| { - this.error_boundary().spawn(async move {sub.clear_notifications().await}); + this.error_boundary() + .spawn(async move { sub.clear_notifications().await }); }); }); //klass.bind_template_instance_callbacks(); @@ -288,24 +289,20 @@ impl NotifyWindow { } fn unsubscribe(&self) { - // let mut req = self.notifier().unsubscribe_request(); - // let sub = self.selected_subscription().unwrap(); + let sub = self.selected_subscription().unwrap(); - // req.get().set_server(sub.server().as_str().into()); - // req.get().set_topic(sub.topic().as_str().into()); + let this = self.clone(); + self.error_boundary().spawn(async move { + this.notifier() + .unsubscribe(sub.server().as_str(), sub.topic().as_str()) + .await?; - // let res = req.send(); - // let this = self.clone(); - - // self.error_boundary().spawn(async move { - // let imp = this.imp(); - // res.promise.await?; - - // if let Some(i) = imp.subscription_list_model.find(&sub) { - // imp.subscription_list_model.remove(i); - // } - // Ok(()) - // }); + let imp = this.imp(); + if let Some(i) = imp.subscription_list_model.find(&sub) { + imp.subscription_list_model.remove(i); + } + Ok(()) + }); } fn notifier(&self) -> &NtfyHandle { self.imp().notifier.get().unwrap() @@ -406,7 +403,7 @@ impl NotifyWindow { { self.selected_subscription().map(|sub| { self.error_boundary() - .spawn(async move {sub.flag_all_as_read().await}); + .spawn(async move { sub.flag_all_as_read().await }); }); } }