summaryrefslogtreecommitdiffstats
path: root/lib/util/blockingkprocess.h
blob: 8d164bef5f8f358ef3b9e59ab3d5b97627fdea6e (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

/***************************************************************************
*   Copyright (C) 2006 by Andras Mantia                                   *
*   [email protected]                                                       *
*                                                                         *
*   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 BLOCKINGKPROCESS_H
#define BLOCKINGKPROCESS_H

#include <kprocess.h>

/**
 *  Blocking version of TDEProcess, which stores the stdout.
 *  Differences between start(TDEProcess::Block, TDEProcess::StdOut) and this
 *  class are:
 *  - the GUI update is not blocked why the external process is running
 *  - in case of problems there is a timeout (defaults to 60 seconds), after which the 
 *    process is terminated.
 *  - the stdout is caught - it the caller request it - and can be read by the caller
 * @author Andras Mantia <[email protected]>
*/

class TQTimer;
class BlockingTDEProcess : public TDEProcess
{
  Q_OBJECT
  
  
public:
    BlockingTDEProcess(TQObject *parent, const char *name=0);
    BlockingTDEProcess();

    virtual ~BlockingTDEProcess();
    
    /**
     * Start the process. It waits until the process exits or the timeout is hit.
     * @param runmode @see TDEProcess, use TDEProcess::NotifyOnExit to get proper behaviour,
     * not TDEProcess::Block
     * @param comm if Stdout is passed, it catches the output. For the rest @see TDEProcess
     * @return true in case of success, false if there are problems to start the process
     *        or it was killed because of the timeout.
     */
    virtual bool start(RunMode runmode=NotifyOnExit, Communication comm=NoCommunication);
  
    /**
     * Get the output of the run process
     * @return the output
     */
    TQString stdOut() { return m_stdOut;}
    /**
     * Clear the internal stdout buffer. Useful in case the class is reused.
     */
    void clearStdOut() { m_stdOut = "";}
    /**
     * Get the error output of the run process
     * @return the output
     */
    TQString stdErr() { return m_stdErr;}
    /**
     * Clear the internal stderr buffer. Useful in case the class is reused.
     */
    void clearStdErr() { m_stdErr = "";}

    /**
     * Sets the timeout
     * @param timeout seconds after which the process is considered hung and killed. 0 disables the timeout.
     */
    void setTimeOut(int timeout) { m_timeoutValue = timeout; }
    
private slots:
    void slotReceivedStdOut(TDEProcess *proc, char *buffer, int buflen);
    void slotReceivedStdErr(TDEProcess *proc, char *buffer, int buflen);
    void slotProcessExited(TDEProcess *proc);
    void slotTimeOut();
            
private:
    void enter_loop();
        
    TQString m_stdOut;   
    TQString m_stdErr;   
    bool m_timeout;     
    int m_timeoutValue;
    TQTimer *m_timer;
};

#endif