diff --git a/src/client.rs b/src/client.rs index 8c162b8..6d10eaf 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,9 +1,9 @@ -use std::{ - future::{self, Future}, - time::Duration, pin::Pin, -}; +use std::time::Duration; -use tokio::{select, time::{sleep, Sleep}}; +use tokio::{ + select, + time::{sleep, Sleep}, +}; use zbus::{dbus_proxy, export::ordered_stream::OrderedStreamExt, Connection, Result}; use crate::{notification::Notification, Format}; diff --git a/src/daemon.rs b/src/daemon.rs index c47f130..e510563 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,100 +1,29 @@ -use crate::notification::Hints; -use crate::notification::Notification; -use crate::notification::ServerInformation; -use std::collections::HashMap; -use zbus::dbus_interface; -use zbus::Connection; -use zbus::Result; -use zbus::SignalContext; -use zbus::fdo; +use std::{env, path::Path}; -pub struct NotifyManager { - next_id: u32, - pendings: HashMap, -} +use tokio::{net::UnixListener, io::{Interest, AsyncReadExt}}; + +use crate::manager::NotifyManager; impl NotifyManager { - pub fn new() -> NotifyManager { - NotifyManager { - next_id: 1, - pendings: HashMap::new(), + pub async fn listen(&self) -> Result<(), zbus::Error> { + let socket = Path::new("/tmp/fldsmdfr/socket.sock"); + let listener = UnixListener::bind(socket)?; + loop { + match listener.accept().await { + Ok((stream, _addr)) => { + tokio::spawn(async move { + let ready = stream.ready(Interest::READABLE | Interest::WRITABLE).await?; + + if ready.is_readable() { + let cmd = stream.read_to_string(dst) + } + }); + } + Err(e) => { + eprintln!("Could not accept connection: {}", e); + } + } } } - - pub async fn start(self) -> Result<()> { - let connection = Connection::session().await?; - - connection - .object_server() - .at("/org/freedesktop/Notifications", self) - .await?; - connection - .request_name("org.freedesktop.Notifications") - .await?; - - loop {} - } } -#[dbus_interface(name = "org.freedesktop.Notifications")] -impl NotifyManager { - async fn notify( - &mut self, - #[zbus(signal_context)] ctxt: SignalContext<'_>, - app_name: String, - replaces_id: u32, - app_icon: String, - summary: String, - body: String, - actions: Vec, - hints: Hints, - expire_timeout: i32, - ) -> fdo::Result { - println!("{}: {}", summary, body); - let notif = Notification { - id: self.next_id, - app_name, - app_icon, - summary, - body, - actions, - hints, - }; - Self::new_notification(&ctxt, ¬if).await?; - self.pendings.insert(self.next_id, notif); - if replaces_id == 0 { - self.next_id += 1; - Ok(self.next_id) - } else { - Ok(replaces_id) - } - } - - fn get_capabilities(&self) -> Vec<&str> { - vec![ - "body", - "actions", - "body-images", - "persistence", - "icon-static", - ] - } - - fn get_server_information(&self) -> ServerInformation { - ServerInformation { - name: env!("CARGO_PKG_NAME").to_string(), - vendor: "zoriya".to_string(), - version: env!("CARGO_PKG_VERSION").to_string(), - spec_version: "1.2".to_string(), - } - } - - #[dbus_interface(signal)] - async fn new_notification(ctxt: &SignalContext<'_>, notif: &Notification) -> Result<()>; - - fn list(&self) -> Vec<&Notification> { - let mut ret: Vec<&Notification> = self.pendings.values().collect(); - ret.sort_by(|a, b| a.id.cmp(&b.id)); - ret - } -} diff --git a/src/main.rs b/src/main.rs index 274679b..8303ce2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ mod client; mod daemon; +mod manager; mod notification; use clap::{Parser, Subcommand, ValueEnum}; -use daemon::NotifyManager; +use manager::NotifyManager; use zbus::Result; #[derive(Parser)] @@ -52,7 +53,11 @@ async fn main() -> Result<()> { let cli = Args::parse(); match cli.command { - Command::Daemon => NotifyManager::new().start().await, + Command::Daemon => { + let manager = NotifyManager::new(); + manager.start().await?; + manager.listen().await + }, Command::List { format } => client::list(format).await, Command::Listen { format, clear } => client::listen(format, clear).await, } diff --git a/src/manager.rs b/src/manager.rs new file mode 100644 index 0000000..9acfffb --- /dev/null +++ b/src/manager.rs @@ -0,0 +1,87 @@ +use crate::notification::Hints; +use crate::notification::Notification; +use crate::notification::ServerInformation; +use std::collections::HashMap; +use zbus::dbus_interface; +use zbus::Connection; +use zbus::Result; +use zbus::fdo; + +pub struct NotifyManager { + next_id: u32, + pendings: HashMap, +} + +impl NotifyManager { + pub fn new() -> NotifyManager { + NotifyManager { + next_id: 1, + pendings: HashMap::new(), + } + } + + pub async fn start(self) -> Result<()> { + let connection = Connection::session().await?; + + connection + .object_server() + .at("/org/freedesktop/Notifications", self) + .await?; + connection + .request_name("org.freedesktop.Notifications") + .await?; + Ok(()) + } +} + +#[dbus_interface(name = "org.freedesktop.Notifications")] +impl NotifyManager { + async fn notify( + &mut self, + app_name: String, + replaces_id: u32, + app_icon: String, + summary: String, + body: String, + actions: Vec, + hints: Hints, + _expire_timeout: i32, + ) -> fdo::Result { + println!("{}: {}", summary, body); + let notif = Notification { + id: self.next_id, + app_name, + app_icon, + summary, + body, + actions, + hints, + }; + self.pendings.insert(self.next_id, notif); + if replaces_id == 0 { + self.next_id += 1; + Ok(self.next_id) + } else { + Ok(replaces_id) + } + } + + fn get_capabilities(&self) -> Vec<&str> { + vec![ + "body", + "actions", + "body-images", + "persistence", + "icon-static", + ] + } + + fn get_server_information(&self) -> ServerInformation { + ServerInformation { + name: env!("CARGO_PKG_NAME").to_string(), + vendor: "zoriya".to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), + spec_version: "1.2".to_string(), + } + } +}