summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/ruleoptionplugins/tos_option/kmfruleeditortos.cpp
blob: 75b1186d4aac97049b4cca7d03ecf99891ee75b9 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/***************************************************************************
 begin                : Wed Mar 27 2002
 copyright            : (C) 2002 by Christian Hubinger
 email                : chubinger@irrsinnig.org
***************************************************************************/

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

// QT includes
#include <tqcombobox.h>
#include <tqcheckbox.h>
#include <tqstring.h>
#include <tqlabel.h>
#include <tqgroupbox.h>

// kde includes
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeapplication.h>

// project includes
#include "../../core/xmlnames.h"
#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"
namespace KMF {
KMFRuleEditorTos::KMFRuleEditorTos( TQWidget *parent, const char *name, WFlags fl ) : KMyFirewallRuleEditorTos( parent, name, fl ) {
	m_option_type = "CHECKTOS";
}
KMFRuleEditorTos::~KMFRuleEditorTos() {}

void KMFRuleEditorTos::setType( const TQString& type ) {
	m_option_type = type;
	if ( m_option_type == "SETTOS" ) {
		gb_title->setTitle( i18n( "Set Type of Service" ) );
		c_use_tos->setText( i18n( "Set TOS" ) );
		setItems();
	} else if ( m_option_type == "CHECKTOS" ) {
		gb_title->setTitle( i18n( "Match Type of Service" ) );
		c_use_tos->setText( i18n( "Check TOS" ) );
		setItems();
	} else if ( m_option_type == "REJECTTYPE" ) {
		gb_title->setTitle( i18n( "Reject with icmp-type" ) );
		c_use_tos->setText( i18n( "Reject with type:" ) );
		setItems();
	}
}

void KMFRuleEditorTos::loadRule( IPTRule* rule ) {
	kdDebug() << "void KMFRuleEditorTos::loadRule( IPTRule* rule )" << endl;
	m_rule = rule;
	if ( m_option_type == "SETTOS" ) {
		setItems();
		IPTRuleOption* opt = rule->getOptionForName("target_set_tos_opt");
		if ( ! opt ->isEmpty()) {
			TQStringList values = opt->getValues();
			TQString val = *values.at(0);
			if ( val != XML::Undefined_Value  ) {
				c_use_tos->setChecked( true );
				int index = getItemNum( val );
				if ( index > 0 )
					cb_tos->setCurrentItem( index );
			} else {
				c_use_tos->setChecked( false );
			}
		} else {
			c_use_tos->setChecked( false );
		}
	} else if ( m_option_type == "CHECKTOS" ) {
		setItems();
		IPTRuleOption* opt = rule->getOptionForName("tos_opt");
		if ( ! opt ->isEmpty() ) {
			TQStringList values = opt->getValues();
			TQString val = *values.at(0);
			if ( val == XML::BoolOn_Value ) {
				TQString val2 = *values.at(1);
				c_use_tos->setChecked( true );
				int index = getItemNum( val2 );
				if ( index > 0 )
					cb_tos->setCurrentItem( index );
			} else {
				c_use_tos->setChecked( false );
			}
		} else {
			c_use_tos->setChecked( false );
		}
	} else if ( m_option_type == "REJECTTYPE" ) {
		setItems();
		IPTRuleOption* opt = rule->getOptionForName("target_reject_type_opt");
		if ( ! opt ->isEmpty() ) {
			TQStringList values = opt->getValues();
			TQString val = *values.at(0);
			if ( val != XML::Undefined_Value) {
				c_use_tos->setChecked( true );
				int index = getItemNum( val );
				if ( index > 0 )
					cb_tos->setCurrentItem( index );
			} else {
				c_use_tos->setChecked( false );
			}
		} else {
			c_use_tos->setChecked( false );
		}
	} else {
		kdDebug() << "Misuse of this class no type set !!" << endl;
	}
}

void KMFRuleEditorTos::accept() {
	KMFUndoEngine::instance()->startTransaction( 
		m_rule,
		i18n("Edit Rule: %1 TOS Option").arg( m_rule->name() ) 
	);
	if ( m_option_type == "SETTOS" ) {
		bool use_tos = c_use_tos->isChecked();
		TQPtrList<TQString>* options = new TQPtrList<TQString>;
		TQString* name = new TQString( "target_set_tos_opt" );
		if ( use_tos ) {
			TQString val = cb_tos->currentText();
			options->append( new TQString( val ) );
		} else {
			options->append( new TQString( XML::BoolOff_Value) );
		}
		// emit sigAddTargetOpt( name, options );
		m_rule->addTargetOption( *name, *options );
		KMFUndoEngine::instance()->endTransaction();
		emit sigDocumentChanged();
	} else if ( m_option_type == "CHECKTOS" ) {
		bool use_tos = c_use_tos->isChecked();
		TQPtrList<TQString>* options = new TQPtrList<TQString>;
		TQString* name = new TQString( "tos_opt" );
		if ( use_tos ) {
			TQString val = cb_tos->currentText();
			options->append( new TQString( XML::BoolOn_Value) );
			options->append( new TQString( val ) );
		} else {
			options->append( new TQString( XML::BoolOff_Value) );
		}
		// emit sigAddRuleOpt( name, options );
		m_rule->addRuleOption( *name, *options );
		KMFUndoEngine::instance()->endTransaction();
		emit sigHideMe();

	} else if ( m_option_type == "REJECTTYPE" ) {
		bool use_tos = c_use_tos->isChecked();
			TQPtrList<TQString>* options = new TQPtrList<TQString>;
			TQString* name = new TQString( "target_reject_type_opt" );
		if ( use_tos ) {
			TQString val = cb_tos->currentText();
			options->append( new TQString( val ) );
		} else {
			options->append( new TQString( XML::BoolOff_Value) );
		}
		// emit sigAddTargetOpt( name, options );
		m_rule->addTargetOption( *name, *options );
		KMFUndoEngine::instance()->endTransaction();
		emit sigHideMe();
	}
	KMFUndoEngine::instance()->abortTransaction();
}

void KMFRuleEditorTos::setItems() {
	kdDebug() << "void KMFRuleEditorTos::setItems(const TQString& type)" << endl;
	cb_tos->clear();
	if ( m_option_type == "SETTOS" || m_option_type == "CHECKTOS" ) {
		cb_tos->insertItem( "Normal-Service" );
		cb_tos->insertItem( "Minimize-Cost" );
		cb_tos->insertItem( "Maximize-Reliability" );
		cb_tos->insertItem( "Maximize-Throughput" );
		cb_tos->insertItem( "Minimize-Delay" );
	} else if ( m_option_type == "REJECTTYPE" ) {
		cb_tos->insertItem( "icmp-port-unreachable" );
		cb_tos->insertItem( "icmp-net-unreachable" );
		cb_tos->insertItem( "icmp-host-unreachable" );
		cb_tos->insertItem( "icmp-proto-unreachable" );
		cb_tos->insertItem( "icmp-net-prohibited" );
		cb_tos->insertItem( "icmp-host-prohibited" );
	}
}

int KMFRuleEditorTos::getItemNum( const TQString& option ) {
	kdDebug() << "int KMFRuleEditorTos::getItemNum(const TQString& option)" << endl;
	int index = -1;
	for ( int i = 0; i < cb_tos->count(); i++ ) {
		TQString tmp_item = cb_tos->text( i );
		if ( tmp_item == option ) {
			index = i;
		}
	}
	return index;
}

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

void KMFRuleEditorTos::reject() {
	kdDebug() << "void KMFRuleEditorTos::reject()" << endl;
	loadRule(m_rule);
	emit sigHideMe();
}

}
#include "kmfruleeditortos.moc"