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)
{
if(pathA[0] != "/")
pathA.prepend("/");
if(pathB[0] != "/")
pathB.prepend("/");
if (pathB == "/") {
return true;
}
if(pathA.length() < pathB.length())
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->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) {

View File

@ -497,11 +497,17 @@ void DirectSearch::run() {
QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir();
QQueue<QString> 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;

View File

@ -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);
}
}
}