Debian Codegraph @

src/flatpak/update_monitor.rs

raw
1 //! # Examples
2 //!
3 //! How to monitor if there's a new update and install it.
4 //! Only available for Flatpak applications.
5 //!
6 //! ```rust,no_run
7 //! use ashpd::flatpak::Flatpak;
8 //! use futures_util::StreamExt;
9 //!
10 //! async fn run() -> ashpd::Result<()> {
11 //! let proxy = Flatpak::new().await?;
12 //!
13 //! let monitor = proxy.create_update_monitor(Default::default()).await?;
14 //! let info = monitor.receive_update_available().await?;
15 //!
16 //! monitor.update(None, Default::default()).await?;
17 //! let progress = monitor
18 //! .receive_progress()
19 //! .await?
20 //! .next()
21 //! .await
22 //! .expect("Stream exhausted");
23 //! println!("{:#?}", progress);
24 //!
25 //! Ok(())
26 //! }
27 //! ```
28
29 use futures_util::Stream;
30 use serde::{Deserialize, Serialize};
31 use serde_repr::{Deserialize_repr, Serialize_repr};
32 use zbus::zvariant::{
33 ObjectPath, Optional, Type,
34 as_value::{self, optional},
35 };
36
37 use crate::{Error, WindowIdentifier, proxy::Proxy};
38
39 #[derive(Serialize, Type, Debug, Default)]
40 /// Specified options for a [`UpdateMonitor::update`] request.
41 #[zvariant(signature = "dict")]
42 pub struct UpdateOptions {}
43
44 #[derive(Deserialize, Type, Debug)]
45 /// A response containing the update information when an update is available.
46 #[zvariant(signature = "dict")]
47 #[serde(rename_all = "kebab-case")]
48 pub struct UpdateInfo {
49 #[serde(with = "as_value")]
50 running_commit: String,
51 #[serde(with = "as_value")]
52 local_commit: String,
53 #[serde(with = "as_value")]
54 remote_commit: String,
55 }
56
57 impl UpdateInfo {
58 /// The currently running OSTree commit.
59 pub fn running_commit(&self) -> &str {
60 &self.running_commit
61 }
62
63 /// The locally installed OSTree commit.
64 pub fn local_commit(&self) -> &str {
65 &self.local_commit
66 }
67
68 /// The available commit to install.
69 pub fn remote_commit(&self) -> &str {
70 &self.remote_commit
71 }
72 }
73
74 #[cfg_attr(feature = "glib", derive(glib::Enum))]
75 #[cfg_attr(feature = "glib", enum_type(name = "AshpdUpdateStatus"))]
76 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Copy, Clone, Debug, Type)]
77 #[repr(u32)]
78 /// The update status.
79 pub enum UpdateStatus {
80 #[doc(alias = "XDP_UPDATE_STATUS_RUNNING")]
81 /// Running.
82 Running = 0,
83 #[doc(alias = "XDP_UPDATE_STATUS_EMPTY")]
84 /// No update to install.
85 Empty = 1,
86 #[doc(alias = "XDP_UPDATE_STATUS_DONE")]
87 /// Done.
88 Done = 2,
89 #[doc(alias = "XDP_UPDATE_STATUS_FAILED")]
90 /// Failed.
91 Failed = 3,
92 }
93
94 #[derive(Deserialize, Type, Debug)]
95 /// A response of the update progress signal.
96 #[zvariant(signature = "dict")]
97 pub struct UpdateProgress {
98 #[serde(default, with = "optional")]
99 n_ops: Option<u32>,
100 #[serde(default, with = "optional")]
101 op: Option<u32>,
102 #[serde(default, with = "optional")]
103 progress: Option<u32>,
104 #[serde(default, with = "optional")]
105 status: Option<UpdateStatus>,
106 #[serde(default, with = "optional")]
107 error: Option<String>,
108 #[serde(default, with = "optional")]
109 error_message: Option<String>,
110 }
111
112 impl UpdateProgress {
113 /// The error message, sent when status is [`UpdateStatus::Failed`].
114 pub fn error_message(&self) -> Option<&str> {
115 self.error_message.as_deref()
116 }
117
118 /// The error name, sent when status is [`UpdateStatus::Failed`].
119 pub fn error(&self) -> Option<&str> {
120 self.error.as_deref()
121 }
122
123 /// The overall status of the update.
124 pub fn status(&self) -> Option<UpdateStatus> {
125 self.status
126 }
127
128 /// The progress of the currently active operation, as a number between 0
129 /// and 100.
130 pub fn progress(&self) -> Option<u32> {
131 self.progress
132 }
133
134 /// The position of the currently active operation.
135 pub fn op(&self) -> Option<u32> {
136 self.op
137 }
138
139 /// The number of operations that the update consists of.
140 pub fn n_ops(&self) -> Option<u32> {
141 self.n_ops
142 }
143 }
144
145 /// The interface exposes some interactions with Flatpak on the host to the
146 /// sandbox. For example, it allows you to restart the applications or start a
147 /// more sandboxed instance.
148 ///
149 /// Wrapper of the DBus interface: [`org.freedesktop.portal.Flatpak.UpdateMonitor`](https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-org.freedesktop.portal.Flatpak.UpdateMonitor).
150 #[derive(Debug)]
151 #[doc(alias = "org.freedesktop.portal.Flatpak.UpdateMonitor")]
152 pub struct UpdateMonitor(Proxy<'static>);
153
154 impl UpdateMonitor {
155 /// Create a new instance of [`UpdateMonitor`].
156 ///
157 /// **Note** A [`UpdateMonitor`] is not supposed to be created
158 /// manually.
159 pub(crate) async fn with_connection(
160 connection: zbus::Connection,
161 path: ObjectPath<'static>,
162 ) -> Result<Self, Error> {
163 let proxy = Proxy::new_flatpak_with_path(
164 connection,
165 "org.freedesktop.portal.Flatpak.UpdateMonitor",
166 path,
167 )
168 .await?;
169 Ok(Self(proxy))
170 }
171
172 /// Returns the version of the portal interface.
173 pub fn version(&self) -> u32 {
174 self.0.version()
175 }
176
177 /// A signal received when there's progress during the application update.
178 ///
179 /// # Specifications
180 ///
181 /// See also [`Progress`](https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-signal-org-freedesktop-portal-Flatpak-UpdateMonitor.Progress).
182 #[doc(alias = "Progress")]
183 #[doc(alias = "XdpPortal::update-progress")]
184 pub async fn receive_progress(&self) -> Result<impl Stream<Item = UpdateProgress>, Error> {
185 self.0.signal("Progress").await
186 }
187
188 /// A signal received when there's an application update.
189 ///
190 /// # Specifications
191 ///
192 /// See also [`UpdateAvailable`](https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-signal-org-freedesktop-portal-Flatpak-UpdateMonitor.UpdateAvailable).
193 #[doc(alias = "UpdateAvailable")]
194 #[doc(alias = "XdpPortal::update-available")]
195 pub async fn receive_update_available(&self) -> Result<impl Stream<Item = UpdateInfo>, Error> {
196 self.0.signal("UpdateAvailable").await
197 }
198
199 /// Asks to install an update of the calling app.
200 ///
201 /// **Note** updates are only allowed if the new version has the same
202 /// permissions (or less) than the currently installed version.
203 ///
204 /// # Specifications
205 ///
206 /// See also [`Update`](https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-method-org-freedesktop-portal-Flatpak-UpdateMonitor.Update).
207 #[doc(alias = "Update")]
208 #[doc(alias = "xdp_portal_update_install")]
209 pub async fn update(
210 &self,
211 identifier: Option<&WindowIdentifier>,
212 options: UpdateOptions,
213 ) -> Result<(), Error> {
214 let identifier = Optional::from(identifier);
215
216 self.0.call("Update", &(identifier, options)).await
217 }
218
219 /// Ends the update monitoring and cancels any ongoing installation.
220 ///
221 /// # Specifications
222 ///
223 /// See also [`Close`](https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-method-org-freedesktop-portal-Flatpak-UpdateMonitor.Close).
224 #[doc(alias = "Close")]
225 pub async fn close(&self) -> Result<(), Error> {
226 self.0.call("Close", &()).await
227 }
228 }
229
230 impl std::ops::Deref for UpdateMonitor {
231 type Target = zbus::Proxy<'static>;
232
233 fn deref(&self) -> &Self::Target {
234 &self.0
235 }
236 }