/***************************************************************************
 *
 * knetworkmanager-strongswan.cpp - A NetworkManager frontend for KDE 
 *
 * Author: Thomas Kallenberg <tkallenb@hsr.ch>, <thomas@no-more-secrets.ch>
 * 
 * Strongly based on the Code of Helmut Schaa <hschaa@suse.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 <klocale.h>
#include <qmessagebox.h>
#include <qbutton.h>
#include <kcombobox.h>
#include <klineedit.h>
#include <kurlrequester.h>
#include <qobjectlist.h>
#include <qobject.h>
#include <qcheckbox.h>
#include <kpassdlg.h>
#include <kgenericfactory.h>
#include <qlabel.h>

#include "knetworkmanager-strongswan.h"

typedef KGenericFactory<StrongswanPlugin> StrongswanPluginFactory;
K_EXPORT_COMPONENT_FACTORY( knetworkmanager_strongswan, StrongswanPluginFactory("knetworkmanager_strongswan"));


StrongswanPlugin::StrongswanPlugin(QObject* parent, const char* name, const QStringList& args) 
	: VPNPlugin(parent, name, args)
{
	KLocale* loc = KGlobal::locale();
	loc->insertCatalogue("NetworkManager-strongswan");
}

StrongswanPlugin::~StrongswanPlugin()
{

}

VPNConfigWidget* StrongswanPlugin::CreateConfigWidget(QWidget* parent)
{
	return new StrongswanConfig(parent);
}

VPNAuthenticationWidget* StrongswanPlugin::CreateAuthenticationWidget(QWidget* parent)
{
	return new StrongswanAuthentication(parent);
}


StrongswanConfig::StrongswanConfig(QWidget* parent)
	: VPNConfigWidget(parent)
{
	QVBoxLayout* layout = new QVBoxLayout(this, 1, 1);
	_strongswanWidget = new StrongswanConfigWidget(this);
	layout->addWidget(_strongswanWidget);
	
	/* TODO not sure if we need this here */
	this->languageChange();
}

StrongswanConfig::~StrongswanConfig()
{

}

void StrongswanConfig::languageChange()
{

}

	//	usercert agent password userkey
StrongswanConnectionType::CONNECTIONTYPE StrongswanConnectionType::mapString2ConnectionType(int prop)
{
    if (prop == 0)
        return psk;
    else if (prop == 1)
        return key;
    else if (prop == 2)
        return agent;
    return UNKNOWN;
}

int StrongswanConnectionType::mapConnectionType2String(CONNECTIONTYPE connType)
{
    switch(connType)
    {
        case psk:
            return 0;
        case key:
            return 1;
        case agent:
            return 2;
        default:
            return -1;
    }
    return -1;
}

void StrongswanConfig::setVPNData(const QStringList& routes, const QMap<QString, QString>& properties)
{
	// fill up our inputfields (only textfields atm)
	for(QMap<QString, QString>::ConstIterator it = properties.begin(); it != properties.end(); ++it)
	{
		QString entry = it.key();
		QString value = it.data();

		if (entry == "gateway")
		{
			_strongswanWidget->gateway->setText(value);
		}
		else if (entry == "certificate")
		{
			_strongswanWidget->certificate->setURL(value);
		}
		else if (entry == "username")
		{
			_strongswanWidget->username->setText(value);
		}
		else if (entry == "method")
		{
            StrongswanConnectionType::CONNECTIONTYPE type = StrongswanConnectionType::mapString2ConnectionType(value.toInt());
			_strongswanWidget->authtype->setCurrentItem(type);
		}
		// Options
		else if (entry == "chkUDPenc")
		{
			_strongswanWidget->chkUDPenc->setChecked(value == "true");
		}
		else if (entry == "chkIPcomp")
		{
			_strongswanWidget->chkIPcomp->setChecked(value == "true");
		}
		else if (entry == "chkIPinner")
		{
			_strongswanWidget->chkIPinner->setChecked(value == "true");
		}
	}
}

QMap<QString, QString> StrongswanConfig::getVPNProperties()
{
	// build a StingList of properties
	QMap<QString, QString> strlist;

	strlist.insert("gateway", QString(_strongswanWidget->gateway->text()));
	strlist.insert("certificate", QString(_strongswanWidget->certificate->url()));
	strlist.insert("username", QString(_strongswanWidget->username->text()));
	strlist.insert("method", 
		QString::number(StrongswanConnectionType::mapConnectionType2String((StrongswanConnectionType::CONNECTIONTYPE)_strongswanWidget->authtype->currentItem())));

	if (_strongswanWidget->chkUDPenc->isChecked())
		strlist.insert("encap", QString("yes"));

	if (_strongswanWidget->chkIPcomp->isChecked())
		strlist.insert("ipcomp", QString("yes"));
	
	if (_strongswanWidget->chkIPinner->isChecked())
		strlist.insert("virtual", QString("yes"));

	return strlist;
}

QStringList StrongswanConfig::getVPNRoutes()
{
	QStringList strlist;
	/*if(_strongswanWidget->chkIPAdresses->isChecked())
	{
		strlist = QStringList::split(" ", _strongswanWidget->routes->text());
	}
	*/
	return strlist;

}

bool StrongswanConfig::hasChanged()
{
	return true;
}

bool StrongswanConfig::isValid(QStringList& err_msg)
{
	bool retval = true;
	if(_strongswanWidget->gateway->text() == "" || _strongswanWidget->username->text() == "")
	{
		err_msg.append(i18n("At least the gateway and group has to be supplied."));
		retval = false;
	}
	return retval;
}

StrongswanAuthentication::StrongswanAuthentication(QWidget* parent, char* name)
	: VPNAuthenticationWidget(parent, name)
{
	QVBoxLayout* layout = new QVBoxLayout(this, 1, 1);
	_strongswanAuth = new StrongswanAuthenticationWidget(this);
	layout->addWidget(_strongswanAuth);
}

StrongswanAuthentication::~StrongswanAuthentication()
{

}

QMap<QString, QString> StrongswanAuthentication::getPasswords()
{
	QMap<QString, QString> pwds;
	pwds.insert("user", QString(_strongswanAuth->username->text()));
	pwds.insert("password", QString(_strongswanAuth->password->password()));
	return pwds;
}
