Some kind of mess I don't remember what I did

This commit is contained in:
Candifloss 2024-08-26 02:07:59 +05:30
parent 102ce0018c
commit bb3cfe4683
3 changed files with 156 additions and 62 deletions

View File

@ -5,5 +5,5 @@ edition = "2021"
authors = ["candifloss <candifloss.cc>"] authors = ["candifloss <candifloss.cc>"]
[dependencies] [dependencies]
dbus = "0.9.77" dbus = "0.9.7"
rson_rs = "0.2.1" # rson_rs = "0.2.1"

View File

@ -1,8 +1,12 @@
use dbus::arg::{RefArg, Variant};
use dbus::blocking::Connection;
use dbus::message::MatchRule;
use dbus::strings::Interface;
use dbus::MessageType::Signal;
use dbus::{Message, Path};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use dbus::blocking::{Connection, Message, MessageType, Sender}; use std::time::Duration;
use dbus::message::{MatchRule, MessageType::MethodCall};
use dbus::prelude::*;
// Structure of a notification // Structure of a notification
struct Notif { struct Notif {
@ -16,7 +20,6 @@ struct Notif {
expir_timeout: Option<u32>, expir_timeout: Option<u32>,
} }
// Function to print the contents of the notification // Function to print the contents of the notification
impl fmt::Display for Notif { impl fmt::Display for Notif {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -54,78 +57,87 @@ fn truncate_summary(notif: &mut Notif) {
} }
// Callback function // Callback function
fn handle_notification_added(message: Message) { fn handle_notif(signal: &dbus::Message) -> Result<(), dbus::Error> {
if message.get_type() == MessageType::Signal { // Extract all items as variants
let signal_name = message.get_signature().unwrap(); let items = signal.get_items();
if signal_name == "notification_added" { // Helper functions to safely extract data
let (id, app_name, icon, summary, body, actions, hints, timeout) = let get_string = |item: &dyn RefArg| item.as_str().unwrap_or_default().to_string();
message.get_arguments::<(u32, String, String, String, String, Vec<String>, HashMap<String, dbus::Value>, i32)>().unwrap(); 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;
let notif = Notif { // 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, app_name,
replace_id: Some(id), // Assuming you want to use the ID for replacement replace_id: Some(replace_id),
ico: icon, ico,
summary, summary,
body, body,
actions: actions.iter().map(|(key, value)| (key.clone(), value.clone())).collect(), actions: notif_actions,
hints: hints.iter().map(|(key, value)| (key.clone(), value.to_string())).collect(), hints: notif_hints,
expir_timeout: if timeout < 0 { None } else { Some(timeout as u32) }, expir_timeout: if expir_timeout >= 0 {
Some(expir_timeout as u32)
} else {
None
},
}; };
// Print or process the notification object truncate_summary(&mut notif);
println!("{notification}");
}
}
}
println!("{}", notif);
Ok(())
}
fn main() -> Result<(), dbus::Error> { fn main() -> Result<(), dbus::Error> {
/*
// Example notif
let mut not = Notif {
app_name: "snot".to_string(),
replace_id: Some(0),
ico: "alert.png".to_string(),
summary: "This is a very long suuummmaaarrryyy! Don't you believe me????".to_string(),
body: "short body(hehe)".to_string(),
actions: vec![
("reply".to_string(), "Reply".to_string()),
("close".to_string(), "Close".to_string()),
],
hints: HashMap::new(), // Create an empty HashMap for hints, add the hints later
expir_timeout: Some(0),
};
not.hints
.insert("urgency".to_string(), "critical".to_string());
not.hints
.insert("category".to_string(), "network.error".to_string());
// End of eg. notif
truncate_summary(&mut not); //Limit the summary length
*/
// Connect to the system bus // Connect to the system bus
let conn = Connection::new_session()?; let conn = Connection::new_session()?;
// Get the object path of the notification service // Get the object path of the notification service
let object_path = "/org/freedesktop/Notifications"; //let object_path = "/org/freedesktop/Notifications";
//let member = "Notify";
// Get the interface name of the notification service // Get the interface name of the notification service
let interface_name = "org.freedesktop.Notifications"; //let interface_name = "org.freedesktop.Notifications".to_string();
// Subscribe to the "handle_notification_added" signal // Create a match rule to listen for the notifications
let match_rule = MatchRule::new_for_signal(object_path, interface_name, "handle_notification_added"); let match_rule = MatchRule::new_signal("org.freedesktop.Notifications".into(), "Notify".into());
conn.add_match(match_rule)?;
// Create a sender to send messages // Add the match rule to the connection
let sender = conn.sender(object_path, interface_name)?; conn.add_match(match_rule, |msg, _conn, _| {
if let Err(e) = handle_notif(&msg) {
// Add the callback to the connection eprintln!("Error handling notification: {:?}", e);
conn.add_signal_handler(handle_notification_added)?; }
true // Returning true keeps the callback active
})?;
// Wait for signals // Wait for signals
conn.process_incoming(None)?; loop {
conn.process(Duration::from_millis(1000))?;
}
//println!("{not}");
Ok(()) Ok(())
} }

82
src/unused.txt Normal file
View File

@ -0,0 +1,82 @@
fn main() -> Result<(), dbus::Error> {
/*
// Example notif
let mut not = Notif {
app_name: "snot".to_string(),
replace_id: Some(0),
ico: "alert.png".to_string(),
summary: "This is a very long suuummmaaarrryyy! Don't you believe me????".to_string(),
body: "short body(hehe)".to_string(),
actions: vec![
("reply".to_string(), "Reply".to_string()),
("close".to_string(), "Close".to_string()),
],
hints: HashMap::new(), // Create an empty HashMap for hints, add the hints later
expir_timeout: Some(0),
};
not.hints
.insert("urgency".to_string(), "critical".to_string());
not.hints
.insert("category".to_string(), "network.error".to_string());
// End of eg. notif
truncate_summary(&mut not); //Limit the summary length
*/
}
//println!("{not}");
fn handle_notification_added(message: Message) {
if message.get_type() == MessageType::Signal {
let signal_name = message.get_signature().unwrap();
if signal_name == "notification_added" {
let (id, app_name, icon, summary, body, actions, hints, timeout) = message
.get_arguments::<(
u32,
String,
String,
String,
String,
Vec<String>,
HashMap<String, String>,
i32,
)>()
.unwrap();
let notif = Notif {
app_name,
replace_id: Some(id), // Assuming you want to use the ID for replacement
ico: icon,
summary,
body,
actions: actions
.iter()
.map(|(key, value)| (key.clone(), value.clone()))
.collect(),
hints: hints
.iter()
.map(|(key, value)| (key.clone(), value.to_string()))
.collect(),
expir_timeout: if timeout < 0 {
None
} else {
Some(timeout as u32)
},
};
// Print or process the notification object
println!("{notif}");
}
}
}
/* Subscribe to the "notification added" signal
let mut match_rule: MatchRule = MatchRule::new();
match_rule.msg_type = Some(Signal);
match_rule.path = Some(Path::new(object_path).expect("Failed to create path"));
match_rule.path_is_namespace = false;
match_rule.interface = Some(Interface::from(interface_name));
match_rule.eavesdrop = true;
conn.add_match( match_rule, handle_notif)?; */