WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZDir.h
Go to the documentation of this file.
1 /*
2 * Zuble - A run-time system for QML/Javascript applications
3 * Copyright (C) 2013, 2014 Bob Dinitto
4 *
5 * Filename: ZDir.h
6 * Created on: 11/9/2014
7 * Author: Bob Dinitto
8 *
9 * Zuble is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24 
25 #ifndef ZDIR_H
26 #define ZDIR_H
27 
28 #include <QObject>
29 #include <QDir>
30 #include <QVariant>
31 #include "zblcore_global.h"
32 #include "zglobal.h"
33 #include "ZblException.h"
34 
35 namespace Zbl
36 {
37 
42 class ZBLCORESHARED_EXPORT ZDir : public QObject
43 {
44  Q_OBJECT
45  Q_ENUMS(Filter)
46  Q_ENUMS(Sort)
47  Q_DISABLE_COPY(ZDir)
48 
49 public:
50 
51  // to participate in automatic enum propagation
52  // for QML programs we redeclare these QDir enums
53 
54  enum Filter {
55  Dirs = QDir::Dirs,
56  Files = QDir::Files,
57  Drives = QDir::Drives,
58  NoSymLinks = QDir::NoSymLinks,
59  AllEntries = QDir::AllEntries,
60  TypeMask = QDir::TypeMask,
61 
62  Readable = QDir::Readable,
63  Writable = QDir::Writable,
64  Executable = QDir::Executable,
65  PermissionMask = QDir::PermissionMask,
66 
67  Modified = QDir::Modified,
68  Hidden = QDir::Hidden,
69  System = QDir::System,
70 
71  AccessMask = QDir::AccessMask,
72 
73  AllDirs = QDir::AllDirs,
74  CaseSensitive = QDir::CaseSensitive,
75  NoDot = QDir::NoDot,
76  NoDotDot = QDir::NoDotDot,
77  NoDotAndDotDot = QDir::NoDotAndDotDot,
78 
79  NoFilter = QDir::NoFilter
80  };
81  Q_DECLARE_FLAGS(Filters, Filter)
82 
83 
84  enum Sort {
85  Name = QDir::Name,
86  Time = QDir::Time,
87  Size = QDir::Size,
88  Unsorted = QDir::Unsorted,
89  SortByMask = QDir::SortByMask,
90 
91  DirsFirst = QDir::DirsFirst,
92  Reversed = QDir::Reversed,
93  IgnoreCase = QDir::IgnoreCase,
94  DirsLast = QDir::DirsLast,
95  LocaleAware = QDir::LocaleAware,
96  Type = QDir::Type,
97  NoSort = QDir::NoSort
98  };
99  Q_DECLARE_FLAGS(SortFlags, Sort)
100 
101 
106  explicit ZDir(QObject *parent = nullptr);
107 
118  explicit ZDir(const QDir& dir, QObject *parent = nullptr);
119 
120  virtual ~ZDir();
121 
126  static void registerType();
127 
128  // Properties
129 
130  Q_PROPERTY(QVariant tags READ getTags NOTIFY tagsChanged)
131  Q_PROPERTY(uint filter READ getFilter WRITE setFilter NOTIFY filterChanged)
132  Q_PROPERTY(QString path READ getPath NOTIFY pathChanged)
133  Q_PROPERTY(QString absolutePath READ getAbsolutePath NOTIFY pathChanged)
134  Q_PROPERTY(QString canonicalPath READ getCanonicalPath NOTIFY pathChanged)
135  Q_PROPERTY(QString dirName READ getDirName NOTIFY pathChanged)
136  Q_PROPERTY(uint count READ getCount)
137  Q_PROPERTY(bool isReadable READ isReadable)
138  Q_PROPERTY(bool exists READ exists)
139  Q_PROPERTY(bool isRoot READ isRoot)
140 
141  // Javascript methods
142 
143  Q_INVOKABLE void setPath(const QString & path);
144  Q_INVOKABLE void setToCurrentPath();
145  Q_INVOKABLE void setToHomePath();
146  Q_INVOKABLE bool rename(const QString & oldName, const QString & newName);
147  Q_INVOKABLE void swap(QObject* zDir);
148 
149  Q_INVOKABLE bool setCurrent(const QString &path);
150  Q_INVOKABLE QString currentPath();
151  Q_INVOKABLE QString homePath();
152  Q_INVOKABLE QString rootPath();
153  Q_INVOKABLE QString tempPath();
154 
155  Q_INVOKABLE QString filePath(const QString &fileName) const;
156  Q_INVOKABLE QString absoluteFilePath(const QString &fileName) const;
157  Q_INVOKABLE QString relativeFilePath(const QString &fileName) const;
158 
159  Q_INVOKABLE bool cd(const QString &dirName);
160  Q_INVOKABLE bool cdUp();
161 
162  Q_INVOKABLE QString getChildName(int pos);
163 
164  Q_INVOKABLE bool mkdir(const QString &dirName) const;
165  Q_INVOKABLE bool rmdir(const QString &dirName) const;
166  Q_INVOKABLE bool mkpath(const QString &dirPath) const;
167  Q_INVOKABLE bool rmpath(const QString &dirPath) const;
168 
169  Q_INVOKABLE bool removeRecursively();
170 
171 
172  Q_INVOKABLE bool remove(const QString &fileName);
173  Q_INVOKABLE bool fileExists(const QString &name) const;
174 
175  Q_INVOKABLE void refresh() const;
176 
177  Q_INVOKABLE bool isRelativePath(const QString &path);
178  Q_INVOKABLE bool isAbsolutePath(const QString &path);
179  Q_INVOKABLE bool isRelative() const;
180  Q_INVOKABLE bool isAbsolute() const;
181  Q_INVOKABLE bool makeAbsolute();
182 
183  Q_INVOKABLE QString cleanPath(const QString &path);
184 
185 
191  Q_INVOKABLE QObject* entryInfoList(
192  int filters,
193  int sort) const;
194 
195 
201  Q_INVOKABLE QStringList entryList(int filters,
202  int sort) const;
203 
204  // Property access methods
205 
206  QVariant getTags();
207  uint getFilter() const;
208  void setFilter(uint filters);
209 
210  QString getPath() const;
211  QString getAbsolutePath() const;
212  QString getCanonicalPath() const;
213  QString getDirName() const;
214  uint getCount() const;
215  bool isReadable() const;
216  bool exists() const;
217  bool isRoot() const;
218 
219 #if 0
220 
221 
222  //inline void swap(QDir &other)
223  //{ qSwap(d_ptr, other.d_ptr); }
224 
225  //void setPath(const QString &path);
226  //QString path() const;
227  //QString absolutePath() const;
228  //QString canonicalPath() const;
229 
230  static void addResourceSearchPath(const QString &path);
231 
232  static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
233  static void addSearchPath(const QString &prefix, const QString &path);
234  static QStringList searchPaths(const QString &prefix);
235 
236  //QString dirName() const;
237  //QString filePath(const QString &fileName) const;
238  //QString absoluteFilePath(const QString &fileName) const;
239  //QString relativeFilePath(const QString &fileName) const;
240 
241  static QString toNativeSeparators(const QString &pathName);
242  static QString fromNativeSeparators(const QString &pathName);
243 
244  //bool cd(const QString &dirName);
245  //bool cdUp();
246 
247  QStringList nameFilters() const;
248  void setNameFilters(const QStringList &nameFilters);
249 
250  //Filters filter() const;
251  // void setFilter(Filters filter);
252  SortFlags sorting() const;
253  void setSorting(SortFlags sort);
254 
255  //uint count() const;
256  //QString operator[](int) const;
257 
258  static QStringList nameFiltersFromString(const QString &nameFilter);
259 
260  QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
261  QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
262  SortFlags sort = NoSort) const;
263 
264  QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
265  QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
266  SortFlags sort = NoSort) const;
267 
268  //bool mkdir(const QString &dirName) const;
269  //bool rmdir(const QString &dirName) const;
270  //bool mkpath(const QString &dirPath) const;
271  //bool rmpath(const QString &dirPath) const;
272 
273  //bool removeRecursively();
274 
275  //bool isReadable() const;
276  //bool exists() const;
277  //bool isRoot() const;
278 
279  //static bool isRelativePath(const QString &path);
280  //inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
281  //bool isRelative() const;
282  //inline bool isAbsolute() const { return !isRelative(); }
283  //bool makeAbsolute();
284 
285  bool operator==(const QDir &dir) const;
286  inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
287 
288  //bool remove(const QString &fileName);
289  //bool rename(const QString &oldName, const QString &newName);
290  // bool exists(const QString &name) const;
291 
292  static QFileInfoList drives();
293 
294  static QChar separator();
295 
296  //static bool setCurrent(const QString &path);
297  //static inline QDir current() { return QDir(currentPath()); }
298  //static QString currentPath();
299 
300  //static inline QDir home() { return QDir(homePath()); }
301  //static QString homePath();
302  //static inline QDir root() { return QDir(rootPath()); }
303  //static QString rootPath();
304  //static inline QDir temp() { return QDir(tempPath()); }
305  //static QString tempPath();
306 
307 
308  //void refresh() const;
309 
310 
311 #endif
312 
313 signals:
314 
315  void filterChanged();
316  void pathChanged();
317  void tagsChanged();
318 
319 public slots:
320 
321 
322 protected:
323 
324  void initDir();
325  void validateDir() const;
326  void createTags();
327 
328  QDir* m_dir;
329 
330  static QVariant m_tags;
331 };
332 
333 Q_DECLARE_OPERATORS_FOR_FLAGS(ZDir::Filters)
334 Q_DECLARE_OPERATORS_FOR_FLAGS(ZDir::SortFlags)
335 
336 
337 inline void ZDir::initDir()
338 {
339  if(!m_dir)
340  m_dir = new QDir();
341 }
342 
343 inline void ZDir::validateDir() const
344 {
345  if(!m_dir)
346  throw ZblException("Directory object not initialized. Use ZDir.setPath or ZDir.setToCurrentPath methods.");
347 }
348 
349 
350 } // Zbl
351 
352 
353 #endif // ZDIR_H
#define ZBLCORESHARED_EXPORT
Access and manipulate file directories. This class is a Javascript wrapper for QDir objects...
Definition: ZDir.h:42
Definition: ZAndGate.cpp:6
void validateDir() const
Definition: ZDir.h:343
Sort
Definition: ZDir.h:84
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
static QVariant m_tags
Definition: ZDir.h:330
Filter
Definition: ZDir.h:54
QDir * m_dir
Definition: ZDir.h:328