/***************************************************************************
 begin                : Wed Mar 13 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 "kmfruleeditorlimit.h"

// qt includes
#include <qstring.h>
#include <qptrlist.h>
#include <qspinbox.h>
#include <qcombobox.h>
#include <qcheckbox.h>

// kde includes
#include <klocale.h>
#include <kdebug.h>
#include <kapplication.h>
#include <kmessagebox.h>

// Project includes
#include "../../core/iptruleoption.h"
#include "../../core/iptrule.h"
#include "../../core/iptchain.h"
#include "../../core/iptable.h"
#include "../../core/kmfdoc.h"
#include "../../core/kmfiptdoc.h"


KMFRuleEditorLimit::KMFRuleEditorLimit( QWidget *parent, const char *name, WFlags fl /*,IPTRule* cr*/ ) : KMyFirewallRuleEditorLimit( parent, name, fl ) {}
KMFRuleEditorLimit::~KMFRuleEditorLimit() {}
void KMFRuleEditorLimit::loadRule( IPTRule * rule ) {
	kdDebug() << "void KMFRuleEditorLimit::loadRule( IPTRule * rule )" << endl;
	m_rule = rule;
	c_use_limit->setChecked( false );
	c_limit->setChecked( false );
	sb_limit->setValue( 5 );
	cb_interval->setCurrentItem( 0 );
	c_burst->setChecked( false );
	readRuleConfig();
}

void KMFRuleEditorLimit::readRuleConfig() {

	IPTRuleOption *opt = 0;
	opt = m_rule->getOptionForName("limit_opt");
	if (opt) {
		QStringList vals = opt->getValues();
		QString limit = "";
		QString burst = "";
		limit = *vals.at(1);
		burst = *vals.at(2);
		if ( limit != "UNDEFINED" ) {
			c_use_limit->setChecked( true );
			c_limit->setChecked( true );
			int i = limit.find( "/" );
			QString str_rate = limit.left( i );
			kdDebug() << "Found Rate: " << str_rate << endl;
			int rate = str_rate.toInt();
			sb_limit->setValue( rate );
			QString interval = limit.right( limit.length() - i - 1 );
			kdDebug() << "Found Limit: " << str_rate << "/" << interval << endl;
			//cb_interval->setEnabled(true);
			if ( interval == "second" )
				cb_interval->setCurrentItem( 0 );
			if ( interval == "minute" )
				cb_interval->setCurrentItem( 1 );
			if ( interval == "hour" )
				cb_interval->setCurrentItem( 2 );
		}
		if ( burst != "UNDEFINED" ) {
			c_burst->setChecked( true );
			kdDebug() << "Found Burst: " << burst << endl;
			int rate = burst.toInt();
			sb_burst->setValue( rate );
		}
	}
}

void KMFRuleEditorLimit::slotOk() {
	m_rule->chain()->table()->kmfDoc()->startTransaction();
	m_rule->saveState();

	QPtrList<QString>* options = new QPtrList<QString>;
	QString* name = new QString( "limit_opt" );
	if ( c_use_limit->isChecked() ) {
		QString* opt = new QString( "" );
		if ( !c_limit->isChecked() && !c_burst->isChecked() ) {
			KMessageBox::sorry( this, i18n( "You cannot enable a limit without at least the rate." ), i18n( "Limit Matches" ) );
			m_rule->chain()->table()->kmfDoc()->endTransaction();
			return ;
		}
		if ( c_limit->isChecked() ) {
			QString str_rate = sb_limit->text();
			QString interval = cb_interval->currentText();
			opt->append( str_rate );
			opt->append( "/" );
			opt->append( interval );
			options->append( new QString( "bool:on" )  );
			options->append( opt );
			if ( c_burst->isChecked() ) {
				str_rate = sb_burst->text();
				QString* burst_rate = new QString( str_rate );
				options->append( burst_rate );
			} else {
				options->append( new QString( "bool:off" )  );
				options->append( new QString( "bool:off" )  );
			}
		} else {
			options->append( new QString( "bool:off" )  );
			options->append( new QString( "bool:off" )  );
		}
	}
	emit sigAddRuleOpt( name, options );
	m_rule->chain()->table()->kmfDoc()->endTransaction();
	emit sigHideMe();
	//	accept();
}

void KMFRuleEditorLimit::slotHelp() {
	kdDebug() << "void KMFRuleEditorLimit::slotHelp()" << endl;
	kapp->invokeHelp( "limit" );
}

void KMFRuleEditorLimit::reject() {
	kdDebug() << "void KMFRuleEditorLimit::reject()" << endl;
	emit sigHideMe();
}
#include "kmfruleeditorlimit.moc"
