| 1 | #include "mainwindow.h" |
| 2 | #include "ui_mainwindow.h" |
| 3 | |
| 4 | #include <QDebug> |
| 5 | #include <QFile> |
| 6 | #include <QScrollBar> |
| 7 | #include <QClipboard> |
| 8 | #include <QHeaderView> |
| 9 | #include <QFileDialog> |
| 10 | #include <QVector> |
| 11 | #include <QMessageBox> |
| 12 | #include <QSettings> |
| 13 | #include <QDir> |
| 14 | #include <QStandardPaths> |
| 15 | #include <QProcess> |
| 16 | #include <QStyledItemDelegate> |
| 17 | #include <QRegExp> |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include "FormAbout.h" |
| 23 | #include "ProcessExplorer.h" |
| 24 | #include "DialogPasswordPrompt.h" |
| 25 | #include "config_qnetstatview.h" |
| 26 | |
| 27 | Q_DECLARE_METATYPE(QVector<sNetStat>) |
| 28 | |
| 29 | class elideLeftItemC : public QStyledItemDelegate |
| 30 | { |
| 31 | virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 32 | { |
| 33 | QStyleOptionViewItem opt = option; |
| 34 | opt.textElideMode=Qt::ElideLeft; |
| 35 | QStyledItemDelegate::paint(painter, opt, index); |
| 36 | } |
| 37 | } elideLeftItem; |
| 38 | |
| 39 | MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ |
| 40 | ui->setupUi(this); |
| 41 | |
| 42 | qRegisterMetaType < QVector<sNetStat> > ("QVector<sNetStat>"); |
| 43 | |
| 44 | // date start 19.02.2013 |
| 45 | this->setWindowTitle(QString("QNetStatView %1 (%2)").arg(QNETSTATVIEW_VERSION).arg(QNETSTATVIEW_DATEBUILD)); |
| 46 | this->setWindowIcon(QIcon(QString(PATH_USERDATA)+"/images/qnetstatview.png")); |
| 47 | |
| 48 | conf = new QSettings(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)+"/qnetstatview.ini", QSettings::IniFormat); |
| 49 | conf->setPath(QSettings::IniFormat, QSettings::UserScope, QDir::currentPath()); |
| 50 | |
| 51 | |
| 52 | // Создание ContextMenu |
| 53 | initPopupMenu(); |
| 54 | |
| 55 | connect(ui->tableWidget->horizontalHeader(),&QHeaderView::sectionClicked,this,&MainWindow::tableWidget_sectionClicked); |
| 56 | connect(ui->tableWidget, &QWidget::customContextMenuRequested, this, &MainWindow::popupCustomMenu ); |
| 57 | |
| 58 | connect(ui->actionCopy,&QAction::triggered,this,&MainWindow::CopyToClipboard); |
| 59 | connect(ui->actionSave_As,&QAction::triggered,this,&MainWindow::CopyToFile); |
| 60 | connect(ui->actionAbout,&QAction::triggered,this,&MainWindow::showAbout); |
| 61 | |
| 62 | connect(ui->actionRefresh,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 63 | connect(ui->actionRestart_as_Root,&QAction::triggered,this,&MainWindow::restartAsRoot); |
| 64 | connect(ui->actionExit,&QAction::triggered,this,&QWidget::close); |
| 65 | |
| 66 | connect(ui->actionTCP,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 67 | connect(ui->actionTCP6,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 68 | connect(ui->actionUDP,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 69 | connect(ui->actionUDP6,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 70 | |
| 71 | connect(ui->actionESTABLISHED,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 72 | connect(ui->actionLISTEN,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 73 | connect(ui->actionCLOSE,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 74 | connect(ui->actionAllOther,&QAction::triggered,this,&MainWindow::timerUpdate_timeout); |
| 75 | |
| 76 | connect(ui->actionUtilite_for_kill,&QAction::triggered,this,&MainWindow::showSetupKill); |
| 77 | connect(ui->actionResolve_Addresses,&QAction::triggered,this,&MainWindow::Resolve_Addresses); |
| 78 | |
| 79 | connect(ui->action1_second,&QAction::triggered,this,&MainWindow::timer_speed1); |
| 80 | connect(ui->action3_second,&QAction::triggered,this,&MainWindow::timer_speed3); |
| 81 | connect(ui->action5_second,&QAction::triggered,this,&MainWindow::timer_speed5); |
| 82 | connect(ui->actionPause,&QAction::triggered,this,&MainWindow::timer_pause); |
| 83 | |
| 84 | |
| 85 | ui->tableWidget->clear(); |
| 86 | ui->tableWidget->setRowCount(0); |
| 87 | |
| 88 | // init tableWidget |
| 89 | ui->tableWidget->verticalHeader()->hide(); |
| 90 | ui->tableWidget->setColumnCount(8); |
| 91 | ui->tableWidget->sortByColumn(1,Qt::AscendingOrder); |
| 92 | ui->tableWidget->setColumnHidden(0,true); |
| 93 | ui->tableWidget->setItemDelegateForColumn(6, &elideLeftItem); |
| 94 | |
| 95 | ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "" << tr("prot") << tr("local address") << tr("rem address") << tr("state") << tr("pid") << tr("program") << tr("cmdline")); |
| 96 | |
| 97 | ui->tableWidget->horizontalScrollBar()->setMaximum(ui->tableWidget->columnCount()); |
| 98 | |
| 99 | ui->tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu); |
| 100 | ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 101 | ui->tableWidget->setContextMenuPolicy( Qt::CustomContextMenu ); |
| 102 | ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
| 103 | |
| 104 | // default value |
| 105 | ui->tableWidget->setColumnWidth(1,41); |
| 106 | ui->tableWidget->setColumnWidth(2,160); |
| 107 | ui->tableWidget->setColumnWidth(3,160); |
| 108 | ui->tableWidget->setColumnWidth(5,45); |
| 109 | ui->tableWidget->setColumnWidth(7,345); |
| 110 | |
| 111 | |
| 112 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
| 113 | if (getuid()==0){ |
| 114 | ui->actionRestart_as_Root->setVisible(false); |
| 115 | } |
| 116 | #elif defined(Q_OS_WIN) |
| 117 | ui->actionRestart_as_Root->setVisible(false); |
| 118 | #endif |
| 119 | |
| 120 | |
| 121 | netstat = new cNetStat(); |
| 122 | connect(netstat,SIGNAL(endReadStat(QVector<sNetStat>)),this,SLOT(drawTable(QVector<sNetStat>))); |
| 123 | |
| 124 | sortcol=2; |
| 125 | |
| 126 | // Загрузка services |
| 127 | QFile file(":services"); |
| 128 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ |
| 129 | QTextStream in(&file); |
| 130 | while (!in.atEnd()) { |
| 131 | QStringList service = in.readLine().split(QRegularExpression("\\s+")); |
| 132 | if (service.size()<2) continue; |
| 133 | ServicesCache[service.at(1).toUpper()]=service.at(0); |
| 134 | } |
| 135 | file.close(); |
| 136 | } |
| 137 | |
| 138 | timerUpdate_timeout(); |
| 139 | timerUpdate = new QTimer(this); |
| 140 | connect (timerUpdate,SIGNAL(timeout()),this,SLOT(timerUpdate_timeout())); |
| 141 | timerUpdate->start(3000); |
| 142 | |
| 143 | |
| 144 | // restore from config |
| 145 | this->resize(conf->value("form/width",this->width()).toInt(),conf->value("form/height",this->height()).toInt()); |
| 146 | |
| 147 | for (int tableCol=1;tableCol<ui->tableWidget->columnCount();tableCol++){ |
| 148 | ui->tableWidget->setColumnWidth(tableCol,conf->value("tableWidget/column"+QString::number(tableCol),ui->tableWidget->columnWidth(tableCol)).toInt()); |
| 149 | } |
| 150 | |
| 151 | ui->actionResolve_Addresses->setChecked(conf->value("view/resolve","false").toBool()); |
| 152 | ui->actionTCP->setChecked(conf->value("view/TCP","true").toBool()); |
| 153 | ui->actionUDP->setChecked(conf->value("view/UDP","true").toBool()); |
| 154 | ui->actionTCP6->setChecked(conf->value("view/TCP6","true").toBool()); |
| 155 | ui->actionUDP6->setChecked(conf->value("view/UDP6","true").toBool()); |
| 156 | |
| 157 | ui->actionLISTEN->setChecked(conf->value("view/status_listen","true").toBool()); |
| 158 | ui->actionESTABLISHED->setChecked(conf->value("view/status_established","true").toBool()); |
| 159 | ui->actionCLOSE->setChecked(conf->value("view/status_close","true").toBool()); |
| 160 | ui->actionAllOther->setChecked(conf->value("view/status_other","true").toBool()); |
| 161 | |
| 162 | |
| 163 | int speed = conf->value("setup/speed",3).toInt(); |
| 164 | switch (speed) { |
| 165 | case 0: |
| 166 | timer_pause(); |
| 167 | break; |
| 168 | case 1: |
| 169 | timer_speed1(); |
| 170 | break; |
| 171 | case 5: |
| 172 | timer_speed5(); |
| 173 | break; |
| 174 | default: |
| 175 | timer_speed3(); |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | MainWindow::~MainWindow(){ |
| 182 | timerUpdate->stop(); |
| 183 | |
| 184 | conf->setValue("form/width",this->width()); |
| 185 | conf->setValue("form/height",this->height()); |
| 186 | |
| 187 | for (int tableCol=1;tableCol<ui->tableWidget->columnCount();tableCol++){ |
| 188 | conf->setValue("tableWidget/column"+QString::number(tableCol),ui->tableWidget->columnWidth(tableCol)); |
| 189 | } |
| 190 | |
| 191 | conf->setValue("view/resolve",ui->actionResolve_Addresses->isChecked()); |
| 192 | |
| 193 | conf->setValue("view/TCP",ui->actionTCP->isChecked()); |
| 194 | conf->setValue("view/UDP",ui->actionUDP->isChecked()); |
| 195 | conf->setValue("view/TCP6",ui->actionTCP6->isChecked()); |
| 196 | conf->setValue("view/UDP6",ui->actionUDP6->isChecked()); |
| 197 | |
| 198 | conf->setValue("view/status_listen",ui->actionLISTEN->isChecked()); |
| 199 | conf->setValue("view/status_established",ui->actionESTABLISHED->isChecked()); |
| 200 | conf->setValue("view/status_close",ui->actionCLOSE->isChecked()); |
| 201 | conf->setValue("view/status_other",ui->actionAllOther->isChecked()); |
| 202 | |
| 203 | int speed=3; |
| 204 | if (ui->actionPause->isChecked()) speed=0; |
| 205 | if (ui->action1_second->isChecked()) speed=1; |
| 206 | if (ui->action5_second->isChecked()) speed=5; |
| 207 | conf->setValue("setup/speed",speed); |
| 208 | |
| 209 | delete timerUpdate; |
| 210 | delete conf; |
| 211 | delete ui; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void MainWindow::initPopupMenu(){ |
| 216 | menu = new QMenu("my", this); |
| 217 | |
| 218 | actionPropertiesProcess = new QAction(tr("Properties..."),menu); |
| 219 | connect(actionPropertiesProcess,SIGNAL(triggered()),this,SLOT(PropertiesProcess())); |
| 220 | |
| 221 | actionKillProcess = new QAction(tr("Kill process"),menu); |
| 222 | connect(actionKillProcess,SIGNAL(triggered()),this,SLOT(killProcess())); |
| 223 | |
| 224 | actionCloseConnection = new QAction(tr("Close connection"),menu); |
| 225 | connect(actionCloseConnection,SIGNAL(triggered()),this,SLOT(closeConnection())); |
| 226 | |
| 227 | |
| 228 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
| 229 | menu->addAction(actionPropertiesProcess); |
| 230 | #endif |
| 231 | menu->addAction(actionKillProcess); |
| 232 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
| 233 | menu->addAction(actionCloseConnection); |
| 234 | #endif |
| 235 | |
| 236 | |
| 237 | } |
| 238 | void MainWindow::popupCustomMenu( const QPoint &pos ){ |
| 239 | if (ui->tableWidget->currentRow()==-1) return; |
| 240 | |
| 241 | QString proc_id = ui->tableWidget->item(ui->tableWidget->currentRow(),5)->text(); |
| 242 | QString prog = ui->tableWidget->item(ui->tableWidget->currentRow(),6)->text(); |
| 243 | |
| 244 | if (proc_id.isEmpty() or proc_id=="0" or prog=="-"){ |
| 245 | actionPropertiesProcess->setEnabled(false); |
| 246 | actionKillProcess->setEnabled(false); |
| 247 | }else{ |
| 248 | actionPropertiesProcess->setEnabled(true); |
| 249 | actionKillProcess->setEnabled(true); |
| 250 | } |
| 251 | menu->popup(ui->tableWidget->mapToGlobal(pos)); |
| 252 | } |
| 253 | |
| 254 | void MainWindow::killProcess(){ |
| 255 | ProcessList::killProcess(ui->tableWidget->item(ui->tableWidget->currentRow(),5)->text()); |
| 256 | } |
| 257 | |
| 258 | void MainWindow::PropertiesProcess(){ |
| 259 | int pid = ui->tableWidget->item(ui->tableWidget->currentRow(),5)->text().toInt(); |
| 260 | |
| 261 | ProcessExplorer form(pid,this); |
| 262 | form.exec(); |
| 263 | } |
| 264 | |
| 265 | void MainWindow::closeConnection(){ |
| 266 | #if defined(Q_OS_WIN) |
| 267 | QMessageBox::critical(this,tr("close connection"),tr("This function is not implemented for this OS")); |
| 268 | return; |
| 269 | #else |
| 270 | if (getuid()==0){ |
| 271 | if (ui->tableWidget->item(ui->tableWidget->currentRow(),4)->text() == "ESTABLISHED"){ |
| 272 | ProcessList::closeConnection(ui->tableWidget->item(ui->tableWidget->currentRow(),0)->text().split("/").at(0),ui->tableWidget->item(ui->tableWidget->currentRow(),0)->text().split("/").at(1)); |
| 273 | }else{ |
| 274 | QMessageBox::critical(this,tr("close connection"),tr("Only ESTABLISHED")); |
| 275 | } |
| 276 | }else{ |
| 277 | QMessageBox::critical(this,tr("close connection"),tr("Only root can do this")); |
| 278 | } |
| 279 | #endif |
| 280 | |
| 281 | } |
| 282 | |
| 283 | void MainWindow::tableWidget_sectionClicked(int col){ |
| 284 | sortcol=col; |
| 285 | timerUpdate_timeout(); |
| 286 | } |
| 287 | |
| 288 | void MainWindow::showAbout(){ |
| 289 | FormAbout form(this); |
| 290 | form.exec(); |
| 291 | } |
| 292 | |
| 293 | void MainWindow::CopyToClipboard(){ |
| 294 | if (ui->tableWidget->currentRow()==-1){ |
| 295 | QMessageBox::critical(this,tr("Copy to clipboard"),tr("First select the line for copying")); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | QString buffer=""; |
| 301 | QClipboard *clipboard = QApplication::clipboard(); |
| 302 | |
| 303 | for (int i=0;i<ui->tableWidget->columnCount();i++){ |
| 304 | buffer+=ui->tableWidget->item(ui->tableWidget->currentRow(),i)->text() +";"; |
| 305 | } |
| 306 | |
| 307 | clipboard->setText(buffer); |
| 308 | |
| 309 | QMessageBox::information(this,tr("Copy to clipboard"),tr("Line was successfully copied to the clipboard")); |
| 310 | } |
| 311 | |
| 312 | void MainWindow::CopyToFile(){ |
| 313 | int interval = timerUpdate->interval(); |
| 314 | timerUpdate->stop(); |
| 315 | QString fileName = QFileDialog::getSaveFileName(this,tr("Save to file"), "", tr("All Files (*.*)")); |
| 316 | if (fileName.size() > 0){ |
| 317 | QFile file(fileName); |
| 318 | if (file.open(QIODevice::WriteOnly | QIODevice::Text)){ |
| 319 | QTextStream out(&file); |
| 320 | |
| 321 | for (int r=0;r<ui->tableWidget->rowCount();r++){ |
| 322 | for (int i=0;i<ui->tableWidget->columnCount();i++){ |
| 323 | out << ui->tableWidget->item(r,i)->text() +"\t"; |
| 324 | } |
| 325 | out << "\n"; |
| 326 | } |
| 327 | |
| 328 | file.close(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | timerUpdate->start(interval); |
| 333 | } |
| 334 | |
| 335 | void MainWindow::Resolve_Addresses(){ |
| 336 | timerUpdate_timeout(); |
| 337 | } |
| 338 | |
| 339 | void MainWindow::lookedUp(const QHostInfo &host){ |
| 340 | if (host.error() != QHostInfo::NoError) { |
| 341 | //qDebug() << "Lookup failed:" << host.errorString(); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | foreach (const QHostAddress &address, host.addresses()) |
| 346 | DNSCache[address.toString()]=host.hostName(); |
| 347 | } |
| 348 | |
| 349 | QString MainWindow::resolve(QString ipaddr,const QString &prot){ |
| 350 | if (ipaddr.split(":").size()!=2) return ipaddr; |
| 351 | if (ipaddr=="*") return ipaddr; |
| 352 | |
| 353 | if (DNSCache[ipaddr.split(":").at(0)] != ""){ |
| 354 | ipaddr=DNSCache[ipaddr.split(":").at(0)]+":"+ipaddr.split(":").at(1); |
| 355 | } |
| 356 | if (ServicesCache[ipaddr.split(":").at(1)+"/"+prot] != ""){ |
| 357 | ipaddr=ipaddr.split(":").at(0)+":"+ServicesCache[ipaddr.split(":").at(1)+"/"+prot]; |
| 358 | } |
| 359 | |
| 360 | return ipaddr; |
| 361 | } |
| 362 | |
| 363 | void MainWindow::timer_speed1(){ |
| 364 | ui->action1_second->setChecked(true); |
| 365 | ui->action3_second->setChecked(false); |
| 366 | ui->action5_second->setChecked(false); |
| 367 | ui->actionPause->setChecked(false); |
| 368 | |
| 369 | timerUpdate_timeout(); |
| 370 | timerUpdate->start(1000); |
| 371 | } |
| 372 | void MainWindow::timer_speed3(){ |
| 373 | ui->action1_second->setChecked(false); |
| 374 | ui->action3_second->setChecked(true); |
| 375 | ui->action5_second->setChecked(false); |
| 376 | ui->actionPause->setChecked(false); |
| 377 | timerUpdate_timeout(); |
| 378 | timerUpdate->start(3000); |
| 379 | } |
| 380 | void MainWindow::timer_speed5(){ |
| 381 | ui->action1_second->setChecked(false); |
| 382 | ui->action3_second->setChecked(false); |
| 383 | ui->action5_second->setChecked(true); |
| 384 | ui->actionPause->setChecked(false); |
| 385 | timerUpdate_timeout(); |
| 386 | timerUpdate->start(5000); |
| 387 | } |
| 388 | void MainWindow::timer_pause(){ |
| 389 | ui->action1_second->setChecked(false); |
| 390 | ui->action3_second->setChecked(false); |
| 391 | ui->action5_second->setChecked(false); |
| 392 | ui->actionPause->setChecked(true); |
| 393 | |
| 394 | timerUpdate->stop(); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | void MainWindow::restartAsRoot(){ |
| 399 | this->hide(); |
| 400 | QProcess::startDetached(QApplication::applicationDirPath()+"/"+qAppName(),QStringList() << "--run-as-root"); |
| 401 | exit(0); |
| 402 | } |
| 403 | |
| 404 | void MainWindow::drawTable(QVector<sNetStat> newNetStat){ |
| 405 | |
| 406 | QString key=""; |
| 407 | int horizontalScrollBarIndex = 0; |
| 408 | int verticalScrollBarIndex = 0; |
| 409 | int listen=0; |
| 410 | int established=0; |
| 411 | QMap<int,int> oldSizeCols; |
| 412 | |
| 413 | if (ui->tableWidget->currentRow()>=0){ |
| 414 | key = ui->tableWidget->item(ui->tableWidget->currentRow(),2)->text() +"+"+ui->tableWidget->item(ui->tableWidget->currentRow(),3)->text(); |
| 415 | } |
| 416 | verticalScrollBarIndex = ui->tableWidget->verticalScrollBar()->value(); |
| 417 | horizontalScrollBarIndex = ui->tableWidget->horizontalScrollBar()->value(); |
| 418 | |
| 419 | for (int i=0;i<ui->tableWidget->columnCount();i++) oldSizeCols[i]=ui->tableWidget->columnWidth(i); |
| 420 | |
| 421 | |
| 422 | ui->tableWidget->setRowCount(0); |
| 423 | |
| 424 | int row=0; |
| 425 | for (int i=0;i<newNetStat.size();i++){ |
| 426 | // filter prot |
| 427 | if (newNetStat.at(i).prot=="TCP"){ |
| 428 | if (ui->actionTCP->isChecked()!=true) continue; |
| 429 | }else if (newNetStat.at(i).prot=="UDP"){ |
| 430 | if (ui->actionUDP->isChecked()!=true) continue; |
| 431 | }else if (newNetStat.at(i).prot=="TCP6"){ |
| 432 | if (ui->actionTCP6->isChecked()!=true) continue; |
| 433 | }else if (newNetStat.at(i).prot=="UDP6"){ |
| 434 | if (ui->actionUDP6->isChecked()!=true) continue; |
| 435 | } |
| 436 | |
| 437 | // filter state |
| 438 | if (newNetStat.at(i).stat=="ESTABLISHED" or newNetStat.at(i).stat=="ESTABLISH"){ |
| 439 | if (ui->actionESTABLISHED->isChecked()!=true) continue; |
| 440 | }else if(newNetStat.at(i).stat=="LISTEN" or newNetStat.at(i).stat=="LISTENING"){ |
| 441 | if (ui->actionLISTEN->isChecked()!=true) continue; |
| 442 | }else if(newNetStat.at(i).stat=="CLOSE"){ |
| 443 | if (ui->actionCLOSE->isChecked()!=true) continue; |
| 444 | }else{ |
| 445 | if (ui->actionAllOther->isChecked()!=true) continue; |
| 446 | } |
| 447 | |
| 448 | QString local_address = newNetStat.at(i).local_address; |
| 449 | QString rem_address = newNetStat.at(i).rem_address; |
| 450 | QString pid = newNetStat.at(i).process.pid; |
| 451 | |
| 452 | if (!ui->lineEdit->text().isEmpty()){ |
| 453 | if (ui->lineEdit->text()!=rem_address.split(":").at(0) and |
| 454 | ui->lineEdit->text()!=local_address.split(":").at(0) and |
| 455 | ui->lineEdit->text()!=pid and |
| 456 | newNetStat.at(i).process.program.toLower().indexOf(ui->lineEdit->text().toLower())==-1 |
| 457 | ){ |
| 458 | continue; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1); |
| 464 | |
| 465 | QTableWidgetItem *itemKey = new QTableWidgetItem(local_address+"/"+rem_address); |
| 466 | ui->tableWidget->setItem(row, 0, itemKey); |
| 467 | |
| 468 | if (local_address.split(":").at(0) != "*"){ |
| 469 | if (DNSCache[local_address.split(":").at(0)] == ""){ |
| 470 | QHostInfo::lookupHost(local_address.split(":").at(0),this,SLOT(lookedUp(QHostInfo))); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (rem_address != "*"){ |
| 475 | if (DNSCache[rem_address.split(":").at(0)] == ""){ |
| 476 | QHostInfo::lookupHost(rem_address.split(":").at(0),this,SLOT(lookedUp(QHostInfo))); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (ui->actionResolve_Addresses->isChecked()==true){ |
| 481 | local_address=resolve(local_address,newNetStat.at(i).prot); |
| 482 | rem_address=resolve(rem_address,newNetStat.at(i).prot); |
| 483 | } |
| 484 | |
| 485 | QTableWidgetItem *itemProt = new QTableWidgetItem(newNetStat.at(i).prot); |
| 486 | ui->tableWidget->setItem(row, 1, itemProt); |
| 487 | |
| 488 | QTableWidgetItem *itemLocalAddress = new QTableWidgetItem(local_address); |
| 489 | ui->tableWidget->setItem(row, 2, itemLocalAddress); |
| 490 | |
| 491 | QTableWidgetItem *itemRemoteAddress = new QTableWidgetItem(rem_address); |
| 492 | ui->tableWidget->setItem(row, 3, itemRemoteAddress); |
| 493 | |
| 494 | QTableWidgetItem *itemStat = new QTableWidgetItem(newNetStat.at(i).stat); |
| 495 | ui->tableWidget->setItem(row, 4, itemStat); |
| 496 | |
| 497 | QTableWidgetItem *itemPID = new QTableWidgetItem(newNetStat.at(i).process.pid); |
| 498 | ui->tableWidget->setItem(row, 5, itemPID); |
| 499 | |
| 500 | QTableWidgetItem *itemProg = new QTableWidgetItem(newNetStat.at(i).process.program); |
| 501 | ui->tableWidget->setItem(row, 6, itemProg); |
| 502 | ui->tableWidget->item(row,6)->setToolTip(itemProg->text()); |
| 503 | |
| 504 | QTableWidgetItem *itemCmdLine = new QTableWidgetItem(newNetStat.at(i).process.cmdline); |
| 505 | ui->tableWidget->setItem(row, 7, itemCmdLine); |
| 506 | ui->tableWidget->item(row,7)->setToolTip(itemCmdLine->text()); |
| 507 | |
| 508 | for (int c=1;c<ui->tableWidget->columnCount();c++){ |
| 509 | if (newNetStat.at(i).operation==1){ |
| 510 | ui->tableWidget->item(row,c)->setBackground(QBrush(QColor(0,255,0))); |
| 511 | } |
| 512 | if (newNetStat.at(i).operation==2){ |
| 513 | ui->tableWidget->item(row,c)->setBackground(QBrush(QColor(255,0,0))); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (local_address+"+"+rem_address ==key){ |
| 518 | ui->tableWidget->selectRow(row); |
| 519 | } |
| 520 | |
| 521 | ui->tableWidget->setRowHeight(row,18); |
| 522 | |
| 523 | if (newNetStat.at(i).stat=="LISTEN" or newNetStat.at(i).stat=="LISTENING") listen++; |
| 524 | if (newNetStat.at(i).stat=="ESTABLISHED" or newNetStat.at(i).stat=="ESTABLISH") established++; |
| 525 | |
| 526 | row++; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | // Sorting and restoring the cursor position |
| 531 | if (ui->tableWidget->rowCount()>0){ |
| 532 | ui->tableWidget->sortByColumn(sortcol,Qt::AscendingOrder); |
| 533 | |
| 534 | ui->tableWidget->verticalScrollBar()->setMaximum(ui->tableWidget->rowCount()); |
| 535 | ui->tableWidget->verticalScrollBar()->setValue(verticalScrollBarIndex); |
| 536 | |
| 537 | |
| 538 | for (int i=0;i<ui->tableWidget->columnCount();i++) ui->tableWidget->setColumnWidth(i,oldSizeCols[i]); |
| 539 | |
| 540 | ui->tableWidget->horizontalScrollBar()->setMaximum(ui->tableWidget->columnCount()); |
| 541 | ui->tableWidget->horizontalScrollBar()->setValue(horizontalScrollBarIndex); |
| 542 | } |
| 543 | |
| 544 | ui->label->setText(tr("Count connections: ")+QString::number(row)); |
| 545 | ui->label_2->setText(tr("Count LISTEN: ")+QString::number(listen)); |
| 546 | ui->label_3->setText(tr("Count ESTABLISHED: ")+QString::number(established)); |
| 547 | |
| 548 | } |
| 549 | |
| 550 | void MainWindow::timerUpdate_timeout(){ |
| 551 | netstat->start(); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | void MainWindow::showSetupKill(){ |
| 556 | ui->tableWidget->horizontalScrollBar()->setValue(ui->tableWidget->horizontalScrollBar()->maximum()); |
| 557 | |
| 558 | setupUtiliteKill frm; |
| 559 | frm.exec(); |
| 560 | } |