syncthingtray/widgets/misc/syncthinglauncher.h

125 lines
3.6 KiB
C
Raw Normal View History

#ifndef SYNCTHINGWIDGETS_SYNCTHINGLAUNCHER_H
#define SYNCTHINGWIDGETS_SYNCTHINGLAUNCHER_H
#include "../global.h"
#include "../../connector/syncthingprocess.h"
#include "../../libsyncthing/interface.h"
#include <QFuture>
namespace LibSyncthing {
struct RuntimeOptions;
}
namespace Settings {
struct Launcher;
}
namespace Data {
class SYNCTHINGWIDGETS_EXPORT SyncthingLauncher : public QObject {
Q_OBJECT
Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged)
2019-06-10 22:48:26 +02:00
Q_PROPERTY(CppUtilities::DateTime activeSince READ activeSince)
Q_PROPERTY(bool manuallyStopped READ isManuallyStopped)
Q_PROPERTY(bool useLibSyncthing READ isUseLibSyncthing WRITE setUseLibSyncthing)
public:
explicit SyncthingLauncher(QObject *parent = nullptr);
bool isRunning() const;
2019-06-10 22:48:26 +02:00
CppUtilities::DateTime activeSince() const;
bool isActiveFor(unsigned int atLeastSeconds) const;
bool isManuallyStopped() const;
bool isUseLibSyncthing() const;
static bool isLibSyncthingAvailable();
static SyncthingLauncher *mainInstance();
static void setMainInstance(SyncthingLauncher *mainInstance);
Q_SIGNALS:
void confirmKill();
void runningChanged(bool isRunning);
void outputAvailable(const QByteArray &data);
void exited(int exitCode, QProcess::ExitStatus exitStatus);
2019-07-07 14:31:09 +02:00
void errorOccurred(QProcess::ProcessError error);
public Q_SLOTS:
void setUseLibSyncthing(bool useLibSyncthing);
void launch(const QString &program, const QStringList &arguments);
void launch(const Settings::Launcher &launcherSettings);
void launch(const LibSyncthing::RuntimeOptions &runtimeOptions);
void terminate();
void kill();
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);
void runLibSyncthing(const LibSyncthing::RuntimeOptions &runtimeOptions);
void runLibSyncthing(const std::vector<std::string> &arguments);
private:
SyncthingProcess m_process;
QFuture<void> m_future;
2019-06-10 22:48:26 +02:00
CppUtilities::DateTime m_futureStarted;
bool m_manuallyStopped;
bool m_useLibSyncthing;
static SyncthingLauncher *s_mainInstance;
};
inline bool SyncthingLauncher::isRunning() const
{
return m_process.isRunning() || m_future.isRunning();
}
2019-06-10 22:48:26 +02:00
inline CppUtilities::DateTime SyncthingLauncher::activeSince() const
{
if (m_process.isRunning()) {
return m_process.activeSince();
} else if (m_future.isRunning()) {
return m_futureStarted;
}
2019-06-10 22:48:26 +02:00
return CppUtilities::DateTime();
}
inline bool SyncthingLauncher::isActiveFor(unsigned int atLeastSeconds) const
{
const auto activeSince(this->activeSince());
2019-06-10 22:48:26 +02:00
return !activeSince.isNull() && (CppUtilities::DateTime::gmtNow() - activeSince).totalSeconds() > atLeastSeconds;
}
inline bool SyncthingLauncher::isManuallyStopped() const
{
return m_manuallyStopped;
}
inline bool SyncthingLauncher::isUseLibSyncthing() const
{
return m_useLibSyncthing;
}
inline void SyncthingLauncher::setUseLibSyncthing(bool useLibSyncthing)
{
m_useLibSyncthing = useLibSyncthing;
}
inline SyncthingLauncher *SyncthingLauncher::mainInstance()
{
return s_mainInstance;
}
inline void SyncthingLauncher::setMainInstance(SyncthingLauncher *mainInstance)
{
if ((s_mainInstance = mainInstance) && !SyncthingProcess::mainInstance()) {
SyncthingProcess::setMainInstance(&mainInstance->m_process);
}
}
SyncthingLauncher SYNCTHINGWIDGETS_EXPORT &syncthingLauncher();
} // namespace Data
#endif // SYNCTHINGWIDGETS_SYNCTHINGLAUNCHER_H