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

180 lines
4.8 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 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"
2011-03-02 16:57:41 +01:00
#ifdef PORTABLE_BUILD
#define DEFAULT_ENABLE_PLUGINS false
#else
#define DEFAULT_ENABLE_PLUGINS true
#endif
Plugins::Plugins(QObject* parent)
: QObject(parent)
, m_pluginsLoaded(false)
, m_speedDial(new SpeedDial(this))
2011-03-02 16:57:41 +01:00
{
loadSettings();
}
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(iPlugin, plugin->pluginLoader);
m_availablePlugins.append(*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();
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", DEFAULT_ENABLE_PLUGINS).toBool();
2011-03-02 16:57:41 +01:00
m_allowedPluginFileNames = settings.value("AllowedPlugins", QStringList()).toStringList();
settings.endGroup();
c2f_loadSettings();
2011-03-02 16:57:41 +01:00
}
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 || 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;
dirs << mApp->DATADIR + "plugins/"
#ifdef Q_WS_X11
<< "/usr/lib/qupzilla/"
#endif
<< mApp->PROFILEDIR + "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)) {
QPluginLoader* loader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
if (!iPlugin) {
continue;
}
2011-03-02 16:57:41 +01:00
Plugin plugin;
plugin.fileName = fileName;
plugin.fullPath = pluginsDir.absoluteFilePath(fileName);
plugin.pluginSpec = iPlugin->pluginSpec();
plugin.pluginLoader = loader;
plugin.instance = 0;
2011-03-02 16:57:41 +01:00
if (m_allowedPluginFileNames.contains(fileName)) {
plugin.instance = initPlugin(iPlugin, loader);
}
else {
loader->unload();
2011-03-02 16:57:41 +01:00
}
m_availablePlugins.append(plugin);
2011-03-02 16:57:41 +01:00
}
}
refreshLoadedPlugins();
std::cout << m_loadedPlugins.count() << " plugins loaded" << std::endl;
2011-03-02 16:57:41 +01:00
}
PluginInterface* Plugins::initPlugin(PluginInterface* interface, QPluginLoader* loader)
2011-03-02 16:57:41 +01:00
{
if (!interface) {
2011-03-02 16:57:41 +01:00
return 0;
}
2011-03-27 21:59:40 +02:00
interface->init(mApp->getActiveProfilPath() + "plugins.ini");
if (!interface->testPlugin()) {
interface->unload();
loader->unload();
2011-03-02 16:57:41 +01:00
return 0;
}
qApp->installTranslator(interface->getTranslator(mApp->getActiveLanguage()));
return interface;
}
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
}