From fbd391d110eaa14c4e002c7a884437fd4925f00b Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Wed, 12 Apr 2023 15:05:25 +0800 Subject: [PATCH] Fix the proble that can not add '/' to the block dirs in ukcc plugin. --- libsearch/file-utils.cpp | 7 +++-- libsearch/global-settings.cpp | 19 +++++++++++-- libsearch/index/search-manager.cpp | 14 +++++++--- search-ukcc-plugin/search.cpp | 43 ++++++++++++++++++++---------- 4 files changed, 59 insertions(+), 24 deletions(-) diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 89ccb03..681f311 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -185,10 +185,9 @@ QString FileUtils::getSettingName(const QString &setting) { bool FileUtils::isOrUnder(QString pathA, QString pathB) { - if(pathA[0] != "/") - pathA.prepend("/"); - if(pathB[0] != "/") - pathB.prepend("/"); + if (pathB == "/") { + return true; + } if(pathA.length() < pathB.length()) return false; diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index 44cb0ff..38e4955 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -37,7 +37,11 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) //搜索黑名单过滤 m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this); m_blockDirsSettings->setIniCodec(QTextCodec::codecForName("UTF-8")); - m_blockDirsSettings->setValue("These_are_block_dirs_conf_for_ukui_search","0"); + if (!QFile::exists(BLOCK_DIRS)) { + QFile file(BLOCK_DIRS); + file.open(QIODevice::ReadOnly); + file.close(); + } m_blockDirsSettings->sync(); m_confWatcher = new QFileSystemWatcher(this); @@ -177,7 +181,18 @@ bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool rem } QStringList GlobalSettings::getBlockDirs() { - return m_blockDirsSettings->allKeys(); + QStringList blockList; + QStringList tmp = m_blockDirsSettings->allKeys(); + for (const QString& blockDir : tmp) { + QString wholePath = "/" + blockDir; + if (QFile::exists(wholePath)) { + blockList.append(wholePath); + } else { + m_blockDirsSettings->remove(blockDir); + m_blockDirsSettings->sync(); + } + } + return blockList; } void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) { diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index e6aa032..1623145 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -497,11 +497,17 @@ void DirectSearch::run() { QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir(); QQueue bfs; for (const QString &path : searchPath) { - if (blockList.contains(path)) { - continue; + bool underBlock(false); + for (const QString &blockDir : blockList) { + if (FileUtils::isOrUnder(path, blockDir)) { + underBlock = true; + break; + } + } + if (!underBlock) { + blockList.append(DirWatcher::getDirWatcher()->blackListOfDir(path)); + bfs.enqueue(path); } - blockList.append(DirWatcher::getDirWatcher()->blackListOfDir(path)); - bfs.enqueue(path); } if (bfs.isEmpty()) { return; diff --git a/search-ukcc-plugin/search.cpp b/search-ukcc-plugin/search.cpp index 352415d..cdc9a55 100644 --- a/search-ukcc-plugin/search.cpp +++ b/search-ukcc-plugin/search.cpp @@ -520,8 +520,18 @@ void Search::initFileDialog() void Search::getBlockDirs() { m_blockDirs.clear(); - if (m_dirSettings) - m_blockDirs = m_dirSettings->allKeys(); + if (m_dirSettings) { + QStringList blockList = m_dirSettings->allKeys(); + for (const QString &blockDir : blockList) { + QString wholePath = "/" + blockDir; + if (QFile::exists(wholePath)) { + m_blockDirs << wholePath; + } else { + m_dirSettings->remove(blockDir); + m_dirSettings->sync(); + } + } + } } /** @@ -538,31 +548,37 @@ int Search::setBlockDir(const QString &dirPath, const bool &is_add) } //删除黑名单目录 m_dirSettings->remove(dirPath); + m_dirSettings->sync(); removeBlockDirFromList(dirPath); return ReturnCode::Succeed; } -// if (!dirPath.startsWith(QDir::homePath())) { -// return ReturnCode::NotInHomeDir; -// } - QString pathKey = dirPath.right(dirPath.length() - 1); + QStringList oldBlockList = m_blockDirs; + getBlockDirs(); + for (const QString& oldBlockDir : oldBlockList) { + if (!m_blockDirs.contains(oldBlockDir)) { + removeBlockDirFromList(oldBlockDir); + } + } + for (QString dir : m_blockDirs) { - if (pathKey == dir) { + if (dirPath == dir) { return ReturnCode::HasBeenBlocked; } - if (pathKey.startsWith(dir)) { + if (dirPath.startsWith(dir + "/") || dir == "/") { return ReturnCode::ParentExist; } //有它的子文件夹已被添加,删除这些子文件夹 - if (dir.startsWith(pathKey)) { + if (dir.startsWith(dirPath + "/") || dirPath == "/") { m_dirSettings->remove(dir); - removeBlockDirFromList("/" + dir); + removeBlockDirFromList(dir); } } - m_dirSettings->setValue(pathKey, "0"); + m_dirSettings->setValue(dirPath.right(dirPath.length() - 1), "0"); + m_dirSettings->sync(); appendBlockDirToList(dirPath); return ReturnCode::Succeed; } @@ -574,9 +590,8 @@ void Search::initBlockDirsList() { getBlockDirs(); for (QString path: m_blockDirs) { - QString wholePath = QString("/%1").arg(path); - if (QFileInfo(wholePath).isDir() /*&& path.startsWith("home")*/) { - appendBlockDirToList(wholePath); + if (QFileInfo(path).isDir() /*&& path.startsWith("home")*/) { + appendBlockDirToList(path); } } }