feat: support apps launched directly by systemd

Do not filter out those not launched by application manager. Assume
unit name as the app id. When Identify, assume there is only one
unique instance.

Log: support apps launched directly by systemd
Related-to: https://github.com/linuxdeepin/developer-center/issues/8088
This commit is contained in:
Yixue Wang 2024-04-22 17:26:59 +08:00 committed by asterwyx
parent 60965359cb
commit e6fd0a61a5
3 changed files with 17 additions and 34 deletions

View File

@ -187,10 +187,6 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce
void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath) void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath)
{ {
if (!isApplication(systemdUnitPath)) {
return;
}
auto info = processUnitName(unitName); auto info = processUnitName(unitName);
auto appId = std::move(info.applicationID); auto appId = std::move(info.applicationID);
auto launcher = std::move(info.Launcher); auto launcher = std::move(info.Launcher);
@ -199,6 +195,9 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam
if (appId.isEmpty()) { if (appId.isEmpty()) {
return; return;
} }
if (instanceId.isEmpty()) {
instanceId = QUuid::createUuid().toString(QUuid::Id128);
}
auto appIt = std::find_if(m_applicationList.cbegin(), auto appIt = std::find_if(m_applicationList.cbegin(),
m_applicationList.cend(), m_applicationList.cend(),
@ -220,10 +219,6 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam
void ApplicationManager1Service::removeInstanceFromApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath) void ApplicationManager1Service::removeInstanceFromApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath)
{ {
if (!isApplication(systemdUnitPath)) {
return;
}
auto info = processUnitName(unitName); auto info = processUnitName(unitName);
auto appId = std::move(info.applicationID); auto appId = std::move(info.applicationID);
auto launcher = std::move(info.Launcher); auto launcher = std::move(info.Launcher);
@ -316,9 +311,6 @@ void ApplicationManager1Service::scanInstances() noexcept
QList<SystemdUnitDBusMessage> units; QList<SystemdUnitDBusMessage> units;
v.value<QDBusArgument>() >> units; v.value<QDBusArgument>() >> units;
for (const auto &unit : units) { for (const auto &unit : units) {
if (!isApplication(unit.objectPath)) {
continue;
}
if (unit.subState == "running") { if (unit.subState == "running") {
this->addInstanceToApplication(unit.name, unit.objectPath); this->addInstanceToApplication(unit.name, unit.objectPath);
} }
@ -567,10 +559,16 @@ QString ApplicationManager1Service::Identify(const QDBusUnixFileDescriptor &pidf
return {}; return {};
} }
auto instancePath = (*app)->findInstance(ret.InstanceId); QDBusObjectPath instancePath;
const auto &instances = (*app)->instances();
if (auto path = instancePath.path(); path.isEmpty()) { if (ret.InstanceId.isEmpty() && instances.size() == 1) {
sendErrorReply(QDBusError::Failed, "can't find instance:" % path); // Maybe a dbus systemd service
instancePath = instances.constFirst();
} else {
instancePath = (*app)->findInstance(ret.InstanceId);
}
if (instancePath.path().isEmpty()) {
sendErrorReply(QDBusError::Failed, "can't find instance:" % ret.InstanceId);
return {}; return {};
} }

View File

@ -481,11 +481,6 @@ inline QString getCurrentDesktop()
return desktops.first(); return desktops.first();
} }
inline bool isApplication(const QDBusObjectPath &path)
{
return path.path().split('/').last().startsWith("app");
}
struct unitInfo struct unitInfo
{ {
QString applicationID; QString applicationID;
@ -503,8 +498,10 @@ inline unitInfo processUnitName(const QString &unitName)
decltype(auto) appPrefix = u8"app-"; decltype(auto) appPrefix = u8"app-";
if (!unitName.startsWith(appPrefix)) { if (!unitName.startsWith(appPrefix)) {
qDebug() << "this unit " << unitName << "isn't an app unit."; // If not started by application manager, just remove suffix and take name as app id.
return {}; auto lastDotIndex = unitName.lastIndexOf('.');
applicationId = unitName.sliced(0, lastDotIndex);
return {unescapeApplicationId(applicationId), std::move(launcher), std::move(instanceId)};
} }
auto unit = unitName.sliced(sizeof(appPrefix) - 1); auto unit = unitName.sliced(sizeof(appPrefix) - 1);
@ -540,10 +537,6 @@ inline unitInfo processUnitName(const QString &unitName)
return {}; return {};
} }
if (instanceId.isEmpty()) {
instanceId = QUuid::createUuid().toString(QUuid::Id128);
}
return {unescapeApplicationId(applicationId), std::move(launcher), std::move(instanceId)}; return {unescapeApplicationId(applicationId), std::move(launcher), std::move(instanceId)};
} }

View File

@ -54,18 +54,10 @@ void SystemdSignalDispatcher::onPropertiesChanged(QString interface, QVariantMap
void SystemdSignalDispatcher::onUnitNew(QString unitName, QDBusObjectPath systemdUnitPath) void SystemdSignalDispatcher::onUnitNew(QString unitName, QDBusObjectPath systemdUnitPath)
{ {
if (!unitName.startsWith("app-")) {
return;
}
emit SystemdUnitNew(unitName, systemdUnitPath); emit SystemdUnitNew(unitName, systemdUnitPath);
} }
void SystemdSignalDispatcher::onUnitRemoved(QString unitName, QDBusObjectPath systemdUnitPath) void SystemdSignalDispatcher::onUnitRemoved(QString unitName, QDBusObjectPath systemdUnitPath)
{ {
if (!unitName.startsWith("app-")) {
return;
}
emit SystemdUnitRemoved(unitName, systemdUnitPath); emit SystemdUnitRemoved(unitName, systemdUnitPath);
} }