diff options
Diffstat (limited to 'src/filter')
-rw-r--r-- | src/filter/blockermanager.cpp | 41 | ||||
-rw-r--r-- | src/filter/blockermanager.h | 34 | ||||
-rw-r--r-- | src/filter/filter.cpp | 34 | ||||
-rw-r--r-- | src/filter/filter.h | 42 | ||||
-rw-r--r-- | src/filter/filtercollection.cpp | 157 | ||||
-rw-r--r-- | src/filter/filtercollection.h | 53 | ||||
-rw-r--r-- | src/filter/filtertree.cpp | 151 | ||||
-rw-r--r-- | src/filter/filtertree.h | 39 | ||||
-rw-r--r-- | src/filter/regexp.cpp | 60 | ||||
-rw-r--r-- | src/filter/regexp.h | 26 | ||||
-rw-r--r-- | src/filter/subscriptiondialog.ui | 67 | ||||
-rw-r--r-- | src/filter/subscriptionform.ui | 125 |
12 files changed, 0 insertions, 829 deletions
diff --git a/src/filter/blockermanager.cpp b/src/filter/blockermanager.cpp deleted file mode 100644 index 718f8a1..0000000 --- a/src/filter/blockermanager.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "blockermanager.h" -#include "ui_subscriptiondialog.h" - -#include "browser.h" -#include <QLabel> -#include <QListWidget> - -#include "filtercollection.h" - -BlockerManager::BlockerManager(QWidget *parent) : - QDialog(parent), - ui(new Ui::UrlInterceptorDialog) -{ - ui->setupUi(this); - - const QStringList subscriptions = browser->settings()->value("blocker.subscriptions").toStringList(); - QStringList::const_iterator i; - for(i = subscriptions.constBegin(); i != subscriptions.constEnd(); ++i) { - FilterCollection *sub = new FilterCollection(QString(*i), this); - m_subscriptions.append(sub); - ui->tabWidget->addTab(sub, sub->name()); - } -} - -BlockerManager::~BlockerManager() -{ - delete ui; -} - -QVector<FilterCollection *> BlockerManager::subscriptions() const -{ - return m_subscriptions; -} diff --git a/src/filter/blockermanager.h b/src/filter/blockermanager.h deleted file mode 100644 index df426b3..0000000 --- a/src/filter/blockermanager.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef URLINTERCEPTORDIALOG_H -#define URLINTERCEPTORDIALOG_H - -#include <QDialog> - -namespace Ui { -class UrlInterceptorDialog; -} - -class FilterCollection; -class BlockerManager : public QDialog -{ - Q_OBJECT - -public: - explicit BlockerManager(QWidget *parent = 0); - ~BlockerManager(); - - QVector<FilterCollection* > subscriptions() const; - -private: - Ui::UrlInterceptorDialog *ui; - QVector<FilterCollection *> m_subscriptions; -}; - -#endif // URLINTERCEPTORDIALOG_H diff --git a/src/filter/filter.cpp b/src/filter/filter.cpp deleted file mode 100644 index 35eb9e7..0000000 --- a/src/filter/filter.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "filter.h" -#include <QUrl> - -FilterRule::FilterRule(const QString &line) -{ - parse(line); -} - -FilterRule::~FilterRule() -{ -} - -bool FilterRule::shouldBlock(const QUrl &requestUrl) const -{ - if(matcher.indexIn(requestUrl.toString()) == -1) { - // pattern not found in url - return false; - } - - return true; -} - -void FilterRule::parse(const QString &line) -{ - matcher.setPattern(line); -} diff --git a/src/filter/filter.h b/src/filter/filter.h deleted file mode 100644 index f152e7a..0000000 --- a/src/filter/filter.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef FILTERRULE_H -#define FILTERRULE_H - -#include <QStringMatcher> - -class QUrl; -class FilterRule -{ -public: - - FilterRule(const QString &line); - - // delete the copy constructor and assignment operator - FilterRule(const FilterRule&) = delete; - FilterRule& operator=(const FilterRule&) = delete; - - // move constructor - FilterRule(FilterRule&& other) { - matcher = other.matcher; - } - - ~FilterRule(); - - bool shouldBlock(const QUrl &requestUrl) const; - -private: - void parse(const QString &line); - - // QStringMatcher: holds a pattern you want to repeatedly match against some strings - QStringMatcher matcher; - -}; - -#endif // FILTERRULE_H diff --git a/src/filter/filtercollection.cpp b/src/filter/filtercollection.cpp deleted file mode 100644 index e151a10..0000000 --- a/src/filter/filtercollection.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "filtercollection.h" -#include "ui_subscriptionform.h" - -#include "browser.h" -#include <QNetworkRequest> -#include <QNetworkReply> - -#include <QJsonDocument> -#include <QJsonObject> -#include <QJsonArray> - -#include <QListWidget> -#include <QLabel> - -#include <QTreeView> -#include "filtertree.h" -#include "filter.h" - -FilterCollection::FilterCollection(const QString path, QWidget *parent) : - QWidget(parent), - ui(new Ui::SubscriptionForm) -{ - ui->setupUi(this); - - ui->homepage->setText(path); - - qDebug("Adding subscription [%s]", qUtf8Printable(path)); - - m_filters = new FilterTree(this); - ui->treeView->setModel(m_filters); - - QFile filterFile(path); - if(!filterFile.open(QIODevice::ReadOnly)) { - qWarning("Could not open filter!"); - return; - } - - QJsonDocument filters(QJsonDocument::fromJson(filterFile.readAll())); - qDebug("Added %i rules", load(filters.object())); -} - -FilterCollection::~FilterCollection() -{ - delete ui; -} - -QString FilterCollection::name() const -{ - return ui->title->text(); -} - -/** - * Check if a URL request should be blocked or not - * @param info - * @return true if it should be blocked; false otherwise - */ -bool FilterCollection::match(QWebEngineUrlRequestInfo &info) -{ - for(QString domain : m_filterlist.keys()) { - if(domain.isEmpty() || info.firstPartyUrl().toString().contains(domain)) { - // domain matched - - for(QString rule : m_filterlist.value(domain).keys()) { - if(rule.isEmpty() || info.requestUrl().toString().contains(rule)) { - return m_filterlist.value(domain).value(rule); - } - } - } - } - - return false; -} - -/** - * Load rules from JSON object - * @param json - */ -int FilterCollection::load(const QJsonObject &json) -{ - int number = 0; - - ui->title->setText(json["name"].toString()); - ui->homepage->setText(json["url"].toString()); - - QJsonObject rules = json["rules"].toObject(); - for(QString key : rules.keys()) { - ++number; - qDebug("+ '%s'", qUtf8Printable(key)); - - QJsonObject domain = rules.value(key).toObject(); - QHash<QString, bool> d; - - for(QString r : domain.keys()) { - d.insert(r, domain.value(r).toBool()); - qDebug("|-'%s': %i", qUtf8Printable(r), domain.value(r).toBool()); - } - - m_filterlist.insert(key, d); - - } - return number; -} - -Filter::Resources FilterCollection::parseJsonRules(const QJsonValue &obj) -{ - Filter::Resources res; - for(QJsonValue v : obj.toArray()) { - QString t = v.toString(); - if(t == "MainFrame") { - res.setFlag(Filter::ResourceType::MainFrame); - } else if(t == "SubFrame") { - res.setFlag(Filter::ResourceType::SubFrame); - } else if(t == "Stylesheet") { - res.setFlag(Filter::ResourceType::Stylesheet); - } else if(t == "Script") { - res.setFlag(Filter::ResourceType::Script); - } else if(t == "Image") { - res.setFlag(Filter::ResourceType::Image); - } else if(t == "FontResource") { - res.setFlag(Filter::ResourceType::Font); - } else if(t == "SubResource") { - res.setFlag(Filter::ResourceType::SubResource); - } else if(t == "Object") { - res.setFlag(Filter::ResourceType::Object); - } else if(t == "Media") { - res.setFlag(Filter::ResourceType::Media); - } else if(t == "Worker") { - res.setFlag(Filter::ResourceType::Worker); - } else if(t == "SharedWorker") { - res.setFlag(Filter::ResourceType::SharedWorker); - } else if(t == "Prefetch") { - res.setFlag(Filter::ResourceType::Prefetch); - } else if(t == "Favicon") { - res.setFlag(Filter::ResourceType::Favicon); - } else if(t == "Xhr") { - res.setFlag(Filter::ResourceType::Xhr); - } else if(t == "Ping") { - res.setFlag(Filter::ResourceType::Ping); - } else if(t == "ServiceWorker") { - res.setFlag(Filter::ResourceType::ServiceWorker); - } else if(t == "CspWorker") { - res.setFlag(Filter::ResourceType::CspReport); - } else if(t == "PluginResource") { - res.setFlag(Filter::ResourceType::PluginResource); - } - } - - return res; -} diff --git a/src/filter/filtercollection.h b/src/filter/filtercollection.h deleted file mode 100644 index 4249eea..0000000 --- a/src/filter/filtercollection.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SUBSCRIPTIONFORM_H -#define SUBSCRIPTIONFORM_H - -#include <QWidget> -#include <QFile> -#include "filtertree.h" - -#include <QHash> - -namespace Ui { -class SubscriptionForm; -} - -class FilterCollection : public QWidget -{ - Q_OBJECT - -public: - struct MatchResult { - bool match; - bool block; - QString pattern; - }; - - explicit FilterCollection(const QString path, QWidget *parent = 0); - ~FilterCollection(); - - QString name() const; - bool match(QWebEngineUrlRequestInfo &info); - -private slots: - int load(const QJsonObject &json); - -private: - Filter::Resources parseJsonRules(const QJsonValue &obj); - - Ui::SubscriptionForm *ui; - - FilterTree *m_filters; - - QHash<QString, QHash<QString, bool>> m_filterlist; - -}; - -#endif // SUBSCRIPTIONFORM_H diff --git a/src/filter/filtertree.cpp b/src/filter/filtertree.cpp deleted file mode 100644 index 73cc261..0000000 --- a/src/filter/filtertree.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "filtertree.h" - -FilterTree::FilterTree(QObject *parent) : - QAbstractItemModel(parent) -{ - rootItem = new Filter("", "", Filter::ResourceRules(), false); -} - -FilterTree::~FilterTree() -{ - delete rootItem; -} - -QModelIndex FilterTree::index(int row, int column, const QModelIndex &parent) const -{ - if(!hasIndex(row, column, parent)) { - return QModelIndex(); - } - - Filter *parentItem; - if(!parent.isValid()) { - parentItem = rootItem; - } else { - parentItem = static_cast<Filter*>(parent.internalPointer()); - } - - Filter *childItem = parentItem->child(row); - if(childItem) { - return createIndex(row, column, childItem); - } else { - return QModelIndex(); - } -} - -QModelIndex FilterTree::parent(const QModelIndex &index) const -{ - if(!index.isValid()) { - return QModelIndex(); - } - - Filter *childItem = static_cast<Filter*>(index.internalPointer()); - Filter *parentItem = childItem->parentItem(); - - if(parentItem == rootItem) { - return QModelIndex(); - } - - return createIndex(parentItem->row(), 0, parentItem); -} - -int FilterTree::rowCount(const QModelIndex &parent) const -{ - Filter *parentItem; - if(parent.column() > 0) { - return 0; - } - - if(!parent.isValid()) { - parentItem = rootItem; - } else { - parentItem = static_cast<Filter*>(parent.internalPointer()); - } - - return parentItem->childCount(); -} - -int FilterTree::columnCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent) - return 5; -} - -QVariant FilterTree::data(const QModelIndex &index, int role) const -{ - if(!index.isValid()) { - return QVariant(); - } - - if(role != Qt::DisplayRole) { - return QVariant(); - } - - Filter *n = static_cast<Filter*>(index.internalPointer()); - switch (index.column()) { - case 0: // domain - return QVariant(n->domain()); - case 1: // request - return QVariant(n->request()); - case 2: // should block - return QVariant(n->isBlocking() ? "true" : "false"); - case 3: // allowed types - return QVariant(n->allowedTypes()); - case 4: // blocked types - return QVariant(n->blockedTypes()); - default: - return QVariant(); - } -} - -QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const -{ - if(orientation != Qt::Horizontal) { - return QVariant(); - } - - if(role != Qt::DisplayRole) { - return QVariant(); - } - - switch (section) { - case 0: - return QVariant(tr("Domain")); - case 1: - return QVariant(tr("Request")); - case 2: - return QVariant(tr("Should Block")); - case 3: - return QVariant(tr("Allowed Types")); - case 4: - return QVariant(tr("Blocked Types")); - default: - return QVariant(); - } -} - -Filter *FilterTree::addFilter(const QString &domain, const QString &request, Filter::ResourceRules rules, bool shouldBlock) -{ - Filter *node = new Filter(domain, request, rules, shouldBlock, rootItem); - node->enable(); - rootItem->appendChild(node); - return node; -} - -QVector<Filter*> FilterTree::filters(const QString &domain) -{ - QVector<Filter*> nodes; - for(int i = 0; i < rootItem->childCount(); ++i) { - if(rootItem->child(i)->hasDomainMatch(domain)) { - nodes.append(rootItem->child(i)); - } - } - return nodes; -} diff --git a/src/filter/filtertree.h b/src/filter/filtertree.h deleted file mode 100644 index cbf339f..0000000 --- a/src/filter/filtertree.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef FILTERTREE_H -#define FILTERTREE_H - -#include <QAbstractItemModel> -#include <QModelIndex> -#include <QVariant> -#include "filter.h" - -class FilterTree : public QAbstractItemModel -{ - Q_OBJECT - -public: - explicit FilterTree(QObject *parent = 0); - ~FilterTree(); - - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; - QModelIndex parent(const QModelIndex &index) const override; - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role) const override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - - Filter *addFilter(const QString &domain, const QString &request, Filter::ResourceRules rules, bool shouldBlock); - QVector<Filter*> filters(const QString &domain = ""); - -private: - Filter *rootItem; -}; - -#endif // FILTERTREE_H diff --git a/src/filter/regexp.cpp b/src/filter/regexp.cpp deleted file mode 100644 index e46bc75..0000000 --- a/src/filter/regexp.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "regexp.h" - -RegExp::RegExp(const QString &pattern, PatternOptions options) : - QRegularExpression() -{ - setPattern(pattern); - setPatternOptions(options); -} - -bool RegExp::hasMatch(const QString &subject, int offset, MatchType matchType, MatchOptions matchOptions) const -{ - // Empty matches all - if(pattern().isEmpty()) { - return true; - } - - return QRegularExpression::match(subject, offset, matchType, matchOptions).hasMatch(); -} - -void RegExp::setWildcardPattern(const QString &pattern) -{ - QString parsed; - - for(int i=0; i<pattern.length(); i++) { - const QChar c = pattern.at(i); - switch (c.toLatin1()) { - case '*': - // remove * at the start and end - if(i != 0 && i != pattern.length()-1) { - 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; - } - } - - setPattern(parsed); -} diff --git a/src/filter/regexp.h b/src/filter/regexp.h deleted file mode 100644 index 32ba5c3..0000000 --- a/src/filter/regexp.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef REGEXP_H -#define REGEXP_H - -#include <QRegularExpression> - -/*! - * Regular Expression class for AdBlockPlus filters - */ -class RegExp : public QRegularExpression -{ -public: - explicit RegExp(const QString &pattern = "", PatternOptions options = NoPatternOption); - - bool hasMatch(const QString &subject, int offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const; - void setWildcardPattern(const QString &pattern); -}; - -#endif // REGEXP_H diff --git a/src/filter/subscriptiondialog.ui b/src/filter/subscriptiondialog.ui deleted file mode 100644 index 7a63cc9..0000000 --- a/src/filter/subscriptiondialog.ui +++ /dev/null @@ -1,67 +0,0 @@ -<?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>Blocker</string> - </property> - <layout class="QVBoxLayout" name="verticalLayout"> - <item> - <widget class="QTabWidget" name="tabWidget"/> - </item> - <item> - <widget class="QDialogButtonBox" name="buttonBox"> - <property name="orientation"> - <enum>Qt::Horizontal</enum> - </property> - <property name="standardButtons"> - <set>QDialogButtonBox::Close</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/filter/subscriptionform.ui b/src/filter/subscriptionform.ui deleted file mode 100644 index 0a20582..0000000 --- a/src/filter/subscriptionform.ui +++ /dev/null @@ -1,125 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<ui version="4.0"> - <class>SubscriptionForm</class> - <widget class="QWidget" name="SubscriptionForm"> - <property name="geometry"> - <rect> - <x>0</x> - <y>0</y> - <width>640</width> - <height>480</height> - </rect> - </property> - <property name="windowTitle"> - <string>Form</string> - </property> - <layout class="QVBoxLayout" name="verticalLayout"> - <item> - <widget class="QGroupBox" name="groupBox"> - <property name="title"> - <string>Subscription</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="QTreeView" name="treeView"/> - </item> - </layout> - </widget> - <resources/> - <connections/> -</ui> |