Selaa lähdekoodia

HexEditor: Use FileSystemAccessClient::try_* APIs

Mustafa Quraish 3 vuotta sitten
vanhempi
commit
aae96af812

+ 1 - 6
Userland/Applications/HexEditor/HexEditor.cpp

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

+ 1 - 1
Userland/Applications/HexEditor/HexEditor.h

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

+ 14 - 42
Userland/Applications/HexEditor/HexEditorWidget.cpp

@@ -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);
-
-        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;
             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);
-
-        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;
             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);
             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);
-        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&) {
     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);
-
-        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;
             return;
-
-        open_file(*file_response.fd, urls.first().path());
+        open_file(response.value());
     }
     }
 }
 }

+ 1 - 1
Userland/Applications/HexEditor/HexEditorWidget.h

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

+ 4 - 8
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));
     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]);
-
-        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;
             return 1;
-        }
-
-        hex_editor_widget->open_file(*response.fd, *response.chosen_file);
+        hex_editor_widget->open_file(response.value());
     }
     }
 
 
     return app->exec();
     return app->exec();