mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-24 07:55:35 +08:00
refs #510 Remove underscores from all the source file names
This commit is contained in:
151
src/blackcore/pluginmanager.cpp
Normal file
151
src/blackcore/pluginmanager.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/* Copyright (C) 2013
|
||||
* swift Project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "pluginmanager.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QDirIterator>
|
||||
#include <QLibrary>
|
||||
#include <QPluginLoader>
|
||||
#include <QStringBuilder>
|
||||
|
||||
using namespace BlackMisc;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
IPluginManager::IPluginManager(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void IPluginManager::collectPlugins()
|
||||
{
|
||||
QDir pluginDir(pluginDirectory());
|
||||
if (!pluginDir.exists())
|
||||
{
|
||||
CLogMessage(this).warning("No such directory: %1") << pluginDir.path();
|
||||
return;
|
||||
}
|
||||
|
||||
QDirIterator it(pluginDir, QDirIterator::FollowSymlinks);
|
||||
while (it.hasNext())
|
||||
{
|
||||
tryLoad(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
QString IPluginManager::getPluginConfigId(const QString &identifier)
|
||||
{
|
||||
return m_configs.contains(identifier) ? m_configs.value(identifier) : QString();
|
||||
}
|
||||
|
||||
QString IPluginManager::pluginDirectory() const
|
||||
{
|
||||
return qApp->applicationDirPath() % QStringLiteral("/plugins");
|
||||
}
|
||||
|
||||
bool IPluginManager::isValid(const QJsonObject &metadata) const
|
||||
{
|
||||
if (!metadata["MetaData"].isObject())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonObject data = metadata["MetaData"].toObject();
|
||||
if (!data.contains("identifier") || !data["identifier"].isString())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto iids = acceptedIids();
|
||||
for (const QString &iid : iids)
|
||||
{
|
||||
if (metadata["IID"].toString() == iid)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString IPluginManager::pluginIdentifier(const QJsonObject &metadata) const
|
||||
{
|
||||
Q_ASSERT(isValid(metadata));
|
||||
return metadata.value("MetaData").toObject().value("identifier").toString();
|
||||
}
|
||||
|
||||
QString IPluginManager::getIdByPlugin(const QObject *instance) const
|
||||
{
|
||||
return m_instanceIds.value(instance, QString());
|
||||
}
|
||||
|
||||
bool IPluginManager::tryLoad(QString path)
|
||||
{
|
||||
if (!QLibrary::isLibrary(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CLogMessage(this).debug() << "Loading plugin: " << path;
|
||||
QPluginLoader loader(path);
|
||||
QJsonObject json = loader.metaData();
|
||||
if (!isValid(json))
|
||||
{
|
||||
CLogMessage(this).warning("Plugin %1 invalid, not loading it") << path;
|
||||
return false;
|
||||
}
|
||||
|
||||
QString identifier = pluginIdentifier(json);
|
||||
m_paths.insert(identifier, path);
|
||||
m_metadatas.push_back(json);
|
||||
|
||||
if (json.value("MetaData").toObject().contains("config")) {
|
||||
QString configId = json.value("MetaData").toObject().value("config").toString();
|
||||
if (!configId.isEmpty())
|
||||
m_configs.insert(identifier, configId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QObject *IPluginManager::getPluginByIdImpl(const QString &identifier)
|
||||
{
|
||||
if (m_instances.contains(identifier))
|
||||
{
|
||||
return m_instances.value(identifier);
|
||||
}
|
||||
|
||||
if (!m_paths.contains(identifier))
|
||||
{
|
||||
CLogMessage(this).warning("Plugin with id %1 does not exist") << identifier;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QString path = m_paths.value(identifier);
|
||||
QPluginLoader loader(path);
|
||||
QObject *instance = loader.instance();
|
||||
if (instance)
|
||||
{
|
||||
m_instances.insert(identifier, instance);
|
||||
m_instanceIds.insert(instance, identifier);
|
||||
return instance;
|
||||
}
|
||||
else
|
||||
{
|
||||
CLogMessage(this).error(loader.errorString());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user