From 0250559bcf5764fb8cf3a8ccc4e330b8ed855f96 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 24 Jan 2017 16:09:07 +0100 Subject: Blocker UI --- src/webengine/adblockinterceptor.cpp | 56 ------------------ src/webengine/adblockinterceptor.h | 25 --------- src/webengine/adblockrule.cpp | 12 ---- src/webengine/adblockrule.h | 24 -------- src/webengine/blockerrule.cpp | 12 ++++ src/webengine/blockerrule.h | 24 ++++++++ src/webengine/blockersubscription.cpp | 103 ++++++++++++++++++++++++++++++++++ src/webengine/blockersubscription.h | 41 ++++++++++++++ src/webengine/urlinterceptor.cpp | 19 +++++++ src/webengine/urlinterceptor.h | 21 +++++++ 10 files changed, 220 insertions(+), 117 deletions(-) delete mode 100644 src/webengine/adblockinterceptor.cpp delete mode 100644 src/webengine/adblockinterceptor.h delete mode 100644 src/webengine/adblockrule.cpp delete mode 100644 src/webengine/adblockrule.h create mode 100644 src/webengine/blockerrule.cpp create mode 100644 src/webengine/blockerrule.h create mode 100644 src/webengine/blockersubscription.cpp create mode 100644 src/webengine/blockersubscription.h create mode 100644 src/webengine/urlinterceptor.cpp create mode 100644 src/webengine/urlinterceptor.h (limited to 'src/webengine') 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 -#include - -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/adblockinterceptor.h b/src/webengine/adblockinterceptor.h deleted file mode 100644 index 061b626..0000000 --- a/src/webengine/adblockinterceptor.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef ADBLOCKINTERCEPTOR_H -#define ADBLOCKINTERCEPTOR_H - -#include -#include "adblockrule.h" - -class AdBlockInterceptor : public QWebEngineUrlRequestInterceptor -{ - Q_OBJECT -public: - explicit AdBlockInterceptor(QObject *parent = 0); - - void interceptRequest(QWebEngineUrlRequestInfo &info); - - int loadSubscription(const QString &subpath); - -signals: - -public slots: - -private: - QList m_urlBlacklist; -}; - -#endif // ADBLOCKINTERCEPTOR_H diff --git a/src/webengine/adblockrule.cpp b/src/webengine/adblockrule.cpp deleted file mode 100644 index fe3e64e..0000000 --- a/src/webengine/adblockrule.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "adblockrule.h" - -AdBlockRule::AdBlockRule(QString rule, QObject *parent) : - QObject(parent) -{ - ruleExpression.setPattern(rule); -} - -bool AdBlockRule::match(const QUrl &url) -{ - return ruleExpression.match(url.toString()).hasMatch(); -} diff --git a/src/webengine/adblockrule.h b/src/webengine/adblockrule.h deleted file mode 100644 index 8c68de7..0000000 --- a/src/webengine/adblockrule.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef ADBLOCKRULE_H -#define ADBLOCKRULE_H - -#include -#include -#include - -class AdBlockRule : public QObject -{ - Q_OBJECT -public: - explicit AdBlockRule(QString rule, QObject *parent = 0); - - bool match(const QUrl &url); - -signals: - -public slots: - -private: - QRegularExpression ruleExpression; -}; - -#endif // ADBLOCKRULE_H diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp new file mode 100644 index 0000000..a4a0982 --- /dev/null +++ b/src/webengine/blockerrule.cpp @@ -0,0 +1,12 @@ +#include "blockerrule.h" + +BlockerRule::BlockerRule(QString rule, QObject *parent) : + QObject(parent) +{ + ruleExpression.setPattern(rule); +} + +bool BlockerRule::match(const QUrl &url) +{ + return ruleExpression.match(url.toString()).hasMatch(); +} diff --git a/src/webengine/blockerrule.h b/src/webengine/blockerrule.h new file mode 100644 index 0000000..9d73004 --- /dev/null +++ b/src/webengine/blockerrule.h @@ -0,0 +1,24 @@ +#ifndef ADBLOCKRULE_H +#define ADBLOCKRULE_H + +#include +#include +#include + +class BlockerRule : public QObject +{ + Q_OBJECT +public: + explicit BlockerRule(QString rule, QObject *parent = 0); + + bool match(const QUrl &url); + +signals: + +public slots: + +private: + QRegularExpression ruleExpression; +}; + +#endif // ADBLOCKRULE_H 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 +#include + +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 +#include +#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 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/urlinterceptor.h b/src/webengine/urlinterceptor.h new file mode 100644 index 0000000..7834430 --- /dev/null +++ b/src/webengine/urlinterceptor.h @@ -0,0 +1,21 @@ +#ifndef ADBLOCKINTERCEPTOR_H +#define ADBLOCKINTERCEPTOR_H + +#include + +class AdBlockInterceptor : public QWebEngineUrlRequestInterceptor +{ + Q_OBJECT +public: + explicit AdBlockInterceptor(QObject *parent = 0); + + void interceptRequest(QWebEngineUrlRequestInfo &info); + +signals: + +public slots: + +private: +}; + +#endif // ADBLOCKINTERCEPTOR_H -- cgit v1.2.1