Changed: #1193 Code cleanup.

This commit is contained in:
dnk-88 2010-11-29 17:36:12 +02:00
parent 68891726db
commit 376bc221df
6 changed files with 356 additions and 356 deletions

View file

@ -21,13 +21,13 @@
#define IPLUGIN_H #define IPLUGIN_H
#include <QtPlugin> #include <QtPlugin>
#include <QtCore/QString> #include <QtCore/QString>
#include "iplugin_manager.h" #include "iplugin_manager.h"
namespace NLQT namespace NLQT
{ {
class IPlugin class IPlugin
{ {
public: public:
@ -37,9 +37,9 @@ public:
virtual void extensionsInitialized() = 0; virtual void extensionsInitialized() = 0;
virtual void shutdown() { } virtual void shutdown() { }
virtual QString name() const = 0; virtual QString name() const = 0;
virtual QString version() const = 0; virtual QString version() const = 0;
virtual QString vendor() const = 0; virtual QString vendor() const = 0;
virtual QString description() const = 0; virtual QString description() const = 0;
}; };

View file

@ -39,13 +39,13 @@ public:
virtual void addObject(QObject *obj) = 0; virtual void addObject(QObject *obj) = 0;
virtual void removeObject(QObject *obj) = 0; virtual void removeObject(QObject *obj) = 0;
virtual QList<QObject *> allObjects() const = 0; virtual QList<QObject *> allObjects() const = 0;
// Plugin operations // Plugin operations
virtual void loadPlugins() = 0; virtual void loadPlugins() = 0;
virtual QStringList getPluginPaths() const = 0; virtual QStringList getPluginPaths() const = 0;
virtual void setPluginPaths(const QStringList &paths) = 0; virtual void setPluginPaths(const QStringList &paths) = 0;
virtual QList<NLQT::CPluginSpec *> plugins() const = 0; virtual QList<NLQT::CPluginSpec *> plugins() const = 0;
Q_SIGNALS: Q_SIGNALS:
void objectAdded(QObject *obj); void objectAdded(QObject *obj);
void aboutToRemoveObject(QObject *obj); void aboutToRemoveObject(QObject *obj);

View file

@ -1,129 +1,129 @@
/* /*
Object Viewer Qt Object Viewer Qt
Copyright (C) 2010 Dzmitry Kamiahin <dnk-88@tut.by> Copyright (C) 2010 Dzmitry Kamiahin <dnk-88@tut.by>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "plugin_manager.h"
#include <QtCore/QDir>
#include <nel/misc/debug.h> This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "plugin_spec.h" the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
namespace NLQT
{ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
CPluginManager::CPluginManager(QObject *parent): MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IPluginManager(parent) GNU General Public License for more details.
{
} You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
CPluginManager::~CPluginManager()
*/
#include "plugin_manager.h"
#include <QtCore/QDir>
#include <nel/misc/debug.h>
#include "plugin_spec.h"
namespace NLQT
{
CPluginManager::CPluginManager(QObject *parent):
IPluginManager(parent)
{
}
CPluginManager::~CPluginManager()
{ {
stopAll(); stopAll();
qDeleteAll(_pluginSpecs); qDeleteAll(_pluginSpecs);
} }
void CPluginManager::addObject(QObject *obj) void CPluginManager::addObject(QObject *obj)
{ {
QWriteLocker lock(&_lock); QWriteLocker lock(&_lock);
if (obj == 0) if (obj == 0)
{ {
nlwarning("trying to add null object"); nlwarning("trying to add null object");
return; return;
} }
if (_allObjects.contains(obj)) if (_allObjects.contains(obj))
{ {
nlwarning("trying to add duplicate object"); nlwarning("trying to add duplicate object");
return; return;
} }
nlinfo(QString("addObject:" + obj->objectName()).toStdString().c_str()); nlinfo(QString("addObject:" + obj->objectName()).toStdString().c_str());
_allObjects.append(obj); _allObjects.append(obj);
Q_EMIT objectAdded(obj); Q_EMIT objectAdded(obj);
} }
void CPluginManager::removeObject(QObject *obj) void CPluginManager::removeObject(QObject *obj)
{ {
if (obj == 0) if (obj == 0)
{ {
nlwarning("trying to remove null object"); 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()); nlinfo(QString("object not in list:" + obj->objectName()).toStdString().c_str());
return; return;
} }
nlinfo(QString("removeObject:" + obj->objectName()).toStdString().c_str()); nlinfo(QString("removeObject:" + obj->objectName()).toStdString().c_str());
Q_EMIT aboutToRemoveObject(obj); Q_EMIT aboutToRemoveObject(obj);
QWriteLocker lock(&_lock); QWriteLocker lock(&_lock);
_allObjects.removeAll(obj); _allObjects.removeAll(obj);
} }
QList<QObject *> CPluginManager::allObjects() const QList<QObject *> CPluginManager::allObjects() const
{
return _allObjects;
}
void CPluginManager::loadPlugins()
{ {
Q_FOREACH (CPluginSpec *spec, _pluginSpecs) return _allObjects;
setPluginState(spec, State::Loaded); }
Q_FOREACH (CPluginSpec *spec, _pluginSpecs) void CPluginManager::loadPlugins()
setPluginState(spec, State::Initialized); {
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
setPluginState(spec, State::Loaded);
Q_FOREACH (CPluginSpec *spec, _pluginSpecs) Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
setPluginState(spec, State::Running); setPluginState(spec, State::Initialized);
Q_EMIT pluginsChanged(); Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
} setPluginState(spec, State::Running);
QStringList CPluginManager::getPluginPaths() const Q_EMIT pluginsChanged();
{ }
return _pluginPaths;
} QStringList CPluginManager::getPluginPaths() const
{
void CPluginManager::setPluginPaths(const QStringList &paths) return _pluginPaths;
{ }
_pluginPaths = paths;
readPluginPaths(); void CPluginManager::setPluginPaths(const QStringList &paths)
} {
_pluginPaths = paths;
QList<CPluginSpec *> CPluginManager::plugins() const readPluginPaths();
{ }
return _pluginSpecs;
} QList<CPluginSpec *> CPluginManager::plugins() const
{
void CPluginManager::readPluginPaths() return _pluginSpecs;
}
void CPluginManager::readPluginPaths()
{ {
qDeleteAll(_pluginSpecs); qDeleteAll(_pluginSpecs);
_pluginSpecs.clear(); _pluginSpecs.clear();
QStringList pluginsList; QStringList pluginsList;
QStringList searchPaths = _pluginPaths; QStringList searchPaths = _pluginPaths;
while (!searchPaths.isEmpty()) while (!searchPaths.isEmpty())
{ {
const QDir dir(searchPaths.takeFirst()); const QDir dir(searchPaths.takeFirst());
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.dll"), QDir::Files); const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.dll"), QDir::Files);
#elif defined(Q_OS_MAC) #elif defined(Q_OS_MAC)
@ -132,62 +132,62 @@ void CPluginManager::readPluginPaths()
const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.so"), QDir::Files); const QFileInfoList files = dir.entryInfoList(QStringList() << QString("*.so"), QDir::Files);
#endif #endif
Q_FOREACH (const QFileInfo &file, files) Q_FOREACH (const QFileInfo &file, files)
pluginsList << file.absoluteFilePath(); pluginsList << file.absoluteFilePath();
const QFileInfoList dirs = dir.entryInfoList(QDir::Dirs|QDir::NoDotAndDotDot); const QFileInfoList dirs = dir.entryInfoList(QDir::Dirs|QDir::NoDotAndDotDot);
Q_FOREACH (const QFileInfo &subdir, dirs) 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)) if (spec->setFileName(pluginFile))
_pluginSpecs.append(spec); _pluginSpecs.append(spec);
else else
delete spec; delete spec;
} }
Q_EMIT pluginsChanged(); Q_EMIT pluginsChanged();
} }
CPluginSpec *CPluginManager::pluginByName(const QString &name) const CPluginSpec *CPluginManager::pluginByName(const QString &name) const
{ {
Q_FOREACH (CPluginSpec *spec, _pluginSpecs) Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
if (spec->name() == name) if (spec->name() == name)
return spec; return spec;
return 0; return 0;
} }
void CPluginManager::setPluginState(CPluginSpec *spec, int destState) void CPluginManager::setPluginState(CPluginSpec *spec, int destState)
{ {
if (spec->hasError()) if (spec->hasError())
return; return;
if (destState == State::Running) if (destState == State::Running)
{ {
spec->initializeExtensions(); spec->initializeExtensions();
return; return;
} }
else if (destState == State::Deleted) else if (destState == State::Deleted)
{ {
spec->kill(); spec->kill();
return; return;
} }
if (destState == State::Loaded) if (destState == State::Loaded)
spec->loadLibrary(); spec->loadLibrary();
else if (destState == State::Initialized) else if (destState == State::Initialized)
spec->initializePlugin(); spec->initializePlugin();
else if (destState == State::Stopped) else if (destState == State::Stopped)
spec->stop(); spec->stop();
} }
void CPluginManager::stopAll() void CPluginManager::stopAll()
{ {
Q_FOREACH (CPluginSpec *spec, _pluginSpecs) Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
setPluginState(spec, State::Stopped); setPluginState(spec, State::Stopped);
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
setPluginState(spec, State::Deleted);
}
Q_FOREACH (CPluginSpec *spec, _pluginSpecs)
setPluginState(spec, State::Deleted);
}
}; // namespace NLQT }; // namespace NLQT

