better ways to convert actions & hints to string, & handle GetAll
This commit is contained in:
parent
7caf294f38
commit
df42d63461
@ -4,39 +4,44 @@ impl Notification {
|
||||
// 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();
|
||||
let actions: String = if self.actions().is_empty() {
|
||||
"None".to_string()
|
||||
} else {
|
||||
for actn in &self.actions() {
|
||||
actions = actions + "\n\t" + actn.0.as_str() + ": " + actn.1.as_str();
|
||||
}
|
||||
}
|
||||
self.actions()
|
||||
.iter()
|
||||
.fold(String::new(), |mut acc, (id, label)| {
|
||||
// the formatted string is appended to the accumulator string, acc
|
||||
acc.push_str(&format!("\n\t{id}: {label}"));
|
||||
acc
|
||||
})
|
||||
};
|
||||
// Default action
|
||||
let default_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();
|
||||
let hints = if self.hints().is_empty() {
|
||||
"None".to_string()
|
||||
} else {
|
||||
for (key, value) in self.hints() {
|
||||
hints = hints + "\n\t" + key + ": " + &value.to_string();
|
||||
}
|
||||
}
|
||||
self.hints()
|
||||
.iter()
|
||||
.fold(String::new(), |mut acc, (key, value)| {
|
||||
acc.push_str(&format!("\n\t{}: {}", key, **value));
|
||||
acc
|
||||
})
|
||||
};
|
||||
|
||||
// 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.replace_id(),
|
||||
&self.icon(),
|
||||
&self.summary(),
|
||||
&self.body(),
|
||||
actions,
|
||||
hints,
|
||||
&self.expir_timeout().to_string(),
|
||||
&self.expir_timeout(),
|
||||
&self.urgency(),
|
||||
default_action,
|
||||
);
|
||||
|
21
src/main.rs
21
src/main.rs
@ -42,18 +42,23 @@ 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();
|
||||
// Reply with the properties
|
||||
connection.reply(&msg, &properties).await?;
|
||||
println!("Request received: GetAll for interface {interface_name}");
|
||||
println!("GetAll request received for interface: {interface_name}");
|
||||
} else {
|
||||
println!("Unknown interface requested: {interface_name}");
|
||||
// Reply with an error or empty hashmap
|
||||
let empty_props: HashMap<String, String> = HashMap::new();
|
||||
connection.reply(&msg, &empty_props).await?;
|
||||
// Reply with an error
|
||||
connection
|
||||
.reply_error(
|
||||
&msg,
|
||||
"org.freedesktop.DBus.Error.UnknownInterface",
|
||||
&"Unknown interface".to_string(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
"GetServerInformation" => {
|
||||
@ -75,15 +80,15 @@ async fn main() -> Result<()> {
|
||||
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();
|
||||
// Convert the msg body to a Notification object
|
||||
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)?;
|
||||
// Handle the notif
|
||||
println!("New notification!\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
|
||||
let notification_id: u32 = msg.body().deserialize()?; // This method has only one parameter, the id
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user