summaryrefslogtreecommitdiff
path: root/Userland/Applications/Spreadsheet
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2023-01-14 14:28:24 -0500
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2023-01-16 17:28:32 +0330
commitc6aeb9811c6ead85de321988720af131ad201dc1 (patch)
tree6174816caaa1ca2ea3faa16d93743a18f76eda6e /Userland/Applications/Spreadsheet
parent81008062a7cab4cd50308011d60bf13cc4913907 (diff)
downloadserenity-c6aeb9811c6ead85de321988720af131ad201dc1.zip
Spreadsheet: Port to `Core::Stream`
It also takes advantage of the new, `Core::Stream`-friendly, interface in `LibFileSystemAccessClient`.
Diffstat (limited to 'Userland/Applications/Spreadsheet')
-rw-r--r--Userland/Applications/Spreadsheet/ExportDialog.cpp6
-rw-r--r--Userland/Applications/Spreadsheet/ExportDialog.h2
-rw-r--r--Userland/Applications/Spreadsheet/ImportDialog.cpp19
-rw-r--r--Userland/Applications/Spreadsheet/ImportDialog.h2
-rw-r--r--Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp28
-rw-r--r--Userland/Applications/Spreadsheet/SpreadsheetWidget.h7
-rw-r--r--Userland/Applications/Spreadsheet/Workbook.cpp28
-rw-r--r--Userland/Applications/Spreadsheet/Workbook.h6
-rw-r--r--Userland/Applications/Spreadsheet/main.cpp8
9 files changed, 52 insertions, 54 deletions
diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp
index d185b1259a..9be6f8d91e 100644
--- a/Userland/Applications/Spreadsheet/ExportDialog.cpp
+++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp
@@ -178,7 +178,7 @@ void CSVExportDialogPage::update_preview()
m_data_preview_text_editor->set_text(DeprecatedString::formatted("Cannot update preview: {}", maybe_error.error()));
}
-ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, NonnullOwnPtr<Core::Stream::File> file, DeprecatedString filename, Workbook& workbook)
+ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, Core::Stream::File& file, DeprecatedString filename, Workbook& workbook)
{
auto wizard = GUI::WizardDialog::construct(GUI::Application::the()->active_window());
wizard->set_title("File Export Wizard");
@@ -195,7 +195,7 @@ ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, NonnullOwnPtr<Core
if (wizard->exec() != GUI::Dialog::ExecResult::OK)
return Error::from_string_literal("CSV Export was cancelled");
- TRY(page.generate(*file, CSVExportDialogPage::GenerationType::Normal));
+ TRY(page.generate(file, CSVExportDialogPage::GenerationType::Normal));
return {};
};
@@ -205,7 +205,7 @@ ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, NonnullOwnPtr<Core
array.append(sheet.to_json());
auto file_content = array.to_deprecated_string();
- return file->write_entire_buffer(file_content.bytes());
+ return file.write_entire_buffer(file_content.bytes());
};
if (mime == "text/csv") {
diff --git a/Userland/Applications/Spreadsheet/ExportDialog.h b/Userland/Applications/Spreadsheet/ExportDialog.h
index 2f07f87653..6890da315e 100644
--- a/Userland/Applications/Spreadsheet/ExportDialog.h
+++ b/Userland/Applications/Spreadsheet/ExportDialog.h
@@ -58,7 +58,7 @@ private:
};
struct ExportDialog {
- static ErrorOr<void> make_and_run_for(StringView mime, NonnullOwnPtr<Core::Stream::File>, DeprecatedString filename, Workbook&);
+ static ErrorOr<void> make_and_run_for(StringView mime, Core::Stream::File&, DeprecatedString filename, Workbook&);
};
}
diff --git a/Userland/Applications/Spreadsheet/ImportDialog.cpp b/Userland/Applications/Spreadsheet/ImportDialog.cpp
index 65197a6cd5..a22c32bfe4 100644
--- a/Userland/Applications/Spreadsheet/ImportDialog.cpp
+++ b/Userland/Applications/Spreadsheet/ImportDialog.cpp
@@ -175,15 +175,17 @@ void CSVImportDialogPage::update_preview()
m_data_preview_table_view->update();
}
-ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook& workbook)
+ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, String const& filename, Core::Stream::File& file, Workbook& workbook)
{
auto wizard = GUI::WizardDialog::construct(&parent);
wizard->set_title("File Import Wizard");
wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16));
auto import_xsv = [&]() -> ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> {
- auto contents = file.read_all();
- CSVImportDialogPage page { contents };
+ auto contents_or_error = file.read_until_eof();
+ if (contents_or_error.is_error())
+ return DeprecatedString::formatted("{}", contents_or_error.release_error());
+ CSVImportDialogPage page { contents_or_error.release_value() };
wizard->replace_page(page.page());
auto result = wizard->exec();
@@ -209,13 +211,16 @@ ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> ImportDialog::make_and_run
};
auto import_worksheet = [&]() -> ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> {
- auto json_value_option = JsonParser(file.read_all()).parse();
+ auto contents_or_error = file.read_until_eof();
+ if (contents_or_error.is_error())
+ return DeprecatedString::formatted("{}", contents_or_error.release_error());
+ auto json_value_option = JsonParser(contents_or_error.release_value()).parse();
if (json_value_option.is_error())
- return DeprecatedString::formatted("Failed to parse {}", file.filename());
+ return DeprecatedString::formatted("Failed to parse {}", filename);
auto& json_value = json_value_option.value();
if (!json_value.is_array())
- return DeprecatedString::formatted("Did not find a spreadsheet in {}", file.filename());
+ return DeprecatedString::formatted("Did not find a spreadsheet in {}", filename);
NonnullRefPtrVector<Sheet> sheets;
@@ -240,7 +245,7 @@ ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> ImportDialog::make_and_run
} else {
auto page = GUI::WizardPage::construct(
"Import File Format",
- DeprecatedString::formatted("Select the format you wish to import '{}' as", LexicalPath::basename(file.filename())));
+ DeprecatedString::formatted("Select the format you wish to import '{}' as", LexicalPath::basename(filename.to_deprecated_string())));
page->on_next_page = [] { return nullptr; };
diff --git a/Userland/Applications/Spreadsheet/ImportDialog.h b/Userland/Applications/Spreadsheet/ImportDialog.h
index bbf7f79529..7e8b5b00f3 100644
--- a/Userland/Applications/Spreadsheet/ImportDialog.h
+++ b/Userland/Applications/Spreadsheet/ImportDialog.h
@@ -55,7 +55,7 @@ private:
};
struct ImportDialog {
- static ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook&);
+ static ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> make_and_run_for(GUI::Window& parent, StringView mime, String const& filename, Core::Stream::File& file, Workbook&);
};
}
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
index bbc9cdf66a..c69a732a1c 100644
--- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
+++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
@@ -127,18 +127,18 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe
if (!request_close())
return;
- auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(window());
+ auto response = FileSystemAccessClient::Client::the().open_file(window());
if (response.is_error())
return;
- load_file(*response.value());
+ load_file(response.value().filename(), response.value().stream());
});
m_import_action = GUI::Action::create("Import sheets...", [&](auto&) {
- auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(window());
+ auto response = FileSystemAccessClient::Client::the().open_file(window());
if (response.is_error())
return;
- import_sheets(*response.value());
+ import_sheets(response.value().filename(), response.value().stream());
});
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
@@ -147,18 +147,18 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe
return;
}
- auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), current_filename(), Core::OpenMode::WriteOnly);
+ auto response = FileSystemAccessClient::Client::the().request_file(window(), current_filename(), Core::Stream::OpenMode::Write);
if (response.is_error())
return;
- save(*response.value());
+ save(response.value().filename(), response.value().stream());
});
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
DeprecatedString name = "workbook";
- auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(window(), name, "sheets");
+ auto response = FileSystemAccessClient::Client::the().save_file(window(), name, "sheets");
if (response.is_error())
return;
- save(*response.value());
+ save(response.value().filename(), response.value().stream());
update_window_title();
});
@@ -493,9 +493,9 @@ void SpreadsheetWidget::change_cell_static_color_format(Spreadsheet::FormatType
}
}
-void SpreadsheetWidget::save(Core::File& file)
+void SpreadsheetWidget::save(String const& filename, Core::Stream::File& file)
{
- auto result = m_workbook->write_to_file(file);
+ auto result = m_workbook->write_to_file(filename, file);
if (result.is_error()) {
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Cannot save file: {}", result.error()));
return;
@@ -504,9 +504,9 @@ void SpreadsheetWidget::save(Core::File& file)
window()->set_modified(false);
}
-void SpreadsheetWidget::load_file(Core::File& file)
+void SpreadsheetWidget::load_file(String const& filename, Core::Stream::File& file)
{
- auto result = m_workbook->open_file(file);
+ auto result = m_workbook->open_file(filename, file);
if (result.is_error()) {
GUI::MessageBox::show_error(window(), result.error());
return;
@@ -523,9 +523,9 @@ void SpreadsheetWidget::load_file(Core::File& file)
update_window_title();
}
-void SpreadsheetWidget::import_sheets(Core::File& file)
+void SpreadsheetWidget::import_sheets(String const& filename, Core::Stream::File& file)
{
- auto result = m_workbook->import_file(file);
+ auto result = m_workbook->import_file(filename, file);
if (result.is_error()) {
GUI::MessageBox::show_error(window(), result.error());
return;
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h
index 3a38156729..63fe723b89 100644
--- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h
+++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h
@@ -9,6 +9,7 @@
#include "SpreadsheetView.h"
#include "Workbook.h"
#include <AK/NonnullRefPtrVector.h>
+#include <LibCore/Stream.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/Widget.h>
@@ -23,9 +24,9 @@ class SpreadsheetWidget final
public:
virtual ~SpreadsheetWidget() override = default;
- void save(Core::File&);
- void load_file(Core::File&);
- void import_sheets(Core::File&);
+ void save(String const& filename, Core::Stream::File&);
+ void load_file(String const& filename, Core::Stream::File&);
+ void import_sheets(String const& filename, Core::Stream::File&);
bool request_close();
void add_sheet();
void add_sheet(NonnullRefPtr<Sheet>&&);
diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp
index 751d4235fb..bc426b2a12 100644
--- a/Userland/Applications/Spreadsheet/Workbook.cpp
+++ b/Userland/Applications/Spreadsheet/Workbook.cpp
@@ -8,13 +8,11 @@
#include "ExportDialog.h"
#include "ImportDialog.h"
#include "JSIntegration.h"
-#include "Readers/CSV.h"
#include <AK/ByteBuffer.h>
#include <AK/StringView.h>
-#include <LibCore/File.h>
#include <LibCore/MimeData.h>
+#include <LibCore/Stream.h>
#include <LibFileSystemAccessClient/Client.h>
-#include <LibGUI/MessageBox.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/Window.h>
#include <LibJS/Runtime/GlobalObject.h>
@@ -52,37 +50,35 @@ bool Workbook::set_filename(DeprecatedString const& filename)
return true;
}
-ErrorOr<void, DeprecatedString> Workbook::open_file(Core::File& file)
+ErrorOr<void, DeprecatedString> Workbook::open_file(String const& filename, Core::Stream::File& file)
{
- auto mime = Core::guess_mime_type_based_on_filename(file.filename());
+ auto mime = Core::guess_mime_type_based_on_filename(filename);
// Make an import dialog, we might need to import it.
- m_sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, file, *this));
+ m_sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, filename, file, *this));
- set_filename(file.filename());
+ set_filename(filename.to_deprecated_string());
return {};
}
-ErrorOr<void> Workbook::write_to_file(Core::File& file)
+ErrorOr<void> Workbook::write_to_file(String const& filename, Core::Stream::File& stream)
{
- 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));
+ auto mime = Core::guess_mime_type_based_on_filename(filename);
// Make an export dialog, we might need to import it.
- TRY(ExportDialog::make_and_run_for(mime, move(file_stream), file.filename(), *this));
+ TRY(ExportDialog::make_and_run_for(mime, stream, filename.to_deprecated_string(), *this));
- set_filename(file.filename());
+ set_filename(filename.to_deprecated_string());
set_dirty(false);
return {};
}
-ErrorOr<bool, DeprecatedString> Workbook::import_file(Core::File& file)
+ErrorOr<bool, DeprecatedString> Workbook::import_file(String const& filename, Core::Stream::File& file)
{
- auto mime = Core::guess_mime_type_based_on_filename(file.filename());
+ auto mime = Core::guess_mime_type_based_on_filename(filename);
- auto sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, file, *this));
+ auto sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, filename, file, *this));
auto has_any_changes = !sheets.is_empty();
m_sheets.extend(move(sheets));
diff --git a/Userland/Applications/Spreadsheet/Workbook.h b/Userland/Applications/Spreadsheet/Workbook.h
index 007cf10ebb..2020a23943 100644
--- a/Userland/Applications/Spreadsheet/Workbook.h
+++ b/Userland/Applications/Spreadsheet/Workbook.h
@@ -16,10 +16,10 @@ class Workbook {
public:
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
- ErrorOr<void, DeprecatedString> open_file(Core::File&);
- ErrorOr<void> write_to_file(Core::File&);
+ ErrorOr<void, DeprecatedString> open_file(String const& filename, Core::Stream::File&);
+ ErrorOr<void> write_to_file(String const& filename, Core::Stream::File&);
- ErrorOr<bool, DeprecatedString> import_file(Core::File&);
+ ErrorOr<bool, DeprecatedString> import_file(String const& filename, Core::Stream::File&);
DeprecatedString const& current_filename() const { return m_current_filename; }
bool set_filename(DeprecatedString const& filename);
diff --git a/Userland/Applications/Spreadsheet/main.cpp b/Userland/Applications/Spreadsheet/main.cpp
index d6ab7f96d8..73bfcab70f 100644
--- a/Userland/Applications/Spreadsheet/main.cpp
+++ b/Userland/Applications/Spreadsheet/main.cpp
@@ -14,15 +14,11 @@
#include <LibCore/System.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Application.h>
-#include <LibGUI/Clipboard.h>
-#include <LibGUI/FilePicker.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
-#include <LibGUI/MessageBox.h>
#include <LibGUI/Window.h>
#include <LibMain/Main.h>
-#include <unistd.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
@@ -69,8 +65,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->show();
if (filename) {
- auto file = TRY(FileSystemAccessClient::Client::the().try_request_file_read_only_approved_deprecated(window, filename));
- spreadsheet_widget->load_file(file);
+ auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, filename));
+ spreadsheet_widget->load_file(file.filename(), file.stream());
}
return app->exec();