refact: apply review suggestions

Signed-off-by: black-desk <me@black-desk.cn>
This commit is contained in:
black-desk 2023-08-11 14:53:05 +08:00 committed by Comix
parent 4394edd8b8
commit 5183716873
10 changed files with 49 additions and 53 deletions

View File

@ -8,7 +8,7 @@ bool registerObjectToDBus(QObject *o, const QString &path, const QString &interf
{ {
auto &con = ApplicationManager1DBus::instance().globalServerBus(); auto &con = ApplicationManager1DBus::instance().globalServerBus();
if (!con.registerObject(path, interface, o, QDBusConnection::RegisterOption::ExportAdaptors)) { if (!con.registerObject(path, interface, o, QDBusConnection::RegisterOption::ExportAdaptors)) {
qFatal() << "register object failed:" << path << interface << con.lastError(); qCritical() << "register object failed:" << path << interface << con.lastError();
} else { } else {
qInfo() << "register object:" << path << interface; qInfo() << "register object:" << path << interface;
} }

View File

View File

@ -40,6 +40,7 @@ public:
} }
} }
// TODO: should be QList<QDBusObjectPath>
void launchApp(const QString &appId) void launchApp(const QString &appId)
{ {
auto msg = auto msg =

View File

@ -29,30 +29,38 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
connect(&dispatcher, connect(&dispatcher,
&SystemdSignalDispatcher::SystemdUnitNew, &SystemdSignalDispatcher::SystemdUnitNew,
this, this,
[this](const QString &serviceName, const QDBusObjectPath &systemdUnitPath) { [this](const QString &unitName, const QDBusObjectPath &systemdUnitPath) {
auto [appId, instanceId] = processServiceName(serviceName); auto pair = processUnitName(unitName);
auto appId = pair.first;
auto instanceId = pair.second;
if (appId.isEmpty()) { if (appId.isEmpty()) {
return; return;
} }
for (const auto &app : m_applicationList) { auto appIt = std::find_if(m_applicationList.cbegin(),
if (app->id() == appId) [[unlikely]] { m_applicationList.cend(),
const auto &applicationPath = app->m_applicationPath.path(); [&appId](const QSharedPointer<ApplicationService> &app) { return app->id() == appId; });
if (!app->addOneInstance(instanceId, applicationPath, systemdUnitPath.path())) {
qWarning() << "add Instance failed:" << applicationPath << serviceName << systemdUnitPath.path(); if (appIt == m_applicationList.cend()) [[unlikely]] {
} qWarning() << "couldn't find app" << appId << "in application manager.";
return; return;
}
} }
qWarning() << "couldn't find application:" << serviceName << "in application manager."; const auto &appRef = *appIt;
const auto &applicationPath = appRef->m_applicationPath.path();
if (!appRef->addOneInstance(instanceId, applicationPath, systemdUnitPath.path())) [[likely]] {
qCritical() << "add Instance failed:" << applicationPath << unitName << systemdUnitPath.path();
}
return;
}); });
connect(&dispatcher, connect(&dispatcher,
&SystemdSignalDispatcher::SystemdUnitRemoved, &SystemdSignalDispatcher::SystemdUnitRemoved,
this, this,
[this](const QString &serviceName, QDBusObjectPath systemdUnitPath) { [this](const QString &serviceName, QDBusObjectPath systemdUnitPath) {
auto pair = processServiceName(serviceName); auto pair = processUnitName(serviceName);
auto appId = pair.first; auto appId = pair.first;
auto instanceId = pair.second; auto instanceId = pair.second;
if (appId.isEmpty()) { if (appId.isEmpty()) {
@ -82,14 +90,14 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
}); });
} }
QPair<QString, QString> ApplicationManager1Service::processServiceName(const QString &serviceName) noexcept QPair<QString, QString> ApplicationManager1Service::processUnitName(const QString &unitName) noexcept
{ {
QString instanceId; QString instanceId;
QString applicationId; QString applicationId;
if (serviceName.endsWith(".service")) { if (unitName.endsWith(".service")) {
auto lastDotIndex = serviceName.lastIndexOf('.'); auto lastDotIndex = unitName.lastIndexOf('.');
auto app = serviceName.sliced(0, lastDotIndex - 1); // remove suffix auto app = unitName.sliced(0, lastDotIndex - 1); // remove suffix
if (app.contains('@')) { if (app.contains('@')) {
auto atIndex = app.indexOf('@'); auto atIndex = app.indexOf('@');
@ -98,15 +106,15 @@ QPair<QString, QString> ApplicationManager1Service::processServiceName(const QSt
} }
applicationId = app.split('-').last(); // drop launcher if it exists. applicationId = app.split('-').last(); // drop launcher if it exists.
} else if (serviceName.endsWith(".scope")) { } else if (unitName.endsWith(".scope")) {
auto lastDotIndex = serviceName.lastIndexOf('.'); auto lastDotIndex = unitName.lastIndexOf('.');
auto app = serviceName.sliced(0, lastDotIndex - 1); auto app = unitName.sliced(0, lastDotIndex - 1);
auto components = app.split('-'); auto components = app.split('-');
instanceId = components.takeLast(); instanceId = components.takeLast();
applicationId = components.takeLast(); applicationId = components.takeLast();
} else { } else {
qDebug() << "it's not service or scope:" << serviceName << "ignore."; qDebug() << "it's not service or scope:" << unitName << "ignore.";
return {}; return {};
} }

View File

@ -61,7 +61,7 @@ private:
QScopedPointer<JobManager1Service> m_jobManager{nullptr}; QScopedPointer<JobManager1Service> m_jobManager{nullptr};
QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList; QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList;
static QPair<QString, QString> processServiceName(const QString &serviceName) noexcept; static QPair<QString, QString> processUnitName(const QString &serviceName) noexcept;
}; };
#endif #endif

View File

@ -23,16 +23,6 @@ ApplicationService::~ApplicationService()
} }
} }
qsizetype ApplicationService::applicationCheck(const QString &serviceName)
{
const auto &ApplicationId = id();
if (!serviceName.startsWith(ApplicationId)) [[likely]] {
return 0;
}
return ApplicationId.size();
}
QString ApplicationService::GetActionName(const QString &identifier, const QStringList &env) QString ApplicationService::GetActionName(const QString &identifier, const QStringList &env)
{ {
const auto &supportedActions = actions(); const auto &supportedActions = actions();

View File

@ -133,7 +133,6 @@ private:
QSharedPointer<DesktopIcons> m_Icons{nullptr}; QSharedPointer<DesktopIcons> m_Icons{nullptr};
QMap<QDBusObjectPath, QSharedPointer<InstanceService>> m_Instances; QMap<QDBusObjectPath, QSharedPointer<InstanceService>> m_Instances;
QString userNameLookup(uid_t uid); QString userNameLookup(uid_t uid);
qsizetype applicationCheck(const QString &serviceName);
[[nodiscard]] LaunchTask unescapeExec(const QString &str, const QStringList &fields); [[nodiscard]] LaunchTask unescapeExec(const QString &str, const QStringList &fields);
}; };

