diff --git a/Cargo.toml b/Cargo.toml index a085537..0b2c6a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ zbus = "4.4.0" zvariant = "4.2.0" tokio = { version = "1.40.0", features = ["full"] } futures-util = "0.3.30" +serde_json = "1.0.128" # rson_rs = "0.2.1" \ No newline at end of file diff --git a/src/formats/json.rs b/src/formats/json.rs new file mode 100644 index 0000000..0c45e06 --- /dev/null +++ b/src/formats/json.rs @@ -0,0 +1,13 @@ +use crate::notification::Notification; +use serde_json::json; + + +impl Notification { + // Json format, useful for data exchange + pub fn json(&self) { + let actions = json!( + {} + ); + } + +} \ No newline at end of file diff --git a/src/formats.rs b/src/formats/plain.rs similarity index 82% rename from src/formats.rs rename to src/formats/plain.rs index 7ce9239..703719b 100644 --- a/src/formats.rs +++ b/src/formats/plain.rs @@ -10,7 +10,7 @@ impl Notification { self.actions() .iter() .fold(String::new(), |mut acc, (id, label)| { - // the formatted string is appended to the accumulator string, acc + // The formatted string is appended to the accumulator string, acc acc.push_str(&format!("\n\t{id}: {label}")); acc }) @@ -21,7 +21,7 @@ impl Notification { Some(actn) => actn.0, }; // Hints - let hints = if self.hints().is_empty() { + let hints: String = if self.hints().is_empty() { "None".to_string() } else { self.hints() @@ -33,7 +33,7 @@ impl Notification { }; // 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: {}", + let plain_string: String = format!("App Name: {}\nReplace ID: {}\nIcon: {}\nSummary: {}\nBody: {}\nActions: {}\nHints: {}\nExpiration Timeout: {}\nUrgency: {}\nDefault Action: {}", &self.app_name(), &self.replace_id(), &self.icon(), diff --git a/src/main.rs b/src/main.rs index eb86e0c..97a075c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,19 @@ -mod formats; +pub mod formats { + pub mod plain; + pub mod json; +} mod notification; -use notification::to_notif; +use notification::{to_notif, Notification}; use std::collections::HashMap; use futures_util::stream::TryStreamExt; -use zbus::{Connection, Result}; +use zbus::{message::Body, Connection, Result}; 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 +const NOTIF_INTERFACE: &str = "org.freedesktop.Notifications"; // DBus interface name // Properties of this server fn server_properties() -> HashMap { @@ -42,7 +45,8 @@ async fn main() -> Result<()> { "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 + + // Check if the requested interface is the right one if interface_name == NOTIF_INTERFACE { // Properties for the interface let properties = server_properties(); @@ -71,7 +75,7 @@ async fn main() -> Result<()> { "GetCapabilities" => { // Client requested server capabilities. Respond with the supported capabilities let capabilities = vec!["actions", "body", "body-hyperlinks"]; // Add more LATER - connection.reply(&msg, &capabilities).await?; + connection.reply(&msg, &capabili ties).await?; println!("Request received: {member}\n\tCapabilities: {capabilities:?}"); // Remove this LATER } @@ -79,12 +83,14 @@ async fn main() -> Result<()> { // New notification received. Now, respond to the client with a notification ID let notification_id: u32 = 1; // This could be incremented or generated. DO IT LATER connection.reply(&msg, ¬ification_id).await?; // The client waits for this response in order to disconnect - // Get the body of the message - let msg_body = msg.body(); // The body has 8 parameters, ie., 8 parts of a notification - // Convert the msg body to a Notification object - let notif = to_notif(&msg_body)?; + + // Get the body of the message + let msg_body: Body = msg.body(); // The body has 8 parameters, ie., 8 fields of a Notification object + + // Convert the msg body to a Notification object + let notif: Notification = to_notif(&msg_body)?; // Handle the notif - println!("New notification!\n{}", ¬if.plain()); // Print the plain version + println!("New notification!\n{}\n", ¬if.plain()); // Print the plain version } "CloseNotification" => { // Client sent a close signal. Extract notification ID of the notif to be closed from the message body diff --git a/src/notification.rs b/src/notification.rs index 778c8e6..a8d541c 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -16,7 +16,7 @@ pub struct Notification { body: String, // Action requests that can be sent back to the client - "Reply," "Mark as Read," "Play/Pause/Next," "Snooze/Dismiss," etc. actions: Vec, - // Extra useful data - notif type, urgency, sound file, icon data, etc. + // Useful extra data - notif type, urgency, notif sound, icon data, etc. hints: HashMap, // Seconds till this notif expires. Optional expir_timeout: i32, @@ -105,7 +105,7 @@ pub fn to_notif(msg_body: &Body) -> Result { // Deserialize body into a tuple let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) = msg_body.deserialize()?; - // Form a Notification object from the obtained tuple + // Form a Notification object from the obtained tuple's fields Ok(Notification { app_name, replace_id,