2018-06-28 05:02:29 +00:00
|
|
|
#include "boxartmanager.h"
|
2018-08-17 04:04:47 +00:00
|
|
|
#include "../path.h"
|
2018-06-28 05:02:29 +00:00
|
|
|
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QImageWriter>
|
|
|
|
|
|
|
|
BoxArtManager::BoxArtManager(QObject *parent) :
|
|
|
|
QObject(parent),
|
2018-09-08 00:02:52 +00:00
|
|
|
m_BoxArtDir(Path::getBoxArtCacheDir()),
|
|
|
|
m_ThreadPool(this)
|
2018-06-28 05:02:29 +00:00
|
|
|
{
|
2018-09-08 00:30:43 +00:00
|
|
|
// 4 is a good balance between fast loading for large
|
|
|
|
// app grids and not crushing GFE with tons of requests
|
|
|
|
// and causing UI jank from constantly stalling to decode
|
|
|
|
// new images.
|
|
|
|
m_ThreadPool.setMaxThreadCount(4);
|
2018-06-28 05:02:29 +00:00
|
|
|
if (!m_BoxArtDir.exists()) {
|
|
|
|
m_BoxArtDir.mkpath(".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
BoxArtManager::getFilePathForBoxArt(NvComputer* computer, int appId)
|
|
|
|
{
|
|
|
|
QDir dir = m_BoxArtDir;
|
|
|
|
|
|
|
|
// Create the cache directory if it did not already exist
|
|
|
|
if (!dir.exists(computer->uuid)) {
|
|
|
|
dir.mkdir(computer->uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to this computer's box art cache folder
|
|
|
|
dir.cd(computer->uuid);
|
|
|
|
|
|
|
|
// Try to open the cached file
|
|
|
|
return dir.filePath(QString::number(appId) + ".png");
|
|
|
|
}
|
|
|
|
|
2018-09-29 09:19:44 +00:00
|
|
|
class NetworkBoxArtLoadTask : public QObject, public QRunnable
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
NetworkBoxArtLoadTask(BoxArtManager* boxArtManager, NvComputer* computer, NvApp& app)
|
|
|
|
: m_Bam(boxArtManager),
|
|
|
|
m_Computer(computer),
|
|
|
|
m_App(app)
|
|
|
|
{
|
|
|
|
connect(this, SIGNAL(boxArtFetchCompleted(NvComputer*,NvApp,QUrl)),
|
|
|
|
boxArtManager, SLOT(handleBoxArtLoadComplete(NvComputer*,NvApp,QUrl)));
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void boxArtFetchCompleted(NvComputer* computer, NvApp app, QUrl image);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
QUrl image = m_Bam->loadBoxArtFromNetwork(m_Computer, m_App.id);
|
|
|
|
if (image.isEmpty()) {
|
|
|
|
// Give it another shot if it fails once
|
|
|
|
image = m_Bam->loadBoxArtFromNetwork(m_Computer, m_App.id);
|
|
|
|
}
|
|
|
|
emit boxArtFetchCompleted(m_Computer, m_App, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
BoxArtManager* m_Bam;
|
|
|
|
NvComputer* m_Computer;
|
|
|
|
NvApp m_App;
|
|
|
|
};
|
|
|
|
|
2018-07-06 03:07:05 +00:00
|
|
|
QUrl BoxArtManager::loadBoxArt(NvComputer* computer, NvApp& app)
|
2018-06-28 05:02:29 +00:00
|
|
|
{
|
|
|
|
// Try to open the cached file
|
2018-07-06 03:07:05 +00:00
|
|
|
QString cacheFilePath = getFilePathForBoxArt(computer, app.id);
|
|
|
|
if (QFile::exists(cacheFilePath)) {
|
|
|
|
return QUrl::fromLocalFile(cacheFilePath);
|
2018-06-28 05:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we need to fetch asynchronously.
|
|
|
|
// Kick off a worker on our thread pool to do just that.
|
|
|
|
NetworkBoxArtLoadTask* netLoadTask = new NetworkBoxArtLoadTask(this, computer, app);
|
2018-09-08 00:02:52 +00:00
|
|
|
m_ThreadPool.start(netLoadTask);
|
2018-06-28 05:02:29 +00:00
|
|
|
|
|
|
|
// Return the placeholder then we can notify the caller
|
|
|
|
// later when the real image is ready.
|
2018-07-06 03:07:05 +00:00
|
|
|
return QUrl("qrc:/res/no_app_image.png");
|
2018-06-28 05:02:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 03:07:05 +00:00
|
|
|
void BoxArtManager::handleBoxArtLoadComplete(NvComputer* computer, NvApp app, QUrl image)
|
2018-06-28 05:02:29 +00:00
|
|
|
{
|
2018-07-06 03:07:05 +00:00
|
|
|
if (image.isEmpty()) {
|
|
|
|
image = QUrl("qrc:/res/no_app_image.png");
|
|
|
|
}
|
2018-06-28 05:02:29 +00:00
|
|
|
emit boxArtLoadComplete(computer, app, image);
|
|
|
|
}
|
|
|
|
|
2018-07-06 03:07:05 +00:00
|
|
|
QUrl BoxArtManager::loadBoxArtFromNetwork(NvComputer* computer, int appId)
|
2018-06-28 05:02:29 +00:00
|
|
|
{
|
|
|
|
NvHTTP http(computer->activeAddress);
|
|
|
|
|
2018-07-06 03:07:05 +00:00
|
|
|
QString cachePath = getFilePathForBoxArt(computer, appId);
|
2018-06-28 05:02:29 +00:00
|
|
|
QImage image;
|
|
|
|
try {
|
|
|
|
image = http.getBoxArt(appId);
|
|
|
|
} catch (...) {}
|
|
|
|
|
|
|
|
// Cache the box art on disk if it loaded
|
|
|
|
if (!image.isNull()) {
|
2018-07-06 03:07:05 +00:00
|
|
|
if (image.save(cachePath)) {
|
|
|
|
return QUrl::fromLocalFile(cachePath);
|
|
|
|
}
|
2018-06-28 05:02:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 03:07:05 +00:00
|
|
|
return QUrl();
|
2018-06-28 05:02:29 +00:00
|
|
|
}
|
2018-09-29 09:19:44 +00:00
|
|
|
|
|
|
|
#include "boxartmanager.moc"
|