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
ZProcess.cpp
Go to the documentation of this file.
1 #include "ZProcess.h"
2 #include "ZByteArray.h"
3 #include <QtQml>
4 
5 
6 namespace Zbl
7 {
8 
9 QVariant ZProcess::m_tags;
10 
12 
13 ZProcess::ZProcess(QObject *parent) : QObject(parent)
14 {
15  zDebug() << "ZFile::ZFile";
16 
17  createTags();
18 
19  m_p = new QProcess(this);
20 
21  initZioDevice();
22 
23  m_p->setProcessEnvironment(QProcessEnvironment::systemEnvironment());
24 
25  connect(m_p, &QProcess::started, this, &ZProcess::started);
26  //connect(m_p, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SIGNAL(errorOccurred(QProcess::ProcessError)));
27 
28  //connect(m_p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SIGNAL(finished(int,QProcess::ExitStatus)));
29  //connect(m_p, SIGNAL(stateChanged(QProcess::ProcessState state)), this, SIGNAL(stateChanged(QProcess::ProcessState state)));
30  connect(m_p, SIGNAL(readyReadStandardOutput()), this, SIGNAL(readyReadStandardOutput()));
31  connect(m_p, SIGNAL(readyReadStandardError()), this, SIGNAL(readyReadStandardError()));
32 }
33 
35 {
36  zDebug() << "ZProcess::~ZProcess";
37  //close();
38 }
39 
41 {
43 
44  qmlRegisterType<ZProcess>("org.zuble.qml", 1, 0, "ZProcess");
45 }
46 
48 {
49  m_zd = new ZioDevice(this);
50 
51  connect(m_zd, SIGNAL(getCurrentDevice(QIODevice**)),
52  SLOT(getQDevice(QIODevice**)), Qt::DirectConnection);
53 }
54 
56 {
57  if(!m_tags.isNull())
58  return;
59 
60  QVariantMap map;
61 
62  map.insert("errorFailedToStart", FailedToStart);
63  map.insert("errorCrashed", Crashed);
64  map.insert("errorTimedout", Timedout);
65  map.insert("errorReadError", ReadError);
66  map.insert("errorWriteError", WriteError);
67  map.insert("errorUnknownError", UnknownError);
68  map.insert("stateNotRunning", NotRunning);
69  map.insert("stateStarting", Starting);
70  map.insert("stateRunning", Running);
71  map.insert("channelStandardOutput", StandardOutput);
72  map.insert("channelStandardError", StandardError);
73  map.insert("channelModeSeparateChannels", SeparateChannels);
74  map.insert("channelModeMergedChannels", MergedChannels);
75  map.insert("channelModeForwardedChannels", ForwardedChannels);
76  map.insert("channelModeForwardedOutputChannel", ForwardedOutputChannel);
77  map.insert("channelModeForwardedErrorChannel", ForwardedErrorChannel);
78  map.insert("exitNormalExit", NormalExit);
79  map.insert("exitCrashExit", CrashExit);
80 
81  m_tags = QVariant::fromValue(map);
82 }
83 
85 {
86  return m_tags;
87 }
88 
90 {
93  Z_FAC_JS, ZProcess::getStatus, getStatus failed)
94 }
95 
96 void ZProcess::getQDevice(QIODevice** device)
97 {
98  *device = qobject_cast<QIODevice*>(m_p);
99 }
100 
101 #if 0
102 void ZProcess::release()
103 {
105  m_p.clear();
106  ZBL_SLOT_END_VOID(Z_FAC_JS, ZProcess::release, release failed)
107 }
108 #endif
109 
110 #if 0
111 void ZProcess::close()
112 {
114 
115  //validateFile();
116 
117  if(m_p)
118  m_p->close();
119 
120  ZBL_SLOT_END_VOID(Z_FAC_JS, ZProcess::close, close failed)
121 }
122 #endif
123 
124 void ZProcess::start(int mode)
125 {
126  m_p->start(static_cast<QIODevice::OpenMode>(mode));
127 }
128 
130 {
131  return m_p->arguments();
132 }
133 
134 void ZProcess::setArguments(const QStringList &arguments)
135 {
136  m_p->setArguments(arguments);
137 }
138 
139 QString ZProcess::getProgram() const
140 {
141  return m_p->program();
142 }
143 
144 void ZProcess::setProgram(const QString &program)
145 {
146  m_p->setProgram(program);
147 }
148 
150 {
151  m_p->closeWriteChannel();
152 }
153 
154 void ZProcess::closeReadChannel(int channel)
155 {
156  m_p->closeReadChannel(static_cast<QProcess::ProcessChannel>(channel));
157 }
158 
160 {
161  return m_p->processId();
162 }
163 
165 {
166  return m_p->readChannel();
167 }
168 
169 void ZProcess::setReadChannel(int channel)
170 {
171  m_p->setReadChannel(static_cast<QProcess::ProcessChannel>(channel));
172 }
173 
175 {
176  QByteArray data = m_p->readAllStandardError();
177 
178  return new ZByteArray(data);
179 }
180 
182 {
183  QByteArray data = m_p->readAllStandardOutput();
184 
185  return new ZByteArray(data);
186 }
187 
189 {
190  return static_cast<int>(m_p->state());
191 }
192 
194 {
195  return static_cast<int>(m_p->error());
196 }
197 
199 {
200  m_p->setProcessChannelMode(static_cast<QProcess::ProcessChannelMode>(mode));
201 }
202 
204 {
205  return static_cast<int>(m_p->processChannelMode());
206 }
207 
209 {
210  return static_cast<int>(m_p->exitCode());
211 }
212 
214 {
215  return static_cast<int>(m_p->exitStatus());
216 }
217 
219 {
220  m_p->setProcessEnvironment(QProcessEnvironment());
221 }
222 
223 QStringList ZProcess::envGetList()
224 {
225  return m_p->processEnvironment().toStringList();
226 }
227 
228 void ZProcess::envSetValue(const QString& name, const QString& value)
229 {
230  QProcessEnvironment env(m_p->processEnvironment());
231  env.insert(name, value);
232  m_p->setProcessEnvironment(env);
233 }
234 
235 QString ZProcess::envGetValue(const QString& name)
236 {
237  return m_p->processEnvironment().value(name);
238 }
239 
240 bool ZProcess::envValueExists(const QString& name)
241 {
242  return m_p->processEnvironment().contains(name);
243 }
244 
245 
246 } // Zbl
virtual ~ZProcess()
Definition: ZProcess.cpp:34
void getQDevice(QIODevice **device)
Obtains the QIODevice pointer for this object.
Definition: ZProcess.cpp:96
Q_INVOKABLE void envClear()
Clears the process execution environment that will be passed to the child process.
Definition: ZProcess.cpp:218
int exitCode() const
Obtains the exit code of the last process that finished.
Definition: ZProcess.cpp:208
This class supports streaming of text and binary data.
Definition: ZByteArray.h:41
QVariant getTags()
Definition: ZProcess.cpp:84
QML/Javascript wrapper for the QProcess class, allows QML programs to spawn and communicate with a ba...
Definition: ZProcess.h:20
void start(int mode=static_cast< int >(ZioDevice::ReadWrite))
Starts the process.
Definition: ZProcess.cpp:124
ZBL_DECLARE_LOGGED_OBJECT void initZioDevice()
Construct and connect a ZioDevice object that presents a Javascript interface to this process&#39;s QIODe...
Definition: ZProcess.cpp:47
QObject * getJsDevice()
Definition: ZProcess.cpp:89
#define Z_FAC_JS
Definition: zglobal.h:123
Q_INVOKABLE void envSetValue(const QString &name, const QString &value)
Sets an environment variable that will be passed to the child process.
Definition: ZProcess.cpp:228
Q_INVOKABLE void closeReadChannel(int channel)
Closes the specified read channel of the process.
Definition: ZProcess.cpp:154
int getReadChannel() const
Obtain the current read channel.
Definition: ZProcess.cpp:164
ZioDevice * m_zd
Pointer to QioDevice interface object.
Definition: ZProcess.h:416
#define ZBL_REGISTER_LOGGED_OBJECT
Definition: zglobal.h:104
void closeWriteChannel()
Schedules the write channel of the process to close once all input data has been read by the process...
Definition: ZProcess.cpp:149
Q_INVOKABLE QString envGetValue(const QString &name)
Returns the value of an environment variable that will be passed to the child process.
Definition: ZProcess.cpp:235
void setArguments(const QStringList &arguments)
Sets the arguments that will be passed to the process when it starts.
Definition: ZProcess.cpp:134
void createTags()
Create the m_tag object that presents a Javascript interface for QProcess enumeration values...
Definition: ZProcess.cpp:55
Q_INVOKABLE QStringList envGetList()
Obtains the list of process environment variables that will be passed to the child process...
Definition: ZProcess.cpp:223
QString program
Set/get the program for the process.
Definition: ZProcess.h:99
qint64 getProcessID() const
Obtain the process ID. Note that Linux will reuse process IDs. They are ephemeral, not unique.
Definition: ZProcess.cpp:159
Q_INVOKABLE bool envValueExists(const QString &name)
Determine if an environment variable has been set to be passed to the child process.
Definition: ZProcess.cpp:240
void setReadChannel(int channel)
Determine which read channel is currently active.
Definition: ZProcess.cpp:169
static void registerType()
Registers ZFile as a QML type.
Definition: ZProcess.cpp:40
int exitStatus() const
Obtains the exit status of the last process that finished.
Definition: ZProcess.cpp:213
A javascript wrapper for QIODevice.
Definition: ZioDevice.h:40
Definition: ZAndGate.cpp:6
void setProcessChannelMode(int mode)
Sets the process channel mode that will be used the next time the process starts. ...
Definition: ZProcess.cpp:198
int getError() const
Obtains the current error state.
Definition: ZProcess.cpp:193
#define ZBL_SLOT_BEGIN_TRY
Definition: zglobal.h:128
static QVariant m_tags
A QVariantMap used to pass QFile enumeration values to Javascript programs.
Definition: ZProcess.h:423
#define ZBL_DEFINE_LOGGED_OBJECT(class_name)
Definition: zglobal.h:99
void setProgram(const QString &program)
Sets the path to the program the process will be started with.
Definition: ZProcess.cpp:144
Q_INVOKABLE QObject * readAllStandardOutput()
Obtains all available data from the process&#39;s stdout channel.
Definition: ZProcess.cpp:181
QStringList arguments
Set/get the argument list for the process.
Definition: ZProcess.h:93
#define ZBL_SLOT_END_VOID(facility, code, error_message)
Definition: zglobal.h:134
int getProcessChannelMode() const
Returns the channel mode of the QProcess standard output and standard error channels.
Definition: ZProcess.cpp:203
Q_INVOKABLE QObject * readAllStandardError()
Obtains all available data from the process&#39;s stderr channel.
Definition: ZProcess.cpp:174
#define zDebug()
Definition: zglobal.h:113
QStringList getArguments()
Returns the arguments that will be passed to the process when it starts.
Definition: ZProcess.cpp:129
QObject device
A ZioDevice object that represents the File&#39;s QioDevice interface.
Definition: ZProcess.h:81
int getState() const
Obtains the current process state.
Definition: ZProcess.cpp:188
QProcess * m_p
Smart pointer to the embedded QFile object.
Definition: ZProcess.h:411
#define ZBL_SLOT_END_RETURN(return_success, return_failed, facility, code, error_message)
Definition: zglobal.h:141
QString getProgram() const
Returns the path to the program the process will be started with.
Definition: ZProcess.cpp:139