modularity, urgency method, and other changes
This commit is contained in:
parent
b9513b903c
commit
ff213fdd70
69
src/main.rs
69
src/main.rs
@ -1,83 +1,52 @@
|
|||||||
|
mod notification;
|
||||||
|
use notification::{print_notif, to_notif};
|
||||||
|
|
||||||
use futures_util::stream::TryStreamExt;
|
use futures_util::stream::TryStreamExt;
|
||||||
use std::collections::HashMap;
|
use zbus::{Connection, Result};
|
||||||
use zbus::{message::Body, Connection, Result};
|
|
||||||
use zvariant::OwnedValue;
|
|
||||||
|
|
||||||
struct Notification {
|
|
||||||
app_name: String,
|
|
||||||
replace_id: u32,
|
|
||||||
icon: String,
|
|
||||||
summary: String,
|
|
||||||
body: String,
|
|
||||||
actions: Vec<String>,
|
|
||||||
hints: HashMap<String, OwnedValue>,
|
|
||||||
expir_timeout: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_notif(msg_body: Body) -> Result<Notification> {
|
|
||||||
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
|
|
||||||
msg_body.deserialize()?;
|
|
||||||
Ok(Notification {
|
|
||||||
app_name,
|
|
||||||
replace_id,
|
|
||||||
icon,
|
|
||||||
summary,
|
|
||||||
body,
|
|
||||||
actions,
|
|
||||||
hints,
|
|
||||||
expir_timeout,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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:?}");
|
|
||||||
}
|
|
||||||
println!("Expiration Timeout: {0} seconds\n", notif.expir_timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let connection = Connection::session().await?;
|
let connection = Connection::session().await?;
|
||||||
connection
|
connection
|
||||||
.request_name("org.freedesktop.Notifications") // Requesting dbus for this service name. Any other services using this name should be stopped/disabled before this
|
.request_name("org.freedesktop.Notifications") // Requesting dbus for this service name. Any other services/progs using this name should be stopped/disabled before this
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut stream = zbus::MessageStream::from(&connection); // Convert connection to a MessageStream, yields Message items
|
let mut stream = zbus::MessageStream::from(&connection); // Convert connection to a MessageStream, yields Message items
|
||||||
|
|
||||||
while let Some(msg) = stream.try_next().await? {
|
while let Some(msg) = stream.try_next().await? {
|
||||||
// Check if the message is a method call to the "Notify" method
|
// Check if the message is a method call to the "Notify" method
|
||||||
if let Some(member) = msg.header().member().map(|m| m.as_str()) {
|
if let Some(member) = msg.header().member() {
|
||||||
match member {
|
let member_name = member.as_str();
|
||||||
|
match member_name {
|
||||||
"GetServerInformation" => {
|
"GetServerInformation" => {
|
||||||
// Respond with server information
|
// Respond with server information
|
||||||
let response = ("SNot", "candifloss.cc", "0.1.0", "0.1.0"); // (name, vendor, version, spec_version)
|
let response = ("SNot", "candifloss.cc", "0.1.0", "0.1.0"); // (name, vendor, version, spec_version)
|
||||||
connection.reply(&msg, &response).await?;
|
connection.reply(&msg, &response).await?;
|
||||||
println!("Request received: {member}\n\tName: SNot\n\tVendor: candifloss.cc\n\tVersion: 0.1.0\n\tSpec_version: 0.1.0");
|
println!("Request received: {member}\n\tName: SNot\n\tVendor: candifloss.cc\n\tVersion: 0.1.0\n\tSpec_version: 0.1.0");
|
||||||
}
|
}
|
||||||
|
"GetCapabilities" => {
|
||||||
|
// Respond with supported capabilities
|
||||||
|
let capabilities = vec!["actions", "body", "body-hyperlinks"];
|
||||||
|
connection.reply(&msg, &capabilities).await?;
|
||||||
|
println!("Request received: {member}\n\tCapabilities: {capabilities:?}");
|
||||||
|
}
|
||||||
"Notify" => {
|
"Notify" => {
|
||||||
// Handle new received notif
|
// Handle new received notif
|
||||||
println!("New notification:");
|
println!("New notification:");
|
||||||
let msg_body = msg.body();
|
let msg_body = msg.body();
|
||||||
// get the app_name, summary, body, etc. from the msg_body
|
// get the app_name, summary, body, etc. from the msg_body
|
||||||
let notif = to_notif(msg_body);
|
let notif = to_notif(&msg_body)?;
|
||||||
// Print the notif
|
// Print the notif
|
||||||
let _ = print_notif(notif.expect("Failed to parse notification"));
|
print_notif(¬if);
|
||||||
|
println!("{}", notif.urgency());
|
||||||
// Done. Respond to the client with a notification ID
|
// Done. Respond to the client with a notification ID
|
||||||
let notification_id: u32 = 1; // This could be incremented or generated. Do it l8r
|
let notification_id: u32 = 1; // This could be incremented or generated. Do it l8r
|
||||||
connection.reply(&msg, ¬ification_id).await?; // The client will stop when it gets the response
|
connection.reply(&msg, ¬ification_id).await?; // The client will disconnect when it gets this response
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Unhandled method: {member}");
|
println!("Unhandled method: {member}");
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
60
src/notification.rs
Normal file
60
src/notification.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use zbus::{message::Body, Result};
|
||||||
|
use zvariant::OwnedValue;
|
||||||
|
|
||||||
|
pub struct Notification {
|
||||||
|
app_name: String,
|
||||||
|
replace_id: u32,
|
||||||
|
icon: String,
|
||||||
|
summary: String,
|
||||||
|
body: String,
|
||||||
|
actions: Vec<String>,
|
||||||
|
hints: HashMap<String, OwnedValue>,
|
||||||
|
expir_timeout: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Notification {
|
||||||
|
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(),
|
||||||
|
_ => "Unknown".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => "Unknown".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_notif(msg_body: &Body) -> Result<Notification> {
|
||||||
|
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
|
||||||
|
msg_body.deserialize()?;
|
||||||
|
Ok(Notification {
|
||||||
|
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 ¬if.hints {
|
||||||
|
println!(" {key}: {value:?}");
|
||||||
|
}
|
||||||
|
println!("Expiration Timeout: {0} seconds\n", notif.expir_timeout);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user