fix: 修复设置开机自启动应用重启后失效问题

1. 设置自动启动时未将应用desktop文件写入到~/.config/autostart/目录下
2. 应用自动启动接口优化,确保调用时,接口返回正确。
(设置为自启动时,手动将Hidden字段写入到自启动目录的desktop文件中,并设置为false,只有这样,
安全中心才不会弹出自启动确认窗口, 这种操作是沿用V20阶段的约定规范,这块已经与安全中心研发对接过)

Log:
Influence: 设置自启动后重启应用也会保持生效
Bug: https://pms.uniontech.com/bug-view-172263.html
Bug: https://pms.uniontech.com/bug-view-172281.html
Task: https://pms.uniontech.com/task-view-215413.html
Change-Id: Idd03ac40850c95ef8cef2ac169cfe006405b809b
This commit is contained in:
songwentao 2022-11-23 14:36:04 +08:00
parent f6e6739532
commit 8be1b299e6
5 changed files with 85 additions and 114 deletions

View File

@ -188,9 +188,10 @@ void KeyFile::setKey(const std::string &section, const std::string &key, const s
bool KeyFile::saveToFile(const std::string &filePath)
{
FILE *sfp = fopen(filePath.data(), "w+");
if (!sfp)
if (!sfp) {
perror("open file failed...");
return false;
}
for (const auto &im : m_mainKeyMap) {
const auto &keyMap = im.second;

View File

@ -56,7 +56,7 @@ StartManager::StartManager(QObject *parent)
// load sysMemLimitConfig
loadSysMemLimitConfig();
autostartFiles = getAutostartList();
m_autostartFiles = getAutostartList();
// listen autostart files
listenAutostartFileEvents();
@ -66,49 +66,42 @@ StartManager::StartManager(QObject *parent)
//startAutostartProgram();
}
/**
* @brief StartManager::addAutostart
* @param fileName desktopFile
* @return
*/
bool StartManager::addAutostart(QString fileName)
bool StartManager::addAutostart(const QString &desktop)
{
return setAutostart(fileName, true);
return setAutostart(desktop, true);
}
/**
* @brief StartManager::removeAutostart
* @param fileName desktopFile
* @return
*/
bool StartManager::removeAutostart(QString fileName)
bool StartManager::removeAutostart(const QString &desktop)
{
return setAutostart(fileName, false);
return setAutostart(desktop, false);
}
QStringList StartManager::autostartList()
{
if (autostartFiles.size() == 0) {
autostartFiles = getAutostartList();
if (m_autostartFiles.isEmpty()) {
m_autostartFiles = getAutostartList();
}
return autostartFiles;
return m_autostartFiles;
}
/**
/**desktop为全路径或者相对路径都应该返回true
* false
* @brief StartManager::isAutostart
* @param fileName desktopFile
* @param desktop
* @return
*/
bool StartManager::isAutostart(QString fileName)
bool StartManager::isAutostart(const QString &desktop)
{
if (!fileName.endsWith(DESKTOPEXT))
if (!desktop.endsWith(DESKTOPEXT))
return false;
QFileInfo file(desktop);
for (auto autostartDir : BaseDir::autoStartDirs()) {
std::string filePath = autostartDir + fileName.toStdString();
if (DFile::isExisted(filePath)) {
DesktopInfo info(filePath);
std::string filePath = autostartDir + file.baseName().toStdString();
QDir dir(autostartDir.c_str());
if (dir.exists(file.fileName())) {
DesktopInfo info(desktop.toStdString());
if (info.isValidDesktop() && !info.getIsHidden()) {
return true;
}
@ -182,7 +175,7 @@ void StartManager::onAutoStartupPathChange(const QString &dirPath)
{
QStringList autostartFilesList = getAutostartList();
QSet<QString> newAutostartFiles = QSet<QString>::fromList(autostartFilesList);
QSet<QString> oldAutostartFiles = QSet<QString>::fromList(autostartFiles);
QSet<QString> oldAutostartFiles = QSet<QString>::fromList(m_autostartFiles);
// 添加
QSet<QString> newFiles = newAutostartFiles - oldAutostartFiles;
@ -193,7 +186,7 @@ void StartManager::onAutoStartupPathChange(const QString &dirPath)
QStringList deleteFile = deletedFiles.toList();
// 更新autostartFiles记录
autostartFiles = autostartFilesList;
m_autostartFiles = autostartFilesList;
for (auto &file : newFile) {
Q_EMIT autostartChanged(autostartAdded, file);
@ -204,72 +197,65 @@ void StartManager::onAutoStartupPathChange(const QString &dirPath)
}
}
bool StartManager::setAutostart(QString fileName, bool value)
bool StartManager::setAutostart(const QString &desktop, const bool value)
{
if (!fileName.endsWith(DESKTOPEXT))
return false;
if (isAutostart(fileName) == value)
return true;
QFileInfo info(fileName);
QString appId = info.baseName();
QString autostartDir(BaseDir::userAutoStartDir().c_str());
QString autostartFileName = fileName;
bool isUserAutostart = false;
// if has no user autostart file, create it
if (info.isAbsolute()) {
if (info.exists() && info.baseName() == autostartDir) {
isUserAutostart = true;
}
} else {
autostartFileName = autostartDir + fileName;
if (DFile::isExisted(autostartFileName.toStdString())) {
isUserAutostart = true;
}
}
if (!isUserAutostart && !DFile::isExisted(autostartFileName.toStdString())) {
// get system autostart desktop file
for (auto appDir : BaseDir::appDirs()) {
QString desktopFullPath = desktop;
QFileInfo info(desktopFullPath);
if (!info.isAbsolute()) {
for (const std::string &appDir : BaseDir::appDirs()) {
QDir dir(appDir.c_str());
dir.setFilter(QDir::Files);
dir.setNameFilters({ "*.desktop" });
bool hiddenStatusChanged = false;
for (auto entry : dir.entryInfoList()) {
QString desktopFile = entry.absoluteFilePath();
if (!desktopFile.contains(fileName))
for (const auto &entry : dir.entryInfoList()) {
const QString &desktopPath = entry.absoluteFilePath();
if (!desktopPath.contains(desktop))
continue;
if (!QFile::copy(desktopFile, autostartFileName)) // copy origin file
return false;
hiddenStatusChanged = true;
desktopFullPath = desktopPath;
info.setFile(desktopFullPath);
break;
}
if (hiddenStatusChanged)
break;
}
}
QDir autostartDir(BaseDir::userAutoStartDir().c_str());
const QString &appId = info.baseName();
// change autostart hidden status in file
KeyFile kf;
kf.loadFile(autostartFileName.toStdString());
kf.setKey(MainSection, KeyXDeepinCreatedBy.toStdString(), AMServiceName.toStdString());
kf.setKey(MainSection, KeyXDeepinAppID.toStdString(), appId.toStdString());
kf.setBool(MainSection, KeyHidden, !value ? "true" : "false");
kf.saveToFile(autostartFileName.toStdString());
if (value && isAutostart(desktopFullPath)) {
qWarning() << "invalid desktop or item is already in the autostart list.";
return false;
}
if (value && autostartFiles.indexOf(fileName) != -1) {
autostartFiles.push_back(fileName);
} else if (!value) {
autostartFiles.removeAll(fileName);
}
if (!value && !isAutostart(desktopFullPath)) {
qWarning() << "can't find autostart item";
return false;
}
Q_EMIT autostartChanged(value ? autostartAdded : autostartDeleted, fileName);
return true;
const QString &autostartDesktopPath = autostartDir.path() + QString("/") + info.fileName();
if (value && !m_autostartFiles.contains(desktopFullPath)) {
m_autostartFiles.push_back(desktopFullPath);
const bool ret = QFile::copy(info.filePath(), autostartDesktopPath);
if (!ret)
qWarning() << "add to autostart list failed...";
/* 设置为自启动时手动将Hidden字段写入到自启动目录的desktop文件中并设置为false只有这样
* , 沿V20阶段的约定规范 */
KeyFile kf;
kf.loadFile(autostartDesktopPath.toStdString());
kf.setKey(MainSection, KeyXDeepinCreatedBy.toStdString(), AMServiceName.toStdString());
kf.setKey(MainSection, KeyXDeepinAppID.toStdString(), appId.toStdString());
kf.setBool(MainSection, KeyHidden, "false");
kf.saveToFile(autostartDesktopPath.toStdString());
} else if (!value && m_autostartFiles.contains(desktopFullPath)) {
m_autostartFiles.removeAll(desktopFullPath);
autostartDir.remove(info.fileName());
} else {
qWarning() << "error happen...";
return false;
}
Q_EMIT autostartChanged(value ? autostartAdded : autostartDeleted, desktopFullPath);
return true;
}
bool StartManager::doLaunchAppWithOptions(const QString &desktopFile)
@ -473,21 +459,5 @@ void StartManager::startAutostartProgram()
QStringList StartManager::getAutostartList()
{
QStringList ret;
for (auto autostartDir : BaseDir::autoStartDirs()) {
if (!DFile::isExisted(autostartDir))
continue;
QDir dir(autostartDir.c_str());
dir.setFilter(QDir::Files);
dir.setNameFilters({ "*.desktop" });
for (auto entry : dir.entryInfoList()) {
if (ret.contains(entry.absoluteFilePath()))
continue;
ret.push_back(entry.absoluteFilePath());
}
}
return ret;
return m_autostartFiles;
}

View File

@ -38,10 +38,10 @@ class StartManager : public QObject
public:
explicit StartManager(QObject *parent = nullptr);
bool addAutostart(QString fileName);
bool removeAutostart(QString fileName);
bool addAutostart(const QString &desktop);
bool removeAutostart(const QString &desktop);
QStringList autostartList();
bool isAutostart(QString fileName);
bool isAutostart(const QString &desktop);
bool isMemSufficient();
void launchApp(const QString &desktopFile);
void launchApp(QString desktopFile, uint32_t timestamp, QStringList files);
@ -51,13 +51,13 @@ public:
void runCommandWithOptions(QString exe, QStringList args, QMap<QString, QString> options);
Q_SIGNALS:
void autostartChanged(QString status, QString fileName);
void autostartChanged(const QString &status, const QString &fileName);
public Q_SLOTS:
void onAutoStartupPathChange(const QString &dirPath);
private:
bool setAutostart(QString fileName, bool value);
bool setAutostart(const QString &fileName, const bool value);
bool doLaunchAppWithOptions(const QString &desktopFile);
bool doLaunchAppWithOptions(QString desktopFile, uint32_t timestamp, QStringList files, QMap<QString, QString> options);
bool launch(DesktopInfo *info, QString cmdLine, uint32_t timestamp, QStringList files);
@ -74,7 +74,7 @@ private:
uint64_t minMemAvail;
uint64_t maxSwapUsed;
StartManagerDBusHandler *dbusHandler;
QStringList autostartFiles;
QStringList m_autostartFiles;
QFileSystemWatcher *fileWatcher;
ApplicationManager *am;
};

View File

@ -246,16 +246,16 @@ QList<QDBusObjectPath> ApplicationManager::GetInstances(const QString& id)
return {};
}
bool ApplicationManager::AddAutostart(QString fileName)
bool ApplicationManager::AddAutostart(const QString &desktop)
{
Q_D(ApplicationManager);
if (!d->checkDMsgUid())
return false;
return d->startManager->addAutostart(fileName);
return d->startManager->addAutostart(desktop);
}
bool ApplicationManager::RemoveAutostart(QString fileName)
bool ApplicationManager::RemoveAutostart(const QString &fileName)
{
Q_D(ApplicationManager);
if (!d->checkDMsgUid())
@ -273,7 +273,7 @@ QStringList ApplicationManager::AutostartList()
return d->startManager->autostartList();
}
bool ApplicationManager::IsAutostart(QString fileName)
bool ApplicationManager::IsAutostart(const QString &fileName)
{
Q_D(ApplicationManager);
if (!d->checkDMsgUid())
@ -301,7 +301,7 @@ void ApplicationManager::LaunchApp(const QString &desktopFile, uint32_t timestam
d->startManager->launchApp(desktopFile, timestamp, files);
}
void ApplicationManager::LaunchAppAction(QString desktopFile, QString action, uint32_t timestamp)
void ApplicationManager::LaunchAppAction(const QString &desktopFile, const QString &action, uint32_t timestamp)
{
Q_D(ApplicationManager);
if (!d->checkDMsgUid())

View File

@ -64,16 +64,16 @@ public:
void processInstanceStatus(Methods::ProcessStatus instanceStatus);
Q_SIGNALS:
void AutostartChanged(QString status, QString filePath);
void AutostartChanged(const QString &status, const QString &filePath);
public Q_SLOTS:
bool AddAutostart(QString fileName);
bool AddAutostart(const QString &desktop);
QStringList AutostartList();
bool IsAutostart(QString fileName);
bool RemoveAutostart(QString fileName);
bool IsAutostart(const QString &fileName);
bool RemoveAutostart(const QString &fileName);
void Launch(const QString &desktopFile);
void LaunchApp(const QString &desktopFile, uint32_t timestamp, const QStringList &files);
void LaunchAppAction(QString desktopFile, QString action, uint32_t timestamp);
void LaunchAppAction(const QString &desktopFile, const QString &action, uint32_t timestamp);
protected:
ApplicationManager(QObject *parent = nullptr);