diff --git a/src/imports/mvvmcore/plugins.qmltypes b/src/imports/mvvmcore/plugins.qmltypes index 8ab2e16..c301d40 100644 --- a/src/imports/mvvmcore/plugins.qmltypes +++ b/src/imports/mvvmcore/plugins.qmltypes @@ -485,6 +485,36 @@ Module { name: "registerObject" Parameter { name: "componentUrl"; type: "QUrl" } } + Method { + name: "registerPlugin" + Parameter { name: "iid"; type: "string" } + Parameter { name: "pluginType"; type: "string" } + Parameter { name: "pluginKey"; type: "string" } + Parameter { name: "scope"; type: "DestructionScope" } + Parameter { name: "weak"; type: "bool" } + } + Method { + name: "registerPlugin" + Parameter { name: "iid"; type: "string" } + Parameter { name: "pluginType"; type: "string" } + Parameter { name: "pluginKey"; type: "string" } + Parameter { name: "scope"; type: "DestructionScope" } + } + Method { + name: "registerPlugin" + Parameter { name: "iid"; type: "string" } + Parameter { name: "pluginType"; type: "string" } + Parameter { name: "pluginKey"; type: "string" } + } + Method { + name: "registerPlugin" + Parameter { name: "iid"; type: "string" } + Parameter { name: "pluginType"; type: "string" } + } + Method { + name: "registerPlugin" + Parameter { name: "iid"; type: "string" } + } Method { name: "service" type: "QObject*" diff --git a/src/imports/mvvmcore/qqmlserviceregistry.cpp b/src/imports/mvvmcore/qqmlserviceregistry.cpp index 5237dcf..c454451 100644 --- a/src/imports/mvvmcore/qqmlserviceregistry.cpp +++ b/src/imports/mvvmcore/qqmlserviceregistry.cpp @@ -60,6 +60,15 @@ void QQmlServiceRegistry::registerObject(const QUrl &componentUrl, bool weak) }, {}, ServiceRegistry::DestroyOnAppQuit, weak); } +void QQmlServiceRegistry::registerPlugin(const QString &iid, QString pluginType, QString pluginKey, QQmlServiceRegistry::DestructionScope scope, bool weak) +{ + ServiceRegistry::instance()->registerPlugin(iid.toUtf8(), + std::move(pluginType), + std::move(pluginKey), + static_cast(scope), + weak); +} + QObject *QQmlServiceRegistry::service(const QString &iid) { return ServiceRegistry::instance()->serviceObj(iid.toUtf8()); diff --git a/src/imports/mvvmcore/qqmlserviceregistry.h b/src/imports/mvvmcore/qqmlserviceregistry.h index 4f09423..66bb452 100644 --- a/src/imports/mvvmcore/qqmlserviceregistry.h +++ b/src/imports/mvvmcore/qqmlserviceregistry.h @@ -32,6 +32,12 @@ public: Q_INVOKABLE void registerObject(const QString &iid, const QJSValue &function, bool weak = false); Q_INVOKABLE void registerObject(const QUrl &componentUrl, bool weak = false); + Q_INVOKABLE void registerPlugin(const QString &iid, + QString pluginType = {}, + QString pluginKey = {}, + DestructionScope scope = DestroyOnAppDestroy, + bool weak = false); + Q_INVOKABLE QObject *service(const QString &iid); private: diff --git a/src/mvvmcore/mvvmcore.pro b/src/mvvmcore/mvvmcore.pro index 8e6b2b2..4212a9f 100644 --- a/src/mvvmcore/mvvmcore.pro +++ b/src/mvvmcore/mvvmcore.pro @@ -76,3 +76,5 @@ never_true_for_lupdate { SOURCES += $$files(../imports/mvvmcore/*.cpp) \ $$files(../imports/mvvmcore/*.qml) } + +mingw: LIBS_PRIVATE += -lQt5Gui -lQt5Core diff --git a/src/mvvmcore/serviceregistry.cpp b/src/mvvmcore/serviceregistry.cpp index f8c937d..d596b03 100644 --- a/src/mvvmcore/serviceregistry.cpp +++ b/src/mvvmcore/serviceregistry.cpp @@ -68,13 +68,13 @@ void ServiceRegistry::registerService(const QByteArray &iid, const std::function registerService(iid, fn, std::move(injectables), DestroyOnAppDestroy, weak); } -void ServiceRegistry::registerService(QByteArray iid, QString pluginKey, QString pluginType, ServiceRegistry::DestructionScope scope, bool weak) +void ServiceRegistry::registerPlugin(QByteArray iid, QString pluginType, QString pluginKey, ServiceRegistry::DestructionScope scope, bool weak) { QMutexLocker _(&d->serviceMutex); if(d->serviceBlocked(iid)) throw ServiceExistsException(iid); - auto info = QSharedPointer::create(std::move(pluginKey), - std::move(pluginType), + auto info = QSharedPointer::create(std::move(pluginType), + std::move(pluginKey), std::move(iid), weak, scope); @@ -276,10 +276,10 @@ QObject *ServiceRegistryPrivate::MetaServiceInfo::construct(ServiceRegistryPriva -ServiceRegistryPrivate::PluginServiceInfo::PluginServiceInfo(QString &&key, QString &&type, QByteArray &&iid, bool weak, ServiceRegistry::DestructionScope scope) : +ServiceRegistryPrivate::PluginServiceInfo::PluginServiceInfo(QString &&type, QString &&key, QByteArray &&iid, bool weak, ServiceRegistry::DestructionScope scope) : ServiceInfo{weak, scope}, - _key{std::move(key)}, _type{std::move(type)}, + _key{std::move(key)}, _iid{std::move(iid)} {} @@ -291,7 +291,13 @@ const QByteArray &ServiceRegistryPrivate::PluginServiceInfo::iid() const QObject *ServiceRegistryPrivate::PluginServiceInfo::construct(ServiceRegistryPrivate *d) const { try { - QPluginFactoryBase factory{_type, _iid}; + QFileInfo typeInfo{_type}; + QPluginFactoryBase factory{typeInfo.isAbsolute() ? QString{} : _type, _iid}; + if(typeInfo.isAbsolute()) { + factory.addSearchDir(typeInfo.absolutePath(), false); + factory.reloadPlugins(); + } + QObject *obj = nullptr; if(_key.isEmpty()){ if(!factory.allKeys().isEmpty()) diff --git a/src/mvvmcore/serviceregistry.h b/src/mvvmcore/serviceregistry.h index 6700506..ff67709 100644 --- a/src/mvvmcore/serviceregistry.h +++ b/src/mvvmcore/serviceregistry.h @@ -58,7 +58,7 @@ public: void registerObject(TService *service, DestructionScope scope = DestroyOnAppDestroy, bool weak = false); template - void registerPlugin(QString pluginKey, QString pluginType, DestructionScope scope = DestroyOnAppDestroy, bool weak = false); + void registerPlugin(QString pluginType = {}, QString pluginKey = {}, DestructionScope scope = DestroyOnAppDestroy, bool weak = false); //! Register a service by an iid via their metadata void registerService(const QByteArray &iid, @@ -78,11 +78,11 @@ public: const std::function &fn, QByteArrayList injectables, bool weak); - void registerService(QByteArray iid, - QString pluginKey, - QString pluginType, - DestructionScope scope = DestroyOnAppDestroy, - bool weak = false); + void registerPlugin(QByteArray iid, + QString pluginType = {}, + QString pluginKey = {}, + DestructionScope scope = DestroyOnAppDestroy, + bool weak = false); //! Returns the service for the given interface template @@ -235,9 +235,9 @@ void ServiceRegistry::registerObject(TService *service, DestructionScope scope, #undef QTMVVM_SERVICE_ASSERT template -void ServiceRegistry::registerPlugin(QString pluginKey, QString pluginType, DestructionScope scope, bool weak) +void ServiceRegistry::registerPlugin(QString pluginType, QString pluginKey, DestructionScope scope, bool weak) { - registerService(qobject_interface_iid(), std::move(pluginKey), std::move(pluginType), scope, weak); + registerPlugin(qobject_interface_iid(), std::move(pluginType), std::move(pluginKey), scope, weak); } template diff --git a/src/mvvmcore/serviceregistry_p.h b/src/mvvmcore/serviceregistry_p.h index 9e67bf2..e8f3243 100644 --- a/src/mvvmcore/serviceregistry_p.h +++ b/src/mvvmcore/serviceregistry_p.h @@ -57,8 +57,8 @@ public: class PluginServiceInfo : public ServiceInfo { public: - PluginServiceInfo(QString &&key, - QString &&type, + PluginServiceInfo(QString &&type, + QString &&key, QByteArray &&iid, bool weak, ServiceRegistry::DestructionScope scope); @@ -69,8 +69,8 @@ public: QObject *construct(ServiceRegistryPrivate *d) const final; private: - QString _key; QString _type; + QString _key; QByteArray _iid; };