mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-06 13:33:55 -07:00
[Settings] Split Paths from SettingsCache into PathSettings
This commit is contained in:
parent
baddbfae14
commit
21f57b1acb
31 changed files with 350 additions and 254 deletions
|
|
@ -34,6 +34,8 @@ add_library(
|
|||
libcockatrice/settings/recents_settings.cpp
|
||||
libcockatrice/settings/servers_settings.cpp
|
||||
libcockatrice/settings/settings_manager.cpp
|
||||
libcockatrice/settings/path_settings.cpp
|
||||
libcockatrice/settings/path_settings.h
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
|
|
|
|||
163
libcockatrice_settings/libcockatrice/settings/path_settings.cpp
Normal file
163
libcockatrice_settings/libcockatrice/settings/path_settings.cpp
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
#include "path_settings.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
||||
PathSettings::PathSettings(const QString &settingPath, QObject *parent)
|
||||
: SettingsManager(settingPath + "global.ini", "paths", QString(), parent)
|
||||
{
|
||||
}
|
||||
|
||||
QString PathSettings::getSafeConfigPath(const QString &configEntry, const QString &defaultPath) const
|
||||
{
|
||||
QString tmp = getValue(configEntry).toString();
|
||||
// if the config settings is empty or refers to a not-existing folder,
|
||||
// ensure that the default path exists and return it
|
||||
if (tmp.isEmpty() || !QDir(tmp).exists()) {
|
||||
if (!QDir().mkpath(defaultPath)) {
|
||||
qCInfo(PathSettingsLog) << "[PathSettings] Could not create folder:" << defaultPath;
|
||||
}
|
||||
tmp = defaultPath;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
QString PathSettings::getSafeConfigFilePath(const QString &configEntry, const QString &defaultPath) const
|
||||
{
|
||||
QString tmp = getValue(configEntry).toString();
|
||||
// if the config settings is empty or refers to a not-existing file,
|
||||
// return the default path
|
||||
if (tmp.isEmpty() || !QFile::exists(tmp)) {
|
||||
tmp = defaultPath;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void PathSettings::recomputeCustomPicsPath()
|
||||
{
|
||||
// this has never been exposed as a user-configurable setting
|
||||
if (picsPath.endsWith("/")) {
|
||||
customPicsPath = getSafeConfigPath("custompics", picsPath + "CUSTOM/");
|
||||
} else {
|
||||
customPicsPath = getSafeConfigPath("custompics", picsPath + "/CUSTOM/");
|
||||
}
|
||||
}
|
||||
|
||||
void PathSettings::load(const QString &dataPath, const QString &cachePath)
|
||||
{
|
||||
deckPath = getSafeConfigPath("decks", dataPath + "/decks/");
|
||||
filtersPath = getSafeConfigPath("filters", dataPath + "/filters/");
|
||||
replaysPath = getSafeConfigPath("replays", dataPath + "/replays/");
|
||||
themesPath = getSafeConfigPath("themes", dataPath + "/themes/");
|
||||
picsPath = getSafeConfigPath("pics", dataPath + "/pics/");
|
||||
redirectCachePath = getSafeConfigPath("redirects", cachePath + "/redirects/");
|
||||
recomputeCustomPicsPath();
|
||||
customCardDatabasePath = getSafeConfigPath("customsets", dataPath + "/customsets/");
|
||||
|
||||
cardDatabasePath = getSafeConfigFilePath("carddatabase", dataPath + "/cards.xml");
|
||||
tokenDatabasePath = getSafeConfigFilePath("tokendatabase", dataPath + "/tokens.xml");
|
||||
spoilerDatabasePath = getSafeConfigFilePath("spoilerdatabase", dataPath + "/spoiler.xml");
|
||||
}
|
||||
|
||||
void PathSettings::reset(const QString &dataPath, const QString &cachePath)
|
||||
{
|
||||
auto settings = getSettings();
|
||||
settings.beginGroup("paths");
|
||||
settings.remove(""); // removes all keys in Path/*
|
||||
settings.endGroup();
|
||||
|
||||
load(dataPath, cachePath);
|
||||
}
|
||||
|
||||
/*void SettingsCache::loadPaths()
|
||||
{
|
||||
QString dataPath = getDataPath();
|
||||
deckPath = getSafeConfigPath("paths/decks", dataPath + "/decks/");
|
||||
filtersPath = getSafeConfigPath("paths/filters", dataPath + "/filters/");
|
||||
replaysPath = getSafeConfigPath("paths/replays", dataPath + "/replays/");
|
||||
themesPath = getSafeConfigPath("paths/themes", dataPath + "/themes/");
|
||||
picsPath = getSafeConfigPath("paths/pics", dataPath + "/pics/");
|
||||
redirectCachePath = getSafeConfigPath("paths/redirects", getCachePath() + "/redirects/");
|
||||
// this has never been exposed as an user-configurable setting
|
||||
if (picsPath.endsWith("/")) {
|
||||
customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "CUSTOM/");
|
||||
} else {
|
||||
customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "/CUSTOM/");
|
||||
}
|
||||
customCardDatabasePath = getSafeConfigPath("paths/customsets", dataPath + "/customsets/");
|
||||
|
||||
cardDatabasePath = getSafeConfigFilePath("paths/carddatabase", dataPath + "/cards.xml");
|
||||
tokenDatabasePath = getSafeConfigFilePath("paths/tokendatabase", dataPath + "/tokens.xml");
|
||||
spoilerDatabasePath = getSafeConfigFilePath("paths/spoilerdatabase", dataPath + "/spoiler.xml");
|
||||
}
|
||||
|
||||
void SettingsCache::resetPaths()
|
||||
{
|
||||
QStringList databasePaths{customCardDatabasePath, cardDatabasePath, spoilerDatabasePath, tokenDatabasePath};
|
||||
QString picsPath_ = picsPath;
|
||||
settings->remove("paths"); // removes all keys in paths/
|
||||
loadPaths();
|
||||
if (databasePaths !=
|
||||
QStringList{customCardDatabasePath, cardDatabasePath, spoilerDatabasePath, tokenDatabasePath}) {
|
||||
emit cardDatabasePathChanged();
|
||||
}
|
||||
if (picsPath_ != picsPath) {
|
||||
emit picsPathChanged();
|
||||
}
|
||||
}*/
|
||||
|
||||
void PathSettings::setDeckPath(const QString &_deckPath)
|
||||
{
|
||||
deckPath = _deckPath;
|
||||
setValue(deckPath, "decks");
|
||||
}
|
||||
|
||||
void PathSettings::setFiltersPath(const QString &_filtersPath)
|
||||
{
|
||||
filtersPath = _filtersPath;
|
||||
setValue(filtersPath, "filters");
|
||||
}
|
||||
|
||||
void PathSettings::setReplaysPath(const QString &_replaysPath)
|
||||
{
|
||||
replaysPath = _replaysPath;
|
||||
setValue(replaysPath, "replays");
|
||||
}
|
||||
|
||||
void PathSettings::setThemesPath(const QString &_themesPath)
|
||||
{
|
||||
themesPath = _themesPath;
|
||||
setValue(themesPath, "themes");
|
||||
}
|
||||
|
||||
void PathSettings::setPicsPath(const QString &_picsPath)
|
||||
{
|
||||
picsPath = _picsPath;
|
||||
setValue(picsPath, "pics");
|
||||
// get a new value for customPicsPath, currently derived from picsPath
|
||||
recomputeCustomPicsPath();
|
||||
}
|
||||
|
||||
void PathSettings::setCustomCardDatabasePath(const QString &_customCardDatabasePath)
|
||||
{
|
||||
customCardDatabasePath = _customCardDatabasePath;
|
||||
setValue(customCardDatabasePath, "customsets");
|
||||
}
|
||||
|
||||
void PathSettings::setCardDatabasePath(const QString &_cardDatabasePath)
|
||||
{
|
||||
cardDatabasePath = _cardDatabasePath;
|
||||
setValue(cardDatabasePath, "carddatabase");
|
||||
}
|
||||
|
||||
void PathSettings::setSpoilerDatabasePath(const QString &_spoilerDatabasePath)
|
||||
{
|
||||
spoilerDatabasePath = _spoilerDatabasePath;
|
||||
setValue(spoilerDatabasePath, "spoilerdatabase");
|
||||
}
|
||||
|
||||
void PathSettings::setTokenDatabasePath(const QString &_tokenDatabasePath)
|
||||
{
|
||||
tokenDatabasePath = _tokenDatabasePath;
|
||||
setValue(tokenDatabasePath, "tokendatabase");
|
||||
}
|
||||
112
libcockatrice_settings/libcockatrice/settings/path_settings.h
Normal file
112
libcockatrice_settings/libcockatrice/settings/path_settings.h
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#ifndef COCKATRICE_PATH_SETTINGS_H
|
||||
#define COCKATRICE_PATH_SETTINGS_H
|
||||
|
||||
/**
|
||||
* @file path_settings.h
|
||||
* @ingroup Settings
|
||||
*/
|
||||
//! \todo Document this file.
|
||||
|
||||
#include "settings_manager.h"
|
||||
#include <libcockatrice/interfaces/interface_card_database_path_provider.h>
|
||||
|
||||
#include <QLoggingCategory>
|
||||
#include <QString>
|
||||
|
||||
inline Q_LOGGING_CATEGORY(PathSettingsLog, "path_settings");
|
||||
|
||||
/**
|
||||
* Owns every user-configurable filesystem path used by the client (decks, filters,
|
||||
* replays, themes, pictures, card databases, and the redirect cache).
|
||||
*
|
||||
* Backed by the "paths" group of the existing global.ini, so extracting this class out
|
||||
* of SettingsCache is a pure code-organization change: no stored user data moves.
|
||||
*/
|
||||
class PathSettings : public SettingsManager, public ICardDatabasePathProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
signals:
|
||||
void cardDatabasePathChanged();
|
||||
|
||||
public:
|
||||
explicit PathSettings(const QString &settingPath, QObject *parent = nullptr);
|
||||
|
||||
[[nodiscard]] QString getDeckPath() const
|
||||
{
|
||||
return deckPath;
|
||||
}
|
||||
[[nodiscard]] QString getFiltersPath() const
|
||||
{
|
||||
return filtersPath;
|
||||
}
|
||||
[[nodiscard]] QString getReplaysPath() const
|
||||
{
|
||||
return replaysPath;
|
||||
}
|
||||
[[nodiscard]] QString getThemesPath() const
|
||||
{
|
||||
return themesPath;
|
||||
}
|
||||
[[nodiscard]] QString getPicsPath() const
|
||||
{
|
||||
return picsPath;
|
||||
}
|
||||
[[nodiscard]] QString getRedirectCachePath() const
|
||||
{
|
||||
return redirectCachePath;
|
||||
}
|
||||
[[nodiscard]] QString getCustomPicsPath() const
|
||||
{
|
||||
return customPicsPath;
|
||||
}
|
||||
[[nodiscard]] QString getCustomCardDatabasePath() const override
|
||||
{
|
||||
return customCardDatabasePath;
|
||||
}
|
||||
[[nodiscard]] QString getCardDatabasePath() const override
|
||||
{
|
||||
return cardDatabasePath;
|
||||
}
|
||||
[[nodiscard]] QString getSpoilerCardDatabasePath() const override
|
||||
{
|
||||
return spoilerDatabasePath;
|
||||
}
|
||||
[[nodiscard]] QString getTokenDatabasePath() const override
|
||||
{
|
||||
return tokenDatabasePath;
|
||||
}
|
||||
|
||||
void setDeckPath(const QString &_deckPath);
|
||||
void setFiltersPath(const QString &_filtersPath);
|
||||
void setReplaysPath(const QString &_replaysPath);
|
||||
void setThemesPath(const QString &_themesPath);
|
||||
void setPicsPath(const QString &_picsPath);
|
||||
void setCustomCardDatabasePath(const QString &_customCardDatabasePath);
|
||||
void setCardDatabasePath(const QString &_cardDatabasePath);
|
||||
void setSpoilerDatabasePath(const QString &_spoilerDatabasePath);
|
||||
void setTokenDatabasePath(const QString &_tokenDatabasePath);
|
||||
|
||||
/**
|
||||
* (Re)computes every path from defaults rooted at dataPath/cachePath, honoring any
|
||||
* user overrides already stored in settings. Must be called once after construction.
|
||||
*/
|
||||
void load(const QString &dataPath, const QString &cachePath);
|
||||
|
||||
/**
|
||||
* Clears all stored path overrides and reloads defaults rooted at dataPath/cachePath.
|
||||
*/
|
||||
void reset(const QString &dataPath, const QString &cachePath);
|
||||
|
||||
private:
|
||||
QString deckPath, filtersPath, replaysPath, picsPath, redirectCachePath, customPicsPath, cardDatabasePath,
|
||||
customCardDatabasePath, themesPath, spoilerDatabasePath, tokenDatabasePath;
|
||||
|
||||
[[nodiscard]] QString getSafeConfigPath(const QString &configEntry, const QString &defaultPath) const;
|
||||
[[nodiscard]] QString getSafeConfigFilePath(const QString &configEntry, const QString &defaultPath) const;
|
||||
|
||||
void recomputeCustomPicsPath();
|
||||
};
|
||||
|
||||
|
||||
#endif //COCKATRICE_PATH_SETTINGS_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue