1 #ifndef _KVI_KVS_PROCESSMANAGER_H_
2 #define _KVI_KVS_PROCESSMANAGER_H_
3 //=============================================================================
4 //
5 //   File : KviKvsProcessManager.h
6 //   Creation date : Wed 07 Apr 2004 03:03:52 by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC client distribution
9 //   Copyright (C) 2004-2010 Szymon Stefanek <pragma at kvirc dot net>
10 //
11 //   This program is FREE software. You can redistribute it and/or
12 //   modify it under the terms of the GNU General Public License
13 //   as published by the Free Software Foundation; either version 2
14 //   of the License, or (at your option) any later version.
15 //
16 //   This program is distributed in the HOPE that it will be USEFUL,
17 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //   See the GNU General Public License for more details.
20 //
21 //   You should have received a copy of the GNU General Public License
22 //   along with this program. If not, write to the Free Software Foundation,
23 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "kvi_settings.h"
28 #include "KviKvsAsyncOperation.h"
29 #include "KviQString.h"
30 #include "KviWindow.h"
31 #include "KviKvsVariant.h"
32 #include "KviKvsRunTimeContext.h"
33 
34 #include "KviPointerHashTable.h"
35 
36 #ifdef Unsorted
37 #undef Unsorted
38 #endif
39 
40 #include "QProcess"
41 
42 #include <QTimer>
43 
44 #define KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDOUT 1
45 #define KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTDERR 2
46 #define KVI_KVS_PROCESSDESCRIPTOR_TRIGGERTERMINATED 4
47 #define KVI_KVS_PROCESSDESCRIPTOR_OUTPUTBYBLOCKS 8
48 #define KVI_KVS_PROCESSDESCRIPTOR_TRIGGERSTARTED 16
49 #define KVI_KVS_PROCESSDESCRIPTOR_NOSHELL 32
50 #define KVI_KVS_PROCESSDESCRIPTOR_KILLIFNOWINDOW 64
51 
52 class KviWindow;
53 class KviKvsScript;
54 class KviKvsVariant;
55 
56 struct KviKvsProcessDescriptorData
57 {
58 	QString szShell;
59 	QString szCommandline;
60 	KviKvsScript * pCallback;
61 	KviKvsVariant * pMagic;
62 	KviWindow * pWnd;
63 	int iFlags;
64 	int iMaxRunTime;  // 0 for no timeout
65 	int iPingTimeout; // 0 for no ping timeout
66 };
67 
68 class KviKvsProcessManager;
69 
70 class KVIRC_API KviKvsProcessAsyncOperation : public KviKvsAsyncOperation
71 {
72 	friend class KviKvsProcessManager;
73 	Q_OBJECT
74 public:
75 	KviKvsProcessAsyncOperation(KviKvsProcessDescriptorData * d);
76 	~KviKvsProcessAsyncOperation();
77 
78 protected:
79 	enum CallbackEvent
80 	{
81 		EventStarted,
82 		EventTerminated,
83 		EventStdout,
84 		EventStderr,
85 		EventPing
86 	};
87 
88 private:
89 	QProcess * m_pProcess;
90 	KviKvsExtendedRunTimeData * m_pExtendedRunTimeData;
91 	QString m_szStdoutBuffer;
92 	QString m_szStderrBuffer;
93 	KviKvsProcessDescriptorData * m_pData;
94 	QTimer * m_pPingTimer;
95 	QTimer * m_pRunTimeTimer;
96 	bool m_bDeletePending;
97 
98 public:
99 	bool start();
100 private slots:
101 	void maxRunTimeExpired();
102 	void ping();
103 	void readStdout();
104 	void readStderr();
105 	void processExited(int exitCode);
106 	void processStarted();
107 	void selfDelete();
108 
109 private:
110 	bool trigger(CallbackEvent e, const QString & szData);
111 	void triggerSelfDelete();
112 };
113 
114 #endif //!_KVI_KVS_PROCESSMANAGER_H_
115