| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
MaybeAppID, WindowIdentifierType, |
| 7 |
backend::{ |
| 8 |
Result, |
| 9 |
request::{Request, RequestImpl}, |
| 10 |
}, |
| 11 |
desktop::{ |
| 12 |
HandleToken, |
| 13 |
file_chooser::{OpenFileOptions, SaveFileOptions, SaveFilesOptions, SelectedFiles}, |
| 14 |
request::Response, |
| 15 |
}, |
| 16 |
zvariant::{Optional, OwnedObjectPath}, |
| 17 |
}; |
| 18 |
|
| 19 |
#[async_trait] |
| 20 |
pub trait FileChooserImpl: RequestImpl { |
| 21 |
#[doc(alias = "OpenFile")] |
| 22 |
async fn open_file( |
| 23 |
&self, |
| 24 |
token: HandleToken, |
| 25 |
app_id: Option<MaybeAppID>, |
| 26 |
window_identifier: Option<WindowIdentifierType>, |
| 27 |
title: &str, |
| 28 |
options: OpenFileOptions, |
| 29 |
) -> Result<SelectedFiles>; |
| 30 |
|
| 31 |
#[doc(alias = "SaveFile")] |
| 32 |
async fn save_file( |
| 33 |
&self, |
| 34 |
token: HandleToken, |
| 35 |
app_id: Option<MaybeAppID>, |
| 36 |
window_identifier: Option<WindowIdentifierType>, |
| 37 |
title: &str, |
| 38 |
options: SaveFileOptions, |
| 39 |
) -> Result<SelectedFiles>; |
| 40 |
|
| 41 |
#[doc(alias = "SaveFiles")] |
| 42 |
async fn save_files( |
| 43 |
&self, |
| 44 |
token: HandleToken, |
| 45 |
app_id: Option<MaybeAppID>, |
| 46 |
window_identifier: Option<WindowIdentifierType>, |
| 47 |
title: &str, |
| 48 |
options: SaveFilesOptions, |
| 49 |
) -> Result<SelectedFiles>; |
| 50 |
} |
| 51 |
|
| 52 |
pub(crate) struct FileChooserInterface { |
| 53 |
imp: Arc<dyn FileChooserImpl>, |
| 54 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 55 |
cnx: zbus::Connection, |
| 56 |
} |
| 57 |
|
| 58 |
impl FileChooserInterface { |
| 59 |
pub fn new( |
| 60 |
imp: Arc<dyn FileChooserImpl>, |
| 61 |
cnx: zbus::Connection, |
| 62 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 63 |
) -> Self { |
| 64 |
Self { imp, cnx, spawn } |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[zbus::interface(name = "org.freedesktop.impl.portal.FileChooser")] |
| 69 |
impl FileChooserInterface { |
| 70 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 71 |
fn version(&self) -> u32 { |
| 72 |
4 |
| 73 |
} |
| 74 |
|
| 75 |
#[zbus(out_args("response", "results"))] |
| 76 |
async fn open_file( |
| 77 |
&self, |
| 78 |
handle: OwnedObjectPath, |
| 79 |
app_id: Optional<MaybeAppID>, |
| 80 |
window_identifier: Optional<WindowIdentifierType>, |
| 81 |
title: String, |
| 82 |
options: OpenFileOptions, |
| 83 |
) -> Result<Response<SelectedFiles>> { |
| 84 |
let imp = Arc::clone(&self.imp); |
| 85 |
|
| 86 |
Request::spawn( |
| 87 |
"FileChooser::OpenFile", |
| 88 |
&self.cnx, |
| 89 |
handle.clone(), |
| 90 |
Arc::clone(&self.imp), |
| 91 |
Arc::clone(&self.spawn), |
| 92 |
async move { |
| 93 |
imp.open_file( |
| 94 |
HandleToken::try_from(&handle).unwrap(), |
| 95 |
app_id.into(), |
| 96 |
window_identifier.into(), |
| 97 |
&title, |
| 98 |
options, |
| 99 |
) |
| 100 |
.await |
| 101 |
}, |
| 102 |
) |
| 103 |
.await |
| 104 |
} |
| 105 |
|
| 106 |
#[zbus(out_args("response", "results"))] |
| 107 |
async fn save_file( |
| 108 |
&self, |
| 109 |
handle: OwnedObjectPath, |
| 110 |
app_id: Optional<MaybeAppID>, |
| 111 |
window_identifier: Optional<WindowIdentifierType>, |
| 112 |
title: String, |
| 113 |
options: SaveFileOptions, |
| 114 |
) -> Result<Response<SelectedFiles>> { |
| 115 |
let imp = Arc::clone(&self.imp); |
| 116 |
|
| 117 |
Request::spawn( |
| 118 |
"FileChooser::SaveFile", |
| 119 |
&self.cnx, |
| 120 |
handle.clone(), |
| 121 |
Arc::clone(&self.imp), |
| 122 |
Arc::clone(&self.spawn), |
| 123 |
async move { |
| 124 |
imp.save_file( |
| 125 |
HandleToken::try_from(&handle).unwrap(), |
| 126 |
app_id.into(), |
| 127 |
window_identifier.into(), |
| 128 |
&title, |
| 129 |
options, |
| 130 |
) |
| 131 |
.await |
| 132 |
}, |
| 133 |
) |
| 134 |
.await |
| 135 |
} |
| 136 |
|
| 137 |
#[zbus(out_args("response", "results"))] |
| 138 |
async fn save_files( |
| 139 |
&self, |
| 140 |
handle: OwnedObjectPath, |
| 141 |
app_id: Optional<MaybeAppID>, |
| 142 |
window_identifier: Optional<WindowIdentifierType>, |
| 143 |
title: String, |
| 144 |
options: SaveFilesOptions, |
| 145 |
) -> Result<Response<SelectedFiles>> { |
| 146 |
let imp = Arc::clone(&self.imp); |
| 147 |
|
| 148 |
Request::spawn( |
| 149 |
"FileChooser::SaveFiles", |
| 150 |
&self.cnx, |
| 151 |
handle.clone(), |
| 152 |
Arc::clone(&self.imp), |
| 153 |
Arc::clone(&self.spawn), |
| 154 |
async move { |
| 155 |
imp.save_files( |
| 156 |
HandleToken::try_from(&handle).unwrap(), |
| 157 |
app_id.into(), |
| 158 |
window_identifier.into(), |
| 159 |
&title, |
| 160 |
options, |
| 161 |
) |
| 162 |
.await |
| 163 |
}, |
| 164 |
) |
| 165 |
.await |
| 166 |
} |
| 167 |
} |