bigsnitch/mainwindow.cpp
2021-03-19 17:40:33 +01:00

127 lines
4.8 KiB
C++

#include "mainwindow.h"
#include <historymodel.h>
#include "./ui_mainwindow.h"
#include <QtGui>
#include "networkthread.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
qRegisterMetaType<http::Flow>("httpflow");
current_session = new Session();
current_session->load("/tmp/littlesnitch.session");
plugin_handler.loadPlugins(QDir("/home/end/projects/bigsnitch/build/plugins/"));
/*
thread = new QThread;
NetworkThread* worker = new NetworkThread();
worker->moveToThread(thread);
connect(thread, SIGNAL (started()), worker, SLOT (process()));
//connect(worker, SIGNAL (error(QString)), this, SLOT (errorString(QString)));
connect(worker, SIGNAL (httpMessage(http::Flow)), current_session, SLOT (saveFlow(http::Flow)), Qt::QueuedConnection);
connect(worker, SIGNAL (httpMessage(http::Flow)), this, SLOT (updateHistory()), Qt::QueuedConnection);
thread->start();
*/
history_model.update(current_session->getHistoryItems());
history_proxy.setSourceModel(&history_model);
ui->setupUi(this);
ui->historyHTTPTable->setShowGrid(true);
ui->historyHTTPTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->historyHTTPTable->setModel(&history_proxy);
connect(ui->historyHTTPTable->selectionModel(), SIGNAL (selectionChanged(QItemSelection, QItemSelection)), this, SLOT (on_selectionChange(QItemSelection)));
ui->historyHTTPTable->setContextMenuPolicy(Qt::CustomContextMenu);
ui->requestHeaderEdit->setContextMenuPolicy(Qt::CustomContextMenu);
ui->requestDataEdit->setContextMenuPolicy(Qt::CustomContextMenu);
ui->responseHeaderEdit->setContextMenuPolicy(Qt::CustomContextMenu);
ui->responseDataEdit->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->historyHTTPTable, SIGNAL(customContextMenuRequested(QPoint)), SLOT(historyMenuRequested(QPoint)));
connect(ui->requestHeaderEdit, SIGNAL(customContextMenuRequested(QPoint)), SLOT(editMenuRequested(QPoint)));
connect(ui->requestDataEdit, SIGNAL(customContextMenuRequested(QPoint)), SLOT(edit2MenuRequested(QPoint)));
connect(ui->responseHeaderEdit, SIGNAL(customContextMenuRequested(QPoint)), SLOT(edit3MenuRequested(QPoint)));
connect(ui->responseDataEdit, SIGNAL(customContextMenuRequested(QPoint)), SLOT(edit4MenuRequested(QPoint)));
editAndResendMenu = new QAction("Edit and Resend", this);
connect(editAndResendMenu, &QAction::triggered, this, &MainWindow::openEditAndResend);
updateHistory();
std::make_tuple(QString(), QString(), QString(), QString());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openEditAndResend() {
auto wnd = new EditAndResend(current_selection, this);
wnd->show();
}
void MainWindow::updateHistory() {
history_model.update(current_session->getHistoryItems());
history_proxy.invalidate();
ui->historyHTTPTable->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents);
}
void MainWindow::editMenuRequested(QPoint pos)
{
QMenu *menu=new QMenu(this);
menu->addAction(editAndResendMenu);
menu->popup(ui->requestHeaderEdit->viewport()->mapToGlobal(pos));
}
void MainWindow::edit2MenuRequested(QPoint pos)
{
QMenu *menu=new QMenu(this);
menu->addAction(editAndResendMenu);
menu->popup(ui->requestDataEdit->viewport()->mapToGlobal(pos));
}
void MainWindow::edit3MenuRequested(QPoint pos)
{
QMenu *menu=new QMenu(this);
menu->addAction(editAndResendMenu);
menu->popup(ui->responseHeaderEdit->viewport()->mapToGlobal(pos));
}
void MainWindow::edit4MenuRequested(QPoint pos)
{
QMenu *menu=new QMenu(this);
menu->addAction(editAndResendMenu);
menu->popup(ui->responseDataEdit->viewport()->mapToGlobal(pos));
}
void MainWindow::historyMenuRequested(QPoint pos)
{
QMenu *menu=new QMenu(this);
menu->addAction(editAndResendMenu);
menu->popup(ui->historyHTTPTable->viewport()->mapToGlobal(pos));
}
void MainWindow::on_selectionChange(const QItemSelection &selection)
{
if(!selection.indexes().isEmpty()) {
auto index = selection.indexes().at(7);
auto requestHeader = history_proxy.data(index).toString();
index = selection.indexes().at(8);
auto requestData = history_proxy.data(index).toString();
index = selection.indexes().at(9);
auto responseHeader = history_proxy.data(index).toString();
index = selection.indexes().at(10);
auto responseData = history_proxy.data(index).toString();
ui->requestHeaderEdit->setPlainText(requestHeader);
ui->requestDataEdit->setPlainText(requestData);
ui->responseHeaderEdit->setPlainText(responseHeader);
ui->responseDataEdit->setPlainText(responseData);
current_selection = std::make_tuple(requestHeader, requestData, responseHeader, responseData);
}
}
void MainWindow::on_searchEdit_textEdited(const QString &arg1)
{
}