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"
|
2012-02-22 18:33:44 +01:00
|
|
|
#include "speeddial.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
#include "datapaths.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-03-25 21:45:16 +02:00
|
|
|
#include <iostream>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QPluginLoader>
|
|
|
|
#include <QDir>
|
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
Plugins::Plugins(QObject* parent)
|
|
|
|
: QObject(parent)
|
2012-02-19 16:01:51 +01:00
|
|
|
, m_pluginsLoaded(false)
|
2015-05-24 20:49:50 +02:00
|
|
|
, m_speedDial(new SpeedDial(this))
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
2012-03-12 18:21:00 +01:00
|
|
|
QList<Plugins::Plugin> Plugins::getAvailablePlugins()
|
|
|
|
{
|
|
|
|
loadAvailablePlugins();
|
|
|
|
|
|
|
|
return m_availablePlugins;
|
|
|
|
}
|
|
|
|
|
2012-02-22 18:33:44 +01:00
|
|
|
bool Plugins::loadPlugin(Plugins::Plugin* plugin)
|
2012-02-19 16:01:51 +01:00
|
|
|
{
|
|
|
|
if (plugin->isLoaded()) {
|
2012-02-22 18:33:44 +01:00
|
|
|
return true;
|
2012-02-19 16:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
plugin->pluginLoader->setFileName(plugin->fullPath);
|
|
|
|
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(plugin->pluginLoader->instance());
|
|
|
|
if (!iPlugin) {
|
2012-02-22 18:33:44 +01:00
|
|
|
return false;
|
2012-02-19 16:01:51 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:01:57 +01:00
|
|
|
m_availablePlugins.removeOne(*plugin);
|
2013-05-24 11:03:16 +02:00
|
|
|
plugin->instance = initPlugin(PluginInterface::LateInitState, iPlugin, plugin->pluginLoader);
|
2012-03-12 18:21:00 +01:00
|
|
|
m_availablePlugins.prepend(*plugin);
|
2012-02-19 16:01:51 +01:00
|
|
|
|
|
|
|
refreshLoadedPlugins();
|
2012-02-22 18:33:44 +01:00
|
|
|
|
|
|
|
return plugin->isLoaded();
|
2012-02-19 16:01:51 +01:00
|
|
|
}
|
|
|
|
|
2012-02-20 15:56:26 +01:00
|
|
|
void Plugins::unloadPlugin(Plugins::Plugin* plugin)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-02-19 16:01:51 +01:00
|
|
|
if (!plugin->isLoaded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin->instance->unload();
|
|
|
|
plugin->pluginLoader->unload();
|
2012-04-04 21:12:52 +02:00
|
|
|
emit pluginUnloaded(plugin->instance);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
m_availablePlugins.removeOne(*plugin);
|
|
|
|
plugin->instance = 0;
|
|
|
|
m_availablePlugins.append(*plugin);
|
|
|
|
|
|
|
|
refreshLoadedPlugins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Plugins::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.beginGroup("Plugin-Settings");
|
2014-01-23 13:02:07 +01:00
|
|
|
m_pluginsEnabled = settings.value("EnablePlugins", !mApp->isPortable()).toBool();
|
2012-03-12 18:21:00 +01:00
|
|
|
m_allowedPlugins = settings.value("AllowedPlugins", QStringList()).toStringList();
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.endGroup();
|
2012-02-22 18:33:44 +01:00
|
|
|
|
2014-10-16 12:24:48 +02:00
|
|
|
// Plugins are saved with relative path in portable mode
|
2016-12-23 15:01:19 +01:00
|
|
|
#ifdef NO_SYSTEM_DATAPATH
|
|
|
|
if (true) {
|
|
|
|
#else
|
2014-10-16 12:24:48 +02:00
|
|
|
if (mApp->isPortable()) {
|
2016-12-23 15:01:19 +01:00
|
|
|
#endif
|
2014-10-16 12:24:48 +02:00
|
|
|
QDir dir(DataPaths::path(DataPaths::Plugins));
|
2016-12-23 15:09:08 +01:00
|
|
|
for (int i = 0; i < m_allowedPlugins.count(); ++i) {
|
|
|
|
if (QFileInfo(m_allowedPlugins[i]).isRelative()) {
|
|
|
|
m_allowedPlugins[i] = dir.absoluteFilePath(m_allowedPlugins[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 12:24:48 +02:00
|
|
|
}
|
|
|
|
|
2012-02-22 18:33:44 +01:00
|
|
|
c2f_loadSettings();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-07-08 14:03:50 +02:00
|
|
|
void Plugins::shutdown()
|
|
|
|
{
|
|
|
|
c2f_saveSettings();
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (PluginInterface* iPlugin, m_loadedPlugins) {
|
2012-07-08 14:03:50 +02:00
|
|
|
iPlugin->unload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 18:33:44 +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()
|
|
|
|
{
|
2012-03-12 18:21:00 +01:00
|
|
|
if (!m_pluginsEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 22:17:13 +01:00
|
|
|
QDir settingsDir(DataPaths::currentProfilePath() + "/extensions/");
|
2012-04-02 16:00:08 +02:00
|
|
|
if (!settingsDir.exists()) {
|
|
|
|
settingsDir.mkdir(settingsDir.absolutePath());
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &fullPath, m_allowedPlugins) {
|
2012-03-12 18:21:00 +01:00
|
|
|
QPluginLoader* loader = new QPluginLoader(fullPath);
|
|
|
|
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
|
2014-01-21 23:53:45 +01:00
|
|
|
|
2012-03-12 18:21:00 +01:00
|
|
|
if (!iPlugin) {
|
2014-01-21 23:53:45 +01:00
|
|
|
qWarning() << "Plugins::loadPlugins Loading" << fullPath << "failed:" << loader->errorString();
|
2012-03-12 18:21:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plugin plugin;
|
|
|
|
plugin.fullPath = fullPath;
|
|
|
|
plugin.pluginLoader = loader;
|
2013-05-24 11:03:16 +02:00
|
|
|
plugin.instance = initPlugin(PluginInterface::StartupInitState, iPlugin, loader);
|
2012-03-12 18:21:00 +01:00
|
|
|
|
|
|
|
if (plugin.isLoaded()) {
|
2012-04-04 21:12:52 +02:00
|
|
|
plugin.pluginSpec = iPlugin->pluginSpec();
|
|
|
|
|
2012-03-12 18:21:00 +01:00
|
|
|
m_loadedPlugins.append(plugin.instance);
|
2012-04-04 21:12:52 +02:00
|
|
|
m_availablePlugins.append(plugin);
|
2012-03-12 18:21:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshLoadedPlugins();
|
|
|
|
|
2012-03-13 15:10:18 +01:00
|
|
|
std::cout << "QupZilla: " << m_loadedPlugins.count() << " extensions loaded" << std::endl;
|
2012-03-12 18:21:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Plugins::loadAvailablePlugins()
|
|
|
|
{
|
|
|
|
if (m_pluginsLoaded) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
m_pluginsLoaded = true;
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
QStringList dirs = DataPaths::allPaths(DataPaths::Plugins);
|
2014-01-23 13:02:07 +01:00
|
|
|
|
|
|
|
// Portable build: Load only plugins from DATADIR/plugins/ directory.
|
2016-12-23 15:01:19 +01:00
|
|
|
#ifndef NO_SYSTEM_DATAPATH
|
2014-10-16 12:24:48 +02:00
|
|
|
if (mApp->isPortable())
|
2016-12-23 15:01:19 +01:00
|
|
|
#endif
|
2014-03-09 21:51:42 +01:00
|
|
|
dirs = QStringList(DataPaths::path(DataPaths::Plugins));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &dir, dirs) {
|
2012-02-19 16:01:51 +01:00
|
|
|
QDir pluginsDir = QDir(dir);
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &fileName, pluginsDir.entryList(QDir::Files)) {
|
2012-03-12 18:21:00 +01:00
|
|
|
const QString absolutePath = pluginsDir.absoluteFilePath(fileName);
|
|
|
|
|
|
|
|
QPluginLoader* loader = new QPluginLoader(absolutePath);
|
2012-02-19 16:01:51 +01:00
|
|
|
PluginInterface* iPlugin = qobject_cast<PluginInterface*>(loader->instance());
|
2013-03-11 17:19:11 +01:00
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
if (!iPlugin) {
|
2013-04-12 17:49:22 +02:00
|
|
|
qWarning() << "Plugins::loadAvailablePlugins" << loader->errorString();
|
2012-02-19 16:01:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
Plugin plugin;
|
2013-09-10 15:52:18 +02:00
|
|
|
plugin.fileName = fileName;
|
2012-03-12 18:21:00 +01:00
|
|
|
plugin.fullPath = absolutePath;
|
2012-02-19 16:01:51 +01:00
|
|
|
plugin.pluginSpec = iPlugin->pluginSpec();
|
|
|
|
plugin.pluginLoader = loader;
|
|
|
|
plugin.instance = 0;
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-03-12 18:21:00 +01:00
|
|
|
loader->unload();
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-04-06 10:15:17 +02:00
|
|
|
if (!alreadySpecInAvailable(plugin.pluginSpec)) {
|
|
|
|
m_availablePlugins.append(plugin);
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-19 11:38:16 +01:00
|
|
|
PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInterface* pluginInterface, QPluginLoader* loader)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-03-19 11:38:16 +01:00
|
|
|
if (!pluginInterface) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return 0;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2014-09-16 15:20:09 +02:00
|
|
|
pluginInterface->init(state, DataPaths::currentProfilePath() + QL1S("/extensions"));
|
2012-02-19 16:01:51 +01:00
|
|
|
|
2014-03-19 11:38:16 +01:00
|
|
|
if (!pluginInterface->testPlugin()) {
|
|
|
|
pluginInterface->unload();
|
2012-02-19 16:01:51 +01:00
|
|
|
loader->unload();
|
2012-04-04 21:12:52 +02:00
|
|
|
|
2014-03-19 11:38:16 +01:00
|
|
|
emit pluginUnloaded(pluginInterface);
|
2012-04-04 21:12:52 +02:00
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
return 0;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-02-19 16:01:51 +01:00
|
|
|
|
2014-03-19 11:38:16 +01:00
|
|
|
qApp->installTranslator(pluginInterface->getTranslator(mApp->currentLanguageFile()));
|
2012-04-04 21:12:52 +02:00
|
|
|
|
2014-03-19 11:38:16 +01:00
|
|
|
return pluginInterface;
|
2012-02-19 16:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Plugins::refreshLoadedPlugins()
|
|
|
|
{
|
|
|
|
m_loadedPlugins.clear();
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const Plugin &plugin, m_availablePlugins) {
|
2012-02-19 16:01:51 +01:00
|
|
|
if (plugin.isLoaded()) {
|
|
|
|
m_loadedPlugins.append(plugin.instance);
|
|
|
|
}
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2012-04-06 10:15:17 +02:00
|
|
|
|
|
|
|
bool Plugins::alreadySpecInAvailable(const PluginSpec &spec)
|
|
|
|
{
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const Plugin &plugin, m_availablePlugins) {
|
2012-04-06 10:15:17 +02:00
|
|
|
if (plugin.pluginSpec == spec) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|