From ad09ee10e1e923950c59177f2f85b800d72be621 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 13 Dec 2023 19:43:49 +0400 Subject: [PATCH] Better grouping of code --- src/rownode.cpp | 135 ++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 63 deletions(-) diff --git a/src/rownode.cpp b/src/rownode.cpp index 0d86f4b..39374fb 100644 --- a/src/rownode.cpp +++ b/src/rownode.cpp @@ -3,11 +3,83 @@ #include "kiwixapp.h" #include "descriptionnode.h" +//////////////////////////////////////////////////////////////////////////////// +// DowloadState +//////////////////////////////////////////////////////////////////////////////// + DownloadState::DownloadState() : m_downloadInfo({0, "", "", false}) { } +void DownloadState::setIsDownloading(bool val) +{ + assert(val != isDownloading()); + if ( val ) { + m_downloadUpdateTimer.reset(new QTimer); + m_downloadUpdateTimer->start(1000); + } else { + m_downloadUpdateTimer->stop(); + + // Deleting the timer object immediately instead of via + // QObject::deleteLater() seems to be safe since it is not a recipient + // of any events that may be in the process of being delivered to it + // from another thread. + m_downloadUpdateTimer.reset(); + m_downloadInfo = {0, "", "", false}; + } +} + +namespace +{ + +QString convertToUnits(QString size) +{ + QStringList units = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"}; + int unitIndex = 0; + auto bytes = size.toDouble(); + while (bytes >= 1024 && unitIndex < units.size()) { + bytes /= 1024; + unitIndex++; + } + + const auto preciseBytes = QString::number(bytes, 'g', 3); + return preciseBytes + " " + units[unitIndex]; +} + +} // unnamed namespace + +void DownloadState::updateDownloadStatus(QString id) +{ + auto downloadInfos = KiwixApp::instance()->getContentManager()->updateDownloadInfos(id, {"status", "completedLength", "totalLength", "downloadSpeed"}); + double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble(); + percent *= 100; + percent = QString::number(percent, 'g', 3).toDouble(); + auto completedLength = convertToUnits(downloadInfos["completedLength"].toString()); + auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toString()) + "/s"; + m_downloadInfo = {percent, completedLength, downloadSpeed, false}; + if (!downloadInfos["status"].isValid()) { + setIsDownloading(false); // this stops & deletes the timer + } +} + +void DownloadState::pauseDownload() +{ + m_downloadInfo.paused = true; + m_downloadUpdateTimer->stop(); +} + +void DownloadState::resumeDownload() +{ + m_downloadInfo.paused = false; + m_downloadUpdateTimer->start(1000); +} + + +//////////////////////////////////////////////////////////////////////////////// +// RowNode +//////////////////////////////////////////////////////////////////////////////// + RowNode::RowNode(QList itemData, QString bookId, std::weak_ptr parent) : m_itemData(itemData), m_parentItem(parent), m_bookId(bookId) { @@ -78,66 +150,3 @@ bool RowNode::isChild(Node *candidate) } return false; } - -void DownloadState::setIsDownloading(bool val) -{ - assert(val != isDownloading()); - if ( val ) { - m_downloadUpdateTimer.reset(new QTimer); - m_downloadUpdateTimer->start(1000); - } else { - m_downloadUpdateTimer->stop(); - - // Deleting the timer object immediately instead of via - // QObject::deleteLater() seems to be safe since it is not a recipient - // of any events that may be in the process of being delivered to it - // from another thread. - m_downloadUpdateTimer.reset(); - m_downloadInfo = {0, "", "", false}; - } -} - -namespace -{ - -QString convertToUnits(QString size) -{ - QStringList units = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"}; - int unitIndex = 0; - auto bytes = size.toDouble(); - while (bytes >= 1024 && unitIndex < units.size()) { - bytes /= 1024; - unitIndex++; - } - - const auto preciseBytes = QString::number(bytes, 'g', 3); - return preciseBytes + " " + units[unitIndex]; -} - -} // unnamed namespace - -void DownloadState::updateDownloadStatus(QString id) -{ - auto downloadInfos = KiwixApp::instance()->getContentManager()->updateDownloadInfos(id, {"status", "completedLength", "totalLength", "downloadSpeed"}); - double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble(); - percent *= 100; - percent = QString::number(percent, 'g', 3).toDouble(); - auto completedLength = convertToUnits(downloadInfos["completedLength"].toString()); - auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toString()) + "/s"; - m_downloadInfo = {percent, completedLength, downloadSpeed, false}; - if (!downloadInfos["status"].isValid()) { - setIsDownloading(false); // this stops & deletes the timer - } -} - -void DownloadState::pauseDownload() -{ - m_downloadInfo.paused = true; - m_downloadUpdateTimer->stop(); -} - -void DownloadState::resumeDownload() -{ - m_downloadInfo.paused = false; - m_downloadUpdateTimer->start(1000); -}