SNot/src/main.rs

106 lines
2.8 KiB
Rust
Raw Normal View History

2024-08-26 18:51:37 +00:00
use std::collections::HashMap;
2024-08-27 11:28:05 +00:00
use zbus::Connection;
use zbus::blocking::MessageIterator;
2024-08-25 21:10:15 +00:00
use zbus::fdo::Result;
2024-08-26 18:51:37 +00:00
use zbus::zvariant::Value;
2024-08-25 21:10:15 +00:00
use zbus::MatchRule;
2024-08-27 11:28:05 +00:00
use zbus_names::InterfaceName;
use zbus_names::WellKnownName;
use std::fmt;
//use tokio;
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>());
}
// Actions field of notication
struct NotifAction<'a> {
action_id: &'a str,
action: &'a str,
}
impl fmt::Display for NotifAction<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{}: {}", &self.action_id, &self.action)?;
Ok(())
}
}
// Structure of a notification
struct Notif<'a> {
app_name: &'a str,
replace_id: u32,
ico: &'a str,
summary: &'a str,
body: &'a str,
actions: &'a [&'a str],
hints: HashMap<&'a str, &'a zbus::zvariant::Value<'a >>,
expir_timeout: i32,
}
// Function to print the contents of the notification
impl fmt::Display for Notif<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "AppName: {}", &self.app_name)?;
writeln!(f, "ReplaceId: {}", &self.replace_id)?;
write!(
f,
"Icon: {}\nSummary: {}\nBody: {}\nActions:\n",
&self.ico, &self.summary, &self.body
)?;
let actions_len = self.actions.len();
2024-08-28 11:27:57 +00:00
let mut i = 0;
2024-08-27 11:28:05 +00:00
while i < actions_len {
2024-08-28 11:27:57 +00:00
let a = NotifAction {
action_id: &self.actions[i],
action: &self.actions[i+1],
};
2024-08-27 11:28:05 +00:00
println!("{a}");
2024-08-28 11:27:57 +00:00
i += 2;
2024-08-27 11:28:05 +00:00
};
writeln!(f, "Hints:")?;
for (key, value) in &self.hints {
writeln!(f, "\t{key}: {value}")?;
}
match self.expir_timeout {
-1 => writeln!(f, "None")?,
2024-08-28 11:27:57 +00:00
_ => writeln!(f, "Expiration Timeout: {}", self.expir_timeout)?,
//None => writeln!(f, "Error getting timeout")?,
2024-08-27 11:28:05 +00:00
}
Ok(()) // Return Ok to indicate success
}
}
#[tokio::main]
async fn main() -> Result<()> {
let interface = InterfaceName::try_from("org.freedesktop.Notifications").unwrap();
let busname = WellKnownName::try_from("org.freedesktop.Notifications").unwrap();
2024-08-25 21:10:15 +00:00
// Establish a connection to the session bus
2024-08-27 11:28:05 +00:00
let connection = Connection::session().await?;
2024-08-25 21:10:15 +00:00
2024-08-20 04:56:36 +00:00
2024-08-26 18:51:37 +00:00
// Loop to handle incoming messages
2024-08-27 11:28:05 +00:00
loop {
let msg = connection.receive_message()?;
let msg_header = msg.header()?;
dbg!(&msg);
match msg_header.message_type()? {
zbus::message::Type::Signal => {
// real code would check msg_header path(), interface() and member()
// handle invalid calls, introspection, errors etc
let arg: &str = msg.body()?;
println!("Msg.body: {arg}");
2024-08-25 21:10:15 +00:00
}
2024-08-27 11:28:05 +00:00
_ => continue,
2024-08-26 18:51:37 +00:00
}
}
2024-08-26 11:16:10 +00:00
Ok(())
}