/*******************************************************************************
**
** 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
*/
bool FilterCollection::match(QWebEngineUrlRequestInfo &info)
{
for(QString domain : m_filterlist.keys()) {
if(domain.isEmpty() || info.firstPartyUrl().toString().contains(domain)) {
// domain matched
for(QString rule : m_filterlist.value(domain).keys()) {
if(rule.isEmpty() || info.requestUrl().toString().contains(rule)) {
return m_filterlist.value(domain).value(rule);
}
}
}
}
return false;
}
/**
* Load rules from JSON object
* @param json
*/
int FilterCollection::load(const QJsonObject &json)
{
int number = 0;
ui->title->setText(json["name"].toString());
ui->homepage->setText(json["url"].toString());
QJsonObject rules = json["rules"].toObject();
for(QString key : rules.keys()) {
++number;
qDebug("+ '%s'", qUtf8Printable(key));
QJsonObject domain = rules.value(key).toObject();
QHash d;
for(QString r : domain.keys()) {
d.insert(r, domain.value(r).toBool());
qDebug("|-'%s': %i", qUtf8Printable(r), domain.value(r).toBool());
}
m_filterlist.insert(key, d);
}
return number;
}
Filter::Resources FilterCollection::parseJsonRules(const QJsonValue &obj)
{
Filter::Resources res;
for(QJsonValue v : obj.toArray()) {
QString t = v.toString();
if(t == "MainFrame") {
res.setFlag(Filter::ResourceType::MainFrame);
} else if(t == "SubFrame") {
res.setFlag(Filter::ResourceType::SubFrame);
} else if(t == "Stylesheet") {
res.setFlag(Filter::ResourceType::Stylesheet);
} else if(t == "Script") {
res.setFlag(Filter::ResourceType::Script);
} else if(t == "Image") {
res.setFlag(Filter::ResourceType::Image);
} else if(t == "FontResource") {
res.setFlag(Filter::ResourceType::Font);
} else if(t == "SubResource") {
res.setFlag(Filter::ResourceType::SubResource);
} else if(t == "Object") {
res.setFlag(Filter::ResourceType::Object);
} else if(t == "Media") {
res.setFlag(Filter::ResourceType::Media);
} else if(t == "Worker") {
res.setFlag(Filter::ResourceType::Worker);
} else if(t == "SharedWorker") {
res.setFlag(Filter::ResourceType::SharedWorker);
} else if(t == "Prefetch") {
res.setFlag(Filter::ResourceType::Prefetch);
} else if(t == "Favicon") {
res.setFlag(Filter::ResourceType::Favicon);
} else if(t == "Xhr") {
res.setFlag(Filter::ResourceType::Xhr);
} else if(t == "Ping") {
res.setFlag(Filter::ResourceType::Ping);
} else if(t == "ServiceWorker") {
res.setFlag(Filter::ResourceType::ServiceWorker);
} else if(t == "CspWorker") {
res.setFlag(Filter::ResourceType::CspReport);
} else if(t == "PluginResource") {
res.setFlag(Filter::ResourceType::PluginResource);
}
}
return res;
}