Skip to content

Commit e490812

Browse files
committed
fix(watch): fix path format and add watch for newly created directories
Ensure directories exist before adding watches and fix path format consistency to match getDesktopFileDirs(). Dynamically add watch for newly created application directories. 修复目录路径格式问题,并在添加监听前确保目录存在。为动态创建的应用目录添加监听。 Log: 修复目录监听问题 PMS: BUG-340395 Influence: 修复了路径格式不一致导致的目录监听失败问题,确保新创建的应用目录能被正确监听,避免应用更新后无法及时刷新。
1 parent 7c0a6e9 commit e490812

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

src/dbus/applicationmanager1service.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,21 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce
8787
}
8888

8989
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &ApplicationManager1Service::ReloadApplications);
90-
auto unhandled = m_watcher.addPaths(getDesktopFileDirs());
90+
91+
// Ensure all directories exist before adding watches
92+
auto desktopDirs = getDesktopFileDirs();
93+
for (const auto &dirPath : std::as_const(desktopDirs)) {
94+
QDir dir(dirPath);
95+
if (!dir.exists()) {
96+
if (dir.mkpath(dirPath)) {
97+
qInfo() << "Created directory for watching:" << dirPath;
98+
} else {
99+
qWarning() << "Failed to create directory:" << dirPath;
100+
}
101+
}
102+
}
103+
104+
auto unhandled = m_watcher.addPaths(desktopDirs);
91105
for (const auto &dir : std::as_const(unhandled)) {
92106
qCritical() << "couldn't watch directory:" << dir;
93107
}
@@ -682,12 +696,27 @@ QString ApplicationManager1Service::addUserApplication(const QVariantMap &deskto
682696
}
683697

684698
QDir xdgDataHome;
685-
QString dir{getXDGDataHome() + "/applications"};
699+
QString dir{getXDGDataHome()};
700+
// Ensure consistent path format (with trailing separator) to match getDesktopFileDirs()
701+
if (!dir.endsWith(QDir::separator())) {
702+
dir.append(QDir::separator());
703+
}
704+
dir.append("applications");
705+
const bool dirExisted = QDir(dir).exists();
686706
if (!xdgDataHome.mkpath(dir)) {
687707
safe_sendErrorReply(QDBusError::Failed, "couldn't create directory of user applications.");
688708
return {};
689709
}
690710

711+
// If directory is newly created, add watch for it dynamically
712+
if (!dirExisted && !m_watcher.directories().contains(dir)) {
713+
if (!m_watcher.addPath(dir)) {
714+
qWarning() << "Failed to add watch for newly created directory:" << dir;
715+
} else {
716+
qInfo() << "Added watch for newly created directory:" << dir;
717+
}
718+
}
719+
691720
// 判断当前是否已经存在了desktop文件
692721
xdgDataHome.setPath(dir);
693722
const auto &filePath = xdgDataHome.filePath(name);

0 commit comments

Comments
 (0)