[FIX] Block dir won't take effect issue;

Remove '\n' when get text content.
This commit is contained in:
zhangpengfei 2021-01-12 16:07:50 +08:00
parent 7fcf2b1a9b
commit 6158b46487
8 changed files with 31 additions and 27 deletions

View File

@ -472,22 +472,21 @@ QStringList FileUtils::findMultiToneWords(const QString& hanzi)
* @param path: abs path
* @return docx to QString
*/
QString *FileUtils::getDocxTextContent(QString &path)
void FileUtils::getDocxTextContent(QString &path,QString &textcontent)
{
QFileInfo info = QFileInfo(path);
if(!info.exists()||info.isDir())
return nullptr;
return;
QuaZip file(path);
if(!file.open(QuaZip::mdUnzip))
return nullptr;
return;
if(!file.setCurrentFile("word/document.xml",QuaZip::csSensitive))
return nullptr;
return;
QuaZipFile fileR(&file);
fileR.open(QIODevice::ReadOnly); //读取方式打开
QString *allText = new QString();
QDomDocument doc;
doc.setContent(fileR.readAll());
QDomElement first = doc.firstChildElement("w:document");
@ -498,19 +497,19 @@ QString *FileUtils::getDocxTextContent(QString &path)
while(!wr.isNull())
{
QDomElement wt = wr.firstChildElement("w:t");
allText->append(wt.text());
textcontent.append(wt.text().replace("\n",""));
wr = wr.nextSiblingElement();
}
first = first.nextSiblingElement();
}
return allText;
return;
}
QString *FileUtils::getTxtContent(QString &path)
void FileUtils::getTxtContent(QString &path, QString &textcontent)
{
QFile file(path);
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
return nullptr;
QString *allText = new QString(file.readAll());
return allText;
return;
textcontent = QString(file.readAll()).replace("\n","");
return;
}

View File

@ -27,8 +27,8 @@ public:
//parse text,docx.....
static QString getMimetype(QString &path, bool getsuffix = false);
static QString *getDocxTextContent(QString &path);
static QString *getTxtContent(QString &path);
static void getDocxTextContent(QString &path, QString &textcontent);
static void getTxtContent(QString &path, QString &textcontent);
static size_t _max_index_count;
static size_t _current_index_count;

View File

@ -17,6 +17,7 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
{
m_settings = new QSettings("org.ukui", "ukui-search", this);
m_block_dirs_settings = new QSettings("org.ukui","ukui-search-block-dirs",this);
m_block_dirs_settings->setIniCodec(QTextCodec::codecForName("UTF-8"));
this->forceSync();
//the default number of transparency in mainwindow is 0.7
//if someone changes the num in mainwindow, here should be modified too
@ -82,13 +83,15 @@ void GlobalSettings::resetAll()
bool GlobalSettings::setBlockDirs(const QString &path, QString &returnMessage, bool remove)
{
//why QSetting's key can't start with "/"??
QString pathKey = path.right(path.length()-1);
if(remove)
{
m_block_dirs_settings->remove(pathKey);
m_block_dirs_settings->remove(path);
return true;
}
//why QSetting's key can't start with "/"??
QString pathKey = path.right(path.length()-1);
QStringList blockDirs = m_block_dirs_settings->allKeys();
for(QString i:blockDirs)
{

View File

@ -6,15 +6,15 @@ FileReader::FileReader(QObject *parent) : QObject(parent)
}
QString *FileReader::getTextContent(QString path)
void FileReader::getTextContent(QString path, QString &textContent)
{
//获取所有文件内容
//先分类
QString type =FileUtils::getMimetype(path,true);
if(type == "application/zip")
return FileUtils::getDocxTextContent(path);
FileUtils::getDocxTextContent(path,textContent);
else if(type == "text/plain")
return FileUtils::getTxtContent(path);
FileUtils::getTxtContent(path,textContent);
return new QString();
return;
}

View File

@ -8,7 +8,7 @@ class FileReader : public QObject
Q_OBJECT
public:
explicit FileReader(QObject *parent = nullptr);
static QString* getTextContent(QString path);
static void getTextContent(QString path, QString &textContent);
};

View File

@ -354,7 +354,7 @@ bool FileSearcher::isBlocked(QString &path)
QStringList blockList = GlobalSettings::getInstance()->getBlockDirs();
for(QString i :blockList)
{
if(path.startsWith(i))
if(path.startsWith(i.prepend("/")))
return true;
}
return false;

View File

@ -79,7 +79,7 @@ void FirstIndex::run(){
//why???????????????????????????????????????????????????????????????
//why not quit?
// this->quit();
exit(0);
return;
// return;
// this->wait();
}

View File

@ -131,7 +131,7 @@ IndexGenerator::~IndexGenerator()
GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2");
qDebug() << "QThread::currentThreadId()" << QThread::currentThreadId();
qDebug() << "~IndexGenerator 22222222222222222222";
qDebug() << "~IndexGenerator end";
}
void IndexGenerator::insertIntoDatabase(Document doc)
@ -240,11 +240,12 @@ Document IndexGenerator::GenerateDocument(const QVector<QString> &list)
Document IndexGenerator::GenerateContentDocument(const QString &path)
{
// 构造文本索引的document
QString *content = FileReader::getTextContent(path);
QString content;
FileReader::getTextContent(path,content);
QString uniqueterm = QString::fromStdString(FileUtils::makeDocUterm(path));
QVector<SKeyWord> term = ChineseSegmentation::getInstance()->callSegement(content);
QVector<SKeyWord> term = ChineseSegmentation::getInstance()->callSegement(&content);
Document doc;
doc.setData(*content);
doc.setData(content);
doc.setUniqueTerm(uniqueterm);
doc.addValue(path);
for(int i = 0;i<term.size();++i)
@ -349,6 +350,7 @@ bool IndexGenerator::deleteAllIndex(QStringList *pathlist)
qDebug()<<"--delete start--";
m_datebase_path->delete_document(uniqueterm);
m_database_content->delete_document(uniqueterm);
qDebug()<<"delete path"<<doc;
qDebug()<<"delete md5"<<QString::fromStdString(uniqueterm);
m_datebase_path->commit();
qDebug()<< "--delete finish--";