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.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  * ZFile.cpp
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 
27 #include <QtQml>
28 #include <QFileInfo>
29 #include <QDir>
30 
31 #include "ZFile.h"
32 #include "ZblThreadErr.h"
33 #include "ZByteArray.h"
34 #include "ZTextStream.h"
35 #include "ZDataStream.h"
36 #include "ZApplication.h"
37 #include "zglobal.h"
38 
39 namespace Zbl
40 {
55 QVariant ZFile::m_tags;
56 
58 
59 ZFile::ZFile(QObject* parent) :
60  QObject(parent)
61 {
62  zDebug() << "ZFile::ZFile";
63 
64  createTags();
65 
66  initZioDevice();
67 }
68 
70 {
71  zDebug() << "::~ZFile";
72  close();
73 }
74 
75 
77 {
79 
80  qmlRegisterType<ZFile>("org.zuble.qml", 1, 0, "ZFile");
81 }
82 
84 {
85  m_zd = new ZioDevice(this);
86 
87  connect(m_zd, SIGNAL(getCurrentDevice(QIODevice**)),
88  SLOT(getQDevice(QIODevice**)), Qt::DirectConnection);
89 }
90 
92 {
93  if(!m_tags.isNull())
94  return;
95 
96  QVariantMap map;
97 
98  map.insert("permissionReadOwner", ReadOwner);
99  map.insert("permissionWriteOwner", WriteOwner);
100  map.insert("permissionExeOwner", ExeOwner);
101  map.insert("permissionReadUser", ReadUser);
102  map.insert("permissionWriteUser", WriteUser);
103  map.insert("permissionExeUser", ExeUser);
104  map.insert("permissionReadGroup", ReadGroup);
105  map.insert("permissionWriteGroup", WriteGroup);
106  map.insert("permissionExeGroup", ExeGroup);
107  map.insert("permissionReadOther", ReadOther);
108  map.insert("permissionWriteOther", WriteOther);
109  map.insert("permissionExeOther", ExeOther);
110 
111  m_tags = QVariant::fromValue(map);
112 }
113 
114 QVariant ZFile::getTags()
115 {
116  return m_tags;
117 }
118 
119 
121 {
124  Z_FAC_JS, ZFile::getStatus, getStatus failed)
125 }
126 
127 void ZFile::getQDevice(QIODevice** device)
128 {
129  *device = qobject_cast<QIODevice*>(m_f.data());
130 }
131 
133 {
135  m_f.clear();
137 }
138 
140 {
142 
143  //validateFile();
144 
145  if(m_f)
146  m_f->close();
147 
149 }
150 
151 QString ZFile::getFileName() const
152 {
153  QString fileName;
154 
156  if(m_f)
157  fileName = m_f->fileName();
158  ZBL_SLOT_END_RETURN(fileName, fileName,
160 }
161 
162 // note: setFileName will destroy and re-create the QFile
163 
164 void ZFile::setFileName(const QString& fileName)
165 {
167 
168  close();
169  initFile(ZApplication::resolvePath(fileName, false));
171 }
172 
173 bool ZFile::open(int openMode)
174 {
175  bool retValue;
177  validateFile();
178  retValue = m_f->open(QFile::OpenMode(openMode));
179  ZBL_SLOT_END_RETURN(retValue, false,
180  Z_FAC_JS, ZFile::open, open failed)
181 }
182 
183 bool ZFile::copy(const QString& newName)
184 {
185  bool retValue;
187  validateFile();
188  retValue = m_f->copy(newName);
189  ZBL_SLOT_END_RETURN(retValue, false,
190  Z_FAC_JS, ZFile::copy, copy failed)
191 }
192 
193 bool ZFile::rename(const QString& newName)
194 {
195  bool retValue;
197  validateFile();
198  retValue = m_f->rename(newName);
199  ZBL_SLOT_END_RETURN(retValue, false,
200  Z_FAC_JS, ZFile::rename, rename failed)
201 }
202 
204 {
205  bool retValue;
207  validateFile();
208  retValue = m_f->remove();
209  ZBL_SLOT_END_RETURN(retValue, false,
210  Z_FAC_JS, ZFile::remove, remove failed)
211 }
212 
214 {
215  bool retValue;
217  validateFile();
218  retValue = m_f->exists();
219  ZBL_SLOT_END_RETURN(retValue, false,
220  Z_FAC_JS, ZFile::exists, exists failed)
221 }
222 
223 bool ZFile::exists(const QString& fileName)
224 {
225  bool retValue;
227  retValue = QFile::exists(fileName);
228  ZBL_SLOT_END_RETURN(retValue, false,
229  Z_FAC_JS, ZFile::exists, exists failed)
230 }
231 
232 QString ZFile::getAbsoluteFilePath(const QString& filePath)
233 {
234  QString retValue;
236  QFileInfo fi(QDir::cleanPath(filePath));
237  retValue = fi.absoluteFilePath();
238  ZBL_SLOT_END_RETURN(retValue, QString(),
240 }
241 
242 QString ZFile::getCanonicalFilePath(const QString& filePath)
243 {
244  QString retValue;
246  QFileInfo fi(QDir::cleanPath(filePath));
247  retValue = fi.canonicalFilePath();
248  ZBL_SLOT_END_RETURN(retValue, QString(),
250 }
251 
252 QObject* ZFile::zReadDataLine(int maxSize)
253 {
254  zDebug() << "::zReadDataLine";
257  QByteArray ba = m_f->readLine(maxSize);
259  ZBL_SLOT_END_RETURN(new ZByteArray(ba), NULL,
261 }
262 
263 QObject* ZFile::zReadData(int maxSize)
264 {
265  zDebug() << "::zReadData";
268  QByteArray ba = m_f->read(maxSize);
270  ZBL_SLOT_END_RETURN(new ZByteArray(ba), NULL,
272 }
273 
275 {
276  zDebug() << "::zReadDataAll";
279  QByteArray ba = m_f->readAll();
281  ZBL_SLOT_END_RETURN(new ZByteArray(ba), NULL,
283 }
284 
285 QList<int> ZFile::jReadDataLine(int maxSize)
286 {
287  zDebug() << "::jReadDataLine";
289  ZBL_SLOT_END_RETURN(jGetData(maxSize, true), QList<int>(),
291 }
292 
293 QList<int> ZFile::jReadData(int maxSize)
294 {
295  zDebug() << "::jReadData";
297 
298  ZBL_SLOT_END_RETURN(jGetData(maxSize, false), QList<int>(),
300 }
301 
303 {
304  zDebug() << "::jReadDataAll";
305 
306  QList<int> array;
307 
310 
311  const int maxSize = 1024 * 16;
312 
313  while(true)
314  {
315  int previousSize = array.size();
316  array.append(jGetData(maxSize,false));
317  if((array.size() - previousSize < maxSize))
318  {
319  if(m_f->error() != QFileDevice::NoError)
320  {
321  if(m_f->error() != QFileDevice::NoError)
322  {
323  QString msg("File error attempting to read data: %1");
324  throw ZblException(msg.arg(m_f->errorString()));
325  }
326  }
327  break;
328  }
329  }
330  ZBL_SLOT_END_RETURN(array, array,
332 }
333 
334 QList<int> ZFile::jGetData(int maxSize, bool lineModeOn)
335 {
337 
338  qint64 sizeReturned = 0;
339  QList<int> array;
340  char* data = new char[maxSize];
341 
342  try
343  {
344  //ASSERT(m_f);
345 
346  if(lineModeOn)
347  sizeReturned = m_f->readLine(data, maxSize);
348  else
349  sizeReturned = m_f->read(data, maxSize);
350 
351  if(sizeReturned == -1 )
352  {
353  QString msg("File error attempting to read data: %1");
354  throw ZblException(msg.arg(m_f->errorString()));
355  }
356  else if(sizeReturned < maxSize)
357  {
358  if(m_f->error() != QFileDevice::NoError)
359  {
360  QString msg("File error attempting to read data: %1");
361  throw ZblException(msg.arg(m_f->errorString()));
362  }
363  }
364  for(int i = 0; i < sizeReturned; i++)
365  array.append(static_cast<int>(data[i]));
366  }
367  catch(...)
368  {
369  if(data)
370  delete data;
371 
372  throw;
373  }
374 
375  return array;
376 }
377 
378 bool ZFile::zWriteData(const ZByteArray& data)
379 {
380  zDebug() << "::zWriteData";
383  qint64 sizeWritten = m_f->write(*data.m_ba);
384 
385  if((sizeWritten == -1)
386  || ((sizeWritten < data.m_ba->size())
387  && (m_f->error() != QFileDevice::NoError)))
388  {
389  QString msg("File error attempting to write data: %1");
390  throw ZblException(msg.arg(m_f->errorString()));
391  }
392  ZBL_SLOT_END_RETURN(true, false,
394 }
395 
396 bool ZFile::jWriteData(const QList<int> data)
397 {
398  zDebug() << "::jWriteData";
401  const int size = data.size();
402  for(int i = 0; i < size; i++)
403  {
404  if(!m_f->putChar(static_cast<char>(data.at(i))))
405  {
406  QString msg("File error attempting to write data: %1");
407  throw ZblException(msg.arg(m_f->errorString()));
408  }
409  }
410  ZBL_SLOT_END_RETURN(true, false,
412 }
413 
415 {
416 zDebug() << "::zTextStream";
418 ZBL_SLOT_END_RETURN(new ZTextStream(m_f.dynamicCast<QIODevice>()),
420 }
421 
423 {
424 zDebug() << "::zDataStream";
426 ZBL_SLOT_END_RETURN(new ZDataStream(m_f.dynamicCast<QIODevice>()),
428 }
429 
430 
431 
432 } // Zbl
433 
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
Q_INVOKABLE QString getCanonicalFilePath(const QString &filePath)
Returns the canonical file path for the given file path.
Definition: ZFile.cpp:242
#define Z_FAC_JS
Definition: zglobal.h:123
Q_INVOKABLE void close()
Closes the device and sets its OpenMode to NotOpen. The error string is also reset.
Definition: ZFile.cpp:139
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
#define ZBL_REGISTER_LOGGED_OBJECT
Definition: zglobal.h:104
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
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
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
#define ZBL_SLOT_BEGIN_TRY
Definition: zglobal.h:128
#define ZBL_DEFINE_LOGGED_OBJECT(class_name)
Definition: zglobal.h:99
void createTags()
Create the m_tag object that presents a Javascript interface to QFile enumeration values...
Definition: ZFile.cpp:91
#define ZBL_SLOT_END_VOID(facility, code, error_message)
Definition: zglobal.h:134
#define zDebug()
Definition: zglobal.h:113
static QString resolvePath(const QString &path, bool includeUrlScheme=true)
Converts relative file paths into canonical file paths. Paths prefixed with prefix are mapped relativ...
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
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
#define ZBL_SLOT_END_RETURN(return_success, return_failed, facility, code, error_message)
Definition: zglobal.h:141
ZqByteArrayPtr m_ba
Definition: ZByteArray.h:103
QObject * getJsDevice()
Definition: ZFile.cpp:120