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"
|
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>
|
2013-04-12 17:49:22 +02:00
|
|
|
#include <QDebug>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
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)
|
2012-02-22 18:33:44 +01: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
|
|
|
|
|
|
|
c2f_loadSettings();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-07-08 14:03:50 +02:00
|
|
|
void Plugins::shutdown()
|
|
|
|
{
|
|
|
|
c2f_saveSettings();
|
|
|
|
m_speedDial->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;
|
|
|
|
}
|
|
|
|
|
2012-04-04 18:48:54 +02:00
|
|
|
QDir settingsDir(mApp->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
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
QStringList dirs;
|
2014-01-23 13:02:07 +01:00
|
|
|
dirs << mApp->DATADIR + "plugins/";
|
|
|
|
|
|
|
|
// Portable build: Load only plugins from DATADIR/plugins/ directory.
|
|
|
|
// Loaded plugins are saved with relative path, instead of absolute for
|
|
|
|
// normal build.
|
|
|
|
|
|
|
|
if (!mApp->isPortable()) {
|
|
|
|
dirs
|
2014-03-03 14:47:47 +01:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(NO_SYSTEM_DATAPATH)
|
2012-04-02 21:05:06 +02:00
|
|
|
#ifdef USE_LIBPATH
|
2014-03-03 14:47:47 +01:00
|
|
|
<< USE_LIBPATH "/qupzilla/"
|
2012-04-02 21:05:06 +02:00
|
|
|
#else
|
2014-01-25 17:47:21 +01:00
|
|
|
<< "/usr/lib/qupzilla/"
|
2012-04-02 21:05:06 +02:00
|
|
|
#endif
|
2012-03-15 19:35:37 +01:00
|
|
|
#endif
|
2014-01-25 17:47:21 +01:00
|
|
|
<< mApp->PROFILEDIR + "plugins/";
|
2014-01-23 13:02:07 +01:00
|
|
|
}
|
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);
|
|
|
|
if (m_allowedPlugins.contains(absolutePath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 11:03:16 +02:00
|
|
|
PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInterface* interface, QPluginLoader* loader)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-02-19 16:01:51 +01:00
|
|
|
if (!interface) {
|
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
|
|
|
|
2013-05-24 11:03:16 +02:00
|
|
|
interface->init(state, mApp->currentProfilePath() + "extensions/");
|
2012-02-19 16:01:51 +01:00
|
|
|
|
|
|
|
if (!interface->testPlugin()) {
|
|
|
|
interface->unload();
|
|
|
|
loader->unload();
|
2012-04-04 21:12:52 +02:00
|
|
|
|
|
|
|
emit pluginUnloaded(interface);
|
|
|
|
|
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
|
|
|
|
2013-02-05 20:43:53 +01:00
|
|
|
qApp->installTranslator(interface->getTranslator(mApp->currentLanguageFile()));
|
2012-04-04 21:12:52 +02:00
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
return interface;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|