logplus/HPluginManage/HPluginManage.cpp
2025-10-29 17:23:30 +08:00

178 lines
4.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "HPluginManage.h"
#include <QPluginLoader>
#include <QDebug>
#include <QCoreApplication>
#include <QDir>
#include <QtCore/QFileInfo>
//#include "CallManage.h"
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif // WIN32
//控件对象单例指针
HPluginManage *_pInstance = nullptr;
HPluginManage::HPluginManage()
{
//加载所有窗口控件.
#ifdef QT_DEBUG
loadPlugin("/customPlugind/");
#else
loadPlugin("/customPlugin/");
#endif // QT_DEBUG
//加载组件库组件
//loadPlugin("/customPlugin/");
}
HPluginManage::~HPluginManage()
{
}
HPluginManage *HPluginManage::getInstance()
{
if (_pInstance == nullptr)
{
_pInstance = new HPluginManage();
}
return _pInstance;
}
std::list<HPLUGIN_INFO *> HPluginManage::getPluginList()
{
return m_PluInfoList;
}
QList<QString> HPluginManage::scan(QString filePath)
{
int i = 0;
//用来临时存储文件路径
QString strFile;
//取文件路径并给QDir类型
QDir dir(filePath);
//链表,存储文件路径
QList<QString> fileList;
//文件信息
QFileInfo fileInfo;
#ifdef _WIN32
fileList << "*.dll";
#else
fileList << "*.so";
#endif
//过滤文件
dir.setNameFilters(fileList);
//列出所有符合条件的文件
QFileInfoList fileInfoList = dir.entryInfoList();
fileList.clear();
//注意fileList.at(0) = "*.so" ,从第一个开始才是所需文件完整路径
while (i < fileInfoList.length())
{
fileInfo = fileInfoList.at(i);
strFile = fileInfo.filePath();
fileList << strFile;
i++;
}
return fileList;
}
bool HPluginManage::loadPlugin(QString path)
{
if (path == "")
return false;
//获取plugins绝对路径.
QString pluPath = QCoreApplication::applicationDirPath() + path;
//获取目录下面的所有动态库(dll/.so)
//QStringList pluList = QvtObjectApi::getInstance()->scan(pluPath);
QStringList pluList = this->scan(pluPath);
//qDebug() << "gggg" << pluPath;
//加载目录下的所有控件插件.
foreach (QString item, pluList)
{
//加载插件
qDebug() << "plugin path:" << item;
QFileInfo info(item);
QPluginLoader *loder = new QPluginLoader(item);
loder->load();
if (loder->isLoaded())
{
HPluginInterface *pInterface = dynamic_cast<HPluginInterface *>(loder->instance());
if (pInterface != nullptr)
{
//将插件对象指针存储在QHash
qDebug() << "load plugin path:" << item << " success";
m_PluginMap.insert(pInterface->getPluName(), pInterface);
//emit CallManage::getInstance()->SigPluLoadInfo(pInterface->getPluName(), true);
}
else
{
QString error = QString("load plugin error %1 窗口插件加载失败").arg(item);
//QvtLogManage::getInstance()->qvtWriteLog(logData,Qvt_LEVEL_LOG_HIGH);
error += "错误原因:" + loder->errorString();
QString logData = error;
qDebug() << logData;
QString strPluginName = "";
if (pInterface != NULL)
{
strPluginName = pInterface->getPluName();
}
if (strPluginName == "")
strPluginName = info.fileName();
//emit CallManage::getInstance()->SigPluLoadInfo(strPluginName, false);
}
m_pluHash.push_back(loder);
}
else
{
HPluginInterface *pInterface = dynamic_cast<HPluginInterface *>(loder->instance());
//错误信息
QString error = QString("load plugin error %1 窗口插件加载失败!\t\t\t\t").arg(item);
error += "错误原因:" + loder->errorString();
QString logData = error;
qDebug() << logData;
//写入日志.
//QvtLogManage::getInstance()->qvtWriteLog(logData,Qvt_LEVEL_LOG_HIGH);
QString strPluginName = "";
if (pInterface != NULL)
{
strPluginName = pInterface->getPluName();
}
if (strPluginName == "")
strPluginName = info.fileName();
//emit CallManage::getInstance()->SigPluLoadInfo(strPluginName, false);
}
}
m_PluInfoList.clear();
QMap<QString, HPluginInterface *>::iterator pluItem = m_PluginMap.begin();
for (; pluItem != m_PluginMap.end(); ++pluItem)
{
HPLUGIN_INFO *plu_info = new HPLUGIN_INFO;
plu_info->pluName = pluItem.value()->getPluName();
plu_info->pluIcon = pluItem.value()->icon();
plu_info->pluClass = pluItem.value();
m_PluInfoList.push_back(plu_info);
}
return true;
}
bool HPluginManage::unloadPlugin()
{
foreach(QPluginLoader *pluItem, m_pluHash)
{
if (pluItem->isLoaded())
{
pluItem->unload();
}
}
return true;
}