From 301f9de915b9991b88d59be5a1f703af1db67e44 Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sat, 17 Dec 2022 17:51:26 +0100 Subject: [PATCH] PixelPaint: Port to `Core::Stream` :^) --- Userland/Applications/PixelPaint/Image.cpp | 19 +++------- Userland/Applications/PixelPaint/Image.h | 7 ++-- .../Applications/PixelPaint/ImageEditor.cpp | 19 +++++----- .../Applications/PixelPaint/ImageEditor.h | 2 +- .../Applications/PixelPaint/MainWidget.cpp | 37 +++++++++---------- Userland/Applications/PixelPaint/MainWidget.h | 3 +- .../Applications/PixelPaint/PaletteWidget.cpp | 28 ++++++-------- .../Applications/PixelPaint/PaletteWidget.h | 7 ++-- .../Applications/PixelPaint/ProjectLoader.cpp | 5 +-- .../Applications/PixelPaint/ProjectLoader.h | 2 +- Userland/Applications/PixelPaint/main.cpp | 4 +- 11 files changed, 59 insertions(+), 74 deletions(-) diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 15d572d71c..a6abb8df08 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -171,40 +171,33 @@ RefPtr Image::try_copy_bitmap(Selection const& selection) const return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors(); } -ErrorOr Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel) const +ErrorOr Image::export_bmp_to_file(NonnullOwnPtr stream, bool preserve_alpha_channel) const { auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888; auto bitmap = TRY(try_compose_bitmap(bitmap_format)); Gfx::BMPWriter dumper; auto encoded_data = dumper.dump(bitmap); - - if (!file.write(encoded_data.data(), encoded_data.size())) - return Error::from_errno(file.error()); - + TRY(stream->write_entire_buffer(encoded_data)); return {}; } -ErrorOr Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel) const +ErrorOr Image::export_png_to_file(NonnullOwnPtr stream, bool preserve_alpha_channel) const { auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888; auto bitmap = TRY(try_compose_bitmap(bitmap_format)); auto encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap)); - if (!file.write(encoded_data.data(), encoded_data.size())) - return Error::from_errno(file.error()); - + TRY(stream->write_entire_buffer(encoded_data)); return {}; } -ErrorOr Image::export_qoi_to_file(Core::File& file) const +ErrorOr Image::export_qoi_to_file(NonnullOwnPtr stream) const { auto bitmap = TRY(try_compose_bitmap(Gfx::BitmapFormat::BGRA8888)); auto encoded_data = Gfx::QOIWriter::encode(bitmap); - if (!file.write(encoded_data.data(), encoded_data.size())) - return Error::from_errno(file.error()); - + TRY(stream->write_entire_buffer(encoded_data)); return {}; } diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index 7ae7b0c7dd..b029056d22 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -73,9 +72,9 @@ public: void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const; ErrorOr serialize_as_json(JsonObjectSerializer& json) const; - ErrorOr export_bmp_to_file(Core::File&, bool preserve_alpha_channel) const; - ErrorOr export_png_to_file(Core::File&, bool preserve_alpha_channel) const; - ErrorOr export_qoi_to_file(Core::File&) const; + ErrorOr export_bmp_to_file(NonnullOwnPtr, bool preserve_alpha_channel) const; + ErrorOr export_png_to_file(NonnullOwnPtr, bool preserve_alpha_channel) const; + ErrorOr export_qoi_to_file(NonnullOwnPtr) const; void move_layer_to_front(Layer&); void move_layer_to_back(Layer&); diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index dca4dbeb4f..6a61f7b23b 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -710,10 +710,10 @@ void ImageEditor::save_project() save_project_as(); return; } - auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), path(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); + auto response = FileSystemAccessClient::Client::the().request_file(window(), path(), Core::Stream::OpenMode::Truncate | Core::Stream::OpenMode::Write); if (response.is_error()) return; - auto result = save_project_to_file(*response.value()); + auto result = save_project_to_file(response.value().release_stream()); if (result.is_error()) { GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Could not save {}: {}", path(), result.error())); return; @@ -723,21 +723,21 @@ void ImageEditor::save_project() void ImageEditor::save_project_as() { - auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(window(), m_title, "pp"); + auto response = FileSystemAccessClient::Client::the().save_file(window(), m_title, "pp"); if (response.is_error()) return; - auto file = response.value(); - auto result = save_project_to_file(*file); + auto file = response.release_value(); + auto result = save_project_to_file(file.release_stream()); if (result.is_error()) { - GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Could not save {}: {}", file->filename(), result.error())); + GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Could not save {}: {}", file.filename(), result.error())); return; } - set_path(file->filename()); + set_path(file.filename().to_deprecated_string()); set_loaded_from_image(false); set_unmodified(); } -ErrorOr ImageEditor::save_project_to_file(Core::File& file) const +ErrorOr ImageEditor::save_project_to_file(NonnullOwnPtr file) const { StringBuilder builder; auto json = TRY(JsonObjectSerializer<>::try_create(builder)); @@ -755,8 +755,7 @@ ErrorOr ImageEditor::save_project_to_file(Core::File& file) const TRY(json_guides.finish()); TRY(json.finish()); - if (!file.write(builder.string_view())) - return Error::from_errno(file.error()); + TRY(file->write_entire_buffer(builder.string_view().bytes())); return {}; } diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index 694a068c84..97da298482 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -151,7 +151,7 @@ private: GUI::MouseEvent event_adjusted_for_layer(GUI::MouseEvent const&, Layer const&) const; GUI::MouseEvent event_with_pan_and_scale_applied(GUI::MouseEvent const&) const; - ErrorOr save_project_to_file(Core::File&) const; + ErrorOr save_project_to_file(NonnullOwnPtr) const; int calculate_ruler_step_size() const; Gfx::IntRect mouse_indicator_rect_x() const; diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index f09f0ccfec..5aa7f3bde7 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -19,9 +19,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -192,10 +190,10 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) }); m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) { - auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(&window); + auto response = FileSystemAccessClient::Client::the().open_file(&window); if (response.is_error()) return; - open_image(response.value()); + open_image(response.release_value()); }); m_save_image_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { @@ -223,11 +221,11 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) "As &BMP", [&](auto&) { auto* editor = current_image_editor(); VERIFY(editor); - auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, editor->title(), "bmp"); + auto response = FileSystemAccessClient::Client::the().save_file(&window, editor->title(), "bmp"); if (response.is_error()) return; auto preserve_alpha_channel = GUI::MessageBox::show(&window, "Do you wish to preserve transparency?"sv, "Preserve transparency?"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); - auto result = editor->image().export_bmp_to_file(response.value(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes); + auto result = editor->image().export_bmp_to_file(response.value().release_stream(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes); if (result.is_error()) GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Export to BMP failed: {}", result.error())); })); @@ -238,11 +236,11 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) auto* editor = current_image_editor(); VERIFY(editor); // TODO: fix bmp on line below? - auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, editor->title(), "png"); + auto response = FileSystemAccessClient::Client::the().save_file(&window, editor->title(), "png"); if (response.is_error()) return; auto preserve_alpha_channel = GUI::MessageBox::show(&window, "Do you wish to preserve transparency?"sv, "Preserve transparency?"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); - auto result = editor->image().export_png_to_file(response.value(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes); + auto result = editor->image().export_png_to_file(response.value().release_stream(), preserve_alpha_channel == GUI::MessageBox::ExecResult::Yes); if (result.is_error()) GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Export to PNG failed: {}", result.error())); })); @@ -252,10 +250,10 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) "As &QOI", [&](auto&) { auto* editor = current_image_editor(); VERIFY(editor); - auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, editor->title(), "qoi"); + auto response = FileSystemAccessClient::Client::the().save_file(&window, editor->title(), "qoi"); if (response.is_error()) return; - auto result = editor->image().export_qoi_to_file(response.value()); + auto result = editor->image().export_qoi_to_file(response.value().release_stream()); if (result.is_error()) GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Export to QOI failed: {}", result.error())); })); @@ -415,11 +413,11 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) })); m_edit_menu->add_action(GUI::Action::create( "&Load Color Palette", g_icon_bag.load_color_palette, [&](auto&) { - auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(&window, "Load Color Palette"); + auto response = FileSystemAccessClient::Client::the().open_file(&window, "Load Color Palette"); if (response.is_error()) return; - auto result = PixelPaint::PaletteWidget::load_palette_file(*response.value()); + auto result = PixelPaint::PaletteWidget::load_palette_file(response.release_value().release_stream()); if (result.is_error()) { GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Loading color palette failed: {}", result.error())); return; @@ -429,11 +427,11 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) })); m_edit_menu->add_action(GUI::Action::create( "Sa&ve Color Palette", g_icon_bag.save_color_palette, [&](auto&) { - auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, "untitled", "palette"); + auto response = FileSystemAccessClient::Client::the().save_file(&window, "untitled", "palette"); if (response.is_error()) return; - auto result = PixelPaint::PaletteWidget::save_palette_file(m_palette_widget->colors(), *response.value()); + auto result = PixelPaint::PaletteWidget::save_palette_file(m_palette_widget->colors(), response.release_value().release_stream()); if (result.is_error()) GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Writing color palette failed: {}", result.error())); })); @@ -1070,10 +1068,9 @@ void MainWidget::set_actions_enabled(bool enabled) m_zoom_combobox->set_enabled(enabled); } -void MainWidget::open_image(Core::File& file) +void MainWidget::open_image(FileSystemAccessClient::File file) { - auto try_load = m_loader.try_load_from_file(file); - + auto try_load = m_loader.try_load_from_file(file.release_stream()); if (try_load.is_error()) { GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Unable to open file: {}, {}", file.filename(), try_load.error())); return; @@ -1082,7 +1079,7 @@ void MainWidget::open_image(Core::File& file) auto& image = *m_loader.release_image(); auto& editor = create_new_editor(image); editor.set_loaded_from_image(m_loader.is_raw_image()); - editor.set_path(file.filename()); + editor.set_path(file.filename().to_deprecated_string()); editor.set_unmodified(); m_layer_list_widget->set_image(&image); } @@ -1264,10 +1261,10 @@ void MainWidget::drop_event(GUI::DropEvent& event) if (url.scheme() != "file") continue; - auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), url.path(), Core::OpenMode::ReadOnly); + auto response = FileSystemAccessClient::Client::the().request_file(window(), url.path(), Core::Stream::OpenMode::Read); if (response.is_error()) return; - open_image(response.value()); + open_image(response.release_value()); } } diff --git a/Userland/Applications/PixelPaint/MainWidget.h b/Userland/Applications/PixelPaint/MainWidget.h index 8ffb548da3..b30bc5b222 100644 --- a/Userland/Applications/PixelPaint/MainWidget.h +++ b/Userland/Applications/PixelPaint/MainWidget.h @@ -20,6 +20,7 @@ #include "ToolboxWidget.h" #include "Tools/Tool.h" #include "VectorscopeWidget.h" +#include #include #include #include @@ -40,7 +41,7 @@ public: ErrorOr initialize_menubar(GUI::Window&); - void open_image(Core::File&); + void open_image(FileSystemAccessClient::File); ErrorOr create_default_image(); bool request_close(); diff --git a/Userland/Applications/PixelPaint/PaletteWidget.cpp b/Userland/Applications/PixelPaint/PaletteWidget.cpp index a5a3fd836a..96fb38965a 100644 --- a/Userland/Applications/PixelPaint/PaletteWidget.cpp +++ b/Userland/Applications/PixelPaint/PaletteWidget.cpp @@ -11,7 +11,6 @@ #include "ImageEditor.h" #include #include -#include #include #include #include @@ -225,11 +224,14 @@ Vector PaletteWidget::colors() return colors; } -Result, DeprecatedString> PaletteWidget::load_palette_file(Core::File& file) +ErrorOr> PaletteWidget::load_palette_file(NonnullOwnPtr file) { Vector palette; + Array buffer; + auto buffered_file = TRY(Core::Stream::BufferedFile::create(move(file))); - for (auto line : file.lines()) { + while (TRY(buffered_file->can_read_line())) { + auto line = TRY(buffered_file->read_line(buffer)); if (line.is_whitespace()) continue; @@ -242,29 +244,23 @@ Result, DeprecatedString> PaletteWidget::load_palette_file(Core::F palette.append(color.value()); } - file.close(); - if (palette.is_empty()) - return DeprecatedString { "The palette file did not contain any usable colors"sv }; + return Error::from_string_literal("The palette file did not contain any usable colors"); return palette; } -Result, DeprecatedString> PaletteWidget::load_palette_path(DeprecatedString const& file_path) +ErrorOr> PaletteWidget::load_palette_path(DeprecatedString const& file_path) { - auto file_or_error = Core::File::open(file_path, Core::OpenMode::ReadOnly); - if (file_or_error.is_error()) - return DeprecatedString { strerror(file_or_error.error().code()) }; - - auto& file = *file_or_error.value(); - return load_palette_file(file); + auto file = TRY(Core::Stream::File::open(file_path, Core::Stream::OpenMode::Read)); + return load_palette_file(move(file)); } -Result PaletteWidget::save_palette_file(Vector palette, Core::File& file) +ErrorOr PaletteWidget::save_palette_file(Vector palette, NonnullOwnPtr file) { for (auto& color : palette) { - file.write(color.to_deprecated_string_without_alpha()); - file.write("\n"sv); + TRY(file->write_entire_buffer(color.to_deprecated_string_without_alpha().bytes())); + TRY(file->write_entire_buffer({ "\n", 1 })); } return {}; } diff --git a/Userland/Applications/PixelPaint/PaletteWidget.h b/Userland/Applications/PixelPaint/PaletteWidget.h index c2914bcc73..aedfce3438 100644 --- a/Userland/Applications/PixelPaint/PaletteWidget.h +++ b/Userland/Applications/PixelPaint/PaletteWidget.h @@ -31,9 +31,10 @@ public: Vector colors(); - static Result, DeprecatedString> load_palette_file(Core::File&); - static Result, DeprecatedString> load_palette_path(DeprecatedString const&); - static Result save_palette_file(Vector, Core::File&); + static ErrorOr> load_palette_file(NonnullOwnPtr); + static ErrorOr> load_palette_path(DeprecatedString const&); + static ErrorOr save_palette_file(Vector, NonnullOwnPtr); + static Vector fallback_colors(); void set_image_editor(ImageEditor*); diff --git a/Userland/Applications/PixelPaint/ProjectLoader.cpp b/Userland/Applications/PixelPaint/ProjectLoader.cpp index 6acc8d963b..fd9077cc16 100644 --- a/Userland/Applications/PixelPaint/ProjectLoader.cpp +++ b/Userland/Applications/PixelPaint/ProjectLoader.cpp @@ -10,15 +10,14 @@ #include #include #include -#include #include #include namespace PixelPaint { -ErrorOr ProjectLoader::try_load_from_file(Core::File& file) +ErrorOr ProjectLoader::try_load_from_file(NonnullOwnPtr file) { - auto contents = file.read_all(); + auto contents = TRY(file->read_until_eof()); auto json_or_error = JsonValue::from_string(contents); if (json_or_error.is_error()) { diff --git a/Userland/Applications/PixelPaint/ProjectLoader.h b/Userland/Applications/PixelPaint/ProjectLoader.h index 0fc7a059d8..3acf3f0cdd 100644 --- a/Userland/Applications/PixelPaint/ProjectLoader.h +++ b/Userland/Applications/PixelPaint/ProjectLoader.h @@ -18,7 +18,7 @@ public: ProjectLoader() = default; ~ProjectLoader() = default; - ErrorOr try_load_from_file(Core::File&); + ErrorOr try_load_from_file(NonnullOwnPtr); bool is_raw_image() const { return m_is_raw_image; } bool has_image() const { return !m_image.is_null(); } diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 05aa370b24..7f3cf01acf 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -74,10 +74,10 @@ ErrorOr serenity_main(Main::Arguments arguments) window->show(); if (image_file) { - auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved_deprecated(window, image_file); + auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, image_file); if (response.is_error()) return 1; - main_widget->open_image(response.value()); + main_widget->open_image(response.release_value()); } else { TRY(main_widget->create_default_image()); }