use notify_rust::{Notification, Timeout};
use error;
use language::loader;
use std::fs;
use yaml_rust::YamlLoader;
use ServiceKind;
enum NotificationKind {
Sent,
SendFailure,
}
fn notification(service: ServiceKind, notification: NotificationKind) -> String {
let mut locator = &YamlLoader::load_from_str(&loader()).unwrap()[0]["Notification"];
match notification {
NotificationKind::Sent => {
locator = &locator["Sent"];
match service {
ServiceKind::Twitter => locator = &locator["Twitter"],
ServiceKind::Mastodon => locator = &locator["Mastodon"],
ServiceKind::Imgur => locator = &locator["Imgur"],
}
}
NotificationKind::SendFailure => {
locator = &locator["Not_Sent"];
match service {
ServiceKind::Twitter => locator = &locator["Twitter"],
ServiceKind::Mastodon => locator = &locator["Mastodon"],
ServiceKind::Imgur => locator = &locator["Imgur"],
}
}
}
return format!("{}", locator.as_str().unwrap());
}
pub fn image_sent(service: ServiceKind, text: &str, img: &str) {
match Notification::new()
.appname("ShareXin")
.summary(¬ification(service, NotificationKind::Sent))
.body(text)
.icon(img)
.timeout(Timeout::Milliseconds(2000))
.show()
{
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", error::message(23));
return;
}
};
match fs::remove_file(img.clone()) {
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", error::message(0));
return;
}
};
}
pub fn message_sent(service: ServiceKind, text: &str) {
match Notification::new()
.appname("ShareXin")
.summary(¬ification(service, NotificationKind::Sent))
.body(text)
.timeout(Timeout::Milliseconds(2000))
.show()
{
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", error::message(23));
return;
}
};
}
pub fn not_sent(service: ServiceKind) {
match Notification::new()
.appname("ShareXin")
.summary(¬ification(service, NotificationKind::SendFailure))
.show()
{
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", error::message(23));
return;
}
};
}
pub fn error(code: usize) {
match Notification::new()
.appname("ShareXin")
.summary(&error::message(code))
.show()
{
Ok(ok) => ok,
Err(_) => {
eprintln!("{}", error::message(23));
return;
}
};
}