Client.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <LibCore/DeprecatedFile.h>
  9. #include <LibFileSystem/FileSystem.h>
  10. #include <LibFileSystemAccessClient/Client.h>
  11. #include <LibGUI/ConnectionToWindowServer.h>
  12. #include <LibGUI/MessageBox.h>
  13. #include <LibGUI/Window.h>
  14. namespace FileSystemAccessClient {
  15. static RefPtr<Client> s_the = nullptr;
  16. Client& Client::the()
  17. {
  18. if (!s_the || !s_the->is_open())
  19. s_the = Client::try_create().release_value_but_fixme_should_propagate_errors();
  20. return *s_the;
  21. }
  22. Result Client::request_file_read_only_approved(GUI::Window* parent_window, DeprecatedString const& path)
  23. {
  24. auto const id = get_new_id();
  25. m_promises.set(id, PromiseAndWindow { { Core::Promise<Result>::construct() }, parent_window });
  26. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  27. auto child_window_server_client_id = expose_window_server_client_id();
  28. auto parent_window_id = parent_window->window_id();
  29. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  30. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  31. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  32. });
  33. if (path.starts_with('/')) {
  34. async_request_file_read_only_approved(id, parent_window_server_client_id, parent_window_id, path);
  35. } else {
  36. auto full_path = LexicalPath::join(Core::DeprecatedFile::current_working_directory(), path).string();
  37. async_request_file_read_only_approved(id, parent_window_server_client_id, parent_window_id, full_path);
  38. }
  39. return handle_promise(id);
  40. }
  41. Result Client::request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::File::OpenMode mode)
  42. {
  43. auto const id = get_new_id();
  44. m_promises.set(id, PromiseAndWindow { { Core::Promise<Result>::construct() }, parent_window });
  45. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  46. auto child_window_server_client_id = expose_window_server_client_id();
  47. auto parent_window_id = parent_window->window_id();
  48. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  49. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  50. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  51. });
  52. if (path.starts_with('/')) {
  53. async_request_file(id, parent_window_server_client_id, parent_window_id, path, mode);
  54. } else {
  55. auto full_path = LexicalPath::join(Core::DeprecatedFile::current_working_directory(), path).string();
  56. async_request_file(id, parent_window_server_client_id, parent_window_id, full_path, mode);
  57. }
  58. return handle_promise(id);
  59. }
  60. Result Client::open_file(GUI::Window* parent_window, DeprecatedString const& window_title, StringView path, Core::File::OpenMode requested_access, Optional<Vector<GUI::FileTypeFilter>> const& allowed_file_types)
  61. {
  62. auto const id = get_new_id();
  63. m_promises.set(id, PromiseAndWindow { { Core::Promise<Result>::construct() }, parent_window });
  64. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  65. auto child_window_server_client_id = expose_window_server_client_id();
  66. auto parent_window_id = parent_window->window_id();
  67. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  68. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  69. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  70. });
  71. async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access, allowed_file_types);
  72. return handle_promise(id);
  73. }
  74. Result Client::save_file(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::File::OpenMode requested_access)
  75. {
  76. auto const id = get_new_id();
  77. m_promises.set(id, PromiseAndWindow { { Core::Promise<Result>::construct() }, parent_window });
  78. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  79. auto child_window_server_client_id = expose_window_server_client_id();
  80. auto parent_window_id = parent_window->window_id();
  81. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  82. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  83. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  84. });
  85. 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);
  86. return handle_promise(id);
  87. }
  88. void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> const& ipc_file, Optional<DeprecatedString> const& chosen_file)
  89. {
  90. auto potential_data = m_promises.get(request_id);
  91. VERIFY(potential_data.has_value());
  92. auto& request_data = potential_data.value();
  93. if (error != 0) {
  94. // We don't want to show an error message for non-existent files since some applications may want
  95. // to handle it as opening a new, named file.
  96. if (error != -1 && error != ENOENT)
  97. GUI::MessageBox::show_error(request_data.parent_window, DeprecatedString::formatted("Opening \"{}\" failed: {}", *chosen_file, strerror(error)));
  98. request_data.promise->resolve(Error::from_errno(error)).release_value_but_fixme_should_propagate_errors();
  99. return;
  100. }
  101. if (FileSystem::is_device(ipc_file->fd())) {
  102. GUI::MessageBox::show_error(request_data.parent_window, DeprecatedString::formatted("Opening \"{}\" failed: Cannot open device files", *chosen_file));
  103. request_data.promise->resolve(Error::from_string_literal("Cannot open device files")).release_value_but_fixme_should_propagate_errors();
  104. return;
  105. }
  106. if (FileSystem::is_directory(ipc_file->fd())) {
  107. GUI::MessageBox::show_error(request_data.parent_window, DeprecatedString::formatted("Opening \"{}\" failed: Cannot open directory", *chosen_file));
  108. request_data.promise->resolve(Error::from_errno(EISDIR)).release_value_but_fixme_should_propagate_errors();
  109. return;
  110. }
  111. auto file_or_error = [&]() -> ErrorOr<File> {
  112. auto stream = TRY(Core::File::adopt_fd(ipc_file->take_fd(), Core::File::OpenMode::ReadWrite));
  113. auto filename = TRY(String::from_deprecated_string(*chosen_file));
  114. return File({}, move(stream), filename);
  115. }();
  116. if (file_or_error.is_error()) {
  117. request_data.promise->resolve(file_or_error.release_error()).release_value_but_fixme_should_propagate_errors();
  118. return;
  119. }
  120. request_data.promise->resolve(file_or_error.release_value()).release_value_but_fixme_should_propagate_errors();
  121. }
  122. void Client::die()
  123. {
  124. for (auto const& entry : m_promises)
  125. handle_prompt_end(entry.key, ECONNRESET, {}, "");
  126. }
  127. int Client::get_new_id()
  128. {
  129. auto const new_id = m_last_id++;
  130. // Note: This verify shouldn't fail, and we should provide a valid ID
  131. // But we probably have more issues if this test fails.
  132. VERIFY(!m_promises.contains(new_id));
  133. return new_id;
  134. }
  135. Result Client::handle_promise(int id)
  136. {
  137. auto result = TRY(m_promises.get(id)->promise->await());
  138. m_promises.remove(id);
  139. return result;
  140. }
  141. }