View file

@ -33,22 +33,22 @@ class IPlugin;
class CPluginManager : public IPluginManager class CPluginManager : public IPluginManager
{ {
Q_OBJECT Q_OBJECT
public: public:
CPluginManager(QObject *parent = 0); CPluginManager(QObject *parent = 0);
~CPluginManager(); ~CPluginManager();
// Object pool operations // Object pool operations
virtual void addObject(QObject *obj); virtual void addObject(QObject *obj);
virtual void removeObject(QObject *obj); virtual void removeObject(QObject *obj);
virtual QList<QObject *> allObjects() const; virtual QList<QObject *> allObjects() const;
// Plugin operations // Plugin operations
virtual void loadPlugins(); virtual void loadPlugins();
virtual QStringList getPluginPaths() const; virtual QStringList getPluginPaths() const;
virtual void setPluginPaths(const QStringList &paths); virtual void setPluginPaths(const QStringList &paths);
virtual QList<CPluginSpec *> plugins() const; virtual QList<CPluginSpec *> plugins() const;
CPluginSpec *pluginByName(const QString &name) const; CPluginSpec *pluginByName(const QString &name) const;
@ -59,10 +59,10 @@ private:
mutable QReadWriteLock _lock; mutable QReadWriteLock _lock;
QList<CPluginSpec *> _pluginSpecs; QList<CPluginSpec *> _pluginSpecs;
QStringList _pluginPaths; QStringList _pluginPaths;
QList<QObject *> _allObjects; QList<QObject *> _allObjects;
}; // class CPluginManager }; // class CPluginManager
} // namespace NLQT } // namespace NLQT

