Refactor da shit.

This commit is contained in:
Martin Rotter 2015-06-17 14:28:18 +02:00
parent 57428510d4
commit 5dbdc02172
10 changed files with 269 additions and 319 deletions

View File

@ -138,6 +138,30 @@
#define APP_NO_THEME "-" #define APP_NO_THEME "-"
#define APP_THEME_SUFFIX ".png" #define APP_THEME_SUFFIX ".png"
#ifndef QSL
#if QT_VERSION >= 0x050000
// Thin macro wrapper for literal strings.
// They are much more memory efficient and faster.
// Use it for all literals except for two cases:
// a) Methods which take QLatin1String (use QLatin1String for literal argument too),
// b) Construction of empty literals "", use QString() instead of QStringLiteral("").
#define QSL(x) QStringLiteral(x)
#else
#define QSL(x) QLatin1String(x)
#endif
#endif
#ifndef QL1S
// Macro for latin strings. Latin strings are
// faster than QStrings created from literals.
#define QL1S(x) QLatin1String(x)
#endif
#ifndef QL1C
// Macro for latin chars.
#define QL1C(x) QLatin1Char(x)
#endif
// Indexes of columns as they are DEFINED IN THE TABLE for MESSAGES. // Indexes of columns as they are DEFINED IN THE TABLE for MESSAGES.
#define MSG_DB_ID_INDEX 0 #define MSG_DB_ID_INDEX 0
#define MSG_DB_READ_INDEX 1 #define MSG_DB_READ_INDEX 1

View File

