summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/kmfwidgets/kmfnethostpropertieswidget.cpp
blob: e3cbeb9aaae8593670e302449b2cc9b83a63d208 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// C++ Implementation:
//
// Description:
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//


#include "kmfnethostpropertieswidget.h"

// QT includes
#include <tqlistview.h>
#include <tqcheckbox.h>
#include <tqspinbox.h>
#include <tqcombobox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqgroupbox.h>
#include <tqwidgetstack.h>
#include <tqtextedit.h>
#include <tqframe.h>

// KDE includes
#include <kdebug.h>
#include <tdelocale.h>
#include <tdelistview.h>
#include <kcombobox.h>
#include <tdepopupmenu.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <knuminput.h>

// Project Includes
#include "../core/kmftarget.h"
#include "../core/ipaddress.h"
#include "../core/kmfnethost.h"
#include "../core/kmfnetzone.h"
#include "../core/kmfundoengine.h"

#include "kmfipv4addresswidget.h"
namespace KMF {
KMFNetHostPropertiesWidget::KMFNetHostPropertiesWidget ( TQWidget* parent, const char* name, WFlags fl )
		: KMyFirewallNetHostProperties ( parent,name,fl ) {
		
	m_address_widget = new KMFIPv4AddressWidget( m_frameAddress, "m_address_widget" );
	
	connect( m_address_widget, TQT_SIGNAL( sigAddressChanged( int, int, int, int ) ),
		this,TQT_SLOT( slotAddressChanged( int, int, int, int ) ) );
	
	connect( m_cb_host_limit, TQT_SIGNAL( toggled( bool ) ), 
		this, TQT_SLOT( slotHostLimitToggled( bool ) ) );	
	
	connect( m_sb_host_rate, TQT_SIGNAL( valueChanged( int ) ),
		this, TQT_SLOT( slotHostLimitValueChanged( int ) ) );
	
	connect( m_cb_limit_host_interval, TQT_SIGNAL( activated( int ) ),
		this, TQT_SLOT( slotHostLimitScaleChanged( int ) ) );
	
	connect( m_cb_host_log, TQT_SIGNAL( toggled( bool ) ),
		this, TQT_SLOT( slotHostLogToggled( bool ) ) );

	connect( m_host_desc, TQT_SIGNAL( textChanged() ),
		this, TQT_SLOT( slotDescriptionChanged() ) );
	
    TQGridLayout *grid = new TQGridLayout( m_frameAddress, 1, 1 );
	grid->addWidget( m_address_widget, 0, 0 );
	m_frameAddress->adjustSize();
	adjustSize();
}

KMFNetHostPropertiesWidget::~KMFNetHostPropertiesWidget() {}

void KMFNetHostPropertiesWidget::blockAllSignals( bool onoff ) {
	m_cb_limit_host_interval->blockSignals( onoff );
	m_cb_host_limit->blockSignals( onoff );
	m_sb_host_rate->blockSignals( onoff );
	m_host_desc->blockSignals( onoff );
}

void KMFNetHostPropertiesWidget::slotHostDeleted() {
	setEnabled( false );
	m_host = 0;
}	

void KMFNetHostPropertiesWidget::loadHost( KMFNetHost* host ) {
	kdDebug() << "KMFNetHostPropertiesWidget::loadTarget( KMFNetHost* host " << endl;
	if ( ! host ) {
		return;
	}
	
	if ( m_host ) {
		disconnect( m_host, TQT_SIGNAL( destroyed() ),
			this,TQT_SLOT( slotHostDeleted() ) );
	}
	
	m_host = host;
	connect( m_host, TQT_SIGNAL( destroyed() ),
			this,TQT_SLOT( slotHostDeleted() ) );
	blockAllSignals( true );
	setEnabled( true );
	m_address_widget->loadIPAddress( 
		m_host->address()->getDigit( 0 ),
		m_host->address()->getDigit( 1 ),
		m_host->address()->getDigit( 2 ),
		m_host->address()->getDigit( 3 )
	);
	if (  m_host->limit() ) {
		m_cb_host_limit->setChecked( true );
		m_sb_host_rate->setEnabled( true );
		m_cb_limit_host_interval->setEnabled( true );
		m_sb_host_rate->setValue( m_host->limitRate() );
		kdDebug() << "Found host log scale: " << m_host->limitScale() << endl;
		
		m_cb_limit_host_interval->blockSignals( true );
		if (  m_host->limitScale() == "second" ) {
			m_cb_limit_host_interval->setCurrentItem( 0 );
		} else if ( m_host->limitScale() == "minute" ) {
			m_cb_limit_host_interval->setCurrentItem( 1 );
		} else if ( m_host->limitScale() == "hour" ) {
			m_cb_limit_host_interval->setCurrentItem( 2 );
		} else {
			kdDebug() << "Found illefal value: " << m_host->limitScale() << endl;
		}
		m_cb_limit_host_interval->blockSignals( false );
		
	} else {
		m_cb_host_limit->setChecked( false );
		m_sb_host_rate->setEnabled( false );
		m_cb_limit_host_interval->setEnabled( false );
	}
	
	KMFNetZone* parentZone = m_host->zone()->rootZone();
	if ( parentZone->name() == "incoming_world" ) {
		m_cb_host_log->setChecked( m_host->logIncoming() );
	} else if (  parentZone->name() == "outgoing_world" ) {
		m_cb_host_log->setChecked( m_host->logOutgoing() );
	}
	
	if ( m_host_desc->text().simplifyWhiteSpace() != m_host->description() ) {
		m_host_desc->blockSignals( true );
		m_host_desc->setText(  m_host->description() );
		m_host_desc->blockSignals( false );
	}
	m_host_desc->setReadOnly( false );
	blockAllSignals( false );
}

void KMFNetHostPropertiesWidget::slotAddressChanged( int d0, int d1, int d2, int d3 ) {
	kdDebug() << "KMFNetHostPropertiesWidget::slotAddressChanged( " 
		<< d0 << "." 
		<< d1 << "." 
		<< d2 << "."
		<< d3 << ")" << endl;
	KMFUndoEngine::instance()->startTransaction( 
			m_host,
			i18n("Change address of Host: %1.").arg( m_host->guiName() )  
	);
	m_host->address()->setAddress( d0, d1, d2, d3 );
	m_host->zone()->rootZone()->placeHostInZone( m_host );
	m_host->zone()->rootZone()->refreshNetworkTree();
	KMFUndoEngine::instance()->endTransaction();
	
	emit sigHostAddressChanged( m_host->address()->toString() );
	// emit sigHostChanged( m_host );
}


void KMFNetHostPropertiesWidget::slotHostLogToggled( bool ) {
	kdDebug() << "void KMFNetHostPropertiesWidget::slotHostLogToggled( bool )" << endl;
	if ( ! m_host )
		return;
	 kdDebug() << "Changing host: " << m_host->guiName() << endl;		
	
	KMFNetZone* parentZone = m_host->zone()->rootZone();
	if ( parentZone->name() == "incoming_world" ) {
		if ( m_host->logIncoming() == m_cb_host_log->isChecked() ) {
			return;
		}
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "%1 logging of incoming packets for host %2." ).arg( m_cb_host_log->isChecked() ? i18n( "Enable" ) : i18n( "Disable" ) ).arg( m_host->guiName() )
		);
		m_host->setLogIncoming( m_cb_host_log->isChecked() );
		KMFUndoEngine::instance()->endTransaction();
	} else if (  parentZone->name() == "outgoing_world" ) {
		if ( m_host->logOutgoing() == m_cb_host_log->isChecked() ) {
			return;
		}
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "%1 logging of outgoing packets for host %2." ).arg( m_cb_host_log->isChecked() ? i18n( "Enable" ) : i18n( "Disable" ) ).arg( m_host->guiName() )
		);
		m_host->setLogOutgoing( m_cb_host_log->isChecked() );
		KMFUndoEngine::instance()->endTransaction();
	}
	emit sigHostChanged( m_host );
}

