1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:57:45 +00:00

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
5 changed files with 21 additions and 58 deletions

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