KNMap – nmap graphical interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commonwidget.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /***************************************************************************
  2. * *
  3. * Copyright (C) 2005, 2006 by Kevin Gilbert *
  4. * kev.gilbert@cdu.edu.au *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. * *
  21. ***************************************************************************/
  22. #include <qbuttongroup.h>
  23. #include <qcheckbox.h>
  24. #include <qcombobox.h>
  25. #include <qlabel.h>
  26. #include <qlayout.h>
  27. #include <qlineedit.h>
  28. #include <qlistbox.h>
  29. #include <qpushbutton.h>
  30. #include <qradiobutton.h>
  31. #include <qstringlist.h>
  32. #include <qvalidator.h>
  33. #include <kconfig.h>
  34. #include <kdebug.h>
  35. #include <kglobal.h>
  36. #include <kiconloader.h>
  37. #include <klocale.h>
  38. #include <kmessagebox.h>
  39. #include "commonwidget.h"
  40. #include "whatsthis.h"
  41. // constructor
  42. // ===========
  43. CommonWidget::CommonWidget( QWidget* parent, const char* name )
  44. : QWidget( parent, name ),
  45. m_hosts( NULL ),
  46. m_portRangeIgnoreChange( false ),
  47. m_portRanges( NULL )
  48. { createLayout( );
  49. m_portRangeAddButton->setEnabled( false );
  50. m_portRangeDeleteButton->setEnabled( false );
  51. }
  52. // destructor
  53. // ==========
  54. CommonWidget::~CommonWidget( )
  55. { if( m_hosts != NULL )
  56. delete m_hosts;
  57. if( m_portRanges != NULL )
  58. delete m_portRanges;
  59. }
  60. // buildPortRangesTables
  61. // =====================
  62. bool CommonWidget::buildPortRangesTables( const boolPtr tcpPorts, const boolPtr udpPorts )
  63. { memset( tcpPorts, 0, 64 * 1024 );
  64. memset( udpPorts, 0, 64 * 1024 );
  65. QStringList portRanges;
  66. getPortRanges( &portRanges );
  67. for( QStringList::Iterator it = portRanges.begin( ); it != portRanges.end( ); ++it )
  68. { QString portRange = *it;
  69. bool tcp = false;
  70. bool udp = false;
  71. if( portRange[ 0 ].isDigit( ))
  72. { tcp = true;
  73. udp = true;
  74. }
  75. else
  76. if( portRange[ 0 ].lower( ) == 't' )
  77. { tcp = true;
  78. portRange = portRange.right( portRange.length( ) - 2 );
  79. }
  80. else
  81. if( portRange[ 0 ].lower( ) == 'u' )
  82. { udp = true;
  83. portRange = portRange.right( portRange.length( ) - 2 );
  84. }
  85. else
  86. { KMessageBox::error( this, i18n( QString( "Internal error - Unknown Port Range protocol '%1'" ).arg( portRange[ 0 ])), i18n( "Internal error" ));
  87. return false;
  88. }
  89. QStringList ports = QStringList::split( "-", portRange );
  90. if( ports.count( ) > 2 )
  91. { KMessageBox::error( this, i18n( "Internal error - Illegal Port Range encountered" ), i18n( "Internal error" ));
  92. return false;
  93. }
  94. ushort portFirst = ports.front( ).toUShort( );
  95. ushort portLast = portFirst;
  96. if( ports.count( ) > 1 )
  97. portLast = ports.back( ).toUShort( );
  98. for( uint port = portFirst; port <= portLast; port++ )
  99. { if( tcp )
  100. tcpPorts[ port ] = true;
  101. if( udp )
  102. udpPorts[ port ] = true;
  103. }
  104. }
  105. return true;
  106. }
  107. // createLayout
  108. // ============
  109. void CommonWidget::createLayout( )
  110. { QGridLayout* gridLayout = new QGridLayout( this );
  111. KIconLoader* iconLoader = KGlobal::iconLoader( );
  112. QIconSet clearRightIconSet = iconLoader->loadIconSet( "locationbar_erase", KIcon::Small );
  113. byte row = 1;
  114. m_hostButton = new QPushButton( clearRightIconSet, NULL, this, "host button" );
  115. m_hostComboBox = new QComboBox( true, this, "target hosts combobox" );
  116. QHBoxLayout* hostLayout = new QHBoxLayout( );
  117. byte col = 0;
  118. hostLayout->insertStretch( col++, 0 );
  119. hostLayout->insertWidget( col++, new QLabel( i18n( "Target host(s)" ), this, "target hosts label" ), 0 );
  120. hostLayout->insertStretch( col++, 1 );
  121. hostLayout->insertWidget( col++, m_hostButton, 0 );
  122. hostLayout->insertStretch( col++, 1 );
  123. hostLayout->insertWidget( col++, m_hostComboBox, 100 );
  124. hostLayout->insertStretch( col++, 0 );
  125. gridLayout->addMultiCellLayout( hostLayout, row, row, 1, 5 );
  126. QButtonGroup* resloveGroup = new QButtonGroup( 3, Qt::Vertical, i18n( "Resolve names" ), this, "resolve names group" );
  127. m_resovleDefaultRadio = new QRadioButton( i18n( "Default" ), resloveGroup, "default resolve radio" );
  128. m_resovleAlwaysRadio = new QRadioButton( i18n( "Always (-R)" ), resloveGroup, "always resolve radio" );
  129. m_resovleNeverRadio = new QRadioButton( i18n( "Never (-n)" ), resloveGroup, "never resolve radio" );
  130. row = 4;
  131. gridLayout->addMultiCellWidget( resloveGroup, row, row + 3, 1, 1 );
  132. byte rowBlock1 = ++row;
  133. m_portRangeAddButton = new QPushButton( i18n( "Add" ), this, "add port range button" );
  134. m_portRangeDeleteButton = new QPushButton( i18n( "Delete" ), this, "delete port range button" );
  135. m_portRangeComboBox = new QComboBox( this, "port range combobox" );
  136. m_portRangeLineEdit = new QLineEdit( this, "port range line edit" );
  137. m_portRangeListBox = new QListBox( this, "port range listbox" );
  138. m_portRangesCheckBox = new QCheckBox( i18n( "Port ranges (-p <port ranges>)" ), this, "port ranges checkbox" );
  139. m_portRangeComboBox->insertItem( i18n( "Both" ), BothProtocol );
  140. m_portRangeComboBox->insertItem( i18n( "TCP" ), TCPProtocol );
  141. m_portRangeComboBox->insertItem( i18n( "UDP" ), UDPProtocol );
  142. m_portRangeLineEdit->setValidator( new QRegExpValidator( QRegExp( "^\\d{1,5}-{1}\\d{1,5}$" ), this, "port range regx" ));
  143. row = 3;
  144. gridLayout->addMultiCellWidget( m_portRangesCheckBox, row, row, 3, 5, Qt::AlignCenter );
  145. row++;
  146. m_portRangeButton = new QPushButton( clearRightIconSet, NULL, this, "host button" );
  147. QHBoxLayout* portRangeLayout = new QHBoxLayout( );
  148. col = 0;
  149. portRangeLayout->insertStretch( col++, 0 );
  150. portRangeLayout->insertWidget( col++, new QLabel( i18n( "New range data: " ), this, "new range data label" ), 0 );
  151. portRangeLayout->insertStretch( col++, 1 );
  152. portRangeLayout->insertWidget( col++, m_portRangeButton, 0 );
  153. portRangeLayout->insertStretch( col++, 1 );
  154. portRangeLayout->insertWidget( col++, m_portRangeLineEdit, 100 );
  155. portRangeLayout->insertStretch( col++, 0 );
  156. gridLayout->addMultiCellLayout( portRangeLayout, row, row, 3, 5 );
  157. gridLayout->addWidget( new QLabel( i18n( "Protocol" ), this, "protocol label" ), row + 1, 3 );
  158. gridLayout->addWidget( m_portRangeComboBox, row + 2, 3 );
  159. gridLayout->addWidget( m_portRangeAddButton, row + 4, 3 );
  160. gridLayout->addWidget( m_portRangeDeleteButton, row + 5, 3 );
  161. byte rowBlock2 = _max_( row + 5, rowBlock1 );
  162. gridLayout->addMultiCellWidget( m_portRangeListBox, row + 1, rowBlock2, 5, 5 );
  163. gridLayout->setColStretch( 0, 4 );
  164. gridLayout->setColStretch( 2, 2 );
  165. gridLayout->setColStretch( 5, 10 );
  166. gridLayout->setColStretch( 6, 4 );
  167. gridLayout->setRowStretch( 0, 10 );
  168. gridLayout->setRowStretch( 2, 10 );
  169. gridLayout->setRowStretch( rowBlock2 + 1, 10 );
  170. }
  171. // enablePortRangeWidgets
  172. // ======================
  173. void CommonWidget::enablePortRangeWidgets( )
  174. { bool portRangesState = m_portRangesCheckBox->isChecked( );
  175. m_portRangeAddButton->setEnabled( false );
  176. m_portRangeDeleteButton->setEnabled( false );
  177. m_portRangeButton->setEnabled( portRangesState );
  178. m_portRangeLineEdit->setEnabled( portRangesState );
  179. m_portRangeLineEdit->clear( );
  180. m_portRangeComboBox->setEnabled( portRangesState );
  181. m_portRangeListBox->setEnabled( portRangesState );
  182. if( portRangesState )
  183. emit( disableFastScan( ));
  184. }
  185. // finaliseInitialisation
  186. // ======================
  187. void CommonWidget::finaliseInitialisation( )
  188. { WhatsThis* whatsThis = new WhatsThis( this );
  189. connect( m_hostButton, SIGNAL( clicked( )), SLOT( slotHostButtonClicked( )));
  190. connect( m_hostComboBox, SIGNAL( activated( int )), SLOT( slotHostActivated( int )));
  191. connect( m_hostComboBox, SIGNAL( textChanged( const QString& )), SLOT( slotHostTextChanged( const QString& )));
  192. connect( m_portRangeAddButton, SIGNAL( clicked( )), SLOT( slotPortRangeAddClicked( )));
  193. connect( m_portRangeButton, SIGNAL( clicked( )), SLOT( slotPortRangeButtonClicked( )));
  194. connect( m_portRangeDeleteButton, SIGNAL( clicked( )), SLOT( slotPortRangeDeleteClicked( )));
  195. connect( m_portRangeComboBox, SIGNAL( activated( int )), SLOT( slotPortRangeActivated( int )));
  196. connect( m_portRangeLineEdit, SIGNAL( textChanged( const QString& )), SLOT( slotPortRangeTextChanged( const QString& )));
  197. connect( m_portRangeListBox, SIGNAL( doubleClicked( QListBoxItem* )), SLOT( slotPortRangeDoubleClicked( QListBoxItem* )));
  198. connect( m_portRangeListBox, SIGNAL( highlighted( int )), SLOT( slotPortRangeHighlighted( )));
  199. connect( m_portRangesCheckBox, SIGNAL( clicked( )), SLOT( slotPortRangesClicked( )));
  200. connect( whatsThis, SIGNAL( clicked( )), SLOT( slotWhatsThisClicked( )));
  201. connect( m_hostButton, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  202. connect( m_hostComboBox, SIGNAL( activated ( int )), SIGNAL( optionsDirty( )));
  203. connect( m_hostComboBox, SIGNAL( textChanged( const QString& )), SIGNAL( optionsDirty( )));
  204. connect( m_portRangeAddButton, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  205. connect( m_portRangeButton, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  206. connect( m_portRangeComboBox, SIGNAL( activated( int )), SIGNAL( optionsDirty( )));
  207. connect( m_portRangeDeleteButton, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  208. connect( m_portRangeLineEdit, SIGNAL( textChanged( const QString& )), SIGNAL( optionsDirty( )));
  209. connect( m_portRangeListBox, SIGNAL( doubleClicked( QListBoxItem* )), SIGNAL( optionsDirty( )));
  210. connect( m_portRangeListBox, SIGNAL( highlighted ( int )), SIGNAL( optionsDirty( )));
  211. connect( m_portRangesCheckBox, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  212. connect( m_resovleAlwaysRadio, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  213. connect( m_resovleDefaultRadio, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  214. connect( m_resovleNeverRadio, SIGNAL( clicked( )), SIGNAL( optionsDirty( )));
  215. emit( targetChanged( m_hostComboBox->currentText( )));
  216. }
  217. QString CommonWidget::getHostName( ) const
  218. { return m_hostComboBox->currentText( );
  219. }
  220. // getHosts
  221. // ========
  222. void CommonWidget::getHosts( QStringList* hosts )
  223. { hosts->clear( );
  224. for( ushort i = 0; i < m_hostComboBox->count( ); i++ )
  225. hosts->append( m_hostComboBox->text( i ));
  226. }
  227. // getOptions
  228. // ==========
  229. bool CommonWidget::getOptions( )
  230. { QStringList portRanges;
  231. getPortRanges( &portRanges );
  232. bool portRangesState = m_portRangesCheckBox->isChecked( );
  233. if( portRangesState && portRanges.isEmpty( ))
  234. { KMessageBox::error( this, i18n( QString( "Port ranges not specified" )), i18n( "Port ranges error" ));
  235. return false;
  236. }
  237. m_host = m_hostComboBox->currentText( );
  238. m_hostCurrentItem = m_hostComboBox->currentItem( );
  239. m_portRangeCurrentItem = m_portRangeComboBox->currentItem( );
  240. m_portRangesState = portRangesState;
  241. m_resovleAlwaysState = m_resovleAlwaysRadio->isChecked( );
  242. m_resovleDefaultState = m_resovleDefaultRadio->isChecked( );
  243. m_resovleNeverState = m_resovleNeverRadio->isChecked( );
  244. if( m_portRanges != NULL )
  245. delete m_portRanges;
  246. m_portRanges = new QStringList( portRanges );
  247. getHosts( m_hosts );
  248. disconnect( m_hostComboBox, SIGNAL( activated( int )), this, SLOT( slotHostActivated( int )));
  249. disconnect( m_hostComboBox, SIGNAL( textChanged( const QString& )), this, SLOT( slotHostTextChanged( const QString& )));
  250. disconnect( m_hostComboBox, SIGNAL( activated ( int )), this, SIGNAL( optionsDirty( )));
  251. disconnect( m_hostComboBox, SIGNAL( textChanged( const QString& )), this, SIGNAL( optionsDirty( )));
  252. byte i;
  253. QStringList::Iterator it;
  254. for( i = 0, it = m_hosts->begin( ); it != m_hosts->end( ); i++, ++it )
  255. if( *it == m_host )
  256. break;
  257. if( it != m_hosts->end( ))
  258. { m_hostComboBox->removeItem( i );
  259. m_hosts->remove( it );
  260. }
  261. else
  262. while( m_hostComboBox->count( ) > MAX_HOST_HISTORY - 1 )
  263. { m_hostComboBox->removeItem( MAX_HOST_HISTORY - 1 );
  264. m_hosts->pop_back( );
  265. }
  266. m_hostComboBox->insertItem( m_host, 0 );
  267. m_hostComboBox->setCurrentItem( 0 );
  268. m_hosts->prepend( m_host );
  269. connect( m_hostComboBox, SIGNAL( activated( int )), SLOT( slotHostActivated( int )));
  270. connect( m_hostComboBox, SIGNAL( textChanged( const QString& )), SLOT( slotHostTextChanged( const QString& )));
  271. connect( m_hostComboBox, SIGNAL( activated ( int )), SIGNAL( optionsDirty( )));
  272. connect( m_hostComboBox, SIGNAL( textChanged( const QString& )), SIGNAL( optionsDirty( )));
  273. return true;
  274. }
  275. // getPortRanges
  276. // =============
  277. void CommonWidget::getPortRanges( QStringList* portRanges )
  278. { portRanges->clear( );
  279. for( ushort i = 0; i < m_portRangeListBox->count( ); i++ )
  280. portRanges->append( m_portRangeListBox->text( i ));
  281. }
  282. // portRangesOverlapping
  283. // =====================
  284. bool CommonWidget::portRangesOverlapping( const ushort portFirst, const ushort portLast )
  285. { boolPtr tcpPorts = new bool[ 64 * 1024 ];
  286. boolPtr udpPorts = new bool[ 64 * 1024 ];
  287. if( !buildPortRangesTables( tcpPorts, udpPorts ))
  288. { delete [] tcpPorts;
  289. delete [] udpPorts;
  290. return false;
  291. }
  292. bool tcp = false;
  293. bool udp = false;
  294. switch( m_portRangeComboBox->currentItem( ))
  295. { case 0: tcp = udp = true; break;
  296. case 1: tcp = true; break;
  297. case 2: udp = true; break;
  298. default:
  299. kdDebug( ) << i18n( "unknown value in Port Range Protocol combo box" ) << endl;
  300. ASSERT( false );
  301. delete [] tcpPorts;
  302. delete [] udpPorts;
  303. return false;
  304. }
  305. uint port;
  306. QString protocol;
  307. for( port = portFirst; port <= portLast; port++ )
  308. { if( tcp && tcpPorts[ port ])
  309. { protocol = "TCP";
  310. break;
  311. }
  312. if( udp && udpPorts[ port ])
  313. { protocol = "UDP";
  314. break;
  315. }
  316. }
  317. if( port <= portLast )
  318. { KMessageBox::sorry( this, i18n( "Overlapping Port Range - %1 port %2" ).arg( protocol ).arg( QString::number( port )), i18n( "Overlapping Port Range" ));
  319. delete [] tcpPorts;
  320. delete [] udpPorts;
  321. return false;
  322. }
  323. delete [] tcpPorts;
  324. delete [] udpPorts;
  325. return true;
  326. }
  327. // readProfile
  328. // ===========
  329. void CommonWidget::readProfile( KConfig* config )
  330. { m_hostCurrentItem = config->readNumEntry( "hostCurrentItem" );
  331. m_portRangeCurrentItem = config->readNumEntry( "portRangeCurrentItem" );
  332. m_portRangesState = config->readBoolEntry( "portRangesState" );
  333. m_resovleAlwaysState = config->readBoolEntry( "resovleAlwaysState" );
  334. m_resovleDefaultState = config->readBoolEntry( "resovleDefaultState" );
  335. m_resovleNeverState = config->readBoolEntry( "resovleNeverState" );
  336. m_hosts = new QStringList( config->readListEntry( "hosts" ));
  337. m_portRanges = new QStringList( config->readListEntry( "portRanges" ));
  338. }
  339. // saveProfile
  340. // ===========
  341. void CommonWidget::saveProfile( KConfig* config )
  342. { config->writeEntry( "hostCurrentItem", m_hostCurrentItem );
  343. config->writeEntry( "hosts", *m_hosts );
  344. config->writeEntry( "portRanges", *m_portRanges );
  345. config->writeEntry( "portRangeCurrentItem", m_portRangeCurrentItem );
  346. config->writeEntry( "portRangesState", m_portRangesState );
  347. config->writeEntry( "resovleAlwaysState", m_resovleAlwaysState );
  348. config->writeEntry( "resovleDefaultState", m_resovleDefaultState );
  349. config->writeEntry( "resovleNeverState", m_resovleNeverState );
  350. }
  351. // setInitialValues
  352. // ================
  353. void CommonWidget::setInitialValues( )
  354. { m_hostComboBox->clear( );
  355. m_hostComboBox->insertStringList( *m_hosts );
  356. m_portRangeListBox->insertStringList( *m_portRanges );
  357. m_portRangeComboBox->setCurrentItem( m_portRangeCurrentItem );
  358. m_portRangesCheckBox->setChecked( m_portRangesState );
  359. m_resovleAlwaysRadio->setChecked( m_resovleAlwaysState );
  360. m_resovleDefaultRadio->setChecked( m_resovleDefaultState );
  361. m_resovleNeverRadio->setChecked( m_resovleNeverState );
  362. if( m_hostComboBox->count( ))
  363. m_hostComboBox->setCurrentItem( m_hostCurrentItem );
  364. enablePortRangeWidgets( );
  365. }
  366. // slotDisablePortRanges
  367. // =====================
  368. void CommonWidget::slotDisablePortRanges( )
  369. { m_portRangesCheckBox->setChecked( false );
  370. slotPortRangesClicked( );
  371. }
  372. // slotHostActivated
  373. // =================
  374. void CommonWidget::slotHostActivated( int /* index */)
  375. { emit( targetChanged( m_hostComboBox->currentText( )));
  376. }
  377. // slotHostButtonClicked
  378. // =====================
  379. void CommonWidget::slotHostButtonClicked( )
  380. { m_hostComboBox->clearEdit( );
  381. }
  382. // slotHostTextChanged
  383. // ===================
  384. void CommonWidget::slotHostTextChanged( const QString& text )
  385. { emit( targetChanged( text ));
  386. }
  387. // slotPortRangeAddClicked
  388. // =======================
  389. void CommonWidget::slotPortRangeAddClicked( )
  390. { QString text;
  391. if( !validatePortRange( text ))
  392. return;
  393. switch( m_portRangeComboBox->currentItem( ))
  394. { case BothProtocol: break;
  395. case TCPProtocol: text = "T:" + text; break;
  396. case UDPProtocol: text = "U:" + text; break;
  397. default:
  398. kdDebug( ) << i18n( "unknown value in Port Range Protocol combo box" ) << endl;
  399. ASSERT( false );
  400. }
  401. m_portRangeAddButton->setEnabled( false );
  402. emit( optionsDirty( ));
  403. m_portRangeLineEdit->clear( );
  404. m_portRangeListBox->insertItem( text );
  405. }
  406. // slotPortRangeDeleteClicked
  407. // ==========================
  408. void CommonWidget::slotPortRangeDeleteClicked( )
  409. { m_portRangeIgnoreChange = true;
  410. m_portRangeListBox->removeItem( m_portRangeListBox->currentItem( ));
  411. m_portRangeAddButton->setEnabled( true );
  412. m_portRangeDeleteButton->setEnabled( false );
  413. emit( optionsDirty( ));
  414. }
  415. // slotPortRangeActivated
  416. // ======================
  417. void CommonWidget::slotPortRangeActivated( int /* index */)
  418. { m_portRangeAddButton->setEnabled( true );
  419. }
  420. // slotPortRangeActivated
  421. // ======================
  422. void CommonWidget::slotPortRangeButtonClicked( )
  423. { m_portRangeAddButton->setEnabled( false );
  424. m_portRangeDeleteButton->setEnabled( false );
  425. m_portRangeLineEdit->clear( );
  426. }
  427. // slotPortRangeDoubleClicked
  428. // ==========================
  429. void CommonWidget::slotPortRangeDoubleClicked( QListBoxItem* item )
  430. { if( item == NULL )
  431. return;
  432. QString text = item->text( );
  433. if( text[ 0 ].isDigit( ))
  434. m_portRangeComboBox->setCurrentItem( BothProtocol );
  435. else
  436. { switch( text[ 0 ].latin1( ))
  437. { case 'T': m_portRangeComboBox->setCurrentItem( TCPProtocol ); break;
  438. case 'U': m_portRangeComboBox->setCurrentItem( UDPProtocol ); break;
  439. default:
  440. kdDebug( ) << i18n( "unknown value in Port Range Protocol" ) << endl;
  441. ASSERT( false );
  442. m_portRangeComboBox->setCurrentItem( BothProtocol );
  443. }
  444. text = text.right( text.length( ) - 2 );
  445. }
  446. m_portRangeIgnoreChange = true;
  447. m_portRangeLineEdit->setText( text );
  448. m_portRangeDeleteButton->setEnabled( true );
  449. }
  450. // slotPortRangeHighlighted
  451. // ========================
  452. void CommonWidget::slotPortRangeHighlighted( )
  453. { if( m_portRangeIgnoreChange )
  454. m_portRangeIgnoreChange = false;
  455. else
  456. { m_portRangeAddButton->setEnabled( false );
  457. m_portRangeDeleteButton->setEnabled( false );
  458. m_portRangeLineEdit->clear( );
  459. }
  460. }
  461. // slotPortRangesClicked
  462. // =====================
  463. void CommonWidget::slotPortRangesClicked( )
  464. { enablePortRangeWidgets( );
  465. }
  466. // slotPortRangeTextChanged
  467. // ========================
  468. void CommonWidget::slotPortRangeTextChanged( const QString& /* text */)
  469. { if( m_portRangeIgnoreChange )
  470. m_portRangeIgnoreChange = false;
  471. else
  472. m_portRangeAddButton->setEnabled( m_portRangeLineEdit->text( ).length( ));
  473. }
  474. // slotWhatsThisClicked
  475. // ====================
  476. void CommonWidget::slotWhatsThisClicked( )
  477. { if( m_hostButton->hasMouse( )) emit( displayHelp( "target" ));
  478. else if( m_hostComboBox->hasMouse( )) emit( displayHelp( "target" ));
  479. else if( m_portRangeAddButton->hasMouse( )) emit( displayDocBook( "common_options" ));
  480. else if( m_portRangeButton->hasMouse( )) emit( displayDocBook( "common_options" ));
  481. else if( m_portRangeComboBox->hasMouse( )) emit( displayDocBook( "common_options" ));
  482. else if( m_portRangeDeleteButton->hasMouse( )) emit( displayDocBook( "common_options" ));
  483. else if( m_portRangeLineEdit->hasMouse( )) emit( displayDocBook( "common_options" ));
  484. else if( m_portRangeListBox->hasMouse( )) emit( displayDocBook( "common_options" ));
  485. else if( m_portRangesCheckBox->hasMouse( )) emit( displayHelp( "-p" ));
  486. else if( m_resovleAlwaysRadio->hasMouse( )) emit( displayHelp( "-R" ));
  487. else if( m_resovleDefaultRadio->hasMouse( )) emit( displayDocBook( "common_options" ));
  488. else if( m_resovleNeverRadio->hasMouse( )) emit( displayHelp( "-n" ));
  489. else emit( displayUnknown( ));
  490. }
  491. // validatePortRange
  492. // =================
  493. bool CommonWidget::validatePortRange( QString& text )
  494. { uint portFirst;
  495. uint portLast;
  496. if( !validatePortRangeText( text, portFirst, portLast ))
  497. return false;
  498. if( !portRangesOverlapping( portFirst, portLast ))
  499. return false;
  500. return true;
  501. }
  502. // validatePortRangeText
  503. // =====================
  504. bool CommonWidget::validatePortRangeText( QString& text, uint& portFirst, uint& portLast )
  505. { text = m_portRangeLineEdit->text( );
  506. QStringList ports = QStringList::split( "-", text );
  507. if( ports.count( ) > 2 )
  508. { KMessageBox::error( this, i18n( "Internal error - you seem to have been allowed to entered an illegal Port Range" ), i18n( "Internal error" ));
  509. return false;
  510. }
  511. portFirst = ports.front( ).toUInt( );
  512. portLast = portFirst;
  513. if( ports.count( ) > 1 )
  514. portLast = ports.back( ).toUInt( );
  515. if( !portFirst || portFirst > 65535 || !portLast || portLast > 65535 || portFirst > portLast )
  516. { KMessageBox::sorry( this,
  517. i18n( "Ports must be in the range 1 to 65,535 and ranges must be in ascending order" ),
  518. i18n( "Invalid Port Range" ));
  519. return false;
  520. }
  521. return true;
  522. }