summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarol Kosek <krkk@serenityos.org>2022-12-31 15:38:12 +0100
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2023-01-07 04:03:01 +0330
commit5793f7749c951a36b0c9c35852a056f8e97ca5a0 (patch)
treeb5f3a70a24ea360085aa704233e0d5d3046bf04c
parent29a3cdcfb7d1026bf46893a2fd5b792de8f4fb4c (diff)
downloadserenity-5793f7749c951a36b0c9c35852a056f8e97ca5a0.zip
Spreadsheet: Make file export functions return ErrorOr<>
-rw-r--r--Userland/Applications/Spreadsheet/ExportDialog.cpp119
-rw-r--r--Userland/Applications/Spreadsheet/ExportDialog.h4
-rw-r--r--Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp2
-rw-r--r--Userland/Applications/Spreadsheet/Workbook.cpp4
-rw-r--r--Userland/Applications/Spreadsheet/Workbook.h2
5 files changed, 61 insertions, 70 deletions
diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp
index b415ebf372..653cea3bd4 100644
--- a/Userland/Applications/Spreadsheet/ExportDialog.cpp
+++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp
@@ -90,51 +90,50 @@ CSVExportDialogPage::CSVExportDialogPage(Sheet const& sheet)
update_preview();
}
-auto CSVExportDialogPage::make_writer(OutputStream& stream) -> Optional<XSV>
+auto CSVExportDialogPage::make_writer(OutputStream& stream) -> ErrorOr<XSV>
{
- DeprecatedString delimiter;
- DeprecatedString quote;
- Writer::WriterTraits::QuoteEscape quote_escape;
-
- // Delimiter
- if (m_delimiter_other_radio->is_checked())
- delimiter = m_delimiter_other_text_box->text();
- else if (m_delimiter_comma_radio->is_checked())
- delimiter = ",";
- else if (m_delimiter_semicolon_radio->is_checked())
- delimiter = ";";
- else if (m_delimiter_tab_radio->is_checked())
- delimiter = "\t";
- else if (m_delimiter_space_radio->is_checked())
- delimiter = " ";
- else
- return {};
-
- // Quote separator
- if (m_quote_other_radio->is_checked())
- quote = m_quote_other_text_box->text();
- else if (m_quote_single_radio->is_checked())
- quote = "'";
- else if (m_quote_double_radio->is_checked())
- quote = "\"";
- 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 delimiter = TRY([this]() -> ErrorOr<DeprecatedString> {
+ if (m_delimiter_other_radio->is_checked()) {
+ 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");
+ }());
+
+ auto quote = TRY([this]() -> ErrorOr<DeprecatedString> {
+ if (m_quote_other_radio->is_checked()) {
+ if (m_quote_other_text_box->text().is_empty())
+ return Error::from_string_literal("Quote separator unset");
+ return m_quote_other_text_box->text();
+ }
+ if (m_quote_single_radio->is_checked())
+ return "'";
+ if (m_quote_double_radio->is_checked())
+ return "\"";
+ return Error::from_string_literal("Quote separator unset");
+ }());
+
+ auto quote_escape = [this]() {
+ auto index = m_quote_escape_combo_box->selected_index();
+ if (index == 0)
+ return Writer::WriterTraits::Repeat;
+ if (index == 1)
+ return Writer::WriterTraits::Backslash;
+ VERIFY_NOT_REACHED();
+ }();
auto should_export_headers = m_export_header_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 {
move(delimiter),
move(quote),
@@ -159,46 +158,45 @@ auto CSVExportDialogPage::make_writer(OutputStream& stream) -> Optional<XSV>
void CSVExportDialogPage::update_preview()
{
DuplexMemoryStream memory_stream;
- auto maybe_writer = make_writer(memory_stream);
- if (!maybe_writer.has_value()) {
- m_data_preview_text_editor->set_text({});
+ auto writer_or_error = make_writer(memory_stream);
+ if (!writer_or_error.is_error()) {
+ m_data_preview_text_editor->set_text(DeprecatedString::formatted("Cannot update preview: {}", writer_or_error.error()));
return;
}
+ auto writer = writer_or_error.release_value();
- maybe_writer->generate_preview();
+ writer.generate_preview();
auto buffer = memory_stream.copy_into_contiguous_buffer();
m_data_preview_text_editor->set_text(StringView(buffer));
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());
wizard->set_title("File Export Wizard");
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
// For now, export the first sheet (if available)
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() };
wizard->replace_page(page.page());
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 writer = page.make_writer(file_stream);
- if (!writer.has_value())
- return DeprecatedString::formatted("CSV Export failed");
- writer->generate();
- if (writer->has_error())
- return DeprecatedString::formatted("CSV Export failed: {}", writer->error_string());
+ auto writer = TRY(page.make_writer(file_stream));
+ writer.generate();
+ if (writer.has_error())
+ return Error::from_string_literal("CSV Export failed");
return {};
};
- auto export_worksheet = [&]() -> Result<void, DeprecatedString> {
+ auto export_worksheet = [&]() -> ErrorOr<void> {
JsonArray array;
for (auto& sheet : workbook.sheets())
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();
bool result = file.write(file_content);
if (!result) {
- int error_number = errno;
- 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 Error::from_string_literal("Unable to save file.");
}
return {};
@@ -242,7 +233,7 @@ Result<void, DeprecatedString> ExportDialog::make_and_run_for(StringView mime, C
wizard->push_page(page);
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)
return export_xsv();
diff --git a/Userland/Applications/Spreadsheet/ExportDialog.h b/Userland/Applications/Spreadsheet/ExportDialog.h
index c249c065f5..4b4600a136 100644
--- a/Userland/Applications/Spreadsheet/ExportDialog.h
+++ b/Userland/Applications/Spreadsheet/ExportDialog.h
@@ -23,7 +23,7 @@ struct CSVExportDialogPage {
explicit CSVExportDialogPage(Sheet const&);
NonnullRefPtr<GUI::WizardPage> page() { return *m_page; }
- Optional<XSV> make_writer(OutputStream&);
+ ErrorOr<XSV> make_writer(OutputStream&);
protected:
void update_preview();
@@ -54,7 +54,7 @@ private:
};
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&);
};
}
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
index 5500f28a41..6d4668e43a 100644
--- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
+++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
@@ -497,7 +497,7 @@ void SpreadsheetWidget::save(Core::File& file)
{
auto result = m_workbook->write_to_file(file);
if (result.is_error()) {
- GUI::MessageBox::show_error(window(), result.error());
+ GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Cannot save file: {}", result.error()));
return;
}
undo_stack().set_current_unmodified();
diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp
index 04b8823945..beec24f0bc 100644
--- a/Userland/Applications/Spreadsheet/Workbook.cpp
+++ b/Userland/Applications/Spreadsheet/Workbook.cpp
@@ -64,7 +64,7 @@ Result<bool, DeprecatedString> Workbook::open_file(Core::File& file)
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());
@@ -73,7 +73,7 @@ Result<bool, DeprecatedString> Workbook::write_to_file(Core::File& file)
set_filename(file.filename());
set_dirty(false);
- return true;
+ return {};
}
Result<bool, DeprecatedString> Workbook::import_file(Core::File& file)
diff --git a/Userland/Applications/Spreadsheet/Workbook.h b/Userland/Applications/Spreadsheet/Workbook.h
index 34adc76929..6529b064f7 100644
--- a/Userland/Applications/Spreadsheet/Workbook.h
+++ b/Userland/Applications/Spreadsheet/Workbook.h
@@ -18,7 +18,7 @@ public:
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
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&);