/*! Creates and returns a screensaver on the given token. \param token Identifies the screensaver to be created. \return The created state. */ Screensaver* ScreensaverFactoryPrivate::createScreensaver(const ScreensaverToken &token) { QStringList pluginPaths; // check plugin dirs from root of different drives QFileInfoList drives = QDir::drives(); for(int i=0; i < drives.count(); i++) { QFileInfo drive = drives.at(i); QString driveLetter = drive.absolutePath(); QString path = driveLetter + mPluginDirectory; if (QDir(path).exists()) { pluginPaths << path; } } // check plugin dir relative to current dir if (QDir(mPluginDirectory).exists() && !pluginPaths.contains(QDir(mPluginDirectory).absolutePath())) { pluginPaths << mPluginDirectory; } IScreensaverProvider *provider(0); QPluginLoader *loader = new QPluginLoader(); QObject *plugin(0); for(int i=0; i < pluginPaths.count(); i++) { QString path = pluginPaths.at(i); QString fileName = QDir(path).absoluteFilePath(token.mLibrary); loader->setFileName(fileName); plugin = loader->instance(); provider = qobject_cast<IScreensaverProvider*>(plugin); if (provider) { break; } } Screensaver *screensaver(0); if (provider) { screensaver = provider->createScreensaver(token); if (!screensaver) { qWarning() << "Screensaver creation failed."; qWarning() << token.mLibrary << "cannot provide" << token.mUri; loader->unload(); delete loader; } else { // unload plugin once screensaver gets deleted ScreensaverPluginUnloader *unloader = new ScreensaverPluginUnloader(loader); unloader->connect(screensaver, SIGNAL(destroyed()), SLOT(deleteLater())); } } else { qDebug() << "Screensaver creation failed."; qWarning() << token.mLibrary << "- provider not found"; loader->unload(); delete loader; } return screensaver; }
HsWidget* HsWidgetFactoryPrivate::createWidget(const HsWidgetToken &token) { QPluginLoader* loader = new QPluginLoader(token.mLibrary); QObject* plugin = loader->instance(); IHsWidgetProvider* provider = qobject_cast<IHsWidgetProvider*>(plugin); HsWidget* widget(0); if (provider) { widget = provider->createWidget(token); if (!widget) { HSDEBUG("Widget creation failed.") loader->unload(); delete loader; } else { HsPluginUnloader* p = new HsPluginUnloader(loader); p->connect(widget,SIGNAL(destroyed()),SLOT(deleteLater())); } } else { HSDEBUG("Widget creation failed - No provider.") loader->unload(); delete loader; } return widget; }
ToolPluginManager::ToolPluginManager() : mCustomizer() { mPluginsDir = QDir(qApp->applicationDirPath()); while (!mPluginsDir.isRoot() && !mPluginsDir.entryList(QDir::Dirs).contains("plugins")) { mPluginsDir.cdUp(); } mPluginsDir.cd("plugins"); for (QString const &fileName : mPluginsDir.entryList(QDir::Files)) { // TODO: Free memory QPluginLoader *loader = new QPluginLoader(mPluginsDir.absoluteFilePath(fileName)); QObject *plugin = loader->instance(); if (plugin) { ToolPluginInterface *toolPlugin = qobject_cast<ToolPluginInterface *>(plugin); if (toolPlugin) { mPlugins << toolPlugin; mLoaders << loader; } else { // TODO: Does not work on linux. See editorManager.cpp for more details. // loader->unload(); delete loader; } } else { loader->unload(); delete loader; } } loadDefaultSettings(); setHotKeyActions(); }
void AbstractPluginLoader::loadPlugins(const QRegExp& fileRx, QVector<QString>* errors) { Q_D(AbstractPluginLoader); const QDir pluginsFolder(this->loadingFolder()); QStringList entries(pluginsFolder.entryList(QDir::Files)); foreach (const QString& entry, entries) { if (fileRx.indexIn(entry) != -1 && ::isLibrary(entry)) { // Try to load the plugin #ifdef DEBUG_ABSTRACT_PLUGIN_LOADER qDebug() << "try to load" << entry; #endif // DEBUG_ABSTRACT_PLUGIN_LOADER QPluginLoader* pluginLoader = new QPluginLoader(pluginsFolder.absoluteFilePath(entry)); QObject* plugin = pluginLoader->instance(); // Is the plugin compatible ? if (this->isPluginCompatible(plugin)) { d->m_plugins.append(plugin); d->m_pluginLoaders.append(pluginLoader); d->m_pluginFilenames.insert(plugin, entry); } else { #ifdef DEBUG_ABSTRACT_PLUGIN_LOADER qDebug() << " not added"; #endif // DEBUG_ABSTRACT_PLUGIN_LOADER if (errors != NULL) //: %1 holds the path to a plugin (DLL) //: %2 holds an error description errors->append(QObject::tr("Failed to load plugin (maybe wrong interface) %1 : %2") .arg(pluginLoader->fileName()) .arg(pluginLoader->errorString())); pluginLoader->unload(); delete pluginLoader; } // end if (this->isPluginCompatible(plugin)) } // end if (fileRx.indexIn(entry) != -1) } // end foreach () }
bool ComponentManager::unloadModelComponentInfoById(const QString& id) { if(m_modelComponentInfoById.contains(id)) { IModelComponentInfo* componentInfo = m_modelComponentInfoById[id]; if (m_modelComponentInfoHash.contains(componentInfo)) { QPluginLoader* plugin = m_modelComponentInfoHash[componentInfo]; m_modelComponentInfoHash.remove(componentInfo); m_modelComponentInfoById.remove(id); emit modelComponentInfoUnloaded(componentInfo->id()); emit postMessage("Model component library " + componentInfo->id() + " unloaded"); plugin->unload(); return true; } else { emit postMessage("Unable to unload model component library " + componentInfo->id()); return false; } } else { return false; } }
void tst_QPluginLoader::reloadPlugin() { QPluginLoader loader; loader.setFileName( sys_qualifiedLibraryName("theplugin")); //a plugin loader.load(); // not recommended, instance() should do the job. PluginInterface *instance = qobject_cast<PluginInterface*>(loader.instance()); QVERIFY(instance); QCOMPARE(instance->pluginName(), QLatin1String("Plugin ok")); QSignalSpy spy(loader.instance(), SIGNAL(destroyed())); QVERIFY(spy.isValid()); QVERIFY(loader.unload()); // refcount reached 0, did really unload QCOMPARE(spy.count(), 1); // reload plugin QVERIFY(loader.load()); QVERIFY(loader.isLoaded()); PluginInterface *instance2 = qobject_cast<PluginInterface*>(loader.instance()); QVERIFY(instance2); QCOMPARE(instance2->pluginName(), QLatin1String("Plugin ok")); QVERIFY(loader.unload()); }
bool EditorManager::unloadPlugin(QString const &pluginName) { QPluginLoader *loader = mLoaders[mPluginFileName[pluginName]]; if (loader != NULL) { mLoaders.remove(mPluginFileName[pluginName]); mPluginIface.remove(pluginName); mPluginFileName.remove(pluginName); mPluginsLoaded.removeAll(pluginName); if (!loader->unload()) { QMessageBox::warning(NULL, tr("error"), tr("Plugin unloading failed: ") + loader->errorString()); delete loader; return false; } delete loader; return true; } return false; }
QString PluginManagerImplementation::unloadPlugin(QString const &pluginName) { QPluginLoader *loader = mLoaders[pluginName]; if (loader) { mLoaders.remove(pluginName); if (!loader->unload()) { QString const error = loader->errorString(); delete loader; return error; } delete loader; return QString(); } return QString("Plugin was not found"); }
bool EditorManager::loadPlugin(QString const &pluginName) { QPluginLoader *loader = new QPluginLoader(mPluginsDir.absoluteFilePath(pluginName)); loader->load(); QObject *plugin = loader->instance(); if (plugin) { EditorInterface *iEditor = qobject_cast<EditorInterface *>(plugin); if (iEditor) { mPluginsLoaded += iEditor->id(); mPluginFileName.insert(iEditor->id(), pluginName); mPluginIface[iEditor->id()] = iEditor; mLoaders.insert(pluginName, loader); return true; } } QMessageBox::warning(NULL, tr("error"), tr("Plugin loading failed: ") + loader->errorString()); loader->unload(); delete loader; return false; }
bool ComponentManager::unloadAdaptedOutputFactoryComponentInfoById(const QString& id) { if(m_adaptedOutputFactoryComponentInfoById.contains(id)) { IAdaptedOutputFactoryComponentInfo* componentInfo = m_adaptedOutputFactoryComponentInfoById[id]; if (m_adaptedOutputFactoryComponentInfoHash.contains(componentInfo)) { QPluginLoader* plugin = m_adaptedOutputFactoryComponentInfoHash[componentInfo]; m_adaptedOutputFactoryComponentInfoHash.remove(componentInfo); m_adaptedOutputFactoryComponentInfoById.remove(id); IAdaptedOutputFactoryComponent* factoryComponent = m_adaptedOutputFactoryComponentByComponentInfo[componentInfo]; m_adaptedOutputFactoryComponentByComponentInfo.remove(componentInfo); m_adaptedOutputFactoryComponentById.remove(factoryComponent->id()); emit adaptedOutputFactoryComponentUnloaded(componentInfo->id()); emit postMessage("Adapted output factory component library " + componentInfo->id() + " unloaded"); delete factoryComponent; plugin->unload(); return true; } else { emit postMessage("Unable to unload adapted output factory component library " + componentInfo->id()); return false; } } else { return false; } }
EditorManager::EditorManager(QObject *parent) : QObject(parent) { mPluginsDir = QDir(qApp->applicationDirPath()); while (!mPluginsDir.isRoot() && !mPluginsDir.entryList(QDir::Dirs).contains("plugins")) { mPluginsDir.cdUp(); } mPluginsDir.cd("plugins"); foreach (QString const &fileName, mPluginsDir.entryList(QDir::Files)) { QPluginLoader *loader = new QPluginLoader(mPluginsDir.absoluteFilePath(fileName)); QObject *plugin = loader->instance(); if (plugin) { EditorInterface *iEditor = qobject_cast<EditorInterface *>(plugin); if (iEditor) { mPluginsLoaded += iEditor->id(); mPluginFileName.insert(iEditor->id(), fileName); mPluginIface[iEditor->id()] = iEditor; mLoaders.insert(fileName, loader); } else { // TODO: Just does not work under Linux. Seems to be memory corruption when // loading, unloading, and then loading .so file again. // To reproduce, uncomment this, build VisualInterpreter, and try to launch QReal. // With some tool plugins, like MetaEditorSupport or Exterminatus, works fine, // also works fine on Windows. Investigation required. // loader->unload(); delete loader; } } else { qDebug() << "Plugin loading failed: " << loader->errorString(); loader->unload(); delete loader; } } }
void suiPluginManager::unloadPlugin(const QString &fileName) { qDebug() << "Unload plugin: " << fileName; if (mPluginLoaders.find(fileName) == mPluginLoaders.end()) SuiExcept(SuiExceptionItemNotFound, QString("Plugin '%1' doesn't loaded").arg(fileName), "void suiPluginManager::unloadPlugin(const QString &fileName)"); QMap<QString, QPluginLoader*>::iterator it = mPluginLoaders.find(fileName); QPluginLoader *loader = it.value(); UiPluginInterface *plugInterface = qobject_cast<UiPluginInterface*>(loader->instance()); if (!plugInterface) SuiExcept(SuiExceptionInternalError, QString("There are no plugin interface in '%1'").arg(fileName), "void suiPluginManager::unloadPlugin(const QString &fileName)"); processUnloadPlugin(plugInterface); mPluginLoaders.erase(it); if (!loader->unload()) SuiExcept(SuiExceptionInternalError, QString("Can't unload plugin '%1'").arg(fileName), "void suiPluginManager::unloadPlugin(const QString &fileName)"); delete loader; }
void tst_QPluginLoader::errorString() { #if defined(Q_OS_WINCE) // On WinCE we need an QCoreApplication object for current dir int argc = 0; QCoreApplication app(argc,0); #endif const QString unknown(QLatin1String("Unknown error")); { QPluginLoader loader; // default constructed bool loaded = loader.load(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(loaded, false); QCOMPARE(loader.errorString(), unknown); QObject *obj = loader.instance(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(obj, static_cast<QObject*>(0)); QCOMPARE(loader.errorString(), unknown); bool unloaded = loader.unload(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(unloaded, false); QCOMPARE(loader.errorString(), unknown); } { QPluginLoader loader( sys_qualifiedLibraryName("tst_qpluginloaderlib")); //not a plugin bool loaded = loader.load(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(loaded, false); QVERIFY(loader.errorString() != unknown); QObject *obj = loader.instance(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(obj, static_cast<QObject*>(0)); QVERIFY(loader.errorString() != unknown); bool unloaded = loader.unload(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(unloaded, false); QVERIFY(loader.errorString() != unknown); } { QPluginLoader loader( sys_qualifiedLibraryName("nosuchfile")); //not a file bool loaded = loader.load(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(loaded, false); QVERIFY(loader.errorString() != unknown); QObject *obj = loader.instance(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(obj, static_cast<QObject*>(0)); QVERIFY(loader.errorString() != unknown); bool unloaded = loader.unload(); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QCOMPARE(unloaded, false); QVERIFY(loader.errorString() != unknown); } #if !defined Q_OS_WIN && !defined Q_OS_MAC && !defined Q_OS_HPUX && !defined Q_OS_SYMBIAN && !defined Q_OS_QNX { QPluginLoader loader( sys_qualifiedLibraryName("almostplugin")); //a plugin with unresolved symbols loader.setLoadHints(QLibrary::ResolveAllSymbolsHint); QCOMPARE(loader.load(), false); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QVERIFY(loader.errorString() != unknown); QCOMPARE(loader.instance(), static_cast<QObject*>(0)); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QVERIFY(loader.errorString() != unknown); QCOMPARE(loader.unload(), false); #ifdef SHOW_ERRORS qDebug() << loader.errorString(); #endif QVERIFY(loader.errorString() != unknown); } #endif { QPluginLoader loader( sys_qualifiedLibraryName("theplugin")); //a plugin QCOMPARE(loader.load(), true); QCOMPARE(loader.errorString(), unknown); QVERIFY(loader.instance() != static_cast<QObject*>(0)); QCOMPARE(loader.errorString(), unknown); // Make sure that plugin really works PluginInterface* theplugin = qobject_cast<PluginInterface*>(loader.instance()); QString pluginName = theplugin->pluginName(); QCOMPARE(pluginName, QLatin1String("Plugin ok")); QCOMPARE(loader.unload(), true); QCOMPARE(loader.errorString(), unknown); } }
bool unloadBinary() { return loader->unload(); }
IComponentInfo* ComponentManager::loadComponent(const QFileInfo& file) { QString message; if (file.exists()) { if (hasValidExtension(file)) { QPluginLoader* pluginLoader = new QPluginLoader(file.absoluteFilePath(), this); QObject* component = pluginLoader->instance(); if (component) { { IModelComponentInfo* mcomponentInfo = qobject_cast<IModelComponentInfo*>(component); if (mcomponentInfo) { for (IModelComponentInfo* model : m_modelComponentInfoHash.keys()) { if (!mcomponentInfo->id().compare(model->id())) { pluginLoader->unload(); emit postMessage("Model component library " + mcomponentInfo->id() + "has already been loaded"); return model; } } mcomponentInfo->setLibraryFilePath(file.absoluteFilePath()); m_modelComponentInfoHash[mcomponentInfo] = pluginLoader; m_modelComponentInfoById[mcomponentInfo->id()] = mcomponentInfo; emit modelComponentInfoLoaded(mcomponentInfo); return mcomponentInfo; } } { IAdaptedOutputFactoryComponentInfo* acomponentInfo = qobject_cast<IAdaptedOutputFactoryComponentInfo*>(component); if (acomponentInfo) { acomponentInfo->setLibraryFilePath(file.absoluteFilePath()); for (IAdaptedOutputFactoryComponentInfo* model : m_adaptedOutputFactoryComponentInfoHash.keys()) { if (!acomponentInfo->id().compare(model->id())) { pluginLoader->unload(); emit postMessage("Adapted output factory component library " + acomponentInfo->id() + "has already been loaded"); return model; } } m_adaptedOutputFactoryComponentInfoHash[acomponentInfo] = pluginLoader; m_adaptedOutputFactoryComponentInfoById[acomponentInfo->id()] = acomponentInfo; IAdaptedOutputFactoryComponent* adaptedFactoryComponent = acomponentInfo->createComponentInstance(); m_adaptedOutputFactoryComponentByComponentInfo[acomponentInfo] = adaptedFactoryComponent; m_adaptedOutputFactoryComponentById[adaptedFactoryComponent->id()] = adaptedFactoryComponent; acomponentInfo->setLibraryFilePath(file.absoluteFilePath()); emit adaptedOutputFactoryComponentInfoLoaded(acomponentInfo); return acomponentInfo; } } delete component; message = "\"" + file.filePath() + "\" is not a valid HydroCouple component library"; emit postMessage(message); return nullptr; } else { message = "'/" + file.filePath() + "'/ is not a valid Qt plugin library"; emit postMessage(message); } pluginLoader->unload(); } } else { message = "File '" + file.filePath() + "' does not exist"; emit postMessage(message); } return nullptr; }