/***************************************************************************
 *
 * knetworkmanager-vpnc.cpp - A NetworkManager frontend for KDE 
 *
 * Copyright (C) 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <hschaa@suse.de>, <Helmut.Schaa@gmx.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-vpnc.h"

typedef KGenericFactory<VPNCPlugin> VPNCPluginFactory;
K_EXPORT_COMPONENT_FACTORY( knetworkmanager_vpnc, VPNCPluginFactory("knetworkmanager_vpnc"));

#define NAT_MODE_CISCO 0
#define NAT_MODE_NATT 1
#define NAT_MODE_DISABLED 2


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

VPNCPlugin::~VPNCPlugin()
{

}

VPNConfigWidget* VPNCPlugin::CreateConfigWidget(QWidget* parent)
{
	return new VPNCConfig(parent);
}

VPNAuthenticationWidget* VPNCPlugin::CreateAuthenticationWidget(QWidget* parent)
{
	return new VPNCAuthentication(parent);
}


VPNCConfig::VPNCConfig(QWidget* parent)
	: VPNConfigWidget(parent)
{
	QVBoxLayout* layout = new QVBoxLayout(this, 1, 1);
	_vpncWidget = new VPNCConfigWidget(this);
	layout->addWidget(_vpncWidget);
	
	connect(_vpncWidget->chkOverrideUsername, SIGNAL(toggled(bool)), _vpncWidget->Xauth_username, SLOT(setEnabled(bool)));
	connect(_vpncWidget->chkOverrideUsername, SIGNAL(toggled(bool)), _vpncWidget->lblUsername, SLOT(setEnabled(bool)));

	connect(_vpncWidget->chkUseDomain, SIGNAL(toggled(bool)), _vpncWidget->Domain, SLOT(setEnabled(bool)));
	connect(_vpncWidget->chkUseDomain, SIGNAL(toggled(bool)), _vpncWidget->lblDomain, SLOT(setEnabled(bool)));

	connect(_vpncWidget->chkIPAdresses, SIGNAL(toggled(bool)), _vpncWidget->routes, SLOT(setEnabled(bool)));

	connect(_vpncWidget->chkUseKeepAlive, SIGNAL(toggled(bool)), _vpncWidget->nat_keep_alive_interval, SLOT(setEnabled(bool)));
	connect(_vpncWidget->chkUseKeepAlive, SIGNAL(toggled(bool)), _vpncWidget->lblKeepAliveInterval, SLOT(setEnabled(bool)));

	_vpncWidget->cboNAT->insertItem(i18n("Cisco UDP (default)"), NAT_MODE_CISCO);
	_vpncWidget->cboNAT->insertItem(i18n("NAT-T"), NAT_MODE_NATT);
	_vpncWidget->cboNAT->insertItem(i18n("Disabled"), NAT_MODE_DISABLED);

	this->languageChange();
}

VPNCConfig::~VPNCConfig()
{

}

void VPNCConfig::languageChange()
{

}

void VPNCConfig::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 == "IPSec gateway")
		{
			_vpncWidget->IPSec_gateway->setText(value);
		}
		else if (entry == "IPSec ID")
		{
			_vpncWidget->IPSec_ID->setText(value);
		}
		else if (entry == "Xauth username")
		{
			_vpncWidget->Xauth_username->setText(value);
			_vpncWidget->chkOverrideUsername->setChecked(true);
		}
		else if (entry == "Domain")
		{
			_vpncWidget->Domain->setText(value);
			_vpncWidget->chkUseDomain->setChecked(true);
		}
		else if (entry == "NAT-Keepalive packet interval")
		{
			_vpncWidget->nat_keep_alive_interval->setText(value);
			_vpncWidget->chkUseKeepAlive->setChecked(true);
		}
		// backwards compatibility
		else if (entry == "Disable NAT Traversal")
		{
			_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_DISABLED);
		}
		else if (entry == "NAT Traversal Mode")
		{
			if (value == "natt")
				_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_NATT);
			else if (value == "cisco-udp")
				_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_CISCO);
			else if (value == "none")
				_vpncWidget->cboNAT->setCurrentItem(NAT_MODE_DISABLED);
			else
			{
				// FIXME: unknown NAT mode
			}
		}
		else if (entry == "Enable Single DES")
		{
			_vpncWidget->chkSingleDES->setChecked(value == "yes" || value == "true");
		}
	}

	// set routes
	if (!routes.empty())
	{
		_vpncWidget->chkIPAdresses->setChecked(true);
		_vpncWidget->routes->setText(routes.join(" "));
	}
}

QMap<QString, QString> VPNCConfig::getVPNProperties()
{
	// build a StingList of properties
	QMap<QString, QString> strlist;
	strlist.insert("IPSec gateway", QString(_vpncWidget->IPSec_gateway->text()));
	strlist.insert("IPSec ID", QString(_vpncWidget->IPSec_ID->text()));

	if (_vpncWidget->chkOverrideUsername->isChecked())
		strlist.insert("Xauth username", QString(_vpncWidget->Xauth_username->text()));

	if (_vpncWidget->chkUseDomain->isChecked())
		strlist.insert("Domain", QString(_vpncWidget->Domain->text()));
	
	if (_vpncWidget->chkUseKeepAlive->isChecked())
		strlist.insert("NAT-Keepalive packet interval", QString(_vpncWidget->nat_keep_alive_interval->text()));

	switch (_vpncWidget->cboNAT->currentItem())
	{
		case NAT_MODE_CISCO:
			strlist.insert("NAT Traversal Mode", "cisco-udp");
			break;
		case NAT_MODE_NATT:
			strlist.insert("NAT Traversal Mode", "natt");
			break;
		case NAT_MODE_DISABLED:
		default:
			strlist.insert("NAT Traversal Mode", "none");
			break;
	}

	if (_vpncWidget->chkSingleDES->isChecked())
		strlist.insert("Enable Single DES", "yes");
	else
		strlist.insert("Enable Single DES", "no");

	return strlist;
}

QStringList VPNCConfig::getVPNRoutes()
{
	QStringList strlist;
	if(_vpncWidget->chkIPAdresses->isChecked())
	{
		strlist = QStringList::split(" ", _vpncWidget->routes->text());
	}
	return strlist;

}

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

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

VPNCAuthentication::VPNCAuthentication(QWidget* parent, char* name)
	: VPNAuthenticationWidget(parent, name)
{
	QVBoxLayout* layout = new QVBoxLayout(this, 1, 1);
	_vpncAuth = new VPNCAuthenticationWidget(this);
	layout->addWidget(_vpncAuth);
}

VPNCAuthentication::~VPNCAuthentication()
{

}

QMap<QString, QString> VPNCAuthentication::getPasswords()
{
	QMap<QString, QString> pwds;
	pwds.insert("IPSec secret", QString(_vpncAuth->editGroupPassword->password()));
	pwds.insert("Xauth password", QString(_vpncAuth->editUserPassword->password()));
	return pwds;
}
