mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:57:35 +00:00
TextEditor: Exit program when file is not opened
When trying to open files not permitted by the user, display the error message and exit.
This commit is contained in:
parent
67f01fd82e
commit
5df34f6567
3 changed files with 9 additions and 6 deletions
|
@ -583,12 +583,12 @@ void TextEditorWidget::update_title()
|
||||||
window()->set_title(builder.to_string());
|
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);
|
auto file = Core::File::construct(path);
|
||||||
if (!file->open(Core::IODevice::ReadOnly) && file->error() != ENOENT) {
|
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);
|
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());
|
m_editor->set_text(file->read_all());
|
||||||
|
@ -598,6 +598,8 @@ void TextEditorWidget::open_file(const String& path)
|
||||||
set_path(LexicalPath(path));
|
set_path(LexicalPath(path));
|
||||||
|
|
||||||
m_editor->set_focus(true);
|
m_editor->set_focus(true);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TextEditorWidget::request_close()
|
bool TextEditorWidget::request_close()
|
||||||
|
|
|
@ -40,7 +40,7 @@ class TextEditorWidget final : public GUI::Widget {
|
||||||
C_OBJECT(TextEditorWidget)
|
C_OBJECT(TextEditorWidget)
|
||||||
public:
|
public:
|
||||||
virtual ~TextEditorWidget() override;
|
virtual ~TextEditorWidget() override;
|
||||||
void open_file(const String& path);
|
bool open_file(const String& path);
|
||||||
bool request_close();
|
bool request_close();
|
||||||
|
|
||||||
GUI::TextEditor& editor() { return *m_editor; }
|
GUI::TextEditor& editor() { return *m_editor; }
|
||||||
|
|
|
@ -90,9 +90,10 @@ int main(int argc, char** argv)
|
||||||
app->set_menubar(menubar);
|
app->set_menubar(menubar);
|
||||||
|
|
||||||
if (file_to_edit)
|
if (file_to_edit)
|
||||||
text_widget.open_file(file_to_edit);
|
if (!text_widget.open_file(file_to_edit))
|
||||||
else
|
return 1;
|
||||||
text_widget.update_title();
|
|
||||||
|
text_widget.update_title();
|
||||||
|
|
||||||
if (initial_line_number != 0)
|
if (initial_line_number != 0)
|
||||||
text_widget.editor().set_cursor_and_focus_line(initial_line_number - 1, 0);
|
text_widget.editor().set_cursor_and_focus_line(initial_line_number - 1, 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue