From 90603190fea105f3a55631f69e775fe666a391b4 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 18 Feb 2024 16:29:35 +0400 Subject: [PATCH] Dropped ContentManagerModel::m_downloads ContentManagerModel is explicitly given access to the list of active downloads during calls to ContentManagerModel::setBooksData(). This limits by design the time when the list of downloads can be accessed and facilitates making it thread-safe. --- src/contentmanager.cpp | 4 ++-- src/contentmanagermodel.cpp | 13 ++++++------- src/contentmanagermodel.h | 7 +++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 64cc47d..bde2dbb 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -90,7 +90,7 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, // mp_view will be passed to the tab who will take ownership, // so, we don't need to delete it. mp_view = new ContentManagerView(); - managerModel = new ContentManagerModel(&m_downloads, this); + managerModel = new ContentManagerModel(this); updateModel(); auto treeView = mp_view->getView(); treeView->setModel(managerModel); @@ -145,7 +145,7 @@ void ContentManager::updateModel() auto mp = getBookInfos(bookId, keys); bookList.append(mp); } - managerModel->setBooksData(bookList); + managerModel->setBooksData(bookList, m_downloads); } void ContentManager::onCustomContextMenu(const QPoint &point) diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index bb8169a..d743eb1 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -7,9 +7,8 @@ #include "kiwixapp.h" #include -ContentManagerModel::ContentManagerModel(const Downloads* downloads, QObject *parent) +ContentManagerModel::ContentManagerModel(QObject *parent) : QAbstractItemModel(parent) - , m_downloads(*downloads) { connect(&td, &ThumbnailDownloader::oneThumbnailDownloaded, this, &ContentManagerModel::updateImage); } @@ -112,11 +111,11 @@ QVariant ContentManagerModel::headerData(int section, Qt::Orientation orientatio } } -void ContentManagerModel::setBooksData(const BookInfoList& data) +void ContentManagerModel::setBooksData(const BookInfoList& data, const Downloads& downloads) { m_data = data; rootNode = std::shared_ptr(new RowNode({tr("Icon"), tr("Name"), tr("Date"), tr("Size"), tr("Content Type"), tr("Download")}, "", std::weak_ptr())); - setupNodes(); + setupNodes(downloads); emit dataChanged(QModelIndex(), QModelIndex()); } @@ -151,7 +150,7 @@ std::shared_ptr ContentManagerModel::createNode(BookInfo bookItem) cons return rowNodePtr; } -void ContentManagerModel::setupNodes() +void ContentManagerModel::setupNodes(const Downloads& downloads) { beginResetModel(); bookIdToRowMap.clear(); @@ -159,8 +158,8 @@ void ContentManagerModel::setupNodes() const auto rowNode = createNode(bookItem); // Restore download state during model updates (filtering, etc) - const auto downloadIter = m_downloads.constFind(rowNode->getBookId()); - if ( downloadIter != m_downloads.constEnd() ) { + const auto downloadIter = downloads.constFind(rowNode->getBookId()); + if ( downloadIter != downloads.constEnd() ) { rowNode->setDownloadState(downloadIter.value()); } diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h index b6f0e5b..09b1a63 100644 --- a/src/contentmanagermodel.h +++ b/src/contentmanagermodel.h @@ -25,7 +25,7 @@ public: // types typedef QMap> Downloads; public: // functions - ContentManagerModel(const Downloads* downloads, QObject *parent = nullptr); + explicit ContentManagerModel(QObject *parent = nullptr); ~ContentManagerModel(); QVariant data(const QModelIndex &index, int role) const override; @@ -37,8 +37,8 @@ public: // functions QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; - void setBooksData(const BookInfoList& data); - void setupNodes(); + void setBooksData(const BookInfoList& data, const Downloads& downloads); + void setupNodes(const Downloads& downloads); bool hasChildren(const QModelIndex &parent) const override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; @@ -63,7 +63,6 @@ private: // data mutable ThumbnailDownloader td; QMap bookIdToRowMap; QMap m_iconMap; - const Downloads& m_downloads; }; #endif // CONTENTMANAGERMODEL_H