use super::{DESTINATION, PATH};
use crate::{helpers::call_method, Error};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::os::unix::io::AsRawFd;
use zvariant::Fd;
use zvariant_derive::Type;
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Clone, Copy, Hash, Debug, Type)]
#[repr(u32)]
enum TrashStatus {
Failed = 0,
Succeeded = 1,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Trash")]
pub struct TrashProxy<'a>(zbus::azync::Proxy<'a>);
impl<'a> TrashProxy<'a> {
pub async fn new(connection: &zbus::azync::Connection) -> Result<TrashProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Trash")
.path(PATH)?
.destination(DESTINATION)
.build_async()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::azync::Proxy<'_> {
&self.0
}
#[doc(alias = "TrashFile")]
pub async fn trash_file<T>(&self, fd: &T) -> Result<(), Error>
where
T: AsRawFd,
{
let status = call_method(&self.0, "TrashFile", &(Fd::from(fd.as_raw_fd()))).await?;
match status {
TrashStatus::Failed => Err(Error::TrashFailed),
TrashStatus::Succeeded => Ok(()),
}
}
}
#[doc(alias = "xdp_portal_trash_file")]
pub async fn trash_file<F: AsRawFd>(fd: &F) -> Result<(), Error> {
let connection = zbus::azync::Connection::new_session().await?;
let proxy = TrashProxy::new(&connection).await?;
proxy.trash_file(fd).await
}