File: syncthinglauncher.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (582 lines) | stat: -rw-r--r-- 21,088 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include "./syncthinglauncher.h"

#include <syncthingconnector/syncthingconnection.h>
#include <syncthingconnector/utils.h>

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
#include "../settings/settings.h"
#endif

#include <c++utilities/io/ansiescapecodes.h>

#include <QMetaObject>
#include <QtConcurrentRun>

#ifdef SYNCTHINGCONNECTION_SUPPORT_METERED
#include <QNetworkInformation>
#endif

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <string_view>
#include <utility>

using namespace std;
using namespace std::placeholders;
using namespace CppUtilities;

namespace Data {

SyncthingLauncher *SyncthingLauncher::s_mainInstance = nullptr;

/*!
 * \class SyncthingLauncher
 * \brief The SyncthingLauncher class starts a Syncthing instance either as an external process or using a library version of Syncthing.
 * \remarks
 * - This is *not* strictly a singleton class. However, one instance is supposed to be the "main instance" (see SyncthingLauncher::setMainInstance()).
 * - A SyncthingLauncher instance can only launch one Syncthing instance at a time.
 * - Using Syncthing as library must be explicitly enabled by setting the CMake variable USE_LIBSYNCTHING.
 * - When using Syncthing as library only one instance of SyncthingLauncher can start Syncthing at a time; trying to start a 2nd Syncthing instance
 *   via another SyncthingLauncher will leads to a failure (but not to undefined behavior).
 * - You must not try to Syncthing as library from multiple threads at the same time. This will lead to undefined behavior even when using different
 *   SyncthingLauncher instances.
 */

/*!
 * \brief Constructs a new Syncthing launcher.
 */
SyncthingLauncher::SyncthingLauncher(QObject *parent)
    : QObject(parent)
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
    , m_lastLauncherSettings(nullptr)
#endif
    , m_relevantConnection(nullptr)
    , m_guiListeningUrlSearch("Access the GUI via the following URL: ", "\n\r", std::string_view(), BufferSearch::CallbackType())
    , m_exitSearch("Syncthing exited: ", "\n\r", std::string_view(), BufferSearch::CallbackType())
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    , m_libsyncthingLogLevel(LibSyncthing::LogLevel::Info)
#endif
    , m_manuallyStopped(true)
    , m_stoppedMetered(false)
    , m_emittingOutput(false)
    , m_useLibSyncthing(false)
    , m_stopOnMeteredConnection(false)
{
    connect(&m_process, &SyncthingProcess::readyRead, this, &SyncthingLauncher::handleProcessReadyRead, Qt::QueuedConnection);
    connect(&m_process, static_cast<void (SyncthingProcess::*)(int exitCode, QProcess::ExitStatus exitStatus)>(&SyncthingProcess::finished), this,
        &SyncthingLauncher::handleProcessFinished, Qt::QueuedConnection);
    connect(&m_process, &SyncthingProcess::stateChanged, this, &SyncthingLauncher::handleProcessStateChanged, Qt::QueuedConnection);
    connect(&m_process, &SyncthingProcess::errorOccurred, this, &SyncthingLauncher::errorOccurred, Qt::QueuedConnection);
    connect(&m_process, &SyncthingProcess::confirmKill, this, &SyncthingLauncher::confirmKill);
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    connect(&m_startWatcher, &QFutureWatcher<std::int64_t>::finished, this, &SyncthingLauncher::handleLibSyncthingFinished);
#endif

    // initialize handling of metered connections
#ifdef SYNCTHINGCONNECTION_SUPPORT_METERED
    if (const auto *const networkInformation = loadNetworkInformationBackendForMetered()) {
        connect(networkInformation, &QNetworkInformation::isMeteredChanged, this, [this](bool isMetered) { setNetworkConnectionMetered(isMetered); });
        setNetworkConnectionMetered(networkInformation->isMetered());
    }
#endif
}

/*!
 * \brief Ensures the built-in Syncthing instance is stopped if it was started by this SyncthingLauncher instance.
 */
SyncthingLauncher::~SyncthingLauncher()
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    if (!m_startFuture.isCanceled()) {
        stopLibSyncthing();
        m_startFuture.waitForFinished();
    }
#endif
}

#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
/*!
 * \brief Sets whether Syncthing is supposed to run or not.
 * \remarks
 * - This function will takes runtime conditions such as isStoppingOnMeteredConnection() into account.
 * - This function so far only supports launching via the built-in Syncthing library.
 */
