diff --git a/ScreenPlay/assets.qrc b/ScreenPlay/assets.qrc index 9f5594df..bc4034ee 100644 --- a/ScreenPlay/assets.qrc +++ b/ScreenPlay/assets.qrc @@ -129,5 +129,6 @@ assets/icons/icon_sort-down-solid.svg assets/icons/brand_reddit.svg assets/icons/steam_default_avatar.png + assets/macos/app.screenplay.plist diff --git a/ScreenPlay/assets/macos/app.screenplay.plist b/ScreenPlay/assets/macos/app.screenplay.plist new file mode 100644 index 00000000..228bf652 --- /dev/null +++ b/ScreenPlay/assets/macos/app.screenplay.plist @@ -0,0 +1,18 @@ + + + + + Label + app.screenplay + ProgramArguments + + {{SCREENPLAY_PATH}} + + ProcessType + Interactive + RunAtLoad + <{{SCREENPLAY_AUTOSTART}}/> + KeepAlive + + + diff --git a/ScreenPlay/src/settings.cpp b/ScreenPlay/src/settings.cpp index e32ba403..67315b9e 100644 --- a/ScreenPlay/src/settings.cpp +++ b/ScreenPlay/src/settings.cpp @@ -39,6 +39,16 @@ Settings::Settings(const std::shared_ptr& globalVariables, : QObject(parent) , m_globalVariables { globalVariables } { +#ifdef Q_OS_WIN + setDesktopEnvironment(DesktopEnvironment::Windows); +#endif +#ifdef Q_OS_OSX + setDesktopEnvironment(DesktopEnvironment::OSX); +#endif +#ifdef Q_OS_LINUX + // We only support KDE for now + setDesktopEnvironment(DesktopEnvironment::KDE); +#endif qRegisterMetaType("Settings::Language"); qRegisterMetaType("Settings::Theme"); @@ -47,15 +57,15 @@ Settings::Settings(const std::shared_ptr& globalVariables, qmlRegisterUncreatableType("Settings", 1, 0, "Settings", "Error only for enums"); if (!m_qSettings.contains("Autostart")) { -#ifdef Q_OS_WIN - QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); - if (!m_qSettings.value("Autostart").toBool()) { - if (!settings.contains("ScreenPlay")) { + if (desktopEnvironment() == DesktopEnvironment::Windows) { + QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + if (!m_qSettings.value("Autostart").toBool()) { + if (!settings.contains("ScreenPlay")) { + } } + settings.setValue("ScreenPlay", QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + " -silent"); + settings.sync(); } - settings.setValue("ScreenPlay", QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + " -silent"); - settings.sync(); -#endif m_qSettings.setValue("Autostart", true); m_qSettings.sync(); } else { @@ -87,54 +97,10 @@ Settings::Settings(const std::shared_ptr& globalVariables, writeJsonFileFromResource("profiles"); } - //If empty use steam workshop location - if (QString(m_qSettings.value("ScreenPlayContentPath").toString()).isEmpty()) { - - /* - * ! We must use this (ugly) method, because to stay FOSS we cannot call the steamAPI here ! - * - * We start with the assumption that when we go up 2 folder. - * So that there must be at least a common folder: - * Windows example: - * From -> C:\Program Files (x86)\Steam\steamapps\common\ScreenPlay - * To -> C:\Program Files (x86)\Steam\steamapps\ - * Dest.-> C:\Program Files (x86)\Steam\steamapps\workshop\content\672870 - * - * When we reach the folder it _can_ contain a workshop folder when the user - * previously installed any workshop content. If the folder does not exsist we - * need to create it by hand. Normally Steam will create this folder but we need to - * set it here at this point so that the QFileSystemWatcher in InstalledListModel does - * not generate warnings. - */ - QDir dir; - QString path = QApplication::instance()->applicationDirPath() + "/../../workshop/content/672870"; - if (!dir.mkpath(path)) { - qWarning() << "Could not create steam workshop path for path: " << path; - } else { - m_globalVariables->setLocalStoragePath(QUrl::fromUserInput(path)); - m_qSettings.setValue("ScreenPlayContentPath", dir.cleanPath(path)); - m_qSettings.sync(); - } - - } else { - m_globalVariables->setLocalStoragePath(QUrl::fromUserInput(m_qSettings.value("ScreenPlayContentPath").toString())); - } + initInstalledPath(); setupWidgetAndWindowPaths(); setGitBuildHash(COMPILE_INFO); - -#ifdef Q_OS_WIN - setDesktopEnvironment(DesktopEnvironment::Windows); -#endif - -#ifdef Q_OS_OSX - setDesktopEnvironment(DesktopEnvironment::OSX); -#endif - -#ifdef Q_OS_LINUX - // We only support KDE for now - setDesktopEnvironment(DesktopEnvironment::KDE); -#endif } /*! @@ -204,6 +170,53 @@ void Settings::restoreDefault(const QString& appConfigLocation, const QString& s writeJsonFileFromResource(settingsFileType); } +void Settings::initInstalledPath() +{ + //If empty use steam workshop location + qInfo() << m_qSettings.value("ScreenPlayContentPath").toString(); + if (QString(m_qSettings.value("ScreenPlayContentPath").toString()).isEmpty()) { + + /* + * ! We must use this (ugly) method, because to stay FOSS we cannot call the steamAPI here ! + * + * We start with the assumption that when we go up 2 folder. + * So that there must be at least a common folder: + * Windows example: + * From -> C:\Program Files (x86)\Steam\steamapps\common\ScreenPlay + * To -> C:\Program Files (x86)\Steam\steamapps\ + * Dest.-> C:\Program Files (x86)\Steam\steamapps\workshop\content\672870 + * + * When we reach the folder it _can_ contain a workshop folder when the user + * previously installed any workshop content. If the folder does not exsist we + * need to create it by hand. Normally Steam will create this folder but we need to + * set it here at this point so that the QFileSystemWatcher in InstalledListModel does + * not generate warnings. + */ + QDir dir; + QString appBasePath = QApplication::instance()->applicationDirPath(); + if (desktopEnvironment() == DesktopEnvironment::OSX) { + appBasePath += "/../../.."; + } + QString path = appBasePath + "/../../workshop/content/672870"; + qInfo() << path; + + if (!dir.mkpath(path)) { + qWarning() << "Could not create steam workshop path for path: " << path; + } + + if (QDir(path).exists()) { + m_globalVariables->setLocalStoragePath(QUrl::fromUserInput(path)); + m_qSettings.setValue("ScreenPlayContentPath", dir.cleanPath(path)); + m_qSettings.sync(); + } else { + qWarning() << "The following path could not be resolved to search for workshop content: " << path; + } + + } else { + m_globalVariables->setLocalStoragePath(QUrl::fromUserInput(m_qSettings.value("ScreenPlayContentPath").toString())); + } +} + /*! \brief Checks if there is already a saved language. If not we try to use the system langauge. If we do not support the system language we use english. diff --git a/ScreenPlay/src/settings.h b/ScreenPlay/src/settings.h index 48c48b27..2ca8ec6f 100644 --- a/ScreenPlay/src/settings.h +++ b/ScreenPlay/src/settings.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -185,19 +186,48 @@ public slots: void setAutostart(bool autostart) { - if (m_autostart == autostart) - return; + if (desktopEnvironment() == DesktopEnvironment::Windows) { -#ifdef Q_OS_WIN - QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); - if (autostart) { - settings.setValue("ScreenPlay", QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + " -silent"); - settings.sync(); - } else { - settings.remove("ScreenPlay"); + QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + if (autostart) { + settings.setValue("ScreenPlay", QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + " -silent"); + settings.sync(); + } else { + settings.remove("ScreenPlay"); + } } -#endif + if (desktopEnvironment() == DesktopEnvironment::OSX) { + const QString plistFileName = "app.screenplay.plist"; + QFile defaultPListFile(":/assets/macos/" + plistFileName); + defaultPListFile.open(QIODevice::ReadOnly); + QString settingsPlistContent = defaultPListFile.readAll(); + if(!settingsPlistContent.contains("{{SCREENPLAY_PATH}}")){ + qCritical() << "Unable to load plist settings template from qrc to set autostart!"; + } + QDir workingDir(QGuiApplication::applicationDirPath()); + workingDir.cdUp(); + workingDir.cdUp(); + workingDir.cdUp(); + const QString screenPlayPath = QUrl::fromUserInput(workingDir.path() + "/ScreenPlay.app/Contents/MacOS/ScreenPlay").toLocalFile(); + settingsPlistContent.replace("{{SCREENPLAY_PATH}}", screenPlayPath); + settingsPlistContent.replace("{{SCREENPLAY_AUTOSTART}}", autostart ? "true":"false"); + + const QString homePath = QDir::homePath(); + QFile settingsPlist(homePath + "/Library/LaunchAgents/" + plistFileName); + if (settingsPlist.exists()) { + if(!settingsPlist.remove()){ + qCritical() << "Unable to remove: " << settingsPlist; + } + } + + settingsPlist.open(QIODevice::WriteOnly | QIODevice::Truncate); + QTextStream out(&settingsPlist); + out.setCodec("UTF-8"); + out << settingsPlistContent; + settingsPlist.flush(); + settingsPlist.close(); + } setqSetting("Autostart", autostart); m_autostart = autostart; @@ -371,6 +401,7 @@ public slots: private: void restoreDefault(const QString& appConfigLocation, const QString& settingsFileType); + void initInstalledPath(); private: QSettings m_qSettings;