2011-03-27 21:59:40 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2011-10-17 09:57:07 +02:00
|
|
|
* Copyright (C) 2010-2011 David Rosca
|
2011-03-27 21:59:40 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include "adblocksubscription.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "networkmanager.h"
|
|
|
|
// #define ADBLOCKSUBSCRIPTION_DEBUG
|
|
|
|
|
2011-03-29 20:30:05 +02:00
|
|
|
AdBlockSubscription::AdBlockSubscription(QObject* parent)
|
2011-03-27 21:59:40 +02:00
|
|
|
: QObject(parent)
|
|
|
|
, m_downloading(0)
|
|
|
|
{
|
|
|
|
loadRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::loadRules()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
QString fileName = mApp->getActiveProfilPath() + "adblocklist.txt";
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
QFile file(fileName);
|
|
|
|
if (file.exists()) {
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for reading" << fileName;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-27 21:59:40 +02:00
|
|
|
QTextStream textStream(&file);
|
|
|
|
QString header = textStream.readLine(1024);
|
|
|
|
if (!header.startsWith("[Adblock")) {
|
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "adblock file does not start with [Adblock" << fileName << "Header:" << header;
|
|
|
|
file.close();
|
|
|
|
file.remove();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-27 21:59:40 +02:00
|
|
|
m_rules.clear();
|
|
|
|
while (!textStream.atEnd()) {
|
|
|
|
QString line = textStream.readLine();
|
|
|
|
m_rules.append(AdBlockRule(line));
|
|
|
|
}
|
|
|
|
populateCache();
|
|
|
|
emit rulesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::updateNow()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_downloading) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
QNetworkRequest request(QUrl("https://easylist-downloads.adblockplus.org/easylist.txt"));
|
2011-03-29 20:30:05 +02:00
|
|
|
QNetworkReply* reply = mApp->networkManager()->get(request);
|
2011-03-27 21:59:40 +02:00
|
|
|
m_downloading = reply;
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(rulesDownloaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::rulesDownloaded()
|
|
|
|
{
|
2011-03-29 20:30:05 +02:00
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!reply) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
QByteArray response = reply->readAll();
|
|
|
|
reply->close();
|
|
|
|
reply->deleteLater();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (response.isEmpty()) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-10-17 13:43:51 +02:00
|
|
|
QString fileName = mApp->getActiveProfilPath() + "adblocklist.txt";
|
2011-03-27 21:59:40 +02:00
|
|
|
QFile file(fileName);
|
2011-03-29 20:30:05 +02:00
|
|
|
if (!file.open(QFile::WriteOnly)) {
|
2011-03-27 21:59:40 +02:00
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << fileName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
response = response.left(response.indexOf("!-----------------General element hiding rules-----------------!"));
|
|
|
|
|
|
|
|
bool customRules = false;
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(const AdBlockRule & rule, allRules()) {
|
2011-03-27 21:59:40 +02:00
|
|
|
if (rule.filter().contains("*******- user custom filters")) {
|
|
|
|
customRules = true;
|
|
|
|
response.append("! *******- user custom filters -*************\n");
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!customRules) {
|
2011-03-27 21:59:40 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
response.append(rule.filter() + "\n");
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file.write(response);
|
|
|
|
file.close();
|
|
|
|
loadRules();
|
|
|
|
emit changed();
|
|
|
|
m_downloading = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::saveRules()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
QString fileName = mApp->getActiveProfilPath() + "adblocklist.txt";
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QFile::ReadWrite | QIODevice::Truncate)) {
|
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << fileName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream textStream(&file);
|
|
|
|
textStream << "[Adblock Plus 1.1.1]" << endl;
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(const AdBlockRule & rule, m_rules)
|
|
|
|
textStream << rule.filter() << endl;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const AdBlockRule* AdBlockSubscription::allow(const QString &urlString) const
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(const AdBlockRule * rule, m_networkExceptionRules) {
|
|
|
|
if (rule->networkMatch(urlString)) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return rule;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AdBlockRule* AdBlockSubscription::block(const QString &urlString) const
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(const AdBlockRule * rule, m_networkBlockRules) {
|
|
|
|
if (rule->networkMatch(urlString)) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return rule;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<AdBlockRule> AdBlockSubscription::allRules() const
|
|
|
|
{
|
|
|
|
return m_rules;
|
|
|
|
}
|
|
|
|
|
2011-03-29 21:51:27 +02:00
|
|
|
int AdBlockSubscription::addRule(const AdBlockRule &rule)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
|
|
|
m_rules.append(rule);
|
|
|
|
populateCache();
|
|
|
|
emit rulesChanged();
|
2011-03-29 21:51:27 +02:00
|
|
|
return m_rules.count() - 1;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::removeRule(int offset)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (offset < 0 || offset >= m_rules.count()) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
m_rules.removeAt(offset);
|
|
|
|
populateCache();
|
|
|
|
emit rulesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::replaceRule(const AdBlockRule &rule, int offset)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (offset < 0 || offset >= m_rules.count()) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
m_rules[offset] = rule;
|
|
|
|
populateCache();
|
|
|
|
emit rulesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::populateCache()
|
|
|
|
{
|
|
|
|
m_networkExceptionRules.clear();
|
|
|
|
m_networkBlockRules.clear();
|
|
|
|
m_pageRules.clear();
|
|
|
|
|
|
|
|
for (int i = 0; i < m_rules.count(); ++i) {
|
2011-03-29 20:30:05 +02:00
|
|
|
const AdBlockRule* rule = &m_rules.at(i);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!rule->isEnabled()) {
|
2011-03-27 21:59:40 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
if (rule->isCSSRule()) {
|
|
|
|
m_pageRules.append(rule);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rule->isException()) {
|
|
|
|
m_networkExceptionRules.append(rule);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-27 21:59:40 +02:00
|
|
|
m_networkBlockRules.append(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|