Add code to serialize and deserialize hosts and applists for persistence

This commit is contained in:
Cameron Gutman 2018-06-27 00:43:46 -07:00
parent d6b820d707
commit a5d1099e26
3 changed files with 104 additions and 0 deletions

View file

@ -3,6 +3,75 @@
#include <QThread> #include <QThread>
#define SER_HOSTS "hosts"
#define SER_NAME "hostname"
#define SER_UUID "uuid"
#define SER_MAC "mac"
#define SER_CODECSUPP "codecsupport"
#define SER_LOCALADDR "localaddress"
#define SER_REMOTEADDR "remoteaddress"
#define SER_MANUALADDR "manualaddress"
#define SER_APPLIST "apps"
#define SER_APPNAME "name"
#define SER_APPID "id"
#define SER_APPHDR "hdr"
NvComputer::NvComputer(QSettings& settings)
{
this->name = settings.value(SER_NAME).toString();
this->uuid = settings.value(SER_UUID).toString();
this->macAddress = settings.value(SER_MAC).toByteArray();
this->serverCodecModeSupport = settings.value(SER_CODECSUPP).toInt();
this->localAddress = settings.value(SER_LOCALADDR).toString();
this->remoteAddress = settings.value(SER_REMOTEADDR).toString();
this->manualAddress = settings.value(SER_MANUALADDR).toString();
int appCount = settings.beginReadArray(SER_APPLIST);
for (int i = 0; i < appCount; i++) {
NvApp app;
settings.setArrayIndex(i);
app.name = settings.value(SER_APPNAME).toString();
app.id = settings.value(SER_APPID).toInt();
app.hdrSupported = settings.value(SER_APPHDR).toBool();
this->appList.append(app);
}
settings.endArray();
this->activeAddress = nullptr;
this->currentGameId = 0;
this->pairState = PS_UNKNOWN;
this->state = CS_UNKNOWN;
}
void
NvComputer::serialize(QSettings& settings)
{
QReadLocker lock(&this->lock);
settings.setValue(SER_NAME, name);
settings.setValue(SER_UUID, uuid);
settings.setValue(SER_MAC, macAddress);
settings.setValue(SER_CODECSUPP, serverCodecModeSupport);
settings.setValue(SER_LOCALADDR, localAddress);
settings.setValue(SER_REMOTEADDR, remoteAddress);
settings.setValue(SER_MANUALADDR, manualAddress);
settings.remove(SER_APPLIST);
settings.beginWriteArray(SER_APPLIST);
for (int i = 0; i < appList.count(); i++) {
settings.setArrayIndex(i);
settings.setValue(SER_APPNAME, appList[i].name);
settings.setValue(SER_APPID, appList[i].id);
settings.setValue(SER_APPHDR, appList[i].hdrSupported);
}
settings.endArray();
}
NvComputer::NvComputer(QString address, QString serverInfo) NvComputer::NvComputer(QString address, QString serverInfo)
{ {
this->name = NvHTTP::getXmlString(serverInfo, "hostname"); this->name = NvHTTP::getXmlString(serverInfo, "hostname");
@ -77,7 +146,29 @@ bool NvComputer::update(NvComputer& that)
ComputerManager::ComputerManager() ComputerManager::ComputerManager()
: m_Polling(false) : m_Polling(false)
{ {
QSettings settings;
// Inflate our hosts from QSettings
int hosts = settings.beginReadArray(SER_HOSTS);
for (int i = 0; i < hosts; i++) {
settings.setArrayIndex(i);
NvComputer* computer = new NvComputer(settings);
m_KnownHosts[computer->uuid] = computer;
}
settings.endArray();
}
void ComputerManager::saveHosts()
{
QSettings settings;
settings.remove(SER_HOSTS);
settings.beginWriteArray(SER_HOSTS);
for (int i = 0; i < m_KnownHosts.count(); i++) {
settings.setArrayIndex(i);
m_KnownHosts[m_KnownHosts.keys()[i]]->serialize(settings);
}
settings.endArray();
} }
void ComputerManager::startPolling() void ComputerManager::startPolling()

View file

@ -3,15 +3,21 @@
#include <QThread> #include <QThread>
#include <QReadWriteLock> #include <QReadWriteLock>
#include <QSettings>
class NvComputer class NvComputer
{ {
public: public:
NvComputer(QString address, QString serverInfo); NvComputer(QString address, QString serverInfo);
NvComputer(QSettings& settings);
bool bool
update(NvComputer& that); update(NvComputer& that);
void
serialize(QSettings& settings);
enum PairState enum PairState
{ {
PS_UNKNOWN, PS_UNKNOWN,
@ -225,6 +231,8 @@ private slots:
void handlePollThreadTermination(NvComputer* computer); void handlePollThreadTermination(NvComputer* computer);
private: private:
void saveHosts();
void startPollingComputer(NvComputer* computer); void startPollingComputer(NvComputer* computer);
bool m_Polling; bool m_Polling;

View file

@ -16,6 +16,11 @@ int main(int argc, char *argv[])
// password prompts on macOS. // password prompts on macOS.
qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1")); qputenv("QT_SSL_USE_TEMPORARY_KEYCHAIN", QByteArray("1"));
// Set these here to allow us to use the default QSettings constructor
QCoreApplication::setOrganizationName("Moonlight Game Streaming Project");
QCoreApplication::setOrganizationDomain("moonlight-stream.com");
QCoreApplication::setApplicationName("Moonlight");
QApplication a(argc, argv); QApplication a(argc, argv);
MainWindow w; MainWindow w;
w.show(); w.show();