moonlight-qt/app/backend/boxartmanager.cpp
2018-09-07 17:30:43 -07:00

83 lines
2.3 KiB
C++

#include "boxartmanager.h"
#include "../path.h"
#include <QImageReader>
#include <QImageWriter>
BoxArtManager::BoxArtManager(QObject *parent) :
QObject(parent),
m_BoxArtDir(Path::getBoxArtCacheDir()),
m_ThreadPool(this)
{
// 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);
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");
}
QUrl BoxArtManager::loadBoxArt(NvComputer* computer, NvApp& app)
{
// Try to open the cached file
QString cacheFilePath = getFilePathForBoxArt(computer, app.id);
if (QFile::exists(cacheFilePath)) {
return QUrl::fromLocalFile(cacheFilePath);
}
// 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);
m_ThreadPool.start(netLoadTask);
// Return the placeholder then we can notify the caller
// later when the real image is ready.
return QUrl("qrc:/res/no_app_image.png");
}
void BoxArtManager::handleBoxArtLoadComplete(NvComputer* computer, NvApp app, QUrl image)
{
if (image.isEmpty()) {
image = QUrl("qrc:/res/no_app_image.png");
}
emit boxArtLoadComplete(computer, app, image);
}
QUrl BoxArtManager::loadBoxArtFromNetwork(NvComputer* computer, int appId)
{
NvHTTP http(computer->activeAddress);
QString cachePath = getFilePathForBoxArt(computer, appId);
QImage image;
try {
image = http.getBoxArt(appId);
} catch (...) {}
// Cache the box art on disk if it loaded
if (!image.isNull()) {
if (image.save(cachePath)) {
return QUrl::fromLocalFile(cachePath);
}
}
return QUrl();
}