DownloadManager::pauseDownload()

This commit is contained in:
Veloman Yunkan 2024-05-23 13:51:37 +04:00 committed by Kelson
parent 1354363bfc
commit 24cce79c1e
3 changed files with 29 additions and 22 deletions

View File

@ -727,28 +727,7 @@ void ContentManager::eraseBook(const QString& id)
void ContentManager::pauseBook(const QString& id, QModelIndex index)
{
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 pause-download action was triggered (most likely through
// the context menu which can stay open for an arbitrarily long time,
// or, unlikely, through the ⏸ button during the last milliseconds of
// the download progress).
return;
}
auto download = mp_downloader->getDownload(downloadId);
if (download->getStatus() == kiwix::Download::K_ACTIVE) {
try {
download->pauseDownload();
} catch (const kiwix::AriaError&) {
// Download has completed before the pause request was handled.
// Most likely the download was already complete at the time
// when ContentManager::pauseBook() started executing, but its
// completion was not yet detected (and/or handled) by the download
// updater thread.
}
}
DownloadManager::pauseDownload(id);
managerModel->triggerDataUpdateAt(index);
}

View File

@ -103,3 +103,30 @@ DownloadInfo DownloadManager::getDownloadInfo(QString bookId) const
{ "path" , QString::fromStdString(d->getPath()) }
};
}
void DownloadManager::pauseDownload(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 pause-download action was triggered (most likely through
// the context menu which can stay open for an arbitrarily long time,
// or, unlikely, through the ⏸ button during the last milliseconds of
// the download progress).
return;
}
auto download = mp_downloader->getDownload(downloadId);
if (download->getStatus() == kiwix::Download::K_ACTIVE) {
try {
download->pauseDownload();
} catch (const kiwix::AriaError&) {
// Download has completed before the pause request was handled.
// Most likely the download was already complete at the time
// when ContentManager::pauseBook() started executing, but its
// completion was not yet detected (and/or handled) by the download
// updater thread.
}
}
}

View File

@ -73,6 +73,7 @@ public: // functions
DownloadInfo getDownloadInfo(QString bookId) const;
void restoreDownloads();
void updateDownloads();
void pauseDownload(const QString& bookId);
signals:
void downloadUpdated(QString bookId, const DownloadInfo& );