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);
|
||||
|
||||
// search the dataPath()
|
||||
|
||||
// seach app data standard path
|
||||
if(!foundLoggingRules && !isPortable() && dirParam.isEmpty()) {
|
||||
logRulesPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, logRulesFile);
|
||||
logRulesPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, FS::PathCombine("..", logRulesFile));
|
||||
if(!logRulesPath.isEmpty()) {
|
||||
qDebug() << "Found" << logRulesPath << "...";
|
||||
foundLoggingRules = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(!QFile::exists(logRulesPath)) {
|
||||
// seach root path
|
||||
if(!foundLoggingRules) {
|
||||
logRulesPath = FS::PathCombine(m_rootPath, logRulesFile);
|
||||
qDebug() << "Testing" << logRulesPath << "...";
|
||||
foundLoggingRules = QFile::exists(logRulesPath);
|
||||
|
@ -18,6 +18,8 @@
|
||||
#include <MMCTime.h>
|
||||
|
||||
#include <QObject>
|
||||
#include <QDateTime>
|
||||
#include <QTextStream>
|
||||
|
||||
QString Time::prettifyDuration(int64_t duration) {
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
/**
|
||||
* @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 <cmath>
|
||||
#include <QRegularExpression>
|
||||
|
||||
/// 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
|
||||
@ -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
|
||||
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
|
||||
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
|
||||
namespace StringUtils {
|
||||
|
||||
@ -29,4 +30,15 @@ inline QString fromStdString(string s)
|
||||
#endif
|
||||
|
||||
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
|
||||
|
@ -37,7 +37,6 @@
|
||||
*/
|
||||
|
||||
#include "Download.h"
|
||||
#include <QRegularExpression>
|
||||
#include <QUrl>
|
||||
|
||||
#include <QDateTime>
|
||||
@ -45,107 +44,19 @@
|
||||
|
||||
#include "ByteArraySink.h"
|
||||
#include "ChecksumValidator.h"
|
||||
#include "FileSystem.h"
|
||||
#include "MetaCacheSink.h"
|
||||
|
||||
#include "BuildConfig.h"
|
||||
#include "Application.h"
|
||||
#include "BuildConfig.h"
|
||||
|
||||
#include "net/Logging.h"
|
||||
#include "net/NetAction.h"
|
||||
|
||||
#include "MMCTime.h"
|
||||
#include "StringUtils.h"
|
||||
|
||||
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 dl = makeShared<Download>();
|
||||
@ -185,7 +96,7 @@ void Download::addValidator(Validator* v)
|
||||
|
||||
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) {
|
||||
qCWarning(taskDownloadLogC) << getUid().toString() << "Attempt to start an aborted Download:" << m_url.toString();
|
||||
@ -252,12 +163,15 @@ void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
||||
auto remaing_time_s = (bytesTotal - bytesReceived) / dl_speed_bps;
|
||||
|
||||
//: 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;
|
||||
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)
|
||||
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 {
|
||||
//: Download speed at 0 bytes per second
|
||||
dl_speed_str = tr("0 B/s");
|
||||
@ -290,7 +204,8 @@ void Download::sslErrors(const QList<QSslError>& errors)
|
||||
{
|
||||
int i = 1;
|
||||
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();
|
||||
qCCritical(taskDownloadLogC) << getUid().toString() << "Certificate in question:\n" << cert.toText();
|
||||
i++;
|
||||
|
@ -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
|
||||
|
@ -3,6 +3,8 @@
|
||||
# prevent log spam and strange bugs
|
||||
# qt.qpa.drawing in particular causes theme artifacts on MacOS
|
||||
qt.*.debug=false
|
||||
# dont log credentials buy defualt
|
||||
launcher.auth.credentials.debug=false
|
||||
# remove the debug lines, other log levels still get through
|
||||
launcher.task.net.download.debug=false
|
||||
# enable or disable whole catageries
|
||||
|
@ -35,17 +35,17 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <QUuid>
|
||||
#include <QHash>
|
||||
#include <QQueue>
|
||||
#include <QSet>
|
||||
#include <QUuid>
|
||||
#include <memory>
|
||||
|
||||
#include "tasks/Task.h"
|
||||
|
||||
class ConcurrentTask : public Task {
|
||||
Q_OBJECT
|
||||
public:
|
||||
public:
|
||||
using Ptr = shared_qobject_ptr<ConcurrentTask>;
|
||||
|
||||
explicit ConcurrentTask(QObject* parent = nullptr, QString task_name = "", int max_concurrent = 6);
|
||||
@ -58,7 +58,7 @@ public:
|
||||
|
||||
void addTask(Task::Ptr task);
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
bool abort() override;
|
||||
|
||||
/** Resets the internal state of the task.
|
||||
@ -66,20 +66,19 @@ public slots:
|
||||
*/
|
||||
void clear();
|
||||
|
||||
protected
|
||||
slots:
|
||||
protected slots:
|
||||
void executeTask() override;
|
||||
|
||||
virtual void startNext();
|
||||
|
||||
void subTaskSucceeded(Task::Ptr);
|
||||
void subTaskFailed(Task::Ptr, const QString &msg);
|
||||
void subTaskStatus(Task::Ptr task, const QString &msg);
|
||||
void subTaskDetails(Task::Ptr task, const QString &msg);
|
||||
void subTaskFailed(Task::Ptr, const QString& msg);
|
||||
void subTaskStatus(Task::Ptr task, const QString& msg);
|
||||
void subTaskDetails(Task::Ptr task, const QString& msg);
|
||||
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
|
||||
void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_step_progress);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
// NOTE: This is not thread-safe.
|
||||
[[nodiscard]] unsigned int totalSize() const { return m_queue.size() + m_doing.size() + m_done.size(); }
|
||||
|
||||
@ -88,7 +87,7 @@ protected:
|
||||
|
||||
virtual void updateState();
|
||||
|
||||
protected:
|
||||
protected:
|
||||
QString m_name;
|
||||
QString m_step_status;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user