Skip to content

Commit 6eae0e6

Browse files
committed
Add missing calls
1 parent 0a083a6 commit 6eae0e6

File tree

4 files changed

+84
-54
lines changed

4 files changed

+84
-54
lines changed

ntfy-daemon/src/ntfy.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ pub enum NtfyMessage {
4242
WatchSubscribed {
4343
respond_to: oneshot::Sender<anyhow::Result<()>>,
4444
},
45+
AddAccount {
46+
server: String,
47+
username: String,
48+
password: String,
49+
respond_to: oneshot::Sender<anyhow::Result<()>>,
50+
},
51+
RemoveAccount {
52+
server: String,
53+
respond_to: oneshot::Sender<anyhow::Result<()>>,
54+
},
4555
Shutdown,
4656
}
4757

@@ -173,6 +183,21 @@ impl NtfyActor {
173183
let _ = respond_to.send(result);
174184
}
175185

186+
NtfyMessage::AddAccount {
187+
server,
188+
username,
189+
password,
190+
respond_to,
191+
} => {
192+
let result = self.env.credentials.insert(&server, &username, &password).await;
193+
let _ = respond_to.send(result);
194+
}
195+
196+
NtfyMessage::RemoveAccount { server, respond_to } => {
197+
let result = self.env.credentials.delete(&server).await;
198+
let _ = respond_to.send(result);
199+
}
200+
176201
NtfyMessage::Shutdown => break,
177202
}
178203
}
@@ -296,6 +321,34 @@ impl NtfyHandle {
296321

297322
rx.await.map_err(|_| anyhow!("Actor response error"))?
298323
}
324+
325+
pub async fn add_account(&self, server: &str, username: &str, password: &str) -> anyhow::Result<()> {
326+
let (tx, rx) = oneshot::channel();
327+
self.command_tx
328+
.send(NtfyMessage::AddAccount {
329+
server: server.to_string(),
330+
username: username.to_string(),
331+
password: password.to_string(),
332+
respond_to: tx,
333+
})
334+
.await
335+
.map_err(|_| anyhow!("Actor mailbox error"))?;
336+
337+
rx.await.map_err(|_| anyhow!("Actor response error"))?
338+
}
339+
340+
pub async fn remove_account(&self, server: &str) -> anyhow::Result<()> {
341+
let (tx, rx) = oneshot::channel();
342+
self.command_tx
343+
.send(NtfyMessage::RemoveAccount {
344+
server: server.to_string(),
345+
respond_to: tx,
346+
})
347+
.await
348+
.map_err(|_| anyhow!("Actor mailbox error"))?;
349+
350+
rx.await.map_err(|_| anyhow!("Actor response error"))?
351+
}
299352
}
300353

