qprocess.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QPROCESS_H
00043 #define QPROCESS_H
00044 
00045 #include <QtCore/qiodevice.h>
00046 #include <QtCore/qstringlist.h>
00047 #include <QtCore/qshareddata.h>
00048 
00049 QT_BEGIN_HEADER
00050 
00051 QT_BEGIN_NAMESPACE
00052 
00053 QT_MODULE(Core)
00054 
00055 #ifndef QT_NO_PROCESS
00056 
00057 #if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc)
00058 typedef qint64 Q_PID;
00059 #else
00060 QT_END_NAMESPACE
00061 typedef struct _PROCESS_INFORMATION *Q_PID;
00062 QT_BEGIN_NAMESPACE
00063 #endif
00064 
00065 class QProcessPrivate;
00066 class QProcessEnvironmentPrivate;
00067 
00068 class Q_CORE_EXPORT QProcessEnvironment
00069 {
00070 public:
00071     QProcessEnvironment();
00072     QProcessEnvironment(const QProcessEnvironment &other);
00073     ~QProcessEnvironment();
00074     QProcessEnvironment &operator=(const QProcessEnvironment &other);
00075 
00076     bool operator==(const QProcessEnvironment &other) const;
00077     inline bool operator!=(const QProcessEnvironment &other) const
00078     { return !(*this == other); }
00079 
00080     bool isEmpty() const;
00081     void clear();
00082 
00083     bool contains(const QString &name) const;
00084     void insert(const QString &name, const QString &value);
00085     void remove(const QString &name);
00086     QString value(const QString &name, const QString &defaultValue = QString()) const;
00087 
00088     QStringList toStringList() const;
00089 
00090     static QProcessEnvironment systemEnvironment();
00091 
00092 private:
00093     friend class QProcessPrivate;
00094     friend class QProcessEnvironmentPrivate;
00095     QSharedDataPointer<QProcessEnvironmentPrivate> d;
00096 };
00097 
00098 class Q_CORE_EXPORT QProcess : public QIODevice
00099 {
00100     Q_OBJECT
00101 public:
00102     enum ProcessError {
00103         FailedToStart, //### file not found, resource error
00104         Crashed,
00105         Timedout,
00106         ReadError,
00107         WriteError,
00108         UnknownError
00109     };
00110     enum ProcessState {
00111         NotRunning,
00112         Starting,
00113         Running
00114     };
00115     enum ProcessChannel {
00116         StandardOutput,
00117         StandardError
00118     };
00119     enum ProcessChannelMode {
00120         SeparateChannels,
00121         MergedChannels,
00122         ForwardedChannels
00123     };
00124     enum ExitStatus {
00125         NormalExit,
00126         CrashExit
00127     };
00128 
00129     explicit QProcess(QObject *parent = 0);
00130     virtual ~QProcess();
00131 
00132     void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite);
00133     void start(const QString &program, OpenMode mode = ReadWrite);
00134 
00135     ProcessChannelMode readChannelMode() const;
00136     void setReadChannelMode(ProcessChannelMode mode);
00137     ProcessChannelMode processChannelMode() const;
00138     void setProcessChannelMode(ProcessChannelMode mode);
00139 
00140     ProcessChannel readChannel() const;
00141     void setReadChannel(ProcessChannel channel);
00142 
00143     void closeReadChannel(ProcessChannel channel);
00144     void closeWriteChannel();
00145 
00146     void setStandardInputFile(const QString &fileName);
00147     void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
00148     void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
00149     void setStandardOutputProcess(QProcess *destination);
00150 
00151 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
00152     QString nativeArguments() const;
00153     void setNativeArguments(const QString &arguments);
00154 #endif
00155 
00156     QString workingDirectory() const;
00157     void setWorkingDirectory(const QString &dir);
00158 
00159     void setEnvironment(const QStringList &environment);
00160     QStringList environment() const;
00161     void setProcessEnvironment(const QProcessEnvironment &environment);
00162     QProcessEnvironment processEnvironment() const;
00163 
00164     QProcess::ProcessError error() const;
00165     QProcess::ProcessState state() const;
00166 
00167     // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
00168     Q_PID pid() const;
00169 
00170     bool waitForStarted(int msecs = 30000);
00171     bool waitForReadyRead(int msecs = 30000);
00172     bool waitForBytesWritten(int msecs = 30000);
00173     bool waitForFinished(int msecs = 30000);
00174 
00175     QByteArray readAllStandardOutput();
00176     QByteArray readAllStandardError();
00177 
00178     int exitCode() const;
00179     QProcess::ExitStatus exitStatus() const;
00180 
00181     // QIODevice
00182     qint64 bytesAvailable() const;
00183     qint64 bytesToWrite() const;
00184     bool isSequential() const;
00185     bool canReadLine() const;
00186     void close();
00187     bool atEnd() const;
00188 
00189     static int execute(const QString &program, const QStringList &arguments);
00190     static int execute(const QString &program);
00191 
00192     static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory,
00193                               qint64 *pid = 0);
00194     static bool startDetached(const QString &program, const QStringList &arguments);
00195     static bool startDetached(const QString &program);
00196 
00197     static QStringList systemEnvironment();
00198 
00199 public Q_SLOTS:
00200     void terminate();
00201     void kill();
00202 
00203 Q_SIGNALS:
00204     void started();
00205     void finished(int exitCode);
00206     void finished(int exitCode, QProcess::ExitStatus exitStatus);
00207     void error(QProcess::ProcessError error);
00208     void stateChanged(QProcess::ProcessState state);
00209 
00210     void readyReadStandardOutput();
00211     void readyReadStandardError();
00212 
00213 protected:
00214     void setProcessState(ProcessState state);
00215 
00216     virtual void setupChildProcess();
00217 
00218     // QIODevice
00219     qint64 readData(char *data, qint64 maxlen);
00220     qint64 writeData(const char *data, qint64 len);
00221 
00222 private:
00223     Q_DECLARE_PRIVATE(QProcess)
00224     Q_DISABLE_COPY(QProcess)
00225 
00226     Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
00227     Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
00228     Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
00229     Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
00230     Q_PRIVATE_SLOT(d_func(), bool _q_processDied())
00231     Q_PRIVATE_SLOT(d_func(), void _q_notified())
00232     friend class QProcessManager;
00233 };
00234 
00235 #endif // QT_NO_PROCESS
00236 
00237 QT_END_NAMESPACE
00238 
00239 QT_END_HEADER
00240 
00241 #endif // QPROCESS_H