From 8bcf5b72eb883c4a281b1f9e0cc623d92f836d81 Mon Sep 17 00:00:00 2001 From: Caoimhe Date: Thu, 11 May 2023 12:28:05 +0100 Subject: [PATCH] VideoPlayer: Use `LibFileSystemAccessClient` --- .../VideoPlayer/VideoPlayerWidget.cpp | 24 ++++++++++++------- .../VideoPlayer/VideoPlayerWidget.h | 5 ++-- Userland/Applications/VideoPlayer/main.cpp | 15 +++++++++--- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp b/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp index 3c1bb57880..0b94f77307 100644 --- a/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp +++ b/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp @@ -121,16 +121,21 @@ void VideoPlayerWidget::close_file() m_playback_manager = nullptr; } -void VideoPlayerWidget::open_file(StringView filename) +void VideoPlayerWidget::open_file(FileSystemAccessClient::File file) { - auto load_file_result = Video::PlaybackManager::from_file(filename); + auto mapped_file_result = Core::MappedFile::map_from_file(file.release_stream(), file.filename()); + if (mapped_file_result.is_error()) { + GUI::MessageBox::show_error(window(), String::formatted("Failed to read file: {}", file.filename()).release_value_but_fixme_should_propagate_errors()); + return; + } + auto load_file_result = Video::PlaybackManager::from_mapped_file(mapped_file_result.release_value()); if (load_file_result.is_error()) { on_decoding_error(load_file_result.release_error()); return; } - m_path = filename; + m_path = file.filename(); update_title(); close_file(); @@ -292,7 +297,8 @@ void VideoPlayerWidget::drop_event(GUI::DropEvent& event) auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().serialize_path()); if (response.is_error()) return; - open_file(response.value().filename()); + + open_file(response.release_value()); } } @@ -343,7 +349,7 @@ void VideoPlayerWidget::update_title() if (m_path.is_empty()) { string_builder.append("No video"sv); } else { - string_builder.append(m_path.view()); + string_builder.append(m_path); } string_builder.append("[*] - Video Player"sv); @@ -378,9 +384,11 @@ ErrorOr VideoPlayerWidget::initialize_menubar(GUI::Window& window) // File menu auto file_menu = TRY(window.try_add_menu("&File"_short_string)); TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) { - Optional path = GUI::FilePicker::get_open_filepath(&window, "Open video file..."); - if (path.has_value()) - open_file(path.value()); + auto response = FileSystemAccessClient::Client::the().open_file(&window, "Open video file..."); + if (response.is_error()) + return; + + open_file(response.release_value()); }))); TRY(file_menu->try_add_separator()); TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) { diff --git a/Userland/Applications/VideoPlayer/VideoPlayerWidget.h b/Userland/Applications/VideoPlayer/VideoPlayerWidget.h index 739ec68efd..93385bc2c2 100644 --- a/Userland/Applications/VideoPlayer/VideoPlayerWidget.h +++ b/Userland/Applications/VideoPlayer/VideoPlayerWidget.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -26,7 +27,7 @@ public: static ErrorOr> try_create(); virtual ~VideoPlayerWidget() override = default; void close_file(); - void open_file(StringView filename); + void open_file(FileSystemAccessClient::File filename); void resume_playback(); void pause_playback(); void toggle_pause(); @@ -55,7 +56,7 @@ private: virtual void drop_event(GUI::DropEvent&) override; - DeprecatedString m_path; + String m_path; RefPtr m_video_display; RefPtr m_seek_slider; diff --git a/Userland/Applications/VideoPlayer/main.cpp b/Userland/Applications/VideoPlayer/main.cpp index 24164de59e..aa5ff79bc8 100644 --- a/Userland/Applications/VideoPlayer/main.cpp +++ b/Userland/Applications/VideoPlayer/main.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -29,15 +30,23 @@ ErrorOr serenity_main(Main::Arguments arguments) window->resize(640, 480); window->set_resizable(true); + TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw")); + TRY(Core::System::unveil("/res", "r")); + TRY(Core::System::unveil(nullptr, nullptr)); + auto main_widget = TRY(window->set_main_widget()); main_widget->update_title(); TRY(main_widget->initialize_menubar(window)); - if (!filename.is_empty()) - main_widget->open_file(filename); - window->show(); window->set_icon(GUI::Icon::default_icon("app-video-player"sv).bitmap_for_size(16)); + if (!filename.is_empty()) { + auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, filename); + if (!response.is_error()) { + main_widget->open_file(response.release_value()); + } + } + return app->exec(); }