#include "boxartmanager.h" #include #include #include BoxArtManager::BoxArtManager(QObject *parent) : QObject(parent), m_BoxArtDir( QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/boxart") { 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); QThreadPool::globalInstance()->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(); }