| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#![allow(unused_imports, unused_variables, dead_code)] |
| 10 |
|
| 11 |
use std::cell::Cell; |
| 12 |
use std::collections::{HashMap, HashSet}; |
| 13 |
use std::sync::{Arc, Mutex}; |
| 14 |
|
| 15 |
#[cfg(feature = "dbus")] |
| 16 |
use dbus::{ |
| 17 |
arg::{self, RefArg}, |
| 18 |
ffidisp::{BusType, Connection, NameFlag}, |
| 19 |
tree::{self, Factory, Interface, MTFn, MTSync, Tree}, |
| 20 |
Path, |
| 21 |
}; |
| 22 |
|
| 23 |
use super::{Hint, Notification, Timeout}; |
| 24 |
use crate::xdg::{NOTIFICATION_NAMESPACE, NOTIFICATION_OBJECTPATH}; |
| 25 |
|
| 26 |
static DBUS_ERROR_FAILED: &str = "org.freedesktop.DBus.Error.Failed"; |
| 27 |
|
| 28 |
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug, Default)] |
| 33 |
pub struct NotificationServer { |
| 34 |
|
| 35 |
counter: Mutex<Cell<u32>>, |
| 36 |
|
| 37 |
|
| 38 |
stopped: Mutex<Cell<bool>>, |
| 39 |
} |
| 40 |
|
| 41 |
impl NotificationServer { |
| 42 |
fn count_up(&self) { |
| 43 |
if let Ok(counter) = self.counter.lock() { |
| 44 |
counter.set(counter.get() + 1); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
fn stop(&self) { |
| 49 |
if let Ok(stop) = self.stopped.lock() { |
| 50 |
stop.set(true); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
fn is_stopped(&self) -> bool { |
| 55 |
if let Ok(stop) = self.stopped.lock() { |
| 56 |
stop.get() |
| 57 |
} else { |
| 58 |
true |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
pub fn create() -> Arc<NotificationServer> { |
| 64 |
Arc::new(NotificationServer::default()) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
pub fn start<F: 'static>(me: &Arc<Self>, closure: F) |
| 75 |
where |
| 76 |
F: Fn(&Notification), |
| 77 |
{ |
| 78 |
let connection = Connection::get_private(BusType::Session).unwrap(); |
| 79 |
|
| 80 |
connection.release_name(NOTIFICATION_NAMESPACE).unwrap(); |
| 81 |
connection |
| 82 |
.register_name(NOTIFICATION_NAMESPACE, NameFlag::ReplaceExisting as u32) |
| 83 |
.unwrap(); |
| 84 |
connection |
| 85 |
.register_object_path(NOTIFICATION_OBJECTPATH) |
| 86 |
.unwrap(); |
| 87 |
|
| 88 |
let mytex = Arc::new(Mutex::new(me.clone())); |
| 89 |
|
| 90 |
let factory = Factory::new_fn::<()>(); |
| 91 |
let tree = factory.tree(()).add( |
| 92 |
factory |
| 93 |
.object_path(NOTIFICATION_OBJECTPATH, ()) |
| 94 |
.introspectable() |
| 95 |
.add( |
| 96 |
factory |
| 97 |
.interface(NOTIFICATION_NAMESPACE, ()) |
| 98 |
.add_m(method_notify(&factory, closure)) |
| 99 |
.add_m(method_close_notification(&factory)) |
| 100 |
.add_m(Self::stop_server(mytex.clone(), &factory)) |
| 101 |
|
| 102 |
|
| 103 |
.add_m(method_get_capabilities(&factory)) |
| 104 |
.add_m(method_get_server_information(&factory)), |
| 105 |
), |
| 106 |
); |
| 107 |
|
| 108 |
connection.add_handler(tree); |
| 109 |
|
| 110 |
while !me.is_stopped() { |
| 111 |
|
| 112 |
|
| 113 |
if let Some(received) = connection.incoming(1000).next() { |
| 114 |
println!("RECEIVED {:?}", received); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
fn stop_server( |
| 120 |
me: Arc<Mutex<Arc<Self>>>, |
| 121 |
factory: &Factory<MTFn>, |
| 122 |
) -> tree::Method<MTFn<()>, ()> { |
| 123 |
factory |
| 124 |
.method("Stop", (), move |minfo| { |
| 125 |
if let Ok(me) = me.lock() { |
| 126 |
me.stop(); |
| 127 |
println!("STOPPING"); |
| 128 |
Ok(vec![]) |
| 129 |
} else { |
| 130 |
Err(tree::MethodErr::failed(&String::from("nope!"))) |
| 131 |
} |
| 132 |
}) |
| 133 |
.out_arg(("", "u")) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
fn hints_from_variants<A: RefArg>(hints: &HashMap<String, A>) -> HashSet<Hint> { |
| 138 |
hints.iter().map(Into::into).collect() |
| 139 |
} |
| 140 |
|
| 141 |
fn method_notify<F: 'static>( |
| 142 |
factory: &Factory<MTFn>, |
| 143 |
on_notification: F, |
| 144 |
) -> tree::Method<MTFn<()>, ()> |
| 145 |
where |
| 146 |
F: Fn(&Notification), |
| 147 |
{ |
| 148 |
factory |
| 149 |
.method("Notify", (), move |minfo| { |
| 150 |
let mut i = minfo.msg.iter_init(); |
| 151 |
let appname: String = i.read()?; |
| 152 |
let replaces_id: u32 = i.read()?; |
| 153 |
let icon: String = i.read()?; |
| 154 |
let summary: String = i.read()?; |
| 155 |
let body: String = i.read()?; |
| 156 |
let actions: Vec<String> = i.read()?; |
| 157 |
let hints: ::std::collections::HashMap<String, arg::Variant<Box<dyn RefArg>>> = |
| 158 |
i.read()?; |
| 159 |
let timeout: i32 = i.read()?; |
| 160 |
println!("hints {:?} ", hints); |
| 161 |
|
| 162 |
|
| 163 |
let notification = Notification { |
| 164 |
appname, |
| 165 |
icon, |
| 166 |
summary, |
| 167 |
body, |
| 168 |
actions, |
| 169 |
hints: hints_from_variants(&hints), |
| 170 |
timeout: Timeout::from(timeout), |
| 171 |
id: if replaces_id == 0 { |
| 172 |
None |
| 173 |
} else { |
| 174 |
Some(replaces_id) |
| 175 |
}, |
| 176 |
subtitle: None, |
| 177 |
}; |
| 178 |
|
| 179 |
on_notification(¬ification); |
| 180 |
|
| 181 |
let arg0 = 43; |
| 182 |
let rm = minfo.msg.method_return(); |
| 183 |
let rm = rm.append1(arg0); |
| 184 |
Ok(vec![rm]) |
| 185 |
}) |
| 186 |
.in_arg(("app_name", "s")) |
| 187 |
.in_arg(("replaces_id", "u")) |
| 188 |
.in_arg(("app_icon", "s")) |
| 189 |
.in_arg(("summary", "s")) |
| 190 |
.in_arg(("body", "s")) |
| 191 |
.in_arg(("actions", "as")) |
| 192 |
.in_arg(("hints", "a{sv}")) |
| 193 |
.in_arg(("timeout", "i")) |
| 194 |
.out_arg(("", "u")) |
| 195 |
} |
| 196 |
|
| 197 |
fn method_close_notification(factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> { |
| 198 |
factory |
| 199 |
.method("CloseNotification", (), |minfo| { |
| 200 |
let i = minfo.msg.iter_init(); |
| 201 |
let rm = minfo.msg.method_return(); |
| 202 |
Ok(vec![rm]) |
| 203 |
}) |
| 204 |
.in_arg(("id", "u")) |
| 205 |
} |
| 206 |
|
| 207 |
fn method_get_capabilities(factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> { |
| 208 |
factory |
| 209 |
.method("GetCapabilities", (), |minfo| { |
| 210 |
let caps: Vec<String> = vec![]; |
| 211 |
let rm = minfo.msg.method_return(); |
| 212 |
let rm = rm.append1(caps); |
| 213 |
Ok(vec![rm]) |
| 214 |
}) |
| 215 |
.out_arg(("caps", "as")) |
| 216 |
} |
| 217 |
|
| 218 |
fn method_get_server_information(factory: &Factory<MTFn>) -> tree::Method<MTFn<()>, ()> { |
| 219 |
factory |
| 220 |
.method("GetServerInformation", (), |minfo| { |
| 221 |
let (name, vendor, version, spec_version) = ( |
| 222 |
"notify-rust", |
| 223 |
"notify-rust", |
| 224 |
env!("CARGO_PKG_VERSION"), |
| 225 |
"0.0.0", |
| 226 |
); |
| 227 |
let rm = minfo.msg.method_return(); |
| 228 |
let rm = rm.append1(name); |
| 229 |
let rm = rm.append1(vendor); |
| 230 |
let rm = rm.append1(version); |
| 231 |
let rm = rm.append1(spec_version); |
| 232 |
Ok(vec![rm]) |
| 233 |
}) |
| 234 |
.out_arg(("name", "s")) |
| 235 |
.out_arg(("vendor", "s")) |
| 236 |
.out_arg(("version", "s")) |
| 237 |
.out_arg(("spec_version", "s")) |
| 238 |
} |