summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/kmfwidgets/kmfgenericinterfaceeditprotocol.cpp
blob: 45ad652e66851309bdb25f63b2636b18db0e33bc (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//
// C++ Implementation: kmfgenericinterfaceeditprotocol
//
// Description:
//
//
// Author: Christian Hubinger <[email protected]>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
/***************************************************************************
 *                                                                         *
 *   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 "kmfgenericinterfaceeditprotocol.h"

// QT includes
#include <qstring.h>
#include <qcheckbox.h>
#include <qfile.h>
#include <qdir.h>
#include <qdom.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qlistview.h>
#include <qradiobutton.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qtextedit.h>
#include <qbuttongroup.h>
#include <qgroupbox.h>
#include <qspinbox.h>
#include <quuid.h>
#include <qcombobox.h>

// KDE includes
#include <klocale.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kio/netaccess.h>
#include <kio/job.h>
#include <ktrader.h>
#include <klibloader.h>
#include <klistview.h>
#include <klistbox.h>
#include <knuminput.h>
#include <kpushbutton.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include <kcombobox.h>

// Project includes
#include "kmflistviewitem.h"
#include "../version.h"
#include "../core/kmfgenericdoc.h"
#include "../core/kmfnetwork.h"
#include "../core/kmfprotocol.h"
#include "../core/kmfprotocollibrary.h"
#include "../core/kmfprotocolcategory.h"
#include "../core/kmferror.h"
#include "../core/kmferrorhandler.h"

namespace KMF {
KMFGenericInterfaceEditProtocol::KMFGenericInterfaceEditProtocol( QWidget *parent, const char *name, WFlags f )
		: KMyFirewallGenericInterfaceEditProtocolWidget( parent, name, f ) {
	// m_protocolsLoaded = false;
//	m_protocols.clear();
		
	connect( m_lv_protocols, SIGNAL( pressed( QListViewItem* ) ) ,
		this, SLOT( slotNewItemSelected( QListViewItem* ) ) );
	
	connect( m_lb_tcpPorts, SIGNAL( currentChanged ( QListBoxItem * ) ), this, SLOT( slotCurrentTCPPortChanged( QListBoxItem * ) ) );
	
	connect( m_lb_udpPorts, SIGNAL( currentChanged ( QListBoxItem * ) ), this, SLOT( slotCurrentUDPPortChanged( QListBoxItem * ) ) );
	
	// connect( m_sb_port_num, SIGNAL( valueChanged( int ) ), this, SLOT( slotPortVauleChanged( int ) ) );
	
	connect( m_te_desc, SIGNAL( textChanged() ), this, SLOT( slotNameDescritionChanged() ) );
	connect( m_le_protocolName , SIGNAL( textChanged( const QString& ) ), this, SLOT( slotNameDescritionChanged() ) );
	connect( m_rb_tcp, SIGNAL( clicked() ), this, SLOT( slotProtocolTypeChanged() ) );
	connect( m_rb_udp, SIGNAL( clicked() ), this, SLOT( slotProtocolTypeChanged() ) );
	
	connect( m_b_add_port, SIGNAL( clicked() ), this, SLOT( slotAddPort() ) );
	connect( m_b_del_port, SIGNAL( clicked() ), this, SLOT( slotDelPort() ) );
	
	connect( m_b_new_protocol, SIGNAL( clicked() ), this, SLOT( slotAddProtocol() ) );
	connect( m_b_del_protocol, SIGNAL( clicked() ), this, SLOT( slotDelProtocol() ) );
	
	connect( m_cb_showAll,SIGNAL( toggled( bool ) ), this , SLOT( slotUpdateView() ) );
}


KMFGenericInterfaceEditProtocol::~KMFGenericInterfaceEditProtocol() {}



void KMFGenericInterfaceEditProtocol::slotNewItemSelected( QListViewItem* qit ) {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotNewItemSelected(...)" << endl;
	
	KMFListViewItem *item = dynamic_cast<KMFListViewItem*> ( qit ); 
	if ( ! item ) {
		kdDebug() << "Item was no KMFListViewItem!" << endl;
		m_gb_edit->setEnabled( false );
		m_b_del_protocol->setEnabled( false );
		return;
	}
	
	if ( m_protocol && 
			item->type() == NetfilterObject::PROTOCOL && 
			m_protocol->uuid() == item->protocol()->uuid() ) {
		kdDebug() << "Selected Same Protocal again" << endl;
		m_gb_edit->setEnabled( m_protocol->customProtocol() );
		m_b_del_protocol->setEnabled( m_protocol->customProtocol() );
		return;
	}
	if ( item->type() != NetfilterObject::PROTOCOL ) {
		return;
	} 
	m_gb_edit->setEnabled( true );
	m_b_del_protocol->setEnabled( true );
	m_protocol = item->protocol();
	updateEdit();
}

void  KMFGenericInterfaceEditProtocol::slotCurrentTCPPortChanged( QListBoxItem * item ) {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotCurrentTCPPortChanged()" << endl;
	if ( ! item ) {
		return;
	}

	m_lb_udpPorts->clearSelection();
// 	m_sb_port_num->setValue( item->text().toInt() );
	m_rb_tcp->setChecked( true );
	
}

void  KMFGenericInterfaceEditProtocol::slotCurrentUDPPortChanged( QListBoxItem * item ) {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotCurrentUDPPortChanged()" << endl;
	if ( ! item ) {
		return;
	}
	m_lb_tcpPorts->clearSelection();
/*	m_sb_port_num->blockSignals( true );
	m_sb_port_num->setValue( item->text().toInt() );
	m_sb_port_num->blockSignals( false );*/
	m_rb_udp->setChecked( true );
}

