Add missing calls

This commit is contained in:
ranfdev
2024-11-20 20:35:34 +01:00
parent 0a083a6e2b
commit 6eae0e6972
4 changed files with 84 additions and 54 deletions

View File

@ -42,6 +42,16 @@ pub enum NtfyMessage {
WatchSubscribed {
respond_to: oneshot::Sender<anyhow::Result<()>>,
},
AddAccount {
server: String,
username: String,
password: String,
respond_to: oneshot::Sender<anyhow::Result<()>>,
},
RemoveAccount {
server: String,
respond_to: oneshot::Sender<anyhow::Result<()>>,
},
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(

View File

@ -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 {

View File

@ -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<gtk::ListBox>,
#[template_child]
pub added_accounts_group: TemplateChild<adw::PreferencesGroup>,
pub notifier: OnceCell<system_notifier::Client>,
pub notifier: OnceCell<NtfyHandle>,
}
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(())
}
}

View File

@ -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 });
});
}
}