| 1 |
//! Request to run in the background or started automatically when the user |
| 2 |
//! logs in. |
| 3 |
//! |
| 4 |
//! **Note** This portal only works for sandboxed applications. |
| 5 |
//! |
| 6 |
//! Wrapper of the DBus interface: [`org.freedesktop.portal.Background`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Background.html). |
| 7 |
//! |
| 8 |
//! ### Examples |
| 9 |
//! |
| 10 |
//! ```rust,no_run |
| 11 |
//! use ashpd::desktop::background::Background; |
| 12 |
//! |
| 13 |
//! async fn run() -> ashpd::Result<()> { |
| 14 |
//! let response = Background::request() |
| 15 |
//! .reason("Automatically fetch your latest mails") |
| 16 |
//! .auto_start(true) |
| 17 |
//! .command(&["geary"]) |
| 18 |
//! .dbus_activatable(false) |
| 19 |
//! .send() |
| 20 |
//! .await? |
| 21 |
//! .response()?; |
| 22 |
//! |
| 23 |
//! println!("{}", response.auto_start()); |
| 24 |
//! println!("{}", response.run_in_background()); |
| 25 |
//! |
| 26 |
//! Ok(()) |
| 27 |
//! } |
| 28 |
//! ``` |
| 29 |
//! |
| 30 |
//! If no `command` is provided, the [`Exec`](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables) line from the [desktop |
| 31 |
//! file](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#introduction) will be used. |
| 32 |
|
| 33 |
use serde::{Deserialize, Serialize}; |
| 34 |
use zbus::zvariant::{ |
| 35 |
Optional, Type, |
| 36 |
as_value::{self, optional}, |
| 37 |
}; |
| 38 |
|
| 39 |
use super::{HandleToken, Request}; |
| 40 |
use crate::{Error, WindowIdentifier, proxy::Proxy}; |
| 41 |
|
| 42 |
#[derive(Serialize, Type, Debug, Default)] |
| 43 |
#[zvariant(signature = "dict")] |
| 44 |
/// Specified options for a [`BackgroundProxy::request_background`] request. |
| 45 |
pub struct BackgroundRequestOptions { |
| 46 |
#[serde(with = "as_value")] |
| 47 |
handle_token: HandleToken, |
| 48 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 49 |
reason: Option<String>, |
| 50 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 51 |
autostart: Option<bool>, |
| 52 |
#[serde( |
| 53 |
with = "optional", |
| 54 |
rename = "dbus-activatable", |
| 55 |
skip_serializing_if = "Option::is_none" |
| 56 |
)] |
| 57 |
dbus_activatable: Option<bool>, |
| 58 |
#[serde( |
| 59 |
with = "as_value", |
| 60 |
rename = "commandline", |
| 61 |
skip_serializing_if = "Vec::is_empty" |
| 62 |
)] |
| 63 |
command: Vec<String>, |
| 64 |
} |
| 65 |
|
| 66 |
impl BackgroundRequestOptions { |
| 67 |
#[must_use] |
| 68 |
/// Sets whether to auto start the application or not. |
| 69 |
pub fn set_auto_start(mut self, auto_start: impl Into<Option<bool>>) -> Self { |
| 70 |
self.autostart = auto_start.into(); |
| 71 |
self |
| 72 |
} |
| 73 |
|
| 74 |
#[must_use] |
| 75 |
/// Sets whether the application is dbus activatable. |
| 76 |
pub fn set_dbus_activatable(mut self, dbus_activatable: impl Into<Option<bool>>) -> Self { |
| 77 |
self.dbus_activatable = dbus_activatable.into(); |
| 78 |
self |
| 79 |
} |
| 80 |
|
| 81 |
#[must_use] |
| 82 |
/// Specifies the command line to execute. |
| 83 |
/// If this is not specified, the [`Exec`](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables) line from the [desktop |
| 84 |
/// file](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#introduction) |
| 85 |
pub fn set_command<P: IntoIterator<Item = I>, I: AsRef<str> + Type + Serialize>( |
| 86 |
mut self, |
| 87 |
command: P, |
| 88 |
) -> Self { |
| 89 |
self.command = command.into_iter().map(|s| s.as_ref().to_owned()).collect(); |
| 90 |
self |
| 91 |
} |
| 92 |
|
| 93 |
#[must_use] |
| 94 |
/// Sets a user-visible reason for the request. |
| 95 |
pub fn set_reason<'a>(mut self, reason: impl Into<Option<&'a str>>) -> Self { |
| 96 |
self.reason = reason.into().map(ToOwned::to_owned); |
| 97 |
self |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
#[derive(Deserialize, Type, Debug)] |
| 102 |
/// The response of a [`BackgroundRequest`] request. |
| 103 |
#[zvariant(signature = "dict")] |
| 104 |
pub struct Background { |
| 105 |
#[serde(with = "as_value")] |
| 106 |
background: bool, |
| 107 |
#[serde(with = "as_value")] |
| 108 |
autostart: bool, |
| 109 |
} |
| 110 |
|
| 111 |
impl Background { |
| 112 |
/// Creates a new builder-pattern struct instance to construct |
| 113 |
/// [`Background`]. |
| 114 |
/// |
| 115 |
/// This method returns an instance of [`BackgroundRequest`]. |
| 116 |
pub fn request() -> BackgroundRequest { |
| 117 |
BackgroundRequest::default() |
| 118 |
} |
| 119 |
|
| 120 |
/// If the application is allowed to run in the background. |
| 121 |
pub fn run_in_background(&self) -> bool { |
| 122 |
self.background |
| 123 |
} |
| 124 |
|
| 125 |
/// If the application will be auto-started. |
| 126 |
pub fn auto_start(&self) -> bool { |
| 127 |
self.autostart |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#[derive(Serialize, Type, Debug, Default)] |
| 132 |
#[zvariant(signature = "dict")] |
| 133 |
/// Specified options for a [`BackgroundProxy::set_status`] request. |
| 134 |
pub struct SetStatusOptions { |
| 135 |
#[serde(with = "as_value")] |
| 136 |
message: String, |
| 137 |
} |
| 138 |
|
| 139 |
impl SetStatusOptions { |
| 140 |
/// Sets the message to be displayed to the user. |
| 141 |
pub fn set_message(mut self, message: &str) -> Self { |
| 142 |
self.message = message.to_owned(); |
| 143 |
self |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/// The interface lets sandboxed applications request that the application |
| 148 |
/// is allowed to run in the background or started automatically when the user |
| 149 |
/// logs in. |
| 150 |
/// |
| 151 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Background`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Background.html). |
| 152 |
#[doc(alias = "org.freedesktop.portal.Background")] |
| 153 |
pub struct BackgroundProxy(Proxy<'static>); |
| 154 |
|
| 155 |
impl BackgroundProxy { |
| 156 |
/// Create a new instance of [`BackgroundProxy`]. |
| 157 |
pub async fn new() -> Result<BackgroundProxy, Error> { |
| 158 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.Background").await?; |
| 159 |
Ok(Self(proxy)) |
| 160 |
} |
| 161 |
|
| 162 |
/// Create a new instance of [`BackgroundProxy`]. |
| 163 |
pub async fn with_connection(connection: zbus::Connection) -> Result<BackgroundProxy, Error> { |
| 164 |
let proxy = |
| 165 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Background") |
| 166 |
.await?; |
| 167 |
Ok(Self(proxy)) |
| 168 |
} |
| 169 |
|
| 170 |
/// Returns the version of the portal interface. |
| 171 |
pub fn version(&self) -> u32 { |
| 172 |
self.0.version() |
| 173 |
} |
| 174 |
|
| 175 |
/// Sets the status of the application running in background. |
| 176 |
/// |
| 177 |
/// # Arguments |
| 178 |
/// |
| 179 |
/// * `message` - A string that will be used as the status message of the |
| 180 |
/// application. |
| 181 |
/// |
| 182 |
/// # Required version |
| 183 |
/// |
| 184 |
/// The method requires the 2nd version implementation of the portal and |
| 185 |
/// would fail with [`Error::RequiresVersion`] otherwise. |
| 186 |
/// |
| 187 |
/// # Specifications |
| 188 |
/// |
| 189 |
/// See also [`SetStatus`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Background.html#org-freedesktop-portal-background-setstatus). |
| 190 |
#[doc(alias = "SetStatus")] |
| 191 |
pub async fn set_status(&self, options: SetStatusOptions) -> Result<(), Error> { |
| 192 |
self.0.call_versioned("SetStatus", &(options), 2).await |
| 193 |
} |
| 194 |
|
| 195 |
/// Request background access. |
| 196 |
/// |
| 197 |
/// # Specifications |
| 198 |
/// |
| 199 |
/// See also [`RequestBackground`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Background.html#org-freedesktop-portal-background-requestbackground). |
| 200 |
#[doc(alias = "RequestBackground")] |
| 201 |
pub async fn request_background( |
| 202 |
&self, |
| 203 |
identifier: Option<&WindowIdentifier>, |
| 204 |
options: BackgroundRequestOptions, |
| 205 |
) -> Result<Request<Background>, Error> { |
| 206 |
let identifier = Optional::from(identifier); |
| 207 |
self.0 |
| 208 |
.request( |
| 209 |
&options.handle_token, |
| 210 |
"RequestBackground", |
| 211 |
(identifier, &options), |
| 212 |
) |
| 213 |
.await |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl std::ops::Deref for BackgroundProxy { |
| 218 |
type Target = zbus::Proxy<'static>; |
| 219 |
|
| 220 |
fn deref(&self) -> &Self::Target { |
| 221 |
&self.0 |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
#[doc(alias = "xdp_portal_request_background")] |
| 226 |
/// A [builder-pattern] type to construct [`Background`]. |
| 227 |
/// |
| 228 |
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html |
| 229 |
#[derive(Debug, Default)] |
| 230 |
pub struct BackgroundRequest { |
| 231 |
identifier: Option<WindowIdentifier>, |
| 232 |
options: BackgroundRequestOptions, |
| 233 |
} |
| 234 |
|
| 235 |
impl BackgroundRequest { |
| 236 |
#[must_use] |
| 237 |
/// Sets a window identifier. |
| 238 |
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self { |
| 239 |
self.identifier = identifier.into(); |
| 240 |
self |
| 241 |
} |
| 242 |
|
| 243 |
#[must_use] |
| 244 |
/// Sets whether to auto start the application or not. |
| 245 |
pub fn auto_start(mut self, auto_start: impl Into<Option<bool>>) -> Self { |
| 246 |
self.options.autostart = auto_start.into(); |
| 247 |
self |
| 248 |
} |
| 249 |
|
| 250 |
#[must_use] |
| 251 |
/// Sets whether the application is dbus activatable. |
| 252 |
pub fn dbus_activatable(mut self, dbus_activatable: impl Into<Option<bool>>) -> Self { |
| 253 |
self.options.dbus_activatable = dbus_activatable.into(); |
| 254 |
self |
| 255 |
} |
| 256 |
|
| 257 |
#[must_use] |
| 258 |
/// Specifies the command line to execute. |
| 259 |
/// If this is not specified, the [`Exec`](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables) line from the [desktop |
| 260 |
/// file](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#introduction) |
| 261 |
pub fn command<P: IntoIterator<Item = I>, I: AsRef<str> + Type + Serialize>( |
| 262 |
mut self, |
| 263 |
command: P, |
| 264 |
) -> Self { |
| 265 |
self.options.command = command.into_iter().map(|s| s.as_ref().to_owned()).collect(); |
| 266 |
self |
| 267 |
} |
| 268 |
|
| 269 |
#[must_use] |
| 270 |
/// Sets a user-visible reason for the request. |
| 271 |
pub fn reason<'a>(mut self, reason: impl Into<Option<&'a str>>) -> Self { |
| 272 |
self.options.reason = reason.into().map(ToOwned::to_owned); |
| 273 |
self |
| 274 |
} |
| 275 |
|
| 276 |
/// Build the [`Background`]. |
| 277 |
pub async fn send(self) -> Result<Request<Background>, Error> { |
| 278 |
let proxy = BackgroundProxy::new().await?; |
| 279 |
proxy |
| 280 |
.request_background(self.identifier.as_ref(), self.options) |
| 281 |
.await |
| 282 |
} |
| 283 |
} |