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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#![allow(unused_imports, unused_variables, dead_code)]
use std::cell::Cell;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use dbus::tree::{self, Factory, Interface, MTFn, MTSync, Tree};
use dbus::{arg, BusType, Connection, NameFlag, Path};
use super::{Notification, NotificationHint, Timeout};
use crate::util::*;
use crate::xdg::{NOTIFICATION_NAMESPACE, NOTIFICATION_OBJECTPATH};
use crate::hints::hints_from_variants;
static DBUS_ERROR_FAILED: &str = "org.freedesktop.DBus.Error.Failed";
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug, Default)]
pub struct NotificationServer {
counter: Mutex<Cell<u32>>,
stopped: Mutex<Cell<bool>>,
}
impl NotificationServer {
fn count_up(&self) {
if let Ok(counter) = self.counter.lock() {
counter.set(counter.get() + 1);
}
}
fn stop(&self) {
if let Ok(stop) = self.stopped.lock() {
stop.set(true);
}
}
fn is_stopped(&self) -> bool{
if let Ok(stop) = self.stopped.lock() {
stop.get()
} else {
true
}
}
pub fn create() -> Arc<NotificationServer> {
Arc::new(NotificationServer::default())
}
pub fn start<F: 'static>(me: &Arc<Self>, closure: F)
where F: Fn(&Notification),
{
let connection = Connection::get_private(BusType::Session).unwrap();
connection.release_name(NOTIFICATION_NAMESPACE).unwrap();
connection.register_name(NOTIFICATION_NAMESPACE, NameFlag::ReplaceExisting as u32).unwrap();
connection.register_object_path(NOTIFICATION_OBJECTPATH).unwrap();
let mytex = Arc::new(Mutex::new(me.clone()));
let factory = Factory::new_fn::<()>();
let tree = factory.tree(()).add(
factory.object_path(NOTIFICATION_OBJECTPATH, ())
.introspectable()
.add(factory
.interface(NOTIFICATION_NAMESPACE, ())
.add_m(method_notify(&factory, closure))
.add_m(method_close_notification(&factory))
.add_m(Self::stop_server(mytex.clone(), &factory))
.add_m(method_get_capabilities(&factory))
.add_m(method_get_server_information(&factory))
),
);
connection.add_handler(tree);
while !me.is_stopped() {
if let Some(received) = connection.incoming(1000).next() {
println!("RECEIVED {:?}", received);
}
}
}
fn stop_server(me: Arc<Mutex<Arc<Self>>>, factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> {
factory.method("Stop", (), move |minfo| {
if let Ok(me) = me.lock() {
me.stop();
println!("STOPPING");
Ok(vec![])
} else {
Err(tree::MethodErr::failed(&String::from("nope!")))
}
})
.out_arg(("", "u"))
}
}
fn method_notify<F: 'static>(factory: &Factory<MTFn>, on_notification: F) -> tree::Method<MTFn<()>, ()>
where F: Fn(&Notification),
{
factory.method("Notify", (), move |minfo| {
let mut i = minfo.msg.iter_init();
let appname: String = i.read()?;
let replaces_id: u32 = i.read()?;
let icon: String = i.read()?;
let summary: String = i.read()?;
let body: String = i.read()?;
let actions: Vec<String> = i.read()?;
let hints: ::std::collections::HashMap<String, arg::Variant<Box<dyn arg::RefArg>>> = i.read()?;
let timeout: i32 = i.read()?;
println!("hints {:?} ", hints);
let notification = Notification{
appname,
icon,
summary,
body,
actions,
hints: hints_from_variants(&hints),
timeout: Timeout::from(timeout),
id: if replaces_id == 0 { None } else { Some(replaces_id) },
subtitle: None,
};
on_notification(¬ification);
let arg0 = 43;
let rm = minfo.msg.method_return();
let rm = rm.append1(arg0);
Ok(vec![rm])
})
.in_arg(("app_name", "s"))
.in_arg(("replaces_id", "u"))
.in_arg(("app_icon", "s"))
.in_arg(("summary", "s"))
.in_arg(("body", "s"))
.in_arg(("actions", "as"))
.in_arg(("hints", "a{sv}"))
.in_arg(("timeout", "i"))
.out_arg(("", "u"))
}
fn method_close_notification(factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> {
factory.method("CloseNotification", (), |minfo| {
let mut i = minfo.msg.iter_init();
let id: u32 = r#try!(i.read());
let rm = minfo.msg.method_return();
Ok(vec!(rm))
})
.in_arg(("id", "u"))
}
fn method_get_capabilities(factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> {
factory.method("GetCapabilities", (), |minfo| {
let caps: Vec<String> = vec![];
let rm = minfo.msg.method_return();
let rm = rm.append1(caps);
Ok(vec!(rm))
})
.out_arg(("caps", "as"))
}
fn method_get_server_information(factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> {
factory.method("GetServerInformation", (), |minfo| {
let (name, vendor, version, spec_version) = (
"notify-rust", "notify-rust", env!("CARGO_PKG_VERSION"), "0.0.0"
);
let rm = minfo.msg.method_return();
let rm = rm.append1(name);
let rm = rm.append1(vendor);
let rm = rm.append1(version);
let rm = rm.append1(spec_version);
Ok(vec!(rm))
})
.out_arg(("name", "s"))
.out_arg(("vendor", "s"))
.out_arg(("version", "s"))
.out_arg(("spec_version", "s"))
}