| 1 |
use serde::Serialize; |
| 2 |
use zbus::zvariant; |
| 3 |
|
| 4 |
use crate::{AppID, Error, proxy::Proxy}; |
| 5 |
|
| 6 |
#[derive(Debug, Serialize, zvariant::Type, Default)] |
| 7 |
#[zvariant(signature = "dict")] |
| 8 |
struct RegisterOptions {} |
| 9 |
|
| 10 |
struct RegistryProxy(Proxy<'static>); |
| 11 |
|
| 12 |
impl RegistryProxy { |
| 13 |
pub async fn new() -> Result<Self, Error> { |
| 14 |
let proxy = Proxy::new_desktop("org.freedesktop.host.portal.Registry").await?; |
| 15 |
Ok(Self(proxy)) |
| 16 |
} |
| 17 |
|
| 18 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> { |
| 19 |
let proxy = |
| 20 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.host.portal.Registry") |
| 21 |
.await?; |
| 22 |
Ok(Self(proxy)) |
| 23 |
} |
| 24 |
|
| 25 |
pub async fn register(&self, app_id: AppID, options: RegisterOptions) -> Result<(), Error> { |
| 26 |
self.0.call_method("Register", &(&app_id, &options)).await?; |
| 27 |
Ok(()) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl std::ops::Deref for RegistryProxy { |
| 32 |
type Target = zbus::Proxy<'static>; |
| 33 |
|
| 34 |
fn deref(&self) -> &Self::Target { |
| 35 |
&self.0 |
| 36 |
} |
| 37 |
} |
| 38 |
/// Registers a host application for portal usage. |
| 39 |
/// |
| 40 |
/// Portals rely on the application ID to store and manage the permissions of |
| 41 |
/// individual apps. However, for non-sandboxed (host) applications, this |
| 42 |
/// information cannot be directly retrieved. To resolve this, the method first |
| 43 |
/// verifies that the application is not sandboxed. It then uses the |
| 44 |
/// `org.freedesktop.host.portal.Registry` interface to register the process |
| 45 |
/// communicating over DBus with the portal as the owner of the specified |
| 46 |
/// application ID. |
| 47 |
/// For more technical details, see <https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.host.portal.Registry.html> |
| 48 |
pub async fn register_host_app(app_id: AppID) -> crate::Result<()> { |
| 49 |
if crate::is_sandboxed() { |
| 50 |
return Ok(()); |
| 51 |
} |
| 52 |
let proxy = RegistryProxy::new().await?; |
| 53 |
proxy.register(app_id, Default::default()).await?; |
| 54 |
Ok(()) |
| 55 |
} |
| 56 |
|
| 57 |
/// Similar to [`register_host_app`] that takes a connection parameter. |
| 58 |
pub async fn register_host_app_with_connection( |
| 59 |
connection: zbus::Connection, |
| 60 |
app_id: AppID, |
| 61 |
) -> crate::Result<()> { |
| 62 |
if crate::is_sandboxed() { |
| 63 |
return Ok(()); |
| 64 |
} |
| 65 |
let proxy = RegistryProxy::with_connection(connection).await?; |
| 66 |
proxy.register(app_id, Default::default()).await?; |
| 67 |
Ok(()) |
| 68 |
} |