diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index c31d2e6..3de9736 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -484,10 +484,13 @@ void ContentManager::updateDownload(QString bookId) { const auto downloadState = m_downloads.value(bookId); if ( downloadState && !downloadState->paused ) { - // This calls ContentManager::updateDownloadInfos() in a convoluted way - // and also has some other side-effects - if ( ! managerModel->updateDownload(bookId) ) { + const auto downloadInfo = updateDownloadInfos(bookId, {"status", "completedLength", "totalLength", "downloadSpeed"}); + const bool downloadStillValid = downloadState->update(downloadInfo); + if ( ! downloadStillValid ) { m_downloads.remove(bookId); + managerModel->removeDownload(bookId); + } else { + managerModel->updateDownload(bookId); } } } diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 4dfc6de..8428263 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -245,31 +245,15 @@ void ContentManagerModel::updateImage(QString bookId, QString url, QByteArray im emit dataChanged(index, index); } -bool ContentManagerModel::updateDownload(QString bookId) +void ContentManagerModel::updateDownload(QString bookId) { - const auto download = m_downloads.value(bookId); - - if ( ! download ) - return true; - - const auto downloadInfos = KiwixApp::instance()->getContentManager()->updateDownloadInfos(bookId, {"status", "completedLength", "totalLength", "downloadSpeed"}); - const bool downloadStillValid = download->update(downloadInfos); - - // The ContentManager::updateDownloadInfos() 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 ) { - removeDownload(bookId); - } else if ( it != bookIdToRowMap.constEnd() ) { + if ( it != bookIdToRowMap.constEnd() ) { const size_t row = it.value(); const QModelIndex newIndex = this->index(row, 5); emit dataChanged(newIndex, newIndex); } - return downloadStillValid; } diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h index f7f67f9..bfc0e52 100644 --- a/src/contentmanagermodel.h +++ b/src/contentmanagermodel.h @@ -50,7 +50,7 @@ public slots: void pauseDownload(QModelIndex index); void resumeDownload(QModelIndex index); void removeDownload(QString bookId); - bool updateDownload(QString bookId); + void updateDownload(QString bookId); protected: // functions bool canFetchMore(const QModelIndex &parent) const override;