Add file search plugin demo; Optimized ukui search iface.

This commit is contained in:
iaom 2022-01-24 09:44:42 +08:00
parent b8dd42ad4a
commit c7117369b8
14 changed files with 132 additions and 54 deletions

View File

@ -3,13 +3,13 @@
namespace UkuiSearch { namespace UkuiSearch {
enum class SearchType enum class SearchType
{ {
File = 0, File = 0x1 << 0,
FileContent, FileContent = 0x1 << 1,
Application, Application = 0x1 << 2,
Setting, Setting = 0x1 << 3,
Note, Note = 0x1 << 4,
Mail, Mail = 0x1 << 5,
Custom Custom = 0x1 << 6
}; };
} }
#endif // COMMONDEFINES_H #endif // COMMONDEFINES_H

View File

@ -17,7 +17,7 @@ public:
virtual QString getCustomSearchType() = 0; virtual QString getCustomSearchType() = 0;
virtual SearchType getSearchType() = 0; virtual SearchType getSearchType() = 0;
//Asynchronous,multithread. //Asynchronous,multithread.
virtual void startSearch(SearchController searchController) = 0; virtual void startSearch(std::shared_ptr<SearchController> searchController) = 0;
virtual void stop() = 0; virtual void stop() = 0;
Q_SIGNALS: Q_SIGNALS:
void searchFinished(size_t searchId); void searchFinished(size_t searchId);

View File

@ -1,5 +1,7 @@
#include "search-task-plugin-manager.h" #include "search-task-plugin-manager.h"
#include <QDebug> #include <QDebug>
#include "file-search-task.h"
using namespace UkuiSearch; using namespace UkuiSearch;
static SearchTaskPluginManager *global_instance = nullptr; static SearchTaskPluginManager *global_instance = nullptr;
SearchTaskPluginManager *SearchTaskPluginManager::getInstance() SearchTaskPluginManager *SearchTaskPluginManager::getInstance()
@ -10,6 +12,14 @@ SearchTaskPluginManager *SearchTaskPluginManager::getInstance()
return global_instance; return global_instance;
} }
void SearchTaskPluginManager::initPlugins(SearchType searchType)
{
size_t type = static_cast<size_t>(searchType);
if (type & static_cast<size_t>(SearchType::File)) {
registerBuildinPlugin(new FileSearchTask(this));
}
}
bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin) bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin)
{ {
//这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。 //这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。
@ -26,34 +36,34 @@ bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin)
bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugin) bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugin)
{ {
m_buildinPlugin.insert(static_cast<int>(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);
return true; return true;
} }
SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(parent) SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(parent)
{ {
//register buildin plugin into m_buildinPlugin
//registerBuildinPlugin();
} }
void SearchTaskPluginManager::pluginSearch(SearchType searchType, SearchController searchController) void SearchTaskPluginManager::pluginSearch(SearchType searchType, std::shared_ptr<SearchController> searchController)
{ {
int type = static_cast<int>(searchType); size_t type = static_cast<size_t>(searchType);
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.getCurrentSearchId()); Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId());
return; return;
} }
qDebug() << "start search";
m_buildinPlugin.value(type)->startSearch(searchController); m_buildinPlugin.value(type)->startSearch(searchController);
} }
} }
void SearchTaskPluginManager::pluginSearch(QString customSearchType, SearchController searchController) void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared_ptr<SearchController> searchController)
{ {
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.getCurrentSearchId()); Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId());
return; return;
} }
m_loadedPlugin.value(customSearchType)->startSearch(searchController); m_loadedPlugin.value(customSearchType)->startSearch(searchController);

View File

