refact: scanAutostart and get/set autostart

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2024-01-04 11:17:26 +08:00 committed by Comix
parent 5c75836fd2
commit 8e83422603
4 changed files with 66 additions and 36 deletions

View File

@ -378,6 +378,7 @@ QHash<QSharedPointer<ApplicationService>, 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<QSharedPointer<ApplicationService>, 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<QSharedPointer<ApplicationService>, 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<QDBusObjectPath> ApplicationManager1Service::list() const
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();
QSharedPointer<ApplicationService> application =
ApplicationService::createApplicationService(std::move(desktopFileSource), this, m_storage);
@ -454,15 +464,7 @@ QSharedPointer<ApplicationService> 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;

View File

@ -107,14 +107,7 @@ QSharedPointer<ApplicationService> 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,10 +574,14 @@ 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;
DesktopEntry s;
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();
@ -592,11 +589,11 @@ bool ApplicationService::autostartCheck(const QString &filePath) noexcept
}
QTextStream stream{&file};
DesktopEntry s;
if (auto err = s.parse(stream); err != ParserError::NoError) {
qWarning() << "parse" << filePath << "failed:" << err;
return false;
}
}
auto hiddenVal = s.value(DesktopFileEntryKey, DesktopEntryHidden);
if (!hiddenVal) {
@ -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();

View File

@ -23,11 +23,16 @@
#include <QTextStream>
#include <QUuid>
#include <memory>
#include <utility>
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<ApplicationManager1Storage> m_storage;
AutostartSource m_autostartSource;
QDBusObjectPath m_applicationPath;
QString m_launcher{getApplicationLauncherBinary()};
DesktopFile m_desktopSource;
@ -179,7 +185,8 @@ private:
QMap<QDBusObjectPath, QSharedPointer<InstanceService>> m_Instances;
void updateAfterLaunch(bool isLaunch) 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]] const ApplicationManager1Service *parent() const
{

View File

@ -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