View File

@ -25,17 +25,15 @@ using IconMap = QMap<QString, QMap<uint, QMap<QString, QDBusUnixFileDescriptor>>
inline QString getApplicationLauncherBinary() inline QString getApplicationLauncherBinary()
{ {
static bool logFlag{true}; static const QString bin = []() -> QString {
auto value = qgetenv("DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN"); auto value = qgetenv("DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN");
if (value.isEmpty()) { if (value.isEmpty()) {
return ApplicationLaunchHelperBinary; return ApplicationLaunchHelperBinary;
} }
if (logFlag) {
qWarning() << "Using app launch helper defined in environment variable DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN."; qWarning() << "Using app launch helper defined in environment variable DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN.";
logFlag = false; return value;
} }();
return value; return bin;
} }
enum class DBusType { Session = QDBusConnection::SessionBus, System = QDBusConnection::SystemBus, Custom }; enum class DBusType { Session = QDBusConnection::SessionBus, System = QDBusConnection::SystemBus, Custom };

View File

@ -31,20 +31,20 @@ bool SystemdSignalDispatcher::connectToSignals() noexcept
return true; return true;
} }
void SystemdSignalDispatcher::onUnitNew(QString serviceName, QDBusObjectPath systemdUnitPath) void SystemdSignalDispatcher::onUnitNew(QString unitName, QDBusObjectPath systemdUnitPath)
{ {
if (!serviceName.startsWith("app-")) { if (!unitName.startsWith("app-")) {
return; return;
} }
emit SystemdUnitNew(serviceName.sliced(4), systemdUnitPath); // remove "app-" emit SystemdUnitNew(unitName.sliced(sizeof("app-") - 1), systemdUnitPath);
} }
void SystemdSignalDispatcher::onUnitRemoved(QString serviceName, QDBusObjectPath systemdUnitPath) void SystemdSignalDispatcher::onUnitRemoved(QString unitName, QDBusObjectPath systemdUnitPath)
{ {
if (!serviceName.startsWith("app-")) { if (!unitName.startsWith("app-")) {
return; return;
} }
emit SystemdUnitRemoved(serviceName.sliced(4), systemdUnitPath); emit SystemdUnitRemoved(unitName.sliced(sizeof("app-") - 1), systemdUnitPath);
} }

View File

@ -18,12 +18,12 @@ public:
return dispatcher; return dispatcher;
} }
Q_SIGNALS: Q_SIGNALS:
void SystemdUnitNew(QString serviceName, QDBusObjectPath systemdUnitPath); void SystemdUnitNew(QString unitName, QDBusObjectPath systemdUnitPath);
void SystemdUnitRemoved(QString serviceName, QDBusObjectPath systemdUnitPath); void SystemdUnitRemoved(QString unitName, QDBusObjectPath systemdUnitPath);
private Q_SLOTS: private Q_SLOTS:
void onUnitNew(QString serviceName, QDBusObjectPath systemdUnitPath); void onUnitNew(QString unitName, QDBusObjectPath systemdUnitPath);
void onUnitRemoved(QString serviceName, QDBusObjectPath systemdUnitPath); void onUnitRemoved(QString unitName, QDBusObjectPath systemdUnitPath);
private: private:
explicit SystemdSignalDispatcher(QObject *parent = nullptr) explicit SystemdSignalDispatcher(QObject *parent = nullptr)