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)
|
|
|
|
{
|
2021-03-03 00:14:15 +00:00
|
|
|
connect(this, &NetworkBoxArtLoadTask::boxArtFetchCompleted,
|
|
|
|
boxArtManager, &BoxArtManager::handleBoxArtLoadComplete);
|
2018-09-29 09:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-02-21 03:06:19 +00:00
|
|
|
// Try to open the cached file if it exists and contains data
|
|
|
|
QFile cacheFile(getFilePathForBoxArt(computer, app.id));
|
|
|
|
if (cacheFile.exists() && cacheFile.size() > 0) {
|
|
|
|
return QUrl::fromLocalFile(cacheFile.fileName());
|
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
|
|
|
}
|
|
|
|
|
2020-05-30 04:44:38 +00:00
|
|
|
void BoxArtManager::deleteBoxArt(NvComputer* computer)
|
|
|
|
{
|
|
|
|
QDir dir(Path::getBoxArtCacheDir());
|
|
|
|
|
|
|
|
// Delete everything in this computer's box art directory
|
|
|
|
if (dir.cd(computer->uuid)) {
|
|
|
|
dir.removeRecursively();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-04-28 01:48:06 +00:00
|
|
|
if (!image.isEmpty()) {
|
|
|
|
emit boxArtLoadComplete(computer, app, image);
|
2018-07-06 03:07:05 +00:00
|
|
|
}
|
2018-06-28 05:02:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 03:07:05 +00:00
|
|
|
QUrl BoxArtManager::loadBoxArtFromNetwork(NvComputer* computer, int appId)
|
2018-06-28 05:02:29 +00:00
|
|
|
{
|
2021-07-02 22:14:48 +00:00
|
|
|
NvHTTP http(computer);
|
2018-06-28 05:02:29 +00:00
|
|
|
|
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);
|
|
|
|
}
|
2020-02-21 03:06:19 +00:00
|
|
|
else {
|
|
|
|
// A failed save() may leave a zero byte file. Make sure that's removed.
|
|
|
|
QFile(cachePath).remove();
|
|
|
|
}
|
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"
|