void SyncthingLauncher::setRunning(bool running, LibSyncthing::RuntimeOptions &&runtimeOptions)
{
    // check runtime conditions
    auto shouldBeRunning = running;
    if (isStoppingOnMeteredConnection() && isNetworkConnectionMetered().value_or(false)) {
        m_stoppedMetered = running;
        shouldBeRunning = false;
    }
    // start/stop Syncthing depending on \a running
    if (shouldBeRunning) {
        launch(runtimeOptions);
    } else {
        tearDownLibSyncthing();
    }
    // save runtime options so Syncthing can resume in case runtime conditions allow it
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
    m_lastLauncherSettings = nullptr;
#endif
    m_lastRuntimeOptions = std::move(runtimeOptions);
    // emit signal in any case (even if there's no change) so runningStatus() is re-evaluated
    emit runningChanged(shouldBeRunning);
}
#endif

/*!
 * \brief Returns a short message about whether Syncthing is running.
 */
QString SyncthingLauncher::runningStatus() const
{
    if (isRunning()) {
        return tr("Syncthing is running");
    } else if (m_stoppedMetered) {
        return tr("Syncthing is temporarily stopped due to metered connection");
    } else if (m_lastExitStatus.has_value()) {
        return tr("Syncthing exited with status %1").arg(m_lastExitStatus.value().code);
    } else {
        return tr("Syncthing is not running");
    }
}

/*!
 * \brief Sets whether the output/log should be emitted via outputAvailable() signal.
 */
void SyncthingLauncher::setEmittingOutput(bool emittingOutput)
{
    if (m_emittingOutput == emittingOutput || !(m_emittingOutput = emittingOutput) || m_outputBuffer.isEmpty()) {
        return;
    }
    QByteArray data;
    m_outputBuffer.swap(data);
    emit outputAvailable(std::move(data));
}

/*!
 * \brief Returns a short status message about whether the network connection is metered.
 */
QString SyncthingLauncher::meteredStatus() const
{
    if (m_metered.has_value()) {
        return m_metered.value() ? tr("Network connection is metered") : tr("Network connection is not metered");
    } else {
        return tr("State of network connection cannot be determined");
    }
}

/*!
 * \brief Sets whether the current network connection is metered and stops/starts Syncthing accordingly as needed.
 * \remarks
 * - This is detected and monitored automatically. A manually set value will be overridden again on the next change.
 * - One may set this manually for testing purposes or in case the automatic detection is not supported (then
 *   isNetworkConnectionMetered() returns a std::optional<bool> without value).
 */
void SyncthingLauncher::setNetworkConnectionMetered(std::optional<bool> metered)
{
    if (metered != m_metered) {
        m_metered = metered;
        if (m_stopOnMeteredConnection) {
            if (metered.value_or(false)) {
                terminateDueToMeteredConnection();
            } else if (!metered.value_or(true) && m_stoppedMetered) {
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
                if (m_lastLauncherSettings) {
                    launch(*m_lastLauncherSettings);
                }
#endif
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS) && defined(SYNCTHINGWIDGETS_USE_LIBSYNCTHING)
                else
#endif
#if defined(SYNCTHINGWIDGETS_USE_LIBSYNCTHING)
                    if (m_lastRuntimeOptions) {
                    launch(*m_lastRuntimeOptions);
                }
#endif
            }
        }
        emit networkConnectionMeteredChanged(metered);
    }
}

/*!
 * \brief Sets whether Syncthing should automatically be stopped as long as the network connection is metered.
 */
void SyncthingLauncher::setStoppingOnMeteredConnection(bool stopOnMeteredConnection)
{
    if ((stopOnMeteredConnection != m_stopOnMeteredConnection) && (m_stopOnMeteredConnection = stopOnMeteredConnection)
        && m_metered.value_or(false)) {
        terminateDueToMeteredConnection();
    }
}

/*!
 * \brief Returns whether the built-in Syncthing library is available.
 */
bool SyncthingLauncher::isLibSyncthingAvailable()
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    return true;
#else
    return false;
#endif
}

/*!
 * \brief Returns the Syncthing version provided by libsyncthing or "Not built with libsyncthing support." if not built with libsyncthing support.
 */