@ -17,7 +17,8 @@
#include "miscellaneous/iofactory.h" #include "miscellaneous/iofactory.h"
#include <exceptions/ioexception.h> #include "definitions/definitions.h"
#include "exceptions/ioexception.h"
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
@ -36,6 +37,46 @@ QString IOFactory::getSystemFolder(SYSTEM_FOLDER_ENUM::StandardLocation location
#endif #endif
} }
QString IOFactory::ensureUniqueFilename(const QString &name, const QString &append_format) {
if (!QFile::exists(name)) {
return name;
}
QString tmp_filename = name;
int i = 1;
while (QFile::exists(tmp_filename)) {
tmp_filename = name;
int index = tmp_filename.lastIndexOf(QL1C('.'));
QString append_string = append_format.arg(i++);
if (index < 0) {
tmp_filename.append(append_string);
}
else {
tmp_filename = tmp_filename.left(index) + append_string + tmp_filename.mid(index);
}
}
return tmp_filename;
}
QString IOFactory::filterBadCharsFromFilename(const QString &name) {
QString value = name;
value.replace(QL1C('/'), QL1C('-'));
value.remove(QL1C('\\'));
value.remove(QL1C(':'));
value.remove(QL1C('*'));
value.remove(QL1C('?'));
value.remove(QL1C('"'));
value.remove(QL1C('<'));
value.remove(QL1C('>'));
value.remove(QL1C('|'));
return value;
}
QByteArray IOFactory::readTextFile(const QString &file_path) { QByteArray IOFactory::readTextFile(const QString &file_path) {
QFile input_file(file_path); QFile input_file(file_path);
QByteArray input_data; QByteArray input_data;

View File

@ -20,6 +20,8 @@
#include <QCoreApplication> #include <QCoreApplication>
#include "definitions/definitions.h"
#if QT_VERSION >= 0x050000 #if QT_VERSION >= 0x050000
#include <QStandardPaths> #include <QStandardPaths>
#define SYSTEM_FOLDER_ENUM QStandardPaths #define SYSTEM_FOLDER_ENUM QStandardPaths
@ -39,6 +41,13 @@ class IOFactory {
// Returns system-wide folder according to type. // Returns system-wide folder according to type.
static QString getSystemFolder(SYSTEM_FOLDER_ENUM::StandardLocation location); static QString getSystemFolder(SYSTEM_FOLDER_ENUM::StandardLocation location);
// Checks given file if it exists and if it does, then generates non-existing new file
// according to format.
static QString ensureUniqueFilename(const QString &name, const QString &append_format = QSL("(%1)"));
// Filters out shit characters from filename.
static QString filterBadCharsFromFilename(const QString &name);
// Returns contents of a file. // Returns contents of a file.
// Throws exception when no such file exists. // Throws exception when no such file exists.
static QByteArray readTextFile(const QString &file_path); static QByteArray readTextFile(const QString &file_path);

View File

@ -217,6 +217,21 @@ DVALUE(char*) Database::ActiveDriverDef = APP_DB_SQLITE_DRIVER;
// Keyboard. // Keyboard.
DKEY Keyboard::ID = "keyboard"; DKEY Keyboard::ID = "keyboard";
// Adblock.
DKEY AdBlock::ID = "adblock";
DKEY AdBlock::Enabled = "enabled";
DVALUE(bool) AdBlock::EnabledDef = false;
DKEY AdBlock::UseLimitedEasyList = "use_limited_easylist";
DVALUE(bool) AdBlock::UseLimitedEasyListDef = true;
DKEY AdBlock::DisabledRules = "disabled_rules";
DVALUE(QStringList) AdBlock::DisabledRulesDef = QStringList();
DKEY AdBlock::LastUpdated = "last_updated";
DVALUE(QDateTime) AdBlock::LastUpdatedDef = QDateTime();
// Web browser. // Web browser.
DKEY Browser::ID = "browser"; DKEY Browser::ID = "browser";

View File

@ -25,6 +25,7 @@
#include "miscellaneous/settingsproperties.h" #include "miscellaneous/settingsproperties.h"
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QStringList>
#define KEY extern const char* #define KEY extern const char*
#define DKEY const char* #define DKEY const char*
@ -239,6 +240,23 @@ namespace Keyboard {
KEY ID; KEY ID;
} }
// Adblock.
namespace AdBlock {
KEY ID;
KEY Enabled;
VALUE(bool) EnabledDef;
KEY UseLimitedEasyList;
VALUE(bool) UseLimitedEasyListDef;
KEY DisabledRules;
VALUE(QStringList) DisabledRulesDef;
KEY LastUpdated;
VALUE(QDateTime) LastUpdatedDef;
}
// Web browser. // Web browser.
namespace Browser { namespace Browser {
KEY ID; KEY ID;

View File

@ -1,48 +1,32 @@
/* ============================================================ // This file is part of RSS Guard.
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader //
* Copyright (C) 2011-2015 QuiteRSS Team <quiterssteam@gmail.com> // Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
* // Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
* 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 // RSS Guard is free software: you can redistribute it and/or modify
* the Free Software Foundation, either version 3 of the License, or // it under the terms of the GNU General Public License as published by
* (at your option) any later version. // 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 // RSS Guard is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. // 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/>. // You should have received a copy of the GNU General Public License
* ============================================================ */ // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
*
* 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 "adblockmanager.h"
#include "adblockdialog.h"
#include "adblockmatcher.h"
#include "adblocksubscription.h"
#include "adblockblockednetworkreply.h"
#include "adblockicon.h"
#include "webpage.h"
#include "network-web/adblock/adblockmanager.h"
#include "network-web/adblock/adblockdialog.h"
#include "network-web/adblock/adblockmatcher.h"
#include "network-web/adblock/adblocksubscription.h"
#include "network-web/adblock/adblockblockednetworkreply.h"
#include "network-web/adblock/adblockicon.h"
#include "network-web/webpage.h"
#include "network-web/silentnetworkaccessmanager.h"
#include "miscellaneous/application.h" #include "miscellaneous/application.h"
#include "miscellaneous/settings.h" #include "miscellaneous/settings.h"
#include "network-web/silentnetworkaccessmanager.h"
#include "gui/formmain.h" #include "gui/formmain.h"
#include <QDateTime> #include <QDateTime>
@ -50,13 +34,7 @@
#include <QDir> #include <QDir>
#include <QTimer> #include <QTimer>
#include <QWebFrame> #include <QWebFrame>
#include <QDebug>
#define ADBLOCK_DEBUG
#ifdef ADBLOCK_DEBUG
#include <QElapsedTimer>
#endif
AdBlockManager* AdBlockManager::s_adBlockManager = NULL; AdBlockManager* AdBlockManager::s_adBlockManager = NULL;
@ -78,262 +56,183 @@ AdBlockManager* AdBlockManager::instance() {
return s_adBlockManager; return s_adBlockManager;
} }
QString AdBlockManager::filterCharsFromFilename(const QString &name) void AdBlockManager::setEnabled(bool enabled) {
{
QString value = name;
value.replace(QLatin1Char('/'), QLatin1Char('-'));
value.remove(QLatin1Char('\\'));
value.remove(QLatin1Char(':'));
value.remove(QLatin1Char('*'));
value.remove(QLatin1Char('?'));
value.remove(QLatin1Char('"'));
value.remove(QLatin1Char('<'));
value.remove(QLatin1Char('>'));
value.remove(QLatin1Char('|'));
return value;
}
QString AdBlockManager::ensureUniqueFilename(const QString &name, const QString &appendFormat)
{
if (!QFile::exists(name)) {
return name;
}
QString tmpFileName = name;
int i = 1;
while (QFile::exists(tmpFileName)) {
tmpFileName = name;
int index = tmpFileName.lastIndexOf(QLatin1Char('.'));
QString appendString = appendFormat.arg(i);
if (index == -1) {
tmpFileName.append(appendString);
}
else {
tmpFileName = tmpFileName.left(index) + appendString + tmpFileName.mid(index);
}
i++;
}
return tmpFileName;
}
void AdBlockManager::setEnabled(bool enabled)
{
if (m_enabled == enabled) { if (m_enabled == enabled) {
return; return;
} }
m_enabled = enabled; m_enabled = enabled;
emit enabledChanged(enabled); emit enabledChanged(enabled);
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::Enabled, m_enabled);
Settings *settings = qApp->settings();
settings->beginGroup("AdBlock");
settings->setValue("enabled", m_enabled);
settings->endGroup();
// Load subscriptions and other data. // Load subscriptions and other data.
load(); load();
// Inform others (mainly ICON and MATCHER), that there are some changes.
// TODO // TODO
//mainApp->reloadUserStyleBrowser(); //mainApp->reloadUserStyleBrowser();
} }
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const QList<AdBlockSubscription*> AdBlockManager::subscriptions() const {
{
return m_subscriptions; return m_subscriptions;
} }
QNetworkReply* AdBlockManager::block(const QNetworkRequest &request) QNetworkReply *AdBlockManager::block(const QNetworkRequest &request) {
{ const QString url_string = request.url().toEncoded().toLower();
#ifdef ADBLOCK_DEBUG const QString url_domain = request.url().host().toLower();
QElapsedTimer timer; const QString url_scheme = request.url().scheme().toLower();
timer.start();
#endif
const QString urlString = request.url().toEncoded().toLower();
const QString urlDomain = request.url().host().toLower();
const QString urlScheme = request.url().scheme().toLower();
if (!isEnabled() || !canRunOnScheme(urlScheme)) if (!isEnabled() || !canRunOnScheme(url_scheme)) {
return 0; return NULL;
}
const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString); const AdBlockRule* blocked_rule = m_matcher->match(request, url_domain, url_string);
if (blockedRule) { if (blocked_rule != NULL) {
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100)); QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
WebPage* webPage = static_cast<WebPage*>(v.value<void*>()); WebPage* webPage = static_cast<WebPage*>(v.value<void*>());
if (WebPage::isPointerSafeToUse(webPage)) { if (WebPage::isPointerSafeToUse(webPage)) {
if (!canBeBlocked(webPage->mainFrame()->url())) { if (!canBeBlocked(webPage->mainFrame()->url())) {
return 0; return NULL;
} }
webPage->addAdBlockRule(blockedRule, request.url()); webPage->addAdBlockRule(blocked_rule, request.url());
} }
AdBlockBlockedNetworkReply* reply = new AdBlockBlockedNetworkReply(blockedRule, this); AdBlockBlockedNetworkReply* reply = new AdBlockBlockedNetworkReply(blocked_rule, this);
reply->setRequest(request); reply->setRequest(request);
#ifdef ADBLOCK_DEBUG
qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.url();
#endif
return reply; return reply;
} }
#ifdef ADBLOCK_DEBUG return NULL;
qDebug() << timer.elapsed() << request.url();
#endif
return 0;
} }
QStringList AdBlockManager::disabledRules() const QStringList AdBlockManager::disabledRules() const {
{
return m_disabledRules; return m_disabledRules;
} }
void AdBlockManager::addDisabledRule(const QString &filter) void AdBlockManager::addDisabledRule(const QString &filter) {
{
m_disabledRules.append(filter); m_disabledRules.append(filter);
} }
void AdBlockManager::removeDisabledRule(const QString &filter) void AdBlockManager::removeDisabledRule(const QString &filter) {
{
m_disabledRules.removeOne(filter); m_disabledRules.removeOne(filter);
} }
AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) {
{
if (title.isEmpty() || url.isEmpty()) { if (title.isEmpty() || url.isEmpty()) {
return NULL; return NULL;
} }
QString fileName = filterCharsFromFilename(title.toLower()) + ".txt"; QString file_name = IOFactory::filterBadCharsFromFilename(title.toLower()) + QL1S(".txt");
QString filePath = ensureUniqueFilename(qApp->homeFolderPath() + "/adblock/" + fileName); QString file_path = IOFactory::ensureUniqueFilename(qApp->homeFolderPath() + QL1S("/adblock/") + file_name);
QFile file(file_path);
QByteArray data = QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1(); if (!file.open(QFile::WriteOnly | QFile::Truncate | QFile::Unbuffered)) {
qWarning("Cannot write to file '%s'.",qPrintable(file_path));
QFile file(filePath); return NULL;
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
qWarning() << "AdBlockManager: Cannot write to file" << filePath;
return 0;
} }
file.write(data); file.write(QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1());
file.close(); file.close();
AdBlockSubscription *subscription = new AdBlockSubscription(title, this); AdBlockSubscription *subscription = new AdBlockSubscription(title, this);
subscription->setUrl(QUrl(url)); subscription->setUrl(QUrl(url));
subscription->setFilePath(filePath); subscription->setFilePath(file_path);
subscription->loadSubscription(m_disabledRules); subscription->loadSubscription(m_disabledRules);
// This expects that there is at least "Custom rules" subscription available in
// active subscriptions.
m_subscriptions.insert(m_subscriptions.count() - 1, subscription); m_subscriptions.insert(m_subscriptions.count() - 1, subscription);
return subscription; return subscription;
} }
bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) bool AdBlockManager::removeSubscription(AdBlockSubscription *subscription) {
{
if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) { if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) {
return false; return false;
} }
else {
QFile(subscription->filePath()).remove(); QFile::remove(subscription->filePath());
m_subscriptions.removeOne(subscription); m_subscriptions.removeOne(subscription);
delete subscription; delete subscription;
return true; return true;
} }
}
AdBlockCustomList* AdBlockManager::customList() const AdBlockCustomList *AdBlockManager::customList() const {
{
foreach (AdBlockSubscription *subscription, m_subscriptions) { foreach (AdBlockSubscription *subscription, m_subscriptions) {
AdBlockCustomList *list = qobject_cast<AdBlockCustomList*>(subscription); AdBlockCustomList *list = qobject_cast<AdBlockCustomList*>(subscription);
if (list) { if (list != NULL) {
return list; return list;
} }
} }
return 0; return NULL;
} }
void AdBlockManager::load() void AdBlockManager::load() {
{
if (m_loaded) { if (m_loaded) {
// It is already loaded: subscriptions are loaded from files into objects,
// enabled status is determined, disabled rules are also determined.
return; return;
} }
#ifdef ADBLOCK_DEBUG
QElapsedTimer timer;
timer.start();
#endif
Settings *settings = qApp->settings(); Settings *settings = qApp->settings();
m_enabled = settings->value("AdBlock","enabled", m_enabled).toBool(); m_enabled = settings->value(GROUP(AdBlock), SETTING(AdBlock::Enabled)).toBool();
m_useLimitedEasyList = settings->value("AdBlock","useLimitedEasyList", m_useLimitedEasyList).toBool(); m_useLimitedEasyList = settings->value(GROUP(AdBlock), SETTING(AdBlock::UseLimitedEasyList)).toBool();
m_disabledRules = settings->value("AdBlock","disabledRules", QStringList()).toStringList(); m_disabledRules = settings->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList();
QDateTime lastUpdate = settings->value("AdBlock","lastUpdate", QDateTime()).toDateTime(); QDateTime last_update = settings->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdated)).toDateTime();
if (!m_enabled) { if (!m_enabled) {
// We loaded settings, but Adblock should be disabled. Do not continue to save memory. // We loaded settings, but Adblock should be disabled. Do not continue to save memory.
return; return;
} }
QDir adblockDir(qApp->homeFolderPath() + "/adblock"); QDir adblock_dir(qApp->homeFolderPath() + QL1S("/adblock"));
// Create if neccessary
if (!adblockDir.exists()) { if (!adblock_dir.exists()) {
QDir(qApp->homeFolderPath()).mkdir("adblock"); QDir(qApp->homeFolderPath()).mkdir(QSL("adblock"));
} }
foreach (const QString &fileName, adblockDir.entryList(QStringList("*.txt"), QDir::Files)) { foreach (const QString &file_name, adblock_dir.entryList(QStringList(QL1S("*.txt")), QDir::Files)) {
if (fileName == QLatin1String("customlist.txt")) { if (file_name == QSL("customlist.txt")) {
continue; continue;
} }
const QString absolutePath = adblockDir.absoluteFilePath(fileName); const QString absolute_path = adblock_dir.absoluteFilePath(file_name);
QFile file(absolutePath); QFile file(absolute_path);
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::ReadOnly)) {
continue; continue;
} }
QTextStream textStream(&file); QTextStream stream(&file);
textStream.setCodec("UTF-8"); stream.setCodec("UTF-8");
QString title = textStream.readLine(1024).remove(QLatin1String("Title: ")); QString title = stream.readLine(1024).remove(QSL("Title: "));
QUrl url = QUrl(textStream.readLine(1024).remove(QLatin1String("Url: "))); QUrl url = QUrl(stream.readLine(1024).remove(QSL("Url: ")));
// Close the file.
file.close();
if (title.isEmpty() || !url.isValid()) { if (title.isEmpty() || !url.isValid()) {
qWarning() << "AdBlockManager: Invalid subscription file" << absolutePath; qWarning("Invalid subscription file '%s'.", qPrintable(absolute_path));
continue; continue;
} }
AdBlockSubscription *subscription = new AdBlockSubscription(title, this); AdBlockSubscription *subscription = new AdBlockSubscription(title, this);
subscription->setUrl(url); subscription->setUrl(url);
subscription->setFilePath(absolutePath); subscription->setFilePath(absolute_path);
m_subscriptions.append(subscription); m_subscriptions.append(subscription);
} }
// Prepend EasyList if subscriptions are empty // Append list for "Custom rules".
if (m_subscriptions.isEmpty()) { m_subscriptions.append(new AdBlockCustomList(this));
AdBlockSubscription* easyList = new AdBlockSubscription(tr("EasyList"), this);
easyList->setUrl(QUrl(ADBLOCK_EASYLIST_URL));
easyList->setFilePath(qApp->homeFolderPath() + "/adblock/easylist.txt");
// TODO // Load all subscriptions, including obligatory "Custom rules" list.
//connect(easyList, SIGNAL(subscriptionUpdated()), mainApp, SLOT(reloadUserStyleBrowser()));
m_subscriptions.prepend(easyList);
}
// Append CustomList
AdBlockCustomList* customList = new AdBlockCustomList(this);
m_subscriptions.append(customList);
// Load all subscriptions
foreach (AdBlockSubscription* subscription, m_subscriptions) { foreach (AdBlockSubscription* subscription, m_subscriptions) {
subscription->loadSubscription(m_disabledRules); subscription->loadSubscription(m_disabledRules);
@ -342,33 +241,23 @@ void AdBlockManager::load()
connect(subscription, SIGNAL(subscriptionChanged()), m_matcher, SLOT(update())); connect(subscription, SIGNAL(subscriptionChanged()), m_matcher, SLOT(update()));
} }
if (lastUpdate.addDays(5) < QDateTime::currentDateTime()) { // Notify matcher about loaded subscriptions
QTimer::singleShot(1000 * 60, this, SLOT(updateAllSubscriptions())); // and mark all this shit as loaded.
}
#ifdef ADBLOCK_DEBUG
qDebug() << "AdBlock loaded in" << timer.elapsed();
#endif
m_matcher->update(); m_matcher->update();
m_loaded = true; m_loaded = true;
} }
void AdBlockManager::updateAllSubscriptions() void AdBlockManager::updateAllSubscriptions() {
{
foreach (AdBlockSubscription *subscription, m_subscriptions) { foreach (AdBlockSubscription *subscription, m_subscriptions) {
subscription->updateSubscription(); subscription->updateSubscription();
} }
Settings *settings = qApp->settings(); qApp->settings()->setValue(GROUP(AdBlock), AdBlock::LastUpdated, QDateTime::currentDateTime());
settings->beginGroup("AdBlock");
settings->setValue("lastUpdate", QDateTime::currentDateTime());
settings->endGroup();
} }
void AdBlockManager::save() void AdBlockManager::save() {
{
if (!m_loaded) { if (!m_loaded) {
// There is nothing to save, because these is nothing loaded.
return; return;
} }
@ -377,72 +266,63 @@ void AdBlockManager::save()
} }
Settings *settings = qApp->settings(); Settings *settings = qApp->settings();
settings->beginGroup("AdBlock"); settings->setValue(GROUP(AdBlock), AdBlock::Enabled, m_enabled);
settings->setValue("enabled", m_enabled); settings->setValue(GROUP(AdBlock), AdBlock::UseLimitedEasyList, m_useLimitedEasyList);
settings->setValue("useLimitedEasyList", m_useLimitedEasyList); settings->setValue(GROUP(AdBlock), AdBlock::DisabledRules, m_disabledRules);
settings->setValue("disabledRules", m_disabledRules);
settings->endGroup();
} }
bool AdBlockManager::isEnabled() const bool AdBlockManager::isEnabled() const {
{
return m_enabled; return m_enabled;
} }
bool AdBlockManager::canRunOnScheme(const QString &scheme) const bool AdBlockManager::canRunOnScheme(const QString &scheme) const {
{ return !(scheme == QL1S("file") || scheme == QL1S("qrc") || scheme == QL1S("data") || scheme == QL1S("abp"));
return !(scheme == QLatin1String("file") || scheme == QLatin1String("qrc")
|| scheme == QLatin1String("qupzilla") || scheme == QLatin1String("data")
|| scheme == QLatin1String("abp"));
} }
bool AdBlockManager::useLimitedEasyList() const bool AdBlockManager::useLimitedEasyList() const {
{
return m_useLimitedEasyList; return m_useLimitedEasyList;
} }
void AdBlockManager::setUseLimitedEasyList(bool useLimited) void AdBlockManager::setUseLimitedEasyList(bool use_limited) {
{ m_useLimitedEasyList = use_limited;
m_useLimitedEasyList = useLimited;
foreach (AdBlockSubscription *subscription, m_subscriptions) { foreach (AdBlockSubscription *subscription, m_subscriptions) {
if (subscription->url() == QUrl(ADBLOCK_EASYLIST_URL)) { if (subscription->url() == QUrl(ADBLOCK_EASYLIST_URL)) {
// User really has EasyList activated, update it.
subscription->updateSubscription(); subscription->updateSubscription();
} }
} }
} }
bool AdBlockManager::canBeBlocked(const QUrl &url) const bool AdBlockManager::canBeBlocked(const QUrl &url) const {
{
return !m_matcher->adBlockDisabledForUrl(url); return !m_matcher->adBlockDisabledForUrl(url);
} }
QString AdBlockManager::elementHidingRules() const QString AdBlockManager::elementHidingRules() const {
{
return m_matcher->elementHidingRules(); return m_matcher->elementHidingRules();
} }
QString AdBlockManager::elementHidingRulesForDomain(const QUrl &url) const QString AdBlockManager::elementHidingRulesForDomain(const QUrl &url) const {
{ if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url)) {
if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url))
return QString(); return QString();
}
// Acid3 doesn't like the way element hiding rules are embedded into page // Acid3 doesn't like the way element hiding rules are embedded into page
if (url.host() == QLatin1String("acid3.acidtests.org")) else if (url.host() == QL1S("acid3.acidtests.org")) {
return QString(); return QString();
}
else {
return m_matcher->elementHidingRulesForDomain(url.host()); return m_matcher->elementHidingRulesForDomain(url.host());
} }
}
AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) const AdBlockSubscription *AdBlockManager::subscriptionByName(const QString &name) const {
{
foreach (AdBlockSubscription *subscription, m_subscriptions) { foreach (AdBlockSubscription *subscription, m_subscriptions) {
if (subscription->title() == name) { if (subscription->title() == name) {
return subscription; return subscription;
} }
} }
return 0; return NULL;
} }
AdBlockDialog *AdBlockManager::showDialog() { AdBlockDialog *AdBlockManager::showDialog() {
@ -450,16 +330,14 @@ AdBlockDialog *AdBlockManager::showDialog() {
form_pointer.data()->show(); form_pointer.data()->show();
form_pointer.data()->raise(); form_pointer.data()->raise();
form_pointer.data()->activateWindow(); form_pointer.data()->activateWindow();
form_pointer.data()->setAttribute(Qt::WA_DeleteOnClose, true);
return form_pointer.data(); return form_pointer.data();
} }
void AdBlockManager::showRule() void AdBlockManager::showRule() {
{
if (QAction *action = qobject_cast<QAction*>(sender())) { if (QAction *action = qobject_cast<QAction*>(sender())) {
const AdBlockRule* rule = static_cast<const AdBlockRule*>(action->data().value<void*>()); const AdBlockRule* rule = static_cast<const AdBlockRule*>(action->data().value<void*>());
if (rule) { if (rule != NULL) {
showDialog()->showRule(rule); showDialog()->showRule(rule);
} }
} }

View File

@ -1,57 +1,24 @@
/* ============================================================ // This file is part of RSS Guard.
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader //
* Copyright (C) 2011-2015 QuiteRSS Team <quiterssteam@gmail.com> // Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
* // Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
* 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 // RSS Guard is free software: you can redistribute it and/or modify
* the Free Software Foundation, either version 3 of the License, or // it under the terms of the GNU General Public License as published by
* (at your option) any later version. // 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 // RSS Guard is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. // 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/>. // You should have received a copy of the GNU General Public License
* ============================================================ */ // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
*
* 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 ADBLOCKMANAGER_H #ifndef ADBLOCKMANAGER_H
#define ADBLOCKMANAGER_H #define ADBLOCKMANAGER_H
#ifndef QSL
#if QT_VERSION >= 0x050000
#define QSL(x) QStringLiteral(x)
#else
#define QSL(x) QLatin1String(x)
#endif
#endif
#ifndef QL1S
#define QL1S(x) QLatin1String(x)
#endif
#ifndef QL1C
#define QL1C(x) QLatin1Char(x)
#endif
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <QPointer> #include <QPointer>
@ -76,10 +43,6 @@ class AdBlockManager : public QObject
static AdBlockManager* instance(); static AdBlockManager* instance();
static QString filterCharsFromFilename(const QString &name);
static QString ensureUniqueFilename(const QString &name, const QString &appendFormat = QString("(%1)"));
void load(); void load();
void save(); void save();
@ -87,7 +50,7 @@ class AdBlockManager : public QObject
bool canRunOnScheme(const QString &scheme) const; bool canRunOnScheme(const QString &scheme) const;
bool useLimitedEasyList() const; bool useLimitedEasyList() const;
void setUseLimitedEasyList(bool useLimited); void setUseLimitedEasyList(bool use_limited);
QString elementHidingRules() const; QString elementHidingRules() const;
QString elementHidingRulesForDomain(const QUrl &url) const; QString elementHidingRulesForDomain(const QUrl &url) const;

View File

@ -50,13 +50,13 @@ const AdBlockRule* AdBlockMatcher::match(const QNetworkRequest &request, const Q
{ {
// Exception rules // Exception rules
if (m_networkExceptionTree.find(request, urlDomain, urlString)) if (m_networkExceptionTree.find(request, urlDomain, urlString))
return 0; return NULL;
int count = m_networkExceptionRules.count(); int count = m_networkExceptionRules.count();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_networkExceptionRules.at(i); const AdBlockRule* rule = m_networkExceptionRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString)) if (rule->networkMatch(request, urlDomain, urlString))
return 0; return NULL;
} }
// Block rules // Block rules
@ -70,7 +70,7 @@ const AdBlockRule* AdBlockMatcher::match(const QNetworkRequest &request, const Q
return rule; return rule;
} }
return 0; return NULL;
} }
bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const

View File

@ -65,6 +65,8 @@
#include "adblocksubscription.h" #include "adblocksubscription.h"
#include "adblockmanager.h" #include "adblockmanager.h"
#include "definitions/definitions.h"
#include <QDebug> #include <QDebug>
#include <QUrl> #include <QUrl>
#include <QString> #include <QString>

View File

@ -53,7 +53,7 @@ QNetworkReply *WebBrowserNetworkAccessManager::createRequest(QNetworkAccessManag
if (op == QNetworkAccessManager::GetOperation) { if (op == QNetworkAccessManager::GetOperation) {
QNetworkReply *reply = AdBlockManager::instance()->block(request); QNetworkReply *reply = AdBlockManager::instance()->block(request);
if (reply) { if (reply != NULL) {
return reply; return reply;
} }
} }