Debian Codegraph @

src/desktop/trash.rs

raw
1 //! Move a file to the trash.
2 //!
3 //! # Examples
4 //!
5 //!
6 //! ```rust,no_run
7 //! use std::{fs::File, os::fd::AsFd};
8 //!
9 //! use ashpd::desktop::trash;
10 //!
11 //! async fn run() -> ashpd::Result<()> {
12 //! let path = "/home/bilelmoussaoui/adwaita-night.jpg";
13 //! let file = std::fs::OpenOptions::new()
14 //! .write(true)
15 //! .read(true)
16 //! .open(path)
17 //! .unwrap();
18 //! trash::trash_file(&file.as_fd()).await?;
19 //! Ok(())
20 //! }
21 //! ```
22 //!
23 //! Or by using the Proxy directly
24 //!
25 //! ```rust,no_run
26 //! use std::{fs::File, os::fd::AsFd};
27 //!
28 //! use ashpd::desktop::trash::TrashProxy;
29 //!
30 //! async fn run() -> ashpd::Result<()> {
31 //! let path = "/home/bilelmoussaoui/adwaita-night.jpg";
32 //! let file = std::fs::OpenOptions::new()
33 //! .write(true)
34 //! .read(true)
35 //! .open(path)
36 //! .unwrap();
37 //! let proxy = TrashProxy::new().await?;
38 //! proxy.trash_file(&file.as_fd()).await?;
39 //! Ok(())
40 //! }
41 //! ```
42
43 use std::os::fd::AsFd;
44
45 use serde_repr::{Deserialize_repr, Serialize_repr};
46 use zbus::zvariant::{Fd, Type};
47
48 use crate::{Error, error::PortalError, proxy::Proxy};
49
50 #[derive(Debug, Deserialize_repr, Serialize_repr, PartialEq, Type)]
51 #[repr(u32)]
52 enum TrashStatus {
53 Failed = 0,
54 Succeeded = 1,
55 }
56
57 /// The interface lets sandboxed applications send files to the trashcan.
58 ///
59 /// Wrapper of the DBus interface: [`org.freedesktop.portal.Trash`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Trash.html).
60 #[derive(Debug)]
61 #[doc(alias = "org.freedesktop.portal.Trash")]
62 pub struct TrashProxy(Proxy<'static>);
63
64 impl TrashProxy {
65 /// Create a new instance of [`TrashProxy`].
66 pub async fn new() -> Result<Self, Error> {
67 let proxy = Proxy::new_desktop("org.freedesktop.portal.Trash").await?;
68 Ok(Self(proxy))
69 }
70
71 /// Create a new instance of [`TrashProxy`].
72 pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
73 let proxy =
74 Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Trash").await?;
75 Ok(Self(proxy))
76 }
77
78 /// Returns the version of the portal interface.
79 pub fn version(&self) -> u32 {
80 self.0.version()
81 }
82
83 /// Sends a file to the trashcan.
84 /// Applications are allowed to trash a file if they can open it in
85 /// read/write mode.
86 ///
87 /// # Arguments
88 ///
89 /// * `fd` - The file descriptor.
90 ///
91 /// # Specifications
92 ///
93 /// See also [`TrashFile`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Trash.html#org-freedesktop-portal-trash-trashfile).
94 #[doc(alias = "TrashFile")]
95 #[doc(alias = "xdp_portal_trash_file")]
96 pub async fn trash_file(&self, fd: &impl AsFd) -> Result<(), Error> {
97 let status = self.0.call("TrashFile", &(Fd::from(fd))).await?;
98 match status {
99 TrashStatus::Failed => Err(Error::Portal(PortalError::Failed(
100 "Failed to trash file".to_string(),
101 ))),
102 TrashStatus::Succeeded => Ok(()),
103 }
104 }
105 }
106
107 impl std::ops::Deref for TrashProxy {
108 type Target = zbus::Proxy<'static>;
109
110 fn deref(&self) -> &Self::Target {
111 &self.0
112 }
113 }
114
115 #[doc(alias = "xdp_portal_trash_file")]
116 /// A handy wrapper around [`TrashProxy::trash_file`].
117 pub async fn trash_file(fd: &impl AsFd) -> Result<(), Error> {
118 let proxy = TrashProxy::new().await?;
119 proxy.trash_file(fd).await
120 }
121
122 #[cfg(test)]
123 mod test {
124 use super::TrashStatus;
125
126 #[test]
127 fn status_serde() {
128 #[derive(serde::Serialize, serde::Deserialize)]
129 struct Test {
130 status: TrashStatus,
131 }
132
133 let status = Test {
134 status: TrashStatus::Failed,
135 };
136
137 let x = serde_json::to_string(&status).unwrap();
138 let y: Test = serde_json::from_str(&x).unwrap();
139 assert_eq!(y.status, TrashStatus::Failed);
140 }
141 }