summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/ruleoptionplugins/limit_option/kmfruleeditorlimit.cpp
blob: 1d8c9f00d1e9eb65f28fcc1e58f6d32b366dff43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************
 begin                : Wed Mar 13 2002
 copyright            : (C) 2002 by Christian Hubinger
 email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <tqstring.h>
#include <tqptrlist.h>
#include <tqspinbox.h>
#include <tqcombobox.h>
#include <tqcheckbox.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"
#include "../../core/kmfnetwork.h"
#include "../../core/kmfundoengine.h"
#include "../../core/xmlnames.h"
namespace KMF {
KMFRuleEditorLimit::KMFRuleEditorLimit( TQWidget *tqparent, const char *name, WFlags fl /*,IPTRule* cr*/ ) : KMyFirewallRuleEditorLimit( tqparent, 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) {
		TQStringList vals = opt->getValues();
		TQString limit = "";
		TQString burst = "";
		limit = *vals.at(1);
		burst = *vals.at(2);
		if ( limit != XML::Undefined_Value ) {
			c_use_limit->setChecked( true );
			c_limit->setChecked( true );
			int i = limit.tqfind( "/" );
			TQString str_rate = limit.left( i );
			kdDebug() << "Found Rate: " << str_rate << endl;
			int rate = str_rate.toInt();
			sb_limit->setValue( rate );
			TQString 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 != XML::Undefined_Value ) {
			c_burst->setChecked( true );
			kdDebug() << "Found Burst: " << burst << endl;
			int rate = burst.toInt();
			sb_burst->setValue( rate );
		}
	}
}

void KMFRuleEditorLimit::slotOk() {
	KMFUndoEngine::instance()->startTransaction( 
		m_rule,
		i18n("Edit Rule: %1 Limit Option").tqarg( m_rule->name() ) 
	);
	TQPtrList<TQString>* options = new TQPtrList<TQString>;
	TQString* name = new TQString( "limit_opt" );
	if ( c_use_limit->isChecked() ) {
		TQString* opt = new TQString( "" );
		if ( !c_limit->isChecked() && !c_burst->isChecked() ) {
			KMessageBox::sorry( this, i18n( "You cannot enable a limit without at least the rate." ), i18n( "Limit Matches" ) );
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		if ( c_limit->isChecked() ) {
			TQString str_rate = sb_limit->text();
			TQString interval = cb_interval->currentText();
			opt->append( str_rate );
			opt->append( "/" );
			opt->append( interval );
			options->append( new TQString( XML::BoolOn_Value )  );
			options->append( opt );
			if ( c_burst->isChecked() ) {
				str_rate = sb_burst->text();
				TQString* burst_rate = new TQString( str_rate );
				options->append( burst_rate );
			} else {
				options->append( new TQString( XML::BoolOff_Value )  );
				options->append( new TQString( XML::BoolOff_Value )  );
			}
		} else {
			options->append( new TQString( XML::BoolOff_Value )  );
			options->append( new TQString( XML::BoolOff_Value )  );
		}
	}
	m_rule->addRuleOption( *name, *options );
	KMFUndoEngine::instance()->endTransaction();
	emit sigHideMe();
}

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

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

}

#include "kmfruleeditorlimit.moc"