Merge pull request #221 from iaom/0502-dev

Add plugin manager.
This commit is contained in:
fensite 2021-05-06 09:55:05 +08:00 committed by GitHub
commit b8c5861ebf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 189 additions and 10 deletions

View File

@ -1,5 +1,6 @@
QT += core xml widgets dbus concurrent
VERSION = 0.0.1
DEFINES += VERSION='\\"$${VERSION}\\"'
TARGET = ukui-search
TEMPLATE = lib
@ -16,6 +17,9 @@ CONFIG += c++11 link_pkgconfig no_keywords lrelease
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
PLUGIN_INSTALL_DIRS = $$[QT_INSTALL_LIBS]/ukui-search-plugins
DEFINES += PLUGIN_INSTALL_DIRS='\\"$${PLUGIN_INSTALL_DIRS}\\"'
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
@ -25,7 +29,8 @@ include(index/index.pri)
include(parser/parser.pri))
include(appsearch/appsearch.pri)
include(settingsearch/settingsearch.pri))
include(plugininterface/plugininterface.pri)
include(plugininterface/plugin-interface.pri)
include(pluginmanage/plugin-manager.pri)
LIBS += -L$$OUT_PWD/../libchinese-segmentation/ -lchinese-segmentation
LIBS += -lxapian -lquazip5 -luchardet

View File

@ -6,7 +6,7 @@ namespace Zeeker {
class PluginInterface
{
public:
enum PluginType
enum class PluginType
{
MenuPlugin,
PreviewPagePlugin,

View File

@ -4,4 +4,3 @@ HEADERS += \
$$PWD/plugin-iface.h \
$$PWD/search-plugin-iface.h
SOURCES +=

View File

@ -11,9 +11,9 @@ namespace Zeeker {
class SearchPluginIface : public PluginInterface
{
public:
///
/// \brief The resultInfo struct
///
/**
* @brief The resultInfo struct
*/
struct resultInfo
{
QIcon icon;
@ -21,13 +21,13 @@ public:
QString description;
QMap<QString,QString> actionMap;//action name and action key
};
virtual ~SearchInterface(){}
virtual ~SearchPluginIface() {}
virtual QString getPluginName() = 0;
virtual void KeywordSearch(QString keyword,QQueue<resultInfo>);
virtual void openAction(QString key);
virtual void KeywordSearch(QString keyword,QQueue<resultInfo> *searchResult);
virtual void openAction(QString name, QString key);
};
}
Q_DECLARE_INTERFACE(SearchPluginIface, SearchPluginIface_iid)
Q_DECLARE_INTERFACE(Zeeker::SearchPluginIface, SearchPluginIface_iid)
#endif // SEARCHPLUGINIFACE_H

View File

@ -0,0 +1,59 @@
#include "plugin-manager.h"
#include "search-plugin-manager.h"
using namespace Zeeker;
static PluginManager *global_instance = nullptr;
void PluginManager::init()
{
PluginManager::getInstance();
}
PluginManager *PluginManager::getInstance()
{
if (!global_instance) {
global_instance = new PluginManager;
}
return global_instance;
}
void PluginManager::setPluginEnableByName(const QString &pluginName, bool enable)
{
m_hash.value(pluginName)->setEnable(enable);
}
PluginManager::PluginManager(QObject *parent) : QObject(parent)
{
QDir pluginsDir(PLUGIN_INSTALL_DIRS);
pluginsDir.setFilter(QDir::Files);
Q_FOREACH(QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
// version check
if (pluginLoader.metaData().value("MetaData").toObject().value("version").toString() != VERSION)
continue;
QObject *plugin = pluginLoader.instance();
if (!plugin)
continue;
PluginInterface *piface = dynamic_cast<PluginInterface*>(plugin);
if (!piface)
continue;
m_hash.insert(piface->name(), piface);
switch (piface->pluginType()) {
case PluginInterface::PluginType::SearchPlugin: {
auto p = dynamic_cast<SearchPluginIface *>(plugin);
SearchPluginManager::getInstance()->registerPlugin(p);
break;
}
default:
break;
}
}
}
PluginManager::~PluginManager()
{
m_hash.clear();
SearchPluginManager::getInstance()->close();
}

View File

@ -0,0 +1,38 @@
#ifndef PLUGINMANAGER_H
#define PLUGINMANAGER_H
#include <QObject>
#include <QDebug>
#include <QDir>
#include <QPluginLoader>
#include <QApplication>
#include "libsearch_global.h"
#include "plugininterface/plugin-iface.h"
#include "plugininterface/search-plugin-iface.h"
namespace Zeeker {
class LIBSEARCH_EXPORT PluginManager : public QObject
{
Q_OBJECT
public:
static void init();
static PluginManager *getInstance();
void close();
Q_SIGNALS:
void pluginStateChanged(const QString &pluginName, bool enable);
public Q_SLOTS:
void setPluginEnableByName(const QString &pluginName, bool enable);
private:
explicit PluginManager(QObject *parent = nullptr);
~PluginManager();
QHash<QString, PluginInterface*> m_hash;
};
}
#endif // PLUGINMANAGER_H

View File

@ -0,0 +1,9 @@
INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/search-plugin-manager.h \
$$PWD/plugin-manager.h
SOURCES += \
$$PWD/search-plugin-manager.cpp \
$$PWD/plugin-manager.cpp

View File

@ -0,0 +1,40 @@
#include "search-plugin-manager.h"
using namespace Zeeker;
static SearchPluginManager *global_instance = nullptr;
bool SearchPluginManager::registerPlugin(Zeeker::SearchPluginIface *plugin)
{
if (m_hash.value(plugin->name())) {
return false;
}
m_hash.insert(plugin->name(), plugin);
return true;
}
SearchPluginManager *SearchPluginManager::getInstance()
{
if (!global_instance) {
global_instance = new SearchPluginManager;
}
return global_instance;
}
const QStringList SearchPluginManager::getPluginIds()
{
return m_hash.keys();
}
SearchPluginIface *SearchPluginManager::getPlugin(const QString &pluginId)
{
return m_hash.value(pluginId);
}
void SearchPluginManager::close()
{
this->deleteLater();
}
SearchPluginManager::SearchPluginManager(QObject *parent)
{
}

View File

@ -0,0 +1,29 @@
#ifndef SEARCHPLUGINFACTORY_H
#define SEARCHPLUGINFACTORY_H
#include <QObject>
#include "plugininterface/search-plugin-iface.h"
namespace Zeeker {
class SearchPluginManager : public QObject
{
Q_OBJECT
public:
bool registerPlugin(SearchPluginIface *plugin);
static SearchPluginManager *getInstance();
const QStringList getPluginIds();
SearchPluginIface *getPlugin(const QString &pluginId);
void close();
private:
QHash<QString, SearchPluginIface*> m_hash;
explicit SearchPluginManager(QObject *parent = nullptr);
~SearchPluginManager();
};
}
#endif // SEARCHPLUGINFACTORY_H