QtWebApp
qtservice.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the Qt Solutions component.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
45 #ifndef QTSERVICE_H
46 #define QTSERVICE_H
47 
48 #include <QtGlobal>
49 #include <QCoreApplication>
50 
51 // This is specific to Windows dll's
52 #if defined(Q_OS_WIN)
53  #if defined(QTWEBAPPLIB_EXPORT)
54  #define DECLSPEC Q_DECL_EXPORT
55  #elif defined(QTWEBAPPLIB_IMPORT)
56  #define DECLSPEC Q_DECL_IMPORT
57  #endif
58 #endif
59 #if !defined(DECLSPEC)
60  #define DECLSPEC
61 #endif
62 
63 class QStringList;
65 
66 class DECLSPEC QtServiceController
67 {
68  Q_DECLARE_PRIVATE(QtServiceController)
69 public:
71  {
72  AutoStartup = 0, ManualStartup
73  };
74 
75  QtServiceController(const QString &name);
76  virtual ~QtServiceController();
77 
78  bool isInstalled() const;
79  bool isRunning() const;
80 
81  QString serviceName() const;
82  QString serviceDescription() const;
83  StartupType startupType() const;
84  QString serviceFilePath() const;
85 
86  static bool install(const QString &serviceFilePath, const QString &account = QString(),
87  const QString &password = QString());
88  bool uninstall();
89 
90  bool start(const QStringList &arguments);
91  bool start();
92  bool stop();
93  bool pause();
94  bool resume();
95  bool sendCommand(int code);
96 
97 private:
99 };
100 
102 
103 class DECLSPEC QtServiceBase
104 {
105  Q_DECLARE_PRIVATE(QtServiceBase)
106 public:
107 
109  {
110  Success = 0, Error, Warning, Information
111  };
112 
114  {
115  Default = 0x00,
116  CanBeSuspended = 0x01,
117  CannotBeStopped = 0x02,
118  NeedsStopOnShutdown = 0x04
119  };
120 
121  Q_DECLARE_FLAGS(ServiceFlags, ServiceFlag)
122 
123  QtServiceBase(int argc, char **argv, const QString &name);
124  virtual ~QtServiceBase();
125 
126  QString serviceName() const;
127 
128  QString serviceDescription() const;
129  void setServiceDescription(const QString &description);
130 
131  QtServiceController::StartupType startupType() const;
132  void setStartupType(QtServiceController::StartupType startupType);
133 
134  ServiceFlags serviceFlags() const;
135  void setServiceFlags(ServiceFlags flags);
136 
137  int exec();
138 
139  void logMessage(const QString &message, MessageType type = Success,
140  int id = 0, uint category = 0, const QByteArray &data = QByteArray());
141 
142  static QtServiceBase *instance();
143 
144 protected:
145 
146  virtual void start() = 0;
147  virtual void stop();
148  virtual void pause();
149  virtual void resume();
150  virtual void processCommand(int code);
151 
152  virtual void createApplication(int &argc, char **argv) = 0;
153 
154  virtual int executeApplication() = 0;
155 
156 private:
157 
158  friend class QtServiceSysPrivate;
159  QtServiceBasePrivate *d_ptr;
160 };
161 
162 template <typename Application>
163 class QtService : public QtServiceBase
164 {
165 public:
166  QtService(int argc, char **argv, const QString &name)
167  : QtServiceBase(argc, argv, name), app(0)
168  { }
170  {
171  }
172 
173 protected:
174  Application *application() const
175  { return app; }
176 
177  virtual void createApplication(int &argc, char **argv)
178  {
179  app = new Application(argc, argv);
180  QCoreApplication *a = app;
181  Q_UNUSED(a);
182  }
183 
184  virtual int executeApplication()
185  { return Application::exec(); }
186 
187 private:
188  Application *app;
189 };
190 
191 Q_DECLARE_OPERATORS_FOR_FLAGS(QtServiceBase::ServiceFlags)
192 
193 #endif // QTSERVICE_H
~QtService()
Definition: qtservice.h:169
QtService(int argc, char **argv, const QString &name)
Definition: qtservice.h:166
The QtServiceBase class provides an API for implementing Windows services and Unix daemons...
Definition: qtservice.h:103
Application * application() const
Definition: qtservice.h:174
The QtServiceController class allows you to control services from separate applications.
Definition: qtservice.h:66
The QtService is a convenient template class that allows you to create a service for a particular app...
Definition: qtservice.h:163
virtual void createApplication(int &argc, char **argv)
Definition: qtservice.h:177
virtual int executeApplication()
Definition: qtservice.h:184