Debian Codegraph @

src/backend/xdg_desktop_portal/portal/file_dialog.rs

raw
1 use std::{
2 ffi::{CStr, CString},
3 time::{SystemTime, UNIX_EPOCH},
4 };
5
6 use super::{
7 ffi,
8 libdbus::{Message, MessageIter},
9 };
10
11 #[derive(Debug)]
12 pub struct HandleToken(pub CString);
13
14 impl Default for HandleToken {
15 fn default() -> Self {
16 let now = SystemTime::now()
17 .duration_since(UNIX_EPOCH)
18 .unwrap()
19 .as_nanos();
20 let token = format!("rfd_{now}");
21 Self(CString::new(token).unwrap())
22 }
23 }
24
25 pub type FileFilter = (CString, Vec<CString>);
26
27 #[derive(Debug)]
28 pub struct FilePath(pub CString);
29
30 #[derive(Debug, Default)]
31 pub struct OpenFileOptions {
32 pub parent_window: CString,
33 pub title: CString,
34 pub handle_token: HandleToken,
35 pub accept_label: Option<CString>,
36 pub modal: Option<bool>,
37 pub multiple: Option<bool>,
38 pub directory: Option<bool>,
39 pub filters: Vec<FileFilter>,
40 // TODO: Serialize this if needed
41 #[allow(unused)]
42 pub current_filter: Option<FileFilter>,
43 pub current_folder: Option<FilePath>,
44 }
45
46 fn append_filters(dict: &mut MessageIter, filters: Vec<FileFilter>) {
47 let filters: Vec<_> = filters
48 .into_iter()
49 .filter(|(_, globs)| !globs.is_empty())
50 .collect();
51
52 if !filters.is_empty() {
53 dict.with_dict_entry(c"filters", c"a(sa(us))", |variant| {
54 variant.with_container(ffi::DBUS_TYPE_ARRAY, Some(c"(sa(us))"), |array| {
55 for (label, globs) in filters.iter() {
56 array.with_container(ffi::DBUS_TYPE_STRUCT, None, |s| {
57 s.append_string(label);
58 s.with_container(ffi::DBUS_TYPE_ARRAY, Some(c"(us)"), |array| {
59 for glob in globs {
60 array.with_container(ffi::DBUS_TYPE_STRUCT, None, |s| {
61 s.append_u32(0); // Glob type
62 s.append_string(glob);
63 })
64 }
65 })
66 });
67 }
68 });
69 });
70 }
71 }
72
73 fn append_path(dict: &mut MessageIter, key: &CStr, path: &FilePath) {
74 dict.with_dict_entry(key, c"ay", |variant| {
75 variant.with_container(ffi::DBUS_TYPE_ARRAY, Some(c"y"), |array| {
76 for byte in path.0.as_bytes_with_nul() {
77 array.append_byte(*byte);
78 }
79 })
80 });
81 }
82
83 impl Message {
84 pub fn open_file(opts: OpenFileOptions) -> Self {
85 let mut msg = Message::new_method_call(
86 c"org.freedesktop.portal.Desktop",
87 c"/org/freedesktop/portal/desktop",
88 c"org.freedesktop.portal.FileChooser",
89 c"OpenFile",
90 )
91 .unwrap();
92
93 let mut iter = MessageIter::init_append(&mut msg);
94
95 iter.append_string(&opts.parent_window);
96 iter.append_string(&opts.title);
97
98 iter.with_container(ffi::DBUS_TYPE_ARRAY, Some(c"{sv}"), |dict| {
99 dict.with_dict_entry(c"handle_token", c"s", |variant| {
100 variant.append_string(&opts.handle_token.0);
101 });
102
103 if let Some(accept_label) = opts.accept_label.as_ref() {
104 dict.with_dict_entry(c"accept_label", c"s", |variant| {
105 variant.append_string(accept_label);
106 });
107 }
108
109 if let Some(modal) = opts.modal {
110 dict.with_dict_entry(c"modal", c"b", |variant| {
111 variant.append_bool(modal);
112 });
113 }
114 if let Some(multiple) = opts.multiple {
115 dict.with_dict_entry(c"multiple", c"b", |variant| {
116 variant.append_bool(multiple);
117 });
118 }
119 if let Some(directory) = opts.directory {
120 dict.with_dict_entry(c"directory", c"b", |variant| {
121 variant.append_bool(directory);
122 });
123 }
124
125 if let Some(current_folder) = opts.current_folder {
126 append_path(dict, c"current_folder", &current_folder);
127 }
128
129 append_filters(dict, opts.filters);
130 });
131
132 msg
133 }
134 }
135
136 #[derive(Debug, Default)]
137 pub struct SaveFileOptions {
138 pub parent_window: CString,
139 pub title: CString,
140 pub handle_token: HandleToken,
141 pub accept_label: Option<CString>,
142 pub modal: Option<bool>,
143 pub current_name: Option<CString>,
144 pub current_folder: Option<FilePath>,
145 pub current_file: Option<FilePath>,
146 pub filters: Vec<FileFilter>,
147 // TODO: Serialize this if needed
148 #[allow(unused)]
149 pub current_filter: Option<FileFilter>,
150 }
151
152 impl Message {
153 pub fn save_file(opts: SaveFileOptions) -> Self {
154 let mut msg = Message::new_method_call(
155 c"org.freedesktop.portal.Desktop",
156 c"/org/freedesktop/portal/desktop",
157 c"org.freedesktop.portal.FileChooser",
158 c"SaveFile",
159 )
160 .unwrap();
161
162 let mut iter = MessageIter::init_append(&mut msg);
163
164 iter.append_string(&opts.parent_window);
165 iter.append_string(&opts.title);
166
167 iter.with_container(ffi::DBUS_TYPE_ARRAY, Some(c"{sv}"), |dict| {
168 dict.with_dict_entry(c"handle_token", c"s", |variant| {
169 variant.append_string(&opts.handle_token.0);
170 });
171
172 if let Some(accept_label) = opts.accept_label.as_ref() {
173 dict.with_dict_entry(c"accept_label", c"s", |variant| {
174 variant.append_string(accept_label);
175 });
176 }
177
178 if let Some(modal) = opts.modal {
179 dict.with_dict_entry(c"modal", c"b", |variant| {
180 variant.append_bool(modal);
181 });
182 }
183
184 if let Some(current_name) = opts.current_name.as_ref() {
185 dict.with_dict_entry(c"current_name", c"s", |variant| {
186 variant.append_string(current_name);
187 });
188 }
189
190 if let Some(current_folder) = opts.current_folder {
191 append_path(dict, c"current_folder", &current_folder);
192 }
193
194 if let Some(current_file) = opts.current_file {
195 append_path(dict, c"current_file", &current_file);
196 }
197
198 append_filters(dict, opts.filters);
199 });
200
201 msg
202 }
203 }