diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index 6851eda..b91042a 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -36,6 +36,7 @@ public: QStringList m_searchDirs; QStringList m_FileLabels; bool m_recurse = true; + bool m_matchAllIfNoKeyword = false; bool m_activeKeywordSegmentation = false; bool m_onlySearchFile = false; bool m_onlySearchDir = false; @@ -127,6 +128,11 @@ void SearchController::addKeyword(const QString &keyword) d->m_keywords.append(keyword); } +void SearchController::setMatchAllIfNoKeyword(bool matchAll) +{ + d->m_matchAllIfNoKeyword = matchAll; +} + size_t SearchController::getCurrentSearchId() { d->m_searchIdMutex.lock(); @@ -208,6 +214,11 @@ QStringList SearchController::getKeyword() return d->m_keywords; } +bool SearchController::getMatchAllIfNoKeyword() +{ + return d->m_matchAllIfNoKeyword; +} + bool SearchController::isKeywordSegmentationActived() { return d->m_activeKeywordSegmentation; diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index 64e4d37..719073f 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -47,6 +47,7 @@ public: void addSearchDir(const QString &path); void setRecurse(bool recurse = true); void addKeyword(const QString &keyword); + void setMatchAllIfNoKeyword(bool matchAll); void setActiveKeywordSegmentation(bool active); void addFileLabel(const QString &label); void setOnlySearchFile(bool onlySearchFile); @@ -68,6 +69,7 @@ public: QStringList getSearchDir(); bool isRecurse(); QStringList getKeyword(); + bool getMatchAllIfNoKeyword(); bool isKeywordSegmentationActived(); QStringList getFileLabel(); bool isSearchFileOnly(); diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index a17e11f..af8bfdf 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -176,20 +176,24 @@ Xapian::Query FileSearchWorker::creatQueryForFileSearch() { } std::vector queries; - for (QString &keyword : m_searchController->getKeyword()) { - if (!keyword.isEmpty()) { - std::vector queryOfKeyword; - QString inputKeyWord = keyword.toLower(); - QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower()); - int start = 0; - for(; bf.position() != -1; bf.toNextBoundary()) { - int end = bf.position(); - if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { - queryOfKeyword.emplace_back(QUrl::toPercentEncoding(inputKeyWord.toLower().mid(start, end - start)).toStdString()); + if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) { + queries.emplace_back(Xapian::Query::MatchAll); + } else { + for (QString &keyword : m_searchController->getKeyword()) { + if (!keyword.isEmpty()) { + std::vector queryOfKeyword; + QString inputKeyWord = keyword.toLower(); + QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower()); + int start = 0; + for(; bf.position() != -1; bf.toNextBoundary()) { + int end = bf.position(); + if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { + queryOfKeyword.emplace_back(QUrl::toPercentEncoding(inputKeyWord.toLower().mid(start, end - start)).toStdString()); + } + start = end; } - start = end; + queries.emplace_back(Xapian::Query::OP_PHRASE, queryOfKeyword.begin(), queryOfKeyword.end()); } - queries.emplace_back(Xapian::Query::OP_PHRASE, queryOfKeyword.begin(), queryOfKeyword.end()); } } @@ -303,13 +307,17 @@ bool FileSearchWorker::directSearch() } bool matched = false; - //同时包含几个key为成功匹配 - for (const QString &keyword: m_searchController->getKeyword()) { - if (!keyword.isEmpty()) { - //TODO 修改匹配方式,对结果进行排序 - if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) { - matched = true; - break; + if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) { + matched = true; + } else { + //同时包含几个key为成功匹配 + for (const QString &keyword: m_searchController->getKeyword()) { + if (!keyword.isEmpty()) { + //TODO 修改匹配方式,对结果进行排序 + if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) { + matched = true; + break; + } } } } diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index de15d97..642461e 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -63,6 +63,11 @@ void UkuiSearchTask::addKeyword(const QString &keyword) d->m_searchCotroller.addKeyword(keyword); } +void UkuiSearchTask::setMatchAllIfNoKeyword(bool matchAll) +{ + d->m_searchCotroller.setMatchAllIfNoKeyword(matchAll); +} + void UkuiSearchTask::addFileLabel(const QString &label) { d->m_searchCotroller.addFileLabel(label); diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index b45eb42..bcbeb29 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -35,6 +35,12 @@ public: void addSearchDir(const QString &path); void setRecurse(bool recurse = true); void addKeyword(const QString &keyword); + /** + * 是否在未添加关键词的情况下进行所有文件名匹配 + * @brief setMatchAllIfNoKeyword + * @param matchAll + */ + void setMatchAllIfNoKeyword(bool matchAll); void addFileLabel(const QString &label); void setOnlySearchFile(bool onlySearchFile); void setOnlySearchDir(bool onlySearchDir);