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/forms/blockerdialog.cpp | 31 +++++++ src/forms/blockerdialog.h | 24 +++++ src/forms/blockerdialog.ui | 168 ++++++++++++++++++++++++++++++++++ src/mainwindow.cpp | 2 + src/mainwindow.h | 4 +- src/smolbote.pro | 15 ++- 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 +++++ test/config.ini | 3 + 17 files changed, 461 insertions(+), 123 deletions(-) create mode 100644 src/forms/blockerdialog.cpp create mode 100644 src/forms/blockerdialog.h create mode 100644 src/forms/blockerdialog.ui 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 diff --git a/src/forms/blockerdialog.cpp b/src/forms/blockerdialog.cpp new file mode 100644 index 0000000..f755af6 --- /dev/null +++ b/src/forms/blockerdialog.cpp @@ -0,0 +1,31 @@ +#include "blockerdialog.h" +#include "ui_blockerdialog.h" + +#include "settings.h" +#include + +BlockerDialog::BlockerDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::UrlInterceptorDialog) +{ + Settings settings; + ui->setupUi(this); + + subscription = new BlockerSubscription(this); + QString sublocation = settings.value("blocker/subscription").toString(); + if(!sublocation.isEmpty()) { + subscription->loadFromFile(sublocation); + } + + ui->title->setText(subscription->title()); + ui->homepage->setText(subscription->homepage()); + ui->license->setText(subscription->license()); + ui->version->setText(subscription->version()); + ui->lastModified->setText(subscription->lastModified().toString()); + ui->expires->setText(subscription->expires().toString()); +} + +BlockerDialog::~BlockerDialog() +{ + delete ui; +} diff --git a/src/forms/blockerdialog.h b/src/forms/blockerdialog.h new file mode 100644 index 0000000..0c8e8ba --- /dev/null +++ b/src/forms/blockerdialog.h @@ -0,0 +1,24 @@ +#ifndef URLINTERCEPTORDIALOG_H +#define URLINTERCEPTORDIALOG_H + +#include +#include "webengine/blockersubscription.h" + +namespace Ui { +class UrlInterceptorDialog; +} + +class BlockerDialog : public QDialog +{ + Q_OBJECT + +public: + explicit BlockerDialog(QWidget *parent = 0); + ~BlockerDialog(); + +private: + Ui::UrlInterceptorDialog *ui; + BlockerSubscription *subscription; +}; + +#endif // URLINTERCEPTORDIALOG_H diff --git a/src/forms/blockerdialog.ui b/src/forms/blockerdialog.ui new file mode 100644 index 0000000..dc0a1f4 --- /dev/null +++ b/src/forms/blockerdialog.ui @@ -0,0 +1,168 @@ + + + UrlInterceptorDialog + + + + 0 + 0 + 640 + 480 + + + + Dialog + + + + + + GroupBox + + + + + + + + Title + + + + + + + TextLabel + + + + + + + Homepage + + + + + + + TextLabel + + + + + + + License + + + + + + + TextLabel + + + + + + + + + + + Version + + + + + + + TextLabel + + + + + + + Last Modified + + + + + + + TextLabel + + + + + + + Expires + + + + + + + TextLabel + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + UrlInterceptorDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + UrlInterceptorDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2b7a2b9..1062823 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -34,6 +34,7 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : QMainWindow(parent), downloadManager(new DownloadDialog(this)), + blocklistManager(new BlockerDialog(this)), ui(new Ui::MainWindow), navigationToolBar(new QToolBar(this)), tabToolBar(new QToolBar(this)), @@ -63,6 +64,7 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : QMenu *toolsMenu = new QMenu(tr("Tools"), ui->menuBar); ui->menuBar->addMenu(toolsMenu); toolsMenu->addAction(tr("Downloads"), downloadManager, SLOT(show())); + toolsMenu->addAction(tr("Blocker"), blocklistManager, SLOT(show())); // Profile menu QMenu *profileMenu = new QMenu(tr("Profile"), ui->menuBar); diff --git a/src/mainwindow.h b/src/mainwindow.h index 351a794..e4c1a1f 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -29,7 +29,8 @@ #include #include "widgets/webviewtabbar.h" #include "forms/downloaddialog.h" -#include "webengine/adblockinterceptor.h" +#include "webengine/urlinterceptor.h" +#include "forms/blockerdialog.h" namespace Ui { class MainWindow; @@ -66,6 +67,7 @@ private slots: private: Browser *browserInstance; DownloadDialog *downloadManager; + BlockerDialog *blocklistManager; QString profileName; WebEngineProfile *profile = nullptr; diff --git a/src/smolbote.pro b/src/smolbote.pro index 857a60a..c8be516 100644 --- a/src/smolbote.pro +++ b/src/smolbote.pro @@ -20,8 +20,10 @@ SOURCES += main.cpp \ webengine/webengineprofile.cpp \ forms/downloaddialog.cpp \ webengine/downloaditemform.cpp \ - webengine/adblockinterceptor.cpp \ - webengine/adblockrule.cpp + webengine/urlinterceptor.cpp \ + forms/blockerdialog.cpp \ + webengine/blockersubscription.cpp \ + webengine/blockerrule.cpp HEADERS += mainwindow.h \ browser.h \ @@ -31,13 +33,16 @@ HEADERS += mainwindow.h \ webengine/webengineprofile.h \ forms/downloaddialog.h \ webengine/downloaditemform.h \ - webengine/adblockinterceptor.h \ - webengine/adblockrule.h + webengine/urlinterceptor.h \ + forms/blockerdialog.h \ + webengine/blockersubscription.h \ + webengine/blockerrule.h FORMS += mainwindow.ui \ forms/profiledialog.ui \ forms/downloaddialog.ui \ - webengine/downloaditemform.ui + webengine/downloaditemform.ui \ + forms/blockerdialog.ui RESOURCES += \ data/resources.qrc 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 diff --git a/test/config.ini b/test/config.ini index 2c4b114..03447e4 100644 --- a/test/config.ini +++ b/test/config.ini @@ -5,3 +5,6 @@ profile=Default [window] width=1280 height=720 + +[blocker] +subscription=blocklist.txt -- cgit v1.2.1