diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 98110eb..05e57c7 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -110,20 +110,6 @@ void ContentManagerModel::setBooksData(const BookInfoList& data) emit dataChanged(QModelIndex(), QModelIndex()); } -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]; -} - std::shared_ptr ContentManagerModel::createNode(BookInfo bookItem, QMap iconMap) const { auto faviconUrl = "https://" + bookItem["faviconUrl"].toString(); @@ -254,19 +240,9 @@ void ContentManagerModel::startDownload(QModelIndex index) { auto node = getSharedPointer(static_cast(index.internalPointer())); node->setIsDownloading(true); // this starts the internal timer - auto id = node->getBookId(); QTimer *timer = node->getDownloadUpdateTimer(); connect(timer, &QTimer::timeout, this, [=]() { - 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"; - node->setDownloadInfo({percent, completedLength, downloadSpeed, false}); - if (!downloadInfos["status"].isValid()) { - node->setIsDownloading(false); // this stops & deletes the timer - } + node->updateDownloadStatus(); emit dataChanged(index, index); }); } diff --git a/src/rownode.cpp b/src/rownode.cpp index 09f076c..3f8da3d 100644 --- a/src/rownode.cpp +++ b/src/rownode.cpp @@ -91,3 +91,37 @@ void RowNode::setIsDownloading(bool val) m_downloadUpdateTimer.reset(); } } + +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 RowNode::updateDownloadStatus() +{ + const auto id = getBookId(); + 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"; + setDownloadInfo({percent, completedLength, downloadSpeed, false}); + if (!downloadInfos["status"].isValid()) { + setIsDownloading(false); // this stops & deletes the timer + } +} diff --git a/src/rownode.h b/src/rownode.h index 23e68d3..d8a493f 100644 --- a/src/rownode.h +++ b/src/rownode.h @@ -35,6 +35,8 @@ public: void setIsDownloading(bool val); bool isChild(Node* candidate); + void updateDownloadStatus(); + private: QList m_itemData; QList> m_childItems;