From e86324d242f5aa50b8d509c3fc27cb2509db1707 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sat, 1 Jun 2024 18:42:17 +0400 Subject: [PATCH] Enter DownloadState::Status The binary flag DownloadState::paused was replaced with a richer DownloadState::status but the old logic of distinguishing only between active and paused downloads still remains. --- src/contentmanager.cpp | 2 +- src/contentmanagerdelegate.cpp | 2 +- src/downloadmanagement.cpp | 15 ++++++++++++--- src/downloadmanagement.h | 10 +++++++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 0530de4..7b257ca 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -379,7 +379,7 @@ ContentManager::BookInfo ContentManager::getBookInfos(QString id, const QStringL ContentManager::BookState ContentManager::getBookState(QString bookId) { if ( const auto downloadState = DownloadManager::getDownloadState(bookId) ) { - return downloadState->paused + return downloadState->status == DownloadState::PAUSED ? BookState::DOWNLOAD_PAUSED : BookState::DOWNLOADING; // TODO: a download may be in error state diff --git a/src/contentmanagerdelegate.cpp b/src/contentmanagerdelegate.cpp index 80f4ba9..4727901 100644 --- a/src/contentmanagerdelegate.cpp +++ b/src/contentmanagerdelegate.cpp @@ -131,7 +131,7 @@ void showDownloadProgress(QPainter *painter, QRect box, const DownloadState& dow auto completedLength = downloadInfo.completedLength; auto downloadSpeed = downloadInfo.downloadSpeed; - if (downloadInfo.paused) { + if (downloadInfo.status == DownloadState::PAUSED) { createResumeSymbol(painter, dcl.pauseResumeButtonRect); createCancelButton(painter, dcl.cancelButtonRect); } else { diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index 88b6f76..f1011c6 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -26,6 +26,15 @@ QString convertToUnits(double bytes) return preciseBytes + " " + units[unitIndex]; } +DownloadState::Status getDownloadStatus(QString status) +{ + if ( status == "active" ) return DownloadState::DOWNLOADING; + if ( status == "paused" ) return DownloadState::PAUSED; + if ( status == "waiting" ) return DownloadState::WAITING; + if ( status == "error" ) return DownloadState::DOWNLOAD_ERROR; + return DownloadState::UNKNOWN; +} + } // unnamed namespace void DownloadState::update(const DownloadInfo& downloadInfos) @@ -35,8 +44,8 @@ void DownloadState::update(const DownloadInfo& downloadInfos) percent = QString::number(percent, 'g', 3).toDouble(); auto completedLength = convertToUnits(downloadInfos["completedLength"].toDouble()); auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toDouble()) + "/s"; - const bool paused = downloadInfos["status"] == "paused"; - *this = {percent, completedLength, downloadSpeed, paused}; + const auto status = getDownloadStatus(downloadInfos["status"].toString()); + *this = {percent, completedLength, downloadSpeed, status}; } //////////////////////////////////////////////////////////////////////////////// @@ -85,7 +94,7 @@ void DownloadManager::restoreDownloads() const kiwix::Book& book = mp_library->getBookById(bookId); if ( ! book.getDownloadId().empty() ) { const auto newDownload = std::make_shared(); - newDownload->paused = true; + newDownload->status = DownloadState::UNKNOWN; m_downloads.set(bookId, newDownload); } } diff --git a/src/downloadmanagement.h b/src/downloadmanagement.h index 5d1d16c..23c2256 100644 --- a/src/downloadmanagement.h +++ b/src/downloadmanagement.h @@ -19,10 +19,18 @@ typedef QMap DownloadInfo; class DownloadState { public: + enum Status { + UNKNOWN, + WAITING, + DOWNLOAD_ERROR, + DOWNLOADING, + PAUSED + }; + double progress = 0; QString completedLength; QString downloadSpeed; - bool paused = false; + Status status = UNKNOWN; public: void update(const DownloadInfo& info);