TextEditor: Port to Core::Stream

This commit is contained in:
Lucas CHOLLET 2023-01-14 17:45:51 -05:00 committed by Jelle Raaijmakers
parent 36e66502c9
commit 40f9cf2de6
3 changed files with 19 additions and 16 deletions

View file

@ -272,11 +272,11 @@ MainWidget::MainWidget()
return; return;
} }
auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(window()); auto response = FileSystemAccessClient::Client::the().open_file(window());
if (response.is_error()) if (response.is_error())
return; return;
read_file(*response.value()); read_file(response.value().filename(), response.value().stream());
}); });
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
@ -284,18 +284,18 @@ MainWidget::MainWidget()
if (extension.is_null() && m_editor->syntax_highlighter()) if (extension.is_null() && m_editor->syntax_highlighter())
extension = Syntax::common_language_extension(m_editor->syntax_highlighter()->language()); extension = Syntax::common_language_extension(m_editor->syntax_highlighter()->language());
auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(window(), m_name, extension); auto response = FileSystemAccessClient::Client::the().save_file(window(), m_name, extension);
if (response.is_error()) if (response.is_error())
return; return;
auto file = response.release_value(); auto file = response.release_value();
if (!m_editor->write_to_file(*file)) { if (auto result = m_editor->write_to_file(file.stream()); result.is_error()) {
GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error);
return; return;
} }
set_path(file->filename()); set_path(file.filename());
dbgln("Wrote document to {}", file->filename()); dbgln("Wrote document to {}", file.filename());
}); });
m_save_action = GUI::CommonActions::make_save_action([&](auto&) { m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
@ -303,11 +303,11 @@ MainWidget::MainWidget()
m_save_as_action->activate(); m_save_as_action->activate();
return; return;
} }
auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); auto response = FileSystemAccessClient::Client::the().request_file(window(), m_path, Core::Stream::OpenMode::Truncate | Core::Stream::OpenMode::Write);
if (response.is_error()) if (response.is_error())
return; return;
if (!m_editor->write_to_file(*response.value())) { if (auto result = m_editor->write_to_file(response.value().stream()); result.is_error()) {
GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error);
} }
}); });
@ -746,10 +746,13 @@ void MainWidget::update_title()
window()->set_title(builder.to_deprecated_string()); window()->set_title(builder.to_deprecated_string());
} }
bool MainWidget::read_file(Core::File& file) bool MainWidget::read_file(String const& filename, Core::Stream::File& file)
{ {
m_editor->set_text(file.read_all()); auto result = file.read_until_eof();
set_path(file.filename()); if (result.is_error())
return false;
m_editor->set_text(result.value());
set_path(filename);
m_editor->set_focus(true); m_editor->set_focus(true);
return true; return true;
} }
@ -804,10 +807,10 @@ void MainWidget::drop_event(GUI::DropEvent& event)
return; return;
// TODO: A drop event should be considered user consent for opening a file // TODO: A drop event should be considered user consent for opening a file
auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), urls.first().path(), Core::OpenMode::ReadOnly); auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().path(), Core::Stream::OpenMode::Read);
if (response.is_error()) if (response.is_error())
return; return;
read_file(*response.value()); read_file(response.value().filename(), response.value().stream());
} }
} }

View file

@ -25,7 +25,7 @@ class MainWidget final : public GUI::Widget {
public: public:
virtual ~MainWidget() override = default; virtual ~MainWidget() override = default;
bool read_file(Core::File&); bool read_file(String const& filename, Core::Stream::File&);
void open_nonexistent_file(DeprecatedString const& path); void open_nonexistent_file(DeprecatedString const& path);
bool request_close(); bool request_close();

View file

@ -73,7 +73,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (file_to_edit) { if (file_to_edit) {
FileArgument parsed_argument(file_to_edit); FileArgument parsed_argument(file_to_edit);
auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved_deprecated(window, parsed_argument.filename()); auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, parsed_argument.filename());
if (response.is_error()) { if (response.is_error()) {
if (response.error().code() == ENOENT) if (response.error().code() == ENOENT)
@ -81,7 +81,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
else else
return 1; return 1;
} else { } else {
if (!text_widget->read_file(*response.value())) if (!text_widget->read_file(response.value().filename(), response.value().stream()))
return 1; return 1;
text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0)); text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
} }