From 3284722993773ab7d9a54bfcef36dab0c804f83d Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 3 Feb 2017 14:24:29 +0100 Subject: Listing rules in Blocker dialog Slightly better rule parsing --- src/forms/blockerdialog.cpp | 9 +++++ src/forms/blockerdialog.ui | 32 ++++++++++++++--- src/webengine/blockerrule.cpp | 82 +++++++++++++++++++++++++++++++++++++++---- src/webengine/blockerrule.h | 17 +++++++-- test/blocklist.txt | 8 +++-- 5 files changed, 134 insertions(+), 14 deletions(-) diff --git a/src/forms/blockerdialog.cpp b/src/forms/blockerdialog.cpp index da5fae6..5377c97 100644 --- a/src/forms/blockerdialog.cpp +++ b/src/forms/blockerdialog.cpp @@ -23,6 +23,7 @@ #include "settings.h" #include +#include BlockerDialog::BlockerDialog(QWidget *parent) : QDialog(parent), @@ -43,6 +44,14 @@ BlockerDialog::BlockerDialog(QWidget *parent) : ui->version->setText(m_subscription->version()); ui->lastModified->setText(m_subscription->lastModified().toString()); ui->expires->setText(m_subscription->expires().toString()); + + // show subscription items + for(BlockerRule *rule : m_subscription->urlBlacklist()) { + ui->blacklist_listWidget->addItem(rule->pattern()); + } + for(BlockerRule *rule : m_subscription->urlWhitelist()) { + ui->whitelist_listWidget->addItem(rule->pattern()); + } } BlockerDialog::~BlockerDialog() diff --git a/src/forms/blockerdialog.ui b/src/forms/blockerdialog.ui index dc0a1f4..a84dac8 100644 --- a/src/forms/blockerdialog.ui +++ b/src/forms/blockerdialog.ui @@ -11,13 +11,13 @@ - Dialog + Blocker - GroupBox + Subscription @@ -116,7 +116,31 @@ - + + + 0 + + + + URL Blacklist + + + + + + + + + + URL Whitelist + + + + + + + + @@ -124,7 +148,7 @@ Qt::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::Close diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp index 3a35ab6..3146854 100644 --- a/src/webengine/blockerrule.cpp +++ b/src/webengine/blockerrule.cpp @@ -20,20 +20,55 @@ #include "blockerrule.h" +/* AdBlock filter reference + * https://adblockplus.org/en/filters + * https://adblockplus.org/en/filter-cheatsheet + */ + BlockerRule::BlockerRule(QString rule, QObject *parent) : QObject(parent) { QString pattern = rule; - if(rule.startsWith("@@")) { + // Empty rule or comment + if(pattern.trimmed().isEmpty() || pattern.startsWith("!")) { + m_type = RuleType::Invalid; + return; + } + + // Exception + if(pattern.startsWith("@@")) { m_exception = true; pattern = pattern.remove(0, 2); - } else { - m_exception = false; } - ruleExpression.setPattern(pattern); - m_valid = true; + // Domain + if(pattern.startsWith("||")) { + m_type = RuleType::DomainMatch; + m_domain = pattern.mid(2, pattern.indexOf("^")-2); + return; + } + + // Regular expression + if(rule.startsWith("/") && rule.endsWith("/")) { + m_type = RuleType::RegularExpressionMatch; + ruleExpression.setPattern(pattern); + return; + } + + // Regular rule + ruleExpression.setPattern(fromWildcardMatch(pattern)); + m_type = RuleType::WildcardMatch; +} + +QString BlockerRule::pattern() const +{ + return ruleExpression.pattern(); +} + +QString BlockerRule::domain() const +{ + return m_domain; } bool BlockerRule::match(const QUrl &url) @@ -43,9 +78,44 @@ bool BlockerRule::match(const QUrl &url) bool BlockerRule::isValid() { - return m_valid; + if(m_type == RuleType::Invalid) { + return false; + } else { + return true; + } } bool BlockerRule::isException() { return m_exception; } + +QString BlockerRule::fromWildcardMatch(const QString &pattern) +{ + QString parsed; + + for(int i=0; i