From 1354363bfc25bfa1b88b84bba73c700e4e7ebead Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 23 May 2024 13:23:18 +0400 Subject: [PATCH] {Content->Download}Manager::updateDownloads() One subtlety of this refactoring is that ContentManager::downloadDisappeared() is now NOT called from updateDownloads() directly/synchronously but is invoked via a signal and is thus executed in the main thread. --- src/contentmanager.cpp | 20 ++++---------------- src/contentmanager.h | 4 +--- src/downloadmanagement.cpp | 15 +++++++++++++++ src/downloadmanagement.h | 10 +++++++++- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index cc9fec6..8bbf556 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -143,9 +143,12 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader) setCategories(); setLanguages(); - connect(this, &ContentManager::downloadUpdated, + connect(this, &DownloadManager::downloadUpdated, this, &ContentManager::updateDownload); + connect(this, &DownloadManager::downloadDisappeared, + this, &ContentManager::downloadDisappeared); + if ( mp_downloader ) { startDownloadUpdaterThread(); } @@ -570,21 +573,6 @@ void ContentManager::updateDownload(QString bookId, const DownloadInfo& download } } -void ContentManager::updateDownloads() -{ - DownloadInfo downloadInfo; - for ( const auto& bookId : m_downloads.keys() ) { - try { - downloadInfo = getDownloadInfo(bookId); - } catch ( ... ) { - downloadDisappeared(bookId); - continue; - } - - emit downloadUpdated(bookId, downloadInfo); - } -} - namespace { diff --git a/src/contentmanager.h b/src/contentmanager.h index eeca2f2..18f8fad 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -9,7 +9,7 @@ #include "contentmanagermodel.h" #include "downloadmanagement.h" -class ContentManager : public QObject, private DownloadManager +class ContentManager : public DownloadManager { Q_OBJECT Q_PROPERTY(bool isLocal MEMBER m_local READ isLocal WRITE setLocal NOTIFY localChanged) @@ -81,7 +81,6 @@ signals: void categoriesLoaded(QStringList); void languagesLoaded(LanguageList); void localChanged(const bool); - void downloadUpdated(QString bookId, const DownloadInfo& ); public slots: QStringList getTranslations(const QStringList &keys); @@ -105,7 +104,6 @@ public slots: void cancelBook(const QString& id); void onCustomContextMenu(const QPoint &point); void openBookWithIndex(const QModelIndex& index); - void updateDownloads(); void updateDownload(QString bookId, const DownloadInfo& downloadInfo); private: // functions diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index b07719d..dc0e92b 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -56,6 +56,21 @@ void DownloadManager::restoreDownloads() } } +void DownloadManager::updateDownloads() +{ + DownloadInfo downloadInfo; + for ( const auto& bookId : m_downloads.keys() ) { + try { + downloadInfo = getDownloadInfo(bookId); + } catch ( ... ) { + emit downloadDisappeared(bookId); + continue; + } + + emit downloadUpdated(bookId, downloadInfo); + } +} + namespace { diff --git a/src/downloadmanagement.h b/src/downloadmanagement.h index 9065013..2450af5 100644 --- a/src/downloadmanagement.h +++ b/src/downloadmanagement.h @@ -1,6 +1,7 @@ #ifndef DOWNLOADMANAGEMENT_H #define DOWNLOADMANAGEMENT_H +#include #include #include #include @@ -27,8 +28,10 @@ public: void update(const DownloadInfo& info); }; -class DownloadManager +class DownloadManager : public QObject { + Q_OBJECT + public: // types // BookId -> DownloadState map @@ -69,6 +72,11 @@ public: // functions DownloadInfo getDownloadInfo(QString bookId) const; void restoreDownloads(); + void updateDownloads(); + +signals: + void downloadUpdated(QString bookId, const DownloadInfo& ); + void downloadDisappeared(QString bookId); protected: // data const Library* const mp_library;