Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ServiceManager.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "ServiceManager.h"
4#include "RabbitCommonDir.h"
5#include "Service.h"
6#ifdef HAVE_ICE
7 #include "Ice.h"
8#endif
9
10#include "PluginService.h"
11#include <QCommandLineOption>
12#include <QCommandLineParser>
13#include <QDebug>
14#include <QSettings>
15#include <QLoggingCategory>
16#include <stdexcept>
17
18Q_LOGGING_CATEGORY(Service, "Service")
19
20CServiceManager::CServiceManager(int argc, char **argv, const QString& appName, const QString &name)
21 : QtService<QCoreApplication>(argc, argv, name)
22{
23 // Add default parameters
24 QStringList args;
25 for (int i = 0; i < argc; ++i)
26 args.append(QString::fromLocal8Bit(argv[i]));
27 if(args.size() >= 2)
28 {
29 QString a = args.at(1);
30 if (a == QLatin1String("-s") || a == QLatin1String("-save"))
31 {
32 createApplication(argc, argv);
33 application()->setApplicationName(appName);
34 QString szDir;
35 if(args.size() >= 3)
36 szDir = args.at(2);
37 if(szDir.isEmpty())
38 szDir = RabbitCommon::CDir::Instance()->GetDirUserConfig();
39 if(!szDir.isEmpty())
40 {
41 // Make sure the plugin is loaded only after the application is created
42 if(!m_Plugins)
43 m_Plugins = QSharedPointer<CManagePlugins>(new CManagePlugins());
44 foreach(auto p, m_Plugins->m_Plugins)
45 {
46 if(p)
47 {
48 CService* pService = p->NewService();
49 if(pService)
50 pService->SaveConfigure(szDir);
51 }
52 }
53
54#ifdef HAVE_ICE
55 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
56 QSettings::IniFormat);
57 CICE::Instance()->GetParameter()->Save(set);
58 set.sync();
59#endif
60 application()->quit();
61 return;
62 }
63 } else if (a == QLatin1String("-h") || a == QLatin1String("-help")) {
64 exec();
65 printf("\t-s(ave) [Directory] \t: Generate configuration file in [Directory]\n");
66 throw std::invalid_argument("Help argument");
67 }
68 }
69
70#ifdef HAVE_ICE
71 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
72 QSettings::IniFormat);
73 CICE::Instance()->GetParameter()->Load(set);
74#endif
75}
76
77CServiceManager::~CServiceManager()
78{
79 qDebug(Service) << "CServiceManager::~CServiceManager";
80}
81
82void CServiceManager::start()
83{
84 qDebug(Service) << "Service", "Start ...";
85 // Make sure the plugin is loaded only after the application is created
86 if(!m_Plugins)
87 m_Plugins = QSharedPointer<CManagePlugins>(new CManagePlugins());
88 foreach(auto p, m_Plugins->m_Plugins)
89 if(p) p->Start();
90
91#ifdef HAVE_ICE
92 CICE::Instance()->slotStart();
93#endif
94}
95
96void CServiceManager::stop()
97{
98 qDebug(Service) << "Service", "Stop ...";
99 if(!m_Plugins) return;
100 foreach(auto p, m_Plugins->m_Plugins)
101 if(p) p->Stop();
102
103#ifdef HAVE_ICE
104 CICE::Instance()->slotStop();
105#endif
106}
static CICE * Instance()
Single instance.
Definition Ice.cpp:55
Manage plugin.
Manage service.
The service interface.
Definition Service.h:38