From 1143429887f61d8b3c74a4c3a1fafca632eb4b87 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 25 May 2017 20:59:43 +0200 Subject: Filter code refactoring --- src/filter/blockermanager.cpp | 53 +++++++++ src/filter/blockermanager.h | 46 ++++++++ src/filter/filter.cpp | 237 +++++++++++++++++++++++++++++++++++++++ src/filter/filter.h | 106 +++++++++++++++++ src/filter/filtercollection.cpp | 171 ++++++++++++++++++++++++++++ src/filter/filtercollection.h | 61 ++++++++++ src/filter/filtertree.cpp | 161 ++++++++++++++++++++++++++ src/filter/filtertree.h | 51 +++++++++ src/filter/regexp.cpp | 72 ++++++++++++ src/filter/regexp.h | 38 +++++++ src/filter/subscriptiondialog.ui | 67 +++++++++++ src/filter/subscriptionform.ui | 125 +++++++++++++++++++++ 12 files changed, 1188 insertions(+) create mode 100644 src/filter/blockermanager.cpp create mode 100644 src/filter/blockermanager.h create mode 100644 src/filter/filter.cpp create mode 100644 src/filter/filter.h create mode 100644 src/filter/filtercollection.cpp create mode 100644 src/filter/filtercollection.h create mode 100644 src/filter/filtertree.cpp create mode 100644 src/filter/filtertree.h create mode 100644 src/filter/regexp.cpp create mode 100644 src/filter/regexp.h create mode 100644 src/filter/subscriptiondialog.ui create mode 100644 src/filter/subscriptionform.ui (limited to 'src/filter') 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 . + ** + ******************************************************************************/ + +#include "blockermanager.h" +#include "ui_subscriptiondialog.h" + +#include "browser.h" +#include +#include + +#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 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 . + ** + ******************************************************************************/ + +#ifndef URLINTERCEPTORDIALOG_H +#define URLINTERCEPTORDIALOG_H + +#include + +namespace Ui { +class UrlInterceptorDialog; +} + +class FilterCollection; +class BlockerManager : public QDialog +{ + Q_OBJECT + +public: + explicit BlockerManager(QWidget *parent = 0); + ~BlockerManager(); + + QVector subscriptions() const; + +private: + Ui::UrlInterceptorDialog *ui; + QVector 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 . + ** + ******************************************************************************/ + +#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(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 . + ** + ******************************************************************************/ + +#ifndef FILTERNODE_H +#define FILTERNODE_H + +#include +#include +#include +#include "regexp.h" +#include + +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 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 . + ** + ******************************************************************************/ + +#include "filtercollection.h" +#include "ui_subscriptionform.h" + +#include "browser.h" +#include +#include + +#include +#include +#include + +#include +#include + +#include +#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 . + ** + ******************************************************************************/ + +#ifndef SUBSCRIPTIONFORM_H +#define SUBSCRIPTIONFORM_H + +#include +#include +#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 . + ** + ******************************************************************************/ + +#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(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(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(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(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 FilterTree::filters(const QString &domain) +{ + QVector 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 . + ** + ******************************************************************************/ + +#ifndef FILTERTREE_H +#define FILTERTREE_H + +#include +#include +#include +#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 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 . + ** + ******************************************************************************/ + +#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. + ** + ******************************************************************************/ + +#ifndef REGEXP_H +#define REGEXP_H + +#include + +/*! + * 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 @@ + + + UrlInterceptorDialog + + + + 0 + 0 + 640 + 480 + + + + Blocker + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + accepted() + UrlInterceptorDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + UrlInterceptorDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + 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 @@ + + + SubscriptionForm + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + + Subscription + + + + + + + + Title + + + + + + + TextLabel + + + + + + + Homepage + + + + + + + TextLabel + + + + + + + License + + + + + + + TextLabel + + + + + + + + + + + Version + + + + + + + TextLabel + + + + + + + Last Modified + + + + + + + TextLabel + + + + + + + Expires + + + + + + + TextLabel + + + + + + + + + + + + + + + + -- cgit v1.2.1