Eliminated indirect call to ContentManager::updateDownloadInfos()

Now ContentManager::updateDownloadInfos() is called directly by
ContentManager (rather than indirectly via ContentManagerModel).
This commit is contained in:
Veloman Yunkan 2024-02-11 16:13:08 +04:00
parent b112c1b34c
commit d089dced05
3 changed files with 9 additions and 22 deletions

View File

@ -484,10 +484,13 @@ void ContentManager::updateDownload(QString bookId)
{ {
const auto downloadState = m_downloads.value(bookId); const auto downloadState = m_downloads.value(bookId);
if ( downloadState && !downloadState->paused ) { if ( downloadState && !downloadState->paused ) {
// This calls ContentManager::updateDownloadInfos() in a convoluted way const auto downloadInfo = updateDownloadInfos(bookId, {"status", "completedLength", "totalLength", "downloadSpeed"});
// and also has some other side-effects const bool downloadStillValid = downloadState->update(downloadInfo);
if ( ! managerModel->updateDownload(bookId) ) { if ( ! downloadStillValid ) {
m_downloads.remove(bookId); m_downloads.remove(bookId);
managerModel->removeDownload(bookId);
} else {
managerModel->updateDownload(bookId);
} }
} }
} }

View File

@ -245,31 +245,15 @@ void ContentManagerModel::updateImage(QString bookId, QString url, QByteArray im
emit dataChanged(index, index); 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); const auto it = bookIdToRowMap.constFind(bookId);
if ( ! downloadStillValid ) { if ( it != bookIdToRowMap.constEnd() ) {
removeDownload(bookId);
} else if ( it != bookIdToRowMap.constEnd() ) {
const size_t row = it.value(); const size_t row = it.value();
const QModelIndex newIndex = this->index(row, 5); const QModelIndex newIndex = this->index(row, 5);
emit dataChanged(newIndex, newIndex); emit dataChanged(newIndex, newIndex);
} }
return downloadStillValid;
} }

View File

@ -50,7 +50,7 @@ public slots:
void pauseDownload(QModelIndex index); void pauseDownload(QModelIndex index);
void resumeDownload(QModelIndex index); void resumeDownload(QModelIndex index);
void removeDownload(QString bookId); void removeDownload(QString bookId);
bool updateDownload(QString bookId); void updateDownload(QString bookId);
protected: // functions protected: // functions
bool canFetchMore(const QModelIndex &parent) const override; bool canFetchMore(const QModelIndex &parent) const override;