diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/blocker/filtercollection.cpp | 11 | ||||
-rw-r--r-- | src/blocker/filtercollection.h | 2 | ||||
-rw-r--r-- | src/blocker/filternode.cpp | 71 | ||||
-rw-r--r-- | src/blocker/filternode.h | 53 | ||||
-rw-r--r-- | src/blocker/filtertree.cpp | 131 | ||||
-rw-r--r-- | src/blocker/filtertree.h | 50 | ||||
-rw-r--r-- | src/blocker/subscriptionform.ui | 26 |
7 files changed, 318 insertions, 26 deletions
diff --git a/src/blocker/filtercollection.cpp b/src/blocker/filtercollection.cpp index 2262008..5b0f96f 100644 --- a/src/blocker/filtercollection.cpp +++ b/src/blocker/filtercollection.cpp @@ -32,6 +32,10 @@ #include <QListWidget> #include <QLabel> +#include <QTreeView> +#include "filtertree.h" +#include "filternode.h" + FilterCollection::FilterCollection(const QString path, QWidget *parent) : QWidget(parent), ui(new Ui::SubscriptionForm) @@ -43,6 +47,8 @@ FilterCollection::FilterCollection(const QString path, QWidget *parent) : qDebug("Adding subscription [%s]", qUtf8Printable(m_url)); + m_filters = new FilterTree(this); + ui->treeView->setModel(m_filters); QFile filterFile(m_url); if(!filterFile.open(QIODevice::ReadOnly)) { @@ -104,7 +110,6 @@ void FilterCollection::load(const QJsonObject &json) for(QJsonValue v : json["rules"].toArray()) { r = createRule(v.toObject()); m_rules.append(r); - ui->blacklist_listWidget->addItem(r->filter()); } } @@ -123,6 +128,10 @@ FilterRule* FilterCollection::createRule(const QJsonObject &obj) rule = new FilterRule(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool(), this); + QList<QVariant> nodeData; + nodeData << obj["firstPartyUrl"].toString() << obj["requestUrl"].toString(); + m_filters->addFilter(nodeData); + return rule; } diff --git a/src/blocker/filtercollection.h b/src/blocker/filtercollection.h index 08b42f3..689f0a0 100644 --- a/src/blocker/filtercollection.h +++ b/src/blocker/filtercollection.h @@ -24,6 +24,7 @@ #include <QWidget> #include <QFile> #include "blocker/filterrule.h" +#include "filtertree.h" namespace Ui { class SubscriptionForm; @@ -58,6 +59,7 @@ private: QString m_url; QVector<FilterRule*> m_rules; + FilterTree *m_filters; }; diff --git a/src/blocker/filternode.cpp b/src/blocker/filternode.cpp new file mode 100644 index 0000000..e0612b5 --- /dev/null +++ b/src/blocker/filternode.cpp @@ -0,0 +1,71 @@ +/******************************************************************************* + ** + ** 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 "filternode.h" + +FilterNode::FilterNode(const QList<QVariant> data, FilterNode *parentItem) +{ + m_itemData = data; + m_parentItem = parentItem; +} + +FilterNode::~FilterNode() +{ + qDeleteAll(m_children); +} + +FilterNode *FilterNode::parentItem() +{ + return m_parentItem; +} + +void FilterNode::appendChild(FilterNode *child) +{ + m_children.append(child); +} + +FilterNode *FilterNode::child(int row) +{ + return m_children.value(row); +} + +int FilterNode::childCount() const +{ + return m_children.count(); +} + +int FilterNode::columnCount() const +{ + return m_itemData.count(); +} + +QVariant FilterNode::data(int column) const +{ + return m_itemData.value(column); +} + +int FilterNode::row() const +{ + if(m_parentItem) { + return m_parentItem->m_children.indexOf(const_cast<FilterNode*>(this)); + } + + return 0; +} diff --git a/src/blocker/filternode.h b/src/blocker/filternode.h new file mode 100644 index 0000000..104f67d --- /dev/null +++ b/src/blocker/filternode.h @@ -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/>. + ** + ******************************************************************************/ + +#ifndef FILTERNODE_H +#define FILTERNODE_H + +#include <QList> +#include <QVariant> + +class FilterNode +{ +public: + explicit FilterNode(const QList<QVariant> data, FilterNode *parentItem = 0); + ~FilterNode(); + + FilterNode *parentItem(); + + // children + void appendChild(FilterNode *child); + FilterNode *child(int row); + int childCount() const; + + // data + int columnCount() const; + QVariant data(int column) const; + + int row() const; + +private: + FilterNode *m_parentItem; + QList<FilterNode*> m_children; + + QList<QVariant> m_itemData; +}; + +#endif // FILTERNODE_H diff --git a/src/blocker/filtertree.cpp b/src/blocker/filtertree.cpp new file mode 100644 index 0000000..e8b7ed3 --- /dev/null +++ b/src/blocker/filtertree.cpp @@ -0,0 +1,131 @@ +/******************************************************************************* + ** + ** 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" +#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); +} + +FilterTree::~FilterTree() +{ + delete rootItem; +} + +QModelIndex FilterTree::index(int row, int column, const QModelIndex &parent) const +{ + if(!hasIndex(row, column, parent)) { + return QModelIndex(); + } + + FilterNode *parentItem; + if(!parent.isValid()) { + parentItem = rootItem; + } else { + parentItem = static_cast<FilterNode*>(parent.internalPointer()); + } + + FilterNode *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(); + } + + FilterNode *childItem = static_cast<FilterNode*>(index.internalPointer()); + FilterNode *parentItem = childItem->parentItem(); + + if(parentItem == rootItem) { + return QModelIndex(); + } + + return createIndex(parentItem->row(), 0, parentItem); +} + +int FilterTree::rowCount(const QModelIndex &parent) const +{ + FilterNode *parentItem; + if(parent.column() > 0) { + return 0; + } + + if(!parent.isValid()) { + parentItem = rootItem; + } else { + parentItem = static_cast<FilterNode*>(parent.internalPointer()); + } + + return parentItem->childCount(); +} + +int FilterTree::columnCount(const QModelIndex &parent) const +{ + if(parent.isValid()) { + return static_cast<FilterNode*>(parent.internalPointer())->columnCount(); + } else { + return rootItem->columnCount(); + } +} + +QVariant FilterTree::data(const QModelIndex &index, int role) const +{ + if(!index.isValid()) { + return QVariant(); + } + + if(role != Qt::DisplayRole) { + return QVariant(); + } + + return static_cast<FilterNode*>(index.internalPointer())->data(index.column()); +} + +QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const +{ + if(orientation != Qt::Horizontal) { + return QVariant(); + } + + if(role != Qt::DisplayRole) { + return QVariant(); + } + + return rootItem->data(section); +} + +FilterNode *FilterTree::addFilter(QList<QVariant> &data) +{ + FilterNode *node = new FilterNode(data, rootItem); + rootItem->appendChild(node); + return node; +} diff --git a/src/blocker/filtertree.h b/src/blocker/filtertree.h new file mode 100644 index 0000000..eabca38 --- /dev/null +++ b/src/blocker/filtertree.h @@ -0,0 +1,50 @@ +/******************************************************************************* + ** + ** 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> + +class FilterNode; +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; + + FilterNode *addFilter(QList<QVariant> &data); + +private: + FilterNode *rootItem; +}; + +#endif // FILTERTREE_H diff --git a/src/blocker/subscriptionform.ui b/src/blocker/subscriptionform.ui index 703fb6d..0a20582 100644 --- a/src/blocker/subscriptionform.ui +++ b/src/blocker/subscriptionform.ui @@ -116,31 +116,7 @@ </widget> </item> <item> - <widget class="QTabWidget" name="tabWidget"> - <property name="currentIndex"> - <number>0</number> - </property> - <widget class="QWidget" name="blacklist_tab"> - <attribute name="title"> - <string>URL Blacklist</string> - </attribute> - <layout class="QVBoxLayout" name="verticalLayout_2"> - <item> - <widget class="QListWidget" name="blacklist_listWidget"/> - </item> - </layout> - </widget> - <widget class="QWidget" name="whitelist_tab"> - <attribute name="title"> - <string>URL Whitelist</string> - </attribute> - <layout class="QVBoxLayout" name="verticalLayout_3"> - <item> - <widget class="QListWidget" name="whitelist_listWidget"/> - </item> - </layout> - </widget> - </widget> + <widget class="QTreeView" name="treeView"/> </item> </layout> </widget> |