From 18ca5b169b0386b46894ed567a48573da9dab28e Mon Sep 17 00:00:00 2001 From: Eulalie Coevoet Date: Tue, 5 Nov 2024 15:55:52 +0100 Subject: [PATCH 1/4] [main] changes screenshots and config directories location --- .../Helper/src/sofa/helper/Utils.cpp | 87 ++++++++++++++----- Sofa/framework/Helper/src/sofa/helper/Utils.h | 6 ++ applications/projects/runSofa/Main.cpp | 4 +- 3 files changed, 74 insertions(+), 23 deletions(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp index 9bb99fc101c..6762824cd8f 100644 --- a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp @@ -234,16 +234,16 @@ std::map Utils::readBasicIniFile(const std::string& pa } // no standard/portable way -const std::string& Utils::getUserLocalDirectory() +const std::string& Utils::getUserHomeDirectory() { auto computeUserHomeDirectory = []() { -// Windows: "LocalAppData" directory i.e ${HOME}\AppData\Local +// Windows: ${HOME} #ifdef WIN32 std::wstring wresult; wchar_t* path = nullptr; - const auto hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &path); + const auto hr = SHGetKnownFolderPath(FOLDERID_Home, 0, nullptr, &path); if (SUCCEEDED(hr)) { wresult = std::wstring(path); @@ -256,20 +256,20 @@ const std::string& Utils::getUserLocalDirectory() return Utils::narrowString(wresult); #elif defined(__APPLE__) // macOS : ${HOME}/Library/Application Support - // https://stackoverflow.com/questions/5123361/finding-library-application-support-from-c - + // https://stackoverflow.com/questions/5123361/finding-library-application-support-from-c + char path[PATH_MAX]; auto state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_APPLICATION_SUPPORT, SYSDIR_DOMAIN_MASK_USER); if ((state = sysdir_get_next_search_path_enumeration(state, path))) { glob_t globbuf; - if (glob(path, GLOB_TILDE, nullptr, &globbuf) == 0) + if (glob(path, GLOB_TILDE, nullptr, &globbuf) == 0) { std::string result(globbuf.gl_pathv[0]); globfree(&globbuf); return result; - } + } else { // "Unable to expand tilde" @@ -281,34 +281,79 @@ const std::string& Utils::getUserLocalDirectory() // "Failed to get settings folder" return std::string(""); } - + +#else // Linux: ${HOME} + + const char* homeDir; + + // if HOME is defined + if ((homeDir = std::getenv("HOME")) == nullptr) + { + // else system calls are used + homeDir = getpwuid(getuid())->pw_dir; + } + + return std::string(homeDir); + +#endif + }; + + static std::string homeDir = FileSystem::cleanPath(computeUserHomeDirectory()); + return homeDir; +} + +const std::string& Utils::getSofaDataDirectory() +{ + constexpr std::string_view sofaDataDirSuffix = "SOFAData"; + + static std::string sofaDataDirectory = FileSystem::cleanPath(FileSystem::findOrCreateAValidPath( + FileSystem::append(getUserHomeDirectory(), sofaDataDirSuffix))); + + return sofaDataDirectory; +} + + +// no standard/portable way +const std::string& Utils::getUserLocalDirectory() +{ + + auto computeUserLocalDirectory = []() + { +// Windows: "LocalAppData" directory i.e ${HOME}\AppData\Local +#ifdef WIN32 + std::wstring wresult; + wchar_t* path = nullptr; + const auto hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &path); + if (SUCCEEDED(hr)) + { + wresult = std::wstring(path); + } + if (path) + { + CoTaskMemFree(path); + } + + return Utils::narrowString(wresult); + +#elif defined(__APPLE__) // macOS : ${HOME}/Library/Application Support + return getUserHomeDirectory(); #else // Linux: either ${XDG_CONFIG_HOME} if defined, or ${HOME}/.config (should be equivalent) const char* configDir; // if env.var XDG_CONFIG_HOME is defined if ((configDir = std::getenv("XDG_CONFIG_HOME")) == nullptr) { - const char* homeDir; - - // else if HOME is defined - if ((homeDir = std::getenv("HOME")) == nullptr) - { - // else system calls are used - homeDir = getpwuid(getuid())->pw_dir; - } - - return std::string(homeDir) + std::string("/.config"); + return getUserHomeDirectory() + std::string("/.config"); } else { return std::string(configDir); } - #endif }; - static std::string homeDir = FileSystem::cleanPath(computeUserHomeDirectory()); - return homeDir; + static std::string userLocalDir = FileSystem::cleanPath(computeUserLocalDirectory()); + return userLocalDir; } const std::string& Utils::getSofaUserLocalDirectory() diff --git a/Sofa/framework/Helper/src/sofa/helper/Utils.h b/Sofa/framework/Helper/src/sofa/helper/Utils.h index 388c09e5d40..816ce6051b3 100644 --- a/Sofa/framework/Helper/src/sofa/helper/Utils.h +++ b/Sofa/framework/Helper/src/sofa/helper/Utils.h @@ -75,6 +75,12 @@ static const std::string& getExecutablePath(); /// @brief Get the path to the directory of the executable that is currently running. static const std::string& getExecutableDirectory(); +/// @brief Get the path to the current user home directory. +static const std::string& getUserHomeDirectory(); + +/// @brief Get the path to the SOFA data directory into the current user home directory. +static const std::string& getSofaDataDirectory(); + /// @brief Get the path to the current user local config directory. static const std::string& getUserLocalDirectory(); diff --git a/applications/projects/runSofa/Main.cpp b/applications/projects/runSofa/Main.cpp index 6688d961f1e..5e272e43fd7 100644 --- a/applications/projects/runSofa/Main.cpp +++ b/applications/projects/runSofa/Main.cpp @@ -374,8 +374,8 @@ int main(int argc, char** argv) msg_info(appName) << "GuiDataRepository paths = " << GuiDataRepository.getPathsJoined(); // Initialise paths - BaseGUI::setConfigDirectoryPath(Utils::getSofaPathPrefix() + "/config", true); - BaseGUI::setScreenshotDirectoryPath(Utils::getSofaPathPrefix() + "/screenshots", true); + BaseGUI::setConfigDirectoryPath(Utils::getSofaUserLocalDirectory() + "/config", true); + BaseGUI::setScreenshotDirectoryPath(Utils::getSofaDataDirectory() + "/screenshots", true); // Add Batch GUI (runSofa without any GUIs won't be useful) sofa::gui::batch::init(); From 1c0b80b1ea28568efc13d51b77f259bfa92a3f8d Mon Sep 17 00:00:00 2001 From: Eulalie Coevoet Date: Tue, 5 Nov 2024 16:58:19 +0100 Subject: [PATCH 2/4] [helper] Utils: on Windows, check env to get user home directory --- .../Helper/src/sofa/helper/Utils.cpp | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp index 6762824cd8f..301d76d13de 100644 --- a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp @@ -236,24 +236,19 @@ std::map Utils::readBasicIniFile(const std::string& pa // no standard/portable way const std::string& Utils::getUserHomeDirectory() { - auto computeUserHomeDirectory = []() { -// Windows: ${HOME} -#ifdef WIN32 - std::wstring wresult; - wchar_t* path = nullptr; - const auto hr = SHGetKnownFolderPath(FOLDERID_Home, 0, nullptr, &path); - if (SUCCEEDED(hr)) - { - wresult = std::wstring(path); - } - if (path) +#ifdef WIN32 // Windows: ${HOME} + const char* homeDir; + + // if USERPROFILE is defined + if ((homeDir = std::getenv("USERPROFILE")) == nullptr) { - CoTaskMemFree(path); + // else system calls are used + homeDir = getpwuid(getuid())->pw_dir; } - return Utils::narrowString(wresult); + return std::string(homeDir); #elif defined(__APPLE__) // macOS : ${HOME}/Library/Application Support // https://stackoverflow.com/questions/5123361/finding-library-application-support-from-c @@ -319,8 +314,7 @@ const std::string& Utils::getUserLocalDirectory() auto computeUserLocalDirectory = []() { -// Windows: "LocalAppData" directory i.e ${HOME}\AppData\Local -#ifdef WIN32 +#ifdef WIN32 // Windows: "LocalAppData" directory i.e ${HOME}\AppData\Local std::wstring wresult; wchar_t* path = nullptr; const auto hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &path); @@ -334,7 +328,6 @@ const std::string& Utils::getUserLocalDirectory() } return Utils::narrowString(wresult); - #elif defined(__APPLE__) // macOS : ${HOME}/Library/Application Support return getUserHomeDirectory(); #else // Linux: either ${XDG_CONFIG_HOME} if defined, or ${HOME}/.config (should be equivalent) From 9fe83ddb190b9dbc0cabb0a15342781686662dff Mon Sep 17 00:00:00 2001 From: Eulalie Coevoet Date: Wed, 13 Nov 2024 12:13:58 +0100 Subject: [PATCH 3/4] [Helper] fixes getUserHomeDirectory for Windows --- Sofa/framework/Helper/src/sofa/helper/Utils.cpp | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp index 301d76d13de..96b7e770ffd 100644 --- a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp @@ -239,17 +239,8 @@ const std::string& Utils::getUserHomeDirectory() auto computeUserHomeDirectory = []() { #ifdef WIN32 // Windows: ${HOME} - const char* homeDir; - - // if USERPROFILE is defined - if ((homeDir = std::getenv("USERPROFILE")) == nullptr) - { - // else system calls are used - homeDir = getpwuid(getuid())->pw_dir; - } - + const char* homeDir = std::getenv("USERPROFILE")); return std::string(homeDir); - #elif defined(__APPLE__) // macOS : ${HOME}/Library/Application Support // https://stackoverflow.com/questions/5123361/finding-library-application-support-from-c @@ -311,7 +302,6 @@ const std::string& Utils::getSofaDataDirectory() // no standard/portable way const std::string& Utils::getUserLocalDirectory() { - auto computeUserLocalDirectory = []() { #ifdef WIN32 // Windows: "LocalAppData" directory i.e ${HOME}\AppData\Local From aa17977c28cc4f4bedbfa6aaaaa4036ad9fa4cda Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Fri, 15 Nov 2024 09:10:10 +0900 Subject: [PATCH 4/4] Update Utils.cpp (bogus bracket) --- Sofa/framework/Helper/src/sofa/helper/Utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp index 96b7e770ffd..27158f42202 100644 --- a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp @@ -239,7 +239,7 @@ const std::string& Utils::getUserHomeDirectory() auto computeUserHomeDirectory = []() { #ifdef WIN32 // Windows: ${HOME} - const char* homeDir = std::getenv("USERPROFILE")); + const char* homeDir = std::getenv("USERPROFILE"); return std::string(homeDir); #elif defined(__APPLE__) // macOS : ${HOME}/Library/Application Support // https://stackoverflow.com/questions/5123361/finding-library-application-support-from-c