#include "historymodel.h" #include #include 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(); } 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); return 11; } 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(); } } QVariant HistoryModel::data(const QModelIndex &index, int role) const { const std::lock_guard lock(history_mutex); 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); return timestamp.toString("yyyy-mm-dd hh:mm:ss"); } 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); } case 7:{ return QString::fromStdString(getRequestHeader(item)); } case 8:{ std::string tmp = item.request_content; //replaceAll(tmp, "\\n", "\n"); return QString::fromUtf8(tmp.c_str()); } case 9:{ return QString::fromStdString(getResponseHeader(item)); } case 10:{ std::string tmp = item.response_content; //replaceAll(tmp, "\\", "\"); //std::cout << tmp << std::endl; return QString::fromUtf8(tmp.c_str()); //return QString::fromStdString(tmp); } } throw std::out_of_range("history model col"); } catch (std::out_of_range const& exc) { qDebug() << "historymodel data " << exc.what(); } return QVariant(); } QVariant HistoryModel::headerData(int section, Qt::Orientation orientation, int role) const { const std::lock_guard lock(history_mutex); 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"; } case 7: { return "request headers"; } case 8: { return "request content"; } case 9: { return "response headers"; } case 10: { return "response content"; } } } return QVariant(); } void HistoryModel::update(std::vector *items) { const std::lock_guard lock(history_mutex); if(current_items) { delete current_items; } current_items = items; } 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; }