Przeglądaj źródła

TextEditor: Use FileSystemAccessClient::try_* APIs

Mustafa Quraish 3 lat temu
rodzic
commit
f674102447

+ 18 - 54
Userland/Applications/TextEditor/MainWidget.cpp

@@ -266,13 +266,9 @@ MainWidget::MainWidget()
     });
     });
 
 
     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());
-
-        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());
+        if (response.is_error())
             return;
             return;
-        }
 
 
         if (editor().document().is_modified()) {
         if (editor().document().is_modified()) {
             auto save_document_first_result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path, editor().document().undo_stack().last_unmodified_timestamp());
             auto save_document_first_result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path, editor().document().undo_stack().last_unmodified_timestamp());
@@ -282,25 +278,22 @@ MainWidget::MainWidget()
                 return;
                 return;
         }
         }
 
 
-        read_file_and_close(*response.fd, *response.chosen_file);
+        read_file(*response.value());
     });
     });
 
 
     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);
-
-        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);
+        if (response.is_error())
             return;
             return;
-        }
 
 
-        if (!m_editor->write_to_file_and_close(*response.fd)) {
+        auto file = response.release_value();
+        if (!m_editor->write_to_file(*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;
         }
         }
 
 
-        set_path(*response.chosen_file);
-        dbgln("Wrote document to {}", *response.chosen_file);
+        set_path(file->filename());
+        dbgln("Wrote document to {}", file->filename());
     });
     });
 
 
     m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
     m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
@@ -308,17 +301,11 @@ MainWidget::MainWidget()
             m_save_as_action->activate();
             m_save_as_action->activate();
             return;
             return;
         }
         }
-        auto response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
-
-        if (response.error != 0) {
-            if (response.error != -1)
-                GUI::MessageBox::show_error(window(), String::formatted("Unable to save file: {}", strerror(response.error)));
+        auto response = FileSystemAccessClient::Client::the().try_request_file(window(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
+        if (response.is_error())
             return;
             return;
-        }
-
-        int fd = *response.fd;
 
 
-        if (!m_editor->write_to_file_and_close(fd)) {
+        if (!m_editor->write_to_file(*response.value())) {
             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);
         }
         }
     });
     });
@@ -688,32 +675,11 @@ void MainWidget::update_title()
     window()->set_title(builder.to_string());
     window()->set_title(builder.to_string());
 }
 }
 
 
-bool MainWidget::read_file_and_close(int fd, String const& path)
+bool MainWidget::read_file(Core::File& file)
 {
 {
-    VERIFY(path.starts_with("/"sv));
-    auto file = Core::File::construct();
-
-    if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) {
-        GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
-        return false;
-    }
-
-    if (file->is_device()) {
-        GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error);
-        return false;
-    }
-
-    if (file->is_directory()) {
-        GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open directories", path), "Error", GUI::MessageBox::Type::Error);
-        return false;
-    }
-
-    m_editor->set_text(file->read_all());
-
-    set_path(path);
-
+    m_editor->set_text(file.read_all());
+    set_path(file.filename());
     m_editor->set_focus(true);
     m_editor->set_focus(true);
-
     return true;
     return true;
 }
 }
 
 
@@ -758,12 +724,10 @@ void MainWidget::drop_event(GUI::DropEvent& event)
         }
         }
 
 
         // 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;
-
-        read_file_and_close(*file_response.fd, urls.first().path());
+        read_file(*response.value());
     }
     }
 }
 }
 
 

+ 1 - 1
Userland/Applications/TextEditor/MainWidget.h

