Let user choose if continue to save, if backup file creation failed.

This commit is contained in:
Roy Qu 2023-01-15 09:43:54 +08:00
parent ac20a2c458
commit edeb47d2c2
5 changed files with 24 additions and 60 deletions

View File

@ -53,24 +53,6 @@
#include "project.h" #include "project.h"
#include <qt_utils/charsetinfo.h> #include <qt_utils/charsetinfo.h>
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<ParserLanguage,std::weak_ptr<CppParser>> Editor::mSharedParsers; QHash<ParserLanguage,std::weak_ptr<CppParser>> Editor::mSharedParsers;
Editor::Editor(QWidget *parent): Editor::Editor(QWidget *parent):
@ -252,13 +234,19 @@ void Editor::saveFile(QString filename) {
} }
if (!fileExists(filename)) { if (!fileExists(filename)) {
if (!stringToFile(text(),backupFilename)) { if (!stringToFile(text(),backupFilename)) {
QMessageBox::critical(pMainWindow,tr("Error"), if (QMessageBox::question(pMainWindow,tr("Error"),
tr("Can't generate temporary backup file '%1'.").arg(backupFilename)); tr("Can't generate temporary backup file '%1'.").arg(backupFilename)
+"<br />"
+tr("Continue to save?"),
QMessageBox::Yes | QMessageBox::No,QMessageBox::No)!=QMessageBox::Yes)
return; return;
} }
} else if (!QFile::copy(filename,backupFilename)) { } else if (!QFile::copy(filename,backupFilename)) {
QMessageBox::critical(pMainWindow,tr("Error"), if (QMessageBox::question(pMainWindow,tr("Error"),
tr("Can't generate temporary backup file '%1'.").arg(backupFilename)); tr("Can't generate temporary backup file '%1'.").arg(backupFilename)
+"<br />"
+tr("Continue to save?"),
QMessageBox::Yes | QMessageBox::No,QMessageBox::No)!=QMessageBox::Yes)
return; return;
} }
this->document()->saveToFile(file,encoding, this->document()->saveToFile(file,encoding,
@ -298,7 +286,7 @@ bool Editor::save(bool force, bool doReparse) {
setModified(false); setModified(false);
mIsNew = false; mIsNew = false;
updateCaption(); updateCaption();
} catch (SaveException& exception) { } catch (FileError& exception) {
if (!force) { if (!force) {
QMessageBox::critical(pMainWindow,tr("Error"), QMessageBox::critical(pMainWindow,tr("Error"),
exception.reason()); exception.reason());
@ -414,7 +402,7 @@ bool Editor::saveAs(const QString &name, bool fromProject){
saveFile(mFilename); saveFile(mFilename);
mIsNew = false; mIsNew = false;
setModified(false); setModified(false);
} catch (SaveException& exception) { } catch (FileError& exception) {
QMessageBox::critical(pMainWindow,tr("Error"), QMessageBox::critical(pMainWindow,tr("Error"),
exception.reason()); exception.reason());
return false; return false;

View File

@ -41,20 +41,6 @@ struct TabStop {
using PTabStop = std::shared_ptr<TabStop>; using PTabStop = std::shared_ptr<TabStop>;
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 class Editor : public QSynedit::QSynEdit
{ {
Q_OBJECT Q_OBJECT

View File

@ -5372,13 +5372,9 @@ void MainWindow::on_actionSave_triggered()
{ {
Editor * editor = mEditorList->getEditor(); Editor * editor = mEditorList->getEditor();
if (editor) { if (editor) {
try {
editor->save(); editor->save();
// if (editor->inProject() && (mProject)) // if (editor->inProject() && (mProject))
// mProject->saveAll(); // 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(); Editor * editor = mEditorList->getEditor();
if (editor) { if (editor) {
try {
editor->saveAs(); editor->saveAs();
} catch(FileError e) {
QMessageBox::critical(editor,tr("Error"),e.reason());
}
} }
} }

View File

@ -547,21 +547,19 @@ void Document::loadUTF32BOMFile(QFile &file)
this->setText(text); 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) if (!codec)
return; return;
QString text=getTextStr();
file.write(codec->fromUnicode(text)); 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) if (!codec)
return; return;
QString text=getTextStr();
file.write(codec->fromUnicode(text)); file.write(codec->fromUnicode(text));
} }
@ -757,10 +755,10 @@ void Document::saveToFile(QFile &file, const QByteArray& encoding,
if (mLines.isEmpty()) if (mLines.isEmpty())
return; return;
if (realEncoding == ENCODING_UTF16) { if (realEncoding == ENCODING_UTF16) {
saveUTF16File(file); saveUTF16File(file,codec);
return; return;
} else if (realEncoding == ENCODING_UTF32) { } else if (realEncoding == ENCODING_UTF32) {
saveUTF32File(file); saveUTF32File(file,codec);
return; return;
} if (realEncoding == ENCODING_UTF8_BOM) { } if (realEncoding == ENCODING_UTF8_BOM) {
file.putChar(0xEF); file.putChar(0xEF);

View File

@ -136,8 +136,8 @@ private:
bool tryLoadFileByEncoding(QByteArray encodingName, QFile& file); bool tryLoadFileByEncoding(QByteArray encodingName, QFile& file);
void loadUTF16BOMFile(QFile& file); void loadUTF16BOMFile(QFile& file);
void loadUTF32BOMFile(QFile& file); void loadUTF32BOMFile(QFile& file);
void saveUTF16File(QFile& file); void saveUTF16File(QFile& file, QTextCodec* codec);
void saveUTF32File(QFile& file); void saveUTF32File(QFile& file, QTextCodec* codec);
private: private:
DocumentLines mLines; DocumentLines mLines;