RowNode::updateDownloadStatus()

This commit is contained in:
Veloman Yunkan 2023-12-13 17:28:01 +04:00
parent 4fcf1fbea4
commit f8ec68cfb8
3 changed files with 37 additions and 25 deletions

View File

@ -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<RowNode> ContentManagerModel::createNode(BookInfo bookItem, QMap<QString, QByteArray> iconMap) const
{
auto faviconUrl = "https://" + bookItem["faviconUrl"].toString();
@ -254,19 +240,9 @@ void ContentManagerModel::startDownload(QModelIndex index)
{
auto node = getSharedPointer(static_cast<RowNode*>(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);
});
}

View File

@ -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
}
}

View File

@ -35,6 +35,8 @@ public:
void setIsDownloading(bool val);
bool isChild(Node* candidate);
void updateDownloadStatus();
private:
QList<QVariant> m_itemData;
QList<std::shared_ptr<Node>> m_childItems;