aboutsummaryrefslogtreecommitdiff
path: root/src/webengine
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-01-24 16:09:07 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-01-24 16:09:07 +0100
commit0250559bcf5764fb8cf3a8ccc4e330b8ed855f96 (patch)
tree2283350e44f78e83a8c0257b66c184939cd95a98 /src/webengine
parentMade Profile menu a regular menu (diff)
downloadsmolbote-0250559bcf5764fb8cf3a8ccc4e330b8ed855f96.tar.xz
Blocker UI
Diffstat (limited to 'src/webengine')
-rw-r--r--src/webengine/adblockinterceptor.cpp56
-rw-r--r--src/webengine/blockerrule.cpp (renamed from src/webengine/adblockrule.cpp)6
-rw-r--r--src/webengine/blockerrule.h (renamed from src/webengine/adblockrule.h)4
-rw-r--r--src/webengine/blockersubscription.cpp103
-rw-r--r--src/webengine/blockersubscription.h41
-rw-r--r--src/webengine/urlinterceptor.cpp19
-rw-r--r--src/webengine/urlinterceptor.h (renamed from src/webengine/adblockinterceptor.h)4
7 files changed, 168 insertions, 65 deletions
diff --git a/src/webengine/adblockinterceptor.cpp b/src/webengine/adblockinterceptor.cpp
deleted file mode 100644
index 02bf2f4..0000000
--- a/src/webengine/adblockinterceptor.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-#include "adblockinterceptor.h"
-
-#include <QFile>
-#include <QTextStream>
-
-AdBlockInterceptor::AdBlockInterceptor(QObject *parent) :
- QWebEngineUrlRequestInterceptor(parent)
-{
- loadSubscription("blocklist.txt");
-}
-
-void AdBlockInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
-{
- bool blocked = false;
- for(AdBlockRule *rule : m_urlBlacklist) {
- if(rule->match(info.requestUrl())) {
- info.block(true);
- blocked = true;
- }
- }
-
- qDebug("%i %i %i %s %s", blocked, info.navigationType(), info.resourceType(), qUtf8Printable(info.requestMethod()), qUtf8Printable(info.requestUrl().toString()));
-}
-
-int AdBlockInterceptor::loadSubscription(const QString &subpath)
-{
- QFile subfile(subpath);
- if(!subfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
- qDebug("AdBlockInterceptor: cannot load subscription: %s", qUtf8Printable(subpath));
- return -1;
- }
-
- QTextStream subscription(&subfile);
-
- QString header = subscription.readLine();
- if(header != "[Adblock Plus 2.0]") {
- qDebug("AdBlockInterceptor: invalid format of subscription: %s", qUtf8Printable(subpath));
- return -1;
- }
-
- int rules = 0;
-
- while(!subscription.atEnd()) {
- QString line = subscription.readLine();
- if(!line.isEmpty() && !line.startsWith('!')) {
- // The line is not a comment
- AdBlockRule *rule = new AdBlockRule(line, this);
- m_urlBlacklist.append(rule);
- rules++;
- }
- }
-
- qDebug("Loaded %i rules from subscription %s", rules, qUtf8Printable(subpath));
-
- return rules;
-}
diff --git a/src/webengine/adblockrule.cpp b/src/webengine/blockerrule.cpp
index fe3e64e..a4a0982 100644
--- a/src/webengine/adblockrule.cpp
+++ b/src/webengine/blockerrule.cpp
@@ -1,12 +1,12 @@
-#include "adblockrule.h"
+#include "blockerrule.h"
-AdBlockRule::AdBlockRule(QString rule, QObject *parent) :
+BlockerRule::BlockerRule(QString rule, QObject *parent) :
QObject(parent)
{
ruleExpression.setPattern(rule);
}
-bool AdBlockRule::match(const QUrl &url)
+bool BlockerRule::match(const QUrl &url)
{
return ruleExpression.match(url.toString()).hasMatch();
}
diff --git a/src/webengine/adblockrule.h b/src/webengine/blockerrule.h
index 8c68de7..9d73004 100644
--- a/src/webengine/adblockrule.h
+++ b/src/webengine/blockerrule.h
@@ -5,11 +5,11 @@
#include <QUrl>
#include <QRegularExpression>
-class AdBlockRule : public QObject
+class BlockerRule : public QObject
{
Q_OBJECT
public:
- explicit AdBlockRule(QString rule, QObject *parent = 0);
+ explicit BlockerRule(QString rule, QObject *parent = 0);
bool match(const QUrl &url);
diff --git a/src/webengine/blockersubscription.cpp b/src/webengine/blockersubscription.cpp
new file mode 100644
index 0000000..5a0664b
--- /dev/null
+++ b/src/webengine/blockersubscription.cpp
@@ -0,0 +1,103 @@
+#include "blockersubscription.h"
+
+#include <QFile>
+#include <QTextStream>
+
+BlockerSubscription::BlockerSubscription(QObject *parent) :
+ QObject(parent)
+{
+}
+
+int BlockerSubscription::loadFromFile(const QString &file)
+{
+ QFile subfile(file);
+ if(!subfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ qDebug("Cannot open subscription: %s", qUtf8Printable(file));
+ return -1;
+ }
+
+ QTextStream subscription(&subfile);
+
+ QString header = subscription.readLine();
+ if(header != "[Adblock Plus 2.0]") {
+ qDebug("Invalid format of subscription: %s", qUtf8Printable(file));
+ return -1;
+ }
+
+ int rules = 0;
+
+ while(!subscription.atEnd()) {
+ QString line = subscription.readLine();
+ if(!line.isEmpty()) {
+ if(line.startsWith('!')) {
+ parseComment(line);
+ } else {
+ // The line is not a comment
+ BlockerRule *rule = new BlockerRule(line, this);
+ m_urlBlacklist.append(rule);
+ rules++;
+ }
+ }
+ }
+
+ qDebug("Loaded %i rules from subscription %s", rules, qUtf8Printable(file));
+ return rules;
+}
+
+const QString BlockerSubscription::title()
+{
+ return m_title;
+}
+
+const QString BlockerSubscription::homepage()
+{
+ return m_homepage;
+}
+
+const QString BlockerSubscription::license()
+{
+ return m_license;
+}
+
+const QString BlockerSubscription::version()
+{
+ return m_version;
+}
+
+const QDateTime BlockerSubscription::lastModified()
+{
+ return m_lastModified;
+}
+
+const QDateTime BlockerSubscription::expires()
+{
+ return m_expires;
+}
+
+void BlockerSubscription::parseComment(const QString &line)
+{
+ if(line.startsWith("! Title: ")) {
+ m_title = line.right(line.length() - 9);
+ return;
+ }
+ if(line.startsWith("! Homepage: ")) {
+ m_homepage = line.right(line.length() - 12);
+ return;
+ }
+ if(line.startsWith("! Licence: ")) {
+ m_license = line.right(line.length() - 11);
+ return;
+ }
+ if(line.startsWith("! Version: ")) {
+ m_version = line.right(line.length() - 11);
+ return;
+ }
+ if(line.startsWith("! Last modified: ")) {
+ m_lastModified = QDateTime::fromString(line.right(line.length() - 17), Qt::RFC2822Date);
+ return;
+ }
+ if(line.startsWith("! Expires: ")) {
+ m_expires = m_lastModified.addDays(line.right(line.length() - 11).left(2).toInt());
+ return;
+ }
+}
diff --git a/src/webengine/blockersubscription.h b/src/webengine/blockersubscription.h
new file mode 100644
index 0000000..1379936
--- /dev/null
+++ b/src/webengine/blockersubscription.h
@@ -0,0 +1,41 @@
+#ifndef URLINTERCEPTORSUBSCRIPTION_H
+#define URLINTERCEPTORSUBSCRIPTION_H
+
+#include <QObject>
+#include <QDateTime>
+#include "blockerrule.h"
+
+class BlockerSubscription : public QObject
+{
+ Q_OBJECT
+public:
+ explicit BlockerSubscription(QObject *parent = 0);
+
+ int loadFromFile(const QString &file);
+
+ const QString title();
+ const QString homepage();
+ const QString license();
+ const QString version();
+ const QDateTime lastModified();
+ const QDateTime expires();
+
+signals:
+
+public slots:
+
+private:
+ void parseComment(const QString &line);
+
+ QString m_title;
+ QString m_homepage;
+ QString m_license;
+
+ QString m_version;
+ QDateTime m_lastModified;
+ QDateTime m_expires;
+
+ QList<BlockerRule*> m_urlBlacklist;
+};
+
+#endif // URLINTERCEPTORSUBSCRIPTION_H
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp
new file mode 100644
index 0000000..87879a5
--- /dev/null
+++ b/src/webengine/urlinterceptor.cpp
@@ -0,0 +1,19 @@
+#include "urlinterceptor.h"
+
+AdBlockInterceptor::AdBlockInterceptor(QObject *parent) :
+ QWebEngineUrlRequestInterceptor(parent)
+{
+}
+
+void AdBlockInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
+{
+// bool blocked = false;
+// for(AdBlockRule *rule : m_urlBlacklist) {
+// if(rule->match(info.requestUrl())) {
+// info.block(true);
+// blocked = true;
+// }
+// }
+
+ qDebug("%i %i %s %s", info.navigationType(), info.resourceType(), qUtf8Printable(info.requestMethod()), qUtf8Printable(info.requestUrl().toString()));
+}
diff --git a/src/webengine/adblockinterceptor.h b/src/webengine/urlinterceptor.h
index 061b626..7834430 100644
--- a/src/webengine/adblockinterceptor.h
+++ b/src/webengine/urlinterceptor.h
@@ -2,7 +2,6 @@
#define ADBLOCKINTERCEPTOR_H
#include <QWebEngineUrlRequestInterceptor>
-#include "adblockrule.h"
class AdBlockInterceptor : public QWebEngineUrlRequestInterceptor
{
@@ -12,14 +11,11 @@ public:
void interceptRequest(QWebEngineUrlRequestInfo &info);
- int loadSubscription(const QString &subpath);
-
signals:
public slots:
private:
- QList<AdBlockRule*> m_urlBlacklist;
};
#endif // ADBLOCKINTERCEPTOR_H