Changed: #1193 Code cleanup.
This commit is contained in:
parent
68891726db
commit
376bc221df
6 changed files with 356 additions and 356 deletions
|
@ -45,18 +45,18 @@ void CPluginManager::addObject(QObject *obj)
|
|||
if (obj == 0)
|
||||
{
|
||||
nlwarning("trying to add null object");
|
||||
return;
|
||||
return;
|
||||
}
|
||||
if (_allObjects.contains(obj))
|
||||
if (_allObjects.contains(obj))
|
||||
{
|
||||
nlwarning("trying to add duplicate object");
|
||||
return;
|
||||
}
|
||||
}
|
||||
nlinfo(QString("addObject:" + obj->objectName()).toStdString().c_str());
|
||||
|
||||
_allObjects.append(obj);
|
||||
|
||||
Q_EMIT objectAdded(obj);
|
||||
Q_EMIT objectAdded(obj);
|
||||
}
|
||||
|
||||
void CPluginManager::removeObject(QObject *obj)
|
||||
|
@ -64,19 +64,19 @@ void CPluginManager::removeObject(QObject *obj)
|
|||
if (obj == 0)
|
||||
{
|
||||
nlwarning("trying to remove null object");
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_allObjects.contains(obj))
|
||||
if (!_allObjects.contains(obj))
|
||||
{
|
||||
nlinfo(QString("object not in list:" + obj->objectName()).toStdString().c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
nlinfo(QString("removeObject:" + obj->objectName()).toStdString().c_str());
|
||||
|
||||
Q_EMIT aboutToRemoveObject(obj);
|
||||
Q_EMIT aboutToRemoveObject(obj);
|
||||
QWriteLocker lock(&_lock);
|
||||
_allObjects.removeAll(obj);
|
||||
_allObjects.removeAll(obj);
|
||||
}
|
||||
|
||||
QList<QObject *> CPluginManager::allObjects() const
|
||||
|
@ -87,15 +87,15 @@ QList<QObject *> CPluginManager::allObjects() const
|
|||
void CPluginManager::loadPlugins()
|
||||
{
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
setPluginState(spec, State::Loaded);
|
||||
setPluginState(spec, State::Loaded);
|
||||
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
setPluginState(spec, State::Initialized);
|
||||
setPluginState(spec, State::Initialized);
|
||||
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
setPluginState(spec, State::Running);
|
||||
setPluginState(spec, State::Running);
|
||||
|
||||
Q_EMIT pluginsChanged();
|
||||
Q_EMIT pluginsChanged();
|
||||
}
|
||||
|
||||
QStringList CPluginManager::getPluginPaths() const
|
||||
|
@ -117,13 +117,13 @@ QList<CPluginSpec *> CPluginManager::plugins() const
|
|||
void CPluginManager::readPluginPaths()
|
||||
{
|
||||
qDeleteAll(_pluginSpecs);
|
||||
_pluginSpecs.clear();
|
||||
_pluginSpecs.clear();
|
||||
|
||||
QStringList pluginsList;
|
||||
QStringList searchPaths = _pluginPaths;
|
||||
while (!searchPaths.isEmpty())
|
||||
QStringList searchPaths = _pluginPaths;
|
||||
while (!searchPaths.isEmpty())
|
||||
{
|
||||
const QDir dir(searchPaths.takeFirst());
|
||||
const QDir dir(searchPaths.takeFirst());
|
||||
#ifdef Q_OS_WIN
|
||||
const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.dll"), QDir::Files);
|
||||
#elif defined(Q_OS_MAC)
|
||||
|
@ -132,62 +132,62 @@ void CPluginManager::readPluginPaths()
|
|||
const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.so"), QDir::Files);
|
||||
#endif
|
||||
Q_FOREACH (const QFileInfo &file, files)
|
||||
pluginsList << file.absoluteFilePath();
|
||||
pluginsList << file.absoluteFilePath();
|
||||
const QFileInfoList dirs = dir.entryInfoList(QDir::Dirs|QDir::NoDotAndDotDot);
|
||||
Q_FOREACH (const QFileInfo &subdir, dirs)
|
||||
searchPaths << subdir.absoluteFilePath();
|
||||
}
|
||||
searchPaths << subdir.absoluteFilePath();
|
||||
}
|
||||
|
||||
Q_FOREACH (const QString &pluginFile, pluginsList)
|
||||
Q_FOREACH (const QString &pluginFile, pluginsList)
|
||||
{
|
||||
CPluginSpec *spec = new CPluginSpec;
|
||||
CPluginSpec *spec = new CPluginSpec;
|
||||
if (spec->setFileName(pluginFile))
|
||||
_pluginSpecs.append(spec);
|
||||
else
|
||||
delete spec;
|
||||
}
|
||||
}
|
||||
|
||||
Q_EMIT pluginsChanged();
|
||||
Q_EMIT pluginsChanged();
|
||||
}
|
||||
|
||||
CPluginSpec *CPluginManager::pluginByName(const QString &name) const
|
||||
{
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
if (spec->name() == name)
|
||||
return spec;
|
||||
return 0;
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
if (spec->name() == name)
|
||||
return spec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CPluginManager::setPluginState(CPluginSpec *spec, int destState)
|
||||
{
|
||||
if (spec->hasError())
|
||||
return;
|
||||
if (destState == State::Running)
|
||||
if (destState == State::Running)
|
||||
{
|
||||
spec->initializeExtensions();
|
||||
return;
|
||||
return;
|
||||
}
|
||||
else if (destState == State::Deleted)
|
||||
{
|
||||
spec->kill();
|
||||
return;
|
||||
}
|
||||
spec->kill();
|
||||
return;
|
||||
}
|
||||
|
||||
if (destState == State::Loaded)
|
||||
spec->loadLibrary();
|
||||
else if (destState == State::Initialized)
|
||||
spec->initializePlugin();
|
||||
else if (destState == State::Stopped)
|
||||
spec->stop();
|
||||
if (destState == State::Loaded)
|
||||
spec->loadLibrary();
|
||||
else if (destState == State::Initialized)
|
||||
spec->initializePlugin();
|
||||
else if (destState == State::Stopped)
|
||||
spec->stop();
|
||||
}
|
||||
|
||||
void CPluginManager::stopAll()
|
||||
{
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
setPluginState(spec, State::Stopped);
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
setPluginState(spec, State::Stopped);
|
||||
|
||||
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
|
||||
setPluginState(spec, State::Deleted);
|
||||
setPluginState(spec, State::Deleted);
|
||||
}
|
||||
|
||||
}; // namespace NLQT
|
|
@ -33,22 +33,22 @@ class IPlugin;
|
|||
|
||||
class CPluginManager : public IPluginManager
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CPluginManager(QObject *parent = 0);
|
||||
~CPluginManager();
|
||||
CPluginManager(QObject *parent = 0);
|
||||
~CPluginManager();
|
||||
|
||||
// Object pool operations
|
||||
virtual void addObject(QObject *obj);
|
||||
virtual void removeObject(QObject *obj);
|
||||
virtual QList<QObject *> allObjects() const;
|
||||
// Object pool operations
|
||||
virtual void addObject(QObject *obj);
|
||||
virtual void removeObject(QObject *obj);
|
||||
virtual QList<QObject *> allObjects() const;
|
||||
|
||||
// Plugin operations
|
||||
virtual void loadPlugins();
|
||||
virtual QStringList getPluginPaths() const;
|
||||
virtual void setPluginPaths(const QStringList &paths);
|
||||
virtual QList<CPluginSpec *> plugins() const;
|
||||
// Plugin operations
|
||||
virtual void loadPlugins();
|
||||
virtual QStringList getPluginPaths() const;
|
||||
virtual void setPluginPaths(const QStringList &paths);
|
||||
virtual QList<CPluginSpec *> plugins() const;
|
||||
|
||||
CPluginSpec *pluginByName(const QString &name) const;
|
||||
|
||||
|
@ -59,9 +59,9 @@ private:
|
|||
|
||||
mutable QReadWriteLock _lock;
|
||||
|
||||
QList<CPluginSpec *> _pluginSpecs;
|
||||
QStringList _pluginPaths;
|
||||
QList<QObject *> _allObjects;
|
||||
QList<CPluginSpec *> _pluginSpecs;
|
||||
QStringList _pluginPaths;
|
||||
QList<QObject *> _allObjects;
|
||||
|
||||
}; // class CPluginManager
|
||||
|
||||
|
|
|
@ -31,8 +31,8 @@ namespace NLQT
|
|||
{
|
||||
|
||||
CPluginSpec::CPluginSpec():
|
||||
_state(State::Invalid),
|
||||
_hasError(false),
|
||||
_state(State::Invalid),
|
||||
_hasError(false),
|
||||
_plugin(NULL)
|
||||
{
|
||||
}
|
||||
|
@ -99,125 +99,125 @@ QString CPluginSpec::errorString() const
|
|||
bool CPluginSpec::setFileName(const QString &fileName)
|
||||
{
|
||||
_name
|
||||
= _version
|
||||
= _vendor
|
||||
= _description
|
||||
= _location
|
||||
= _filePath
|
||||
= _fileName
|
||||
= "";
|
||||
_state = State::Invalid;
|
||||
_hasError = false;
|
||||
_errorString = "";
|
||||
QFile file(fileName);
|
||||
if (!file.exists())
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "File does not exist: %1").arg(file.fileName()));
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Could not open file for read: %1").arg(file.fileName()));
|
||||
= _version
|
||||
= _vendor
|
||||
= _description
|
||||
= _location
|
||||
= _filePath
|
||||
= _fileName
|
||||
= "";
|
||||
_state = State::Invalid;
|
||||
_hasError = false;
|
||||
_errorString = "";
|
||||
QFile file(fileName);
|
||||
if (!file.exists())
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "File does not exist: %1").arg(file.fileName()));
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Could not open file for read: %1").arg(file.fileName()));
|
||||
|
||||
QFileInfo fileInfo(file);
|
||||
_location = fileInfo.absolutePath();
|
||||
_filePath = fileInfo.absoluteFilePath();
|
||||
_location = fileInfo.absolutePath();
|
||||
_filePath = fileInfo.absoluteFilePath();
|
||||
_fileName = fileInfo.fileName();
|
||||
|
||||
_state = State::Read;
|
||||
return true;
|
||||
_state = State::Read;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CPluginSpec::loadLibrary()
|
||||
{
|
||||
if (_hasError)
|
||||
return false;
|
||||
if (_state != State::Read)
|
||||
if (_state != State::Read)
|
||||
{
|
||||
if (_state == State::Loaded)
|
||||
return true;
|
||||
if (_state == State::Loaded)
|
||||
return true;
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Loading the library failed because state != Resolved"));
|
||||
}
|
||||
QString libName = QString("%1/%2").arg(_location).arg(_fileName);
|
||||
}
|
||||
QString libName = QString("%1/%2").arg(_location).arg(_fileName);
|
||||
|
||||
QPluginLoader loader(libName);
|
||||
if (!loader.load())
|
||||
QPluginLoader loader(libName);
|
||||
if (!loader.load())
|
||||
return reportError(libName + QString::fromLatin1(": ") + loader.errorString());
|
||||
|
||||
IPlugin *pluginObject = qobject_cast<IPlugin*>(loader.instance());
|
||||
if (!pluginObject)
|
||||
IPlugin *pluginObject = qobject_cast<IPlugin*>(loader.instance());
|
||||
if (!pluginObject)
|
||||
{
|
||||
loader.unload();
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Plugin is not valid (does not derive from IPlugin)"));
|
||||
}
|
||||
}
|
||||
|
||||
_name = pluginObject->name();
|
||||
_version = pluginObject->version();
|
||||
_vendor = pluginObject->vendor();
|
||||
_description = pluginObject->description();
|
||||
|
||||
_state = State::Loaded;
|
||||
_plugin = pluginObject;
|
||||
return true;
|
||||
_state = State::Loaded;
|
||||
_plugin = pluginObject;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CPluginSpec::initializePlugin()
|
||||
{
|
||||
if (_hasError)
|
||||
return false;
|
||||
if (_state != State::Loaded)
|
||||
return false;
|
||||
if (_state != State::Loaded)
|
||||
{
|
||||
if (_state == State::Initialized)
|
||||
return true;
|
||||
if (_state == State::Initialized)
|
||||
return true;
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Initializing the plugin failed because state != Loaded)"));
|
||||
}
|
||||
if (!_plugin)
|
||||
}
|
||||
if (!_plugin)
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Internal error: have no plugin instance to initialize"));
|
||||
|
||||
QString err;
|
||||
if (!_plugin->initialize(_pluginManager, &err))
|
||||
if (!_plugin->initialize(_pluginManager, &err))
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Plugin initialization failed: %1").arg(err));
|
||||
|
||||
_state = State::Initialized;
|
||||
return true;
|
||||
_state = State::Initialized;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CPluginSpec::initializeExtensions()
|
||||
{
|
||||
if (_hasError)
|
||||
return false;
|
||||
if (_state != State::Initialized)
|
||||
if (_state != State::Initialized)
|
||||
{
|
||||
if (_state == State::Running)
|
||||
return true;
|
||||
if (_state == State::Running)
|
||||
return true;
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Cannot perform extensionsInitialized because state != Initialized"));
|
||||
}
|
||||
if (!_plugin)
|
||||
}
|
||||
if (!_plugin)
|
||||
return reportError(QCoreApplication::translate("CPluginSpec", "Internal error: have no plugin instance to perform extensionsInitialized"));
|
||||
|
||||
_plugin->extensionsInitialized();
|
||||
_state = State::Running;
|
||||
return true;
|
||||
_state = State::Running;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPluginSpec::stop()
|
||||
{
|
||||
if (!_plugin)
|
||||
return;
|
||||
_plugin->shutdown();
|
||||
_state = State::Stopped;
|
||||
_plugin->shutdown();
|
||||
_state = State::Stopped;
|
||||
}
|
||||
|
||||
void CPluginSpec::kill()
|
||||
{
|
||||
if (!_plugin)
|
||||
return;
|
||||
delete _plugin;
|
||||
_plugin = NULL;
|
||||
_state = State::Deleted;
|
||||
delete _plugin;
|
||||
_plugin = NULL;
|
||||
_state = State::Deleted;
|
||||
}
|
||||
|
||||
bool CPluginSpec::reportError(const QString &err)
|
||||
{
|
||||
_errorString = err;
|
||||
_hasError = true;
|
||||
return false;
|
||||
_errorString = err;
|
||||
_hasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace NLQT
|
|
@ -56,7 +56,7 @@ public:
|
|||
QString filePath() const;
|
||||
QString fileName() const;
|
||||
|
||||
IPlugin *plugin() const;
|
||||
IPlugin *plugin() const;
|
||||
|
||||
// state
|
||||
int getState() const;
|
||||
|
@ -68,15 +68,15 @@ private:
|
|||
|
||||
bool setFileName(const QString &fileName);
|
||||
bool loadLibrary();
|
||||
bool initializePlugin();
|
||||
bool initializeExtensions();
|
||||
void stop();
|
||||
void kill();
|
||||
bool initializePlugin();
|
||||
bool initializeExtensions();
|
||||
void stop();
|
||||
void kill();
|
||||
|
||||
bool reportError(const QString &err);
|
||||
|
||||
QString _location;
|
||||
QString _filePath;
|
||||
QString _filePath;
|
||||
QString _fileName;
|
||||
|
||||
QString _name;
|
||||
|
@ -85,8 +85,8 @@ private:
|
|||
QString _description;
|
||||
|
||||
int _state;
|
||||
bool _hasError;
|
||||
QString _errorString;
|
||||
bool _hasError;
|
||||
QString _errorString;
|
||||
|
||||
IPlugin *_plugin;
|
||||
IPluginManager *_pluginManager;
|
||||
|
|
Loading…
Reference in a new issue