use std::collections::HashMap; use zbus::Connection; use zbus::blocking::MessageIterator; use zbus::fdo::Result; use zbus::zvariant::Value; use zbus::MatchRule; use zbus_names::InterfaceName; use zbus_names::WellKnownName; use std::fmt; //use tokio; fn print_type_of(_: &T) { println!("{}", std::any::type_name::()); } // 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(); let i = 0; while i < actions_len { let a:NotifAction; a.action_id = &self.actions[i]; a.action = &self.actions[i+1]; println!("{a}"); i += 1; }; writeln!(f, "Hints:")?; for (key, value) in &self.hints { writeln!(f, "\t{key}: {value}")?; } match self.expir_timeout { -1 => writeln!(f, "None")?, Some(millisec) => writeln!(f, "Expiration Timeout: {millisec}")?, None => writeln!(f, "Error getting timeout")?, } 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(); // Establish a connection to the session bus let connection = Connection::session().await?; // Create a match rule for the Notify signal /*let match_rule = MatchRule::builder() .msg_type(zbus::message::Type::Signal) .sender("org.freedesktop.Notifications")? .interface("org.freedesktop.Notifications")? .arg_path(0, "/org/freedesktop/Notifications")? .member("Notify")? .build();*/ // Create a message iterator for the match rule //let mut iterator = MessageIterator::for_match_rule(match_rule, &connection, None)?; // Loop to handle incoming messages 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}"); //connection.reply(&msg, &(format!("Hello {}!", arg)))?; } _ => continue, } } Ok(()) }