From a5d1099e266be6959a01216a02e5b127424fea9b Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 27 Jun 2018 00:43:46 -0700 Subject: [PATCH] Add code to serialize and deserialize hosts and applists for persistence --- app/http/computermanager.cpp | 91 ++++++++++++++++++++++++++++++++++++ app/http/computermanager.h | 8 ++++ app/main.cpp | 5 ++ 3 files changed, 104 insertions(+) diff --git a/app/http/computermanager.cpp b/app/http/computermanager.cpp index 52c2759a..bd14653f 100644 --- a/app/http/computermanager.cpp +++ b/app/http/computermanager.cpp @@ -3,6 +3,75 @@ #include +#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) { this->name = NvHTTP::getXmlString(serverInfo, "hostname"); @@ -77,7 +146,29 @@ bool NvComputer::update(NvComputer& that) ComputerManager::ComputerManager() : 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() diff --git a/app/http/computermanager.h b/app/http/computermanager.h index ad2d90a4..96e57ec1 100644 --- a/app/http/computermanager.h +++ b/app/http/computermanager.h @@ -3,15 +3,21 @@ #include #include +#include class NvComputer { public: NvComputer(QString address, QString serverInfo); + NvComputer(QSettings& settings); + bool update(NvComputer& that); + void + serialize(QSettings& settings); + enum PairState { PS_UNKNOWN, @@ -225,6 +231,8 @@ private slots: void handlePollThreadTermination(NvComputer* computer); private: + void saveHosts(); + void startPollingComputer(NvComputer* computer); bool m_Polling; diff --git a/app/main.cpp b/app/main.cpp index 13d648a8..b54e4f51 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -16,6 +16,11 @@ int main(int argc, char *argv[]) // password prompts on macOS. 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); MainWindow w; w.show();