From 8e8342260358e360eb3d5ac9971049c044537bde Mon Sep 17 00:00:00 2001 From: ComixHe Date: Thu, 4 Jan 2024 11:17:26 +0800 Subject: [PATCH] refact: scanAutostart and get/set autostart Signed-off-by: ComixHe --- src/dbus/applicationmanager1service.cpp | 24 +++++----- src/dbus/applicationservice.cpp | 59 +++++++++++++++---------- src/dbus/applicationservice.h | 11 ++++- src/global.h | 8 ++++ 4 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/dbus/applicationmanager1service.cpp b/src/dbus/applicationmanager1service.cpp index c50cf1b..0851506 100644 --- a/src/dbus/applicationmanager1service.cpp +++ b/src/dbus/applicationmanager1service.cpp @@ -378,6 +378,7 @@ QHash, QString> ApplicationManager1Service::s auto realExec = tmp.value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString(); qInfo() << "launch normal autostart application " << app->id() << " by " << realExec; ret.insert(app, realExec); + app->setAutostartSource({desktopFile.sourcePath(), std::move(tmp)}); continue; } @@ -394,6 +395,7 @@ QHash, QString> ApplicationManager1Service::s auto realExec = tmp.value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString(); qInfo() << "launch exist autostart application " << (*appIt)->id() << " by " << realExec; ret.insert(*appIt, realExec); + (*appIt)->setAutostartSource({desktopFile.sourcePath(), std::move(tmp)}); continue; } @@ -405,9 +407,9 @@ QHash, QString> ApplicationManager1Service::s continue; } - auto realExec = newApp->m_entry->value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString(); - qInfo() << "launch new autostart application " << newApp->id() << " by " << realExec; - ret.insert(newApp, realExec); + qInfo() << "launch new autostart application " << newApp->id(); + newApp->setAutostartSource({desktopFile.sourcePath()}); + ret.insert(newApp, {}); } return ret; @@ -446,6 +448,14 @@ QList ApplicationManager1Service::list() const QSharedPointer ApplicationManager1Service::addApplication(DesktopFile desktopFileSource) noexcept { + auto objectPath = QDBusObjectPath{getObjectPathFromAppId(desktopFileSource.desktopId())}; + if (auto app = m_applicationList.constFind(objectPath); app != m_applicationList.cend()) { + qInfo() << "this application already exists." + << "current desktop source:" << desktopFileSource.sourcePath() + << "exists app source:" << app->data()->desktopFileSource().sourcePath(); + return *app; + } + auto source = desktopFileSource.sourcePath(); QSharedPointer application = ApplicationService::createApplicationService(std::move(desktopFileSource), this, m_storage); @@ -454,15 +464,7 @@ QSharedPointer ApplicationManager1Service::addApplication(De return nullptr; } - if (auto app = m_applicationList.constFind(application->applicationPath()); app != m_applicationList.cend()) { - qInfo() << "this application already exists." - << "current desktop source:" << application->desktopFileSource().sourcePath() - << "exists app source:" << app->data()->desktopFileSource().sourcePath(); - return *app; - } - auto *ptr = application.data(); - if (auto *adaptor = new (std::nothrow) ApplicationAdaptor{ptr}; adaptor == nullptr) { qCritical() << "new ApplicationAdaptor failed."; return nullptr; diff --git a/src/dbus/applicationservice.cpp b/src/dbus/applicationservice.cpp index eea56d0..69964ce 100644 --- a/src/dbus/applicationservice.cpp +++ b/src/dbus/applicationservice.cpp @@ -107,14 +107,7 @@ QSharedPointer ApplicationService::createApplicationService( QString objectPath; QTextStream sourceStream; - auto appId = app->desktopFileSource().desktopId(); - - if (!appId.isEmpty()) { - objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + escapeToObjectPath(appId); - } else { - objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128); - } - + objectPath = getObjectPathFromAppId(app->desktopFileSource().desktopId()); DesktopFileGuard guard{app->desktopFileSource()}; if (!guard.try_open()) { @@ -581,21 +574,25 @@ void ApplicationService::setScaleFactor(double value) noexcept emit scaleFactorChanged(); } -bool ApplicationService::autostartCheck(const QString &filePath) noexcept +bool ApplicationService::autostartCheck(const QString &filePath) const noexcept { qDebug() << "current check autostart file:" << filePath; - QFile file{filePath}; - if (!file.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) { - qWarning() << "open" << filePath << "failed:" << file.errorString(); - return false; - } - - QTextStream stream{&file}; DesktopEntry s; - if (auto err = s.parse(stream); err != ParserError::NoError) { - qWarning() << "parse" << filePath << "failed:" << err; - return false; + if (!m_autostartSource.m_entry.data().isEmpty()) { + s = m_autostartSource.m_entry; + } else { + QFile file{filePath}; + if (!file.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) { + qWarning() << "open" << filePath << "failed:" << file.errorString(); + return false; + } + + QTextStream stream{&file}; + if (auto err = s.parse(stream); err != ParserError::NoError) { + qWarning() << "parse" << filePath << "failed:" << err; + return false; + } } auto hiddenVal = s.value(DesktopFileEntryKey, DesktopEntryHidden); @@ -610,6 +607,10 @@ bool ApplicationService::autostartCheck(const QString &filePath) noexcept bool ApplicationService::isAutoStart() const noexcept { + if (m_autostartSource.m_filePath.isEmpty()) { + return false; + } + auto appId = id(); auto dirs = getAutoStartDirs(); QString destDesktopFile; @@ -620,7 +621,6 @@ bool ApplicationService::isAutoStart() const noexcept auto filePath = file.absoluteFilePath(); if (appId == getAutostartAppIdFromAbsolutePath(filePath)) { destDesktopFile = filePath; - return true; } return false; }, @@ -628,8 +628,8 @@ bool ApplicationService::isAutoStart() const noexcept {"*.desktop"}, QDir::Name | QDir::DirsLast); - if (destDesktopFile.isEmpty()) { - qDebug() << "couldn't find autostart desktopFile."; + // file has been removed + if (destDesktopFile != m_autostartSource.m_filePath) { return false; } @@ -650,10 +650,18 @@ void ApplicationService::setAutoStart(bool autostart) noexcept return; } - auto newEntry = *m_entry; + DesktopEntry newEntry; + if (!m_autostartSource.m_entry.data().isEmpty()) { + newEntry = m_autostartSource.m_entry; + } else { + newEntry = *m_entry; + } + newEntry.insert(DesktopFileEntryKey, X_Deepin_GenerateSource, m_desktopSource.sourcePath()); newEntry.insert(DesktopFileEntryKey, DesktopEntryHidden, !autostart); + setAutostartSource({fileName, newEntry}); + auto hideAutostart = toString(newEntry.data()).toLocal8Bit(); auto writeBytes = autostartFile.write(hideAutostart); @@ -1082,6 +1090,11 @@ void ApplicationService::updateAfterLaunch(bool isLaunch) noexcept } } +void ApplicationService::setAutostartSource(AutostartSource &&source) noexcept +{ + m_autostartSource = std::move(source); +} + double getScaleFactor() noexcept { auto sessionBus = QDBusConnection::sessionBus(); diff --git a/src/dbus/applicationservice.h b/src/dbus/applicationservice.h index b34094f..3c46d22 100644 --- a/src/dbus/applicationservice.h +++ b/src/dbus/applicationservice.h @@ -23,11 +23,16 @@ #include #include #include -#include QString getDeepinWineScaleFactor(const QString &appId) noexcept; double getScaleFactor() noexcept; +struct AutostartSource +{ + QString m_filePath; + DesktopEntry m_entry; +}; + class ApplicationService : public QObject, protected QDBusContext { Q_OBJECT @@ -172,6 +177,7 @@ private: qint64 m_lastLaunch{0}; double m_scaleFactor; std::weak_ptr m_storage; + AutostartSource m_autostartSource; QDBusObjectPath m_applicationPath; QString m_launcher{getApplicationLauncherBinary()}; DesktopFile m_desktopSource; @@ -179,7 +185,8 @@ private: QMap> m_Instances; void updateAfterLaunch(bool isLaunch) noexcept; static bool shouldBeShown(const std::unique_ptr &entry) noexcept; - [[nodiscard]] static bool autostartCheck(const QString &filePath) noexcept; + [[nodiscard]] bool autostartCheck(const QString &filePath) const noexcept; + void setAutostartSource(AutostartSource &&source) noexcept; [[nodiscard]] ApplicationManager1Service *parent() { return dynamic_cast(QObject::parent()); } [[nodiscard]] const ApplicationManager1Service *parent() const { diff --git a/src/global.h b/src/global.h index f560d4b..e4a8c64 100644 --- a/src/global.h +++ b/src/global.h @@ -644,4 +644,12 @@ inline QString getAutostartAppIdFromAbsolutePath(const QString &path) return appId; } +inline QString getObjectPathFromAppId(const QString &appId) +{ + if (!appId.isEmpty()) { + return QString{DDEApplicationManager1ObjectPath} + "/" + escapeToObjectPath(appId); + } + return QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128); +} + #endif