2024-09-17 18:32:16 +00:00
|
|
|
use std::collections::HashMap;
|
2024-09-16 20:51:27 +00:00
|
|
|
use zbus::{message::Body, Result};
|
|
|
|
use zvariant::OwnedValue;
|
|
|
|
|
2024-09-17 18:32:16 +00:00
|
|
|
// A notificaion object
|
2024-09-16 20:51:27 +00:00
|
|
|
pub struct Notification {
|
2024-09-17 18:32:16 +00:00
|
|
|
// The application that sent the notification
|
|
|
|
app_name: String,
|
|
|
|
// (Optional) ID of an existing notification to be updated, replaced by this
|
|
|
|
replace_id: u32,
|
|
|
|
// See icon specifications: https://specifications.freedesktop.org/notification-spec/latest/icons-and-images.html
|
|
|
|
icon: String,
|
|
|
|
// Notification title
|
|
|
|
summary: String,
|
|
|
|
// Notification content/body
|
|
|
|
body: String,
|
|
|
|
// Action requests that can be sent back to the client - "Reply," "Mark as Read," "Play/Pause/Next," "Snooze/Dismiss," etc.
|
|
|
|
actions: Vec<String>,
|
|
|
|
// Extra useful data - notif type, urgency, sound file, icon data, etc.
|
|
|
|
hints: HashMap<String, OwnedValue>,
|
|
|
|
// Seconds till this notif expires. Optional
|
|
|
|
expir_timeout: i32,
|
2024-09-16 20:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Notification {
|
2024-09-17 18:32:16 +00:00
|
|
|
// Obtain urgency
|
2024-09-16 20:51:27 +00:00
|
|
|
pub fn urgency(&self) -> String {
|
|
|
|
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-16 20:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-17 06:47:05 +00:00
|
|
|
None => "Unknown".to_string(), // This possibly never occurs, or something might be wrong
|
2024-09-16 20:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-17 11:23:57 +00:00
|
|
|
|
2024-09-17 18:32:16 +00:00
|
|
|
// Key:Val pairs of actions
|
|
|
|
pub fn actions(&self) -> Vec<(String, String)> {
|
|
|
|
let mut actions: Vec<(String, String)> = vec![];
|
|
|
|
let acts = &self.actions;
|
|
|
|
let act_len = acts.len();
|
|
|
|
let mut i = 0;
|
|
|
|
while i < act_len {
|
|
|
|
// Action ID, used by the sender to id the clicked action
|
2024-09-18 11:23:39 +00:00
|
|
|
let action_id = &acts[i];
|
|
|
|
// Localised human-readable string that describes the action
|
|
|
|
let action_label = &acts[i + 1];
|
|
|
|
// Pair of (id, label)
|
2024-09-17 18:32:16 +00:00
|
|
|
let action_pair = (action_id.to_owned(), action_label.to_owned());
|
|
|
|
// Add it to the Vec
|
|
|
|
actions.push(action_pair);
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
actions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default action
|
|
|
|
pub fn default_action(&self) -> Option<(String, String)> {
|
|
|
|
if self.actions().is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.actions()[0].clone())
|
2024-09-17 11:23:57 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-18 11:23:39 +00:00
|
|
|
|
|
|
|
// Plain string format, useful for printing
|
|
|
|
pub fn plain(&self) -> String {
|
|
|
|
// Actions
|
|
|
|
let mut actions: String = String::new();
|
|
|
|
if self.actions().is_empty() {
|
|
|
|
actions = "None".to_string();
|
|
|
|
} else {
|
|
|
|
for actn in &self.actions() {
|
|
|
|
actions = actions + "\n\t" + actn.0.as_str() + ": " + actn.1.as_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Default action
|
|
|
|
let def_action: String = match self.default_action() {
|
|
|
|
None => "None".to_string(),
|
|
|
|
Some(actn) => actn.0,
|
|
|
|
};
|
|
|
|
// Hints
|
|
|
|
let mut hints: String = String::new();
|
|
|
|
if self.hints.is_empty() {
|
|
|
|
hints = "None".to_string();
|
|
|
|
} else {
|
|
|
|
for (key, value) in &self.hints {
|
|
|
|
hints = hints + "\n\t" + key + ": " + &value.to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the notification as a plain string
|
|
|
|
let plain_string = format!("App Name: {}\nReplace ID: {}\nIcon: {}\nSummary: {}\nBody: {}\nActions: {}\nHints: {}\nExpiration Timeout: {}\nUrgency: {}\nDefault Action: {}",
|
|
|
|
&self.app_name,
|
|
|
|
&self.replace_id.to_string(),
|
|
|
|
&self.icon,
|
|
|
|
&self.summary,
|
|
|
|
&self.body,
|
|
|
|
actions,
|
|
|
|
hints,
|
|
|
|
&self.expir_timeout,
|
|
|
|
&self.urgency(),
|
|
|
|
def_action,
|
|
|
|
);
|
|
|
|
plain_string
|
|
|
|
}
|
2024-09-16 20:51:27 +00:00
|
|
|
}
|
|
|
|
|
2024-09-17 18:32:16 +00:00
|
|
|
// Convert the DBus message body into Notification
|
2024-09-16 20:51:27 +00:00
|
|
|
pub fn to_notif(msg_body: &Body) -> Result<Notification> {
|
2024-09-17 18:32:16 +00:00
|
|
|
// Deserialized body into a tuple
|
2024-09-16 20:51:27 +00:00
|
|
|
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
|
2024-09-17 18:32:16 +00:00
|
|
|
msg_body.deserialize()?;
|
|
|
|
// Make a Notification object from the obtained tuple
|
2024-09-16 20:51:27 +00:00
|
|
|
Ok(Notification {
|
|
|
|
app_name,
|
|
|
|
replace_id,
|
|
|
|
icon,
|
|
|
|
summary,
|
|
|
|
body,
|
|
|
|
actions,
|
|
|
|
hints,
|
|
|
|
expir_timeout,
|
|
|
|
})
|
|
|
|
}
|