aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-05-25 16:12:29 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2017-05-25 16:12:29 +0200
commit49324c8d4acbf36902754a2f1c62fb134df1b19e (patch)
tree3e4be38562d17f01a6051b9a895c5681e8f0b8ff
parentDisplaying filter rules in a tree model view (diff)
downloadsmolbote-49324c8d4acbf36902754a2f1c62fb134df1b19e.tar.xz
Merged FilterRule into FilterNode
-rw-r--r--data/poi.toml2
-rw-r--r--smolbote.qbs2
-rw-r--r--src/blocker/filtercollection.cpp106
-rw-r--r--src/blocker/filtercollection.h7
-rw-r--r--src/blocker/filternode.cpp109
-rw-r--r--src/blocker/filternode.h58
-rw-r--r--src/blocker/filterrule.cpp119
-rw-r--r--src/blocker/filterrule.h89
-rw-r--r--src/blocker/filtertree.cpp57
-rw-r--r--src/blocker/filtertree.h5
-rw-r--r--test/filter.json2
11 files changed, 249 insertions, 307 deletions
diff --git a/data/poi.toml b/data/poi.toml
index 36247ca..7359630 100644
--- a/data/poi.toml
+++ b/data/poi.toml
@@ -46,7 +46,7 @@ tabtoolbarMovable=false
# URL blocker
[blocker]
-shortcut="Ctrl+Shift+A"
+shortcut="Ctrl+Shift+F"
path="$settings/"
subscriptions=[
"filter.json"
diff --git a/smolbote.qbs b/smolbote.qbs
index 63ea35d..3015750 100644
--- a/smolbote.qbs
+++ b/smolbote.qbs
@@ -123,8 +123,6 @@ Project {
"src/blocker/filtertree.h",
"src/blocker/filtercollection.cpp",
"src/blocker/filtercollection.h",
- "src/blocker/filterrule.cpp",
- "src/blocker/filterrule.h",
"src/blocker/regexp.cpp",
"src/blocker/regexp.h",
"src/blocker/subscriptiondialog.ui",
diff --git a/src/blocker/filtercollection.cpp b/src/blocker/filtercollection.cpp
index 5b0f96f..935776c 100644
--- a/src/blocker/filtercollection.cpp
+++ b/src/blocker/filtercollection.cpp
@@ -42,15 +42,14 @@ FilterCollection::FilterCollection(const QString path, QWidget *parent) :
{
ui->setupUi(this);
- m_name = "TODO";
- m_url = path;
+ ui->homepage->setText(path);
- qDebug("Adding subscription [%s]", qUtf8Printable(m_url));
+ qDebug("Adding subscription [%s]", qUtf8Printable(path));
m_filters = new FilterTree(this);
ui->treeView->setModel(m_filters);
- QFile filterFile(m_url);
+ QFile filterFile(path);
if(!filterFile.open(QIODevice::ReadOnly)) {
qWarning("Could not open filter!");
return;
@@ -59,7 +58,7 @@ FilterCollection::FilterCollection(const QString path, QWidget *parent) :
QJsonDocument filters(QJsonDocument::fromJson(filterFile.readAll()));
load(filters.object());
- qDebug("Added %i rules", m_rules.size());
+ qDebug("Added %i rules", m_filters->columnCount());
}
FilterCollection::~FilterCollection()
@@ -69,7 +68,7 @@ FilterCollection::~FilterCollection()
QString FilterCollection::name() const
{
- return m_name;
+ return ui->title->text();
}
/**
@@ -81,15 +80,17 @@ FilterCollection::MatchResult FilterCollection::match(QWebEngineUrlRequestInfo &
{
MatchResult result;
- for(FilterRule *rule : qAsConst(m_rules)) {
- result.match = rule->hasMatch(info);
- if(result.match) {
- result.block = rule->shouldBlock(info);
- } else {
- result.block = false;
+ for(FilterNode *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;
}
- result.pattern = rule->filter();
- return result;
}
// request matches neither whitelist nor blacklist
@@ -100,82 +101,61 @@ FilterCollection::MatchResult FilterCollection::match(QWebEngineUrlRequestInfo &
void FilterCollection::load(const QJsonObject &json)
{
- m_name = json["name"].toString();
- ui->title->setText(m_name);
+ ui->title->setText(json["name"].toString());
+ ui->homepage->setText(json["url"].toString());
- m_url = json["url"].toString();
- ui->homepage->setText(m_url);
-
- FilterRule *r;
for(QJsonValue v : json["rules"].toArray()) {
- r = createRule(v.toObject());
- m_rules.append(r);
- }
-}
-
-/**
- * Create rule from JSON object
- * @param obj
- * @return
- */
-FilterRule* FilterCollection::createRule(const QJsonObject &obj)
-{
- FilterRule *rule;
-
- FilterRule::ResourceRules r;
- r.allowed = parseJsonRules(obj["allowTypes"]);
- r.blocked = parseJsonRules(obj["blockTypes"]);
+ QJsonObject obj = v.toObject();
- rule = new FilterRule(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool(), this);
+ FilterNode::ResourceRules r;
+ r.allowed = parseJsonRules(obj["allowTypes"]);
+ r.blocked = parseJsonRules(obj["blockTypes"]);
- QList<QVariant> nodeData;
- nodeData << obj["firstPartyUrl"].toString() << obj["requestUrl"].toString();
- m_filters->addFilter(nodeData);
-
- return rule;
+ m_filters->addFilter(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool());
+ }
}
-FilterRule::Resources FilterCollection::parseJsonRules(const QJsonValue &obj)
+FilterNode::Resources FilterCollection::parseJsonRules(const QJsonValue &obj)
{
- FilterRule::Resources res;
+ FilterNode::Resources res;
for(QJsonValue v : obj.toArray()) {
QString t = v.toString();
if(t == "MainFrame") {
- res.setFlag(FilterRule::ResourceType::MainFrame);
+ res.setFlag(FilterNode::ResourceType::MainFrame);
} else if(t == "SubFrame") {
- res.setFlag(FilterRule::ResourceType::SubFrame);
+ res.setFlag(FilterNode::ResourceType::SubFrame);
} else if(t == "Stylesheet") {
- res.setFlag(FilterRule::ResourceType::Stylesheet);
+ res.setFlag(FilterNode::ResourceType::Stylesheet);
} else if(t == "Script") {
- res.setFlag(FilterRule::ResourceType::Script);
+ res.setFlag(FilterNode::ResourceType::Script);
} else if(t == "Image") {
- res.setFlag(FilterRule::ResourceType::Image);
+ res.setFlag(FilterNode::ResourceType::Image);
} else if(t == "FontResource") {
- res.setFlag(FilterRule::ResourceType::FontResource);
+ res.setFlag(FilterNode::ResourceType::FontResource);
} else if(t == "SubResource") {
- res.setFlag(FilterRule::ResourceType::SubResource);
+ res.setFlag(FilterNode::ResourceType::SubResource);
} else if(t == "Object") {
- res.setFlag(FilterRule::ResourceType::Object);
+ res.setFlag(FilterNode::ResourceType::Object);
} else if(t == "Media") {
- res.setFlag(FilterRule::ResourceType::Media);
+ res.setFlag(FilterNode::ResourceType::Media);
} else if(t == "Worker") {
- res.setFlag(FilterRule::ResourceType::Worker);
+ res.setFlag(FilterNode::ResourceType::Worker);
} else if(t == "SharedWorker") {
- res.setFlag(FilterRule::ResourceType::SharedWorker);
+ res.setFlag(FilterNode::ResourceType::SharedWorker);
} else if(t == "Prefetch") {
- res.setFlag(FilterRule::ResourceType::Prefetch);
+ res.setFlag(FilterNode::ResourceType::Prefetch);
} else if(t == "Favicon") {
- res.setFlag(FilterRule::ResourceType::Favicon);
+ res.setFlag(FilterNode::ResourceType::Favicon);
} else if(t == "Xhr") {
- res.setFlag(FilterRule::ResourceType::Xhr);
+ res.setFlag(FilterNode::ResourceType::Xhr);
} else if(t == "Ping") {
- res.setFlag(FilterRule::ResourceType::Ping);
+ res.setFlag(FilterNode::ResourceType::Ping);
} else if(t == "ServiceWorker") {
- res.setFlag(FilterRule::ResourceType::ServiceWorker);
+ res.setFlag(FilterNode::ResourceType::ServiceWorker);
} else if(t == "CspWorker") {
- res.setFlag(FilterRule::ResourceType::CspReport);
+ res.setFlag(FilterNode::ResourceType::CspReport);
} else if(t == "PluginResource") {
- res.setFlag(FilterRule::ResourceType::PluginResource);
+ res.setFlag(FilterNode::ResourceType::PluginResource);
}
}
diff --git a/src/blocker/filtercollection.h b/src/blocker/filtercollection.h
index 689f0a0..4059ff1 100644
--- a/src/blocker/filtercollection.h
+++ b/src/blocker/filtercollection.h
@@ -23,7 +23,6 @@
#include <QWidget>
#include <QFile>
-#include "blocker/filterrule.h"
#include "filtertree.h"
namespace Ui {
@@ -51,14 +50,10 @@ private slots:
void load(const QJsonObject &json);
private:
- FilterRule* createRule(const QJsonObject &obj);
- FilterRule::Resources parseJsonRules(const QJsonValue &obj);
+ FilterNode::Resources parseJsonRules(const QJsonValue &obj);
Ui::SubscriptionForm *ui;
- QString m_name;
- QString m_url;
- QVector<FilterRule*> m_rules;
FilterTree *m_filters;
};
diff --git a/src/blocker/filternode.cpp b/src/blocker/filternode.cpp
index e0612b5..67614ce 100644
--- a/src/blocker/filternode.cpp
+++ b/src/blocker/filternode.cpp
@@ -20,10 +20,20 @@
#include "filternode.h"
-FilterNode::FilterNode(const QList<QVariant> data, FilterNode *parentItem)
+FilterNode::FilterNode(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, FilterNode *parentItem)
{
- m_itemData = data;
m_parentItem = parentItem;
+
+ m_domainUrl.setPattern(domain);
+ m_requestUrl.setPattern(request);
+ m_rules = rules;
+ m_shouldBlock = shouldBlock;
+
+ m_valid = false;
+
+#ifdef DEBUG_VERBOSE
+ qDebug("Created rule [%s] [%s]", qUtf8Printable(domain), qUtf8Printable(request));
+#endif
}
FilterNode::~FilterNode()
@@ -31,6 +41,11 @@ FilterNode::~FilterNode()
qDeleteAll(m_children);
}
+void FilterNode::enable()
+{
+ m_valid = true;
+}
+
FilterNode *FilterNode::parentItem()
{
return m_parentItem;
@@ -51,14 +66,19 @@ int FilterNode::childCount() const
return m_children.count();
}
-int FilterNode::columnCount() const
+QString FilterNode::domain() const
+{
+ return m_domainUrl.pattern();
+}
+
+QString FilterNode::request() const
{
- return m_itemData.count();
+ return m_requestUrl.pattern();
}
-QVariant FilterNode::data(int column) const
+bool FilterNode::isBlocking()
{
- return m_itemData.value(column);
+ return m_shouldBlock;
}
int FilterNode::row() const
@@ -69,3 +89,80 @@ int FilterNode::row() const
return 0;
}
+
+bool FilterNode::hasMatch(const QWebEngineUrlRequestInfo &info)
+{
+ if(m_domainUrl.hasMatch(info.firstPartyUrl().toString()) && m_requestUrl.hasMatch(info.requestUrl().toString())) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool FilterNode::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(FontResource);
+ 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 FilterNode::isValid()
+{
+ return m_valid;
+}
+
+bool FilterNode::testFlag(ResourceType flag)
+{
+ if(m_rules.allowed.testFlag(flag)) {
+ return false;
+ } else if(m_rules.blocked.testFlag(flag)) {
+ return true;
+ } else {
+ return m_shouldBlock;
+ }
+}
diff --git a/src/blocker/filternode.h b/src/blocker/filternode.h
index 104f67d..e580383 100644
--- a/src/blocker/filternode.h
+++ b/src/blocker/filternode.h
@@ -23,13 +23,48 @@
#include <QList>
#include <QVariant>
+#include <QUrl>
+#include "regexp.h"
+#include <QWebEngineUrlRequestInfo>
class FilterNode
{
public:
- explicit FilterNode(const QList<QVariant> data, FilterNode *parentItem = 0);
+
+ enum ResourceType {
+ NoType = 0,
+ MainFrame = 1,
+ SubFrame = 2,
+ Stylesheet = 4,
+ Script = 8,
+ Image = 16,
+ FontResource = 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 FilterNode(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, FilterNode *parentItem = 0);
~FilterNode();
+ void enable();
+
FilterNode *parentItem();
// children
@@ -38,16 +73,31 @@ public:
int childCount() const;
// data
- int columnCount() const;
- QVariant data(int column) const;
+ QString domain() const;
+ QString request() const;
+ bool isBlocking();
int row() const;
+ // filtering
+ bool hasMatch(const QWebEngineUrlRequestInfo &info);
+ bool shouldBlock(const QWebEngineUrlRequestInfo &info);
+
+ bool isValid();
+
private:
+ bool testFlag(ResourceType flag);
+
FilterNode *m_parentItem;
QList<FilterNode*> m_children;
- QList<QVariant> m_itemData;
+ RegExp m_domainUrl;
+ RegExp m_requestUrl;
+
+ ResourceRules m_rules;
+
+ bool m_valid = false;
+ bool m_shouldBlock;
};
#endif // FILTERNODE_H
diff --git a/src/blocker/filterrule.cpp b/src/blocker/filterrule.cpp
deleted file mode 100644
index f7ba00b..0000000
--- a/src/blocker/filterrule.cpp
+++ /dev/null
@@ -1,119 +0,0 @@
-/*******************************************************************************
- **
- ** 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 "filterrule.h"
-
-FilterRule::FilterRule(const QString firstPartyUrl, const QString requestUrl, ResourceRules rules, bool shouldBlock, QObject *parent) :
- QObject(parent)
-{
- m_firstPartyUrl.setPattern(firstPartyUrl);
- m_requestUrl.setPattern(requestUrl);
- m_rules = rules;
- m_shouldBlock = shouldBlock;
-
- m_valid = true;
-
-#ifdef DEBUG_VERBOSE
- qDebug("Created rule [%s] [%s]", qUtf8Printable(firstPartyUrl), qUtf8Printable(requestUrl));
-#endif
-
-}
-
-bool FilterRule::hasMatch(const QWebEngineUrlRequestInfo &info)
-{
- if(m_firstPartyUrl.hasMatch(info.firstPartyUrl().toString()) && m_requestUrl.hasMatch(info.requestUrl().toString())) {
- return true;
- } else {
- return false;
- }
-}
-
-bool FilterRule::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(FontResource);
- 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 FilterRule::isValid()
-{
- return m_valid;
-}
-
-QString FilterRule::filter() const
-{
- return "'" + m_firstPartyUrl.pattern() + "' | '" + m_requestUrl.pattern() + "'";
-}
-
-bool FilterRule::testFlag(ResourceType flag)
-{
- if(m_rules.allowed.testFlag(flag)) {
- return false;
- } else if(m_rules.blocked.testFlag(flag)) {
- return true;
- } else {
- return m_shouldBlock;
- }
-}
diff --git a/src/blocker/filterrule.h b/src/blocker/filterrule.h
deleted file mode 100644
index 44340fc..0000000
--- a/src/blocker/filterrule.h
+++ /dev/null
@@ -1,89 +0,0 @@
-/*******************************************************************************
- **
- ** 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 ADBLOCKRULE_H
-#define ADBLOCKRULE_H
-
-#include <QObject>
-#include <QUrl>
-#include "regexp.h"
-#include <QWebEngineUrlRequestInfo>
-
-class FilterRule : public QObject
-{
- Q_OBJECT
-public:
-
- enum ResourceType {
- NoType = 0,
- MainFrame = 1,
- SubFrame = 2,
- Stylesheet = 4,
- Script = 8,
- Image = 16,
- FontResource = 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 FilterRule(const QString firstPartyUrl, const QString requestUrl, ResourceRules rules, bool shouldBlock, QObject *parent = 0);
-
- bool hasMatch(const QWebEngineUrlRequestInfo &info);
- bool shouldBlock(const QWebEngineUrlRequestInfo &info);
-
- bool isValid();
-
- QString filter() const;
-
-signals:
-
-public slots:
-
-private:
- bool testFlag(ResourceType flag);
-
- RegExp m_firstPartyUrl;
- RegExp m_requestUrl;
-
- ResourceRules m_rules;
-
- bool m_valid = false;
- bool m_shouldBlock;
-
-};
-
-#endif // ADBLOCKRULE_H
diff --git a/src/blocker/filtertree.cpp b/src/blocker/filtertree.cpp
index e8b7ed3..e264015 100644
--- a/src/blocker/filtertree.cpp
+++ b/src/blocker/filtertree.cpp
@@ -19,15 +19,11 @@
******************************************************************************/
#include "filtertree.h"
-#include "filternode.h"
FilterTree::FilterTree(QObject *parent) :
QAbstractItemModel(parent)
{
- // create some nodes here
- QList<QVariant> rootData;
- rootData << "Domain" << "Request" << "Blocked" << "Allowed Types" << "Blocked Types";
- rootItem = new FilterNode(rootData);
+ rootItem = new FilterNode("", "", FilterNode::ResourceRules(), false);
}
FilterTree::~FilterTree()
@@ -90,11 +86,7 @@ int FilterTree::rowCount(const QModelIndex &parent) const
int FilterTree::columnCount(const QModelIndex &parent) const
{
- if(parent.isValid()) {
- return static_cast<FilterNode*>(parent.internalPointer())->columnCount();
- } else {
- return rootItem->columnCount();
- }
+ return 5;
}
QVariant FilterTree::data(const QModelIndex &index, int role) const
@@ -107,7 +99,21 @@ QVariant FilterTree::data(const QModelIndex &index, int role) const
return QVariant();
}
- return static_cast<FilterNode*>(index.internalPointer())->data(index.column());
+ FilterNode *n = static_cast<FilterNode*>(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("---");
+ case 4: // blocked types
+ return QVariant("---");
+ default:
+ return QVariant();
+ }
}
QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const
@@ -120,12 +126,35 @@ QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int ro
return QVariant();
}
- return rootItem->data(section);
+ 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();
+ }
}
-FilterNode *FilterTree::addFilter(QList<QVariant> &data)
+FilterNode *FilterTree::addFilter(const QString &domain, const QString &request, FilterNode::ResourceRules rules, bool shouldBlock)
{
- FilterNode *node = new FilterNode(data, rootItem);
+ FilterNode *node = new FilterNode(domain, request, rules, shouldBlock, rootItem);
+ node->enable();
rootItem->appendChild(node);
return node;
}
+
+QVector<FilterNode*> FilterTree::filters(const QString &domain)
+{
+ QVector<FilterNode*> nodes;
+ for(int i = 0; i < rootItem->childCount(); ++i) {
+ nodes.append(rootItem->child(i));
+ }
+ return nodes;
+}
diff --git a/src/blocker/filtertree.h b/src/blocker/filtertree.h
index eabca38..0bae0e0 100644
--- a/src/blocker/filtertree.h
+++ b/src/blocker/filtertree.h
@@ -24,8 +24,8 @@
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
+#include "filternode.h"
-class FilterNode;
class FilterTree : public QAbstractItemModel
{
Q_OBJECT
@@ -41,7 +41,8 @@ public:
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
- FilterNode *addFilter(QList<QVariant> &data);
+ FilterNode *addFilter(const QString &domain, const QString &request, FilterNode::ResourceRules rules, bool shouldBlock);
+ QVector<FilterNode*> filters(const QString &domain = "");
private:
FilterNode *rootItem;
diff --git a/test/filter.json b/test/filter.json
index ffcdf56..dc4c55b 100644
--- a/test/filter.json
+++ b/test/filter.json
@@ -7,7 +7,7 @@
"requestUrl": "css",
"allowTypes": ["Stylesheet"],
"blockTypes": [],
- "shouldBlock": false
+ "shouldBlock": true
}
]
}