/***************************************************************************
 *   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.             *
 ***************************************************************************/
#include "kalvachannellistprovider.h"
#include "generalsettingspage.h"
#include "settings.h"

#include <KLocale>
#include <KGlobal>
#include <KStandardDirs>
#include <KConfigDialog>
#include <KDebug>
#include <QtGui>
#include <QFile>

namespace KalvaChannellistProvider {

Plugin::Plugin()
	: m_channellist(new QList<KalvaData::Channel*>()),
	  m_PropertyLoader(new KalvaUtil::PropertyLoader(KalvaUtil::CHANNELLIST_TYPE))
{
}

Plugin::~Plugin()
{
	delete m_PropertyLoader;
	delete m_channellist;
}

/**
 * @see KalvaPlugin::PluginInterface
 */
QString Plugin::title()
{
    return i18n("Kalva channellist provider");
}

/**
 * @see KalvaPlugin::PluginInterface
 */
QString Plugin::toolTip()
{
    return i18n("Allows to use kalva channellists from http://www.kde-files.org/index.php?xcontentmode=655.");
}

/**
 * @see KalvaPlugin::PluginInterface
 */
QString Plugin::whatsThis()
{
    return i18n("The kalva channellist provider allows to import and use kalva channellists provided by other kalva users via http://www.kde-files.org/index.php?xcontentmode=655.");
}

/**
 * @see KalvaPlugin::PluginInterface
 */
void Plugin::settingsChanged()
{
   loadChannellist();
}

/**
 * @see KalvaPlugin::PluginInterface
 */
void Plugin::addToConfigDialog( KConfigDialog *dialog )
{
	kDebug() << "addToConfigDialog()" << endl;

	GeneralSettingsPage* generalSettings = new GeneralSettingsPage();
	dialog->addPage(generalSettings,
		KalvaChannellistProviderSettings::self(),
		i18n("Channellist settings"),
		"kalva_channellistprovider_generalsetting");
	connect(dialog, SIGNAL(settingsChanged(const QString&)), generalSettings, SLOT(loadSettings()));
}

/**
 *
 */
void Plugin::loadChannellist()
{
	QString chlstsDir = KGlobal::dirs()->locateLocal( "data", "kalva2/chlst/", true );
	QString filename = chlstsDir.append(KalvaChannellistProviderSettings::channellistName());
	kDebug() << i18n("channellist name is %1", filename);

	QFile file( filename );
	if (file.open(QIODevice::ReadOnly) == false)
	{
		kDebug() << "could not open file " << file.fileName() << "for reading";
		return;
	}
	QTextStream chlstFromFile(&file);
	QStringList chlst = chlstFromFile.readAll().split("\n",QString::SkipEmptyParts);
	file.close();

	QString str;
	KalvaData::Channel* channel = 0;

	m_channellist->clear();

	for ( QStringList::Iterator it = chlst.begin(); it != chlst.end(); ++it ) {

		str = *it;
		kDebug() << "str " << str;

		if (str.contains("[", Qt::CaseInsensitive))  {
			if (channel != 0) {
				kDebug() << "add channel with station=" << channel->station();
				m_channellist->append(channel);
			}
			str = str.remove("[");
			str = str.remove("]");
			kDebug() << "station=" << str;
			channel = new KalvaData::Channel();
			channel->setStation(str);
        	}
        	if (str.contains("channel=", Qt::CaseInsensitive))  {
			str = str.remove("channel=");
			kDebug() << "channel=" << str;
			channel->setChannel(str);
		}
		if (str.contains("freq=", Qt::CaseInsensitive))  {
			str = str.remove("freq=");
			kDebug() << "freq=" << str;
			channel->setFrequence(str);
		}
	}
	m_changed = false;
}

/**
 * @see KalvaPlugin::ChannellistProviderInterface
 */
void Plugin::setChannellist(const QList<KalvaData::Channel*> & list)
{
	*m_channellist = list;
}

/**
 * @see KalvaPlugin::ChannellistProviderInterface
 */
const QList<KalvaData::Channel*>& Plugin::getChannellist()
{
	kDebug() << "m_channellist.size=" << m_channellist->size();
	return *m_channellist;
}

/**
 * @see KalvaPlugin::ChannellistProviderInterface
 */
void Plugin::addChannel(KalvaData::Channel* channel)
{
	if (! (m_channellist->contains( channel )) ) {
		m_channellist->append( channel );
	}
}

/**
 * @see KalvaPlugin::ChannellistProviderInterface
 */
void Plugin::removeChannel(KalvaData::Channel* channel)
{
	if (m_channellist->contains( channel ) ) {
		m_channellist->removeOne( channel );
	}
}

}
Q_EXPORT_PLUGIN2( kalva2_kalva_channellist_provider, KalvaChannellistProvider::Plugin )

#include "kalvachannellistprovider.moc"
