Client.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2021, timmot <tiwwot@protonmail.com>
  3. * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <LibFileSystem/FileSystem.h>
  9. #include <LibFileSystemAccessClient/Client.h>
  10. #include <LibGUI/ConnectionToWindowServer.h>
  11. #include <LibGUI/MessageBox.h>
  12. #include <LibGUI/Window.h>
  13. namespace FileSystemAccessClient {
  14. static RefPtr<Client> s_the = nullptr;
  15. Client& Client::the()
  16. {
  17. if (!s_the || !s_the->is_open())
  18. s_the = Client::try_create().release_value_but_fixme_should_propagate_errors();
  19. return *s_the;
  20. }
  21. Result Client::request_file_read_only_approved(GUI::Window* parent_window, DeprecatedString const& path)
  22. {
  23. auto const id = get_new_id();
  24. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, Core::File::OpenMode::Read });
  25. if (path.starts_with('/')) {
  26. async_request_file_read_only_approved(id, path);
  27. } else {
  28. auto full_path = LexicalPath::join(TRY(FileSystem::current_working_directory()), path).string();
  29. async_request_file_read_only_approved(id, full_path);
  30. }
  31. return handle_promise(id);
  32. }
  33. Result Client::request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::File::OpenMode mode)
  34. {
  35. auto const id = get_new_id();
  36. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, mode });
  37. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  38. auto child_window_server_client_id = expose_window_server_client_id();
  39. auto parent_window_id = parent_window->window_id();
  40. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  41. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  42. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  43. });
  44. if (path.starts_with('/')) {
  45. async_request_file(id, parent_window_server_client_id, parent_window_id, path, mode);
  46. } else {
  47. auto full_path = LexicalPath::join(TRY(FileSystem::current_working_directory()), path).string();
  48. async_request_file(id, parent_window_server_client_id, parent_window_id, full_path, mode);
  49. }
  50. return handle_promise(id);
  51. }
  52. Result Client::open_file(GUI::Window* parent_window, OpenFileOptions const& options)
  53. {
  54. auto const id = get_new_id();
  55. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, options.requested_access });
  56. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  57. auto child_window_server_client_id = expose_window_server_client_id();
  58. auto parent_window_id = parent_window->window_id();
  59. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  60. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  61. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  62. });
  63. async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, options.window_title, options.path, options.requested_access, options.allowed_file_types);
  64. return handle_promise(id);
  65. }
  66. Result Client::save_file(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::File::OpenMode requested_access)
  67. {
  68. auto const id = get_new_id();
  69. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, requested_access });
  70. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  71. auto child_window_server_client_id = expose_window_server_client_id();
  72. auto parent_window_id = parent_window->window_id();
  73. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  74. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  75. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  76. });
  77. async_prompt_save_file(id, parent_window_server_client_id, parent_window_id, name.is_empty() ? "Untitled" : name, ext.is_empty() ? "txt" : ext, Core::StandardPaths::home_directory(), requested_access);
  78. return handle_promise(id);
  79. }
  80. void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> const& ipc_file, Optional<DeprecatedString> const& chosen_file)
  81. {
  82. auto potential_data = m_promises.get(request_id);
  83. VERIFY(potential_data.has_value());
  84. auto& request_data = potential_data.value();
  85. auto action = "Requesting"sv;
  86. if (has_flag(request_data.mode, Core::File::OpenMode::Read))
  87. action = "Opening"sv;
  88. else if (has_flag(request_data.mode, Core::File::OpenMode::Write))
  89. action = "Saving"sv;
  90. if (ipc_file.has_value()) {
  91. if (FileSystem::is_device(ipc_file->fd()))
  92. error = is_silencing_devices() ? ESUCCESS : EINVAL;
  93. else if (FileSystem::is_directory(ipc_file->fd()))
  94. error = is_silencing_directories() ? ESUCCESS : EISDIR;
  95. }
  96. switch (error) {
  97. case ESUCCESS:
  98. case ECANCELED:
  99. break;
  100. case ENOENT:
  101. if (is_silencing_nonexistent_entries())
  102. break;
  103. [[fallthrough]];
  104. default:
  105. ErrorOr<String> maybe_message = String {};
  106. if (error == ECONNRESET)
  107. maybe_message = String::formatted("FileSystemAccessClient: {}", Error::from_errno(error));
  108. else
  109. maybe_message = String::formatted("{} \"{}\" failed: {}", action, *chosen_file, Error::from_errno(error));
  110. if (!maybe_message.is_error())
  111. (void)GUI::MessageBox::try_show_error(request_data.parent_window, maybe_message.release_value());
  112. }
  113. if (error != ESUCCESS)
  114. return (void)request_data.promise->resolve(Error::from_errno(error));
  115. auto file_or_error = [&]() -> ErrorOr<File> {
  116. auto stream = TRY(Core::File::adopt_fd(ipc_file->take_fd(), Core::File::OpenMode::ReadWrite));
  117. auto filename = TRY(String::from_deprecated_string(*chosen_file));
  118. return File({}, move(stream), filename);
  119. }();
  120. if (file_or_error.is_error()) {
  121. auto maybe_message = String::formatted("{} \"{}\" failed: {}", action, *chosen_file, file_or_error.error());
  122. if (!maybe_message.is_error())
  123. (void)GUI::MessageBox::try_show_error(request_data.parent_window, maybe_message.release_value());
  124. return (void)request_data.promise->resolve(file_or_error.release_error());
  125. }
  126. (void)request_data.promise->resolve(file_or_error.release_value());
  127. }
  128. void Client::die()
  129. {
  130. for (auto const& entry : m_promises)
  131. handle_prompt_end(entry.key, ECONNRESET, {}, "");
  132. }
  133. int Client::get_new_id()
  134. {
  135. auto const new_id = m_last_id++;
  136. // Note: This verify shouldn't fail, and we should provide a valid ID
  137. // But we probably have more issues if this test fails.
  138. VERIFY(!m_promises.contains(new_id));
  139. return new_id;
  140. }
  141. Result Client::handle_promise(int id)
  142. {
  143. auto result = TRY(m_promises.get(id)->promise->await());
  144. m_promises.remove(id);
  145. return result;
  146. }
  147. }