use std::{path::PathBuf, sync::Arc, time::Duration};
use tracing::instrument;
use zbus::fdo;
use crate::{service::WallpaperService, types::CyclingMode};
#[derive(Debug)]
pub(crate) struct WallpaperDaemon {
pub service: Arc<WallpaperService>,
}
impl WallpaperDaemon {
fn monitor_option(monitor: &str) -> Option<&str> {
if monitor.is_empty() {
None
} else {
Some(monitor)
}
}
}
#[zbus::interface(name = "com.wayle.Wallpaper1")]
impl WallpaperDaemon {
#[instrument(skip(self), fields(path = %path, monitor = %monitor))]
pub async fn set_wallpaper(&self, path: String, monitor: String) -> fdo::Result<()> {
self.service
.set_wallpaper(PathBuf::from(&path), Self::monitor_option(&monitor))
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))
}
#[instrument(skip(self), fields(mode = %mode, monitor = %monitor))]
pub async fn set_fit_mode(&self, mode: String, monitor: String) -> fdo::Result<()> {
let fit_mode = mode
.parse()
.map_err(|err: String| fdo::Error::InvalidArgs(err))?;
self.service
.set_fit_mode(fit_mode, Self::monitor_option(&monitor))
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))
}
#[instrument(skip(self), fields(dir = %directory, interval = %interval_secs, mode = %mode))]
pub async fn start_cycling(
&self,
directory: String,
interval_secs: u32,
mode: String,
) -> fdo::Result<()> {
let cycling_mode: CyclingMode = mode
.parse()
.map_err(|err: String| fdo::Error::InvalidArgs(err))?;
self.service
.start_cycling(
PathBuf::from(directory),
Duration::from_secs(u64::from(interval_secs)),
cycling_mode,
)
.map_err(|err| fdo::Error::Failed(err.to_string()))
}
#[instrument(skip(self))]
pub async fn stop_cycling(&self) -> fdo::Result<()> {
self.service.stop_cycling();
Ok(())
}
#[instrument(skip(self))]
pub async fn next(&self) -> fdo::Result<()> {
self.service
.advance_cycle()
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))
}
#[instrument(skip(self))]
pub async fn previous(&self) -> fdo::Result<()> {
self.service
.rewind_cycle()
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))
}
#[instrument(skip(self))]
pub async fn extract_colors(&self) -> fdo::Result<()> {
self.service
.extract_colors()
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))
}
#[instrument(skip(self), fields(monitor = %monitor))]
pub async fn wallpaper_for_monitor(&self, monitor: String) -> String {
self.service
.wallpaper(&monitor)
.map(|path| path.to_string_lossy().to_string())
.unwrap_or_default()
}
#[instrument(skip(self), fields(monitor = %monitor))]
pub async fn get_fit_mode(&self, monitor: String) -> String {
self.service
.fit_mode(&monitor)
.unwrap_or_default()
.to_string()
}
#[instrument(skip(self))]
pub async fn get_is_cycling(&self) -> bool {
self.service.cycling_config().is_some()
}
#[instrument(skip(self), fields(monitor = %monitor))]
pub async fn set_theming_monitor(&self, monitor: String) {
self.service
.set_theming_monitor(Self::monitor_option(&monitor).map(String::from));
}
#[instrument(skip(self), fields(monitor = %monitor))]
pub async fn register_monitor(&self, monitor: String) {
self.service.register_monitor(&monitor);
}
#[instrument(skip(self), fields(monitor = %monitor))]
pub async fn unregister_monitor(&self, monitor: String) {
self.service.unregister_monitor(&monitor);
}
#[instrument(skip(self))]
pub async fn list_monitors(&self) -> Vec<String> {
self.service.monitor_names()
}
#[zbus(property)]
pub async fn is_cycling(&self) -> bool {
self.service.cycling_config().is_some()
}
#[zbus(property)]
pub async fn theming_monitor(&self) -> String {
self.service.theming_monitor.get().unwrap_or_default()
}
#[zbus(signal)]
pub async fn colors_extracted(
emitter: &zbus::object_server::SignalEmitter<'_>,
) -> zbus::Result<()>;
}