1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include "./setupdetection.h"
#include "./settingsdialog.h"
// use meta-data of syncthingtray application here
#include "resources/../../tray/resources/config.h"
#include <qtutilities/misc/compat.h>
#if defined(LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD) && (defined(PLATFORM_UNIX) || defined(PLATFORM_MINGW) || defined(PLATFORM_CYGWIN))
#define PLATFORM_HAS_GETLOGIN
#include <unistd.h>
#endif
namespace QtGui {
SetupDetection::SetupDetection(QObject *parent)
: QObject(parent)
{
// assume default service names
const auto defaultUserUnit = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SYSTEMD_USER_UNIT", QStringLiteral("syncthing.service"));
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
userService.setUnitName(defaultUserUnit);
systemService.setUnitName(QStringLiteral("syncthing@") %
#ifdef PLATFORM_HAS_GETLOGIN
QString::fromLocal8Bit(getlogin()) %
#endif
QStringLiteral(".service"));
#endif
// configure launcher to test invocation of "syncthing --version" capturing output
defaultSyncthingArgs = launcherSettings.syncthingArgs;
launcherSettings.syncthingArgs = QStringLiteral("--version");
launcher.setEmittingOutput(true);
// configure timeout
auto hasConfiguredTimeout = false;
auto configuredTimeout = qEnvironmentVariableIntValue(PROJECT_VARNAME_UPPER "_WIZARD_SETUP_DETECTION_TIMEOUT", &hasConfiguredTimeout);
timeout.setInterval(hasConfiguredTimeout ? configuredTimeout : 2500);
timeout.setSingleShot(true);
// connect signals & slots
connect(&connection, &Data::SyncthingConnection::error, this, &SetupDetection::handleConnectionError);
connect(&connection, &Data::SyncthingConnection::statusChanged, this, &SetupDetection::checkDone);
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
connect(&userService, &Data::SyncthingService::unitFileStateChanged, this, &SetupDetection::checkDone);
connect(&systemService, &Data::SyncthingService::unitFileStateChanged, this, &SetupDetection::checkDone);
#endif
connect(&launcher, &Data::SyncthingLauncher::outputAvailable, this, &SetupDetection::handleLauncherOutput);
connect(&launcher, &Data::SyncthingLauncher::exited, this, &SetupDetection::handleLauncherExit);
connect(&launcher, &Data::SyncthingLauncher::errorOccurred, this, &SetupDetection::handleLauncherError);
connect(&timeout, &QTimer::timeout, this, &SetupDetection::handleTimeout);
}
void SetupDetection::determinePaths()
{
configFilePath = Data::SyncthingConfig::locateConfigFile();
#ifndef QT_NO_SSL
certPath = Data::SyncthingConfig::locateHttpsCertificate();
#endif
}
void SetupDetection::restoreConfig()
{
configOk = config.restore(configFilePath);
}
void SetupDetection::initConnection()
{
auto settings = Data::SyncthingConnectionSettings();
settings.syncthingUrl = config.syncthingUrl();
settings.apiKey = config.guiApiKey.toLocal8Bit();
#ifndef QT_NO_SSL
settings.httpsCertPath = certPath;
settings.loadHttpsCert();
#endif
connection.applySettings(settings);
}
bool SetupDetection::hasConfig() const
{
return configOk && !config.guiAddress.isEmpty() && !config.guiApiKey.isEmpty();
}
bool SetupDetection::isDone() const
{
return timedOut
|| (!connection.isConnecting() && (launcherExitCode.has_value() || launcherError.has_value())
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
&& !userService.unitFileState().isEmpty() && !systemService.unitFileState().isEmpty()
#endif
);
}
void SetupDetection::reset()
{
timeout.stop();
timedOut = false;
configOk = false;
autostartEnabled = false;
autostartConfiguredPath.reset();
config.guiAddress.clear();
config.guiApiKey.clear();
connection.disconnect();
launcher.terminate();
connectionErrors.clear();
launcherExitCode.reset();
launcherExitStatus.reset();
launcherError.reset();
launcherOutput.clear();
m_testStarted = false;
}
void SetupDetection::startTest()
{
if (m_testStarted) {
return;
}
m_testStarted = true;
restoreConfig();
initConnection();
connection.reconnect();
launcher.launch(launcherSettings);
autostartConfiguredPath = configuredAutostartPath();
autostartEnabled = autostartConfiguredPath.has_value() ? !autostartConfiguredPath.value().isEmpty() : isAutostartEnabled();
autostartSupposedPath = supposedAutostartPath();
timeout.start();
}
void SetupDetection::handleConnectionError(const QString &error)
{
connectionErrors << error;
}
void SetupDetection::handleLauncherExit(int exitCode, QProcess::ExitStatus exitStatus)
{
launcherExitCode = exitCode;
launcherExitStatus = exitStatus;
checkDone();
}
void SetupDetection::handleLauncherError(QProcess::ProcessError error)
{
launcherError = error;
checkDone();
}
void SetupDetection::handleLauncherOutput(const QByteArray &output)
{
launcherOutput.append(output);
}
void SetupDetection::handleTimeout()
{
timedOut = true;
checkDone();
}
void SetupDetection::checkDone()
{
if (!m_testStarted) {
return;
}
if (isDone()) {
timeout.stop();
m_testStarted = false;
emit done();
}
}
} // namespace QtGui
|