Menu

[11a223]: / controller.cpp  Maximize  Restore  History

Download this file

226 lines (193 with data), 8.6 kB

  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
/*
Copyright (C) 2015-2016 Jan Christian Rohde
This file is part of netObservator.
netObservator 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 3
of the License, or (at your option) any later version.
netObservator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with netObservator; if not, see http://www.gnu.org/licenses.
*/
#include "controller.h"
Controller::Controller(Model *m)
: model(m),
packetTrafficView(TrafficPacketInfoPresenter::infoType::PACKETS),
byteTrafficView(TrafficPacketInfoPresenter::infoType::BYTES)
{
settings = new SettingsDialog;
viewSettingsDialog = new ViewSettingsDialog;
statisticsDialog = new StatisticsDialog(&statisticsView);
packetTrafficDialog = new TrafficDialog(&packetTrafficView);
byteTrafficDialog = new TrafficDialog(&byteTrafficView);
searchTabDialog = new SearchDialog(&tabStrategy);
searchFilesDialog = new SearchDialog(&filesStrategy);
filterEditor = new PacketFilterEditor;
model->view.setStatistics(&statisticsView);
model->server.registerObserver(&tabStrategy);
model->sniff.registerObserver(&filesStrategy);
model->sniff.registerObserver(packetTrafficDialog);
model->sniff.registerObserver(byteTrafficDialog);
packetTrafficView.parser = &model->parser;
byteTrafficView.parser = &model->parser;
statisticsView.parser = &model->parser;
QObject::connect(searchTabDialog,SIGNAL(sig(SearchCommand&)),this,SLOT(search(SearchCommand&)));
QObject::connect(&tabStrategy,SIGNAL(browse(bool)),this,SLOT(changeText(bool)));
QObject::connect(searchFilesDialog,SIGNAL(sig(SearchCommand&)),this,SLOT(search(SearchCommand&)));
QObject::connect(viewSettingsDialog,SIGNAL(change(viewSettings&)),this,SLOT(getSettings(viewSettings&)));
QObject::connect(settings,SIGNAL(change(sniffSettings&)),this,SLOT(getSniffSettings(sniffSettings&)));
QObject::connect(filterEditor,SIGNAL(commit(QString)),this,SLOT(getFilter(QString)));
QObject::connect(statisticsDialog,SIGNAL(closeSignal()),this,SLOT(removeStatisticsView()));
QObject::connect(packetTrafficDialog,SIGNAL(closeSignal()),this,SLOT(removePacketView()));
QObject::connect(byteTrafficDialog,SIGNAL(closeSignal()),this,SLOT(removeByteView()));
QObject::connect(this,SIGNAL(packets(int)),packetTrafficDialog,SLOT(getForPlot(int)));
QObject::connect(this,SIGNAL(bytes(int)),byteTrafficDialog,SLOT(getForPlot(int)));
model->parser.firePacketNumber = [&](traffic &data) {emit packets(data.packetNumber);};
model->parser.fireByteNumber = [&](traffic &data) {emit bytes(data.packetNumber);};
}
void Controller::setModelView(modelView *mv) {
model->view.set(&mv->view);
statisticsView.getContent = mv->view.getContent;
packetTrafficView.getContent = mv->view.getContent;
byteTrafficView.getContent = mv->view.getContent;
mv->view.parser = &model->parser;
mv->model.parser = &model->parser;
model->parser.setPacketInfoPresenter(&mv->view.tablePacketInfo);
model->server.set(&mv->model);
}
bool Controller::isSomeDialogOpened() {
return fileDialog.isVisible()
|| (searchTabDialog && searchTabDialog->isVisible())
|| (searchFilesDialog && searchFilesDialog->isVisible())
|| (settings && settings->isVisible())
|| (filterEditor && filterEditor->isVisible());
}
void Controller::executeCommand(Command cmd) {
CommandCode command = cmd.code;
if (command == CommandCode::LOAD)
load();
else if (command == CommandCode::LOADFOLDER)
loadFolder();
else if (command == CommandCode::LOADFILE)
model->server.load(cmd.arguments[0]);
else if (command == CommandCode::ADDTAB)
emit addTab();
else if (command == CommandCode::CLEAR)
model->server.clear();
else if (command == CommandCode::SAVEFILE)
saveFile();
else if (command == CommandCode::SAVEFILEAS)
saveFileAs();
else if (command == CommandCode::BACK)
model->server.changeText(false);
else if (command == CommandCode::FORWARD)
model->server.changeText(true);
else if (command == CommandCode::SHOWPACKETFILTEREDITOR)
filterEditor->show();
else if (command == CommandCode::SHOWSEARCHTABDIALOG)
searchTabDialog->show();
else if (command == CommandCode::SHOWSEARCHFILEDIALOG)
searchFilesDialog->show();
else if (command == CommandCode::SHOWSETTINGSDIALOG)
settings->show();
else if (command == CommandCode::SHOWCOLUMNSETTINGSDIALOG)
viewSettingsDialog->show();
else if (command == CommandCode::SHOWSTATISTICSDIALOG) {
model->view.attachStatistics(true);
statisticsView.setSliceNames(model->server.getSliceNames());
statisticsDialog->show();
}
else if (command == CommandCode::LOADSLICE)
model->server.loadSlice(cmd.arguments[0]);
else if (command == CommandCode::SHOWTRAFFICDIALOG) {
model->server.registerObserver(&packetTrafficView);
packetTrafficView.setSliceNames(model->server.getSliceNames());
packetTrafficDialog->show();
model->view.setPacketPlot(true);
}
else if (command == CommandCode::SHOWBYTEDIALOG) {
model->server.registerObserver(&byteTrafficView);
byteTrafficView.setSliceNames(model->server.getSliceNames());
byteTrafficDialog->show();
model->view.setBytePlot(true);
}
else if (command == CommandCode::CHANGEDEVICE) {
std::vector<ipAddress> localAddresses;
model->sniff.devs.getLocalIpAddresses(cmd.arguments[0].toInt(),localAddresses);
model->parser.update(localAddresses);
}
else if (command == CommandCode::SNIFF)
handleSniffing(cmd);
}
void Controller::load() {
QString filename = fileDialog.getOpenFileName(
NULL,
QObject::tr("Open Document"),
QDir::currentPath(),
QObject::tr("") );
if (!filename.isEmpty() && !model->server.isEmpty())
addTab();
model->server.load(filename);
}
void Controller::loadFolder() {
QString folderName = fileDialog.getExistingDirectory(
NULL,
QObject::tr("Open Document"),
QDir::currentPath() );
if (!folderName.isEmpty() && !model->server.isEmpty())
addTab();
model->server.loadFolder(folderName);
}
void Controller::saveFile() {
if (lastSavedFileName.size() > 0)
model->server.save(lastSavedFileName);
else
saveFileAs();
}
void Controller::saveFileAs() {
QString fileName = fileDialog.getSaveFileName(NULL,QObject::tr("Save"),"Capture",QObject::tr("pcap (*.pcap)"));
if(fileName.size() > 0) {
model->server.save(fileName);
lastSavedFileName = fileName;
}
}
void Controller::handleSniffing(Command cmd) {
if (model->sniff.isSniffing())
model->sniff.stop();
else {
if (statisticsDialog && statisticsDialog->isVisible())
setErrorMessage(QString("One cannot start to sniff while the Statistics Dialog is opened."),QMessageBox::Warning);
else {
model->parser.setPacketInfoPresenter(&model->view.view->tablePacketInfo);
model->sniff.start(cmd.arguments[0].toInt());
}
}
}
void Controller::getCommand(Command command) {
if (command.code != CommandCode::SNIFF && model->sniff.isSniffing()) {
setErrorMessage(QString("The selected action cannot be performed while the program is sniffing."),QMessageBox::Warning);
}
else if (isSomeDialogOpened()) {
setErrorMessage(QString("The selected action cannot be performed while some other sub dialog is opened."),QMessageBox::Warning);
}
else
executeCommand(command);
}
void Controller::getComposition(ViewComposition &composition) {
}
void Controller::search(SearchCommand &command) {
model->server.search(command);
}
void Controller::changeText(bool forward) {
model->server.changeText(forward);
}
void Controller::getSettings(viewSettings &set) {
model->view.update(set);
searchTabDialog->update(set);
searchFilesDialog->update(set);
}
void Controller::getSniffSettings(sniffSettings &settings) {
model->sniff.update(settings);
model->parser.setDurationTime(settings.duration);
}
MongoDB Logo MongoDB