synchronousprocess.h 6.4 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32
#ifndef SYNCHRONOUSPROCESS_H
#define SYNCHRONOUSPROCESS_H

hjk's avatar
hjk committed
33 34
#include "utils_global.h"

con's avatar
con committed
35 36 37
#include <QtCore/QObject>
#include <QtCore/QProcess>
#include <QtCore/QStringList>
38
#include <QtCore/QSharedPointer>
con's avatar
con committed
39 40 41 42 43 44 45 46 47 48 49 50

QT_BEGIN_NAMESPACE
class QTextCodec;
class QDebug;
class QByteArray;
QT_END_NAMESPACE

namespace Utils {

struct SynchronousProcessPrivate;

/* Result of SynchronousProcess execution */
51
struct QTCREATOR_UTILS_EXPORT SynchronousProcessResponse
hjk's avatar
hjk committed
52
{
con's avatar
con committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    enum Result {
        // Finished with return code 0
        Finished,
        // Finished with return code != 0
        FinishedError,
        // Process terminated abnormally (kill)
        TerminatedAbnormally,
        // Executable could not be started
        StartFailed,
        // Hang, no output after time out
        Hang };

    SynchronousProcessResponse();
    void clear();

68 69 70
    // Helper to format an exit message.
    QString exitMessage(const QString &binary, int timeoutMS) const;

con's avatar
con committed
71 72 73 74 75 76
    Result result;
    int exitCode;
    QString stdOut;
    QString stdErr;
};

77
QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug str, const SynchronousProcessResponse &);
con's avatar
con committed
78 79 80 81 82 83 84 85 86 87 88

/* SynchronousProcess: Runs a synchronous process in its own event loop
 * that blocks only user input events. Thus, it allows for the gui to
 * repaint and append output to log windows.
 *
 * The stdOut(), stdErr() signals are emitted unbuffered as the process
 * writes them.
 *
 * The stdOutBuffered(), stdErrBuffered() signals are emitted with complete
 * lines based on the '\n' marker if they are enabled using
 * stdOutBufferedSignalsEnabled()/setStdErrBufferedSignalsEnabled().
89 90 91 92 93
 * They would typically be used for log windows.
 *
 * There is a timeout handling that takes effect after the last data have been
 * read from stdout/stdin (as opposed to waitForFinished(), which measures time
 * since it was invoked). It is thus also suitable for slow processes that continously
94 95 96 97
 * output data (like version system operations).
 *
 * The property timeOutMessageBoxEnabled influences whether a message box is
 * shown asking the user if they want to kill the process on timeout (default: false). */
con's avatar
con committed
98

99
class QTCREATOR_UTILS_EXPORT SynchronousProcess : public QObject
hjk's avatar
hjk committed
100
{
con's avatar
con committed
101 102
    Q_OBJECT
public:
103 104 105 106 107
    enum Flags {
        // Unix: Do not give the child process a terminal for input prompting.
        UnixTerminalDisabled = 0x1
    };

con's avatar
con committed
108 109 110
    SynchronousProcess();
    virtual ~SynchronousProcess();

111 112
    /* Timeout for hanging processes (triggers after no more output
     * occurs on stderr/stdout). */
con's avatar
con committed
113 114 115 116 117 118
    void setTimeout(int timeoutMS);
    int timeout() const;

    void setStdOutCodec(QTextCodec *c);
    QTextCodec *stdOutCodec() const;

119 120 121
    QProcess::ProcessChannelMode processChannelMode () const;
    void setProcessChannelMode(QProcess::ProcessChannelMode m);

con's avatar
con committed
122 123 124 125 126 127
    bool stdOutBufferedSignalsEnabled() const;
    void setStdOutBufferedSignalsEnabled(bool);

    bool stdErrBufferedSignalsEnabled() const;
    void setStdErrBufferedSignalsEnabled(bool);

128 129 130
    bool timeOutMessageBoxEnabled() const;
    void setTimeOutMessageBoxEnabled(bool);

con's avatar
con committed
131 132 133
    QStringList environment() const;
    void setEnvironment(const QStringList &);

134 135 136
    void setProcessEnvironment(const QProcessEnvironment &environment);
    QProcessEnvironment processEnvironment() const;

137 138 139
    void setWorkingDirectory(const QString &workingDirectory);
    QString workingDirectory() const;

140 141 142
    unsigned flags() const;
    void setFlags(unsigned);

con's avatar
con committed
143 144
    SynchronousProcessResponse run(const QString &binary, const QStringList &args);

145 146 147
    // Create a (derived) processes with flags applied.
    static QSharedPointer<QProcess> createProcess(unsigned flags);

148 149 150 151 152
    // Static helper for running a process synchronously in the foreground with timeout
    // detection similar SynchronousProcess' handling (taking effect after no more output
    // occurs on stderr/stdout as opposed to waitForFinished()). Returns false if a timeout
    // occurs. Checking of the process' exit state/code still has to be done.
    static bool readDataFromProcess(QProcess &p, int timeOutMS,
153 154
                                    QByteArray *stdOut = 0, QByteArray *stdErr = 0,
                                    bool timeOutMessageBox = false);
155 156 157 158
    // Stop a process by first calling terminate() (allowing for signal handling) and
    // then kill().
    static bool stopProcess(QProcess &p);

159 160 161 162 163 164
    // Helpers to find binaries. Do not use it for other path variables
    // and file types.
    static QString locateBinary(const QString &binary);
    static QString locateBinary(const QString &path, const QString &binary);
    static QChar pathSeparator();

con's avatar
con committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
signals:
    void stdOut(const QByteArray &data, bool firstTime);
    void stdErr(const QByteArray &data, bool firstTime);

    void stdOutBuffered(const QString &data, bool firstTime);
    void stdErrBuffered(const QString &data, bool firstTime);

private slots:
    void slotTimeout();
    void finished(int exitCode, QProcess::ExitStatus e);
    void error(QProcess::ProcessError);
    void stdOutReady();
    void stdErrReady();

private:
    void processStdOut(bool emitSignals);
    void processStdErr(bool emitSignals);
    static QString convertStdErr(const QByteArray &);
    QString convertStdOut(const QByteArray &) const;

    SynchronousProcessPrivate *m_d;
};

hjk's avatar
hjk committed
188 189
} // namespace Utils

con's avatar
con committed
190
#endif