syncthingtray/syncthingconnector/syncthingconfig.cpp

138 lines
4.2 KiB
C++
Raw Permalink Normal View History

2018-04-08 21:53:23 +02:00
#include "./syncthingconfig.h"
#include "./utils.h"
2016-09-01 16:34:30 +02:00
#include "resources/config.h"
#include <qtutilities/misc/compat.h>
2016-09-01 16:34:30 +02:00
#include <QFile>
2017-05-01 03:34:43 +02:00
#include <QStandardPaths>
#include <QStringBuilder>
2017-05-01 03:34:43 +02:00
#include <QXmlStreamReader>
2016-09-01 16:34:30 +02:00
namespace Data {
/*!
* \struct SyncthingConfig
2016-09-03 20:14:52 +02:00
* \brief The SyncthingConfig struct holds the configuration of the local Syncthing instance read from config.xml in the Syncthing home directory.
2016-09-01 16:34:30 +02:00
* \remarks Only a few fields are required since most of the Syncthing config can be accessed via SyncthingConnection class.
*/
/*!
* \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)
2016-09-01 16:34:30 +02:00
{
// check override via environment variable
auto path = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SYNCTHING_CONFIG_DIR");
if (!path.isEmpty()) {
if (!QFile::exists(path = path % QChar('/') % fileName)) {
path.clear();
}
return path;
}
// check usual standard locations
static const QString casings[] = { QStringLiteral("syncthing/"), QStringLiteral("Syncthing/") };
static const QStandardPaths::StandardLocation locations[] = { QStandardPaths::GenericConfigLocation, QStandardPaths::RuntimeLocation };
for (const auto location : locations) {
for (const auto &casing : casings) {
if (!(path = QStandardPaths::locate(location, casing + fileName)).isEmpty()) {
return path;
}
}
2016-09-01 16:34:30 +02:00
}
// check state dir used by Syncthing on Unix systems as of v1.27.0 (commit b5082f6af8b0a70afd3bc42977dad26920e72b68)
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
if (!(path = qEnvironmentVariable("XDG_STATE_HOME")).isEmpty()) {
if (QFile::exists(path = path % QStringLiteral("/syncthing/") % fileName)) {
return path;
}
}
if (!(path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation)).isEmpty()) {
if (QFile::exists(path = path % QStringLiteral("/.local/state/syncthing/") % fileName)) {
return path;
}
}
#endif
path.clear();
2016-09-01 16:34:30 +02:00
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.
*/
2016-09-01 16:34:30 +02:00
QString SyncthingConfig::locateHttpsCertificate()
{
return locateConfigFile(QStringLiteral("https-cert.pem"));
2016-09-01 16:34:30 +02:00
}
bool SyncthingConfig::restore(const QString &configFilePath)
{
QFile configFile(configFilePath);
2017-05-01 03:34:43 +02:00
if (!configFile.open(QFile::ReadOnly)) {
2016-09-01 16:34:30 +02:00
return false;
}
QXmlStreamReader xmlReader(&configFile);
bool ok = false;
#include <qtutilities/misc/xmlparsermacros.h>
2017-05-01 03:34:43 +02:00
children
{
2016-09-01 16:34:30 +02:00
// only version 16 supported, try to parse other versions anyways since the changes might not affect
// the few parts read here
version = attribute("version").toString();
2017-05-01 03:34:43 +02:00
children
{
iftag("gui")
{
2016-09-01 16:34:30 +02:00
ok = true;
guiEnabled = attributeFlag("enabled");
guiEnforcesSecureConnection = attributeFlag("tls");
2017-05-01 03:34:43 +02:00
children
{
iftag("address")
{
2016-09-01 16:34:30 +02:00
guiAddress = text;
2017-05-01 03:34:43 +02:00
}
eliftag("user")
{
2016-09-01 16:34:30 +02:00
guiUser = text;
2017-05-01 03:34:43 +02:00
}
eliftag("password")
{
2016-09-01 16:34:30 +02:00
guiPasswordHash = text;
2017-05-01 03:34:43 +02:00
}
eliftag("apikey")
{
2016-09-01 16:34:30 +02:00
guiApiKey = text;
2017-05-01 03:34:43 +02:00
}
else_skip
2016-09-01 16:34:30 +02:00
}
2017-05-01 03:34:43 +02:00
}
else_skip
2016-09-01 16:34:30 +02:00
}
}
#include <qtutilities/misc/undefxmlparsermacros.h>
return ok;
}
QString SyncthingConfig::syncthingUrl() const
{
return (guiEnforcesSecureConnection || !isLocal(stripPort(guiAddress)) ? QStringLiteral("https://") : QStringLiteral("http://")) + guiAddress;
}
2016-09-01 16:34:30 +02:00
} // namespace Data