Tulip  4.7.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
TulipProject.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef TULIPPROJECT_H
21 #define TULIPPROJECT_H
22 
23 #include <QDir>
24 #include <QString>
25 #include <QStringList>
26 #include <QDate>
27 #include <QObject>
28 #include <QIODevice>
29 
30 #include <tulip/tulipconf.h>
31 
32 #include <fstream>
33 
34 namespace tlp {
35 
36 class PluginProgress;
37 
38 /**
39  @ingroup Plugins
40 
41  @brief The TulipProject object handles the content of a Tulip project.
42 
43  All tulip projects contain a set of defined static meta-information:
44  @list
45  @li name (QString): the name of the project
46  @li description (QString): Comments about the project
47  @li author (QString): the author of the project
48  @li perspective (QString): the name of the perspective plugin associated to the project
49  @li date (QDate): the date of project's last modification
50  @li version (QString): the version of the Tulip project format
51  @endlist
52 
53  Alongside this information, one can store any kind of file into a Tulip project. Since a project is meant to be associated to a specific perspective, the responisbility of those file
54  is left to the perspective.
55 
56  A TulipProject DOES NOT automatically save to disk. One will have to call the write() method to serialize data.
57  @warning Precise implementation of the TulipProject object should NOT be known or used by the user since it could be subject to changes.
58 
59  If something wrong happens when calling a method from TulipProject, this method will return either false or a invalid result (see specific method documentation). The last error message can be retrieved with the lastError() method.
60 
61  After opening and before saving a project, user will be able to list/delete files and directories available in the project and open them using std filestreams or Qt's QIODevice.
62  Files can be opened using the stdFileStram and fileStream methods. They will always be opened in Read/Write mode.
63 
64  Files in a tulip project are identified by their path. Those path ar similar to the path of a standard file system and use the "/" character as a separator.
65  The root directory is identified by "/".
66 
67  @warning TulipProject paths ALWAYS use the "/" character as a directory separator. This is OS-independant.
68 
69  @note A file called graph.tlp located at the top-level directory will be identified by the "/graph.tlp" path while the file "graph.tlp" located in the "data" directory will be identified by "/data/graph.tlp".
70  */
71 class TLP_QT_SCOPE TulipProject: public QObject {
72  Q_OBJECT
73 
74  TulipProject();
75  TulipProject(const QString &);
76 public:
77 
78  virtual ~TulipProject();
79 
80  /**
81  @brief Starts a new TulipProject from scratch
82 
83  This method builds up a new TulipProject file without taking any input.
84  @see openProject()
85  */
86  static TulipProject *newProject();
87 
88  /**
89  @brief Opens a previously saved tulip project file and returns the corresponding project
90 
91  This method will unpack a tulip project file into some directory and allow the user to manipulate the files.
92  @see TulipProject::save()
93  @param file The file to open.
94  @param progress A progress handler.
95  @return a pointer to a TulipProject object.
96  */
97  static TulipProject *openProject(const QString &file, tlp::PluginProgress *progress=NULL);
98 
99  /**
100  @brief Opens a previously saved tulip project file
101 
102  This method will unpack a tulip project file into some directory and allow the user to manipulate the files.
103  @see TulipProject::save()
104  @param file The file to open.
105  @param progress A progress handler.
106  @return true if the file has been successfully opened
107  */
108  bool openProjectFile(const QString &file, tlp::PluginProgress *progress=NULL);
109 
110  /**
111  @brief Restores a project which has already been extracted into path
112 
113  @warning Using several TulipProject instances on the same directory may result in undefined behavior. This method should only be used for crash handling purposes.
114  @param path The path where the archive was previously extracted
115  @return a pointer to a TulipProject object.
116  */
117  static TulipProject *restoreProject(const QString &path);
118 
119 
120  /**
121  @brief Writes files in the TulipProject into a packed archive.
122 
123  This method packs every file in the project into a single archive.
124  @note This method DOES NOT close the project. It only commits changes to the specified file. A TulipProject is only closed when destroyed.
125  @param file Absolute path where files should be packed.
126  @param progress A progress handler
127  @return False if method failed
128  */
129  bool write(const QString &file,tlp::PluginProgress *progress=NULL);
130 
131  /**
132  @brief Lists entries in a directory
133 
134  @see QDir documentation for a complete description of filtering arguments
135  @param path The path to scan. @see TulipProject
136  @return The list of files and directories present in the given directory
137  */
138  QStringList entryList(const QString &path, QDir::Filters filters = QDir::NoFilter, QDir::SortFlags sort = QDir::NoSort);
139 
140  /**
141  @brief Lists entries in a directory
142 
143  @see QDir documentation for a complete description of filtering arguments
144  @param path The path to scan. @see TulipProject
145  @return The list of files and directories present in the given directory
146  */
147  QStringList entryList(const QString &path, const QStringList &nameFilters, QDir::Filters filters = QDir::NoFilter, QDir::SortFlags sort = QDir::NoSort);
148 
149  /**
150  @brief Checks if the specified file/folder exists
151 
152  @param path The path to check.
153  @return true if the path exists.
154  */
155  bool exists(const QString &path);
156 
157  /**
158  @brief Recursively creates the specified path.
159 
160  Created folders will be empty
161  @return true if path was successfully created.
162  */
163  bool mkpath(const QString &path);
164 
165  /**
166  @brief Checks if the given path is a directory.
167 
168  @param path The path to check. @see TulipProject
169  @return true/false wether the path is a directory.
170  */
171  bool isDir(const QString &path);
172 
173  /**
174  @brief Removes a file from the project.
175 
176  If the given path points to a directory, or if the file does not exist, this method will fail and return false
177  @param path The path to delete. @see TulipProject
178  */
179  bool removeFile(const QString &path);
180 
181  /**
182  @brief Removes a directory from the project.
183 
184  If the given file points to a file, or if the directory does not exist, or if the directory is not empty, this method will fail and return false.
185  @see removeAllDir to remove a non-empty directory.
186  @param path The path to delete. @see TulipProject
187  */
188  bool removeDir(const QString &path);
189 
190  /**
191  @brief Removes a directory and all its content from the project.
192 
193  If the given file points to a file, or if the directory does not exist, this method will fail and return false.
194  @warning This will remove every file stored in the specified directory.
195  @param path The path to delete. @see TulipProject
196  */
197  bool removeAllDir(const QString &path);
198 
199  /**
200  @brief Copies a file from the local filesystem into the project
201 
202  @param source The absolute path of the file to copy
203  @param destination The project path where to copy the file
204  @return false if copy failed
205  */
206  bool copy(const QString& source, const QString& destination);
207 
208  /**
209  @brief Creates a empty file
210 
211  This method is similar to the UNIX's touch shell command. Except it won't renew the file's creation date if the file already exists.
212  @param file the file to create
213  @return true if file creation was sucessful.
214  */
215  bool touch(const QString& path);
216 
217  /**
218  @brief Gets a STL file stream (default to R/W access mode) to the given path.
219 
220  @warning This method does not check if the given path is a directory or a file. User might get an invalid filestream.
221  @warning It is up to the user to delete the std::fstream returned.
222  @param path The path to open. @see TulipProject
223  @return an opened filestream on the given path.
224  */
225  std::fstream *stdFileStream(const QString &path, std::ios_base::openmode=std::fstream::in | std::fstream::out | std::fstream::app);
226 
227  /**
228  @brief Gets a Qt I/O device (default to R/W access mode) to the given path.
229 
230  @warning This method does not check if the given path is a directory or a file. User might get an invalid filestream.
231  @warning User SHOULD NOT cast the QIODevice returned by this method into any of its subclass since the implementation might change in future versions.
232  @warning It is up to the user to delete the QIODevice returned.
233  @param path The path to open. @see TulipProject
234  @param mode The opening mode as described in the Qt documentation.
235  @return an opened Qt device on the given path.
236  */
237  QIODevice *fileStream(const QString &path, QIODevice::OpenMode mode=QIODevice::ReadWrite);
238 
239  /**
240  @brief Returns the last error raised.
241 
242  @note The returned string is null if no error was raised.
243  */
244  QString lastError() const {
245  return _lastError;
246  }
247 
248  /**
249  @brief Check if the object is a valid TulipProject.
250 
251  @warning Calling methods on invalid TulipProject instances may result in undefined behavior.
252  */
253  bool isValid() const {
254  return _isValid;
255  }
256 
257  /**
258  @brief Return the archive file associated with this project.
259 
260  If the project has been opened from an existing file or if the write method has already been called, this method will return the last file path specified.
261  In other cases, this method will return an empty string.
262  */
263  QString projectFile() const {
264  return _projectFile;
265  }
266 
267  /**
268  @brief This method returns the real absolute path corresponding to / in the TulipProject.
269 
270  This can be used to create a TulipProject directly from a path.
271  @warning Using several TulipProject instances at the same time on the same path may result in undefined behavior.
272  */
273  QString absoluteRootPath() const;
274 
275  // Developer note: Every field in the TulipProject tagged as a Q_PROPERTY will automaticaly be serialized in the project.xml file
276  /**
277  @brief the name of the project
278  */
279  Q_PROPERTY(QString name READ name WRITE setName)
280  /**
281  * @see name
282  */
283  QString name() const;
284 
285  /**
286  @brief User-written description of the project
287  */
288  Q_PROPERTY(QString description READ description WRITE setDescription)
289  /**
290  * @see description
291  */
292  QString description() const;
293 
294  /**
295  @brief Name of the author
296  */
297  Q_PROPERTY(QString author READ author WRITE setAuthor)
298  /**
299  * @see author
300  */
301  QString author() const;
302 
303  /**
304  @brief Name of the perspective associated to the project.
305 
306  When the user open a project from Tulip, this porperty is first read to identify find kind of perspective plugin should be launched to
307  open the project
308 
309  @warning If the perspective name associated to the project is invalid or correspond to a missing plugin, tulip may not be able to open the file.
310  */
311  Q_PROPERTY(QString perspective READ perspective WRITE setPerspective)
312  /**
313  * @see perspective
314  */
315  QString perspective() const;
316 
317  /**
318  @brief The version of the Tulip project format with which the file was created.
319  Project from older format versions will always by saved into the newest version available.
320  */
321  QString version() const;
322 
323  /**
324  @brief Returns the absolute filesystem path used to store the file
325  @warning Be cautious though since directly modifying project files without using TulipProject methods could result in undefined behavior.
326  */
327  QString toAbsolutePath(const QString &relativePath);
328 
329 signals:
330  void projectFileChanged(const QString& projectFile);
331 
332 public slots:
333  /**
334  * @see name
335  */
336  void setName(const QString &);
337  /**
338  * @see description
339  */
340  void setDescription(const QString &);
341  /**
342  * @see author
343  */
344  void setAuthor(const QString &);
345  /**
346  * @see perspective
347  */
348  void setPerspective(const QString &);
349 
350 private:
351  static QString temporaryPath();
352 
353  bool writeMetaInfos();
354  bool readMetaInfos();
355 
356  bool removeAllDirPrivate(const QString &path);
357 
358  // Core fileset
359  QDir _rootDir;
360  QDir _dataDir;
361  QString _projectFile;
362 
363  // Meta information
364  QString _author;
365  QString _name;
366  QString _description;
367  QString _perspective;
368 
369  // Error handling
370  QString _lastError;
371  bool _isValid;
372 };
373 
374 }
375 #endif // TULIPPROJECT_H
The TulipProject object handles the content of a Tulip project.
Definition: TulipProject.h:71
QString lastError() const
Returns the last error raised.
Definition: TulipProject.h:244
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...
QString projectFile() const
Return the archive file associated with this project.
Definition: TulipProject.h:263
bool isValid() const
Check if the object is a valid TulipProject.
Definition: TulipProject.h:253