/***************************************************************************
begin                : Sun May 26 2002
copyright            : (C) 2002 by Christian Hubinger
email                : chubinger@gmail.com
***************************************************************************/

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

#include "kmfruleeditortargetnat.h"

// QT includes
#include <qstring.h>
#include <qspinbox.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qmessagebox.h>

// KDE include
#include <kdebug.h>
#include <klocale.h>
#include <kapplication.h>
#include <kmessagebox.h>

// MY includes
#include "../../core/iptrule.h"
#include "../../core/iptchain.h"
#include "../../core/iptable.h"
#include "../../core/iptruleoption.h"
#include "../../core/kmfdoc.h"
#include "../../core/kmfiptdoc.h"
#include "../../core/kmferror.h"
#include "../../core/kmfcheckinput.h"
#include "../../core/kmferrorhandler.h"

KMFRuleEditorTargetNat::KMFRuleEditorTargetNat( QWidget *parent, const char *name, WFlags fl ) : KMyFirewallRuleEditorTargetNat( parent, name, fl ) {
	m_CheckInput = new KMFCheckInput();
	m_ErrorHandler = new KMFErrorHandler( "KMFRuleEditProtocol" );
	m_err = new KMFError() ;

}



KMFRuleEditorTargetNat::~KMFRuleEditorTargetNat() {}

void KMFRuleEditorTargetNat::loadRule( IPTRule *rule ) {
	c_use_ports->setChecked( false );
	c_use_port_range->setChecked( false );
	c_ip_range->setChecked( false );
	t_ip1->clear();
	t_ip2->clear();
	sb_port1->setValue(1);
	sb_port2->setValue(2);
	m_rule = rule;

	kdDebug() << "KMFRuleEditorTargetNat::loadRule(IPTRule *rule)" << endl;
	m_rule = rule;
	bool has_port = false;
	IPTRuleOption *opt = m_rule->getOptionForName( "tcp_opt" );
	if ( ! opt->isEmpty() )
		has_port = true;
	if ( ! has_port ) {
		opt = m_rule->getOptionForName( "udp_opt" );
		if ( ! opt->isEmpty() )
			has_port = true;
	}
	if ( ! has_port ) {
		opt = m_rule->getOptionForName( "tcp_multiport_opt" );
		if ( ! opt->isEmpty() )
			has_port = true;
	}
	if ( ! has_port ) {
		opt = m_rule->getOptionForName( "udp_multiport_opt" );
		if ( ! opt->isEmpty() )
			has_port = true;
	}
	c_use_ports->setEnabled( has_port );

	if ( mbsnat )
		opt = m_rule->getOptionForName( "target_snat_opt" );
	else
		opt = m_rule->getOptionForName( "target_dnat_opt" );

	QStringList vals;
	vals = opt->getValues();
	QString arg = "";
	arg = *vals.at( 0 );
	kdDebug() << "Found option String: " << arg << endl;
	if ( arg != "UNDEFINED" && !arg.isEmpty() && arg != "bool:off" ) {
		kdDebug() << "Found option String: " << arg << endl;
		QString ips = "";
		QString ports = "";
		QString ip1 = "";
		QString ip2 = "";
		QString port1 = "";
		QString port2 = "";
		if ( arg.contains( ":" ) && has_port ) {
			c_use_ports->setChecked( true );
			int del = arg.find( ':' );
			ips = arg.left( del );
			kdDebug() << "IP arg: " << ips << endl;
			ports = arg.right( arg.length() - del - 1 );
			kdDebug() << "Port arg: " << ports << endl;
			if ( ips.contains( "-" ) ) {
				c_ip_range->setChecked( true );
				int delim = ips.find( '-' );

				ip1 = ips.left( delim );
				kdDebug() << "IP arg1: " << ip1 << endl;
				t_ip1 ->setText( ip1 );

				ip2 = ips.right( ips.length() - delim - 1 );
				kdDebug() << "IP arg2: " << ip2 << endl;
				t_ip2 ->setText( ip2 );
			} else {
				c_ip_range->setChecked( false );
				t_ip1 ->setText( ips );
			}
			if ( ports.contains( "-" ) ) {
				c_use_port_range->setChecked( true );

				int delim = ports.find( '-' );
				port1 = ports.left( delim );
				kdDebug() << "Port arg1: " << port1 << endl;
				;
				int int_port1 = port1.toInt();
				sb_port1 ->setValue( int_port1 );

				port2 = ports.right( ports.length() - delim - 1 );
				kdDebug() << "Port arg2: " << port2 << endl;
				;
				int int_port2 = port2.toInt();
				sb_port2 ->setValue( int_port2 );

			} else {
				c_use_port_range->setChecked( false );
				kdDebug() << "Port arg: " << ports << endl;
				;
				int int_port1 = ports.toInt();
				sb_port1 ->setValue( int_port1 );
			}
		} else if ( arg.contains( "-" ) ) {
			c_use_ports->setChecked( false );
			c_ip_range->setChecked( true );
			int delim = arg.find( '-' );
			ip1 = arg.left( delim );
			kdDebug() << "IP arg1: " << ip1 << endl;
			t_ip1 ->setText( ip1 );
			ip2 = arg.right( arg.length() - delim - 1 );
			kdDebug() << "IP arg2: " << ip2 << endl;
			t_ip2 ->setText( ip2 );
		} else if ( ! arg.isEmpty() ){
			c_use_ports->setChecked( false );
			c_ip_range->setChecked( false );
			ip1 = arg;
			t_ip1 ->setText( ip1 );
		} else {
			c_use_ports->setChecked( false );
			c_ip_range->setChecked( false );
			t_ip1 ->clear();
			t_ip2 ->clear();
		}
	}
}


