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
use crate::{error::*, notification::Notification, xdg};
use zbus::Connection;
use super::{ActionResponse, ActionResponseHandler, CloseReason};
#[derive(Debug)]
pub struct ZbusNotificationHandle {
pub(crate) id: u32,
pub(crate) connection: Connection,
pub(crate) notification: Notification,
}
impl ZbusNotificationHandle {
pub(crate) fn new(id: u32, connection: Connection, notification: Notification) -> ZbusNotificationHandle {
ZbusNotificationHandle {
id,
connection,
notification,
}
}
pub fn wait_for_action(mut self, invocation_closure: impl ActionResponseHandler) {
wait_for_action_signal(&mut self.connection, self.id, invocation_closure);
}
pub fn close(self) {
self.connection
.call_method(
Some(crate::xdg::NOTIFICATION_NAMESPACE),
crate::xdg::NOTIFICATION_OBJECTPATH,
Some(crate::xdg::NOTIFICATION_NAMESPACE),
"CloseNotification",
&(self.id),
)
.unwrap();
}
pub fn on_close<F>(self, closure: F)
where
F: FnOnce(CloseReason),
{
self.wait_for_action(|action: &ActionResponse| {
if let ActionResponse::Closed(reason) = action {
closure(*reason);
}
});
}
pub fn update(&mut self) {
self.id = send_notificaion_via_connection(&self.notification, self.id, &self.connection).unwrap();
}
}
pub fn send_notificaion_via_connection(notification: &Notification, id: u32, connection: &Connection) -> Result<u32> {
let reply: u32 = connection
.call_method(
Some(crate::xdg::NOTIFICATION_NAMESPACE),
crate::xdg::NOTIFICATION_OBJECTPATH,
Some(crate::xdg::NOTIFICATION_NAMESPACE),
"Notify",
&(
¬ification.appname,
id,
¬ification.icon,
¬ification.summary,
¬ification.body,
¬ification.actions,
crate::hints::hints_to_map(notification),
notification.timeout.into_i32(),
),
)?
.body()
.unwrap();
Ok(reply)
}
pub fn connect_and_send_notification(notification: &Notification) -> Result<ZbusNotificationHandle> {
let connection = zbus::Connection::new_session()?;
let inner_id = notification.id.unwrap_or(0);
let id = send_notificaion_via_connection(notification, inner_id, &connection)?;
Ok(ZbusNotificationHandle::new(id, connection, notification.clone()))
}
pub fn get_capabilities() -> Result<Vec<String>> {
let connection = zbus::Connection::new_session()?;
let info: Vec<String> = connection
.call_method(
Some(crate::xdg::NOTIFICATION_NAMESPACE),
crate::xdg::NOTIFICATION_OBJECTPATH,
Some(crate::xdg::NOTIFICATION_NAMESPACE),
"GetCapabilities",
&(),
)?
.body()
.unwrap();
Ok(info)
}
pub fn get_server_information() -> Result<xdg::ServerInformation> {
let connection = zbus::Connection::new_session()?;
let info: xdg::ServerInformation = connection
.call_method(
Some(crate::xdg::NOTIFICATION_NAMESPACE),
crate::xdg::NOTIFICATION_OBJECTPATH,
Some(crate::xdg::NOTIFICATION_NAMESPACE),
"GetServerInformation",
&(),
)?
.body()
.unwrap();
Ok(info)
}
pub fn handle_action(id: u32, func: impl ActionResponseHandler) {
let mut connection = Connection::new_session().unwrap();
wait_for_action_signal(&mut connection, id, func);
}
fn wait_for_action_signal(connection: &mut Connection, id: u32, handler: impl ActionResponseHandler) {
let proxy = zbus::fdo::DBusProxy::new(connection).unwrap();
proxy
.add_match("interface='org.freedesktop.Notifications',member='ActionInvoked'")
.unwrap();
proxy
.add_match("interface='org.freedesktop.Notifications',member='NotificationClosed'")
.unwrap();
while let Ok(msg) = connection.receive_message() {
if let Ok(header) = msg.header() {
if let Ok(zbus::MessageType::Signal) = header.message_type() {
match header.member() {
Ok(Some("ActionInvoked")) => match msg.body::<(u32, String)>() {
Ok((nid, action)) if nid == id => {
handler.call(&ActionResponse::Custom(&action));
break;
}
_ => {}
},
Ok(Some("NotificationClosed")) => match msg.body::<(u32, u32)>() {
Ok((nid, reason)) if nid == id => {
handler.call(&ActionResponse::Closed(reason.into()));
break;
}
_ => {}
},
Ok(_) => {}
Err(_) => {}
}
}
}
}
}