Linux-specific: Optionally store history in XDG_DATA_HOME in Qt5 build

Separate data directory is only used if history file isn't already
present in the configuration directory.

This commit together with two previous ones allows to switch to
layout described in XDG Base Directory specification. Data is not
migrated automatically and old ~/.goldendict home dir has precedence.
If you wish to use standards-compliant locations, move files and
directories manually:

```
CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/goldendict"
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/goldendict"
DATA="${XDG_DATA_HOME:-$HOME/.local/share}/goldendict"
mkdir -p "${CACHE}"
mkdir -p "${DATA}"
mv ~/.goldendict/index "${CACHE}"
mv ~/.goldendict/history "${DATA}"
mv ~/.goldendict/ "${CONFIG}"
```
This commit is contained in:
fenuks 2021-10-31 21:49:38 +01:00 committed by Abs62
parent ae4eea3513
commit b9b7067b5b
2 changed files with 37 additions and 4 deletions

View file

@ -28,10 +28,31 @@
#include <QDesktopServices>
#endif
#if defined( HAVE_X11 ) && QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
// Whether XDG Base Directory specification might be followed.
// Only Qt5 builds are supported, as Qt4 doesn't provide all functions needed
// to get XDG Base Directory compliant locations.
#define XDG_BASE_DIRECTORY_COMPLIANCE
#endif
namespace Config {
namespace
{
#ifdef XDG_BASE_DIRECTORY_COMPLIANCE
const char xdgSubdirName[] = "goldendict";
QDir getDataDir()
{
QDir dir = QStandardPaths::writableLocation( QStandardPaths::GenericDataLocation );
dir.mkpath( xdgSubdirName );
if ( !dir.cd( xdgSubdirName ) )
throw exCantUseDataDir();
return dir;
}
#endif
QString portableHomeDirPath()
{
return QCoreApplication::applicationDirPath() + "/portable";
@ -52,12 +73,12 @@ namespace
result = QDir::fromNativeSeparators( QString::fromWCharArray( _wgetenv( L"APPDATA" ) ) );
#else
char const * pathInHome = ".goldendict";
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) && defined( HAVE_X11 )
#ifdef XDG_BASE_DIRECTORY_COMPLIANCE
// check if an old config dir is present, otherwise use standards-compliant location
if ( !result.exists( pathInHome ) )
{
result.setPath( QStandardPaths::writableLocation( QStandardPaths::ConfigLocation ) );
pathInHome = "goldendict";
pathInHome = xdgSubdirName;
}
#endif
#endif
@ -2214,7 +2235,7 @@ QString getIndexDir() THROW_SPEC( exError )
{
QDir result = getHomeDir();
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) && defined( HAVE_X11 )
#ifdef XDG_BASE_DIRECTORY_COMPLIANCE
// store index in XDG_CACHE_HOME in non-portable version
// *and* when an old index directory in GoldenDict home doesn't exist
if ( !isPortableVersion() && !result.exists( "index" ) )
@ -2238,7 +2259,18 @@ QString getPidFileName() THROW_SPEC( exError )
QString getHistoryFileName() THROW_SPEC( exError )
{
return getHomeDir().filePath( "history" );
QString homeHistoryPath = getHomeDir().filePath( "history" );
#ifdef XDG_BASE_DIRECTORY_COMPLIANCE
// use separate data dir for history, if it is not already stored alongside
// configuration in non-portable mode
if ( !isPortableVersion() && !QFile::exists( homeHistoryPath ) )
{
return getDataDir().filePath( "history" );
}
#endif
return homeHistoryPath;
}
QString getFavoritiesFileName() THROW_SPEC( exError )

View file

@ -758,6 +758,7 @@ private:
};
DEF_EX( exError, "Error with the program's configuration", std::exception )
DEF_EX( exCantUseDataDir, "Can't use XDG_DATA_HOME directory to store GoldenDict data", exError )
DEF_EX( exCantUseHomeDir, "Can't use home directory to store GoldenDict preferences", exError )
DEF_EX( exCantUseIndexDir, "Can't use index directory to store GoldenDict index files", exError )
DEF_EX( exCantReadConfigFile, "Can't read the configuration file", exError )