@ -10,10 +10,11 @@ class SearchTaskPluginManager : public QObject
Q_OBJECT Q_OBJECT
public: public:
static SearchTaskPluginManager *getInstance(); static SearchTaskPluginManager *getInstance();
void initPlugins(SearchType searchType);
bool registerPlugin(SearchTaskPluginIface *plugin); bool registerPlugin(SearchTaskPluginIface *plugin);
bool registerBuildinPlugin(SearchTaskPluginIface *plugin); bool registerBuildinPlugin(SearchTaskPluginIface *plugin);
void pluginSearch(SearchType searchType, SearchController searchController); void pluginSearch(SearchType searchType, std::shared_ptr<SearchController> searchController);
void pluginSearch(QString customSearchType, 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 pluginDisable(size_t searchId);
@ -21,7 +22,7 @@ private:
explicit SearchTaskPluginManager(QObject *parent = nullptr); explicit SearchTaskPluginManager(QObject *parent = nullptr);
//这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分? //这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分?
QHash<int, SearchTaskPluginIface*> m_buildinPlugin; QHash<size_t, SearchTaskPluginIface*> m_buildinPlugin;
QHash<QString, SearchTaskPluginIface*> m_loadedPlugin; QHash<QString, SearchTaskPluginIface*> m_loadedPlugin;
}; };

View File

@ -10,6 +10,8 @@ public:
~ResultItemPrivate(); ~ResultItemPrivate();
void setSearchId(size_t searchId); void setSearchId(size_t searchId);
void setItemKey(QString itemKey); void setItemKey(QString itemKey);
size_t getSearchId();
QString getItemKey();
private: private:
size_t m_searchId; size_t m_searchId;
QString m_itemKey; QString m_itemKey;

View File

@ -19,6 +19,16 @@ void ResultItemPrivate::setItemKey(QString itemKey)
{ {
m_itemKey = itemKey; m_itemKey = itemKey;
} }
size_t ResultItemPrivate::getSearchId()
{
return m_searchId;
}
QString ResultItemPrivate::getItemKey()
{
return m_itemKey;
}
ResultItem::ResultItem() : d(new ResultItemPrivate(this)) ResultItem::ResultItem() : d(new ResultItemPrivate(this))
{ {
} }
@ -39,6 +49,16 @@ ResultItem::ResultItem(const size_t searchId, const QString itemKey) : d(new Res
d->setItemKey(itemKey); d->setItemKey(itemKey);
} }
size_t ResultItem::getSearchId()
{
return d->getSearchId();
}
QString ResultItem::getItemKey()
{
return d->getItemKey();
}
ResultItem::~ResultItem() ResultItem::~ResultItem()
{ {
} }

View File

@ -12,6 +12,8 @@ public:
explicit ResultItem(const size_t searchId); explicit ResultItem(const size_t searchId);
explicit ResultItem(const QString itemKey); explicit ResultItem(const QString itemKey);
explicit ResultItem(const size_t searchId, const QString itemKey); explicit ResultItem(const size_t searchId, const QString itemKey);
size_t getSearchId();
QString getItemKey();
~ResultItem(); ~ResultItem();
private: private:
ResultItemPrivate *d; ResultItemPrivate *d;

View File

@ -171,7 +171,10 @@ SearchController::SearchController(std::shared_ptr<SearchController> parent) : m
SearchController::~SearchController() SearchController::~SearchController()
{ {
delete d; if(d) {
delete d;
d = nullptr;
}
} }
DataQueue<ResultItem> *SearchController::refreshDataqueue() DataQueue<ResultItem> *SearchController::refreshDataqueue()

View File

@ -19,6 +19,8 @@ class SearchController
friend class SearchControllerPrivate; friend class SearchControllerPrivate;
public: public:
explicit SearchController(std::shared_ptr<SearchController> parent = nullptr); explicit SearchController(std::shared_ptr<SearchController> parent = nullptr);
SearchController(SearchController &) = delete;
SearchController &operator =(const SearchController &) = delete;
~SearchController(); ~SearchController();
DataQueue<ResultItem>* refreshDataqueue(); DataQueue<ResultItem>* refreshDataqueue();
size_t refreshSearchId(); size_t refreshSearchId();

View File

