Avoid duplicated code in functions for locating config files

This commit is contained in:
Martchus 2023-09-04 20:23:32 +02:00
parent bd924330ad
commit b7d19ea904
2 changed files with 29 additions and 14 deletions

View File

@ -7,6 +7,7 @@
#include <QFile> #include <QFile>
#include <QStandardPaths> #include <QStandardPaths>
#include <QStringBuilder>
#include <QXmlStreamReader> #include <QXmlStreamReader>
namespace Data { namespace Data {
@ -17,33 +18,46 @@ namespace Data {
* \remarks Only a few fields are required since most of the Syncthing config can be accessed via SyncthingConnection class. * \remarks Only a few fields are required since most of the Syncthing config can be accessed via SyncthingConnection class.
*/ */
QString SyncthingConfig::locateConfigFile() /*!
* \brief Locates the file with the specified \a fileName in Syncthing's config directory.
* \remarks The lookup within QStandardPaths::RuntimeLocation is mainly for macOS where the full path
* is "$HOME/Library/Application Support/Syncthing/config.xml".
*/
QString SyncthingConfig::locateConfigFile(const QString &fileName)
{ {
auto path = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SYNCTHING_CONFIG_DIR"); auto path = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SYNCTHING_CONFIG_DIR");
if (!path.isEmpty()) { if (!path.isEmpty()) {
if (!QFile::exists(path += QStringLiteral("/config.xml"))) { if (!QFile::exists(path = path % QChar('/') % fileName)) {
path.clear(); path.clear();
} }
return path; return path;
} }
path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("syncthing/config.xml")); static const QString casings[] = { QStringLiteral("syncthing/"), QStringLiteral("Syncthing/") };
if (path.isEmpty()) { static const QStandardPaths::StandardLocation locations[] = { QStandardPaths::GenericConfigLocation, QStandardPaths::RuntimeLocation };
path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("Syncthing/config.xml")); for (const auto location : locations) {
} for (const auto &casing : casings) {
// The default Syncthing config path on macOS. The full path is "$HOME/Library/Application Support/Syncthing/config.xml". if (!(path = QStandardPaths::locate(location, casing + fileName)).isEmpty()) {
if (path.isEmpty()) { return path;
path = QStandardPaths::locate(QStandardPaths::RuntimeLocation, QStringLiteral("Syncthing/config.xml")); }
}
} }
return path; return path;
} }
/*!
* \brief Locates Syncthing's main config file ("config.xml").
*/
QString SyncthingConfig::locateConfigFile()
{
return locateConfigFile(QStringLiteral("config.xml"));
}
/*!
* \brief Locates Syncthing's GUI HTTPS certificate.
*/
QString SyncthingConfig::locateHttpsCertificate() QString SyncthingConfig::locateHttpsCertificate()
{ {
QString path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("syncthing/https-cert.pem")); return locateConfigFile(QStringLiteral("https-cert.pem"));
if (path.isEmpty()) {
path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("Syncthing/https-cert.pem"));
}
return path;
} }
bool SyncthingConfig::restore(const QString &configFilePath) bool SyncthingConfig::restore(const QString &configFilePath)

View File

@ -16,6 +16,7 @@ struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingConfig {
QString guiPasswordHash; QString guiPasswordHash;
QString guiApiKey; QString guiApiKey;
static QString locateConfigFile(const QString &fileName);
static QString locateConfigFile(); static QString locateConfigFile();
static QString locateHttpsCertificate(); static QString locateHttpsCertificate();
bool restore(const QString &configFilePath); bool restore(const QString &configFilePath);