| 1 |
//! Open a URI or a directory. |
| 2 |
//! |
| 3 |
//! Wrapper of the DBus interface: [`org.freedesktop.portal.OpenURI`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html). |
| 4 |
//! |
| 5 |
//! # Examples |
| 6 |
//! |
| 7 |
//! ## Open a file |
| 8 |
//! |
| 9 |
//! ```rust,no_run |
| 10 |
//! use std::{fs::File, os::fd::AsFd}; |
| 11 |
//! |
| 12 |
//! use ashpd::desktop::open_uri::OpenFileRequest; |
| 13 |
//! |
| 14 |
//! async fn run() -> ashpd::Result<()> { |
| 15 |
//! let file = File::open("/home/bilelmoussaoui/adwaita-day.jpg").unwrap(); |
| 16 |
//! OpenFileRequest::default() |
| 17 |
//! .ask(true) |
| 18 |
//! .send_file(&file.as_fd()) |
| 19 |
//! .await?; |
| 20 |
//! Ok(()) |
| 21 |
//! } |
| 22 |
//! ``` |
| 23 |
//! |
| 24 |
//! ## Open a file from a URI |
| 25 |
//! |
| 26 |
//! |
| 27 |
//! ```rust,no_run |
| 28 |
//! use ashpd::desktop::open_uri::OpenFileRequest; |
| 29 |
//! |
| 30 |
//! async fn run() -> ashpd::Result<()> { |
| 31 |
//! let uri = |
| 32 |
//! ashpd::Uri::parse("file:///home/bilelmoussaoui/Downloads/adwaita-night.jpg").unwrap(); |
| 33 |
//! OpenFileRequest::default().ask(true).send_uri(&uri).await?; |
| 34 |
//! Ok(()) |
| 35 |
//! } |
| 36 |
//! ``` |
| 37 |
//! |
| 38 |
//! ## Open a directory |
| 39 |
//! |
| 40 |
//! ```rust,no_run |
| 41 |
//! use std::{fs::File, os::fd::AsFd}; |
| 42 |
//! |
| 43 |
//! use ashpd::desktop::open_uri::OpenDirectoryRequest; |
| 44 |
//! |
| 45 |
//! async fn run() -> ashpd::Result<()> { |
| 46 |
//! let directory = File::open("/home/bilelmoussaoui/Downloads").unwrap(); |
| 47 |
//! OpenDirectoryRequest::default() |
| 48 |
//! .send(&directory.as_fd()) |
| 49 |
//! .await?; |
| 50 |
//! Ok(()) |
| 51 |
//! } |
| 52 |
//! ``` |
| 53 |
|
| 54 |
use std::os::fd::AsFd; |
| 55 |
|
| 56 |
use serde::Serialize; |
| 57 |
use zbus::zvariant::{ |
| 58 |
Fd, Optional, Type, |
| 59 |
as_value::{self, optional}, |
| 60 |
}; |
| 61 |
|
| 62 |
use super::{HandleToken, Request}; |
| 63 |
use crate::{ActivationToken, Error, Uri, WindowIdentifier, proxy::Proxy}; |
| 64 |
|
| 65 |
#[derive(Serialize, Type, Debug, Default)] |
| 66 |
#[zvariant(signature = "dict")] |
| 67 |
/// Options passed to [`OpenURIProxy::open_directory`]. |
| 68 |
pub struct OpenDirOptions { |
| 69 |
#[serde(with = "as_value")] |
| 70 |
handle_token: HandleToken, |
| 71 |
/// The activation token. |
| 72 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 73 |
pub activation_token: Option<ActivationToken>, |
| 74 |
} |
| 75 |
|
| 76 |
#[derive(Serialize, Type, Debug, Default)] |
| 77 |
#[zvariant(signature = "dict")] |
| 78 |
/// Options passed to [`OpenURIProxy::open_file`]. |
| 79 |
pub struct OpenFileOptions { |
| 80 |
#[serde(with = "as_value")] |
| 81 |
handle_token: HandleToken, |
| 82 |
/// Whether the file should be writable. |
| 83 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 84 |
pub writeable: Option<bool>, |
| 85 |
/// Whether to ask the user. |
| 86 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 87 |
pub ask: Option<bool>, |
| 88 |
/// The activation token. |
| 89 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 90 |
pub activation_token: Option<ActivationToken>, |
| 91 |
} |
| 92 |
|
| 93 |
#[derive(Debug, Serialize, Type, Default)] |
| 94 |
#[zvariant(signature = "dict")] |
| 95 |
/// Options passed to [`OpenURIProxy::scheme_supported`]. |
| 96 |
pub struct SchemeSupportedOptions {} |
| 97 |
|
| 98 |
/// The interface lets sandboxed applications open URIs |
| 99 |
/// (e.g. a http: link to the applications homepage) under the control of the |
| 100 |
/// user. |
| 101 |
/// |
| 102 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.OpenURI`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html). |
| 103 |
#[derive(Debug)] |
| 104 |
pub struct OpenURIProxy(Proxy<'static>); |
| 105 |
|
| 106 |
impl OpenURIProxy { |
| 107 |
/// Create a new instance of [`OpenURIProxy`]. |
| 108 |
pub async fn new() -> Result<OpenURIProxy, Error> { |
| 109 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.OpenURI").await?; |
| 110 |
Ok(Self(proxy)) |
| 111 |
} |
| 112 |
|
| 113 |
/// Create a new instance of [`OpenURIProxy`]. |
| 114 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> { |
| 115 |
let proxy = |
| 116 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.OpenURI") |
| 117 |
.await?; |
| 118 |
Ok(Self(proxy)) |
| 119 |
} |
| 120 |
|
| 121 |
/// Returns the version of the portal interface. |
| 122 |
pub fn version(&self) -> u32 { |
| 123 |
self.0.version() |
| 124 |
} |
| 125 |
|
| 126 |
/// Asks to open the directory containing a local file in the file browser. |
| 127 |
/// |
| 128 |
/// # Arguments |
| 129 |
/// |
| 130 |
/// * `identifier` - Identifier for the application window. |
| 131 |
/// * `directory` - File descriptor for a file. |
| 132 |
/// * `options` - The options to pass to the request. |
| 133 |
/// |
| 134 |
/// # Specifications |
| 135 |
/// |
| 136 |
/// See also [`OpenDirectory`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html#org-freedesktop-portal-openuri-opendirectory). |
| 137 |
#[doc(alias = "OpenDirectory")] |
| 138 |
pub async fn open_directory( |
| 139 |
&self, |
| 140 |
identifier: Option<&WindowIdentifier>, |
| 141 |
directory: &impl AsFd, |
| 142 |
options: OpenDirOptions, |
| 143 |
) -> Result<Request<()>, Error> { |
| 144 |
let identifier = Optional::from(identifier); |
| 145 |
self.0 |
| 146 |
.empty_request( |
| 147 |
&options.handle_token, |
| 148 |
"OpenDirectory", |
| 149 |
&(identifier, Fd::from(directory), &options), |
| 150 |
) |
| 151 |
.await |
| 152 |
} |
| 153 |
|
| 154 |
/// Asks to open a local file. |
| 155 |
/// |
| 156 |
/// # Arguments |
| 157 |
/// |
| 158 |
/// * `identifier` - Identifier for the application window. |
| 159 |
/// * `file` - File descriptor for the file to open. |
| 160 |
/// * `options` - The options to pass to the request. |
| 161 |
/// |
| 162 |
/// # Specifications |
| 163 |
/// |
| 164 |
/// See also [`OpenFile`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html#org-freedesktop-portal-openuri-openfile). |
| 165 |
#[doc(alias = "OpenFile")] |
| 166 |
pub async fn open_file( |
| 167 |
&self, |
| 168 |
identifier: Option<&WindowIdentifier>, |
| 169 |
file: &impl AsFd, |
| 170 |
options: OpenFileOptions, |
| 171 |
) -> Result<Request<()>, Error> { |
| 172 |
let identifier = Optional::from(identifier); |
| 173 |
self.0 |
| 174 |
.empty_request( |
| 175 |
&options.handle_token, |
| 176 |
"OpenFile", |
| 177 |
&(identifier, Fd::from(file), &options), |
| 178 |
) |
| 179 |
.await |
| 180 |
} |
| 181 |
|
| 182 |
/// Asks to open a local file. |
| 183 |
/// |
| 184 |
/// # Arguments |
| 185 |
/// |
| 186 |
/// * `identifier` - Identifier for the application window. |
| 187 |
/// * `uri` - The uri to open. |
| 188 |
/// * `options` - The options to pass to the request. |
| 189 |
/// |
| 190 |
/// *Note* that `file` uris are explicitly not supported by this method. |
| 191 |
/// Use [`Self::open_file`] or [`Self::open_directory`] instead. |
| 192 |
/// |
| 193 |
/// # Specifications |
| 194 |
/// |
| 195 |
/// See also [`OpenURI`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html#org-freedesktop-portal-openuri-openuri). |
| 196 |
#[doc(alias = "OpenURI")] |
| 197 |
pub async fn open_uri( |
| 198 |
&self, |
| 199 |
identifier: Option<&WindowIdentifier>, |
| 200 |
uri: &Uri, |
| 201 |
options: OpenFileOptions, |
| 202 |
) -> Result<Request<()>, Error> { |
| 203 |
let identifier = Optional::from(identifier); |
| 204 |
self.0 |
| 205 |
.empty_request( |
| 206 |
&options.handle_token, |
| 207 |
"OpenURI", |
| 208 |
&(identifier, uri, &options), |
| 209 |
) |
| 210 |
.await |
| 211 |
} |
| 212 |
|
| 213 |
/// Whether a scheme is supported or not. |
| 214 |
/// |
| 215 |
/// # Arguments |
| 216 |
/// |
| 217 |
/// * `scheme` - URI protocol scheme to check. |
| 218 |
/// * `options` - The options to pass to the request. |
| 219 |
/// |
| 220 |
/// # Specifications |
| 221 |
/// |
| 222 |
/// See also [`SchemeSupported`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html#org-freedesktop-portal-openuri-schemesupported). |
| 223 |
#[doc(alias = "SchemeSupported")] |
| 224 |
pub async fn scheme_supported( |
| 225 |
&self, |
| 226 |
schema: &str, |
| 227 |
options: SchemeSupportedOptions, |
| 228 |
) -> Result<bool, Error> { |
| 229 |
self.0.call("SchemeSupported", &(schema, &options)).await |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl std::ops::Deref for OpenURIProxy { |
| 234 |
type Target = zbus::Proxy<'static>; |
| 235 |
|
| 236 |
fn deref(&self) -> &Self::Target { |
| 237 |
&self.0 |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#[derive(Debug, Default)] |
| 242 |
#[doc(alias = "org.freedesktop.portal.OpenURI")] |
| 243 |
#[doc(alias = "xdp_portal_open_uri")] |
| 244 |
/// A [builder-pattern] type to open a file. |
| 245 |
/// |
| 246 |
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html |
| 247 |
pub struct OpenFileRequest { |
| 248 |
identifier: Option<WindowIdentifier>, |
| 249 |
options: OpenFileOptions, |
| 250 |
connection: Option<zbus::Connection>, |
| 251 |
} |
| 252 |
|
| 253 |
impl OpenFileRequest { |
| 254 |
#[must_use] |
| 255 |
/// Sets a window identifier. |
| 256 |
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self { |
| 257 |
self.identifier = identifier.into(); |
| 258 |
self |
| 259 |
} |
| 260 |
|
| 261 |
#[must_use] |
| 262 |
/// Whether the file should be writeable or not. |
| 263 |
pub fn writeable(mut self, writeable: impl Into<Option<bool>>) -> Self { |
| 264 |
self.options.writeable = writeable.into(); |
| 265 |
self |
| 266 |
} |
| 267 |
|
| 268 |
#[must_use] |
| 269 |
/// Whether to always ask the user which application to use or not. |
| 270 |
pub fn ask(mut self, ask: impl Into<Option<bool>>) -> Self { |
| 271 |
self.options.ask = ask.into(); |
| 272 |
self |
| 273 |
} |
| 274 |
|
| 275 |
/// Sets the token that can be used to activate the chosen application. |
| 276 |
#[must_use] |
| 277 |
pub fn activation_token( |
| 278 |
mut self, |
| 279 |
activation_token: impl Into<Option<ActivationToken>>, |
| 280 |
) -> Self { |
| 281 |
self.options.activation_token = activation_token.into(); |
| 282 |
self |
| 283 |
} |
| 284 |
|
| 285 |
#[must_use] |
| 286 |
/// Sets a connection to use other than the internal one. |
| 287 |
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self { |
| 288 |
self.connection = connection; |
| 289 |
self |
| 290 |
} |
| 291 |
|
| 292 |
/// Send the request for a file. |
| 293 |
pub async fn send_file(self, file: &impl AsFd) -> Result<Request<()>, Error> { |
| 294 |
let proxy = if let Some(connection) = self.connection { |
| 295 |
OpenURIProxy::with_connection(connection).await? |
| 296 |
} else { |
| 297 |
OpenURIProxy::new().await? |
| 298 |
}; |
| 299 |
proxy |
| 300 |
.open_file(self.identifier.as_ref(), file, self.options) |
| 301 |
.await |
| 302 |
} |
| 303 |
|
| 304 |
/// Send the request for a URI. |
| 305 |
pub async fn send_uri(self, uri: &Uri) -> Result<Request<()>, Error> { |
| 306 |
let proxy = if let Some(connection) = self.connection { |
| 307 |
OpenURIProxy::with_connection(connection).await? |
| 308 |
} else { |
| 309 |
OpenURIProxy::new().await? |
| 310 |
}; |
| 311 |
proxy |
| 312 |
.open_uri(self.identifier.as_ref(), uri, self.options) |
| 313 |
.await |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
#[derive(Debug, Default)] |
| 318 |
#[doc(alias = "xdp_portal_open_directory")] |
| 319 |
#[doc(alias = "org.freedesktop.portal.OpenURI")] |
| 320 |
/// A [builder-pattern] type to open a directory. |
| 321 |
/// |
| 322 |
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html |
| 323 |
pub struct OpenDirectoryRequest { |
| 324 |
identifier: Option<WindowIdentifier>, |
| 325 |
options: OpenDirOptions, |
| 326 |
connection: Option<zbus::Connection>, |
| 327 |
} |
| 328 |
|
| 329 |
impl OpenDirectoryRequest { |
| 330 |
#[must_use] |
| 331 |
/// Sets a window identifier. |
| 332 |
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self { |
| 333 |
self.identifier = identifier.into(); |
| 334 |
self |
| 335 |
} |
| 336 |
|
| 337 |
/// Sets the token that can be used to activate the chosen application. |
| 338 |
#[must_use] |
| 339 |
pub fn activation_token( |
| 340 |
mut self, |
| 341 |
activation_token: impl Into<Option<ActivationToken>>, |
| 342 |
) -> Self { |
| 343 |
self.options.activation_token = activation_token.into(); |
| 344 |
self |
| 345 |
} |
| 346 |
|
| 347 |
#[must_use] |
| 348 |
/// Sets a connection to use other than the internal one. |
| 349 |
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self { |
| 350 |
self.connection = connection; |
| 351 |
self |
| 352 |
} |
| 353 |
|
| 354 |
/// Send the request. |
| 355 |
pub async fn send(self, directory: &impl AsFd) -> Result<Request<()>, Error> { |
| 356 |
let proxy = if let Some(connection) = self.connection { |
| 357 |
OpenURIProxy::with_connection(connection).await? |
| 358 |
} else { |
| 359 |
OpenURIProxy::new().await? |
| 360 |
}; |
| 361 |
proxy |
| 362 |
.open_directory(self.identifier.as_ref(), directory, self.options) |
| 363 |
.await |
| 364 |
} |
| 365 |
} |