Falkon Develop
Cross-platform Qt-based web browser
testplugin.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "testplugin.h"
19#include "testplugin_sidebar.h"
20#include "browserwindow.h"
21#include "webview.h"
22#include "pluginproxy.h"
23#include "mainapplication.h"
24#include "sidebar.h"
25#include "webhittestresult.h"
26#include "../config.h"
27
28#include <QMenu>
29#include <QPushButton>
30
32 : QObject()
33 , m_view(nullptr)
34{
35}
36
37void TestPlugin::init(InitState state, const QString &settingsPath)
38{
39 qDebug() << __FUNCTION__ << "called";
40
41 // This function is called right after plugin is loaded
42 // it will be called even if we return false from testPlugin()
43 // so it is recommended not to call any Falkon function here
44
45 // Settings path is PROFILE/extensions (without trailign slash),
46 // in this directory you can use global .ini file for QSettings
47 // named "extensions.ini" or create new folder for your plugin
48 // and save in it anything you want
49 m_settingsPath = settingsPath;
50
51 // State can be either StartupInitState or LateInitState, and it
52 // indicates when the plugin have been loaded.
53 // Currently, it can be from preferences, or automatically at startup.
54 // Plugins are loaded before first BrowserWindow is created.
55 Q_UNUSED(state)
56
57 // Registering this plugin as a MousePressHandler.
58 // Otherwise mousePress() function will never be called
59 mApp->plugins()->registerAppEventHandler(PluginProxy::MousePressHandler, this);
60
61 // Adding new sidebar into application
62 m_sideBar = new TestPlugin_Sidebar(this);
63 SideBarManager::addSidebar(QSL("testplugin-sidebar"), m_sideBar);
64}
65
67{
68 qDebug() << __FUNCTION__ << "called";
69
70 // This function will be called when unloading plugin
71 // it will be also called if we return false from testPlugin()
72
73 // Removing sidebar from application
75 delete m_sideBar;
76
77 // Deleting settings dialog if opened
78 delete m_settings.data();
79}
80
82{
83 // This function is called right after init()
84 // There should be some testing if plugin is loaded correctly
85 // If this function returns false, plugin is automatically unloaded
86
87 return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION));
88}
89
90void TestPlugin::showSettings(QWidget* parent)
91{
92 // This function will be called from Preferences after clicking on Settings button.
93 // Settings button will be enabled if PluginSpec.hasSettings == true
94
95 if (!m_settings) {
96 m_settings = new QDialog(parent);
97 auto* b = new QPushButton(QSL("Example Plugin v0.0.1"));
98 auto* closeButton = new QPushButton(tr("Close"));
99 auto* label = new QLabel();
100 label->setPixmap(QPixmap(QSL(":icons/other/about.svg")));
101
102 auto* l = new QVBoxLayout(m_settings.data());
103 l->addWidget(label);
104 l->addWidget(b);
105 l->addWidget(closeButton);
106 m_settings.data()->setLayout(l);
107
108 m_settings.data()->setAttribute(Qt::WA_DeleteOnClose);
109 m_settings.data()->setWindowTitle(tr("Example Plugin Settings"));
110 m_settings.data()->setWindowIcon(QIcon(QSL(":icons/falkon.svg")));
111 connect(closeButton, SIGNAL(clicked()), m_settings.data(), SLOT(close()));
112 }
113
114 m_settings.data()->show();
115 m_settings.data()->raise();
116}
117
119{
120 Q_UNUSED(r)
121
122 // Called from WebView when creating context menu
123
124 m_view = view;
125
126 QString title;
127 if (!r.imageUrl().isEmpty()) {
128 title += QSL(" on image");
129 }
130
131 if (!r.linkUrl().isEmpty()) {
132 title += QSL(" on link");
133 }
134
135 if (r.isContentEditable()) {
136 title += QSL(" on input");
137 }
138
139 menu->addAction(tr("My first plugin action") + title, this, SLOT(actionSlot()));
140}
141
142bool TestPlugin::mousePress(Qz::ObjectName type, QObject* obj, QMouseEvent* event)
143{
144 qDebug() << "mousePress" << type << obj << event;
145
146 // Returning false means, that we don't want to block propagating this event
147 // Returning true may affect behaviour of Falkon, so make sure you know what
148 // you are doing!
149 return false;
150}
151
152void TestPlugin::actionSlot()
153{
154 QMessageBox::information(m_view, tr("Hello"), tr("First plugin action works :-)"));
155}
@ MousePressHandler
Definition: pluginproxy.h:33
static void removeSidebar(SideBarInterface *interface)
Definition: sidebar.cpp:141
static void addSidebar(const QString &id, SideBarInterface *interface)
Definition: sidebar.cpp:136
void unload() override
Definition: testplugin.cpp:66
void populateWebViewMenu(QMenu *menu, WebView *view, const WebHitTestResult &r) override
Definition: testplugin.cpp:118
bool mousePress(Qz::ObjectName type, QObject *obj, QMouseEvent *event) override
Definition: testplugin.cpp:142
void init(InitState state, const QString &settingsPath) override
Definition: testplugin.cpp:37
bool testPlugin() override
Definition: testplugin.cpp:81
void showSettings(QWidget *parent) override
Definition: testplugin.cpp:90
bool isContentEditable() const
#define mApp
ObjectName
Definition: qzcommon.h:89
FALKON_EXPORT const char * VERSION
Definition: qzcommon.cpp:26
State state
#define QSL(x)
Definition: qzcommon.h:40