搜索接口增加搜索结果通知机制和状态查询功能

This commit is contained in:
iaom 2023-05-19 15:15:28 +08:00
parent ed768ff48f
commit 255518c245
15 changed files with 122 additions and 91 deletions

View File

@ -40,9 +40,11 @@ public:
//Asynchronous,multithread.
virtual void startSearch() = 0;
virtual void stop() = 0;
virtual bool isSearching() = 0;
Q_SIGNALS:
void searchFinished(size_t searchId);
void searchError(size_t searchId, QString msg = {});
void reachInformNum();
};
}
Q_DECLARE_INTERFACE(UkuiSearch::SearchTaskPluginIface, SearchTaskPluginIface_iid)

View File

@ -78,8 +78,8 @@ PluginManager::PluginManager(QObject *parent) : QObject(parent)
}
case PluginInterface::PluginType::SearchTaskPlugin: {
auto p = dynamic_cast<SearchTaskPluginIface*>(plugin);
SearchTaskPluginManager::getInstance()->registerPlugin(p);
SearchTaskPluginManager::getInstance()->registerPluginPath(p->getCustomSearchType(), pluginsDir.absoluteFilePath(fileName));
delete p;
break;
}
default:

View File

@ -35,78 +35,10 @@ SearchTaskPluginManager *SearchTaskPluginManager::getInstance()
return global_instance;
}
void SearchTaskPluginManager::initPlugins(SearchProperty::SearchType searchType)
{
switch (searchType) {
case SearchProperty::SearchType::File:
registerBuildinPlugin(new FileSearchTask(this));
break;
case SearchProperty::SearchType::FileContent:
registerBuildinPlugin(new FileContentSearchTask(this));
break;
case SearchProperty::SearchType::Application:
registerBuildinPlugin(new AppSearchTask(this));
default:
break;
}
}
bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin)
{
//这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。
//但异常处理我没想好怎么加。。
//by zpf
if(plugin->getCustomSearchType().isEmpty() || m_loadedPlugin.contains(plugin->getCustomSearchType())) {
qWarning() << "search task plugin load failed! plugin type:" << plugin->getCustomSearchType();
return false;
}
m_loadedPlugin.insert(plugin->getCustomSearchType(), plugin);
connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished);
connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError);
return true;
}
bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugin)
{
m_buildinPlugin.insert(static_cast<size_t>(plugin->getSearchType()), plugin);
connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished);
connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError);
return true;
}
SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(parent)
{
}
void SearchTaskPluginManager::pluginSearch(SearchProperty::SearchType searchType, std::shared_ptr<SearchController> searchController)
{
size_t type = static_cast<size_t>(searchType);
qDebug() << "search type" << type;
if(m_buildinPlugin.contains(type)) {
if(!m_buildinPlugin.value(type)->isEnable()) {
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(type));
return;
}
qDebug() << "start search";
m_buildinPlugin.value(type)->startSearch();
} else {
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(type));
}
}
void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared_ptr<SearchController> searchController)
{
if(m_loadedPlugin.contains(customSearchType)) {
if(!m_loadedPlugin.value(customSearchType)->isEnable()) {
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(customSearchType));
return;
}
m_loadedPlugin.value(customSearchType)->startSearch();
} else {
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(customSearchType));
}
}
bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType)
{
if (m_managedPlugins.contains(uuid)) {
@ -167,6 +99,26 @@ void SearchTaskPluginManager::registerPluginPath(const QString& customType, cons
}
}
bool SearchTaskPluginManager::isSearching(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType)
{
if (m_managedPlugins.contains(uuid)) {
ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid);
SearchTaskPluginIface *plugin = nullptr;
if (searchType == SearchProperty::SearchType::Custom) {
plugin = managedPlugin->externalPlugin(customType);
} else {
plugin = managedPlugin->internalPlugin(searchType);
}
if (plugin) {
return plugin->isSearching();
}
}
return false;
}
SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, SearchProperty::SearchType searchType, const QString& customType)
{
if (!m_managedPlugins.contains(uuid)) {
@ -177,7 +129,6 @@ SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, S
bool succeed = false;
if (searchType == SearchProperty::SearchType::Custom) {
succeed = m_managedPlugins.value(uuid)->insertExternalPlugin(customType, plugin);
} else {
succeed = m_managedPlugins.value(uuid)->insertInternalPlugin(searchType, plugin);
}
@ -197,7 +148,6 @@ void SearchTaskPluginManager::destroyPlugins(const QUuid &uuid)
ManagedPlugin::~ManagedPlugin()
{
}
bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searchType, SearchTaskPluginIface *plugin)

View File

@ -50,19 +50,11 @@ class SearchTaskPluginManager : public QObject
Q_OBJECT
public:
static SearchTaskPluginManager *getInstance();
void initPlugins(SearchProperty::SearchType searchType);
SearchTaskPluginIface *initPlugins(const QUuid&, SearchProperty::SearchType, const QString& customType = QString());
bool registerPlugin(SearchTaskPluginIface *plugin);
bool registerBuildinPlugin(SearchTaskPluginIface *plugin);
void pluginSearch(SearchProperty::SearchType searchType, std::shared_ptr<SearchController> searchController);
void pluginSearch(QString customSearchType, std::shared_ptr<SearchController> searchController);
bool startSearch(const QUuid&, SearchProperty::SearchType, const QString& customType = QString());
void destroyPlugins(const QUuid& uuid);
void registerPluginPath(const QString& customType, const QString& pluginPath);
Q_SIGNALS:
void searchFinished(size_t searchId);
void searchError(size_t searchId, QString msg);
bool isSearching(const QUuid &uuid, SearchProperty::SearchType searchType, const QString& customType = QString());
private:
explicit SearchTaskPluginManager(QObject *parent = nullptr);

View File

@ -53,8 +53,10 @@ public:
* @param maxResults
*/
void setMaxResultNum(unsigned int maxResults);
void setInformNum(int num = 0);
unsigned int maxResults() const;
int informNum() const;
private:
void copyData();
@ -74,6 +76,7 @@ private:
bool m_onlySearchDir = false;
bool m_searchOnlineApps = false;
unsigned int m_maxResults = 100; //默认取100条结果
int m_informNum = 0;
QMap<SearchProperty::SearchType, SearchResultProperties> m_searchType2ResultProperties;
QMap<QString, QStringList> m_customSearchType2ResultDataType;

View File

@ -184,6 +184,7 @@ void SearchControllerPrivate::copyData()
m_recurse = m_formerController->d->m_recurse;
m_activeKeywordSegmentation = m_formerController->d->m_activeKeywordSegmentation;
m_maxResults = m_formerController->d->m_maxResults;
m_informNum = m_formerController->d->m_informNum;
m_searchOnlineApps = m_formerController->d->m_searchOnlineApps;
m_searchType2ResultProperties = m_formerController->d->m_searchType2ResultProperties;
m_customSearchType2ResultDataType = m_formerController->d->m_customSearchType2ResultDataType;
@ -229,11 +230,23 @@ void SearchControllerPrivate::setMaxResultNum(unsigned int maxResults)
m_maxResults = maxResults;
}
void SearchControllerPrivate::setInformNum(int num)
{
if(num >= 0) {
m_informNum = num;
}
}
unsigned int SearchControllerPrivate::maxResults() const
{
return m_maxResults;
}
int SearchControllerPrivate::informNum() const
{
return m_informNum;
}
SearchController::SearchController(std::shared_ptr<SearchController> parent) : m_parent(parent), d(new SearchControllerPrivate(this))
{
}
@ -401,11 +414,21 @@ void SearchController::setMaxResultNum(unsigned int maxResults)
d->setMaxResultNum(maxResults);
}
void SearchController::setInformNum(int num)
{
d->setInformNum(num);
}
unsigned int SearchController::maxResults() const
{
return d->maxResults();
}
int SearchController::informNum() const
{
return d->informNum();
}
bool SearchController::setResultProperties(SearchProperty::SearchType searchType, SearchResultProperties searchResultProperties)
{
return d->setResultProperties(searchType, searchResultProperties);

View File

@ -34,6 +34,8 @@ public:
void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir);
void setSearchOnlineApps(bool searchOnlineApps);
void setMaxResultNum(unsigned int maxResults);
void setInformNum(int num = 0);
//以上方法插件请不要调用
//以下方法插件可以调用
@ -57,9 +59,8 @@ public:
void clearSearchDir();
void clearFileLabel();
void setMaxResultNum(unsigned int maxResults);
unsigned int first() const;
unsigned int maxResults() const;
int informNum() const;
bool setResultProperties(SearchProperty::SearchType searchType, UkuiSearch::SearchResultProperties searchResultProperties);
void setCustomResultDataType(QString customSearchType, QStringList dataType);

