2011-03-29 20:30:05 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-05 14:03:31 +01:00
|
|
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
2011-03-29 20:30:05 +02: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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "adblockicon.h"
|
2012-07-01 18:13:49 +02:00
|
|
|
#include "adblockrule.h"
|
2011-03-29 20:30:05 +02:00
|
|
|
#include "adblockmanager.h"
|
2012-08-09 19:02:25 +02:00
|
|
|
#include "adblocksubscription.h"
|
2012-02-04 18:45:59 +01:00
|
|
|
#include "mainapplication.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2011-03-29 20:30:05 +02:00
|
|
|
#include "webpage.h"
|
2012-01-21 23:19:38 +01:00
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "tabwidget.h"
|
2012-02-04 18:45:59 +01:00
|
|
|
#include "desktopnotificationsfactory.h"
|
2016-12-29 16:48:09 +01:00
|
|
|
#include "qztools.h"
|
2011-03-29 20:30:05 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
|
|
|
|
2018-01-05 14:03:31 +01:00
|
|
|
AdBlockIcon::AdBlockIcon(QObject *parent)
|
|
|
|
: AbstractButtonInterface(parent)
|
2011-03-29 20:30:05 +02:00
|
|
|
{
|
2018-01-05 14:03:31 +01:00
|
|
|
setTitle(tr("AdBlock"));
|
2018-01-05 21:15:28 +01:00
|
|
|
setIcon(QIcon(QSL(":icons/other/adblock.png")));
|
2011-03-29 20:30:05 +02:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
updateState();
|
2018-01-05 14:03:31 +01:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
connect(this, &AbstractButtonInterface::clicked, this, &AdBlockIcon::clicked);
|
|
|
|
connect(this, &AbstractButtonInterface::webPageChanged, this, &AdBlockIcon::webPageChanged);
|
|
|
|
connect(AdBlockManager::instance(), &AdBlockManager::enabledChanged, this, &AdBlockIcon::updateState);
|
2014-03-09 12:49:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AdBlockIcon::~AdBlockIcon()
|
|
|
|
{
|
2015-05-28 11:10:41 +02:00
|
|
|
for (int i = 0; i < m_blockedPopups.count(); ++i)
|
|
|
|
delete m_blockedPopups.at(i).first;
|
2011-03-29 20:30:05 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 14:03:31 +01:00
|
|
|
QString AdBlockIcon::id() const
|
|
|
|
{
|
|
|
|
return QSL("adblock-icon");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AdBlockIcon::name() const
|
|
|
|
{
|
|
|
|
return tr("AdBlock Icon");
|
|
|
|
}
|
|
|
|
|
2012-07-01 18:13:49 +02:00
|
|
|
void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url)
|
2012-02-04 18:45:59 +01:00
|
|
|
{
|
2012-09-04 12:42:45 +02:00
|
|
|
int index = ruleString.lastIndexOf(QLatin1String(" ("));
|
2012-07-01 18:13:49 +02:00
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString subscriptionName = ruleString.left(index);
|
|
|
|
const QString filter = ruleString.mid(index + 2, ruleString.size() - index - 3);
|
2012-07-01 18:13:49 +02:00
|
|
|
AdBlockSubscription* subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName);
|
|
|
|
if (filter.isEmpty() || !subscription) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-28 11:10:41 +02:00
|
|
|
QPair<AdBlockRule*, QUrl> pair;
|
|
|
|
pair.first = new AdBlockRule(filter, subscription);
|
2012-02-04 18:45:59 +01:00
|
|
|
pair.second = url;
|
|
|
|
m_blockedPopups.append(pair);
|
|
|
|
|
2012-07-08 14:03:50 +02:00
|
|
|
mApp->desktopNotifications()->showNotification(QPixmap(":html/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window."));
|
2018-01-05 21:15:28 +01:00
|
|
|
}
|
2012-02-04 18:45:59 +01:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
void AdBlockIcon::toggleCustomFilter()
|
|
|
|
{
|
|
|
|
QAction* action = qobject_cast<QAction*>(sender());
|
|
|
|
if (!action) {
|
|
|
|
return;
|
2012-02-04 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
const QString filter = action->data().toString();
|
|
|
|
AdBlockManager* manager = AdBlockManager::instance();
|
|
|
|
AdBlockCustomList* customList = manager->customList();
|
2012-02-04 18:45:59 +01:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
if (customList->containsFilter(filter)) {
|
|
|
|
customList->removeFilter(filter);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AdBlockRule* rule = new AdBlockRule(filter, customList);
|
|
|
|
customList->addRule(rule);
|
|
|
|
}
|
2012-02-04 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
void AdBlockIcon::updateState()
|
2011-03-29 20:30:05 +02:00
|
|
|
{
|
2018-01-05 21:15:28 +01:00
|
|
|
WebPage *page = webPage();
|
|
|
|
if (!page) {
|
|
|
|
setActive(false);
|
|
|
|
setToolTip(name());
|
|
|
|
return;
|
2012-02-04 18:45:59 +01:00
|
|
|
}
|
2018-01-05 21:15:28 +01:00
|
|
|
if (!AdBlockManager::instance()->isEnabled()) {
|
|
|
|
setActive(false);
|
|
|
|
setToolTip(tr("AdBlock is disabled"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!AdBlockManager::instance()->canRunOnScheme(page->url().scheme())) {
|
|
|
|
setActive(false);
|
|
|
|
setToolTip(tr("AdBlock is disabled on this site "));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setActive(true);
|
|
|
|
setToolTip(tr("AdBlock is active"));
|
2012-02-04 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
void AdBlockIcon::webPageChanged(WebPage *page)
|
2012-02-04 18:45:59 +01:00
|
|
|
{
|
2018-01-05 21:15:28 +01:00
|
|
|
updateState();
|
|
|
|
|
|
|
|
if (m_page) {
|
|
|
|
disconnect(m_page, &QWebEnginePage::urlChanged, this, &AdBlockIcon::updateState);
|
2012-02-04 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
m_page = page;
|
2012-02-04 18:45:59 +01:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
if (m_page) {
|
|
|
|
connect(m_page, &QWebEnginePage::urlChanged, this, &AdBlockIcon::updateState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockIcon::clicked(ClickController *controller)
|
|
|
|
{
|
|
|
|
WebPage *page = webPage();
|
2018-01-05 14:03:31 +01:00
|
|
|
if (!page) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-29 20:30:05 +02:00
|
|
|
AdBlockManager* manager = AdBlockManager::instance();
|
2012-08-09 19:02:25 +02:00
|
|
|
AdBlockCustomList* customList = manager->customList();
|
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QUrl pageUrl = page->url();
|
2011-03-29 20:30:05 +02:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
QMenu menu;
|
|
|
|
menu.addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog()));
|
|
|
|
menu.addSeparator();
|
2012-08-09 19:02:25 +02:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
if (!pageUrl.host().isEmpty() && manager->isEnabled() && manager->canRunOnScheme(pageUrl.scheme())) {
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString host = page->url().host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host();
|
|
|
|
const QString hostFilter = QString("@@||%1^$document").arg(host);
|
|
|
|
const QString pageFilter = QString("@@|%1|$document").arg(pageUrl.toString());
|
2012-08-09 19:02:25 +02:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
QAction* act = menu.addAction(tr("Disable on %1").arg(host));
|
2012-08-09 19:02:25 +02:00
|
|
|
act->setCheckable(true);
|
|
|
|
act->setChecked(customList->containsFilter(hostFilter));
|
|
|
|
act->setData(hostFilter);
|
|
|
|
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
|
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
act = menu.addAction(tr("Disable only on this page"));
|
2012-08-09 19:02:25 +02:00
|
|
|
act->setCheckable(true);
|
|
|
|
act->setChecked(customList->containsFilter(pageFilter));
|
|
|
|
act->setData(pageFilter);
|
|
|
|
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
|
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
menu.addSeparator();
|
2012-08-09 19:02:25 +02:00
|
|
|
}
|
|
|
|
|
2012-02-04 18:45:59 +01:00
|
|
|
if (!m_blockedPopups.isEmpty()) {
|
2018-01-05 21:15:28 +01:00
|
|
|
menu.addAction(tr("Blocked Popup Windows"))->setEnabled(false);
|
2012-02-04 18:45:59 +01:00
|
|
|
for (int i = 0; i < m_blockedPopups.count(); i++) {
|
2015-05-28 11:10:41 +02:00
|
|
|
const QPair<AdBlockRule*, QUrl> &pair = m_blockedPopups.at(i);
|
2012-07-01 18:13:49 +02:00
|
|
|
|
2012-02-04 18:45:59 +01:00
|
|
|
QString address = pair.second.toString().right(55);
|
2015-05-28 11:10:41 +02:00
|
|
|
QString actionText = tr("%1 with (%2)").arg(address, pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&"));
|
2012-07-01 18:13:49 +02:00
|
|
|
|
2018-01-05 21:15:28 +01:00
|
|
|
QAction* action = menu.addAction(actionText, manager, SLOT(showRule()));
|
2015-05-28 11:10:41 +02:00
|
|
|
action->setData(QVariant::fromValue((void*)pair.first));
|
2012-02-04 18:45:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 14:03:31 +01:00
|
|
|
menu.exec(controller->popupPosition(menu.sizeHint()));
|
|
|
|
}
|