mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:57:35 +00:00
Spreadsheet: Make file export functions return ErrorOr<>
This commit is contained in:
parent
29a3cdcfb7
commit
5793f7749c
5 changed files with 59 additions and 68 deletions
|
@ -90,51 +90,50 @@ CSVExportDialogPage::CSVExportDialogPage(Sheet const& sheet)
|
||||||
update_preview();
|
update_preview();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto CSVExportDialogPage::make_writer(OutputStream& stream) -> Optional<XSV>
|
auto CSVExportDialogPage::make_writer(OutputStream& stream) -> ErrorOr<XSV>
|
||||||
{
|
{
|
||||||
DeprecatedString delimiter;
|
auto delimiter = TRY([this]() -> ErrorOr<DeprecatedString> {
|
||||||
DeprecatedString quote;
|
if (m_delimiter_other_radio->is_checked()) {
|
||||||
Writer::WriterTraits::QuoteEscape quote_escape;
|
if (m_delimiter_other_text_box->text().is_empty())
|
||||||
|
return Error::from_string_literal("Delimiter unset");
|
||||||
|
return m_delimiter_other_text_box->text();
|
||||||
|
}
|
||||||
|
if (m_delimiter_comma_radio->is_checked())
|
||||||
|
return ",";
|
||||||
|
if (m_delimiter_semicolon_radio->is_checked())
|
||||||
|
return ";";
|
||||||
|
if (m_delimiter_tab_radio->is_checked())
|
||||||
|
return "\t";
|
||||||
|
if (m_delimiter_space_radio->is_checked())
|
||||||
|
return " ";
|
||||||
|
return Error::from_string_literal("Delimiter unset");
|
||||||
|
}());
|
||||||
|
|
||||||
// Delimiter
|
auto quote = TRY([this]() -> ErrorOr<DeprecatedString> {
|
||||||
if (m_delimiter_other_radio->is_checked())
|
if (m_quote_other_radio->is_checked()) {
|
||||||
delimiter = m_delimiter_other_text_box->text();
|
if (m_quote_other_text_box->text().is_empty())
|
||||||
else if (m_delimiter_comma_radio->is_checked())
|
return Error::from_string_literal("Quote separator unset");
|
||||||
delimiter = ",";
|
return m_quote_other_text_box->text();
|
||||||
else if (m_delimiter_semicolon_radio->is_checked())
|
}
|
||||||
delimiter = ";";
|
if (m_quote_single_radio->is_checked())
|
||||||
else if (m_delimiter_tab_radio->is_checked())
|
return "'";
|
||||||
delimiter = "\t";
|
if (m_quote_double_radio->is_checked())
|
||||||
else if (m_delimiter_space_radio->is_checked())
|
return "\"";
|
||||||
delimiter = " ";
|
return Error::from_string_literal("Quote separator unset");
|
||||||
else
|
}());
|
||||||
return {};
|
|
||||||
|
|
||||||
// Quote separator
|
auto quote_escape = [this]() {
|
||||||
if (m_quote_other_radio->is_checked())
|
auto index = m_quote_escape_combo_box->selected_index();
|
||||||
quote = m_quote_other_text_box->text();
|
if (index == 0)
|
||||||
else if (m_quote_single_radio->is_checked())
|
return Writer::WriterTraits::Repeat;
|
||||||
quote = "'";
|
if (index == 1)
|
||||||
else if (m_quote_double_radio->is_checked())
|
return Writer::WriterTraits::Backslash;
|
||||||
quote = "\"";
|
VERIFY_NOT_REACHED();
|
||||||
else
|
}();
|
||||||
return {};
|
|
||||||
|
|
||||||
// Quote escape
|
|
||||||
auto index = m_quote_escape_combo_box->selected_index();
|
|
||||||
if (index == 0)
|
|
||||||
quote_escape = Writer::WriterTraits::Repeat;
|
|
||||||
else if (index == 1)
|
|
||||||
quote_escape = Writer::WriterTraits::Backslash;
|
|
||||||
else
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto should_export_headers = m_export_header_check_box->is_checked();
|
auto should_export_headers = m_export_header_check_box->is_checked();
|
||||||
auto should_quote_all_fields = m_quote_all_fields_check_box->is_checked();
|
auto should_quote_all_fields = m_quote_all_fields_check_box->is_checked();
|
||||||
|
|
||||||
if (quote.is_empty() || delimiter.is_empty())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
Writer::WriterTraits traits {
|
Writer::WriterTraits traits {
|
||||||
move(delimiter),
|
move(delimiter),
|
||||||
move(quote),
|
move(quote),
|
||||||
|
@ -159,46 +158,45 @@ auto CSVExportDialogPage::make_writer(OutputStream& stream) -> Optional<XSV>
|
||||||
void CSVExportDialogPage::update_preview()
|
void CSVExportDialogPage::update_preview()
|
||||||
{
|
{
|
||||||
DuplexMemoryStream memory_stream;
|
DuplexMemoryStream memory_stream;
|
||||||
auto maybe_writer = make_writer(memory_stream);
|
auto writer_or_error = make_writer(memory_stream);
|
||||||
if (!maybe_writer.has_value()) {
|
if (!writer_or_error.is_error()) {
|
||||||
m_data_preview_text_editor->set_text({});
|
m_data_preview_text_editor->set_text(DeprecatedString::formatted("Cannot update preview: {}", writer_or_error.error()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
auto writer = writer_or_error.release_value();
|
||||||
|
|
||||||
maybe_writer->generate_preview();
|
writer.generate_preview();
|
||||||
auto buffer = memory_stream.copy_into_contiguous_buffer();
|
auto buffer = memory_stream.copy_into_contiguous_buffer();
|
||||||
m_data_preview_text_editor->set_text(StringView(buffer));
|
m_data_preview_text_editor->set_text(StringView(buffer));
|
||||||
m_data_preview_text_editor->update();
|
m_data_preview_text_editor->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void, DeprecatedString> ExportDialog::make_and_run_for(StringView mime, Core::File& file, Workbook& workbook)
|
ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, Core::File& file, Workbook& workbook)
|
||||||
{
|
{
|
||||||
auto wizard = GUI::WizardDialog::construct(GUI::Application::the()->active_window());
|
auto wizard = GUI::WizardDialog::construct(GUI::Application::the()->active_window());
|
||||||
wizard->set_title("File Export Wizard");
|
wizard->set_title("File Export Wizard");
|
||||||
wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16));
|
wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16));
|
||||||
|
|
||||||
auto export_xsv = [&]() -> Result<void, DeprecatedString> {
|
auto export_xsv = [&]() -> ErrorOr<void> {
|
||||||
// FIXME: Prompt for the user to select a specific sheet to export
|
// FIXME: Prompt for the user to select a specific sheet to export
|
||||||
// For now, export the first sheet (if available)
|
// For now, export the first sheet (if available)
|
||||||
if (!workbook.has_sheets())
|
if (!workbook.has_sheets())
|
||||||
return DeprecatedString { "The workbook has no sheets to export!" };
|
return Error::from_string_literal("The workbook has no sheets to export!");
|
||||||
|
|
||||||
CSVExportDialogPage page { workbook.sheets().first() };
|
CSVExportDialogPage page { workbook.sheets().first() };
|
||||||
wizard->replace_page(page.page());
|
wizard->replace_page(page.page());
|
||||||
if (wizard->exec() != GUI::Dialog::ExecResult::OK)
|
if (wizard->exec() != GUI::Dialog::ExecResult::OK)
|
||||||
return DeprecatedString { "CSV Export was cancelled" };
|
return Error::from_string_literal("CSV Export was cancelled");
|
||||||
|
|
||||||
auto file_stream = Core::OutputFileStream(file);
|
auto file_stream = Core::OutputFileStream(file);
|
||||||
auto writer = page.make_writer(file_stream);
|
auto writer = TRY(page.make_writer(file_stream));
|
||||||
if (!writer.has_value())
|
writer.generate();
|
||||||
return DeprecatedString::formatted("CSV Export failed");
|
if (writer.has_error())
|
||||||
writer->generate();
|
return Error::from_string_literal("CSV Export failed");
|
||||||
if (writer->has_error())
|
|
||||||
return DeprecatedString::formatted("CSV Export failed: {}", writer->error_string());
|
|
||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
auto export_worksheet = [&]() -> Result<void, DeprecatedString> {
|
auto export_worksheet = [&]() -> ErrorOr<void> {
|
||||||
JsonArray array;
|
JsonArray array;
|
||||||
for (auto& sheet : workbook.sheets())
|
for (auto& sheet : workbook.sheets())
|
||||||
array.append(sheet.to_json());
|
array.append(sheet.to_json());
|
||||||
|
@ -206,14 +204,7 @@ Result<void, DeprecatedString> ExportDialog::make_and_run_for(StringView mime, C
|
||||||
auto file_content = array.to_deprecated_string();
|
auto file_content = array.to_deprecated_string();
|
||||||
bool result = file.write(file_content);
|
bool result = file.write(file_content);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
int error_number = errno;
|
return Error::from_string_literal("Unable to save file.");
|
||||||
auto const* error = strerror(error_number);
|
|
||||||
|
|
||||||
StringBuilder sb;
|
|
||||||
sb.append("Unable to save file. Error: "sv);
|
|
||||||
sb.append({ error, strlen(error) });
|
|
||||||
|
|
||||||
return sb.to_deprecated_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
@ -242,7 +233,7 @@ Result<void, DeprecatedString> ExportDialog::make_and_run_for(StringView mime, C
|
||||||
wizard->push_page(page);
|
wizard->push_page(page);
|
||||||
|
|
||||||
if (wizard->exec() != GUI::Dialog::ExecResult::OK)
|
if (wizard->exec() != GUI::Dialog::ExecResult::OK)
|
||||||
return DeprecatedString { "Export was cancelled" };
|
return Error::from_string_literal("Export was cancelled");
|
||||||
|
|
||||||
if (format_combo_box->selected_index() == 0)
|
if (format_combo_box->selected_index() == 0)
|
||||||
return export_xsv();
|
return export_xsv();
|
||||||
|
|
|
@ -23,7 +23,7 @@ struct CSVExportDialogPage {
|
||||||
explicit CSVExportDialogPage(Sheet const&);
|
explicit CSVExportDialogPage(Sheet const&);
|
||||||
|
|
||||||
NonnullRefPtr<GUI::WizardPage> page() { return *m_page; }
|
NonnullRefPtr<GUI::WizardPage> page() { return *m_page; }
|
||||||
Optional<XSV> make_writer(OutputStream&);
|
ErrorOr<XSV> make_writer(OutputStream&);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void update_preview();
|
void update_preview();
|
||||||
|
@ -54,7 +54,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ExportDialog {
|
struct ExportDialog {
|
||||||
static Result<void, DeprecatedString> make_and_run_for(StringView mime, Core::File& file, Workbook&);
|
static ErrorOr<void> make_and_run_for(StringView mime, Core::File& file, Workbook&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -497,7 +497,7 @@ void SpreadsheetWidget::save(Core::File& file)
|
||||||
{
|
{
|
||||||
auto result = m_workbook->write_to_file(file);
|
auto result = m_workbook->write_to_file(file);
|
||||||
if (result.is_error()) {
|
if (result.is_error()) {
|
||||||
GUI::MessageBox::show_error(window(), result.error());
|
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Cannot save file: {}", result.error()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
undo_stack().set_current_unmodified();
|
undo_stack().set_current_unmodified();
|
||||||
|
|
|
@ -64,7 +64,7 @@ Result<bool, DeprecatedString> Workbook::open_file(Core::File& file)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<bool, DeprecatedString> Workbook::write_to_file(Core::File& file)
|
ErrorOr<void> Workbook::write_to_file(Core::File& file)
|
||||||
{
|
{
|
||||||
auto mime = Core::guess_mime_type_based_on_filename(file.filename());
|
auto mime = Core::guess_mime_type_based_on_filename(file.filename());
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ Result<bool, DeprecatedString> Workbook::write_to_file(Core::File& file)
|
||||||
|
|
||||||
set_filename(file.filename());
|
set_filename(file.filename());
|
||||||
set_dirty(false);
|
set_dirty(false);
|
||||||
return true;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<bool, DeprecatedString> Workbook::import_file(Core::File& file)
|
Result<bool, DeprecatedString> Workbook::import_file(Core::File& file)
|
||||||
|
|
|
@ -18,7 +18,7 @@ public:
|
||||||
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
|
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
|
||||||
|
|
||||||
Result<bool, DeprecatedString> open_file(Core::File&);
|
Result<bool, DeprecatedString> open_file(Core::File&);
|
||||||
Result<bool, DeprecatedString> write_to_file(Core::File&);
|
ErrorOr<void> write_to_file(Core::File&);
|
||||||
|
|
||||||
Result<bool, DeprecatedString> import_file(Core::File&);
|
Result<bool, DeprecatedString> import_file(Core::File&);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue