From 5a62b9a68c616db9e4afa547e315913bfa7bbc45 Mon Sep 17 00:00:00 2001 From: polirritmico Date: Thu, 28 Jul 2022 20:55:43 -0400 Subject: [PATCH] [PATCH] Follow XDG Base Directory spec on Linux Uses QStandardPaths to get cache, data and config XDG folders. The config file go to $XDG_CONFIG_HOME/hydrogen/hydrogen.conf The cache folder go to $XDG_CACHE_HOME/hydrogen/ And all the data subfolders (drumkits, plugins, playlists, etc.) go to $XDG_DATA_HOME/hydrogen/ along with the hydrogen.log file. If old folder (~/.hydrogen/) is found, use that instead of the XDG paths. The __usr_*_paths are setted outside a function, so QStandardPaths isn't avaliable to get the XDG paths. Also no APPNAME is avaliable until setApplicationName() is called after the Filesystem::check_usr_paths() is used. To avoid this update_usr_paths() is added at the begining of check_usr_paths() to update the correct values from QStandardPaths. --- src/core/Helpers/Filesystem.cpp | 57 ++++++++++++++++++++++++++++++++- src/core/Helpers/Filesystem.h | 7 ++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/core/Helpers/Filesystem.cpp b/src/core/Helpers/Filesystem.cpp index f8f2f5118a..6be5efda8d 100644 --- a/src/core/Helpers/Filesystem.cpp +++ b/src/core/Helpers/Filesystem.cpp @@ -31,6 +31,10 @@ #include #include #include +#if defined(Q_OS_MACX) || defined(WIN32) +#else +#include +#endif #ifdef H2CORE_HAVE_OSC #include @@ -100,6 +104,7 @@ const QString Filesystem::playlists_filter_name = "Hydrogen Playlists (*.h2playl QString Filesystem::__sys_data_path; QString Filesystem::__usr_data_path; +QString Filesystem::__usr_cache_path; QString Filesystem::__usr_cfg_path; #ifdef Q_OS_MACX @@ -107,7 +112,8 @@ QString Filesystem::__usr_cfg_path; #elif WIN32 QString Filesystem::__usr_log_path = QDir::homePath().append( "/.hydrogen/" LOG_FILE ); #else - QString Filesystem::__usr_log_path = QDir::homePath().append( "/" H2_USR_PATH "/" LOG_FILE); + QString Filesystem::__usr_log_path = QDir::homePath().append( "/" H2_USR_PATH "/" ); + bool __usr_log_path_initialized = false; #endif @@ -412,9 +418,36 @@ bool Filesystem::check_sys_paths() } +#if defined(Q_OS_MACX) || defined(WIN32) +#else +void Filesystem::update_usr_paths() +{ + // If the old path exists (e.g. ~/.hydrogen), old path is used; else uses + // QStandardPaths to get XDG Paths on Linux. + if ( !QFileInfo::exists( QFileInfo(__usr_cfg_path).absolutePath() ) ) + { + __usr_cfg_path = QStandardPaths::writableLocation( + QStandardPaths::AppConfigLocation).append("/" USR_CONFIG); + __usr_data_path = QStandardPaths::writableLocation( + QStandardPaths::AppLocalDataLocation).append("/"); + __usr_cache_path = QStandardPaths::writableLocation( + QStandardPaths::CacheLocation).append("/"); + } else { + __usr_cache_path = __usr_data_path + CACHE; + } +} +#endif + + bool Filesystem::check_usr_paths() { bool ret = true; + +#if defined(Q_OS_MACX) || defined(WIN32) +#else + update_usr_paths(); + if ( !path_usable( QFileInfo(__usr_cfg_path).absolutePath() ) ) ret = false; +#endif if( !path_usable( tmp_dir() ) ) ret = false; if( !path_usable( __usr_data_path ) ) ret = false; if( !path_usable( cache_dir() ) ) ret = false; @@ -518,6 +551,20 @@ QString Filesystem::playlist_xsd_path( ) } QString Filesystem::log_file_path() { +#if defined(Q_OS_MACX) || defined(WIN32) +#else + if ( !__usr_log_path_initialized ) + { + if ( !QFileInfo::exists( __usr_log_path ) ) + { + __usr_log_path = QStandardPaths::writableLocation( + QStandardPaths::AppLocalDataLocation).append( "/" LOG_FILE ); + } else { + __usr_log_path.append( LOG_FILE ); + } + __usr_log_path_initialized = true; + } +#endif return __usr_log_path; } @@ -592,11 +639,19 @@ QString Filesystem::playlist_path( const QString& pl_name ) } QString Filesystem::cache_dir() { +#if defined(Q_OS_MACX) || defined(WIN32) return __usr_data_path + CACHE; +#else + return __usr_cache_path; +#endif } QString Filesystem::repositories_cache_dir() { +#if defined(Q_OS_MACX) || defined(WIN32) return __usr_data_path + CACHE + REPOSITORIES; +#else + return __usr_cache_path + REPOSITORIES; +#endif } QString Filesystem::demos_dir() { diff --git a/src/core/Helpers/Filesystem.h b/src/core/Helpers/Filesystem.h index 37844fda57..ba205808a6 100644 --- a/src/core/Helpers/Filesystem.h +++ b/src/core/Helpers/Filesystem.h @@ -446,6 +446,12 @@ namespace H2Core */ static bool check_permissions( const QString& path, const int perms, bool silent ); + /** + * Update the data, config and cache paths with QStandardPaths if old + * folder doesn't exist (e.g. ~/.hydrogen/). + */ + static void update_usr_paths(); + /** * Path to the system files set in Filesystem::bootstrap(). * @@ -471,6 +477,7 @@ namespace H2Core */ static QString __sys_data_path; ///< the path to the system files static QString __usr_data_path; ///< the path to the user files + static QString __usr_cache_path; ///< the path to the cache files static QString __usr_cfg_path; ///< the path to the user config file static QString __usr_log_path; ///< the path to the log file static QStringList __ladspa_paths; ///< paths to laspa plugins