From 26a0b67157657005e64e024d5158c88497772de9 Mon Sep 17 00:00:00 2001 From: ComixHe Date: Tue, 29 Aug 2023 11:13:03 +0800 Subject: [PATCH] feat: filter service and scope by subState and `app` prefix Signed-off-by: ComixHe --- src/dbus/applicationmanager1service.cpp | 18 ++++++++++++++---- src/global.h | 13 +++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/dbus/applicationmanager1service.cpp b/src/dbus/applicationmanager1service.cpp index 142155a..da83db2 100644 --- a/src/dbus/applicationmanager1service.cpp +++ b/src/dbus/applicationmanager1service.cpp @@ -57,6 +57,10 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptraddOneInstance(instanceId, applicationPath, systemdUnitPath.path())) [[likely]] { qCritical() << "add Instance failed:" << applicationPath << unitName << systemdUnitPath.path(); } - - return; } void ApplicationManager1Service::removeInstanceFromApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath) { + if (!isApplication(systemdUnitPath)) { + return; + } + auto pair = processUnitName(unitName); auto appId = pair.first; auto instanceId = pair.second; @@ -145,8 +151,12 @@ void ApplicationManager1Service::scanInstances() noexcept QList units; v.value() >> units; for (const auto &unit : units) { - // FIXME(black_desk): Should check this unit is active or not. - this->addInstanceToApplication(unit.name, unit.objectPath); + if (!isApplication(unit.objectPath)) { + continue; + } + if (unit.subState == "running") { + this->addInstanceToApplication(unit.name, unit.objectPath); + } } } diff --git a/src/global.h b/src/global.h index 36bc21e..dbcb32f 100644 --- a/src/global.h +++ b/src/global.h @@ -39,6 +39,7 @@ Q_DECLARE_METATYPE(PropMap) struct SystemdUnitDBusMessage { QString name; + QString subState; QDBusObjectPath objectPath; }; @@ -51,7 +52,7 @@ inline const QDBusArgument &operator>>(const QDBusArgument &argument, QList> unit.name >> _str >> _str >> _str >> _str >> _str >> unit.objectPath >> _uint >> _str >> _path; + argument >> unit.name >> _str >> _str >> _str >> unit.subState >> _str >> unit.objectPath >> _uint >> _str >> _path; units.push_back(unit); argument.endStructure(); } @@ -421,9 +422,13 @@ inline QStringList getAutoStartDirs() return XDGConfigDirs; } +inline bool isApplication(const QDBusObjectPath &path) +{ + return path.path().split('/').last().startsWith("app"); +} + inline QPair processUnitName(const QString &unitName) { - // FIXME: rewrite, using regexp. QString instanceId; QString applicationId; @@ -443,10 +448,6 @@ inline QPair processUnitName(const QString &unitName) auto app = unitName.sliced(0, lastDotIndex); auto components = app.split('-'); - if (components.size() < 2) { - qDebug() << unitName << "is not a xdg application ignore"; - return {}; - } instanceId = components.takeLast(); applicationId = components.takeLast(); } else {