extern crate getopts;
extern crate haiku;
use getopts::Options;
use haiku::app::{Application, ApplicationDelegate, ApplicationHooks, Notification, NotificationType};
const SIGNATURE: &str = "application/x-vnd.HaikuRS-notify";
struct NotifyApp {
options: Options,
notification: Option<Notification>
}
impl ApplicationHooks for NotifyApp {
fn ready_to_run(&mut self, application: &ApplicationDelegate) {
match &self.notification {
Some(n) => n.send(&application.messenger, None).expect("Error sending notification"),
None => print_usage("notify", &self.options)
}
application.quit();
}
fn argv_received(&mut self, _application: &ApplicationDelegate, argv: Vec<String>) {
if argv.len() <= 1 {
return;
}
let mut matches = match self.options.parse(&argv[1..]) {
Ok(m) => m,
Err(f) => {
println!("{}", f.to_string());
return;
}
};
if matches.opt_present("h") {
return;
}
let mut notification = Notification::default();
if matches.free.len() != 1 {
println!("cannot find the required MESSAGE parameter");
return;
}
notification.content = matches.free.pop();
if matches.opt_present("t") {
let t = matches.opt_str("t").unwrap();
notification.notification_type = match t.as_str() {
"information" => NotificationType::Information,
"important" => NotificationType::Important,
"error" => NotificationType::Error,
"progress" => NotificationType::Progress,
_ => {
println!("Invalid TYPE parameter. Please pass one of 'information', 'important', 'error' or 'progress'");
return
}
};
}
if matches.opt_present("T") {
notification.title = matches.opt_str("T");
}
if matches.opt_present("g") {
notification.group = matches.opt_str("g");
}
if matches.opt_present("i") {
notification.id = matches.opt_str("i");
}
if matches.opt_present("p") {
if notification.notification_type != NotificationType::Progress {
println!("You can only set the progress parameter when the notification type is of `progress`");
return;
}
let progress_string = matches.opt_str("p").unwrap();
let progress = match progress_string.parse::<f32>() {
Ok(p) => p,
Err(_) => {
println!("Invalid value for the progress parameter");
return;
}
};
if progress < 0.0 || progress > 1.0 {
println!("Enter a value between 0.0 and 1.0 for the progress parameter");
return;
}
notification.progress = progress;
}
self.notification = Some(notification);
}
}
fn build_options() -> Options {
let mut options = Options::new();
options.optopt("t", "type", "the type of option (information, important, error, progress)", "TYPE");
options.optopt("T", "title", "title for your notification", "TITLE");
options.optopt("g", "group", "the group name for this notification", "GROUP");
options.optopt("i", "id", "the id that uniquely identifies this notification", "ID");
options.optopt("p", "progress", "the value between 0.0 and 1.0 that expresses the progress, defaults to 0.0", "PROGRESS");
options.optflag("h", "help", "print this help menu");
options
}
fn print_usage(program: &str, opts: &Options) {
let brief = format!("Usage: {} [options] MESSAGE", program);
print!("{}", opts.usage(&brief));
}
fn main() {
let state = NotifyApp{ options: build_options(), notification: None };
let app = Application::new(SIGNATURE, state);
app.run().unwrap();
}