summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/ruleoptionplugins/ip_option/kmfruleeditip.cpp
blob: b63ab42fb3b4e26a733895dc051b3f01ea86fd72 (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
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
/*
Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2001-2004
*/

#include "kmfruleeditip.h"
// qt includes
#include <tqcheckbox.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqvariant.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>

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

// project includes
#include "../../core/xmlnames.h"
#include "../../core/iptrule.h"
#include "../../core/iptruleoption.h"
#include "../../core/iptchain.h"
#include "../../core/iptable.h"
#include "../../core/kmfdoc.h"
#include "../../core/kmfiptdoc.h"
#include "../../core/kmferror.h"
#include "../../core/kmfcheckinput.h"
#include "../../core/kmferrorhandler.h"
#include "../../core/kmfnetwork.h"
#include "../../core/kmfundoengine.h"

namespace KMF {
/*
 *  Constructs a KMFRuleEditIP which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f' 
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */
KMFRuleEditIP::KMFRuleEditIP( TQWidget* parent, const char* name, WFlags fl )
		: KMyFirewallRuleEditorIP ( parent, name, fl ) {
	m_CheckInput = new KMFCheckInput();
	m_ErrorHandler = new KMFErrorHandler( "KMFRuleEditIP" );
	m_err = new KMFError();
}

/*
 *  Destroys the object and frees any allocated resources
 */
KMFRuleEditIP::~KMFRuleEditIP() {
	// no need to delete child widgets, TQt does it all for us
}

/*
 *  Main event handler. Reimplemented to handle application
 *  font changes
 */
bool KMFRuleEditIP::event( TQEvent* ev ) {
	bool ret = TQWidget::event( ev );
	if ( ev->type() == TQEvent::ApplicationFontChange ) {}
	return ret;
}

void KMFRuleEditIP::loadRule( IPTRule * rule ) {
	kdDebug() << "void KMFRuleEditIP::loadRule( IPTRule * rule )" << endl;
	c_src_ip->setChecked( false );
	c_dest_ip->setChecked( false );
	c_inv_src_ip->setChecked( false );
	c_inv_dest_ip->setChecked( false );
	t_src_ip->setText( "" );
	t_dest_ip->clear();
	m_rule = rule;

	IPTRuleOption* opt = 0;
	opt = m_rule->getOptionForName("ip_opt");
	if ( opt ) {
		TQStringList args = opt->getValues();
		TQString src, dest;
		src = *args.at(0);
		dest = *args.at(1);

		if ( !src.isEmpty() && src != XML::Undefined_Value && src != XML::BoolOff_Value) {
			c_src_ip->setChecked( true );
			if ( src.startsWith( "! " ) ) {
				c_inv_src_ip->setChecked( true );
				t_src_ip->setText( src.right( src.length() - 2 ) );
			} else {
				t_src_ip->setText( src );
			}
		}
		if ( !dest.isEmpty() && dest != XML::Undefined_Value && dest != XML::BoolOff_Value ) {
			c_dest_ip->setChecked( true );
			if ( dest.startsWith( "! " ) ) {
				c_inv_dest_ip->setChecked( true );
				t_dest_ip->setText( dest.right( dest.length() - 2 ) );
			} else {
				t_dest_ip->setText( dest );
			}
		}

	}
	return ;
}


void KMFRuleEditIP::accept() {
	kdDebug() << "KMFRuleEditIP::slotOk()" << endl;
	KMFUndoEngine::instance()->startTransaction( 
		m_rule,
		i18n("Edit Rule: %1 IP Option").tqarg( m_rule->name() ) 
	);
	TQString* src_ip = new TQString( t_src_ip->text() );
	TQString* dest_ip = new TQString( t_dest_ip->text() );

	bool en_c_src_ip = c_src_ip->isChecked();
	bool en_c_dest_ip = c_dest_ip->isChecked();
	bool is_inv_src_ip = c_inv_src_ip->isChecked();
	bool is_inv_dest_ip = c_inv_dest_ip->isChecked();

	TQString* option_name = new TQString( "ip_opt" );
	TQPtrList<TQString>* args = new TQPtrList<TQString>;
	TQPtrList<TQString>* empty_args = new TQPtrList<TQString>;
	empty_args->append( new TQString(XML::BoolOff_Value) );
	empty_args->append(  new TQString(XML::BoolOff_Value) );
	m_rule->addRuleOption( *option_name, *empty_args );
	// emit sigAddRuleOpt( option_name, empty_args );

	if ( en_c_src_ip && !src_ip->isEmpty() ) {
		TQString cmd = *src_ip;
		// Sanity checks +++++++++++++++++
		m_CheckInput->checkInput( cmd, "IP/NETWORK/FTQHN", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		// ++++++++++++++++++++++++++++++++

		if ( is_inv_src_ip ) {
			src_ip->prepend( "! " );
		}
		args->append( src_ip );
	} else {
		args->append( new TQString(XML::BoolOff_Value) );
	}

	if ( en_c_dest_ip && !dest_ip->isEmpty() ) {
		kdDebug() << "Add new dest_ip option" << endl;
		TQString cmd = *dest_ip;
		// Sanity checks +++++++++++++++++
		m_CheckInput->checkInput( cmd, "IP/NETWORK/FTQHN", m_err );
		if ( !m_ErrorHandler->showError( m_err ) ) {
			KMFUndoEngine::instance()->abortTransaction();
			return ;
		}
		// ++++++++++++++++++++++++++++++++
		if ( is_inv_dest_ip ) {
			dest_ip->prepend( "! " );
		}
		args->append( dest_ip );
	}  else {
		args->append( new TQString(XML::BoolOff_Value) );
	}

	m_rule->addRuleOption( *option_name, *args );
	KMFUndoEngine::instance()->endTransaction();
	emit sigHideMe();
}

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

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


}


#include "kmfruleeditip.moc"