From 6911e159adf41c2c1897084954dc15022759cb97 Mon Sep 17 00:00:00 2001 From: iaom Date: Sat, 8 Apr 2023 11:45:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0dirWatcher=E4=B8=AD=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=85=BC=E5=AE=B9=E8=80=81=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dirwatcher/config.cpp | 103 ++++++++++++++---- .../dirwatcher/config.h | 16 ++- .../dirwatcher/dir-watcher.cpp | 3 + .../dirwatcher/search-dir.cpp | 10 ++ .../dirwatcher/search-dir.h | 5 + .../dirwatcher/volume-manager.cpp | 6 +- 6 files changed, 114 insertions(+), 29 deletions(-) diff --git a/ukui-search-service-dir-manager/dirwatcher/config.cpp b/ukui-search-service-dir-manager/dirwatcher/config.cpp index 61e6a8d..c68f241 100644 --- a/ukui-search-service-dir-manager/dirwatcher/config.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/config.cpp @@ -2,6 +2,7 @@ #include #include #include +#include static const QString HOME_PATH = QDir::homePath(); static const QString CURRENT_INDEXABLE_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf"; @@ -10,29 +11,35 @@ static const QString CONFIG_VERSION_KEY = "ConfigVersion"; static const QString CONFIG_VERSION = "1.0"; static const QString GLOBAL_BLACK_LIST_KEY = "GlobalBlackList"; static const QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"}; +static const QString GLOBAL_SETTINGS_GROUP = "GlobalSettings"; +static const QString SEARCH_DIRS_GROUP = "SearchDirs"; + static std::once_flag flag; -static Config *global_intance = nullptr; +static Config *global_instance = nullptr; Config *Config::self() { std::call_once(flag, [ & ] { - global_intance = new Config(); + global_instance = new Config(); }); - return global_intance; + return global_instance; } void Config::addDir(const SearchDir &dir) { - m_settings->setValue(dir.getPath(), dir.getBlackList()); + m_searchDirs.append(dir); + m_settings->beginGroup(SEARCH_DIRS_GROUP); + m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList()); + m_settings->endGroup(); m_settings->sync(); } QStringList Config::removeDir(const SearchDir &dir) { QString url = QUrl::fromLocalFile(dir.getPath()).toString(); - QStringList blackDirs = m_settings->value(url).toStringList(); - m_settings->remove(url); - m_settings->sync(); + QStringList blackDirs = m_settings->value(SEARCH_DIRS_GROUP + "/" + url).toStringList(); + removeDir(url); + m_searchDirs.removeOne(dir); return blackDirs; } @@ -41,31 +48,47 @@ bool Config::isCompatibilityMode() return m_compatibilityMode; } -QStringList Config::searchDirs() const +QStringList Config::searchDirs() { QStringList tmp; for(const SearchDir& dir : m_searchDirs) { - tmp.append(dir.getPath()); + if(QFile::exists(dir.getPath())) { + tmp.append(dir.getPath()); + } else { + m_searchDirs.removeOne(dir); + removeDir(QUrl::fromLocalFile(dir.getPath()).toString()); + } } return tmp; } -QStringList Config::blackDirs() const +QStringList Config::blackDirs() { QStringList tmp; for(const SearchDir& dir : m_searchDirs) { - tmp.append(dir.getBlackList()); + if(QFile::exists(dir.getPath())) { + tmp.append(dir.getBlackList()); + } else { + m_searchDirs.removeOne(dir); + removeDir(QUrl::fromLocalFile(dir.getPath()).toString()); + } } return tmp; } QStringList Config::globalBlackList() const { - return m_settings->value(GLOBAL_BLACK_LIST_KEY).toStringList(); + return m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + GLOBAL_BLACK_LIST_KEY).toStringList(); } -QVector Config::searchDirObjects() const +QVector Config::searchDirObjects() { + for(const SearchDir& dir : m_searchDirs) { + if(!QFile::exists(dir.getPath())) { + m_searchDirs.removeOne(dir); + removeDir(QUrl::fromLocalFile(dir.getPath()).toString()); + } + } return m_searchDirs; } @@ -74,22 +97,62 @@ Config::Config() m_settings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat); if(!(m_settings->value(CONFIG_VERSION_KEY).toString() == CONFIG_VERSION)) { m_compatibilityMode = true; - for(const QString& path : m_settings->value(INDEXABLE_DIR_KEY).toStringList()) { - SearchDir dir(QUrl::fromLocalFile(path).toString()); - m_searchDirs.append(dir); - m_settings->setValue(QUrl::fromLocalFile(path).toString(), dir.getBlackList()); + for(const QString& path : m_settings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) { + if(QFile::exists(path)) { + SearchDir dir(path, false); + m_compatibleCache.append(dir); + } } m_settings->remove(INDEXABLE_DIR_KEY); + m_settings->beginGroup(GLOBAL_SETTINGS_GROUP); m_settings->setValue(CONFIG_VERSION_KEY, CONFIG_VERSION); + m_settings->endGroup(); } else { + m_settings->beginGroup(SEARCH_DIRS_GROUP); for(const QString& urlStr : m_settings->allKeys()) { - SearchDir dir(QUrl(urlStr).toString(), false); - dir.setBlackList(m_settings->value(urlStr).toStringList()); - m_searchDirs.append(dir); + QString path = QUrl(urlStr).toLocalFile(); + if(QFile::exists(path)) { + SearchDir dir(path, false); + dir.setBlackList(m_settings->value(urlStr).toStringList()); + m_searchDirs.append(dir); + } else { + removeDir(urlStr); + } } + m_settings->endGroup(); } + m_settings->beginGroup(GLOBAL_SETTINGS_GROUP); if(m_settings->value(GLOBAL_BLACK_LIST_KEY).isNull()) { m_settings->setValue(GLOBAL_BLACK_LIST_KEY, GLOBAL_BLACK_LIST); } + m_settings->endGroup(); + m_settings->sync(); } + +void Config::removeDir(const QString &url) +{ + m_settings->beginGroup(SEARCH_DIRS_GROUP); + m_settings->remove(url); + m_settings->endGroup(); + m_settings->sync(); +} + +void Config::processCompatibleCache() +{ + if(this->isCompatibilityMode()) { + m_settings->beginGroup(SEARCH_DIRS_GROUP); + for(SearchDir dir : m_compatibleCache) { + dir.generateBlackList(); + if(dir.error() == SearchDir::ErrorInfo::Successful) { + m_searchDirs.append(dir); + m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList()); + } + } + m_settings->endGroup(); + + m_settings->sync(); + m_compatibleCache.clear(); + m_compatibilityMode = false; + } +} diff --git a/ukui-search-service-dir-manager/dirwatcher/config.h b/ukui-search-service-dir-manager/dirwatcher/config.h index 97979c8..91ec307 100644 --- a/ukui-search-service-dir-manager/dirwatcher/config.h +++ b/ukui-search-service-dir-manager/dirwatcher/config.h @@ -11,21 +11,25 @@ public: static Config *self(); void addDir(const SearchDir& dir); QStringList removeDir(const SearchDir& dir); + /** - * @brief 兼容模式,自动提取老版本数据生成黑名单 - * @return + * @brief 处理老版本数据,需要调用 */ - bool isCompatibilityMode(); - QVector searchDirObjects() const; - QStringList searchDirs() const; - QStringList blackDirs() const; + void processCompatibleCache(); + QVector searchDirObjects(); + QStringList searchDirs(); + QStringList blackDirs(); QStringList globalBlackList() const; private: Config(); + void removeDir(const QString& url); + bool isCompatibilityMode(); + QSettings *m_settings = nullptr; bool m_compatibilityMode = false; QVector m_searchDirs; + QVector m_compatibleCache; }; #endif // CONFIG_H diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp index 9054740..2734583 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp @@ -19,6 +19,9 @@ QMutex DirWatcher::s_mutex; DirWatcher::DirWatcher(QObject *parent) : QObject(parent) { + //兼容旧版配置 +// Config::self()->processCompatibleCache(); + m_qSettings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat); this->currentIndexableDir(); if (m_indexableDirList.isEmpty()) { diff --git a/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp b/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp index 45e97b1..a14c6b2 100644 --- a/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/search-dir.cpp @@ -8,6 +8,11 @@ SearchDir::SearchDir(const QString &path, bool generateBlackList) } } +bool SearchDir::operator ==(const SearchDir &rhs) const +{ + return (m_path == rhs.m_path); +} + SearchDir::ErrorInfo SearchDir::error() { return m_error; @@ -45,3 +50,8 @@ QStringList SearchDir::getBlackList() const { return m_blackList; } + +void SearchDir::generateBlackList() +{ + //生成黑名单 +} diff --git a/ukui-search-service-dir-manager/dirwatcher/search-dir.h b/ukui-search-service-dir-manager/dirwatcher/search-dir.h index e6a9ce2..f645713 100644 --- a/ukui-search-service-dir-manager/dirwatcher/search-dir.h +++ b/ukui-search-service-dir-manager/dirwatcher/search-dir.h @@ -13,11 +13,16 @@ public: NotExists }; SearchDir(const QString& path, bool generateBlackList = true); + bool operator == (const SearchDir& rhs) const; ErrorInfo error(); QString errorString(); QString getPath() const; void setBlackList(const QStringList& blackList); QStringList getBlackList() const; + /** + * @brief 重新生成黑名单 + */ + void generateBlackList(); private: QString m_path; QStringList m_blackList; diff --git a/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp b/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp index c502509..c491b02 100644 --- a/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp @@ -3,7 +3,7 @@ #include #include static std::once_flag flag; -static VolumeManager *global_intance = nullptr; +static VolumeManager *global_instance = nullptr; Volume::Volume() { @@ -62,9 +62,9 @@ void Volume::addMountPoint(const QString &mountPoint, const QString &subVolume) VolumeManager *VolumeManager::self() { std::call_once(flag, [ & ] { - global_intance = new VolumeManager(); + global_instance = new VolumeManager(); }); - return global_intance; + return global_instance; } VolumeManager::VolumeManager(QObject *parent)