File indexing completed on 2024-05-12 04:51:19

0001 /*
0002     SPDX-FileCopyrightText: 2009 Nokia Corporation and /or its subsidiary(-ies).
0003     Contact: Qt Software Information (qt-info@nokia.com)
0004 
0005     This file is part of the QtCore module of the Qt Toolkit.
0006 
0007     $QT_BEGIN_LICENSE:LGPL$
0008     Commercial Usage
0009     Licensees holding valid Qt Commercial licenses may use this file in
0010     accordance with the Qt Commercial License Agreement provided with the
0011     Software or, alternatively, in accordance with the terms contained in
0012     a written agreement between you and Nokia.
0013 
0014     GNU Lesser General Public License Usage
0015     Alternatively, this file may be used under the terms of the GNU Lesser
0016     General Public License version 2.1 as published by the Free Software
0017     Foundation and appearing in the file LICENSE.LGPL included in the
0018     packaging of this file.  Please review the following information to
0019     ensure the GNU Lesser General Public License version 2.1 requirements
0020     will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
0021 
0022     In addition, as a special exception, Nokia gives you certain
0023     additional rights. These rights are described in the Nokia Qt LGPL
0024     Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
0025     package.
0026 
0027     GNU General Public License Usage
0028     Alternatively, this file may be used under the terms of the GNU
0029     General Public License version 3.0 as published by the Free Software
0030     Foundation and appearing in the file LICENSE.GPL included in the
0031     packaging of this file.  Please review the following information to
0032     ensure the GNU General Public License version 3.0 requirements will be
0033     met: https://www.gnu.org/licenses/gpl-3.0.html.
0034 
0035     If you are unsure which license is appropriate for your use, please
0036     contact the sales department at qt-sales@nokia.com.
0037     $QT_END_LICENSE$
0038 
0039 */
0040 
0041 #ifndef K3B_QPROCESS_H
0042 #define K3B_QPROCESS_H
0043 
0044 #include <qiodevice.h>
0045 #include <QStringList>
0046 #include <qprocess.h>
0047 
0048 #include "k3b_export.h"
0049 
0050 // QT_BEGIN_HEADER
0051 
0052 // QT_BEGIN_NAMESPACE
0053 
0054 // QT_MODULE(Core)
0055 
0056 #ifndef QT_NO_PROCESS
0057 
0058 #if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc)
0059 typedef qint64 Q_PID;
0060 #else
0061 QT_END_NAMESPACE
0062 typedef struct _PROCESS_INFORMATION *Q_PID;
0063 QT_BEGIN_NAMESPACE
0064 #endif
0065 
0066 class K3bQProcessPrivate;
0067 
0068 class LIBK3B_EXPORT K3bQProcess : public QIODevice
0069 {
0070     Q_OBJECT
0071 public:
0072     // BE AWARE: we use the original enums from QProcess to make the future transition of slots easier
0073     /*
0074     enum ProcessError {
0075         FailedToStart, //### file not found, resource error
0076         Crashed,
0077         Timedout,
0078         ReadError,
0079         WriteError,
0080         UnknownError
0081     };
0082     enum ProcessState {
0083         NotRunning,
0084         Starting,
0085         Running
0086     };
0087     enum ProcessChannel {
0088         StandardOutput,
0089         StandardError
0090     };
0091     enum ProcessChannelMode {
0092         SeparateChannels,
0093         MergedChannels,
0094         ForwardedChannels
0095     };
0096     enum ExitStatus {
0097         NormalExit,
0098         CrashExit
0099     };
0100     */
0101     enum ProcessFlag {
0102         NoFlags = 0x0,
0103         RawStdin = 0x1,
0104         RawStdout = 0x2
0105     };
0106     Q_DECLARE_FLAGS( ProcessFlags, ProcessFlag )
0107 
0108     explicit K3bQProcess(QObject *parent = 0);
0109     ~K3bQProcess() override;
0110 
0111     void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite);
0112     void start(const QString &program, OpenMode mode = ReadWrite);
0113 
0114     ::QProcess::ProcessChannelMode readChannelMode() const;
0115     void setReadChannelMode(::QProcess::ProcessChannelMode mode);
0116     ::QProcess::ProcessChannelMode processChannelMode() const;
0117     void setProcessChannelMode(::QProcess::ProcessChannelMode mode);
0118 
0119     ProcessFlags flags() const;
0120     void setFlags( ProcessFlags flags );
0121 
0122     ::QProcess::ProcessChannel readChannel() const;
0123     void setReadChannel(::QProcess::ProcessChannel channel);
0124 
0125     void closeReadChannel(::QProcess::ProcessChannel channel);
0126     void closeWriteChannel();
0127 
0128     void setStandardInputFile(const QString &fileName);
0129     void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
0130     void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
0131     void setStandardOutputProcess(K3bQProcess *destination);
0132 
0133     QString workingDirectory() const;
0134     void setWorkingDirectory(const QString &dir);
0135 
0136     void setEnvironment(const QStringList &environment);
0137     QStringList environment() const;
0138 
0139     ::QProcess::ProcessError error() const;
0140     ::QProcess::ProcessState state() const;
0141 
0142     // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
0143     Q_PID pid() const;
0144 
0145     bool waitForStarted(int msecs = 30000);
0146     bool waitForReadyRead(int msecs = 30000) override;
0147     bool waitForBytesWritten(int msecs = 30000) override;
0148     bool waitForFinished(int msecs = 30000);
0149 
0150     QByteArray readAllStandardOutput();
0151     QByteArray readAllStandardError();
0152 
0153     int exitCode() const;
0154     ::QProcess::ExitStatus exitStatus() const;
0155 
0156     // QIODevice
0157     qint64 bytesAvailable() const override;
0158     qint64 bytesToWrite() const override;
0159     bool isSequential() const override;
0160     bool canReadLine() const override;
0161     void close() override;
0162     bool atEnd() const override;
0163 
0164     bool isReadyWrite() const;
0165 
0166     static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory,
0167                               qint64 *pid = 0);
0168     static bool startDetached(const QString &program, const QStringList &arguments);
0169     static bool startDetached(const QString &program);
0170 
0171     static QStringList systemEnvironment();
0172 
0173 public Q_SLOTS:
0174     void terminate();
0175     void kill();
0176 
0177 Q_SIGNALS:
0178     void started();
0179     void finished(int exitCode);
0180     void finished(int exitCode, QProcess::ExitStatus exitStatus);
0181     void error(QProcess::ProcessError error);
0182     void stateChanged(QProcess::ProcessState state);
0183 
0184     void readyReadStandardOutput();
0185     void readyReadStandardError();
0186     void readyWrite();
0187 
0188 protected:
0189     void setProcessState(QProcess::ProcessState state);
0190 
0191     virtual void setupChildProcess();
0192 
0193     // QIODevice
0194     qint64 readData(char *data, qint64 maxlen) override;
0195     qint64 writeData(const char *data, qint64 len) override;
0196 
0197 private:
0198     Q_DECLARE_PRIVATE(K3bQProcess)
0199     Q_DISABLE_COPY(K3bQProcess)
0200 
0201     K3bQProcessPrivate* d_ptr;
0202 
0203     Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
0204     Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
0205     Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
0206     Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
0207     Q_PRIVATE_SLOT(d_func(), bool _q_processDied())
0208     Q_PRIVATE_SLOT(d_func(), bool _q_notifyProcessDied())
0209     Q_PRIVATE_SLOT(d_func(), void _q_notified())
0210     friend class K3bQProcessManager;
0211 };
0212 
0213 #endif // QT_NO_PROCESS
0214 
0215 // QT_END_NAMESPACE
0216 
0217 // QT_END_HEADER
0218 
0219 Q_DECLARE_OPERATORS_FOR_FLAGS( K3bQProcess::ProcessFlags )
0220 
0221 #endif // QPROCESS_H