feat: support AutoStart

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2023-09-04 18:18:20 +08:00 committed by Comix
parent 30a03974f3
commit acba7b727e
3 changed files with 74 additions and 3 deletions

View File

@ -49,10 +49,27 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
this, this,
&ApplicationManager1Service::removeInstanceFromApplication); &ApplicationManager1Service::removeInstanceFromApplication);
this->scanApplications(); scanApplications();
this->scanInstances();
// TODO: send custom event; scanInstances();
auto runtimePath = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
if (runtimePath.isEmpty()) {
runtimePath = QString{"/run/user/%1"}.arg(getCurrentUID());
}
bool firstStart{false};
QDir runtimeDir{runtimePath};
if (!runtimeDir.exists("ApplicationManager")) {
QFile flag{runtimeDir.filePath("ApplicationManager")};
if (!flag.open(QFile::WriteOnly | QFile::NewOnly)) {
qWarning() << "create record file failed.";
} else {
firstStart = true;
}
}
scanAutoStart(firstStart);
} }
void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath) void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath)
@ -160,6 +177,33 @@ void ApplicationManager1Service::scanInstances() noexcept
} }
} }
void ApplicationManager1Service::scanAutoStart(bool firstStart) noexcept
{
if (!firstStart) {
return;
}
auto autostartDirs = getAutoStartDirs();
QStringList needToLaunch;
applyIteratively(QList<QDir>{autostartDirs.cbegin(), autostartDirs.cend()}, [&needToLaunch](const QFileInfo &info) {
if (info.isSymbolicLink()) {
needToLaunch.emplace_back(info.symLinkTarget());
}
return false;
});
while (!needToLaunch.isEmpty()) {
const auto &filePath = needToLaunch.takeFirst();
auto appIt =
std::find_if(m_applicationList.constKeyValueBegin(),
m_applicationList.constKeyValueEnd(),
[&filePath](const auto &pair) { return pair.second->m_desktopSource.sourcePath() == filePath; });
if (appIt != m_applicationList.constKeyValueEnd()) {
appIt->second->Launch({}, {}, {});
}
}
}
QList<QDBusObjectPath> ApplicationManager1Service::list() const QList<QDBusObjectPath> ApplicationManager1Service::list() const
{ {
return m_applicationList.keys(); return m_applicationList.keys();

View File

@ -56,6 +56,7 @@ private:
void scanApplications() noexcept; void scanApplications() noexcept;
void scanInstances() noexcept; void scanInstances() noexcept;
void scanAutoStart(bool firstStart) noexcept;
void addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath); void addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath);
void removeInstanceFromApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath); void removeInstanceFromApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath);
}; };

View File

@ -372,6 +372,32 @@ bool ApplicationService::isAutoStart() const noexcept
void ApplicationService::setAutoStart(bool autostart) noexcept void ApplicationService::setAutoStart(bool autostart) noexcept
{ {
if (autostart == m_AutoStart) {
return;
}
auto path = getAutoStartDirs().first();
auto linkName = QDir{path}.filePath(m_desktopSource.desktopId() + ".desktop");
auto &file = m_desktopSource.sourceFileRef();
if (autostart) {
if (!file.link(linkName)) {
qWarning() << "link to autostart failed:" << file.errorString();
return;
}
} else {
QFileInfo info{linkName};
if (!info.exists() or !info.isSymbolicLink()) {
qWarning() << "same name desktop file exists:" << linkName << "but this may not created by AM.";
return;
}
QFile linkFile{linkName};
if (!linkFile.remove()) {
qWarning() << "remove link failed:" << linkFile.errorString();
return;
}
}
m_AutoStart = autostart; m_AutoStart = autostart;
} }