QtWebApp
QtServiceController Class Reference

The QtServiceController class allows you to control services from separate applications. More...

Public Types

enum  StartupType { AutoStartup = 0, ManualStartup }
 

Public Member Functions

 QtServiceController (const QString &name)
 
virtual ~QtServiceController ()
 
bool isInstalled () const
 
bool isRunning () const
 
QString serviceName () const
 
QString serviceDescription () const
 
StartupType startupType () const
 
QString serviceFilePath () const
 
bool uninstall ()
 
bool start (const QStringList &arguments)
 
bool start ()
 
bool stop ()
 
bool pause ()
 
bool resume ()
 
bool sendCommand (int code)
 

Static Public Member Functions

static bool install (const QString &serviceFilePath, const QString &account=QString(), const QString &password=QString())
 

Detailed Description

The QtServiceController class allows you to control services from separate applications.

QtServiceController provides a collection of functions that lets you install and run a service controlling its execution, as well as query its status.

In order to run a service, the service must be installed in the system's service database using the install() function. The system will start the service depending on the specified StartupType; it can either be started during system startup, or when a process starts it manually.

Once a service is installed, the service can be run and controlled manually using the start(), stop(), pause(), resume() or sendCommand() functions. You can at any time query for the service's status using the isInstalled() and isRunning() functions, or you can query its properties using the serviceDescription(), serviceFilePath(), serviceName() and startupType() functions. For example:

MyService service; \\ which inherits QtService
QtServiceController controller(service.serviceName());
if (controller.install(serviceFilePath))
controller.start()
if (controller.isRunning())
QMessageBox::information(this, tr("Service Status"),
tr("The %1 service is started").arg(controller.serviceName()));
...
controller.stop();
controller.uninstall();
}

An instance of the service controller can only control one single service. To control several services within one application, you must create en equal number of service controllers.

The QtServiceController destructor neither stops nor uninstalls the associated service. To stop a service the stop() function must be called explicitly. To uninstall a service, you can use the uninstall() function.

See also
QtServiceBase, QtService

Definition at line 66 of file qtservice.h.

Member Enumeration Documentation

◆ StartupType

This enum describes when a service should be started.

AutoStartup The service is started during system startup. ManualStartup The service must be started manually by a process.

Warning
The StartupType enum is ignored under UNIX-like systems. A service, or daemon, can only be started manually on such systems with current implementation.
See also
startupType()

Definition at line 70 of file qtservice.h.

Constructor & Destructor Documentation

◆ QtServiceController()

QtServiceController::QtServiceController ( const QString &  name)

Creates a controller object for the service with the given name.

Definition at line 209 of file qtservice.cpp.

◆ ~QtServiceController()

QtServiceController::~QtServiceController ( )
virtual

Destroys the service controller. This neither stops nor uninstalls the controlled service.

To stop a service the stop() function must be called explicitly. To uninstall a service, you can use the uninstall() function.

See also
stop(), QtServiceController::uninstall()

Definition at line 226 of file qtservice.cpp.

Member Function Documentation

◆ install()

bool QtServiceController::install ( const QString &  serviceFilePath,
const QString &  account = QString(),
const QString &  password = QString() 
)
static

Installs the service with the given serviceFilePath and returns true if the service is installed successfully; otherwise returns false.

On Windows service is installed in the system's service control manager with the given account and password.

On Unix service configuration is written to QSettings::SystemScope using "QtSoftware" as organization name. account and password arguments are ignored.

Warning
Due to the different implementations of how services (daemons) are installed on various UNIX-like systems, this method doesn't integrate the service into the system's startup scripts.
See also
uninstall(), start()

Definition at line 304 of file qtservice.cpp.

◆ isInstalled()

bool QtServiceController::isInstalled ( ) const

Returns true if the service is installed; otherwise returns false.

On Windows it uses the system's service control manager.

On Unix it checks configuration written to QSettings::SystemScope using "QtSoftware" as organization name.

See also
install()

Definition at line 248 of file qtservice_unix.cpp.

◆ isRunning()

bool QtServiceController::isRunning ( ) const

Returns true if the service is running; otherwise returns false. A service must be installed before it can be run using a controller.

See also
start(), isInstalled()

Definition at line 266 of file qtservice_unix.cpp.

◆ pause()

bool QtServiceController::pause ( )

Requests the running service to pause. If the service's state is QtServiceBase::CanBeSuspended, the service will call the QtServiceBase::pause() implementation. The function does nothing if the service is not running.

Returns true if a running service was successfully paused; otherwise returns false.

See also
resume(), QtServiceBase::pause(), QtServiceBase::ServiceFlags

Definition at line 233 of file qtservice_unix.cpp.

◆ resume()

bool QtServiceController::resume ( )

Requests the running service to continue. If the service's state is QtServiceBase::CanBeSuspended, the service will call the QtServiceBase::resume() implementation. This function does nothing if the service is not running.

Returns true if a running service was successfully resumed; otherwise returns false.

See also
pause(), QtServiceBase::resume(), QtServiceBase::ServiceFlags

Definition at line 238 of file qtservice_unix.cpp.

◆ sendCommand()

bool QtServiceController::sendCommand ( int  code)

Sends the user command code to the service. The service will call the QtServiceBase::processCommand() implementation. This function does nothing if the service is not running.

Returns true if the request was sent to a running service; otherwise returns false.

See also
QtServiceBase::processCommand()

Definition at line 243 of file qtservice_unix.cpp.

◆ serviceDescription()

QString QtServiceController::serviceDescription ( ) const

Returns the description of the controlled service.

See also
install(), serviceName()

Definition at line 157 of file qtservice_unix.cpp.

◆ serviceFilePath()

QString QtServiceController::serviceFilePath ( ) const

Returns the file path to the controlled service.

See also
install(), serviceName()

Definition at line 185 of file qtservice_unix.cpp.

◆ serviceName()

QString QtServiceController::serviceName ( ) const

Returns the name of the controlled service.

See also
QtServiceController(), serviceDescription()

Definition at line 257 of file qtservice.cpp.

◆ start() [1/2]

bool QtServiceController::start ( const QStringList &  arguments)

Starts the installed service passing the given arguments to the service. A service must be installed before a controller can run it.

Returns true if the service could be started; otherwise returns false.

See also
install(), stop()

Definition at line 219 of file qtservice_unix.cpp.

◆ start() [2/2]

bool QtServiceController::start ( )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Starts the installed service without passing any arguments to the service.

Definition at line 346 of file qtservice.cpp.

◆ startupType()

QtServiceController::StartupType QtServiceController::startupType ( ) const

Returns the startup type of the controlled service.

See also
install(), serviceName()

Definition at line 171 of file qtservice_unix.cpp.

◆ stop()

bool QtServiceController::stop ( )

Requests the running service to stop. The service will call the QtServiceBase::stop() implementation unless the service's state is QtServiceBase::CannotBeStopped. This function does nothing if the service is not running.

Returns true if a running service was successfully stopped; otherwise false.

See also
start(), QtServiceBase::stop(), QtServiceBase::ServiceFlags

Definition at line 228 of file qtservice_unix.cpp.

◆ uninstall()

bool QtServiceController::uninstall ( )

Uninstalls the service and returns true if successful; otherwise returns false.

On Windows service is uninstalled using the system's service control manager.

On Unix service configuration is cleared using QSettings::SystemScope with "QtSoftware" as organization name.

See also
install()

Definition at line 199 of file qtservice_unix.cpp.


The documentation for this class was generated from the following files: