6
6
*/
7
7
8
8
#include < AK/LexicalPath.h>
9
+ #include < LibCore/DeprecatedFile.h>
9
10
#include < LibFileSystemAccessClient/Client.h>
10
11
#include < LibGUI/ConnectionToWindowServer.h>
11
12
#include < LibGUI/MessageBox.h>
@@ -22,31 +23,6 @@ Client& Client::the()
22
23
return *s_the;
23
24
}
24
25
25
- DeprecatedResult Client::try_request_file_read_only_approved_deprecated (GUI::Window* parent_window, DeprecatedString const & path)
26
- {
27
- auto const id = get_new_id ();
28
- m_promises.set (id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct () }, parent_window });
29
-
30
- auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the ().expose_client_id ();
31
- auto child_window_server_client_id = expose_window_server_client_id ();
32
- auto parent_window_id = parent_window->window_id ();
33
-
34
- GUI::ConnectionToWindowServer::the ().add_window_stealing_for_client (child_window_server_client_id, parent_window_id);
35
-
36
- ScopeGuard guard ([parent_window_id, child_window_server_client_id] {
37
- GUI::ConnectionToWindowServer::the ().remove_window_stealing_for_client (child_window_server_client_id, parent_window_id);
38
- });
39
-
40
- if (path.starts_with (' /' )) {
41
- async_request_file_read_only_approved (id, parent_window_server_client_id, parent_window_id, path);
42
- } else {
43
- auto full_path = LexicalPath::join (Core::DeprecatedFile::current_working_directory (), path).string ();
44
- async_request_file_read_only_approved (id, parent_window_server_client_id, parent_window_id, full_path);
45
- }
46
-
47
- return handle_promise<DeprecatedResult>(id);
48
- }
49
-
50
26
Result Client::request_file_read_only_approved (GUI::Window* parent_window, DeprecatedString const & path)
51
27
{
52
28
auto const id = get_new_id ();
@@ -69,55 +45,7 @@ Result Client::request_file_read_only_approved(GUI::Window* parent_window, Depre
69
45
async_request_file_read_only_approved (id, parent_window_server_client_id, parent_window_id, full_path);
70
46
}
71
47
72
- return handle_promise<Result>(id);
73
- }
74
-
75
- static Core::File::OpenMode to_stream_open_mode (Core::OpenMode open_mode)
76
- {
77
- Core::File::OpenMode result {};
78
- if ((open_mode & Core::OpenMode::ReadOnly) == Core::OpenMode::ReadOnly)
79
- result |= Core::File::OpenMode::Read;
80
- if ((open_mode & Core::OpenMode::WriteOnly) == Core::OpenMode::WriteOnly)
81
- result |= Core::File::OpenMode::Write;
82
- if ((open_mode & Core::OpenMode::ReadWrite) == Core::OpenMode::ReadWrite)
83
- result |= Core::File::OpenMode::ReadWrite;
84
- if ((open_mode & Core::OpenMode::Append) == Core::OpenMode::Append)
85
- result |= Core::File::OpenMode::Append;
86
- if ((open_mode & Core::OpenMode::Truncate) == Core::OpenMode::Truncate)
87
- result |= Core::File::OpenMode::Truncate;
88
- if ((open_mode & Core::OpenMode::MustBeNew) == Core::OpenMode::MustBeNew)
89
- result |= Core::File::OpenMode::MustBeNew;
90
- if ((open_mode & Core::OpenMode::KeepOnExec) == Core::OpenMode::KeepOnExec)
91
- result |= Core::File::OpenMode::KeepOnExec;
92
-
93
- return result;
94
- }
95
-
96
- DeprecatedResult Client::try_request_file_deprecated (GUI::Window* parent_window, DeprecatedString const & path, Core::OpenMode deprecated_mode)
97
- {
98
- auto const id = get_new_id ();
99
- m_promises.set (id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct () }, parent_window });
100
-
101
- auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the ().expose_client_id ();
102
- auto child_window_server_client_id = expose_window_server_client_id ();
103
- auto parent_window_id = parent_window->window_id ();
104
-
105
- GUI::ConnectionToWindowServer::the ().add_window_stealing_for_client (child_window_server_client_id, parent_window_id);
106
-
107
- ScopeGuard guard ([parent_window_id, child_window_server_client_id] {
108
- GUI::ConnectionToWindowServer::the ().remove_window_stealing_for_client (child_window_server_client_id, parent_window_id);
109
- });
110
-
111
- auto const mode = to_stream_open_mode (deprecated_mode);
112
-
113
- if (path.starts_with (' /' )) {
114
- async_request_file (id, parent_window_server_client_id, parent_window_id, path, mode);
115
- } else {
116
- auto full_path = LexicalPath::join (Core::DeprecatedFile::current_working_directory (), path).string ();
117
- async_request_file (id, parent_window_server_client_id, parent_window_id, full_path, mode);
118
- }
119
-
120
- return handle_promise<DeprecatedResult>(id);
48
+ return handle_promise (id);
121
49
}
122
50
123
51
Result Client::request_file (GUI::Window* parent_window, DeprecatedString const & path, Core::File::OpenMode mode)
@@ -142,29 +70,7 @@ Result Client::request_file(GUI::Window* parent_window, DeprecatedString const&
142
70
async_request_file (id, parent_window_server_client_id, parent_window_id, full_path, mode);
143
71
}
144
72
145
- return handle_promise<Result>(id);
146
- }
147
-
148
- DeprecatedResult Client::try_open_file_deprecated (GUI::Window* parent_window, DeprecatedString const & window_title, StringView path, Core::OpenMode deprecated_requested_access)
149
- {
150
- auto const id = get_new_id ();
151
- m_promises.set (id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct () }, parent_window });
152
-
153
- auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the ().expose_client_id ();
154
- auto child_window_server_client_id = expose_window_server_client_id ();
155
- auto parent_window_id = parent_window->window_id ();
156
-
157
- GUI::ConnectionToWindowServer::the ().add_window_stealing_for_client (child_window_server_client_id, parent_window_id);
158
-
159
- ScopeGuard guard ([parent_window_id, child_window_server_client_id] {
160
- GUI::ConnectionToWindowServer::the ().remove_window_stealing_for_client (child_window_server_client_id, parent_window_id);
161
- });
162
-
163
- auto const requested_access = to_stream_open_mode (deprecated_requested_access);
164
-
165
- async_prompt_open_file (id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access);
166
-
167
- return handle_promise<DeprecatedResult>(id);
73
+ return handle_promise (id);
168
74
}
169
75
170
76
Result Client::open_file (GUI::Window* parent_window, DeprecatedString const & window_title, StringView path, Core::File::OpenMode requested_access)
@@ -184,29 +90,7 @@ Result Client::open_file(GUI::Window* parent_window, DeprecatedString const& win
184
90
185
91
async_prompt_open_file (id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access);
186
92
187
- return handle_promise<Result>(id);
188
- }
189
-
190
- DeprecatedResult Client::try_save_file_deprecated (GUI::Window* parent_window, DeprecatedString const & name, DeprecatedString const ext, Core::OpenMode deprecated_requested_access)
191
- {
192
- auto const id = get_new_id ();
193
- m_promises.set (id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct () }, parent_window });
194
-
195
- auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the ().expose_client_id ();
196
- auto child_window_server_client_id = expose_window_server_client_id ();
197
- auto parent_window_id = parent_window->window_id ();
198
-
199
- GUI::ConnectionToWindowServer::the ().add_window_stealing_for_client (child_window_server_client_id, parent_window_id);
200
-
201
- ScopeGuard guard ([parent_window_id, child_window_server_client_id] {
202
- GUI::ConnectionToWindowServer::the ().remove_window_stealing_for_client (child_window_server_client_id, parent_window_id);
203
- });
204
-
205
- auto const requested_access = to_stream_open_mode (deprecated_requested_access);
206
-
207
- async_prompt_save_file (id, parent_window_server_client_id, parent_window_id, name.is_null () ? " Untitled" : name, ext.is_null () ? " txt" : ext, Core::StandardPaths::home_directory (), requested_access);
208
-
209
- return handle_promise<DeprecatedResult>(id);
93
+ return handle_promise (id);
210
94
}
211
95
212
96
Result Client::save_file (GUI::Window* parent_window, DeprecatedString const & name, DeprecatedString const ext, Core::File::OpenMode requested_access)
@@ -226,7 +110,7 @@ Result Client::save_file(GUI::Window* parent_window, DeprecatedString const& nam
226
110
227
111
async_prompt_save_file (id, parent_window_server_client_id, parent_window_id, name.is_null () ? " Untitled" : name, ext.is_null () ? " txt" : ext, Core::StandardPaths::home_directory (), requested_access);
228
112
229
- return handle_promise<Result> (id);
113
+ return handle_promise (id);
230
114
}
231
115
232
116
void Client::handle_prompt_end (i32 request_id, i32 error, Optional<IPC::File> const & ipc_file, Optional<DeprecatedString> const & chosen_file)
@@ -235,42 +119,24 @@ void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> co
235
119
VERIFY (potential_data.has_value ());
236
120
auto & request_data = potential_data.value ();
237
121
238
- auto const resolve_any_promise = [&promise = request_data.promise ](Error&& error) {
239
- if (promise.has <PromiseType<DeprecatedResult>>()) {
240
- promise.get <PromiseType<DeprecatedResult>>()->resolve (move (error));
241
- return ;
242
- }
243
- promise.get <PromiseType<Result>>()->resolve (move (error));
244
- };
245
-
246
122
if (error != 0 ) {
247
123
// We don't want to show an error message for non-existent files since some applications may want
248
124
// to handle it as opening a new, named file.
249
125
if (error != -1 && error != ENOENT)
250
126
GUI::MessageBox::show_error (request_data.parent_window , DeprecatedString::formatted (" Opening \" {}\" failed: {}" , *chosen_file, strerror (error)));
251
- resolve_any_promise (Error::from_errno (error));
127
+ request_data. promise -> resolve (Error::from_errno (error));
252
128
return ;
253
129
}
254
130
255
131
if (Core::DeprecatedFile::is_device (ipc_file->fd ())) {
256
132
GUI::MessageBox::show_error (request_data.parent_window , DeprecatedString::formatted (" Opening \" {}\" failed: Cannot open device files" , *chosen_file));
257
- resolve_any_promise (Error::from_string_literal (" Cannot open device files" ));
133
+ request_data. promise -> resolve (Error::from_string_literal (" Cannot open device files" ));
258
134
return ;
259
135
}
260
136
261
137
if (Core::DeprecatedFile::is_directory (ipc_file->fd ())) {
262
138
GUI::MessageBox::show_error (request_data.parent_window , DeprecatedString::formatted (" Opening \" {}\" failed: Cannot open directory" , *chosen_file));
263
- resolve_any_promise (Error::from_errno (EISDIR));
264
- return ;
265
- }
266
-
267
- if (request_data.promise .has <PromiseType<DeprecatedResult>>()) {
268
- auto file = Core::DeprecatedFile::construct ();
269
- auto fd = ipc_file->take_fd ();
270
- file->open (fd, Core::OpenMode::ReadWrite, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes);
271
- file->set_filename (*chosen_file);
272
-
273
- request_data.promise .get <PromiseType<DeprecatedResult>>()->resolve (file);
139
+ request_data.promise ->resolve (Error::from_errno (EISDIR));
274
140
return ;
275
141
}
276
142
@@ -280,11 +146,11 @@ void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> co
280
146
return File ({}, move (stream), filename);
281
147
}();
282
148
if (file_or_error.is_error ()) {
283
- resolve_any_promise (file_or_error.release_error ());
149
+ request_data. promise -> resolve (file_or_error.release_error ());
284
150
return ;
285
151
}
286
152
287
- request_data.promise . get <PromiseType<Result>>() ->resolve (file_or_error.release_value ());
153
+ request_data.promise ->resolve (file_or_error.release_value ());
288
154
}
289
155
290
156
void Client::die ()
@@ -302,10 +168,9 @@ int Client::get_new_id()
302
168
return new_id;
303
169
}
304
170
305
- template <typename AnyResult>
306
- AnyResult Client::handle_promise (int id)
171
+ Result Client::handle_promise (int id)
307
172
{
308
- auto result = m_promises.get (id)->promise . get <PromiseType<AnyResult>>() ->await ();
173
+ auto result = m_promises.get (id)->promise ->await ();
309
174
m_promises.remove (id);
310
175
return result;
311
176
}
0 commit comments