-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.cpp
212 lines (175 loc) · 6.41 KB
/
filesystem.cpp
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
#include "filesystem.h"
#include <QFileInfo>
#include <QDateTime>
#include <QTextStream>
#include <QDirIterator>
Property::Property(QString name, QString text)
{
this->name = name;
this->text = text;
}
FileSystem::FileSystem(CodeEditor *specificationEditor, QWidget *parent)
{
this->specificationEditor = specificationEditor;
this->parent = parent;
specificationModified = false;
connect(specificationEditor, SIGNAL(textChanged()), this, SLOT(setSpecificationModified()));
projectOpen = false;
makeSureProjectsFolderExists();
}
void FileSystem::makeSureProjectsFolderExists()
{
if (!QDir(projectsFolderPath).exists()) {
QDir().mkpath(projectsFolderPath);
}
}
void FileSystem::makeSureProjectFolderExists()
{
if (!QDir(projectFolderPath(projectName)).exists()) {
QDir().mkpath(projectFolderPath(projectName));
}
}
void FileSystem::makeSurePropertiesFolderExists()
{
if (!QDir(propertiesFolderPath(projectName)).exists()) {
QDir().mkpath(propertiesFolderPath(projectName));
}
}
QString FileSystem::projectFolderPath(QString projectName)
{
return projectsFolderPath + QDir::separator() + projectName;
}
QString FileSystem::propertiesFolderPath(QString projectName)
{
return projectFolderPath(projectName) + QDir::separator() + propertiesFolderName;
}
QString FileSystem::specificationFilePath()
{
return projectFolderPath(projectName) + QDir::separator() + projectName + "_spec.mcrl";
}
QString FileSystem::lpsFilePath()
{
return projectFolderPath(projectName) + QDir::separator() + projectName + "_lps.lps";
}
QString FileSystem::propertyFilePath(QString propertyName)
{
return propertiesFolderPath(projectName) + QDir::separator() + propertyName + ".mcf";
}
QString FileSystem::pbesFilePath(QString propertyName)
{
return propertiesFolderPath(projectName) + QDir::separator() + projectName + "_" + propertyName + "_pbes.pbes";
}
const QDir *FileSystem::getExecutablesFolder()
{
return executablesFolder;
}
bool FileSystem::projectOpened()
{
return projectOpen;
}
QString FileSystem::getCurrentSpecification()
{
return specificationEditor->toPlainText();
}
bool FileSystem::isSpecificationModified()
{
return specificationModified;
}
void FileSystem::setSpecificationModified()
{
specificationModified = true;
emit hasChanges(true);
}
bool FileSystem::isPropertyModified(QString propertyName)
{
return propertymodified[propertyName];
}
void FileSystem::setPropertyModified(QString propertyName)
{
propertymodified[propertyName] = true;
emit hasChanges(true);
}
bool FileSystem::upToDateLpsFileExists()
{
/* an lps file is up to date if the lps file exists and the lps file is created after the the last time the specification file was modified */
return QFile(lpsFilePath()).exists()
&& QFileInfo(specificationFilePath()).fileTime(QFileDevice::FileModificationTime) <= QFileInfo(lpsFilePath()).fileTime(QFileDevice::FileModificationTime);
}
bool FileSystem::upToDatePbesFileExists(QString propertyName)
{
/* a pbes file is up to date if the pbes file exists and the pbes file is created after the last time both the lps and the property files were modified */
return QFile(pbesFilePath(propertyName)).exists()
&& QFileInfo(lpsFilePath()).fileTime(QFileDevice::FileModificationTime) <= QFileInfo(pbesFilePath(propertyName)).fileTime(QFileDevice::FileModificationTime)
&& QFileInfo(propertyFilePath(propertyName)).fileTime(QFileDevice::FileModificationTime) <= QFileInfo(pbesFilePath(propertyName)).fileTime(QFileDevice::FileModificationTime);
}
QString FileSystem::newProject(QString projectName)
{
makeSureProjectsFolderExists();
/* the project name may not be empty */
if (projectName.isEmpty()) {
return "The project name may not be empty";
}
/* create the folder for this project */
if (QDir(projectsFolderPath).mkdir(projectName)) {
/* if successful, create the properties folder too */
this->projectName = projectName;
QDir(projectFolderPath(projectName)).mkdir(propertiesFolderName);
projectOpen = true;
return "";
} else {
/* if not successful, there already is a project folder with this name */
return "A project with this name already exists";
}
}
QStringList FileSystem::getAllProjects()
{
makeSureProjectsFolderExists();
return QDir(projectsFolderPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
}
std::list<Property*> FileSystem::openProject(QString projectName)
{
this->projectName = projectName;
projectOpen = true;
/* read the specification and put it in the specification editor */
QFile *specificationFile = new QFile(specificationFilePath());
specificationFile->open(QIODevice::ReadOnly);
QTextStream *openStream = new QTextStream(specificationFile);
QString spec = openStream->readAll();
specificationEditor->setPlainText(spec);
/* get all properties and return them */
std::list<Property*> properties;
QDirIterator *dirIterator = new QDirIterator(QDir(propertiesFolderPath(projectName)));
while (dirIterator->hasNext()) {
QFile *propertyFile = new QFile(dirIterator->next());
QFileInfo *propertyFileInfo = new QFileInfo(*propertyFile);
QString fileName = propertyFileInfo->fileName();
if (propertyFileInfo->isFile() && fileName.endsWith(".mcf")) {
fileName.chop(4);
propertyFile->open(QIODevice::ReadOnly);
QTextStream *openStream = new QTextStream(propertyFile);
QString propertyText = openStream->readAll();
properties.push_back(new Property(fileName, propertyText));
}
}
return properties;
}
void FileSystem::saveSpecification()
{
makeSureProjectFolderExists();
QFile *specificationFile = new QFile(specificationFilePath());
specificationFile->open(QIODevice::WriteOnly);
QTextStream *saveStream = new QTextStream(specificationFile);
*saveStream << specificationEditor->toPlainText();
specificationFile->close();
specificationModified = false;
}
void FileSystem::saveProperty(Property *property)
{
makeSurePropertiesFolderExists();
QFile *propertyFile = new QFile(propertyFilePath(property->name));
propertyFile->open(QIODevice::WriteOnly);
QTextStream *saveStream = new QTextStream(propertyFile);
*saveStream << property->text;
propertyFile->close();
propertymodified[property->name] = false;
}