pluginloader.cpp
560 lines
| 13.7 KiB
| text/x-c
|
CppLexer
Jeandet Alexis
|
r0 | #include "pluginloader.h" | ||
#include <QDir> | ||||
#include <QFile> | ||||
#include <QFileInfoList> | ||||
#include <QFileInfo> | ||||
#include <QString> | ||||
#include <QStringList> | ||||
#include <QLabel> | ||||
#include <QSettings> | ||||
#include <QApplication> | ||||
#include <QCoreApplication> | ||||
#include <socexplorerengine.h> | ||||
#ifdef SOCEXPLORER_CUSTOM_PLUGIN_LOADER | ||||
#include "unix/unixpluginloader.h" | ||||
#endif | ||||
pluginloader* pluginloader::_self = NULL; | ||||
PluginsCache* pluginloader::_cache = NULL; | ||||
QStringList* pluginloader::_folderList = NULL; | ||||
pluginloader::pluginloader() | ||||
{ | ||||
_cache = new PluginsCache(); | ||||
_folderList = new QStringList(); | ||||
Jeandet Alexis
|
r65 | _folderList->append(SocExplorerEngine::pluginFolders()); | ||
Jeandet Alexis
|
r0 | scanFolders(); | ||
} | ||||
QStringList pluginloader::readFoldersList(const QStringList confFiles) | ||||
{ | ||||
QDir testDir; | ||||
QStringList folders; | ||||
QFile confFile; | ||||
for(int i=0;i<confFiles.count();i++) | ||||
{ | ||||
confFile.setFileName(confFiles.at(i)); | ||||
if(confFile.exists()) | ||||
{ | ||||
if (confFile.open(QIODevice::ReadOnly | QIODevice::Text)) | ||||
{ | ||||
QTextStream in(&confFile); | ||||
QString line = in.readLine(); | ||||
while (!line.isNull()) | ||||
{ | ||||
testDir.setPath(line); | ||||
if(testDir.exists()) | ||||
{ | ||||
if(!folders.contains(line)) | ||||
folders << line; | ||||
} | ||||
line = in.readLine(); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
return folders; | ||||
} | ||||
void pluginloader::scanFolders() | ||||
{ | ||||
QDir dir; | ||||
QStringList filters; | ||||
filters <<"*.so"<< "*.dll"; | ||||
_cache->flush(); | ||||
for(int d=0;d<_folderList->count();d++) | ||||
{ | ||||
dir.setPath(_folderList->at(d)); | ||||
dir.setFilter(QDir::Files); | ||||
dir.setNameFilters(filters); | ||||
QFileInfoList list = dir.entryInfoList(); | ||||
for (int i = 0; i < list.size(); ++i) | ||||
{ | ||||
QFileInfo fileInfo = list.at(i); | ||||
if(checklibrary(fileInfo.filePath())!=0) | ||||
{ | ||||
_cache->append(fileInfo.fileName(),fileInfo.path(),_getlibName(fileInfo.filePath()),_getlibPID(fileInfo.filePath()),_getlibPID(fileInfo.filePath())); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
int pluginloader::p_checklibraryQlib(const QString fileName) | ||||
{ | ||||
QLibrary* lib = new QLibrary; | ||||
lib->setFileName(fileName); | ||||
lib->setLoadHints(QLibrary::PreventUnloadHint); | ||||
lib->load(); | ||||
if(!lib->isLoaded()) | ||||
{ | ||||
qDebug()<<lib->errorString(); | ||||
lib->~QLibrary(); | ||||
lib = new QLibrary(fileName); | ||||
lib->load(); | ||||
} | ||||
delete lib; | ||||
if(QLibrary::resolve(fileName,"socexplorerpluginCreateObject")) | ||||
{ | ||||
if(QLibrary::resolve(fileName,"socexplorerpluginpid")) | ||||
{ | ||||
if(QLibrary::resolve(fileName,"socexplorerpluginvid")) | ||||
{ | ||||
if(QLibrary::resolve(fileName,"socexplorerpluginVersion")) | ||||
{ | ||||
if(QLibrary::resolve(fileName,"socexplorerpluginAuthor")) | ||||
{ | ||||
if(QLibrary::resolve(fileName,"socexplorerpluginDescription")) | ||||
{ | ||||
return 1; | ||||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||
return 0; | ||||
} | ||||
int pluginloader::p_checklibraryCustom(const QString fileName) | ||||
{ | ||||
#ifdef SOCEXPLORER_CUSTOM_PLUGIN_LOADER | ||||
unixPluginLoader lib(fileName); | ||||
if(NULL!=lib.resolve("socexplorerpluginCreateObject")) | ||||
{ | ||||
r5 | if(NULL!=lib.resolve("socexplorerpluginpid")) | |||
Jeandet Alexis
|
r0 | { | ||
if(NULL!=lib.resolve("socexplorerpluginvid")) | ||||
{ | ||||
if(NULL!=lib.resolve("socexplorerpluginVersion")) | ||||
{ | ||||
if(NULL!=lib.resolve("socexplorerpluginAuthor")) | ||||
{ | ||||
if(NULL!=lib.resolve("socexplorerpluginDescription")) | ||||
{ | ||||
return 1; | ||||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||
} | ||||
#endif | ||||
return 0; | ||||
} | ||||
socexplorerplugin *pluginloader::p_newsocexplorerpluginQlib(const QString Name) | ||||
{ | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
socexplorerpluginCreateObjectT newDrvr = NULL; | ||||
newDrvr=(socexplorerpluginCreateObjectT)lib->resolve("socexplorerpluginCreateObject"); | ||||
if(newDrvr==NULL) | ||||
{ | ||||
return NULL; | ||||
} | ||||
return (socexplorerplugin*) newDrvr(); | ||||
} | ||||
socexplorerplugin *pluginloader::p_newsocexplorerpluginCustom(const QString Name) | ||||
{ | ||||
#ifdef SOCEXPLORER_CUSTOM_PLUGIN_LOADER | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return NULL; | ||||
unixPluginLoader lib(*libfile); | ||||
delete libfile; | ||||
socexplorerpluginCreateObjectT newDrvr = NULL; | ||||
newDrvr=(socexplorerpluginCreateObjectT)lib.resolve("socexplorerpluginCreateObject"); | ||||
if(newDrvr==NULL) | ||||
{ | ||||
return NULL; | ||||
} | ||||
return (socexplorerplugin*) newDrvr(); | ||||
#endif | ||||
} | ||||
QList<PluginsCacheItem*> pluginloader::listAvailiables(bool rescan) | ||||
{ | ||||
if(_self==NULL) | ||||
{ | ||||
init(); | ||||
return _cache->listDrivers(); | ||||
} | ||||
if(rescan) | ||||
{ | ||||
scanFolders(); | ||||
} | ||||
return _cache->listDrivers(); | ||||
} | ||||
void pluginloader::init() | ||||
{ | ||||
if(_self==NULL) | ||||
{ | ||||
_self=new pluginloader(); | ||||
} | ||||
} | ||||
pluginloader* pluginloader::self() | ||||
{ | ||||
if(_self==NULL) | ||||
{ | ||||
init(); | ||||
} | ||||
return _self; | ||||
} | ||||
bool pluginloader::isvalid(QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return false; | ||||
else | ||||
{ | ||||
delete libfile; | ||||
return true; | ||||
} | ||||
} | ||||
int pluginloader::checklibrary(const QString fileName) | ||||
{ | ||||
#ifdef SOCEXPLORER_CUSTOM_PLUGIN_LOADER | ||||
return _self->p_checklibraryCustom(fileName); | ||||
#else | ||||
return _self->p_checklibraryQlib(fileName); | ||||
#endif | ||||
} | ||||
socexplorerplugin* pluginloader::newsocexplorerplugin(const QString Name) | ||||
{ | ||||
#ifdef SOCEXPLORER_CUSTOM_PLUGIN_LOADER | ||||
return _self->p_newsocexplorerpluginCustom(Name); | ||||
#else | ||||
return _self->p_newsocexplorerpluginQlib(Name); | ||||
#endif | ||||
} | ||||
QString pluginloader::getlibTypeStr(QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginTypeT plugintype = (socexplorerpluginTypeT)lib->resolve("socexplorerpluginType"); | ||||
if(plugintype!=NULL) | ||||
{ | ||||
pluginT type = plugintype(); | ||||
switch(type) | ||||
{ | ||||
case ComDriverT: | ||||
////lib->unload(); | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Comunaication Driver Plugin."); | ||||
break; | ||||
case PerifDriverT: | ||||
////lib->unload(); | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Periferial Driver Plugin."); | ||||
break; | ||||
default: | ||||
////lib->unload(); | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Unknow Plugin."); | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Can't load Plugin."); | ||||
} | ||||
pluginT pluginloader::getlibType(QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return (pluginT)NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginTypeT plugintype = (socexplorerpluginTypeT)lib->resolve("socexplorerpluginType"); | ||||
if(plugintype!=NULL) | ||||
{ | ||||
return plugintype(); | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return -1; | ||||
} | ||||
QString pluginloader::getlibVersion(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginVersionT pluginversion = (socexplorerpluginVersionT)lib->resolve("socexplorerpluginVersion"); | ||||
if(pluginversion!=NULL) | ||||
{ | ||||
QString version = pluginversion(); | ||||
////lib->unload(); | ||||
lib->~QLibrary(); | ||||
return version; | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Can't load Plugin."); | ||||
} | ||||
QString pluginloader::getlibPIDstr(const QString Name) | ||||
{ | ||||
return QString("0x" + QString::number(pluginloader::getlibPID(Name) , 16)); | ||||
} | ||||
QString pluginloader::getlibVIDstr(const QString Name) | ||||
{ | ||||
return QString("0x" + QString::number(pluginloader::getlibVID(Name) , 16)); | ||||
} | ||||
int pluginloader::libcanbechild(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return (int)NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerplugincanbechildT canbechild = (socexplorerplugincanbechildT)lib->resolve("socexplorerplugincanbechild"); | ||||
if(canbechild!=NULL) | ||||
{ | ||||
int value = canbechild(); | ||||
////lib->unload(); | ||||
//lib->~QLibrary(); | ||||
return value; | ||||
} | ||||
} | ||||
//lib->~QLibrary(); | ||||
return 0; | ||||
} | ||||
int pluginloader::libcanberoot(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return (int)NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerplugincanberootT canberoot = (socexplorerplugincanberootT)lib->resolve("socexplorerplugincanberoot"); | ||||
if(canberoot!=NULL) | ||||
{ | ||||
int value = canberoot(); | ||||
////lib->unload(); | ||||
//lib->~QLibrary(); | ||||
return value; | ||||
} | ||||
} | ||||
delete lib; | ||||
//lib->~QLibrary(); | ||||
return 0; | ||||
} | ||||
int pluginloader::getlibVID(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return 0; | ||||
QString file(*libfile); | ||||
delete libfile; | ||||
return _getlibVID(file); | ||||
} | ||||
int pluginloader::getlibPID(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return 0; | ||||
QString file(*libfile); | ||||
delete libfile; | ||||
return _getlibPID(file); | ||||
} | ||||
QString pluginloader::getlibAuthor(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginAuthorT pluginauthor = (socexplorerpluginAuthorT)lib->resolve("socexplorerpluginAuthor"); | ||||
if(pluginauthor!=NULL) | ||||
{ | ||||
QString author = pluginauthor(); | ||||
////lib->unload(); | ||||
lib->~QLibrary(); | ||||
return author; | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Can't load Plugin."); | ||||
} | ||||
QString pluginloader::getlibName(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return QString(""); | ||||
QString file(*libfile); | ||||
delete libfile; | ||||
return _getlibName(file); | ||||
} | ||||
QString pluginloader::getlibDescription(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
QString* libfile= _cacheLookup(Name); | ||||
if(libfile==NULL)return NULL; | ||||
QLibrary* lib = new QLibrary(*libfile); | ||||
delete libfile; | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginDescriptionT plugindescription = (socexplorerpluginDescriptionT)lib->resolve("socexplorerpluginDescription"); | ||||
if(plugindescription!=NULL) | ||||
{ | ||||
QString description = plugindescription(); | ||||
////lib->unload(); | ||||
lib->~QLibrary(); | ||||
return description; | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Can't load Plugin."); | ||||
} | ||||
QString pluginloader::getlibDir(const QString Name) | ||||
{ | ||||
if(_self==NULL)init(); | ||||
return *_cacheLookup(Name); | ||||
} | ||||
QString pluginloader::_getlibName(const QString fileName) | ||||
{ | ||||
QLibrary* lib = new QLibrary(fileName); | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginNameT pluginName = (socexplorerpluginAuthorT)lib->resolve("socexplorerpluginName"); | ||||
if(pluginName!=NULL) | ||||
{ | ||||
QString name = pluginName(); | ||||
//lib->unload(); | ||||
lib->~QLibrary(); | ||||
return name; | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return QObject::tr("Can't load Plugin."); | ||||
} | ||||
int pluginloader::_getlibPID(const QString fileName) | ||||
{ | ||||
QLibrary* lib = new QLibrary(fileName); | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginpidT pluginpid = (socexplorerpluginpidT)lib->resolve("socexplorerpluginpid"); | ||||
if(pluginpid!=NULL) | ||||
{ | ||||
int pid = pluginpid(); | ||||
//lib->unload(); | ||||
lib->~QLibrary(); | ||||
return pid; | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return 0; | ||||
} | ||||
int pluginloader::_getlibVID(const QString fileName) | ||||
{ | ||||
QLibrary* lib = new QLibrary(fileName); | ||||
lib->load(); | ||||
if(lib->isLoaded()) | ||||
{ | ||||
socexplorerpluginvidT pluginvid = (socexplorerpluginvidT)lib->resolve("socexplorerpluginvid"); | ||||
if(pluginvid!=NULL) | ||||
{ | ||||
int vid = pluginvid(); | ||||
//lib->unload(); | ||||
lib->~QLibrary(); | ||||
return vid; | ||||
} | ||||
} | ||||
lib->~QLibrary(); | ||||
return 0; | ||||
} | ||||
QString* pluginloader::_cacheLookup(const QString Name) | ||||
{ | ||||
QString* libfile= new QString(_cache->first(Name)); | ||||
if(!QFile::exists(*libfile)) | ||||
{ | ||||
scanFolders(); | ||||
*libfile = _cache->first(Name); | ||||
if(QFile::exists(*libfile))return libfile; | ||||
} | ||||
else | ||||
{ | ||||
return libfile; | ||||
} | ||||
delete libfile; | ||||
return NULL; | ||||
} | ||||
/*QString findlib(QString name) | ||||
{ | ||||
}*/ | ||||