plain text method

This commit is contained in:
Candifloss 2024-09-18 16:53:39 +05:30
parent d33ee29eb3
commit f21b3ea172
3 changed files with 58 additions and 36 deletions

View File

@ -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(&notif);
//print_notif(&notif);
println!("{}", &notif.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, &notification_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
}
}
}

View File

@ -48,9 +48,9 @@ impl Notification {
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
// Localised human-readable string that describes the action
let action_label = &acts[i + 1];
// Pair of (id: label)
// 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<Notification> {
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 &notif.hints {
println!("\t{key}: {value:?}");
}
println!("Urgency:{}", notif.urgency());
println!("Expiration Timeout: {0} seconds", notif.expir_timeout);
}

View File

@ -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`.
//!