From e1c91cbeba00f69c25cad600e6be1c8e02110b11 Mon Sep 17 00:00:00 2001 From: Lukas Waslowski Date: Fri, 26 Apr 2024 19:27:30 +0000 Subject: [PATCH] PlaylistStatsDAO: Add TrackCollection::getPlaylistStatsDAO() --- src/library/dao/playliststatsdao.cpp | 5 +++-- src/library/dao/playliststatsdao.h | 2 +- src/library/trackcollection.cpp | 16 +++++++++++---- src/library/trackcollection.h | 21 ++++++++++++++++++++ src/library/trackset/baseplaylistfeature.cpp | 14 ++++--------- src/library/trackset/baseplaylistfeature.h | 2 -- src/library/trackset/playlistfeature.cpp | 18 ++++++----------- src/library/trackset/setlogfeature.cpp | 14 ++++--------- 8 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/library/dao/playliststatsdao.cpp b/src/library/dao/playliststatsdao.cpp index d1faee16b29..a04e4fa5bfb 100644 --- a/src/library/dao/playliststatsdao.cpp +++ b/src/library/dao/playliststatsdao.cpp @@ -11,13 +11,14 @@ #include "util/math.h" PlaylistStatsDAO::PlaylistStatsDAO(const QString& countsDurationTableName, - const PlaylistDAO::HiddenType hiddenType) + const PlaylistDAO::HiddenType playlistType) : m_countsDurationTableName(countsDurationTableName), - m_hiddenType(hiddenType) { + m_hiddenType(playlistType) { } void PlaylistStatsDAO::initialize(const QSqlDatabase& database) { DAO::initialize(database); + preparePlaylistSummaryTable(); } void PlaylistStatsDAO::preparePlaylistSummaryTable() { diff --git a/src/library/dao/playliststatsdao.h b/src/library/dao/playliststatsdao.h index b964df05f74..a0f911e9fcc 100644 --- a/src/library/dao/playliststatsdao.h +++ b/src/library/dao/playliststatsdao.h @@ -13,7 +13,7 @@ class PlaylistStatsDAO : public QObject, public virtual DAO { Q_OBJECT public: PlaylistStatsDAO(const QString& countsDurationTableName, - const PlaylistDAO::HiddenType hiddenType); + const PlaylistDAO::HiddenType playlistType); ~PlaylistStatsDAO() override = default; void initialize(const QSqlDatabase& database) override; diff --git a/src/library/trackcollection.cpp b/src/library/trackcollection.cpp index 8bd9b511b0f..1813f6151da 100644 --- a/src/library/trackcollection.cpp +++ b/src/library/trackcollection.cpp @@ -16,12 +16,18 @@ mixxx::Logger kLogger("TrackCollection"); } // anonymous namespace TrackCollection::TrackCollection( - QObject* parent, - const UserSettingsPointer& pConfig) + QObject* parent, const UserSettingsPointer& pConfig) : QObject(parent), + m_playlistStatsDao(QStringLiteral("PlaylistsCountsDurations"), + PlaylistDAO::PLHT_NOT_HIDDEN), + m_setlogStatsDao(QStringLiteral("SetlogCountsDurations"), + PlaylistDAO::PLHT_SET_LOG), m_analysisDao(pConfig), - m_trackDao(m_cueDao, m_playlistDao, - m_analysisDao, m_libraryHashDao, pConfig) { + m_trackDao(m_cueDao, + m_playlistDao, + m_analysisDao, + m_libraryHashDao, + pConfig) { // Forward signals from TrackDAO connect(&m_trackDao, &TrackDAO::trackClean, @@ -78,6 +84,8 @@ void TrackCollection::connectDatabase(const QSqlDatabase& database) { m_database = database; m_trackDao.initialize(database); m_playlistDao.initialize(database); + m_playlistStatsDao.initialize(database); + m_setlogStatsDao.initialize(database); m_cueDao.initialize(database); m_directoryDao.initialize(database); m_analysisDao.initialize(database); diff --git a/src/library/trackcollection.h b/src/library/trackcollection.h index 167f3446c15..2290c5efd72 100644 --- a/src/library/trackcollection.h +++ b/src/library/trackcollection.h @@ -11,6 +11,7 @@ #include "library/dao/directorydao.h" #include "library/dao/libraryhashdao.h" #include "library/dao/playlistdao.h" +#include "library/dao/playliststatsdao.h" #include "library/dao/trackdao.h" #include "library/trackset/crate/cratestorage.h" #include "preferences/usersettings.h" @@ -60,6 +61,24 @@ class TrackCollection : public QObject, DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this); return m_playlistDao; } + PlaylistStatsDAO& getPlaylistStatsDAO(PlaylistDAO::HiddenType playlistType) { + DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this); + switch (playlistType) { + case PlaylistDAO::PLHT_NOT_HIDDEN: { + return m_playlistStatsDao; + } + case PlaylistDAO::PLHT_SET_LOG: { + return m_setlogStatsDao; + } + case PlaylistDAO::PLHT_AUTO_DJ: + case PlaylistDAO::PLHT_UNKNOWN: + default: { + // This can only ever happen due to a coding error + DEBUG_ASSERT(false); + return m_playlistStatsDao; + } + } + } const DirectoryDAO& getDirectoryDAO() const { DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this); return m_directoryDao; @@ -169,6 +188,8 @@ class TrackCollection : public QObject, QSqlDatabase m_database; PlaylistDAO m_playlistDao; + PlaylistStatsDAO m_playlistStatsDao; + PlaylistStatsDAO m_setlogStatsDao; CrateStorage m_crates; CueDAO m_cueDao; DirectoryDAO m_directoryDao; diff --git a/src/library/trackset/baseplaylistfeature.cpp b/src/library/trackset/baseplaylistfeature.cpp index 2e77662df38..e64fae9fee7 100644 --- a/src/library/trackset/baseplaylistfeature.cpp +++ b/src/library/trackset/baseplaylistfeature.cpp @@ -41,14 +41,12 @@ BasePlaylistFeature::BasePlaylistFeature( PlaylistTableModel* pModel, const QString& rootViewName, const QString& iconName, - const QString& countsDurationTableName, bool keepHiddenTracks) : BaseTrackSetFeature(pLibrary, pConfig, rootViewName, iconName), m_playlistDao(pLibrary->trackCollectionManager() ->internalCollection() ->getPlaylistDAO()), m_pPlaylistTableModel(pModel), - m_countsDurationTableName(countsDurationTableName), m_keepHiddenTracks(keepHiddenTracks) { pModel->setParent(this); @@ -715,14 +713,10 @@ void BasePlaylistFeature::htmlLinkClicked(const QUrl& link) { QString BasePlaylistFeature::fetchPlaylistLabel(int playlistId) { // This queries the temporary id/count/duration table that was has been created // by the features' createPlaylistLabels() (updated each time playlists are added/removed) - QSqlDatabase database = - m_pLibrary->trackCollectionManager()->internalCollection()->database(); - - PlaylistStatsDAO playlistStatsDao( - m_countsDurationTableName, - PlaylistDAO::PLHT_NOT_HIDDEN); - - playlistStatsDao.initialize(database); + PlaylistStatsDAO& playlistStatsDao = + m_pLibrary->trackCollectionManager() + ->internalCollection() + ->getPlaylistStatsDAO(PlaylistDAO::PLHT_NOT_HIDDEN); auto playlistInfo = playlistStatsDao.getPlaylistSummary(playlistId); if (!playlistInfo.isEmpty()) { diff --git a/src/library/trackset/baseplaylistfeature.h b/src/library/trackset/baseplaylistfeature.h index 095ae395dcb..741ad0992f5 100644 --- a/src/library/trackset/baseplaylistfeature.h +++ b/src/library/trackset/baseplaylistfeature.h @@ -26,7 +26,6 @@ class BasePlaylistFeature : public BaseTrackSetFeature { PlaylistTableModel* pModel, const QString& rootViewName, const QString& iconName, - const QString& countsDurationTableName, bool keepHiddenTracks = false); ~BasePlaylistFeature() override = default; @@ -117,7 +116,6 @@ class BasePlaylistFeature : public BaseTrackSetFeature { PlaylistTableModel* m_pPlaylistTableModel; QSet m_playlistIdsOfSelectedTrack; - const QString m_countsDurationTableName; private slots: void slotTrackSelected(TrackId trackId); diff --git a/src/library/trackset/playlistfeature.cpp b/src/library/trackset/playlistfeature.cpp index 556f5fb6d21..6852b34fa34 100644 --- a/src/library/trackset/playlistfeature.cpp +++ b/src/library/trackset/playlistfeature.cpp @@ -26,8 +26,7 @@ PlaylistFeature::PlaylistFeature(Library* pLibrary, UserSettingsPointer pConfig) pLibrary->trackCollectionManager(), "mixxx.db.model.playlist"), QStringLiteral("PLAYLISTHOME"), - QStringLiteral("playlist"), - QStringLiteral("PlaylistsCountsDurations")) { + QStringLiteral("playlist")) { // construct child model std::unique_ptr pRootItem = TreeItem::newRoot(this); m_pSidebarModel->setRootItem(std::move(pRootItem)); @@ -120,17 +119,12 @@ bool PlaylistFeature::dragMoveAcceptChild(const QModelIndex& index, const QUrl& } QList PlaylistFeature::createPlaylistLabels() { - QSqlDatabase database = - m_pLibrary->trackCollectionManager()->internalCollection()->database(); - - PlaylistStatsDAO playlistStatsDao( - m_countsDurationTableName, - PlaylistDAO::PLHT_NOT_HIDDEN); - - playlistStatsDao.initialize(database); - playlistStatsDao.preparePlaylistSummaryTable(); - // Setup the sidebar playlist model + PlaylistStatsDAO& playlistStatsDao = + m_pLibrary->trackCollectionManager() + ->internalCollection() + ->getPlaylistStatsDAO(PlaylistDAO::PLHT_NOT_HIDDEN); + QList playlistLabels; for (auto playlistInfo : playlistStatsDao.getPlaylistSummaries()) { BasePlaylistFeature::IdAndLabel idAndLabel; diff --git a/src/library/trackset/setlogfeature.cpp b/src/library/trackset/setlogfeature.cpp index 890d5dd1106..d6b51979640 100644 --- a/src/library/trackset/setlogfeature.cpp +++ b/src/library/trackset/setlogfeature.cpp @@ -38,7 +38,6 @@ SetlogFeature::SetlogFeature( /*keep hidden tracks*/ true), QStringLiteral("SETLOGHOME"), QStringLiteral("history"), - QStringLiteral("SetlogCountsDurations"), /*keep hidden tracks*/ true), m_currentPlaylistId(kInvalidPlaylistId), m_yearNodeId(kInvalidPlaylistId), @@ -231,15 +230,10 @@ void SetlogFeature::onRightClickChild(const QPoint& globalPos, const QModelIndex QModelIndex SetlogFeature::constructChildModel(int selectedId) { // qDebug() << "SetlogFeature::constructChildModel() selected:" << selectedId; // Setup the sidebar playlist model - QSqlDatabase database = - m_pLibrary->trackCollectionManager()->internalCollection()->database(); - - PlaylistStatsDAO playlistStatsDao( - m_countsDurationTableName, - PlaylistDAO::PLHT_SET_LOG); - - playlistStatsDao.initialize(database); - playlistStatsDao.preparePlaylistSummaryTable(); + PlaylistStatsDAO& playlistStatsDao = + m_pLibrary->trackCollectionManager() + ->internalCollection() + ->getPlaylistStatsDAO(PlaylistDAO::PLHT_SET_LOG); // Nice to have: restore previous expanded/collapsed state of YEAR items clearChildModel();