@@ -24,7 +24,7 @@ class MainWidget final : public GUI::Widget {
 
 
 public:
 public:
     virtual ~MainWidget() override;
     virtual ~MainWidget() override;
-    bool read_file_and_close(int fd, String const& path);
+    bool read_file(Core::File&);
     void open_nonexistent_file(String const& path);
     void open_nonexistent_file(String const& path);
     bool request_close();
     bool request_close();
 
 

+ 5 - 5
Userland/Applications/TextEditor/main.cpp

@@ -74,14 +74,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 
 
     if (file_to_edit) {
     if (file_to_edit) {
         FileArgument parsed_argument(file_to_edit);
         FileArgument parsed_argument(file_to_edit);
-        auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), parsed_argument.filename());
+        auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, parsed_argument.filename());
 
 
-        if (response.error == 0) {
-            if (!text_widget->read_file_and_close(*response.fd, *response.chosen_file))
+        if (response.is_error() && response.error().code() == ENOENT) {
+            text_widget->open_nonexistent_file(parsed_argument.filename());
+        } else {
+            if (!text_widget->read_file(*response.value()))
                 return 1;
                 return 1;
             text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
             text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
-        } else {
-            text_widget->open_nonexistent_file(parsed_argument.filename());
         }
         }
     }
     }
     text_widget->update_title();
     text_widget->update_title();

+ 8 - 10
Userland/Libraries/LibGUI/TextEditor.cpp

@@ -1260,19 +1260,17 @@ void TextEditor::timer_event(Core::TimerEvent&)
 
 
 bool TextEditor::write_to_file(String const& path)
 bool TextEditor::write_to_file(String const& path)
 {
 {
-    int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
-    if (fd < 0) {
-        perror("open");
+    auto file = Core::File::construct(path);
+    if (!file->open(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)) {
+        warnln("Error opening {}: {}", path, strerror(file->error()));
         return false;
         return false;
     }
     }
 
 
-    return write_to_file_and_close(fd);
+    return write_to_file(*file);
 }
 }
 
 
-bool TextEditor::write_to_file_and_close(int fd)
+bool TextEditor::write_to_file(Core::File& file)
 {
 {
-    ScopeGuard fd_guard = [fd] { close(fd); };
-
     off_t file_size = 0;
     off_t file_size = 0;
     if (line_count() == 1 && line(0).is_empty()) {
     if (line_count() == 1 && line(0).is_empty()) {
         // Truncate to zero.
         // Truncate to zero.
@@ -1284,7 +1282,7 @@ bool TextEditor::write_to_file_and_close(int fd)
         file_size += line_count();
         file_size += line_count();
     }
     }
 
 
-    if (ftruncate(fd, file_size) < 0) {
+    if (!file.truncate(file_size)) {
         perror("ftruncate");
         perror("ftruncate");
         return false;
         return false;
     }
     }
@@ -1296,14 +1294,14 @@ bool TextEditor::write_to_file_and_close(int fd)
             auto& line = this->line(i);
             auto& line = this->line(i);
             if (line.length()) {
             if (line.length()) {
                 auto line_as_utf8 = line.to_utf8();
                 auto line_as_utf8 = line.to_utf8();
-                ssize_t nwritten = write(fd, line_as_utf8.characters(), line_as_utf8.length());
+                ssize_t nwritten = file.write(line_as_utf8);
                 if (nwritten < 0) {
                 if (nwritten < 0) {
                     perror("write");
                     perror("write");
                     return false;
                     return false;
                 }
                 }
             }
             }
             char ch = '\n';
             char ch = '\n';
-            ssize_t nwritten = write(fd, &ch, 1);
+            ssize_t nwritten = file.write((u8*)&ch, 1);
             if (nwritten != 1) {
             if (nwritten != 1) {
                 perror("write");
                 perror("write");
                 return false;
                 return false;

+ 1 - 1
Userland/Libraries/LibGUI/TextEditor.h

@@ -123,7 +123,7 @@ public:
 
 
     void insert_at_cursor_or_replace_selection(StringView);
     void insert_at_cursor_or_replace_selection(StringView);
     bool write_to_file(String const& path);
     bool write_to_file(String const& path);
-    bool write_to_file_and_close(int fd);
+    bool write_to_file(Core::File&);
     bool has_selection() const { return m_selection.is_valid(); }
     bool has_selection() const { return m_selection.is_valid(); }
     String selected_text() const;
     String selected_text() const;
     size_t number_of_words() const;
     size_t number_of_words() const;