29 #include <KConfigGroup>
30 #include <KTemporaryFile>
32 #include <KTextEditor/Document>
49 KConfigGroup initKConfigObject() {
51 kDebug() <<
"Creating KConfig object temporary project file: " << _projectFile.toLocalFile();
52 _config =
new KConfig(_projectFile.toLocalFile());
54 KConfigGroup projectGroup(_config,
"Project");
56 foreach(
const QString& offset, codeFileIDs) {
57 _codeFileGroup.
insert(offset.
toInt(),
"CodeFile" + offset);
61 foreach(
const QString& offset, graphFileIDs) {
62 _graphFileGroup.
insert(offset.
toInt(),
"GraphFile" + offset);
66 KConfigGroup journalGroup(_config,
"Journal");
67 journalGroup.writeEntry(
"JournalHtml",
"journal.html");
74 : d(new ProjectPrivate)
76 KTemporaryFile tmpProjectFile;
77 tmpProjectFile.setPrefix(
"rocsproject");
78 tmpProjectFile.setSuffix(
".tmp");
79 tmpProjectFile.setAutoRemove(
false);
80 tmpProjectFile.open();
81 d->_projectFile = KUrl::fromLocalFile(tmpProjectFile.fileName());
83 d->initKConfigObject();
93 d->initKConfigObject();
94 if (!d->_config->isConfigWritable(
true)) {
97 d->_temporary =
false;
103 : d(new ProjectPrivate)
106 KTar tar(projectArchive.toLocalFile());
107 if (!tar.open(QIODevice::ReadOnly)) {
108 kError() <<
"Could not open export archive to read.";
111 tar.directory()->copyTo(projectDirectory.path(),
true);
112 KUrl
projectFile = projectDirectory.resolved(KUrl(
"project.rocs"));
115 d->initKConfigObject();
116 if (!d->_config->isConfigWritable(
true)) {
117 d->_temporary =
true;
119 d->_temporary =
false;
121 d->_projectFile = projectArchive;
122 d->_modified =
false;
131 KConfigGroup projectGroup(d->_config,
"Project");
132 projectGroup.writeEntry(
"Name", name);
138 KConfigGroup projectGroup(d->_config,
"Project");
139 return projectGroup.readEntry(
"Name",
QString());
144 if (d->_temporary ==
true) {
147 return d->_projectFile.directory(KUrl::AppendTrailingSlash);
152 return d->_projectFile;
157 d->_projectFile = fileUrl;
158 d->_temporary =
false;
163 QList<int> keys = d->_codeFileGroup.uniqueKeys();
165 if (keys.
count() > 0) {
166 newKey = keys.
last() + 1;
169 KConfigGroup newGroup(d->_config,
"CodeFile" +
QString(
"%1").arg(newKey));
170 newGroup.writeEntry(
"file", KUrl::relativePath(
projectDirectory(), file.toLocalFile()));
171 newGroup.writeEntry(
"identifier", newKey);
172 d->_codeFileGroup.insert(newKey,
"CodeFile" +
QString(
"%1").arg(newKey));
180 d->_config->deleteGroup(
"CodeFile" + fileID);
181 d->_codeFileGroup.remove(fileID);
187 foreach(
const QString& fileGroup, d->_codeFileGroup) {
188 KConfigGroup group(d->_config, fileGroup);
189 QString file = group.readEntry(
"file");
190 if (KUrl::isRelativeUrl(file)) {
193 files.
append(KUrl::fromLocalFile(group.readEntry(
"file")));
201 return d->_codeFileNew;
206 d->_codeFileNew.append(document);
211 d->_codeFileNew.removeAll(document);
217 document->saveAs(file);
223 QList<int> keys = d->_graphFileGroup.uniqueKeys();
225 if (keys.
count() > 0) {
226 newKey = keys.
last() + 1;
229 KConfigGroup newGroup(d->_config,
"GraphFile" +
QString(
"%1").arg(newKey));
230 newGroup.writeEntry(
"file", KUrl::relativePath(
projectDirectory(), file.toLocalFile()));
231 newGroup.writeEntry(
"identifier", newKey);
232 d->_graphFileGroup.insert(newKey,
"GraphFile" +
QString(
"%1").arg(newKey));
240 d->_config->deleteGroup(
"GraphFile" + fileID);
241 d->_graphFileGroup.remove(fileID);
247 foreach(
const QString& fileGroup, d->_graphFileGroup) {
248 KConfigGroup group(d->_config, fileGroup);
249 QString file = group.readEntry(
"file");
250 if (KUrl::isRelativeUrl(file)) {
253 files.
append(KUrl::fromLocalFile(group.readEntry(
"file")));
261 d->_graphFileNew.append(document);
267 d->_graphFileNew.removeAll(document);
284 if (d->_graphFileNew.contains(document)) {
289 int filekey = d->_graphFileGroup.key(document->
fileUrl());
290 d->_graphFileGroup[filekey] = file;
299 KConfigGroup group(d->_config,
"Journal");
303 bool Project::writeNewProjectFile()
305 if (!d->_config->isConfigWritable(
true)) {
306 kError() <<
"Cannot write to project config file.";
310 d->_modified =
false;
318 kError() <<
"Could not save temporary project file: no file URL specified.";
324 d->_config->markAsClean();
325 d->_projectFile = KUrl::fromLocalFile(fileUrl);
328 KConfig* temp = d->_config->copyTo(fileUrl);
334 KConfigGroup projectGroup(d->_config,
"Project");
337 foreach(
const QString& fileGroup, d->_codeFileGroup) {
338 KConfigGroup group(d->_config, fileGroup);
340 codeFileIDs.
append(group.readEntry(
"identifier"));
342 projectGroup.writeEntry(
"CodeFiles", codeFileIDs);
345 foreach(
const QString& fileGroup, d->_graphFileGroup) {
346 KConfigGroup group(d->_config, fileGroup);
348 graphFileIDs.
append(group.readEntry(
"identifier"));
350 projectGroup.writeEntry(
"GraphFiles", graphFileIDs);
354 d->_temporary =
false;
355 d->_modified =
false;
362 KTar tar(exportUrl.toLocalFile());
363 if (!tar.open(QIODevice::WriteOnly)) {
364 kError() <<
"Could not open export archive to write.";
369 KTemporaryFile tmpProjectConfig;
370 tmpProjectConfig.setPrefix(
"export");
371 tmpProjectConfig.setSuffix(
".rocs");
372 tmpProjectConfig.open();
373 KConfig* exportConfig = d->_config->copyTo(tmpProjectConfig.fileName());
374 KConfigGroup projectGroup(exportConfig,
"Project");
379 while (iter != d->_codeFileGroup.constEnd()) {
380 KConfigGroup group(exportConfig, (*iter));
383 QString configFileString = group.readEntry(
"file");
385 if (KUrl::isRelativeUrl(configFileString)) {
388 file = KUrl::fromLocalFile(configFileString);
390 tar.addLocalFile(file.toLocalFile(), file.fileName());
393 group.writeEntry(
"file", KUrl::relativePath(
projectDirectory(), file.fileName()));
395 codeFileIDs.
append(group.readEntry(
"identifier"));
397 projectGroup.writeEntry(
"CodeFiles", codeFileIDs);
400 iter = d->_graphFileGroup.constBegin();
401 while (iter != d->_graphFileGroup.constEnd()) {
402 KConfigGroup group(exportConfig, (*iter));
405 QString configFileString = group.readEntry(
"file");
407 if (KUrl::isRelativeUrl(configFileString)) {
410 file = KUrl::fromLocalFile(configFileString);
412 tar.addLocalFile(file.toLocalFile(), file.fileName());
415 group.writeEntry(
"file", KUrl::relativePath(
projectDirectory(), file.fileName()));
417 graphFileIDs.
append(group.readEntry(
"identifier"));
419 projectGroup.writeEntry(
"GraphFiles", graphFileIDs);
422 if (!journal.isEmpty()) {
423 tar.addLocalFile(journal.toLocalFile(), journal.fileName());
427 projectGroup.writeEntry(
"Directory",
"");
428 exportConfig->sync();
429 tar.addLocalFile(tmpProjectConfig.fileName(),
"project.rocs");
432 tmpProjectConfig.close();
439 return d->_temporary;
449 d->_modified = modified;
int addCodeFile(const KUrl &file)
Add code file to project.
Project()
Constructor for project that creates a new project with empty configuration.
QString name() const
Returns name of the project.
void removeGraphFile(int fileID)
Remove graph file from the project.
void removeCodeFileNew(KTextEditor::Document *document)
Remove code file document from list of temporary code files of the project.
void removeCodeFile(int fileID)
Remove code file from the project.
int addGraphFile(const KUrl &file)
Add graph file to project.
int count(const T &value) const
void append(const T &value)
void saveCodeFileNew(KTextEditor::Document *document, const KUrl &file)
Save a temporary code document to a code file and update association to the project.
void removeGraphFileNew(Document *document)
Remove graph file document from list of temporary graph files of the project.
int toInt(bool *ok, int base) const
bool writeProjectFile(const QString &fileUrl=QString())
Save the project to its current file if empty or no filename is given.
void addCodeFileNew(KTextEditor::Document *document)
Add a new temporary code file to the project.
void saveGraphFileNew(Document *document, const QString &file)
Save a temporary graph document to a graph file and update association to the project.
void setName(const QString &name)
Set project name.
bool isModified() const
Returns true if the poject is modified.
void saveGraphFileAs(Document *document, const QString &file)
Save existing graph document file under new filename.
QList< KTextEditor::Document * > codeFilesNew() const
Get list of all temporary code files of the project, i.e., code files that do not have a path...
QList< KUrl > graphFiles() const
Get list of all graph files of the project that have a path.
bool exportProject(const KUrl &exportUrl)
Exports the project with all its components (scripts, graph, journal) to an archive file...
void setProjectFile(const KUrl &fileUrl)
Set path of project to fileUrl and set project to being non-temporary.
void addGraphFileNew(Document *document)
Add a new temporary graph document to the project.
void insert(int i, const T &value)
QList< KUrl > codeFiles() const
Get list of all code files of the project that have a path.
bool isTemporary()
Specifies if the project file is only temporary or not.
void setModified(bool modified=true)
Set modified value of project to modified.
QString projectDirectory() const
Gives the working directory of the project.
void saveAs(const QString &fileUrl)
Save graph document under the given fileUrl.
KUrl journalFile() const
Returns URL to local file containing the project journal.