2024-09-16 20:51:27 +00:00
mod notification ;
use notification ::{ print_notif , to_notif } ;
2024-09-16 11:19:08 +00:00
2024-09-16 20:51:27 +00:00
use futures_util ::stream ::TryStreamExt ;
use zbus ::{ Connection , Result } ;
2024-08-27 11:28:05 +00:00
2024-09-16 21:28:58 +00:00
const SERVER_NAME : & str = " SNot " ;
const VENDOR : & str = " candifloss.cc " ;
const VERSION : & str = " 0.1.0 " ;
2024-09-17 06:47:05 +00:00
const SPEC_VERSION : & str = " 1.0 " ;
2024-09-16 21:28:58 +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-16 20:51:27 +00:00
. request_name ( " org.freedesktop.Notifications " ) // Requesting dbus for this service name. Any other services/progs 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
2024-09-16 20:51:27 +00:00
if let Some ( member ) = msg . header ( ) . member ( ) {
let member_name = member . as_str ( ) ;
match member_name {
2024-09-15 18:10:19 +00:00
" GetServerInformation " = > {
// Respond with server information
2024-09-16 21:28:58 +00:00
let response = ( SERVER_NAME , VENDOR , VERSION , SPEC_VERSION ) ; // (name, vendor, version, spec_version)
2024-09-15 18:10:19 +00:00
connection . reply ( & msg , & response ) . await ? ;
2024-09-17 06:47:05 +00:00
println! ( " Request received: {member} \n \t Name: {SERVER_NAME} , \t Vendor: {VENDOR} , \t Version: {VERSION} , \t Spec_version: {SPEC_VERSION} " ) ;
2024-09-15 18:10:19 +00:00
}
2024-09-16 20:51:27 +00:00
" GetCapabilities " = > {
// Respond with supported capabilities
let capabilities = vec! [ " actions " , " body " , " body-hyperlinks " ] ;
connection . reply ( & msg , & capabilities ) . await ? ;
println! ( " Request received: {member} \n \t Capabilities: {capabilities:?} " ) ;
}
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
2024-09-16 20:51:27 +00:00
let notif = to_notif ( & msg_body ) ? ;
2024-09-16 11:19:08 +00:00
// Print the notif
2024-09-16 20:51:27 +00:00
print_notif ( & notif ) ;
2024-09-16 21:28:58 +00:00
println! ( " {} \n " , notif . urgency ( ) ) ;
2024-09-16 11:19:08 +00:00
// Done. Respond to the client with a notification ID
let notification_id : u32 = 1 ; // This could be incremented or generated. Do it l8r
2024-09-16 20:51:27 +00:00
connection . reply ( & msg , & notification_id ) . await ? ; // The client will disconnect when it gets this response
2024-09-15 18:10:19 +00:00
}
_ = > {
println! ( " Unhandled method: {member} " ) ;
}
2024-09-16 20:51:27 +00:00
}
2024-09-14 10:33:40 +00:00
}
2024-09-01 11:31:19 +00:00
}
Ok ( ( ) )
}