增加搜索错误信号,通知调用者搜索异常的信息

This commit is contained in:
hewenfei 2022-04-26 10:43:02 +08:00
parent 338a090081
commit d5ddceee51
9 changed files with 36 additions and 10 deletions

View File

@ -21,6 +21,7 @@ public:
virtual void stop() = 0; virtual void stop() = 0;
Q_SIGNALS: Q_SIGNALS:
void searchFinished(size_t searchId); void searchFinished(size_t searchId);
void searchError(size_t searchId, QString msg = {});
}; };
} }
Q_DECLARE_INTERFACE(UkuiSearch::SearchTaskPluginIface, SearchTaskPluginIface_iid) Q_DECLARE_INTERFACE(UkuiSearch::SearchTaskPluginIface, SearchTaskPluginIface_iid)

View File

@ -39,6 +39,7 @@ bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin)
} }
m_loadedPlugin.insert(plugin->getCustomSearchType(), plugin); m_loadedPlugin.insert(plugin->getCustomSearchType(), plugin);
connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished); connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished);
connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError);
return true; return true;
} }
@ -46,6 +47,7 @@ bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugi
{ {
m_buildinPlugin.insert(static_cast<size_t>(plugin->getSearchType()), plugin); m_buildinPlugin.insert(static_cast<size_t>(plugin->getSearchType()), plugin);
connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished); connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished);
connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError);
return true; return true;
} }
@ -59,11 +61,13 @@ void SearchTaskPluginManager::pluginSearch(SearchType searchType, std::shared_pt
qDebug() << "search type" << type; qDebug() << "search type" << type;
if(m_buildinPlugin.contains(type)) { if(m_buildinPlugin.contains(type)) {
if(!m_buildinPlugin.value(type)->isEnable()) { if(!m_buildinPlugin.value(type)->isEnable()) {
Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId()); Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(type));
return; return;
} }
qDebug() << "start search"; qDebug() << "start search";
m_buildinPlugin.value(type)->startSearch(searchController); m_buildinPlugin.value(type)->startSearch(searchController);
} else {
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(type));
} }
} }
@ -71,9 +75,11 @@ void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared
{ {
if(m_loadedPlugin.contains(customSearchType)) { if(m_loadedPlugin.contains(customSearchType)) {
if(!m_loadedPlugin.value(customSearchType)->isEnable()) { if(!m_loadedPlugin.value(customSearchType)->isEnable()) {
Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId()); Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(customSearchType));
return; return;
} }
m_loadedPlugin.value(customSearchType)->startSearch(searchController); m_loadedPlugin.value(customSearchType)->startSearch(searchController);
} else {
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(customSearchType));
} }
} }

View File

@ -17,7 +17,8 @@ public:
void pluginSearch(QString customSearchType, std::shared_ptr<SearchController> searchController); void pluginSearch(QString customSearchType, std::shared_ptr<SearchController> searchController);
Q_SIGNALS: Q_SIGNALS:
void searchFinished(size_t searchId); void searchFinished(size_t searchId);
void pluginDisable(size_t searchId); void searchError(size_t searchId, QString msg);
private: private:
explicit SearchTaskPluginManager(QObject *parent = nullptr); explicit SearchTaskPluginManager(QObject *parent = nullptr);

View File

@ -81,11 +81,6 @@ void FileContentSearchTask::stop()
} }
void FileContentSearchTask::sendFinishSignal(size_t searchId)
{
Q_EMIT sendFinishSignal(searchId);
}
FileContentSearchWorker::FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, std::shared_ptr<SearchController> searchController) FileContentSearchWorker::FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, std::shared_ptr<SearchController> searchController)
{ {
m_fileContentSearchTask = fileContentSearchTask; m_fileContentSearchTask = fileContentSearchTask;
@ -114,11 +109,21 @@ void FileContentSearchWorker::run()
} else { } else {
qWarning() << "content index incomplete"; qWarning() << "content index incomplete";
sendErrorMsg(QObject::tr("Content index incomplete."));
finished = false;
} }
if (finished) QMetaObject::invokeMethod(m_fileContentSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId)); if (finished) QMetaObject::invokeMethod(m_fileContentSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId));
} }
void FileContentSearchWorker::sendErrorMsg(const QString &msg)
{
QMetaObject::invokeMethod(m_fileContentSearchTask, "searchError",
Q_ARG(size_t, m_currentSearchId),
Q_ARG(QString, msg));
}
bool FileContentSearchWorker::execSearch() bool FileContentSearchWorker::execSearch()
{ {
try { try {
@ -150,6 +155,7 @@ bool FileContentSearchWorker::execSearch()
} catch (const Xapian::Error &e) { } catch (const Xapian::Error &e) {
qWarning() << QString::fromStdString(e.get_description()); qWarning() << QString::fromStdString(e.get_description());
sendErrorMsg("Xapian Error: " + QString::fromStdString(e.get_description()));
return false; return false;
} }
} }

