From 9d7345d17bc5f2949a9f928c4bd759ec8d7b03e4 Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sun, 7 Aug 2022 00:51:41 +0200 Subject: [PATCH] Chess: Import/Export chessboards using LibFileSystemAccessClient With this change, the wpath and cpath promises as well as unveiling user's entire home directory are no longer needed. :^) --- Userland/Games/Chess/CMakeLists.txt | 2 +- Userland/Games/Chess/ChessWidget.cpp | 24 ++------------------ Userland/Games/Chess/ChessWidget.h | 4 ++-- Userland/Games/Chess/main.cpp | 34 +++++++++------------------- 4 files changed, 16 insertions(+), 48 deletions(-) diff --git a/Userland/Games/Chess/CMakeLists.txt b/Userland/Games/Chess/CMakeLists.txt index bfde3a5e36..1a5cb0e780 100644 --- a/Userland/Games/Chess/CMakeLists.txt +++ b/Userland/Games/Chess/CMakeLists.txt @@ -13,4 +13,4 @@ set(SOURCES ) serenity_app(Chess ICON app-chess) -target_link_libraries(Chess LibChess LibConfig LibGUI LibCore LibMain LibDesktop) +target_link_libraries(Chess LibChess LibConfig LibFileSystemAccessClient LibGUI LibCore LibMain LibDesktop) diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp index 4f1cb192b5..57f4b3904b 100644 --- a/Userland/Games/Chess/ChessWidget.cpp +++ b/Userland/Games/Chess/ChessWidget.cpp @@ -518,15 +518,8 @@ String ChessWidget::get_fen() const return m_playback ? m_board_playback.to_fen() : m_board.to_fen(); } -bool ChessWidget::import_pgn(StringView import_path) +void ChessWidget::import_pgn(Core::File& file) { - auto file_or_error = Core::File::open(import_path, Core::OpenMode::ReadOnly); - if (file_or_error.is_error()) { - warnln("Couldn't open '{}': {}", import_path, file_or_error.error()); - return false; - } - auto& file = *file_or_error.value(); - m_board = Chess::Board(); ByteBuffer bytes = file.read_all(); @@ -618,20 +611,10 @@ bool ChessWidget::import_pgn(StringView import_path) m_playback_move_number = m_board_playback.moves().size(); m_playback = true; update(); - - file.close(); - return true; } -bool ChessWidget::export_pgn(StringView export_path) const +void ChessWidget::export_pgn(Core::File& file) const { - auto file_or_error = Core::File::open(export_path, Core::OpenMode::WriteOnly); - if (file_or_error.is_error()) { - warnln("Couldn't open '{}': {}", export_path, file_or_error.error()); - return false; - } - auto& file = *file_or_error.value(); - // Tag Pair Section file.write("[Event \"Casual Game\"]\n"sv); file.write("[Site \"SerenityOS Chess\"]\n"sv); @@ -669,9 +652,6 @@ bool ChessWidget::export_pgn(StringView export_path) const file.write(" } "sv); file.write(Chess::Board::result_to_points(m_board.game_result(), m_board.turn())); file.write("\n"sv); - - file.close(); - return true; } void ChessWidget::flip_board() diff --git a/Userland/Games/Chess/ChessWidget.h b/Userland/Games/Chess/ChessWidget.h index ad80b767f5..0a1b6db321 100644 --- a/Userland/Games/Chess/ChessWidget.h +++ b/Userland/Games/Chess/ChessWidget.h @@ -48,8 +48,8 @@ public: void set_show_available_moves(bool e) { m_show_available_moves = e; } String get_fen() const; - bool import_pgn(StringView import_path); - bool export_pgn(StringView export_path) const; + void import_pgn(Core::File&); + void export_pgn(Core::File&) const; int resign(); void flip_board(); diff --git a/Userland/Games/Chess/main.cpp b/Userland/Games/Chess/main.cpp index a323cdc151..8fe0d46f2b 100644 --- a/Userland/Games/Chess/main.cpp +++ b/Userland/Games/Chess/main.cpp @@ -9,10 +9,10 @@ #include #include #include +#include #include #include #include -#include #include #include #include @@ -22,7 +22,7 @@ ErrorOr serenity_main(Main::Arguments arguments) { - TRY(Core::System::pledge("stdio rpath wpath cpath recvfd sendfd thread proc exec unix")); + TRY(Core::System::pledge("stdio rpath recvfd sendfd thread proc exec unix")); auto app = TRY(GUI::Application::try_create(arguments)); @@ -31,8 +31,6 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man6/Chess.md") })); TRY(Desktop::Launcher::seal_allowlist()); - TRY(Core::System::pledge("stdio rpath wpath cpath recvfd sendfd thread proc exec")); - auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-chess"sv)); auto window = TRY(GUI::Window::try_create()); @@ -42,7 +40,7 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::unveil("/bin/ChessEngine", "x")); TRY(Core::System::unveil("/etc/passwd", "r")); TRY(Core::System::unveil("/tmp/100/portal/launch", "rw")); - TRY(Core::System::unveil(Core::StandardPaths::home_directory(), "wcbr"sv)); + TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw")); TRY(Core::System::unveil(nullptr, nullptr)); auto size = Config::read_i32("Chess"sv, "Display"sv, "size"sv, 512); @@ -69,30 +67,20 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(game_menu->try_add_separator()); TRY(game_menu->try_add_action(GUI::Action::create("&Import PGN...", { Mod_Ctrl, Key_O }, [&](auto&) { - Optional import_path = GUI::FilePicker::get_open_filepath(window); - - if (!import_path.has_value()) + auto result = FileSystemAccessClient::Client::the().try_open_file(window); + if (result.is_error()) return; - if (!widget->import_pgn(import_path.value())) { - GUI::MessageBox::show(window, "Unable to import game.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); - return; - } - - dbgln("Imported PGN file from {}", import_path.value()); + widget->import_pgn(result.value()); + dbgln("Imported PGN file from {}", result.value()->filename()); }))); TRY(game_menu->try_add_action(GUI::Action::create("&Export PGN...", { Mod_Ctrl, Key_S }, [&](auto&) { - Optional export_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "pgn"); - - if (!export_path.has_value()) + auto result = FileSystemAccessClient::Client::the().try_save_file(window, "Untitled", "pgn"); + if (result.is_error()) return; - if (!widget->export_pgn(export_path.value())) { - GUI::MessageBox::show(window, "Unable to export game.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); - return; - } - - dbgln("Exported PGN file to {}", export_path.value()); + widget->export_pgn(result.value()); + dbgln("Exported PGN file to {}", result.value()->filename()); }))); TRY(game_menu->try_add_action(GUI::Action::create("&Copy FEN", { Mod_Ctrl, Key_C }, [&](auto&) { GUI::Clipboard::the().set_data(widget->get_fen().bytes());