changed library

This commit is contained in:
Candifloss 2024-08-23 07:48:27 +05:30
parent 21bb5f2ebb
commit 102ce0018c
2 changed files with 57 additions and 4 deletions

View File

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

View File

@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::fmt;
use zbus_notification::NotifyUnit;
use dbus::blocking::{Connection, Message, MessageType, Sender};
use dbus::message::{MatchRule, MessageType::MethodCall};
use dbus::prelude::*;
// Structure of a notification
struct Notif {
@ -14,6 +16,7 @@ struct Notif {
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 {
@ -50,7 +53,35 @@ fn truncate_summary(notif: &mut Notif) {
}
}
fn main() {
// Callback function
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, dbus::Value>, 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!("{notification}");
}
}
}
fn main() -> Result<(), dbus::Error> {
/*
// Example notif
let mut not = Notif {
app_name: "snot".to_string(),
@ -71,8 +102,30 @@ fn main() {
.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
let conn = Connection::new_session()?;
// Get the object path of the notification service
let object_path = "/org/freedesktop/Notifications";
println!("{not}");
// Get the interface name of the notification service
let interface_name = "org.freedesktop.Notifications";
// Subscribe to the "handle_notification_added" signal
let match_rule = MatchRule::new_for_signal(object_path, interface_name, "handle_notification_added");
conn.add_match(match_rule)?;
// Create a sender to send messages
let sender = conn.sender(object_path, interface_name)?;
// Add the callback to the connection
conn.add_signal_handler(handle_notification_added)?;
// Wait for signals
conn.process_incoming(None)?;
//println!("{not}");
Ok(())
}