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.
This commit is contained in:
Veloman Yunkan 2024-02-18 16:29:35 +04:00
parent ef1b36fd28
commit 90603190fe
3 changed files with 11 additions and 13 deletions

View File

@ -90,7 +90,7 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
// mp_view will be passed to the tab who will take ownership, // mp_view will be passed to the tab who will take ownership,
// so, we don't need to delete it. // so, we don't need to delete it.
mp_view = new ContentManagerView(); mp_view = new ContentManagerView();
managerModel = new ContentManagerModel(&m_downloads, this); managerModel = new ContentManagerModel(this);
updateModel(); updateModel();
auto treeView = mp_view->getView(); auto treeView = mp_view->getView();
treeView->setModel(managerModel); treeView->setModel(managerModel);
@ -145,7 +145,7 @@ void ContentManager::updateModel()
auto mp = getBookInfos(bookId, keys); auto mp = getBookInfos(bookId, keys);
bookList.append(mp); bookList.append(mp);
} }
managerModel->setBooksData(bookList); managerModel->setBooksData(bookList, m_downloads);
} }
void ContentManager::onCustomContextMenu(const QPoint &point) void ContentManager::onCustomContextMenu(const QPoint &point)

View File

@ -7,9 +7,8 @@
#include "kiwixapp.h" #include "kiwixapp.h"
#include <kiwix/tools.h> #include <kiwix/tools.h>
ContentManagerModel::ContentManagerModel(const Downloads* downloads, QObject *parent) ContentManagerModel::ContentManagerModel(QObject *parent)
: QAbstractItemModel(parent) : QAbstractItemModel(parent)
, m_downloads(*downloads)
{ {
connect(&td, &ThumbnailDownloader::oneThumbnailDownloaded, this, &ContentManagerModel::updateImage); 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; m_data = data;
rootNode = std::shared_ptr<RowNode>(new RowNode({tr("Icon"), tr("Name"), tr("Date"), tr("Size"), tr("Content Type"), tr("Download")}, "", std::weak_ptr<RowNode>())); rootNode = std::shared_ptr<RowNode>(new RowNode({tr("Icon"), tr("Name"), tr("Date"), tr("Size"), tr("Content Type"), tr("Download")}, "", std::weak_ptr<RowNode>()));
setupNodes(); setupNodes(downloads);
emit dataChanged(QModelIndex(), QModelIndex()); emit dataChanged(QModelIndex(), QModelIndex());
} }
@ -151,7 +150,7 @@ std::shared_ptr<RowNode> ContentManagerModel::createNode(BookInfo bookItem) cons
return rowNodePtr; return rowNodePtr;
} }
void ContentManagerModel::setupNodes() void ContentManagerModel::setupNodes(const Downloads& downloads)
{ {
beginResetModel(); beginResetModel();
bookIdToRowMap.clear(); bookIdToRowMap.clear();
@ -159,8 +158,8 @@ void ContentManagerModel::setupNodes()
const auto rowNode = createNode(bookItem); const auto rowNode = createNode(bookItem);
// Restore download state during model updates (filtering, etc) // Restore download state during model updates (filtering, etc)
const auto downloadIter = m_downloads.constFind(rowNode->getBookId()); const auto downloadIter = downloads.constFind(rowNode->getBookId());
if ( downloadIter != m_downloads.constEnd() ) { if ( downloadIter != downloads.constEnd() ) {
rowNode->setDownloadState(downloadIter.value()); rowNode->setDownloadState(downloadIter.value());
} }

View File

@ -25,7 +25,7 @@ public: // types
typedef QMap<QString, std::shared_ptr<DownloadState>> Downloads; typedef QMap<QString, std::shared_ptr<DownloadState>> Downloads;
public: // functions public: // functions
ContentManagerModel(const Downloads* downloads, QObject *parent = nullptr); explicit ContentManagerModel(QObject *parent = nullptr);
~ContentManagerModel(); ~ContentManagerModel();
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
@ -37,8 +37,8 @@ public: // functions
QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override;
void setBooksData(const BookInfoList& data); void setBooksData(const BookInfoList& data, const Downloads& downloads);
void setupNodes(); void setupNodes(const Downloads& downloads);
bool hasChildren(const QModelIndex &parent) const override; bool hasChildren(const QModelIndex &parent) const override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
@ -63,7 +63,6 @@ private: // data
mutable ThumbnailDownloader td; mutable ThumbnailDownloader td;
QMap<QString, size_t> bookIdToRowMap; QMap<QString, size_t> bookIdToRowMap;
QMap<QString, QByteArray> m_iconMap; QMap<QString, QByteArray> m_iconMap;
const Downloads& m_downloads;
}; };
#endif // CONTENTMANAGERMODEL_H #endif // CONTENTMANAGERMODEL_H