More elegant ContentManager::updateDownload()

This commit is contained in:
Veloman Yunkan 2024-02-11 20:11:51 +04:00
parent defe52a246
commit e0f79bf9f2
2 changed files with 3 additions and 10 deletions

View File

@ -418,6 +418,7 @@ void ContentManager::removeDownload(QString bookId)
void ContentManager::downloadCancelled(QString bookId) void ContentManager::downloadCancelled(QString bookId)
{ {
removeDownload(bookId);
kiwix::Book bCopy(mp_library->getBookById(bookId)); kiwix::Book bCopy(mp_library->getBookById(bookId));
bCopy.setDownloadId(""); bCopy.setDownloadId("");
mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy); mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy);
@ -427,6 +428,7 @@ void ContentManager::downloadCancelled(QString bookId)
void ContentManager::downloadCompleted(QString bookId, QString path) void ContentManager::downloadCompleted(QString bookId, QString path)
{ {
removeDownload(bookId);
kiwix::Book bCopy(mp_library->getBookById(bookId)); kiwix::Book bCopy(mp_library->getBookById(bookId));
bCopy.setPath(QDir::toNativeSeparators(path).toStdString()); bCopy.setPath(QDir::toNativeSeparators(path).toStdString());
bCopy.setDownloadId(""); bCopy.setDownloadId("");
@ -480,12 +482,8 @@ void ContentManager::updateDownload(QString bookId)
downloadCancelled(bookId); downloadCancelled(bookId);
} else if ( downloadInfo["status"] == "completed" ) { } else if ( downloadInfo["status"] == "completed" ) {
downloadCompleted(bookId, downloadInfo["path"].toString()); downloadCompleted(bookId, downloadInfo["path"].toString());
}
const bool downloadStillValid = downloadState->update(downloadInfo);
if ( ! downloadStillValid ) {
removeDownload(bookId);
} else { } else {
downloadState->update(downloadInfo);
managerModel->updateDownload(bookId); managerModel->updateDownload(bookId);
} }
} }

View File

@ -28,11 +28,6 @@ QString convertToUnits(QString size)
bool DownloadState::update(const DownloadInfo& downloadInfos) bool DownloadState::update(const DownloadInfo& downloadInfos)
{ {
if (!downloadInfos["status"].isValid()) {
*this = {0, "", "", false};
return false;
}
double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble(); double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble();
percent *= 100; percent *= 100;
percent = QString::number(percent, 'g', 3).toDouble(); percent = QString::number(percent, 'g', 3).toDouble();