From 06c5118e6fbf56a7a2103c9501e39f72831b661d Mon Sep 17 00:00:00 2001 From: nowrep Date: Thu, 9 Aug 2012 19:02:25 +0200 Subject: [PATCH] AdBlock: New options in AdBlock menu to disable it for domain - and also "only for this page" --- src/lib/adblock/adblockicon.cpp | 48 ++++++++++++++++++++++++- src/lib/adblock/adblockicon.h | 1 + src/lib/adblock/adblockmanager.cpp | 13 +++++++ src/lib/adblock/adblockmanager.h | 3 ++ src/lib/adblock/adblockrule.cpp | 8 ++--- src/lib/adblock/adblocksubscription.cpp | 24 +++++++++++++ src/lib/adblock/adblocksubscription.h | 3 ++ 7 files changed, 95 insertions(+), 5 deletions(-) diff --git a/src/lib/adblock/adblockicon.cpp b/src/lib/adblock/adblockicon.cpp index dbc65156e..99b421fc8 100644 --- a/src/lib/adblock/adblockicon.cpp +++ b/src/lib/adblock/adblockicon.cpp @@ -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 entries = p_QupZilla->weView()->page()->adBlockedEntries(); + QList 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(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; diff --git a/src/lib/adblock/adblockicon.h b/src/lib/adblock/adblockicon.h index 240c4aa6c..489306a34 100644 --- a/src/lib/adblock/adblockicon.h +++ b/src/lib/adblock/adblockicon.h @@ -45,6 +45,7 @@ public slots: private slots: void showMenu(const QPoint &pos); + void toggleCustomFilter(); void animateIcon(); void stopAnimation(); diff --git a/src/lib/adblock/adblockmanager.cpp b/src/lib/adblock/adblockmanager.cpp index 1c37840c3..a98bee19b 100644 --- a/src/lib/adblock/adblockmanager.cpp +++ b/src/lib/adblock/adblockmanager.cpp @@ -165,6 +165,19 @@ bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) return true; } +AdBlockCustomList* AdBlockManager::customList() const +{ + foreach(AdBlockSubscription * subscription, m_subscriptions) { + AdBlockCustomList* list = qobject_cast(subscription); + + if (list) { + return list; + } + } + + return 0; +} + void AdBlockManager::load() { if (m_loaded) { diff --git a/src/lib/adblock/adblockmanager.h b/src/lib/adblock/adblockmanager.h index 289bd9f82..5051c28f4 100644 --- a/src/lib/adblock/adblockmanager.h +++ b/src/lib/adblock/adblockmanager.h @@ -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(); diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index 2eee7ece6..d339e8bfe 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -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); } diff --git a/src/lib/adblock/adblocksubscription.cpp b/src/lib/adblock/adblocksubscription.cpp index 8fc05a1de..c8385f6a0 100644 --- a/src/lib/adblock/adblocksubscription.cpp +++ b/src/lib/adblock/adblocksubscription.cpp @@ -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); diff --git a/src/lib/adblock/adblocksubscription.h b/src/lib/adblock/adblocksubscription.h index e80a7406b..0ffdfb2f4 100644 --- a/src/lib/adblock/adblocksubscription.h +++ b/src/lib/adblock/adblocksubscription.h @@ -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);