summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_timermanager.h
blob: 83b73d1a76cd7cff99589d607be91478e2f17a6d (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
#ifndef _KVI_KVS_TIMERMANAGER_H_
#define _KVI_KVS_TIMERMANAGER_H_
//=============================================================================
//
//   File : kvi_kvs_timermanager.h
//   Created on Fri 19 Dec 2003 01:29:22 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2003 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================


#include "kvi_settings.h"
#include "kvi_qstring.h"

#include <tqobject.h>
#include "kvi_pointerhashtable.h"
#include "kvi_pointerhashtable.h"
#include "kvi_pointerlist.h"

class KviKvsTimerManager;
class KviKvsScript;
class KviKvsHash;
class KviKvsVariantList;
class KviKvsExtendedRunTimeData;

class KviWindow;

// FIXME: This stuff could be moved to a module
//        called timer
//        timer.start(){}
//        timer.stop
//        timer.exists
//        timer.timeout
//        timer.suspend
//        etc...
//        Then we could remap timer.start to the old /timer call
//        by hardcoding it...

class KVIRC_API KviKvsTimer
{
	friend class KviKvsTimerManager;
public:
	enum Lifetime { SingleShot, WindowLifetime, Persistent };
protected:
	KviKvsTimer(const TQString &szName,Lifetime l,KviWindow * pWnd,int iDelay,int iId,KviKvsScript * pCallback,KviKvsVariantList * pParams);
public:
	~KviKvsTimer();
protected:
	Lifetime                    m_eLifetime;        // the type of this timer
	KviWindow                 * m_pWnd;             // the window that this timer is (currently) bound to
	TQString                     m_szName;           // this timer name
	KviKvsScript              * m_pCallback;        // callback to be executed at timer shots
	int                         m_iDelay;           // the timer delay in msecs
	int                         m_iId;              // the system id of this timer
	KviKvsExtendedRunTimeData * m_pRunTimeData;     // ext run time data for this timer object
	KviKvsVariantList         * m_pParameterList;   // parameter list
public:
	KviWindow * window(){ return m_pWnd; };
	const TQString & name(){ return m_szName; };
	const KviKvsScript * callback(){ return m_pCallback; };
	Lifetime lifetime(){ return m_eLifetime; };
	int delay(){ return m_iDelay; };
	int id(){ return m_iId; };
	//KviKvsHash * variables(){ return m_pVariables; };
	KviKvsExtendedRunTimeData * runTimeData(){ return m_pRunTimeData; };
	KviKvsVariantList * parameterList(){ return m_pParameterList; };
protected:
	void setWindow(KviWindow * pWnd){ m_pWnd = pWnd; };
};


class KVIRC_API KviKvsTimerManager : public TQObject
{
	Q_OBJECT
  TQ_OBJECT
protected: // it only can be created and destroyed by KviKvsTimerManager::init()/done()
	KviKvsTimerManager();
	~KviKvsTimerManager();
private:
	KviPointerHashTable<int,KviKvsTimer>     * m_pTimerDictById;      // stored by id
	KviPointerHashTable<TQString,KviKvsTimer>        * m_pTimerDictByName;    // stored by name
	static KviKvsTimerManager   * m_pInstance;             // the one and only timer manager instance
	KviPointerList<KviKvsTimer>     * m_pKilledTimerList;      // list of timers for that killing has been scheduled
	int                           m_iAssassinTimer;        // assassin timer id
	int                           m_iCurrentTimer;         // the timer currently executed
public:
	static KviKvsTimerManager * instance(){ return m_pInstance; };
	static void init();
	static void done();
	// the pCallback and pParams are owned by the timer: they WILL be deleted
	bool addTimer(const TQString &szName,KviKvsTimer::Lifetime l,KviWindow * pWnd,int iDelay,KviKvsScript * pCallback,KviKvsVariantList * pParams);
	bool deleteTimer(const TQString &szName);
	bool deleteTimer(int iId);
	// the timer manager does not trigger timers concurrently
	// this means that if this is called from a timer handler
	// the current timer will be unique
	bool deleteCurrentTimer();
	void deleteAllTimers();
	bool timerExists(const TQString &szName){ return m_pTimerDictByName->find(szName); };
	KviPointerHashTable<TQString,KviKvsTimer> * timerDict()
		{ return m_pTimerDictByName; };
protected:
	void scheduleKill(KviKvsTimer * t);
	virtual void timerEvent(TQTimerEvent *e);
};


#endif //!_KVI_KVS_TIMERMANAGER_H_