From 0961720835ac53353abd724b7261ce6f140a06c8 Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 27 Jan 2023 01:07:15 +0100 Subject: [PATCH] Join path components using path::operator/ instead of string::operator+ --- apps/launcher/maindialog.cpp | 22 ++++++++---------- apps/wizard/mainwizard.cpp | 19 ++++++++-------- components/CMakeLists.txt | 1 + components/files/configurationmanager.cpp | 2 -- components/files/configurationmanager.hpp | 2 ++ components/files/qtconfigpath.hpp | 27 +++++++++++++++++++++++ 6 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 components/files/qtconfigpath.hpp diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index 4faef75cb0..44c8a30be2 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -12,7 +12,9 @@ #include #include + #include +#include #include #include @@ -286,11 +288,9 @@ bool Launcher::MainDialog::setupLauncherSettings() mLauncherSettings.setMultiValueEnabled(true); - const auto userPath = Files::pathToQString(mCfgMgr.getUserConfigPath()); - QStringList paths; paths.append(QString(Config::LauncherSettings::sLauncherConfigFileName)); - paths.append(userPath + QString(Config::LauncherSettings::sLauncherConfigFileName)); + paths.append(Files::pathToQString(mCfgMgr.getUserConfigPath() / Config::LauncherSettings::sLauncherConfigFileName)); for (const QString& path : paths) { @@ -322,10 +322,6 @@ bool Launcher::MainDialog::setupGameSettings() { mGameSettings.clear(); - const auto localPath = Files::pathToQString(mCfgMgr.getLocalPath()); - const auto userPath = Files::pathToQString(mCfgMgr.getUserConfigPath()); - const auto globalPath = Files::pathToQString(mCfgMgr.getGlobalPath()); - QFile file; auto loadFile = [&](const QString& path, bool (Config::GameSettings::*reader)(QTextStream&, bool), @@ -355,19 +351,19 @@ bool Launcher::MainDialog::setupGameSettings() // Load the user config file first, separately // So we can write it properly, uncontaminated - if (!loadFile(userPath + QLatin1String("openmw.cfg"), &Config::GameSettings::readUserFile)) + if (!loadFile(Files::getUserConfigPathQString(mCfgMgr), &Config::GameSettings::readUserFile)) return false; // Now the rest - priority: user > local > global - if (auto result = loadFile(localPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true)) + if (auto result = loadFile(Files::getLocalConfigPathQString(mCfgMgr), &Config::GameSettings::readFile, true)) { // Load global if local wasn't found - if (!*result && !loadFile(globalPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true)) + if (!*result && !loadFile(Files::getGlobalConfigPathQString(mCfgMgr), &Config::GameSettings::readFile, true)) return false; } else return false; - if (!loadFile(userPath + QString("openmw.cfg"), &Config::GameSettings::readFile)) + if (!loadFile(Files::getUserConfigPathQString(mCfgMgr), &Config::GameSettings::readFile)) return false; return true; @@ -495,9 +491,9 @@ bool Launcher::MainDialog::writeSettings() // Game settings #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - QFile file(userPath / "openmw.cfg"); + QFile file(userPath / Files::openmwCfgFile); #else - QFile file(Files::pathToQString(userPath / "openmw.cfg")); + QFile file(Files::getUserConfigPathQString(mCfgMgr)); #endif if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) diff --git a/apps/wizard/mainwizard.cpp b/apps/wizard/mainwizard.cpp index 69bf7536b4..c0fd1b16e1 100644 --- a/apps/wizard/mainwizard.cpp +++ b/apps/wizard/mainwizard.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -137,8 +138,6 @@ void Wizard::MainWizard::addLogText(const QString& text) void Wizard::MainWizard::setupGameSettings() { - QString userPath(Files::pathToQString(mCfgMgr.getUserConfigPath())); - QString globalPath(Files::pathToQString(mCfgMgr.getGlobalPath())); QString message( tr("

Could not open %1 for reading

\

