
//  Copyright (C) 2009 by Arnaud Dupuis
//  a.dupuis@infinityperl.org
//  http://www.infinityperl.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 3 of the License, or
//  (at your option) 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


#ifndef ASSUMA_ADDONENGINE_H
#define ASSUMA_ADDONENGINE_H

#include <QObject>
#include <QScriptEngine>
#include <QWidget>
#include <QMap>
#include "assuma_addoninstance.h"

class QString;
/*! \class AssumaAddonEngine
* \brief the engine for Association Subscribers Manager's addon system.
*
* This class is designed to make simple loading, instanciation and destruction of addons.
* This is the core class of the addon system.
*/
class AssumaAddonEngine : public QObject {
	Q_OBJECT
	public:
		//! Constructor
		/*!
		The constructor of the engine.Do nothing but to initialyze the engine.
		\param addonsDirectory the directory where addons are stored.
		*/
		AssumaAddonEngine(const  QString &);
		/*!
		This function returns the list of addons that is really instanciated.
		\returns an addons list
		*/
		QList<AssumaAddonInstance*> instanciatedAddons();
		/*!
		This function allow you to fully instanciate (and run) a addon. 
		This means read the addon package's XML and execute the addon code.
		\param addonName the name of the addon to instanciate.
		\returns a boolean which indicates the success or fail of the instanciation process.
		*/
		bool instanciateAddon( const QString & ); // Create a full functionnal addon.
		/*!
		This function allow you to load an addon description (through the addon.xml file). 
		This means read the addon package's XML and that's all. There is no code execution in this function.
		Only the description is loaded.
		\param addonName the name of the addon to load.
		\returns the addon instance (a pointer to an AssumaAddonInstance object).
		*/
		AssumaAddonInstance loadAddon(const QString &); // Only load XML.
		/*!
		Destroy a previously instanciated addon. Return a boolean wich tells the caller if the destruction was successfull or not.
		\param addonName the name of the addon to destroy.
		\returns a boolean
		*/
		bool destroyAddon( const QString & );
		/*!
		Destroy all previously instanciated addons.
		*/
		void destroyAllAddons();
		/*!
		This function allow to set a property (with an attached object) for all instanciated addons.
		A property must be understood in the QtScript module's meaning.
		\param propName the name of the property.
		\param object the object to set.
		*/
		void setAddonObjectProperty(const QString &, QObject *);
		/*!
		Set a property (with an attached QObject) for a specific addon.
		A property must be understood in the QtScript module's meaning.
		\param addonName the name of the addon to set the property in.
		\param propName the name of the property.
		\param object the object to set.
		*/
		void setAddonObjectProperty(const QString &, const QString &, QObject *);
	public slots:
		/*!
		This slot try to instanciate all addons in the given directory.
		*/
		void instanciateAllAddons();
		
	signals:
		/*!
		This signal is emitted when the loading (or instanciation) of an addon failed.
		The name of the addon is sent as a parameter of this signal.
		*/
		void addonLoadingFailed( QString & );
		
	private:
		bool validateAddonData( const QMap<QString,QString> & );
		QMap<QString, QScriptEngine*> _engines;
		QMap<QString, QWidget*> _uis;
		QMap<QString, AssumaAddonInstance*> _addonInstances;
		QString _addonsDirectory;
};

#endif
