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
ZFile.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  * ZFile.h
6  *
7  * Project: bitumen
8  * Created: Feb 23, 2012
9  * Author: Bob Dinitto
10  *
11  * Zuble is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #ifndef ZFILE_H_
27 #define ZFILE_H_
28 
29 #include "zglobal.h"
30 #include "ZioDevice.h"
31 
32 #include <QObject>
33 #include <QFile>
34 #include <QSharedPointer>
35 
36 
37 #include <QTextStream>
38 #include <QDataStream>
39 
40 
41 namespace Zbl
42 {
43 class ZByteArray;
44 class ZTextStream;
45 class ZDataStream;
46 
53 class ZFile: public QObject
54 {
55  Q_OBJECT
56  Q_ENUMS(Permission)
57 public:
58 
59  // to participate in automatic enum propagation
60  // for QML programs we redeclare these QFileDevice enums;
61  // note background Javascript programs can't use QML
62  // type engine, those programs must use tags property instead
63 
64  enum Permission {
65  ReadOwner = QFileDevice::ReadOwner,
66  WriteOwner = QFileDevice::WriteOwner,
67  ExeOwner = QFileDevice::ExeOwner,
68  ReadUser = QFileDevice::ReadUser,
69  WriteUser = QFileDevice::WriteUser,
70  ExeUser = QFileDevice::ExeUser,
71  ReadGroup = QFileDevice::ReadGroup,
72  WriteGroup = QFileDevice::WriteGroup,
73  ExeGroup = QFileDevice::ExeGroup,
74  ReadOther = QFileDevice::ReadOther,
75  WriteOther = QFileDevice::WriteOther,
76  ExeOther = QFileDevice::ExeOther
77  };
78  Q_DECLARE_FLAGS(Permissions, Permission)
79 
80 
81  explicit ZFile(QObject* parent = 0);
82  virtual ~ZFile();
83 
84 
88  static void registerType();
89 
100  Q_PROPERTY(QString fileName READ getFileName WRITE setFileName)
101 
102 
107  Q_PROPERTY(QObject* device READ getJsDevice CONSTANT)
108 
113  Q_PROPERTY(QVariant tags READ getTags NOTIFY tagsChanged)
114 
115 
116 
117  QString getFileName() const;
118  void setFileName(const QString& fileName);
119  QObject* getJsDevice();
120  QVariant getTags();
121 
122 
123 
124 
135  Q_INVOKABLE void release();
136 
141  Q_INVOKABLE void close();
142 
147  Q_INVOKABLE bool open(int openMode);
148 
153  Q_INVOKABLE bool copy(const QString& newName);
154 
159  Q_INVOKABLE bool rename(const QString& newName);
160 
165  Q_INVOKABLE bool remove();
166 
171  Q_INVOKABLE bool exists();
172 
180  Q_INVOKABLE bool exists(const QString& fileName);
181 
188  Q_INVOKABLE QString getAbsoluteFilePath(const QString& filePath);
189 
196  Q_INVOKABLE QString getCanonicalFilePath(const QString& filePath);
197 
203  Q_INVOKABLE QObject* zReadData(int maxSize);
204 
210  Q_INVOKABLE QObject* zReadDataLine(int maxSize);
211 
216  Q_INVOKABLE QObject* zReadDataAll();
217 
223  Q_INVOKABLE QList<int> jReadData(int maxSize);
224 
230  Q_INVOKABLE QList<int> jReadDataLine(int maxSize);
231 
236  Q_INVOKABLE QList<int> jReadDataAll();
237 
242  Q_INVOKABLE bool zWriteData(const ZByteArray& data); // tbd: this should be QObject*?
243 
252  Q_INVOKABLE bool jWriteData(const QList<int> data);
253 
258  Q_INVOKABLE QObject* zTextStream();
259 
264  Q_INVOKABLE QObject* zDataStream();
265 
266 
267 public slots:
268 
269  void getQDevice(QIODevice** device); // for ZioDevice::getCurrentDevice
270 
271 
272 signals:
273 
274  void tagsChanged();
275 
276 
277 
278 protected:
279 
281 
286  void initZioDevice();
287 
292  void createTags();
293 
294 
306  QList<int> jGetData(int maxSize, bool lineModeOn = false);
307 
308 
314  inline void initFile(const QString& fileName);
315 
320  inline void validateFile() const;
321 
326  inline void validateReadable() const;
327 
332  inline void validateWritable() const;
333 
341  inline void validateReadDataSuccess(const QByteArray& ba) const;
342 
343 
348 
353 
359  static QVariant m_tags;
360 };
361 
362 
363 
364 inline void ZFile::initFile(const QString& fileName)
365 {
366  close();
367 
368  m_f = ZqFilePtr::create();
369 
370  m_f->setFileName(fileName);
371 }
372 
373 inline void ZFile::validateFile() const
374 {
375  if(!m_f)
376  throw ZblException("File object not initialized. "
377  "Use ZFile::fileName property to set the file name.");
378 }
379 
380 
381 
382 
383 inline void ZFile::validateReadable() const
384 {
385  validateFile();
386 
387  if(!m_f->isReadable())
388  throw ZblException(
389  "Error, file is not readable.");
390 }
391 
392 inline void ZFile::validateWritable() const
393 {
394  validateFile();
395 
396  if(!m_f->isWritable())
397  throw ZblException(
398  "Error, file is not writable.");
399 }
400 
401 inline void ZFile::validateReadDataSuccess(const QByteArray& ba) const
402 {
403  if(ba.isEmpty())
404  {
405  validateFile();
406 
407  if(m_f->error() != QFileDevice::NoError)
408  {
409  QString msg("File error attempting to read data: %1");
410  msg = msg.arg(m_f->errorString());
411  throw ZblException(msg);
412  }
413  }
414 }
415 
416 
417 } // Zbl
418 
419 #endif /* ZFILE_H_ */
void tagsChanged()
This class supports streaming of text and binary data.
Definition: ZByteArray.h:41
void getQDevice(QIODevice **device)
Definition: ZFile.cpp:127
Q_INVOKABLE bool zWriteData(const ZByteArray &data)
Write the contents of a ZByteArray to the device.
Definition: ZFile.cpp:378
void validateReadable() const
Ensures the embedded QFile object is readable by throwing an exception if not.
Definition: ZFile.h:383
Q_INVOKABLE QObject * zTextStream()
Returns a Zbl::ZTextStream object that can be used to stream text to or from the device.
Definition: ZFile.cpp:414
Q_INVOKABLE QObject * zReadDataLine(int maxSize)
Reads up to the next new line or up to maxSize bytes or up to end of data, whichever comes first...
Definition: ZFile.cpp:252
Q_INVOKABLE bool open(int openMode)
Opens the device and sets its open mode to openMode. Returns true if successful; otherwise returns fa...
Definition: ZFile.cpp:173
Q_INVOKABLE bool jWriteData(const QList< int > data)
Write the contents of a Javascript array of numbers to the device as bytes.
Definition: ZFile.cpp:396
Q_INVOKABLE QObject * zReadDataAll()
Reads up to the end of data into a ZByteArray.
Definition: ZFile.cpp:274
#define ZBL_DECLARE_LOGGED_OBJECT
Definition: zglobal.h:94
Q_INVOKABLE QString getCanonicalFilePath(const QString &filePath)
Returns the canonical file path for the given file path.
Definition: ZFile.cpp:242
Q_INVOKABLE void close()
Closes the device and sets its OpenMode to NotOpen. The error string is also reset.
Definition: ZFile.cpp:139
ZFile(QObject *parent=0)
Definition: ZFile.cpp:59
Q_INVOKABLE QList< int > jReadData(int maxSize)
Reads up to maxSize bytes or up to end of data, whichever comes first, into a Javascript array...
Definition: ZFile.cpp:293
QString fileName
The name of the file.
Definition: ZFile.h:100
static void registerType()
Registers ZFile as a QML type.
Definition: ZFile.cpp:76
QVariant getTags()
Definition: ZFile.cpp:114
Q_INVOKABLE bool remove()
Removes the file. Returns true if successful; otherwise returns false.
Definition: ZFile.cpp:203
QString getFileName() const
Definition: ZFile.cpp:151
static QVariant m_tags
A QVariantMap used to pass QFile enumeration values to Javascript programs.
Definition: ZFile.h:359
Q_INVOKABLE bool exists()
Determine if the file currently exists. Returns true if file exists; otherwise returns false...
Definition: ZFile.cpp:213
QObject device
A ZioDevice object that represents the File&#39;s QioDevice interface.
Definition: ZFile.h:107
virtual ~ZFile()
Definition: ZFile.cpp:69
Q_INVOKABLE void release()
Releases references from this object to wrapped Qt C++ objects.
Definition: ZFile.cpp:132
QSharedPointer< QFile > ZqFilePtr
Definition: zglobal.h:163
A javascript wrapper for QFile.
Definition: ZFile.h:53
Q_INVOKABLE bool copy(const QString &newName)
Copies this file to newName.
Definition: ZFile.cpp:183
Permission
Definition: ZFile.h:64
A javascript wrapper for QIODevice.
Definition: ZioDevice.h:40
void setFileName(const QString &fileName)
Definition: ZFile.cpp:164
Definition: ZAndGate.cpp:6
void initFile(const QString &fileName)
Closes the current file if opened, then creates a new embedded QFile object with the specified file n...
Definition: ZFile.h:364
void validateWritable() const
Ensures the embedded QFile object is writable by throwing an exception if not.
Definition: ZFile.h:392
Q_INVOKABLE QObject * zReadData(int maxSize)
Reads up to maxSize bytes or up to end of data, whichever comes first, into a ZByteArray.
Definition: ZFile.cpp:263
void createTags()
Create the m_tag object that presents a Javascript interface to QFile enumeration values...
Definition: ZFile.cpp:91
Q_INVOKABLE QString getAbsoluteFilePath(const QString &filePath)
Returns the absolute file path for the given file path.
Definition: ZFile.cpp:232
Q_INVOKABLE QList< int > jReadDataLine(int maxSize)
Reads up to the next new line or up to maxSize bytes or up to end of data, whichever comes first...
Definition: ZFile.cpp:285
ZBL_DECLARE_LOGGED_OBJECT void initZioDevice()
Construct and connect a ZioDevice object that presents a Javascript interface to this file&#39;s QIODevic...
Definition: ZFile.cpp:83
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
ZqFilePtr m_f
Smart pointer to the embedded QFile object.
Definition: ZFile.h:347
ZioDevice * m_zd
Pointer to QioDevice interface object.
Definition: ZFile.h:352
QVariant tags
A Javascript object containing QFile enumeration values.
Definition: ZFile.h:113
Q_INVOKABLE QList< int > jReadDataAll()
Reads up to the end of data into a Javascript array.
Definition: ZFile.cpp:302
void validateReadDataSuccess(const QByteArray &ba) const
Throws an exception if both the supplied QByteArray object is empty and an error condition exists on ...
Definition: ZFile.h:401
void validateFile() const
Ensures the embedded QFile object has been initialized by throwing an exception if not...
Definition: ZFile.h:373
Q_INVOKABLE bool rename(const QString &newName)
Renames this file to newName.
Definition: ZFile.cpp:193
QList< int > jGetData(int maxSize, bool lineModeOn=false)
A common method for line and no-line mode data fetch operations into a Javascript array...
Definition: ZFile.cpp:334
Q_INVOKABLE QObject * zDataStream()
Returns a Zbl::ZDataStream object that can be used to stream binary data to or from the device...
Definition: ZFile.cpp:422
QObject * getJsDevice()
Definition: ZFile.cpp:120