Please make sure you have the right permissions \ @@ -146,7 +145,7 @@ void Wizard::MainWizard::setupGameSettings() // Load the user config file first, separately // So we can write it properly, uncontaminated - QString path(userPath + QLatin1String("openmw.cfg")); + QString path(Files::getUserConfigPathQString(mCfgMgr)); QFile file(path); qDebug() << "Loading config file:" << path.toUtf8().constData(); @@ -174,9 +173,9 @@ void Wizard::MainWizard::setupGameSettings() // Now the rest QStringList paths; - paths.append(userPath + QLatin1String("openmw.cfg")); + paths.append(Files::getUserConfigPathQString(mCfgMgr)); paths.append(QLatin1String("openmw.cfg")); - paths.append(globalPath + QLatin1String("openmw.cfg")); + paths.append(Files::getGlobalConfigPathQString(mCfgMgr)); for (const QString& path2 : paths) { @@ -258,8 +257,7 @@ void Wizard::MainWizard::runSettingsImporter() QString path(field(QLatin1String("installation.path")).toString()); - QString userPath(Files::pathToQString(mCfgMgr.getUserConfigPath())); - QFile file(userPath + QLatin1String("openmw.cfg")); + QFile file(Files::getUserConfigPathQString(mCfgMgr)); // Construct the arguments to run the importer QStringList arguments; @@ -304,7 +302,7 @@ void Wizard::MainWizard::runSettingsImporter() } arguments.append(QLatin1String("--cfg")); - arguments.append(userPath + QLatin1String("openmw.cfg")); + arguments.append(Files::getUserConfigPathQString(mCfgMgr)); if (!mImporterInvoker->startProcess(QLatin1String("openmw-iniimporter"), arguments, false)) return qApp->quit(); @@ -440,7 +438,7 @@ void Wizard::MainWizard::writeSettings() } // Game settings - QFile file(userPath + QLatin1String("openmw.cfg")); + QFile file(Files::getUserConfigPathQString(mCfgMgr)); if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) { @@ -466,7 +464,8 @@ void Wizard::MainWizard::writeSettings() file.close(); // Launcher settings - file.setFileName(userPath + QLatin1String(Config::LauncherSettings::sLauncherConfigFileName)); + file.setFileName( + Files::pathToQString(mCfgMgr.getUserConfigPath() / Config::LauncherSettings::sLauncherConfigFileName)); if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) { diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index f863f246d0..405c36b18e 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -383,6 +383,7 @@ if (USE_QT) add_component_qt_dir (files qtconversion + qtconfigpath ) QT5_WRAP_UI(ESM_UI_HDR ${ESM_UI}) diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 19a25dc702..40dbeb3526 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -18,8 +18,6 @@ namespace Files namespace bpo = boost::program_options; - static const char* const openmwCfgFile = "openmw.cfg"; - #if defined(_WIN32) || defined(__WINDOWS__) static const char* const applicationName = "OpenMW"; #else diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 49f777b798..8a5bca0869 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -19,6 +20,7 @@ namespace boost::program_options */ namespace Files { + inline constexpr std::string_view openmwCfgFile = "openmw.cfg"; /** * \struct ConfigurationManager diff --git a/components/files/qtconfigpath.hpp b/components/files/qtconfigpath.hpp new file mode 100644 index 0000000000..e6d7b4202c --- /dev/null +++ b/components/files/qtconfigpath.hpp @@ -0,0 +1,27 @@ +#ifndef OPENMW_COMPONENTS_FILES_QTCONFIGPATH_H +#define OPENMW_COMPONENTS_FILES_QTCONFIGPATH_H + +#include "configurationmanager.hpp" +#include "qtconversion.hpp" + +#include + +namespace Files +{ + inline QString getLocalConfigPathQString(const Files::ConfigurationManager& cfgMgr) + { + return Files::pathToQString(cfgMgr.getLocalPath() / openmwCfgFile); + } + + inline QString getUserConfigPathQString(const Files::ConfigurationManager& cfgMgr) + { + return Files::pathToQString(cfgMgr.getUserConfigPath() / openmwCfgFile); + } + + inline QString getGlobalConfigPathQString(const Files::ConfigurationManager& cfgMgr) + { + return Files::pathToQString(cfgMgr.getGlobalPath() / openmwCfgFile); + } +} + +#endif // OPENMW_COMPONENTS_FILES_QTCONFIGPATH_H