From 0731b3a309f48ec0202e50d8d034b0723a20f8eb Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Wed, 6 Dec 2017 08:50:50 -0500 Subject: Make sure that on closing, it attempts to save *all* files with pending changes. If popup warnings are disabled just close the app (never automatically change the underlying file(s) - this can be disastrous) Add status reporting to all the "Save" functions so that it returns false if the user cancelled it. --- src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp') diff --git a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp index 653bd0e8..6e04d67b 100644 --- a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp +++ b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp @@ -103,12 +103,17 @@ void PlainTextEditor::LoadFile(QString filepath){ emit FileLoaded(this->whatsThis()); } -void PlainTextEditor::SaveFile(bool newname){ +bool PlainTextEditor::SaveFile(bool newname){ + //NOTE: This returns true for proper behaviour, and false for a user-cancelled process //qDebug() << "Save File:" << this->whatsThis(); + //Quick check for a non-editable file + if(!newname && this->whatsThis().startsWith("/")){ + if(!QFileInfo(this->whatsThis()).isWritable()){ newname = true; } //cannot save the current file name/location + } if( !this->whatsThis().startsWith("/") || newname ){ //prompt for a filename/path QString file = QFileDialog::getSaveFileName(this, tr("Save File"), this->whatsThis(), tr("Text File (*)")); - if(file.isEmpty()){ return; } + if(file.isEmpty()){ return false; } //cancelled this->setWhatsThis(file); SYNTAX->loadRules( Custom_Syntax::ruleForFile(this->whatsThis().section("/",-1), settings) ); SYNTAX->rehighlight(); @@ -118,6 +123,7 @@ void PlainTextEditor::SaveFile(bool newname){ hasChanges = !ok; if(ok){ lastSaveContents = this->toPlainText(); emit FileLoaded(this->whatsThis()); } watcher->addPath(currentFile()); + return true; //qDebug() << " - Success:" << ok << hasChanges; } -- cgit From eee843173b5f1f3d7e7d7fc1a822f0e735378d38 Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Wed, 6 Dec 2017 09:28:30 -0500 Subject: Add better handling of read-only files in LTE. --- .../desktop-utils/lumina-textedit/PlainTextEditor.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp') diff --git a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp index 6e04d67b..8626b2b4 100644 --- a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp +++ b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp @@ -24,7 +24,7 @@ PlainTextEditor::PlainTextEditor(QSettings *set, QWidget *parent) : QPlainTextEd LNW = new LNWidget(this); showLNW = true; watcher = new QFileSystemWatcher(this); - hasChanges = false; + hasChanges = readonly = false; lastSaveContents.clear(); matchleft = matchright = -1; this->setTabStopWidth( 8 * this->fontMetrics().width(" ") ); //8 character spaces per tab (UNIX standard) @@ -99,7 +99,14 @@ void PlainTextEditor::LoadFile(QString filepath){ this->centerCursor(); //scroll until cursor is centered (if possible) } hasChanges = false; - if(QFile::exists(filepath)){ watcher->addPath(filepath); } + readonly = false; + if(QFile::exists(filepath)){ + readonly = !QFileInfo(filepath).isWritable(); + watcher->addPath(filepath); + }else if(filepath.startsWith("/")){ + //See if the containing directory is writable instead + readonly = !QFileInfo(filepath.section("/",0,-2)).isWritable(); + } emit FileLoaded(this->whatsThis()); } @@ -117,6 +124,7 @@ bool PlainTextEditor::SaveFile(bool newname){ this->setWhatsThis(file); SYNTAX->loadRules( Custom_Syntax::ruleForFile(this->whatsThis().section("/",-1), settings) ); SYNTAX->rehighlight(); + readonly = !QFileInfo(this->whatsThis()).isWritable(); //update this flag } if( !watcher->files().isEmpty() ){ watcher->removePaths(watcher->files()); } bool ok = LUtils::writeFile(this->whatsThis(), this->toPlainText().split("\n"), true); @@ -135,6 +143,11 @@ bool PlainTextEditor::hasChange(){ return hasChanges; } +bool PlainTextEditor::readOnlyFile(){ + //qDebug() << "Read Only File:" << readonly << this->whatsThis(); + return readonly; +} + //Functions for managing the line number widget int PlainTextEditor::LNWWidth(){ //Get the number of chars we need for line numbers -- cgit From 16e6402abdc43b64467682c370237eb60f031af0 Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Thu, 28 Dec 2017 10:17:02 -0500 Subject: Quick update to lumina-textedit: 1. Add a new syntax highlighting "meta" option for auto-selecting a ruleset based on the first line of text in the file, and add rules for shell, json, and python to support first-line matches. 2. Fix up the detection of "read-only" property on newly-saved files. --- src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp') diff --git a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp index 8626b2b4..b1592cc3 100644 --- a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp +++ b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp @@ -76,17 +76,22 @@ void PlainTextEditor::LoadFile(QString filepath){ bool diffFile = (filepath != this->whatsThis()); this->setWhatsThis(filepath); this->clear(); - QList files = SyntaxFile::availableFiles(settings); + /*QList files = SyntaxFile::availableFiles(settings); for(int i=0; iloadRules(files[i]); break; } - } + }*/ //SYNTAX->loadRules( Custom_Syntax::ruleForFile(filepath.section("/",-1), settings) ); lastSaveContents = LUtils::readFile(filepath).join("\n"); if(diffFile){ + SYNTAX->loadRules( Custom_Syntax::ruleForFile(this->whatsThis().section("/",-1), settings) ); + if(SYNTAX->loadedRules().isEmpty()){ + SYNTAX->loadRules( Custom_Syntax::ruleForFirstLine( lastSaveContents.section("\n",0,0,QString::SectionSkipEmpty) , settings) ); + } + SYNTAX->setupDocument(this); this->setPlainText( lastSaveContents ); }else{ //Try to keep the mouse cursor/scroll in the same position @@ -123,14 +128,18 @@ bool PlainTextEditor::SaveFile(bool newname){ if(file.isEmpty()){ return false; } //cancelled this->setWhatsThis(file); SYNTAX->loadRules( Custom_Syntax::ruleForFile(this->whatsThis().section("/",-1), settings) ); + if(SYNTAX->loadedRules().isEmpty()){ + SYNTAX->loadRules( Custom_Syntax::ruleForFirstLine( this->toPlainText().section("\n",0,0,QString::SectionSkipEmpty) , settings) ); + } + SYNTAX->setupDocument(this); SYNTAX->rehighlight(); - readonly = !QFileInfo(this->whatsThis()).isWritable(); //update this flag } if( !watcher->files().isEmpty() ){ watcher->removePaths(watcher->files()); } bool ok = LUtils::writeFile(this->whatsThis(), this->toPlainText().split("\n"), true); hasChanges = !ok; if(ok){ lastSaveContents = this->toPlainText(); emit FileLoaded(this->whatsThis()); } watcher->addPath(currentFile()); + readonly = !QFileInfo(this->whatsThis()).isWritable(); //update this flag return true; //qDebug() << " - Success:" << ok << hasChanges; } -- cgit