Spreadsheet: Port to Core::Stream

It also takes advantage of the new, `Core::Stream`-friendly, interface
in `LibFileSystemAccessClient`.
This commit is contained in:
Lucas CHOLLET 2023-01-14 14:28:24 -05:00 committed by Ali Mohammad Pur
parent 81008062a7
commit c6aeb9811c
Notes: sideshowbarker 2024-07-17 02:56:25 +09:00
9 changed files with 52 additions and 54 deletions

View file

@ -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") {

View file

@ -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&);
};
}

View file

@ -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; };

View file

@ -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&);
};
}

View file

@ -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;

View file

@ -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>&&);

View file

@ -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));

View file

@ -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);

View file

@ -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();