QtService
1.1.0
A platform independent library to easily create system services and use some of their features
|
A class to interact with the systems service manager. More...
#include <servicecontrol.h>
Public Types | |
enum | SupportFlag { SupportsStart = 0x0001, SupportsStop = 0x0002, SupportsPause = 0x0004, SupportsResume = 0x0008, SupportsReload = 0x0010, SupportsGetAutostart = 0x0020, SupportsSetAutostart = 0x0040, SupportsBlocking = 0x0080, SupportsNonBlocking = 0x0100, SupportsStatus = 0x0200, SupportsCustomCommands = 0x0400, SupportsStartStop = (SupportsStart | SupportsStop), SupportsPauseResume = (SupportsPause | SupportsResume), SupportsAutostart = (SupportsGetAutostart | SupportsSetAutostart), SupportsBlockingNonBlocking = (SupportsBlocking | SupportsNonBlocking) } |
Flags that indicate what kind of queries and commands the specific control implementation provides. More... | |
enum | ServiceStatus { ServiceStatusUnknown = 0, ServiceStopped, ServiceStarting, ServiceRunning, ServicePausing, ServicePaused, ServiceResuming, ServiceReloading, ServiceStopping, ServiceErrored } |
The different states a service can be in. More... | |
Public Slots | |
virtual bool | start () |
Send a start command for the controls service to the service manager. More... | |
virtual bool | stop () |
Send a stop command for the controls service to the service manager. More... | |
virtual bool | pause () |
Send a pause command for the controls service to the service manager. More... | |
virtual bool | resume () |
Send a resume command for the controls service to the service manager. More... | |
virtual bool | reload () |
Send a reload command for the controls service to the service manager. More... | |
virtual bool | enableAutostart () |
Enables autostart for the controls service. More... | |
virtual bool | disableAutostart () |
Disables autostart for the controls service. More... | |
void | setBlocking (bool blocking) |
WRITE accessor for ServiceControl::blocking. | |
void | clearError () |
RESET accessor for ServiceControl::error. | |
Signals | |
void | blockingChanged (bool blocking, QPrivateSignal) |
NOTIFY accessor for ServiceControl::blocking. | |
void | errorChanged (QString error, QPrivateSignal) |
NOTIFY accessor for ServiceControl::error. | |
Public Member Functions | |
virtual QString | backend () const =0 |
READ accessor for ServiceControl::backend. | |
QString | serviceId () const |
READ accessor for ServiceControl::serviceId. | |
virtual SupportFlags | supportFlags () const =0 |
READ accessor for ServiceControl::supportFlags. | |
bool | isBlocking () const |
READ accessor for ServiceControl::blocking. | |
QString | error () const |
READ accessor for ServiceControl::error. | |
virtual Q_INVOKABLE QVariant | callGenericCommand (const QByteArray &kind, const QVariantList &args={}) |
Calls the command of kind with the given arguments and returns it's result. More... | |
template<typename TRet , typename... TArgs> | |
TRet | callCommand (const QByteArray &kind, TArgs... args) |
Calls the command of kind with the given arguments and returns it's result. More... | |
template<typename... TArgs> | |
void | callCommand (const QByteArray &kind, TArgs... args) |
Calls the command of kind with the given arguments and returns it's result. More... | |
virtual Q_INVOKABLE bool | serviceExists () const =0 |
Checks if the service this control was created for actually exists. More... | |
virtual Q_INVOKABLE QtService::ServiceControl::ServiceStatus | status () const |
Returns the current status of the service. More... | |
virtual Q_INVOKABLE bool | isAutostartEnabled () const |
Returns the current autostart state of the service. More... | |
Q_INVOKABLE QDir | runtimeDir () const |
Returns the runtime directory of this controls service. More... | |
Public Member Functions inherited from QObject | |
virtual const QMetaObject * | metaObject () const const |
virtual void * | qt_metacast (const char *) |
virtual int | qt_metacall (QMetaObject::Call, int, void **) |
QObject (QObject *parent) | |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
QString | objectName () const const |
void | setObjectName (const QString &name) |
bool | isWidgetType () const const |
bool | isWindowType () const const |
bool | signalsBlocked () const const |
bool | blockSignals (bool block) |
QThread * | thread () const const |
void | moveToThread (QThread *targetThread) |
int | startTimer (int interval, Qt::TimerType timerType) |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType) |
void | killTimer (int id) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegExp ®Exp, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
const QObjectList & | children () const const |
void | setParent (QObject *parent) |
void | installEventFilter (QObject *filterObj) |
void | removeEventFilter (QObject *obj) |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
bool | disconnect (const QObject *receiver, const char *method) const const |
void | dumpObjectTree () |
void | dumpObjectInfo () |
void | dumpObjectTree () const const |
void | dumpObjectInfo () const const |
bool | setProperty (const char *name, const QVariant &value) |
QVariant | property (const char *name) const const |
QList< QByteArray > | dynamicPropertyNames () const const |
void | destroyed (QObject *obj) |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () const const |
bool | inherits (const char *className) const const |
void | deleteLater () |
Static Public Member Functions | |
static QStringList | listBackends () |
Returns a list of all available backends. | |
static QString | likelyBackend () |
Returns the backend that is most likely to be used on the current platform. More... | |
static ServiceControl * | create (const QString &backend, QString serviceId, QObject *parent=nullptr) |
Creates a new ServiceControl for the given service on the service manager defined by backend. More... | |
static ServiceControl * | createFromName (const QString &backend, const QString &serviceName, QObject *parent=nullptr) |
Creates a new ServiceControl by guessing the service id from the given name and this applications domain. More... | |
static ServiceControl * | createFromName (const QString &backend, const QString &serviceName, const QString &domain, QObject *parent=nullptr) |
Creates a new ServiceControl by guessing the service id from the given name and domain. More... | |
Static Public Member Functions inherited from QObject | |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
QString | trUtf8 (const char *sourceText, const char *disambiguation, int n) |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
bool | disconnect (const QMetaObject::Connection &connection) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
Protected Member Functions | |
virtual QString | serviceName () const |
Returns the common name of the controls service. More... | |
void | setError (QString error) const |
WRITE accessor for ServiceControl::error. | |
Protected Member Functions inherited from QObject | |
QObject * | sender () const const |
int | senderSignalIndex () const const |
int | receivers (const char *signal) const const |
bool | isSignalConnected (const QMetaMethod &signal) const const |
virtual void | timerEvent (QTimerEvent *event) |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
Properties | |
QString | backend |
The service backend this control interacts with. More... | |
QString | serviceId |
The ID of the service this control manages. More... | |
QtService::ServiceControl::SupportFlags | supportFlags |
The features that this implementation supports. More... | |
bool | blocking |
Specifies if service control commands are blocking. More... | |
QString | error |
A string describing the last error that occured. More... | |
Properties inherited from QObject | |
objectName | |
A class to interact with the systems service manager.
This class can be used to get information about a specific service and control it by sending service commands to it through the service manager. This can be used for all services of the corresponding manager, not only services created by this library.
The concrete instances are implemented by each service backend plugin, thus the creation via a helper method.
Definition at line 16 of file servicecontrol.h.
The different states a service can be in.
Definition at line 58 of file servicecontrol.h.
Flags that indicate what kind of queries and commands the specific control implementation provides.
Enumerator | |
---|---|
SupportsStart | Can start services via ServiceControl::start. |
SupportsStop | Can stop services via ServiceControl::stop. |
SupportsPause | Can pause services via ServiceControl::pause. |
SupportsResume | Can resume services via ServiceControl::resume. |
SupportsReload | Can reload services via ServiceControl::reload. |
SupportsGetAutostart | Can read the autostart state of a service. |
SupportsSetAutostart | Can write the autostart state of a service. |
SupportsBlocking | Supports service commands that block for the execution. |
SupportsNonBlocking | Supports service commands the only trigger execution without blocking. |
SupportsStatus | Can read the current status of a service. |
SupportsCustomCommands | Supports the execution of specific custom commands. |
SupportsStartStop | SupportsStart | SupportsStop. |
SupportsPauseResume | SupportsPause | SupportsResume. |
SupportsAutostart | SupportsGetAutostart | SupportsSetAutostart. |
SupportsBlockingNonBlocking | SupportsBlocking | SupportsNonBlocking. |
Definition at line 35 of file servicecontrol.h.
TRet QtService::ServiceControl::callCommand | ( | const QByteArray & | kind, |
TArgs... | args | ||
) |
Calls the command of kind with the given arguments and returns it's result.
TRet | The return value of the command |
TArgs | The arguments passed to the command |
kind | The kind of command to be called |
args | The arguments of the command |
Custom commands can only be used if the control supports the ServiceControl::SupportsCustomCommands flag is set. If the operation is not supported, an invalid variant will be returned and ServiceControl::error is set.
This method can be used to execute a custom command, i.e. one that is not a standard command. Each backend can define it's own custom commands. The kind is what should be done and the arguments are parameters for the execution of that command. The command will follow the standard blocking rules and return a value of whatever kind.
The commands that are possible are defined by each backend. For the standard backends you can find the commans on the Supported Service Backends Page.
This generic variant will automatically convert the arguments to variant types and pass the to the callGenericCommand() method.
Definition at line 179 of file servicecontrol.h.
void QtService::ServiceControl::callCommand | ( | const QByteArray & | kind, |
TArgs... | args | ||
) |
Calls the command of kind with the given arguments and returns it's result.
TArgs | The arguments passed to the command |
kind | The kind of command to be called |
args | The arguments of the command |
Custom commands can only be used if the control supports the ServiceControl::SupportsCustomCommands flag is set. If the operation is not supported, an invalid variant will be returned and ServiceControl::error is set.
This method can be used to execute a custom command, i.e. one that is not a standard command. Each backend can define it's own custom commands. The kind is what should be done and the arguments are parameters for the execution of that command. The command will follow the standard blocking rules and return a value of whatever kind.
The commands that are possible are defined by each backend. For the standard backends you can find the commans on the Supported Service Backends Page.
This generic variant will automatically convert the arguments to variant types and pass the to the callGenericCommand() method.
Definition at line 185 of file servicecontrol.h.
|
virtual |
Calls the command of kind with the given arguments and returns it's result.
kind | The kind of command to be called |
args | The arguments of the command |
Custom commands can only be used if the control supports the ServiceControl::SupportsCustomCommands flag is set. If the operation is not supported, an invalid variant will be returned and ServiceControl::error is set.
This method can be used to execute a custom command, i.e. one that is not a standard command. Each backend can define it's own custom commands. The kind is what should be done and the arguments are parameters for the execution of that command. The command will follow the standard blocking rules and return a value of whatever kind.
The commands that are possible are defined by each backend. For the standard backends you can find the commans on the Supported Service Backends Page.
|
static |
Creates a new ServiceControl for the given service on the service manager defined by backend.
backend | The backend to create a control from |
serviceId | The id of the service to control |
parent | The parent object of the new control |
QException | In case loading fails |
Uses the plugin defined by backend to create a service control for the given service. If no plugin can be found that provides this backend, nullptr is returned. If a plugin was found, but actually loading it and creating the control failed, an exception is thrown.
The returned instance is owned by the caller. To check if the control operates on a valid service, use ServiceControl::serviceExists
|
static |
Creates a new ServiceControl by guessing the service id from the given name and this applications domain.
backend | The backend to create a control from |
serviceName | The generic name of the service to control |
parent | The parent object of the new control |
QException | In case loading fails |
Uses the plugin defined by backend to create a service control for the given service. If no plugin can be found that provides this backend, nullptr is returned. If a plugin was found, but actually loading it and creating the control failed, an exception is thrown.
The service identity is derived from the name and domain. The actual method depends on the backend:
Backend | Deriviation method |
---|---|
standard | Searches for an executable named <serviceName> next to the app and in the path |
systemd | First tries <domain>.<serviceName>.service , if not found <serviceName>.service |
windows | Simply <serviceName> |
launchd | Uses <domain>.<serviceName> |
android | Generates the java class name <domain>.<serviceName> |
The returned instance is owned by the caller. To check if the control operates on a valid service, use ServiceControl::serviceExists
|
static |
Creates a new ServiceControl by guessing the service id from the given name and domain.
domain | The domain of the service, instead of the domain of this application |
backend | The backend to create a control from |
serviceName | The generic name of the service to control |
parent | The parent object of the new control |
QException | In case loading fails |
Uses the plugin defined by backend to create a service control for the given service. If no plugin can be found that provides this backend, nullptr is returned. If a plugin was found, but actually loading it and creating the control failed, an exception is thrown.
The service identity is derived from the name and domain. The actual method depends on the backend:
Backend | Deriviation method |
---|---|
standard | Searches for an executable named <serviceName> next to the app and in the path |
systemd | First tries <domain>.<serviceName>.service , if not found <serviceName>.service |
windows | Simply <serviceName> |
launchd | Uses <domain>.<serviceName> |
android | Generates the java class name <domain>.<serviceName> |
The returned instance is owned by the caller. To check if the control operates on a valid service, use ServiceControl::serviceExists
|
virtualslot |
Disables autostart for the controls service.
The command can only be used if the control supports the ServiceControl::SupportsSetAutostart flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The method will only set the autostart state. This will not stop a service that is currently running.
|
virtualslot |
Enables autostart for the controls service.
The command can only be used if the control supports the ServiceControl::SupportsSetAutostart flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The method will only set the autostart state. This will not start a service that is currently not running.
|
virtual |
Returns the current autostart state of the service.
The status can only be returned if the control supports the ServiceControl::SupportsGetAutostart flag is set.
A backend should query the manager from this method in a synchronous manner to get and return the autostart status. If the backend fails to find out, ServiceControl::setError can be used to set an error string and false should be returned.
|
static |
Returns the backend that is most likely to be used on the current platform.
Platform | Backend |
---|---|
Android | android |
Linux | systemd |
Windows | windows |
MacOs | launchd |
Others | standard |
|
virtualslot |
Send a pause command for the controls service to the service manager.
The command can only be used if the control supports the ServiceControl::SupportsPause flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The command will follow the standard blocking rules and return true either after successfully pausing or dispatching the pause command.
|
virtualslot |
Send a reload command for the controls service to the service manager.
The command can only be used if the control supports the ServiceControl::SupportsReload flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The command will follow the standard blocking rules and return true either after successfully reloading or dispatching the reload command.
|
virtualslot |
Send a resume command for the controls service to the service manager.
The command can only be used if the control supports the ServiceControl::SupportsResume flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The command will follow the standard blocking rules and return true either after successfully resuming or dispatching the resume command.
QtService::ServiceControl::runtimeDir | ( | ) | const |
Returns the runtime directory of this controls service.
This directoy is determined by the service engine to find the runtime directory of a service on each platform. This should always be the same value as the Service::runtimeDir for the service controlled by this instance.
|
pure virtual |
Checks if the service this control was created for actually exists.
This method uses the service backend to perform a check whether the service defined by ServiceControl::serviceId actually exists. On errors, ServiceControl::error will be set.
For backends that cannot determine this they should simply always return true. If the backend fails to find out, ServiceControl::setError can be used to set an error string.
|
protectedvirtual |
Returns the common name of the controls service.
This should be the QCoreApplication::name of the target service. This however is only a guessed value and is only valid if the service follows the standard naming rules.
|
virtualslot |
Send a start command for the controls service to the service manager.
The command can only be used if the control supports the ServiceControl::SupportsStart flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The command will follow the standard blocking rules and return true either after successfully starting or dispatching the start command.
|
virtual |
Returns the current status of the service.
The status can only be returned if the control supports the ServiceControl::SupportsStatus flag is set. The kinds of status returned differ on the capabilities of each service manager. If the operation is not supported, ServiceControl::ServiceStatusUnknown will be returned and ServiceControl::error is set.
A backend should query the manager from this method in a synchronous manner to get and return the service status. If the backend fails to find out, ServiceControl::setError can be used to set an error string and ServiceControl::ServiceStatusUnknown should be returned to indicate the error.
|
virtualslot |
Send a stop command for the controls service to the service manager.
The command can only be used if the control supports the ServiceControl::SupportsStop flag is set. If the operation is not supported, false will be returned and ServiceControl::error is set.
The command will follow the standard blocking rules and return true either after successfully stopping or dispatching the stop command.
|
read |
The service backend this control interacts with.
Default: Set on creation
This is simply the backend this control was created by. Typically this is exactly the same as what was passed to the ServiceControl::create function - allthough for backends that have multiple names this may not be the case.
Accessors | |
---|---|
READ | backend() |
CONSTANT |
Definition at line 21 of file servicecontrol.h.
|
readwrite |
Specifies if service control commands are blocking.
Default: supportFlags().testFlag(SupportsBlocking)
Whether a backend supports either of those modes depends on the support flags. The following combinations are possible:
SupportsBlocking | SupportsNonBlocking | Behaviour |
---|---|---|
yes | yes | The behaviour of all commands is determined by this property |
yes | no | This property is ignored - services will always behave blocking |
no | yes | This property is ignored - services will always behave non blocking |
no | no | This property is ignored - the services behaviour is undefined - some methods may be blocking, other unblocking |
If commands are blocking, it means that the control won't return from a command until it's execution has been completed. So if you for example start a service in blocking mode, the method will return only after the service reached the running state or error. In this case the method will return the actual result of the operation.
In nonblocking mode the command is only dispatched to the service manager and true is returned if the manager accepted the command. You will have to use ServiceControl::status to check the services state, if available.
Accessors | |
---|---|
READ | isBlocking() |
WRITE | setBlocking() |
NOTIFY | blockingChanged() |
Definition at line 28 of file servicecontrol.h.
|
read |
A string describing the last error that occured.
Default: null string
This error can be set form the implementations in case an operation or query failed. It is a localized string describing what went wrong. The error message is not cleared automatically. Use clearError() to clear it.
Use setError() to set an error from the implementation
Accessors | |
---|---|
READ | error() |
RESET | clearError() |
NOTIFY | errorChanged() |
Definition at line 31 of file servicecontrol.h.
|
read |
The ID of the service this control manages.
Default: Set on creation
This is simply the service id this control was created by. Typically this is exactly the same as what was passed to the ServiceControl::create function, but backends may normalize the name if applicable.
Accessors | |
---|---|
READ | serviceId() |
CONSTANT |
Definition at line 23 of file servicecontrol.h.
|
read |
The features that this implementation supports.
Default: Depends on backend
The support flags indicate what this concrete implementation is capable of doing. The flags returned depend on the backend and should be checked before performing an operation, as performing an unsupported operation will cause an error.
Accessors | |
---|---|
READ | supportFlags() |
CONSTANT |
Definition at line 26 of file servicecontrol.h.