From edeb47d2c260719115d2ccef28018c735d491d26 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sun, 15 Jan 2023 09:43:54 +0800 Subject: [PATCH] Let user choose if continue to save, if backup file creation failed. --- RedPandaIDE/editor.cpp | 40 ++++++++++------------------- RedPandaIDE/editor.h | 14 ---------- RedPandaIDE/mainwindow.cpp | 12 ++------- libs/qsynedit/qsynedit/document.cpp | 14 +++++----- libs/qsynedit/qsynedit/document.h | 4 +-- 5 files changed, 24 insertions(+), 60 deletions(-) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index e2c90fce..0d289371 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -53,24 +53,6 @@ #include "project.h" #include -SaveException::SaveException(const QString& reason) { - mReason = reason; - mReasonBuffer = mReason.toLocal8Bit(); -} - -SaveException::SaveException(const QString&& reason) { - mReason = reason; - mReasonBuffer = mReason.toLocal8Bit(); -} - -const QString& SaveException::reason() const noexcept{ - return mReason; -} - -const char* SaveException::what() const noexcept { - return mReasonBuffer; -} - QHash> Editor::mSharedParsers; Editor::Editor(QWidget *parent): @@ -252,14 +234,20 @@ void Editor::saveFile(QString filename) { } if (!fileExists(filename)) { if (!stringToFile(text(),backupFilename)) { - QMessageBox::critical(pMainWindow,tr("Error"), - tr("Can't generate temporary backup file '%1'.").arg(backupFilename)); - return; + if (QMessageBox::question(pMainWindow,tr("Error"), + tr("Can't generate temporary backup file '%1'.").arg(backupFilename) + +"
" + +tr("Continue to save?"), + QMessageBox::Yes | QMessageBox::No,QMessageBox::No)!=QMessageBox::Yes) + return; } } else if (!QFile::copy(filename,backupFilename)) { - QMessageBox::critical(pMainWindow,tr("Error"), - tr("Can't generate temporary backup file '%1'.").arg(backupFilename)); - return; + if (QMessageBox::question(pMainWindow,tr("Error"), + tr("Can't generate temporary backup file '%1'.").arg(backupFilename) + +"
" + +tr("Continue to save?"), + QMessageBox::Yes | QMessageBox::No,QMessageBox::No)!=QMessageBox::Yes) + return; } this->document()->saveToFile(file,encoding, pSettings->editor().defaultEncoding(), @@ -298,7 +286,7 @@ bool Editor::save(bool force, bool doReparse) { setModified(false); mIsNew = false; updateCaption(); - } catch (SaveException& exception) { + } catch (FileError& exception) { if (!force) { QMessageBox::critical(pMainWindow,tr("Error"), exception.reason()); @@ -414,7 +402,7 @@ bool Editor::saveAs(const QString &name, bool fromProject){ saveFile(mFilename); mIsNew = false; setModified(false); - } catch (SaveException& exception) { + } catch (FileError& exception) { QMessageBox::critical(pMainWindow,tr("Error"), exception.reason()); return false; diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index a965ad5a..b1d421e0 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -41,20 +41,6 @@ struct TabStop { using PTabStop = std::shared_ptr; -class SaveException: public std::exception { - -public: - explicit SaveException(const QString& reason); - explicit SaveException(const QString&& reason); - // exception interface - const QString& reason() const noexcept; -public: - const char *what() const noexcept override; -private: - QString mReason; - QByteArray mReasonBuffer; -}; - class Editor : public QSynedit::QSynEdit { Q_OBJECT diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 99e0f80e..17e32568 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -5372,13 +5372,9 @@ void MainWindow::on_actionSave_triggered() { Editor * editor = mEditorList->getEditor(); if (editor) { - try { - editor->save(); + editor->save(); // if (editor->inProject() && (mProject)) // mProject->saveAll(); - } catch(FileError e) { - QMessageBox::critical(editor,tr("Error"),e.reason()); - } } } @@ -5386,11 +5382,7 @@ void MainWindow::on_actionSaveAs_triggered() { Editor * editor = mEditorList->getEditor(); if (editor) { - try { - editor->saveAs(); - } catch(FileError e) { - QMessageBox::critical(editor,tr("Error"),e.reason()); - } + editor->saveAs(); } } diff --git a/libs/qsynedit/qsynedit/document.cpp b/libs/qsynedit/qsynedit/document.cpp index a072efb9..25b2267a 100644 --- a/libs/qsynedit/qsynedit/document.cpp +++ b/libs/qsynedit/qsynedit/document.cpp @@ -547,21 +547,19 @@ void Document::loadUTF32BOMFile(QFile &file) this->setText(text); } -void Document::saveUTF16File(QFile &file) +void Document::saveUTF16File(QFile &file, QTextCodec* codec) { - QString text=getTextStr(); - QTextCodec* codec=QTextCodec::codecForName(ENCODING_UTF16); if (!codec) return; + QString text=getTextStr(); file.write(codec->fromUnicode(text)); } -void Document::saveUTF32File(QFile &file) +void Document::saveUTF32File(QFile &file, QTextCodec* codec) { - QString text=getTextStr(); - QTextCodec* codec=QTextCodec::codecForName(ENCODING_UTF32); if (!codec) return; + QString text=getTextStr(); file.write(codec->fromUnicode(text)); } @@ -757,10 +755,10 @@ void Document::saveToFile(QFile &file, const QByteArray& encoding, if (mLines.isEmpty()) return; if (realEncoding == ENCODING_UTF16) { - saveUTF16File(file); + saveUTF16File(file,codec); return; } else if (realEncoding == ENCODING_UTF32) { - saveUTF32File(file); + saveUTF32File(file,codec); return; } if (realEncoding == ENCODING_UTF8_BOM) { file.putChar(0xEF); diff --git a/libs/qsynedit/qsynedit/document.h b/libs/qsynedit/qsynedit/document.h index b1879c42..438381a9 100644 --- a/libs/qsynedit/qsynedit/document.h +++ b/libs/qsynedit/qsynedit/document.h @@ -136,8 +136,8 @@ private: bool tryLoadFileByEncoding(QByteArray encodingName, QFile& file); void loadUTF16BOMFile(QFile& file); void loadUTF32BOMFile(QFile& file); - void saveUTF16File(QFile& file); - void saveUTF32File(QFile& file); + void saveUTF16File(QFile& file, QTextCodec* codec); + void saveUTF32File(QFile& file, QTextCodec* codec); private: DocumentLines mLines;