diff options
author | Rachel Powers <508861+Ryex@users.noreply.github.com> | 2023-03-30 11:22:55 -0700 |
---|---|---|
committer | Rachel Powers <508861+Ryex@users.noreply.github.com> | 2023-05-01 10:45:34 -0700 |
commit | f997529cd4fb077b06d05da9c6ff0c23b85b4ebb (patch) | |
tree | 56817cf6faba1e4dac8f7cd515651e79a02fa62b /launcher/net/NetAction.h | |
parent | ae75585b52078ca6d89b4014668c08d9aea4a17b (diff) | |
download | PrismLauncher-f997529cd4fb077b06d05da9c6ff0c23b85b4ebb.tar.gz PrismLauncher-f997529cd4fb077b06d05da9c6ff0c23b85b4ebb.tar.bz2 PrismLauncher-f997529cd4fb077b06d05da9c6ff0c23b85b4ebb.zip |
feat: better task tracking
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
Diffstat (limited to 'launcher/net/NetAction.h')
-rw-r--r-- | launcher/net/NetAction.h | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/launcher/net/NetAction.h b/launcher/net/NetAction.h index 38fe058b..f9456bd6 100644 --- a/launcher/net/NetAction.h +++ b/launcher/net/NetAction.h @@ -35,18 +35,39 @@ #pragma once +#include <cmath> + #include <QNetworkReply> #include <QUrl> #include "QObjectPtr.h" #include "tasks/Task.h" +static const QStringList s_units_si {"kb", "MB", "GB", "TB"}; +static const QStringList s_units_kibi {"kiB", "MiB", "Gib", "TiB"}; + +inline QString humanReadableFileSize(qint64 bytes, bool use_si = false, int decimal_points = 1) { + const QStringList units = use_si ? s_units_si : s_units_kibi; + const int scale = use_si ? 1000 : 1024; + double size = bytes; + + int u = -1; + double r = pow(10, decimal_points); + + do { + size /= scale; + u++; + } while (round(abs(size) * r) / r >= scale && u < units.length() - 1); + + return QString::number(size, 'f', 2) + " " + units[u]; +} + class NetAction : public Task { Q_OBJECT - protected: +protected: explicit NetAction() : Task() {}; - public: +public: using Ptr = shared_qobject_ptr<NetAction>; virtual ~NetAction() = default; @@ -55,23 +76,23 @@ class NetAction : public Task { void setNetwork(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; } - protected slots: +protected slots: virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0; virtual void downloadError(QNetworkReply::NetworkError error) = 0; virtual void downloadFinished() = 0; virtual void downloadReadyRead() = 0; - public slots: +public slots: void startAction(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; executeTask(); } - protected: +protected: void executeTask() override {}; - public: +public: shared_qobject_ptr<QNetworkAccessManager> m_network; /// the network reply |