Client.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021, timmot <tiwwot@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <FileSystemAccessServer/FileSystemAccessClientEndpoint.h>
  8. #include <FileSystemAccessServer/FileSystemAccessServerEndpoint.h>
  9. #include <LibCore/File.h>
  10. #include <LibCore/Promise.h>
  11. #include <LibCore/StandardPaths.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibIPC/ServerConnection.h>
  14. namespace FileSystemAccessClient {
  15. struct Result {
  16. i32 error;
  17. Optional<i32> fd;
  18. Optional<String> chosen_file;
  19. };
  20. using FileResult = ErrorOr<NonnullRefPtr<Core::File>>;
  21. class Client final
  22. : public IPC::ServerConnection<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>
  23. , public FileSystemAccessClientEndpoint {
  24. IPC_CLIENT_CONNECTION(Client, "/tmp/portal/filesystemaccess")
  25. public:
  26. Result request_file_read_only_approved(i32 parent_window_id, String const& path);
  27. Result request_file(i32 parent_window_id, String const& path, Core::OpenMode mode);
  28. Result open_file(i32 parent_window_id, String const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), Core::OpenMode requested_access = Core::OpenMode::ReadOnly);
  29. Result save_file(i32 parent_window_id, String const& name, String const ext, Core::OpenMode requested_access = Core::OpenMode::WriteOnly | Core::OpenMode::Truncate);
  30. FileResult try_request_file_read_only_approved(GUI::Window* parent_window, String const& path);
  31. FileResult try_request_file(GUI::Window* parent_window, String const& path, Core::OpenMode mode);
  32. FileResult try_open_file(GUI::Window* parent_window, String const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), Core::OpenMode requested_access = Core::OpenMode::ReadOnly);
  33. FileResult try_save_file(GUI::Window* parent_window, String const& name, String const ext, Core::OpenMode requested_access = Core::OpenMode::WriteOnly | Core::OpenMode::Truncate);
  34. static Client& the();
  35. protected:
  36. void die() override;
  37. private:
  38. explicit Client(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  39. : IPC::ServerConnection<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket))
  40. {
  41. }
  42. virtual void handle_prompt_end(i32 error, Optional<IPC::File> const& fd, Optional<String> const& chosen_file) override;
  43. RefPtr<Core::Promise<Result>> m_promise;
  44. RefPtr<Core::Promise<FileResult>> m_file_promise;
  45. GUI::Window* m_parent_window { nullptr };
  46. };
  47. }