From 1bb1cf7aac639b6bb6df89b3a091bebb61a4462b Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 2 Mar 2019 21:40:57 +0200 Subject: [PATCH] add notification filter concerns #64 --- src/bin.rs | 1 + ui/src/components/notifications.rs | 35 ++++++++++++++++++++++++++++++ ui/src/conf.rs | 5 +++++ ui/src/conf/notifications.rs | 33 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 ui/src/conf/notifications.rs diff --git a/src/bin.rs b/src/bin.rs index fb0071d15..a0e8edd5b 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -75,6 +75,7 @@ fn main() { let xdg_notifications = Entity::from(Box::new(ui::components::notifications::XDGNotifications {})); state.register_entity(xdg_notifications); + state.register_entity(Entity::from(Box::new(ui::components::notifications::NotificationFilter {}))); /* Keep track of the input mode. See ui::UIMode for details */ 'main: loop { diff --git a/ui/src/components/notifications.rs b/ui/src/components/notifications.rs index 7fd846ede..cb73d0f53 100644 --- a/ui/src/components/notifications.rs +++ b/ui/src/components/notifications.rs @@ -23,6 +23,7 @@ Notification handling components. */ use notify_rust::Notification as notify_Notification; +use std::process::{Command, Stdio}; use super::*; @@ -104,3 +105,37 @@ mod tests { .unwrap(); } } + +/// Passes notifications to the OS using the XDG specifications. +#[derive(Debug)] +pub struct NotificationFilter {} + +impl fmt::Display for NotificationFilter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // TODO display subject/info + write!(f, "") + } +} + +impl Component for NotificationFilter { + fn draw(&mut self, _grid: &mut CellBuffer, _area: Area, _context: &mut Context) {} + fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool { + if let UIEventType::Notification(ref title, ref body) = event.event_type { + if let Some(ref bin) = context.runtime_settings.notifications.script { + if let Err(v) = Command::new(bin) + .arg(title + .as_ref() + .map(|v| v.as_str()) + .unwrap_or("Event")) + .arg(body) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() { + eprintln!("{:?}",v); + } + } + } + false + } + fn set_dirty(&mut self) {} +} diff --git a/ui/src/conf.rs b/ui/src/conf.rs index 7e6a1c267..81e5f2238 100644 --- a/ui/src/conf.rs +++ b/ui/src/conf.rs @@ -25,6 +25,7 @@ extern crate xdg; extern crate bincode; pub mod pager; +pub mod notifications; pub mod accounts; pub use self::accounts::Account; @@ -33,6 +34,7 @@ use self::config::{Config, File, FileFormat}; use melib::conf::AccountSettings; use melib::error::*; use pager::PagerSettings; +use self::notifications::NotificationsSettings; use self::serde::{de, Deserialize, Deserializer}; use std::collections::HashMap; @@ -110,6 +112,7 @@ impl FileAccount { struct FileSettings { accounts: HashMap, pager: PagerSettings, + notifications: NotificationsSettings, } #[derive(Debug, Clone, Default)] @@ -134,6 +137,7 @@ impl AccountConf { pub struct Settings { pub accounts: HashMap, pub pager: PagerSettings, + pub notifications: NotificationsSettings, } impl FileSettings { @@ -179,6 +183,7 @@ impl Settings { Settings { accounts: s, pager: fs.pager, + notifications: fs.notifications, } } } diff --git a/ui/src/conf/notifications.rs b/ui/src/conf/notifications.rs new file mode 100644 index 000000000..96c754992 --- /dev/null +++ b/ui/src/conf/notifications.rs @@ -0,0 +1,33 @@ +/* + * meli - notifications conf module + * + * Copyright 2018 Manos Pitsidianakis + * + * This file is part of meli. + * + * meli is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * meli is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with meli. If not, see . + */ + +fn none() -> Option { + None +} + +/// Settings for the notifications function. +#[derive(Debug, Deserialize, Clone, Default)] +pub struct NotificationsSettings { + /// A command to pipe notifications through + /// Default: None + #[serde(default = "none")] + pub script: Option, +}