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
|
/***************************************************************************
* Copyright (C) 2005 Nicolas Hadacek <hadacek@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#ifndef PROCESS_H
#define PROCESS_H
#include <signal.h>
#include <qstringlist.h>
#include <qobject.h>
#include <qtimer.h>
class KProcess;
#include "global.h"
namespace PURL { class Directory; }
namespace Process
{
enum State { Stopped, StartFailed, Running, Exited, Timedout };
class Base;
enum RunAction { NoRunAction = 0, Start = 1 };
Q_DECLARE_FLAGS(RunActions, RunAction)
Q_DECLARE_OPERATORS_FOR_FLAGS(RunActions)
extern State runSynchronously(Base &process, RunActions actions, uint timeout); // in ms (0 == no timeout)
//----------------------------------------------------------------------------
class Base : public QObject
{
Q_OBJECT
public:
Base(QObject *parent, const char *name);
virtual ~Base();
void setup(const QString &executable, const QStringList &options, bool withWine);
QStringList arguments() const;
void setWorkingDirectory(const PURL::Directory &dir);
void setUseShell(bool useShell);
virtual bool start(uint timeout); // in ms (0 == no timeout)
QString prettyCommand() const { return arguments().join(" "); }
void writeToStdin(const QString &s);
bool signal(int n);
bool isRunning() const;
State state() const { return _state; }
int exitCode() const;
signals:
void requestSynchronousStop();
void done(int code);
void timeout();
void stdoutDataReceived();
void stderrDataReceived();
protected slots:
void exited();
void timeoutSlot();
virtual void receivedStdout(KProcess*, char *buffer, int len) = 0;
virtual void receivedStderr(KProcess*, char *buffer, int len) = 0;
friend State runSynchronously(Base &, RunActions, uint);
protected:
State _state;
KProcess *_process;
QTimer *_timer;
QString _stdout, _stderr;
static bool isFilteredLine(const QString &line);
};
//----------------------------------------------------------------------------
class StringOutput : public Base
{
Q_OBJECT
public:
StringOutput(QObject *parent = 0, const char *name = 0) : Base(parent, name) {}
QString sout() const { return _stdout; }
QString serr() const { return _stderr; }
private slots:
virtual void receivedStdout(KProcess *, char *buffer, int len);
virtual void receivedStderr(KProcess *, char *buffer, int len);
};
//----------------------------------------------------------------------------
class LineBase : public Base
{
Q_OBJECT
public:
LineBase(QObject *parent = 0, const char *name = 0) : Base(parent, name) {}
private slots:
virtual void receivedStdout(KProcess *, char *buffer, int len);
virtual void receivedStderr(KProcess *, char *buffer, int len);
private:
virtual void addStdoutLine(const QString &line) = 0;
virtual void addStderrLine(const QString &line) = 0;
};
//----------------------------------------------------------------------------
class LineOutput : public LineBase
{
Q_OBJECT
public:
LineOutput(QObject *parent = 0, const char *name = 0) : LineBase(parent, name) {}
virtual bool start(uint timeout);
QStringList sout() const { return _stdoutLines; }
QStringList serr() const { return _stderrLines; }
protected:
QStringList _stdoutLines, _stderrLines;
virtual void addStdoutLine(const QString &line) { _stdoutLines += line; }
virtual void addStderrLine(const QString &line) { _stderrLines += line; }
};
//----------------------------------------------------------------------------
class LineSignal : public LineBase
{
Q_OBJECT
public:
LineSignal(QObject *parent = 0, const char *name = 0) : LineBase(parent, name) {}
signals:
void logStdoutLine(const QString &line);
void logStderrLine(const QString &line);
private:
virtual void addStdoutLine(const QString &line) { emit logStdoutLine(line); }
virtual void addStderrLine(const QString &line) { emit logStderrLine(line); }
};
} // namespace
#endif
|