From 6aa77e72526601dc787e16c56d223fb5c536a61d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 28 Dec 2021 08:52:49 +0300 Subject: [PATCH] ServiceInfoManager: Emit serviceChanged() signal --- src/ui/serviceinfo/serviceinfomanager.cpp | 30 +++++++++++++++++------ src/ui/serviceinfo/serviceinfomanager.h | 4 +-- src/ui/serviceinfo/serviceinfomonitor.cpp | 21 ++++++++++------ src/ui/serviceinfo/serviceinfomonitor.h | 5 ++-- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/ui/serviceinfo/serviceinfomanager.cpp b/src/ui/serviceinfo/serviceinfomanager.cpp index ae4ac833..c9ab4bf1 100644 --- a/src/ui/serviceinfo/serviceinfomanager.cpp +++ b/src/ui/serviceinfo/serviceinfomanager.cpp @@ -95,9 +95,11 @@ void ServiceInfoManager::setupServiceMonitors() return; } - const auto services = getServiceInfoList(mngr); - for (const auto &info : services) { - startServiceMonitor(info.serviceName, info.processId, mngr); + if (!m_serviceGroups.isEmpty()) { + const auto services = getServiceInfoList(mngr); + for (const auto &info : services) { + startServiceMonitor(info.serviceName, info.processId, mngr); + } } startServiceListMonitor(mngr); @@ -116,13 +118,13 @@ void ServiceInfoManager::clearServiceMonitors() void ServiceInfoManager::startServiceMonitor( const QString &name, quint32 processId, void *managerHandle) { + if (!m_serviceGroups.contains(name)) + return; + auto m = new ServiceInfoMonitor(processId, name, managerHandle); connect(m, &ServiceInfoMonitor::stateChanged, this, &ServiceInfoManager::onServiceStateChanged, Qt::QueuedConnection); - connect( - m, &ServiceInfoMonitor::errorOccurred, this, [=] { stopServiceMonitor(m); }, - Qt::QueuedConnection); m_serviceMonitors.insert(name, m); } @@ -159,14 +161,28 @@ void ServiceInfoManager::stopServiceListMonitor() m->deleteLater(); } -void ServiceInfoManager::onServiceStateChanged(ServiceInfo::State state) +void ServiceInfoManager::onServiceStateChanged(ServiceInfo::State state, quint32 processId) { const auto m = qobject_cast(sender()); Q_ASSERT(m); if (state == ServiceInfo::StateDeleted) { + Q_ASSERT(processId == 0); + processId = m->processId(); + if (processId != 0) { + state = ServiceInfo::StateInactive; + } + stopServiceMonitor(m); } + + const int groupIndex = groupIndexByName(m->name()); + if (groupIndex == -1) + return; + + Q_ASSERT(processId != 0); + + emit serviceChanged(processId, (state == ServiceInfo::StateActive ? groupIndex : -1)); } void ServiceInfoManager::onServiceCreated(const QStringList &nameList) diff --git a/src/ui/serviceinfo/serviceinfomanager.h b/src/ui/serviceinfo/serviceinfomanager.h index c8f21149..551ab262 100644 --- a/src/ui/serviceinfo/serviceinfomanager.h +++ b/src/ui/serviceinfo/serviceinfomanager.h @@ -27,7 +27,7 @@ public: static QVector loadServiceInfoList(); signals: - void servicesChanged(); + void serviceChanged(quint32 processId, int groupIndex = -1); private: void setupServiceMonitors(); @@ -40,7 +40,7 @@ private: void startServiceListMonitor(void *managerHandle = nullptr); void stopServiceListMonitor(); - void onServiceStateChanged(ServiceInfo::State state); + void onServiceStateChanged(ServiceInfo::State state, quint32 processId = 0); void onServiceCreated(const QStringList &nameList); private: diff --git a/src/ui/serviceinfo/serviceinfomonitor.cpp b/src/ui/serviceinfo/serviceinfomonitor.cpp index 7d3fd4a4..70206b95 100644 --- a/src/ui/serviceinfo/serviceinfomonitor.cpp +++ b/src/ui/serviceinfo/serviceinfomonitor.cpp @@ -29,16 +29,16 @@ static void CALLBACK notifyCallback(PVOID parameter) } const bool running = (notify->ServiceStatus.dwCurrentState == SERVICE_RUNNING); - m->setRunning(running); - if (running) { - m->setProcessId(notify->ServiceStatus.dwProcessId); - } + + const quint32 processId = notify->ServiceStatus.dwProcessId; + const quint32 oldProcessId = m->processId(); + m->setProcessId(processId); const ServiceInfo::State state = running ? ServiceInfo::StateActive : ServiceInfo::StateInactive; - emit m->stateChanged(state); + emit m->stateChanged(state, (running ? processId : oldProcessId)); // NotifyServiceStatusChange() must not be called from the callback m->requestStartNotifier(); @@ -129,7 +129,7 @@ void ServiceInfoMonitor::openService(void *managerHandle) } break; default: qCCritical(LC) << "Open service error:" << name() << res; - emit errorOccurred(); + errorOccurred(); } return; } @@ -149,7 +149,7 @@ void ServiceInfoMonitor::reopenService() { if (m_isReopening) { qCCritical(LC) << "Reopen service error:" << name(); - emit errorOccurred(); + errorOccurred(); return; } @@ -188,7 +188,12 @@ void ServiceInfoMonitor::startNotifier() } break; default: qCCritical(LC) << "Start notifier error:" << name() << res; - emit errorOccurred(); + errorOccurred(); } } } + +void ServiceInfoMonitor::errorOccurred() +{ + emit stateChanged(ServiceInfo::StateDeleted); +} diff --git a/src/ui/serviceinfo/serviceinfomonitor.h b/src/ui/serviceinfo/serviceinfomonitor.h index fc9e3d4a..99d3a16d 100644 --- a/src/ui/serviceinfo/serviceinfomonitor.h +++ b/src/ui/serviceinfo/serviceinfomonitor.h @@ -25,8 +25,7 @@ public: QVector ¬ifyBuffer() { return m_notifyBuffer; } signals: - void stateChanged(ServiceInfo::State state); - void errorOccurred(); + void stateChanged(ServiceInfo::State state, quint32 processId = 0); public slots: void terminate(); @@ -41,6 +40,8 @@ private: void startNotifier(); + void errorOccurred(); + private: bool m_terminated : 1; bool m_isReopening : 1;