diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index ea458a1..4f8a423 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -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(); } } }); -} \ No newline at end of file + 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(); + } +} diff --git a/ukui-search-app-data-service/app-db-manager.h b/ukui-search-app-data-service/app-db-manager.h index 6552720..00ad7f7 100644 --- a/ukui-search-app-data-service/app-db-manager.h +++ b/ukui-search-app-data-service/app-db-manager.h @@ -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;