@ -1,7 +1,13 @@
#include "file-search-task.h" #include "file-search-task.h"
#include <QDir>
#include <QFile>
#include <QQueue>
#include <QDebug>
using namespace UkuiSearch; using namespace UkuiSearch;
FileSearchTask::FileSearchTask(SearchController searchController): m_searchControl(searchController) FileSearchTask::FileSearchTask(QObject *parent)
{ {
this->setParent(parent);
qRegisterMetaType<size_t>("size_t");
m_pool = new QThreadPool(this); m_pool = new QThreadPool(this);
m_pool->setMaxThreadCount(1); m_pool->setMaxThreadCount(1);
} }
@ -21,7 +27,7 @@ QString FileSearchTask::getCustomSearchType()
return "File"; return "File";
} }
void FileSearchTask::startSearch(SearchController searchController) void FileSearchTask::startSearch(std::shared_ptr<SearchController> searchController)
{ {
FileSearchWorker *fileSearchWorker; FileSearchWorker *fileSearchWorker;
fileSearchWorker = new FileSearchWorker(this, searchController); fileSearchWorker = new FileSearchWorker(this, searchController);
@ -38,35 +44,56 @@ void FileSearchTask::sendFinishSignal(size_t searchId)
Q_EMIT searchFinished(searchId); Q_EMIT searchFinished(searchId);
} }
void FileSearchTask::run()
{
//file search, based on index or direct search?
//1.do search
if(m_searchControl.beginSearchIdCheck(m_searchControl.getCurrentSearchId())) {
//do enqueue here FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr<SearchController> searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController)
m_searchControl.finishSearchIdCheck();
}
m_searchControl.finishSearchIdCheck();
//finish
}
FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController)
{ {
} }
void FileSearchWorker::run() void FileSearchWorker::run()
{ {
qDebug() << "File search start";
//TODO do search here //TODO do search here
QQueue<QString> bfs;
size_t searchId = m_searchController.getCurrentSearchId(); bfs.enqueue(QDir::homePath());
ResultItem ri(searchId); QFileInfoList list;
if(m_searchController.beginSearchIdCheck(m_searchController.getCurrentSearchId())) { QDir dir;
m_searchController.getDataQueue()->enqueue(ri); if(true == m_searchController.get()->isSearchDirOnly()) {
m_searchController.finishSearchIdCheck(); dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
} else { } else {
m_searchController.finishSearchIdCheck(); dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
return; dir.setSorting(QDir::DirsFirst);
} }
QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, searchId)); while(!bfs.empty()) {
dir.setPath(bfs.dequeue());
list = dir.entryInfoList();
for (auto i : list) {
if (i.isDir() && (!(i.isSymLink()))) {
bfs.enqueue(i.absoluteFilePath());
}
bool matched = true;
for(QString word : m_searchController.get()->getKeyword()) {
if(!i.fileName().contains(word, Qt::CaseInsensitive)) {
matched = false;
// return;
}
}
if(matched) {
if((i.isDir() && true == m_searchController.get()->isSearchFileOnly()) ||
(i.isFile() && true == m_searchController.get()->isSearchDirOnly())) {
continue;
} else {
ResultItem ri(m_searchController.get()->getCurrentSearchId(), i.absoluteFilePath());
if(m_searchController.get()->beginSearchIdCheck(m_searchController.get()->getCurrentSearchId())) {
m_searchController.get()->getDataQueue()->enqueue(ri);
m_searchController.get()->finishSearchIdCheck();
} else {
qDebug() << "Search id changed!";
m_searchController.get()->finishSearchIdCheck();
return;
}
}
}
}
}
QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, m_searchController.get()->getCurrentSearchId()));
} }

View File

