ladybird/Userland/Libraries/LibFileSystemAccessClient/Client.h
Mustafa Quraish 0c98e553e8 FileSystemAccessClient: Add try_* variants returning Core::File
The current implementation is a bit of a hack since we also want to keep
around the previous variants for now, but will be cleaned up later once
all applications have been ported to the new API.
2022-01-20 10:39:12 +01:00

61 lines
2.5 KiB
C++

/*
* Copyright (c) 2021, timmot <tiwwot@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <FileSystemAccessServer/FileSystemAccessClientEndpoint.h>
#include <FileSystemAccessServer/FileSystemAccessServerEndpoint.h>
#include <LibCore/File.h>
#include <LibCore/Promise.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/Window.h>
#include <LibIPC/ServerConnection.h>
namespace FileSystemAccessClient {
struct Result {
i32 error;
Optional<i32> fd;
Optional<String> chosen_file;
};
using FileResult = ErrorOr<NonnullRefPtr<Core::File>>;
class Client final
: public IPC::ServerConnection<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>
, public FileSystemAccessClientEndpoint {
IPC_CLIENT_CONNECTION(Client, "/tmp/portal/filesystemaccess")
public:
Result request_file_read_only_approved(i32 parent_window_id, String const& path);
Result request_file(i32 parent_window_id, String const& path, Core::OpenMode mode);
Result open_file(i32 parent_window_id, String const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), Core::OpenMode requested_access = Core::OpenMode::ReadOnly);
Result save_file(i32 parent_window_id, String const& name, String const ext, Core::OpenMode requested_access = Core::OpenMode::WriteOnly | Core::OpenMode::Truncate);
FileResult try_request_file_read_only_approved(GUI::Window* parent_window, String const& path);
FileResult try_request_file(GUI::Window* parent_window, String const& path, Core::OpenMode mode);
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);
FileResult try_save_file(GUI::Window* parent_window, String const& name, String const ext, Core::OpenMode requested_access = Core::OpenMode::WriteOnly | Core::OpenMode::Truncate);
static Client& the();
protected:
void die() override;
private:
explicit Client(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
: IPC::ServerConnection<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket))
{
}
virtual void handle_prompt_end(i32 error, Optional<IPC::File> const& fd, Optional<String> const& chosen_file) override;
RefPtr<Core::Promise<Result>> m_promise;
RefPtr<Core::Promise<FileResult>> m_file_promise;
GUI::Window* m_parent_window { nullptr };
};
}