Notification Struct

This commit is contained in:
Candifloss 2024-09-16 16:49:08 +05:30
parent ce4e7a8a43
commit b9513b903c

View File

@ -3,32 +3,44 @@ use std::collections::HashMap;
use zbus::{message::Body, Connection, Result}; use zbus::{message::Body, Connection, Result};
use zvariant::OwnedValue; use zvariant::OwnedValue;
fn print_notif(msg_body: Body) -> Result<()> { struct Notification {
// Deserialize the message body to the expected tuple app_name: String,
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout): ( replace_id: u32,
String, icon: String,
u32, summary: String,
String, body: String,
String, actions: Vec<String>,
String, hints: HashMap<String, OwnedValue>,
Vec<String>, expir_timeout: i32,
HashMap<String, OwnedValue>, }
i32,
) = msg_body.deserialize()?; fn to_notif(msg_body: Body) -> Result<Notification> {
// Print the notification details let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
println!("App Name: {app_name}"); msg_body.deserialize()?;
println!("Replace ID: {replace_id}"); Ok(Notification {
println!("Icon: {icon}"); app_name,
println!("Summary: {summary}"); replace_id,
println!("Body: {body}"); icon,
println!("Actions: {actions:?}"); 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:"); println!("Hints:");
for (key, value) in hints { for (key, value) in notif.hints {
println!(" {key}: {value:?}"); println!(" {key}: {value:?}");
} }
println!("Expiration Timeout: {expir_timeout} seconds\n"); println!("Expiration Timeout: {0} seconds\n", notif.expir_timeout);
Ok(())
} }
#[tokio::main] #[tokio::main]
@ -51,14 +63,16 @@ async fn main() -> Result<()> {
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");
} }
"Notify" => { "Notify" => {
println!("Member is: {member}"); // Handle new received notif
// Handle notif println!("New notification:");
let msg_body = msg.body(); let msg_body = msg.body();
// get the app_name, summary, body, etc. from the msg_body and print it // get the app_name, summary, body, etc. from the msg_body
let _ = print_notif(msg_body); let notif = to_notif(msg_body);
// Done. Respond with a notification ID // Print the notif
let notification_id: u32 = 1; // This could be incremented or generated let _ = print_notif(notif.expect("Failed to parse notification"));
connection.reply(&msg, &notification_id).await?; // Done. Respond to the client with a notification ID
let notification_id: u32 = 1; // This could be incremented or generated. Do it l8r
connection.reply(&msg, &notification_id).await?; // The client will stop when it gets the response
} }
_ => { _ => {
println!("Unhandled method: {member}"); println!("Unhandled method: {member}");