From 8d033daf6bd7cbd4322d385e8e650999b2e87b00 Mon Sep 17 00:00:00 2001 From: ComixHe Date: Wed, 6 Sep 2023 13:22:01 +0800 Subject: [PATCH] fix: Manual removal of symbolic links leads to state error Signed-off-by: ComixHe --- src/dbus/applicationservice.cpp | 39 +++++++++++++++++++-------------- src/dbus/applicationservice.h | 6 ++++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/dbus/applicationservice.cpp b/src/dbus/applicationservice.cpp index 7226556..5148aae 100644 --- a/src/dbus/applicationservice.cpp +++ b/src/dbus/applicationservice.cpp @@ -376,40 +376,45 @@ qulonglong ApplicationService::lastLaunchedTime() const noexcept return m_lastLaunch; } +bool ApplicationService::autostartCheck(const QString &linkPath) noexcept +{ + QFileInfo info{linkPath}; + + if (!info.exists() or !info.isSymbolicLink()) { + qWarning() << "same name desktop file exists:" << linkPath << "but this may not created by AM."; + return false; + } + + return true; +} + bool ApplicationService::isAutoStart() const noexcept { - return m_AutoStart; + auto path = getAutoStartDirs().first(); + auto linkName = QDir{path}.filePath(m_desktopSource.desktopId() + ".desktop"); + return autostartCheck(linkName); } 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)) { + if (!autostartCheck(linkName) and !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; + if (autostartCheck(linkName)) { + QFile linkFile{linkName}; + if (!linkFile.remove()) { + qWarning() << "remove link failed:" << linkFile.errorString(); + return; + } } } - - m_AutoStart = autostart; } QList ApplicationService::instances() const noexcept diff --git a/src/dbus/applicationservice.h b/src/dbus/applicationservice.h index ee533e4..e913657 100644 --- a/src/dbus/applicationservice.h +++ b/src/dbus/applicationservice.h @@ -53,6 +53,10 @@ public: Q_PROPERTY(qulonglong LastLaunchedTime READ lastLaunchedTime) [[nodiscard]] qulonglong lastLaunchedTime() const noexcept; + // FIXME: + // This property should implement with fuse guarded $XDG_CONFIG_HOME/autostart/. + // Current implementation has some problems, + // such as it will not emit changed signal. Q_PROPERTY(bool AutoStart READ isAutoStart WRITE setAutoStart) [[nodiscard]] bool isAutoStart() const noexcept; void setAutoStart(bool autostart) noexcept; @@ -104,7 +108,6 @@ private: explicit ApplicationService(DesktopFile source, ApplicationManager1Service *parent); static QSharedPointer createApplicationService(DesktopFile source, ApplicationManager1Service *parent) noexcept; - bool m_AutoStart{false}; qlonglong m_lastLaunch{0}; QDBusObjectPath m_applicationPath; QString m_launcher{getApplicationLauncherBinary()}; @@ -117,6 +120,7 @@ private: EntryValueType type, const QLocale &locale = getUserLocale()) const noexcept; static bool shouldBeShown(const std::unique_ptr &entry) noexcept; + static bool autostartCheck(const QString &linkPath) noexcept; }; #endif