refact: scanAutostart and get/set autostart
Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
		@ -378,6 +378,7 @@ QHash<QSharedPointer<ApplicationService>, QString> ApplicationManager1Service::s
 | 
				
			|||||||
            auto realExec = tmp.value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString();
 | 
					            auto realExec = tmp.value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString();
 | 
				
			||||||
            qInfo() << "launch normal autostart application " << app->id() << " by " << realExec;
 | 
					            qInfo() << "launch normal autostart application " << app->id() << " by " << realExec;
 | 
				
			||||||
            ret.insert(app, realExec);
 | 
					            ret.insert(app, realExec);
 | 
				
			||||||
 | 
					            app->setAutostartSource({desktopFile.sourcePath(), std::move(tmp)});
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -394,6 +395,7 @@ QHash<QSharedPointer<ApplicationService>, QString> ApplicationManager1Service::s
 | 
				
			|||||||
            auto realExec = tmp.value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString();
 | 
					            auto realExec = tmp.value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString();
 | 
				
			||||||
            qInfo() << "launch exist autostart application " << (*appIt)->id() << " by " << realExec;
 | 
					            qInfo() << "launch exist autostart application " << (*appIt)->id() << " by " << realExec;
 | 
				
			||||||
            ret.insert(*appIt, realExec);
 | 
					            ret.insert(*appIt, realExec);
 | 
				
			||||||
 | 
					            (*appIt)->setAutostartSource({desktopFile.sourcePath(), std::move(tmp)});
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -405,9 +407,9 @@ QHash<QSharedPointer<ApplicationService>, QString> ApplicationManager1Service::s
 | 
				
			|||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        auto realExec = newApp->m_entry->value(DesktopFileEntryKey, "Exec").value_or(QString{""}).toString();
 | 
					        qInfo() << "launch new autostart application " << newApp->id();
 | 
				
			||||||
        qInfo() << "launch new autostart application " << newApp->id() << " by " << realExec;
 | 
					        newApp->setAutostartSource({desktopFile.sourcePath()});
 | 
				
			||||||
        ret.insert(newApp, realExec);
 | 
					        ret.insert(newApp, {});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return ret;
 | 
					    return ret;
 | 
				
			||||||
@ -446,6 +448,14 @@ QList<QDBusObjectPath> ApplicationManager1Service::list() const
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
QSharedPointer<ApplicationService> ApplicationManager1Service::addApplication(DesktopFile desktopFileSource) noexcept
 | 
					QSharedPointer<ApplicationService> 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();
 | 
					    auto source = desktopFileSource.sourcePath();
 | 
				
			||||||
    QSharedPointer<ApplicationService> application =
 | 
					    QSharedPointer<ApplicationService> application =
 | 
				
			||||||
        ApplicationService::createApplicationService(std::move(desktopFileSource), this, m_storage);
 | 
					        ApplicationService::createApplicationService(std::move(desktopFileSource), this, m_storage);
 | 
				
			||||||
