• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
Project.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 #include "Project.h"
21 #include "Document.h"
22 
23 #include <QString>
24 #include <QMap>
25 #include <QList>
26 
27 #include <KUrl>
28 #include <KConfig>
29 #include <KConfigGroup>
30 #include <KTemporaryFile>
31 #include <KDebug>
32 #include <KTextEditor/Document>
33 #include <KTar>
34 
35 class ProjectPrivate
36 {
37 public:
38  ProjectPrivate() {}
39 
40  KUrl _projectFile;
41  QMap<int, QString> _codeFileGroup;
42  QMap<int, QString> _graphFileGroup;
43  QList<Document*> _graphFileNew;
44  QList<KTextEditor::Document*> _codeFileNew;
45  KConfig* _config;
46  bool _temporary;
47  bool _modified;
48 
49  KConfigGroup initKConfigObject() {
50  // helper method for Project::open()
51  kDebug() << "Creating KConfig object temporary project file: " << _projectFile.toLocalFile();
52  _config = new KConfig(_projectFile.toLocalFile());
53 
54  KConfigGroup projectGroup(_config, "Project");
55  QStringList codeFileIDs = projectGroup.readEntry("CodeFiles", QStringList());
56  foreach(const QString& offset, codeFileIDs) {
57  _codeFileGroup.insert(offset.toInt(), "CodeFile" + offset);
58  }
59 
60  QStringList graphFileIDs = projectGroup.readEntry("GraphFiles", QStringList());
61  foreach(const QString& offset, graphFileIDs) {
62  _graphFileGroup.insert(offset.toInt(), "GraphFile" + offset);
63  }
64 
65  // for now, journal files only have name "journal.html"
66  KConfigGroup journalGroup(_config, "Journal");
67  journalGroup.writeEntry("JournalHtml", "journal.html");
68 
69  return projectGroup;
70  }
71 };
72 
73 Project::Project()
74  : d(new ProjectPrivate)
75 {
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());
82 
83  d->initKConfigObject();
84  d->_temporary = true;
85  d->_modified = false;
86 }
87 
88 Project::Project(const KUrl &projectFile) :
89  d(new ProjectPrivate)
90 {
91  d->_projectFile = projectFile;
92 
93  d->initKConfigObject();
94  if (!d->_config->isConfigWritable(true)) {
95  d->_temporary = true;
96  } else {
97  d->_temporary = false;
98  }
99  d->_modified = false;
100 }
101 
102 Project::Project(const KUrl &projectArchive, const KUrl &projectDirectory)
103  : d(new ProjectPrivate)
104 {
105  // extract archive into project directory
106  KTar tar(projectArchive.toLocalFile());
107  if (!tar.open(QIODevice::ReadOnly)) {
108  kError() << "Could not open export archive to read.";
109  return;
110  }
111  tar.directory()->copyTo(projectDirectory.path(), true);
112  KUrl projectFile = projectDirectory.resolved(KUrl("project.rocs"));
113 
114  d->_projectFile = projectFile;
115  d->initKConfigObject();
116  if (!d->_config->isConfigWritable(true)) {
117  d->_temporary = true;
118  } else {
119  d->_temporary = false;
120  }
121  d->_projectFile = projectArchive;
122  d->_modified = false;
123 }
124 
125 Project::~Project()
126 {
127 }
128 
129 void Project::setName(const QString &name)
130 {
131  KConfigGroup projectGroup(d->_config, "Project");
132  projectGroup.writeEntry("Name", name);
133  d->_modified = true;
134 }
135 
136 QString Project::name() const
137 {
138  KConfigGroup projectGroup(d->_config, "Project");
139  return projectGroup.readEntry("Name", QString());
140 }
141 
142 QString Project::projectDirectory() const
143 {
144  if (d->_temporary == true) {
145  return "";
146  }
147  return d->_projectFile.directory(KUrl::AppendTrailingSlash);
148 }
149 
150 KUrl Project::projectFile() const
151 {
152  return d->_projectFile;
153 }
154 
155 void Project::setProjectFile(const KUrl &fileUrl)
156 {
157  d->_projectFile = fileUrl;
158  d->_temporary = false;
159 }
160 
161 int Project::addCodeFile(const KUrl &file)
162 {
163  QList<int> keys = d->_codeFileGroup.uniqueKeys();
164  int newKey = 1;
165  if (keys.count() > 0) {
166  newKey = keys.last() + 1;
167  }
168 
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));
173  d->_modified = true;
174 
175  return newKey;
176 }
177 
178 void Project::removeCodeFile(int fileID)
179 {
180  d->_config->deleteGroup("CodeFile" + fileID);
181  d->_codeFileGroup.remove(fileID);
182 }
183 
184 QList<KUrl> Project::codeFiles() const
185 {
186  QList<KUrl> files;
187  foreach(const QString& fileGroup, d->_codeFileGroup) {
188  KConfigGroup group(d->_config, fileGroup);
189  QString file = group.readEntry("file");
190  if (KUrl::isRelativeUrl(file)) {
191  files.append(KUrl(projectDirectory(), group.readEntry("file")));
192  } else {
193  files.append(KUrl::fromLocalFile(group.readEntry("file")));
194  }
195  }
196  return files;
197 }
198 
199 QList< KTextEditor::Document* > Project::codeFilesNew() const
200 {
201  return d->_codeFileNew;
202 }
203 
204 void Project::addCodeFileNew(KTextEditor::Document *document)
205 {
206  d->_codeFileNew.append(document);
207 }
208 
209 void Project::removeCodeFileNew(KTextEditor::Document *document)
210 {
211  d->_codeFileNew.removeAll(document);
212 }
213 
214 void Project::saveCodeFileNew(KTextEditor::Document *document, const KUrl &file)
215 {
216  removeCodeFileNew(document);
217  document->saveAs(file);
218  addCodeFile(file);
219 }
220 
221 int Project::addGraphFile(const KUrl &file)
222 {
223  QList<int> keys = d->_graphFileGroup.uniqueKeys();
224  int newKey = 1;
225  if (keys.count() > 0) {
226  newKey = keys.last() + 1;
227  }
228 
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));
233  d->_modified = true;
234 
235  return newKey;
236 }
237 
238 void Project::removeGraphFile(int fileID)
239 {
240  d->_config->deleteGroup("GraphFile" + fileID);
241  d->_graphFileGroup.remove(fileID);
242 }
243 
244 QList<KUrl> Project::graphFiles() const
245 {
246  QList< KUrl > files;
247  foreach(const QString& fileGroup, d->_graphFileGroup) {
248  KConfigGroup group(d->_config, fileGroup);
249  QString file = group.readEntry("file");
250  if (KUrl::isRelativeUrl(file)) {
251  files.append(KUrl(projectDirectory(), group.readEntry("file")));
252  } else {
253  files.append(KUrl::fromLocalFile(group.readEntry("file")));
254  }
255  }
256  return files;
257 }
258 
259 void Project::addGraphFileNew(Document *document)
260 {
261  d->_graphFileNew.append(document);
262 }
263 
264 
265 void Project::removeGraphFileNew(Document *document)
266 {
267  d->_graphFileNew.removeAll(document);
268 }
269 
270 void Project::saveGraphFileNew(Document *document, const QString &file)
271 {
272  removeGraphFileNew(document);
273  document->saveAs(file);
274  addGraphFile(KUrl::fromLocalFile(document->fileUrl()));
275 }
276 
277 void Project::saveGraphFileAs(Document *document, const QString &file)
278 {
279  Q_ASSERT(document);
280  if (d == 0) {
281  return;
282  }
283 
284  if (d->_graphFileNew.contains(document)) {
285  saveGraphFileNew(document, file);
286  return;
287  }
288  // TODO the following is probably error prone
289  int filekey = d->_graphFileGroup.key(document->fileUrl());
290  d->_graphFileGroup[filekey] = file;
291  document->saveAs(file);
292 }
293 
294 KUrl Project::journalFile() const
295 {
296  if (projectDirectory().isEmpty()) {
297  return KUrl();
298  }
299  KConfigGroup group(d->_config, "Journal");
300  return KUrl::fromLocalFile(projectDirectory().append(group.readEntry("JournalHtml", QString())));
301 }
302 
303 bool Project::writeNewProjectFile()
304 {
305  if (!d->_config->isConfigWritable(true)) {
306  kError() << "Cannot write to project config file.";
307  return false;
308  }
309  d->_config->sync();
310  d->_modified = false;
311 
312  return true;
313 }
314 
315 bool Project::writeProjectFile(const QString &fileUrl)
316 {
317  if (fileUrl.isEmpty() && isTemporary()) {
318  kError() << "Could not save temporary project file: no file URL specified.";
319  return false;
320  }
321 
322  if (!fileUrl.isEmpty()) {
323  // do not save to the old file
324  d->_config->markAsClean();
325  d->_projectFile = KUrl::fromLocalFile(fileUrl);
326 
327  // copy and save
328  KConfig* temp = d->_config->copyTo(fileUrl);
329  delete d->_config;
330  d->_config = temp;
331  }
332 
333  // update file reference lists
334  KConfigGroup projectGroup(d->_config, "Project");
335 
336  QStringList codeFileIDs;
337  foreach(const QString& fileGroup, d->_codeFileGroup) {
338  KConfigGroup group(d->_config, fileGroup);
339  // TODO change to order given by editor
340  codeFileIDs.append(group.readEntry("identifier"));
341  }
342  projectGroup.writeEntry("CodeFiles", codeFileIDs);
343 
344  QStringList graphFileIDs;
345  foreach(const QString& fileGroup, d->_graphFileGroup) {
346  KConfigGroup group(d->_config, fileGroup);
347  // TODO change to order given by editor
348  graphFileIDs.append(group.readEntry("identifier"));
349  }
350  projectGroup.writeEntry("GraphFiles", graphFileIDs);
351 
352  // write back
353  d->_config->sync();
354  d->_temporary = false;
355  d->_modified = false;
356 
357  return true;
358 }
359 
360 bool Project::exportProject(const KUrl &exportUrl)
361 {
362  KTar tar(exportUrl.toLocalFile());
363  if (!tar.open(QIODevice::WriteOnly)) {
364  kError() << "Could not open export archive to write.";
365  return false;
366  }
367 
368  // create project configuration for export
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");
375 
376  // add all scripts to tarball
377  QStringList codeFileIDs;
378  QMap<int, QString>::const_iterator iter = d->_codeFileGroup.constBegin();
379  while (iter != d->_codeFileGroup.constEnd()) {
380  KConfigGroup group(exportConfig, (*iter));
381 
382  // get file url and add to Tar
383  QString configFileString = group.readEntry("file");
384  KUrl file;
385  if (KUrl::isRelativeUrl(configFileString)) {
386  file = KUrl(projectDirectory(), configFileString);
387  } else {
388  file = KUrl::fromLocalFile(configFileString);
389  }
390  tar.addLocalFile(file.toLocalFile(), file.fileName());
391 
392  // update export project config in case of out-of-project-dir files
393  group.writeEntry("file", KUrl::relativePath(projectDirectory(), file.fileName()));
394  ++iter;
395  codeFileIDs.append(group.readEntry("identifier"));
396  }
397  projectGroup.writeEntry("CodeFiles", codeFileIDs);
398 
399  QStringList graphFileIDs;
400  iter = d->_graphFileGroup.constBegin();
401  while (iter != d->_graphFileGroup.constEnd()) {
402  KConfigGroup group(exportConfig, (*iter));
403 
404  // get file url and add to Tar
405  QString configFileString = group.readEntry("file");
406  KUrl file;
407  if (KUrl::isRelativeUrl(configFileString)) {
408  file = KUrl(projectDirectory(), configFileString);
409  } else {
410  file = KUrl::fromLocalFile(configFileString);
411  }
412  tar.addLocalFile(file.toLocalFile(), file.fileName());
413 
414  // update export project config in case of out-of-project-dir files
415  group.writeEntry("file", KUrl::relativePath(projectDirectory(), file.fileName()));
416  ++iter;
417  graphFileIDs.append(group.readEntry("identifier"));
418  }
419  projectGroup.writeEntry("GraphFiles", graphFileIDs);
420 
421  KUrl journal(journalFile());
422  if (!journal.isEmpty()) {
423  tar.addLocalFile(journal.toLocalFile(), journal.fileName());
424  }
425 
426  // write project to export
427  projectGroup.writeEntry("Directory", "");
428  exportConfig->sync();
429  tar.addLocalFile(tmpProjectConfig.fileName(), "project.rocs");
430 
431  tar.close(); // write tar
432  tmpProjectConfig.close(); // delete temporary config
433 
434  return true;
435 }
436 
437 bool Project::isTemporary()
438 {
439  return d->_temporary;
440 }
441 
442 bool Project::isModified() const
443 {
444  return d->_modified;
445 }
446 
447 void Project::setModified(bool modified)
448 {
449  d->_modified = modified;
450 }
Project::addCodeFile
int addCodeFile(const KUrl &file)
Add code file to project.
Definition: Project.cpp:161
Project::Project
Project()
Constructor for project that creates a new project with empty configuration.
Definition: Project.cpp:73
Project::name
QString name() const
Returns name of the project.
Definition: Project.cpp:136
Project::removeGraphFile
void removeGraphFile(int fileID)
Remove graph file from the project.
Definition: Project.cpp:238
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
Project::removeCodeFileNew
void removeCodeFileNew(KTextEditor::Document *document)
Remove code file document from list of temporary code files of the project.
Definition: Project.cpp:209
Project.h
Project::removeCodeFile
void removeCodeFile(int fileID)
Remove code file from the project.
Definition: Project.cpp:178
Project::addGraphFile
int addGraphFile(const KUrl &file)
Add graph file to project.
Definition: Project.cpp:221
Document.h
Project::saveCodeFileNew
void saveCodeFileNew(KTextEditor::Document *document, const KUrl &file)
Save a temporary code document to a code file and update association to the project.
Definition: Project.cpp:214
Project::removeGraphFileNew
void removeGraphFileNew(Document *document)
Remove graph file document from list of temporary graph files of the project.
Definition: Project.cpp:265
Project::writeProjectFile
bool writeProjectFile(const QString &fileUrl=QString())
Save the project to its current file if empty or no filename is given.
Definition: Project.cpp:315
Project::addCodeFileNew
void addCodeFileNew(KTextEditor::Document *document)
Add a new temporary code file to the project.
Definition: Project.cpp:204
Project::saveGraphFileNew
void saveGraphFileNew(Document *document, const QString &file)
Save a temporary graph document to a graph file and update association to the project.
Definition: Project.cpp:270
Project::setName
void setName(const QString &name)
Set project name.
Definition: Project.cpp:129
Document
Definition: Document.h:41
Project::isModified
bool isModified() const
Returns true if the poject is modified.
Definition: Project.cpp:442
Project::saveGraphFileAs
void saveGraphFileAs(Document *document, const QString &file)
Save existing graph document file under new filename.
Definition: Project.cpp:277
Project::codeFilesNew
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...
Definition: Project.cpp:199
Project::graphFiles
QList< KUrl > graphFiles() const
Get list of all graph files of the project that have a path.
Definition: Project.cpp:244
Project::exportProject
bool exportProject(const KUrl &exportUrl)
Exports the project with all its components (scripts, graph, journal) to an archive file...
Definition: Project.cpp:360
Project::setProjectFile
void setProjectFile(const KUrl &fileUrl)
Set path of project to fileUrl and set project to being non-temporary.
Definition: Project.cpp:155
Project::addGraphFileNew
void addGraphFileNew(Document *document)
Add a new temporary graph document to the project.
Definition: Project.cpp:259
Project::codeFiles
QList< KUrl > codeFiles() const
Get list of all code files of the project that have a path.
Definition: Project.cpp:184
Project::projectFile
KUrl projectFile() const
Definition: Project.cpp:150
Project::isTemporary
bool isTemporary()
Specifies if the project file is only temporary or not.
Definition: Project.cpp:437
Document::fileUrl
QString fileUrl() const
Definition: Document.cpp:386
Project::setModified
void setModified(bool modified=true)
Set modified value of project to modified.
Definition: Project.cpp:447
Project::projectDirectory
QString projectDirectory() const
Gives the working directory of the project.
Definition: Project.cpp:142
Project::~Project
virtual ~Project()
Definition: Project.cpp:125
Document::saveAs
void saveAs(const QString &fileUrl)
Save graph document under the given fileUrl.
Definition: Document.cpp:381
Project::journalFile
KUrl journalFile() const
Returns URL to local file containing the project journal.
Definition: Project.cpp:294
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal