diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2022-01-16 22:43:40 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-20 10:39:12 +0100 |
commit | f674102447a1e120988e94009ab571d3f9a0aad7 (patch) | |
tree | 45b44db44f31e6af629575875c89357f81590694 /Userland/Applications | |
parent | effb19f9966eead7f1d93fecced3b62fb40f30bc (diff) | |
download | serenity-f674102447a1e120988e94009ab571d3f9a0aad7.zip |
TextEditor: Use FileSystemAccessClient::try_* APIs
Diffstat (limited to 'Userland/Applications')
-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 |
3 files changed, 24 insertions, 60 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(); |