summaryrefslogtreecommitdiffstats
path: root/src/jomlib/process.h
blob: 013142b9e36c98165bd6405274c7dfc8d517b229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/****************************************************************************
 **
 ** Copyright (C) 2015 The Qt Company Ltd.
 ** Contact: http://www.qt.io/licensing/
 **
 ** This file is part of the jom project on Trolltech Labs.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License version 2.0 or 3.0 as published by the Free Software Foundation
 ** and appearing in the file LICENSE.GPL included in the packaging of
 ** this file.  Please review the following information to ensure GNU
 ** General Public Licensing requirements will be met:
 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
 ** http://www.gnu.org/copyleft/gpl.html.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

#ifndef PROCESS_H
#define PROCESS_H

#include "processenvironment.h"
#include <QObject>
#include <QStringList>

#ifdef USE_QPROCESS

#include <QProcess>

namespace NMakeFile {

class Process : public QProcess
{
    Q_OBJECT
public:
    enum ProcessError
    {
        UnknownError,
        FailedToStart,
        Crashed
    };

    enum ExitStatus
    {
        NormalExit,
        CrashExit
    };

    Process(QObject *parent = 0);
    void setBufferedOutput(bool bufferedOutput);
    bool isBufferedOutputSet() const;
    void setEnvironment(const ProcessEnvironment &e);
    ProcessEnvironment environment() const;
    bool isRunning() const;
    void start(const QString &commandLine);
    void writeToStdOutBuffer(const QByteArray &output);
    void writeToStdErrBuffer(const QByteArray &output);
    ExitStatus exitStatus() const;

signals:
    void error(Process::ProcessError);
    void finished(int, Process::ExitStatus);

private slots:
    void forwardError(QProcess::ProcessError);
    void forwardFinished(int, QProcess::ExitStatus);
};

} // namespace NMakeFile

#else

namespace NMakeFile {

class Process : public QObject
{
    Q_OBJECT
public:
    explicit Process(QObject *parent = 0);
    ~Process();

    enum ProcessError
    {
        UnknownError,
        FailedToStart,
        Crashed
    };

    enum ExitStatus
    {
        NormalExit,
        CrashExit
    };

    enum ProcessState
    {
        NotRunning,
        Starting,
        Running
    };

    void setBufferedOutput(bool b);
    bool isBufferedOutputSet() const { return m_bufferedOutput; }
    void writeToStdOutBuffer(const QByteArray &output);
    void writeToStdErrBuffer(const QByteArray &output);
    void setWorkingDirectory(const QString &path);
    const QString &workingDirectory() const { return m_workingDirectory; }
    void setEnvironment(const ProcessEnvironment &environment);
    const ProcessEnvironment &environment() const { return m_environment; }
    int exitCode() const { return m_exitCode; }
    ExitStatus exitStatus() const { return m_exitStatus; }
    bool isRunning() const { return m_state == Running; }

signals:
    void error(Process::ProcessError);
    void finished(int, Process::ExitStatus);

public slots:
    void start(const QString &commandLine);
    bool waitForFinished();

private:
    void printBufferedOutput();

private slots:
    void tryToRetrieveExitCode();
    void onProcessFinished();

private:
    class ProcessPrivate *d;
    QString m_workingDirectory;
    ProcessEnvironment m_environment;
    QByteArray m_envBlock;
    ProcessState m_state;
    int m_exitCode;
    ExitStatus m_exitStatus;
    bool m_bufferedOutput;

    friend class ProcessPrivate;
};

} // namespace NMakeFile

#endif // USE_QPROCESS

#endif // PROCESS_H