/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   taki@Rivendell   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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 PLUGINMANAGER_H
#define PLUGINMANAGER_H
#include <libkalva_export.h>

#include "pluginfactory.h"

#include <QObject>
#include <QList>

class QTabWidget;
class QSplitter;
class QStackedWidget;
class KConfigDialog;

namespace KalvaPluginSystem {

/**
 * The PluginManager reads in all plugins from kalva's plugin path
 * and constructs a PluginFactory for each plugin. Plugins and their
 * factories are held by the manager and can be accessed through
 * PluginManager::plugins() and PluginManager::factories().
 *
 * @short PluginManager
 * @author Andreas Silberstorff <ml@andreas-silberstorff.de>
 * @version 1.0.91
 */

class KALVALIB_EXPORT PluginManager
 : public QObject
{
        Q_OBJECT
public:

	/**
	 * Get the instance of the manager.
	 * The manager is a singleton. If accessed the first time
	 * the instance is constructed.
	 */
	inline static PluginManager& instance() {
		static PluginManager instance;
		return instance;
	};

	/**
	 * Get a QList of all plugins found in the plugin paths.
	 * The list is read only once. If plugins are installed
	 * while kalva is loaded
	 * kalva has to be restarted to see new plugins.
	 */
	QList<QObject *> plugins();

	/**
	 * Get a QList of all factories.
	 */
	QList<KalvaPluginSystem::PluginFactory *> factories();

	/**
	 * Get a QList of perspectives (factories() filtered).
	 */
	QList<KalvaPluginSystem::PluginFactory *> perspectives();

	/**
	 * Get a QList of devices (factories() filtered).
	 */
	QList<KalvaPluginSystem::PluginFactory *> devices();

	/**
	 * Get a QList of schedulers (factories() filtered).
	 */
	QList<KalvaPluginSystem::PluginFactory *> schedulers();

	/**
	 * Get a QList of schedulers (factories() filtered).
	 */
	QList<KalvaPluginSystem::PluginFactory *> channellistproviders();

	KalvaPlugin::DeviceInterface* device(QString titel);
	KalvaPlugin::ChannellistProviderInterface* channellistprovider(QString titel);
	KalvaPlugin::SchedulerInterface* scheduler(QString titel);

	/**
	 * Adds the config pages of all enabled factories.
	 */
	void addToConfigDialog( KConfigDialog* dialog );

	/**
	 * Stores the tabWidget from kalva's mainview kalvaview.
	 * The pointer is needed for enabling and disabling perspectives.
	 */
	void setTabWidget( QTabWidget* tabWidget );

	/**
	 * Stores the mainViewStack of kalva's mainview kalvaview.
	 * The pointer is needed for enabling and disabling perspectives.
	 */
	void setMainViewStackedWidget( QStackedWidget* mainViewStackedWidget );

public slots:
	/**
	 * Informs all stored factories when the settings have changed.
	 */
	void settingsChanged();

private:
	inline explicit PluginManager() : QObject() {};
	inline ~PluginManager() {};

	QTabWidget* m_manager;
	QStackedWidget* m_mainViewStack;
	QList<QObject *> m_plugins;
	QList<KalvaPluginSystem::PluginFactory *> m_factories;
	QList<KalvaPluginSystem::PluginFactory *> m_perspectives;
	QList<KalvaPluginSystem::PluginFactory *> m_devices;
	QList<KalvaPluginSystem::PluginFactory *> m_schedulers;
	QList<KalvaPluginSystem::PluginFactory *> m_channellistproviders;

	/**
	 * Stores a plugin, constructs the factory for the plugin
	 * and stores both.
	 */
	void appendFactory( QObject * );

	/**
	 * Searches for plugins in the plugin directories, stores them,
	 * constructs the factories for the plugins and stores them.
	 */
	void findPlugins();
};

}

#endif
