Changed library again. Starting over
This commit is contained in:
parent
bb3cfe4683
commit
90ce8d4077
@ -5,5 +5,5 @@ edition = "2021"
|
|||||||
authors = ["candifloss <candifloss.cc>"]
|
authors = ["candifloss <candifloss.cc>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dbus = "0.9.7"
|
zbus = "4.4.0"
|
||||||
# rson_rs = "0.2.1"
|
# rson_rs = "0.2.1"
|
||||||
|
156
src/main.rs
156
src/main.rs
@ -1,143 +1,29 @@
|
|||||||
use dbus::arg::{RefArg, Variant};
|
use zbus::blocking::Connection;
|
||||||
use dbus::blocking::Connection;
|
use zbus::fdo::Result;
|
||||||
use dbus::message::MatchRule;
|
use zbus::Message;
|
||||||
use dbus::strings::Interface;
|
use zbus::MatchRule;
|
||||||
use dbus::MessageType::Signal;
|
|
||||||
use dbus::{Message, Path};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fmt;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
// Structure of a notification
|
fn main() -> Result<()> {
|
||||||
struct Notif {
|
// Establish a connection to the session bus
|
||||||
app_name: String,
|
let connection = Connection::session()?;
|
||||||
replace_id: Option<u32>,
|
|
||||||
ico: String,
|
|
||||||
summary: String,
|
|
||||||
body: String,
|
|
||||||
actions: Vec<(String, String)>,
|
|
||||||
hints: HashMap<String, String>,
|
|
||||||
expir_timeout: Option<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to print the contents of the notification
|
// Define a match rule for the notifications
|
||||||
impl fmt::Display for Notif {
|
let match_rule = MatchRule::builder()
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
.msg_type(zbus::MessageType::Signal)
|
||||||
writeln!(f, "AppName: {}", &self.app_name)?;
|
.interface("org.freedesktop.Notifications")?
|
||||||
match self.replace_id {
|
.path("/org/freedesktop/Notifications")?
|
||||||
Some(notif_id) => writeln!(f, "ReplaceId: {notif_id}")?,
|
.build();
|
||||||
None => writeln!(f, "None")?,
|
|
||||||
}
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"Icon: {}\nSummary: {}\nBody: {}\nActions:\n",
|
|
||||||
&self.ico, &self.summary, &self.body
|
|
||||||
)?;
|
|
||||||
for (key, label) in &self.actions {
|
|
||||||
writeln!(f, "\t{key}: {label}")?;
|
|
||||||
}
|
|
||||||
writeln!(f, "Hints:")?;
|
|
||||||
for (key, value) in &self.hints {
|
|
||||||
writeln!(f, "\t{key}: {value}")?;
|
|
||||||
}
|
|
||||||
match self.expir_timeout {
|
|
||||||
Some(millisec) => writeln!(f, "Expiration Timeout: {millisec}")?,
|
|
||||||
None => writeln!(f, "None")?,
|
|
||||||
}
|
|
||||||
Ok(()) // Return Ok to indicate success
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Summary should be generally <= 40 chars, as per the specification.
|
|
||||||
fn truncate_summary(notif: &mut Notif) {
|
|
||||||
if notif.summary.len() > 40 {
|
|
||||||
notif.summary.truncate(39);
|
|
||||||
notif.summary.push('…');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Callback function
|
|
||||||
fn handle_notif(signal: &dbus::Message) -> Result<(), dbus::Error> {
|
|
||||||
// Extract all items as variants
|
|
||||||
let items = signal.get_items();
|
|
||||||
|
|
||||||
// Helper functions to safely extract data
|
|
||||||
let get_string = |item: &dyn RefArg| item.as_str().unwrap_or_default().to_string();
|
|
||||||
let get_u32 = |item: &dyn RefArg| item.as_u64().unwrap_or_default() as u32;
|
|
||||||
let get_i32 = |item: &dyn RefArg| item.as_i64().unwrap_or_default() as i32;
|
|
||||||
|
|
||||||
// Extract the fields from the message
|
|
||||||
let app_name = get_string(&items[0]);
|
|
||||||
let replace_id = get_u32(&items[1]);
|
|
||||||
let ico = get_string(&items[2]);
|
|
||||||
let summary = get_string(&items[3]);
|
|
||||||
let body = get_string(&items[4]);
|
|
||||||
|
|
||||||
// Extract actions as Vec<String>
|
|
||||||
let actions = items[5].as_array().unwrap_or(&[]).iter()
|
|
||||||
.filter_map(|v| v.as_str().map(|s| s.to_string()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Extract hints as HashMap<String, String>
|
|
||||||
let hints = items[6].as_dict().unwrap_or(&[]).iter()
|
|
||||||
.map(|(k, v)| (get_string(k), get_string(v)))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let expir_timeout = get_i32(&items[7]);
|
|
||||||
|
|
||||||
// Convert actions from Vec<String> to Vec<(String, String)>
|
|
||||||
let notif_actions = actions.chunks(2)
|
|
||||||
.map(|chunk| if chunk.len() == 2 { (chunk[0].clone(), chunk[1].clone()) } else { (chunk[0].clone(), "".to_string()) })
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut notif = Notif {
|
|
||||||
app_name,
|
|
||||||
replace_id: Some(replace_id),
|
|
||||||
ico,
|
|
||||||
summary,
|
|
||||||
body,
|
|
||||||
actions: notif_actions,
|
|
||||||
hints: notif_hints,
|
|
||||||
expir_timeout: if expir_timeout >= 0 {
|
|
||||||
Some(expir_timeout as u32)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
truncate_summary(&mut notif);
|
|
||||||
|
|
||||||
println!("{}", notif);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), dbus::Error> {
|
|
||||||
// Connect to the system bus
|
|
||||||
let conn = Connection::new_session()?;
|
|
||||||
|
|
||||||
// Get the object path of the notification service
|
|
||||||
//let object_path = "/org/freedesktop/Notifications";
|
|
||||||
//let member = "Notify";
|
|
||||||
|
|
||||||
// Get the interface name of the notification service
|
|
||||||
//let interface_name = "org.freedesktop.Notifications".to_string();
|
|
||||||
|
|
||||||
// Create a match rule to listen for the notifications
|
|
||||||
let match_rule = MatchRule::new_signal("org.freedesktop.Notifications".into(), "Notify".into());
|
|
||||||
|
|
||||||
// Add the match rule to the connection
|
// Add the match rule to the connection
|
||||||
conn.add_match(match_rule, |msg, _conn, _| {
|
connection.add_match(match_rule)?;
|
||||||
if let Err(e) = handle_notif(&msg) {
|
|
||||||
eprintln!("Error handling notification: {:?}", e);
|
|
||||||
}
|
|
||||||
true // Returning true keeps the callback active
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// Wait for signals
|
// Process notifications
|
||||||
loop {
|
loop {
|
||||||
conn.process(Duration::from_millis(1000))?;
|
let message = connection.receive_message()?;
|
||||||
|
if let Some(member) = message.member() {
|
||||||
|
if member.as_str() == "Notify" {
|
||||||
|
println!("Received a notification: {:?}", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
@ -80,3 +80,53 @@ fn handle_notification_added(message: Message) {
|
|||||||
match_rule.interface = Some(Interface::from(interface_name));
|
match_rule.interface = Some(Interface::from(interface_name));
|
||||||
match_rule.eavesdrop = true;
|
match_rule.eavesdrop = true;
|
||||||
conn.add_match( match_rule, handle_notif)?; */
|
conn.add_match( match_rule, handle_notif)?; */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Structure of a notification
|
||||||
|
struct Notif {
|
||||||
|
app_name: String,
|
||||||
|
replace_id: Option<u32>,
|
||||||
|
ico: String,
|
||||||
|
summary: String,
|
||||||
|
body: String,
|
||||||
|
actions: Vec<(String, String)>,
|
||||||
|
hints: HashMap<String, String>,
|
||||||
|
expir_timeout: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)?;
|
||||||
|
match self.replace_id {
|
||||||
|
Some(notif_id) => writeln!(f, "ReplaceId: {notif_id}")?,
|
||||||
|
None => writeln!(f, "None")?,
|
||||||
|
}
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"Icon: {}\nSummary: {}\nBody: {}\nActions:\n",
|
||||||
|
&self.ico, &self.summary, &self.body
|
||||||
|
)?;
|
||||||
|
for (key, label) in &self.actions {
|
||||||
|
writeln!(f, "\t{key}: {label}")?;
|
||||||
|
}
|
||||||
|
writeln!(f, "Hints:")?;
|
||||||
|
for (key, value) in &self.hints {
|
||||||
|
writeln!(f, "\t{key}: {value}")?;
|
||||||
|
}
|
||||||
|
match self.expir_timeout {
|
||||||
|
Some(millisec) => writeln!(f, "Expiration Timeout: {millisec}")?,
|
||||||
|
None => writeln!(f, "None")?,
|
||||||
|
}
|
||||||
|
Ok(()) // Return Ok to indicate success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Summary should be generally <= 40 chars, as per the specification.
|
||||||
|
fn truncate_summary(notif: &mut Notif) {
|
||||||
|
if notif.summary.len() > 40 {
|
||||||
|
notif.summary.truncate(39);
|
||||||
|
notif.summary.push('…');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user