From 2209c384aaba34d4f876af93599a5524c9070b2b Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Thu, 6 Apr 2023 11:40:32 +0200 Subject: [PATCH] Added a UserThemeDirectory setting for relocating the user theme directory --- es-app/src/guis/GuiThemeDownloader.cpp | 26 ++++++++++++++++++- es-app/src/main.cpp | 35 ++++++++++++++++++-------- es-core/src/Settings.cpp | 1 + es-core/src/ThemeData.cpp | 30 +++++++++++++++++++--- 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/es-app/src/guis/GuiThemeDownloader.cpp b/es-app/src/guis/GuiThemeDownloader.cpp index c56a63c95..392579dea 100644 --- a/es-app/src/guis/GuiThemeDownloader.cpp +++ b/es-app/src/guis/GuiThemeDownloader.cpp @@ -163,7 +163,31 @@ GuiThemeDownloader::GuiThemeDownloader(std::function updateCallback) std::promise().swap(mPromise); mFuture = mPromise.get_future(); - mThemeDirectory = Utils::FileSystem::getHomePath() + "/.emulationstation/themes/"; + const std::string defaultUserThemeDir {Utils::FileSystem::getHomePath() + + "/.emulationstation/themes"}; + std::string userThemeDirSetting {Utils::FileSystem::expandHomePath( + Settings::getInstance()->getString("UserThemeDirectory"))}; +#if defined(_WIN64) + mThemeDirectory = Utils::String::replace(mThemeDirectory, "\\", "/"); +#endif + + if (userThemeDirSetting == "") { + mThemeDirectory = defaultUserThemeDir; + } + else if (Utils::FileSystem::isDirectory(userThemeDirSetting) || + Utils::FileSystem::isSymlink(userThemeDirSetting)) { + mThemeDirectory = userThemeDirSetting; + } + else { + LOG(LogWarning) << "GuiThemeDownloader: Requested user theme directory \"" + << userThemeDirSetting + << "\" does not exist or is not a directory, reverting to \"" + << defaultUserThemeDir << "\""; + mThemeDirectory = defaultUserThemeDir; + } + + if (mThemeDirectory.back() != '/') + mThemeDirectory.append("/"); } GuiThemeDownloader::~GuiThemeDownloader() diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index b365212ff..4f1847824 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -673,19 +673,32 @@ int main(int argc, char* argv[]) } } - // Create the themes directory in the application home folder. This is not required but - // is rather a convenience in case the user wants to add additional themes. - const std::string themesDir {Utils::FileSystem::getHomePath() + "/.emulationstation/themes"}; - if (!Utils::FileSystem::exists(themesDir)) { + // Create the themes directory in the application home directory (or elsewhere if the + // UserThemeDirectory setting has been defined). + const std::string defaultUserThemeDir {Utils::FileSystem::getHomePath() + + "/.emulationstation/themes"}; + std::string userThemeDirSetting {Utils::FileSystem::expandHomePath( + Settings::getInstance()->getString("UserThemeDirectory"))}; #if defined(_WIN64) - LOG(LogInfo) << "Creating themes directory \"" - << Utils::String::replace(themesDir, "/", "\\") << "\"..."; -#else - LOG(LogInfo) << "Creating themes directory \"" << themesDir << "\"..."; + userThemeDirSetting = Utils::String::replace(userThemeDirSetting, "\\", "/"); #endif - Utils::FileSystem::createDirectory(themesDir); - if (!Utils::FileSystem::exists(themesDir)) { - LOG(LogWarning) << "Couldn't create directory, permission problems?\n"; + std::string userThemeDirectory; + + if (userThemeDirSetting == "") + userThemeDirectory = defaultUserThemeDir; + else + userThemeDirectory = userThemeDirSetting; + + if (!Utils::FileSystem::exists(userThemeDirectory)) { +#if defined(_WIN64) + LOG(LogInfo) << "Creating user theme directory \"" + << Utils::String::replace(userThemeDirectory, "/", "\\") << "\"..."; +#else + LOG(LogInfo) << "Creating themes directory \"" << userThemeDirectory << "\"..."; +#endif + Utils::FileSystem::createDirectory(userThemeDirectory); + if (!Utils::FileSystem::exists(userThemeDirectory)) { + LOG(LogWarning) << "Couldn't create directory, permission problems?"; } } diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp index af77713e1..adc980e9a 100644 --- a/es-core/src/Settings.cpp +++ b/es-core/src/Settings.cpp @@ -316,6 +316,7 @@ void Settings::setDefaults() mStringMap["OpenGLVersion"] = {"", ""}; mStringMap["ROMDirectory"] = {"", ""}; mStringMap["UIMode_passkey"] = {"uuddlrlrba", "uuddlrlrba"}; + mStringMap["UserThemeDirectory"] = {"", ""}; mIntMap["LottieMaxFileCache"] = {150, 150}; mIntMap["LottieMaxTotalCache"] = {1024, 1024}; mIntMap["ScraperConnectionTimeout"] = {30, 30}; diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index d67350c97..b0135f6ee 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -771,8 +771,32 @@ void ThemeData::populateThemeSets() sThemeSets.clear(); LOG(LogInfo) << "Checking for available theme sets..."; - // Check for themes first under the home directory, then under the data installation - // directory (Unix only) and last under the ES-DE binary directory. + // Check for themes first under the user theme directory (which is in the ES-DE home directory + // by default), then under the data installation directory (Unix only) and last under the ES-DE + // binary directory. + const std::string defaultUserThemeDir {Utils::FileSystem::getHomePath() + + "/.emulationstation/themes"}; + std::string userThemeDirSetting {Utils::FileSystem::expandHomePath( + Settings::getInstance()->getString("UserThemeDirectory"))}; +#if defined(_WIN64) + userThemeDirSetting = Utils::String::replace(userThemeDirSetting, "\\", "/"); +#endif + std::string userThemeDirectory; + + if (userThemeDirSetting == "") { + userThemeDirectory = defaultUserThemeDir; + } + else if (Utils::FileSystem::isDirectory(userThemeDirSetting) || + Utils::FileSystem::isSymlink(userThemeDirSetting)) { + userThemeDirectory = userThemeDirSetting; + LOG(LogInfo) << "Setting user theme directory to \"" << userThemeDirectory << "\""; + } + else { + LOG(LogWarning) << "Requested user theme directory \"" << userThemeDirSetting + << "\" does not exist or is not a directory, reverting to \"" + << defaultUserThemeDir << "\""; + userThemeDirectory = defaultUserThemeDir; + } #if defined(__unix__) || defined(__APPLE__) #if defined(APPIMAGE_BUILD) @@ -790,7 +814,7 @@ void ThemeData::populateThemeSets() #elif defined(__unix__) && !defined(APPIMAGE_BUILD) Utils::FileSystem::getProgramDataPath() + "/themes", #endif - Utils::FileSystem::getHomePath() + "/.emulationstation/themes" + userThemeDirectory }; for (size_t i {0}; i < pathCount; ++i) {