bigsnitch/historymodel.cpp

188 lines
4.7 KiB
C++
Raw Normal View History

2020-08-26 18:58:36 +00:00
#include "historymodel.h"
2020-09-01 12:55:49 +00:00
#include <mutex>
2020-08-26 18:58:36 +00:00
#include <QDateTime>
2020-09-02 22:23:37 +00:00
std::string HistoryModel::getRequestHeader(const HistoryItem &item) const
{
std::stringstream result;
result << item.method << " " << item.scheme << "://" << item.host << item.path << " " << item.request_http_version << "\n";
for(auto& [k,v] : item.request_headers) {
result << k << ": " << v << "\n";
}
return result.str();
}
std::string HistoryModel::getResponseHeader(const HistoryItem &item) const
{
std::stringstream result;
result << item.response_http_version << " " << item.status_code << " " << item.reason << "\n";
for(auto& [k,v] : item.response_headers) {
result << k << ": " << v << "\n";
}
return result.str();
}
2020-08-26 18:58:36 +00:00
HistoryModel::HistoryModel(QObject *parent)
{
Q_UNUSED(parent);
}
int HistoryModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
if(current_items) {
return current_items->size();
}
return 0;
}
int HistoryModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
2020-09-02 22:23:37 +00:00
return 11;
2020-08-26 18:58:36 +00:00
}
2020-09-02 23:14:10 +00:00
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
2020-08-26 18:58:36 +00:00
QVariant HistoryModel::data(const QModelIndex &index, int role) const
{
2020-09-01 12:55:49 +00:00
const std::lock_guard<std::mutex> lock(history_mutex);
2020-08-26 18:58:36 +00:00
if(role != Qt::DisplayRole) {
return QVariant();
}
if(!current_items) {
return QVariant();
}
int row = index.row();
int col = index.column();
try {
auto item = current_items->at(row);
switch(col) {
case 0: {
return QString::number(item.id);
}
case 1: {
QDateTime timestamp;
timestamp.setTime_t(item.timestamp);
2020-09-02 12:13:03 +00:00
return timestamp.toString("yyyy-mm-dd hh:mm:ss");
2020-08-26 18:58:36 +00:00
}
case 2: {
std::string url = item.scheme + "://" + item.host + ":" + std::to_string(item.port) + item.path;
return QString::fromStdString(url);
}
case 3: {
return item.status_code;
}
case 4: {
return QString::fromStdString(item.reason);
}
case 5: {
return item.rtt;
}
case 6:{
return QString::number(item.size);
}
2020-09-02 22:23:37 +00:00
case 7:{
return QString::fromStdString(getRequestHeader(item));
}
case 8:{
2020-09-02 23:14:10 +00:00
std::string tmp = item.request_content;
2020-09-03 08:54:25 +00:00
//replaceAll(tmp, "\\n", "\n");
return QString::fromUtf8(tmp.c_str());
2020-09-02 22:23:37 +00:00
}
case 9:{
return QString::fromStdString(getResponseHeader(item));
}
case 10:{
2020-09-02 23:14:10 +00:00
std::string tmp = item.response_content;
2020-09-03 08:54:25 +00:00
//replaceAll(tmp, "\\", "\");
//std::cout << tmp << std::endl;
return QString::fromUtf8(tmp.c_str());
//return QString::fromStdString(tmp);
2020-09-02 22:23:37 +00:00
}
2020-08-26 18:58:36 +00:00
}
2020-08-28 23:09:41 +00:00
throw std::out_of_range("history model col");
2020-08-26 18:58:36 +00:00
} catch (std::out_of_range const& exc) {
qDebug() << "historymodel data " << exc.what();
}
return QVariant();
}
2020-08-28 23:09:41 +00:00
QVariant HistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
2020-09-01 12:55:49 +00:00
const std::lock_guard<std::mutex> lock(history_mutex);
2020-08-28 23:09:41 +00:00
if(role != Qt::DisplayRole) {
return QVariant();
}
if(orientation == Qt::Horizontal) {
switch(section) {
case 0: {
return "id";
}
case 1: {
return "time";
}
case 2: {
return "url";
}
case 3: {
return "code";
}
case 4: {
return "reason";
}
case 5: {
return "rtt";
}
case 6: {
return "size";
}
2020-09-02 22:23:37 +00:00
case 7: {
return "request headers";
}
case 8: {
return "request content";
}
case 9: {
return "response headers";
}
case 10: {
return "response content";
}
2020-08-28 23:09:41 +00:00
}
}
return QVariant();
}
2020-08-26 18:58:36 +00:00
void HistoryModel::update(std::vector<HistoryItem> *items)
{
2020-09-01 12:55:49 +00:00
const std::lock_guard<std::mutex> lock(history_mutex);
2020-08-26 18:58:36 +00:00
if(current_items) {
delete current_items;
}
current_items = items;
2020-08-28 23:09:41 +00:00
}
bool HistoryProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
return QSortFilterProxyModel::lessThan(source_left, source_right);
}
bool HistoryProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
return true;
2020-08-26 18:58:36 +00:00
}
2020-09-01 20:14:49 +00:00