2016-09-03 19:39:43 +02:00
|
|
|
#ifndef DATA_SYNCTHINGPROCESS_H
|
|
|
|
#define DATA_SYNCTHINGPROCESS_H
|
|
|
|
|
2016-09-30 23:55:25 +02:00
|
|
|
#include "./global.h"
|
|
|
|
|
2018-04-02 20:26:00 +02:00
|
|
|
#include <c++utilities/chrono/datetime.h>
|
|
|
|
|
2016-09-03 19:39:43 +02:00
|
|
|
#include <QProcess>
|
2020-04-27 23:08:41 +02:00
|
|
|
#include <QStringList>
|
2018-04-02 23:29:39 +02:00
|
|
|
#include <QTimer>
|
2016-09-03 19:39:43 +02:00
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
|
2017-05-01 03:34:43 +02:00
|
|
|
class LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingProcess : public QProcess {
|
2016-09-03 19:39:43 +02:00
|
|
|
Q_OBJECT
|
2018-04-02 20:26:00 +02:00
|
|
|
Q_PROPERTY(bool running READ isRunning)
|
2019-06-10 22:48:26 +02:00
|
|
|
Q_PROPERTY(CppUtilities::DateTime activeSince READ activeSince)
|
2018-04-02 20:26:00 +02:00
|
|
|
Q_PROPERTY(bool manuallyStopped READ isManuallyStopped)
|
|
|
|
|
2016-09-03 19:39:43 +02:00
|
|
|
public:
|
2017-01-06 15:19:37 +01:00
|
|
|
explicit SyncthingProcess(QObject *parent = nullptr);
|
2018-04-02 20:26:00 +02:00
|
|
|
bool isRunning() const;
|
2019-06-10 22:48:26 +02:00
|
|
|
CppUtilities::DateTime activeSince() const;
|
2018-04-02 20:26:00 +02:00
|
|
|
bool isActiveFor(unsigned int atLeastSeconds) const;
|
|
|
|
bool isManuallyStopped() const;
|
2018-04-11 23:15:15 +02:00
|
|
|
static SyncthingProcess *mainInstance();
|
|
|
|
static void setMainInstance(SyncthingProcess *mainInstance);
|
2018-04-18 23:27:45 +02:00
|
|
|
static QStringList splitArguments(const QString &arguments);
|
2016-09-03 19:39:43 +02:00
|
|
|
|
2018-04-02 23:29:39 +02:00
|
|
|
Q_SIGNALS:
|
|
|
|
void confirmKill();
|
|
|
|
|
2016-09-03 19:39:43 +02:00
|
|
|
public Q_SLOTS:
|
2018-04-18 23:27:45 +02:00
|
|
|
void restartSyncthing(const QString &program, const QStringList &arguments);
|
|
|
|
void startSyncthing(const QString &program, const QStringList &arguments);
|
2017-03-17 00:38:42 +01:00
|
|
|
void stopSyncthing();
|
2018-04-02 23:29:39 +02:00
|
|
|
void killSyncthing();
|
2016-09-03 19:39:43 +02:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
2018-04-02 20:26:00 +02:00
|
|
|
void handleStarted();
|
2016-09-03 19:39:43 +02:00
|
|
|
void handleFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
|
|
|
void killToRestart();
|
|
|
|
|
|
|
|
private:
|
2018-04-18 23:27:45 +02:00
|
|
|
QString m_program;
|
|
|
|
QStringList m_arguments;
|
2019-06-10 22:48:26 +02:00
|
|
|
CppUtilities::DateTime m_activeSince;
|
2018-04-02 23:29:39 +02:00
|
|
|
QTimer m_killTimer;
|
2018-04-02 20:26:00 +02:00
|
|
|
bool m_manuallyStopped;
|
2018-04-11 23:15:15 +02:00
|
|
|
static SyncthingProcess *s_mainInstance;
|
2016-09-03 19:39:43 +02:00
|
|
|
};
|
|
|
|
|
2019-07-13 16:42:19 +02:00
|
|
|
/// \brief Returns whether the process is running.
|
2018-04-02 20:26:00 +02:00
|
|
|
inline bool SyncthingProcess::isRunning() const
|
|
|
|
{
|
|
|
|
return state() != QProcess::NotRunning;
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:42:19 +02:00
|
|
|
/// \brief Returns the last time when QProcess::started() has been emitted.
|
2019-06-10 22:48:26 +02:00
|
|
|
inline CppUtilities::DateTime SyncthingProcess::activeSince() const
|
2018-04-02 20:26:00 +02:00
|
|
|
{
|
|
|
|
return m_activeSince;
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:42:19 +02:00
|
|
|
/// \brief Checks whether the process already runs for the specified number of seconds.
|
2018-04-02 20:26:00 +02:00
|
|
|
inline bool SyncthingProcess::isActiveFor(unsigned int atLeastSeconds) const
|
|
|
|
{
|
2019-06-10 22:48:26 +02:00
|
|
|
return !m_activeSince.isNull() && (CppUtilities::DateTime::gmtNow() - m_activeSince).totalSeconds() > atLeastSeconds;
|
2018-04-02 20:26:00 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 16:42:19 +02:00
|
|
|
/// \brief Returns whether the process has been manually stopped via SyncthingProcess::stopSyncthing(), SyncthingProcess::killSyncthing()
|
|
|
|
/// or SyncthingProcess::restartSyncthing().
|
|
|
|
/// \remarks Resetted on SyncthingProcess::startSyncthing() and SyncthingProcess::restartSyncthing().
|
2018-04-02 20:26:00 +02:00
|
|
|
inline bool SyncthingProcess::isManuallyStopped() const
|
|
|
|
{
|
|
|
|
return m_manuallyStopped;
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:42:19 +02:00
|
|
|
/*!
|
|
|
|
* \brief Returns the "main" instance assigned via SyncthingProcess::setMainInstance().
|
|
|
|
*/
|
2018-04-11 23:15:15 +02:00
|
|
|
inline SyncthingProcess *SyncthingProcess::mainInstance()
|
|
|
|
{
|
|
|
|
return s_mainInstance;
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:42:19 +02:00
|
|
|
/*!
|
|
|
|
* \brief Sets the "main" instance.
|
|
|
|
*/
|
2018-04-11 23:15:15 +02:00
|
|
|
inline void SyncthingProcess::setMainInstance(SyncthingProcess *mainInstance)
|
|
|
|
{
|
|
|
|
s_mainInstance = mainInstance;
|
|
|
|
}
|
2016-09-03 19:39:43 +02:00
|
|
|
|
|
|
|
} // namespace Data
|
|
|
|
|
|
|
|
#endif // DATA_SYNCTHINGPROCESS_H
|