QString SyncthingLauncher::libSyncthingVersionInfo()
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    return QString::fromStdString(LibSyncthing::longSyncthingVersion()).replace(QStringLiteral(" 1970-01-01 00:00:00 UTC"), QString());
#else
    return tr("Not built with libsyncthing support.");
#endif
}

/*!
 * \brief Launches a Syncthing instance using the specified \a arguments.
 *
 * To use the internal library, leave \a program empty. In this case \a arguments are ignored.
 * Otherwise \a program must be the path the external Syncthing executable.
 *
 * \remarks Does nothing if already running an instance.
 */
void SyncthingLauncher::launch(const QString &program, const QStringList &arguments)
{
    if (isRunning() || m_stopFuture.isRunning()) {
        return;
    }
    resetState();

    // start external process
    if (!program.isEmpty()) {
        m_process.startSyncthing(program, arguments);
        return;
    }

#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    runLibSyncthing(LibSyncthing::RuntimeOptions{});
#else
    showLibSyncthingNotSupported();
#endif
}

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
/*!
 * \brief Launches a Syncthing instance according to the specified \a launcherSettings.
 * \remarks Does nothing if already running an instance.
 */
void SyncthingLauncher::launch(const Settings::Launcher &launcherSettings)
{
    if (isRunning()) {
        return;
    }
    if (!launcherSettings.useLibSyncthing && launcherSettings.syncthingPath.isEmpty()) {
        m_process.reportError(QProcess::FailedToStart, QStringLiteral("executable path is empty"));
        return;
    }
    if (launcherSettings.useLibSyncthing) {
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
        const auto &libSyncthingSettings = launcherSettings.libSyncthing;
        auto options = LibSyncthing::RuntimeOptions();
        options.configDir = libSyncthingSettings.configDir.toStdString();
        options.dataDir = libSyncthingSettings.dataDir.isEmpty() ? options.configDir : libSyncthingSettings.dataDir.toStdString();
        if (libSyncthingSettings.expandPaths) {
            options.flags = options.flags | LibSyncthing::RuntimeFlags::ExpandPathsFromEnv;
        }
        setLibSyncthingLogLevel(libSyncthingSettings.logLevel);
        launch(options);
#else
        showLibSyncthingNotSupported();
#endif
    } else {
        launch(launcherSettings.syncthingPath, SyncthingProcess::splitArguments(launcherSettings.syncthingArgs));
    }
    m_stopOnMeteredConnection = launcherSettings.stopOnMeteredConnection;
    m_lastLauncherSettings = &launcherSettings;
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    m_lastRuntimeOptions.reset();
#endif
}
#endif

#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
/*!
 * \brief Returns the specified \a logLevel for the built-in Syncthing instance as string.
 */
QString SyncthingLauncher::libSyncthingLogLevelString(LibSyncthing::LogLevel logLevel)
{
    switch (logLevel) {
    case LibSyncthing::LogLevel::Debug:
        return QStringLiteral("debug");
    case LibSyncthing::LogLevel::Verbose:
        return QStringLiteral("verbose");
    case LibSyncthing::LogLevel::Warning:
        return QStringLiteral("warning");
    case LibSyncthing::LogLevel::Fatal:
        return QStringLiteral("fatal");
    default:
        return QStringLiteral("info");
    }
}

/*!
 * \brief Sets the log level for the built-in Syncthing instance from the specified string.
 * \remarks Assigns \a fallbackLogLevel if \a logLevel is not valid.
 */
void SyncthingLauncher::setLibSyncthingLogLevel(const QString &logLevel, LibSyncthing::LogLevel fallbackLogLevel)
{
    if (logLevel.compare(QLatin1String("debug"), Qt::CaseInsensitive) == 0) {
        m_libsyncthingLogLevel = LibSyncthing::LogLevel::Debug;
    } else if (logLevel.compare(QLatin1String("verbose"), Qt::CaseInsensitive) == 0) {
        m_libsyncthingLogLevel = LibSyncthing::LogLevel::Verbose;
    } else if (logLevel.compare(QLatin1String("info"), Qt::CaseInsensitive) == 0) {
        m_libsyncthingLogLevel = LibSyncthing::LogLevel::Info;
    } else if (logLevel.compare(QLatin1String("warning"), Qt::CaseInsensitive) == 0) {
        m_libsyncthingLogLevel = LibSyncthing::LogLevel::Warning;
    } else if (logLevel.compare(QLatin1String("fatal"), Qt::CaseInsensitive) == 0) {
        m_libsyncthingLogLevel = LibSyncthing::LogLevel::Fatal;
    } else {
        m_libsyncthingLogLevel = fallbackLogLevel;
    }
}

