]> git.lyx.org Git - lyx.git/blob - src/support/SystemcallPrivate.h
0b616dcd3c2dfd7dd91cc6f26d5783f200375a5e
[lyx.git] / src / support / SystemcallPrivate.h
1 // -*- C++ -*-
2 /**
3  * \file SystemcallPrivate.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Enrico Forestieri
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef SYSTEMCALLPRIVATE_H
13 #define SYSTEMCALLPRIVATE_H
14
15 #include <QObject>
16 #include <QProcess>
17
18 #include <string>
19
20 namespace lyx {
21 namespace support {
22
23 class Systemcall;
24
25 /**
26  * Outputs to the console terminal the line buffered standard output and
27  * error of a spawned process when there is a controlling terminal and 
28  * stdout/stderr have not been redirected.
29  */
30 class SystemcallPrivate : public QObject
31 {
32         Q_OBJECT
33
34 public:
35         SystemcallPrivate(std::string const & outfile);
36         ~SystemcallPrivate();
37
38         enum State {
39                 Starting,
40                 Running,
41                 Finished,
42                 Error
43         };
44         State state;
45
46         bool waitWhile(State, bool processEvents, int timeout = -1);
47         void startProcess(const QString& cmd);
48         
49         int exitCode();
50
51         QString errorMessage() const;
52         QString exitStatusMessage() const;
53
54         void flush();
55
56         QProcess* releaseProcess();
57         
58         static void killProcess(QProcess * p);
59
60
61 public Q_SLOTS:
62         void stdOut();
63         void stdErr();
64         void processError(QProcess::ProcessError);
65         void processStarted();
66         void processFinished(int, QProcess::ExitStatus status);
67
68
69 private:
70         /// Pointer to the process to monitor.
71         QProcess * process_;
72
73         /// Index to the standard output buffer.
74         size_t outIndex_;
75         /// Index to the standard error buffer.
76         size_t errIndex_;
77         ///
78         std::string outFile_;
79
80         /// Size of buffers.
81         static size_t const bufferSize_ = 200;
82         /// Standard output buffer.
83         char outData_[bufferSize_];
84         /// Standard error buffer.
85         char errData_[bufferSize_];
86
87         bool terminalErrExists_;
88         bool terminalOutExists_;
89
90         bool processEvents_;
91         void waitAndProcessEvents();
92         void processEvents();
93         void killProcess();
94         QString cmd_;
95
96 };
97
98 } // namespace support
99 } // namespace lyx
100
101 #endif // SYSTEMCALLPRIVATE_H