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

View file

@ -61,7 +61,8 @@ SOURCES += \
http/nvpairingmanager.cpp \
streaming/video.c \
streaming/connection.cpp \
http/computermanager.cpp
http/computermanager.cpp \
http/boxartmanager.cpp
HEADERS += \
utils.h \
@ -71,7 +72,8 @@ HEADERS += \
http/nvhttp.h \
http/nvpairingmanager.h \
streaming/streaming.h \
http/computermanager.h
http/computermanager.h \
http/boxartmanager.h
FORMS += \
gui/mainwindow.ui

View file

@ -1,6 +1,9 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "http/computermanager.h"
#include "http/boxartmanager.h"
#include <QMainWindow>
#include <QtWidgets>
@ -21,10 +24,13 @@ private slots:
void on_newHostBtn_clicked();
void addHostToDisplay(QMap<QString, bool>);
void on_selectHostComboBox_activated(const QString &);
void computerStateChanged(NvComputer* computer);
void boxArtLoadComplete(NvComputer* computer, NvApp app, QImage image);
private:
Ui::MainWindow *ui;
BoxArtManager m_BoxArtManager;
ComputerManager m_ComputerManager;
};
#endif // MAINWINDOW_H

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
app/http/boxartmanager.h Normal file
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;
};

View file

@ -25,6 +25,8 @@ public:
bool hdrSupported;
};
Q_DECLARE_METATYPE(NvApp)
class GfeHttpResponseException : public std::exception
{
public:

View file

@ -1,6 +1,8 @@
#include "gui/mainwindow.h"
#include <QApplication>
#include "http/nvhttp.h"
// Don't let SDL hook our main function, since Qt is already
// doing the same thing
#define SDL_MAIN_HANDLED
@ -8,10 +10,6 @@
int main(int argc, char *argv[])
{
// MacOS directive to prevent the menu bar from being merged into the native bar
// i.e. it's in the window, and not the top left of the screen
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);
// This avoids using the default keychain for SSL, which may cause
// password prompts on macOS.
qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1"));
@ -21,6 +19,9 @@ int main(int argc, char *argv[])
QCoreApplication::setOrganizationDomain("moonlight-stream.com");
QCoreApplication::setApplicationName("Moonlight");
// Register custom metatypes for use in signals
qRegisterMetaType<NvApp>("NvApp");
QApplication a(argc, argv);
MainWindow w;
w.show();