From 55703a59eddf626d8e4101c3f62231deb0e906ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=20=E7=BF=94?= Date: Fri, 22 Dec 2017 10:20:47 +0800 Subject: [PATCH] fixed plugin error for Qt5 --- component/plugininterface.h | 2 +- shredmanager/shred.json | 1 + shredmanager/shredmanager.h | 6 +++--- src/main.cpp | 31 ++++++++++++++++++++++++------- src/pluginmanager.cpp | 17 ++++++++++++++++- src/pluginmanager.h | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 shredmanager/shred.json diff --git a/component/plugininterface.h b/component/plugininterface.h index f92b6cd..875c1de 100644 --- a/component/plugininterface.h +++ b/component/plugininterface.h @@ -35,7 +35,7 @@ public: }; //Q_DECLARE_INTERFACE定义在在qobject.h中,用来告诉Qt meta-object system 这个接口名称 -Q_DECLARE_INTERFACE(PluginInterface, "com.ubuntukylin.Plugin.PluginInterface ") +Q_DECLARE_INTERFACE(PluginInterface, "com.ubuntukylin.Plugin.PluginInterface") #endif // PLUGININTERFACE_H diff --git a/shredmanager/shred.json b/shredmanager/shred.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/shredmanager/shred.json @@ -0,0 +1 @@ +{} diff --git a/shredmanager/shredmanager.h b/shredmanager/shredmanager.h index aa476bc..8f55d56 100644 --- a/shredmanager/shredmanager.h +++ b/shredmanager/shredmanager.h @@ -28,9 +28,9 @@ class ShredManager : public QObject , PluginInterface Q_OBJECT Q_INTERFACES(PluginInterface) -//#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) -// Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "raw.json") -//#endif +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + Q_PLUGIN_METADATA(IID "com.ubuntukylin.Plugin.PluginInterface" FILE "shred.json")//指定IID和.json文件 +#endif public: diff --git a/src/main.cpp b/src/main.cpp index 4f30135..dd19918 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -120,19 +120,36 @@ int make_pid_file() { int main(int argc, char *argv[]) { -// QApplication app(argc, argv); + QApplication app(argc, argv); + + //单程序运行处理 - QtSingleApplication app(argc, argv); + QSharedMemory mem("KA"); + if (!mem.create(1)) { + qDebug() << QObject::tr("kylin-assistant had already running!"); + return 1; + } + +#ifdef QT_NO_DEBUG + qDebug() << "release mode"; +#else + qDebug() << "debug mode"; +#endif + + //单程序运行处理 + /*QtSingleApplication app(argc, argv); if (app.isRunning()) { qDebug() << QObject::tr("kylin-assistant had already running!"); return 0; - } + }*/ -// QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); -// QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); -// QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); -// QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); +#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) + QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); + QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); + QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); + QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); +#endif if (make_pid_file()) { exit(1); diff --git a/src/pluginmanager.cpp b/src/pluginmanager.cpp index abc8364..a0cb61d 100644 --- a/src/pluginmanager.cpp +++ b/src/pluginmanager.cpp @@ -20,6 +20,7 @@ #include "pluginmanager.h" #include "../component/plugininterface.h" #include +#include #include PluginManager::PluginManager(void) @@ -39,9 +40,19 @@ PluginManager* PluginManager::Instance() bool PluginManager::loadPlugin(QString plugin_path) { + /*QDir pluginsDir(qApp->applicationDirPath());//QCoreApplication::applicationDirPath() +#ifdef QT_DEBUG + pluginsDir.cd("libs"); +#else + pluginsDir.cd("../libs"); +#endif*/ + QDir pluginsDir(plugin_path + "/libs"); foreach (QString fileName, pluginsDir.entryList(QStringList("*.so"),QDir::Files)) { - QPluginLoader *pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName)); + if (!QLibrary::isLibrary(fileName)) + continue; + + QPluginLoader *pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName)); QObject *plugin = pluginLoader->instance(); if (plugin) {//测试插件是否有效:使用 qobject_cast()测试插件是否给出了相应接口并进行类型转换,转换成接口对象指针. PluginInterface* interface = qobject_cast(plugin); @@ -49,6 +60,10 @@ bool PluginManager::loadPlugin(QString plugin_path) QString guid = interface->getGuid(); plugin_map.insert(guid, pluginLoader); } + else { + pluginLoader->unload(); + pluginLoader->deleteLater(); + } } else { delete pluginLoader; diff --git a/src/pluginmanager.h b/src/pluginmanager.h index 82a0691..7801a2a 100644 --- a/src/pluginmanager.h +++ b/src/pluginmanager.h @@ -1,3 +1,6 @@ +#ifndef PLUGINSMANAGER_H +#define PLUGINSMANAGER_H + #include #include @@ -60,3 +63,34 @@ public: private: QMap plugin_map; }; + + + +/*#include "frameproxyinterface.h" + +#include + +using namespace dcc; + +class PluginsController : public QObject, public FrameProxyInterface +{ + Q_OBJECT + +public: + explicit PluginsController(QObject *parent = 0); + +signals: + void pluginAdded(QWidget * const w); + void requestModulePage(const QString &module, const QString &page); + +public slots: + void loadPlugins(); + +private: + void pushWidget(ModuleInterface * const, ContentWidget * const) {} + void setFrameAutoHide(ModuleInterface * const, const bool) {} + void setModuleVisible(ModuleInterface * const, const bool) {} + void showModulePage(const QString &module, const QString &page); +};*/ + +#endif // PLUGINSMANAGER_H