SNot/src/notification.rs

113 lines
3.2 KiB
Rust
Raw Normal View History

use crate::formats::serde::serialize_actions;
use serde::Serialize;
2024-09-17 18:32:16 +00:00
use std::collections::HashMap;
use zbus::{message::Body, Result};
use zvariant::OwnedValue;
2024-09-17 18:32:16 +00:00
// A notificaion object
#[derive(Serialize)] // To help with json, rson
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.
#[serde(serialize_with = "serialize_actions")]
2024-09-17 18:32:16 +00:00
actions: Vec<String>,
2024-09-19 10:45:52 +00:00
// Useful extra data - notif type, urgency, notif sound, icon data, etc.
//#[serde(serialize_with = "serialize_hints")]
2024-09-17 18:32:16 +00:00
hints: HashMap<String, OwnedValue>,
// Seconds till this notif expires. Optional
expir_timeout: i32,
}
impl Notification {
// App name
pub fn app_name(&self) -> String {
self.app_name.clone()
}
// Replace ID
pub fn replace_id(&self) -> u32 {
self.replace_id
}
// Icon
pub fn icon(&self) -> String {
self.icon.clone()
}
// Summary
pub fn summary(&self) -> String {
self.summary.clone()
}
// Body
pub fn body(&self) -> String {
self.body.clone()
}
2024-09-17 11:23:57 +00:00
2024-09-17 18:32:16 +00:00
// Key:Val pairs of actions
2024-09-22 05:46:51 +00:00
pub fn actions(&self) -> Vec<(String, String)> {
2024-09-26 08:07:39 +00:00
self.actions
.chunks(2)
.map(|chunk| (chunk[0].clone(), chunk[1].clone()))
.collect()
}
2024-09-17 18:32:16 +00:00
// Hints
pub fn hints(&self) -> &HashMap<String, OwnedValue> {
&self.hints
}
// Expiry timeout
pub fn expir_timeout(&self) -> i32 {
self.expir_timeout
}
2024-09-17 18:32:16 +00:00
// 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
// Urgency
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(),
_ => "Other".to_string(), // There are no accepted values other that these 3
}
2024-09-18 11:23:39 +00:00
}
None => "Unknown".to_string(), // This possibly never occurs, or something might be wrong
2024-09-18 11:23:39 +00:00
}
}
}
2024-09-17 18:32:16 +00:00
// Convert the DBus message body into Notification
pub fn to_notif(msg_body: &Body) -> Result<Notification> {
// Deserialize body into a tuple
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
2024-09-17 18:32:16 +00:00
msg_body.deserialize()?;
2024-09-19 10:45:52 +00:00
// Form a Notification object from the obtained tuple's fields
Ok(Notification {
app_name,
replace_id,
icon,
summary,
body,
actions,
hints,
expir_timeout,
})
}