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.cpp
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.cpp
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 #include <QtQml>
26 
27 #include "ZDir.h"
28 #include "ZblException.h"
29 #include "ZblThreadErr.h"
30 #include "ZFileInfo.h"
31 
32 namespace Zbl
33 
34 {
35 
36 QVariant ZDir::m_tags;
37 
42 ZDir::ZDir(QObject *parent) :
43  QObject(parent), m_dir(nullptr)
44 {
45  createTags();
46 }
47 
48 
49 ZDir::ZDir(const QDir& dir, QObject *parent) :
50  QObject(parent), m_dir(new QDir(dir))
51 {
52  createTags();
53 }
54 
56 {
57  if(m_dir)
58  delete(m_dir);
59 }
60 
66 {
67  qmlRegisterType<ZDir>("org.zuble.qml", 1, 0, "ZDir");
68 }
69 
70 
85 {
86  if(!m_tags.isNull())
87  return;
88 
89  QVariantMap map;
90 
91  map.insert("Dirs", Dirs);
92  map.insert("Files", Files);
93  map.insert("Drives", Drives);
94  map.insert("NoSymLinks", NoSymLinks);
95  map.insert("AllEntries", AllEntries);
96  map.insert("TypeMask", TypeMask);
97  map.insert("Readable", Readable);
98  map.insert("Writable", Writable);
99  map.insert("Executable", Executable);
100  map.insert("PermissionMask", PermissionMask);
101  map.insert("Modified", Modified);
102  map.insert("Hidden", Hidden);
103  map.insert("System", System);
104  map.insert("AccessMask", AccessMask);
105  map.insert("AllDirs", AllDirs);
106  map.insert("CaseSensitive", CaseSensitive);
107  map.insert("NoDot", NoDot);
108  map.insert("NoDotDot", NoDotDot);
109  map.insert("NoDotAndDotDot", NoDotAndDotDot);
110  map.insert("NoFilter", NoFilter);
111 
112  map.insert("Name", Name);
113  map.insert("Time", Time);
114  map.insert("Size", Size);
115  map.insert("Unsorted", Unsorted);
116  map.insert("SortByMask", SortByMask);
117  map.insert("DirsFirst", DirsFirst);
118  map.insert("Reversed", Reversed);
119  map.insert("IgnoreCase", IgnoreCase);
120  map.insert("DirsLast", DirsLast);
121  map.insert("LocaleAware", LocaleAware);
122  map.insert("Type", Type);
123  map.insert("NoSort", NoSort);
124 
125  m_tags = QVariant::fromValue(map);
126 }
127 
128 QVariant ZDir::getTags()
129 {
130  return m_tags;
131 }
132 
133 
134 uint ZDir::getFilter() const
135 {
137  validateDir();
138  ZBL_SLOT_END_RETURN(static_cast<uint>(m_dir->filter()), 0,
140 }
141 
142 void ZDir::setFilter(uint filters)
143 {
145  initDir();
146  m_dir->setFilter(QDir::Filters(filters));
148 }
149 
150 void ZDir::setPath(const QString& path)
151 {
153  initDir();
154  m_dir->setPath(path);
155  ZBL_SLOT_END_VOID(Z_FAC_JS, ZDir::setPath, set path failed)
156 }
157 
159 {
161  initDir();
162  m_dir->setPath(QDir::currentPath());
163  ZBL_SLOT_END_VOID(Z_FAC_JS, ZDir::setToCurrentPath, set to current path failed)
164 }
165 
167 {
169  initDir();
170  m_dir->setPath(QDir::homePath());
171  ZBL_SLOT_END_VOID(Z_FAC_JS, ZDir::setToCurrentPath, set to current path failed)
172 }
173 
174 bool ZDir::rename(const QString& oldName, const QString& newName)
175 {
177  initDir();
178  ZBL_SLOT_END_RETURN(m_dir->rename(oldName, newName), false,
179  Z_FAC_JS, ZDir::rename, directory rename failed)
180 }
181 
182 void ZDir::swap(QObject* zDir)
183 {
185 
186  if(!zDir)
187  throw Zbl::ZblException("can't swap NULL object");
188 
189  initDir();
190 
191  ZDir* inDir = qobject_cast<ZDir*>(zDir);
192 
193  if(!inDir)
194  throw ZblException("Invalid object passed to ZDir.swap, object must be type Zbl::ZDir");
195 
196  m_dir->swap(*inDir->m_dir);
197 
198  ZBL_SLOT_END_VOID(Z_FAC_JS, ZDir::swap, directory swap failed)
199 
200 }
201 
202 
203 QString ZDir::getPath() const
204 {
206  validateDir();
207  ZBL_SLOT_END_RETURN(m_dir->path(), QString(),
208  Z_FAC_JS, ZDir::getPath, getPath failed)
209 }
210 
211 QString ZDir::getAbsolutePath() const
212 {
214  validateDir();
215  ZBL_SLOT_END_RETURN(m_dir->absolutePath(), QString(),
217 }
218 
219 QString ZDir::getCanonicalPath() const
220 {
222  validateDir();
223  ZBL_SLOT_END_RETURN(m_dir->canonicalPath(), QString(),
225 
226 }
227 
228 QString ZDir::getDirName() const
229 {
231  validateDir();
232  ZBL_SLOT_END_RETURN(m_dir->dirName(), QString(),
234 
235 }
236 
237 QString ZDir::filePath(const QString &fileName) const
238 {
240  validateDir();
241  ZBL_SLOT_END_RETURN(m_dir->filePath(fileName), QString(),
243 }
244 
245 QString ZDir::absoluteFilePath(const QString &fileName) const
246 {
248  validateDir();
249  ZBL_SLOT_END_RETURN(m_dir->absoluteFilePath(fileName), QString(),
251 }
252 
253 QString ZDir::relativeFilePath(const QString &fileName) const
254 {
256  validateDir();
257  ZBL_SLOT_END_RETURN(m_dir->relativeFilePath(fileName), QString(),
259 }
260 
261 bool ZDir::cd(const QString &dirName)
262 {
264  validateDir();
265  ZBL_SLOT_END_RETURN(m_dir->cd(dirName), false,
266  Z_FAC_JS, ZDir::cd, cd failed)
267 }
268 
270 {
272  validateDir();
273  ZBL_SLOT_END_RETURN(m_dir->cdUp(), false,
274  Z_FAC_JS, ZDir::cdUp, cdUp failed)
275 }
276 
277 uint ZDir::getCount() const
278 {
280  validateDir();
281  ZBL_SLOT_END_RETURN(m_dir->count(), 0,
283 }
284 
285 QString ZDir::getChildName(int pos)
286 {
288  validateDir();
289  ZBL_SLOT_END_RETURN((*m_dir)[pos], QString(),
291 }
292 
293  bool ZDir::mkdir(const QString &dirName) const
294  {
296  validateDir();
297  ZBL_SLOT_END_RETURN(m_dir->mkdir(dirName), false,
298  Z_FAC_JS, ZDir::mkdir, mkdir failed)
299  }
300 
301  bool ZDir::rmdir(const QString &dirName) const
302  {
304  validateDir();
305  ZBL_SLOT_END_RETURN(m_dir->rmdir(dirName), false,
306  Z_FAC_JS, ZDir::rmdir, rmdir failed)
307  }
308 
309  bool ZDir::mkpath(const QString &dirPath) const
310  {
312  validateDir();
313  ZBL_SLOT_END_RETURN(m_dir->mkpath(dirPath), false,
314  Z_FAC_JS, ZDir::mkpath, mkpath failed)
315  }
316 
317  bool ZDir::rmpath(const QString &dirPath) const
318  {
320  validateDir();
321  ZBL_SLOT_END_RETURN(m_dir->rmpath(dirPath), false,
322  Z_FAC_JS, ZDir::rmpath, rmpath failed)
323  }
324 
326  {
328  validateDir();
329  ZBL_SLOT_END_RETURN(m_dir->removeRecursively(), false,
331  }
332 
333  bool ZDir::isReadable() const
334  {
336  validateDir();
337  ZBL_SLOT_END_RETURN(m_dir->isReadable(), false,
339 
340  bool ZDir::exists() const
341  {
343  validateDir();
344  ZBL_SLOT_END_RETURN(m_dir->exists(), false,
345  Z_FAC_JS, ZDir::exists, exists failed)
346  }
347 
348  bool ZDir::isRoot() const
349  {
351  validateDir();
352  ZBL_SLOT_END_RETURN(m_dir->isRoot(), false,
353  Z_FAC_JS, ZDir::isRoot, isRoot failed)
354  }
355 
356  bool ZDir::remove(const QString &fileName)
357  {
359  validateDir();
360  ZBL_SLOT_END_RETURN(m_dir->remove(fileName), false,
361  Z_FAC_JS, ZDir::remove, remove failed)
362  }
363 
364  bool ZDir::fileExists(const QString &name) const
365  {
367  validateDir();
368  ZBL_SLOT_END_RETURN(m_dir->exists(name), false,
369  Z_FAC_JS, ZDir::exists, exists failed)
370  }
371 
372  void ZDir::refresh() const
373  {
375  validateDir();
376  m_dir->refresh();
378  }
379 
380 
381  bool ZDir::isRelativePath(const QString &path)
382  {
384  ZBL_SLOT_END_RETURN(QDir::isRelativePath(path), false,
386  }
387 
388  bool ZDir::isAbsolutePath(const QString &path)
389  {
391  ZBL_SLOT_END_RETURN(QDir::isAbsolutePath(path), false,
393  }
394 
395  bool ZDir::isRelative() const
396  {
398  validateDir();
399  ZBL_SLOT_END_RETURN(m_dir->isRelative(), false,
401  }
402 
403  bool ZDir::isAbsolute() const
404  {
406  validateDir();
407  ZBL_SLOT_END_RETURN(m_dir->isAbsolute(), false,
409  }
410 
412  {
414  validateDir();
415  ZBL_SLOT_END_RETURN(m_dir->makeAbsolute(), false,
417  }
418 
419 
420  bool ZDir::setCurrent(const QString &path)
421  {
423  ZBL_SLOT_END_RETURN(QDir::setCurrent(path), false,
425  }
426 
428  {
430  ZBL_SLOT_END_RETURN(QDir::currentPath(), QString(),
432  }
433 
434  QString ZDir::homePath()
435  {
437  ZBL_SLOT_END_RETURN(QDir::homePath(), QString(),
439  }
440 
441  QString ZDir::rootPath()
442  {
444  ZBL_SLOT_END_RETURN(QDir::rootPath(), QString(),
446  }
447 
448  QString ZDir::tempPath()
449  {
451  ZBL_SLOT_END_RETURN(QDir::tempPath(), QString(),
453  }
454 
455 
456  QString ZDir::cleanPath(const QString &path)
457  {
459  ZBL_SLOT_END_RETURN(QDir::cleanPath(path), QString(),
461  }
462 
463 
464 
465  QObject* ZDir::entryInfoList(int filters, int sort) const
466  {
467  //Q_UNUSED(filters)
468  //Q_UNUSED(sort)
469 
470  qDebug("ZDir::entryInfoList -- filters = %x, sort = %x", filters, sort);
471 
472  ZFileInfo* info = nullptr;
473 
474 
476  validateDir();
477  info = new ZFileInfo();
478 
479  info->setInfoList(m_dir->entryInfoList(static_cast<QDir::Filter>(filters),
480  static_cast<QDir::SortFlags>(sort)));
481 
482  ZBL_SLOT_END_RETURN(info, NULL,
484 
485  }
486 
487  QStringList ZDir::entryList(int filters, int sort) const
488  {
489 
490  qDebug("ZDir::entryList -- filters = %x, sort = %x", filters, sort);
491 
492  QStringList info;
493 
494 
496  validateDir();
497 
498  info = m_dir->entryList(static_cast<QDir::Filter>(filters),
499  static_cast<QDir::SortFlags>(sort));
500 
501  ZBL_SLOT_END_RETURN(info, info,
503 
504  }
505 
506 } // Zbl
Q_INVOKABLE bool isAbsolutePath(const QString &path)
Definition: ZDir.cpp:388
Q_INVOKABLE bool rmpath(const QString &dirPath) const
Definition: ZDir.cpp:317
bool exists() const
Q_INVOKABLE QString relativeFilePath(const QString &fileName) const
Definition: ZDir.cpp:253
Q_INVOKABLE bool cd(const QString &dirName)
Definition: ZDir.cpp:261
QVariant getTags()
Definition: ZDir.cpp:128
Q_INVOKABLE QString cleanPath(const QString &path)
Definition: ZDir.cpp:456
Q_INVOKABLE bool makeAbsolute()
Definition: ZDir.cpp:411
uint getCount() const
Definition: ZDir.cpp:277
void createTags()
Constructs a QVariantMap representing constant values for ZDir objects and stores that in a QVariant ...
Definition: ZDir.cpp:84
QString getPath() const
Definition: ZDir.cpp:203
Q_INVOKABLE QString rootPath()
Definition: ZDir.cpp:441
Access and manipulate file directories. This class is a Javascript wrapper for QDir objects...
Definition: ZDir.h:42
Q_INVOKABLE bool setCurrent(const QString &path)
Definition: ZDir.cpp:420
Q_INVOKABLE QString absoluteFilePath(const QString &fileName) const
Definition: ZDir.cpp:245
#define Z_FAC_JS
Definition: zglobal.h:123
QString getCanonicalPath() const
Definition: ZDir.cpp:219
Q_INVOKABLE bool rename(const QString &oldName, const QString &newName)
Definition: ZDir.cpp:174
Q_INVOKABLE QString currentPath()
Definition: ZDir.cpp:427
QString getAbsolutePath() const
Definition: ZDir.cpp:211
Q_INVOKABLE QStringList entryList(int filters, int sort) const
Returns a QStringList describing the contents of the directory.
Definition: ZDir.cpp:487
Q_INVOKABLE QString homePath()
Definition: ZDir.cpp:434
Q_INVOKABLE QObject * entryInfoList(int filters, int sort) const
Returns a ZFileInfo object describing the contents of the directory.
Definition: ZDir.cpp:465
uint getFilter() const
Definition: ZDir.cpp:134
Q_INVOKABLE bool mkdir(const QString &dirName) const
Definition: ZDir.cpp:293
ZDir(QObject *parent=nullptr)
Constructs a ZDir object.
Definition: ZDir.cpp:42
Q_INVOKABLE void setPath(const QString &path)
Definition: ZDir.cpp:150
Q_INVOKABLE void refresh() const
Definition: ZDir.cpp:372
bool isReadable() const
Q_INVOKABLE QString getChildName(int pos)
Definition: ZDir.cpp:285
Definition: ZAndGate.cpp:6
void setInfoList(QFileInfoList infoList)
Definition: ZFileInfo.cpp:62
Q_INVOKABLE void setToHomePath()
Definition: ZDir.cpp:166
bool isRoot() const
void setFilter(uint filters)
Definition: ZDir.cpp:142
void validateDir() const
Definition: ZDir.h:343
#define ZBL_SLOT_BEGIN_TRY
Definition: zglobal.h:128
Q_INVOKABLE bool mkpath(const QString &dirPath) const
Definition: ZDir.cpp:309
Q_INVOKABLE bool isRelativePath(const QString &path)
Definition: ZDir.cpp:381
QString path
Definition: ZDir.h:132
#define ZBL_SLOT_END_VOID(facility, code, error_message)
Definition: zglobal.h:134
Q_INVOKABLE void setToCurrentPath()
Definition: ZDir.cpp:158
Q_INVOKABLE bool fileExists(const QString &name) const
Definition: ZDir.cpp:364
Q_INVOKABLE bool rmdir(const QString &dirName) const
Definition: ZDir.cpp:301
Q_INVOKABLE bool cdUp()
Definition: ZDir.cpp:269
Q_INVOKABLE bool isAbsolute() const
Definition: ZDir.cpp:403
Q_INVOKABLE bool removeRecursively()
Definition: ZDir.cpp:325
static void registerType()
Registers ZDir as a QML type.
Definition: ZDir.cpp:65
void initDir()
Definition: ZDir.h:337
A Javascript object for passing Zuble file information from C++ to Javascript. This is a wrapper clas...
Definition: ZFileInfo.h:48
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
Q_INVOKABLE bool isRelative() const
Definition: ZDir.cpp:395
QString getDirName() const
Definition: ZDir.cpp:228
Q_INVOKABLE QString filePath(const QString &fileName) const
Definition: ZDir.cpp:237
Q_INVOKABLE void swap(QObject *zDir)
Definition: ZDir.cpp:182
static QVariant m_tags
Definition: ZDir.h:330
virtual ~ZDir()
Definition: ZDir.cpp:55
Q_INVOKABLE bool remove(const QString &fileName)
Definition: ZDir.cpp:356
QString dirName
Definition: ZDir.h:135
Q_INVOKABLE QString tempPath()
Definition: ZDir.cpp:448
#define ZBL_SLOT_END_RETURN(return_success, return_failed, facility, code, error_message)
Definition: zglobal.h:141
QDir * m_dir
Definition: ZDir.h:328