View File

@ -72,9 +72,9 @@ void AppSearchTask::stop()
{
}
void AppSearchTask::sendFinishSignal(size_t searchId)
bool AppSearchTask::isSearching()
{
Q_EMIT searchFinished(searchId);
return m_pool->activeThreadCount() > 0;
}
AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask)
@ -113,6 +113,10 @@ void AppSearchWorker::run()
item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, oneResult.value(ApplicationProperty::Icon).toString());
}
m_controller->getDataQueue()->enqueue(item);
if(++m_resultNum == m_controller->informNum()) {
QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum");
m_resultNum = 0;
}
m_controller->finishSearchIdCheck();
} else {
qDebug() << "Search id changed!";
@ -158,6 +162,10 @@ void AppSearchWorker::run()
item.setValue(SearchProperty::SearchResultProperty::IsOnlineApplication, 1);
}
m_controller->getDataQueue()->enqueue(item);
if(++m_resultNum == m_controller->informNum()) {
QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum");
m_resultNum = 0;
}
m_controller->finishSearchIdCheck();
} else {
qDebug() << "Search id changed!";

View File

@ -51,7 +51,7 @@ public:
QString getCustomSearchType();
void startSearch();
void stop();
Q_INVOKABLE void sendFinishSignal(size_t searchId);
bool isSearching();
private:
ApplicationInfo m_appinfo;
@ -74,6 +74,7 @@ private:
SearchController* m_controller = nullptr;
AppSearchTask *m_appSearchTask = nullptr;
size_t m_currentSearchId = 0;
int m_resultNum = 0;
};
}
#endif // APPSEARCHTASK_H

