Client.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/String.h>
  10. #include <FileSystemAccessServer/FileSystemAccessClientEndpoint.h>
  11. #include <FileSystemAccessServer/FileSystemAccessServerEndpoint.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/Promise.h>
  14. #include <LibCore/StandardPaths.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibIPC/ConnectionToServer.h>
  17. namespace FileSystemAccessClient {
  18. class Client;
  19. class File {
  20. public:
  21. File(Badge<Client>, NonnullOwnPtr<Core::File> stream, String filename)
  22. : m_stream(move(stream))
  23. , m_filename(filename)
  24. {
  25. }
  26. Core::File& stream() const { return *m_stream; }
  27. NonnullOwnPtr<Core::File> release_stream() { return move(m_stream); }
  28. String filename() const { return m_filename; }
  29. private:
  30. NonnullOwnPtr<Core::File> m_stream;
  31. String m_filename;
  32. };
  33. using Result = ErrorOr<File>;
  34. class Client final
  35. : public IPC::ConnectionToServer<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>
  36. , public FileSystemAccessClientEndpoint {
  37. IPC_CLIENT_CONNECTION(Client, "/tmp/session/%sid/portal/filesystemaccess"sv)
  38. public:
  39. Result request_file_read_only_approved(GUI::Window* parent_window, DeprecatedString const& path);
  40. Result request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::File::OpenMode requested_access);
  41. Result open_file(GUI::Window* parent_window, DeprecatedString const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), Core::File::OpenMode requested_access = Core::File::OpenMode::Read);
  42. Result save_file(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::File::OpenMode requested_access = Core::File::OpenMode::Write | Core::File::OpenMode::Truncate);
  43. static Client& the();
  44. protected:
  45. void die() override;
  46. private:
  47. explicit Client(NonnullOwnPtr<Core::LocalSocket> socket)
  48. : IPC::ConnectionToServer<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket))
  49. {
  50. }
  51. virtual void handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> const& fd, Optional<DeprecatedString> const& chosen_file) override;
  52. int get_new_id();
  53. Result handle_promise(int);
  54. template<typename T>
  55. using PromiseType = RefPtr<Core::Promise<T>>;
  56. struct PromiseAndWindow {
  57. PromiseType<Result> promise;
  58. GUI::Window* parent_window { nullptr };
  59. };
  60. HashMap<int, PromiseAndWindow> m_promises {};
  61. int m_last_id { 0 };
  62. };
  63. }