/***************************************************************************
    Copyright (C) 2004 Christian Hubinger
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "kmfruleeditcustomopt.h"

// KDE includes
#include <kdebug.h>
#include <kapplication.h>

// QT includes
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qstring.h>
#include <qcheckbox.h>

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


KMFRuleEditCustomOpt::KMFRuleEditCustomOpt( QWidget *parent, const char *name ) : KMyFirewallRuleEditorCustomRule( parent, name ) {
	c_options->setChecked( false );
	c_target_options->setChecked( false );
	t_target->clear();
	t_target_options->clear();
	t_options->clear();
}

KMFRuleEditCustomOpt::~KMFRuleEditCustomOpt() {
}

void KMFRuleEditCustomOpt::loadRule( IPTRule * rule ) {
	kdDebug() << "void KMFRuleEditIP::loadRule( IPTRule * rule )" << endl;
	if ( rule == 0 )
		return ;

	m_rule = rule;
	t_target->clear();
	t_target->setText( m_rule->target() );
	t_target_options->clear();
	t_options->clear();
	IPTRuleOption *opt;
	QStringList vals;
	QString val = "";

	opt  = m_rule->getOptionForName("custom_opt");
	vals = opt->getValues();
	val = *vals.at(0);
	if ( val != "UNDEFINED" && !val.isEmpty() && val != "bool:off" ) {
		c_options->setChecked( true );
		t_options->setText( val );
	}
	vals.clear();
	val = "";

	opt  = m_rule->getOptionForName("target_custom_opt");
	vals = opt->getValues();
	val = *vals.at(0);
	if ( val != "UNDEFINED" && !val.isEmpty() && val != "bool:off" ) {
		c_target_options->setChecked( true );
		t_target_options->setText( val );
	}
	return ;
}

void KMFRuleEditCustomOpt::accept() {
	m_rule->chain()->table()->kmfDoc()->startTransaction();
	m_rule->saveState();
	QString target = "";
	target = t_target->text();
	QString option = "";
	if ( c_options->isChecked() )
		option = t_options->text();
	QString target_option = "";
	if ( c_target_options->isChecked() )
		target_option = t_target_options->text();
	QString* op = new QString( "custom_opt" );
	QString *tg_opt = new QString( "target_custom_opt" );
	QPtrList<QString>* values = new QPtrList<QString>;

	kdDebug() << "target: " << target << endl;
	kdDebug() << "option: " << option << endl;
	kdDebug() << "target_option: " << target_option << endl;
	if ( !c_options->isChecked() && ! c_target_options->isChecked() ) {
		m_rule->setCustomRule( false );
		emit sigAddRuleOpt( op, values );
		emit sigAddRuleOpt( tg_opt, values );
		m_rule->chain()->table()->kmfDoc()->endTransaction();
		emit sigHideMe();
		return ;
	}

	m_rule->setCustomRule( true );
	if ( !target.isEmpty() )
		m_rule->setTarget( *( new QString( target ) ) );

	if ( !option.isEmpty() ) {
		values->append( new QString( option ) );
	} else {
		values->append( new QString( "bool:off" ) );
	}

	emit sigAddRuleOpt( op, values );
	values->clear();

	if ( !target_option.isEmpty() ) {
		values->append( new QString( target_option ) );
	} else {
		values->append( new QString( "bool:off" ) );
	}
	emit sigAddTargetOpt( tg_opt, values );

	m_rule->chain()->table()->kmfDoc()->endTransaction();
	emit sigHideMe();
}

void KMFRuleEditCustomOpt::slotHelp() {
	kdDebug() << "void KMFRuleEditIP::slotHelp()" << endl;
	kapp->invokeHelp( "custom_rules" );
}

void KMFRuleEditCustomOpt::reject() {
	kdDebug() << "void KMFRuleEditCustomOpt::reject()" << endl;
	emit sigHideMe();
}



#include "kmfruleeditcustomopt.moc"