/*!
 * \brief Launches a Syncthing instance using the internal library with the specified \a runtimeOptions.
 * \remarks
 * - Does nothing if already running an instance.
 * - In contrast to other overloads and setRunning() this function does *not* keep track of the last launcher
 *   settings or runtime options. Hence Syncthing will not be able to automatically resume in case runtime options
 *   allow it.
 */
void SyncthingLauncher::launch(const LibSyncthing::RuntimeOptions &runtimeOptions)
{
    if (isRunning() || m_stopFuture.isRunning()) {
        return;
    }
    resetState();
    runLibSyncthing(runtimeOptions);
}
#endif

void SyncthingLauncher::terminate(SyncthingConnection *relevantConnection)
{
    if (m_process.isRunning()) {
        m_manuallyStopped = true;
        m_process.stopSyncthing(relevantConnection);
    } else {
        tearDownLibSyncthing();
    }
}

void SyncthingLauncher::kill()
{
    if (m_process.isRunning()) {
        m_manuallyStopped = true;
        m_process.killSyncthing();
    } else {
        tearDownLibSyncthing();
    }
}

/*!
 * \brief Stops the built-in Syncthing instance.
 * \remarks
 * - Does nothing if Syncthing is starting or stopping or has already been stopped or never been started.
 * - Returns immediately. The stopping is performed asynchronously.
 */
void SyncthingLauncher::tearDownLibSyncthing()
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    if (!m_startFuture.isRunning() || m_stopFuture.isRunning()) {
        return;
    }
    m_manuallyStopped = true;
    m_stopFuture = QtConcurrent::run(std::bind(&SyncthingLauncher::stopLibSyncthing, this));
#endif
}

/*!
 * \brief Stops the built-in Syncthing instance.
 * \remarks
 * - Does nothing if Syncthing has already been stopped or never been started.
 * - Blocks the current thread until Syncthing has been stopped.
 */
void SyncthingLauncher::stopLibSyncthing()
{
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    LibSyncthing::stopSyncthing();
    // no need to emit exited/runningChanged here; that is already done after
    // m_startFuture has finished
#endif
}

void SyncthingLauncher::handleProcessReadyRead()
{
    const auto data = m_process.readAll();
    if (m_logFile.isOpen()) {
        m_logFile.write(data);
    }
    handleOutputAvailable(-1, data);
}

void SyncthingLauncher::handleProcessStateChanged(QProcess::ProcessState newState)
{
    switch (newState) {
    case QProcess::NotRunning:
        emit runningChanged(false);
        emit startingChanged();
        break;
    case QProcess::Starting:
        emit runningChanged(true);
        emit startingChanged();
        break;
    default:;
    }
}

void SyncthingLauncher::handleProcessFinished(int code, QProcess::ExitStatus status)
{
    const auto &exitStatus = m_lastExitStatus.emplace(code, status);
    emit exited(exitStatus.code, exitStatus.status);
}

void SyncthingLauncher::resetState()
{
    m_manuallyStopped = false;
    m_stoppedMetered = false;
    delete m_relevantConnection;
    m_relevantConnection = nullptr;
    m_guiListeningUrlSearch.reset();
    m_exitSearch.reset();
    m_lastExitStatus.reset();
    if (!m_guiListeningUrl.isEmpty()) {
        m_guiListeningUrl.clear();
        emit guiUrlChanged(m_guiListeningUrl);
    }
}

#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
static const char *const logLevelStrings[] = {
    "[DEBUG]   ",
    "[VERBOSE] ",
    "[INFO]    ",
    "[WARNING] ",
    "[FATAL]   ",
};

void SyncthingLauncher::handleLoggingCallback(LibSyncthing::LogLevel level, const char *message, size_t messageSize)
{
    auto messageData = QByteArray();
    messageSize = min<size_t>(numeric_limits<int>::max() - 20, messageSize);
    messageData.reserve(static_cast<int>(messageSize) + 20);
    messageData.append(logLevelStrings[static_cast<int>(level)]);
    messageData.append(message, static_cast<int>(messageSize));
    messageData.append('\n');
    if (level >= m_libsyncthingLogLevel && m_logFile.isOpen()) {
        m_logFile.write(messageData);
    }
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
    QMetaObject::invokeMethod(this, &SyncthingLauncher::handleOutputAvailable, Qt::QueuedConnection, static_cast<int>(level), std::move(messageData));
#else
    QMetaObject::invokeMethod(
        this, "handleOutputAvailable", Qt::QueuedConnection, Q_ARG(int, static_cast<int>(level)), Q_ARG(QByteArray, messageData));
#endif
}
#endif

