refactor: move functions to utils + code-review fixes
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
parent
d0b6f0124b
commit
62a402d05a
@ -423,16 +423,16 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
|
|||||||
foundLoggingRules = QFile::exists(logRulesPath);
|
foundLoggingRules = QFile::exists(logRulesPath);
|
||||||
|
|
||||||
// search the dataPath()
|
// search the dataPath()
|
||||||
|
// seach app data standard path
|
||||||
if(!foundLoggingRules && !isPortable() && dirParam.isEmpty()) {
|
if(!foundLoggingRules && !isPortable() && dirParam.isEmpty()) {
|
||||||
logRulesPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, logRulesFile);
|
logRulesPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, FS::PathCombine("..", logRulesFile));
|
||||||
if(!logRulesPath.isEmpty()) {
|
if(!logRulesPath.isEmpty()) {
|
||||||
qDebug() << "Found" << logRulesPath << "...";
|
qDebug() << "Found" << logRulesPath << "...";
|
||||||
foundLoggingRules = true;
|
foundLoggingRules = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// seach root path
|
||||||
if(!QFile::exists(logRulesPath)) {
|
if(!foundLoggingRules) {
|
||||||
logRulesPath = FS::PathCombine(m_rootPath, logRulesFile);
|
logRulesPath = FS::PathCombine(m_rootPath, logRulesFile);
|
||||||
qDebug() << "Testing" << logRulesPath << "...";
|
qDebug() << "Testing" << logRulesPath << "...";
|
||||||
foundLoggingRules = QFile::exists(logRulesPath);
|
foundLoggingRules = QFile::exists(logRulesPath);
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
#include <MMCTime.h>
|
#include <MMCTime.h>
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QTextStream>
|
||||||
|
|
||||||
QString Time::prettifyDuration(int64_t duration) {
|
QString Time::prettifyDuration(int64_t duration) {
|
||||||
int seconds = (int) (duration % 60);
|
int seconds = (int) (duration % 60);
|
||||||
@ -36,3 +38,65 @@ QString Time::prettifyDuration(int64_t duration) {
|
|||||||
}
|
}
|
||||||
return QObject::tr("%1d %2h %3min").arg(days).arg(hours).arg(minutes);
|
return QObject::tr("%1d %2h %3min").arg(days).arg(hours).arg(minutes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString Time::humanReadableDuration(double duration, int precision) {
|
||||||
|
|
||||||
|
using days = std::chrono::duration<int, std::ratio<86400>>;
|
||||||
|
|
||||||
|
QString outStr;
|
||||||
|
QTextStream os(&outStr);
|
||||||
|
|
||||||
|
bool neg = false;
|
||||||
|
if (duration < 0) {
|
||||||
|
neg = true; // flag
|
||||||
|
duration *= -1; // invert
|
||||||
|
}
|
||||||
|
|
||||||
|
auto std_duration = std::chrono::duration<double>(duration);
|
||||||
|
auto d = std::chrono::duration_cast<days>(std_duration);
|
||||||
|
std_duration -= d;
|
||||||
|
auto h = std::chrono::duration_cast<std::chrono::hours>(std_duration);
|
||||||
|
std_duration -= h;
|
||||||
|
auto m = std::chrono::duration_cast<std::chrono::minutes>(std_duration);
|
||||||
|
std_duration -= m;
|
||||||
|
auto s = std::chrono::duration_cast<std::chrono::seconds>(std_duration);
|
||||||
|
std_duration -= s;
|
||||||
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std_duration);
|
||||||
|
|
||||||
|
auto dc = d.count();
|
||||||
|
auto hc = h.count();
|
||||||
|
auto mc = m.count();
|
||||||
|
auto sc = s.count();
|
||||||
|
auto msc = ms.count();
|
||||||
|
|
||||||
|
if (neg) {
|
||||||
|
os << '-';
|
||||||
|
}
|
||||||
|
if (dc) {
|
||||||
|
os << dc << QObject::tr("days");
|
||||||
|
}
|
||||||
|
if (hc) {
|
||||||
|
if (dc)
|
||||||
|
os << " ";
|
||||||
|
os << qSetFieldWidth(2) << hc << QObject::tr("h"); // hours
|
||||||
|
}
|
||||||
|
if (mc) {
|
||||||
|
if (dc || hc)
|
||||||
|
os << " ";
|
||||||
|
os << qSetFieldWidth(2) << mc << QObject::tr("m"); // minutes
|
||||||
|
}
|
||||||
|
if (dc || hc || mc || sc) {
|
||||||
|
if (dc || hc || mc)
|
||||||
|
os << " ";
|
||||||
|
os << qSetFieldWidth(2) << sc << QObject::tr("s"); // seconds
|
||||||
|
}
|
||||||
|
if ((msc && (precision > 0)) || !(dc || hc || mc || sc)) {
|
||||||
|
if (dc || hc || mc || sc)
|
||||||
|
os << " ";
|
||||||
|
os << qSetFieldWidth(0) << qSetRealNumberPrecision(precision) << msc << QObject::tr("ms"); // miliseconds
|
||||||
|
}
|
||||||
|
|
||||||
|
os.flush();
|
||||||
|
|
||||||
|
return outStr;
|
||||||
|
}
|
@ -22,4 +22,13 @@ namespace Time {
|
|||||||
|
|
||||||
QString prettifyDuration(int64_t duration);
|
QString prettifyDuration(int64_t duration);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a string with short form time duration ie. `2days 1h3m4s56.0ms`
|
||||||
|
* miliseconds are only included if `percison` is greater than 0
|
||||||
|
*
|
||||||
|
* @param duration a number of seconds as floating point
|
||||||
|
* @param precision number of decmial points to display on fractons of a second, defualts to 0.
|
||||||
|
* @return QString
|
||||||
|
*/
|
||||||
|
QString humanReadableDuration(double duration, int precision = 0);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#include "StringUtils.h"
|
#include "StringUtils.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
/// If you're wondering where these came from exactly, then know you're not the only one =D
|
/// If you're wondering where these came from exactly, then know you're not the only one =D
|
||||||
|
|
||||||
/// TAKEN FROM Qt, because it doesn't expose it intelligently
|
/// TAKEN FROM Qt, because it doesn't expose it intelligently
|
||||||
@ -74,3 +77,67 @@ int StringUtils::naturalCompare(const QString& s1, const QString& s2, Qt::CaseSe
|
|||||||
// The two strings are the same (02 == 2) so fall back to the normal sort
|
// The two strings are the same (02 == 2) so fall back to the normal sort
|
||||||
return QString::compare(s1, s2, cs);
|
return QString::compare(s1, s2, cs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Truncate a url while keeping its readability py placing the `...` in the middle of the path
|
||||||
|
QString StringUtils::truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit)
|
||||||
|
{
|
||||||
|
auto display_options = QUrl::RemoveUserInfo | QUrl::RemoveFragment | QUrl::NormalizePathSegments;
|
||||||
|
auto str_url = url.toDisplayString(display_options);
|
||||||
|
|
||||||
|
if (str_url.length() <= max_len)
|
||||||
|
return str_url;
|
||||||
|
|
||||||
|
auto url_path_parts = url.path().split('/');
|
||||||
|
QString last_path_segment = url_path_parts.takeLast();
|
||||||
|
|
||||||
|
if (url_path_parts.size() >= 1 && url_path_parts.first().isEmpty())
|
||||||
|
url_path_parts.removeFirst(); // drop empty first segment (from leading / )
|
||||||
|
|
||||||
|
if (url_path_parts.size() >= 1)
|
||||||
|
url_path_parts.removeLast(); // drop the next to last path segment
|
||||||
|
|
||||||
|
auto url_template = QStringLiteral("%1://%2/%3%4");
|
||||||
|
|
||||||
|
auto url_compact = url_path_parts.isEmpty()
|
||||||
|
? url_template.arg(url.scheme(), url.host(), QStringList({ "...", last_path_segment }).join('/'), url.query())
|
||||||
|
: url_template.arg(url.scheme(), url.host(),
|
||||||
|
QStringList({ url_path_parts.join('/'), "...", last_path_segment }).join('/'), url.query());
|
||||||
|
|
||||||
|
// remove url parts one by one if it's still too long
|
||||||
|
while (url_compact.length() > max_len && url_path_parts.size() >= 1) {
|
||||||
|
url_path_parts.removeLast(); // drop the next to last path segment
|
||||||
|
url_compact = url_path_parts.isEmpty()
|
||||||
|
? url_template.arg(url.scheme(), url.host(), QStringList({ "...", last_path_segment }).join('/'), url.query())
|
||||||
|
: url_template.arg(url.scheme(), url.host(),
|
||||||
|
QStringList({ url_path_parts.join('/'), "...", last_path_segment }).join('/'), url.query());
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((url_compact.length() >= max_len) && hard_limit) {
|
||||||
|
// still too long, truncate normaly
|
||||||
|
url_compact = QString(str_url);
|
||||||
|
auto to_remove = url_compact.length() - max_len + 3;
|
||||||
|
url_compact.remove(url_compact.length() - to_remove - 1, to_remove);
|
||||||
|
url_compact.append("...");
|
||||||
|
}
|
||||||
|
|
||||||
|
return url_compact;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static const QStringList s_units_si {"KB", "MB", "GB", "TB"};
|
||||||
|
static const QStringList s_units_kibi {"KiB", "MiB", "Gib", "TiB"};
|
||||||
|
|
||||||
|
QString StringUtils::humanReadableFileSize(double bytes, bool use_si, int decimal_points) {
|
||||||
|
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];
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
namespace StringUtils {
|
namespace StringUtils {
|
||||||
|
|
||||||
@ -29,4 +30,15 @@ inline QString fromStdString(string s)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
int naturalCompare(const QString& s1, const QString& s2, Qt::CaseSensitivity cs);
|
int naturalCompare(const QString& s1, const QString& s2, Qt::CaseSensitivity cs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Truncate a url while keeping its readability py placing the `...` in the middle of the path
|
||||||
|
* @param url Url to truncate
|
||||||
|
* @param max_len max lenght of url in charaters
|
||||||
|
* @param hard_limit if truncating the path can't get the url short enough, truncate it normaly.
|
||||||
|
*/
|
||||||
|
QString truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit = false);
|
||||||
|
|
||||||
|
QString humanReadableFileSize(double bytes, bool use_si = false, int decimal_points = 1);
|
||||||
|
|
||||||
} // namespace StringUtils
|
} // namespace StringUtils
|
||||||
|
@ -846,7 +846,7 @@ void PackInstallTask::downloadMods()
|
|||||||
emitFailed(reason);
|
emitFailed(reason);
|
||||||
});
|
});
|
||||||
connect(jobPtr.get(), &NetJob::progress, [&](qint64 current, qint64 total)
|
connect(jobPtr.get(), &NetJob::progress, [&](qint64 current, qint64 total)
|
||||||
{
|
{
|
||||||
setDetails(tr("%1 out of %2 complete").arg(current).arg(total));
|
setDetails(tr("%1 out of %2 complete").arg(current).arg(total));
|
||||||
abortable = true;
|
abortable = true;
|
||||||
setProgress(current, total);
|
setProgress(current, total);
|
||||||
|
@ -37,7 +37,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Download.h"
|
#include "Download.h"
|
||||||
#include <QRegularExpression>
|
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
@ -45,107 +44,19 @@
|
|||||||
|
|
||||||
#include "ByteArraySink.h"
|
#include "ByteArraySink.h"
|
||||||
#include "ChecksumValidator.h"
|
#include "ChecksumValidator.h"
|
||||||
#include "FileSystem.h"
|
|
||||||
#include "MetaCacheSink.h"
|
#include "MetaCacheSink.h"
|
||||||
|
|
||||||
#include "BuildConfig.h"
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
#include "BuildConfig.h"
|
||||||
|
|
||||||
#include "net/Logging.h"
|
#include "net/Logging.h"
|
||||||
#include "net/NetAction.h"
|
#include "net/NetAction.h"
|
||||||
|
|
||||||
|
#include "MMCTime.h"
|
||||||
|
#include "StringUtils.h"
|
||||||
|
|
||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
QString truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit = false)
|
|
||||||
{
|
|
||||||
auto display_options = QUrl::RemoveUserInfo | QUrl::RemoveFragment | QUrl::NormalizePathSegments;
|
|
||||||
auto str_url = url.toDisplayString(display_options);
|
|
||||||
if (str_url.length() <= max_len)
|
|
||||||
return str_url;
|
|
||||||
|
|
||||||
/* this is a PCRE regular expression that splits a URL (given by the display rules above) into 5 capture groups
|
|
||||||
* the scheme (ie https://) is group 1
|
|
||||||
* the host (with trailing /) is group 2
|
|
||||||
* the first part of the path (with trailing /) is group 3
|
|
||||||
* the last part of the path (with leading /) is group 5
|
|
||||||
* the remainder of the URL is in the .* and in group 4
|
|
||||||
*
|
|
||||||
* See: https://regex101.com/r/inHkek/1
|
|
||||||
* for an interactive breakdown
|
|
||||||
*/
|
|
||||||
QRegularExpression re(R"(^([\w]+:\/\/)([\w._-]+\/)([\w._-]+\/)(.*)(\/[^]+[^]+)$)");
|
|
||||||
|
|
||||||
auto url_compact = QString(str_url);
|
|
||||||
url_compact.replace(re, "\\1\\2\\3...\\5");
|
|
||||||
if (url_compact.length() >= max_len) {
|
|
||||||
url_compact = QString(str_url);
|
|
||||||
url_compact.replace(re, "\\1\\2...\\5");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if ((url_compact.length() >= max_len) && hard_limit) {
|
|
||||||
auto to_remove = url_compact.length() - max_len + 3;
|
|
||||||
url_compact.remove(url_compact.length() - to_remove - 1, to_remove);
|
|
||||||
url_compact.append("...");
|
|
||||||
}
|
|
||||||
|
|
||||||
return url_compact;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
QString humanReadableDuration(double duration, int precision = 0) {
|
|
||||||
|
|
||||||
using days = std::chrono::duration<int, std::ratio<86400>>;
|
|
||||||
|
|
||||||
QString outStr;
|
|
||||||
QTextStream os(&outStr);
|
|
||||||
|
|
||||||
auto std_duration = std::chrono::duration<double>(duration);
|
|
||||||
auto d = std::chrono::duration_cast<days>(std_duration);
|
|
||||||
std_duration -= d;
|
|
||||||
auto h = std::chrono::duration_cast<std::chrono::hours>(std_duration);
|
|
||||||
std_duration -= h;
|
|
||||||
auto m = std::chrono::duration_cast<std::chrono::minutes>(std_duration);
|
|
||||||
std_duration -= m;
|
|
||||||
auto s = std::chrono::duration_cast<std::chrono::seconds>(std_duration);
|
|
||||||
std_duration -= s;
|
|
||||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std_duration);
|
|
||||||
|
|
||||||
auto dc = d.count();
|
|
||||||
auto hc = h.count();
|
|
||||||
auto mc = m.count();
|
|
||||||
auto sc = s.count();
|
|
||||||
auto msc = ms.count();
|
|
||||||
|
|
||||||
if (dc) {
|
|
||||||
os << dc << "days";
|
|
||||||
}
|
|
||||||
if (hc) {
|
|
||||||
if (dc)
|
|
||||||
os << " ";
|
|
||||||
os << qSetFieldWidth(2) << hc << "h";
|
|
||||||
}
|
|
||||||
if (mc) {
|
|
||||||
if (dc || hc)
|
|
||||||
os << " ";
|
|
||||||
os << qSetFieldWidth(2) << mc << "m";
|
|
||||||
}
|
|
||||||
if (dc || hc || mc || sc) {
|
|
||||||
if (dc || hc || mc)
|
|
||||||
os << " ";
|
|
||||||
os << qSetFieldWidth(2) << sc << "s";
|
|
||||||
}
|
|
||||||
if ((msc && (precision > 0)) || !(dc || hc || mc || sc)) {
|
|
||||||
if (dc || hc || mc || sc)
|
|
||||||
os << " ";
|
|
||||||
os << qSetFieldWidth(0) << qSetRealNumberPrecision(precision) << msc << "ms";
|
|
||||||
}
|
|
||||||
|
|
||||||
os.flush();
|
|
||||||
|
|
||||||
return outStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr
|
auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr
|
||||||
{
|
{
|
||||||
auto dl = makeShared<Download>();
|
auto dl = makeShared<Download>();
|
||||||
@ -185,7 +96,7 @@ void Download::addValidator(Validator* v)
|
|||||||
|
|
||||||
void Download::executeTask()
|
void Download::executeTask()
|
||||||
{
|
{
|
||||||
setStatus(tr("Downloading %1").arg(truncateUrlHumanFriendly(m_url, 100)));
|
setStatus(tr("Downloading %1").arg(StringUtils::truncateUrlHumanFriendly(m_url, 80)));
|
||||||
|
|
||||||
if (getState() == Task::State::AbortedByUser) {
|
if (getState() == Task::State::AbortedByUser) {
|
||||||
qCWarning(taskDownloadLogC) << getUid().toString() << "Attempt to start an aborted Download:" << m_url.toString();
|
qCWarning(taskDownloadLogC) << getUid().toString() << "Attempt to start an aborted Download:" << m_url.toString();
|
||||||
@ -222,12 +133,12 @@ void Download::executeTask()
|
|||||||
if (!token.isNull())
|
if (!token.isNull())
|
||||||
request.setRawHeader("Authorization", token.toUtf8());
|
request.setRawHeader("Authorization", token.toUtf8());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_last_progress_time = m_clock.now();
|
m_last_progress_time = m_clock.now();
|
||||||
m_last_progress_bytes = 0;
|
m_last_progress_bytes = 0;
|
||||||
|
|
||||||
QNetworkReply* rep = m_network->get(request);
|
QNetworkReply* rep = m_network->get(request);
|
||||||
|
|
||||||
m_reply.reset(rep);
|
m_reply.reset(rep);
|
||||||
connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
|
connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
|
||||||
connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished);
|
connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished);
|
||||||
@ -252,16 +163,19 @@ void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|||||||
auto remaing_time_s = (bytesTotal - bytesReceived) / dl_speed_bps;
|
auto remaing_time_s = (bytesTotal - bytesReceived) / dl_speed_bps;
|
||||||
|
|
||||||
//: Current amount of bytes downloaded, out of the total amount of bytes in the download
|
//: Current amount of bytes downloaded, out of the total amount of bytes in the download
|
||||||
QString dl_progress = tr("%1 / %2").arg(humanReadableFileSize(bytesReceived)).arg(humanReadableFileSize(bytesTotal));
|
QString dl_progress =
|
||||||
|
tr("%1 / %2").arg(StringUtils::humanReadableFileSize(bytesReceived)).arg(StringUtils::humanReadableFileSize(bytesTotal));
|
||||||
|
|
||||||
QString dl_speed_str;
|
QString dl_speed_str;
|
||||||
if (elapsed_ms.count() > 0) {
|
if (elapsed_ms.count() > 0) {
|
||||||
|
auto str_eta = bytesTotal > 0 ? Time::humanReadableDuration(remaing_time_s) : tr("unknown");
|
||||||
//: Download speed, in bytes per second (remaining download time in parenthesis)
|
//: Download speed, in bytes per second (remaining download time in parenthesis)
|
||||||
dl_speed_str = tr("%1 /s (%2)").arg(humanReadableFileSize(dl_speed_bps)).arg(humanReadableDuration(remaing_time_s));
|
dl_speed_str =
|
||||||
|
tr("%1 /s (%2)").arg(StringUtils::humanReadableFileSize(dl_speed_bps)).arg(str_eta);
|
||||||
} else {
|
} else {
|
||||||
//: Download speed at 0 bytes per second
|
//: Download speed at 0 bytes per second
|
||||||
dl_speed_str = tr("0 B/s");
|
dl_speed_str = tr("0 B/s");
|
||||||
}
|
}
|
||||||
|
|
||||||
setDetails(dl_progress + "\n" + dl_speed_str);
|
setDetails(dl_progress + "\n" + dl_speed_str);
|
||||||
|
|
||||||
@ -290,7 +204,8 @@ void Download::sslErrors(const QList<QSslError>& errors)
|
|||||||
{
|
{
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (auto error : errors) {
|
for (auto error : errors) {
|
||||||
qCCritical(taskDownloadLogC) << getUid().toString() << "Download" << m_url.toString() << "SSL Error #" << i << " : " << error.errorString();
|
qCCritical(taskDownloadLogC) << getUid().toString() << "Download" << m_url.toString() << "SSL Error #" << i << " : "
|
||||||
|
<< error.errorString();
|
||||||
auto cert = error.certificate();
|
auto cert = error.certificate();
|
||||||
qCCritical(taskDownloadLogC) << getUid().toString() << "Certificate in question:\n" << cert.toText();
|
qCCritical(taskDownloadLogC) << getUid().toString() << "Certificate in question:\n" << cert.toText();
|
||||||
i++;
|
i++;
|
||||||
|
@ -36,38 +36,18 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
#include "QObjectPtr.h"
|
#include "QObjectPtr.h"
|
||||||
#include "tasks/Task.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 {
|
class NetAction : public Task {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
protected:
|
protected:
|
||||||
explicit NetAction() : Task() {};
|
explicit NetAction() : Task(){};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Ptr = shared_qobject_ptr<NetAction>;
|
using Ptr = shared_qobject_ptr<NetAction>;
|
||||||
|
|
||||||
virtual ~NetAction() = default;
|
virtual ~NetAction() = default;
|
||||||
@ -76,23 +56,23 @@ public:
|
|||||||
|
|
||||||
void setNetwork(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; }
|
void setNetwork(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; }
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;
|
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;
|
||||||
virtual void downloadError(QNetworkReply::NetworkError error) = 0;
|
virtual void downloadError(QNetworkReply::NetworkError error) = 0;
|
||||||
virtual void downloadFinished() = 0;
|
virtual void downloadFinished() = 0;
|
||||||
virtual void downloadReadyRead() = 0;
|
virtual void downloadReadyRead() = 0;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void startAction(shared_qobject_ptr<QNetworkAccessManager> network)
|
void startAction(shared_qobject_ptr<QNetworkAccessManager> network)
|
||||||
{
|
{
|
||||||
m_network = network;
|
m_network = network;
|
||||||
executeTask();
|
executeTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void executeTask() override {};
|
void executeTask() override{};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
shared_qobject_ptr<QNetworkAccessManager> m_network;
|
shared_qobject_ptr<QNetworkAccessManager> m_network;
|
||||||
|
|
||||||
/// the network reply
|
/// the network reply
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
# prevent log spam and strange bugs
|
# prevent log spam and strange bugs
|
||||||
# qt.qpa.drawing in particular causes theme artifacts on MacOS
|
# qt.qpa.drawing in particular causes theme artifacts on MacOS
|
||||||
qt.*.debug=false
|
qt.*.debug=false
|
||||||
|
# dont log credentials buy defualt
|
||||||
|
launcher.auth.credentials.debug=false
|
||||||
# remove the debug lines, other log levels still get through
|
# remove the debug lines, other log levels still get through
|
||||||
launcher.task.net.download.debug=false
|
launcher.task.net.download.debug=false
|
||||||
# enable or disable whole catageries
|
# enable or disable whole catageries
|
||||||
|
@ -35,17 +35,17 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QUuid>
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QQueue>
|
#include <QQueue>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
|
#include <QUuid>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "tasks/Task.h"
|
#include "tasks/Task.h"
|
||||||
|
|
||||||
class ConcurrentTask : public Task {
|
class ConcurrentTask : public Task {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
using Ptr = shared_qobject_ptr<ConcurrentTask>;
|
using Ptr = shared_qobject_ptr<ConcurrentTask>;
|
||||||
|
|
||||||
explicit ConcurrentTask(QObject* parent = nullptr, QString task_name = "", int max_concurrent = 6);
|
explicit ConcurrentTask(QObject* parent = nullptr, QString task_name = "", int max_concurrent = 6);
|
||||||
@ -58,7 +58,7 @@ public:
|
|||||||
|
|
||||||
void addTask(Task::Ptr task);
|
void addTask(Task::Ptr task);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
bool abort() override;
|
bool abort() override;
|
||||||
|
|
||||||
/** Resets the internal state of the task.
|
/** Resets the internal state of the task.
|
||||||
@ -66,20 +66,19 @@ public slots:
|
|||||||
*/
|
*/
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
protected
|
protected slots:
|
||||||
slots:
|
|
||||||
void executeTask() override;
|
void executeTask() override;
|
||||||
|
|
||||||
virtual void startNext();
|
virtual void startNext();
|
||||||
|
|
||||||
void subTaskSucceeded(Task::Ptr);
|
void subTaskSucceeded(Task::Ptr);
|
||||||
void subTaskFailed(Task::Ptr, const QString &msg);
|
void subTaskFailed(Task::Ptr, const QString& msg);
|
||||||
void subTaskStatus(Task::Ptr task, const QString &msg);
|
void subTaskStatus(Task::Ptr task, const QString& msg);
|
||||||
void subTaskDetails(Task::Ptr task, const QString &msg);
|
void subTaskDetails(Task::Ptr task, const QString& msg);
|
||||||
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
|
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
|
||||||
void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_step_progress);
|
void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_step_progress);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// NOTE: This is not thread-safe.
|
// NOTE: This is not thread-safe.
|
||||||
[[nodiscard]] unsigned int totalSize() const { return m_queue.size() + m_doing.size() + m_done.size(); }
|
[[nodiscard]] unsigned int totalSize() const { return m_queue.size() + m_doing.size() + m_done.size(); }
|
||||||
|
|
||||||
@ -88,13 +87,13 @@ protected:
|
|||||||
|
|
||||||
virtual void updateState();
|
virtual void updateState();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QString m_name;
|
QString m_name;
|
||||||
QString m_step_status;
|
QString m_step_status;
|
||||||
|
|
||||||
QQueue<Task::Ptr> m_queue;
|
QQueue<Task::Ptr> m_queue;
|
||||||
|
|
||||||
QHash<Task*, Task::Ptr> m_doing;
|
QHash<Task*, Task::Ptr> m_doing;
|
||||||
QHash<Task*, Task::Ptr> m_done;
|
QHash<Task*, Task::Ptr> m_done;
|
||||||
QHash<Task*, Task::Ptr> m_failed;
|
QHash<Task*, Task::Ptr> m_failed;
|
||||||
QHash<Task*, Task::Ptr> m_succeeded;
|
QHash<Task*, Task::Ptr> m_succeeded;
|
||||||
|
Loading…
Reference in New Issue
Block a user