View File

@ -103,6 +103,11 @@ void FileContentSearchTask::stop()
}
bool FileContentSearchTask::isSearching()
{
return m_pool->activeThreadCount() > 0;
}
FileContentSearchWorker::FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, std::shared_ptr<SearchController> searchController)
{
m_fileContentSearchTask = fileContentSearchTask;
@ -187,6 +192,10 @@ bool FileContentSearchWorker::execSearch()
QDateTime::fromString(QString::fromStdString(it.get_document().get_value(3)), "yyyyMMddHHmmsszzz"));
}
m_searchController->getDataQueue()->enqueue(resultItem);
if(++m_resultNum == m_searchController->informNum()) {
QMetaObject::invokeMethod(m_fileContentSearchTask, "reachInformNum");
m_resultNum = 0;
}
m_searchController->finishSearchIdCheck();
} else {

View File

@ -51,6 +51,7 @@ public:
SearchProperty::SearchType getSearchType() override;
void startSearch() override;
void stop() override;
bool isSearching();
private:
QThreadPool *m_pool = nullptr;
@ -77,10 +78,9 @@ private:
private:
FileContentSearchTask *m_fileContentSearchTask = nullptr;
SearchController *m_searchController = nullptr;
QStringList m_validDirectories;
size_t m_currentSearchId = 0;
int m_resultNum = 0;
};
class FileContentSearchFilter : public Xapian::MatchDecider {

View File

@ -78,12 +78,11 @@ void FileSearchTask::stop()
}
void FileSearchTask::sendFinishSignal(size_t searchId)
bool FileSearchTask::isSearching()
{
Q_EMIT searchFinished(searchId);
return m_pool->activeThreadCount() > 0;
}
FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr<SearchController> searchController) : m_FileSearchTask(fileSarchTask)
{
m_searchController = new SearchController(searchController);
@ -228,6 +227,10 @@ bool FileSearchWorker::searchWithIndex()
QDateTime::fromString(QString::fromStdString(it.get_document().get_value(2)), "yyyyMMddHHmmsszzz"));
}
m_searchController->getDataQueue()->enqueue(resultItem);
if(++m_resultNum == m_searchController->informNum()) {
QMetaObject::invokeMethod(m_FileSearchTask, "reachInformNum");
m_resultNum = 0;
}
m_searchController->finishSearchIdCheck();
} else {
@ -329,6 +332,10 @@ bool FileSearchWorker::directSearch()
ri.setValue(SearchProperty::SearchResultProperty::ModifiedTime, fileInfo.lastModified());
}
m_searchController->getDataQueue()->enqueue(ri);
if(++m_resultNum == m_searchController->informNum()) {
QMetaObject::invokeMethod(m_FileSearchTask, "reachInformNum");
m_resultNum = 0;
}
--maxResults;
}
m_searchController->finishSearchIdCheck();

