feat(app-database-service):新增对删除应用目录或其父目录的处理。

This commit is contained in:
JunjieBai 2024-01-11 15:52:01 +08:00 committed by iaom
parent dae035d35b
commit c0276df65a
2 changed files with 36 additions and 24 deletions

View File

@ -1614,31 +1614,26 @@ ApplicationDirWatcherHelper::ApplicationDirWatcherHelper(FileSystemWatcher *watc
void ApplicationDirWatcherHelper::addPath(const QString &path) {
QDir dir(path);
if (dir.exists() || dir.mkpath(path)) {
qDebug() << "=====add desktop watch=====" << path;
m_watcher->addWatch(path);
return;
}
auto watcher = new FileSystemWatcher(false);
m_watcherMap.insert(path, watcher);
while (!dir.exists()) {
QString dirPath = dir.absolutePath();
dir.setPath(dirPath.left(dirPath.lastIndexOf("/")));
if (dir.exists() || dir.mkpath(path)) {
m_watcher->addWatch(path);
qDebug() << "=====desktop file's watcher add path:" << path;
watcher->addWatch(path);
qDebug() << "=====desktop dirs' Watcher add path:" << path;
} else {
while (!dir.exists()) {
dir.setPath(dir.absolutePath().left(dir.absolutePath().lastIndexOf("/")));
}
watcher->addWatch(dir.absolutePath());
qDebug() << "=====desktop dirs' Watcher add path:" << dir.absolutePath();
}
qDebug() << "=====addWatch=====" << dir.absolutePath();
watcher->addWatch(dir.absolutePath());
connect(watcher, &FileSystemWatcher::created, this, [ &,watcher ] (const QString &path, bool isDir) {
if (isDir) {
QString appPath = m_watcherMap.key(watcher);
if (appPath == path) {
qDebug() << "=====add desktop watch=====" << appPath;
m_watcher->addWatch(path);
m_watcherMap.value(appPath)->deleteLater();
m_watcherMap.remove(appPath);
} else if (appPath.startsWith(path+ "/")) {
if (appPath.startsWith(path+ "/") || appPath == path) {
watcher->clearAll();
QDir dir(appPath);
//会有类似mkdir -p的一次性将子文件夹创建出来的情况需要挨层判断
@ -1646,16 +1641,32 @@ void ApplicationDirWatcherHelper::addPath(const QString &path) {
dir.setPath(dir.absolutePath().left(dir.absolutePath().lastIndexOf("/")));
}
if (dir.absolutePath() == appPath) {
qDebug() << "=====add desktop watch=====" << appPath;
m_watcher->addWatch(appPath);
m_watcherMap.value(appPath)->deleteLater();
m_watcherMap.remove(appPath);
} else {
qDebug() << "=====addWatch=====" << dir.absolutePath();
watcher->addWatch(dir.absolutePath());
qDebug() << "=====desktop file's watcher add path:" << appPath;
}
watcher->addWatch(dir.absolutePath());
qDebug() << "=====desktop dirs' watcher add path:" << dir.absolutePath();
}
}
});
}
connect(watcher, &FileSystemWatcher::deleted, this, [ &,watcher ](const QString &path, bool isDir) {
if (isDir) {
if (m_watcherMap.keys().contains(path)) {
QDir dir(path);
//会有类似rm -r的一次性将子文件夹删除的情况需要挨层判断
while (!dir.exists()) {
dir.setPath(dir.absolutePath().left(dir.absolutePath().lastIndexOf("/")));
}
watcher->addWatch(dir.absolutePath());
qDebug() << "=====desktop dirs' watcher add path:" << dir.absolutePath();
}
}
});
}
ApplicationDirWatcherHelper::~ApplicationDirWatcherHelper() {
for (auto it = m_watcherMap.constBegin(); it != m_watcherMap.constEnd(); it++) {
it.value()->deleteLater();
}
}

View File

@ -259,6 +259,7 @@ class ApplicationDirWatcherHelper : public QObject
Q_OBJECT
public:
explicit ApplicationDirWatcherHelper(FileSystemWatcher *watcher,QObject *parent = nullptr);
~ApplicationDirWatcherHelper();
void addPath(const QString& path);
private:
FileSystemWatcher *m_watcher = nullptr;