11#include " ProjectDocumentContext.h"
22#include " ProjectDocumentContext_p.h"
33
4+ #include < application_config.h>
5+
46#include < QDir>
57#include < QLoggingCategory>
68
1012#include < opendspx/model.h>
1113#include < opendspxserializer/serializer.h>
1214
15+ #include < SVSCraftCore/Semver.h>
1316#include < SVSCraftQuick/MessageBox.h>
1417
18+ #include < dspxmodel/Model.h>
19+
1520#include < coreplugin/OpenSaveProjectFileScenario.h>
1621#include < coreplugin/internal/BehaviorPreference.h>
22+ #include < coreplugin/DspxDocument.h>
1723
1824namespace Core {
1925
2026 Q_LOGGING_CATEGORY (lcProjectDocumentContext, " diffscope.core.projectdocumentcontext" )
2127
28+ constexpr char kEditorId [] = " org.diffscope.diffscope" ;
29+
30+ static void writeEditorInfo (QDspx::Model &model) {
31+ model.content .global .editorId = kEditorId ;
32+ model.content .global .editorName = QStringLiteral (" DiffScope" );
33+ model.content .workspace [" diffscope" ].insert (" editorVersion" , QStringLiteral (APPLICATION_SEMVER ));
34+ }
35+
36+ static bool checkIsVersionCompatible (const QString &version) {
37+ if (version.isEmpty ())
38+ return true ;
39+ SVS ::Semver currentSemver (QStringLiteral (APPLICATION_SEMVER ));
40+ SVS ::Semver fileSemver (version);
41+ if (fileSemver == currentSemver)
42+ return true ;
43+ if (fileSemver > currentSemver)
44+ return false ;
45+ if (!fileSemver.preRelease ().isEmpty () || !fileSemver.build ().isEmpty ()) {
46+ return false ;
47+ }
48+ return true ;
49+ }
50+
2251 void ProjectDocumentContextPrivate::markSaved () {
2352 // TODO
2453 }
2554
2655 QByteArray ProjectDocumentContextPrivate::serializeDocument () const {
27- // TODO
2856 QDspx::SerializationErrorList errors;
29- return QDspx::Serializer::serialize (model_TODO, errors, QDspx::Serializer::CheckError);
57+ auto model = document->model ()->toQDspx ();
58+ writeEditorInfo (model);
59+ return QDspx::Serializer::serialize (model, errors, QDspx::Serializer::CheckError);
60+ }
61+
62+ bool ProjectDocumentContextPrivate::initializeDocument (const QDspx::Model &model, bool doCheck) {
63+ Q_Q (ProjectDocumentContext);
64+ document = new DspxDocument (q);
65+ if (doCheck) {
66+ if (model.content .global .editorId != kEditorId ) {
67+ if (!openSaveProjectFileScenario->confirmFileCreatedByAnotherApplication (model.content .global .editorName )) {
68+ return false ;
69+ }
70+ } else if (auto version = model.content .workspace .value (" diffscope" ).value (" editorVersion" ).toString (); !checkIsVersionCompatible (version)) {
71+ if (!openSaveProjectFileScenario->confirmFileCreatedByIncompatibleVersion (version)) {
72+ return false ;
73+ }
74+ }
75+ }
76+ auto postProcessedModel = model;
77+ writeEditorInfo (postProcessedModel);
78+ document->model ()->fromQDspx (postProcessedModel);
79+ return true ;
3080 }
3181
32- bool ProjectDocumentContextPrivate::deserializeDocument (const QByteArray &data) {
82+ bool ProjectDocumentContextPrivate::deserializeAndInitializeDocument (const QByteArray &data) {
3383 QDspx::SerializationErrorList errors;
3484 auto model = QDspx::Serializer::deserialize (data, errors);
3585 if (errors.containsFatal () || errors.containsError ()) {
3686 return false ;
3787 }
38- // TODO
39- model_TODO = model;
40- return true ;
88+ return initializeDocument (model, true );
4189 }
4290
4391 ProjectDocumentContext::ProjectDocumentContext (QObject *parent) : QObject(parent), d_ptr(new ProjectDocumentContextPrivate) {
@@ -55,7 +103,7 @@ namespace Core {
55103
56104 DspxDocument *ProjectDocumentContext::document () const {
57105 Q_D (const ProjectDocumentContext);
58- return nullptr ; // TODO
106+ return d-> document ;
59107 }
60108
61109 OpenSaveProjectFileScenario * ProjectDocumentContext::openSaveProjectFileScenario () const {
@@ -65,16 +113,16 @@ namespace Core {
65113
66114 bool ProjectDocumentContext::openFile (const QString &filePath) {
67115 Q_D (ProjectDocumentContext);
68- if (false ) {
69- return false ; // TODO document should not be opened
116+ if (d->document ) {
117+ qCWarning (lcProjectDocumentContext) << " Cannot open file: document already exists." ;
118+ return false ;
70119 }
71120 d->fileLocker = new FileLocker (this );
72121 if (!d->fileLocker ->open (filePath)) {
73122 qCCritical (lcProjectDocumentContext) << " Failed to open file:" << filePath;
74123 d->openSaveProjectFileScenario ->showOpenFailMessageBox (filePath, d->fileLocker ->errorString ());
75124 return false ;
76125 }
77- // TODO initialize document
78126 bool ok;
79127 auto data = d->fileLocker ->readData (&ok);
80128 if (
@@ -91,7 +139,7 @@ namespace Core {
91139 d->openSaveProjectFileScenario ->showOpenFailMessageBox (filePath, d->fileLocker ->errorString ());
92140 return false ;
93141 }
94- if (!d->deserializeDocument (data)) {
142+ if (!d->deserializeAndInitializeDocument (data)) {
95143 qCCritical (lcProjectDocumentContext) << " Failed to deserialize file:" << filePath;
96144 d->openSaveProjectFileScenario ->showDeserializationFailMessageBox (filePath);
97145 return false ;
@@ -101,20 +149,21 @@ namespace Core {
101149
102150 void ProjectDocumentContext::newFile (const QDspx::Model &templateModel, bool isNonFileDocument) {
103151 Q_D (ProjectDocumentContext);
104- if (false ) {
105- return ; // TODO document should not be opened
152+ if (d->document ) {
153+ qCWarning (lcProjectDocumentContext) << " Cannot create new file: document already exists." ;
154+ return ;
106155 }
107156 if (!isNonFileDocument) {
108157 d->fileLocker = new FileLocker (this );
109158 }
110- // TODO initialize document
111- d->model_TODO = templateModel;
159+ d->initializeDocument (templateModel, false );
112160 }
113161
114162 bool ProjectDocumentContext::newFile (const QString &templateFilePath, bool isNonFileDocument) {
115163 Q_D (ProjectDocumentContext);
116- if (false ) {
117- return false ; // TODO document should not be opened
164+ if (d->document ) {
165+ qCWarning (lcProjectDocumentContext) << " Cannot create new file: document already exists." ;
166+ return false ;
118167 }
119168 if (!isNonFileDocument) {
120169 d->fileLocker = new FileLocker (this );
@@ -132,7 +181,7 @@ namespace Core {
132181 d->openSaveProjectFileScenario ->showOpenFailMessageBox (templateFilePath, d->fileLocker ->errorString ());
133182 return false ;
134183 }
135- if (!d->deserializeDocument (data)) {
184+ if (!d->deserializeAndInitializeDocument (data)) {
136185 qCCritical (lcProjectDocumentContext) << " Failed to deserialize template file:" << templateFilePath;
137186 d->openSaveProjectFileScenario ->showDeserializationFailMessageBox (templateFilePath);
138187 return false ;
0 commit comments