View File

@ -49,7 +49,7 @@ public:
QString getCustomSearchType();
void startSearch();
void stop();
Q_INVOKABLE void sendFinishSignal(size_t searchId);
bool isSearching();
private:
QThreadPool *m_pool = nullptr;
@ -85,6 +85,7 @@ private:
SearchController *m_searchController = nullptr;
size_t m_currentSearchId = 0;
int m_resultNum = 0;
QStringList m_validDirectories;
QStringList m_blackList;
QStringList m_labels;

View File

@ -73,6 +73,7 @@ void UkuiSearchTask::initSearchPlugin(SearchProperty::SearchType searchType, con
plugin->setController(d->m_searchCotroller);
connect(plugin, &SearchTaskPluginIface::searchFinished,this, &UkuiSearchTask::searchFinished);
connect(plugin, &SearchTaskPluginIface::searchError,this, &UkuiSearchTask::searchError);
connect(plugin, &SearchTaskPluginIface::reachInformNum,this, &UkuiSearchTask::reachInformNum);
} else {
qWarning() << "The plugin has been initialized or the plugin failed to load.";
}
@ -109,6 +110,11 @@ void UkuiSearchTask::stop()
d->m_searchCotroller->stop();
}
bool UkuiSearchTask::isSearching(SearchProperty::SearchType searchtype, QString customSearchType)
{
return SearchTaskPluginManager::getInstance()->isSearching(d->m_uuid, searchtype, customSearchType);
}
void UkuiSearchTask::clearAllConditions()
{
d->m_searchCotroller->clearAllConditions();
@ -133,3 +139,8 @@ void UkuiSearchTask::setMaxResultNum(unsigned int maxResults)
{
d->m_searchCotroller->setMaxResultNum(maxResults);
}
void UkuiSearchTask::setInformNum(int num)
{
d->m_searchCotroller->setInformNum(num);
}

View File

@ -58,14 +58,37 @@ public:
void clearKeyWords();
void clearSearchDir();
void clearFileLabel();
/**
* @brief setMaxResultNum
* @param maxResults
*/
void setMaxResultNum(unsigned int maxResults = 99999999);
/**
* @brief setInformNum
* @param num
*/
void setInformNum(int num);
/**
* @brief startSearch
* @param searchtype
* @param customSearchType searchType为Custom时可用
* @return
*/
size_t startSearch(SearchProperty::SearchType searchtype, QString customSearchType = QString());
/**
* @brief stop
*/
void stop();
/**
* @brief isSearching
*/
bool isSearching(SearchProperty::SearchType searchtype, QString customSearchType = {});
Q_SIGNALS:
void searchFinished(size_t searchId);
void searchError(size_t searchId, QString msg);
void reachInformNum();
private:
UkuiSearchTaskPrivate* d = nullptr;