Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting activation_token in supported portals #219

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/activation_token/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::ops::Deref;

use serde::{Deserialize, Serialize};
use zbus::zvariant::Type;

/// A token that can be used to activate an application.
///
/// No guarantees are made for the token structure.
#[derive(Debug, Deserialize, Serialize, Type, PartialEq, Eq, Hash, Clone)]
pub struct ActivationToken(String);

impl From<String> for ActivationToken {
fn from(value: String) -> Self {
Self(value)
}
}

impl From<&str> for ActivationToken {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}

impl From<ActivationToken> for String {
fn from(value: ActivationToken) -> String {
value.0
}
}

impl Deref for ActivationToken {
type Target = str;

fn deref(&self) -> &Self::Target {
self.0.as_str()
}
}

impl AsRef<str> for ActivationToken {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}

impl std::fmt::Display for ActivationToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_ref())
}
}
25 changes: 21 additions & 4 deletions src/desktop/dynamic_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{self, DeserializeDict, OwnedValue, SerializeDict, Type, Value};

use super::{HandleToken, Icon, Request};
use crate::{proxy::Proxy, Error, WindowIdentifier};
use crate::{proxy::Proxy, ActivationToken, Error, WindowIdentifier};

#[bitflags]
#[derive(Default, Serialize_repr, Deserialize_repr, PartialEq, Eq, Debug, Copy, Clone, Type)]
Expand Down Expand Up @@ -194,6 +194,25 @@ impl std::fmt::Debug for PrepareInstallResponse {
}
}

#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
/// Options to pass to [`DynamicLauncherProxy::launch`]
pub struct LaunchOptions {
activation_token: Option<ActivationToken>,
}

impl LaunchOptions {
/// Sets the token that can be used to activate the chosen application.
#[must_use]
pub fn activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.activation_token = activation_token.into();
self
}
}

#[derive(Debug)]
/// Wrong type of [`crate::desktop::Icon`] was used.
pub struct UnexpectedIconError;
Expand Down Expand Up @@ -321,9 +340,7 @@ impl<'a> DynamicLauncherProxy<'a> {
/// See also [`Launch`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.DynamicLauncher.html#org-freedesktop-portal-dynamiclauncher-launch).
#[doc(alias = "Launch")]
#[doc(alias = "xdp_portal_dynamic_launcher_launch")]
pub async fn launch(&self, desktop_file_id: &str) -> Result<(), Error> {
// TODO: handle activation_token
let options: HashMap<&str, zvariant::Value<'_>> = HashMap::new();
pub async fn launch(&self, desktop_file_id: &str, options: LaunchOptions) -> Result<(), Error> {
self.0.call("Launch", &(desktop_file_id, &options)).await
}

Expand Down
15 changes: 8 additions & 7 deletions src/desktop/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use serde::Serialize;
use zbus::zvariant::{self, SerializeDict, Type};

use super::{HandleToken, Request};
use crate::{proxy::Proxy, Error, WindowIdentifier};
use crate::{proxy::Proxy, ActivationToken, Error, WindowIdentifier};

#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
Expand All @@ -43,8 +43,7 @@ struct EmailOptions {
subject: Option<String>,
body: Option<String>,
attachment_fds: Option<Vec<zvariant::OwnedFd>>,
// TODO Expose activation_token in the api
activation_token: Option<String>,
activation_token: Option<ActivationToken>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -178,11 +177,13 @@ impl EmailRequest {
}

// TODO Added in version 4 of the interface.
apricotbucket28 marked this conversation as resolved.
Show resolved Hide resolved
/// Sets the activation token.
#[allow(dead_code)]
/// Sets the token that can be used to activate the chosen application.
#[must_use]
fn activation_token<'a>(mut self, activation_token: impl Into<Option<&'a str>>) -> Self {
self.options.activation_token = activation_token.into().map(ToOwned::to_owned);
pub fn activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.options.activation_token = activation_token.into();
self
}

Expand Down
26 changes: 23 additions & 3 deletions src/desktop/open_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ use url::Url;
use zbus::zvariant::{Fd, SerializeDict, Type};

use super::{HandleToken, Request};
use crate::{proxy::Proxy, Error, WindowIdentifier};
use crate::{proxy::Proxy, ActivationToken, Error, WindowIdentifier};

#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct OpenDirOptions {
handle_token: HandleToken,
activation_token: Option<String>,
activation_token: Option<ActivationToken>,
}

#[derive(SerializeDict, Type, Debug, Default)]
Expand All @@ -72,7 +72,7 @@ struct OpenFileOptions {
handle_token: HandleToken,
writeable: Option<bool>,
ask: Option<bool>,
activation_token: Option<String>,
activation_token: Option<ActivationToken>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -171,6 +171,16 @@ impl OpenFileRequest {
self
}

/// Sets the token that can be used to activate the chosen application.
apricotbucket28 marked this conversation as resolved.
Show resolved Hide resolved
#[must_use]
pub fn activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.options.activation_token = activation_token.into();
self
}

/// Send the request for a file.
pub async fn send_file(self, file: &BorrowedFd<'_>) -> Result<Request<()>, Error> {
let proxy = OpenURIProxy::new().await?;
Expand Down Expand Up @@ -203,6 +213,16 @@ impl OpenDirectoryRequest {
self
}

/// Sets the token that can be used to activate the chosen application.
#[must_use]
pub fn activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.options.activation_token = activation_token.into();
self
}

/// Send the request.
pub async fn send(self, directory: &BorrowedFd<'_>) -> Result<Request<()>, Error> {
let proxy = OpenURIProxy::new().await?;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub type Result<T> = std::result::Result<T, Error>;

static IS_SANDBOXED: OnceLock<bool> = OnceLock::new();

mod activation_token;
/// Interact with the user's desktop such as taking a screenshot, setting a
/// background or querying the user's location.
pub mod desktop;
Expand All @@ -22,6 +23,7 @@ pub mod documents;
mod error;
mod window_identifier;

pub use self::activation_token::ActivationToken;
pub use self::window_identifier::WindowIdentifier;
mod app_id;
pub use self::app_id::AppID;
Expand Down
Loading