Client.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <FileSystemAccessServer/FileSystemAccessClientEndpoint.h>
  10. #include <FileSystemAccessServer/FileSystemAccessServerEndpoint.h>
  11. #include <LibCore/File.h>
  12. #include <LibCore/Promise.h>
  13. #include <LibCore/StandardPaths.h>
  14. #include <LibGUI/Window.h>
  15. #include <LibIPC/ConnectionToServer.h>
  16. namespace FileSystemAccessClient {
  17. using Result = ErrorOr<NonnullRefPtr<Core::File>>;
  18. class Client final
  19. : public IPC::ConnectionToServer<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>
  20. , public FileSystemAccessClientEndpoint {
  21. IPC_CLIENT_CONNECTION(Client, "/tmp/portal/filesystemaccess")
  22. public:
  23. Result try_request_file_read_only_approved(GUI::Window* parent_window, String const& path);
  24. Result try_request_file(GUI::Window* parent_window, String const& path, Core::OpenMode mode);
  25. Result try_open_file(GUI::Window* parent_window, String const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), Core::OpenMode requested_access = Core::OpenMode::ReadOnly);
  26. Result try_save_file(GUI::Window* parent_window, String const& name, String const ext, Core::OpenMode requested_access = Core::OpenMode::WriteOnly | Core::OpenMode::Truncate);
  27. static Client& the();
  28. protected:
  29. void die() override;
  30. private:
  31. explicit Client(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  32. : IPC::ConnectionToServer<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket))
  33. {
  34. }
  35. virtual void handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> const& fd, Optional<String> const& chosen_file) override;
  36. int get_new_id();
  37. Result handle_promise(int);
  38. struct PromiseAndWindow {
  39. RefPtr<Core::Promise<Result>> promise {};
  40. GUI::Window* parent_window { nullptr };
  41. };
  42. HashMap<int, PromiseAndWindow> m_promises {};
  43. int m_last_id { 0 };
  44. };
  45. }