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 ****************************************************************************/ 40 41 #ifndef QTSERVICE_H 42 #define QTSERVICE_H 43 44 #include <QCoreApplication> 45 46 #if defined(Q_OS_WIN) 47 # if !defined(QT_QTSERVICE_EXPORT) && !defined(QT_QTSERVICE_IMPORT) 48 # define QT_QTSERVICE_EXPORT 49 # elif defined(QT_QTSERVICE_IMPORT) 50 # if defined(QT_QTSERVICE_EXPORT) 51 # undef QT_QTSERVICE_EXPORT 52 # endif 53 # define QT_QTSERVICE_EXPORT __declspec(dllimport) 54 # elif defined(QT_QTSERVICE_EXPORT) 55 # undef QT_QTSERVICE_EXPORT 56 # define QT_QTSERVICE_EXPORT __declspec(dllexport) 57 # endif 58 #else 59 # define QT_QTSERVICE_EXPORT 60 #endif 61 62 class QStringList; 63 class QtServiceControllerPrivate; 64 65 class QT_QTSERVICE_EXPORT QtServiceController 66 { 67 Q_DECLARE_PRIVATE(QtServiceController) 68 public: 69 enum StartupType 70 { 71 AutoStartup = 0, ManualStartup 72 }; 73 74 QtServiceController(const QString &name); 75 virtual ~QtServiceController(); 76 77 bool isInstalled() const; 78 bool isRunning() const; 79 80 QString serviceName() const; 81 QString serviceDescription() const; 82 StartupType startupType() const; 83 QString serviceFilePath() const; 84 85 static bool install(const QString &serviceFilePath, const QString &account = QString(), 86 const QString &password = QString()); 87 bool uninstall(); 88 89 bool start(const QStringList &arguments); 90 bool start(); 91 bool stop(); 92 bool pause(); 93 bool resume(); 94 bool sendCommand(int code); 95 96 private: 97 QtServiceControllerPrivate *d_ptr; 98 }; 99 100 class QtServiceBasePrivate; 101 102 class QT_QTSERVICE_EXPORT QtServiceBase 103 { 104 Q_DECLARE_PRIVATE(QtServiceBase) 105 public: 106 107 enum MessageType 108 { 109 Success = 0, Error, Warning, Information 110 }; 111 112 enum ServiceFlag 113 { 114 Default = 0x00, 115 CanBeSuspended = 0x01, 116 CannotBeStopped = 0x02, 117 NeedsStopOnShutdown = 0x04 118 }; 119 120 Q_DECLARE_FLAGS(ServiceFlags, ServiceFlag) 121 122 QtServiceBase(int argc, char **argv, const QString &name); 123 virtual ~QtServiceBase(); 124 125 QString serviceName() const; 126 127 QString serviceDescription() const; 128 void setServiceDescription(const QString &description); 129 130 QtServiceController::StartupType startupType() const; 131 void setStartupType(QtServiceController::StartupType startupType); 132 133 ServiceFlags serviceFlags() const; 134 void setServiceFlags(ServiceFlags flags); 135 136 int exec(); 137 138 void logMessage(const QString &message, MessageType type = Success, 139 int id = 0, uint category = 0, const QByteArray &data = QByteArray()); 140 141 static QtServiceBase *instance(); 142 143 protected: 144 145 virtual void start() = 0; 146 virtual void stop(); 147 virtual void pause(); 148 virtual void resume(); 149 virtual void processCommand(int code); 150 151 virtual void createApplication(int &argc, char **argv) = 0; 152 153 virtual int executeApplication() = 0; 154 155 private: 156 157 friend class QtServiceSysPrivate; 158 QtServiceBasePrivate *d_ptr; 159 }; 160 161 template <typename Application> 162 class QtService : public QtServiceBase 163 { 164 public: QtService(int argc,char ** argv,const QString & name)165 QtService(int argc, char **argv, const QString &name) 166 : QtServiceBase(argc, argv, name), app(0) 167 { } ~QtService()168 ~QtService() 169 { 170 } 171 172 protected: application()173 Application *application() const 174 { return app; } 175 createApplication(int & argc,char ** argv)176 virtual void createApplication(int &argc, char **argv) 177 { 178 app = new Application(argc, argv); 179 QCoreApplication *a = app; 180 Q_UNUSED(a); 181 } 182 executeApplication()183 virtual int executeApplication() 184 { return Application::exec(); } 185 186 private: 187 Application *app; 188 }; 189 190 Q_DECLARE_OPERATORS_FOR_FLAGS(QtServiceBase::ServiceFlags) 191 192 #endif // QTSERVICE_H 193