2024-01-30 14:35:04 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* * Authors: iaom <zhangpengfei@kylinos.cn>
|
|
|
|
*/
|
2021-12-28 15:56:41 +08:00
|
|
|
#include "search-controller.h"
|
2023-07-06 15:27:23 +08:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QMap>
|
2021-12-28 15:56:41 +08:00
|
|
|
#include <QDebug>
|
2023-07-06 15:27:23 +08:00
|
|
|
#include "search-result-property.h"
|
|
|
|
#include "ukui-search-task.h"
|
|
|
|
namespace UkuiSearch {
|
|
|
|
|
|
|
|
class SearchControllerPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void clearAllConditions();
|
|
|
|
|
|
|
|
std::shared_ptr<DataQueue<ResultItem>> m_sharedDataQueue = nullptr;
|
|
|
|
size_t m_searchId = 0;
|
|
|
|
QMutex m_searchIdMutex;
|
|
|
|
QStringList m_keywords;
|
|
|
|
QStringList m_searchDirs;
|
|
|
|
QStringList m_FileLabels;
|
|
|
|
bool m_recurse = true;
|
2024-04-25 10:59:18 +08:00
|
|
|
bool m_matchAllIfNoKeyword = false;
|
2023-07-06 15:27:23 +08:00
|
|
|
bool m_activeKeywordSegmentation = false;
|
|
|
|
bool m_onlySearchFile = false;
|
|
|
|
bool m_onlySearchDir = false;
|
|
|
|
bool m_searchOnlineApps = false;
|
|
|
|
bool m_searchHiddenFiles = false;
|
|
|
|
unsigned int m_maxResults = 100; //默认取100条结果
|
|
|
|
int m_informNum = 0;
|
|
|
|
QMap<SearchProperty::SearchType, SearchResultProperties> m_searchType2ResultProperties;
|
|
|
|
QMap<QString, QStringList> m_customSearchType2ResultDataType;
|
|
|
|
};
|
2021-12-28 15:56:41 +08:00
|
|
|
|
2022-03-25 09:40:56 +08:00
|
|
|
void SearchControllerPrivate::clearAllConditions()
|
|
|
|
{
|
|
|
|
m_keywords.clear();
|
|
|
|
m_searchDirs.clear();
|
2022-04-28 15:49:51 +08:00
|
|
|
m_FileLabels.clear();
|
2023-07-06 15:27:23 +08:00
|
|
|
m_recurse = true;
|
|
|
|
m_activeKeywordSegmentation = false;
|
|
|
|
m_onlySearchFile = false;
|
|
|
|
m_onlySearchDir = false;
|
|
|
|
m_searchOnlineApps = false;
|
2024-08-19 10:57:18 +08:00
|
|
|
m_matchAllIfNoKeyword = false;
|
2022-04-28 15:49:51 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
SearchController::SearchController(): d(new SearchControllerPrivate)
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
SearchController::SearchController(const SearchController &other):d(other.d)
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
SearchController &SearchController::operator =(const SearchController &other)
|
2022-03-25 09:40:56 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d = other.d;
|
|
|
|
return *this;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
2023-07-06 15:27:23 +08:00
|
|
|
SearchController &SearchController::operator=(SearchController &&other) Q_DECL_NOEXCEPT
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d = other.d;
|
|
|
|
other.d.reset();
|
|
|
|
return *this;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SearchController::~SearchController()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d.reset();
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DataQueue<ResultItem> *SearchController::refreshDataqueue()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
if(!d->m_sharedDataQueue.get()) {
|
|
|
|
// m_dataQueue = new DataQueue<ResultItem>;
|
|
|
|
d->m_sharedDataQueue = std::make_shared<DataQueue<ResultItem>>();
|
|
|
|
return d->m_sharedDataQueue.get();
|
|
|
|
}
|
|
|
|
d->m_sharedDataQueue.get()->clear();
|
|
|
|
return d->m_sharedDataQueue.get();
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t SearchController::refreshSearchId()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchIdMutex.lock();
|
|
|
|
d->m_searchId += 1;
|
|
|
|
d->m_searchIdMutex.unlock();
|
|
|
|
return d->m_searchId;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DataQueue<ResultItem> *SearchController::initDataQueue()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
if(!d->m_sharedDataQueue.get()) {
|
|
|
|
d->m_sharedDataQueue = std::make_shared<DataQueue<ResultItem>>();
|
|
|
|
return d->m_sharedDataQueue.get();
|
|
|
|
}
|
|
|
|
return d->m_sharedDataQueue.get();
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 17:33:08 +08:00
|
|
|
void SearchController::addSearchDir(const QString &path)
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchDirs.append(path);
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::setRecurse(bool recurse)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_recurse = recurse;;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 17:33:08 +08:00
|
|
|
void SearchController::addKeyword(const QString &keyword)
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_keywords.append(keyword);
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2024-04-25 10:59:18 +08:00
|
|
|
void SearchController::setMatchAllIfNoKeyword(bool matchAll)
|
|
|
|
{
|
|
|
|
d->m_matchAllIfNoKeyword = matchAll;
|
|
|
|
}
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
size_t SearchController::getCurrentSearchId()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchIdMutex.lock();
|
|
|
|
size_t searchId = d->m_searchId;
|
|
|
|
d->m_searchIdMutex.unlock();
|
|
|
|
|
|
|
|
return searchId;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DataQueue<ResultItem> *SearchController::getDataQueue()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_sharedDataQueue.get();
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
SearchResultProperties SearchController::getResultProperties(SearchProperty::SearchType searchType)
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_searchType2ResultProperties[searchType];;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SearchController::getCustomResultDataType(QString customSearchType)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_customSearchType2ResultDataType[customSearchType];
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
void SearchController::setActiveKeywordSegmentation(bool active)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_activeKeywordSegmentation = active;;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 17:33:08 +08:00
|
|
|
void SearchController::addFileLabel(const QString &label)
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_FileLabels.append(label);;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2022-01-11 16:20:40 +08:00
|
|
|
void SearchController::setOnlySearchFile(bool onlySearchFile)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_onlySearchFile = onlySearchFile;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::setOnlySearchDir(bool onlySearchDir)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_onlySearchDir = onlySearchDir;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 13:38:47 +08:00
|
|
|
void SearchController::setSearchOnlineApps(bool searchOnlineApps)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchOnlineApps = searchOnlineApps;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::setSearchHiddenFiles(bool searchHiddenFiles)
|
|
|
|
{
|
|
|
|
d->m_searchHiddenFiles = searchHiddenFiles;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
bool SearchController::beginSearchIdCheck(size_t searchId)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchIdMutex.lock();
|
|
|
|
return d->m_searchId == searchId;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::finishSearchIdCheck()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchIdMutex.unlock();
|
|
|
|
return;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SearchController::getSearchDir()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_searchDirs;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchController::isRecurse()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_recurse;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SearchController::getKeyword()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_keywords;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
2024-04-25 10:59:18 +08:00
|
|
|
bool SearchController::getMatchAllIfNoKeyword()
|
|
|
|
{
|
|
|
|
return d->m_matchAllIfNoKeyword;
|
|
|
|
}
|
|
|
|
|
2022-01-11 16:20:40 +08:00
|
|
|
bool SearchController::isKeywordSegmentationActived()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_activeKeywordSegmentation;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SearchController::getFileLabel()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_FileLabels;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchController::isSearchFileOnly()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_onlySearchFile;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchController::isSearchDirOnly()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_onlySearchDir;
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 13:38:47 +08:00
|
|
|
bool SearchController::isSearchOnlineApps()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_searchOnlineApps;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchController::searchHiddenFiles()
|
|
|
|
{
|
|
|
|
return d->m_searchHiddenFiles;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
void SearchController::stop()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchIdMutex.lock();
|
|
|
|
d->m_searchId += 1;
|
|
|
|
d->m_searchIdMutex.unlock();
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
void SearchController::clearAllConditions()
|
|
|
|
{
|
|
|
|
d->clearAllConditions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::clearKeyWords()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_keywords.clear();
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::clearSearchDir()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchDirs.clear();
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
void SearchController::clearFileLabel()
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_FileLabels.clear();
|
2022-04-28 15:49:51 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
void SearchController::setMaxResultNum(unsigned int maxResults)
|
2022-03-25 09:40:56 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_maxResults = maxResults;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
2023-05-19 15:15:28 +08:00
|
|
|
void SearchController::setInformNum(int num)
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
if(num >= 0) {
|
|
|
|
d->m_informNum = num;
|
|
|
|
}
|
2023-05-19 15:15:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 09:40:56 +08:00
|
|
|
unsigned int SearchController::maxResults() const
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_maxResults;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
2022-06-13 13:38:47 +08:00
|
|
|
|
2023-05-19 15:15:28 +08:00
|
|
|
int SearchController::informNum() const
|
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
return d->m_informNum;
|
2023-05-19 15:15:28 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
bool SearchController::setResultProperties(SearchProperty::SearchType searchType, SearchResultProperties searchResultProperties)
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_searchType2ResultProperties[searchType] = searchResultProperties;
|
|
|
|
return true;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
void SearchController::setCustomResultDataType(QString customSearchType, QStringList dataType)
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
d->m_customSearchType2ResultDataType[customSearchType] = dataType;
|
|
|
|
}
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|