HexEditor: Use FileSystemAccessClient::try_* APIs

This commit is contained in:
Mustafa Quraish 2022-01-16 03:41:34 -05:00 committed by Andreas Kling
parent 0c98e553e8
commit aae96af812
Notes: sideshowbarker 2024-07-17 20:34:38 +09:00
5 changed files with 21 additions and 58 deletions

View file

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

View file

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

View file

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

View file

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

View file

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