/* * Copyright (c) 2021, timmot * Copyright (c) 2022, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace FileSystemAccessClient { class Client; class File { public: File(Badge, NonnullOwnPtr stream, String filename) : m_stream(move(stream)) , m_filename(filename) { } Core::File& stream() const { return *m_stream; } NonnullOwnPtr release_stream() { return move(m_stream); } String filename() const { return m_filename; } private: NonnullOwnPtr m_stream; String m_filename; }; using Result = ErrorOr; class Client final : public IPC::ConnectionToServer , public FileSystemAccessClientEndpoint { IPC_CLIENT_CONNECTION(Client, "/tmp/session/%sid/portal/filesystemaccess"sv) public: Result request_file_read_only_approved(GUI::Window* parent_window, DeprecatedString const& path); Result request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::File::OpenMode requested_access); 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> const& = {}); 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); static Client& the(); protected: void die() override; private: explicit Client(NonnullOwnPtr socket) : IPC::ConnectionToServer(*this, move(socket)) { } virtual void handle_prompt_end(i32 request_id, i32 error, Optional const& fd, Optional const& chosen_file) override; int get_new_id(); Result handle_promise(int); template using PromiseType = RefPtr>; struct PromiseAndWindow { PromiseType promise; GUI::Window* parent_window { nullptr }; }; HashMap m_promises {}; int m_last_id { 0 }; }; }