Skip to content

Commit

Permalink
Add API for setting host apps AppID
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Jan 29, 2025
1 parent 730323d commit 12b9bbe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ mod window_identifier;

pub use self::{activation_token::ActivationToken, window_identifier::WindowIdentifier};
mod app_id;
pub use self::app_id::AppID;
mod registry;
pub use self::{app_id::AppID, registry::register_host_app};
mod file_path;
pub use self::file_path::FilePath;

Expand Down
48 changes: 48 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use zbus::zvariant;

use crate::{proxy::Proxy, AppID, Error};

#[derive(Debug, zvariant::SerializeDict, zvariant::Type)]
#[zvariant(signature = "dict")]
struct RegisterOptions {}

struct RegistryProxy<'a>(Proxy<'a>);

impl<'a> RegistryProxy<'a> {
pub async fn new() -> Result<RegistryProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.host.portal.Registry").await?;
Ok(Self(proxy))
}

pub async fn register(&self, app_id: AppID) -> Result<(), Error> {
let options = RegisterOptions {};
self.0.call_method("Register", &(&app_id, &options)).await?;
Ok(())
}
}

impl<'a> std::ops::Deref for RegistryProxy<'a> {
type Target = zbus::Proxy<'a>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
/// Registers a host application for portal usage.
///
/// Portals rely on the application ID to store and manage the permissions of
/// individual apps. However, for non-sandboxed (host) applications, this
/// information cannot be directly retrieved. To resolve this, the method first
/// verifies that the application is not sandboxed. It then uses the
/// `org.freedesktop.host.portal.Registry` interface to register the process
/// communicating over DBus with the portal as the owner of the specified
/// application ID.
/// For more technical details, see <https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.host.portal.Registry.html>
pub async fn register_host_app(app_id: AppID) -> crate::Result<()> {
if crate::is_sandboxed().await {
return Ok(());
}
let proxy = RegistryProxy::new().await?;
proxy.register(app_id).await?;
Ok(())
}

0 comments on commit 12b9bbe

Please sign in to comment.