From c0276df65ad0ab1cff619014b2dc768225a84e09 Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Thu, 11 Jan 2024 15:52:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(app-database-service):=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=AF=B9=E5=88=A0=E9=99=A4=E5=BA=94=E7=94=A8=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E6=88=96=E5=85=B6=E7=88=B6=E7=9B=AE=E5=BD=95=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app-db-manager.cpp | 59 +++++++++++-------- ukui-search-app-data-service/app-db-manager.h | 1 + 2 files changed, 36 insertions(+), 24 deletions(-) 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;