syncthingtray/connector/syncthingconfig.cpp

92 lines
2.9 KiB
C++
Raw Normal View History

2018-04-08 21:53:23 +02:00
#include "./syncthingconfig.h"
2016-09-01 16:34:30 +02:00
#include <QFile>
#include <QHostAddress>
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()
{
QString 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"));
}
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
{
2017-05-01 03:34:43 +02:00
return (guiEnforcesSecureConnection || !QHostAddress(guiAddress.mid(0, guiAddress.indexOf(QChar(':')))).isLoopback() ? QStringLiteral("https://")
: QStringLiteral("http://"))
+ guiAddress;
}
2016-09-01 16:34:30 +02:00
} // namespace Data