| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
ActivationToken, MaybeAppID, PortalError, Uri, WindowIdentifierType, |
| 8 |
backend::request::{Request, RequestImpl}, |
| 9 |
desktop::{HandleToken, Response}, |
| 10 |
zvariant::{ |
| 11 |
Optional, OwnedObjectPath, Type, |
| 12 |
as_value::{self, optional}, |
| 13 |
}, |
| 14 |
}; |
| 15 |
|
| 16 |
#[derive(Debug, Deserialize, Type)] |
| 17 |
#[zvariant(signature = "dict")] |
| 18 |
pub struct ChooserOptions { |
| 19 |
#[serde(default, with = "optional")] |
| 20 |
last_choice: Option<MaybeAppID>, |
| 21 |
#[serde(default, with = "optional")] |
| 22 |
modal: Option<bool>, |
| 23 |
#[serde(default, with = "optional")] |
| 24 |
content_type: Option<String>, |
| 25 |
#[serde(default, with = "optional")] |
| 26 |
uri: Option<Uri>, |
| 27 |
#[serde(default, with = "optional")] |
| 28 |
filename: Option<String>, |
| 29 |
#[serde(default, with = "optional")] |
| 30 |
activation_token: Option<ActivationToken>, |
| 31 |
} |
| 32 |
|
| 33 |
impl ChooserOptions { |
| 34 |
pub fn last_choice(&self) -> Option<&MaybeAppID> { |
| 35 |
self.last_choice.as_ref() |
| 36 |
} |
| 37 |
|
| 38 |
pub fn modal(&self) -> Option<bool> { |
| 39 |
self.modal |
| 40 |
} |
| 41 |
|
| 42 |
pub fn content_type(&self) -> Option<&str> { |
| 43 |
self.content_type.as_deref() |
| 44 |
} |
| 45 |
|
| 46 |
pub fn uri(&self) -> Option<&Uri> { |
| 47 |
self.uri.as_ref() |
| 48 |
} |
| 49 |
|
| 50 |
pub fn filename(&self) -> Option<&str> { |
| 51 |
self.filename.as_deref() |
| 52 |
} |
| 53 |
|
| 54 |
pub fn activation_token(&self) -> Option<&ActivationToken> { |
| 55 |
self.activation_token.as_ref() |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[derive(Debug, Serialize, Type)] |
| 60 |
#[zvariant(signature = "dict")] |
| 61 |
pub struct Choice { |
| 62 |
#[serde(with = "as_value")] |
| 63 |
choice: MaybeAppID, |
| 64 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 65 |
activation_token: Option<ActivationToken>, |
| 66 |
} |
| 67 |
|
| 68 |
impl Choice { |
| 69 |
pub fn new(choice: MaybeAppID) -> Self { |
| 70 |
Self { |
| 71 |
choice, |
| 72 |
activation_token: None, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[must_use] |
| 77 |
pub fn activation_token( |
| 78 |
mut self, |
| 79 |
activation_token: impl Into<Option<ActivationToken>>, |
| 80 |
) -> Self { |
| 81 |
self.activation_token = activation_token.into(); |
| 82 |
self |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[async_trait] |
| 87 |
pub trait AppChooserImpl: RequestImpl { |
| 88 |
#[doc(alias = "ChooseApplication")] |
| 89 |
async fn choose_application( |
| 90 |
&self, |
| 91 |
token: HandleToken, |
| 92 |
app_id: Option<MaybeAppID>, |
| 93 |
parent_window: Option<WindowIdentifierType>, |
| 94 |
choices: Vec<MaybeAppID>, |
| 95 |
options: ChooserOptions, |
| 96 |
) -> Result<Choice, PortalError>; |
| 97 |
|
| 98 |
#[doc(alias = "UpdateChoices")] |
| 99 |
async fn update_choices( |
| 100 |
&self, |
| 101 |
token: HandleToken, |
| 102 |
choices: Vec<MaybeAppID>, |
| 103 |
) -> Result<(), PortalError>; |
| 104 |
} |
| 105 |
|
| 106 |
pub(crate) struct AppChooserInterface { |
| 107 |
imp: Arc<dyn AppChooserImpl>, |
| 108 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 109 |
cnx: zbus::Connection, |
| 110 |
} |
| 111 |
|
| 112 |
impl AppChooserInterface { |
| 113 |
pub fn new( |
| 114 |
imp: Arc<dyn AppChooserImpl>, |
| 115 |
cnx: zbus::Connection, |
| 116 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 117 |
) -> Self { |
| 118 |
Self { imp, cnx, spawn } |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#[zbus::interface(name = "org.freedesktop.impl.portal.AppChooser")] |
| 123 |
impl AppChooserInterface { |
| 124 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 125 |
fn version(&self) -> u32 { |
| 126 |
2 |
| 127 |
} |
| 128 |
|
| 129 |
#[zbus(out_args("response", "results"))] |
| 130 |
async fn choose_application( |
| 131 |
&self, |
| 132 |
handle: OwnedObjectPath, |
| 133 |
app_id: Optional<MaybeAppID>, |
| 134 |
parent_window: Optional<WindowIdentifierType>, |
| 135 |
choices: Vec<MaybeAppID>, |
| 136 |
options: ChooserOptions, |
| 137 |
) -> Result<Response<Choice>, PortalError> { |
| 138 |
let imp = Arc::clone(&self.imp); |
| 139 |
|
| 140 |
Request::spawn( |
| 141 |
"AppChooser::ChooseApplication", |
| 142 |
&self.cnx, |
| 143 |
handle.clone(), |
| 144 |
Arc::clone(&self.imp), |
| 145 |
Arc::clone(&self.spawn), |
| 146 |
async move { |
| 147 |
imp.choose_application( |
| 148 |
HandleToken::try_from(&handle).unwrap(), |
| 149 |
app_id.into(), |
| 150 |
parent_window.into(), |
| 151 |
choices, |
| 152 |
options, |
| 153 |
) |
| 154 |
.await |
| 155 |
}, |
| 156 |
) |
| 157 |
.await |
| 158 |
} |
| 159 |
|
| 160 |
async fn update_choices( |
| 161 |
&self, |
| 162 |
handle: OwnedObjectPath, |
| 163 |
choices: Vec<MaybeAppID>, |
| 164 |
) -> Result<(), PortalError> { |
| 165 |
#[cfg(feature = "tracing")] |
| 166 |
tracing::debug!("AppChooser::UpdateChoices"); |
| 167 |
|
| 168 |
let token = HandleToken::try_from(&handle).unwrap(); |
| 169 |
let response = self.imp.update_choices(token, choices).await; |
| 170 |
|
| 171 |
#[cfg(feature = "tracing")] |
| 172 |
tracing::debug!("AppChooser::UpdateChoices returned {:#?}", response); |
| 173 |
response |
| 174 |
} |
| 175 |
} |