Override config, data and cache location

This commit is contained in:
Jonas Kvinge
2025-01-05 23:45:29 +01:00
parent f938129d81
commit cbce9f7191
20 changed files with 177 additions and 51 deletions

View File

@@ -438,6 +438,7 @@ set(SOURCES
src/core/enginemetadata.cpp
src/core/songmimedata.cpp
src/core/platforminterface.cpp
src/core/standardpaths.cpp
src/utilities/strutils.cpp
src/utilities/envutils.cpp

View File

@@ -50,14 +50,14 @@
#include <QPixmapCache>
#include <QNetworkDiskCache>
#include <QSettings>
#include <QStandardPaths>
#include <QTimer>
#include "includes/scoped_ptr.h"
#include "includes/shared_ptr.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "core/database.h"
#include "core/iconloader.h"
#include "core/logging.h"
#include "core/settings.h"
#include "core/songmimedata.h"
#include "collectionfilteroptions.h"
@@ -114,7 +114,7 @@ CollectionModel::CollectionModel(const SharedPtr<CollectionBackend> backend, con
pixmap_no_cover_ = nocover.pixmap(nocover_sizes.last()).scaled(kPrettyCoverSize, kPrettyCoverSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
icon_disk_cache_->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u'/' + QLatin1String(kPixmapDiskCacheDir) + u'-' + Song::TextForSource(backend_->source()));
icon_disk_cache_->setCacheDirectory(StandardPaths::WritableLocation(StandardPaths::CacheLocation) + u'/' + QLatin1String(kPixmapDiskCacheDir) + u'-' + Song::TextForSource(backend_->source()));
QObject::connect(&*backend_, &CollectionBackend::SongsAdded, this, &CollectionModel::AddReAddOrUpdate);
QObject::connect(&*backend_, &CollectionBackend::SongsChanged, this, &CollectionModel::AddReAddOrUpdate);

View File

@@ -39,10 +39,10 @@
#include <QSqlDriver>
#include <QSqlDatabase>
#include <QSqlError>
#include <QStandardPaths>
#include <QScopeGuard>
#include "core/logging.h"
#include "logging.h"
#include "standardpaths.h"
#include "taskmanager.h"
#include "database.h"
#include "sqlquery.h"
@@ -78,7 +78,7 @@ Database::Database(SharedPtr<TaskManager> task_manager, QObject *parent, const Q
connection_id_ = sNextConnectionId++;
}
directory_ = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
directory_ = QDir::toNativeSeparators(StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation)).replace(u"Strawberry"_s, u"strawberry"_s);
QMutexLocker l(&mutex_);
Connect();

View File

@@ -27,10 +27,10 @@
#include <QString>
#include <QIcon>
#include <QSize>
#include <QStandardPaths>
#include <QSettings>
#include "logging.h"
#include "standardpaths.h"
#include "settings.h"
#include "includes/iconmapper.h"
#include "iconloader.h"
@@ -51,7 +51,7 @@ void IconLoader::Init() {
#endif
QDir dir;
if (dir.exists(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/icons"_s)) {
if (dir.exists(StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/icons"_s)) {
custom_icons_ = true;
}
@@ -125,7 +125,7 @@ QIcon IconLoader::Load(const QString &name, const bool system_icon, const int fi
}
if (custom_icons_) {
QString custom_icon_path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/icons/%1x%2/%3.png"_s;
QString custom_icon_path = StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/icons/%1x%2/%3.png"_s;
for (int s : std::as_const(sizes)) {
QString filename(custom_icon_path.arg(s).arg(s).arg(name));
if (QFile::exists(filename)) ret.addFile(filename, QSize(s, s));

View File

@@ -1,6 +1,6 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2024-2025, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,13 +18,26 @@
*/
#include <QSettings>
#include <QVariant>
#include <QString>
#include <QCoreApplication>
#include "settings.h"
using namespace Qt::Literals::StringLiterals;
Settings::Settings(QObject *parent)
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
: QSettings(QCoreApplication::organizationName().toLower(), QCoreApplication::applicationName().toLower(), parent) {}
#else
: QSettings(parent) {}
#endif
Settings::Settings(const QSettings::Scope scope, QObject *parent)
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
: QSettings(scope, QCoreApplication::organizationName().toLower(), QCoreApplication::applicationName().toLower(), parent) {}
#else
: QSettings(scope, parent) {}
#endif
Settings::Settings(const QString &filename, const Format format, QObject *parent)
: QSettings(filename, format, parent) {}

View File

@@ -1,6 +1,6 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2024-2025, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -21,14 +21,14 @@
#define SETTINGS_H
#include <QSettings>
#include <QObject>
#include <QVariant>
#include <QString>
class Settings : public QSettings {
Q_OBJECT
public:
explicit Settings(QObject *parent = nullptr);
explicit Settings(const QSettings::Scope scope, QObject *parent = nullptr);
explicit Settings(const QString &filename, const Format format, QObject *parent = nullptr);
};

View File

@@ -45,11 +45,11 @@
#include <QRegularExpression>
#include <QUrl>
#include <QIcon>
#include <QStandardPaths>
#include <QSqlRecord>
#include <taglib/tstring.h>
#include "core/standardpaths.h"
#include "core/iconloader.h"
#include "core/enginemetadata.h"
#include "utilities/strutils.h"
@@ -1335,24 +1335,24 @@ QString Song::ImageCacheDir(const Source source) {
switch (source) {
case Source::Collection:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/collectionalbumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/collectionalbumcovers"_s;
case Source::Subsonic:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/subsonicalbumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/subsonicalbumcovers"_s;
case Source::Tidal:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/tidalalbumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/tidalalbumcovers"_s;
case Source::Spotify:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/spotifyalbumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/spotifyalbumcovers"_s;
case Source::Qobuz:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/qobuzalbumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/qobuzalbumcovers"_s;
case Source::Device:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/devicealbumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/devicealbumcovers"_s;
case Source::LocalFile:
case Source::CDDA:
case Source::Stream:
case Source::SomaFM:
case Source::RadioParadise:
case Source::Unknown:
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + u"/albumcovers"_s;
return StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + u"/albumcovers"_s;
}
return QString();

View File

@@ -0,0 +1,82 @@
/*
* Strawberry Music Player
* Copyright 2025, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <QtGlobal>
#include <QString>
#include <QDir>
#include <QCoreApplication>
#include "standardpaths.h"
using namespace Qt::StringLiterals;
void StandardPaths::AppendOrganizationAndApplication(QString &path) {
const QString organization_name = QCoreApplication::organizationName().toLower();
if (!organization_name.isEmpty()) {
path += u'/' + organization_name;
}
const QString application_name = QCoreApplication::applicationName().toLower();
if (!application_name.isEmpty()) {
path += u'/' + application_name;
}
}
QString StandardPaths::WritableLocation(const StandardLocation type) {
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
switch (type) {
case CacheLocation:
case GenericCacheLocation:{
QString cache_location = qEnvironmentVariable("XDG_CACHE_HOME");
if (!cache_location.startsWith(u'/')) {
cache_location.clear();
}
if (cache_location.isEmpty()) {
cache_location = QDir::homePath() + "/.cache"_L1;
}
if (type == QStandardPaths::CacheLocation) {
AppendOrganizationAndApplication(cache_location);
}
return cache_location;
}
case AppDataLocation:
case AppLocalDataLocation:
case GenericDataLocation:{
QString data_location = qEnvironmentVariable("XDG_DATA_HOME");
if (!data_location.startsWith(u'/')) {
data_location.clear();
}
if (data_location.isEmpty()) {
data_location = QDir::homePath() + "/.local/share"_L1;
}
if (type == AppDataLocation || type == AppLocalDataLocation) {
AppendOrganizationAndApplication(data_location);
}
return data_location;
}
default:
break;
}
#endif
return QStandardPaths::writableLocation(type);
}

36
src/core/standardpaths.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* Strawberry Music Player
* Copyright 2025, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef STANDARDPATHS_H
#define STANDARDPATHS_H
#include <QStandardPaths>
class StandardPaths : public QStandardPaths {
Q_GADGET
public:
static QString WritableLocation(const StandardLocation type);
static QString writableLocation(const StandardLocation type) = delete;
private:
static void AppendOrganizationAndApplication(QString &path);
};
#endif // STANDARDPATHS_H

View File

@@ -24,7 +24,6 @@
#include <QtGlobal>
#include <QObject>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QIODevice>
#include <QMutex>
#include <QNetworkDiskCache>
@@ -32,6 +31,7 @@
#include <QAbstractNetworkCache>
#include <QUrl>
#include "standardpaths.h"
#include "threadsafenetworkdiskcache.h"
using namespace Qt::Literals::StringLiterals;
@@ -48,9 +48,9 @@ ThreadSafeNetworkDiskCache::ThreadSafeNetworkDiskCache(QObject *parent) : QAbstr
if (!sCache) {
sCache = new QNetworkDiskCache;
#ifdef Q_OS_WIN32
sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + u"/strawberry/networkcache"_s);
sCache->setCacheDirectory(StandardPaths::WritableLocation(StandardPaths::TempLocation) + u"/strawberry/networkcache"_s);
#else
sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u"/networkcache"_s);
sCache->setCacheDirectory(StandardPaths::WritableLocation(StandardPaths::CacheLocation) + u"/networkcache"_s);
#endif
}

View File

@@ -27,9 +27,9 @@
#include <QObject>
#include <QDir>
#include <QImage>
#include <QStandardPaths>
#include "core/logging.h"
#include "core/standardpaths.h"
#include "core/song.h"
#include "core/temporaryfile.h"
#include "albumcoverloader.h"
@@ -42,7 +42,7 @@ using namespace Qt::Literals::StringLiterals;
CurrentAlbumCoverLoader::CurrentAlbumCoverLoader(const SharedPtr<AlbumCoverLoader> albumcover_loader, QObject *parent)
: QObject(parent),
albumcover_loader_(albumcover_loader),
temp_file_pattern_(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + u"/strawberry-cover-XXXXXX.jpg"_s),
temp_file_pattern_(StandardPaths::WritableLocation(StandardPaths::TempLocation) + u"/strawberry-cover-XXXXXX.jpg"_s),
id_(0) {
setObjectName(QLatin1String(metaObject()->className()));

View File

@@ -36,10 +36,10 @@
#include <QString>
#include <QUrl>
#include <QImage>
#include <QStandardPaths>
#include "includes/shared_ptr.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "core/temporaryfile.h"
#include "core/taskmanager.h"
#include "core/database.h"
@@ -211,9 +211,9 @@ bool GPodDevice::CopyToStorage(const CopyJob &job, QString &error_text) {
bool result = false;
if (!job.cover_image_.isNull()) {
#ifdef Q_OS_LINUX
QString temp_path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u"/organize"_s;
QString temp_path = StandardPaths::WritableLocation(StandardPaths::CacheLocation) + u"/organize"_s;
#else
QString temp_path = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
QString temp_path = StandardPaths::WritableLocation(StandardPaths::TempLocation);
#endif
if (!QDir(temp_path).exists()) QDir().mkpath(temp_path);
SharedPtr<TemporaryFile> cover_file = make_shared<TemporaryFile>(temp_path + u"/track-albumcover-XXXXXX.jpg"_s);

View File

@@ -26,12 +26,12 @@
#include <gst/pbutils/pbutils.h>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QString>
#include <QDir>
#include <QFile>
#include "core/logging.h"
#include "core/standardpaths.h"
#include "utilities/envutils.h"
#ifdef HAVE_MOODBAR
@@ -145,7 +145,7 @@ void SetEnvironment() {
#endif // USE_BUNDLE
#if defined(Q_OS_WIN32) || defined(Q_OS_MACOS)
QString gst_registry_filename = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QStringLiteral("/gst-registry-%1-bin").arg(QCoreApplication::applicationVersion());
QString gst_registry_filename = StandardPaths::WritableLocation(StandardPaths::AppLocalDataLocation) + QStringLiteral("/gst-registry-%1-bin").arg(QCoreApplication::applicationVersion());
qLog(Debug) << "Setting GStreamer registry file to" << gst_registry_filename;
Utilities::SetEnv("GST_REGISTRY", gst_registry_filename);
#endif

View File

@@ -48,7 +48,6 @@
#include <QApplication>
#include <QCoreApplication>
#include <QSysInfo>
#include <QStandardPaths>
#include <QLibraryInfo>
#include <QFileDevice>
#include <QIODevice>
@@ -70,6 +69,7 @@
#include "includes/shared_ptr.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "core/settings.h"
#include "utilities/envutils.h"
@@ -131,13 +131,8 @@ int main(int argc, char *argv[]) {
mac::MacMain();
#endif
#if defined(Q_OS_WIN32) || defined(Q_OS_MACOS)
QCoreApplication::setApplicationName(u"Strawberry"_s);
QCoreApplication::setOrganizationName(u"Strawberry"_s);
#else
QCoreApplication::setApplicationName(u"strawberry"_s);
QCoreApplication::setOrganizationName(u"strawberry"_s);
#endif
QCoreApplication::setApplicationVersion(QStringLiteral(STRAWBERRY_VERSION_DISPLAY));
QCoreApplication::setOrganizationDomain(u"strawberrymusicplayer.org"_s);
@@ -174,7 +169,7 @@ int main(int argc, char *argv[]) {
#ifdef Q_OS_MACOS
// Must happen after QCoreApplication::setOrganizationName().
Utilities::SetEnv("XDG_CONFIG_HOME", QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
Utilities::SetEnv("XDG_CONFIG_HOME", StandardPaths::WritableLocation(StandardPaths::AppConfigLocation));
#endif
// Output the version, so when people attach log output to bug reports they don't have to tell us which version they're using.

View File

@@ -28,7 +28,6 @@
#include <QObject>
#include <QThread>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QIODevice>
#include <QDir>
#include <QFile>
@@ -43,6 +42,7 @@
#include "includes/scoped_ptr.h"
#include "includes/shared_ptr.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "core/settings.h"
#include "moodbarpipeline.h"
@@ -64,7 +64,7 @@ MoodbarLoader::MoodbarLoader(QObject *parent)
kMaxActiveRequests(qMax(1, QThread::idealThreadCount() / 2)),
save_(false) {
cache_->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u"/moodbar"_s);
cache_->setCacheDirectory(StandardPaths::WritableLocation(StandardPaths::CacheLocation) + u"/moodbar"_s);
cache_->setMaximumCacheSize(60LL * 1024LL * 1024LL); // 60MB - enough for 20,000 moodbars
ReloadSettings();

View File

@@ -25,7 +25,6 @@
#include <memory>
#include <QObject>
#include <QStandardPaths>
#include <QString>
#include <QFile>
#include <QIODevice>
@@ -38,6 +37,7 @@
#include "core/song.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "scrobblercache.h"
#include "scrobblercacheitem.h"
@@ -49,7 +49,7 @@ using std::make_shared;
ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent)
: QObject(parent),
timer_flush_(new QTimer(this)),
filename_(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') + filename),
filename_(StandardPaths::WritableLocation(StandardPaths::CacheLocation) + QLatin1Char('/') + filename),
loaded_(false) {
ReadCache();

View File

@@ -37,7 +37,6 @@
#include <QSpinBox>
#include <QComboBox>
#include <QGroupBox>
#include <QStandardPaths>
#include "constants/behavioursettings.h"
#include "core/iconloader.h"

View File

@@ -24,7 +24,6 @@
#include <utility>
#include <limits>
#include <QStandardPaths>
#include <QAbstractItemModel>
#include <QItemSelectionModel>
#include <QString>
@@ -47,6 +46,7 @@
#include "constants/filesystemconstants.h"
#include "core/iconloader.h"
#include "core/standardpaths.h"
#include "core/settings.h"
#include "utilities/strutils.h"
#include "collection/collectionlibrary.h"
@@ -246,7 +246,7 @@ void CollectionSettingsPage::AddDirectory() {
Settings s;
s.beginGroup(kSettingsGroup);
QString path = s.value(kLastPath, QStandardPaths::writableLocation(QStandardPaths::MusicLocation)).toString();
QString path = s.value(kLastPath, StandardPaths::WritableLocation(StandardPaths::MusicLocation)).toString();
path = QDir::cleanPath(QFileDialog::getExistingDirectory(this, tr("Add directory..."), path));
if (!path.isEmpty()) {

View File

@@ -32,7 +32,6 @@
#include <QThread>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QByteArray>
#include <QDir>
#include <QFileInfo>
@@ -44,6 +43,7 @@
#include "includes/shared_ptr.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "core/signalchecker.h"
#include "core/settings.h"
#include "transcoder.h"
@@ -283,7 +283,7 @@ QString Transcoder::GetFile(const QString &input, const TranscoderPreset &preset
if (!fileinfo_output.isFile() || fileinfo_output.filePath().isEmpty() || fileinfo_output.path().isEmpty() || fileinfo_output.fileName().isEmpty() || fileinfo_output.suffix().isEmpty()) {
QFileInfo fileinfo_input(input);
QString temp_dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/transcoder"_L1;
QString temp_dir = StandardPaths::WritableLocation(StandardPaths::CacheLocation) + "/transcoder"_L1;
if (!QDir(temp_dir).exists()) QDir().mkpath(temp_dir);
QString filename = fileinfo_input.completeBaseName() + QLatin1Char('.') + preset.extension_;
fileinfo_output.setFile(temp_dir + QLatin1Char('/') + filename);

View File

@@ -23,13 +23,13 @@
#include <QUrl>
#include <QFileInfo>
#include <QDir>
#include <QStandardPaths>
#include <QCryptographicHash>
#include "constants/filenameconstants.h"
#include "core/logging.h"
#include "core/standardpaths.h"
#include "transliterate.h"
#include "coverutils.h"
#include "core/logging.h"
using namespace Qt::Literals::StringLiterals;
@@ -85,7 +85,7 @@ QString CoverUtils::CoverFilePath(const CoverOptions &options, const Song::Sourc
QDir dir;
if (!QFileInfo::exists(path) && !dir.mkpath(path)) {
qLog(Error) << "Unable to create directory" << path;
path = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
path = StandardPaths::WritableLocation(StandardPaths::TempLocation);
}
QString filename;