-
Notifications
You must be signed in to change notification settings - Fork 13
/
ui.rs
187 lines (161 loc) 路 5.32 KB
/
ui.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::collections::HashMap;
use std::error::Error;
use std::time::Duration;
use dbus::arg;
use dbus::arg::{RefArg, Variant};
use dbus::blocking::Connection;
use dbus::blocking::Proxy;
use dbus::Message;
use tracing::debug;
use uuid::Uuid;
#[derive(Debug)]
struct NotificationAction {
pub id: String,
pub action: String,
pub parameter: Vec<arg::Variant<Box<dyn arg::RefArg + 'static>>>,
}
impl arg::AppendAll for NotificationAction {
fn append(&self, i: &mut arg::IterAppend) {
arg::RefArg::append(&self.id, i);
arg::RefArg::append(&self.action, i);
arg::RefArg::append(&self.parameter, i);
}
}
impl arg::ReadAll for NotificationAction {
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
Ok(NotificationAction {
id: i.read()?,
action: i.read()?,
parameter: i.read()?,
})
}
}
impl dbus::message::SignalArgs for NotificationAction {
const NAME: &'static str = "ActionInvoked";
const INTERFACE: &'static str = "org.freedesktop.portal.Notification";
}
fn variant_str(string: &str) -> Variant<Box<dyn RefArg>> {
let s = String::from(string);
let b = Box::new(s) as Box<dyn RefArg>;
Variant(b)
}
pub enum CancellationResponse {
UserCancel,
}
pub trait UI {
type Handle;
fn confirm_u2f_usb_register(
&self,
app_id: &str,
timeout: Duration,
callback: fn(CancellationResponse) -> (),
) -> Result<Self::Handle, Box<dyn Error>>;
fn confirm_u2f_usb_sign(
&self,
app_id: &str,
timeout: Duration,
callback: fn(CancellationResponse) -> (),
) -> Result<Self::Handle, Box<dyn Error>>;
fn cancel(&self, handle: Self::Handle) -> Result<(), Box<dyn Error>>;
}
pub struct NotificationPortalUI<'conn> {
dbus_proxy: Proxy<'conn, &'conn Connection>,
}
pub struct NotificationHandle {
id: String,
}
impl NotificationHandle {
fn new(id: &str) -> NotificationHandle {
NotificationHandle {
id: String::from(id),
}
}
}
impl<'conn> NotificationPortalUI<'conn> {
pub fn new(conn: &'conn Connection) -> Self {
let proxy: Proxy<&'conn Connection> = conn.with_proxy(
"org.freedesktop.portal.Desktop", // iface
"/org/freedesktop/portal/desktop", // object
Duration::from_millis(5000),
);
Self { dbus_proxy: proxy }
}
fn _action(
&self,
title: &str,
body: &str,
callback: fn(CancellationResponse) -> (),
) -> Result<NotificationHandle, Box<dyn std::error::Error>> {
let notification_id = Uuid::new_v4().to_hyphenated().to_string();
let mut button1 = HashMap::new();
button1.insert(String::from("action"), variant_str("cancel"));
button1.insert(String::from("label"), variant_str("Cancel"));
let buttons = vec![button1];
let mut options = HashMap::new();
options.insert("title", variant_str(title));
options.insert("body", variant_str(body));
options.insert("priority", variant_str("urgent"));
options.insert("icon", variant_str("dialog-password")); // https://developer.gnome.org/icon-naming-spec/
options.insert("default-action", variant_str("cancel"));
options.insert("buttons", Variant(Box::new(buttons) as Box<dyn RefArg>));
self.dbus_proxy.match_signal(
move |h: NotificationAction, _: &Connection, _: &Message| {
debug!("Received signal: {:?}", h);
match h.action.as_str() {
_ => callback(CancellationResponse::UserCancel),
};
true
},
)?;
self.dbus_proxy.method_call(
"org.freedesktop.portal.Notification",
"AddNotification",
(¬ification_id, options),
)?;
Ok(NotificationHandle::new(¬ification_id))
}
}
impl<'conn> UI for NotificationPortalUI<'conn> {
type Handle = NotificationHandle;
fn confirm_u2f_usb_register(
&self,
app_id: &str,
timeout: Duration,
callback: fn(CancellationResponse) -> (),
) -> Result<Self::Handle, Box<dyn Error>> {
self._action(
"Touch your Security Key to register it",
&format!(
"\nThe application (<b>{}</b>) would like to register your FIDO U2F security key.\n\n\
Touch it within {:?}, or click Cancel.",
app_id, timeout
),
callback,
)
}
fn confirm_u2f_usb_sign(
&self,
app_id: &str,
timeout: Duration,
callback: fn(CancellationResponse) -> (),
) -> Result<Self::Handle, Box<dyn Error>> {
self._action(
"Touch your Security Key to verify your identity ",
&format!(
"\nThe application (<b>{}</b>) would like to verify your\
identity using your FIDO U2F security key.\n\n\
Touch it within {:?}, or click Cancel.",
app_id, timeout
),
callback,
)
}
fn cancel(&self, handle: Self::Handle) -> Result<(), Box<dyn Error>> {
self.dbus_proxy.method_call(
"org.freedesktop.portal.Notification",
"RemoveNotification",
(handle.id,),
)?;
Ok(())
}
}