refactor: move functions to utils + code-review fixes

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
Rachel Powers
2023-05-06 19:10:58 -07:00
parent d0b6f0124b
commit 62a402d05a
10 changed files with 194 additions and 146 deletions

View File

@ -36,38 +36,18 @@
#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(double 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;
int u = -1;
double r = pow(10, decimal_points);
do {
bytes /= scale;
u++;
} while (round(abs(bytes) * r) / r >= scale && u < units.length() - 1);
return QString::number(bytes, 'f', 2) + " " + units[u];
}
class NetAction : public Task {
Q_OBJECT
protected:
explicit NetAction() : Task() {};
protected:
explicit NetAction() : Task(){};
public:
public:
using Ptr = shared_qobject_ptr<NetAction>;
virtual ~NetAction() = default;
@ -76,23 +56,23 @@ public:
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:
void executeTask() override {};
protected:
void executeTask() override{};
public:
public:
shared_qobject_ptr<QNetworkAccessManager> m_network;
/// the network reply