2012-02-29 18:33:50 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2012-02-29 18:33:50 +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 "testplugin.h"
|
2012-04-01 16:36:38 +02:00
|
|
|
#include "testplugin_sidebar.h"
|
2012-02-19 16:01:51 +01:00
|
|
|
#include "qupzilla.h"
|
2012-02-19 17:50:02 +01:00
|
|
|
#include "webview.h"
|
2012-02-22 18:33:44 +01:00
|
|
|
#include "pluginproxy.h"
|
2012-03-16 13:53:49 +01:00
|
|
|
#include "mainapplication.h"
|
2012-04-01 16:36:38 +02:00
|
|
|
#include "sidebar.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QTranslator>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QWebHitTestResult>
|
|
|
|
#include <QMenu>
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
TestPlugin::TestPlugin()
|
|
|
|
: QObject()
|
|
|
|
, m_view(0)
|
|
|
|
{
|
|
|
|
// Don't do anything expensive in constructor!
|
|
|
|
// It will be called even if user doesn't have
|
|
|
|
// plugin allowed
|
|
|
|
}
|
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
PluginSpec TestPlugin::pluginSpec()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-02-19 16:01:51 +01:00
|
|
|
PluginSpec spec;
|
2012-02-20 15:56:26 +01:00
|
|
|
spec.name = "Example Plugin";
|
|
|
|
spec.info = "Example minimal plugin";
|
|
|
|
spec.description = "Very simple minimal plugin example";
|
2012-09-11 11:31:28 +02:00
|
|
|
spec.version = "0.1.6";
|
2012-02-19 16:01:51 +01:00
|
|
|
spec.author = "David Rosca <nowrep@gmail.com>";
|
2012-02-25 10:22:23 +01:00
|
|
|
spec.icon = QPixmap(":qupzilla.png");
|
2012-02-19 16:01:51 +01:00
|
|
|
spec.hasSettings = true;
|
|
|
|
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2013-05-24 11:03:16 +02:00
|
|
|
void TestPlugin::init(InitState state, const QString &settingsPath)
|
2012-02-19 16:01:51 +01:00
|
|
|
{
|
|
|
|
qDebug() << __FUNCTION__ << "called";
|
|
|
|
|
|
|
|
// This function is called right after plugin is loaded
|
|
|
|
// it will be called even if we return false from testPlugin()
|
|
|
|
// so it is recommended not to call any QupZilla function here
|
|
|
|
|
2012-04-02 16:00:08 +02:00
|
|
|
// Settings path is PROFILE/extensions/, in this directory
|
|
|
|
// you can use global .ini file for QSettings named "extensions.ini"
|
|
|
|
// or create new folder for your plugin and save in it anything you want
|
2013-05-24 11:03:16 +02:00
|
|
|
m_settingsPath = settingsPath;
|
|
|
|
|
|
|
|
// State can be either StartupInitState or LateInitState, and it
|
|
|
|
// indicates when the plugin have been loaded.
|
|
|
|
// Currently, it can be from preferences, or automatically at startup.
|
|
|
|
Q_UNUSED(state)
|
2012-02-22 18:33:44 +01:00
|
|
|
|
2012-04-01 16:36:38 +02:00
|
|
|
// Registering this plugin as a MousePressHandler.
|
|
|
|
// Otherwise mousePress() function will never be called
|
2012-02-22 18:33:44 +01:00
|
|
|
QZ_REGISTER_EVENT_HANDLER(PluginProxy::MousePressHandler);
|
2012-04-01 16:36:38 +02:00
|
|
|
|
|
|
|
// Adding new sidebar into application
|
|
|
|
SideBarManager::addSidebar("testplugin-sidebar", new TestPlugin_Sidebar(this));
|
2012-02-19 16:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestPlugin::unload()
|
|
|
|
{
|
2011-03-02 16:57:41 +01:00
|
|
|
qDebug() << __FUNCTION__ << "called";
|
2012-02-19 16:01:51 +01:00
|
|
|
|
|
|
|
// This function will be called when unloading plugin
|
|
|
|
// it will be also called if we return false from testPlugin()
|
2012-04-01 16:36:38 +02:00
|
|
|
|
|
|
|
// Removing sidebar from application
|
|
|
|
SideBarManager::removeSidebar("testplugin-sidebar");
|
2012-09-11 11:31:28 +02:00
|
|
|
|
|
|
|
// Deleting settings dialog if opened
|
|
|
|
delete m_settings.data();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TestPlugin::testPlugin()
|
|
|
|
{
|
2011-03-03 22:52:15 +01:00
|
|
|
//This function is called right after init()
|
2011-03-02 16:57:41 +01:00
|
|
|
//There should be some testing if plugin is loaded correctly
|
|
|
|
//If this function returns false, plugin is automatically unloaded
|
|
|
|
|
2014-01-26 19:12:34 +01:00
|
|
|
return (QupZilla::VERSION == QLatin1String("1.7.0"));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-19 16:01:51 +01:00
|
|
|
QTranslator* TestPlugin::getTranslator(const QString &locale)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-01 16:36:38 +02:00
|
|
|
// Loads translation according to locale file
|
|
|
|
// QString locale will contains "fr_FR.qm" for French locale
|
|
|
|
|
|
|
|
QTranslator* translator = new QTranslator(this);
|
2012-04-04 18:48:54 +02:00
|
|
|
translator->load(locale, ":/testplugin/locale/");
|
2011-03-02 16:57:41 +01:00
|
|
|
return translator;
|
|
|
|
}
|
|
|
|
|
2012-02-22 18:33:44 +01:00
|
|
|
void TestPlugin::showSettings(QWidget* parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-01 16:36:38 +02:00
|
|
|
// This function will be called from Preferences after clicking on Settings button.
|
|
|
|
// Settings button will be enabled if PluginSpec.hasSettings == true
|
|
|
|
|
2012-09-11 11:31:28 +02:00
|
|
|
if (!m_settings) {
|
|
|
|
m_settings = new QDialog(parent);
|
|
|
|
QPushButton* b = new QPushButton("Example Plugin v0.0.1");
|
|
|
|
QPushButton* closeButton = new QPushButton(tr("Close"));
|
|
|
|
QLabel* label = new QLabel();
|
|
|
|
label->setPixmap(QPixmap(":icons/other/about.png"));
|
|
|
|
|
|
|
|
QVBoxLayout* l = new QVBoxLayout(m_settings.data());
|
|
|
|
l->addWidget(label);
|
|
|
|
l->addWidget(b);
|
|
|
|
l->addWidget(closeButton);
|
|
|
|
m_settings.data()->setLayout(l);
|
|
|
|
|
|
|
|
m_settings.data()->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
m_settings.data()->setWindowTitle(tr("Example Plugin Settings"));
|
|
|
|
m_settings.data()->setWindowIcon(QIcon(":qupzilla.png"));
|
|
|
|
connect(closeButton, SIGNAL(clicked()), m_settings.data(), SLOT(close()));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_settings.data()->show();
|
|
|
|
m_settings.data()->raise();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:50:02 +01:00
|
|
|
void TestPlugin::populateWebViewMenu(QMenu* menu, WebView* view, const QWebHitTestResult &r)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-01 16:36:38 +02:00
|
|
|
// Called from WebView when creating context menu
|
|
|
|
|
2012-02-19 17:50:02 +01:00
|
|
|
m_view = view;
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QString title;
|
2011-12-16 19:07:36 +01:00
|
|
|
if (!r.imageUrl().isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
title += " on image";
|
2011-12-16 19:07:36 +01:00
|
|
|
}
|
2012-02-19 17:50:02 +01:00
|
|
|
|
2011-12-16 19:07:36 +01:00
|
|
|
if (!r.linkUrl().isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
title += " on link";
|
2011-12-16 19:07:36 +01:00
|
|
|
}
|
2012-02-19 17:50:02 +01:00
|
|
|
|
2012-02-20 18:28:09 +01:00
|
|
|
if (r.isContentEditable()) {
|
2011-12-16 19:07:36 +01:00
|
|
|
title += " on input";
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-19 17:50:02 +01:00
|
|
|
menu->addAction(tr("My first plugin action") + title, this, SLOT(actionSlot()));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-22 18:33:44 +01:00
|
|
|
bool TestPlugin::mousePress(const Qz::ObjectName &type, QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
qDebug() << "mousePress" << type << obj << event;
|
|
|
|
|
2012-04-01 16:36:38 +02:00
|
|
|
// Returning false means, that we don't want to block propagating this event
|
|
|
|
// Returning true may affect behaviour of QupZilla, so make sure you know what
|
|
|
|
// you are doing!
|
2012-02-22 18:33:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void TestPlugin::actionSlot()
|
|
|
|
{
|
2012-02-19 17:50:02 +01:00
|
|
|
QMessageBox::information(m_view, tr("Hello"), tr("First plugin action works :-)"));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-01 16:36:38 +02:00
|
|
|
// Export plugin macro
|
|
|
|
// This macro has to be only in class derived from PluginInterface
|
|
|
|
// Don't call it in other files!
|
2013-01-16 14:53:17 +01:00
|
|
|
|
|
|
|
#if QT_VERSION < 0x050000
|
2011-03-02 16:57:41 +01:00
|
|
|
Q_EXPORT_PLUGIN2(ExamplePlugin, TestPlugin)
|
2013-01-16 14:53:17 +01:00
|
|
|
#endif
|