File indexing completed on 2025-01-05 04:25:20

0001 #include "albumsmodel.h"
0002 #include "db/collectionDB.h"
0003 
0004 #include "vvave.h"
0005 
0006 #include <MauiKit3/FileBrowsing/downloader.h>
0007 #include <MauiKit3/FileBrowsing/fmstatic.h>
0008 
0009 #include <QTimer>
0010 
0011 AlbumsModel::AlbumsModel(QObject *parent)
0012     : MauiList(parent)
0013 {
0014     qRegisterMetaType<FMH::MODEL_LIST>("FMH::MODEL_LIST");
0015     qRegisterMetaType<FMH::MODEL>("FMH::MODEL");
0016 }
0017 
0018 void AlbumsModel::componentComplete()
0019 {
0020     auto timer = new QTimer(this);
0021     timer->setSingleShot(true);
0022     timer->setInterval(1000);
0023 
0024     if (query == QUERY::ALBUMS) {
0025 
0026         connect(CollectionDB::getInstance(), &CollectionDB::albumInserted, [this, timer](QVariantMap) {
0027             m_newAlbums++;
0028             timer->start();
0029         });
0030 
0031         connect(timer, &QTimer::timeout, [this]()
0032         {
0033             if (m_newAlbums > 0) {
0034                 this->setList();
0035                 m_newAlbums = 0;
0036             }
0037         });
0038 
0039     } else {
0040 
0041         connect(CollectionDB::getInstance(), &CollectionDB::artistInserted, [this, timer](QVariantMap) {
0042             m_newAlbums++;
0043             timer->start();
0044         });
0045 
0046         connect(timer, &QTimer::timeout, [this]()
0047         {
0048             if (m_newAlbums > 0) {
0049                 this->setList();
0050                 m_newAlbums = 0;
0051             }
0052         });
0053     }
0054 
0055     connect(vvave::instance(), &vvave::sourceRemoved, this, &AlbumsModel::setList);
0056     connect(this, &AlbumsModel::queryChanged, this, &AlbumsModel::setList);
0057     setList();
0058 }
0059 
0060 const FMH::MODEL_LIST &AlbumsModel::items() const
0061 {
0062     return this->list;
0063 }
0064 
0065 void AlbumsModel::setQuery(const QUERY &query)
0066 {
0067     if (this->query == query)
0068         return;
0069 
0070     this->query = query;
0071     Q_EMIT this->queryChanged();
0072 }
0073 
0074 AlbumsModel::QUERY AlbumsModel::getQuery() const
0075 {
0076     return this->query;
0077 }
0078 
0079 void AlbumsModel::setList()
0080 {
0081     Q_EMIT this->preListChanged();
0082 
0083     QString m_Query;
0084     if (this->query == AlbumsModel::QUERY::ALBUMS)
0085         m_Query = "select * from albums order by album asc";
0086     else if (this->query == AlbumsModel::QUERY::ARTISTS)
0087         m_Query = "select * from artists order by artist asc";
0088     else
0089         return;
0090 
0091     this->list = CollectionDB::getInstance()->getDBData(m_Query);
0092 
0093     Q_EMIT this->postListChanged();
0094     Q_EMIT this->countChanged();
0095 }
0096 
0097 void AlbumsModel::refresh()
0098 {
0099     this->setList();
0100 }
0101 
0102 int AlbumsModel::indexOfName(const QString &query)
0103 {
0104     const auto it = std::find_if(this->items().constBegin(), this->items().constEnd(), [&](const FMH::MODEL &item) -> bool {
0105         return item[this->query == AlbumsModel::QUERY::ALBUMS ? FMH::MODEL_KEY::ALBUM : FMH::MODEL_KEY::ARTIST].startsWith(query, Qt::CaseInsensitive);
0106     });
0107 
0108     if (it != this->items().constEnd())
0109         return (std::distance(this->items().constBegin(), it));
0110     else
0111         return -1;
0112 }