Add BoxArtManager for loading box art with caching

This commit is contained in:
Cameron Gutman
2018-06-27 22:02:29 -07:00
parent 135568b5ee
commit d7d11635a0
6 changed files with 168 additions and 7 deletions
+77
View File
@@ -0,0 +1,77 @@
#include "boxartmanager.h"
#include <QStandardPaths>
#include <QImageReader>
#include <QImageWriter>
BoxArtManager::BoxArtManager(QObject *parent) :
QObject(parent),
m_BoxArtDir(
QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/boxart"),
m_PlaceholderImage(":/res/no_app_image.png")
{
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");
}
QImage BoxArtManager::loadBoxArt(NvComputer* computer, NvApp& app)
{
// Try to open the cached file
QFile cacheFile(getFilePathForBoxArt(computer, app.id));
if (cacheFile.open(QFile::ReadOnly)) {
// Return what we have if it's a valid image
QImage image = QImageReader(&cacheFile).read();
if (!image.isNull()) {
return image;
}
}
// 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);
QThreadPool::globalInstance()->start(netLoadTask);
// Return the placeholder then we can notify the caller
// later when the real image is ready.
return m_PlaceholderImage;
}
void BoxArtManager::handleBoxArtLoadComplete(NvComputer* computer, NvApp app, QImage image)
{
emit boxArtLoadComplete(computer, app, image);
}
QImage BoxArtManager::loadBoxArtFromNetwork(NvComputer* computer, int appId)
{
NvHTTP http(computer->activeAddress);
QImage image;
try {
image = http.getBoxArt(appId);
} catch (...) {}
// Cache the box art on disk if it loaded
if (!image.isNull()) {
image.save(getFilePathForBoxArt(computer, appId));
}
return image;
}
+73
View File
@@ -0,0 +1,73 @@
#pragma once
#include "computermanager.h"
#include <QDir>
#include <QImage>
#include <QThreadPool>
#include <QRunnable>
class BoxArtManager : public QObject
{
Q_OBJECT
friend class NetworkBoxArtLoadTask;
public:
explicit BoxArtManager(QObject *parent = nullptr);
QImage
loadBoxArt(NvComputer* computer, NvApp& app);
signals:
void
boxArtLoadComplete(NvComputer* computer, NvApp app, QImage image);
public slots:
private slots:
void
handleBoxArtLoadComplete(NvComputer* computer, NvApp app, QImage image);
private:
QImage
loadBoxArtFromNetwork(NvComputer* computer, int appId);
QString
getFilePathForBoxArt(NvComputer* computer, int appId);
QDir m_BoxArtDir;
QImage m_PlaceholderImage;
};
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,QImage)),
boxArtManager, SLOT(handleBoxArtLoadComplete(NvComputer*,NvApp,QImage)));
}
signals:
void boxArtFetchCompleted(NvComputer* computer, NvApp app, QImage image);
private:
void run()
{
QImage image = m_Bam->loadBoxArtFromNetwork(m_Computer, m_App.id);
if (image.isNull()) {
// 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;
};
+2
View File
@@ -25,6 +25,8 @@ public:
bool hdrSupported;
};
Q_DECLARE_METATYPE(NvApp)
class GfeHttpResponseException : public std::exception
{
public: