Falkon Develop
Cross-platform Qt-based web browser
adblockicon.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 "adblockicon.h"
19#include "adblockrule.h"
20#include "adblockmanager.h"
21#include "adblocksubscription.h"
22#include "mainapplication.h"
23#include "browserwindow.h"
24#include "webview.h"
25#include "tabwidget.h"
27#include "qztools.h"
28
29#include <QMenu>
30
33{
34 setTitle(tr("AdBlock"));
35 setIcon(QIcon(QSL(":adblock/data/adblock.png")));
36
37 updateState();
38
39 connect(this, &AbstractButtonInterface::clicked, this, &AdBlockIcon::clicked);
40 connect(this, &AbstractButtonInterface::webViewChanged, this, &AdBlockIcon::webViewChanged);
41 connect(AdBlockManager::instance(), &AdBlockManager::enabledChanged, this, &AdBlockIcon::updateState);
42 connect(AdBlockManager::instance(), &AdBlockManager::blockedRequestsChanged, this, &AdBlockIcon::blockedRequestsChanged);
43}
44
45QString AdBlockIcon::id() const
46{
47 return QSL("adblock-icon");
48}
49
50QString AdBlockIcon::name() const
51{
52 return tr("AdBlock Icon");
53}
54
55void AdBlockIcon::toggleCustomFilter()
56{
57 auto* action = qobject_cast<QAction*>(sender());
58 if (!action) {
59 return;
60 }
61
62 const QString filter = action->data().toString();
64 AdBlockCustomList* customList = manager->customList();
65
66 if (customList->containsFilter(filter)) {
67 customList->removeFilter(filter);
68 }
69 else {
70 auto* rule = new AdBlockRule(filter, customList);
71 customList->addRule(rule);
72 }
73}
74
75void AdBlockIcon::updateState()
76{
77 WebView *view = webView();
78 if (!view) {
79 setActive(false);
81 setBadgeText(QString());
82 return;
83 }
84 if (!AdBlockManager::instance()->isEnabled()) {
85 setActive(false);
86 setToolTip(tr("AdBlock is disabled"));
87 setBadgeText(QString());
88 return;
89 }
90 if (!AdBlockManager::instance()->canRunOnScheme(view->url().scheme())) {
91 setActive(false);
92 setToolTip(tr("AdBlock is disabled on this site "));
93 setBadgeText(QString());
94 return;
95 }
96
97 setActive(true);
98 setToolTip(tr("AdBlock is active"));
99 updateBadgeText();
100}
101
102void AdBlockIcon::updateBadgeText()
103{
104 WebView *view = webView();
105 if (!view) {
106 return;
107 }
108 const int count = AdBlockManager::instance()->blockedRequestsForUrl(view->url()).count();
109 if (count > 0) {
110 setBadgeText(QString::number(count));
111 } else {
112 setBadgeText(QString());
113 }
114}
115
116void AdBlockIcon::webViewChanged(WebView *view)
117{
118 updateState();
119
120 if (m_view) {
121 disconnect(m_view.data(), &WebView::urlChanged, this, &AdBlockIcon::updateState);
122 }
123
124 m_view = view;
125
126 if (m_view) {
127 connect(m_view.data(), &WebView::urlChanged, this, &AdBlockIcon::updateState);
128 }
129}
130
131void AdBlockIcon::clicked(ClickController *controller)
132{
133 WebView *view = webView();
134 if (!view) {
135 return;
136 }
137
139 AdBlockCustomList* customList = manager->customList();
140
141 const QUrl pageUrl = view->url();
142
143 auto *menu = new QMenu();
144 menu->setAttribute(Qt::WA_DeleteOnClose);
145 menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog()));
146 menu->addSeparator();
147
148 if (!pageUrl.host().isEmpty() && manager->isEnabled() && manager->canRunOnScheme(pageUrl.scheme())) {
149 const QString host = view->url().host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host();
150 const QString hostFilter = QSL("@@||%1^$document").arg(host);
151 const QString pageFilter = QSL("@@|%1|$document").arg(pageUrl.toString());
152
153 QAction* act = menu->addAction(tr("Disable on %1").arg(host));
154 act->setCheckable(true);
155 act->setChecked(customList->containsFilter(hostFilter));
156 act->setData(hostFilter);
157 connect(act, &QAction::triggered, this, &AdBlockIcon::toggleCustomFilter);
158
159 act = menu->addAction(tr("Disable only on this page"));
160 act->setCheckable(true);
161 act->setChecked(customList->containsFilter(pageFilter));
162 act->setData(pageFilter);
163 connect(act, &QAction::triggered, this, &AdBlockIcon::toggleCustomFilter);
164 }
165
166 connect(menu, &QMenu::aboutToHide, this, [=]() {
167 controller->popupClosed();
168 });
169
170 menu->popup(controller->popupPosition(menu->sizeHint()));
171}
172
173void AdBlockIcon::blockedRequestsChanged(const QUrl &url)
174{
175 WebView *view = webView();
176 if (!view || url != view->url()) {
177 return;
178 }
179 updateState();
180}
void webViewChanged(WebView *view)
void clicked(AbstractButtonInterface::ClickController *controller)
void setTitle(const QString &text)
void setBadgeText(const QString &badgeText)
void setToolTip(const QString &toolTip)
void setIcon(const QIcon &icon)
int addRule(AdBlockRule *rule) override
bool removeFilter(const QString &filter)
bool containsFilter(const QString &filter) const
QString id() const override
Definition: adblockicon.cpp:45
QString name() const override
Definition: adblockicon.cpp:50
AdBlockIcon(QObject *parent=nullptr)
Definition: adblockicon.cpp:31
bool canRunOnScheme(const QString &scheme) const
void enabledChanged(bool enabled)
void blockedRequestsChanged(const QUrl &url)
AdBlockCustomList * customList() const
QVector< AdBlockedRequest > blockedRequestsForUrl(const QUrl &url) const
static AdBlockManager * instance()
bool isEnabled() const
#define QSL(x)
Definition: qzcommon.h:40