SNot/src/notification.rs

72 lines
2.6 KiB
Rust
Raw Normal View History

2024-09-17 11:23:57 +00:00
use std::collections::{btree_map::Range, HashMap};
use zbus::{message::Body, Result};
use zvariant::OwnedValue;
pub struct Notification {
2024-09-17 06:47:05 +00:00
// A notificaion object
app_name: String, // The application that sent the notification
replace_id: u32, // (Optional) ID of an existing notification to be updated, replaced by this
icon: String, // See icon specifications: https://specifications.freedesktop.org/notification-spec/latest/icons-and-images.html
summary: String, // Notification title
body: String, // Notification content/body
actions: Vec<String>, // Action requests that can be sent back to the client -
hints: HashMap<String, OwnedValue>, // Extra useful data - notif type, urgency, sound file, icon data, etc.
expir_timeout: i32, // Seconds till this notif expires. Optional
}
impl Notification {
pub fn urgency(&self) -> String {
2024-09-17 06:47:05 +00:00
// Obtain urgency
match self.hints.get("urgency") {
Some(value) => {
// Attempt to convert OwnedValue to u8
match u8::try_from(value) {
Ok(0) => "Low".to_string(),
Ok(1) => "Normal".to_string(),
Ok(2) => "Critical".to_string(),
2024-09-17 06:47:05 +00:00
_ => "Other".to_string(), // There are no accepted values other that these 3
}
}
2024-09-17 06:47:05 +00:00
None => "Unknown".to_string(), // This possibly never occurs, or something might be wrong
}
}
2024-09-17 11:23:57 +00:00
pub fn actions(&self) -> HashMap<String, String> {
let mut acts: HashMap<String, String>;
for i in Range(self.actions.len()) {
}
}
}
pub fn to_notif(msg_body: &Body) -> Result<Notification> {
2024-09-17 06:47:05 +00:00
// Convert the DBus message body into Notification
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
2024-09-17 06:47:05 +00:00
msg_body.deserialize()?; // Deserialized into a tuple
Ok(Notification {
2024-09-17 06:47:05 +00:00
// Make a Notification object from the obtained tuple
app_name,
replace_id,
icon,
summary,
body,
actions,
hints,
expir_timeout,
})
}
pub fn print_notif(notif: &Notification) {
println!("App Name: {0}", notif.app_name);
println!("Replace ID: {0}", notif.replace_id);
println!("Icon: {0}", notif.icon);
println!("Summary: {0}", notif.summary);
println!("Body: {0}", notif.body);
println!("Actions: {0:?}", notif.actions);
println!("Hints:");
for (key, value) in &notif.hints {
println!(" {key}: {value:?}");
}
2024-09-16 21:28:58 +00:00
println!("Expiration Timeout: {0} seconds", notif.expir_timeout);
}