@ -15,35 +15,33 @@ class FileSearchTask : public SearchTaskPluginIface
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit FileSearchTask(SearchController searchController); explicit FileSearchTask(QObject *parent);
PluginType pluginType() {return PluginType::SearchTaskPlugin;} PluginType pluginType() {return PluginType::SearchTaskPlugin;}
const QString name(); const QString name();
const QString description(); const QString description();
const QIcon icon() {return QIcon::fromTheme("folder");} const QIcon icon() {return QIcon::fromTheme("folder");}
void setEnable() {} void setEnable(bool enable) {}
bool isEnable() { return true;} bool isEnable() { return true;}
SearchType getSearchType() {return SearchType::File;} SearchType getSearchType() {return SearchType::File;}
QString getCustomSearchType(); QString getCustomSearchType();
void startSearch(SearchController searchController); void startSearch(std::shared_ptr<SearchController> searchController);
void stop(); void stop();
Q_INVOKABLE void sendFinishSignal(size_t searchId); Q_INVOKABLE void sendFinishSignal(size_t searchId);
protected:
void run();
private: private:
SearchController m_searchControl;
QThreadPool *m_pool = nullptr; QThreadPool *m_pool = nullptr;
}; };
class FileSearchWorker : public QRunnable class FileSearchWorker : public QRunnable
{ {
public: public:
explicit FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController); explicit FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr<SearchController> searchController);
protected: protected:
void run(); void run();
private: private:
FileSearchTask *m_FileSearchTask; FileSearchTask *m_FileSearchTask;
SearchController m_searchController; std::shared_ptr<SearchController> m_searchController;
}; };
} }
#endif // FILESEARCHTASK_H #endif // FILESEARCHTASK_H

View File

@ -23,6 +23,7 @@ public:
void setOnlySearchFile(bool onlySearchFile); void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir); void setOnlySearchDir(bool onlySearchDir);
void setSearchOnlineApps(bool searchOnlineApps); void setSearchOnlineApps(bool searchOnlineApps);
void initSearchPlugin(SearchType searchType);
size_t startSearch(SearchType searchtype, QString customSearchType = QString()); size_t startSearch(SearchType searchtype, QString customSearchType = QString());
void stop(); void stop();

View File

@ -8,6 +8,7 @@ UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent)
q(parent) q(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);
} }
UkuiSearchTaskPrivate::~UkuiSearchTaskPrivate() UkuiSearchTaskPrivate::~UkuiSearchTaskPrivate()
@ -54,6 +55,11 @@ void UkuiSearchTaskPrivate::setSearchOnlineApps(bool searchOnlineApps)
{ {
} }
void UkuiSearchTaskPrivate::initSearchPlugin(SearchType searchType)
{
SearchTaskPluginManager::getInstance()->initPlugins(searchType);
}
size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customSearchType) size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customSearchType)
{ {
@ -64,9 +70,9 @@ size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customS
//plugin manager do async search here //plugin manager do async search here
if(SearchType::Custom != searchtype) { if(SearchType::Custom != searchtype) {
SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, SearchController(m_searchCotroller)); SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, m_searchCotroller);
} else { } else {
SearchTaskPluginManager::getInstance()->pluginSearch(customSearchType, SearchController(m_searchCotroller)); SearchTaskPluginManager::getInstance()->pluginSearch(customSearchType, m_searchCotroller);
} }
@ -127,6 +133,11 @@ void UkuiSearchTask::setSearchOnlineApps(bool searchOnlineApps)
d->setSearchOnlineApps(searchOnlineApps); d->setSearchOnlineApps(searchOnlineApps);
} }
void UkuiSearchTask::initSearchPlugin(SearchType searchType)
{
d->initSearchPlugin(searchType);
}
size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchType) size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchType)
{ {
return d->startSearch(searchtype, customSearchType); return d->startSearch(searchtype, customSearchType);

View File

@ -20,6 +20,7 @@ public:
void setOnlySearchFile(bool onlySearchFile); void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir); void setOnlySearchDir(bool onlySearchDir);
void setSearchOnlineApps(bool searchOnlineApps); void setSearchOnlineApps(bool searchOnlineApps);
void initSearchPlugin(SearchType searchType);
size_t startSearch(SearchType searchtype, QString customSearchType = QString()); size_t startSearch(SearchType searchtype, QString customSearchType = QString());
void stop(); void stop();