feat: filter service and scope by subState and app prefix

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2023-08-29 11:13:03 +08:00 committed by Comix
parent b0dd943e1d
commit 26a0b67157
2 changed files with 21 additions and 10 deletions

View File

@ -57,6 +57,10 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath)
{
if (!isApplication(systemdUnitPath)) {
return;
}
auto pair = processUnitName(unitName);
auto appId = pair.first;
auto instanceId = pair.second;
@ -78,12 +82,14 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam
if (!(*appIt)->addOneInstance(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<SystemdUnitDBusMessage> units;
v.value<QDBusArgument>() >> 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);
}
}
}

View File

@ -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<Syst
uint32_t _uint;
QDBusObjectPath _path;
SystemdUnitDBusMessage unit;
argument >> 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<QString, QString> processUnitName(const QString &unitName)
{
// FIXME: rewrite, using regexp.
QString instanceId;
QString applicationId;
@ -443,10 +448,6 @@ inline QPair<QString, QString> 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 {