void SyncthingLauncher::handleOutputAvailable(int logLevel, const QByteArray &data)
{
    const auto *const exitOffset = m_exitSearch.process(data.data(), static_cast<std::size_t>(data.size()));
    const auto *const guiAddressOffset = m_guiListeningUrlSearch.process(data.data(), static_cast<std::size_t>(data.size()));
    if (exitOffset) {
        std::cerr << EscapeCodes::Phrases::Info << "Syncthing exited: " << m_exitSearch.result() << EscapeCodes::Phrases::End;
        emit exitLogged(std::move(m_exitSearch.result()));
        m_exitSearch.reset();
    }
    if (guiAddressOffset > exitOffset) {
        m_guiListeningUrl.setUrl(QString::fromStdString(m_guiListeningUrlSearch.result()));
        std::cerr << EscapeCodes::Phrases::Info << "Syncthing GUI available: " << m_guiListeningUrlSearch.result() << EscapeCodes::Phrases::End;
        m_guiListeningUrlSearch.reset();
        emit guiUrlChanged(m_guiListeningUrl);
        emit startingChanged();
    } else if (exitOffset) {
        m_guiListeningUrl.clear();
        emit guiUrlChanged(m_guiListeningUrl);
    }
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
    if (logLevel < static_cast<int>(m_libsyncthingLogLevel)) {
        return;
    }
#else
    Q_UNUSED(logLevel)
#endif
    if (isEmittingOutput()) {
        emit outputAvailable(data);
    } else {
        m_outputBuffer += data;
    }
}

void SyncthingLauncher::terminateDueToMeteredConnection()
{
    if (!isRunning()) {
        // do not set m_stoppedMetered (and basically don't do anything) if not running anyway; otherwise we'd
        // always start Syncthing once the connection is not metered anymore (even if Syncthing has not even been
        // running before)
        return;
    }
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
    if (m_lastLauncherSettings && !m_relevantConnection) {
        m_relevantConnection = m_lastLauncherSettings->connectionForLauncher(this);
    }
#endif
    terminate(m_relevantConnection);
    m_stoppedMetered = true;
}

#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
/*!
 * \brief Starts the built-in Syncthing instance.
 * \remarks Returns immediately as Syncthing is started in a different thread.
 */
void SyncthingLauncher::runLibSyncthing(const LibSyncthing::RuntimeOptions &runtimeOptions)
{
    if (LibSyncthing::hasLoggingCallback()) {
        showLibSyncthingNotSupported(QByteArrayLiteral("libsyncthing has already been started"));
        return;
    }
    LibSyncthing::setLoggingCallback(std::bind(&SyncthingLauncher::handleLoggingCallback, this, _1, _2, _3));
    emit runningChanged(true);
    emit startingChanged();
    m_startWatcher.setFuture(m_startFuture = QtConcurrent::run(std::bind(&LibSyncthing::runSyncthing, runtimeOptions)));
}

void SyncthingLauncher::handleLibSyncthingFinished()
{
    const auto exitCode = m_startFuture.result();
    const auto &exitStatus = m_lastExitStatus.emplace(static_cast<int>(exitCode), exitCode == 0 ? QProcess::NormalExit : QProcess::CrashExit);
    LibSyncthing::setLoggingCallback(LibSyncthing::LoggingCallback());
    m_guiListeningUrl.clear();
    emit guiUrlChanged(m_guiListeningUrl);
    emit exited(exitStatus.code, exitStatus.status);
    emit runningChanged(false);
    emit startingChanged();
}
#endif

/*!
 * \brief Shows that launching Syncthing is not supported by emitting a non-zero exit status and logging \a reason.
 */
void SyncthingLauncher::showLibSyncthingNotSupported(QByteArray &&reason)
{
    handleOutputAvailable(4, std::move(reason)); // LibSyncthing::LogLevel::Fatal
    const auto &exitStatus = m_lastExitStatus.emplace(-1, QProcess::CrashExit);
    emit exited(exitStatus.code, exitStatus.status);
}

} // namespace Data