@ -454,15 +464,7 @@ QSharedPointer<ApplicationService> ApplicationManager1Service::addApplication(De
 | 
				
			|||||||
        return nullptr;
 | 
					        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();
 | 
					    auto *ptr = application.data();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (auto *adaptor = new (std::nothrow) ApplicationAdaptor{ptr}; adaptor == nullptr) {
 | 
					    if (auto *adaptor = new (std::nothrow) ApplicationAdaptor{ptr}; adaptor == nullptr) {
 | 
				
			||||||
        qCritical() << "new ApplicationAdaptor failed.";
 | 
					        qCritical() << "new ApplicationAdaptor failed.";
 | 
				
			||||||
        return nullptr;
 | 
					        return nullptr;
 | 
				
			||||||
 | 
				
			|||||||
@ -107,14 +107,7 @@ QSharedPointer<ApplicationService> ApplicationService::createApplicationService(
 | 
				
			|||||||
    QString objectPath;
 | 
					    QString objectPath;
 | 
				
			||||||
    QTextStream sourceStream;
 | 
					    QTextStream sourceStream;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto appId = app->desktopFileSource().desktopId();
 | 
					    objectPath = getObjectPathFromAppId(app->desktopFileSource().desktopId());
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!appId.isEmpty()) {
 | 
					 | 
				
			||||||
        objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + escapeToObjectPath(appId);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    DesktopFileGuard guard{app->desktopFileSource()};
 | 
					    DesktopFileGuard guard{app->desktopFileSource()};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!guard.try_open()) {
 | 
					    if (!guard.try_open()) {
 | 
				
			||||||
@ -581,21 +574,25 @@ void ApplicationService::setScaleFactor(double value) noexcept
 | 
				
			|||||||
    emit scaleFactorChanged();
 | 
					    emit scaleFactorChanged();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool ApplicationService::autostartCheck(const QString &filePath) noexcept
 | 
					bool ApplicationService::autostartCheck(const QString &filePath) const noexcept
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    qDebug() << "current check autostart file:" << filePath;
 | 
					    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;
 | 
					    DesktopEntry s;
 | 
				
			||||||
    if (auto err = s.parse(stream); err != ParserError::NoError) {
 | 
					    if (!m_autostartSource.m_entry.data().isEmpty()) {
 | 
				
			||||||
        qWarning() << "parse" << filePath << "failed:" << err;
 | 
					        s = m_autostartSource.m_entry;
 | 
				
			||||||
        return false;
 | 
					    } 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);
 | 
					    auto hiddenVal = s.value(DesktopFileEntryKey, DesktopEntryHidden);
 | 
				
			||||||
@ -610,6 +607,10 @@ bool ApplicationService::autostartCheck(const QString &filePath) noexcept
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
bool ApplicationService::isAutoStart() const noexcept
 | 
					bool ApplicationService::isAutoStart() const noexcept
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    if (m_autostartSource.m_filePath.isEmpty()) {
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto appId = id();
 | 
					    auto appId = id();
 | 
				
			||||||
    auto dirs = getAutoStartDirs();
 | 
					    auto dirs = getAutoStartDirs();
 | 
				
			||||||
    QString destDesktopFile;
 | 
					    QString destDesktopFile;
 | 
				
			||||||
@ -620,7 +621,6 @@ bool ApplicationService::isAutoStart() const noexcept
 | 
				
			|||||||
            auto filePath = file.absoluteFilePath();
 | 
					            auto filePath = file.absoluteFilePath();
 | 
				
			||||||
            if (appId == getAutostartAppIdFromAbsolutePath(filePath)) {
 | 
					            if (appId == getAutostartAppIdFromAbsolutePath(filePath)) {
 | 
				
			||||||
                destDesktopFile = filePath;
 | 
					                destDesktopFile = filePath;
 | 
				
			||||||
                return true;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
@ -628,8 +628,8 @@ bool ApplicationService::isAutoStart() const noexcept
 | 
				
			|||||||
        {"*.desktop"},
 | 
					        {"*.desktop"},
 | 
				
			||||||
        QDir::Name | QDir::DirsLast);
 | 
					        QDir::Name | QDir::DirsLast);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (destDesktopFile.isEmpty()) {
 | 
					    // file has been removed
 | 
				
			||||||
        qDebug() << "couldn't find autostart desktopFile.";
 | 
					    if (destDesktopFile != m_autostartSource.m_filePath) {
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -650,10 +650,18 @@ void ApplicationService::setAutoStart(bool autostart) noexcept
 | 
				
			|||||||
        return;
 | 
					        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, X_Deepin_GenerateSource, m_desktopSource.sourcePath());
 | 
				
			||||||
    newEntry.insert(DesktopFileEntryKey, DesktopEntryHidden, !autostart);
 | 
					    newEntry.insert(DesktopFileEntryKey, DesktopEntryHidden, !autostart);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    setAutostartSource({fileName, newEntry});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto hideAutostart = toString(newEntry.data()).toLocal8Bit();
 | 
					    auto hideAutostart = toString(newEntry.data()).toLocal8Bit();
 | 
				
			||||||
    auto writeBytes = autostartFile.write(hideAutostart);
 | 
					    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
 | 
					double getScaleFactor() noexcept
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    auto sessionBus = QDBusConnection::sessionBus();
 | 
					    auto sessionBus = QDBusConnection::sessionBus();
 | 
				
			||||||
 | 
				
			|||||||
@ -23,11 +23,16 @@
 | 
				
			|||||||
#include <QTextStream>
 | 
					#include <QTextStream>
 | 
				
			||||||
#include <QUuid>
 | 
					#include <QUuid>
 | 
				
			||||||
#include <memory>
 | 
					#include <memory>
 | 
				
			||||||
#include <utility>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
QString getDeepinWineScaleFactor(const QString &appId) noexcept;
 | 
					QString getDeepinWineScaleFactor(const QString &appId) noexcept;
 | 
				
			||||||
double getScaleFactor() noexcept;
 | 
					double getScaleFactor() noexcept;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct AutostartSource
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    QString m_filePath;
 | 
				
			||||||
 | 
					    DesktopEntry m_entry;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ApplicationService : public QObject, protected QDBusContext
 | 
					class ApplicationService : public QObject, protected QDBusContext
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    Q_OBJECT
 | 
					    Q_OBJECT
 | 
				
			||||||
@ -172,6 +177,7 @@ private:
 | 
				
			|||||||
    qint64 m_lastLaunch{0};
 | 
					    qint64 m_lastLaunch{0};
 | 
				
			||||||
    double m_scaleFactor;
 | 
					    double m_scaleFactor;
 | 
				
			||||||
    std::weak_ptr<ApplicationManager1Storage> m_storage;
 | 
					    std::weak_ptr<ApplicationManager1Storage> m_storage;
 | 
				
			||||||
 | 
					    AutostartSource m_autostartSource;
 | 
				
			||||||
    QDBusObjectPath m_applicationPath;
 | 
					    QDBusObjectPath m_applicationPath;
 | 
				
			||||||
    QString m_launcher{getApplicationLauncherBinary()};
 | 
					    QString m_launcher{getApplicationLauncherBinary()};
 | 
				
			||||||
    DesktopFile m_desktopSource;
 | 
					    DesktopFile m_desktopSource;
 | 
				
			||||||
@ -179,7 +185,8 @@ private:
 | 
				
			|||||||
    QMap<QDBusObjectPath, QSharedPointer<InstanceService>> m_Instances;
 | 
					    QMap<QDBusObjectPath, QSharedPointer<InstanceService>> m_Instances;
 | 
				
			||||||
    void updateAfterLaunch(bool isLaunch) noexcept;
 | 
					    void updateAfterLaunch(bool isLaunch) noexcept;
 | 
				
			||||||
    static bool shouldBeShown(const std::unique_ptr<DesktopEntry> &entry) noexcept;
 | 
					    static bool shouldBeShown(const std::unique_ptr<DesktopEntry> &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<ApplicationManager1Service *>(QObject::parent()); }
 | 
					    [[nodiscard]] ApplicationManager1Service *parent() { return dynamic_cast<ApplicationManager1Service *>(QObject::parent()); }
 | 
				
			||||||
    [[nodiscard]] const ApplicationManager1Service *parent() const
 | 
					    [[nodiscard]] const ApplicationManager1Service *parent() const
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
				
			|||||||
@ -644,4 +644,12 @@ inline QString getAutostartAppIdFromAbsolutePath(const QString &path)
 | 
				
			|||||||
    return appId;
 | 
					    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
 | 
					#endif
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user