void KMFGenericInterfaceEditProtocol::slotProtocolTypeChanged() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotProtocolTypeChanged()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "\n\nWARNING: No Protocol Object to save Changes in  Memory!!\n\n" << endl;
		return;
	}
	
	m_lb_udpPorts->clearSelection();
	m_lb_tcpPorts->clearSelection();
}
void KMFGenericInterfaceEditProtocol::slotAddPort() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotAddPort()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "\n\nWARNING: No Protocol Object to save Changes in  Memory!!\n\n" << endl;
		return;
	}
	
	m_lb_udpPorts->clearSelection();
	m_lb_tcpPorts->clearSelection();
	if ( m_rb_tcp->isChecked() ) {
		m_protocol->addPort( m_sb_port_num->text(), KMFProtocol::TCP );
		m_lb_tcpPorts->blockSignals( true );
		m_lb_tcpPorts->clear();
		// QStringList l = QStringList::split( ",", m_protocol->tcpPortsList() );
		m_lb_tcpPorts->insertStringList( QStringList::split( ",", m_protocol->tcpPortsList() ) );
		
	
		m_lb_tcpPorts->blockSignals( false );
		
		for( uint i= 0; i< m_lb_tcpPorts->count(); i++ ) {
			if ( m_lb_tcpPorts->text( i ) == m_sb_port_num->text() ) {
				m_lb_tcpPorts->setSelected( i, true );
			}
		}
		slotProtocolChanged();
	} else if ( m_rb_udp->isChecked() ) {
		m_protocol->addPort( m_sb_port_num->text(), KMFProtocol::UDP );
		m_lb_udpPorts->blockSignals( true );
		m_lb_udpPorts->clear();
// 		for( uint i= 0; i< m_protocol->udpPorts()->count(); i++ ) {
// 			m_lb_udpPorts->insert( *m_protocol->udpPorts().at( i ) );
// 		}
		m_lb_udpPorts->insertStringList( QStringList::split( ",", m_protocol->udpPortsList() ) );
		m_lb_udpPorts->blockSignals( false );
				
		for( uint i= 0; i< m_lb_udpPorts->count(); i++ ) {
			if ( m_lb_udpPorts->text( i ) == m_sb_port_num->text() ) {
				m_lb_udpPorts->setSelected( i, true );
			}
		}
		slotProtocolChanged();
	}
}

void KMFGenericInterfaceEditProtocol::slotDelPort() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotDelPort()" << endl;
	if ( m_lb_tcpPorts->selectedItem() ) {
		m_protocol->delPort( m_lb_tcpPorts->selectedItem()->text(), KMFProtocol::TCP );
		m_lb_tcpPorts->blockSignals( true );
		m_lb_tcpPorts->clear();
		m_lb_tcpPorts->insertStringList( QStringList::split( ",", m_protocol->tcpPortsList() ) );
		m_lb_tcpPorts->blockSignals( false );
		if ( m_lb_tcpPorts->count() > 0 ) {
			m_lb_tcpPorts->setSelected( 0, true );
		}
		slotProtocolChanged();
	} else if ( m_lb_udpPorts->selectedItem() ) {
		m_protocol->delPort( m_lb_udpPorts->selectedItem()->text(), KMFProtocol::UDP );
		m_lb_udpPorts->blockSignals( true );
		m_lb_udpPorts->clear();
		m_lb_udpPorts->insertStringList( QStringList::split( ",", m_protocol->udpPortsList() ) );
		m_lb_udpPorts->blockSignals( false );
		if ( m_lb_udpPorts->count() > 0 ) {
			m_lb_udpPorts->setSelected( 0, true );
		}
		slotProtocolChanged();
	}
}

