From f21b3ea172e115c9df1063564b1de2845fac2159 Mon Sep 17 00:00:00 2001 From: candifloss Date: Wed, 18 Sep 2024 16:53:39 +0530 Subject: [PATCH] plain text method --- src/main.rs | 16 ++++++---- src/notification.rs | 76 +++++++++++++++++++++++++++------------------ src/xmlgen_notif.rs | 2 ++ 3 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index c7ffa05..f7e967e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod notification; -use notification::{print_notif, to_notif}; +use notification::to_notif; use futures_util::stream::TryStreamExt; use zbus::{Connection, Result}; @@ -25,30 +25,34 @@ async fn main() -> Result<()> { match member_name { "GetServerInformation" => { // Respond with server information - let response = (SERVER_NAME, VENDOR, VERSION, SPEC_VERSION); // (name, vendor, version, spec_version) + // (name, vendor, version, dbus_spec_version) + let response = (SERVER_NAME, VENDOR, VERSION, SPEC_VERSION); connection.reply(&msg, &response).await?; - println!("Request received: {member}\n\tName: {SERVER_NAME},\tVendor: {VENDOR},\tVersion: {VERSION},\tSpec_version: {SPEC_VERSION}"); + println!("Request received: {member}\n\tName: {SERVER_NAME}, Vendor: {VENDOR}, Version: {VERSION}, DBus spec version: {SPEC_VERSION}"); + // Remove this later } "GetCapabilities" => { // Respond with supported capabilities let capabilities = vec!["actions", "body", "body-hyperlinks"]; connection.reply(&msg, &capabilities).await?; println!("Request received: {member}\n\tCapabilities: {capabilities:?}"); + // Remove this later } "Notify" => { // Handle new received notif - println!("New notification:"); + println!("New notification!"); let msg_body = msg.body(); // get the app_name, summary, body, etc. from the msg_body let notif = to_notif(&msg_body)?; // Print the notif - print_notif(¬if); + //print_notif(¬if); + println!("{}", ¬if.plain()); // 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, ¬ification_id).await?; // The client will disconnect when it gets this response } _ => { - println!("Unhandled method: {member}"); + println!("Unhandled method: {member}"); // Other methods are irrelevant or not handled at this stage of development } } } diff --git a/src/notification.rs b/src/notification.rs index 1b7a76c..f0cb57b 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -47,10 +47,10 @@ impl Notification { let mut i = 0; while i < act_len { // Action ID, used by the sender to id the clicked action - let action_id = &acts[i]; - // Localised human-readable string that describes the label - let action_label = &acts[i + 1]; - // Pair of (id: label) + let action_id = &acts[i]; + // Localised human-readable string that describes the action + let action_label = &acts[i + 1]; + // Pair of (id, label) let action_pair = (action_id.to_owned(), action_label.to_owned()); // Add it to the Vec actions.push(action_pair); @@ -67,6 +67,48 @@ impl Notification { Some(self.actions()[0].clone()) } } + + // 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 + } } // Convert the DBus message body into Notification @@ -86,29 +128,3 @@ pub fn to_notif(msg_body: &Body) -> Result { 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!("Actions:"); - if notif.actions().is_empty() { - println!("\tNone"); - } else { - for actn in notif.actions() { - println!("\t{}: {},", actn.0, actn.1); - } - } - if let Some(action) = notif.default_action() { - println!("Default action:\t{}:{}", action.0, action.1); - } - println!("Hints:"); - for (key, value) in ¬if.hints { - println!("\t{key}: {value:?}"); - } - println!("Urgency:{}", notif.urgency()); - println!("Expiration Timeout: {0} seconds", notif.expir_timeout); -} diff --git a/src/xmlgen_notif.rs b/src/xmlgen_notif.rs index 2607701..f8ca2d8 100644 --- a/src/xmlgen_notif.rs +++ b/src/xmlgen_notif.rs @@ -1,5 +1,7 @@ //! # D-Bus interface proxy for: `org.freedesktop.Notifications` //! +//! This is client-side code, which can used by applications that send notifications, not the server +//! //! This code was generated by `zbus-xmlgen` `4.1.0` from D-Bus introspection data. //! Source: `notif.xml`. //!