diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-01-24 16:09:07 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-01-24 16:09:07 +0100 |
commit | 0250559bcf5764fb8cf3a8ccc4e330b8ed855f96 (patch) | |
tree | 2283350e44f78e83a8c0257b66c184939cd95a98 /src | |
parent | Made Profile menu a regular menu (diff) | |
download | smolbote-0250559bcf5764fb8cf3a8ccc4e330b8ed855f96.tar.xz |
Blocker UI
Diffstat (limited to 'src')
-rw-r--r-- | src/forms/blockerdialog.cpp | 31 | ||||
-rw-r--r-- | src/forms/blockerdialog.h | 24 | ||||
-rw-r--r-- | src/forms/blockerdialog.ui | 168 | ||||
-rw-r--r-- | src/mainwindow.cpp | 2 | ||||
-rw-r--r-- | src/mainwindow.h | 4 | ||||
-rw-r--r-- | src/smolbote.pro | 15 | ||||
-rw-r--r-- | src/webengine/adblockinterceptor.cpp | 56 | ||||
-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.cpp | 103 | ||||
-rw-r--r-- | src/webengine/blockersubscription.h | 41 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 19 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.h (renamed from src/webengine/adblockinterceptor.h) | 4 |
13 files changed, 406 insertions, 71 deletions
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 <QLabel> + +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 <QDialog> +#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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>UrlInterceptorDialog</class> + <widget class="QDialog" name="UrlInterceptorDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>640</width> + <height>480</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QGroupBox" name="groupBox"> + <property name="title"> + <string>GroupBox</string> + </property> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <layout class="QFormLayout" name="leftFormLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="title_label"> + <property name="text"> + <string>Title</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLabel" name="title"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="homepage_label"> + <property name="text"> + <string>Homepage</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="homepage"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="license_label"> + <property name="text"> + <string>License</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLabel" name="license"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QFormLayout" name="rightFormLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="version_label"> + <property name="text"> + <string>Version</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLabel" name="version"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="lastModified_label"> + <property name="text"> + <string>Last Modified</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="lastModified"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="expires_label"> + <property name="text"> + <string>Expires</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLabel" name="expires"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QListWidget" name="listWidget"/> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>UrlInterceptorDialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>248</x> + <y>254</y> + </hint> + <hint type="destinationlabel"> + <x>157</x> + <y>274</y> + </hint> + </hints> + </connection> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>UrlInterceptorDialog</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>316</x> + <y>260</y> + </hint> + <hint type="destinationlabel"> + <x>286</x> + <y>274</y> + </hint> + </hints> + </connection> + </connections> +</ui> 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 <QUrl> #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 <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 |