void KMFGenericInterfaceEditProtocol::slotAddProtocol() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotAddProtocol()" << endl;
// 	KMFProtocolCategory* catCustom = KMFProtocol::findCategory( KMFProtocolCategory::customCategoryUuid() );
// 	if ( ! catCustom ) {
// 		catCustom = KMFProtocolCategory::createCategory( i18n("Custom Protocols") );
// 		catCustom->setUuid( KMFProtocolCategory::customCategoryUuid().toString() );
// 	}
	
	KMFProtocol *p = KMFProtocolCategory::getCustomCategory()->createProtocol( i18n("New Protocol") );
	p->setName( i18n("New Protocol") );
	p->setCustomProtocol( true );
	m_protocol	= p;
	slotUpdateView();

// 	KMFListViewItem *item = new KMFListViewItem( m_lv_protocols, p );
// 	item->loadNetfilterObject( p );
// 	item->setupProtocolView();
// 	
// 	m_lv_protocols->setSelected( item, true );
// 	slotNewItemSelected( item );
}

void KMFGenericInterfaceEditProtocol::slotDelProtocol() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotDelProtocol()" << endl;
	if ( ! m_protocol ) {
		return;
		kdDebug() << "\n\nWARNING: No Protocol Object to save Changes in  Memory!!\n\n" << endl;
	}
	int doit = 	KMessageBox::questionYesNo ( this , i18n( "<p>Are you sure that you want to delete "
		                                        "the protocol: <b>%1</b>?</p>").arg( m_protocol->name() ),
		                                        i18n( "Delete Protocol" ), KStdGuiItem::yes(), KStdGuiItem::no() /*, "protocol_edit_delete_protocol"*/ );
		                                         
	// kdDebug() << "Got Answer: " << doit << endl;		                                         
	if ( doit != 3 ) {	
		return;
	}
	// FIXME: Casees Mem0ry Leak, but app crashed othwise!
	m_protocol->category()->delProtocol( m_protocol, false );
	m_protocol = 0; 
	slotNewItemSelected( 0 );
	slotUpdateView();
}

void KMFGenericInterfaceEditProtocol::slotOnProtocolDeleleted( QObject* ) {
	m_protocol = 0;
	slotUpdateView();
	slotNewItemSelected( 0 );
}

void KMFGenericInterfaceEditProtocol::slotPortVauleChanged( int val ) {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotPortVauleChanged()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "\n\nWARNING: No Protocol Object to save Changes in  Memory!!\n\n" << endl;
		return;
	}
	
	QString s = "";
	s.setNum( val );
	if ( m_rb_udp->isChecked() ) {
		QListBoxItem *i = m_lb_udpPorts->selectedItem();
		if ( ! i ) {
			return;
		}
		QString strVal = "";
		strVal.setNum( val );
		if ( m_protocol->replaceUDPPort( i->text().toInt(), val ) ) {
			m_lb_udpPorts->blockSignals( true );
			m_lb_udpPorts->clear();
			m_lb_udpPorts->insertStringList( QStringList::split( ",", m_protocol->udpPortsList() ) );
			
			for( uint i= 0; i< m_lb_udpPorts->count(); i++ ) {
				if ( m_lb_udpPorts->text( i ) == strVal ) {
					m_lb_udpPorts->setSelected( i, true );
					break;
				}
			}
			m_lb_udpPorts->blockSignals( false );
			slotProtocolChanged();
		}
	} else if ( m_rb_tcp->isChecked() ) {
		QListBoxItem *i = m_lb_tcpPorts->selectedItem();
		if ( ! i ) {
			return;
		}
		
		QString strVal = "";
		strVal.setNum( val );
		
		if( m_protocol->replaceTCPPort( i->text().toInt(), val ) ) {
			m_lb_tcpPorts->blockSignals( true );
			m_lb_tcpPorts->clear();
			m_lb_tcpPorts->insertStringList( QStringList::split( ",", m_protocol->tcpPortsList() ) );
			for( uint i= 0; i< m_lb_tcpPorts->count(); i++ ) {
				if ( m_lb_tcpPorts->text( i ) == strVal ) {
					m_lb_tcpPorts->setSelected( i, true );
					break;
				}
			}
			m_lb_tcpPorts->blockSignals( false );
			slotProtocolChanged();
		}
	}
}

