From 24cce79c1ef0ddf63ae709b1fb6e82e26083fc43 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 23 May 2024 13:51:37 +0400 Subject: [PATCH] DownloadManager::pauseDownload() --- src/contentmanager.cpp | 23 +---------------------- src/downloadmanagement.cpp | 27 +++++++++++++++++++++++++++ src/downloadmanagement.h | 1 + 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 8bbf556..a06062f 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -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); } diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index dc0e92b..87d3eaa 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -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. + } + } +} + diff --git a/src/downloadmanagement.h b/src/downloadmanagement.h index 2450af5..a1670fc 100644 --- a/src/downloadmanagement.h +++ b/src/downloadmanagement.h @@ -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& );