From 2b5d990c7cf920e9b35aa375dfe699e3d70b88a3 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Sat, 4 Feb 2023 11:54:44 +0100 Subject: [PATCH] Fix save autostart changes ever time This fixes issue 163 --- ScreenPlay/inc/public/ScreenPlay/settings.h | 74 +++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/ScreenPlay/inc/public/ScreenPlay/settings.h b/ScreenPlay/inc/public/ScreenPlay/settings.h index 517803f4..7d97e04c 100644 --- a/ScreenPlay/inc/public/ScreenPlay/settings.h +++ b/ScreenPlay/inc/public/ScreenPlay/settings.h @@ -28,6 +28,9 @@ #include #include #include +#include +#include +#include #include "ScreenPlay/globalvariables.h" #include "ScreenPlay/util.h" @@ -184,6 +187,77 @@ public slots: const QString homePath = QDir::homePath(); QFile settingsPlist(homePath + "/Library/LaunchAgents/" + plistFileName); if (settingsPlist.exists()) { + QDomDocument doc; + if (!doc.setContent(&settingsPlist)){ + settingsPlist.close(); + return ; + } + settingsPlist.close(); + + QDomElement root = doc.firstChildElement(); + QDomNodeList dictList = root.elementsByTagName("dict"); + if(dictList.size() > 1 && dictList.size() < 1 ){ + return; + } + + + // Check if autostart and corresponding path is set and abort if so. This is needed since osx 13.0 Ventura + // because it displays an annoying message every time we change the file. + bool isCorrectPath = false; + bool isAutostartEnabled = false; + QDomNode dictNode = dictList.at(0); + if (dictNode.isElement()){ + QDomElement dictElement = dictNode.toElement(); + QDomNodeList keyList = dictElement.elementsByTagName("key"); + for (int j = 0; j < keyList.size(); j++){ + QDomNode keyNode = keyList.at(j); + if (keyNode.isElement()){ + QDomElement keyElement = keyNode.toElement(); + if (keyElement.text() == "ProgramArguments"){ + QDomNode valueNode = keyNode.nextSibling(); + if (valueNode.isElement()){ + QDomElement valueElement = valueNode.toElement(); + QDomNodeList stringList = valueElement.elementsByTagName("string"); + if (!stringList.isEmpty()){ + QDomNode stringNode = stringList.at(0); + if (stringNode.isElement()){ + QDomElement stringElement = stringNode.toElement(); + const QString path = stringElement.text(); + if(path == screenPlayPath){ + isCorrectPath = true; + } + } + } + } + } + } + } + } + + if (dictNode.isElement()){ + QDomElement dictElement = dictNode.toElement(); + QDomNodeList keyList = dictElement.elementsByTagName("key"); + for (int j = 0; j < keyList.size(); j++){ + QDomNode keyNode = keyList.at(j); + if (keyNode.isElement()){ + QDomElement keyElement = keyNode.toElement(); + if (keyElement.text() == "RunAtLoad"){ + QDomNode valueNode = keyNode.nextSibling(); + if (valueNode.isElement()){ + QDomElement valueElement = valueNode.toElement(); + if (valueElement.tagName() == "true"){ + isAutostartEnabled = true; + } + } + } + } + } + } + + // Nothing to do. Autostart has the same value and the path is also correct. + if(isAutostartEnabled == autostart && isCorrectPath) + return; + if (!settingsPlist.remove()) { qCritical() << "Unable to remove: " << settingsPlist.fileName(); }