void KMFGenericInterfaceEditProtocol::slotNameDescritionChanged() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotNameDescritionChanged()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "\n\nWARNING: No Protocol Object to save Changes in  Memory!!\n\n" << endl;
		return;
	}
	
	if (  ! m_le_protocolName->text().isEmpty() ) {
		m_protocol->setName( m_le_protocolName->text().stripWhiteSpace().simplifyWhiteSpace() );
	}
	m_protocol->setDescription( m_te_desc->text().stripWhiteSpace().simplifyWhiteSpace() );
	
	// m_protocol->setCategory( m_cob_
	slotProtocolChanged();
}

void KMFGenericInterfaceEditProtocol::slotProtocolChanged() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotProtocolChanged()" << endl;
	QListViewItemIterator it( m_lv_protocols );
    while ( it.current() ) {
        KMFListViewItem *kit = dynamic_cast<KMFListViewItem*> ( it.current() );
		if ( 	kit->type() == NetfilterObject::PROTOCOL && 
				kit->protocol()->uuid() == m_protocol->uuid() ) {
			kit->setupProtocolView();
			kit->setOpen( true );
			return;
		}
        ++it;
    }
}

void KMFGenericInterfaceEditProtocol::updateEdit() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::updateEdit()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "\n\nWARNING: No Protocol Object to save Changes in  Memory!!\n\n" << endl;
		m_gb_edit->setEnabled( false );
		m_b_del_protocol->setEnabled( false );
		return;
	}
	m_gb_edit->setEnabled( m_protocol->customProtocol() );
	m_b_del_protocol->setEnabled( m_protocol->customProtocol() );
	
	m_le_protocolName->blockSignals( true );
	m_te_desc->blockSignals( true );
	
	m_le_protocolName->setText( m_protocol->name() );
	m_te_desc->setText( m_protocol->description() );
	
	m_le_protocolName->blockSignals( false );
	m_te_desc->blockSignals( false );
	
	m_lb_tcpPorts->clear();
	m_lb_udpPorts->clear();
	m_lb_tcpPorts->insertStringList(  QStringList::split( ",", m_protocol->tcpPortsList() ) );
	m_lb_udpPorts->insertStringList(  QStringList::split( ",", m_protocol->udpPortsList() ) );
}

void KMFGenericInterfaceEditProtocol::slotUpdateView() {
	kdDebug() << "void KMFGenericInterfaceEditProtocol::slotUpdateView()" << endl;
	m_lv_protocols->clear();
	KMFProtocolCategory::getCustomCategory();
	
	if ( m_cb_showAll->isChecked() ) {
		QValueList<KMFProtocolCategory*>& protCats =  KMFProtocolLibrary::instance()->protocolCategories();
		QValueList<KMFProtocolCategory*>::iterator it;
		for( it = protCats.begin(); it != protCats.end(); ++it ) {
			KMFProtocolCategory* cat = *it;
			kdDebug() << "\nSetup Category: Item" << cat->name() << endl;
			
			KMFListViewItem *item = new KMFListViewItem( m_lv_protocols, cat );
			item->loadNetfilterObject( cat );
			item->setupProtocolCategoryView();
			if ( cat == KMFProtocolCategory::getCustomCategory() ) {
				item->setOpen( true );
			}
		}
	} else {
		KMFProtocolCategory* customCat = KMFProtocolCategory::getCustomCategory();
		KMFListViewItem *item = new KMFListViewItem( m_lv_protocols, customCat );
		item->loadNetfilterObject( customCat );
		item->setupProtocolCategoryView();
		item->setOpen( true );
	}
	
	QValueList< KMFProtocol* >& prots = KMFProtocolCategory::getCustomCategory()->protocols();
	QValueList< KMFProtocol* >::iterator it;
	for( it = prots.begin(); it != prots.end(); ++it ) {
		KMFProtocol* p= *it;
		disconnect( p, SIGNAL( destroyed( QObject* ) ),
			this, SLOT( slotOnProtocolDeleleted( QObject* ) ) );
		connect( p, SIGNAL( destroyed( QObject* ) ),
			this, SLOT( slotOnProtocolDeleleted( QObject* ) ) );
	}
	
	setEnabled( true );
}


void KMFGenericInterfaceEditProtocol::loadDoc( KMFNetwork* doc  ) {
	kdDebug() << "void KMFGenericInterfaceLogging::loadDoc( KMFGenericDoc* )" << endl;
	m_network = doc;
	slotUpdateView();
}

void KMFGenericInterfaceEditProtocol::saveProtocols() {
	kdDebug() << "KMFGenericInterfaceEditProtocol::saveProtocols()" << endl;
	KMFErrorHandler *errH = new KMFErrorHandler( "KMFErrorHandler" );
	errH->showError( KMFProtocolLibrary::instance()->saveProtocolLibrary() );
}

}

#include "kmfgenericinterfaceeditprotocol.moc"