mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-24 04:32:15 -04:00
RowNode::updateDownloadStatus()
This commit is contained in:
parent
4fcf1fbea4
commit
f8ec68cfb8
@ -110,20 +110,6 @@ void ContentManagerModel::setBooksData(const BookInfoList& data)
|
|||||||
emit dataChanged(QModelIndex(), QModelIndex());
|
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
|
std::shared_ptr<RowNode> ContentManagerModel::createNode(BookInfo bookItem, QMap<QString, QByteArray> iconMap) const
|
||||||
{
|
{
|
||||||
auto faviconUrl = "https://" + bookItem["faviconUrl"].toString();
|
auto faviconUrl = "https://" + bookItem["faviconUrl"].toString();
|
||||||
@ -254,19 +240,9 @@ void ContentManagerModel::startDownload(QModelIndex index)
|
|||||||
{
|
{
|
||||||
auto node = getSharedPointer(static_cast<RowNode*>(index.internalPointer()));
|
auto node = getSharedPointer(static_cast<RowNode*>(index.internalPointer()));
|
||||||
node->setIsDownloading(true); // this starts the internal timer
|
node->setIsDownloading(true); // this starts the internal timer
|
||||||
auto id = node->getBookId();
|
|
||||||
QTimer *timer = node->getDownloadUpdateTimer();
|
QTimer *timer = node->getDownloadUpdateTimer();
|
||||||
connect(timer, &QTimer::timeout, this, [=]() {
|
connect(timer, &QTimer::timeout, this, [=]() {
|
||||||
auto downloadInfos = KiwixApp::instance()->getContentManager()->updateDownloadInfos(id, {"status", "completedLength", "totalLength", "downloadSpeed"});
|
node->updateDownloadStatus();
|
||||||
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
|
|
||||||
}
|
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -91,3 +91,37 @@ void RowNode::setIsDownloading(bool val)
|
|||||||
m_downloadUpdateTimer.reset();
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -35,6 +35,8 @@ public:
|
|||||||
void setIsDownloading(bool val);
|
void setIsDownloading(bool val);
|
||||||
bool isChild(Node* candidate);
|
bool isChild(Node* candidate);
|
||||||
|
|
||||||
|
void updateDownloadStatus();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<QVariant> m_itemData;
|
QList<QVariant> m_itemData;
|
||||||
QList<std::shared_ptr<Node>> m_childItems;
|
QList<std::shared_ptr<Node>> m_childItems;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user