mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-23 20:20:58 -04:00
More reliable updates of download status
This fix is rather a temporary and ugly hack, since the current mess with how download states are being maintained (in the presence of multiple sources of their updates) asks for a major clean-up.
This commit is contained in:
parent
79e71e3c3d
commit
3e3cd676b5
@ -144,6 +144,7 @@ std::shared_ptr<RowNode> ContentManagerModel::createNode(BookInfo bookItem, QMap
|
|||||||
void ContentManagerModel::setupNodes()
|
void ContentManagerModel::setupNodes()
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
|
bookIdToRowMap.clear();
|
||||||
for (auto bookItem : m_data) {
|
for (auto bookItem : m_data) {
|
||||||
const auto rowNode = createNode(bookItem, iconMap);
|
const auto rowNode = createNode(bookItem, iconMap);
|
||||||
|
|
||||||
@ -153,6 +154,7 @@ void ContentManagerModel::setupNodes()
|
|||||||
rowNode->setDownloadState(downloadIter.value());
|
rowNode->setDownloadState(downloadIter.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bookIdToRowMap[bookItem["id"].toString()] = rootNode->childCount();
|
||||||
rootNode->appendChild(rowNode);
|
rootNode->appendChild(rowNode);
|
||||||
}
|
}
|
||||||
endResetModel();
|
endResetModel();
|
||||||
@ -254,11 +256,33 @@ void ContentManagerModel::startDownload(QModelIndex index)
|
|||||||
node->setDownloadState(newDownload);
|
node->setDownloadState(newDownload);
|
||||||
QTimer *timer = newDownload->getDownloadUpdateTimer();
|
QTimer *timer = newDownload->getDownloadUpdateTimer();
|
||||||
connect(timer, &QTimer::timeout, this, [=]() {
|
connect(timer, &QTimer::timeout, this, [=]() {
|
||||||
if ( ! newDownload->update(bookId) ) {
|
// We may not use node in this lambda since it may have been
|
||||||
|
// invalidated by a call to ContentManagerModel::setBooksData().
|
||||||
|
|
||||||
|
const bool downloadStillValid = newDownload->update(bookId);
|
||||||
|
|
||||||
|
// newDownload->update() call above may result in
|
||||||
|
// ContentManagerModel::setBooksData() being called (through a chain
|
||||||
|
// of signals), which in turn will rebuild bookIdToRowMap. Hence
|
||||||
|
// bookIdToRowMap access must happen after it.
|
||||||
|
|
||||||
|
const auto it = bookIdToRowMap.constFind(bookId);
|
||||||
|
|
||||||
|
if ( ! downloadStillValid ) {
|
||||||
m_downloads.remove(bookId);
|
m_downloads.remove(bookId);
|
||||||
node->setDownloadState(nullptr);
|
if ( it != bookIdToRowMap.constEnd() ) {
|
||||||
|
const size_t row = it.value();
|
||||||
|
RowNode& rowNode = static_cast<RowNode&>(*rootNode->child(row));
|
||||||
|
rowNode.setDownloadState(nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( it != bookIdToRowMap.constEnd() ) {
|
||||||
|
const size_t row = it.value();
|
||||||
|
const QModelIndex rootNodeIndex = this->index(0, 0);
|
||||||
|
const QModelIndex newIndex = this->index(row, 5, rootNodeIndex);
|
||||||
|
emit dataChanged(newIndex, newIndex);
|
||||||
}
|
}
|
||||||
emit dataChanged(index, index);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +58,7 @@ private: // data
|
|||||||
std::shared_ptr<RowNode> rootNode;
|
std::shared_ptr<RowNode> rootNode;
|
||||||
int zimCount = 0;
|
int zimCount = 0;
|
||||||
ThumbnailDownloader td;
|
ThumbnailDownloader td;
|
||||||
|
QMap<QString, size_t> bookIdToRowMap;
|
||||||
QMap<QString, QByteArray> iconMap;
|
QMap<QString, QByteArray> iconMap;
|
||||||
QMap<QString, std::shared_ptr<DownloadState>> m_downloads;
|
QMap<QString, std::shared_ptr<DownloadState>> m_downloads;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user