/***************************************************************************
 begin                : Tue Mar 5 2002
 copyright            : (C) 2002 by 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.                                   *
 *                                                                         *
 ***************************************************************************/ 
//MY includes
#include "kmfruleeditorinterface.h"

// QT includes
#include <qstring.h>
#include <qstringlist.h>
#include <qcheckbox.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qgroupbox.h> 
// KDE includes
#include <kcombobox.h>
#include <kurl.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kdebug.h> 
// STD includes
// #include <iostream> 
// project include
#include "../../core/iptrule.h"
#include "../../core/iptchain.h"
#include "../../core/iptable.h"
#include "../../core/kmfdoc.h"
#include "../../core/kmfiptdoc.h"
#include "../../core/kmfconfig.h"

KMFRuleEditorInterface::KMFRuleEditorInterface( QWidget *parent, const char *name, WFlags fl /*,IPTRule* cr */ ) : KMyFirewallRuleEditorInterface( parent, name, fl ) {
}
KMFRuleEditorInterface::~KMFRuleEditorInterface() {}

void KMFRuleEditorInterface::loadRule( IPTRule * rule ) {
	kdDebug() << "void  KMFRuleEditorInterface::loadRule( IPTRule * rule )" << endl;
	slotReadConfig();
	c_in_int->setChecked( false );
	c_inv_in_int->setChecked( false );
	cb_in_int->setCurrentItem( 0 );
	c_out_int->setChecked( false );
	c_inv_out_int->setChecked( false );
	cb_out_int->setCurrentItem( 0 );
	m_rule = rule;

	IPTRuleOption* opt = 0;
	opt = m_rule->getOptionForName("interface_opt");
	if ( opt ) {
		QStringList vals = opt->getValues();
		QString in = *vals.at(0);
		QString out = *vals.at(1);
		if ( ! in.isEmpty() &&  in != "UNDEFINED" && in != "bool:off" ) {
			c_in_int->setChecked( true );
			if ( in.startsWith( "! " ) ) {
					c_inv_in_int->setChecked( true );
					cb_in_int->setEditText( in.right( in.length() - 2 ) );
			} else {
			cb_in_int->setEditText( in );
			}
		} else {
			c_in_int->setChecked( false );
		}
		if ( ! out.isEmpty() &&  out != "UNDEFINED" && out != "bool:off" ) {
			c_out_int->setChecked( true );
			if ( out.startsWith( "! " ) ) {
					c_inv_out_int->setChecked( true );
					cb_out_int->setEditText( out.right( out.length() - 2 ) );
			} else {
			cb_out_int->setEditText( out );
			}
		} else {
			c_out_int->setChecked( false );
		}

	}
	gb_in_int->setEnabled( true );
	gb_out_int->setEnabled( true );
}

/** No descriptions */
void KMFRuleEditorInterface::slotOk() {
	//  KMyFirewallRuleEditor* ed = (KMyFirewallRuleEditor*)parentWidget();
	m_rule->chain()->table()->kmfDoc()->startTransaction();
	m_rule->saveState();

	QString out = cb_out_int->currentText();
	QString in = cb_in_int->currentText();
	QString* option_name = new QString( "interface_opt" );
	QPtrList<QString>* args = new QPtrList<QString>;
/*	if ( ( c_in_int->isChecked() && !in.isEmpty() ) ||
		( c_out_int->isChecked() && !out.isEmpty() ) ) {
		args->append( new QString( "bool:on" ) );
	} else {
		args->append( new QString( "bool:off" ) );
	}*/
	
	if ( c_in_int->isChecked() && !in.isEmpty() ) {
		bool inv = c_inv_in_int->isChecked();
		if ( inv ) {
			in.prepend( "! " );
		}
		QString* opt = new QString( in );
		args->append( opt );
	} else {
		args->append( new QString( "bool:off" ) );
	}
	if ( c_out_int->isChecked() && !out.isEmpty() ) {
		bool inv = c_inv_out_int->isChecked();
		if ( inv ) {
			out.prepend( "! " );
		}
		QString* opt = new QString( out );
		args->append( opt );
	} else {
		args->append( new QString( "bool:off" ) );
	}

	emit sigAddRuleOpt( option_name, args );
	m_rule->chain()->table()->kmfDoc()->endTransaction();
	emit sigHideMe();
}

/** No descriptions */
void KMFRuleEditorInterface::slotCheckConfig() {}
/** No descriptions */
void KMFRuleEditorInterface::slotReadConfig() {
	cb_in_int->clear();
	cb_out_int->clear();
	cb_in_int->insertStringList( KMFConfig::interfaces() );
	cb_out_int->insertStringList( KMFConfig::interfaces() );
}

void KMFRuleEditorInterface::slotHelp() {
	kdDebug() << "void KMFRuleEditorInterface::slotHelp()" << endl;
	kapp->invokeHelp( "interfaces" );
}
void KMFRuleEditorInterface::reject() {
	kdDebug() << "void KMFRuleEditorInterface::reject()" << endl;
	emit sigHideMe();
}
#include "kmfruleeditorinterface.moc"
