source: trunk/src/corelib/io/qprocess.h@ 604

Last change on this file since 604 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 7.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QPROCESS_H
43#define QPROCESS_H
44
45#include <QtCore/qiodevice.h>
46#include <QtCore/qstringlist.h>
47#include <QtCore/qshareddata.h>
48
49QT_BEGIN_HEADER
50
51QT_BEGIN_NAMESPACE
52
53QT_MODULE(Core)
54
55#ifndef QT_NO_PROCESS
56
57#if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc)
58typedef qint64 Q_PID;
59#elif defined(Q_OS_OS2)
60typedef int Q_PID;
61#else
62QT_END_NAMESPACE
63typedef struct _PROCESS_INFORMATION *Q_PID;
64QT_BEGIN_NAMESPACE
65#endif
66
67class QProcessPrivate;
68class QProcessEnvironmentPrivate;
69
70class Q_CORE_EXPORT QProcessEnvironment
71{
72public:
73 QProcessEnvironment();
74 QProcessEnvironment(const QProcessEnvironment &other);
75 ~QProcessEnvironment();
76 QProcessEnvironment &operator=(const QProcessEnvironment &other);
77
78 bool operator==(const QProcessEnvironment &other) const;
79 inline bool operator!=(const QProcessEnvironment &other) const
80 { return !(*this == other); }
81
82 bool isEmpty() const;
83 void clear();
84
85 bool contains(const QString &name) const;
86 void insert(const QString &name, const QString &value);
87 void remove(const QString &name);
88 QString value(const QString &name, const QString &defaultValue = QString()) const;
89
90 QStringList toStringList() const;
91
92 static QProcessEnvironment systemEnvironment();
93
94private:
95 friend class QProcessPrivate;
96 friend class QProcessEnvironmentPrivate;
97 QSharedDataPointer<QProcessEnvironmentPrivate> d;
98};
99
100class Q_CORE_EXPORT QProcess : public QIODevice
101{
102 Q_OBJECT
103public:
104 enum ProcessError {
105 FailedToStart, //### file not found, resource error
106 Crashed,
107 Timedout,
108 ReadError,
109 WriteError,
110 UnknownError
111 };
112 enum ProcessState {
113 NotRunning,
114 Starting,
115 Running
116 };
117 enum ProcessChannel {
118 StandardOutput,
119 StandardError
120 };
121 enum ProcessChannelMode {
122 SeparateChannels,
123 MergedChannels,
124 ForwardedChannels
125 };
126 enum ExitStatus {
127 NormalExit,
128 CrashExit
129 };
130
131 explicit QProcess(QObject *parent = 0);
132 virtual ~QProcess();
133
134 void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite);
135 void start(const QString &program, OpenMode mode = ReadWrite);
136
137 ProcessChannelMode readChannelMode() const;
138 void setReadChannelMode(ProcessChannelMode mode);
139 ProcessChannelMode processChannelMode() const;
140 void setProcessChannelMode(ProcessChannelMode mode);
141
142 ProcessChannel readChannel() const;
143 void setReadChannel(ProcessChannel channel);
144
145 void closeReadChannel(ProcessChannel channel);
146 void closeWriteChannel();
147
148 void setStandardInputFile(const QString &fileName);
149 void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
150 void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
151 void setStandardOutputProcess(QProcess *destination);
152
153 QString workingDirectory() const;
154 void setWorkingDirectory(const QString &dir);
155
156 void setEnvironment(const QStringList &environment);
157 QStringList environment() const;
158 void setProcessEnvironment(const QProcessEnvironment &environment);
159 QProcessEnvironment processEnvironment() const;
160
161 QProcess::ProcessError error() const;
162 QProcess::ProcessState state() const;
163
164 // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
165 Q_PID pid() const;
166
167 bool waitForStarted(int msecs = 30000);
168 bool waitForReadyRead(int msecs = 30000);
169 bool waitForBytesWritten(int msecs = 30000);
170 bool waitForFinished(int msecs = 30000);
171
172 QByteArray readAllStandardOutput();
173 QByteArray readAllStandardError();
174
175 int exitCode() const;
176 QProcess::ExitStatus exitStatus() const;
177
178 // QIODevice
179 qint64 bytesAvailable() const;
180 qint64 bytesToWrite() const;
181 bool isSequential() const;
182 bool canReadLine() const;
183 void close();
184 bool atEnd() const;
185
186 static int execute(const QString &program, const QStringList &arguments);
187 static int execute(const QString &program);
188
189 static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory,
190 qint64 *pid = 0);
191 static bool startDetached(const QString &program, const QStringList &arguments);
192 static bool startDetached(const QString &program);
193
194 static QStringList systemEnvironment();
195
196public Q_SLOTS:
197 void terminate();
198 void kill();
199
200Q_SIGNALS:
201 void started();
202 void finished(int exitCode);
203 void finished(int exitCode, QProcess::ExitStatus exitStatus);
204 void error(QProcess::ProcessError error);
205 void stateChanged(QProcess::ProcessState state);
206
207 void readyReadStandardOutput();
208 void readyReadStandardError();
209
210protected:
211 void setProcessState(ProcessState state);
212
213 virtual void setupChildProcess();
214
215 // QIODevice
216 qint64 readData(char *data, qint64 maxlen);
217 qint64 writeData(const char *data, qint64 len);
218
219private:
220 Q_DECLARE_PRIVATE(QProcess)
221 Q_DISABLE_COPY(QProcess)
222
223 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
224 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
225 Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
226 Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
227 Q_PRIVATE_SLOT(d_func(), bool _q_processDied())
228 Q_PRIVATE_SLOT(d_func(), void _q_notified())
229 friend class QProcessManager;
230};
231
232#endif // QT_NO_PROCESS
233
234QT_END_NAMESPACE
235
236QT_END_HEADER
237
238#endif // QPROCESS_H
Note: See TracBrowser for help on using the repository browser.