void KMFNetHostPropertiesWidget::slotHostLimitToggled( bool on ){
	kdDebug() << "void KMFNetHostPropertiesWidget::slotHostLimitToggled( bool " << on << " )" << endl;
	if ( ! m_host )
		return;
	kdDebug() << "Changing host: " << m_host->guiName() << endl;
	if ( on ) {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Enable package limit for host %1." ).arg( m_host->guiName() )
		);
		m_host->setLimit( m_sb_host_rate->value(),  m_cb_limit_host_interval->currentText() );
		KMFUndoEngine::instance()->endTransaction();
	} else {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Disable package limit for host %1." ).arg( m_host->guiName() )
		);
		m_host->setLimit( -1, m_cb_limit_host_interval->currentText() );
		KMFUndoEngine::instance()->endTransaction();
	}
	emit sigHostChanged( m_host );
}

void KMFNetHostPropertiesWidget::slotHostLimitValueChanged( int ) {
	kdDebug() << "void KMFNetHostPropertiesWidget::slotHostLimitValueChanged( int )" << endl;
	if ( ! m_host )
		return;
	kdDebug() << "Changing host: " << m_host->guiName() << endl;	
	if ( m_cb_host_limit->isChecked() ) {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Change package limit for host %1 to %2/%3." ).arg( m_host->guiName() ).arg(  m_sb_host_rate->value() ).arg( m_cb_limit_host_interval->currentText() )
		);
		m_host->setLimit( m_sb_host_rate->value(),  m_cb_limit_host_interval->currentText() );
		KMFUndoEngine::instance()->endTransaction();
	} else {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Disable package limit for host %1." ).arg( m_host->guiName() )
		);
		m_host->setLimit( -1, m_cb_limit_host_interval->currentText() );
		KMFUndoEngine::instance()->endTransaction();
	}
	emit sigHostChanged( m_host );
}

