diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-05-25 20:59:43 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-05-25 20:59:43 +0200 |
commit | 1143429887f61d8b3c74a4c3a1fafca632eb4b87 (patch) | |
tree | 87e03b38241afb33c1e7dc70c299bfb824533429 /src/filter | |
parent | Merged FilterRule into FilterNode (diff) | |
download | smolbote-1143429887f61d8b3c74a4c3a1fafca632eb4b87.tar.xz |
Filter code refactoring
Diffstat (limited to 'src/filter')
-rw-r--r-- | src/filter/blockermanager.cpp | 53 | ||||
-rw-r--r-- | src/filter/blockermanager.h | 46 | ||||
-rw-r--r-- | src/filter/filter.cpp | 237 | ||||
-rw-r--r-- | src/filter/filter.h | 106 | ||||
-rw-r--r-- | src/filter/filtercollection.cpp | 171 | ||||
-rw-r--r-- | src/filter/filtercollection.h | 61 | ||||
-rw-r--r-- | src/filter/filtertree.cpp | 161 | ||||
-rw-r--r-- | src/filter/filtertree.h | 51 | ||||
-rw-r--r-- | src/filter/regexp.cpp | 72 | ||||
-rw-r--r-- | src/filter/regexp.h | 38 | ||||
-rw-r--r-- | src/filter/subscriptiondialog.ui | 67 | ||||
-rw-r--r-- | src/filter/subscriptionform.ui | 125 |
12 files changed, 1188 insertions, 0 deletions
diff --git a/src/filter/blockermanager.cpp b/src/filter/blockermanager.cpp new file mode 100644 index 0000000..90d99a9 --- /dev/null +++ b/src/filter/blockermanager.cpp @@ -0,0 +1,53 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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 = sSettings->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 new file mode 100644 index 0000000..cfa2110 --- /dev/null +++ b/src/filter/blockermanager.h @@ -0,0 +1,46 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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 new file mode 100644 index 0000000..e0d01a9 --- /dev/null +++ b/src/filter/filter.cpp @@ -0,0 +1,237 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#include "filter.h" + +Filter::Filter(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, Filter *parentItem) +{ + m_parentItem = parentItem; + + m_domainUrl.setPattern(domain); + m_requestUrl.setPattern(request); + m_rules = rules; + m_shouldBlock = shouldBlock; + + m_valid = false; +} + +Filter::~Filter() +{ + qDeleteAll(m_children); +} + +void Filter::enable() +{ + m_valid = true; +} + +Filter *Filter::parentItem() +{ + return m_parentItem; +} + +void Filter::appendChild(Filter *child) +{ + m_children.append(child); +} + +Filter *Filter::child(int row) +{ + return m_children.value(row); +} + +int Filter::childCount() const +{ + return m_children.count(); +} + +QString Filter::domain() const +{ + return m_domainUrl.pattern(); +} + +QString Filter::request() const +{ + return m_requestUrl.pattern(); +} + +bool Filter::isBlocking() +{ + return m_shouldBlock; +} + +QString Filter::allowedTypes() const +{ + return flags(m_rules.allowed); +} + +QString Filter::blockedTypes() const +{ + return flags(m_rules.blocked); +} + +int Filter::row() const +{ + if(m_parentItem) { + return m_parentItem->m_children.indexOf(const_cast<Filter*>(this)); + } + + return 0; +} + +bool Filter::hasMatch(const QWebEngineUrlRequestInfo &info) +{ + if(m_domainUrl.hasMatch(info.firstPartyUrl().toString()) && m_requestUrl.hasMatch(info.requestUrl().toString())) { + return true; + } else { + return false; + } +} + +bool Filter::shouldBlock(const QWebEngineUrlRequestInfo &info) +{ + if(!m_valid) { + return false; + } + + // Check options + switch (info.resourceType()) { + case QWebEngineUrlRequestInfo::ResourceTypeMainFrame: + return testFlag(MainFrame); + case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: + return testFlag(SubFrame); + case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: + return testFlag(Stylesheet); + case QWebEngineUrlRequestInfo::ResourceTypeScript: + return testFlag(Script); + case QWebEngineUrlRequestInfo::ResourceTypeImage: + return testFlag(Image); + case QWebEngineUrlRequestInfo::ResourceTypeFontResource: + return testFlag(Font); + case QWebEngineUrlRequestInfo::ResourceTypeSubResource: + return testFlag(SubResource); + case QWebEngineUrlRequestInfo::ResourceTypeObject: + return testFlag(Object); + case QWebEngineUrlRequestInfo::ResourceTypeMedia: + return testFlag(Media); + case QWebEngineUrlRequestInfo::ResourceTypeWorker: + return testFlag(Worker); + case QWebEngineUrlRequestInfo::ResourceTypeSharedWorker: + return testFlag(SharedWorker); + case QWebEngineUrlRequestInfo::ResourceTypePrefetch: + return testFlag(Prefetch); + case QWebEngineUrlRequestInfo::ResourceTypeFavicon: + return testFlag(Favicon); + case QWebEngineUrlRequestInfo::ResourceTypeXhr: + return testFlag(Xhr); + case QWebEngineUrlRequestInfo::ResourceTypePing: + return testFlag(Ping); + case QWebEngineUrlRequestInfo::ResourceTypeServiceWorker: + return testFlag(ServiceWorker); + case QWebEngineUrlRequestInfo::ResourceTypeCspReport: + return testFlag(CspReport); + case QWebEngineUrlRequestInfo::ResourceTypePluginResource: + return testFlag(PluginResource); + case QWebEngineUrlRequestInfo::ResourceTypeUnknown: + break; + } + + return m_shouldBlock; + +} + +bool Filter::isValid() +{ + return m_valid; +} + +bool Filter::testFlag(ResourceType flag) +{ + if(m_rules.allowed.testFlag(flag)) { + return false; + } else if(m_rules.blocked.testFlag(flag)) { + return true; + } else { + return m_shouldBlock; + } +} + +QString Filter::flags(Resources f) const +{ + QStringList r; + if(f.testFlag(MainFrame)) { + r << QObject::tr("Main Frame"); + } + if(f.testFlag(SubFrame)) { + r << QObject::tr("Sub Frame"); + } + if(f.testFlag(Stylesheet)) { + r << QObject::tr("Stylesheet"); + } + if(f.testFlag(Script)) { + r << QObject::tr("Script"); + } + if(f.testFlag(Image)) { + r << QObject::tr("Image"); + } + if(f.testFlag(Font)) { + r << QObject::tr("Font"); + } + if(f.testFlag(SubResource)) { + r << QObject::tr("Sub Resource"); + } + if(f.testFlag(Object)) { + r << QObject::tr("Object"); + } + if(f.testFlag(Media)) { + r << QObject::tr("Media"); + } + if(f.testFlag(Worker)) { + r << QObject::tr("Worker"); + } + if(f.testFlag(SharedWorker)) { + r << QObject::tr("Shared Worker"); + } + if(f.testFlag(Prefetch)) { + r << QObject::tr("Prefetch"); + } + if(f.testFlag(Favicon)) { + r << QObject::tr("Favicon"); + } + if(f.testFlag(Xhr)) { + r << QObject::tr("Xhr"); + } + if(f.testFlag(Ping)) { + r << QObject::tr("Ping"); + } + if(f.testFlag(ServiceWorker)) { + r << QObject::tr("Service Worker"); + } + if(f.testFlag(CspReport)) { + r << QObject::tr("Csp Report"); + } + if(f.testFlag(PluginResource)) { + r << QObject::tr("Plugin Resource"); + } + if(f.testFlag(Unknown)) { + r << QObject::tr("Unknown"); + } + return r.join(','); +} diff --git a/src/filter/filter.h b/src/filter/filter.h new file mode 100644 index 0000000..37604fe --- /dev/null +++ b/src/filter/filter.h @@ -0,0 +1,106 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#ifndef FILTERNODE_H +#define FILTERNODE_H + +#include <QList> +#include <QVariant> +#include <QUrl> +#include "regexp.h" +#include <QWebEngineUrlRequestInfo> + +class Filter +{ +public: + + enum ResourceType { + NoType = 0, + MainFrame = 1, + SubFrame = 2, + Stylesheet = 4, + Script = 8, + Image = 16, + Font = 32, + SubResource = 64, + Object = 128, + Media = 256, + Worker = 512, + SharedWorker = 1024, + Prefetch = 2048, + Favicon = 4096, + Xhr = 8192, + Ping = 16384, + ServiceWorker = 32768, + CspReport = 65536, + PluginResource = 131072, + Unknown = 262144 + }; + Q_DECLARE_FLAGS(Resources, ResourceType) + + struct ResourceRules { + Resources allowed; + Resources blocked; + }; + + explicit Filter(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, Filter *parentItem = 0); + ~Filter(); + + void enable(); + + Filter *parentItem(); + + // children + void appendChild(Filter *child); + Filter *child(int row); + int childCount() const; + + // data + QString domain() const; + QString request() const; + bool isBlocking(); + QString allowedTypes() const; + QString blockedTypes() const; + + int row() const; + + // filtering + bool hasMatch(const QWebEngineUrlRequestInfo &info); + bool shouldBlock(const QWebEngineUrlRequestInfo &info); + + bool isValid(); + +private: + bool testFlag(ResourceType flag); + QString flags(Resources f) const; + + Filter *m_parentItem; + QList<Filter*> m_children; + + RegExp m_domainUrl; + RegExp m_requestUrl; + + ResourceRules m_rules; + + bool m_valid = false; + bool m_shouldBlock; +}; + +#endif // FILTERNODE_H diff --git a/src/filter/filtercollection.cpp b/src/filter/filtercollection.cpp new file mode 100644 index 0000000..7b07606 --- /dev/null +++ b/src/filter/filtercollection.cpp @@ -0,0 +1,171 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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 + */ +FilterCollection::MatchResult FilterCollection::match(QWebEngineUrlRequestInfo &info) +{ + MatchResult result; + + for(Filter *rule : m_filters->filters(info.firstPartyUrl().toString())) { + if(rule->isValid()) { + result.match = rule->hasMatch(info); + if(result.match) { + result.block = rule->shouldBlock(info); + } else { + result.block = false; + } + result.pattern = rule->domain() + " | " + rule->request(); + return result; + } + } + + // request matches neither whitelist nor blacklist + result.match = false; + result.block = false; + return result; +} + +/** + * 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()); + + for(QJsonValue v : json["rules"].toArray()) { + ++number; + + QJsonObject obj = v.toObject(); + + Filter::ResourceRules r; + r.allowed = parseJsonRules(obj["allowTypes"]); + r.blocked = parseJsonRules(obj["blockTypes"]); + + m_filters->addFilter(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool()); + } + + 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 new file mode 100644 index 0000000..c39fb0a --- /dev/null +++ b/src/filter/filtercollection.h @@ -0,0 +1,61 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#ifndef SUBSCRIPTIONFORM_H +#define SUBSCRIPTIONFORM_H + +#include <QWidget> +#include <QFile> +#include "filtertree.h" + +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; + MatchResult match(QWebEngineUrlRequestInfo &info); + +private slots: + int load(const QJsonObject &json); + +private: + Filter::Resources parseJsonRules(const QJsonValue &obj); + + Ui::SubscriptionForm *ui; + + FilterTree *m_filters; + +}; + +#endif // SUBSCRIPTIONFORM_H diff --git a/src/filter/filtertree.cpp b/src/filter/filtertree.cpp new file mode 100644 index 0000000..b71c2a3 --- /dev/null +++ b/src/filter/filtertree.cpp @@ -0,0 +1,161 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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) { + nodes.append(rootItem->child(i)); + } + return nodes; +} diff --git a/src/filter/filtertree.h b/src/filter/filtertree.h new file mode 100644 index 0000000..161b199 --- /dev/null +++ b/src/filter/filtertree.h @@ -0,0 +1,51 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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 new file mode 100644 index 0000000..1f3c449 --- /dev/null +++ b/src/filter/regexp.cpp @@ -0,0 +1,72 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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 new file mode 100644 index 0000000..86cedfd --- /dev/null +++ b/src/filter/regexp.h @@ -0,0 +1,38 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see <http://www.gnu.org/licenses/>. + ** + ******************************************************************************/ + +#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 new file mode 100644 index 0000000..7a63cc9 --- /dev/null +++ b/src/filter/subscriptiondialog.ui @@ -0,0 +1,67 @@ +<?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 new file mode 100644 index 0000000..0a20582 --- /dev/null +++ b/src/filter/subscriptionform.ui @@ -0,0 +1,125 @@ +<?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> |