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