From e9d73a6256efdc576cd9385f2f4bfcb947f6c7df Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Mon, 26 Dec 2022 23:55:00 +0100 Subject: Spreadsheet: Port ExportDialog to Core::Stream --- Userland/Applications/Spreadsheet/ExportDialog.cpp | 15 ++++----------- Userland/Applications/Spreadsheet/ExportDialog.h | 2 +- Userland/Applications/Spreadsheet/Workbook.cpp | 4 +++- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp index 673a3523cb..b56a295b4c 100644 --- a/Userland/Applications/Spreadsheet/ExportDialog.cpp +++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -169,7 +168,7 @@ void CSVExportDialogPage::update_preview() m_data_preview_text_editor->set_text(DeprecatedString::formatted("Cannot update preview: {}", maybe_error.error())); } -ErrorOr ExportDialog::make_and_run_for(StringView mime, Core::File& file, Workbook& workbook) +ErrorOr ExportDialog::make_and_run_for(StringView mime, NonnullOwnPtr file, DeprecatedString filename, Workbook& workbook) { auto wizard = GUI::WizardDialog::construct(GUI::Application::the()->active_window()); wizard->set_title("File Export Wizard"); @@ -186,8 +185,7 @@ ErrorOr ExportDialog::make_and_run_for(StringView mime, Core::File& file, if (wizard->exec() != GUI::Dialog::ExecResult::OK) return Error::from_string_literal("CSV Export was cancelled"); - auto file_stream = TRY(try_make(TRY(try_make(file)))); - auto writer = TRY(page.make_writer(move(file_stream))); + auto writer = TRY(page.make_writer(move(file))); return writer->generate(); }; @@ -197,12 +195,7 @@ ErrorOr ExportDialog::make_and_run_for(StringView mime, Core::File& file, array.append(sheet.to_json()); auto file_content = array.to_deprecated_string(); - bool result = file.write(file_content); - if (!result) { - return Error::from_string_literal("Unable to save file."); - } - - return {}; + return file->write_entire_buffer(file_content.bytes()); }; if (mime == "text/csv") { @@ -212,7 +205,7 @@ ErrorOr ExportDialog::make_and_run_for(StringView mime, Core::File& file, } else { auto page = GUI::WizardPage::construct( "Export File Format", - DeprecatedString::formatted("Select the format you wish to export to '{}' as", LexicalPath::basename(file.filename()))); + DeprecatedString::formatted("Select the format you wish to export to '{}' as", LexicalPath::basename(filename))); page->on_next_page = [] { return nullptr; }; diff --git a/Userland/Applications/Spreadsheet/ExportDialog.h b/Userland/Applications/Spreadsheet/ExportDialog.h index 75a51066b5..da73500443 100644 --- a/Userland/Applications/Spreadsheet/ExportDialog.h +++ b/Userland/Applications/Spreadsheet/ExportDialog.h @@ -54,7 +54,7 @@ private: }; struct ExportDialog { - static ErrorOr make_and_run_for(StringView mime, Core::File& file, Workbook&); + static ErrorOr make_and_run_for(StringView mime, NonnullOwnPtr, DeprecatedString filename, Workbook&); }; } diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp index beec24f0bc..af2da6a599 100644 --- a/Userland/Applications/Spreadsheet/Workbook.cpp +++ b/Userland/Applications/Spreadsheet/Workbook.cpp @@ -68,8 +68,10 @@ ErrorOr Workbook::write_to_file(Core::File& file) { auto mime = Core::guess_mime_type_based_on_filename(file.filename()); + auto file_stream = TRY(Core::Stream::File::adopt_fd(file.leak_fd(), Core::Stream::OpenMode::Write)); + // Make an export dialog, we might need to import it. - TRY(ExportDialog::make_and_run_for(mime, file, *this)); + TRY(ExportDialog::make_and_run_for(mime, move(file_stream), file.filename(), *this)); set_filename(file.filename()); set_dirty(false); -- cgit v1.2.3