diff --git a/src/dbus/applicationmanager1service.cpp b/src/dbus/applicationmanager1service.cpp index f95bdb5..b4c734f 100644 --- a/src/dbus/applicationmanager1service.cpp +++ b/src/dbus/applicationmanager1service.cpp @@ -49,10 +49,27 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptrscanApplications(); - this->scanInstances(); + scanApplications(); - // 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) @@ -160,6 +177,33 @@ void ApplicationManager1Service::scanInstances() noexcept } } +void ApplicationManager1Service::scanAutoStart(bool firstStart) noexcept +{ + if (!firstStart) { + return; + } + + auto autostartDirs = getAutoStartDirs(); + QStringList needToLaunch; + applyIteratively(QList{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 ApplicationManager1Service::list() const { return m_applicationList.keys(); diff --git a/src/dbus/applicationmanager1service.h b/src/dbus/applicationmanager1service.h index a3e0124..381f7d4 100644 --- a/src/dbus/applicationmanager1service.h +++ b/src/dbus/applicationmanager1service.h @@ -56,6 +56,7 @@ private: void scanApplications() noexcept; void scanInstances() noexcept; + void scanAutoStart(bool firstStart) noexcept; void addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath); void removeInstanceFromApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath); }; diff --git a/src/dbus/applicationservice.cpp b/src/dbus/applicationservice.cpp index 36f699b..c060da3 100644 --- a/src/dbus/applicationservice.cpp +++ b/src/dbus/applicationservice.cpp @@ -372,6 +372,32 @@ bool ApplicationService::isAutoStart() const 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; }