/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   ml@andreas-silberstorff.de   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#include "generalsettingspage.h"
#include "settings.h"

#include <PluginFactory>
#include <PluginManager>

#include <QtGui/QWidget>
#include <QFormLayout>
#include <QComboBox>
#include <QLineEdit>
#include <QSettings>
#include <QVariant>


#include <klocale.h>
#include <KDebug>

namespace TvPerspective {

GeneralSettingsPage::GeneralSettingsPage() : QWidget()
{
	_generalsettings_ui.setupUi(this);
	fillDeviceList();
	fillChannellistProviderList();
}

GeneralSettingsPage::~GeneralSettingsPage()
{
}

void GeneralSettingsPage::fillDeviceList()
{
	QString devicedriverName = TvPerspectiveSettings::devicedriver();
	kDebug() << "devicedriver = " << devicedriverName;

	QList<KalvaPluginSystem::PluginFactory *> factories =
		KalvaPluginSystem::PluginManager::instance().devices();

	_generalsettings_ui.mDevicedriver->clear();
        for (int id = 0; id < factories.size(); ++id)
	{
		KalvaPluginSystem::PluginFactory* factory = factories.at(id);
		if (factory->isEnabled())
		{
			KalvaPlugin::PluginInterface* plugin = factory->plugin();
			_generalsettings_ui.mDevicedriver->insertItem(
				id, plugin->title(), QVariant::fromValue(factory));
		}
	}
	_generalsettings_ui.mDevicedriver->setCurrentItem( devicedriverName );
}

void GeneralSettingsPage::fillChannellistProviderList()
{
	QString channellistProviderName = TvPerspectiveSettings::channellistprovider();
	kDebug() << "channellistProvider = " << channellistProviderName;

	QList<KalvaPluginSystem::PluginFactory *> factories =
		KalvaPluginSystem::PluginManager::instance().channellistproviders();

	_generalsettings_ui.mChannellistProvider->clear();
        for (int id = 0; id < factories.size(); ++id)
	{
		KalvaPluginSystem::PluginFactory* factory = factories.at(id);
		if (factory->isEnabled())
		{
			KalvaPlugin::PluginInterface* plugin = factory->plugin();
			_generalsettings_ui.mChannellistProvider->insertItem(
				id, plugin->title(), QVariant::fromValue(factory));
		}
	}
	_generalsettings_ui.mChannellistProvider->setCurrentItem( channellistProviderName );
}

void GeneralSettingsPage::writeSettings()
{
}

void GeneralSettingsPage::loadSettings()
{
	fillDeviceList();
}

}

#include "generalsettingspage.moc"
