From 2731e479ce968b5e1359ead3450d2c5340cce3dd Mon Sep 17 00:00:00 2001 From: Nova Date: Thu, 8 May 2025 02:02:23 +0330 Subject: [PATCH] add download progress report --- include/global/HTTPRequestHelper.hpp | 7 +++++ include/ui/mainwindow.h | 13 +++++++-- src/global/HTTPRequestHelper.cpp | 13 +++++++++ src/ui/mainwindow.cpp | 43 ++++++++++++++++++++-------- src/ui/mainwindow_grpc.cpp | 8 ++++-- 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/include/global/HTTPRequestHelper.hpp b/include/global/HTTPRequestHelper.hpp index 545120b..8ecd873 100644 --- a/include/global/HTTPRequestHelper.hpp +++ b/include/global/HTTPRequestHelper.hpp @@ -10,6 +10,13 @@ namespace NekoGui_network { QList> header; }; + struct DownloadProgressReport + { + QString fileName; + qint64 downloadedSize; + qint64 totalSize; + }; + class NetworkRequestHelper : QObject { Q_OBJECT diff --git a/include/ui/mainwindow.h b/include/ui/mainwindow.h index f6fcae7..f0d49ef 100644 --- a/include/ui/mainwindow.h +++ b/include/ui/mainwindow.h @@ -2,6 +2,7 @@ #include #include +#include #include "include/global/NekoGui.hpp" #include "include/stats/connections/connectionLister.hpp" @@ -87,8 +88,9 @@ public: void UpdateConnectionListWithRecreate(const QList& connections); - // TODO maybe use a more generalized way of passing data later, for now we only need it for speedtest data - void UpdateDataView(const libcore::SpeedTestResult& result, const QString& profileName, bool clear); + void UpdateDataView(); + + void setDownloadReport(const DownloadProgressReport& report, bool show); signals: @@ -196,6 +198,13 @@ private: int toolTipID; // SpeedWidget *speedChartWidget; + // + // for data view + QString currentSptProfileName; + bool showSpeedtestData = false; + bool showDownloadData = false; + libcore::SpeedTestResult currentTestResult; + DownloadProgressReport currentDownloadReport; // could use a list, but don't think can show more than one anyways QList> get_now_selected_list(); diff --git a/src/global/HTTPRequestHelper.cpp b/src/global/HTTPRequestHelper.cpp index 6290855..c508385 100644 --- a/src/global/HTTPRequestHelper.cpp +++ b/src/global/HTTPRequestHelper.cpp @@ -9,6 +9,7 @@ #include #include "include/global/NekoGui.hpp" +#include "include/ui/mainwindow.h" namespace NekoGui_network { @@ -95,9 +96,21 @@ namespace NekoGui_network { } MW_show_log(QString("SSL Errors: %1").arg(error_str.join(","))); }); + connect(_reply, &QNetworkReply::downloadProgress, _reply, [&](qint64 bytesReceived, qint64 bytesTotal) + { + runOnUiThread([=]{ + GetMainWindow()->setDownloadReport(DownloadProgressReport{fileName, bytesReceived, bytesTotal}, true); + GetMainWindow()->UpdateDataView(); + }); + }); QEventLoop loop; connect(_reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(); + runOnUiThread([=] + { + GetMainWindow()->setDownloadReport({}, false); + GetMainWindow()->UpdateDataView(); + }); if(_reply->error() != QNetworkReply::NetworkError::NoError) { return _reply->errorString(); } diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index dd63d1e..12c5120 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -928,26 +928,45 @@ void MainWindow::neko_set_spmode_vpn(bool enable, bool save) { if (NekoGui::dataStore->started_id >= 0) neko_start(NekoGui::dataStore->started_id); } -void MainWindow::UpdateDataView(const libcore::SpeedTestResult& result, const QString& profileName, bool clear) +void MainWindow::UpdateDataView() { - if (clear) + QString html; + if (showDownloadData) { - ui->data_view->clear(); - return; + qint64 count = 10*currentDownloadReport.downloadedSize / currentDownloadReport.totalSize; + QString progressText; + for (int i = 0; i < 10; i++) + { + if (count--; count >=0) progressText += "#"; + else progressText += "-"; + } + QString stat = ReadableSize(currentDownloadReport.downloadedSize) + "/" + ReadableSize(currentDownloadReport.totalSize); + html = QString("

Downloading %1: %2 %3

").arg(currentDownloadReport.fileName, stat, progressText); } - QString html = QString( + if (showSpeedtestData) + { + html += QString( "

Running Speedtest: %1

" - "

Dl↓ %2

" - "

Ul↑ %3

" + "
" + "Dl↓ %2 " + "Ul↑ %3" + "
" "

Server: %4, %5

" - ).arg(profileName, - result.dl_speed().c_str(), - result.ul_speed().c_str(), - result.server_country().c_str(), - result.server_name().c_str()); + ).arg(currentSptProfileName, + currentTestResult.dl_speed().c_str(), + currentTestResult.ul_speed().c_str(), + currentTestResult.server_country().c_str(), + currentTestResult.server_name().c_str()); + } ui->data_view->setHtml(html); } +void MainWindow::setDownloadReport(const DownloadProgressReport& report, bool show) +{ + showDownloadData = show; + currentDownloadReport = report; +} + void MainWindow::setupConnectionList() { diff --git a/src/ui/mainwindow_grpc.cpp b/src/ui/mainwindow_grpc.cpp index 544d8f9..fe71f4b 100644 --- a/src/ui/mainwindow_grpc.cpp +++ b/src/ui/mainwindow_grpc.cpp @@ -247,12 +247,16 @@ void MainWindow::runSpeedTest(const QString& config, bool useDefault, const QStr } runOnUiThread([=] { - UpdateDataView(res.result(), profile->bean->name, false); + showSpeedtestData = true; + currentSptProfileName = profile->bean->name; + currentTestResult = res.result(); + UpdateDataView(); }); } runOnUiThread([=] { - UpdateDataView({}, {}, true); + showSpeedtestData = false; + UpdateDataView(); }); doneMu->unlock(); delete doneMu;