1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 17:42:10 +01:00
falkonOfficial/src/lib/plugins/plugins.cpp

260 lines
7.0 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2014-01-11 16:11:42 +01:00
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "pluginproxy.h"
#include "plugininterface.h"
#include "mainapplication.h"
#include "speeddial.h"
#include "settings.h"
#include "datapaths.h"
2011-03-02 16:57:41 +01:00
#include <iostream>
#include <QPluginLoader>
#include <QDir>
Plugins::Plugins(QObject* parent)
: QObject(parent)
, m_pluginsLoaded(false)
, m_speedDial(new SpeedDial(this))
2011-03-02 16:57:41 +01:00
{
loadSettings();
}
QList<Plugins::Plugin> Plugins::getAvailablePlugins()
{
loadAvailablePlugins();
return m_availablePlugins;
}
bool Plugins::loadPlugin(Plugins::Plugin* plugin)
{
if (plugin->isLoaded()) {
return true;
}
plugin->pluginLoader->setFileName(plugin->fullPath);
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(plugin->pluginLoader->instance());
if (!iPlugin) {
return false;
}
m_availablePlugins.removeOne(*plugin);
plugin->instance = initPlugin(PluginInterface::LateInitState, iPlugin, plugin->pluginLoader);
m_availablePlugins.prepend(*plugin);
refreshLoadedPlugins();
return plugin->isLoaded();
}
void Plugins::unloadPlugin(Plugins::Plugin* plugin)
2011-03-02 16:57:41 +01:00
{
if (!plugin->isLoaded()) {
return;
}
plugin->instance->unload();
plugin->pluginLoader->unload();
emit pluginUnloaded(plugin->instance);
2011-03-02 16:57:41 +01:00
m_availablePlugins.removeOne(*plugin);
plugin->instance = 0;
m_availablePlugins.append(*plugin);
refreshLoadedPlugins();
}
void Plugins::loadSettings()
{
Settings settings;
2011-03-02 16:57:41 +01:00
settings.beginGroup("Plugin-Settings");
m_pluginsEnabled = settings.value("EnablePlugins", !mApp->isPortable()).toBool();
m_allowedPlugins = settings.value("AllowedPlugins", QStringList()).toStringList();
2011-03-02 16:57:41 +01:00
settings.endGroup();
// Plugins are saved with relative path in portable mode
#ifdef NO_SYSTEM_DATAPATH
if (true) {
#else
if (mApp->isPortable()) {
#endif
QDir dir(DataPaths::path(DataPaths::Plugins));
for (int i = 0; i < m_allowedPlugins.count(); ++i) {
if (QFileInfo(m_allowedPlugins[i]).isRelative()) {
m_allowedPlugins[i] = dir.absoluteFilePath(m_allowedPlugins[i]);
}
}
}
c2f_loadSettings();
2011-03-02 16:57:41 +01:00
}
void Plugins::shutdown()
{
c2f_saveSettings();
foreach (PluginInterface* iPlugin, m_loadedPlugins) {
iPlugin->unload();
}
}
void Plugins::c2f_loadSettings()
{
Settings settings;
settings.beginGroup("ClickToFlash");
c2f_whitelist = settings.value("whitelist", QStringList()).toStringList();
c2f_enabled = settings.value("Enabled", true).toBool();
settings.endGroup();
}
void Plugins::c2f_saveSettings()
{
Settings settings;
settings.beginGroup("ClickToFlash");
settings.setValue("whitelist", c2f_whitelist);
settings.setValue("Enabled", c2f_enabled);
settings.endGroup();
}
2011-03-02 16:57:41 +01:00
void Plugins::loadPlugins()
{
if (!m_pluginsEnabled) {
return;
}
QDir settingsDir(DataPaths::currentProfilePath() + "/extensions/");
if (!settingsDir.exists()) {
settingsDir.mkdir(settingsDir.absolutePath());
}
foreach (const QString &fullPath, m_allowedPlugins) {
QPluginLoader* loader = new QPluginLoader(fullPath);
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
if (!iPlugin) {
qWarning() << "Plugins::loadPlugins Loading" << fullPath << "failed:" << loader->errorString();
continue;
}
Plugin plugin;
plugin.fullPath = fullPath;
plugin.pluginLoader = loader;
plugin.instance = initPlugin(PluginInterface::StartupInitState, iPlugin, loader);
if (plugin.isLoaded()) {
plugin.pluginSpec = iPlugin->pluginSpec();
m_loadedPlugins.append(plugin.instance);
m_availablePlugins.append(plugin);
}
}
refreshLoadedPlugins();
std::cout << "QupZilla: " << m_loadedPlugins.count() << " extensions loaded" << std::endl;
}
void Plugins::loadAvailablePlugins()
{
if (m_pluginsLoaded) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
m_pluginsLoaded = true;
2011-03-02 16:57:41 +01:00
QStringList dirs = DataPaths::allPaths(DataPaths::Plugins);
// Portable build: Load only plugins from DATADIR/plugins/ directory.
#ifndef NO_SYSTEM_DATAPATH
if (mApp->isPortable())
#endif
dirs = QStringList(DataPaths::path(DataPaths::Plugins));
2011-03-02 16:57:41 +01:00
foreach (const QString &dir, dirs) {
QDir pluginsDir = QDir(dir);
foreach (const QString &fileName, pluginsDir.entryList(QDir::Files)) {
const QString absolutePath = pluginsDir.absoluteFilePath(fileName);
QPluginLoader* loader = new QPluginLoader(absolutePath);
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
2013-03-11 17:19:11 +01:00
if (!iPlugin) {
qWarning() << "Plugins::loadAvailablePlugins" << loader->errorString();
continue;
}
2011-03-02 16:57:41 +01:00
Plugin plugin;
plugin.fileName = fileName;
plugin.fullPath = absolutePath;
plugin.pluginSpec = iPlugin->pluginSpec();
plugin.pluginLoader = loader;
plugin.instance = 0;
2011-03-02 16:57:41 +01:00
loader->unload();
2011-03-02 16:57:41 +01:00
if (!alreadySpecInAvailable(plugin.pluginSpec)) {
m_availablePlugins.append(plugin);
}
2011-03-02 16:57:41 +01:00
}
}
}
PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInterface* pluginInterface, QPluginLoader* loader)
2011-03-02 16:57:41 +01:00
{
if (!pluginInterface) {
2011-03-02 16:57:41 +01:00
return 0;
}
2011-03-27 21:59:40 +02:00
pluginInterface->init(state, DataPaths::currentProfilePath() + QL1S("/extensions"));
if (!pluginInterface->testPlugin()) {
pluginInterface->unload();
loader->unload();
emit pluginUnloaded(pluginInterface);
2011-03-02 16:57:41 +01:00
return 0;
}
qApp->installTranslator(pluginInterface->getTranslator(mApp->currentLanguageFile()));
return pluginInterface;
}
void Plugins::refreshLoadedPlugins()
{
m_loadedPlugins.clear();
foreach (const Plugin &plugin, m_availablePlugins) {
if (plugin.isLoaded()) {
m_loadedPlugins.append(plugin.instance);
}
}
2011-03-02 16:57:41 +01:00
}
bool Plugins::alreadySpecInAvailable(const PluginSpec &spec)
{
foreach (const Plugin &plugin, m_availablePlugins) {
if (plugin.pluginSpec == spec) {
return true;
}
}
return false;
}