From 7f6c9b22d1016aa0dba709495fabf41397676039 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 28 Feb 2017 10:18:08 +0100 Subject: Blocker rewrites Some code commenting Moved Blocker files for src/blocker Keyboard shortcut for Blocker dialog --- src/blocker/blockersubscription.cpp | 138 ++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/blocker/blockersubscription.cpp (limited to 'src/blocker/blockersubscription.cpp') diff --git a/src/blocker/blockersubscription.cpp b/src/blocker/blockersubscription.cpp new file mode 100644 index 0000000..02def48 --- /dev/null +++ b/src/blocker/blockersubscription.cpp @@ -0,0 +1,138 @@ +/** LICENSE ******************************************************************** + ** + ** 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 "blockersubscription.h" +#include "ui_subscriptionform.h" + +#include "browser.h" +#include +#include +#include + +BlockerSubscription::BlockerSubscription(const QString url, QWidget *parent) : + QWidget(parent), + ui(new Ui::SubscriptionForm) +{ + ui->setupUi(this); + QUrl _url = QUrl::fromUserInput(url); + m_name = _url.fileName(); + + if(!sSettings->value("blocker.path").toString().isEmpty()) { + QString cacheName = sSettings->value("blocker.path").toString() + m_name; + QFile *cache = new QFile(cacheName); + if(cache->exists()) { + load(cache); + } + + } else { + + // no cache path specified - pull the subscription + QNetworkRequest request; + request.setUrl(QUrl::fromUserInput(url)); + + QNetworkReply *reply = sNetwork->get(request); + connect(reply, &QNetworkReply::finished, [this, reply]() { + this->load(reply); + }); + } +} + +BlockerSubscription::~BlockerSubscription() +{ + delete ui; +} + +QString BlockerSubscription::name() const +{ + return m_name; +} + +void BlockerSubscription::load(QIODevice *dev) +{ + QTextStream subscription(dev); + + QString header = subscription.readLine(); + if(!header.startsWith("[Adblock Plus")) { + qDebug("Invalid format of subscription: %s", qUtf8Printable(m_name)); + return; + } + + // clear all lists + m_urlBlacklist.clear(); + m_urlWhitelist.clear(); + int rules = 0; + + while(!subscription.atEnd()) { + QString line = subscription.readLine(); + if(!line.isEmpty()) { + if(line.startsWith('!')) { + parseComment(line); + } else { + // The line is not a comment + + rules++; + BlockerRule *rule = new BlockerRule(line, this); + + if(rule->isValid()) { + if(rule->isException()) { + m_urlWhitelist.append(rule); + ui->whitelist_listWidget->addItem(rule->toString()); + } else { + ui->blacklist_listWidget->addItem(rule->toString()); + m_urlBlacklist.append(rule); + } + } + + } + } + } + + qDebug("Loaded %i/%i rules from subscription %s", m_urlBlacklist.count() + m_urlWhitelist.count(), rules, qUtf8Printable(m_name)); + dev->deleteLater(); +} + + +void BlockerSubscription::parseComment(const QString &line) +{ + if(line.startsWith("! Title: ")) { + ui->title->setText(line.right(line.length() - 9)); + return; + } + if(line.startsWith("! Homepage: ")) { + ui->homepage->setText(line.right(line.length() - 12)); + return; + } + if(line.startsWith("! Licence: ")) { + ui->license->setText(line.right(line.length() - 11)); + return; + } + if(line.startsWith("! Version: ")) { + ui->version->setText(line.right(line.length() - 11)); + return; + } + if(line.startsWith("! Last modified: ")) { + ui->lastModified->setText(line.right(line.length() - 17)); + return; + } + if(line.startsWith("! Expires: ")) { + ui->expires->setText(line.right(line.length() - 11).left(2)); + return; + } +} -- cgit v1.2.1