Merge branch 'release/2.6.2' into develop

This commit is contained in:
Jonathan White 2020-09-27 12:05:33 -04:00
commit e1c2537084
No known key found for this signature in database
GPG key ID: 440FC65F2E0C6E01
32 changed files with 1109 additions and 851 deletions

View file

@ -22,6 +22,7 @@
#include <QCoreApplication>
#include <QDir>
#include <QHash>
#include <QProcessEnvironment>
#include <QSettings>
#include <QSize>
#include <QStandardPaths>
@ -419,49 +420,17 @@ void Config::migrate()
sync();
}
Config::Config(const QString& fileName, QObject* parent)
Config::Config(const QString& configFileName, const QString& localConfigFileName, QObject* parent)
: QObject(parent)
{
init(fileName);
init(configFileName, localConfigFileName);
}
Config::Config(QObject* parent)
: QObject(parent)
{
// Check if we are running in portable mode, if so store the config files local to the app
auto portablePath = QCoreApplication::applicationDirPath().append("/%1");
if (QFile::exists(portablePath.arg(".portable"))) {
init(portablePath.arg("config/keepassxc.ini"), portablePath.arg("config/keepassxc_local.ini"));
return;
}
QString configPath;
QString localConfigPath;
#if defined(Q_OS_WIN)
configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
localConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
#elif defined(Q_OS_MACOS)
configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
localConfigPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
#else
// On case-sensitive Operating Systems, force use of lowercase app directories
configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/keepassxc";
localConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/keepassxc";
#endif
configPath += "/keepassxc";
localConfigPath += "/keepassxc";
#ifdef QT_DEBUG
configPath += "_debug";
localConfigPath += "_debug";
#endif
configPath += ".ini";
localConfigPath += ".ini";
init(QDir::toNativeSeparators(configPath), QDir::toNativeSeparators(localConfigPath));
auto configFiles = defaultConfigFiles();
init(configFiles.first, configFiles.second);
}
Config::~Config()
@ -489,6 +458,45 @@ void Config::init(const QString& configFileName, const QString& localConfigFileN
connect(qApp, &QCoreApplication::aboutToQuit, this, &Config::sync);
}
QPair<QString, QString> Config::defaultConfigFiles()
{
// Check if we are running in portable mode, if so store the config files local to the app
auto portablePath = QCoreApplication::applicationDirPath().append("/%1");
if (QFile::exists(portablePath.arg(".portable"))) {
return {portablePath.arg("config/keepassxc.ini"), portablePath.arg("config/keepassxc_local.ini")};
}
QString configPath;
QString localConfigPath;
#if defined(Q_OS_WIN)
configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
localConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
#elif defined(Q_OS_MACOS)
configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
localConfigPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
#else
// On case-sensitive Operating Systems, force use of lowercase app directories
configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/keepassxc";
localConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/keepassxc";
#endif
QString suffix;
#ifdef QT_DEBUG
suffix = "_debug";
#endif
configPath += QString("/keepassxc%1.ini").arg(suffix);
localConfigPath += QString("/keepassxc%1.ini").arg(suffix);
// Allow overriding the default location with env vars
const auto& env = QProcessEnvironment::systemEnvironment();
configPath = env.value("KPXC_CONFIG", configPath);
localConfigPath = env.value("KPXC_CONFIG_LOCAL", localConfigPath);
return {QDir::toNativeSeparators(configPath), QDir::toNativeSeparators(localConfigPath)};
}
Config* Config::instance()
{
if (!m_instance) {
@ -498,12 +506,16 @@ Config* Config::instance()
return m_instance;
}
void Config::createConfigFromFile(const QString& file)
void Config::createConfigFromFile(const QString& configFileName, const QString& localConfigFileName)
{
if (m_instance) {
delete m_instance;
}
m_instance = new Config(file, qApp);
auto defaultFiles = defaultConfigFiles();
m_instance = new Config(configFileName.isEmpty() ? defaultFiles.first : configFileName,
localConfigFileName.isEmpty() ? defaultFiles.second : localConfigFileName,
qApp);
}
void Config::createTempFileInstance()
@ -515,7 +527,7 @@ void Config::createTempFileInstance()
bool openResult = tmpFile->open();
Q_ASSERT(openResult);
Q_UNUSED(openResult);
m_instance = new Config(tmpFile->fileName(), qApp);
m_instance = new Config(tmpFile->fileName(), "", qApp);
tmpFile->setParent(m_instance);
}