Skip to content

Commit

Permalink
[PATCH] Follow XDG Base Directory spec on Linux
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
polirritmico committed Jul 29, 2022
1 parent 3f30d34 commit 5a62b9a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/core/Helpers/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include <QtCore/QFileInfo>
#include <QtCore/QCoreApplication>
#include <QDateTime>
#if defined(Q_OS_MACX) || defined(WIN32)
#else
#include <QStandardPaths>
#endif

#ifdef H2CORE_HAVE_OSC
#include <core/NsmClient.h>
Expand Down Expand Up @@ -100,14 +104,16 @@ 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
QString Filesystem::__usr_log_path =QDir::homePath().append( "/Library/Application Support/Hydrogen/" LOG_FILE );
#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


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()
{
Expand Down
7 changes: 7 additions & 0 deletions src/core/Helpers/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand All @@ -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
Expand Down

0 comments on commit 5a62b9a

Please sign in to comment.