From 8ab095fafd7ba8ca9db483f6e29692dec287a3de Mon Sep 17 00:00:00 2001 From: Ryzen Date: Sat, 2 Aug 2025 23:49:53 +0300 Subject: [PATCH] added sorting by Outbound and Protocol --- .../stats/connections/connectionLister.hpp | 4 ++- include/ui/mainwindow.ui | 6 ++++ .../connectionLister/connectionLister.cpp | 16 ++++++++++ src/ui/mainwindow.cpp | 32 +++++++------------ 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/include/stats/connections/connectionLister.hpp b/include/stats/connections/connectionLister.hpp index 54c5c04..c4982e2 100644 --- a/include/stats/connections/connectionLister.hpp +++ b/include/stats/connections/connectionLister.hpp @@ -12,7 +12,9 @@ namespace Stats ByDownload, ByUpload, ByProcess, - ByTraffic + ByTraffic, + ByOutbound, + ByProtocol }; class ConnectionMetadata diff --git a/include/ui/mainwindow.ui b/include/ui/mainwindow.ui index 84eac8f..f822bc8 100644 --- a/include/ui/mainwindow.ui +++ b/include/ui/mainwindow.ui @@ -441,11 +441,17 @@ Protocol + + Click To Sort By Protocol + Outbound + + Click To Sort By Outbound + diff --git a/src/stats/connectionLister/connectionLister.cpp b/src/stats/connectionLister/connectionLister.cpp index bd7dcbe..ed6e695 100644 --- a/src/stats/connectionLister/connectionLister.cpp +++ b/src/stats/connectionLister/connectionLister.cpp @@ -114,6 +114,22 @@ namespace Stats return asc ? a.process > b.process : a.process < b.process; }); } + if (sort == ByOutbound) + { + std::sort(sorted.begin(), sorted.end(), [=](const ConnectionMetadata& a, const ConnectionMetadata& b) + { + if (a.outbound == b.outbound) return asc ? a.id > b.id : a.id < b.id; + return asc ? a.outbound > b.outbound : a.outbound < b.outbound; + }); + } + if (sort == ByProtocol) + { + std::sort(sorted.begin(), sorted.end(), [=](const ConnectionMetadata& a, const ConnectionMetadata& b) + { + if (a.protocol == b.protocol) return asc ? a.id > b.id : a.id < b.id; + return asc ? a.protocol > b.protocol : a.protocol < b.protocol; + }); + } runOnUiThread([=] { auto m = GetMainWindow(); m->UpdateConnectionListWithRecreate(sorted); diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index b0501eb..d58a404 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -235,27 +235,19 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi }); connect(ui->connections->horizontalHeader(), &QHeaderView::sectionClicked, this, [=](int index) { - // TODO this is a very bad idea to hardcode it like this, need to refactor it later - if (index == 0) - { - Stats::connection_lister->setSort(Stats::Default); + Stats::ConnectionSort sortType; + + switch (index) + { + case 1: sortType = Stats::ByProcess; break; + case 2: sortType = Stats::ByProtocol; break; + case 3: sortType = Stats::ByOutbound; break; + case 4: sortType = Stats::ByTraffic; break; + default: sortType = Stats::Default; break; + } + + Stats::connection_lister->setSort(sortType); Stats::connection_lister->ForceUpdate(); - } - if (index == 2 || index == 3) - { - // ignore - return; - } - if (index == 1) - { - Stats::connection_lister->setSort(Stats::ByProcess); - Stats::connection_lister->ForceUpdate(); - } - if (index == 4) - { - Stats::connection_lister->setSort(Stats::ByTraffic); - Stats::connection_lister->ForceUpdate(); - } }); // setup Speed Chart