use std::{fmt, os::fd::AsFd, str::FromStr};
use serde::{self, Deserialize, Serialize};
use zbus::zvariant::{Fd, SerializeDict, Type};
use super::Request;
use crate::{desktop::HandleToken, proxy::Proxy, Error, WindowIdentifier};
#[cfg_attr(feature = "glib", derive(glib::Enum))]
#[cfg_attr(feature = "glib", enum_type(name = "AshpdSetOn"))]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Type)]
#[zvariant(signature = "s")]
#[serde(rename_all = "lowercase")]
pub enum SetOn {
Lockscreen,
Background,
Both,
}
impl fmt::Display for SetOn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Lockscreen => write!(f, "Lockscreen"),
Self::Background => write!(f, "Background"),
Self::Both => write!(f, "Both"),
}
}
}
impl AsRef<str> for SetOn {
fn as_ref(&self) -> &str {
match self {
Self::Lockscreen => "Lockscreen",
Self::Background => "Background",
Self::Both => "Both",
}
}
}
impl From<SetOn> for &'static str {
fn from(s: SetOn) -> Self {
match s {
SetOn::Lockscreen => "Lockscreen",
SetOn::Background => "Background",
SetOn::Both => "Both",
}
}
}
impl FromStr for SetOn {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Lockscreen" => Ok(SetOn::Lockscreen),
"Background" => Ok(SetOn::Background),
"Both" => Ok(SetOn::Both),
_ => Err(Error::ParseError("Failed to parse SetOn, invalid value")),
}
}
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct WallpaperOptions {
handle_token: HandleToken,
#[zvariant(rename = "show-preview")]
show_preview: Option<bool>,
#[zvariant(rename = "set-on")]
set_on: Option<SetOn>,
}
struct WallpaperProxy<'a>(Proxy<'a>);
impl<'a> WallpaperProxy<'a> {
pub async fn new() -> Result<WallpaperProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Wallpaper").await?;
Ok(Self(proxy))
}
pub async fn set_wallpaper_file(
&self,
identifier: Option<&WindowIdentifier>,
file: &impl AsFd,
options: WallpaperOptions,
) -> Result<Request<()>, Error> {
let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
self.0
.empty_request(
&options.handle_token,
"SetWallpaperFile",
&(&identifier, Fd::from(file), &options),
)
.await
}
pub async fn set_wallpaper_uri(
&self,
identifier: Option<&WindowIdentifier>,
uri: &url::Url,
options: WallpaperOptions,
) -> Result<Request<()>, Error> {
let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
self.0
.empty_request(
&options.handle_token,
"SetWallpaperURI",
&(&identifier, uri, &options),
)
.await
}
}
impl<'a> std::ops::Deref for WallpaperProxy<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Default)]
#[doc(alias = "xdp_portal_set_wallpaper")]
#[doc(alias = "org.freedesktop.portal.Wallpaper")]
pub struct WallpaperRequest {
identifier: Option<WindowIdentifier>,
options: WallpaperOptions,
}
impl WallpaperRequest {
#[must_use]
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into();
self
}
#[must_use]
pub fn show_preview(mut self, show_preview: impl Into<Option<bool>>) -> Self {
self.options.show_preview = show_preview.into();
self
}
#[must_use]
pub fn set_on(mut self, set_on: impl Into<Option<SetOn>>) -> Self {
self.options.set_on = set_on.into();
self
}
pub async fn build_uri(self, uri: &url::Url) -> Result<Request<()>, Error> {
let proxy = WallpaperProxy::new().await?;
proxy
.set_wallpaper_uri(self.identifier.as_ref(), uri, self.options)
.await
}
pub async fn build_file(self, file: &impl AsFd) -> Result<Request<()>, Error> {
let proxy = WallpaperProxy::new().await?;
proxy
.set_wallpaper_file(self.identifier.as_ref(), file, self.options)
.await
}
}
#[cfg(test)]
mod tests {
use super::SetOn;
#[test]
fn serialize_deserialize() {
let set_on = SetOn::Both;
let string = serde_json::to_string(&set_on).unwrap();
assert_eq!(string, "\"both\"");
let decoded = serde_json::from_str(&string).unwrap();
assert_eq!(set_on, decoded);
}
}