2011-03-27 21:59:40 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-02-08 12:10:12 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
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"
|
2012-07-01 20:11:37 +02:00
|
|
|
#include "adblockmanager.h"
|
2011-03-27 21:59:40 +02:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "networkmanager.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-07-03 21:54:04 +02:00
|
|
|
#include "followredirectreply.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QDebug>
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent)
|
2011-03-27 21:59:40 +02:00
|
|
|
: QObject(parent)
|
2012-06-24 23:46:32 +02:00
|
|
|
, m_reply(0)
|
|
|
|
, m_title(title)
|
2012-07-03 21:54:04 +02:00
|
|
|
, m_updated(false)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
QString AdBlockSubscription::title() const
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
return m_title;
|
|
|
|
}
|
2011-12-28 19:28:34 +01:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
QString AdBlockSubscription::filePath() const
|
|
|
|
{
|
|
|
|
return m_filePath;
|
2011-12-28 19:28:34 +01:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockSubscription::setFilePath(const QString &path)
|
2011-12-28 19:28:34 +01:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
m_filePath = path;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
QUrl AdBlockSubscription::url() const
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
return m_url;
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockSubscription::setUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
m_url = url;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
void AdBlockSubscription::loadSubscription(const QStringList &disabledRules)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
QFile file(m_filePath);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
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
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for reading" << m_filePath;
|
|
|
|
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
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
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
QTextStream textStream(&file);
|
2013-02-20 19:50:59 +01:00
|
|
|
textStream.setCodec("UTF-8");
|
2012-06-24 23:46:32 +02:00
|
|
|
// Header is on 3rd line
|
|
|
|
textStream.readLine(1024);
|
|
|
|
textStream.readLine(1024);
|
|
|
|
QString header = textStream.readLine(1024);
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (!header.startsWith(QLatin1String("[Adblock")) || m_title.isEmpty()) {
|
2012-06-24 23:46:32 +02:00
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "invalid format of adblock file" << m_filePath;
|
|
|
|
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
m_rules.clear();
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
while (!textStream.atEnd()) {
|
2013-02-26 10:42:48 +01:00
|
|
|
AdBlockRule* rule = new AdBlockRule(textStream.readLine(), this);
|
2012-07-01 20:11:37 +02:00
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
if (disabledRules.contains(rule->filter())) {
|
|
|
|
rule->setEnabled(false);
|
2012-07-01 20:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_rules.append(rule);
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
populateCache();
|
2011-12-29 21:25:49 +01:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
// Initial update
|
2012-07-03 21:54:04 +02:00
|
|
|
if (m_rules.isEmpty() && !m_updated) {
|
2012-06-24 23:46:32 +02:00
|
|
|
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockSubscription::saveSubscription()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockSubscription::updateSubscription()
|
|
|
|
{
|
|
|
|
if (m_reply || !m_url.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-03 21:54:04 +02:00
|
|
|
m_reply = new FollowRedirectReply(m_url, mApp->networkManager());
|
2012-06-24 23:46:32 +02:00
|
|
|
|
|
|
|
connect(m_reply, SIGNAL(finished()), this, SLOT(subscriptionDownloaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::subscriptionDownloaded()
|
|
|
|
{
|
2012-07-03 21:54:04 +02:00
|
|
|
if (m_reply != qobject_cast<FollowRedirectReply*>(sender())) {
|
2012-06-24 23:46:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-12 15:14:02 +02:00
|
|
|
QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();
|
2012-06-24 23:46:32 +02:00
|
|
|
|
2012-07-03 21:54:04 +02:00
|
|
|
if (m_reply->error() == QNetworkReply::NoError && response.startsWith("[Adblock")) {
|
2012-06-24 23:46:32 +02:00
|
|
|
// Prepend subscription info
|
|
|
|
response.prepend(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8());
|
|
|
|
|
|
|
|
saveDownloadedData(response);
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
loadSubscription(AdBlockManager::instance()->disabledRules());
|
2012-06-24 23:46:32 +02:00
|
|
|
emit subscriptionUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_reply->deleteLater();
|
|
|
|
m_reply = 0;
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:51:34 +01:00
|
|
|
void AdBlockSubscription::saveDownloadedData(const QByteArray &data)
|
2012-06-24 23:46:32 +02:00
|
|
|
{
|
|
|
|
QFile file(m_filePath);
|
|
|
|
|
|
|
|
if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
|
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-13 23:00:22 +02:00
|
|
|
if (m_url == QUrl("https://easylist-downloads.adblockplus.org/easylist.txt")) {
|
|
|
|
// Third-party advertisers rules are with start domain (||) placeholder which needs regexps
|
|
|
|
// So we are ignoring it for keeping good performance
|
|
|
|
// But we will use whitelist rules at the end of list
|
|
|
|
|
|
|
|
QByteArray part1 = data.left(data.indexOf(QLatin1String("!-----------------------------Third-party adverts-----------------------------!")));
|
|
|
|
QByteArray part2 = data.mid(data.indexOf(QLatin1String("!---------------------------------Whitelists----------------------------------!")));
|
|
|
|
|
|
|
|
file.write(part1 + part2);
|
|
|
|
file.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
file.write(data);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2012-06-28 01:41:01 +02:00
|
|
|
const AdBlockRule* AdBlockSubscription::match(const QNetworkRequest &request, const QString &urlDomain, const QString &urlString) const
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-07-06 16:52:27 +02:00
|
|
|
int count = m_networkExceptionRules.count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2012-07-06 20:22:08 +02:00
|
|
|
const AdBlockRule* rule = m_networkExceptionRules.at(i);
|
2012-06-28 01:41:01 +02:00
|
|
|
if (rule->networkMatch(request, urlDomain, urlString)) {
|
|
|
|
return 0;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 16:52:27 +02:00
|
|
|
count = m_networkBlockRules.count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2012-07-06 20:22:08 +02:00
|
|
|
const AdBlockRule* rule = m_networkBlockRules.at(i);
|
2012-06-28 01:41:01 +02:00
|
|
|
if (rule->networkMatch(request, urlDomain, 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
|
|
|
}
|
2012-06-28 01:41:01 +02:00
|
|
|
|
2011-03-27 21:59:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:00:53 +02:00
|
|
|
bool AdBlockSubscription::adBlockDisabledForUrl(const QUrl &url) const
|
|
|
|
{
|
2012-07-06 16:52:27 +02:00
|
|
|
int count = m_documentRules.count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2012-07-06 20:22:08 +02:00
|
|
|
const AdBlockRule* rule = m_documentRules.at(i);
|
2012-07-04 16:00:53 +02:00
|
|
|
if (rule->urlMatch(url)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdBlockSubscription::elemHideDisabledForUrl(const QUrl &url) const
|
|
|
|
{
|
|
|
|
if (adBlockDisabledForUrl(url)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-06 16:52:27 +02:00
|
|
|
int count = m_elemhideRules.count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2012-07-06 20:22:08 +02:00
|
|
|
const AdBlockRule* rule = m_elemhideRules.at(i);
|
2012-07-04 16:00:53 +02:00
|
|
|
if (rule->urlMatch(url)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
QString AdBlockSubscription::elementHidingRules() const
|
|
|
|
{
|
|
|
|
return m_elementHidingRules;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AdBlockSubscription::elementHidingRulesForDomain(const QString &domain) const
|
|
|
|
{
|
|
|
|
QString rules;
|
|
|
|
|
2013-05-02 21:01:17 +02:00
|
|
|
int addedRulesCount = 0;
|
2012-07-06 16:52:27 +02:00
|
|
|
int count = m_domainRestrictedCssRules.count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2012-07-06 20:22:08 +02:00
|
|
|
const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
|
2013-05-02 21:01:17 +02:00
|
|
|
if (!rule->matchDomain(domain)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Q_UNLIKELY(addedRulesCount == 1000)) {
|
|
|
|
rules.append(rule->cssSelector());
|
|
|
|
rules.append("{display:none !important;}\n");
|
|
|
|
addedRulesCount = 0;
|
|
|
|
}
|
|
|
|
else {
|
2012-09-04 11:24:41 +02:00
|
|
|
rules.append(rule->cssSelector() + QLatin1Char(','));
|
2013-05-02 21:01:17 +02:00
|
|
|
addedRulesCount++;
|
2012-06-25 16:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 21:01:17 +02:00
|
|
|
if (addedRulesCount != 0) {
|
|
|
|
rules = rules.left(rules.size() - 1);
|
|
|
|
rules.append("{display:none !important;}\n");
|
|
|
|
}
|
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
const AdBlockRule* AdBlockSubscription::rule(int offset) const
|
|
|
|
{
|
2013-02-26 10:42:48 +01:00
|
|
|
if (!QzTools::vectorContainsIndex(m_rules, offset)) {
|
2012-07-01 20:11:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
return m_rules[offset];
|
2012-07-01 20:11:37 +02:00
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
QVector<AdBlockRule*> AdBlockSubscription::allRules() const
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
|
|
|
return m_rules;
|
|
|
|
}
|
|
|
|
|
2012-06-25 21:01:30 +02:00
|
|
|
const AdBlockRule* AdBlockSubscription::enableRule(int offset)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2013-02-26 10:42:48 +01:00
|
|
|
if (!QzTools::vectorContainsIndex(m_rules, offset)) {
|
2012-06-25 21:01:30 +02:00
|
|
|
return 0;
|
2012-06-24 23:46:32 +02:00
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
AdBlockRule* rule = m_rules[offset];
|
2012-07-01 20:11:37 +02:00
|
|
|
rule->setEnabled(true);
|
|
|
|
AdBlockManager::instance()->removeDisabledRule(rule->filter());
|
|
|
|
|
2012-07-04 17:53:49 +02:00
|
|
|
if (rule->isCssRule()) {
|
|
|
|
populateCache();
|
|
|
|
mApp->reloadUserStyleSheet();
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
return rule;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-25 21:01:30 +02:00
|
|
|
const AdBlockRule* AdBlockSubscription::disableRule(int offset)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2013-02-26 10:42:48 +01:00
|
|
|
if (!QzTools::vectorContainsIndex(m_rules, offset)) {
|
2012-06-25 21:01:30 +02:00
|
|
|
return 0;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-06-24 23:46:32 +02:00
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
AdBlockRule* rule = m_rules[offset];
|
2012-07-01 20:11:37 +02:00
|
|
|
rule->setEnabled(false);
|
|
|
|
AdBlockManager::instance()->addDisabledRule(rule->filter());
|
|
|
|
|
2012-07-04 17:53:49 +02:00
|
|
|
if (rule->isCssRule()) {
|
|
|
|
populateCache();
|
|
|
|
mApp->reloadUserStyleSheet();
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
return rule;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
bool AdBlockSubscription::canEditRules() const
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdBlockSubscription::canBeRemoved() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
int AdBlockSubscription::addRule(AdBlockRule* rule)
|
2012-06-24 23:46:32 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(rule)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdBlockSubscription::removeRule(int offset)
|
|
|
|
{
|
|
|
|
Q_UNUSED(offset)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
const AdBlockRule* AdBlockSubscription::replaceRule(AdBlockRule* rule, int offset)
|
2012-06-24 23:46:32 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(rule)
|
|
|
|
Q_UNUSED(offset)
|
2012-06-25 21:01:30 +02:00
|
|
|
return 0;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSubscription::populateCache()
|
|
|
|
{
|
|
|
|
m_networkExceptionRules.clear();
|
|
|
|
m_networkBlockRules.clear();
|
2012-06-25 16:07:25 +02:00
|
|
|
m_domainRestrictedCssRules.clear();
|
2012-06-24 23:46:32 +02:00
|
|
|
m_elementHidingRules.clear();
|
2012-07-04 16:00:53 +02:00
|
|
|
m_documentRules.clear();
|
|
|
|
m_elemhideRules.clear();
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2013-05-02 21:01:17 +02:00
|
|
|
// Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes.
|
|
|
|
// (In my testings, 4931 is the number that makes it crash)
|
|
|
|
// So let's split it by 1000 selectors...
|
|
|
|
int hidingRulesCount = 0;
|
|
|
|
|
2012-07-04 16:00:53 +02:00
|
|
|
int count = m_rules.count();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2013-02-26 10:42:48 +01: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
|
|
|
|
2012-06-25 16:07:25 +02:00
|
|
|
if (rule->isCssRule()) {
|
|
|
|
if (rule->isDomainRestricted()) {
|
|
|
|
m_domainRestrictedCssRules.append(rule);
|
|
|
|
}
|
2013-05-02 21:01:17 +02:00
|
|
|
else if (Q_UNLIKELY(hidingRulesCount == 1000)) {
|
|
|
|
m_elementHidingRules.append(rule->cssSelector());
|
|
|
|
m_elementHidingRules.append("{display:none !important;} ");
|
|
|
|
hidingRulesCount = 0;
|
|
|
|
}
|
2012-06-25 16:07:25 +02:00
|
|
|
else {
|
2013-05-02 21:01:17 +02:00
|
|
|
m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(','));
|
|
|
|
hidingRulesCount++;
|
2012-06-25 16:07:25 +02:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
2012-07-04 16:00:53 +02:00
|
|
|
else if (rule->isDocument()) {
|
|
|
|
m_documentRules.append(rule);
|
|
|
|
}
|
|
|
|
else if (rule->isElemhide()) {
|
|
|
|
m_elemhideRules.append(rule);
|
|
|
|
}
|
|
|
|
else if (rule->isException()) {
|
2011-03-27 21:59:40 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 21:01:17 +02:00
|
|
|
|
|
|
|
if (hidingRulesCount != 0) {
|
|
|
|
m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1);
|
|
|
|
m_elementHidingRules.append("{display:none !important;} ");
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2013-02-27 19:51:34 +01:00
|
|
|
AdBlockSubscription::~AdBlockSubscription()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_rules);
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
// AdBlockCustomList
|
|
|
|
|
|
|
|
AdBlockCustomList::AdBlockCustomList(QObject* parent)
|
|
|
|
: AdBlockSubscription(tr("Custom Rules"), parent)
|
|
|
|
{
|
|
|
|
setFilePath(mApp->currentProfilePath() + "adblock/customlist.txt");
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
void AdBlockCustomList::saveSubscription()
|
|
|
|
{
|
|
|
|
QFile file(filePath());
|
|
|
|
|
|
|
|
if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
|
|
|
|
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << filePath();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream textStream(&file);
|
2013-02-20 19:50:59 +01:00
|
|
|
textStream.setCodec("UTF-8");
|
2012-07-01 20:11:37 +02:00
|
|
|
textStream << "Title: " << title() << endl;
|
|
|
|
textStream << "Url: " << url().toString() << endl;
|
|
|
|
textStream << "[Adblock Plus 1.1.1]" << endl;
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const AdBlockRule* rule, m_rules) {
|
2013-02-26 10:42:48 +01:00
|
|
|
textStream << rule->filter() << endl;
|
2012-07-01 20:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
bool AdBlockCustomList::canEditRules() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdBlockCustomList::canBeRemoved() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-09 19:02:25 +02:00
|
|
|
bool AdBlockCustomList::containsFilter(const QString &filter) const
|
|
|
|
{
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const AdBlockRule* rule, m_rules) {
|
2013-02-26 10:42:48 +01:00
|
|
|
if (rule->filter() == filter) {
|
2012-08-09 19:02:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdBlockCustomList::removeFilter(const QString &filter)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_rules.count(); ++i) {
|
2013-02-26 10:42:48 +01:00
|
|
|
const AdBlockRule* rule = m_rules.at(i);
|
2012-08-09 19:02:25 +02:00
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
if (rule->filter() == filter) {
|
2012-08-09 19:02:25 +02:00
|
|
|
return removeRule(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
int AdBlockCustomList::addRule(AdBlockRule* rule)
|
2012-06-24 23:46:32 +02:00
|
|
|
{
|
|
|
|
m_rules.append(rule);
|
|
|
|
populateCache();
|
|
|
|
|
2012-07-04 17:53:49 +02:00
|
|
|
emit subscriptionEdited();
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
return m_rules.count() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdBlockCustomList::removeRule(int offset)
|
|
|
|
{
|
2013-02-26 10:42:48 +01:00
|
|
|
if (!QzTools::vectorContainsIndex(m_rules, offset)) {
|
2012-06-24 23:46:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
AdBlockRule* rule = m_rules.at(offset);
|
|
|
|
const QString &filter = rule->filter();
|
2012-07-01 20:11:37 +02:00
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
m_rules.remove(offset);
|
2012-06-24 23:46:32 +02:00
|
|
|
populateCache();
|
|
|
|
|
2012-07-04 17:53:49 +02:00
|
|
|
emit subscriptionEdited();
|
|
|
|
|
2012-07-01 20:11:37 +02:00
|
|
|
AdBlockManager::instance()->removeDisabledRule(filter);
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
delete rule;
|
2012-06-24 23:46:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
const AdBlockRule* AdBlockCustomList::replaceRule(AdBlockRule* rule, int offset)
|
2012-06-24 23:46:32 +02:00
|
|
|
{
|
2013-02-26 10:42:48 +01:00
|
|
|
if (!QzTools::vectorContainsIndex(m_rules, offset)) {
|
2012-06-25 21:01:30 +02:00
|
|
|
return 0;
|
2012-06-24 23:46:32 +02:00
|
|
|
}
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
delete m_rules.at(offset);
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
m_rules[offset] = rule;
|
|
|
|
populateCache();
|
|
|
|
|
2012-07-04 17:53:49 +02:00
|
|
|
emit subscriptionEdited();
|
|
|
|
|
2013-02-26 10:42:48 +01:00
|
|
|
return m_rules[offset];
|
|
|
|
}
|