ashpd/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(rustdoc::broken_intra_doc_links)]
3#![deny(missing_docs)]
4#![doc(
5    html_logo_url = "https://raw.githubusercontent.com/bilelmoussaoui/ashpd/master/ashpd-demo/data/icons/com.belmoussaoui.ashpd.demo.svg",
6    html_favicon_url = "https://raw.githubusercontent.com/bilelmoussaoui/ashpd/master/ashpd-demo/data/icons/com.belmoussaoui.ashpd.demo-symbolic.svg"
7)]
8#![doc = include_str!("../README.md")]
9#[cfg(all(all(feature = "tokio", feature = "async-std"), not(doc)))]
10compile_error!("You can't enable both async-std & tokio features at once");
11#[cfg(all(not(feature = "tokio"), not(feature = "async-std"), not(doc)))]
12compile_error!("Either the `async-std` or the `tokio` feature has to be enabled");
13
14/// Alias for a [`Result`] with the error type `ashpd::Error`.
15pub type Result<T> = std::result::Result<T, Error>;
16
17static IS_SANDBOXED: OnceLock<bool> = OnceLock::new();
18
19mod activation_token;
20/// Interact with the user's desktop such as taking a screenshot, setting a
21/// background or querying the user's location.
22pub mod desktop;
23/// Interact with the documents store or transfer files across apps.
24pub mod documents;
25mod error;
26mod window_identifier;
27
28pub use self::{activation_token::ActivationToken, window_identifier::WindowIdentifier};
29mod app_id;
30mod registry;
31pub use self::{app_id::AppID, registry::register_host_app};
32mod file_path;
33pub use self::file_path::FilePath;
34
35mod proxy;
36
37#[cfg(feature = "backend")]
38#[cfg_attr(docsrs, doc(cfg(feature = "backend")))]
39pub use self::window_identifier::WindowIdentifierType;
40#[cfg(feature = "backend")]
41#[cfg_attr(docsrs, doc(cfg(feature = "backend")))]
42#[allow(missing_docs)]
43/// Build your custom portals backend.
44pub mod backend;
45/// Spawn commands outside the sandbox or monitor if the running application has
46/// received an update & install it.
47pub mod flatpak;
48mod helpers;
49use std::sync::OnceLock;
50
51#[cfg(feature = "backend")]
52#[cfg_attr(docsrs, doc(cfg(feature = "backend")))]
53pub use async_trait;
54pub use enumflags2;
55pub use url;
56pub use zbus::{self, zvariant};
57
58/// Check whether the application is running inside a sandbox.
59///
60/// The function checks whether the file `/.flatpak-info` exists, or if the app
61/// is running as a snap, or if the environment variable `GTK_USE_PORTAL` is set
62/// to `1`. As the return value of this function will not change during the
63/// runtime of a program; it is cached for future calls.
64pub async fn is_sandboxed() -> bool {
65    if let Some(cached_value) = IS_SANDBOXED.get() {
66        return *cached_value;
67    }
68    let new_value = crate::helpers::is_flatpak().await
69        || crate::helpers::is_snap().await
70        || std::env::var("GTK_USE_PORTAL")
71            .map(|v| v == "1")
72            .unwrap_or(false);
73
74    *IS_SANDBOXED.get_or_init(|| new_value)
75}
76
77pub use self::error::{Error, PortalError};
78
79mod sealed {
80    /// Use as a supertrait for public traits that users should not be able to
81    /// implement
82    pub trait Sealed {}
83}
84
85pub(crate) use sealed::Sealed;
86
87/// Process ID.
88///
89/// Matches the type used in std.
90pub type Pid = u32;