ukui-search/libsearch/searchinterface/search-controller.cpp

283 lines
6.0 KiB
C++
Raw Normal View History

2021-12-28 15:56:41 +08:00
#include "search-controller.h"
#include <QMutex>
#include <QMap>
2021-12-28 15:56:41 +08:00
#include <QDebug>
#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;
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
void SearchControllerPrivate::clearAllConditions()
{
m_keywords.clear();
m_searchDirs.clear();
m_FileLabels.clear();
m_recurse = true;
m_activeKeywordSegmentation = false;
m_onlySearchFile = false;
m_onlySearchDir = false;
m_searchOnlineApps = false;
}
SearchController::SearchController(): d(new SearchControllerPrivate)
{
}
SearchController::SearchController(const SearchController &other):d(other.d)
{
}
SearchController &SearchController::operator =(const SearchController &other)
{
d = other.d;
return *this;
}
SearchController &SearchController::operator=(SearchController &&other) Q_DECL_NOEXCEPT
2021-12-28 15:56:41 +08:00
{
d = other.d;
other.d.reset();
return *this;
2021-12-28 15:56:41 +08:00
}
SearchController::~SearchController()
{
d.reset();
2021-12-28 15:56:41 +08:00
}
DataQueue<ResultItem> *SearchController::refreshDataqueue()
{
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()
{
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()
{
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
}
void SearchController::addSearchDir(const QString &path)
2021-12-28 15:56:41 +08:00
{
d->m_searchDirs.append(path);
2021-12-28 15:56:41 +08:00
}
void SearchController::setRecurse(bool recurse)
{
d->m_recurse = recurse;;
2021-12-28 15:56:41 +08:00
}
void SearchController::addKeyword(const QString &keyword)
2021-12-28 15:56:41 +08:00
{
d->m_keywords.append(keyword);
2021-12-28 15:56:41 +08:00
}
size_t SearchController::getCurrentSearchId()
{
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()
{
return d->m_sharedDataQueue.get();
2021-12-28 15:56:41 +08:00
}
SearchResultProperties SearchController::getResultProperties(SearchProperty::SearchType searchType)
{
return d->m_searchType2ResultProperties[searchType];;
}
QStringList SearchController::getCustomResultDataType(QString customSearchType)
{
return d->m_customSearchType2ResultDataType[customSearchType];
}
2021-12-28 15:56:41 +08:00
void SearchController::setActiveKeywordSegmentation(bool active)
{
d->m_activeKeywordSegmentation = active;;
2021-12-28 15:56:41 +08:00
}
void SearchController::addFileLabel(const QString &label)
2021-12-28 15:56:41 +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)
{
d->m_onlySearchFile = onlySearchFile;
2022-01-11 16:20:40 +08:00
}
void SearchController::setOnlySearchDir(bool onlySearchDir)
{
d->m_onlySearchDir = onlySearchDir;
2022-01-11 16:20:40 +08:00
}
void SearchController::setSearchOnlineApps(bool searchOnlineApps)
{
d->m_searchOnlineApps = searchOnlineApps;
}
void SearchController::setSearchHiddenFiles(bool searchHiddenFiles)
{
d->m_searchHiddenFiles = searchHiddenFiles;
}
2021-12-28 15:56:41 +08:00
bool SearchController::beginSearchIdCheck(size_t searchId)
{
d->m_searchIdMutex.lock();
return d->m_searchId == searchId;
2021-12-28 15:56:41 +08:00
}
void SearchController::finishSearchIdCheck()
{
d->m_searchIdMutex.unlock();
return;
2022-01-11 16:20:40 +08:00
}
QStringList SearchController::getSearchDir()
{
return d->m_searchDirs;
2022-01-11 16:20:40 +08:00
}
bool SearchController::isRecurse()
{
return d->m_recurse;
2022-01-11 16:20:40 +08:00
}
QStringList SearchController::getKeyword()
{
return d->m_keywords;
2022-01-11 16:20:40 +08:00
}
bool SearchController::isKeywordSegmentationActived()
{
return d->m_activeKeywordSegmentation;
2022-01-11 16:20:40 +08:00
}
QStringList SearchController::getFileLabel()
{
return d->m_FileLabels;
2022-01-11 16:20:40 +08:00
}
bool SearchController::isSearchFileOnly()
{
return d->m_onlySearchFile;
2022-01-11 16:20:40 +08:00
}
bool SearchController::isSearchDirOnly()
{
return d->m_onlySearchDir;
2021-12-28 15:56:41 +08:00
}
bool SearchController::isSearchOnlineApps()
{
return d->m_searchOnlineApps;
}
bool SearchController::searchHiddenFiles()
{
return d->m_searchHiddenFiles;
}
2021-12-28 15:56:41 +08:00
void SearchController::stop()
{
d->m_searchIdMutex.lock();
d->m_searchId += 1;
d->m_searchIdMutex.unlock();
2021-12-28 15:56:41 +08:00
}
void SearchController::clearAllConditions()
{
d->clearAllConditions();
}
void SearchController::clearKeyWords()
{
d->m_keywords.clear();
}
void SearchController::clearSearchDir()
{
d->m_searchDirs.clear();
}
void SearchController::clearFileLabel()
{
d->m_FileLabels.clear();
}
void SearchController::setMaxResultNum(unsigned int maxResults)
{
d->m_maxResults = maxResults;
}
void SearchController::setInformNum(int num)
{
if(num >= 0) {
d->m_informNum = num;
}
}
unsigned int SearchController::maxResults() const
{
return d->m_maxResults;
}
int SearchController::informNum() const
{
return d->m_informNum;
}
bool SearchController::setResultProperties(SearchProperty::SearchType searchType, SearchResultProperties searchResultProperties)
{
d->m_searchType2ResultProperties[searchType] = searchResultProperties;
return true;
}
void SearchController::setCustomResultDataType(QString customSearchType, QStringList dataType)
{
d->m_customSearchType2ResultDataType[customSearchType] = dataType;
}
}