mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36:34 +01:00
Plugins: Rework to allow more plugin types
This should make it possible to add more plugin types (such as JavaScript/Python/..) in the future. Added InternalPlugin type which makes the AdBlock plugin visible and (un)loadable in Extensions tab in Preferences.
This commit is contained in:
parent
a4c246ab0e
commit
929851095a
@ -50,14 +50,11 @@ bool Plugins::loadPlugin(Plugins::Plugin* plugin)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin->pluginLoader->setFileName(plugin->fullPath);
|
if (!initPlugin(PluginInterface::LateInitState, plugin)) {
|
||||||
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(plugin->pluginLoader->instance());
|
|
||||||
if (!iPlugin) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_availablePlugins.removeOne(*plugin);
|
m_availablePlugins.removeOne(*plugin);
|
||||||
plugin->instance = initPlugin(PluginInterface::LateInitState, iPlugin, plugin->pluginLoader);
|
|
||||||
m_availablePlugins.prepend(*plugin);
|
m_availablePlugins.prepend(*plugin);
|
||||||
|
|
||||||
refreshLoadedPlugins();
|
refreshLoadedPlugins();
|
||||||
@ -72,11 +69,10 @@ void Plugins::unloadPlugin(Plugins::Plugin* plugin)
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugin->instance->unload();
|
plugin->instance->unload();
|
||||||
plugin->pluginLoader->unload();
|
|
||||||
emit pluginUnloaded(plugin->instance);
|
emit pluginUnloaded(plugin->instance);
|
||||||
|
plugin->instance = nullptr;
|
||||||
|
|
||||||
m_availablePlugins.removeOne(*plugin);
|
m_availablePlugins.removeOne(*plugin);
|
||||||
plugin->instance = 0;
|
|
||||||
m_availablePlugins.append(*plugin);
|
m_availablePlugins.append(*plugin);
|
||||||
|
|
||||||
refreshLoadedPlugins();
|
refreshLoadedPlugins();
|
||||||
@ -86,7 +82,7 @@ void Plugins::loadSettings()
|
|||||||
{
|
{
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings.beginGroup("Plugin-Settings");
|
settings.beginGroup("Plugin-Settings");
|
||||||
m_allowedPlugins = settings.value("AllowedPlugins", QStringList()).toStringList();
|
m_allowedPlugins = settings.value("AllowedPlugins", QStringList(QSL("internal:adblock"))).toStringList();
|
||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,49 +100,22 @@ void Plugins::loadPlugins()
|
|||||||
settingsDir.mkdir(settingsDir.absolutePath());
|
settingsDir.mkdir(settingsDir.absolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (const QString &pluginFile, m_allowedPlugins) {
|
foreach (const QString &pluginId, m_allowedPlugins) {
|
||||||
QString fullPath;
|
Plugin plugin = loadPlugin(pluginId);
|
||||||
if (QFileInfo(pluginFile).isAbsolute()) {
|
if (plugin.pluginSpec.name.isEmpty()) {
|
||||||
fullPath = pluginFile;
|
qWarning() << "Invalid plugin spec of" << pluginId << "plugin";
|
||||||
} else {
|
|
||||||
fullPath = DataPaths::locate(DataPaths::Plugins, pluginFile);
|
|
||||||
if (fullPath.isEmpty()) {
|
|
||||||
qWarning() << "Plugin" << pluginFile << "not found";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QPluginLoader* loader = new QPluginLoader(fullPath);
|
|
||||||
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
|
|
||||||
|
|
||||||
if (!iPlugin) {
|
|
||||||
qWarning() << "Plugins::loadPlugins Loading" << fullPath << "failed:" << loader->errorString();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!initPlugin(PluginInterface::StartupInitState, &plugin)) {
|
||||||
Plugin plugin;
|
qWarning() << "Failed to init" << pluginId << "plugin";
|
||||||
plugin.fileName = QFileInfo(fullPath).fileName();
|
continue;
|
||||||
plugin.fullPath = fullPath;
|
|
||||||
plugin.pluginLoader = loader;
|
|
||||||
plugin.instance = initPlugin(PluginInterface::StartupInitState, iPlugin, loader);
|
|
||||||
|
|
||||||
if (plugin.isLoaded()) {
|
|
||||||
plugin.pluginSpec = createSpec(iPlugin->metaData());
|
|
||||||
if (!plugin.pluginSpec.name.isEmpty()) {
|
|
||||||
m_availablePlugins.append(plugin);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
registerAvailablePlugin(plugin);
|
||||||
|
|
||||||
// Internal plugins
|
|
||||||
AdBlockPlugin *adBlock = new AdBlockPlugin();
|
|
||||||
if (initPlugin(PluginInterface::StartupInitState, adBlock, nullptr)) {
|
|
||||||
m_internalPlugins.append(adBlock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshLoadedPlugins();
|
refreshLoadedPlugins();
|
||||||
|
|
||||||
std::cout << "Falkon: " << (m_loadedPlugins.count() - m_internalPlugins.count()) << " extensions loaded" << std::endl;
|
std::cout << "Falkon: " << m_loadedPlugins.count() << " extensions loaded" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Plugins::loadAvailablePlugins()
|
void Plugins::loadAvailablePlugins()
|
||||||
@ -163,62 +132,33 @@ void Plugins::loadAvailablePlugins()
|
|||||||
if (mApp->isPortable())
|
if (mApp->isPortable())
|
||||||
dirs = QStringList(DataPaths::path(DataPaths::Plugins));
|
dirs = QStringList(DataPaths::path(DataPaths::Plugins));
|
||||||
|
|
||||||
foreach (const QString &dir, dirs) {
|
// InternalPlugin
|
||||||
QDir pluginsDir = QDir(dir);
|
registerAvailablePlugin(loadInternalPlugin(QSL("adblock")));
|
||||||
foreach (const QString &fileName, pluginsDir.entryList(QDir::Files)) {
|
|
||||||
const QString absolutePath = pluginsDir.absoluteFilePath(fileName);
|
|
||||||
|
|
||||||
QPluginLoader* loader = new QPluginLoader(absolutePath);
|
// SharedLibraryPlugin
|
||||||
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
|
for (const QString &dir : qAsConst(dirs)) {
|
||||||
|
const auto files = QDir(dir).entryInfoList(QDir::Files);
|
||||||
if (!iPlugin) {
|
for (const QFileInfo &info : files) {
|
||||||
qWarning() << "Plugins::loadAvailablePlugins" << loader->errorString();
|
Plugin plugin = loadSharedLibraryPlugin(info.absoluteFilePath());
|
||||||
|
if (plugin.pluginSpec.name.isEmpty()) {
|
||||||
|
qWarning() << "Invalid plugin spec of" << info.absoluteFilePath() << "plugin";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
registerAvailablePlugin(plugin);
|
||||||
Plugin plugin;
|
|
||||||
plugin.fileName = fileName;
|
|
||||||
plugin.fullPath = absolutePath;
|
|
||||||
plugin.pluginSpec = createSpec(iPlugin->metaData());
|
|
||||||
plugin.pluginLoader = loader;
|
|
||||||
plugin.instance = 0;
|
|
||||||
|
|
||||||
loader->unload();
|
|
||||||
|
|
||||||
if (!plugin.pluginSpec.name.isEmpty() && !alreadySpecInAvailable(plugin.pluginSpec)) {
|
|
||||||
m_availablePlugins.append(plugin);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInterface* pluginInterface, QPluginLoader* loader)
|
void Plugins::registerAvailablePlugin(const Plugin &plugin)
|
||||||
{
|
{
|
||||||
if (!pluginInterface) {
|
if (!m_availablePlugins.contains(plugin)) {
|
||||||
return 0;
|
m_availablePlugins.append(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginInterface->init(state, DataPaths::currentProfilePath() + QL1S("/extensions"));
|
|
||||||
|
|
||||||
if (!pluginInterface->testPlugin()) {
|
|
||||||
pluginInterface->unload();
|
|
||||||
if (loader) {
|
|
||||||
loader->unload();
|
|
||||||
}
|
|
||||||
|
|
||||||
emit pluginUnloaded(pluginInterface);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
qApp->installTranslator(pluginInterface->getTranslator(mApp->currentLanguageFile()));
|
|
||||||
|
|
||||||
return pluginInterface;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Plugins::refreshLoadedPlugins()
|
void Plugins::refreshLoadedPlugins()
|
||||||
{
|
{
|
||||||
m_loadedPlugins = m_internalPlugins;
|
m_loadedPlugins.clear();
|
||||||
|
|
||||||
foreach (const Plugin &plugin, m_availablePlugins) {
|
foreach (const Plugin &plugin, m_availablePlugins) {
|
||||||
if (plugin.isLoaded()) {
|
if (plugin.isLoaded()) {
|
||||||
@ -227,15 +167,128 @@ void Plugins::refreshLoadedPlugins()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Plugins::alreadySpecInAvailable(const PluginSpec &spec)
|
Plugins::Plugin Plugins::loadPlugin(const QString &id)
|
||||||
{
|
{
|
||||||
foreach (const Plugin &plugin, m_availablePlugins) {
|
QString name;
|
||||||
if (plugin.pluginSpec == spec) {
|
Plugin::Type type = Plugin::Invalid;
|
||||||
return true;
|
|
||||||
|
const int colon = id.indexOf(QL1C(':'));
|
||||||
|
if (colon > -1) {
|
||||||
|
const auto t = id.leftRef(colon);
|
||||||
|
if (t == QL1S("internal")) {
|
||||||
|
type = Plugin::InternalPlugin;
|
||||||
|
} else if (t == QL1S("lib")) {
|
||||||
|
type = Plugin::SharedLibraryPlugin;
|
||||||
|
}
|
||||||
|
name = id.mid(colon + 1);
|
||||||
|
} else {
|
||||||
|
name = id;
|
||||||
|
type = Plugin::SharedLibraryPlugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case Plugin::InternalPlugin:
|
||||||
|
return loadInternalPlugin(name);
|
||||||
|
|
||||||
|
case Plugin::SharedLibraryPlugin:
|
||||||
|
return loadSharedLibraryPlugin(name);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return Plugin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugins::Plugin Plugins::loadInternalPlugin(const QString &name)
|
||||||
|
{
|
||||||
|
if (name == QL1S("adblock")) {
|
||||||
|
Plugin plugin;
|
||||||
|
plugin.type = Plugin::InternalPlugin;
|
||||||
|
plugin.pluginId = QSL("internal:adblock");
|
||||||
|
plugin.internalInstance = new AdBlockPlugin();
|
||||||
|
plugin.pluginSpec = createSpec(plugin.internalInstance->metaData());
|
||||||
|
return plugin;
|
||||||
|
} else {
|
||||||
|
return Plugin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugins::Plugin Plugins::loadSharedLibraryPlugin(const QString &name)
|
||||||
|
{
|
||||||
|
QString fullPath;
|
||||||
|
if (QFileInfo(name).isAbsolute()) {
|
||||||
|
fullPath = name;
|
||||||
|
} else {
|
||||||
|
fullPath = DataPaths::locate(DataPaths::Plugins, name);
|
||||||
|
if (fullPath.isEmpty()) {
|
||||||
|
qWarning() << "Plugin" << name << "not found";
|
||||||
|
return Plugin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
QPluginLoader *loader = new QPluginLoader(fullPath);
|
||||||
|
PluginInterface *iPlugin = qobject_cast<PluginInterface*>(loader->instance());
|
||||||
|
|
||||||
|
if (!iPlugin) {
|
||||||
|
qWarning() << "Loading" << fullPath << "failed:" << loader->errorString();
|
||||||
|
return Plugin();
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugin plugin;
|
||||||
|
plugin.type = Plugin::SharedLibraryPlugin;
|
||||||
|
plugin.pluginId = QSL("lib:%1").arg(QFileInfo(fullPath).fileName());
|
||||||
|
plugin.libraryPath = fullPath;
|
||||||
|
plugin.pluginLoader = loader;
|
||||||
|
plugin.pluginSpec = createSpec(iPlugin->metaData());
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Plugins::initPlugin(PluginInterface::InitState state, Plugin *plugin)
|
||||||
|
{
|
||||||
|
if (!plugin) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (plugin->type) {
|
||||||
|
case Plugin::InternalPlugin:
|
||||||
|
initInternalPlugin(plugin);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Plugin::SharedLibraryPlugin:
|
||||||
|
initSharedLibraryPlugin(plugin);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!plugin->instance) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin->instance->init(state, DataPaths::currentProfilePath() + QL1S("/extensions"));
|
||||||
|
|
||||||
|
if (!plugin->instance->testPlugin()) {
|
||||||
|
emit pluginUnloaded(plugin->instance);
|
||||||
|
plugin->instance = nullptr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
qApp->installTranslator(plugin->instance->getTranslator(mApp->currentLanguageFile()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Plugins::initInternalPlugin(Plugin *plugin)
|
||||||
|
{
|
||||||
|
Q_ASSERT(plugin->type == Plugin::InternalPlugin);
|
||||||
|
|
||||||
|
plugin->instance = plugin->internalInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Plugins::initSharedLibraryPlugin(Plugin *plugin)
|
||||||
|
{
|
||||||
|
Q_ASSERT(plugin->type == Plugin::SharedLibraryPlugin);
|
||||||
|
|
||||||
|
plugin->instance = qobject_cast<PluginInterface*>(plugin->pluginLoader->instance());
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginSpec Plugins::createSpec(const DesktopFile &metaData) const
|
PluginSpec Plugins::createSpec(const DesktopFile &metaData) const
|
||||||
|
@ -50,26 +50,30 @@ class FALKON_EXPORT Plugins : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
struct Plugin {
|
struct Plugin {
|
||||||
QString fileName;
|
enum Type {
|
||||||
QString fullPath;
|
Invalid = 0,
|
||||||
|
InternalPlugin,
|
||||||
|
SharedLibraryPlugin
|
||||||
|
};
|
||||||
|
Type type = Invalid;
|
||||||
|
QString pluginId;
|
||||||
PluginSpec pluginSpec;
|
PluginSpec pluginSpec;
|
||||||
QPluginLoader* pluginLoader;
|
PluginInterface *instance = nullptr;
|
||||||
PluginInterface* instance;
|
|
||||||
|
|
||||||
Plugin() {
|
// InternalPlugin
|
||||||
pluginLoader = 0;
|
PluginInterface *internalInstance = nullptr;
|
||||||
instance = 0;
|
|
||||||
}
|
// SharedLibraryPlugin
|
||||||
|
QString libraryPath;
|
||||||
|
QPluginLoader *pluginLoader = nullptr;
|
||||||
|
|
||||||
bool isLoaded() const {
|
bool isLoaded() const {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Plugin &other) const {
|
bool operator==(const Plugin &other) const {
|
||||||
return (this->fileName == other.fileName &&
|
return this->type == other.type &&
|
||||||
this->fullPath == other.fullPath &&
|
this->pluginId == other.pluginId;
|
||||||
this->pluginSpec == other.pluginSpec &&
|
|
||||||
this->instance == other.instance);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -97,9 +101,16 @@ signals:
|
|||||||
void pluginUnloaded(PluginInterface* plugin);
|
void pluginUnloaded(PluginInterface* plugin);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool alreadySpecInAvailable(const PluginSpec &spec);
|
|
||||||
PluginSpec createSpec(const DesktopFile &metaData) const;
|
PluginSpec createSpec(const DesktopFile &metaData) const;
|
||||||
PluginInterface* initPlugin(PluginInterface::InitState state , PluginInterface* pluginInterface, QPluginLoader* loader);
|
|
||||||
|
Plugin loadPlugin(const QString &id);
|
||||||
|
Plugin loadInternalPlugin(const QString &name);
|
||||||
|
Plugin loadSharedLibraryPlugin(const QString &name);
|
||||||
|
bool initPlugin(PluginInterface::InitState state, Plugin *plugin);
|
||||||
|
void initInternalPlugin(Plugin *plugin);
|
||||||
|
void initSharedLibraryPlugin(Plugin *plugin);
|
||||||
|
|
||||||
|
void registerAvailablePlugin(const Plugin &plugin);
|
||||||
|
|
||||||
void refreshLoadedPlugins();
|
void refreshLoadedPlugins();
|
||||||
void loadAvailablePlugins();
|
void loadAvailablePlugins();
|
||||||
|
@ -74,7 +74,7 @@ void PluginsManager::save()
|
|||||||
|
|
||||||
if (item->checkState() == Qt::Checked) {
|
if (item->checkState() == Qt::Checked) {
|
||||||
const Plugins::Plugin plugin = item->data(Qt::UserRole + 10).value<Plugins::Plugin>();
|
const Plugins::Plugin plugin = item->data(Qt::UserRole + 10).value<Plugins::Plugin>();
|
||||||
allowedPlugins.append(plugin.fileName);
|
allowedPlugins.append(plugin.pluginId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user