Client.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2021, timmot <tiwwot@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. // FIXME: LibIPC Decoder and Encoder are sensitive to include order here
  7. // clang-format off
  8. #include <LibGUI/WindowServerConnection.h>
  9. // clang-format on
  10. #include <LibFileSystemAccessClient/Client.h>
  11. #include <LibGUI/Window.h>
  12. namespace FileSystemAccessClient {
  13. static RefPtr<Client> s_the = nullptr;
  14. Client& Client::the()
  15. {
  16. if (!s_the || !s_the->is_open())
  17. s_the = Client::construct();
  18. return *s_the;
  19. }
  20. Result Client::request_file(i32 parent_window_id, String const& path, Core::OpenMode mode)
  21. {
  22. m_promise = Core::Promise<Result>::construct();
  23. auto parent_window_server_client_id = GUI::WindowServerConnection::the().expose_client_id();
  24. auto child_window_server_client_id = expose_window_server_client_id();
  25. GUI::WindowServerConnection::the().async_add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  26. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  27. GUI::WindowServerConnection::the().async_remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  28. });
  29. async_request_file(parent_window_server_client_id, parent_window_id, path, mode);
  30. return m_promise->await();
  31. }
  32. Result Client::open_file(i32 parent_window_id, String const& window_title, StringView const& path)
  33. {
  34. m_promise = Core::Promise<Result>::construct();
  35. auto parent_window_server_client_id = GUI::WindowServerConnection::the().expose_client_id();
  36. auto child_window_server_client_id = expose_window_server_client_id();
  37. GUI::WindowServerConnection::the().async_add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  38. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  39. GUI::WindowServerConnection::the().async_remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  40. });
  41. async_prompt_open_file(parent_window_server_client_id, parent_window_id, window_title, path, Core::OpenMode::ReadOnly);
  42. return m_promise->await();
  43. }
  44. Result Client::save_file(i32 parent_window_id, String const& name, String const ext)
  45. {
  46. m_promise = Core::Promise<Result>::construct();
  47. auto parent_window_server_client_id = GUI::WindowServerConnection::the().expose_client_id();
  48. auto child_window_server_client_id = expose_window_server_client_id();
  49. GUI::WindowServerConnection::the().async_add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  50. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  51. GUI::WindowServerConnection::the().async_remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  52. });
  53. async_prompt_save_file(parent_window_server_client_id, parent_window_id, name.is_null() ? "Untitled" : name, ext.is_null() ? "txt" : ext, Core::StandardPaths::home_directory(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
  54. return m_promise->await();
  55. }
  56. void Client::handle_prompt_end(i32 error, Optional<IPC::File> const& fd, Optional<String> const& chosen_file)
  57. {
  58. VERIFY(m_promise);
  59. if (error == 0) {
  60. m_promise->resolve({ error, fd->take_fd(), *chosen_file });
  61. } else {
  62. m_promise->resolve({ error, {}, chosen_file });
  63. }
  64. }
  65. void Client::die()
  66. {
  67. if (m_promise)
  68. handle_prompt_end(ECONNRESET, {}, "");
  69. }
  70. }