-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProjectDocumentContext.cpp
More file actions
245 lines (217 loc) · 9.37 KB
/
ProjectDocumentContext.cpp
File metadata and controls
245 lines (217 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "ProjectDocumentContext.h"
#include "ProjectDocumentContext_p.h"
#include <application_config.h>
#include <QDir>
#include <QLoggingCategory>
#include <CoreApi/filelocker.h>
#include <CoreApi/runtimeinterface.h>
#include <opendspx/model.h>
#include <opendspxserializer/serializer.h>
#include <SVSCraftCore/Semver.h>
#include <SVSCraftQuick/MessageBox.h>
#include <dspxmodel/Model.h>
#include <coreplugin/OpenSaveProjectFileScenario.h>
#include <coreplugin/internal/BehaviorPreference.h>
#include <coreplugin/DspxDocument.h>
#include <coreplugin/CoreInterface.h>
#include <coreplugin/DspxCheckerRegistry.h>
namespace Core {
Q_LOGGING_CATEGORY(lcProjectDocumentContext, "diffscope.core.projectdocumentcontext")
static void writeEditorInfo(QDspx::Model &model) {
model.content.global.editorId = CoreInterface::dspxEditorId();
model.content.global.editorName = QStringLiteral("DiffScope");
model.content.workspace["diffscope"].insert("editorVersion", QStringLiteral(APPLICATION_SEMVER));
}
static bool checkIsVersionCompatible(const QString &version) {
if (version.isEmpty())
return true;
SVS::Semver currentSemver(QStringLiteral(APPLICATION_SEMVER));
SVS::Semver fileSemver(version);
if (fileSemver == currentSemver)
return true;
if (fileSemver > currentSemver)
return false;
if (!fileSemver.preRelease().isEmpty() || !fileSemver.build().isEmpty()) {
return false;
}
return true;
}
void ProjectDocumentContextPrivate::markSaved() {
Q_Q(ProjectDocumentContext);
// TODO
Q_EMIT q->saved();
}
QByteArray ProjectDocumentContextPrivate::serializeDocument() const {
QDspx::SerializationErrorList errors;
auto model = document->model()->toQDspx();
writeEditorInfo(model);
return QDspx::Serializer::serialize(model, errors, QDspx::Serializer::CheckError);
}
bool ProjectDocumentContextPrivate::initializeDocument(const QDspx::Model &model, bool doCheck) {
Q_Q(ProjectDocumentContext);
document = new DspxDocument(q);
if (doCheck) {
if (model.content.global.editorId != CoreInterface::dspxEditorId()) {
if (!openSaveProjectFileScenario->confirmFileCreatedByAnotherApplication(model.content.global.editorName)) {
return false;
}
} else if (auto version = model.content.workspace.value("diffscope").value("editorVersion").toString(); !checkIsVersionCompatible(version)) {
if (!openSaveProjectFileScenario->confirmFileCreatedByIncompatibleVersion(version)) {
return false;
}
}
auto customCheckResult = CoreInterface::dspxCheckerRegistry()->runCheck(model, IDspxChecker::Strong, true);
if (!customCheckResult.isEmpty()) {
if (!openSaveProjectFileScenario->confirmCustomCheckWarning(customCheckResult.front().message)) {
return false;
}
}
}
auto postProcessedModel = model;
writeEditorInfo(postProcessedModel);
document->model()->fromQDspx(postProcessedModel);
return true;
}
bool ProjectDocumentContextPrivate::deserializeAndInitializeDocument(const QByteArray &data) {
QDspx::SerializationErrorList errors;
auto model = QDspx::Serializer::deserialize(data, errors);
if (errors.containsFatal() || errors.containsError()) {
return false;
}
return initializeDocument(model, true);
}
ProjectDocumentContext::ProjectDocumentContext(QObject *parent) : QObject(parent), d_ptr(new ProjectDocumentContextPrivate) {
Q_D(ProjectDocumentContext);
d->q_ptr = this;
d->openSaveProjectFileScenario = new OpenSaveProjectFileScenario(this);
}
ProjectDocumentContext::~ProjectDocumentContext() = default;
FileLocker *ProjectDocumentContext::fileLocker() const {
Q_D(const ProjectDocumentContext);
return d->fileLocker;
}
DspxDocument *ProjectDocumentContext::document() const {
Q_D(const ProjectDocumentContext);
return d->document;
}
OpenSaveProjectFileScenario * ProjectDocumentContext::openSaveProjectFileScenario() const {
Q_D(const ProjectDocumentContext);
return d->openSaveProjectFileScenario;
}
bool ProjectDocumentContext::openFile(const QString &filePath) {
Q_D(ProjectDocumentContext);
if (d->document) {
qCWarning(lcProjectDocumentContext) << "Cannot open file: document already exists.";
return false;
}
d->fileLocker = new FileLocker(this);
if (!d->fileLocker->open(filePath)) {
qCCritical(lcProjectDocumentContext) << "Failed to open file:" << filePath;
d->openSaveProjectFileScenario->showOpenFailMessageBox(filePath, d->fileLocker->errorString());
return false;
}
bool ok;
auto data = d->fileLocker->readData(&ok);
if (
#ifdef Q_OS_WIN
!(Internal::BehaviorPreference::fileOption() & Internal::BehaviorPreference::FO_LockOpenedFiles)
#else
true
#endif
) {
d->fileLocker->release();
}
if (!ok) {
qCCritical(lcProjectDocumentContext) << "Failed to read file:" << filePath;
d->openSaveProjectFileScenario->showOpenFailMessageBox(filePath, d->fileLocker->errorString());
return false;
}
if (!d->deserializeAndInitializeDocument(data)) {
qCCritical(lcProjectDocumentContext) << "Failed to deserialize file:" << filePath;
d->openSaveProjectFileScenario->showDeserializationFailMessageBox(filePath);
return false;
}
return true;
}
void ProjectDocumentContext::newFile(const QDspx::Model &templateModel, bool isNonFileDocument) {
Q_D(ProjectDocumentContext);
if (d->document) {
qCWarning(lcProjectDocumentContext) << "Cannot create new file: document already exists.";
return;
}
if (!isNonFileDocument) {
d->fileLocker = new FileLocker(this);
}
d->initializeDocument(templateModel, false);
}
bool ProjectDocumentContext::newFile(const QString &templateFilePath, bool isNonFileDocument) {
Q_D(ProjectDocumentContext);
if (d->document) {
qCWarning(lcProjectDocumentContext) << "Cannot create new file: document already exists.";
return false;
}
if (!isNonFileDocument) {
d->fileLocker = new FileLocker(this);
}
FileLocker openFileLocker;
if (!openFileLocker.open(templateFilePath)) {
qCCritical(lcProjectDocumentContext) << "Failed to open template file:" << templateFilePath;
d->openSaveProjectFileScenario->showOpenFailMessageBox(templateFilePath, openFileLocker.errorString());
return false;
}
bool ok;
auto data = openFileLocker.readData(&ok);
if (!ok) {
qCCritical(lcProjectDocumentContext) << "Failed to read template file:" << templateFilePath;
d->openSaveProjectFileScenario->showOpenFailMessageBox(templateFilePath, d->fileLocker->errorString());
return false;
}
if (!d->deserializeAndInitializeDocument(data)) {
qCCritical(lcProjectDocumentContext) << "Failed to deserialize template file:" << templateFilePath;
d->openSaveProjectFileScenario->showDeserializationFailMessageBox(templateFilePath);
return false;
}
return true;
}
bool ProjectDocumentContext::save() {
Q_D(ProjectDocumentContext);
if (!d->fileLocker || d->fileLocker->path().isEmpty())
return false;
auto data = d->serializeDocument();
bool isSuccess = d->fileLocker->save(data);
if (!isSuccess) {
qCCritical(lcProjectDocumentContext) << "Failed to save file:" << d->fileLocker->path();
d->openSaveProjectFileScenario->showSaveFailMessageBox(d->fileLocker->path(), d->fileLocker->errorString());
return false;
}
d->markSaved();
return true;
}
bool ProjectDocumentContext::saveAs(const QString &filePath) {
Q_D(ProjectDocumentContext);
if (!d->fileLocker)
return false;
auto data = d->serializeDocument();
bool isSuccess = d->fileLocker->saveAs(filePath, data);
if (!isSuccess) {
qCCritical(lcProjectDocumentContext) << "Failed to save file as:" << d->fileLocker->path();
d->openSaveProjectFileScenario->showSaveFailMessageBox(filePath, d->fileLocker->errorString());
return false;
}
d->markSaved();
return true;
}
bool ProjectDocumentContext::saveCopy(const QString &filePath) {
Q_D(ProjectDocumentContext);
FileLocker copyFileLocker;
auto data = d->serializeDocument();
bool isSuccess = copyFileLocker.saveAs(filePath, data);
if (!isSuccess) {
qCCritical(lcProjectDocumentContext) << "Failed to save copy file:" << d->fileLocker->path();
d->openSaveProjectFileScenario->showSaveFailMessageBox(filePath, copyFileLocker.errorString());
return false;
}
return true;
}
}
#include "moc_ProjectDocumentContext.cpp"