ServiceInfoManager: Emit serviceChanged() signal

This commit is contained in:
Nodir Temirkhodjaev 2021-12-28 08:52:49 +03:00
parent ae781a7d63
commit 6aa77e7252
4 changed files with 41 additions and 19 deletions

View File

@ -95,10 +95,12 @@ void ServiceInfoManager::setupServiceMonitors()
return; return;
} }
if (!m_serviceGroups.isEmpty()) {
const auto services = getServiceInfoList(mngr); const auto services = getServiceInfoList(mngr);
for (const auto &info : services) { for (const auto &info : services) {
startServiceMonitor(info.serviceName, info.processId, mngr); startServiceMonitor(info.serviceName, info.processId, mngr);
} }
}
startServiceListMonitor(mngr); startServiceListMonitor(mngr);
} }
@ -116,13 +118,13 @@ void ServiceInfoManager::clearServiceMonitors()
void ServiceInfoManager::startServiceMonitor( void ServiceInfoManager::startServiceMonitor(
const QString &name, quint32 processId, void *managerHandle) const QString &name, quint32 processId, void *managerHandle)
{ {
if (!m_serviceGroups.contains(name))
return;
auto m = new ServiceInfoMonitor(processId, name, managerHandle); auto m = new ServiceInfoMonitor(processId, name, managerHandle);
connect(m, &ServiceInfoMonitor::stateChanged, this, &ServiceInfoManager::onServiceStateChanged, connect(m, &ServiceInfoMonitor::stateChanged, this, &ServiceInfoManager::onServiceStateChanged,
Qt::QueuedConnection); Qt::QueuedConnection);
connect(
m, &ServiceInfoMonitor::errorOccurred, this, [=] { stopServiceMonitor(m); },
Qt::QueuedConnection);
m_serviceMonitors.insert(name, m); m_serviceMonitors.insert(name, m);
} }
@ -159,14 +161,28 @@ void ServiceInfoManager::stopServiceListMonitor()
m->deleteLater(); m->deleteLater();
} }
void ServiceInfoManager::onServiceStateChanged(ServiceInfo::State state) void ServiceInfoManager::onServiceStateChanged(ServiceInfo::State state, quint32 processId)
{ {
const auto m = qobject_cast<ServiceInfoMonitor *>(sender()); const auto m = qobject_cast<ServiceInfoMonitor *>(sender());
Q_ASSERT(m); Q_ASSERT(m);
if (state == ServiceInfo::StateDeleted) { if (state == ServiceInfo::StateDeleted) {
Q_ASSERT(processId == 0);
processId = m->processId();
if (processId != 0) {
state = ServiceInfo::StateInactive;
}
stopServiceMonitor(m); 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) void ServiceInfoManager::onServiceCreated(const QStringList &nameList)

View File

@ -27,7 +27,7 @@ public:
static QVector<ServiceInfo> loadServiceInfoList(); static QVector<ServiceInfo> loadServiceInfoList();
signals: signals:
void servicesChanged(); void serviceChanged(quint32 processId, int groupIndex = -1);
private: private:
void setupServiceMonitors(); void setupServiceMonitors();
@ -40,7 +40,7 @@ private:
void startServiceListMonitor(void *managerHandle = nullptr); void startServiceListMonitor(void *managerHandle = nullptr);
void stopServiceListMonitor(); void stopServiceListMonitor();
void onServiceStateChanged(ServiceInfo::State state); void onServiceStateChanged(ServiceInfo::State state, quint32 processId = 0);
void onServiceCreated(const QStringList &nameList); void onServiceCreated(const QStringList &nameList);
private: private:

View File

@ -29,16 +29,16 @@ static void CALLBACK notifyCallback(PVOID parameter)
} }
const bool running = (notify->ServiceStatus.dwCurrentState == SERVICE_RUNNING); const bool running = (notify->ServiceStatus.dwCurrentState == SERVICE_RUNNING);
m->setRunning(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 = const ServiceInfo::State state =
running ? ServiceInfo::StateActive : ServiceInfo::StateInactive; running ? ServiceInfo::StateActive : ServiceInfo::StateInactive;
emit m->stateChanged(state); emit m->stateChanged(state, (running ? processId : oldProcessId));
// NotifyServiceStatusChange() must not be called from the callback // NotifyServiceStatusChange() must not be called from the callback
m->requestStartNotifier(); m->requestStartNotifier();
@ -129,7 +129,7 @@ void ServiceInfoMonitor::openService(void *managerHandle)
} break; } break;
default: default:
qCCritical(LC) << "Open service error:" << name() << res; qCCritical(LC) << "Open service error:" << name() << res;
emit errorOccurred(); errorOccurred();
} }
return; return;
} }
@ -149,7 +149,7 @@ void ServiceInfoMonitor::reopenService()
{ {
if (m_isReopening) { if (m_isReopening) {
qCCritical(LC) << "Reopen service error:" << name(); qCCritical(LC) << "Reopen service error:" << name();
emit errorOccurred(); errorOccurred();
return; return;
} }
@ -188,7 +188,12 @@ void ServiceInfoMonitor::startNotifier()
} break; } break;
default: default:
qCCritical(LC) << "Start notifier error:" << name() << res; qCCritical(LC) << "Start notifier error:" << name() << res;
emit errorOccurred(); errorOccurred();
} }
} }
} }
void ServiceInfoMonitor::errorOccurred()
{
emit stateChanged(ServiceInfo::StateDeleted);
}

View File

@ -25,8 +25,7 @@ public:
QVector<char> &notifyBuffer() { return m_notifyBuffer; } QVector<char> &notifyBuffer() { return m_notifyBuffer; }
signals: signals:
void stateChanged(ServiceInfo::State state); void stateChanged(ServiceInfo::State state, quint32 processId = 0);
void errorOccurred();
public slots: public slots:
void terminate(); void terminate();
@ -41,6 +40,8 @@ private:
void startNotifier(); void startNotifier();
void errorOccurred();
private: private:
bool m_terminated : 1; bool m_terminated : 1;
bool m_isReopening : 1; bool m_isReopening : 1;