Skip to content

Commit 39092c9

Browse files
committed
Add file integrity check confirm
1 parent 155a7f7 commit 39092c9

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

src/plugins/coreplugin/project/document/ProjectDocumentContext.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,21 @@ namespace Core {
5757
Q_EMIT q->saved();
5858
}
5959

60-
QByteArray ProjectDocumentContextPrivate::serializeDocument() const {
60+
QByteArray ProjectDocumentContextPrivate::serializeDocument(bool *hasError) const {
6161
QDspx::SerializationErrorList errors;
6262
auto model = document->model()->toQDspx();
6363
writeEditorInfo(model);
64-
return QDspx::Serializer::serialize(model, errors, QDspx::Serializer::CheckError);
64+
auto data = QDspx::Serializer::serialize(model, errors, QDspx::Serializer::CheckError);
65+
if (errors.containsError()) {
66+
if (hasError) {
67+
*hasError = true;
68+
}
69+
} else {
70+
if (hasError) {
71+
*hasError = false;
72+
}
73+
}
74+
return data;
6575
}
6676

6777
bool ProjectDocumentContextPrivate::initializeDocument(const QDspx::Model &model, bool doCheck) {
@@ -204,7 +214,14 @@ namespace Core {
204214
Q_D(ProjectDocumentContext);
205215
if (!d->fileLocker || d->fileLocker->path().isEmpty())
206216
return false;
207-
auto data = d->serializeDocument();
217+
bool hasError;
218+
auto data = d->serializeDocument(&hasError);
219+
if (hasError) {
220+
qCWarning(lcProjectDocumentContext) << "Failed to serialize document.";
221+
if (!d->openSaveProjectFileScenario->confirmSaveFileIntegrity()) {
222+
return false;
223+
}
224+
}
208225
bool isSuccess = d->fileLocker->save(data);
209226
if (!isSuccess) {
210227
qCCritical(lcProjectDocumentContext) << "Failed to save file:" << d->fileLocker->path();
@@ -219,7 +236,14 @@ namespace Core {
219236
Q_D(ProjectDocumentContext);
220237
if (!d->fileLocker)
221238
return false;
222-
auto data = d->serializeDocument();
239+
bool hasError;
240+
auto data = d->serializeDocument(&hasError);
241+
if (hasError) {
242+
qCWarning(lcProjectDocumentContext) << "Failed to serialize document.";
243+
if (!d->openSaveProjectFileScenario->confirmSaveFileIntegrity()) {
244+
return false;
245+
}
246+
}
223247
bool isSuccess = d->fileLocker->saveAs(filePath, data);
224248
if (!isSuccess) {
225249
qCCritical(lcProjectDocumentContext) << "Failed to save file as:" << d->fileLocker->path();
@@ -233,7 +257,14 @@ namespace Core {
233257
bool ProjectDocumentContext::saveCopy(const QString &filePath) {
234258
Q_D(ProjectDocumentContext);
235259
FileLocker copyFileLocker;
236-
auto data = d->serializeDocument();
260+
bool hasError;
261+
auto data = d->serializeDocument(&hasError);
262+
if (hasError) {
263+
qCWarning(lcProjectDocumentContext) << "Failed to serialize document.";
264+
if (!d->openSaveProjectFileScenario->confirmSaveFileIntegrity()) {
265+
return false;
266+
}
267+
}
237268
bool isSuccess = copyFileLocker.saveAs(filePath, data);
238269
if (!isSuccess) {
239270
qCCritical(lcProjectDocumentContext) << "Failed to save copy file:" << d->fileLocker->path();

src/plugins/coreplugin/project/document/ProjectDocumentContext_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Core {
1717
OpenSaveProjectFileScenario *openSaveProjectFileScenario;
1818

1919
void markSaved();
20-
QByteArray serializeDocument() const;
20+
QByteArray serializeDocument(bool *hasError = nullptr) const;
2121

2222
bool deserializeAndInitializeDocument(const QByteArray &data);
2323
bool initializeDocument(const QDspx::Model &model, bool doCheck);

src/plugins/coreplugin/project/scenarios/OpenSaveProjectFileScenario.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,15 @@ namespace Core {
161161
) == SVS::SVSCraft::Yes;
162162
}
163163

164+
bool OpenSaveProjectFileScenario::confirmSaveFileIntegrity() const {
165+
Q_D(const OpenSaveProjectFileScenario);
166+
return SVS::MessageBox::warning(
167+
RuntimeInterface::qmlEngine(),
168+
d->window,
169+
tr("Potential document integrity issue"),
170+
tr("The document integrity check failed while saving the file. The file can still be saved, but it may not be parsed correctly when opened again.\n\nUnder normal circumstances, this problem should not occur. It may be caused by defects in %1 or some plugins.\n\nIf you are currently using Save or overwriting an existing file, it is recommended that you cancel the current operation. Instead, use Save Copy to save the document in another file, and then inspect the file using DSPX Inspector.\n\nDo you want to continue?").arg(QApplication::applicationDisplayName()),
171+
SVS::SVSCraft::Yes | SVS::SVSCraft::No
172+
) == SVS::SVSCraft::Yes;
173+
}
174+
164175
}

src/plugins/coreplugin/project/scenarios/OpenSaveProjectFileScenario.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace Core {
3333
Q_INVOKABLE bool confirmFileCreatedByAnotherApplication(const QString &name) const;
3434
Q_INVOKABLE bool confirmFileCreatedByIncompatibleVersion(const QString &version) const;
3535
Q_INVOKABLE bool confirmCustomCheckWarning(const QString &message) const;
36+
Q_INVOKABLE bool confirmSaveFileIntegrity() const;
3637

3738
Q_SIGNALS:
3839
void windowChanged();

0 commit comments

Comments
 (0)