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();
|
|
|
|
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();
|
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-26 18:51:37 +00:00
|
|
|
// Create a match rule for the Notify signal
|
2024-08-27 11:28:05 +00:00
|
|
|
/*let match_rule = MatchRule::builder()
|
2024-08-26 18:51:37 +00:00
|
|
|
.msg_type(zbus::message::Type::Signal)
|
2024-08-27 11:28:05 +00:00
|
|
|
.sender("org.freedesktop.Notifications")?
|
2024-08-25 21:10:15 +00:00
|
|
|
.interface("org.freedesktop.Notifications")?
|
2024-08-27 11:28:05 +00:00
|
|
|
.arg_path(0, "/org/freedesktop/Notifications")?
|
2024-08-26 18:51:37 +00:00
|
|
|
.member("Notify")?
|
2024-08-27 11:28:05 +00:00
|
|
|
.build();*/
|
2024-08-23 02:18:27 +00:00
|
|
|
|
2024-08-26 18:51:37 +00:00
|
|
|
// Create a message iterator for the match rule
|
2024-08-27 11:28:05 +00:00
|
|
|
//let mut iterator = MessageIterator::for_match_rule(match_rule, &connection, None)?;
|
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}");
|
|
|
|
//connection.reply(&msg, &(format!("Hello {}!", 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(())
|
2024-08-11 19:12:21 +00:00
|
|
|
}
|