Client.cpp 7.7 KB

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