2018-06-28 05:02:29 +00:00
|
|
|
#include "boxartmanager.h"
|
|
|
|
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QImageWriter>
|
|
|
|
|
|
|
|
BoxArtManager::BoxArtManager(QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
m_BoxArtDir(
|
2018-07-06 03:07:05 +00:00
|
|
|
QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/boxart")
|
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-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);
|
|
|
|
QThreadPool::globalInstance()->start(netLoadTask);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|