Fix the proble that can not add '/' to the block dirs in ukcc plugin.

This commit is contained in:
JunjieBai 2023-04-12 15:05:25 +08:00
parent 5481dd7ed8
commit fbd391d110
4 changed files with 59 additions and 24 deletions

View File

@ -185,10 +185,9 @@ QString FileUtils::getSettingName(const QString &setting) {
bool FileUtils::isOrUnder(QString pathA, QString pathB) bool FileUtils::isOrUnder(QString pathA, QString pathB)
{ {
if(pathA[0] != "/") if (pathB == "/") {
pathA.prepend("/"); return true;
if(pathB[0] != "/") }
pathB.prepend("/");
if(pathA.length() < pathB.length()) if(pathA.length() < pathB.length())
return false; return false;

View File

@ -37,7 +37,11 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
//搜索黑名单过滤 //搜索黑名单过滤
m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this); m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this);
m_blockDirsSettings->setIniCodec(QTextCodec::codecForName("UTF-8")); 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_blockDirsSettings->sync();
m_confWatcher = new QFileSystemWatcher(this); m_confWatcher = new QFileSystemWatcher(this);
@ -177,7 +181,18 @@ bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool rem
} }
QStringList GlobalSettings::getBlockDirs() { 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) { void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) {

View File

@ -497,12 +497,18 @@ void DirectSearch::run() {
QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir(); QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir();
QQueue<QString> bfs; QQueue<QString> bfs;
for (const QString &path : searchPath) { for (const QString &path : searchPath) {
if (blockList.contains(path)) { bool underBlock(false);
continue; for (const QString &blockDir : blockList) {
if (FileUtils::isOrUnder(path, blockDir)) {
underBlock = true;
break;
} }
}
if (!underBlock) {
blockList.append(DirWatcher::getDirWatcher()->blackListOfDir(path)); blockList.append(DirWatcher::getDirWatcher()->blackListOfDir(path));
bfs.enqueue(path); bfs.enqueue(path);
} }
}
if (bfs.isEmpty()) { if (bfs.isEmpty()) {
return; return;
} }

View File

@ -520,8 +520,18 @@ void Search::initFileDialog()
void Search::getBlockDirs() void Search::getBlockDirs()
{ {
m_blockDirs.clear(); m_blockDirs.clear();
if (m_dirSettings) if (m_dirSettings) {
m_blockDirs = m_dirSettings->allKeys(); 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->remove(dirPath);
m_dirSettings->sync();
removeBlockDirFromList(dirPath); removeBlockDirFromList(dirPath);
return ReturnCode::Succeed; 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) { for (QString dir : m_blockDirs) {
if (pathKey == dir) { if (dirPath == dir) {
return ReturnCode::HasBeenBlocked; return ReturnCode::HasBeenBlocked;
} }
if (pathKey.startsWith(dir)) { if (dirPath.startsWith(dir + "/") || dir == "/") {
return ReturnCode::ParentExist; return ReturnCode::ParentExist;
} }
//有它的子文件夹已被添加,删除这些子文件夹 //有它的子文件夹已被添加,删除这些子文件夹
if (dir.startsWith(pathKey)) { if (dir.startsWith(dirPath + "/") || dirPath == "/") {
m_dirSettings->remove(dir); 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); appendBlockDirToList(dirPath);
return ReturnCode::Succeed; return ReturnCode::Succeed;
} }
@ -574,9 +590,8 @@ void Search::initBlockDirsList()
{ {
getBlockDirs(); getBlockDirs();
for (QString path: m_blockDirs) { for (QString path: m_blockDirs) {
QString wholePath = QString("/%1").arg(path); if (QFileInfo(path).isDir() /*&& path.startsWith("home")*/) {
if (QFileInfo(wholePath).isDir() /*&& path.startsWith("home")*/) { appendBlockDirToList(path);
appendBlockDirToList(wholePath);
} }
} }
} }