1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-24 04:36:34 +01:00

AdBlock: New options in AdBlock menu to disable it for domain

- and also "only for this page"
This commit is contained in:
nowrep 2012-08-09 19:02:25 +02:00
parent 2969c80141
commit 06c5118e6f
7 changed files with 95 additions and 5 deletions

View File

@ -18,6 +18,7 @@
#include "adblockicon.h"
#include "adblockrule.h"
#include "adblockmanager.h"
#include "adblocksubscription.h"
#include "mainapplication.h"
#include "qupzilla.h"
#include "webpage.h"
@ -102,9 +103,34 @@ void AdBlockIcon::createMenu(QMenu* menu)
menu->clear();
AdBlockManager* manager = AdBlockManager::instance();
AdBlockCustomList* customList = manager->customList();
WebPage* page = p_QupZilla->weView()->page();
const QUrl &pageUrl = page->url();
menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog()));
menu->addSeparator();
if (!pageUrl.isEmpty()) {
const QString &host = page->url().host().contains("www.") ? pageUrl.host().mid(4) : pageUrl.host();
const QString &hostFilter = QString("@@||%1^$document").arg(host);
const QString &pageFilter = QString("@@|%1|$document").arg(pageUrl.toString());
QAction* act = menu->addAction(tr("Disable on %1").arg(host));
act->setCheckable(true);
act->setChecked(customList->containsFilter(hostFilter));
act->setData(hostFilter);
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
act = menu->addAction(tr("Disable only on this page"));
act->setCheckable(true);
act->setChecked(customList->containsFilter(pageFilter));
act->setData(pageFilter);
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
menu->addSeparator();
}
if (!m_blockedPopups.isEmpty()) {
menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false);
for (int i = 0; i < m_blockedPopups.count(); i++) {
@ -119,7 +145,7 @@ void AdBlockIcon::createMenu(QMenu* menu)
}
menu->addSeparator();
QList<WebPage::AdBlockedEntry> entries = p_QupZilla->weView()->page()->adBlockedEntries();
QList<WebPage::AdBlockedEntry> entries = page->adBlockedEntries();
if (entries.isEmpty()) {
menu->addAction(tr("No content blocked"))->setEnabled(false);
}
@ -143,6 +169,26 @@ void AdBlockIcon::showMenu(const QPoint &pos)
menu.exec(pos);
}
void AdBlockIcon::toggleCustomFilter()
{
QAction* action = qobject_cast<QAction*>(sender());
if (!action) {
return;
}
const QString &filter = action->data().toString();
AdBlockManager* manager = AdBlockManager::instance();
AdBlockCustomList* customList = manager->customList();
if (customList->containsFilter(filter)) {
customList->removeFilter(filter);
}
else {
AdBlockRule rule(filter, customList);
customList->addRule(rule);
}
}
void AdBlockIcon::animateIcon()
{
++m_timerTicks;

View File

@ -45,6 +45,7 @@ public slots:
private slots:
void showMenu(const QPoint &pos);
void toggleCustomFilter();
void animateIcon();
void stopAnimation();

View File

@ -165,6 +165,19 @@ bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription)
return true;
}
AdBlockCustomList* AdBlockManager::customList() const
{
foreach(AdBlockSubscription * subscription, m_subscriptions) {
AdBlockCustomList* list = qobject_cast<AdBlockCustomList*>(subscription);
if (list) {
return list;
}
}
return 0;
}
void AdBlockManager::load()
{
if (m_loaded) {

View File

@ -29,6 +29,7 @@ class QNetworkReply;
class QNetworkRequest;
class AdBlockDialog;
class AdBlockCustomList;
class AdBlockSubscription;
class QT_QUPZILLA_EXPORT AdBlockManager : public QObject
@ -60,6 +61,8 @@ public:
AdBlockSubscription* addSubscription(const QString &title, const QString &url);
bool removeSubscription(AdBlockSubscription* subscription);
AdBlockCustomList* customList() const;
public slots:
void setEnabled(bool enabled);
void showRule();

View File

@ -203,15 +203,15 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do
bool matched = false;
if (m_useRegExp) {
matched = (m_regExp.indexIn(encodedUrl) != -1);
}
else if (m_useDomainMatch) {
if (m_useDomainMatch) {
matched = domain.endsWith(m_matchString);
}
else if (m_useEndsMatch) {
matched = encodedUrl.endsWith(m_matchString, m_caseSensitivity);
}
else if (m_useRegExp) {
matched = (m_regExp.indexIn(encodedUrl) != -1);
}
else {
matched = encodedUrl.contains(m_matchString, m_caseSensitivity);
}

View File

@ -443,6 +443,30 @@ bool AdBlockCustomList::canBeRemoved() const
return false;
}
bool AdBlockCustomList::containsFilter(const QString &filter) const
{
foreach(const AdBlockRule & rule, m_rules) {
if (rule.filter() == filter) {
return true;
}
}
return false;
}
bool AdBlockCustomList::removeFilter(const QString &filter)
{
for (int i = 0; i < m_rules.count(); ++i) {
const AdBlockRule &rule = m_rules.at(i);
if (rule.filter() == filter) {
return removeRule(i);
}
}
return false;
}
int AdBlockCustomList::addRule(const AdBlockRule &rule)
{
m_rules.append(rule);

View File

@ -154,6 +154,9 @@ public:
bool canEditRules() const;
bool canBeRemoved() const;
bool containsFilter(const QString &filter) const;
bool removeFilter(const QString &filter);
int addRule(const AdBlockRule &rule);
bool removeRule(int offset);
const AdBlockRule* replaceRule(const AdBlockRule &rule, int offset);