Client.h 2.7 KB

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