Canorus  0.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
externprogram.h
Go to the documentation of this file.
1 
8 #ifndef _EXTERN_PROGRAM_H_
9 #define _EXTERN_PROGRAM_H_
10 
11 // Includes
12 #include <QObject>
13 #include <QProcess>
14 #include <QStringList>
15 
16 // Forward declarations
17 
18 // This class is used to run a program in the background
19 // and to get it's input/output via signals
20 
21 // Class definition
22 class CAExternProgram : public QObject
23 {
24  Q_OBJECT
25 
26 public:
27  CAExternProgram( bool bRcvStdErr = true, bool bRcvStdOut = true );
29 
30  void setProgramName( const QString &roProgram );
31  void setProgramPath( const QString &roPath );
32  // Warning: Setting all parameters overwrites all
33  // parameters added via addParameter method!
34  void setParameters( const QStringList &roParams );
35  void inline setParamDelimiter( QString oDelimiter = " " )
36  { _oParamDelimiter = oDelimiter; }
37 
38  inline const QStringList &getParameters() { return _oParameters; }
39  inline bool getRunning()
40  { return _poExternProgram->state() == QProcess::Running; }
41  inline const QString &getParamDelimiter() { return _oParamDelimiter; }
42  int getExitState();
43 
44  void addParameter( const QString &roParam, bool bAddDelimiter = true );
45  inline void clearParameters() { _oParameters.clear(); }
46  bool execProgram( const QString &roCwd = "." );
47  inline bool waitForFinished ( int iMSecs ) { _poExternProgram->waitForFinished( iMSecs ); }
48 
49 signals:
50  void nextOutput( const QByteArray &roData );
51  void programExited( int iExitCode );
52 
53 protected slots:
54  void rcvProgramStdOut() { rcvProgramOutput( _poExternProgram->readAllStandardOutput() ); }
55  void rcvProgramStdErr() { rcvProgramOutput( _poExternProgram->readAllStandardError() ); }
56  void programError( QProcess::ProcessError ) { programExited(); }
57  void programFinished( int, QProcess::ExitStatus ) { programExited(); }
58 
59 protected:
60  void rcvProgramOutput( const QByteArray &roData );
61  void programExited();
62 
63  // References to the real objects(!)
64  QProcess *_poExternProgram; // Process object running the watched program
65  QString _oProgramName; // Program name to be run
66  QString _oProgramPath; // Program path being added to the program name
67  QStringList _oParameters; // List of program parameters
68  QString _oParamDelimiter; // delimiter between the single parameters
69  bool _bRcvStdErr; // 'true': Receive program output from stderr
70 };
71 
72 #endif // _EXTERN_PROGRAM_
73