QtService  1.1.0
A platform independent library to easily create system services and use some of their features
servicecontrol.h
1 #ifndef QTSERVICE_SERVICECONTROL_H
2 #define QTSERVICE_SERVICECONTROL_H
3 
4 #include <QtCore/qobject.h>
5 #include <QtCore/qscopedpointer.h>
6 #include <QtCore/qdir.h>
7 #include <QtCore/qvariant.h>
8 #include <QtCore/qexception.h>
9 
10 #include "QtService/qtservice_global.h"
11 
12 namespace QtService {
13 
14 class ServiceControlPrivate;
16 class Q_SERVICE_EXPORT ServiceControl : public QObject
17 {
18  Q_OBJECT
19 
21  Q_PROPERTY(QString backend READ backend CONSTANT)
23  Q_PROPERTY(QString serviceId READ serviceId CONSTANT)
24 
26  Q_PROPERTY(QtService::ServiceControl::SupportFlags supportFlags READ supportFlags CONSTANT)
28  Q_PROPERTY(bool blocking READ isBlocking WRITE setBlocking NOTIFY blockingChanged)
29 
31  Q_PROPERTY(QString error READ error RESET clearError NOTIFY errorChanged)
32 
33 public:
35  enum SupportFlag {
36  SupportsStart = 0x0001,
37  SupportsStop = 0x0002,
38  SupportsPause = 0x0004,
39  SupportsResume = 0x0008,
40  SupportsReload = 0x0010,
41  SupportsGetAutostart = 0x0020,
42  SupportsSetAutostart = 0x0040,
43  SupportsBlocking = 0x0080,
44  SupportsNonBlocking = 0x0100,
45 
46  SupportsStatus = 0x0200,
47  SupportsCustomCommands = 0x0400,
48 
49  SupportsStartStop = (SupportsStart | SupportsStop),
50  SupportsPauseResume = (SupportsPause | SupportsResume),
51  SupportsAutostart = (SupportsGetAutostart | SupportsSetAutostart),
52  SupportsBlockingNonBlocking = (SupportsBlocking | SupportsNonBlocking)
53  };
54  Q_DECLARE_FLAGS(SupportFlags, SupportFlag)
55  Q_FLAG(SupportFlags)
56 
57 
59  ServiceStatusUnknown = 0,
60 
69  ServiceErrored
70  };
71  Q_ENUM(ServiceStatus)
72 
73 
74  static QStringList listBackends();
76  static QString likelyBackend();
78  static ServiceControl *create(const QString &backend, QString serviceId, QObject *parent = nullptr);
80  static ServiceControl *createFromName(const QString &backend, const QString &serviceName, QObject *parent = nullptr);
82  static ServiceControl *createFromName(const QString &backend, const QString &serviceName, const QString &domain, QObject *parent = nullptr);
83 
85  explicit ServiceControl(QString &&serviceId, QObject *parent = nullptr);
86  ~ServiceControl() override;
87 
89  virtual QString backend() const = 0;
91  QString serviceId() const;
93  virtual SupportFlags supportFlags() const = 0;
95  bool isBlocking() const;
97  QString error() const;
98 
100  Q_INVOKABLE virtual QVariant callGenericCommand(const QByteArray &kind, const QVariantList &args = {});
101 
112  template <typename TRet, typename... TArgs>
113  TRet callCommand(const QByteArray &kind, TArgs... args);
123  template <typename... TArgs>
124  void callCommand(const QByteArray &kind, TArgs... args);
125 
127  Q_INVOKABLE virtual bool serviceExists() const = 0;
129  Q_INVOKABLE virtual QtService::ServiceControl::ServiceStatus status() const;
131  Q_INVOKABLE virtual bool isAutostartEnabled() const;
133  Q_INVOKABLE QDir runtimeDir() const;
134 
135 public Q_SLOTS:
137  virtual bool start();
139  virtual bool stop();
140 
142  virtual bool pause();
144  virtual bool resume();
145 
147  virtual bool reload();
148 
150  virtual bool enableAutostart();
152  virtual bool disableAutostart();
153 
155  void setBlocking(bool blocking);
157  void clearError();
158 
159 Q_SIGNALS:
161  void blockingChanged(bool blocking, QPrivateSignal);
163  void errorChanged(QString error, QPrivateSignal);
164 
165 protected:
167  virtual QString serviceName() const;
168 
170  void setError(QString error) const;
171 
172 private:
174 };
175 
176 // ------------- Generic Implementations -------------
177 
178 template<typename TRet, typename... TArgs>
179 TRet ServiceControl::callCommand(const QByteArray &kind, TArgs... args)
180 {
181  return callGenericCommand(kind, {QVariant::fromValue(args)...}).template value<TRet>();
182 }
183 
184 template<typename... TArgs>
185 void ServiceControl::callCommand(const QByteArray &kind, TArgs... args)
186 {
187  callGenericCommand(kind, {QVariant::fromValue(args)...});
188 }
189 
190 }
191 
192 Q_DECLARE_OPERATORS_FOR_FLAGS(QtService::ServiceControl::SupportFlags)
193 
194 #endif // QTSERVICE_SERVICECONTROL_H
ServiceStatus
The different states a service can be in.
The service is currently trying to resume.
The service is currently reloading it&#39;s configuration.
The service is not running.
The primary namespace of the QtService library.
QVariant fromValue(const T &value)
A class to interact with the systems service manager.
The service is currently trying to start.
SupportFlag
Flags that indicate what kind of queries and commands the specific control implementation provides...
The service is currently trying to stop.
The service is currently trying to pause.
TRet callCommand(const QByteArray &kind, TArgs... args)
Calls the command of kind with the given arguments and returns it&#39;s result.
virtual Q_INVOKABLE QVariant callGenericCommand(const QByteArray &kind, const QVariantList &args={})
Calls the command of kind with the given arguments and returns it&#39;s result.