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) bool KeyFile::saveToFile(const std::string &filePath)
{ {
FILE *sfp = fopen(filePath.data(), "w+"); FILE *sfp = fopen(filePath.data(), "w+");
if (!sfp) if (!sfp) {
perror("open file failed...");
return false; return false;
}
for (const auto &im : m_mainKeyMap) { for (const auto &im : m_mainKeyMap) {
const auto &keyMap = im.second; const auto &keyMap = im.second;

View File

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

View File

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

View File

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

View File

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