DownloadManager::cancelDownload()

This commit is contained in:
Veloman Yunkan 2024-05-24 17:21:44 +04:00 committed by Kelson
parent 28c52e7884
commit 88eb29f791
3 changed files with 26 additions and 17 deletions

View File

@ -744,25 +744,9 @@ void ContentManager::cancelBook(const QString& id)
void ContentManager::reallyCancelBook(const QString& id)
{
const auto downloadId = mp_library->getBookById(id).getDownloadId();
if ( downloadId.empty() ) {
// Completion of the download has been detected (and its id was reset)
// before the confirmation to cancel the download was granted.
if ( !DownloadManager::cancelDownload(id) )
return;
}
auto download = mp_downloader->getDownload(downloadId);
try {
download->cancelDownload();
} catch (const kiwix::AriaError&) {
// Download has completed before the cancel request was handled.
// Most likely the download was already complete at the time
// when ContentManager::reallyCancelBook() started executing, but
// its completion was not yet detected (and/or handled) by the
// download updater thread (letting the code pass past the empty
// downloadId check above).
return;
}
removeDownload(id);
// incompleted downloaded file should be perma deleted

View File

@ -148,3 +148,27 @@ void DownloadManager::resumeDownload(const QString& bookId)
download->resumeDownload();
}
}
bool DownloadManager::cancelDownload(const QString& bookId)
{
const auto downloadId = mp_library->getBookById(bookId).getDownloadId();
if ( downloadId.empty() ) {
// Completion of the download has been detected (and its id was reset)
// before the confirmation to cancel the download was granted.
return false;
}
auto download = mp_downloader->getDownload(downloadId);
try {
download->cancelDownload();
return true;
} catch (const kiwix::AriaError&) {
// Download has completed before the cancel request was handled.
// Most likely the download was already complete at the time
// when ContentManager::reallyCancelBook() started executing, but
// its completion was not yet detected (and/or handled) by the
// download updater thread (letting the code pass past the empty
// downloadId check above).
return false;
}
}

View File

@ -78,6 +78,7 @@ public: // functions
std::string startDownload(const std::string& url, const std::string& downloadDirPath);
void pauseDownload(const QString& bookId);
void resumeDownload(const QString& bookId);
bool cancelDownload(const QString& bookId);
signals:
void downloadUpdated(QString bookId, const DownloadInfo& );