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 {
enum class SearchType
{
File = 0,
FileContent,
Application,
Setting,
Note,
Mail,
Custom
File = 0x1 << 0,
FileContent = 0x1 << 1,
Application = 0x1 << 2,
Setting = 0x1 << 3,
Note = 0x1 << 4,
Mail = 0x1 << 5,
Custom = 0x1 << 6
};
}
#endif // COMMONDEFINES_H

View File

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

View File

@ -1,5 +1,7 @@
#include "search-task-plugin-manager.h"
#include <QDebug>
#include "file-search-task.h"
using namespace UkuiSearch;
static SearchTaskPluginManager *global_instance = nullptr;
SearchTaskPluginManager *SearchTaskPluginManager::getInstance()
@ -10,6 +12,14 @@ SearchTaskPluginManager *SearchTaskPluginManager::getInstance()
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)
{
//这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。
@ -26,34 +36,34 @@ bool SearchTaskPluginManager::registerPlugin(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);
return true;
}
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.value(type)->isEnable()) {
Q_EMIT this->pluginDisable(searchController.getCurrentSearchId());
Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId());
return;
}
qDebug() << "start search";
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.value(customSearchType)->isEnable()) {
Q_EMIT this->pluginDisable(searchController.getCurrentSearchId());
Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId());
return;
}
m_loadedPlugin.value(customSearchType)->startSearch(searchController);

View File

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

View File

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

View File

@ -19,6 +19,16 @@ void ResultItemPrivate::setItemKey(QString itemKey)
{
m_itemKey = itemKey;
}
size_t ResultItemPrivate::getSearchId()
{
return m_searchId;
}
QString ResultItemPrivate::getItemKey()
{
return m_itemKey;
}
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);
}
size_t ResultItem::getSearchId()
{
return d->getSearchId();
}
QString ResultItem::getItemKey()
{
return d->getItemKey();
}
ResultItem::~ResultItem()
{
}

View File

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

View File

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

View File

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

View File

@ -1,7 +1,13 @@
#include "file-search-task.h"
#include <QDir>
#include <QFile>
#include <QQueue>
#include <QDebug>
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->setMaxThreadCount(1);
}
@ -21,7 +27,7 @@ QString FileSearchTask::getCustomSearchType()
return "File";
}
void FileSearchTask::startSearch(SearchController searchController)
void FileSearchTask::startSearch(std::shared_ptr<SearchController> searchController)
{
FileSearchWorker *fileSearchWorker;
fileSearchWorker = new FileSearchWorker(this, searchController);
@ -38,35 +44,56 @@ void FileSearchTask::sendFinishSignal(size_t 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
m_searchControl.finishSearchIdCheck();
}
m_searchControl.finishSearchIdCheck();
//finish
}
FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController)
FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr<SearchController> searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController)
{
}
void FileSearchWorker::run()
{
qDebug() << "File search start";
//TODO do search here
size_t searchId = m_searchController.getCurrentSearchId();
ResultItem ri(searchId);
if(m_searchController.beginSearchIdCheck(m_searchController.getCurrentSearchId())) {
m_searchController.getDataQueue()->enqueue(ri);
m_searchController.finishSearchIdCheck();
QQueue<QString> bfs;
bfs.enqueue(QDir::homePath());
QFileInfoList list;
QDir dir;
if(true == m_searchController.get()->isSearchDirOnly()) {
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
} else {
m_searchController.finishSearchIdCheck();
return;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
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
public:
explicit FileSearchTask(SearchController searchController);
explicit FileSearchTask(QObject *parent);
PluginType pluginType() {return PluginType::SearchTaskPlugin;}
const QString name();
const QString description();
const QIcon icon() {return QIcon::fromTheme("folder");}
void setEnable() {}
void setEnable(bool enable) {}
bool isEnable() { return true;}
SearchType getSearchType() {return SearchType::File;}
QString getCustomSearchType();
void startSearch(SearchController searchController);
void startSearch(std::shared_ptr<SearchController> searchController);
void stop();
Q_INVOKABLE void sendFinishSignal(size_t searchId);
protected:
void run();
private:
SearchController m_searchControl;
QThreadPool *m_pool = nullptr;
};
class FileSearchWorker : public QRunnable
{
public:
explicit FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController);
explicit FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr<SearchController> searchController);
protected:
void run();
private:
FileSearchTask *m_FileSearchTask;
SearchController m_searchController;
std::shared_ptr<SearchController> m_searchController;
};
}
#endif // FILESEARCHTASK_H

View File

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

View File

@ -8,6 +8,7 @@ UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent)
q(parent)
{
m_searchCotroller = std::shared_ptr<SearchController>(new SearchController());
connect(SearchTaskPluginManager::getInstance(), &SearchTaskPluginManager::searchFinished, this, &UkuiSearchTaskPrivate::searchFinished);
}
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)
{
@ -64,9 +70,9 @@ size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customS
//plugin manager do async search here
if(SearchType::Custom != searchtype) {
SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, SearchController(m_searchCotroller));
SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, m_searchCotroller);
} 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);
}
void UkuiSearchTask::initSearchPlugin(SearchType searchType)
{
d->initSearchPlugin(searchType);
}
size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchType)
{
return d->startSearch(searchtype, customSearchType);

View File

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