summaryrefslogtreecommitdiffstats
path: root/libk3b/core/k3bprocess.h
blob: 19fb024f878cfbf69351db4a89b9c6daa7e0b73a (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* 
 *
 * $Id: k3bprocess.h 621644 2007-01-09 12:53:09Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.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.
 * See the file "COPYING" for the exact licensing terms.
 */


#ifndef K3B_PROCESS_H
#define K3B_PROCESS_H


#include <kprocess.h>
#include <tqstring.h>
#include "k3b_export.h"

class K3bExternalBin;


/**
 * This is an enhanced TDEProcess.
 * It splits the stderr output to lines making sure the client gets every line as it 
 * was written by the process.
 * Aditionally one may set raw stdout and stdin handling using the stdin() and stdout() methods
 * to get the process' file descriptors.
 * Last but not least K3bProcess is able to duplicate stdout making it possible to connect two 
 * K3bProcesses like used in K3bDataJob to duplicate mkisofs' stdout to the stdin of the writer 
 * (cdrecord or cdrdao)
 */
class LIBK3B_EXPORT K3bProcess : public TDEProcess
{
  Q_OBJECT
  
    
 public:
  class OutputCollector;

 public:
  K3bProcess();
  ~K3bProcess();

  /**
   * In the future this might also set the nice value
   */
  K3bProcess& operator<<( const K3bExternalBin* );

  K3bProcess& operator<<( const TQString& arg );
  K3bProcess& operator<<( const char* arg );
  K3bProcess& operator<<( const TQCString& arg );
  K3bProcess& operator<<( const TQStringList& args );

  bool start( RunMode run = NotifyOnExit, Communication com = NoCommunication );

  /** 
   * get stdin file descriptor
   * Only makes sense while process is running.
   *
   * Only use with setRawStdin
   */
  int stdinFd() const;

  /** 
   * get stdout file descriptor
   * Only makes sense while process is running.
   *
   * Only use with setRawStdout
   */
  int stdoutFd() const;

  /**
   * @deprecated use writeToFd
   */
  void dupStdout( int fd );

  /**
   * @deprecated use readFromFd
   */
  void dupStdin( int fd );

  /**
   * Make the process write to @fd instead of Stdout.
   * This means you won't get any stdoutReady() or receivedStdout()
   * signals anymore.
   *
   * Only use this before starting the process.
   */
  void writeToFd( int fd );

  /**
   * Make the process read from @fd instead of Stdin.
   * This means you won't get any wroteStdin()
   * signals anymore.
   *
   * Only use this before starting the process.
   */
  void readFromFd( int fd );

  /** 
   * If set true the process' stdin fd will be available
   * through @stdinFd.
   * Be aware that you will not get any wroteStdin signals
   * anymore.
   *
   * Only use this before starting the process.
   */
  void setRawStdin(bool b);

  /** 
   * If set true the process' stdout fd will be available
   * through @stdoutFd.
   * Be aware that you will not get any stdoutReady or receivedStdout
   * signals anymore.
   *
   * Only use this before starting the process.
   */
  void setRawStdout(bool b);

 public slots:
  void setSplitStdout( bool b ) { m_bSplitStdout = b; }
 
  /**
   * default is true
   */
  void setSuppressEmptyLines( bool b );

  bool closeStdin();
  bool closeStdout();

 private slots:
  void slotSplitStderr( TDEProcess*, char*, int );
  void slotSplitStdout( TDEProcess*, char*, int );

 signals:
  void stderrLine( const TQString& line );
  void stdoutLine( const TQString& line );

  /** 
   * Gets emitted if raw stdout mode has been requested
   * The data has to be read from @p fd.
   */
  void stdoutReady( int fd );

 protected:
  /**
   * reimplemeted from TDEProcess
   */
  int commSetupDoneP();

  /**
   * reimplemeted from TDEProcess
   */
  int commSetupDoneC();

  /**
   * reimplemeted from TDEProcess
   */
  int setupCommunication( Communication comm );

  /**
   * reimplemeted from TDEProcess
   */
  void commClose();

 private:
  static TQStringList splitOutput( char*, int, TQString&, bool );

  class Data;
  Data* d;

  bool m_bSplitStdout;
};

class LIBK3B_EXPORT K3bProcessOutputCollector: public TQObject
{
  Q_OBJECT
  
    
 public:
  K3bProcessOutputCollector( TDEProcess* );
  void setProcess( TDEProcess* );
  
  const TQString& output() const { return m_gatheredOutput; }
  const TQString& stderrOutput() const { return m_stderrOutput; }
  const TQString& stdoutOutput() const { return m_stdoutOutput; }
  
 private slots:
  void slotGatherStderr( TDEProcess*, char*, int );
  void slotGatherStdout( TDEProcess*, char*, int );
  
 private:
  TQString m_gatheredOutput;
  TQString m_stderrOutput;
  TQString m_stdoutOutput;
  TDEProcess* m_process;
};


#endif