void KMFNetHostPropertiesWidget::slotHostLimitScaleChanged( int ) {
	kdDebug() << "void KMFGenericInterfaceProtocol::slotHostLimitValueChanged( int )" << endl;
	if ( ! m_host )
		return;
	kdDebug() << "Changing host: " << m_host->guiName() << endl;	
	if ( m_cb_host_limit->isChecked() ) {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Change package limit for host %1 to %2/%3." ).arg( m_host->guiName() ).arg(  m_sb_host_rate->value() ).arg( m_cb_limit_host_interval->currentText() )
		);
		m_host->setLimit( m_sb_host_rate->value(),  m_cb_limit_host_interval->currentText() );
		KMFUndoEngine::instance()->endTransaction();
	} else {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Disable package limit for host %1." ).arg( m_host->guiName() )
		);
		m_host->setLimit( -1, "" );
		KMFUndoEngine::instance()->endTransaction();
	}
	emit sigHostChanged( m_host );
}

void KMFNetHostPropertiesWidget::slotDescriptionChanged() {
	kdDebug() << "void KMFNetHostPropertiesWidget::slotDescriptionChanged( const TQString& )" << endl;
	if ( ! m_host ) {
		return;
	}
	TQString newTxt = m_host_desc->text().simplifyWhiteSpace();
	if ( newTxt != m_host->description() ) {
		KMFUndoEngine::instance()->startTransaction(
			m_host,
			i18n( "Change documentation of host: %1" ).arg( m_host->guiName() )
		);
		m_host->setDescription( newTxt );
		KMFUndoEngine::instance()->endTransaction();
	}
	emit sigHostChanged( m_host );
}

}

#include "kmfnethostpropertieswidget.moc"