1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:07:44 +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

@ -115,13 +115,8 @@ void HexEditor::set_position(size_t position)
update_status(); 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) { if (m_document->type() == HexDocument::Type::File) {
HexDocumentFile* fileDocument = static_cast<HexDocumentFile*>(m_document.ptr()); HexDocumentFile* fileDocument = static_cast<HexDocumentFile*>(m_document.ptr());
if (!fileDocument->write_to_file(new_file)) if (!fileDocument->write_to_file(new_file))

View file

@ -37,7 +37,7 @@ public:
bool open_new_file(size_t size); bool open_new_file(size_t size);
void open_file(NonnullRefPtr<Core::File> file); void open_file(NonnullRefPtr<Core::File> file);
void fill_selection(u8 fill_byte); void fill_selection(u8 fill_byte);
bool save_as(int fd); bool save_as(NonnullRefPtr<Core::File>);
bool save(); bool save();
void select_all(); void select_all();

View file

@ -88,13 +88,9 @@ HexEditorWidget::HexEditorWidget()
}); });
m_open_action = GUI::CommonActions::make_open_action([this](auto&) { 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); auto response = FileSystemAccessClient::Client::the().try_open_file(window(), {}, Core::StandardPaths::home_directory(), Core::OpenMode::ReadWrite);
if (response.is_error())
if (response.error != 0) {
if (response.error != -1)
GUI::MessageBox::show_error(window(), String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
return; return;
}
if (m_document_dirty) { 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); 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; return;
} }
open_file(*response.fd, *response.chosen_file); open_file(response.value());
}); });
m_save_action = GUI::CommonActions::make_save_action([&](auto&) { 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&) { 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); auto response = FileSystemAccessClient::Client::the().try_save_file(window(), m_name, m_extension, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate);
if (response.is_error())
if (response.error != 0) {
if (response.error != -1)
GUI::MessageBox::show_error(window(), String::formatted("Saving \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
return; return;
} auto file = response.release_value();
if (!m_editor->save_as(file)) {
if (!m_editor->save_as(*response.fd)) {
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
return; return;
} }
m_document_dirty = false; m_document_dirty = false;
set_path(*response.chosen_file); set_path(file->filename());
dbgln("Wrote document to {}", *response.chosen_file); 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&) { 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()); 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_document_dirty = false;
m_editor->open_file(file); m_editor->open_file(file);
set_path(path); set_path(file->filename());
} }
bool HexEditorWidget::request_close() bool HexEditorWidget::request_close()
@ -400,11 +374,9 @@ void HexEditorWidget::drop_event(GUI::DropEvent& event)
window()->move_to_front(); window()->move_to_front();
// TODO: A drop event should be considered user consent for opening a file // 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); auto response = FileSystemAccessClient::Client::the().try_request_file(window(), urls.first().path(), Core::OpenMode::ReadOnly);
if (response.is_error())
if (file_response.error != 0)
return; return;
open_file(response.value());
open_file(*file_response.fd, urls.first().path());
} }
} }

View file

@ -22,7 +22,7 @@ class HexEditorWidget final : public GUI::Widget {
C_OBJECT(HexEditorWidget) C_OBJECT(HexEditorWidget)
public: public:
virtual ~HexEditorWidget() override; virtual ~HexEditorWidget() override;
void open_file(int fd, String const& path); void open_file(NonnullRefPtr<Core::File>);
void initialize_menubar(GUI::Window&); void initialize_menubar(GUI::Window&);
bool request_close(); 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)); window->set_icon(app_icon.bitmap_for_size(16));
if (arguments.argc > 1) { if (arguments.argc > 1) {
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), arguments.strings[1]); // 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.error != 0) { if (response.is_error())
if (response.error != -1)
GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
return 1; return 1;
} hex_editor_widget->open_file(response.value());
hex_editor_widget->open_file(*response.fd, *response.chosen_file);
} }
return app->exec(); return app->exec();