Tulip  4.4.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 1 and Inria Bordeaux - Sud Ouest
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
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 Restores a project which has already been extracted into path
101 
102  @warning Using several TulipProject instances on the same directory may result in undefined behavior. This method should only be used for crash handling purposes.
103  @param path The path where the archive was previously extracted
104  @return a pointer to a TulipProject object.
105  */
106  static TulipProject *restoreProject(const QString &path);
107 
108 
109  /**
110  @brief Writes files in the TulipProject into a packed archive.
111 
112  This method packs every file in the project into a single archive.
113  @note This method DOES NOT close the project. It only commits changes to the specified file. A TulipProject is only closed when destroyed.
114  @param file Absolute path where files should be packed.
115  @param progress A progress handler
116  @return False if method failed
117  */
118  bool write(const QString &file,tlp::PluginProgress *progress=NULL);
119 
120  /**
121  @brief Lists entries in a directory
122 
123  @see QDir documentation for a complete description of filtering arguments
124  @param path The path to scan. @see TulipProject
125  @return The list of files and directories present in the given directory
126  */
127  QStringList entryList(const QString &path, QDir::Filters filters = QDir::NoFilter, QDir::SortFlags sort = QDir::NoSort);
128 
129  /**
130  @brief Lists entries in a directory
131 
132  @see QDir documentation for a complete description of filtering arguments
133  @param path The path to scan. @see TulipProject
134  @return The list of files and directories present in the given directory
135  */
136  QStringList entryList(const QString &path, const QStringList &nameFilters, QDir::Filters filters = QDir::NoFilter, QDir::SortFlags sort = QDir::NoSort);
137 
138  /**
139  @brief Checks if the specified file/folder exists
140 
141  @param path The path to check.
142  @return true if the path exists.
143  */
144  bool exists(const QString &path);
145 
146  /**
147  @brief Recursively creates the specified path.
148 
149  Created folders will be empty
150  @return true if path was successfully created.
151  */
152  bool mkpath(const QString &path);
153 
154  /**
155  @brief Checks if the given path is a directory.
156 
157  @param path The path to check. @see TulipProject
158  @return true/false wether the path is a directory.
159  */
160  bool isDir(const QString &path);
161 
162  /**
163  @brief Removes a file from the project.
164 
165  If the given path points to a directory, or if the file does not exist, this method will fail and return false
166  @param path The path to delete. @see TulipProject
167  */
168  bool removeFile(const QString &path);
169 
170  /**
171  @brief Removes a directory from the project.
172 
173  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.
174  @see removeAllDir to remove a non-empty directory.
175  @param path The path to delete. @see TulipProject
176  */
177  bool removeDir(const QString &path);
178 
179  /**
180  @brief Removes a directory and all its content from the project.
181 
182  If the given file points to a file, or if the directory does not exist, this method will fail and return false.
183  @warning This will remove every file stored in the specified directory.
184  @param path The path to delete. @see TulipProject
185  */
186  bool removeAllDir(const QString &path);
187 
188  /**
189  @brief Copies a file from the local filesystem into the project
190 
191  @param source The absolute path of the file to copy
192  @param destination The project path where to copy the file
193  @return false if copy failed
194  */
195  bool copy(const QString& source, const QString& destination);
196 
197  /**
198  @brief Creates a empty file
199 
200  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.
201  @param file the file to create
202  @return true if file creation was sucessful.
203  */
204  bool touch(const QString& path);
205 
206  /**
207  @brief Gets a STL file stream (default to R/W access mode) to the given path.
208 
209  @warning This method does not check if the given path is a directory or a file. User might get an invalid filestream.
210  @warning It is up to the user to delete the std::fstream returned.
211  @param path The path to open. @see TulipProject
212  @return an opened filestream on the given path.
213  */
214  std::fstream *stdFileStream(const QString &path, std::ios_base::openmode=std::fstream::in | std::fstream::out | std::fstream::app);
215 
216  /**
217  @brief Gets a Qt I/O device (default to R/W access mode) to the given path.
218 
219  @warning This method does not check if the given path is a directory or a file. User might get an invalid filestream.
220  @warning User SHOULD NOT cast the QIODevice returned by this method into any of its subclass since the implementation might change in future versions.
221  @warning It is up to the user to delete the QIODevice returned.
222  @param path The path to open. @see TulipProject
223  @param mode The opening mode as described in the Qt documentation.
224  @return an opened Qt device on the given path.
225  */
226  QIODevice *fileStream(const QString &path, QIODevice::OpenMode mode=QIODevice::ReadWrite);
227 
228  /**
229  @brief Returns the last error raised.
230 
231  @note The returned string is null if no error was raised.
232  */
233  QString lastError() const {
234  return _lastError;
235  }
236 
237  /**
238  @brief Check if the object is a valid TulipProject.
239 
240  @warning Calling methods on invalid TulipProject instances may result in undefined behavior.
241  */
242  bool isValid() const {
243  return _isValid;
244  }
245 
246  /**
247  @brief Return the archive file associated with this project.
248 
249  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.
250  In other cases, this method will return an empty string.
251  */
252  QString projectFile() const {
253  return _projectFile;
254  }
255 
256  /**
257  @brief This method returns the real absolute path corresponding to / in the TulipProject.
258 
259  This can be used to create a TulipProject directly from a path.
260  @warning Using several TulipProject instances at the same time on the same path may result in undefined behavior.
261  */
262  QString absoluteRootPath() const;
263 
264  // Developer note: Every field in the TulipProject tagged as a Q_PROPERTY will automaticaly be serialized in the project.xml file
265  /**
266  @brief the name of the project
267  */
268  Q_PROPERTY(QString name READ name WRITE setName)
269  /**
270  * @see name
271  */
272  QString name() const;
273 
274  /**
275  @brief User-written description of the project
276  */
277  Q_PROPERTY(QString description READ description WRITE setDescription)
278  /**
279  * @see description
280  */
281  QString description() const;
282 
283  /**
284  @brief Name of the author
285  */
286  Q_PROPERTY(QString author READ author WRITE setAuthor)
287  /**
288  * @see author
289  */
290  QString author() const;
291 
292  /**
293  @brief Name of the perspective associated to the project.
294 
295  When the user open a project from Tulip, this porperty is first read to identify find kind of perspective plugin should be launched to
296  open the project
297 
298  @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.
299  */
300  Q_PROPERTY(QString perspective READ perspective WRITE setPerspective)
301  /**
302  * @see perspective
303  */
304  QString perspective() const;
305 
306  /**
307  @brief The version of the Tulip project format with which the file was created.
308  Project from older format versions will always by saved into the newest version available.
309  */
310  QString version() const;
311 
312  /**
313  @brief Returns the absolute filesystem path used to store the file
314  @warning Be cautious though since directly modifying project files without using TulipProject methods could result in undefined behavior.
315  */
316  QString toAbsolutePath(const QString &relativePath);
317 
318 public slots:
319  /**
320  * @see name
321  */
322  void setName(const QString &);
323  /**
324  * @see description
325  */
326  void setDescription(const QString &);
327  /**
328  * @see author
329  */
330  void setAuthor(const QString &);
331  /**
332  * @see perspective
333  */
334  void setPerspective(const QString &);
335 
336 private:
337  static QString temporaryPath();
338 
339  bool writeMetaInfos();
340  bool readMetaInfos();
341 
342  bool removeAllDirPrivate(const QString &path);
343 
344  // Core fileset
345  QDir _rootDir;
346  QDir _dataDir;
347  QString _projectFile;
348 
349  // Meta information
350  QString _author;
351  QString _name;
352  QString _description;
353  QString _perspective;
354 
355  // Error handling
356  QString _lastError;
357  bool _isValid;
358 };
359 
360 }
361 #endif // TULIPPROJECT_H