Brak opisu

mainwindow.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // [RAN 2014-06-02] Fixed switch statements.
  2. #include "mainwindow.h"
  3. #include "ui_mainwindow.h"
  4. /// \fn MainWindow::MainWindow()
  5. /// \~English
  6. /// \brief Constructor
  7. /// \~Spanish
  8. /// \brief Constructor
  9. MainWindow::MainWindow(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::MainWindow)
  12. {
  13. ui->setupUi(this);
  14. filterMethod = 0 ;
  15. }
  16. /// \fn MainWindow::~MainWindow()
  17. /// \~English
  18. /// \brief Destructor
  19. /// \~Spanish
  20. /// \brief Destructor
  21. MainWindow::~MainWindow()
  22. {
  23. delete ui;
  24. }
  25. /// \fn void MainWindow::on_Filter_Box_currentIndexChanged(int index)
  26. /// \~English
  27. /// \brief Changes the ID of the filtering function chosen by the user
  28. /// \~Spanish
  29. /// \brief Cambia el ID de la funcion de filtro escogida por el usaurio
  30. void MainWindow::on_Filter_Box_currentIndexChanged(int index)
  31. {
  32. filterMethod = index ;
  33. ui->lineEdit->clear();
  34. }
  35. /// \fn void MainWindow::on_actionLoad_Network_Data_triggered()
  36. /// \~English
  37. /// \brief Loads a network flow file into the GUI.
  38. /// \~Spanish
  39. /// \brief Carga un archivo de fluidos de red a la inteface de usuario.
  40. void MainWindow::on_actionLoad_Network_Data_triggered()
  41. {
  42. QString fname = QFileDialog::getOpenFileName(this, tr("Choose a file"), QDir::currentPath());
  43. if (!fname.isEmpty())
  44. {
  45. if(!ReadFile(fname.toStdString(), netdata)){
  46. QMessageBox::information(this, tr("Choose a correct path"),tr("Cannot load %1.").arg(fname));
  47. netdata.clear();
  48. }
  49. FillTable();
  50. }
  51. }
  52. /// \fn void MainWindow::on_actionAbout_triggered()
  53. /// \~English
  54. /// \brief Displays a short about of the app
  55. /// \~Spanish
  56. /// \brief Despliega un sobre corto de la aplicacion
  57. void MainWindow::on_actionAbout_triggered()
  58. {
  59. //display a message box
  60. QMessageBox::about(this, tr("About Network Filter"),
  61. tr("Network Traffic Analyzer"
  62. "\n\nThe program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY "
  63. "OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE."));
  64. }
  65. /// \fn void MainWindow::on_actionExit_triggered()
  66. /// \~English
  67. /// \brief Exits the app
  68. /// \~Spanish
  69. /// \brief Sale de la aplicacion
  70. void MainWindow::on_actionExit_triggered()
  71. {
  72. QApplication::quit();
  73. }
  74. /// \fn void MainWindow::FillTable()
  75. /// \~English
  76. /// \brief Refresh the GUI packet list.
  77. /// \~Spanish
  78. /// \brief Rellena la lista de paquetes de la interface de usuarios.
  79. void MainWindow::FillTable()
  80. {
  81. numPackets = 0;
  82. numOctects = 0;
  83. ui->table->setRowCount(0);
  84. for(unsigned int i=0; i<netdata.size(); i++){
  85. if (!netdata[i].isEnabled()){
  86. continue ;
  87. }
  88. ui->table->insertRow(ui->table->rowCount());
  89. numPackets += netdata[i].getPackets() ;
  90. numOctects += netdata[i].getOctects() ;
  91. // cout << netdata[i].getSrcPort() << endl ;
  92. ui->table->setItem(ui->table->rowCount()-1,0, new QTableWidgetItem(QString::fromStdString(netdata[i].getSrcAddr()))) ;
  93. ui->table->setItem(ui->table->rowCount()-1,1, new QTableWidgetItem(QString::fromStdString(netdata[i].getDstAddr()))) ;
  94. ui->table->setItem(ui->table->rowCount()-1,2, new QTableWidgetItem(QString::number(netdata[i].getSrcPort()))) ;
  95. ui->table->setItem(ui->table->rowCount()-1,3, new QTableWidgetItem(QString::number(netdata[i].getDstPort()))) ;
  96. ui->table->setItem(ui->table->rowCount()-1,4, new QTableWidgetItem(QString::number(netdata[i].getOctects()))) ;
  97. ui->table->setItem(ui->table->rowCount()-1,5, new QTableWidgetItem(QString::number(netdata[i].getPackets()))) ;
  98. }
  99. ui->Octets_Label->setText("Octets: "+QString::number(numOctects));
  100. ui->Packets_Label->setText("Packets: "+QString::number(numPackets));
  101. if(ui->table->rowCount()>0)
  102. {
  103. ui->Octets_Average_Label->setText("Octets Average: "+QString::number(float(numOctects/ui->table->rowCount())));
  104. ui->Packets_Average_Label->setText("Packets Average: "+QString::number(float(numPackets/ui->table->rowCount())));
  105. }
  106. else
  107. {
  108. ui->Octets_Average_Label->setText("Octets Average: ERROR! ");
  109. ui->Packets_Average_Label->setText("Packets Average: ERROR! ");
  110. }
  111. }
  112. //================================================================================================================
  113. /// \fn void MainWindow::on_runFilter_clicked()
  114. /// \~English
  115. /// \brief Invokes a filtering funtion that filter by the field chosen by the user.
  116. /// \~Spanish
  117. /// \brief Invoca una funcion de filtrar por el campo escogido por el usuario.
  118. void MainWindow::on_runFilter_clicked()
  119. {
  120. //cout << "FILTER" << filterMethod <<endl ;
  121. if(ui->lineEdit->text().length() < 1)
  122. return ;
  123. switch(filterMethod){
  124. case 0:
  125. FilterBySrcAddr(netdata, ui->lineEdit->text().toStdString());
  126. break ;
  127. case 1:
  128. FilterByDstAddr(netdata, ui->lineEdit->text().toStdString());
  129. break ;
  130. case 2:
  131. FilterBySrcPort(netdata, ui->lineEdit->text().toInt()) ;
  132. break ;
  133. case 3:
  134. FilterByDstPort(netdata, ui->lineEdit->text().toInt()) ;
  135. break ;
  136. default:
  137. break ;
  138. } ;
  139. FillTable() ;
  140. }
  141. /// \fn void MainWindow::on_clearButton_clicked()
  142. /// \~English
  143. /// \brief Removes any applied filter to the list of packets.
  144. /// \~Spanish
  145. /// \brief Remueve cualquier filtro aplicado a la lista de paquetes.
  146. void MainWindow::on_clearButton_clicked()
  147. {
  148. for(unsigned int i = 0; i < netdata.size(); i++)
  149. netdata[i].enable() ;
  150. FillTable() ;
  151. }
  152. /// \fn void MainWindow::on_sortButton_clicked()
  153. /// \~English
  154. /// \brief Invokes a sorting funtion that sorts by the field chosen by the user.
  155. /// \~Spanish
  156. /// \brief Invoca una funcion de ordenar por el campo escogido por el usuario.
  157. void MainWindow::on_sortButton_clicked()
  158. {
  159. switch(filterMethod){
  160. case 0:
  161. SortBySrcAddr(netdata);
  162. break ;
  163. case 1:
  164. SortByDstAddr(netdata);
  165. break ;
  166. case 2:
  167. SortBySrcPort(netdata) ;
  168. break ;
  169. case 3:
  170. SortByDstPort(netdata) ;
  171. break ;
  172. default:
  173. break ;
  174. } ;
  175. FillTable() ;
  176. }
  177. /// \fn void MainWindow::on_openFile_clicked()
  178. /// \~English
  179. /// \brief Open a file network flow packets.
  180. /// \~Spanish
  181. /// \brief Abre un archivo con paquetes de fluidos de red.
  182. void MainWindow::on_openFile_clicked()
  183. {
  184. on_actionLoad_Network_Data_triggered() ;
  185. }
  186. /// \fn void MainWindow::on_saveFile_clicked()
  187. /// \~English
  188. /// \brief Saves the network packets as appears in the window.
  189. /// \~Spanish
  190. /// \brief Guarda los paquetes de network como aparecen en la pantalla.
  191. void MainWindow::on_saveFile_clicked()
  192. {
  193. QString fname = QFileDialog::getSaveFileName(this, tr("Choose a file"), QDir::currentPath()) ;
  194. if (!fname.isEmpty())
  195. {
  196. if(!SaveFile(fname.toStdString(), netdata))
  197. QMessageBox::information(this, tr("Choose a correct path"),tr("Cannot save %1.").arg(fname));
  198. }
  199. }