/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  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.
 */

#include "VpnItem.h"

#include <QFile>
#include <QMessageBox>

VpnItem::VpnItem(QListWidget *parent): QListWidgetItem(parent)
{
	mIsConnected = false;
	mIsLock = false;
	setIcon(QIcon(QString(":img/vpn_22.png")));
	setText(QObject::tr("[untitled VPN connection]"));
}

VpnItem::VpnItem(const QString &path, const QString &name, QListWidget *parent):
	QListWidgetItem(parent)
{
	mIsConnected = false;
	mIsLock = false;
	setIcon(QIcon(QString(":img/vpn_22.png")));
	setText(QObject::tr("[untitled VPN connection]"));
	setConfFilePath(path, name);
}

bool
VpnItem::isConnected()
{
	return mIsConnected;
}

void
VpnItem::setIsConnected(bool connected)
{
	mIsConnected = connected;
}

bool
VpnItem::isLock()
{
	return mIsLock;
}

void
VpnItem::setIsLock(bool lock)
{
	mIsLock = lock;
}

QString&
VpnItem::confName()
{
	return mConfName;
}

QString&
VpnItem::confFilePath()
{
	return mConfFilePath;
}

static QString
getOptionValue(const QString &line, QString option)
{
	QString value;
	int pos;
	
	if ((pos = line.indexOf(option)) == -1)
		return value;
	
	if ( (pos != 0) && (!line[pos - 1].isSpace()) && (line[pos - 1] != QChar(';')) 
		&& (line[pos - 1] != QChar('#')) )
		return value;
	
	pos += option.size();
	
	if ((pos < line.size()) && (!line[pos].isSpace()))
		return value;
	
	while (pos < line.size())
	{
		if (line[pos].isSpace()) pos++;
		else break;
	}
	
	value = line;
	value.remove(0, pos);
	
	return value;
}

void
VpnItem::setConfFilePath(const QString &path, const QString &name)
{
	mConfName = name;
	mConfFilePath = path;
	
	QFile file(mConfFilePath);
	file.open(QIODevice::ReadOnly);
	
	setText(QString("[") + mConfName + QString("]"));
	
	while (!file.atEnd())
	{
		QString line = QString().fromUtf8(file.readLine().data());
		QString value;
		
		line.chop(1);
		
		if ((value = getOptionValue(line, QString("sTokenVpnName"))) != QString())
		{
			mSTokenVpnName = value;
		}
		else if ((value = getOptionValue(line, QString("remote"))) != QString())
		{
			mRemoteVpnServer = value;
		}
		else if ((value = getOptionValue(line, QString("rport"))) != QString())
		{
			mRemoteVpnPort = value;
		}
		else if ((value = getOptionValue(line, QString("ca"))) != QString())
		{
			mCaCertFileName = value;
		}
		else if ((value = getOptionValue(line, QString("cert"))) != QString())
		{
			mUserCertFileName = value;
		}
		else if ((value = getOptionValue(line, QString("key"))) != QString())
		{
			mKey = value;
		}
	}
	
	setText(mSTokenVpnName);
}

QString&
VpnItem::sTokenVpnName()
{
	return mSTokenVpnName;
}

void
VpnItem::setSTokenVpnName(const QString &name)
{
	mSTokenVpnName = name;
}

QString&
VpnItem::remoteVpnServer()
{
	return mRemoteVpnServer;
}

void
VpnItem::setRemoteVpnServer(const QString &server)
{
	mRemoteVpnServer = server;
}

QString&
VpnItem::remoteVpnPort()
{
	return mRemoteVpnPort;
}

void
VpnItem::setRemoteVpnPort(const QString &port)
{
	mRemoteVpnPort = port;
}

QString&
VpnItem::caCertFileName()
{
	return mCaCertFileName;
}

void
VpnItem::setCaCertFileName(const QString &name)
{
	mCaCertFileName = name;
}

QString&
VpnItem::userCertFileName()
{
	return mUserCertFileName;
}

void
VpnItem::setUserCertFileName(const QString &name)
{
	mUserCertFileName = name;
}

QString&
VpnItem::key()
{
	return mKey;
}

void
VpnItem::setKey(const QString &name)
{
	mKey = name;
}
