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
ZioDevice.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: ZioDevice.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 "ZioDevice.h"
26 #include <QtQml>
27 //#include <QVariant>
28 #include "zglobal.h"
29 #include "ZByteArray.h"
30 
31 namespace Zbl {
32 
33 QVariant ZioDevice::m_tags;
34 
35 
36 ZioDevice::ZioDevice(QObject *parent) :
37  QObject(parent)
38 {
39  qDebug("ZioDevice::ZioDevice");
40 
41  QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); // TBD: need this? parent has QmlEngine ownership.
42 
43  createTags();
44 }
45 
46 
47 
49 {
50  qmlRegisterUncreatableType<ZioDevice>
51  ("org.zuble.qml", 1, 0, "ZioDevice",
52  QString("ZioDevice is obtained from \"device\" property "
53  "of the I/O object you are reading or writing."));
54 }
55 
57 {
58  if(!m_tags.isNull())
59  return;
60 
61  QVariantMap map;
62 
63  map.insert("NotOpen", QIODevice::NotOpen);
64  map.insert("ReadOnly", QIODevice::ReadOnly);
65  map.insert("WriteOnly", QIODevice::WriteOnly);
66  map.insert("ReadWrite", QIODevice::ReadWrite);
67  map.insert("Append", QIODevice::Append);
68  map.insert("Truncate", QIODevice::Truncate);
69  map.insert("Text", QIODevice::Text);
70  map.insert("Unbuffered", QIODevice::Unbuffered);
71 
72  m_tags = QVariant::fromValue(map);
73 }
74 
75 
76 bool ZioDevice::isOpen() const
77 {
78  qDebug("ZioDevice::isOpen");
79  bool openFlag = false;
81  QIODevice* device = connectedDevice();
82  openFlag = device->isOpen();
83  ZBL_SLOT_END_RETURN(openFlag, openFlag,
85 }
86 
87 int ZioDevice::openMode() const
88 {
89  qDebug("ZioDevice::openMode");
91  QIODevice* device = connectedDevice();
92  ZBL_SLOT_END_RETURN(static_cast<uint>(device->openMode()),
94 }
95 
97 {
98  qDebug("ZioDevice::getTextMode");
100  QIODevice* device = connectedDevice();
101  ZBL_SLOT_END_RETURN(device->isTextModeEnabled(), false,
103 }
104 
105 void ZioDevice::setTextMode(bool textModeEnabled)
106 {
107  qDebug("ZioDevice::setTextMode");
109  QIODevice* device = connectedDevice();
110  device->setTextModeEnabled(textModeEnabled);
113 }
114 
115 qint64 ZioDevice::size() const
116 {
117  qDebug("ZioDevice::size");
119  QIODevice* device = connectedDevice();
120  ZBL_SLOT_END_RETURN(device->size(), -1,
121  Z_FAC_JS, ZioDevice::size, size failed)
122 }
123 
124 qint64 ZioDevice::pos() const
125 {
126  qDebug("ZioDevice::pos");
128  QIODevice* device = connectedDevice();
129  ZBL_SLOT_END_RETURN(device->pos(), -1,
130  Z_FAC_JS, ZioDevice::pos, pos failed)
131 }
132 
133 qint64 ZioDevice::bytesAvailable() const
134 {
135  qDebug("ZioDevice::bytesAvailable");
137  QIODevice* device = connectedDevice();
138  ZBL_SLOT_END_RETURN(device->bytesAvailable(), -1,
140 }
141 
142 qint64 ZioDevice::bytesToWrite() const
143 {
144  qDebug("ZioDevice::bytesToWrite");
146  QIODevice* device = connectedDevice();
147  ZBL_SLOT_END_RETURN(device->bytesToWrite(), -1,
149 }
150 
151 
152 
154 {
155  qDebug("ZioDevice::isSequential");
157  QIODevice* device = connectedDevice();
158  ZBL_SLOT_END_RETURN(device->isSequential(), false,
160 }
161 
162 
164 {
165  qDebug("ZioDevice::isWritable");
167  QIODevice* device = connectedDevice();
168  ZBL_SLOT_END_RETURN(device->isWritable(), false,
170 
171 }
172 
174 {
175  qDebug("ZioDevice::isSequential");
177  QIODevice* device = connectedDevice();
178  ZBL_SLOT_END_RETURN(device->isReadable(), false,
180 
181 }
182 
183 bool ZioDevice::atEnd() const
184 {
186  QIODevice* device = connectedDevice();
187  ZBL_SLOT_END_RETURN(device->atEnd(), true,
189 }
190 
191 
192 QIODevice* ZioDevice::connectedDevice() const
193 {
194  QIODevice* dev = nullptr;
195  emit getCurrentDevice(&dev);
196  if(!dev)
197  throw ZblException("Error: No connected Device.");
198  return dev;
199 }
200 
201 
202 
204 {
205  QIODevice* device = nullptr;
206 
207  emit getCurrentDevice( &device);
208 
209  if(device == nullptr)
210  return QString("Can't get error string: No connected Device.");
211 
212  return device->errorString();
213 }
214 
216 {
217  QIODevice* device = nullptr;
218 
219  emit getCurrentDevice( &device);
220 
221  if(device == nullptr)
222  qDebug("Error: No connected Device.");
223  else
224  device->reset();
225 }
226 
228 {
229  QIODevice* device = nullptr;
230 
231  emit getCurrentDevice( &device);
232 
233  if(device == nullptr)
234  qDebug("Error: No connected Device.");
235  else
236  device->close();
237 }
238 
239 bool ZioDevice::seek(qint64 pos)
240 {
242  QIODevice* device = connectedDevice();
243  ZBL_SLOT_END_RETURN(device->seek(pos), true,
244  Z_FAC_JS, ZioDevice::seek, seek failed)
245 }
246 
248 {
250  QIODevice* device = connectedDevice();
251  ZBL_SLOT_END_RETURN(device->waitForBytesWritten(msecs), true,
253 }
254 
256 {
258  QIODevice* device = connectedDevice();
259  ZBL_SLOT_END_RETURN(device->waitForReadyRead(msecs), true,
261 }
262 
263 qint64 ZioDevice::writeString(const QString& text)
264 {
266 
267  QIODevice* device = connectedDevice();
268 
269  qint64 sizeWritten = device->write(text.toUtf8().constData());
270 
271  if(sizeWritten == -1)
272  {
273  QString msg("File error attempting to write data: %1");
274  throw ZblException(msg.arg(getErrorString()));
275  }
276  ZBL_SLOT_END_RETURN(sizeWritten, -1,
278 }
279 
280 qint64 ZioDevice::writeBytes(const QObject* data)
281 {
283 
284  const ZByteArray* constBa = qobject_cast<const ZByteArray*>(data);
285  ZByteArray* ba = const_cast<ZByteArray*>(constBa);
286 
287  if(!ba)
288  throw ZblException("Programming error: ZioDevice::writeData() argument must be a ZByteArray");
289 
290  QIODevice* device = connectedDevice();
291 
292  qint64 sizeWritten = device->write(*ba->qByteArray());
293 
294  if((sizeWritten == -1)
295  || (sizeWritten < ba->getSize()))
296  {
297  QString msg("ZProcess error attempting to write data: %1");
298  throw ZblException(msg.arg(getErrorString()));
299  }
300  ZBL_SLOT_END_RETURN(sizeWritten, -1,
302 }
303 
304 
306 {
307  return m_tags;
308 }
309 
310 
311 
312 } // Zbl
This class supports streaming of text and binary data.
Definition: ZByteArray.h:41
QVariant getTags()
Definition: ZioDevice.cpp:305
QString getErrorString()
Definition: ZioDevice.cpp:203
static void registerType()
Registers ZioDevice as a QML type.
Definition: ZioDevice.cpp:48
qint64 size() const
qint64 bytesAvailable() const
ZioDevice(QObject *parent=0)
Definition: ZioDevice.cpp:36
Q_INVOKABLE qint64 writeString(const QString &text)
Write a string to the device.
Definition: ZioDevice.cpp:263
#define Z_FAC_JS
Definition: zglobal.h:123
QIODevice * connectedDevice() const
Emits the getCurrentDevice signal and returns the resulting device, or else throws an exception if a ...
Definition: ZioDevice.cpp:192
bool isOpen() const
bool isReadable() const
Definition: ZioDevice.cpp:173
Q_INVOKABLE bool seek(qint64 pos)
Definition: ZioDevice.cpp:239
bool atEnd() const
static QVariant m_tags
A QVariant containing a QVariantMap of QIODevice enumerations for use by background Javascript thread...
Definition: ZioDevice.h:233
Q_INVOKABLE qint64 writeBytes(const QObject *data)
Write the contents of a ZByteArray to the device.
Definition: ZioDevice.cpp:280
Q_INVOKABLE bool waitForReadyRead(int msecs)
Definition: ZioDevice.cpp:255
Definition: ZAndGate.cpp:6
#define ZBL_SLOT_BEGIN_TRY
Definition: zglobal.h:128
bool isWritable() const
Definition: ZioDevice.cpp:163
Q_INVOKABLE void close()
Definition: ZioDevice.cpp:227
#define ZBL_SLOT_END_VOID(facility, code, error_message)
Definition: zglobal.h:134
Q_INVOKABLE void reset()
Definition: ZioDevice.cpp:215
Q_INVOKABLE bool waitForBytesWritten(int msecs)
Definition: ZioDevice.cpp:247
int openMode() const
void getCurrentDevice(QIODevice **device) const
Connected devices should listen to this signal and respond by returning a pointer to the QIODevice ob...
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
qint64 pos() const
void createTags()
Initializes QIODevice enumeration tags.
Definition: ZioDevice.cpp:56
bool getTextMode() const
Definition: ZioDevice.cpp:96
bool isSequential()
#define ZBL_SLOT_END_RETURN(return_success, return_failed, facility, code, error_message)
Definition: zglobal.h:141
void setTextMode(bool textModeEnabled)
Definition: ZioDevice.cpp:105
qint64 bytesToWrite() const