|
| 1 | +use std::str::FromStr; |
| 2 | +use std::time::Duration; |
| 3 | + |
| 4 | +use crossbeam_channel::Sender; |
| 5 | +use serde_derive::Deserialize; |
| 6 | + |
| 7 | +use std::io::Read; |
| 8 | +use std::io::Write; |
| 9 | +use std::os::unix::net::UnixStream; |
| 10 | +use std::path::Path; |
| 11 | + |
| 12 | +use crate::blocks::{Block, ConfigBlock, Update}; |
| 13 | +use crate::config::SharedConfig; |
| 14 | +use crate::de::deserialize_duration; |
| 15 | +use crate::errors::*; |
| 16 | +use crate::formatting::value::Value; |
| 17 | +use crate::formatting::FormatTemplate; |
| 18 | +use crate::protocol::i3bar_event::I3BarEvent; |
| 19 | +use crate::protocol::i3bar_event::MouseButton; |
| 20 | +use crate::scheduler::Task; |
| 21 | +use crate::subprocess::spawn_child_async; |
| 22 | +use crate::widgets::text::TextWidget; |
| 23 | +use crate::widgets::I3BarWidget; |
| 24 | +use crate::widgets::State; |
| 25 | + |
| 26 | +#[derive(Debug)] |
| 27 | +struct RotificationStatus { |
| 28 | + num: u64, |
| 29 | + crit: u64, |
| 30 | +} |
| 31 | + |
| 32 | +pub struct Rofication { |
| 33 | + id: usize, |
| 34 | + text: TextWidget, |
| 35 | + update_interval: Duration, |
| 36 | + format: FormatTemplate, |
| 37 | + |
| 38 | + // UNIX socket to read from |
| 39 | + pub socket_path: String, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Deserialize, Debug, Clone)] |
| 43 | +#[serde(deny_unknown_fields, default)] |
| 44 | +pub struct RoficationConfig { |
| 45 | + /// Update interval in seconds |
| 46 | + #[serde(deserialize_with = "deserialize_duration")] |
| 47 | + pub interval: Duration, |
| 48 | + |
| 49 | + // UNIX socket to read from |
| 50 | + pub socket_path: String, |
| 51 | + |
| 52 | + /// Format override |
| 53 | + pub format: FormatTemplate, |
| 54 | +} |
| 55 | + |
| 56 | +impl Default for RoficationConfig { |
| 57 | + fn default() -> Self { |
| 58 | + Self { |
| 59 | + interval: Duration::from_secs(1), |
| 60 | + socket_path: "/tmp/rofi_notification_daemon".to_string(), |
| 61 | + format: FormatTemplate::default(), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl ConfigBlock for Rofication { |
| 67 | + type Config = RoficationConfig; |
| 68 | + |
| 69 | + fn new( |
| 70 | + id: usize, |
| 71 | + block_config: Self::Config, |
| 72 | + shared_config: SharedConfig, |
| 73 | + _tx_update_request: Sender<Task>, |
| 74 | + ) -> Result<Self> { |
| 75 | + let text = TextWidget::new(id, 0, shared_config.clone()) |
| 76 | + .with_text("?") |
| 77 | + .with_icon("bell")? |
| 78 | + .with_state(State::Good); |
| 79 | + |
| 80 | + Ok(Rofication { |
| 81 | + id, |
| 82 | + update_interval: block_config.interval, |
| 83 | + text, |
| 84 | + socket_path: block_config.socket_path, |
| 85 | + format: block_config.format.with_default("{num}")?, |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl Block for Rofication { |
| 91 | + fn update(&mut self) -> Result<Option<Update>> { |
| 92 | + match rofication_status(&self.socket_path) { |
| 93 | + Ok(status) => { |
| 94 | + let values = map!( |
| 95 | + "num" => Value::from_string(status.num.to_string()), |
| 96 | + ); |
| 97 | + |
| 98 | + self.text.set_texts(self.format.render(&values)?); |
| 99 | + if status.crit > 0 { |
| 100 | + self.text.set_state(State::Critical) |
| 101 | + } else { |
| 102 | + if status.num > 0 { |
| 103 | + self.text.set_state(State::Warning) |
| 104 | + } else { |
| 105 | + self.text.set_state(State::Good) |
| 106 | + } |
| 107 | + } |
| 108 | + self.text.set_icon("bell")?; |
| 109 | + } |
| 110 | + Err(_) => { |
| 111 | + self.text.set_text("?".to_string()); |
| 112 | + self.text.set_state(State::Critical); |
| 113 | + self.text.set_icon("bell-slash")?; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + Ok(Some(self.update_interval.into())) |
| 118 | + } |
| 119 | + |
| 120 | + fn view(&self) -> Vec<&dyn I3BarWidget> { |
| 121 | + vec![&self.text] |
| 122 | + } |
| 123 | + |
| 124 | + fn click(&mut self, event: &I3BarEvent) -> Result<()> { |
| 125 | + if event.button == MouseButton::Left { |
| 126 | + spawn_child_async("rofication-gui", &[]) |
| 127 | + .block_error("rofication", "could not spawn gui")?; |
| 128 | + } |
| 129 | + Ok(()) |
| 130 | + } |
| 131 | + |
| 132 | + fn id(&self) -> usize { |
| 133 | + self.id |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +fn rofication_status(socket_path: &str) -> Result<RotificationStatus> { |
| 138 | + let socket = Path::new(socket_path); |
| 139 | + // Connect to socket |
| 140 | + let mut stream = match UnixStream::connect(&socket) { |
| 141 | + Err(_) => { |
| 142 | + return Err(BlockError( |
| 143 | + "rofication".to_string(), |
| 144 | + "Failed to connect to socket".to_string(), |
| 145 | + )) |
| 146 | + } |
| 147 | + Ok(stream) => stream, |
| 148 | + }; |
| 149 | + |
| 150 | + // Request count |
| 151 | + match stream.write(b"num\n") { |
| 152 | + Err(_) => { |
| 153 | + return Err(BlockError( |
| 154 | + "rofication".to_string(), |
| 155 | + "Failed to write to socket".to_string(), |
| 156 | + )) |
| 157 | + } |
| 158 | + Ok(_) => {} |
| 159 | + }; |
| 160 | + |
| 161 | + // Response must be two comma separated integers: regular and critical |
| 162 | + let mut buffer = String::new(); |
| 163 | + match stream.read_to_string(&mut buffer) { |
| 164 | + Err(_) => { |
| 165 | + return Err(BlockError( |
| 166 | + "rofication".to_string(), |
| 167 | + "Failed to read from socket".to_string(), |
| 168 | + )) |
| 169 | + } |
| 170 | + Ok(_) => {} |
| 171 | + }; |
| 172 | + |
| 173 | + let values = buffer.split(',').collect::<Vec<&str>>(); |
| 174 | + if values.len() != 2 { |
| 175 | + return Err(BlockError( |
| 176 | + "rofication".to_string(), |
| 177 | + "Format error".to_string(), |
| 178 | + )); |
| 179 | + } |
| 180 | + |
| 181 | + let num = match values[0].parse::<u64>() { |
| 182 | + Ok(num) => num, |
| 183 | + Err(_) => { |
| 184 | + return Err(BlockError( |
| 185 | + "rofication".to_string(), |
| 186 | + "Failed to parse num".to_string(), |
| 187 | + )) |
| 188 | + } |
| 189 | + }; |
| 190 | + let crit = match values[1].parse::<u64>() { |
| 191 | + Ok(crit) => crit, |
| 192 | + Err(_) => { |
| 193 | + return Err(BlockError( |
| 194 | + "rofication".to_string(), |
| 195 | + "Failed to parse crit".to_string(), |
| 196 | + )) |
| 197 | + } |
| 198 | + }; |
| 199 | + |
| 200 | + Ok(RotificationStatus { num, crit }) |
| 201 | +} |
0 commit comments