diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2022-01-16 03:41:34 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-20 10:39:12 +0100 |
commit | aae96af8120dec103d0bbd781d6e52ec07104305 (patch) | |
tree | 54fdf64558f638b9531a54d37e4f7aef6196116e | |
parent | 0c98e553e8a49b5fdbee21c4520ee3547e14061d (diff) | |
download | serenity-aae96af8120dec103d0bbd781d6e52ec07104305.zip |
HexEditor: Use FileSystemAccessClient::try_* APIs
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.cpp | 7 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.h | 2 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditorWidget.cpp | 56 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditorWidget.h | 2 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/main.cpp | 12 |
5 files changed, 21 insertions, 58 deletions
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index fefb21ef0b..e6da87403e 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -115,13 +115,8 @@ void HexEditor::set_position(size_t position) update_status(); } -bool HexEditor::save_as(int fd) +bool HexEditor::save_as(NonnullRefPtr<Core::File> new_file) { - auto new_file = Core::File::construct(); - if (!new_file->open(fd, Core::OpenMode::ReadWrite, Core::File::ShouldCloseFileDescriptor::Yes)) { - return false; - } - if (m_document->type() == HexDocument::Type::File) { HexDocumentFile* fileDocument = static_cast<HexDocumentFile*>(m_document.ptr()); if (!fileDocument->write_to_file(new_file)) diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 4b17233274..2ad80a8a4c 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -37,7 +37,7 @@ public: bool open_new_file(size_t size); void open_file(NonnullRefPtr<Core::File> file); void fill_selection(u8 fill_byte); - bool save_as(int fd); + bool save_as(NonnullRefPtr<Core::File>); bool save(); void select_all(); diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index b40cc47700..da3d4a1114 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -88,13 +88,9 @@ HexEditorWidget::HexEditorWidget() }); m_open_action = GUI::CommonActions::make_open_action([this](auto&) { - auto response = FileSystemAccessClient::Client::the().open_file(window()->window_id(), {}, Core::StandardPaths::home_directory(), Core::OpenMode::ReadWrite); - - 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(), {}, Core::StandardPaths::home_directory(), Core::OpenMode::ReadWrite); + if (response.is_error()) return; - } if (m_document_dirty) { auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel); @@ -104,7 +100,7 @@ HexEditorWidget::HexEditorWidget() return; } - open_file(*response.fd, *response.chosen_file); + open_file(response.value()); }); m_save_action = GUI::CommonActions::make_save_action([&](auto&) { @@ -122,22 +118,18 @@ HexEditorWidget::HexEditorWidget() }); 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, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate); - - 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, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate); + if (response.is_error()) return; - } - - if (!m_editor->save_as(*response.fd)) { + auto file = response.release_value(); + if (!m_editor->save_as(file)) { GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); return; } m_document_dirty = false; - set_path(*response.chosen_file); - dbgln("Wrote document to {}", *response.chosen_file); + set_path(file->filename()); + dbgln("Wrote document to {}", file->filename()); }); m_find_action = GUI::Action::create("&Find", { Mod_Ctrl, Key_F }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) { @@ -343,29 +335,11 @@ void HexEditorWidget::update_title() window()->set_title(builder.to_string()); } -void HexEditorWidget::open_file(int fd, String const& path) +void HexEditorWidget::open_file(NonnullRefPtr<Core::File> file) { - VERIFY(path.starts_with("/"sv)); - auto file = Core::File::construct(); - - if (!file->open(fd, Core::OpenMode::ReadWrite, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) { - GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error); - return; - } - - if (file->is_device()) { - GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error); - return; - } - - if (file->is_directory()) { - GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open directories", path), "Error", GUI::MessageBox::Type::Error); - return; - } - m_document_dirty = false; m_editor->open_file(file); - set_path(path); + set_path(file->filename()); } bool HexEditorWidget::request_close() @@ -400,11 +374,9 @@ void HexEditorWidget::drop_event(GUI::DropEvent& event) window()->move_to_front(); // 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; - - open_file(*file_response.fd, urls.first().path()); + open_file(response.value()); } } diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h index 85df85b356..f07f5fe2fc 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.h +++ b/Userland/Applications/HexEditor/HexEditorWidget.h @@ -22,7 +22,7 @@ class HexEditorWidget final : public GUI::Widget { C_OBJECT(HexEditorWidget) public: virtual ~HexEditorWidget() override; - void open_file(int fd, String const& path); + void open_file(NonnullRefPtr<Core::File>); void initialize_menubar(GUI::Window&); bool request_close(); diff --git a/Userland/Applications/HexEditor/main.cpp b/Userland/Applications/HexEditor/main.cpp index 2b05310605..e008675136 100644 --- a/Userland/Applications/HexEditor/main.cpp +++ b/Userland/Applications/HexEditor/main.cpp @@ -48,15 +48,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) window->set_icon(app_icon.bitmap_for_size(16)); if (arguments.argc > 1) { - auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), arguments.strings[1]); - - if (response.error != 0) { - if (response.error != -1) - GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error))); + // FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable. + auto response = FileSystemAccessClient::Client::the().try_request_file(window, arguments.strings[1], Core::OpenMode::ReadWrite); + if (response.is_error()) return 1; - } - - hex_editor_widget->open_file(*response.fd, *response.chosen_file); + hex_editor_widget->open_file(response.value()); } return app->exec(); |