Better decouple syncthingwidgets from libsyncthing if libsyncthing is disabled

Before this change syncthingwidgets unconditionally included the header
from libsyncthing so it couldn't be used as stand-alone library if
libsyncthing was disabled.
This commit is contained in:
Martchus 2021-02-03 00:01:34 +01:00
parent 975e86c895
commit 8ec9038b2c
8 changed files with 68 additions and 26 deletions

1
include/syncthing Symbolic link
View File

@ -0,0 +1 @@
../libsyncthing

View File

@ -90,7 +90,7 @@ option(USE_LIBSYNCTHING "whether libsyncthing should be included for the launche
if (USE_LIBSYNCTHING)
find_package(syncthing ${META_APP_VERSION} REQUIRED)
use_syncthing()
set_source_files_properties(misc/syncthinglauncher.cpp PROPERTIES COMPILE_DEFINITIONS SYNCTHINGWIDGETS_USE_LIBSYNCTHING)
list(APPEND META_PUBLIC_COMPILE_DEFINITIONS SYNCTHINGWIDGETS_USE_LIBSYNCTHING)
endif ()
# configure logging JavaScript events to stderr

View File

@ -30,7 +30,9 @@ SyncthingLauncher *SyncthingLauncher::s_mainInstance = nullptr;
*/
SyncthingLauncher::SyncthingLauncher(QObject *parent)
: QObject(parent)
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
, m_libsyncthingLogLevel(LibSyncthing::LogLevel::Info)
#endif
, m_manuallyStopped(true)
, m_emittingOutput(false)
{
@ -100,8 +102,12 @@ void SyncthingLauncher::launch(const QString &program, const QStringList &argume
return;
}
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
// use libsyncthing
m_startFuture = QtConcurrent::run(std::bind(&SyncthingLauncher::runLibSyncthing, this, LibSyncthing::RuntimeOptions{}));
#else
showLibSyncthingNotSupported();
#endif
}
/*!
@ -118,16 +124,21 @@ void SyncthingLauncher::launch(const Settings::Launcher &launcherSettings)
return;
}
if (launcherSettings.useLibSyncthing) {
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
LibSyncthing::RuntimeOptions options;
options.configDir = launcherSettings.libSyncthing.configDir.toStdString();
options.dataDir = launcherSettings.libSyncthing.configDir.toStdString();
setLibSyncthingLogLevel(launcherSettings.libSyncthing.logLevel);
launch(options);
#else
showLibSyncthingNotSupported();
#endif
} else {
launch(launcherSettings.syncthingPath, SyncthingProcess::splitArguments(launcherSettings.syncthingArgs));
}
}
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
/*!
* \brief Launches a Syncthing instance using the internal library with the specified \a runtimeOptions.
* \remarks Does nothing if already running an instance.
@ -140,6 +151,7 @@ void SyncthingLauncher::launch(const LibSyncthing::RuntimeOptions &runtimeOption
m_manuallyStopped = false;
m_startFuture = QtConcurrent::run(std::bind(&SyncthingLauncher::runLibSyncthing, this, runtimeOptions));
}
#endif
void SyncthingLauncher::terminate()
{
@ -203,11 +215,9 @@ static const char *const logLevelStrings[] = {
"[WARNING] ",
"[FATAL] ",
};
#endif
void SyncthingLauncher::handleLoggingCallback(LibSyncthing::LogLevel level, const char *message, size_t messageSize)
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
if (level < m_libsyncthingLogLevel) {
return;
}
@ -219,12 +229,8 @@ void SyncthingLauncher::handleLoggingCallback(LibSyncthing::LogLevel level, cons
messageData.append('\n');
handleOutputAvailable(move(messageData));
#else
CPP_UTILITIES_UNUSED(level)
CPP_UTILITIES_UNUSED(message)
CPP_UTILITIES_UNUSED(messageSize)
#endif
}
#endif
void SyncthingLauncher::handleOutputAvailable(QByteArray &&data)
{
@ -235,27 +241,29 @@ void SyncthingLauncher::handleOutputAvailable(QByteArray &&data)
}
}
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
void SyncthingLauncher::runLibSyncthing(const LibSyncthing::RuntimeOptions &runtimeOptions)
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
LibSyncthing::setLoggingCallback(bind(&SyncthingLauncher::handleLoggingCallback, this, _1, _2, _3));
emit runningChanged(true);
const auto exitCode = LibSyncthing::runSyncthing(runtimeOptions);
emit exited(static_cast<int>(exitCode), exitCode == 0 ? QProcess::NormalExit : QProcess::CrashExit);
emit runningChanged(false);
#else
CPP_UTILITIES_UNUSED(runtimeOptions)
handleOutputAvailable(QByteArray("libsyncthing support not enabled"));
emit exited(-1, QProcess::CrashExit);
#endif
}
void SyncthingLauncher::stopLibSyncthing()
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
LibSyncthing::stopSyncthing();
// no need to emit exited/runningChanged here; that is already done in runLibSyncthing()
#endif
}
#else
void SyncthingLauncher::showLibSyncthingNotSupported()
{
handleOutputAvailable(QByteArray("libsyncthing support not enabled"));
emit exited(-1, QProcess::CrashExit);
}
#endif
} // namespace Data

View File

@ -3,16 +3,15 @@
#include "../global.h"
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
#include <syncthing/interface.h>
#endif
#include <syncthingconnector/syncthingprocess.h>
#include "../../libsyncthing/interface.h"
#include <QByteArray>
#include <QFuture>
namespace LibSyncthing {
struct RuntimeOptions;
}
namespace Settings {
struct Launcher;
}
@ -35,12 +34,17 @@ public:
bool isManuallyStopped() const;
bool isEmittingOutput() const;
void setEmittingOutput(bool emittingOutput);
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
LibSyncthing::LogLevel libSyncthingLogLevel() const;
void setLibSyncthingLogLevel(LibSyncthing::LogLevel logLevel);
#endif
static bool isLibSyncthingAvailable();
static SyncthingLauncher *mainInstance();
static void setMainInstance(SyncthingLauncher *mainInstance);
static QString libSyncthingVersionInfo();
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
void launch(const LibSyncthing::RuntimeOptions &runtimeOptions);
#endif
Q_SIGNALS:
void confirmKill();
@ -52,7 +56,6 @@ Q_SIGNALS:
public Q_SLOTS:
void launch(const QString &program, const QStringList &arguments);
void launch(const Settings::Launcher &launcherSettings);
void launch(const LibSyncthing::RuntimeOptions &runtimeOptions);
void terminate();
void kill();
void tearDownLibSyncthing();
@ -61,11 +64,17 @@ private Q_SLOTS:
void handleProcessReadyRead();
void handleProcessStateChanged(QProcess::ProcessState newState);
void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
void handleLoggingCallback(LibSyncthing::LogLevel, const char *message, std::size_t messageSize);
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
void runLibSyncthing(const LibSyncthing::RuntimeOptions &runtimeOptions);
void stopLibSyncthing();
#else
void showLibSyncthingNotSupported();
#endif
private:
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
void handleLoggingCallback(LibSyncthing::LogLevel, const char *message, std::size_t messageSize);
#endif
void handleOutputAvailable(QByteArray &&data);
SyncthingProcess m_process;
@ -73,7 +82,9 @@ private:
QFuture<void> m_stopFuture;
QByteArray m_outputBuffer;
CppUtilities::DateTime m_futureStarted;
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
LibSyncthing::LogLevel m_libsyncthingLogLevel;
#endif
bool m_manuallyStopped;
bool m_emittingOutput;
bool m_useLibSyncthing;
@ -118,6 +129,7 @@ inline bool SyncthingLauncher::isEmittingOutput() const
return m_emittingOutput;
}
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
/// \brief Returns the log level used for libsyncthing.
inline LibSyncthing::LogLevel SyncthingLauncher::libSyncthingLogLevel() const
{
@ -129,6 +141,7 @@ inline void SyncthingLauncher::setLibSyncthingLogLevel(LibSyncthing::LogLevel lo
{
m_libsyncthingLogLevel = logLevel;
}
#endif
/// \brief Returns the SyncthingLauncher instance previously assigned via SyncthingLauncher::setMainInstance().
inline SyncthingLauncher *SyncthingLauncher::mainInstance()

View File

@ -301,10 +301,12 @@ void restore()
auto &launcher = v.launcher;
launcher.autostartEnabled = settings.value(QStringLiteral("syncthingAutostart"), launcher.autostartEnabled).toBool();
launcher.useLibSyncthing = settings.value(QStringLiteral("useLibSyncthing"), launcher.useLibSyncthing).toBool();
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
launcher.libSyncthing.configDir = settings.value(QStringLiteral("libSyncthingConfigDir"), launcher.libSyncthing.configDir).toString();
launcher.libSyncthing.dataDir = settings.value(QStringLiteral("libSyncthingDataDir"), launcher.libSyncthing.dataDir).toString();
launcher.libSyncthing.logLevel = static_cast<LibSyncthing::LogLevel>(
settings.value(QStringLiteral("libSyncthingLogLevel"), static_cast<int>(launcher.libSyncthing.logLevel)).toInt());
#endif
launcher.syncthingPath = settings.value(QStringLiteral("syncthingPath"), launcher.syncthingPath).toString();
launcher.syncthingArgs = settings.value(QStringLiteral("syncthingArgs"), launcher.syncthingArgs).toString();
launcher.considerForReconnect = settings.value(QStringLiteral("considerLauncherForReconnect"), launcher.considerForReconnect).toBool();
@ -406,9 +408,11 @@ void save()
const auto &launcher = v.launcher;
settings.setValue(QStringLiteral("syncthingAutostart"), launcher.autostartEnabled);
settings.setValue(QStringLiteral("useLibSyncthing"), launcher.useLibSyncthing);
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
settings.setValue(QStringLiteral("libSyncthingConfigDir"), launcher.libSyncthing.configDir);
settings.setValue(QStringLiteral("libSyncthingDataDir"), launcher.libSyncthing.dataDir);
settings.setValue(QStringLiteral("libSyncthingLogLevel"), static_cast<int>(launcher.libSyncthing.logLevel));
#endif
settings.setValue(QStringLiteral("syncthingPath"), launcher.syncthingPath);
settings.setValue(QStringLiteral("syncthingArgs"), launcher.syncthingArgs);
settings.setValue(QStringLiteral("considerLauncherForReconnect"), launcher.considerForReconnect);

View File

@ -1,11 +1,15 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <syncthingconnector/syncthingconnectionsettings.h>
#include "../../libsyncthing/interface.h"
#include <syncthingmodel/syncthingicons.h>
#include "../global.h"
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
#include <syncthing/interface.h>
#endif
#include <syncthingconnector/syncthingconnectionsettings.h>
#include <syncthingmodel/syncthingicons.h>
#include <qtutilities/settingsdialog/qtsettings.h>
#include <QByteArray>
@ -81,11 +85,13 @@ struct SYNCTHINGWIDGETS_EXPORT Launcher {
bool considerForReconnect = false;
bool showButton = false;
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
struct SYNCTHINGWIDGETS_EXPORT LibSyncthing {
QString configDir;
QString dataDir;
::LibSyncthing::LogLevel logLevel = ::LibSyncthing::LogLevel::Info;
} libSyncthing;
#endif
static Data::SyncthingProcess &toolProcess(const QString &tool);
static std::vector<Data::SyncthingProcess *> allProcesses();

View File

@ -950,8 +950,10 @@ QWidget *LauncherOptionPage::setupWidget()
connect(m_launcher, &SyncthingLauncher::outputAvailable, this, &LauncherOptionPage::handleSyncthingOutputAvailable, Qt::QueuedConnection);
connect(m_launcher, &SyncthingLauncher::exited, this, &LauncherOptionPage::handleSyncthingExited, Qt::QueuedConnection);
connect(m_launcher, &SyncthingLauncher::errorOccurred, this, &LauncherOptionPage::handleSyncthingError, Qt::QueuedConnection);
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
connect(ui()->logLevelComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
&LauncherOptionPage::updateLibSyncthingLogLevel);
#endif
m_launcher->setEmittingOutput(true);
}
connect(ui()->launchNowPushButton, &QPushButton::clicked, this, &LauncherOptionPage::launch);
@ -966,9 +968,11 @@ bool LauncherOptionPage::apply()
if (m_tool.isEmpty()) {
settings.autostartEnabled = ui()->enabledCheckBox->isChecked();
settings.useLibSyncthing = ui()->useBuiltInVersionCheckBox->isChecked();
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
settings.libSyncthing.configDir = ui()->configDirPathSelection->lineEdit()->text();
settings.libSyncthing.dataDir = ui()->dataDirPathSelection->lineEdit()->text();
settings.libSyncthing.logLevel = static_cast<LibSyncthing::LogLevel>(ui()->logLevelComboBox->currentIndex());
#endif
settings.syncthingPath = ui()->syncthingPathSelection->lineEdit()->text();
settings.syncthingArgs = ui()->argumentsLineEdit->text();
settings.considerForReconnect = ui()->considerForReconnectCheckBox->isChecked();
@ -989,9 +993,11 @@ void LauncherOptionPage::reset()
ui()->enabledCheckBox->setChecked(settings.autostartEnabled);
ui()->useBuiltInVersionCheckBox->setChecked(settings.useLibSyncthing);
ui()->useBuiltInVersionCheckBox->setVisible(settings.useLibSyncthing || SyncthingLauncher::isLibSyncthingAvailable());
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
ui()->configDirPathSelection->lineEdit()->setText(settings.libSyncthing.configDir);
ui()->dataDirPathSelection->lineEdit()->setText(settings.libSyncthing.dataDir);
ui()->logLevelComboBox->setCurrentIndex(static_cast<int>(settings.libSyncthing.logLevel));
#endif
ui()->syncthingPathSelection->lineEdit()->setText(settings.syncthingPath);
ui()->argumentsLineEdit->setText(settings.syncthingArgs);
ui()->considerForReconnectCheckBox->setChecked(settings.considerForReconnect);
@ -1128,10 +1134,12 @@ void LauncherOptionPage::launch()
handleSyncthingLaunched(true);
}
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
void LauncherOptionPage::updateLibSyncthingLogLevel()
{
m_launcher->setLibSyncthingLogLevel(static_cast<LibSyncthing::LogLevel>(ui()->logLevelComboBox->currentIndex()));
}
#endif
void LauncherOptionPage::stop()
{

View File

@ -122,7 +122,9 @@ private Q_SLOTS:
void handleSyncthingError(QProcess::ProcessError error);
bool isRunning() const;
void launch();
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
void updateLibSyncthingLogLevel();
#endif
void stop();
void restoreDefaultArguments();