View File

@ -36,8 +36,6 @@ public:
void startSearch(std::shared_ptr<SearchController> searchController) override; void startSearch(std::shared_ptr<SearchController> searchController) override;
void stop() override; void stop() override;
Q_INVOKABLE void sendFinishSignal(size_t searchId);
private: private:
QThreadPool *m_pool = nullptr; QThreadPool *m_pool = nullptr;
}; };
@ -55,6 +53,8 @@ private:
bool execSearch(); bool execSearch();
inline Xapian::Query createQuery(); inline Xapian::Query createQuery();
void sendErrorMsg(const QString &msg);
private: private:
FileContentSearchTask *m_fileContentSearchTask = nullptr; FileContentSearchTask *m_fileContentSearchTask = nullptr;
std::shared_ptr<SearchController> m_searchController; std::shared_ptr<SearchController> m_searchController;

View File

@ -144,11 +144,20 @@ bool FileSearchWorker::searchWithIndex()
} catch(const Xapian::Error &e) { } catch(const Xapian::Error &e) {
qWarning() << QString::fromStdString(e.get_description()); qWarning() << QString::fromStdString(e.get_description());
sendErrorMsg("Xapian Error: " + QString::fromStdString(e.get_description()));
return false;
} }
return true; return true;
} }
void FileSearchWorker::sendErrorMsg(const QString &msg)
{
QMetaObject::invokeMethod(m_FileSearchTask, "searchError",
Q_ARG(size_t, m_currentSearchId),
Q_ARG(QString, msg));
}
bool FileSearchWorker::directSearch() bool FileSearchWorker::directSearch()
{ {
unsigned int maxResults = m_searchController->maxResults(); unsigned int maxResults = m_searchController->maxResults();

View File

@ -57,6 +57,7 @@ private:
//同上 //同上
bool directSearch(); bool directSearch();
Xapian::Query creatQueryForFileSearch(); Xapian::Query creatQueryForFileSearch();
void sendErrorMsg(const QString &msg);
private: private:
FileSearchTask *m_FileSearchTask; FileSearchTask *m_FileSearchTask;

View File

@ -9,6 +9,7 @@ UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent)
{ {
m_searchCotroller = std::shared_ptr<SearchController>(new SearchController()); m_searchCotroller = std::shared_ptr<SearchController>(new SearchController());
connect(SearchTaskPluginManager::getInstance(), &SearchTaskPluginManager::searchFinished, this, &UkuiSearchTaskPrivate::searchFinished); connect(SearchTaskPluginManager::getInstance(), &SearchTaskPluginManager::searchFinished, this, &UkuiSearchTaskPrivate::searchFinished);
connect(SearchTaskPluginManager::getInstance(), &SearchTaskPluginManager::searchError, q, &UkuiSearchTask::searchError);
} }
UkuiSearchTaskPrivate::~UkuiSearchTaskPrivate() UkuiSearchTaskPrivate::~UkuiSearchTaskPrivate()

View File

@ -31,6 +31,7 @@ public:
Q_SIGNALS: Q_SIGNALS:
void searchFinished(size_t searchId); void searchFinished(size_t searchId);
void searchError(size_t searchId, QString msg);
private: private:
UkuiSearchTaskPrivate* d = nullptr; UkuiSearchTaskPrivate* d = nullptr;