Add `getSettings()` to streamline settings locations in my apps
* Keep handling for old location of settings file * Add handling to allow portable settings (see https://github.com/Martchus/tageditor/issues/88)
This commit is contained in:
parent
d08ce3781d
commit
e3e2d24aeb
|
@ -326,6 +326,38 @@ void setupCommonQtApplicationAttributes()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Returns the settings object for the specified \a organization and \a application.
|
||||||
|
* \remarks
|
||||||
|
* - This function always uses INI as that's what I'd like to use in all of my applications consistently, regardless of the platform.
|
||||||
|
* - The parameter \a application might be empty. In fact, most of my applications use just `getSettings(QStringLiteral(PROJECT_NAME))`.
|
||||||
|
* - This function first checks whether a file called `$organization/$application.ini` exists in the current working directory (or just
|
||||||
|
* `$organization.ini` if \a application is empty) and uses that if it exists. That allows having a portable installation.
|
||||||
|
* - Some of my apps where using values from QCoreApplication for \a organization and \a application in the beginning. This function
|
||||||
|
* moves those old config files to their new location if needed. This extra handling will likely removed at some point. Note that
|
||||||
|
* I moved away from using values from QCoreApplication to avoid having spaces and additional config suffixes in the file name.
|
||||||
|
*/
|
||||||
|
std::unique_ptr<QSettings> getSettings(const QString &organization, const QString &application)
|
||||||
|
{
|
||||||
|
auto settings = std::unique_ptr<QSettings>();
|
||||||
|
if (const auto portableFile
|
||||||
|
= QFile(application.isEmpty() ? organization + QStringLiteral(".ini") : organization % QChar('/') % application % QStringLiteral(".ini"));
|
||||||
|
portableFile.exists()) {
|
||||||
|
settings = std::make_unique<QSettings>(portableFile.fileName(), QSettings::IniFormat);
|
||||||
|
} else {
|
||||||
|
settings = std::make_unique<QSettings>(QSettings::IniFormat, QSettings::UserScope, organization, application);
|
||||||
|
// move config created by older versions to new location
|
||||||
|
if (organization != QCoreApplication::organizationName() || application != QCoreApplication::applicationName()) {
|
||||||
|
const auto oldConfig
|
||||||
|
= QSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName())
|
||||||
|
.fileName();
|
||||||
|
QFile::rename(oldConfig, settings->fileName()) || QFile::remove(oldConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
settings->sync();
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
// namespace ApplicationInstances
|
// namespace ApplicationInstances
|
||||||
|
|
||||||
} // namespace QtUtilities
|
} // namespace QtUtilities
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
|
|
||||||
#include "../global.h"
|
#include "../global.h"
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
#include <QtContainerFwd>
|
#include <QtContainerFwd>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
QT_FORWARD_DECLARE_CLASS(QString)
|
QT_FORWARD_DECLARE_CLASS(QString)
|
||||||
QT_FORWARD_DECLARE_CLASS(QSettings)
|
QT_FORWARD_DECLARE_CLASS(QSettings)
|
||||||
|
@ -65,6 +67,7 @@ QT_UTILITIES_EXPORT bool hasCoreApp();
|
||||||
} // namespace ApplicationInstances
|
} // namespace ApplicationInstances
|
||||||
|
|
||||||
QT_UTILITIES_EXPORT void setupCommonQtApplicationAttributes();
|
QT_UTILITIES_EXPORT void setupCommonQtApplicationAttributes();
|
||||||
|
QT_UTILITIES_EXPORT std::unique_ptr<QSettings> getSettings(const QString &organization, const QString &application = QString());
|
||||||
|
|
||||||
} // namespace QtUtilities
|
} // namespace QtUtilities
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue