diff options
-rw-r--r-- | Userland/Applications/TextEditor/MainWidget.cpp | 72 | ||||
-rw-r--r-- | Userland/Applications/TextEditor/MainWidget.h | 2 | ||||
-rw-r--r-- | Userland/Applications/TextEditor/main.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.h | 2 |
5 files changed, 33 insertions, 71 deletions
diff --git a/Userland/Applications/TextEditor/MainWidget.cpp b/Userland/Applications/TextEditor/MainWidget.cpp index 5ec40b832f..1622cb87be 100644 --- a/Userland/Applications/TextEditor/MainWidget.cpp +++ b/Userland/Applications/TextEditor/MainWidget.cpp @@ -266,13 +266,9 @@ MainWidget::MainWidget() }); m_open_action = GUI::CommonActions::make_open_action([this](auto&) { - auto response = FileSystemAccessClient::Client::the().open_file(window()->window_id()); - - if (response.error != 0) { - if (response.error != -1) - GUI::MessageBox::show_error(window(), String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error))); + auto response = FileSystemAccessClient::Client::the().try_open_file(window()); + if (response.is_error()) return; - } if (editor().document().is_modified()) { auto save_document_first_result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path, editor().document().undo_stack().last_unmodified_timestamp()); @@ -282,25 +278,22 @@ MainWidget::MainWidget() return; } - read_file_and_close(*response.fd, *response.chosen_file); + read_file(*response.value()); }); m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { - auto response = FileSystemAccessClient::Client::the().save_file(window()->window_id(), m_name, m_extension); - - if (response.error != 0) { - if (response.error != -1) - GUI::MessageBox::show_error(window(), String::formatted("Saving \"{}\" failed: {}", *response.chosen_file, strerror(response.error))); + auto response = FileSystemAccessClient::Client::the().try_save_file(window(), m_name, m_extension); + if (response.is_error()) return; - } - if (!m_editor->write_to_file_and_close(*response.fd)) { + auto file = response.release_value(); + if (!m_editor->write_to_file(*file)) { GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); return; } - set_path(*response.chosen_file); - dbgln("Wrote document to {}", *response.chosen_file); + set_path(file->filename()); + dbgln("Wrote document to {}", file->filename()); }); m_save_action = GUI::CommonActions::make_save_action([&](auto&) { @@ -308,17 +301,11 @@ MainWidget::MainWidget() m_save_as_action->activate(); return; } - auto response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); - - if (response.error != 0) { - if (response.error != -1) - GUI::MessageBox::show_error(window(), String::formatted("Unable to save file: {}", strerror(response.error))); + auto response = FileSystemAccessClient::Client::the().try_request_file(window(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); + if (response.is_error()) return; - } - - int fd = *response.fd; - if (!m_editor->write_to_file_and_close(fd)) { + if (!m_editor->write_to_file(*response.value())) { GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); } }); @@ -688,32 +675,11 @@ void MainWidget::update_title() window()->set_title(builder.to_string()); } -bool MainWidget::read_file_and_close(int fd, String const& path) +bool MainWidget::read_file(Core::File& file) { - VERIFY(path.starts_with("/"sv)); - auto file = Core::File::construct(); - - if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) { - GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error); - return false; - } - - if (file->is_device()) { - GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error); - return false; - } - - if (file->is_directory()) { - GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open directories", path), "Error", GUI::MessageBox::Type::Error); - return false; - } - - m_editor->set_text(file->read_all()); - - set_path(path); - + m_editor->set_text(file.read_all()); + set_path(file.filename()); m_editor->set_focus(true); - return true; } @@ -758,12 +724,10 @@ void MainWidget::drop_event(GUI::DropEvent& event) } // TODO: A drop event should be considered user consent for opening a file - auto file_response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), urls.first().path(), Core::OpenMode::ReadOnly); - - if (file_response.error != 0) + auto response = FileSystemAccessClient::Client::the().try_request_file(window(), urls.first().path(), Core::OpenMode::ReadOnly); + if (response.is_error()) return; - - read_file_and_close(*file_response.fd, urls.first().path()); + read_file(*response.value()); } } diff --git a/Userland/Applications/TextEditor/MainWidget.h b/Userland/Applications/TextEditor/MainWidget.h index b61289eb9b..4f2be8a589 100644 --- a/Userland/Applications/TextEditor/MainWidget.h +++ b/Userland/Applications/TextEditor/MainWidget.h @@ -24,7 +24,7 @@ class MainWidget final : public GUI::Widget { public: virtual ~MainWidget() override; - bool read_file_and_close(int fd, String const& path); + bool read_file(Core::File&); void open_nonexistent_file(String const& path); bool request_close(); diff --git a/Userland/Applications/TextEditor/main.cpp b/Userland/Applications/TextEditor/main.cpp index 2a0c9fce5e..da086644c4 100644 --- a/Userland/Applications/TextEditor/main.cpp +++ b/Userland/Applications/TextEditor/main.cpp @@ -74,14 +74,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) if (file_to_edit) { FileArgument parsed_argument(file_to_edit); - auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), parsed_argument.filename()); + auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, parsed_argument.filename()); - if (response.error == 0) { - if (!text_widget->read_file_and_close(*response.fd, *response.chosen_file)) + if (response.is_error() && response.error().code() == ENOENT) { + text_widget->open_nonexistent_file(parsed_argument.filename()); + } else { + if (!text_widget->read_file(*response.value())) return 1; text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0)); - } else { - text_widget->open_nonexistent_file(parsed_argument.filename()); } } text_widget->update_title(); diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index f0e56a1166..d47d204e77 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -1260,19 +1260,17 @@ void TextEditor::timer_event(Core::TimerEvent&) bool TextEditor::write_to_file(String const& path) { - int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666); - if (fd < 0) { - perror("open"); + auto file = Core::File::construct(path); + if (!file->open(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)) { + warnln("Error opening {}: {}", path, strerror(file->error())); return false; } - return write_to_file_and_close(fd); + return write_to_file(*file); } -bool TextEditor::write_to_file_and_close(int fd) +bool TextEditor::write_to_file(Core::File& file) { - ScopeGuard fd_guard = [fd] { close(fd); }; - off_t file_size = 0; if (line_count() == 1 && line(0).is_empty()) { // Truncate to zero. @@ -1284,7 +1282,7 @@ bool TextEditor::write_to_file_and_close(int fd) file_size += line_count(); } - if (ftruncate(fd, file_size) < 0) { + if (!file.truncate(file_size)) { perror("ftruncate"); return false; } @@ -1296,14 +1294,14 @@ bool TextEditor::write_to_file_and_close(int fd) auto& line = this->line(i); if (line.length()) { auto line_as_utf8 = line.to_utf8(); - ssize_t nwritten = write(fd, line_as_utf8.characters(), line_as_utf8.length()); + ssize_t nwritten = file.write(line_as_utf8); if (nwritten < 0) { perror("write"); return false; } } char ch = '\n'; - ssize_t nwritten = write(fd, &ch, 1); + ssize_t nwritten = file.write((u8*)&ch, 1); if (nwritten != 1) { perror("write"); return false; diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index ea4e76ffa2..937638bb3d 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -123,7 +123,7 @@ public: void insert_at_cursor_or_replace_selection(StringView); bool write_to_file(String const& path); - bool write_to_file_and_close(int fd); + bool write_to_file(Core::File&); bool has_selection() const { return m_selection.is_valid(); } String selected_text() const; size_t number_of_words() const; |