From 7caf294f38855953005b54fab5621193fe00ae7c Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 19 Sep 2024 01:14:46 +0530 Subject: [PATCH] Handle GetAll method --- src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 97dd609..92c2e64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod formats; mod notification; use notification::to_notif; +use std::collections::HashMap; use futures_util::stream::TryStreamExt; use zbus::{Connection, Result}; @@ -9,12 +10,25 @@ const SERVER_NAME: &str = "SNot"; // Server software name const VENDOR: &str = "candifloss.cc"; // Server software vendor const VERSION: &str = "0.1.0"; // Server software version const SPEC_VERSION: &str = "0.42"; // DBus specification version +const NOTIF_INTERFACE: &str = "org.freedesktop.Notifications"; // DBus interface + +// Properties of this server +fn server_properties() -> HashMap { + let mut properties: HashMap = HashMap::new(); + + properties.insert("ServerName".to_string(), String::from(SERVER_NAME)); + properties.insert("Vendor".to_string(), String::from(VENDOR)); + properties.insert("Version".to_string(), String::from(VERSION)); + properties.insert("SpecVersion".to_string(), String::from(SPEC_VERSION)); + + properties +} #[tokio::main] async fn main() -> Result<()> { let connection = Connection::session().await?; connection - .request_name("org.freedesktop.Notifications") // Requesting dbus for this service name. Any other services/procs using this name should be stopped/disabled before this + .request_name(NOTIF_INTERFACE) // Requesting dbus for this service name. Any other services/procs using this name should be stopped/disabled before this .await?; let mut stream = zbus::MessageStream::from(&connection); // Convert connection to a MessageStream, yields Message items @@ -25,6 +39,23 @@ async fn main() -> Result<()> { if let Some(member) = msg.header().member() { let member_name = member.as_str(); // Convert to &str for the match match member_name { + "GetAll" => { + // Client requested all properties of the server. Extract the interface name from the message body + let interface_name: String = msg.body().deserialize()?; // This method has only one parameter, the interface name + // Check if the requested interface is the right one + if interface_name == NOTIF_INTERFACE { + // Properties for the interface + let properties = server_properties(); + // Reply with the properties + connection.reply(&msg, &properties).await?; + println!("Request received: GetAll for interface {interface_name}"); + } else { + println!("Unknown interface requested: {interface_name}"); + // Reply with an error or empty hashmap + let empty_props: HashMap = HashMap::new(); + connection.reply(&msg, &empty_props).await?; + } + } "GetServerInformation" => { // Client requested server information. Respond with: (Server_name, author, software_version, dbus_spec_version) let response = (SERVER_NAME, VENDOR, VERSION, SPEC_VERSION); @@ -52,7 +83,7 @@ async fn main() -> Result<()> { } "CloseNotification" => { // Client sent a close signal. Extract notification ID of the notif to be closed from the message body - let notification_id: u32 = msg.body().deserialize()?; + let notification_id: u32 = msg.body().deserialize()?; // This method has only one parameter, the id // Tracking notifications by their IDs, closing them, and other features may be implemented later // close_notification(notification_id);