Commit ac74ec62 authored by camilo higuita's avatar camilo higuita

move artworks retrievement to albums model so it can be seen as it gets the...

move artworks retrievement to albums model so it can be seen as it gets the artwork and also fixed issues with thread still runnning on app exit
parent 3c562d17
...@@ -695,7 +695,7 @@ Maui.ApplicationWindow ...@@ -695,7 +695,7 @@ Maui.ApplicationWindow
holder.body: "Add new music sources" holder.body: "Add new music sources"
holder.emojiSize: iconSizes.huge holder.emojiSize: iconSizes.huge
headBarTitle: count + qsTr(" albums") headBarTitle: count + qsTr(" albums")
list.query: Q.GET.allAlbumsAsc list.query: Albums.ALBUMS
list.sortBy: Albums.ALBUM list.sortBy: Albums.ALBUM
Connections Connections
...@@ -749,7 +749,7 @@ Maui.ApplicationWindow ...@@ -749,7 +749,7 @@ Maui.ApplicationWindow
holder.body: qsTr("Add new music sources") holder.body: qsTr("Add new music sources")
holder.emojiSize: iconSizes.huge holder.emojiSize: iconSizes.huge
headBarTitle: count + qsTr(" artists") headBarTitle: count + qsTr(" artists")
list.query: Q.GET.allArtistsAsc list.query: Albums.ARTISTS
list.sortBy: Albums.ARTIST list.sortBy: Albums.ARTIST
table.list.sortBy: Tracks.NONE table.list.sortBy: Tracks.NONE
......
#include "albumsmodel.h" #include "albumsmodel.h"
#include "db/collectionDB.h" #include "db/collectionDB.h"
#include "utils/brain.h"
#include <QtConcurrent>
AlbumsModel::AlbumsModel(QObject *parent) : BaseList(parent) AlbumsModel::AlbumsModel(QObject *parent) : BaseList(parent)
{ {
this->db = CollectionDB::getInstance(); this->db = CollectionDB::getInstance();
connect(this, &AlbumsModel::queryChanged, this, &AlbumsModel::setList); connect(this, &AlbumsModel::queryChanged, this, &AlbumsModel::setList);
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
{
pool.waitForDone();
});
} }
FMH::MODEL_LIST AlbumsModel::items() const FMH::MODEL_LIST AlbumsModel::items() const
...@@ -13,7 +18,7 @@ FMH::MODEL_LIST AlbumsModel::items() const ...@@ -13,7 +18,7 @@ FMH::MODEL_LIST AlbumsModel::items() const
return this->list; return this->list;
} }
void AlbumsModel::setQuery(const QString &query) void AlbumsModel::setQuery(const QUERY &query)
{ {
if(this->query == query) if(this->query == query)
return; return;
...@@ -24,7 +29,7 @@ void AlbumsModel::setQuery(const QString &query) ...@@ -24,7 +29,7 @@ void AlbumsModel::setQuery(const QString &query)
emit this->queryChanged(); emit this->queryChanged();
} }
QString AlbumsModel::getQuery() const AlbumsModel::QUERY AlbumsModel::getQuery() const
{ {
return this->query; return this->query;
} }
...@@ -101,11 +106,112 @@ void AlbumsModel::setList() ...@@ -101,11 +106,112 @@ void AlbumsModel::setList()
{ {
emit this->preListChanged(); emit this->preListChanged();
this->list = this->db->getDBData(this->query); QString m_Query;
if(this->query == AlbumsModel::QUERY::ALBUMS)
m_Query = "select * from albums order by album asc";
else if(this->query == AlbumsModel::QUERY::ARTISTS)
m_Query = "select * from artists order by artist asc";
this->list = this->db->getDBData(m_Query);
qDebug()<< "my LIST" ; qDebug()<< "my LIST" ;
this->sortList(); this->sortList();
emit this->postListChanged(); emit this->postListChanged();
if(this->query == AlbumsModel::QUERY::ALBUMS)
this->runBrain();
}
void AlbumsModel::runBrain()
{
QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
QObject::connect(watcher, &QFutureWatcher<void>::finished, [=]()
{
watcher->deleteLater();
});
auto func = [=]()
{
QList<PULPO::REQUEST> requests;
int index = -1;
for(auto &album : this->list)
{
index++;
if(!album[FMH::MODEL_KEY::ARTWORK].isEmpty())
continue;
if(BAE::artworkCache(album, FMH::MODEL_KEY::ALBUM))
{
db->insertArtwork(album);
this->updateArtwork(index, album[FMH::MODEL_KEY::ARTWORK]);
continue;
}
PULPO::REQUEST request;
request.track = album;
request.ontology = PULPO::ONTOLOGY::ALBUM;
request.services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz};
request.info = {PULPO::INFO::ARTWORK};
request.callback = [=](PULPO::REQUEST request, PULPO::RESPONSES responses)
{
qDebug() << "DONE WITH " << request.track ;
for(const auto &res : responses)
{
if(res.context == PULPO::CONTEXT::IMAGE && !res.value.toString().isEmpty())
{
qDebug()<<"SAVING ARTWORK FOR: " << request.track[FMH::MODEL_KEY::ALBUM];
auto downloader = new FMH::Downloader;
QObject::connect(downloader, &FMH::Downloader::fileSaved, [=](QString path)
{
qDebug()<< "Saving artwork file to" << path;
FMH::MODEL newTrack = request.track;
newTrack[FMH::MODEL_KEY::ARTWORK] = path;
db->insertArtwork(newTrack);
this->updateArtwork(index, path);
downloader->deleteLater();
});
QStringList filePathList = res.value.toString().split('/');
const auto format = "." + filePathList.at(filePathList.count() - 1).split(".").last();
QString name = !request.track[FMH::MODEL_KEY::ALBUM].isEmpty() ? request.track[FMH::MODEL_KEY::ARTIST] + "_" + request.track[FMH::MODEL_KEY::ALBUM] : request.track[FMH::MODEL_KEY::ARTIST];
name.replace("/", "-");
name.replace("&", "-");
downloader->setFile(res.value.toString(), BAE::CachePath + name + format);
}
}
};
requests << request;
}
Pulpo pulpo;
QEventLoop loop;
QObject::connect(&pulpo, &Pulpo::finished, &loop, &QEventLoop::quit);
for(auto i = 0; i < requests.size(); i++)
{
pulpo.request(requests.at(i));
loop.exec();
}
};
QFuture<void> t1 = QtConcurrent::run(&pool, func);
watcher->setFuture(t1);
}
void AlbumsModel::updateArtwork(const int index, const QString &artwork)
{
if(index >= this->list.size() || index < 0)
return;
this->list[index][FMH::MODEL_KEY::ARTWORK] = artwork;
emit this->updateModel(index, {FMH::MODEL_KEY::ARTWORK});
} }
QVariantMap AlbumsModel::get(const int &index) const QVariantMap AlbumsModel::get(const int &index) const
......
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
#include <QObject> #include <QObject>
#include "models/baselist.h" #include "models/baselist.h"
#include <QThreadPool>
class CollectionDB; class CollectionDB;
class AlbumsModel : public BaseList class AlbumsModel : public BaseList
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString query READ getQuery WRITE setQuery NOTIFY queryChanged()) Q_PROPERTY(AlbumsModel::QUERY query READ getQuery WRITE setQuery NOTIFY queryChanged())
Q_PROPERTY(AlbumsModel::SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged) Q_PROPERTY(AlbumsModel::SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
public: public:
...@@ -16,17 +17,24 @@ public: ...@@ -16,17 +17,24 @@ public:
enum SORTBY : uint_fast8_t enum SORTBY : uint_fast8_t
{ {
ADDDATE = FMH::MODEL_KEY::ADDDATE, ADDDATE = FMH::MODEL_KEY::ADDDATE,
RELEASEDATE = FMH::MODEL_KEY::RELEASEDATE, RELEASEDATE = FMH::MODEL_KEY::RELEASEDATE,
ARTIST = FMH::MODEL_KEY::ARTIST, ARTIST = FMH::MODEL_KEY::ARTIST,
ALBUM = FMH::MODEL_KEY::ALBUM ALBUM = FMH::MODEL_KEY::ALBUM
}; Q_ENUM(SORTBY) };
Q_ENUM(SORTBY)
enum QUERY : uint_fast8_t
{
ARTISTS, ALBUMS
};
Q_ENUM(QUERY)
explicit AlbumsModel(QObject *parent = nullptr); explicit AlbumsModel(QObject *parent = nullptr);
FMH::MODEL_LIST items() const override; FMH::MODEL_LIST items() const override;
void setQuery(const QString &query); void setQuery(const AlbumsModel::QUERY &query);
QString getQuery() const; AlbumsModel::QUERY getQuery() const;
void setSortBy(const AlbumsModel::SORTBY &sort); void setSortBy(const AlbumsModel::SORTBY &sort);
AlbumsModel::SORTBY getSortBy() const; AlbumsModel::SORTBY getSortBy() const;
...@@ -37,9 +45,13 @@ private: ...@@ -37,9 +45,13 @@ private:
void sortList(); void sortList();
void setList(); void setList();
QString query; AlbumsModel::QUERY query;
AlbumsModel::SORTBY sort = AlbumsModel::SORTBY::ADDDATE; AlbumsModel::SORTBY sort = AlbumsModel::SORTBY::ADDDATE;
void runBrain();
void updateArtwork(const int index, const QString &artwork);
QThreadPool pool;
signals: signals:
void queryChanged(); void queryChanged();
void sortByChanged(); void sortByChanged();
......
...@@ -34,6 +34,11 @@ public: ...@@ -34,6 +34,11 @@ public:
return res; return res;
} }
int currentIndex()
{
return index;
}
bool hasNext() const bool hasNext() const
{ {
return index + 1 < requests.size(); return index + 1 < requests.size();
...@@ -197,7 +202,7 @@ struct PACKAGE ...@@ -197,7 +202,7 @@ struct PACKAGE
{ {
PULPO::ONTOLOGY ontology; PULPO::ONTOLOGY ontology;
PULPO::INFO info; PULPO::INFO info;
std::function<void()> callback = nullptr; std::function<void(int index)> callback = nullptr;
}; };
typedef QList<PACKAGE> PACKAGES; typedef QList<PACKAGE> PACKAGES;
...@@ -210,16 +215,15 @@ inline void synapse(const BRAIN::PACKAGES &packages) ...@@ -210,16 +215,15 @@ inline void synapse(const BRAIN::PACKAGES &packages)
QEventLoop loop; QEventLoop loop;
QObject::connect(&pulpo, &Pulpo::finished, &loop, &QEventLoop::quit); QObject::connect(&pulpo, &Pulpo::finished, &loop, &QEventLoop::quit);
auto func = [&](QUEUE &m_requests, std::function<void()> cb = nullptr) auto func = [&](QUEUE &m_requests, std::function<void(int index)> cb = nullptr)
{ {
while(m_requests.hasNext()) while(m_requests.hasNext())
{ {
pulpo.request(m_requests.next()); pulpo.request(m_requests.next());
if(cb)
cb(m_requests.currentIndex());
loop.exec(); loop.exec();
} }
if(cb)
cb();
}; };
for(const auto &package : packages) for(const auto &package : packages)
...@@ -315,8 +319,6 @@ void albumInfo(); ...@@ -315,8 +319,6 @@ void albumInfo();
void tags(); void tags();
void wikis(); void wikis();
static QUEUE albumArtworks();
void albumTags(); void albumTags();
void albumWikis(); void albumWikis();
......
...@@ -2,7 +2,7 @@ import QtQuick 2.9 ...@@ -2,7 +2,7 @@ import QtQuick 2.9
import QtGraphicalEffects 1.0 import QtGraphicalEffects 1.0
import QtQuick.Controls 2.2 import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3 import QtQuick.Layouts 1.3
import AlbumsList 1.0
ItemDelegate ItemDelegate
{ {
...@@ -176,7 +176,7 @@ ItemDelegate ...@@ -176,7 +176,7 @@ ItemDelegate
{ {
width: parent.width * 0.8 width: parent.width * 0.8
anchors.centerIn: parent anchors.centerIn: parent
text: typeof album === 'undefined' ? artist : album text: list.query === Albums.ALBUMS ? model.album : model.artist
visible: true visible: true
horizontalAlignment: Qt.AlignHCenter horizontalAlignment: Qt.AlignHCenter
elide: Text.ElideRight elide: Text.ElideRight
...@@ -197,8 +197,8 @@ ItemDelegate ...@@ -197,8 +197,8 @@ ItemDelegate
width: parent.width*0.8 width: parent.width*0.8
anchors.centerIn: parent anchors.centerIn: parent
text: typeof album === 'undefined' ? "" : artist text: list.query === Albums.ALBUMS ? model.artist : undefined
visible: typeof album === 'undefined'? false : true visible: text
horizontalAlignment: Qt.AlignHCenter horizontalAlignment: Qt.AlignHCenter
elide: Text.ElideRight elide: Text.ElideRight
font.pointSize: fontSizes.medium font.pointSize: fontSizes.medium
......
...@@ -61,38 +61,42 @@ void vvave::runBrain() ...@@ -61,38 +61,42 @@ void vvave::runBrain()
{ {
QFutureWatcher<void> *watcher = new QFutureWatcher<void>; QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
QObject::connect(watcher, &QFutureWatcher<void>::finished, watcher, &QFutureWatcher<void>::deleteLater); QObject::connect(watcher, &QFutureWatcher<void>::finished, [=]()
// QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
// {
// if(watcher != nullptr)
// watcher->future().waitForFinished();
// });
auto func = [=]()
{ {
// the album artworks package watcher->deleteLater();
BRAIN::PACKAGE albumPackage; emit this->refreshAlbums();
albumPackage.ontology = PULPO::ONTOLOGY::ALBUM; });
albumPackage.info = PULPO::INFO::ARTWORK;
albumPackage.callback = [=]()
{
emit this->refreshAlbums();
};
BRAIN::PACKAGE artistPackage;
artistPackage.ontology = PULPO::ONTOLOGY::ARTIST;
artistPackage.info = PULPO::INFO::ARTWORK;
artistPackage.callback = [=]()
{
emit this->refreshArtists();
};
BRAIN::synapse(BRAIN::PACKAGES() << albumPackage /*<< artistPackage*/); // QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
}; // {
// if(watcher != nullptr)
// watcher->future().waitForFinished();
// });
QFuture<void> t1 = QtConcurrent::run(func); // auto func = [=]()
watcher->setFuture(t1); // {
// the album artworks package
// BRAIN::PACKAGE albumPackage;
// albumPackage.ontology = PULPO::ONTOLOGY::ALBUM;
// albumPackage.info = PULPO::INFO::ARTWORK;
// albumPackage.callback = [=]()
// {
// emit this->refreshAlbums();
// };
// BRAIN::PACKAGE artistPackage;
// artistPackage.ontology = PULPO::ONTOLOGY::ARTIST;
// artistPackage.info = PULPO::INFO::ARTWORK;
// artistPackage.callback = [=]()
// {
// emit this->refreshArtists();
// };
// BRAIN::synapse(BRAIN::PACKAGES() << albumPackage /*<< artistPackage*/);
// };
// QFuture<void> t1 = QtConcurrent::run(func);
// watcher->setFuture(t1);
} }
...@@ -108,14 +112,14 @@ void vvave::checkCollection(const QStringList &paths, std::function<void(uint)> ...@@ -108,14 +112,14 @@ void vvave::checkCollection(const QStringList &paths, std::function<void(uint)>
}); });
const auto func = [=]() -> uint const auto func = [=]() -> uint
{ {
auto newPaths = paths; auto newPaths = paths;
for(auto path : newPaths) for(auto path : newPaths)
if(path.startsWith("file://")) if(path.startsWith("file://"))
path.replace("file://", ""); path.replace("file://", "");
return FLoader::getTracks(newPaths); return FLoader::getTracks(newPaths);
}; };
QFuture<uint> t1 = QtConcurrent::run(func); QFuture<uint> t1 = QtConcurrent::run(func);
watcher->setFuture(t1); watcher->setFuture(t1);
...@@ -146,7 +150,7 @@ void vvave::scanDir(const QStringList &paths) ...@@ -146,7 +150,7 @@ void vvave::scanDir(const QStringList &paths)
this->checkCollection(paths, [=](uint size) this->checkCollection(paths, [=](uint size)
{ {
emit this->refreshTables(size); emit this->refreshTables(size);
this->runBrain(); // this->runBrain();
}); });
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment