From 102ce0018c31ca2c04ecbfa713d759fe91d36777 Mon Sep 17 00:00:00 2001 From: candifloss Date: Fri, 23 Aug 2024 07:48:27 +0530 Subject: [PATCH] changed library --- Cargo.toml | 2 +- src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b96958..2d07319 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,5 @@ edition = "2021" authors = ["candifloss "] [dependencies] -zbus_notification = "0.3.5" +dbus = "0.9.77" rson_rs = "0.2.1" diff --git a/src/main.rs b/src/main.rs index a4118fc..468fe88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, } + // 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, HashMap, 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"; + + // 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}"); + //println!("{not}"); + Ok(()) }