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,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<ServiceInfoMonitor *>(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)

View File

@ -27,7 +27,7 @@ public:
static QVector<ServiceInfo> 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:

View File

@ -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);
}

View File

@ -25,8 +25,7 @@ public:
QVector<char> &notifyBuffer() { 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;