File indexing completed on 2024-05-19 04:59:20
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "testplugin.h" 0019 #include "testplugin_sidebar.h" 0020 #include "browserwindow.h" 0021 #include "webview.h" 0022 #include "pluginproxy.h" 0023 #include "mainapplication.h" 0024 #include "sidebar.h" 0025 #include "webhittestresult.h" 0026 #include "../config.h" 0027 0028 #include <QMenu> 0029 #include <QPushButton> 0030 0031 TestPlugin::TestPlugin() 0032 : QObject() 0033 , m_view(nullptr) 0034 { 0035 } 0036 0037 void TestPlugin::init(InitState state, const QString &settingsPath) 0038 { 0039 qDebug() << __FUNCTION__ << "called"; 0040 0041 // This function is called right after plugin is loaded 0042 // it will be called even if we return false from testPlugin() 0043 // so it is recommended not to call any Falkon function here 0044 0045 // Settings path is PROFILE/extensions (without trailign slash), 0046 // in this directory you can use global .ini file for QSettings 0047 // named "extensions.ini" or create new folder for your plugin 0048 // and save in it anything you want 0049 m_settingsPath = settingsPath; 0050 0051 // State can be either StartupInitState or LateInitState, and it 0052 // indicates when the plugin have been loaded. 0053 // Currently, it can be from preferences, or automatically at startup. 0054 // Plugins are loaded before first BrowserWindow is created. 0055 Q_UNUSED(state) 0056 0057 // Registering this plugin as a MousePressHandler. 0058 // Otherwise mousePress() function will never be called 0059 mApp->plugins()->registerAppEventHandler(PluginProxy::MousePressHandler, this); 0060 0061 // Adding new sidebar into application 0062 m_sideBar = new TestPlugin_Sidebar(this); 0063 SideBarManager::addSidebar(QSL("testplugin-sidebar"), m_sideBar); 0064 } 0065 0066 void TestPlugin::unload() 0067 { 0068 qDebug() << __FUNCTION__ << "called"; 0069 0070 // This function will be called when unloading plugin 0071 // it will be also called if we return false from testPlugin() 0072 0073 // Removing sidebar from application 0074 SideBarManager::removeSidebar(m_sideBar); 0075 delete m_sideBar; 0076 0077 // Deleting settings dialog if opened 0078 delete m_settings.data(); 0079 } 0080 0081 bool TestPlugin::testPlugin() 0082 { 0083 // This function is called right after init() 0084 // There should be some testing if plugin is loaded correctly 0085 // If this function returns false, plugin is automatically unloaded 0086 0087 return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION)); 0088 } 0089 0090 void TestPlugin::showSettings(QWidget* parent) 0091 { 0092 // This function will be called from Preferences after clicking on Settings button. 0093 // Settings button will be enabled if PluginSpec.hasSettings == true 0094 0095 if (!m_settings) { 0096 m_settings = new QDialog(parent); 0097 auto* b = new QPushButton(QSL("Example Plugin v0.0.1")); 0098 auto* closeButton = new QPushButton(tr("Close")); 0099 auto* label = new QLabel(); 0100 label->setPixmap(QPixmap(QSL(":icons/other/about.svg"))); 0101 0102 auto* l = new QVBoxLayout(m_settings.data()); 0103 l->addWidget(label); 0104 l->addWidget(b); 0105 l->addWidget(closeButton); 0106 m_settings.data()->setLayout(l); 0107 0108 m_settings.data()->setAttribute(Qt::WA_DeleteOnClose); 0109 m_settings.data()->setWindowTitle(tr("Example Plugin Settings")); 0110 m_settings.data()->setWindowIcon(QIcon(QSL(":icons/falkon.svg"))); 0111 connect(closeButton, SIGNAL(clicked()), m_settings.data(), SLOT(close())); 0112 } 0113 0114 m_settings.data()->show(); 0115 m_settings.data()->raise(); 0116 } 0117 0118 void TestPlugin::populateWebViewMenu(QMenu* menu, WebView* view, const WebHitTestResult &r) 0119 { 0120 Q_UNUSED(r) 0121 0122 // Called from WebView when creating context menu 0123 0124 m_view = view; 0125 0126 QString title; 0127 if (!r.imageUrl().isEmpty()) { 0128 title += QSL(" on image"); 0129 } 0130 0131 if (!r.linkUrl().isEmpty()) { 0132 title += QSL(" on link"); 0133 } 0134 0135 if (r.isContentEditable()) { 0136 title += QSL(" on input"); 0137 } 0138 0139 menu->addAction(tr("My first plugin action") + title, this, SLOT(actionSlot())); 0140 } 0141 0142 bool TestPlugin::mousePress(Qz::ObjectName type, QObject* obj, QMouseEvent* event) 0143 { 0144 qDebug() << "mousePress" << type << obj << event; 0145 0146 // Returning false means, that we don't want to block propagating this event 0147 // Returning true may affect behaviour of Falkon, so make sure you know what 0148 // you are doing! 0149 return false; 0150 } 0151 0152 void TestPlugin::actionSlot() 0153 { 0154 QMessageBox::information(m_view, tr("Hello"), tr("First plugin action works :-)")); 0155 }