| 1 |
//! Install launchers like Web Application from your browser or Steam. |
| 2 |
//! |
| 3 |
//! # Examples |
| 4 |
//! |
| 5 |
//! ```rust,no_run |
| 6 |
//! use std::io::Read; |
| 7 |
//! use ashpd::{ |
| 8 |
//! desktop::{ |
| 9 |
//! dynamic_launcher::DynamicLauncherProxy, |
| 10 |
//! Icon, |
| 11 |
//! }, |
| 12 |
//! WindowIdentifier, |
| 13 |
//! }; |
| 14 |
//! |
| 15 |
//! async fn run() -> ashpd::Result<()> { |
| 16 |
//! let proxy = DynamicLauncherProxy::new().await?; |
| 17 |
//! |
| 18 |
//! let filename = "/home/bilalelmoussaoui/Projects/ashpd/ashpd-demo/data/icons/com.belmoussaoui.ashpd.demo.svg"; |
| 19 |
//! let mut f = std::fs::File::open(&filename).expect("no file found"); |
| 20 |
//! let metadata = std::fs::metadata(&filename).expect("unable to read metadata"); |
| 21 |
//! let mut buffer = vec![0; metadata.len() as usize]; |
| 22 |
//! f.read(&mut buffer).expect("buffer overflow"); |
| 23 |
//! |
| 24 |
//! let icon = Icon::Bytes(buffer); |
| 25 |
//! let response = proxy |
| 26 |
//! .prepare_install( |
| 27 |
//! None, |
| 28 |
//! "SomeApp", |
| 29 |
//! icon, |
| 30 |
//! Default::default() |
| 31 |
//! ) |
| 32 |
//! .await? |
| 33 |
//! .response()?; |
| 34 |
//! let token = response.token(); |
| 35 |
//! |
| 36 |
//! |
| 37 |
//! // Name and Icon will be overwritten from what we provided above |
| 38 |
//! // Exec will be overridden to call `flatpak run our-app` if the application is sandboxed |
| 39 |
//! let desktop_entry = r#" |
| 40 |
//! [Desktop Entry] |
| 41 |
//! Comment=My Web App |
| 42 |
//! Type=Application |
| 43 |
//! "#; |
| 44 |
//! proxy |
| 45 |
//! .install(&token, "some_file.desktop", desktop_entry, Default::default()) |
| 46 |
//! .await?; |
| 47 |
//! |
| 48 |
//! proxy.uninstall("some_file.desktop", Default::default()).await?; |
| 49 |
//! Ok(()) |
| 50 |
//! } |
| 51 |
//! ``` |
| 52 |
|
| 53 |
use enumflags2::{BitFlags, bitflags}; |
| 54 |
use serde::{Deserialize, Serialize}; |
| 55 |
use serde_repr::{Deserialize_repr, Serialize_repr}; |
| 56 |
use zbus::zvariant::{ |
| 57 |
self, Optional, OwnedValue, Type, Value, |
| 58 |
as_value::{self, optional}, |
| 59 |
}; |
| 60 |
|
| 61 |
use super::{HandleToken, Icon, Request}; |
| 62 |
use crate::{ActivationToken, Error, WindowIdentifier, proxy::Proxy}; |
| 63 |
|
| 64 |
#[bitflags] |
| 65 |
#[derive(Default, Serialize_repr, Deserialize_repr, PartialEq, Eq, Debug, Copy, Clone, Type)] |
| 66 |
#[repr(u32)] |
| 67 |
#[doc(alias = "XdpLauncherType")] |
| 68 |
/// The type of the launcher. |
| 69 |
pub enum LauncherType { |
| 70 |
#[doc(alias = "XDP_LAUNCHER_APPLICATION")] |
| 71 |
#[default] |
| 72 |
/// A launcher that represents an application |
| 73 |
Application, |
| 74 |
#[doc(alias = "XDP_LAUNCHER_WEBAPP")] |
| 75 |
/// A launcher that represents a web application |
| 76 |
WebApplication, |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg_attr(feature = "glib", derive(glib::Enum))] |
| 80 |
#[cfg_attr(feature = "glib", enum_type(name = "AshpdIconType"))] |
| 81 |
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Type)] |
| 82 |
#[zvariant(signature = "s")] |
| 83 |
#[serde(rename_all = "lowercase")] |
| 84 |
/// The icon format. |
| 85 |
pub enum IconType { |
| 86 |
/// PNG. |
| 87 |
Png, |
| 88 |
/// JPEG. |
| 89 |
Jpeg, |
| 90 |
/// SVG. |
| 91 |
Svg, |
| 92 |
} |
| 93 |
|
| 94 |
#[derive(Debug, Deserialize, Type)] |
| 95 |
#[zvariant(signature = "(vsu)")] |
| 96 |
/// The icon of the launcher. |
| 97 |
pub struct LauncherIcon(zvariant::OwnedValue, IconType, u32); |
| 98 |
|
| 99 |
impl LauncherIcon { |
| 100 |
/// The actual icon. |
| 101 |
pub fn icon(&self) -> Icon { |
| 102 |
Icon::try_from(&self.0).unwrap() |
| 103 |
} |
| 104 |
|
| 105 |
/// The icon type. |
| 106 |
pub fn type_(&self) -> IconType { |
| 107 |
self.1 |
| 108 |
} |
| 109 |
|
| 110 |
/// The icon size. |
| 111 |
pub fn size(&self) -> u32 { |
| 112 |
self.2 |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[derive(Serialize, Type, Debug, Default)] |
| 117 |
#[zvariant(signature = "dict")] |
| 118 |
/// Options to pass to [`DynamicLauncherProxy::prepare_install`] |
| 119 |
pub struct PrepareInstallOptions { |
| 120 |
#[serde(with = "as_value")] |
| 121 |
handle_token: HandleToken, |
| 122 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 123 |
modal: Option<bool>, |
| 124 |
#[serde(with = "as_value")] |
| 125 |
launcher_type: LauncherType, |
| 126 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 127 |
target: Option<String>, |
| 128 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 129 |
editable_name: Option<bool>, |
| 130 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 131 |
editable_icon: Option<bool>, |
| 132 |
} |
| 133 |
|
| 134 |
impl PrepareInstallOptions { |
| 135 |
/// Sets whether the dialog should be a modal. |
| 136 |
pub fn set_modal(mut self, modal: impl Into<Option<bool>>) -> Self { |
| 137 |
self.modal = modal.into(); |
| 138 |
self |
| 139 |
} |
| 140 |
|
| 141 |
/// Sets the launcher type. |
| 142 |
pub fn set_launcher_type(mut self, launcher_type: LauncherType) -> Self { |
| 143 |
self.launcher_type = launcher_type; |
| 144 |
self |
| 145 |
} |
| 146 |
|
| 147 |
/// The URL for a [`LauncherType::WebApplication`] otherwise it is not |
| 148 |
/// needed. |
| 149 |
pub fn set_target<'a>(mut self, target: impl Into<Option<&'a str>>) -> Self { |
| 150 |
self.target = target.into().map(ToOwned::to_owned); |
| 151 |
self |
| 152 |
} |
| 153 |
|
| 154 |
/// Sets whether the name should be editable. |
| 155 |
pub fn set_editable_name(mut self, editable_name: impl Into<Option<bool>>) -> Self { |
| 156 |
self.editable_name = editable_name.into(); |
| 157 |
self |
| 158 |
} |
| 159 |
|
| 160 |
/// Sets whether the icon should be editable. |
| 161 |
pub fn set_editable_icon(mut self, editable_icon: impl Into<Option<bool>>) -> Self { |
| 162 |
self.editable_icon = editable_icon.into(); |
| 163 |
self |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
#[derive(Deserialize, Type)] |
| 168 |
#[zvariant(signature = "dict")] |
| 169 |
/// A response of [`DynamicLauncherProxy::prepare_install`] |
| 170 |
pub struct PrepareInstallResponse { |
| 171 |
#[serde(with = "as_value")] |
| 172 |
name: String, |
| 173 |
#[serde(with = "as_value")] |
| 174 |
icon: OwnedValue, |
| 175 |
#[serde(with = "as_value")] |
| 176 |
token: String, |
| 177 |
} |
| 178 |
|
| 179 |
impl PrepareInstallResponse { |
| 180 |
/// The user defined name or a predefined one |
| 181 |
pub fn name(&self) -> &str { |
| 182 |
&self.name |
| 183 |
} |
| 184 |
|
| 185 |
/// A token to pass to [`DynamicLauncherProxy::install`] |
| 186 |
pub fn token(&self) -> &str { |
| 187 |
&self.token |
| 188 |
} |
| 189 |
|
| 190 |
/// The user selected icon or a predefined one |
| 191 |
pub fn icon(&self) -> Icon { |
| 192 |
let inner = self.icon.downcast_ref::<Value>().unwrap(); |
| 193 |
Icon::try_from(inner).unwrap() |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
impl std::fmt::Debug for PrepareInstallResponse { |
| 198 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 199 |
f.debug_struct("PrepareInstallResponse") |
| 200 |
.field("name", &self.name()) |
| 201 |
.field("icon", &self.icon()) |
| 202 |
.field("token", &self.token()) |
| 203 |
.finish() |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
#[derive(Serialize, Type, Debug, Default)] |
| 208 |
#[zvariant(signature = "dict")] |
| 209 |
/// Options to pass to [`DynamicLauncherProxy::launch`] |
| 210 |
pub struct LaunchOptions { |
| 211 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 212 |
activation_token: Option<ActivationToken>, |
| 213 |
} |
| 214 |
|
| 215 |
impl LaunchOptions { |
| 216 |
/// Sets the token that can be used to activate the chosen application. |
| 217 |
#[must_use] |
| 218 |
pub fn activation_token( |
| 219 |
mut self, |
| 220 |
activation_token: impl Into<Option<ActivationToken>>, |
| 221 |
) -> Self { |
| 222 |
self.activation_token = activation_token.into(); |
| 223 |
self |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
#[derive(Serialize, Type, Debug, Default)] |
| 228 |
/// Specified options for a [`DynamicLauncherProxy::install`] request. |
| 229 |
#[zvariant(signature = "dict")] |
| 230 |
pub struct InstallOptions {} |
| 231 |
|
| 232 |
#[derive(Serialize, Type, Debug, Default)] |
| 233 |
/// Specified options for a [`DynamicLauncherProxy::uninstall`] request. |
| 234 |
#[zvariant(signature = "dict")] |
| 235 |
pub struct UninstallOptions {} |
| 236 |
|
| 237 |
#[derive(Serialize, Type, Debug, Default)] |
| 238 |
/// Specified options for a [`DynamicLauncherProxy::request_install_token`] |
| 239 |
/// request. |
| 240 |
#[zvariant(signature = "dict")] |
| 241 |
pub struct RequestInstallTokenOptions {} |
| 242 |
|
| 243 |
#[derive(Debug)] |
| 244 |
/// Wrong type of [`crate::desktop::Icon`] was used. |
| 245 |
pub struct UnexpectedIconError; |
| 246 |
|
| 247 |
impl std::error::Error for UnexpectedIconError {} |
| 248 |
impl std::fmt::Display for UnexpectedIconError { |
| 249 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 250 |
f.write_str("Unexpected icon type. Only Icon::Bytes is supported") |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/// The interface lets sandboxed applications install launchers like Web |
| 255 |
/// Application from your browser or Steam. |
| 256 |
/// |
| 257 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.DynamicLauncher`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html). |
| 258 |
#[derive(Debug)] |
| 259 |
#[doc(alias = "org.freedesktop.portal.DynamicLauncher")] |
| 260 |
pub struct DynamicLauncherProxy(Proxy<'static>); |
| 261 |
|
| 262 |
impl DynamicLauncherProxy { |
| 263 |
/// Create a new instance of [`DynamicLauncherProxy`]. |
| 264 |
pub async fn new() -> Result<Self, Error> { |
| 265 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.DynamicLauncher").await?; |
| 266 |
Ok(Self(proxy)) |
| 267 |
} |
| 268 |
|
| 269 |
/// Create a new instance of [`DynamicLauncherProxy`]. |
| 270 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> { |
| 271 |
let proxy = Proxy::new_desktop_with_connection( |
| 272 |
connection, |
| 273 |
"org.freedesktop.portal.DynamicLauncher", |
| 274 |
) |
| 275 |
.await?; |
| 276 |
Ok(Self(proxy)) |
| 277 |
} |
| 278 |
|
| 279 |
/// Returns the version of the portal interface. |
| 280 |
pub fn version(&self) -> u32 { |
| 281 |
self.0.version() |
| 282 |
} |
| 283 |
|
| 284 |
/// *Note* Only `Icon::Bytes` is accepted. |
| 285 |
/// |
| 286 |
/// # Specifications |
| 287 |
/// |
| 288 |
/// See also [`PrepareInstall`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-prepareinstall). |
| 289 |
#[doc(alias = "PrepareInstall")] |
| 290 |
#[doc(alias = "xdp_portal_dynamic_launcher_prepare_install")] |
| 291 |
#[doc(alias = "xdp_portal_dynamic_launcher_prepare_install_finish")] |
| 292 |
pub async fn prepare_install( |
| 293 |
&self, |
| 294 |
identifier: Option<&WindowIdentifier>, |
| 295 |
name: &str, |
| 296 |
icon: Icon, |
| 297 |
options: PrepareInstallOptions, |
| 298 |
) -> Result<Request<PrepareInstallResponse>, Error> { |
| 299 |
if !icon.is_bytes() { |
| 300 |
return Err(UnexpectedIconError {}.into()); |
| 301 |
} |
| 302 |
let identifier = Optional::from(identifier); |
| 303 |
self.0 |
| 304 |
.request( |
| 305 |
&options.handle_token, |
| 306 |
"PrepareInstall", |
| 307 |
&(identifier, name, icon.as_value(), &options), |
| 308 |
) |
| 309 |
.await |
| 310 |
} |
| 311 |
|
| 312 |
/// *Note* Only `Icon::Bytes` is accepted. |
| 313 |
/// |
| 314 |
/// # Specifications |
| 315 |
/// |
| 316 |
/// See also [`RequestInstallToken`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-requestinstalltoken). |
| 317 |
#[doc(alias = "RequestInstallToken")] |
| 318 |
#[doc(alias = "xdp_portal_dynamic_launcher_request_install_token")] |
| 319 |
pub async fn request_install_token( |
| 320 |
&self, |
| 321 |
name: &str, |
| 322 |
icon: Icon, |
| 323 |
options: RequestInstallTokenOptions, |
| 324 |
) -> Result<String, Error> { |
| 325 |
if !icon.is_bytes() { |
| 326 |
return Err(UnexpectedIconError {}.into()); |
| 327 |
} |
| 328 |
|
| 329 |
self.0 |
| 330 |
.call::<String>("RequestInstallToken", &(name, icon.as_value(), options)) |
| 331 |
.await |
| 332 |
} |
| 333 |
|
| 334 |
/// # Specifications |
| 335 |
/// |
| 336 |
/// See also [`Install`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-install). |
| 337 |
#[doc(alias = "Install")] |
| 338 |
#[doc(alias = "xdp_portal_dynamic_launcher_install")] |
| 339 |
pub async fn install( |
| 340 |
&self, |
| 341 |
token: &str, |
| 342 |
desktop_file_id: &str, |
| 343 |
desktop_entry: &str, |
| 344 |
options: InstallOptions, |
| 345 |
) -> Result<(), Error> { |
| 346 |
self.0 |
| 347 |
.call::<()>("Install", &(token, desktop_file_id, desktop_entry, options)) |
| 348 |
.await |
| 349 |
} |
| 350 |
|
| 351 |
/// # Specifications |
| 352 |
/// |
| 353 |
/// See also [`Uninstall`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-uninstall). |
| 354 |
#[doc(alias = "Uninstall")] |
| 355 |
#[doc(alias = "xdp_portal_dynamic_launcher_uninstall")] |
| 356 |
pub async fn uninstall( |
| 357 |
&self, |
| 358 |
desktop_file_id: &str, |
| 359 |
options: UninstallOptions, |
| 360 |
) -> Result<(), Error> { |
| 361 |
self.0 |
| 362 |
.call::<()>("Uninstall", &(desktop_file_id, options)) |
| 363 |
.await |
| 364 |
} |
| 365 |
|
| 366 |
/// # Specifications |
| 367 |
/// |
| 368 |
/// See also [`GetDesktopEntry`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-getdesktopentry). |
| 369 |
#[doc(alias = "GetDesktopEntry")] |
| 370 |
#[doc(alias = "xdp_portal_dynamic_launcher_get_desktop_entry")] |
| 371 |
pub async fn desktop_entry(&self, desktop_file_id: &str) -> Result<String, Error> { |
| 372 |
self.0.call("GetDesktopEntry", &(desktop_file_id)).await |
| 373 |
} |
| 374 |
|
| 375 |
/// # Specifications |
| 376 |
/// |
| 377 |
/// See also [`GetIcon`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-geticon). |
| 378 |
#[doc(alias = "GetIcon")] |
| 379 |
#[doc(alias = "xdp_portal_dynamic_launcher_get_icon")] |
| 380 |
pub async fn icon(&self, desktop_file_id: &str) -> Result<LauncherIcon, Error> { |
| 381 |
self.0.call("GetIcon", &(desktop_file_id)).await |
| 382 |
} |
| 383 |
|
| 384 |
/// # Specifications |
| 385 |
/// |
| 386 |
/// See also [`Launch`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-launch). |
| 387 |
#[doc(alias = "Launch")] |
| 388 |
#[doc(alias = "xdp_portal_dynamic_launcher_launch")] |
| 389 |
pub async fn launch(&self, desktop_file_id: &str, options: LaunchOptions) -> Result<(), Error> { |
| 390 |
self.0.call("Launch", &(desktop_file_id, &options)).await |
| 391 |
} |
| 392 |
|
| 393 |
/// # Specifications |
| 394 |
/// |
| 395 |
/// See also [`SupportedLauncherTypes`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-supportedlaunchertypes). |
| 396 |
#[doc(alias = "SupportedLauncherTypes")] |
| 397 |
pub async fn supported_launcher_types(&self) -> Result<BitFlags<LauncherType>, Error> { |
| 398 |
self.0 |
| 399 |
.property::<BitFlags<LauncherType>>("SupportedLauncherTypes") |
| 400 |
.await |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
impl std::ops::Deref for DynamicLauncherProxy { |
| 405 |
type Target = zbus::Proxy<'static>; |
| 406 |
|
| 407 |
fn deref(&self) -> &Self::Target { |
| 408 |
&self.0 |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
#[cfg(test)] |
| 413 |
mod test { |
| 414 |
use super::*; |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn test_icon_signature() { |
| 418 |
assert_eq!(LauncherIcon::SIGNATURE, "(vsu)"); |
| 419 |
|
| 420 |
let icon = vec![IconType::Png]; |
| 421 |
assert_eq!(serde_json::to_string(&icon).unwrap(), "[\"png\"]"); |
| 422 |
} |
| 423 |
} |