301354
pub fn start(

src/application.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ impl NotifyApplication {
229229
}
230230

231231
fn show_preferences(&self) {
232-
// let win = crate::widgets::NotifyPreferences::new(
233-
// self.main_window().imp().notifier.get().unwrap().clone(),
234-
// );
235-
// win.present(Some(&self.main_window()));
232+
let win = crate::widgets::NotifyPreferences::new(
233+
self.main_window().imp().notifier.get().unwrap().clone(),
234+
);
235+
win.present(Some(&self.main_window()));
236236
}
237237

238238
pub fn run(&self) -> glib::ExitCode {

src/widgets/preferences.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use std::cell::OnceCell;
33
use adw::prelude::*;
44
use adw::subclass::prelude::*;
55
use gtk::{gio, glib};
6-
use ntfy_daemon::ntfy_capnp::system_notifier;
76

87
use crate::error::*;
98

109
mod imp {
10+
use ntfy_daemon::NtfyHandle;
11+
1112
use super::*;
1213

1314
#[derive(gtk::CompositeTemplate)]
@@ -25,7 +26,7 @@ mod imp {
2526
pub added_accounts: TemplateChild<gtk::ListBox>,
2627
#[template_child]
2728
pub added_accounts_group: TemplateChild<adw::PreferencesGroup>,
28-
pub notifier: OnceCell<system_notifier::Client>,
29+
pub notifier: OnceCell<NtfyHandle>,
2930
}
3031

3132
impl Default for NotifyPreferences {
@@ -77,7 +78,7 @@ glib::wrapper! {
7778
}
7879

7980
impl NotifyPreferences {
80-
pub fn new(notifier: system_notifier::Client) -> Self {
81+
pub fn new(notifier: ntfy_daemon::NtfyHandle ) -> Self {
8182
let obj: Self = glib::Object::builder().build();
8283
obj.imp()
8384
.notifier
@@ -100,21 +101,16 @@ impl NotifyPreferences {
100101

101102
pub async fn show_accounts(&self) -> anyhow::Result<()> {
102103
let imp = self.imp();
103-
let req = imp.notifier.get().unwrap().list_accounts_request();
104-
let res = req.send().promise.await?;
105-
106-
let accounts = res.get()?.get_list()?;
104+
let accounts = imp.notifier.get().unwrap().list_accounts().await?;
107105

108106
imp.added_accounts_group.set_visible(!accounts.is_empty());
109107

110108
imp.added_accounts.remove_all();
111109
for a in accounts {
112-
let server = a.get_server()?.to_string()?;
113-
let username = a.get_username()?.to_string()?;
114110

115111
let row = adw::ActionRow::builder()
116-
.title(&server)
117-
.subtitle(&username)
112+
.title(&a.server)
113+
.subtitle(&a.username)
118114
.build();
119115
row.add_css_class("property");
120116
row.add_suffix(&{
@@ -125,10 +121,9 @@ impl NotifyPreferences {
125121
let this = self.clone();
126122
btn.connect_clicked(move |btn| {
127123
let this = this.clone();
128-
let username = username.clone();
129-
let server = server.clone();
124+
let a = a.clone();
130125
btn.error_boundary()
131-
.spawn(async move { this.remove_account(&server, &username).await });
126+
.spawn(async move { this.remove_account(&a.server).await });
132127
});
133128
btn
134129
});
@@ -142,29 +137,14 @@ impl NotifyPreferences {
142137
let server = imp.server_entry.text();
143138
let username = imp.username_entry.text();
144139

145-
let mut req = imp.notifier.get().unwrap().add_account_request();
146-
let mut acc = req.get().get_account()?;
147-
acc.set_username(username[..].into());
148-
acc.set_server(server[..].into());
149-
req.get().set_password(password[..].into());
150-
151-
req.send().promise.await?;
152-
140+
imp.notifier.get().unwrap().add_account(&server, &username, &password).await?;
153141
self.show_accounts().await?;
154142

155143
Ok(())
156144
}
157-
pub async fn remove_account(&self, server: &str, username: &str) -> anyhow::Result<()> {
158-
let mut req = self.imp().notifier.get().unwrap().remove_account_request();
159-
let mut acc = req.get().get_account()?;
160-
161-
acc.set_username(username[..].into());
162-
acc.set_server(server[..].into());
163-
164-
req.send().promise.await?;
165-
145+
pub async fn remove_account(&self, server: &str) -> anyhow::Result<()> {
146+
self.imp().notifier.get().unwrap().remove_account(server).await?;
166147
self.show_accounts().await?;
167-
168148
Ok(())
169149
}
170150
}

src/widgets/window.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ mod imp {
141141
});
142142
klass.install_action("win.clear-notifications", None, |this, _, _| {
143143
this.selected_subscription().map(|sub| {
144-
this.error_boundary().spawn(async move {sub.clear_notifications().await});
144+
this.error_boundary()
145+
.spawn(async move { sub.clear_notifications().await });
145146
});
146147
});
147148
//klass.bind_template_instance_callbacks();
@@ -288,24 +289,20 @@ impl NotifyWindow {
288289
}
289290

290291
fn unsubscribe(&self) {
291-
// let mut req = self.notifier().unsubscribe_request();
292-
// let sub = self.selected_subscription().unwrap();
292+
let sub = self.selected_subscription().unwrap();
293293

294-
// req.get().set_server(sub.server().as_str().into());
295-
// req.get().set_topic(sub.topic().as_str().into());
296-
297-
// let res = req.send();
298-
// let this = self.clone();
299-
300-
// self.error_boundary().spawn(async move {
301-
// let imp = this.imp();
302-
// res.promise.await?;
294+
let this = self.clone();
295+
self.error_boundary().spawn(async move {
296+
this.notifier()
297+
.unsubscribe(sub.server().as_str(), sub.topic().as_str())
298+
.await?;
303299

304-
// if let Some(i) = imp.subscription_list_model.find(&sub) {
305-
// imp.subscription_list_model.remove(i);
306-
// }
307-
// Ok(())
308-
// });
300+
let imp = this.imp();
301+
if let Some(i) = imp.subscription_list_model.find(&sub) {
302+
imp.subscription_list_model.remove(i);
303+
}
304+
Ok(())
305+
});
309306
}
310307
fn notifier(&self) -> &NtfyHandle {
311308
self.imp().notifier.get().unwrap()
@@ -406,7 +403,7 @@ impl NotifyWindow {
406403
{
407404
self.selected_subscription().map(|sub| {
408405
self.error_boundary()
409-
.spawn(async move {sub.flag_all_as_read().await});
406+
.spawn(async move { sub.flag_all_as_read().await });
410407
});
411408
}
412409
}

0 commit comments

Comments
 (0)