/***************************************************************************
 *   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 <QStringList>

#include <klocale.h>
#include <KDebug>
#include <KDirWatch>
#include <KStandardDirs>
#include <KFileDialog>
#include <KIO/CopyJob>
#include <KIcon>
#include <knewstuff2/core/entry.h>
#include <knewstuff2/engine.h>

namespace KalvaChannellistProvider {

GeneralSettingsPage::GeneralSettingsPage()
	: QWidget(),
	  m_PropertyLoader(new KalvaUtil::PropertyLoader(KalvaUtil::CHANNELLIST_TYPE))
{
	_generalsettings_ui.setupUi( this );
	_generalsettings_ui.importButton->setIcon(KIcon("document-open"));
	_generalsettings_ui.saveButton->setIcon(KIcon("document-save"));
	_generalsettings_ui.downloadButton->setIcon(KIcon("get-hot-new-stuff"));

	QString chlstsDir = KGlobal::dirs()->locateLocal( "data", "kalva2/chlst/", true );
	dirWatch.addDir( chlstsDir, KDirWatch::WatchFiles);
	connect(&dirWatch, SIGNAL(dirty(const QString & ) ),
           SLOT( fillStoredChannellistsList() ) );
	fillStoredChannellistsList();
	connect (_generalsettings_ui.importButton, SIGNAL( clicked(bool) ),
		 this, SLOT( importFile() ) );
	connect (_generalsettings_ui.downloadButton, SIGNAL( clicked(bool) ),
		 this, SLOT( getNewStuff() ) );
}

GeneralSettingsPage::~GeneralSettingsPage()
{
}

void GeneralSettingsPage::fillStoredChannellistsList()
{
	kDebug() << "fillStoredChannellistsList ";

	_generalsettings_ui.mStoredChannellistsList->clear();
	QStringList filenames = m_PropertyLoader->readDir();
	_generalsettings_ui.mStoredChannellistsList->addItems( filenames );
}

void GeneralSettingsPage::writeSettings()
{
}

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

void GeneralSettingsPage::getNewStuff()
{
	KNS::Engine *engine = new KNS::Engine();
	if (engine->init("kalvachlst.knsrc")) {
		KNS::Entry::List entries = engine->downloadDialogModal(this);
 
// 		kDebug() << "scripts status:" << endl;
// 		foreach ( KNS::Entry* entry, entries )
// 		{
// 			if ( entry->status() == KNS::Entry::Downloadable )
// 			kDebug() << "channellist downloadable!" << endl;
// 		}
		delete engine;
	}
}

void GeneralSettingsPage::importFile()
{
	QString filename = KFileDialog::getOpenFileName();
	kDebug() << i18n("file %1 selected for import", filename);
	if ( filename != 0 ) {
		KUrl fromFile = KUrl(filename);
		int indexOfSeparator = filename.lastIndexOf( QDir::separator() );
		int basenameLength = filename.size() - indexOfSeparator;
// 		kDebug() << "last index of separator = " <<indexOfSeparator;
// 		kDebug() << "basename length = " << basenameLength;
		QString basename = filename.right( basenameLength );
// 		kDebug() << "basename is " << basename;
		KUrl toFile = KUrl(m_PropertyLoader->getDir() + basename);
		KIO::copy(fromFile, toFile);
	}
}
}

#include "generalsettingspage.moc"