void KMFRuleEditorTargetNat::accept() {
	kdDebug() << "KMFRuleEditorTargetNat::accept()";
	m_rule->chain()->table()->kmfDoc()->startTransaction();
	m_rule->saveState();

	bool ip_range = c_ip_range->isChecked();
	bool use_ports = c_use_ports->isChecked();
	bool use_port_range = c_use_port_range->isChecked();
	QString ip1, ip2, port1, port2, arg_ip, arg_port;

	if ( ip_range ) {
		ip1 = t_ip1->text();
		ip2 = t_ip2->text();
		if ( ip1.isEmpty() ) {
			KMessageBox::sorry(this,i18n("You need as a minimum to define an IP address to make NAT working."),
												i18n("Argument not Valid"));
				m_rule->chain()->table()->kmfDoc()->endTransaction();
				return;
		}
		if ( ip2.isEmpty() ) {
			KMessageBox::sorry(this,i18n("Please also set the upper boundary for your address range.."),
												i18n("Argument not Valid"));
				m_rule->chain()->table()->kmfDoc()->endTransaction();
				return;
		}

		// Sanity checks +++++++++++++++++
		m_CheckInput->checkInput( ip1, "IP", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
				m_rule->chain()->table()->kmfDoc()->endTransaction();
			return ;
		}
		m_CheckInput->checkInput( ip2, "IP", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
				m_rule->chain()->table()->kmfDoc()->endTransaction();
			return ;
		}
		// ++++++++++++++++++++++++++++++++
		arg_ip = ip1 + "-" + ip2;
		kdDebug() << "IP Argument: " << arg_ip;
	} else {
		ip1 = t_ip1->text();
		// Sanity checks +++++++++++++++++
		m_CheckInput->checkInput( ip1, "IP", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
				m_rule->chain()->table()->kmfDoc()->endTransaction();
			return ;
		}
		// ++++++++++++++++++++++++++++++++
		arg_ip = ip1;
		kdDebug() << "IP Argument: " << arg_ip;
	}

	if ( use_ports && c_use_ports->isEnabled() ) {
		if ( use_port_range ) {
			port1 = sb_port1->text();
			port2 = sb_port2->text();
			// Sanity checks +++++++++++++++++
			m_CheckInput->checkInput( port1, "PORT", m_err );
			if ( !m_ErrorHandler->showError( m_err ) ) {
				m_rule->chain()->table()->kmfDoc()->endTransaction();
				return ;
			}
			m_CheckInput->checkInput( port2, "PORT", m_err );
			if ( !m_ErrorHandler->showError( m_err ) ) {
				m_rule->chain()->table()->kmfDoc()->endTransaction();
				return ;
			}
			// ++++++++++++++++++++++++++++++++
			arg_port = port1 + "-" + port2;
			kdDebug() << "Port Argument: " << arg_port;
		} else {
			port1 = sb_port1->text();
			// Sanity checks +++++++++++++++++
			m_CheckInput->checkInput( port1, "PORT", m_err );
			if ( !m_ErrorHandler->showError( m_err ) ) {
				m_rule->chain()->table()->kmfDoc()->endTransaction();
				return ;
			}
			// ++++++++++++++++++++++++++++++++

			arg_port = port1;
			kdDebug() << "Port Argument: " << arg_port;
		}
	}

	if ( mbsnat ) {
		QPtrList<QString>* options = new QPtrList<QString>;
		QString* name = new QString( "target_snat_opt" );
		QString val = arg_ip;
		QString arg = "";
		if ( !val.isEmpty() ) {
			arg.append( val );
		} else {
			KMessageBox::sorry( this, i18n( "Unable to work without values.\n"
			                             "Please specify at least an IP address." ) ,
										  i18n( "Nat Options" ) );
				m_rule->chain()->table()->kmfDoc()->endTransaction();
			return ;
		}

		QString val2 = arg_port;
		if ( !val2.isEmpty() ) {
			arg.append( ":" );
			arg.append( val2 );
		}
		options->append( new QString( arg ) );
		emit sigAddTargetOpt( name, options );

	} else {
		QPtrList<QString>* options = new QPtrList<QString>;
		QString* name = new QString( "target_dnat_opt" );
		QString val = arg_ip;
		QString arg = "";
		if ( !val.isEmpty() ) {
			arg.append( val );
		} else {
			KMessageBox::sorry( this, i18n( "Unable to work without values.\n"
			                             "Please specify at least an IP address." ) ,
										  i18n( "Nat Options" ) );
			m_rule->chain()->table()->kmfDoc()->endTransaction();
			return ;
		}
		QString val2 = arg_port;
		if ( !val2.isEmpty() ) {
			arg.append( ":" );
			arg.append( val2 );
		}
		options->append( new QString( arg ) );
		emit sigAddTargetOpt( name, options );
	}

	m_rule->chain()->table()->kmfDoc()->endTransaction();
	emit sigDocumentChanged();
}
void KMFRuleEditorTargetNat::slotHelp() {
	kdDebug() << "void KMFRuleEditorTargetNat::slotHelp()" << endl;
	kapp->invokeHelp( "targets" );
}
void KMFRuleEditorTargetNat::reject() {
	kdDebug() << "void KMFRuleEditorTargetNat::reject()" << endl;
	loadRule(m_rule);
	emit sigHideMe();
}

#include "kmfruleeditortargetnat.moc"
