Falkon Develop
Cross-platform Qt-based web browser
sbi_javascripticon.cpp
Go to the documentation of this file.
1/* ============================================================
2* StatusBarIcons - Extra icons in statusbar for Falkon
3* Copyright (C) 2013-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 "sbi_javascripticon.h"
19#include "browserwindow.h"
20#include "tabwidget.h"
21#include "tabbedwebview.h"
22#include "webpage.h"
23#include "jsoptions.h"
24#include "mainapplication.h"
25
26#include <QGraphicsColorizeEffect>
27#include <QWebEngineSettings>
28#include <QMenu>
29
31 : SBI_Icon(window)
32{
33 setObjectName(QSL("sbi_javascripticon"));
34 setCursor(Qt::PointingHandCursor);
35 setToolTip(tr("Modify JavaScript settings per-site and globally"));
36
37 m_icon = QIcon::fromTheme(QSL("application-x-javascript"), QIcon(QSL(":sbi/data/javascript.png")));
38 setPixmap(m_icon.pixmap(16));
39
40 connect(m_window->tabWidget(), SIGNAL(currentChanged(int)), this, SLOT(updateIcon()));
41 connect(this, &ClickableLabel::clicked, this, &SBI_JavaScriptIcon::showMenu);
42
43 updateIcon();
44}
45
46void SBI_JavaScriptIcon::showMenu(const QPoint &point)
47{
48 QFont boldFont = font();
49 boldFont.setBold(true);
50
51 QMenu menu;
52 menu.addAction(m_icon, tr("Current Page Settings"))->setFont(boldFont);
53
54 if (testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled)) {
55 menu.addAction(tr("Disable JavaScript (temporarily)"), this, &SBI_JavaScriptIcon::toggleJavaScript);
56 }
57 else {
58 menu.addAction(tr("Enable JavaScript (temporarily)"), this, &SBI_JavaScriptIcon::toggleJavaScript);
59 }
60
61 // JavaScript needs to be always enabled for falkon: sites
62 if (currentPage() && currentPage()->url().scheme() == QLatin1String("falkon")) {
63 menu.actions().at(1)->setEnabled(false);
64 }
65
66 menu.addSeparator();
67 menu.addAction(m_icon, tr("Global Settings"))->setFont(boldFont);
68 menu.addAction(tr("Manage JavaScript settings"), this, &SBI_JavaScriptIcon::openJavaScriptSettings);
69 menu.exec(point);
70}
71
72void SBI_JavaScriptIcon::updateIcon()
73{
74 if (testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled)) {
75 setGraphicsEffect(nullptr);
76 }
77 else {
78 auto* effect = new QGraphicsColorizeEffect(this);
79 effect->setColor(Qt::gray);
80 setGraphicsEffect(effect);
81 }
82}
83
84void SBI_JavaScriptIcon::toggleJavaScript()
85{
86 WebPage *page = currentPage();
87 if (!page) {
88 return;
89 }
90
91 bool current = testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled);
92 setCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled, !current);
93
94 m_settings[page] = !current;
95
96 connect(page, &WebPage::navigationRequestAccepted, this, [=](const QUrl &, WebPage::NavigationType, bool isMainFrame) {
97 if (isMainFrame) {
98 page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, m_settings[page]);
99 }
100 });
101
102 m_window->weView()->reload();
103
104 updateIcon();
105}
106
107void SBI_JavaScriptIcon::openJavaScriptSettings()
108{
109 auto* dialog = new JsOptions(m_window);
110 dialog->open();
111}
TabWidget * tabWidget() const
TabbedWebView * weView() const
void clicked(QPoint)
BrowserWindow * m_window
Definition: sbi_icon.h:42
void setCurrentPageWebAttribute(QWebEngineSettings::WebAttribute attr, bool value)
Definition: sbi_icon.cpp:35
WebPage * currentPage() const
Definition: sbi_icon.cpp:51
bool testCurrentPageWebAttribute(QWebEngineSettings::WebAttribute attr) const
Definition: sbi_icon.cpp:30
SBI_JavaScriptIcon(BrowserWindow *window)
void navigationRequestAccepted(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
#define QSL(x)
Definition: qzcommon.h:40