LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / utils / synchronousprocess.h
blobdb0ea01a15b571e038c8b72dc0b4452591ab18ff
1 /**
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.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
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
22 * for more details.
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>
38 QT_BEGIN_NAMESPACE
39 class QTextCodec;
40 class QDebug;
41 class QByteArray;
42 QT_END_NAMESPACE
44 namespace Utils {
45 struct SynchronousProcessPrivate;
47 /* Result of SynchronousProcess execution */
48 struct QTCREATOR_UTILS_EXPORT SynchronousProcessResponse {
49 enum Result {
50 // Finished with return code 0
51 Finished,
52 // Finished with return code != 0
53 FinishedError,
54 // Process terminated abnormally (kill)
55 TerminatedAbnormally,
56 // Executable could not be started
57 StartFailed,
58 // Hang, no output after time out
59 Hang
62 SynchronousProcessResponse();
63 void clear();
65 Result result;
66 int exitCode;
67 QString stdOut;
68 QString stdErr;
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
78 * writes them.
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 {
86 Q_OBJECT
87 public:
88 SynchronousProcess();
89 virtual ~SynchronousProcess();
91 /* Timeout for hanging processes (no reaction on stderr/stdout)*/
92 void setTimeout(int timeoutMS);
93 int timeout() const;
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
116 // and file types.
117 static QString locateBinary(const QString &binary);
118 static QString locateBinary(const QString &path, const QString &binary);
119 static QChar pathSeparator();
121 signals:
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);
128 private slots:
129 void slotTimeout();
130 void finished(int exitCode, QProcess::ExitStatus e);
131 void error(QProcess::ProcessError);
132 void stdOutReady();
133 void stdErrReady();
135 private:
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;
143 } // namespace Utils
145 #endif // ifndef SYNCHRONOUSPROCESS_H