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();
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 {
qInfo() << "register object:" << path << interface;
}

View File

View File

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

View File

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

View File

@ -61,7 +61,7 @@ private:
QScopedPointer<JobManager1Service> m_jobManager{nullptr};
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

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)
{
const auto &supportedActions = actions();

View File

@ -133,7 +133,6 @@ private:
QSharedPointer<DesktopIcons> m_Icons{nullptr};
QMap<QDBusObjectPath, QSharedPointer<InstanceService>> m_Instances;
QString userNameLookup(uid_t uid);
qsizetype applicationCheck(const QString &serviceName);
[[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()
{
static bool logFlag{true};
static const QString bin = []() -> QString {
auto value = qgetenv("DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN");
if (value.isEmpty()) {
return ApplicationLaunchHelperBinary;
}
if (logFlag) {
qWarning() << "Using app launch helper defined in environment variable DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN.";
logFlag = false;
}
return value;
}();
return bin;
}
enum class DBusType { Session = QDBusConnection::SessionBus, System = QDBusConnection::SystemBus, Custom };

View File

@ -31,20 +31,20 @@ bool SystemdSignalDispatcher::connectToSignals() noexcept
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;
}
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;
}
emit SystemdUnitRemoved(serviceName.sliced(4), systemdUnitPath);
emit SystemdUnitRemoved(unitName.sliced(sizeof("app-") - 1), systemdUnitPath);
}

View File

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