diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 164d548..ff1267e 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -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 diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index ddc5201..7fd36f3 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -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; + } +} diff --git a/src/downloadmanagement.h b/src/downloadmanagement.h index e55d563..31a8bc5 100644 --- a/src/downloadmanagement.h +++ b/src/downloadmanagement.h @@ -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& );