2024-09-01 08:20:17 +00:00
use futures_util ::stream ::TryStreamExt ;
2024-09-15 18:10:19 +00:00
use std ::collections ::HashMap ;
use zbus ::{ message ::Body , Connection , Result } ;
use zvariant ::OwnedValue ;
2024-09-16 11:19:08 +00:00
struct Notification {
app_name : String ,
replace_id : u32 ,
icon : String ,
summary : String ,
body : String ,
actions : Vec < String > ,
hints : HashMap < String , OwnedValue > ,
expir_timeout : i32 ,
}
fn to_notif ( msg_body : Body ) -> Result < Notification > {
let ( app_name , replace_id , icon , summary , body , actions , hints , expir_timeout ) =
msg_body . deserialize ( ) ? ;
Ok ( Notification {
app_name ,
replace_id ,
icon ,
summary ,
body ,
actions ,
hints ,
expir_timeout ,
} )
}
fn print_notif ( notif : Notification ) {
println! ( " App Name: {0} " , notif . app_name ) ;
println! ( " Replace ID: {0} " , notif . replace_id ) ;
println! ( " Icon: {0} " , notif . icon ) ;
println! ( " Summary: {0} " , notif . summary ) ;
println! ( " Body: {0} " , notif . body ) ;
println! ( " Actions: {0:?} " , notif . actions ) ;
2024-09-15 18:10:19 +00:00
println! ( " Hints: " ) ;
2024-09-16 11:19:08 +00:00
for ( key , value ) in notif . hints {
2024-09-15 18:10:19 +00:00
println! ( " {key} : {value:?} " ) ;
}
2024-09-16 11:19:08 +00:00
println! ( " Expiration Timeout: {0} seconds \n " , notif . expir_timeout ) ;
2024-09-15 18:10:19 +00:00
}
2024-08-27 11:28:05 +00:00
#[ tokio::main ]
async fn main ( ) -> Result < ( ) > {
let connection = Connection ::session ( ) . await ? ;
2024-09-01 08:20:17 +00:00
connection
2024-09-14 10:33:40 +00:00
. request_name ( " org.freedesktop.Notifications " ) // Requesting dbus for this service name. Any other services using this name should be stopped/disabled before this
2024-09-01 08:20:17 +00:00
. await ? ;
2024-08-25 21:10:15 +00:00
2024-09-15 15:43:09 +00:00
let mut stream = zbus ::MessageStream ::from ( & connection ) ; // Convert connection to a MessageStream, yields Message items
2024-09-01 11:31:19 +00:00
while let Some ( msg ) = stream . try_next ( ) . await ? {
2024-09-15 15:43:09 +00:00
// Check if the message is a method call to the "Notify" method
if let Some ( member ) = msg . header ( ) . member ( ) . map ( | m | m . as_str ( ) ) {
2024-09-15 18:10:19 +00:00
match member {
" GetServerInformation " = > {
// Respond with server information
2024-09-15 19:28:06 +00:00
let response = ( " SNot " , " candifloss.cc " , " 0.1.0 " , " 0.1.0 " ) ; // (name, vendor, version, spec_version)
2024-09-15 18:10:19 +00:00
connection . reply ( & msg , & response ) . await ? ;
2024-09-15 19:28:06 +00:00
println! ( " Request received: {member} \n \t Name: SNot \n \t Vendor: candifloss.cc \n \t Version: 0.1.0 \n \t Spec_version: 0.1.0 " ) ;
2024-09-15 18:10:19 +00:00
}
" Notify " = > {
2024-09-16 11:19:08 +00:00
// Handle new received notif
println! ( " New notification: " ) ;
2024-09-15 18:10:19 +00:00
let msg_body = msg . body ( ) ;
2024-09-16 11:19:08 +00:00
// get the app_name, summary, body, etc. from the msg_body
let notif = to_notif ( msg_body ) ;
// Print the notif
let _ = print_notif ( notif . expect ( " Failed to parse notification " ) ) ;
// Done. Respond to the client with a notification ID
let notification_id : u32 = 1 ; // This could be incremented or generated. Do it l8r
connection . reply ( & msg , & notification_id ) . await ? ; // The client will stop when it gets the response
2024-09-15 18:10:19 +00:00
}
_ = > {
println! ( " Unhandled method: {member} " ) ;
}
} ;
2024-09-14 10:33:40 +00:00
}
2024-09-01 11:31:19 +00:00
}
Ok ( ( ) )
}