View file

@ -1,46 +1,46 @@
/* /*
Object Viewer Qt Object Viewer Qt
Copyright (C) 2010 Dzmitry Kamiahin <dnk-88@tut.by> Copyright (C) 2010 Dzmitry Kamiahin <dnk-88@tut.by>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "plugin_spec.h" #include "plugin_spec.h"
#include <QtCore/QFile> #include <QtCore/QFile>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QPluginLoader> #include <QtCore/QPluginLoader>
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include "iplugin.h" #include "iplugin.h"
#include "iplugin_manager.h" #include "iplugin_manager.h"
namespace NLQT namespace NLQT
{ {
CPluginSpec::CPluginSpec(): CPluginSpec::CPluginSpec():
_state(State::Invalid), _state(State::Invalid),
_hasError(false), _hasError(false),
_plugin(NULL) _plugin(NULL)
{ {
} }
CPluginSpec::~CPluginSpec() CPluginSpec::~CPluginSpec()
{ {
} }
QString CPluginSpec::name() const QString CPluginSpec::name() const
{ {
return _name; return _name;
@ -52,172 +52,172 @@ QString CPluginSpec::version() const
} }
QString CPluginSpec::vendor() const QString CPluginSpec::vendor() const
{ {
return _vendor; return _vendor;
} }
QString CPluginSpec::description() const QString CPluginSpec::description() const
{ {
return _description; return _description;
} }
QString CPluginSpec::location() const QString CPluginSpec::location() const
{ {
return _location; return _location;
} }
QString CPluginSpec::filePath() const QString CPluginSpec::filePath() const
{ {
return _filePath; return _filePath;
} }
QString CPluginSpec::fileName() const QString CPluginSpec::fileName() const
{ {
return _fileName; return _fileName;
} }
IPlugin* CPluginSpec::plugin() const IPlugin* CPluginSpec::plugin() const
{ {
return _plugin; return _plugin;
} }
int CPluginSpec::getState() const int CPluginSpec::getState() const
{ {
return _state; return _state;
} }
bool CPluginSpec::hasError() const bool CPluginSpec::hasError() const
{ {
return _hasError; return _hasError;
} }
QString CPluginSpec::errorString() const QString CPluginSpec::errorString() const
{ {
return _errorString; return _errorString;
} }
bool CPluginSpec::setFileName(const QString &fileName) bool CPluginSpec::setFileName(const QString &fileName)
{ {
_name _name
= _version = _version
= _vendor = _vendor
= _description = _description
= _location = _location
= _filePath = _filePath
= _fileName = _fileName
= ""; = "";
_state = State::Invalid; _state = State::Invalid;
_hasError = false; _hasError = false;
_errorString = ""; _errorString = "";
QFile file(fileName); QFile file(fileName);
if (!file.exists()) if (!file.exists())
return reportError(QCoreApplication::translate("CPluginSpec", "File does not exist: %1").arg(file.fileName())); return reportError(QCoreApplication::translate("CPluginSpec", "File does not exist: %1").arg(file.fileName()));
if (!file.open(QIODevice::ReadOnly)) if (!file.open(QIODevice::ReadOnly))
return reportError(QCoreApplication::translate("CPluginSpec", "Could not open file for read: %1").arg(file.fileName())); return reportError(QCoreApplication::translate("CPluginSpec", "Could not open file for read: %1").arg(file.fileName()));
QFileInfo fileInfo(file); QFileInfo fileInfo(file);
_location = fileInfo.absolutePath(); _location = fileInfo.absolutePath();
_filePath = fileInfo.absoluteFilePath(); _filePath = fileInfo.absoluteFilePath();
_fileName = fileInfo.fileName(); _fileName = fileInfo.fileName();
_state = State::Read; _state = State::Read;
return true; return true;
} }
bool CPluginSpec::loadLibrary() bool CPluginSpec::loadLibrary()
{ {
if (_hasError) if (_hasError)
return false; return false;
if (_state != State::Read) if (_state != State::Read)
{ {
if (_state == State::Loaded) if (_state == State::Loaded)
return true; return true;
return reportError(QCoreApplication::translate("CPluginSpec", "Loading the library failed because state != Resolved")); 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); QPluginLoader loader(libName);
if (!loader.load()) if (!loader.load())
return reportError(libName + QString::fromLatin1(": ") + loader.errorString()); return reportError(libName + QString::fromLatin1(": ") + loader.errorString());
IPlugin *pluginObject = qobject_cast<IPlugin*>(loader.instance()); IPlugin *pluginObject = qobject_cast<IPlugin*>(loader.instance());
if (!pluginObject) if (!pluginObject)
{ {
loader.unload(); loader.unload();
return reportError(QCoreApplication::translate("CPluginSpec", "Plugin is not valid (does not derive from IPlugin)")); return reportError(QCoreApplication::translate("CPluginSpec", "Plugin is not valid (does not derive from IPlugin)"));
} }
_name = pluginObject->name(); _name = pluginObject->name();
_version = pluginObject->version(); _version = pluginObject->version();
_vendor = pluginObject->vendor(); _vendor = pluginObject->vendor();
_description = pluginObject->description(); _description = pluginObject->description();
_state = State::Loaded; _state = State::Loaded;
_plugin = pluginObject; _plugin = pluginObject;
return true; return true;
} }
bool CPluginSpec::initializePlugin() bool CPluginSpec::initializePlugin()
{ {
if (_hasError) if (_hasError)
return false; return false;
if (_state != State::Loaded) if (_state != State::Loaded)
{ {
if (_state == State::Initialized) if (_state == State::Initialized)
return true; return true;
return reportError(QCoreApplication::translate("CPluginSpec", "Initializing the plugin failed because state != Loaded)")); 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")); return reportError(QCoreApplication::translate("CPluginSpec", "Internal error: have no plugin instance to initialize"));
QString err; QString err;
if (!_plugin->initialize(_pluginManager, &err)) if (!_plugin->initialize(_pluginManager, &err))
return reportError(QCoreApplication::translate("CPluginSpec", "Plugin initialization failed: %1").arg(err)); return reportError(QCoreApplication::translate("CPluginSpec", "Plugin initialization failed: %1").arg(err));
_state = State::Initialized; _state = State::Initialized;
return true; return true;
} }
bool CPluginSpec::initializeExtensions() bool CPluginSpec::initializeExtensions()
{ {
if (_hasError) if (_hasError)
return false; return false;
if (_state != State::Initialized) if (_state != State::Initialized)
{ {
if (_state == State::Running) if (_state == State::Running)
return true; return true;
return reportError(QCoreApplication::translate("CPluginSpec", "Cannot perform extensionsInitialized because state != Initialized")); 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")); return reportError(QCoreApplication::translate("CPluginSpec", "Internal error: have no plugin instance to perform extensionsInitialized"));
_plugin->extensionsInitialized(); _plugin->extensionsInitialized();
_state = State::Running; _state = State::Running;
return true; return true;
} }
void CPluginSpec::stop() void CPluginSpec::stop()
{ {
if (!_plugin) if (!_plugin)
return; return;
_plugin->shutdown(); _plugin->shutdown();
_state = State::Stopped; _state = State::Stopped;
} }
void CPluginSpec::kill() void CPluginSpec::kill()
{ {
if (!_plugin) if (!_plugin)
return; return;
delete _plugin; delete _plugin;
_plugin = NULL; _plugin = NULL;
_state = State::Deleted; _state = State::Deleted;
} }
bool CPluginSpec::reportError(const QString &err) bool CPluginSpec::reportError(const QString &err)
{ {
_errorString = err; _errorString = err;
_hasError = true; _hasError = true;
return false; return false;
} }
} // namespace NLQT } // namespace NLQT

View file

@ -26,37 +26,37 @@
namespace NLQT namespace NLQT
{ {
class IPlugin; class IPlugin;
class IPluginManager; class IPluginManager;
struct State struct State
{ {
enum List enum List
{ {
Invalid = 1, Invalid = 1,
Read, Read,
Loaded, Loaded,
Initialized, Initialized,
Running, Running,
Stopped, Stopped,
Deleted Deleted
}; };
}; };
class CPluginSpec class CPluginSpec
{ {
public: public:
~CPluginSpec(); ~CPluginSpec();
QString name() const; QString name() const;
QString version() const; QString version() const;
QString vendor() const; QString vendor() const;
QString description() const; QString description() const;
QString location() const; QString location() const;
QString filePath() const; QString filePath() const;
QString fileName() const; QString fileName() const;
IPlugin *plugin() const; IPlugin *plugin() const;
// state // state
int getState() const; int getState() const;
@ -66,27 +66,27 @@ public:
private: private:
CPluginSpec(); CPluginSpec();
bool setFileName(const QString &fileName); bool setFileName(const QString &fileName);
bool loadLibrary(); bool loadLibrary();
bool initializePlugin(); bool initializePlugin();
bool initializeExtensions(); bool initializeExtensions();
void stop(); void stop();
void kill(); void kill();
bool reportError(const QString &err); bool reportError(const QString &err);
QString _location; QString _location;
QString _filePath; QString _filePath;
QString _fileName; QString _fileName;
QString _name; QString _name;
QString _version; QString _version;
QString _vendor; QString _vendor;
QString _description; QString _description;
int _state; int _state;
bool _hasError; bool _hasError;
QString _errorString; QString _errorString;
IPlugin *_plugin; IPlugin *_plugin;
IPluginManager *_pluginManager; IPluginManager *_pluginManager;