#ifndef _KVI_KVS_EVENTMANAGER_H_
#define _KVI_KVS_EVENTMANAGER_H_
//=============================================================================
//
//   File : kvi_kvs_eventmanager.h
//   Created on Thu Aug 17 2000 13:59:12 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2000-2004 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. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//=============================================================================

#include "kvi_settings.h"

#include "kvi_kvs_event.h"
#include "kvi_list.h"
#include "kvi_kvs_eventtable.h"

class KviWindow;
class KviKvsModuleInterface;
class KviKvsVariantList;

#define KVI_KVS_NUM_RAW_EVENTS 1000

class KVIRC_API KviKvsEventManager
{
	friend class KviModule; // compatibility only!
protected: // it only can be created and destroyed by KviKvsAliasManager::init()/done()
	KviKvsEventManager();
	~KviKvsEventManager();
protected:
	static KviKvsEventManager * m_pInstance;

	static KviKvsEvent               m_appEventTable[KVI_KVS_NUM_APP_EVENTS];
	KviPtrList<KviKvsEventHandler> * m_rawEventTable[KVI_KVS_NUM_RAW_EVENTS];
public:
	static KviKvsEventManager * instance(){ return m_pInstance; };
	static void init(); // called by KviKvs::init()
	static void done(); // called by KviKvs::done()

	KviKvsEvent * appEvent(unsigned int uEvIdx){ return &(m_appEventTable[uEvIdx]); };

	bool hasAppHandlers(unsigned int uEvIdx){ return m_appEventTable[uEvIdx].hasHandlers(); };
	KviPtrList<KviKvsEventHandler> * appHandlers(unsigned int uEvIdx){ return m_appEventTable[uEvIdx].handlers(); };
	
	bool hasRawHandlers(unsigned int uEvIdx){ return m_rawEventTable[uEvIdx]; };
	KviPtrList<KviKvsEventHandler> * rawHandlers(unsigned int uEvIdx){ return m_rawEventTable[uEvIdx]; };

	KviKvsEvent * findAppEventByName(const QString &szName);
	unsigned int findAppEventIndexByName(const QString &szName);
	bool isValidAppEvent(unsigned int uEvIdx){ return (uEvIdx < KVI_KVS_NUM_APP_EVENTS); };

	bool isValidRawEvent(unsigned int uEvIdx){ return (uEvIdx < KVI_KVS_NUM_RAW_EVENTS); };

	bool addAppHandler(unsigned int uEvIdx,KviKvsEventHandler * h);
	bool addRawHandler(unsigned int uRawIdx,KviKvsEventHandler * h);
	
	bool removeScriptAppHandler(unsigned int uEvIdx,const QString &szName);
	bool removeScriptRawHandler(unsigned int uEvIdx,const QString &szName);
	
	bool enableScriptAppHandler(unsigned int uEvIdx,const QString &szName,bool bEnable);
	bool enableScriptRawHandler(unsigned int uEvIdx,const QString &szName,bool bEnable);
	
	bool removeModuleAppHandler(unsigned int uEvIdx,KviKvsModuleInterface *i);
	bool removeModuleRawHandler(unsigned int uRawIdx,KviKvsModuleInterface *i);
	
	void removeAllModuleAppHandlers(KviKvsModuleInterface *i);
	void removeAllModuleRawHandlers(KviKvsModuleInterface *i);
	
	void removeAllModuleHandlers(KviKvsModuleInterface * i);

	KviKvsScriptEventHandler * findScriptRawHandler(unsigned int uEvIdx,const QString &szName);
	KviKvsScriptEventHandler * findScriptAppHandler(unsigned int uEvIdx,const QString &szName);

	// returns true if further processing should be stopped
	bool triggerHandlers(KviPtrList<KviKvsEventHandler> * pHandlers,KviWindow *pWnd,KviKvsVariantList *pParams);
	bool trigger(unsigned int uEvIdx,KviWindow * pWnd,KviKvsVariantList * pParams)
		{ return triggerHandlers(m_appEventTable[uEvIdx].handlers(),pWnd,pParams); };
	bool triggerRaw(unsigned int uEvIdx,KviWindow *pWnd,KviKvsVariantList * pParams)
		{ return triggerHandlers(m_rawEventTable[uEvIdx],pWnd,pParams); };

	void removeAllScriptAppHandlers();
	void removeAllScriptRawHandlers();

	void clearAppEvents();
	void clearRawEvents();
	void clear();

	void loadAppEvents(const QString &szFileName);
	void saveAppEvents(const QString &szFileName);
	void loadRawEvents(const QString &szFileName);
	void saveRawEvents(const QString &szFileName);
};


/*
#define KVS_TRIGGER_EVENT(__idx,__wnd) \
	if(KviKvsEventManager::instance()->hasEventHandlers(__idx))
		KviKvsEventManager::instance()->triggerEvent(__idx,__wnd,0);
		
#define KVS_TRIGGER_EVENT_HALTED(__idx,__wnd) \
	(KviKvsEventManager::instance()->hasEventHandlers(__idx) ? \
		KviKvsEventManager::instance()->triggerEvent(__idx,__wnd,0) : false);
*/

#endif //!_KVI_KVS_EVENTMANAGER_H_
