2 ******************************************************************************
4 * @file synchronousprocess.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef SYNCHRONOUSPROCESS_H
30 #define SYNCHRONOUSPROCESS_H
32 #include "utils_global.h"
34 #include <QtCore/QObject>
35 #include <QtCore/QProcess>
36 #include <QtCore/QStringList>
45 struct SynchronousProcessPrivate
;
47 /* Result of SynchronousProcess execution */
48 struct QTCREATOR_UTILS_EXPORT SynchronousProcessResponse
{
50 // Finished with return code 0
52 // Finished with return code != 0
54 // Process terminated abnormally (kill)
56 // Executable could not be started
58 // Hang, no output after time out
62 SynchronousProcessResponse();
71 QTCREATOR_UTILS_EXPORT QDebug
operator<<(QDebug str
, const SynchronousProcessResponse
&);
73 /* SynchronousProcess: Runs a synchronous process in its own event loop
74 * that blocks only user input events. Thus, it allows for the gui to
75 * repaint and append output to log windows.
77 * The stdOut(), stdErr() signals are emitted unbuffered as the process
80 * The stdOutBuffered(), stdErrBuffered() signals are emitted with complete
81 * lines based on the '\n' marker if they are enabled using
82 * stdOutBufferedSignalsEnabled()/setStdErrBufferedSignalsEnabled().
83 * They would typically be used for log windows. */
85 class QTCREATOR_UTILS_EXPORT SynchronousProcess
: public QObject
{
89 virtual ~SynchronousProcess();
91 /* Timeout for hanging processes (no reaction on stderr/stdout)*/
92 void setTimeout(int timeoutMS
);
95 void setStdOutCodec(QTextCodec
*c
);
96 QTextCodec
*stdOutCodec() const;
98 QProcess::ProcessChannelMode
processChannelMode() const;
99 void setProcessChannelMode(QProcess::ProcessChannelMode m
);
101 bool stdOutBufferedSignalsEnabled() const;
102 void setStdOutBufferedSignalsEnabled(bool);
104 bool stdErrBufferedSignalsEnabled() const;
105 void setStdErrBufferedSignalsEnabled(bool);
107 QStringList
environment() const;
108 void setEnvironment(const QStringList
&);
110 void setWorkingDirectory(const QString
&workingDirectory
);
111 QString
workingDirectory() const;
113 SynchronousProcessResponse
run(const QString
&binary
, const QStringList
&args
);
115 // Helpers to find binaries. Do not use it for other path variables
117 static QString
locateBinary(const QString
&binary
);
118 static QString
locateBinary(const QString
&path
, const QString
&binary
);
119 static QChar
pathSeparator();
122 void stdOut(const QByteArray
&data
, bool firstTime
);
123 void stdErr(const QByteArray
&data
, bool firstTime
);
125 void stdOutBuffered(const QString
&data
, bool firstTime
);
126 void stdErrBuffered(const QString
&data
, bool firstTime
);
130 void finished(int exitCode
, QProcess::ExitStatus e
);
131 void error(QProcess::ProcessError
);
136 void processStdOut(bool emitSignals
);
137 void processStdErr(bool emitSignals
);
138 static QString
convertStdErr(const QByteArray
&);
139 QString
convertStdOut(const QByteArray
&) const;
141 SynchronousProcessPrivate
*m_d
;
145 #endif // ifndef SYNCHRONOUSPROCESS_H