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
holder.body: "Add new music sources"
holder.emojiSize: iconSizes.huge
headBarTitle: count + qsTr(" albums")
list.query: Q.GET.allAlbumsAsc
list.query: Albums.ALBUMS
list.sortBy: Albums.ALBUM
Connections
......@@ -749,7 +749,7 @@ Maui.ApplicationWindow
holder.body: qsTr("Add new music sources")
holder.emojiSize: iconSizes.huge
headBarTitle: count + qsTr(" artists")
list.query: Q.GET.allArtistsAsc
list.query: Albums.ARTISTS
list.sortBy: Albums.ARTIST
table.list.sortBy: Tracks.NONE
......
#include "albumsmodel.h"
#include "db/collectionDB.h"
#include "utils/brain.h"
#include <QtConcurrent>
AlbumsModel::AlbumsModel(QObject *parent) : BaseList(parent)
{
this->db = CollectionDB::getInstance();
connect(this, &AlbumsModel::queryChanged, this, &AlbumsModel::setList);
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
{
pool.waitForDone();
});
}
FMH::MODEL_LIST AlbumsModel::items() const
......@@ -13,7 +18,7 @@ FMH::MODEL_LIST AlbumsModel::items() const
return this->list;
}
void AlbumsModel::setQuery(const QString &query)
void AlbumsModel::setQuery(const QUERY &query)
{
if(this->query == query)
return;
......@@ -24,7 +29,7 @@ void AlbumsModel::setQuery(const QString &query)
emit this->queryChanged();
}
QString AlbumsModel::getQuery() const
AlbumsModel::QUERY AlbumsModel::getQuery() const
{
return this->query;
}
......@@ -101,11 +106,112 @@ void AlbumsModel::setList()
{
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" ;
this->sortList();
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
......
......@@ -3,12 +3,13 @@
#include <QObject>
#include "models/baselist.h"
#include <QThreadPool>
class CollectionDB;
class AlbumsModel : public BaseList
{
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)
public:
......@@ -16,17 +17,24 @@ public:
enum SORTBY : uint_fast8_t
{
ADDDATE = FMH::MODEL_KEY::ADDDATE,
RELEASEDATE = FMH::MODEL_KEY::RELEASEDATE,
RELEASEDATE = FMH::MODEL_KEY::RELEASEDATE,
ARTIST = FMH::MODEL_KEY::ARTIST,
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);
FMH::MODEL_LIST items() const override;
void setQuery(const QString &query);
QString getQuery() const;
void setQuery(const AlbumsModel::QUERY &query);
AlbumsModel::QUERY getQuery() const;
void setSortBy(const AlbumsModel::SORTBY &sort);
AlbumsModel::SORTBY getSortBy() const;
......@@ -37,9 +45,13 @@ private:
void sortList();
void setList();
QString query;
AlbumsModel::QUERY query;
AlbumsModel::SORTBY sort = AlbumsModel::SORTBY::ADDDATE;
void runBrain();
void updateArtwork(const int index, const QString &artwork);
QThreadPool pool;
signals:
void queryChanged();
void sortByChanged();
......
......@@ -34,6 +34,11 @@ public:
return res;
}
int currentIndex()
{
return index;
}
bool hasNext() const
{
return index + 1 < requests.size();
......@@ -197,7 +202,7 @@ struct PACKAGE
{
PULPO::ONTOLOGY ontology;
PULPO::INFO info;
std::function<void()> callback = nullptr;
std::function<void(int index)> callback = nullptr;
};
typedef QList<PACKAGE> PACKAGES;
......@@ -210,16 +215,15 @@ inline void synapse(const BRAIN::PACKAGES &packages)
QEventLoop loop;
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())
{
pulpo.request(m_requests.next());
if(cb)
cb(m_requests.currentIndex());
loop.exec();
}
if(cb)
cb();
};
for(const auto &package : packages)
......@@ -315,8 +319,6 @@ void albumInfo();
void tags();
void wikis();
static QUEUE albumArtworks();
void albumTags();
void albumWikis();
......
......@@ -2,7 +2,7 @@ import QtQuick 2.9
import QtGraphicalEffects 1.0
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import AlbumsList 1.0
ItemDelegate
{
......@@ -176,7 +176,7 @@ ItemDelegate
{
width: parent.width * 0.8
anchors.centerIn: parent
text: typeof album === 'undefined' ? artist : album
text: list.query === Albums.ALBUMS ? model.album : model.artist
visible: true
horizontalAlignment: Qt.AlignHCenter
elide: Text.ElideRight
......@@ -197,8 +197,8 @@ ItemDelegate
width: parent.width*0.8
anchors.centerIn: parent
text: typeof album === 'undefined' ? "" : artist
visible: typeof album === 'undefined'? false : true
text: list.query === Albums.ALBUMS ? model.artist : undefined
visible: text
horizontalAlignment: Qt.AlignHCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
......
......@@ -61,38 +61,42 @@ void vvave::runBrain()
{
QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
QObject::connect(watcher, &QFutureWatcher<void>::finished, watcher, &QFutureWatcher<void>::deleteLater);
// QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
// {
// if(watcher != nullptr)
// watcher->future().waitForFinished();
// });
auto func = [=]()
QObject::connect(watcher, &QFutureWatcher<void>::finished, [=]()
{
// 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();
};
watcher->deleteLater();
emit this->refreshAlbums();
});
BRAIN::synapse(BRAIN::PACKAGES() << albumPackage /*<< artistPackage*/);
};
// QObject::connect(qApp, &QCoreApplication::aboutToQuit, [=]()
// {
// if(watcher != nullptr)
// watcher->future().waitForFinished();
// });
QFuture<void> t1 = QtConcurrent::run(func);
watcher->setFuture(t1);
// auto func = [=]()
// {
// 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)>
});
const auto func = [=]() -> uint
{
auto newPaths = paths;
auto newPaths = paths;
for(auto path : newPaths)
for(auto path : newPaths)
if(path.startsWith("file://"))
path.replace("file://", "");
path.replace("file://", "");
return FLoader::getTracks(newPaths);
};
return FLoader::getTracks(newPaths);
};
QFuture<uint> t1 = QtConcurrent::run(func);
watcher->setFuture(t1);
......@@ -146,7 +150,7 @@ void vvave::scanDir(const QStringList &paths)
this->checkCollection(paths, [=](uint 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