syncthingtray/syncthingconnector/syncthingconfig.cpp

105 lines
3.3 KiB
C++
Raw 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 <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.
*/
QString SyncthingConfig::locateConfigFile()
{
auto path = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SYNCTHING_CONFIG_DIR");
if (!path.isEmpty()) {
if (!QFile::exists(path += QStringLiteral("/config.xml"))) {
path.clear();
}
return path;
}
path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("syncthing/config.xml"));
2017-05-01 03:34:43 +02:00
if (path.isEmpty()) {
2016-09-01 16:34:30 +02:00
path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("Syncthing/config.xml"));
2019-07-18 05:07:37 +02:00
}
// The default Syncthing config path on macOS. The full path is "$HOME/Library/Application Support/Syncthing/config.xml".
if (path.isEmpty()) {
path = QStandardPaths::locate(QStandardPaths::RuntimeLocation, QStringLiteral("Syncthing/config.xml"));
2016-09-01 16:34:30 +02:00
}
return path;
}
QString SyncthingConfig::locateHttpsCertificate()
{
QString path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("syncthing/https-cert.pem"));
2017-05-01 03:34:43 +02:00
if (path.isEmpty()) {
2016-09-01 16:34:30 +02:00
path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("Syncthing/https-cert.pem"));
}
return path;
}
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