aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-02-03 14:24:29 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-02-03 14:24:29 +0100
commit3284722993773ab7d9a54bfcef36dab0c804f83d (patch)
treeab231c385e41388ba141e389901921e5b0624312
parentAdded loading bar to status bar (diff)
downloadsmolbote-3284722993773ab7d9a54bfcef36dab0c804f83d.tar.xz
Listing rules in Blocker dialog
Slightly better rule parsing
-rw-r--r--src/forms/blockerdialog.cpp9
-rw-r--r--src/forms/blockerdialog.ui32
-rw-r--r--src/webengine/blockerrule.cpp82
-rw-r--r--src/webengine/blockerrule.h17
-rw-r--r--test/blocklist.txt8
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 <QLabel>
+#include <QListWidget>
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 @@
</rect>
</property>
<property name="windowTitle">
- <string>Dialog</string>
+ <string>Blocker</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
- <string>GroupBox</string>
+ <string>Subscription</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
@@ -116,7 +116,31 @@
</widget>
</item>
<item>
- <widget class="QListWidget" name="listWidget"/>
+ <widget class="QTabWidget" name="tabWidget">
+ <property name="currentIndex">
+ <number>0</number>
+ </property>
+ <widget class="QWidget" name="blacklist_tab">
+ <attribute name="title">
+ <string>URL Blacklist</string>
+ </attribute>
+ <layout class="QVBoxLayout" name="verticalLayout_2">
+ <item>
+ <widget class="QListWidget" name="blacklist_listWidget"/>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QWidget" name="whitelist_tab">
+ <attribute name="title">
+ <string>URL Whitelist</string>
+ </attribute>
+ <layout class="QVBoxLayout" name="verticalLayout_3">
+ <item>
+ <widget class="QListWidget" name="whitelist_listWidget"/>
+ </item>
+ </layout>
+ </widget>
+ </widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
@@ -124,7 +148,7 @@
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
- <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+ <set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
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<pattern.length(); i++) {
+ const QChar c = pattern.at(i);
+ switch (c.toLatin1()) {
+ case '*':
+ parsed.append(".*");
+ break;
+ case '^':
+ parsed.append("(?:[^\\w\\d\\_\\-\\.\\%]|$)");
+ break;
+ case '|':
+ if(i == 0) {
+ // beginning of string
+ parsed.append('^');
+ } else {
+ // end of string
+ parsed.append('$');
+ }
+ break;
+ default:
+ parsed.append(c);
+ break;
+ }
+ }
+
+ return parsed;
+}
diff --git a/src/webengine/blockerrule.h b/src/webengine/blockerrule.h
index cf1dc19..95d82b7 100644
--- a/src/webengine/blockerrule.h
+++ b/src/webengine/blockerrule.h
@@ -29,8 +29,17 @@ class BlockerRule : public QObject
{
Q_OBJECT
public:
+ enum RuleType {
+ Invalid,
+ DomainMatch,
+ RegularExpressionMatch,
+ WildcardMatch
+ };
+
explicit BlockerRule(QString rule, QObject *parent = 0);
+ QString pattern() const;
+ QString domain() const;
bool match(const QUrl &url);
bool isValid();
bool isException();
@@ -40,8 +49,12 @@ signals:
public slots:
private:
- bool m_valid;
- bool m_exception;
+ // TODO: subclass QRegularExpression and move this there
+ QString fromWildcardMatch(const QString &pattern);
+
+ RuleType m_type;
+ bool m_exception = false;
+ QString m_domain;
QRegularExpression ruleExpression;
};
diff --git a/test/blocklist.txt b/test/blocklist.txt
index 1c22798..1c3af75 100644
--- a/test/blocklist.txt
+++ b/test/blocklist.txt
@@ -12,5 +12,9 @@
!
! -----------------------General advert blocking filters-----------------------!
!css
-1@@s1252.css
-!@@t1252.css
+!@@s1256.css
+!@@t1256.css
+
+!font/*woff
+
+!duckduckgo.com^