diff options
author | Edgar Araújo <edgararaj@gmail.com> | 2021-03-18 22:07:59 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-19 09:15:43 +0100 |
commit | 5df34f656760a5091d98c9926a874dfd5f74e42a (patch) | |
tree | 492086a39ccf7442bab6f6a0093e3eb2ba7f5095 | |
parent | 67f01fd82eb74bc4eabb1273a300d17784167283 (diff) | |
download | serenity-5df34f656760a5091d98c9926a874dfd5f74e42a.zip |
TextEditor: Exit program when file is not opened
When trying to open files not permitted by the user, display the error
message and exit.
-rw-r--r-- | Userland/Applications/TextEditor/TextEditorWidget.cpp | 6 | ||||
-rw-r--r-- | Userland/Applications/TextEditor/TextEditorWidget.h | 2 | ||||
-rw-r--r-- | Userland/Applications/TextEditor/main.cpp | 7 |
3 files changed, 9 insertions, 6 deletions
diff --git a/Userland/Applications/TextEditor/TextEditorWidget.cpp b/Userland/Applications/TextEditor/TextEditorWidget.cpp index 9c03b15a3a..1c95f69616 100644 --- a/Userland/Applications/TextEditor/TextEditorWidget.cpp +++ b/Userland/Applications/TextEditor/TextEditorWidget.cpp @@ -583,12 +583,12 @@ void TextEditorWidget::update_title() window()->set_title(builder.to_string()); } -void TextEditorWidget::open_file(const String& path) +bool TextEditorWidget::open_file(const String& path) { auto file = Core::File::construct(path); if (!file->open(Core::IODevice::ReadOnly) && file->error() != ENOENT) { GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error); - return; + return false; } m_editor->set_text(file->read_all()); @@ -598,6 +598,8 @@ void TextEditorWidget::open_file(const String& path) set_path(LexicalPath(path)); m_editor->set_focus(true); + + return true; } bool TextEditorWidget::request_close() diff --git a/Userland/Applications/TextEditor/TextEditorWidget.h b/Userland/Applications/TextEditor/TextEditorWidget.h index 2b3fcca7eb..25c848054f 100644 --- a/Userland/Applications/TextEditor/TextEditorWidget.h +++ b/Userland/Applications/TextEditor/TextEditorWidget.h @@ -40,7 +40,7 @@ class TextEditorWidget final : public GUI::Widget { C_OBJECT(TextEditorWidget) public: virtual ~TextEditorWidget() override; - void open_file(const String& path); + bool open_file(const String& path); bool request_close(); GUI::TextEditor& editor() { return *m_editor; } diff --git a/Userland/Applications/TextEditor/main.cpp b/Userland/Applications/TextEditor/main.cpp index 0bf5c5c8a5..ab72dd97f2 100644 --- a/Userland/Applications/TextEditor/main.cpp +++ b/Userland/Applications/TextEditor/main.cpp @@ -90,9 +90,10 @@ int main(int argc, char** argv) app->set_menubar(menubar); if (file_to_edit) - text_widget.open_file(file_to_edit); - else - text_widget.update_title(); + if (!text_widget.open_file(file_to_edit)) + return 1; + + text_widget.update_title(); if (initial_line_number != 0) text_widget.editor().set_cursor_and_focus_line(initial_line_number - 1, 0); |