diff --git a/src/gui/statusbar.cpp b/src/gui/statusbar.cpp index 7d3c3c244..bd9d581ee 100755 --- a/src/gui/statusbar.cpp +++ b/src/gui/statusbar.cpp @@ -33,7 +33,8 @@ StatusBar::StatusBar(QWidget *parent) : QStatusBar(parent) { setSizeGripEnabled(false); setContentsMargins(0, 0, 0, 0); - m_adblockIcon = new AdBlockIcon(this); + m_adBlockIcon = new AdBlockIcon(this); + m_adBlockIcon->activate(); // Initializations of widgets for status bar. m_fullscreenSwitcher = new PlainToolButton(this); @@ -69,7 +70,7 @@ StatusBar::StatusBar(QWidget *parent) : QStatusBar(parent) { addPermanentWidget(m_barProgressFeeds); addPermanentWidget(m_lblProgressDownload); addPermanentWidget(m_barProgressDownload); - addPermanentWidget(m_adblockIcon); + addPermanentWidget(m_adBlockIcon); addPermanentWidget(m_fullscreenSwitcher); } diff --git a/src/gui/statusbar.h b/src/gui/statusbar.h index dd8bf72f1..cf5103895 100755 --- a/src/gui/statusbar.h +++ b/src/gui/statusbar.h @@ -39,7 +39,7 @@ class StatusBar : public QStatusBar { } inline AdBlockIcon *adBlockIcon() { - return m_adblockIcon; + return m_adBlockIcon; } public slots: @@ -59,7 +59,7 @@ class StatusBar : public QStatusBar { QProgressBar *m_barProgressDownload; QLabel *m_lblProgressDownload; PlainToolButton *m_fullscreenSwitcher; - AdBlockIcon* m_adblockIcon; + AdBlockIcon* m_adBlockIcon; }; #endif // STATUSBAR_H diff --git a/src/miscellaneous/databasefactory.cpp b/src/miscellaneous/databasefactory.cpp index 561611e7f..eaab76d7c 100755 --- a/src/miscellaneous/databasefactory.cpp +++ b/src/miscellaneous/databasefactory.cpp @@ -208,6 +208,8 @@ QSqlDatabase DatabaseFactory::sqliteInitializeInMemoryDatabase() { copy_contents.exec(QString("INSERT INTO main.%1 SELECT * FROM storage.%1;").arg(table)); } + qDebug("Copying data from file-based database into working in-memory database."); + // Detach database and finish. copy_contents.exec("DETACH 'storage'"); copy_contents.finish(); diff --git a/src/network-web/adblock/adblockicon.cpp b/src/network-web/adblock/adblockicon.cpp index 23deb36b7..fa6ac8173 100755 --- a/src/network-web/adblock/adblockicon.cpp +++ b/src/network-web/adblock/adblockicon.cpp @@ -50,7 +50,7 @@ void AdBlockIcon::popupBlocked(const QString &rule_string, const QUrl &url) { return; } - QPair pair; + QPair pair; pair.first = new AdBlockRule(filter, subscription); pair.second = url; m_blockedPopups.append(pair); @@ -85,6 +85,10 @@ QAction *AdBlockIcon::menuAction() { return m_menuAction; } +void AdBlockIcon::activate() { + setEnabled(AdBlockManager::instance()->shouldBeEnabled()); +} + void AdBlockIcon::createMenu(QMenu *menu) { if (menu == NULL) { menu = qobject_cast(sender()); diff --git a/src/network-web/adblock/adblockicon.h b/src/network-web/adblock/adblockicon.h index ed7070266..6927e2d56 100755 --- a/src/network-web/adblock/adblockicon.h +++ b/src/network-web/adblock/adblockicon.h @@ -38,6 +38,7 @@ class AdBlockIcon : public PlainToolButton { QAction *menuAction(); public slots: + void activate(); void setEnabled(bool enabled); void createMenu(QMenu *menu = NULL); diff --git a/src/network-web/adblock/adblockmanager.cpp b/src/network-web/adblock/adblockmanager.cpp index d64b2393f..27212deff 100755 --- a/src/network-web/adblock/adblockmanager.cpp +++ b/src/network-web/adblock/adblockmanager.cpp @@ -173,6 +173,10 @@ AdBlockCustomList *AdBlockManager::customList() const { return NULL; } +bool AdBlockManager::shouldBeEnabled() { + return qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::Enabled)).toBool(); +} + void AdBlockManager::load() { if (m_loaded) { // It is already loaded: subscriptions are loaded from files into objects, @@ -327,6 +331,7 @@ AdBlockSubscription *AdBlockManager::subscriptionByName(const QString &name) con AdBlockDialog *AdBlockManager::showDialog() { QPointer form_pointer = new AdBlockDialog(qApp->mainForm()); + form_pointer.data()->setModal(true); form_pointer.data()->show(); form_pointer.data()->raise(); form_pointer.data()->activateWindow(); diff --git a/src/network-web/adblock/adblockmanager.h b/src/network-web/adblock/adblockmanager.h index 1f11f0814..fd3aaf213 100755 --- a/src/network-web/adblock/adblockmanager.h +++ b/src/network-web/adblock/adblockmanager.h @@ -69,6 +69,8 @@ class AdBlockManager : public QObject { AdBlockCustomList *customList() const; + bool shouldBeEnabled(); + static AdBlockManager *instance(); signals: diff --git a/src/network-web/adblock/adblockrule.cpp b/src/network-web/adblock/adblockrule.cpp index a7371738e..22f70f9ce 100755 --- a/src/network-web/adblock/adblockrule.cpp +++ b/src/network-web/adblock/adblockrule.cpp @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + /** * Copyright (c) 2009, Zsombor Gegesy * Copyright (c) 2009, Benjamin C. Meyer @@ -61,13 +45,13 @@ * SUCH DAMAGE. */ -#include "adblockrule.h" -#include "adblocksubscription.h" -#include "adblockmanager.h" +#include "network-web/adblock/adblockrule.h" +#include "network-web/adblock/adblocksubscription.h" +#include "network-web/adblock/adblockmanager.h" +#include "network-web/webfactory.h" #include "definitions/definitions.h" -#include #include #include #include @@ -75,63 +59,18 @@ #include #include -// Version for Qt < 4.8 has one issue, it will wrongly -// count .co.uk (and others) as second-level domain -static QString toSecondLevelDomain(const QUrl &url) -{ -#if QT_VERSION >= 0x040800 - const QString topLevelDomain = url.topLevelDomain(); - const QString urlHost = url.host(); - - if (topLevelDomain.isEmpty() || urlHost.isEmpty()) { - return QString(); - } - - QString domain = urlHost.left(urlHost.size() - topLevelDomain.size()); - - if (domain.count(QL1C('.')) == 0) { - return urlHost; - } - - while (domain.count(QL1C('.')) != 0) { - domain = domain.mid(domain.indexOf(QL1C('.')) + 1); - } - - return domain + topLevelDomain; -#else - QString domain = url.host(); - - if (domain.count(QL1C('.')) == 0) { - return QString(); - } - - while (domain.count(QL1C('.')) != 1) { - domain = domain.mid(domain.indexOf(QL1C('.')) + 1); - } - - return domain; -#endif -} AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscription) - : m_subscription(subscription) - , m_type(StringContainsMatchRule) - , m_caseSensitivity(Qt::CaseInsensitive) - , m_isEnabled(true) - , m_isException(false) - , m_isInternalDisabled(false) - , m_regExp(0) -{ + : m_subscription(subscription), m_type(StringContainsMatchRule), m_caseSensitivity(Qt::CaseInsensitive), m_isEnabled(true), + m_isException(false), m_isInternalDisabled(false), m_regExp(NULL) { setFilter(filter); } -AdBlockRule::~AdBlockRule() -{ +AdBlockRule::~AdBlockRule() { delete m_regExp; } -AdBlockRule* AdBlockRule::copy() const -{ +AdBlockRule *AdBlockRule::copy() const { AdBlockRule* rule = new AdBlockRule(); rule->m_subscription = m_subscription; rule->m_type = m_type; @@ -146,8 +85,8 @@ AdBlockRule* AdBlockRule::copy() const rule->m_allowedDomains = m_allowedDomains; rule->m_blockedDomains = m_blockedDomains; - if (m_regExp) { - rule->m_regExp = new RegExp; + if (m_regExp != NULL) { + rule->m_regExp = new RegExp(); rule->m_regExp->regExp = m_regExp->regExp; rule->m_regExp->matchers = m_regExp->matchers; } @@ -155,96 +94,77 @@ AdBlockRule* AdBlockRule::copy() const return rule; } -AdBlockSubscription* AdBlockRule::subscription() const -{ +AdBlockSubscription *AdBlockRule::subscription() const { return m_subscription; } -void AdBlockRule::setSubscription(AdBlockSubscription* subscription) -{ +void AdBlockRule::setSubscription(AdBlockSubscription *subscription) { m_subscription = subscription; } -QString AdBlockRule::filter() const -{ +QString AdBlockRule::filter() const { return m_filter; } -void AdBlockRule::setFilter(const QString &filter) -{ +void AdBlockRule::setFilter(const QString &filter) { m_filter = filter; parseFilter(); } -bool AdBlockRule::isCssRule() const -{ +bool AdBlockRule::isCssRule() const { return m_type == CssRule; } -QString AdBlockRule::cssSelector() const -{ +QString AdBlockRule::cssSelector() const { return m_matchString; } -bool AdBlockRule::isDocument() const -{ +bool AdBlockRule::isDocument() const { return hasOption(DocumentOption); } -bool AdBlockRule::isElemhide() const -{ +bool AdBlockRule::isElemhide() const { return hasOption(ElementHideOption); } -bool AdBlockRule::isDomainRestricted() const -{ +bool AdBlockRule::isDomainRestricted() const { return hasOption(DomainRestrictedOption); } -bool AdBlockRule::isException() const -{ +bool AdBlockRule::isException() const { return m_isException; } -bool AdBlockRule::isComment() const -{ +bool AdBlockRule::isComment() const { return m_filter.startsWith(QL1C('!')); } -bool AdBlockRule::isEnabled() const -{ +bool AdBlockRule::isEnabled() const { return m_isEnabled; } -void AdBlockRule::setEnabled(bool enabled) -{ +void AdBlockRule::setEnabled(bool enabled) { m_isEnabled = enabled; } -bool AdBlockRule::isSlow() const -{ - return m_regExp != 0; +bool AdBlockRule::isSlow() const { + return m_regExp != NULL; } -bool AdBlockRule::isInternalDisabled() const -{ +bool AdBlockRule::isInternalDisabled() const { return m_isInternalDisabled; } -bool AdBlockRule::urlMatch(const QUrl &url) const -{ +bool AdBlockRule::urlMatch(const QUrl &url) const { if (!hasOption(DocumentOption) && !hasOption(ElementHideOption)) { return false; } - - const QString encodedUrl = url.toEncoded(); - const QString domain = url.host(); - - return networkMatch(QNetworkRequest(url), domain, encodedUrl); + else { + return networkMatch(QNetworkRequest(url), url.host(), url.toEncoded()); + } } -bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &domain, const QString &encodedUrl) const -{ +bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &domain, const QString &encoded_url) const { if (m_type == CssRule || !m_isEnabled || m_isInternalDisabled) { return false; } @@ -252,39 +172,39 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do bool matched = false; if (m_type == StringContainsMatchRule) { - matched = encodedUrl.contains(m_matchString, m_caseSensitivity); + matched = encoded_url.contains(m_matchString, m_caseSensitivity); } else if (m_type == DomainMatchRule) { matched = isMatchingDomain(domain, m_matchString); } else if (m_type == StringEndsMatchRule) { - matched = encodedUrl.endsWith(m_matchString, m_caseSensitivity); + matched = encoded_url.endsWith(m_matchString, m_caseSensitivity); } else if (m_type == RegExpMatchRule) { - if (!isMatchingRegExpStrings(encodedUrl)) { + if (!isMatchingRegExpStrings(encoded_url)) { return false; } - matched = (m_regExp->regExp.indexIn(encodedUrl) != -1); + matched = (m_regExp->regExp.indexIn(encoded_url) != -1); } if (matched) { - // Check domain restrictions + // Check domain restrictions. if (hasOption(DomainRestrictedOption) && !matchDomain(domain)) { return false; } - // Check third-party restriction + // Check third-party restriction. if (hasOption(ThirdPartyOption) && !matchThirdParty(request)) { return false; } - // Check object restrictions + // Check object restrictions. if (hasOption(ObjectOption) && !matchObject(request)) { return false; } - // Check subdocument restriction + // Check subdocument restriction. if (hasOption(SubdocumentOption) && !matchSubdocument(request)) { return false; } @@ -295,7 +215,7 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do } // Check image restriction - if (hasOption(ImageOption) && !matchImage(encodedUrl)) { + if (hasOption(ImageOption) && !matchImage(encoded_url)) { return false; } } @@ -303,8 +223,7 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do return matched; } -bool AdBlockRule::matchDomain(const QString &domain) const -{ +bool AdBlockRule::matchDomain(const QString &domain) const { if (!m_isEnabled) { return false; } @@ -326,6 +245,7 @@ bool AdBlockRule::matchDomain(const QString &domain) const return false; } } + return true; } else { @@ -345,39 +265,38 @@ bool AdBlockRule::matchDomain(const QString &domain) const return false; } -bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const -{ +bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const { const QString referer = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()).toString(); if (referer.isEmpty()) { return false; } - // Third-party matching should be performed on second-level domains - const QString refererHost = toSecondLevelDomain(QUrl(referer)); - const QString host = toSecondLevelDomain(request.url()); + // Third-party matching should be performed on second-level domains. + const QString refererHost = WebFactory::instance()->toSecondLevelDomain(QUrl(referer)); + const QString host = WebFactory::instance()->toSecondLevelDomain(request.url()); bool match = refererHost != host; return hasException(ThirdPartyOption) ? !match : match; } -bool AdBlockRule::matchObject(const QNetworkRequest &request) const -{ +bool AdBlockRule::matchObject(const QNetworkRequest &request) const { bool match = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 150)).toString() == QL1S("object"); return hasException(ObjectOption) ? !match : match; } -bool AdBlockRule::matchSubdocument(const QNetworkRequest &request) const -{ - QWebFrame* originatingFrame = static_cast(request.originatingObject()); - if (!originatingFrame) { +bool AdBlockRule::matchSubdocument(const QNetworkRequest &request) const { + QWebFrame *originatingFrame = static_cast(request.originatingObject()); + + if (originatingFrame == NULL) { return false; } - QWebPage* page = originatingFrame->page(); - if (!page) { + QWebPage *page = originatingFrame->page(); + + if (page == NULL) { return false; } @@ -386,65 +305,62 @@ bool AdBlockRule::matchSubdocument(const QNetworkRequest &request) const return hasException(SubdocumentOption) ? !match : match; } -bool AdBlockRule::matchXmlHttpRequest(const QNetworkRequest &request) const -{ +bool AdBlockRule::matchXmlHttpRequest(const QNetworkRequest &request) const { bool match = request.rawHeader("X-Requested-With") == QByteArray("XMLHttpRequest"); return hasException(XMLHttpRequestOption) ? !match : match; } -bool AdBlockRule::matchImage(const QString &encodedUrl) const -{ - bool match = encodedUrl.endsWith(QL1S(".png")) || - encodedUrl.endsWith(QL1S(".jpg")) || - encodedUrl.endsWith(QL1S(".gif")) || - encodedUrl.endsWith(QL1S(".jpeg")); +bool AdBlockRule::matchImage(const QString &encoded_url) const { + bool match = encoded_url.endsWith(QL1S(".png")) || + encoded_url.endsWith(QL1S(".jpg")) || + encoded_url.endsWith(QL1S(".gif")) || + encoded_url.endsWith(QL1S(".jpeg")); return hasException(ImageOption) ? !match : match; } -void AdBlockRule::parseFilter() -{ - QString parsedLine = m_filter; +void AdBlockRule::parseFilter() { + QString parsed_line = m_filter; - // Empty rule or just comment + // Empty rule or just comment. if (m_filter.trimmed().isEmpty() || m_filter.startsWith(QL1C('!'))) { // We want to differentiate rule disabled by user and rule disabled in subscription file - // m_isInternalDisabled is also used when rule is disabled due to all options not being supported + // m_isInternalDisabled is also used when rule is disabled due to all options not being supported. m_isEnabled = false; m_isInternalDisabled = true; m_type = Invalid; return; } - // CSS Element hiding rule - if (parsedLine.contains(QL1S("##")) || parsedLine.contains(QL1S("#@#"))) { + // CSS Element hiding rule. + if (parsed_line.contains(QL1S("##")) || parsed_line.contains(QL1S("#@#"))) { m_type = CssRule; - int pos = parsedLine.indexOf(QL1C('#')); + int pos = parsed_line.indexOf(QL1C('#')); // Domain restricted rule - if (!parsedLine.startsWith(QL1S("##"))) { - QString domains = parsedLine.left(pos); + if (!parsed_line.startsWith(QL1S("##"))) { + QString domains = parsed_line.left(pos); parseDomains(domains, QL1C(',')); } - m_isException = parsedLine.at(pos + 1) == QL1C('@'); - m_matchString = parsedLine.mid(m_isException ? pos + 3 : pos + 2); + m_isException = parsed_line.at(pos + 1) == QL1C('@'); + m_matchString = parsed_line.mid(m_isException ? pos + 3 : pos + 2); - // CSS rule cannot have more options -> stop parsing + // CSS rule cannot have more options -> stop parsing. return; } // Exception always starts with @@ - if (parsedLine.startsWith(QL1S("@@"))) { + if (parsed_line.startsWith(QL1S("@@"))) { m_isException = true; - parsedLine = parsedLine.mid(2); + parsed_line = parsed_line.mid(2); } - // Parse all options following $ char - int optionsIndex = parsedLine.indexOf(QL1C('$')); + // Parse all options following $ char. + int optionsIndex = parsed_line.indexOf(QL1C('$')); if (optionsIndex >= 0) { - const QStringList options = parsedLine.mid(optionsIndex + 1).split(QL1C(','), QString::SkipEmptyParts); + const QStringList options = parsed_line.mid(optionsIndex + 1).split(QL1C(','), QString::SkipEmptyParts); int handledOptions = 0; foreach (const QString &option, options) { @@ -490,92 +406,90 @@ void AdBlockRule::parseFilter() ++handledOptions; } else if (option == QL1S("collapse")) { - // Hiding placeholders of blocked elements is enabled by default + // Hiding placeholders of blocked elements is enabled by default. ++handledOptions; } } - // If we don't handle all options, it's safer to just disable this rule + // If we don't handle all options, it's safer to just disable this rule. if (handledOptions != options.count()) { m_isInternalDisabled = true; m_type = Invalid; return; } - parsedLine = parsedLine.left(optionsIndex); + parsed_line = parsed_line.left(optionsIndex); } - // Rule is classic regexp - if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/'))) { - parsedLine = parsedLine.mid(1); - parsedLine = parsedLine.left(parsedLine.size() - 1); + // Rule is classic regexp. + if (parsed_line.startsWith(QL1C('/')) && parsed_line.endsWith(QL1C('/'))) { + parsed_line = parsed_line.mid(1); + parsed_line = parsed_line.left(parsed_line.size() - 1); m_type = RegExpMatchRule; - m_regExp = new RegExp; - m_regExp->regExp = QRegExp(parsedLine, m_caseSensitivity); - m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine)); + m_regExp = new RegExp(); + m_regExp->regExp = QRegExp(parsed_line, m_caseSensitivity); + m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsed_line)); return; } - // Remove starting and ending wildcards (*) - if (parsedLine.startsWith(QL1C('*'))) { - parsedLine = parsedLine.mid(1); + // Remove starting and ending wildcards (*). + if (parsed_line.startsWith(QL1C('*'))) { + parsed_line = parsed_line.mid(1); } - if (parsedLine.endsWith(QL1C('*'))) { - parsedLine = parsedLine.left(parsedLine.size() - 1); + if (parsed_line.endsWith(QL1C('*'))) { + parsed_line = parsed_line.left(parsed_line.size() - 1); } - // We can use fast string matching for domain here - if (filterIsOnlyDomain(parsedLine)) { - parsedLine = parsedLine.mid(2); - parsedLine = parsedLine.left(parsedLine.size() - 1); + // We can use fast string matching for domain here. + if (filterIsOnlyDomain(parsed_line)) { + parsed_line = parsed_line.mid(2); + parsed_line = parsed_line.left(parsed_line.size() - 1); m_type = DomainMatchRule; - m_matchString = parsedLine; + m_matchString = parsed_line; return; } - // If rule contains only | at end, we can also use string matching - if (filterIsOnlyEndsMatch(parsedLine)) { - parsedLine = parsedLine.left(parsedLine.size() - 1); + // If rule contains only | at end, we can also use string matching. + if (filterIsOnlyEndsMatch(parsed_line)) { + parsed_line = parsed_line.left(parsed_line.size() - 1); m_type = StringEndsMatchRule; - m_matchString = parsedLine; + m_matchString = parsed_line; return; } // If we still find a wildcard (*) or separator (^) or (|) - // we must modify parsedLine to comply with QzRegExp - if (parsedLine.contains(QL1C('*')) || - parsedLine.contains(QL1C('^')) || - parsedLine.contains(QL1C('|')) + // we must modify parsedLine to comply with QzRegExp. + if (parsed_line.contains(QL1C('*')) || + parsed_line.contains(QL1C('^')) || + parsed_line.contains(QL1C('|')) ) { m_type = RegExpMatchRule; m_regExp = new RegExp; - m_regExp->regExp = QRegExp(createRegExpFromFilter(parsedLine), m_caseSensitivity); - m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine)); + m_regExp->regExp = QRegExp(createRegExpFromFilter(parsed_line), m_caseSensitivity); + m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsed_line)); return; } // We haven't found anything that needs use of regexp, yay! m_type = StringContainsMatchRule; - m_matchString = parsedLine; + m_matchString = parsed_line; } -void AdBlockRule::parseDomains(const QString &domains, const QChar &separator) -{ - QStringList domainsList = domains.split(separator, QString::SkipEmptyParts); +void AdBlockRule::parseDomains(const QString &domains, const QChar &separator) { + QStringList domains_list = domains.split(separator, QString::SkipEmptyParts); - foreach (const QString domain, domainsList) { - if (domain.isEmpty()) { - continue; - } - if (domain.startsWith(QL1C('~'))) { - m_blockedDomains.append(domain.mid(1)); - } - else { - m_allowedDomains.append(domain); + foreach (const QString domain, domains_list) { + if (!domain.isEmpty()) { + if (domain.startsWith(QL1C('~'))) { + m_blockedDomains.append(domain.mid(1)); + } + else { + m_allowedDomains.append(domain); + } } } @@ -584,91 +498,94 @@ void AdBlockRule::parseDomains(const QString &domains, const QChar &separator) } } -bool AdBlockRule::filterIsOnlyDomain(const QString &filter) const -{ - if (!filter.endsWith(QL1C('^')) || !filter.startsWith(QL1S("||"))) +bool AdBlockRule::filterIsOnlyDomain(const QString &filter) const { + if (!filter.endsWith(QL1C('^')) || !filter.startsWith(QL1S("||"))) { return false; + } - for (int i = 0; i < filter.size(); ++i) { + for (int i = 0; i < filter.size(); i++) { switch (filter.at(i).toLatin1()) { - case '/': - case ':': - case '?': - case '=': - case '&': - case '*': - return false; - default: - break; + case '/': + case ':': + case '?': + case '=': + case '&': + case '*': + return false; + + default: + break; } } return true; } -bool AdBlockRule::filterIsOnlyEndsMatch(const QString &filter) const -{ +bool AdBlockRule::filterIsOnlyEndsMatch(const QString &filter) const { for (int i = 0; i < filter.size(); ++i) { switch (filter.at(i).toLatin1()) { - case '^': - case '*': - return false; - case '|': - return i == filter.size() - 1; - default: - break; + case '^': + case '*': + return false; + + case '|': + return i == filter.size() - 1; + + default: + break; } } return false; } -static bool wordCharacter(const QChar &c) -{ +static bool wordCharacter(const QChar &c) { return c.isLetterOrNumber() || c.isMark() || c == QL1C('_'); } -QString AdBlockRule::createRegExpFromFilter(const QString &filter) const -{ +QString AdBlockRule::createRegExpFromFilter(const QString &filter) const { QString parsed; parsed.reserve(filter.size()); - bool hadWildcard = false; // Filter multiple wildcards + bool hadWildcard = false; // Filter multiple wildcards. - for (int i = 0; i < filter.size(); ++i) { + for (int i = 0; i < filter.size(); i++) { const QChar c = filter.at(i); + switch (c.toLatin1()) { - case '^': - parsed.append(QL1S("(?:[^\\w\\d\\-.%]|$)")); - break; + case '^': + parsed.append(QL1S("(?:[^\\w\\d\\-.%]|$)")); + break; - case '*': - if (!hadWildcard) - parsed.append(QL1S(".*")); - break; + case '*': + if (!hadWildcard) + parsed.append(QL1S(".*")); + break; - case '|': - if (i == 0) { - if (filter.size() > 1 && filter.at(1) == QL1C('|')) { - parsed.append(QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?")); - i++; + case '|': + if (i == 0) { + if (filter.size() > 1 && filter.at(1) == QL1C('|')) { + parsed.append(QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?")); + i++; + } + else { + parsed.append('^'); + } + break; + } + else if (i == filter.size() - 1) { + parsed.append(QL1C('$')); + break; + } + // Fall through. + + default: + if (!wordCharacter(c)) { + parsed.append(QL1C('\\') + c); } else { - parsed.append('^'); + parsed.append(c); } - break; - } - else if (i == filter.size() - 1) { - parsed.append(QL1C('$')); - break; - } - // fallthrough - - default: - if (!wordCharacter(c)) - parsed.append(QL1C('\\') + c); - else - parsed.append(c); } hadWildcard = c == QL1C('*'); @@ -677,8 +594,7 @@ QString AdBlockRule::createRegExpFromFilter(const QString &filter) const return parsed; } -QList AdBlockRule::createStringMatchers(const QStringList &filters) const -{ +QList AdBlockRule::createStringMatchers(const QStringList &filters) const { QList matchers; matchers.reserve(filters.size()); @@ -689,8 +605,7 @@ QList AdBlockRule::createStringMatchers(const QStringList &filte return matchers; } -bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &pattern) const -{ +bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &pattern) const { if (pattern == domain) { return true; } @@ -704,62 +619,63 @@ bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &pattern return index > 0 && domain[index - 1] == QLatin1Char('.'); } -bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const -{ +bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const { Q_ASSERT(m_regExp); foreach (const QStringMatcher &matcher, m_regExp->matchers) { - if (matcher.indexIn(url) == -1) + if (matcher.indexIn(url) == -1) { return false; + } } return true; } -// Split regexp filter into strings that can be used with QString::contains -// Don't use parts that contains only 1 char and duplicated parts -QStringList AdBlockRule::parseRegExpFilter(const QString &filter) const -{ +// Split regexp filter into strings that can be used with QString::contains. +// Don't use parts that contains only 1 char and duplicated parts. +QStringList AdBlockRule::parseRegExpFilter(const QString &filter) const { QStringList list; - int startPos = -1; + int start_pos = -1; - for (int i = 0; i < filter.size(); ++i) { + for (int i = 0; i < filter.size(); i++) { const QChar c = filter.at(i); - // Meta characters in AdBlock rules are | * ^ + + // Meta characters in AdBlock rules are "| * ^". if (c == QL1C('|') || c == QL1C('*') || c == QL1C('^')) { - const QString sub = filter.mid(startPos, i - startPos); - if (sub.size() > 1) + const QString sub = filter.mid(start_pos, i - start_pos); + + if (sub.size() > 1) { list.append(sub); - startPos = i + 1; + } + + start_pos = i + 1; } } - const QString sub = filter.mid(startPos); - if (sub.size() > 1) + const QString sub = filter.mid(start_pos); + + if (sub.size() > 1) { list.append(sub); + } list.removeDuplicates(); return list; } -bool AdBlockRule::hasOption(const AdBlockRule::RuleOption &opt) const -{ +bool AdBlockRule::hasOption(const AdBlockRule::RuleOption &opt) const { return (m_options & opt); } -bool AdBlockRule::hasException(const AdBlockRule::RuleOption &opt) const -{ +bool AdBlockRule::hasException(const AdBlockRule::RuleOption &opt) const { return (m_exceptions & opt); } -void AdBlockRule::setOption(const AdBlockRule::RuleOption &opt) -{ +void AdBlockRule::setOption(const AdBlockRule::RuleOption &opt) { m_options |= opt; } -void AdBlockRule::setException(const AdBlockRule::RuleOption &opt, bool on) -{ +void AdBlockRule::setException(const AdBlockRule::RuleOption &opt, bool on) { if (on) { m_exceptions |= opt; } diff --git a/src/network-web/adblock/adblockrule.h b/src/network-web/adblock/adblockrule.h index e5dc497a5..19b1a37a0 100755 --- a/src/network-web/adblock/adblockrule.h +++ b/src/network-web/adblock/adblockrule.h @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + /** * Copyright (c) 2009, Benjamin C. Meyer * @@ -71,120 +55,125 @@ class QUrl; class AdBlockSubscription; -class AdBlockRule -{ -public: - AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = NULL); - ~AdBlockRule(); +class AdBlockRule { + friend class AdBlockMatcher; + friend class AdBlockSearchTree; + friend class AdBlockSubscription; - AdBlockRule* copy() const; + public: + explicit AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = NULL); + virtual ~AdBlockRule(); - AdBlockSubscription* subscription() const; - void setSubscription(AdBlockSubscription* subscription); + AdBlockRule *copy() const; - QString filter() const; - void setFilter(const QString &filter); + AdBlockSubscription *subscription() const; + void setSubscription(AdBlockSubscription *subscription); - bool isCssRule() const; - QString cssSelector() const; + QString filter() const; + void setFilter(const QString &filter); - bool isDocument() const; - bool isElemhide() const; + bool isCssRule() const; + QString cssSelector() const; - bool isDomainRestricted() const; - bool isException() const; + bool isDocument() const; + bool isElemhide() const; - bool isComment() const; - bool isEnabled() const; - void setEnabled(bool enabled); + bool isDomainRestricted() const; + bool isException() const; - bool isSlow() const; - bool isInternalDisabled() const; + bool isComment() const; + bool isEnabled() const; + void setEnabled(bool enabled); - bool urlMatch(const QUrl &url) const; - bool networkMatch(const QNetworkRequest &request, const QString &domain, const QString &encodedUrl) const; + bool isSlow() const; + bool isInternalDisabled() const; - bool matchDomain(const QString &domain) const; - bool matchThirdParty(const QNetworkRequest &request) const; - bool matchObject(const QNetworkRequest &request) const; - bool matchSubdocument(const QNetworkRequest &request) const; - bool matchXmlHttpRequest(const QNetworkRequest &request) const; - bool matchImage(const QString &encodedUrl) const; + bool urlMatch(const QUrl &url) const; + bool networkMatch(const QNetworkRequest &request, const QString &domain, const QString &encoded_url) const; -protected: - bool isMatchingDomain(const QString &domain, const QString &pattern) const; - bool isMatchingRegExpStrings(const QString &url) const; - QStringList parseRegExpFilter(const QString &filter) const; + bool matchDomain(const QString &domain) const; + bool matchThirdParty(const QNetworkRequest &request) const; + bool matchObject(const QNetworkRequest &request) const; + bool matchSubdocument(const QNetworkRequest &request) const; + bool matchXmlHttpRequest(const QNetworkRequest &request) const; + bool matchImage(const QString &encoded_url) const; -private: - enum RuleType { - CssRule = 0, - DomainMatchRule = 1, - RegExpMatchRule = 2, - StringEndsMatchRule = 3, - StringContainsMatchRule = 4, - Invalid = 5 - }; + protected: + bool isMatchingDomain(const QString &domain, const QString &pattern) const; + bool isMatchingRegExpStrings(const QString &url) const; + QStringList parseRegExpFilter(const QString &filter) const; - enum RuleOption { - DomainRestrictedOption = 1, - ThirdPartyOption = 2, - ObjectOption = 4, - SubdocumentOption = 8, - XMLHttpRequestOption = 16, - ImageOption = 32, + private: + enum RuleType { + CssRule = 0, + DomainMatchRule = 1, + RegExpMatchRule = 2, + StringEndsMatchRule = 3, + StringContainsMatchRule = 4, + Invalid = 5 + }; - // Exception only options - DocumentOption = 64, - ElementHideOption = 128 - }; + enum RuleOption { + DomainRestrictedOption = 1, + ThirdPartyOption = 2, + ObjectOption = 4, + SubdocumentOption = 8, + XMLHttpRequestOption = 16, + ImageOption = 32, - Q_DECLARE_FLAGS(RuleOptions, RuleOption) + // Exception only options. + DocumentOption = 64, + ElementHideOption = 128 + }; - inline bool hasOption(const RuleOption &opt) const; - inline bool hasException(const RuleOption &opt) const; + Q_DECLARE_FLAGS(RuleOptions, RuleOption) - inline void setOption(const RuleOption &opt); - inline void setException(const RuleOption &opt, bool on); + inline bool hasOption(const RuleOption &opt) const; + inline bool hasException(const RuleOption &opt) const; - void parseFilter(); - void parseDomains(const QString &domains, const QChar &separator); - bool filterIsOnlyDomain(const QString &filter) const; - bool filterIsOnlyEndsMatch(const QString &filter) const; - QString createRegExpFromFilter(const QString &filter) const; - QList createStringMatchers(const QStringList &filters) const; + inline void setOption(const RuleOption &opt); + inline void setException(const RuleOption &opt, bool on); - AdBlockSubscription* m_subscription; + void parseFilter(); + void parseDomains(const QString &domains, const QChar &separator); + bool filterIsOnlyDomain(const QString &filter) const; + bool filterIsOnlyEndsMatch(const QString &filter) const; + QString createRegExpFromFilter(const QString &filter) const; + QList createStringMatchers(const QStringList &filters) const; - RuleType m_type; - RuleOptions m_options; - RuleOptions m_exceptions; + AdBlockSubscription *m_subscription; - // Original rule filter - QString m_filter; - // Parsed rule for string matching (CSS Selector for CSS rules) - QString m_matchString; - // Case sensitivity for string matching - Qt::CaseSensitivity m_caseSensitivity; + RuleType m_type; + RuleOptions m_options; + RuleOptions m_exceptions; - bool m_isEnabled; - bool m_isException; - bool m_isInternalDisabled; + // Original rule filter. + QString m_filter; - QStringList m_allowedDomains; - QStringList m_blockedDomains; + // Parsed rule for string matching (CSS Selector for CSS rules). + QString m_matchString; - struct RegExp { - QRegExp regExp; - QList matchers; - }; + // Case sensitivity for string matching. + Qt::CaseSensitivity m_caseSensitivity; - // Use dynamic allocation to save memory - RegExp* m_regExp; + bool m_isEnabled; + bool m_isException; + bool m_isInternalDisabled; - friend class AdBlockMatcher; - friend class AdBlockSearchTree; - friend class AdBlockSubscription; + QStringList m_allowedDomains; + QStringList m_blockedDomains; + + struct RegExp { + public: + explicit RegExp() { + } + + QRegExp regExp; + QList matchers; + }; + + // Use dynamic allocation to save memory + RegExp *m_regExp; }; #endif // ADBLOCKRULE_H diff --git a/src/network-web/adblock/adblocksearchtree.cpp b/src/network-web/adblock/adblocksearchtree.cpp index a8d694f99..3ac36a272 100755 --- a/src/network-web/adblock/adblocksearchtree.cpp +++ b/src/network-web/adblock/adblocksearchtree.cpp @@ -1,60 +1,38 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2013-2014 David Rosca -* -* 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 "adblocksearchtree.h" -#include "adblockrule.h" +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . -#include +#include "network-web/adblock/adblocksearchtree.h" +#include "network-web/adblock/adblockrule.h" -AdBlockSearchTree::AdBlockSearchTree() - : m_root(new Node) -{ + +AdBlockSearchTree::AdBlockSearchTree() : m_root(new Node()) { } -AdBlockSearchTree::~AdBlockSearchTree() -{ +AdBlockSearchTree::~AdBlockSearchTree() { deleteNode(m_root); } -void AdBlockSearchTree::clear() -{ +void AdBlockSearchTree::clear() { deleteNode(m_root); - m_root = new Node; + m_root = new Node(); } -bool AdBlockSearchTree::add(const AdBlockRule* rule) -{ +bool AdBlockSearchTree::add(const AdBlockRule *rule) { if (rule->m_type != AdBlockRule::StringContainsMatchRule) { return false; } @@ -63,16 +41,17 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule) int len = filter.size(); if (len <= 0) { - qDebug() << "AdBlockSearchTree: Inserting rule with filter len <= 0!"; + qWarning("Inserting rule with filter len <= 0!"); return false; } - Node* node = m_root; + Node *node = m_root; - for (int i = 0; i < len; ++i) { + for (int i = 0; i < len; i++) { const QChar c = filter.at(i); + if (!node->children.contains(c)) { - Node* n = new Node; + Node *n = new Node(); n->c = c; node->children[c] = n; @@ -86,19 +65,20 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule) return true; } -const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const QString &domain, const QString &urlString) const -{ - int len = urlString.size(); +const AdBlockRule *AdBlockSearchTree::find(const QNetworkRequest &request, const QString &domain, + const QString &url_string) const { + int len = url_string.size(); if (len <= 0) { return NULL; } - const QChar* string = urlString.constData(); + const QChar *string = url_string.constData(); - for (int i = 0; i < len; ++i) { - const AdBlockRule* rule = prefixSearch(request, domain, urlString, string++, len - i); - if (rule) { + for (int i = 0; i < len; i++) { + const AdBlockRule *rule = prefixSearch(request, domain, url_string, string++, len - i); + + if (rule != NULL) { return rule; } } @@ -106,48 +86,48 @@ const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const return NULL; } -const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QChar* string, int len) const -{ +const AdBlockRule *AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, + const QString &url_string, const QChar* string, int len) const { if (len <= 0) { - return 0; + return NULL; } QChar c = string[0]; if (!m_root->children.contains(c)) { - return 0; + return NULL; } - Node* node = m_root->children[c]; + Node *node = m_root->children[c]; - for (int i = 1; i < len; ++i) { + for (int i = 1; i < len; i++) { const QChar c = (++string)[0]; - if (node->rule && node->rule->networkMatch(request, domain, urlString)) { + if (node->rule && node->rule->networkMatch(request, domain, url_string)) { return node->rule; } if (!node->children.contains(c)) { - return 0; + return NULL; } node = node->children[c]; } - if (node->rule && node->rule->networkMatch(request, domain, urlString)) { + if (node->rule && node->rule->networkMatch(request, domain, url_string)) { return node->rule; } - return 0; + return NULL; } -void AdBlockSearchTree::deleteNode(AdBlockSearchTree::Node* node) -{ - if (!node) { +void AdBlockSearchTree::deleteNode(Node *node) { + if (node == NULL) { return; } - QHashIterator i(node->children); + QHashIterator i(node->children); + while (i.hasNext()) { i.next(); deleteNode(i.value()); diff --git a/src/network-web/adblock/adblocksearchtree.h b/src/network-web/adblock/adblocksearchtree.h index 884f875bf..83e3c6431 100755 --- a/src/network-web/adblock/adblocksearchtree.h +++ b/src/network-web/adblock/adblocksearchtree.h @@ -1,73 +1,57 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2013-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + #ifndef ADBLOCKSEARCHTREE_H #define ADBLOCKSEARCHTREE_H #include #include -class QNetworkRequest; +class QNetworkRequest; class AdBlockRule; -class AdBlockSearchTree -{ -public: - explicit AdBlockSearchTree(); - ~AdBlockSearchTree(); +class AdBlockSearchTree { + public: + explicit AdBlockSearchTree(); + virtual ~AdBlockSearchTree(); - void clear(); + void clear(); - bool add(const AdBlockRule* rule); - const AdBlockRule* find(const QNetworkRequest &request, const QString &domain, const QString &urlString) const; + bool add(const AdBlockRule *rule); + const AdBlockRule *find(const QNetworkRequest &request, const QString &domain, const QString &url_string) const; -private: - struct Node { - QChar c; - const AdBlockRule* rule; - QHash children; + private: + struct Node { + public: + QChar c; + const AdBlockRule *rule; + QHash children; - Node() : c(0) , rule(0) { } - }; + Node() : c(0) , rule(0) { + } + }; - const AdBlockRule* prefixSearch(const QNetworkRequest &request, const QString &domain, - const QString &urlString, const QChar* string, int len) const; + void deleteNode(Node *node); + const AdBlockRule *prefixSearch(const QNetworkRequest &request, const QString &domain, + const QString &url_string, const QChar* string, int len) const; - void deleteNode(Node* node); - - Node* m_root; + Node *m_root; }; #endif // ADBLOCKSEARCHTREE_H diff --git a/src/network-web/adblock/adblocksubscription.cpp b/src/network-web/adblock/adblocksubscription.cpp index cc46bbbbe..93e046205 100755 --- a/src/network-web/adblock/adblocksubscription.cpp +++ b/src/network-web/adblock/adblocksubscription.cpp @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + /** * Copyright (c) 2009, Benjamin C. Meyer * @@ -59,85 +43,78 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -#include "adblocksubscription.h" -#include "adblockmanager.h" -#include "adblocksearchtree.h" -#include "followredirectreply.h" +#include "network-web/adblock/adblocksubscription.h" + +#include "network-web/adblock/adblockmanager.h" +#include "network-web/adblock/adblocksearchtree.h" +#include "network-web/adblock/followredirectreply.h" #include "network-web/silentnetworkaccessmanager.h" #include "miscellaneous/iofactory.h" #include "miscellaneous/application.h" +#include "exceptions/applicationexception.h" #include #include #include -#include +#include #include -#include -AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent) - : QObject(parent) - , m_reply(0) - , m_title(title) - , m_updated(false) -{ +AdBlockSubscription::AdBlockSubscription(const QString &title, QObject *parent) + : QObject(parent), m_reply(NULL), m_title(title), m_updated(false) { } -QString AdBlockSubscription::title() const -{ +QString AdBlockSubscription::title() const { return m_title; } -void AdBlockSubscription::setTitle(const QString &title) -{ +void AdBlockSubscription::setTitle(const QString &title) { m_title = title; } -QString AdBlockSubscription::filePath() const -{ +QString AdBlockSubscription::filePath() const { return m_filePath; } -void AdBlockSubscription::setFilePath(const QString &path) -{ +void AdBlockSubscription::setFilePath(const QString &path) { m_filePath = path; } -QUrl AdBlockSubscription::url() const -{ +QUrl AdBlockSubscription::url() const { return m_url; } -void AdBlockSubscription::setUrl(const QUrl &url) -{ +void AdBlockSubscription::setUrl(const QUrl &url) { m_url = url; } -void AdBlockSubscription::loadSubscription(const QStringList &disabledRules) -{ +void AdBlockSubscription::loadSubscription(const QStringList &disabled_rules) { QFile file(m_filePath); if (!file.exists()) { + qWarning("Cannot load subscription '%s'. Requesting its update.", qPrintable(title())); QTimer::singleShot(0, this, SLOT(updateSubscription())); return; } if (!file.open(QFile::ReadOnly)) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for reading" << m_filePath; + qWarning("Unable to open subscription file '%s' for reading.", qPrintable(QDir::toNativeSeparators(m_filePath))); QTimer::singleShot(0, this, SLOT(updateSubscription())); return; } QTextStream textStream(&file); textStream.setCodec("UTF-8"); - // Header is on 3rd line + + // Header is on 3rd line. textStream.readLine(1024); textStream.readLine(1024); QString header = textStream.readLine(1024); if (!header.startsWith(QLatin1String("[Adblock")) || m_title.isEmpty()) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "invalid format of adblock file" << m_filePath; + qWarning("Invalid format of subscription file '%s'.", qPrintable(QDir::toNativeSeparators(m_filePath))); + file.close(); QTimer::singleShot(0, this, SLOT(updateSubscription())); return; } @@ -145,38 +122,38 @@ void AdBlockSubscription::loadSubscription(const QStringList &disabledRules) m_rules.clear(); while (!textStream.atEnd()) { - AdBlockRule* rule = new AdBlockRule(textStream.readLine(), this); + AdBlockRule *rule = new AdBlockRule(textStream.readLine(), this); - if (disabledRules.contains(rule->filter())) { + if (disabled_rules.contains(rule->filter())) { rule->setEnabled(false); } m_rules.append(rule); } - // Initial update + file.close(); + + // Initial update. if (m_rules.isEmpty() && !m_updated) { QTimer::singleShot(0, this, SLOT(updateSubscription())); } } -void AdBlockSubscription::saveSubscription() -{ +void AdBlockSubscription::saveSubscription() { } -void AdBlockSubscription::updateSubscription() -{ - if (m_reply || !m_url.isValid()) { +void AdBlockSubscription::updateSubscription() { + if (m_reply != NULL || !m_url.isValid()) { return; } + // TODO: Refaktorovat. m_reply = new FollowRedirectReply(m_url, SilentNetworkAccessManager::instance()); connect(m_reply, SIGNAL(finished()), this, SLOT(subscriptionDownloaded())); } -void AdBlockSubscription::subscriptionDownloaded() -{ +void AdBlockSubscription::subscriptionDownloaded() { if (m_reply != qobject_cast(sender())) { return; } @@ -186,8 +163,7 @@ void AdBlockSubscription::subscriptionDownloaded() if (m_reply->error() != QNetworkReply::NoError || !response.startsWith(QByteArray("[Adblock")) || - !saveDownloadedData(response) - ) { + !saveDownloadedData(response)) { error = true; } @@ -196,21 +172,20 @@ void AdBlockSubscription::subscriptionDownloaded() if (error) { emit subscriptionError(tr("Cannot load subscription!")); - return; } + else { + loadSubscription(AdBlockManager::instance()->disabledRules()); - loadSubscription(AdBlockManager::instance()->disabledRules()); - - emit subscriptionUpdated(); - emit subscriptionChanged(); + emit subscriptionUpdated(); + emit subscriptionChanged(); + } } -bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) -{ +bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) { QFile file(m_filePath); if (!file.open(QFile::ReadWrite | QFile::Truncate)) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath; + qWarning("Unable to open subscription file '%s' for writting.", qPrintable(QDir::toNativeSeparators(m_filePath))); return false; } @@ -227,36 +202,36 @@ bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) file.write(part1); file.write(part2); + file.flush(); file.close(); return true; } file.write(data); + file.flush(); file.close(); return true; } -const AdBlockRule* AdBlockSubscription::rule(int offset) const -{ - if (!(offset >= 0 && m_rules.count() > offset)) { - return 0; +const AdBlockRule *AdBlockSubscription::rule(int offset) const{ + if (!(offset >= 0 && m_rules.size() > offset)) { + return NULL; + } + else { + return m_rules[offset]; } - - return m_rules[offset]; } -QVector AdBlockSubscription::allRules() const -{ +QVector AdBlockSubscription::allRules() const { return m_rules; } -const AdBlockRule* AdBlockSubscription::enableRule(int offset) -{ - if (!(offset >= 0 && m_rules.count() > offset)) { - return 0; +const AdBlockRule *AdBlockSubscription::enableRule(int offset) { + if (!(offset >= 0 && m_rules.size() > offset)) { + return NULL; } - AdBlockRule* rule = m_rules[offset]; + AdBlockRule *rule = m_rules[offset]; rule->setEnabled(true); AdBlockManager::instance()->removeDisabledRule(rule->filter()); @@ -270,13 +245,12 @@ const AdBlockRule* AdBlockSubscription::enableRule(int offset) return rule; } -const AdBlockRule* AdBlockSubscription::disableRule(int offset) -{ - if (!(offset >= 0 && m_rules.count() > offset)) { - return 0; +const AdBlockRule *AdBlockSubscription::disableRule(int offset) { + if (!(offset >= 0 && m_rules.size() > offset)) { + return NULL; } - AdBlockRule* rule = m_rules[offset]; + AdBlockRule *rule = m_rules[offset]; rule->setEnabled(false); AdBlockManager::instance()->addDisabledRule(rule->filter()); @@ -291,87 +265,48 @@ const AdBlockRule* AdBlockSubscription::disableRule(int offset) return rule; } -bool AdBlockSubscription::canEditRules() const -{ +bool AdBlockSubscription::canEditRules() const { return false; } -bool AdBlockSubscription::canBeRemoved() const -{ +bool AdBlockSubscription::canBeRemoved() const { return true; } -int AdBlockSubscription::addRule(AdBlockRule* rule) -{ +int AdBlockSubscription::addRule(AdBlockRule *rule) { Q_UNUSED(rule) return -1; } -bool AdBlockSubscription::removeRule(int offset) -{ +bool AdBlockSubscription::removeRule(int offset) { Q_UNUSED(offset) return false; } -const AdBlockRule* AdBlockSubscription::replaceRule(AdBlockRule* rule, int offset) -{ +const AdBlockRule *AdBlockSubscription::replaceRule(AdBlockRule *rule, int offset) { Q_UNUSED(rule) Q_UNUSED(offset) return 0; } -AdBlockSubscription::~AdBlockSubscription() -{ +AdBlockSubscription::~AdBlockSubscription() { qDeleteAll(m_rules); } -// AdBlockCustomList - -AdBlockCustomList::AdBlockCustomList(QObject* parent) : AdBlockSubscription(tr("Custom rules"), parent) { +AdBlockCustomList::AdBlockCustomList(QObject *parent) : AdBlockSubscription(tr("Custom rules"), parent) { setTitle(tr("Custom rules")); - // TODO + // TODO cesta setFilePath(qApp->homeFolderPath() + "/adblock/customlist.txt"); } -void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) -{ - // DuckDuckGo ad whitelist rules - // They cannot be removed, but can be disabled. - // Please consider not disabling them. Thanks! - - const QString ddg1 = QSL("@@||duckduckgo.com^$document"); - const QString ddg2 = QSL("duckduckgo.com#@#.has-ad"); - - QString rules = QString(); - - try { - rules = QString::fromUtf8(IOFactory::readTextFile((filePath()))); - } - catch (ApplicationException &ex) { - } - - QFile file(filePath()); - if (file.open(QFile::WriteOnly | QFile::Append)) { - QTextStream stream(&file); - stream.setCodec("UTF-8"); - - if (!rules.contains(ddg1 + QLatin1String("\n"))) - stream << ddg1 << endl; - - if (!rules.contains(QLatin1String("\n") + ddg2)) - stream << ddg2 << endl; - } - file.close(); - - AdBlockSubscription::loadSubscription(disabledRules); +AdBlockCustomList::~AdBlockCustomList() { } -void AdBlockCustomList::saveSubscription() -{ - QFile file(filePath()); +void AdBlockCustomList::saveSubscription() { + QFile file(m_filePath); - if (!file.open(QFile::ReadWrite | QFile::Truncate)) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << filePath(); + if (!file.open(QFile::WriteOnly | QFile::Truncate)) { + qWarning("Unable to open custom subscription file '%s' for writting.", qPrintable(QDir::toNativeSeparators(m_filePath))); return; } @@ -385,22 +320,20 @@ void AdBlockCustomList::saveSubscription() textStream << rule->filter() << endl; } + file.flush(); file.close(); } -bool AdBlockCustomList::canEditRules() const -{ +bool AdBlockCustomList::canEditRules() const { return true; } -bool AdBlockCustomList::canBeRemoved() const -{ +bool AdBlockCustomList::canBeRemoved() const { return false; } -bool AdBlockCustomList::containsFilter(const QString &filter) const -{ - foreach (const AdBlockRule* rule, m_rules) { +bool AdBlockCustomList::containsFilter(const QString &filter) const { + foreach (const AdBlockRule *rule, m_rules) { if (rule->filter() == filter) { return true; } @@ -409,10 +342,9 @@ bool AdBlockCustomList::containsFilter(const QString &filter) const return false; } -bool AdBlockCustomList::removeFilter(const QString &filter) -{ - for (int i = 0; i < m_rules.count(); ++i) { - const AdBlockRule* rule = m_rules.at(i); +bool AdBlockCustomList::removeFilter(const QString &filter) { + for (int i = 0; i < m_rules.size(); i++) { + const AdBlockRule *rule = m_rules.at(i); if (rule->filter() == filter) { return removeRule(i); @@ -422,10 +354,8 @@ bool AdBlockCustomList::removeFilter(const QString &filter) return false; } -int AdBlockCustomList::addRule(AdBlockRule* rule) -{ +int AdBlockCustomList::addRule(AdBlockRule *rule) { m_rules.append(rule); - emit subscriptionChanged(); // TODO @@ -433,20 +363,18 @@ int AdBlockCustomList::addRule(AdBlockRule* rule) if (rule->isCssRule()) mainApp->reloadUserStyleBrowser(); */ - return m_rules.count() - 1; + return m_rules.size() - 1; } -bool AdBlockCustomList::removeRule(int offset) -{ - if (!(offset >= 0 && m_rules.count() > offset)) { +bool AdBlockCustomList::removeRule(int offset) { + if (!(offset >= 0 && m_rules.size() > offset)) { return false; } - AdBlockRule* rule = m_rules.at(offset); + AdBlockRule *rule = m_rules.at(offset); const QString filter = rule->filter(); m_rules.remove(offset); - emit subscriptionChanged(); // TODO /* @@ -455,20 +383,17 @@ bool AdBlockCustomList::removeRule(int offset) */ AdBlockManager::instance()->removeDisabledRule(filter); - delete rule; return true; } -const AdBlockRule* AdBlockCustomList::replaceRule(AdBlockRule* rule, int offset) -{ - if (!(offset >= 0 && m_rules.count() > offset)) { - return 0; +const AdBlockRule *AdBlockCustomList::replaceRule(AdBlockRule *rule, int offset) { + if (!(offset >= 0 && m_rules.size() > offset)) { + return NULL; } - AdBlockRule* oldRule = m_rules.at(offset); + AdBlockRule *oldRule = m_rules.at(offset); m_rules[offset] = rule; - emit subscriptionChanged(); // TODO diff --git a/src/network-web/adblock/adblocksubscription.h b/src/network-web/adblock/adblocksubscription.h index 4772af3bd..f861621e7 100755 --- a/src/network-web/adblock/adblocksubscription.h +++ b/src/network-web/adblock/adblocksubscription.h @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + /** * Copyright (c) 2009, Benjamin C. Meyer * @@ -63,24 +47,26 @@ #ifndef ADBLOCKSUBSCRIPTION_H #define ADBLOCKSUBSCRIPTION_H +#include + #include #include -#include "adblockrule.h" -#include "adblocksearchtree.h" +#include "network-web/adblock/adblockrule.h" +#include "network-web/adblock/adblocksearchtree.h" + class QNetworkRequest; class QNetworkReply; class QUrl; - class FollowRedirectReply; -class AdBlockSubscription : public QObject -{ +class AdBlockSubscription : public QObject { Q_OBJECT + public: - explicit AdBlockSubscription(const QString &title, QObject* parent = 0); - ~AdBlockSubscription(); + explicit AdBlockSubscription(const QString &title, QObject *parent = 0); + virtual ~AdBlockSubscription(); QString title() const; void setTitle(const QString &title); @@ -91,21 +77,21 @@ class AdBlockSubscription : public QObject QUrl url() const; void setUrl(const QUrl &url); - virtual void loadSubscription(const QStringList &disabledRules); + virtual void loadSubscription(const QStringList &disabled_rules); virtual void saveSubscription(); - const AdBlockRule* rule(int offset) const; + const AdBlockRule *rule(int offset) const; QVector allRules() const; - const AdBlockRule* enableRule(int offset); - const AdBlockRule* disableRule(int offset); + const AdBlockRule *enableRule(int offset); + const AdBlockRule *disableRule(int offset); virtual bool canEditRules() const; virtual bool canBeRemoved() const; - virtual int addRule(AdBlockRule* rule); + virtual int addRule(AdBlockRule *rule); virtual bool removeRule(int offset); - virtual const AdBlockRule* replaceRule(AdBlockRule* rule, int offset); + virtual const AdBlockRule *replaceRule(AdBlockRule *rule, int offset); public slots: void updateSubscription(); @@ -121,25 +107,23 @@ class AdBlockSubscription : public QObject protected: virtual bool saveDownloadedData(const QByteArray &data); - FollowRedirectReply* m_reply; - QVector m_rules; - - private: QString m_title; QString m_filePath; + private: + FollowRedirectReply *m_reply; QUrl m_url; bool m_updated; }; -class AdBlockCustomList : public AdBlockSubscription -{ +class AdBlockCustomList : public AdBlockSubscription { Q_OBJECT + public: explicit AdBlockCustomList(QObject* parent = 0); + virtual ~AdBlockCustomList(); - void loadSubscription(const QStringList &disabledRules); void saveSubscription(); bool canEditRules() const; @@ -148,9 +132,9 @@ class AdBlockCustomList : public AdBlockSubscription bool containsFilter(const QString &filter) const; bool removeFilter(const QString &filter); - int addRule(AdBlockRule* rule); + int addRule(AdBlockRule *rule); bool removeRule(int offset); - const AdBlockRule* replaceRule(AdBlockRule* rule, int offset); + const AdBlockRule *replaceRule(AdBlockRule *rule, int offset); }; #endif // ADBLOCKSUBSCRIPTION_H diff --git a/src/network-web/adblock/adblocktreewidget.cpp b/src/network-web/adblock/adblocktreewidget.cpp index 02b8a39a4..0104dfbe6 100755 --- a/src/network-web/adblock/adblocktreewidget.cpp +++ b/src/network-web/adblock/adblocktreewidget.cpp @@ -1,39 +1,24 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 "adblocktreewidget.h" -#include "adblocksubscription.h" +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + +#include "network-web/adblock/adblocktreewidget.h" + +#include "network-web/adblock/adblocksubscription.h" #include #include @@ -41,13 +26,10 @@ #include #include -AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent) - : QTreeWidget(parent) - , m_subscription(subscription) - , m_topItem(0) - , m_itemChangingBlock(false) - , m_refreshAllItemsNeeded(true) -{ + +AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent) + : QTreeWidget(parent), m_subscription(subscription), m_topItem(NULL), + m_itemChangingBlock(false), m_refreshAllItemsNeeded(true) { setContextMenuPolicy(Qt::CustomContextMenu); setHeaderHidden(true); setAlternatingRowColors(true); @@ -59,20 +41,20 @@ AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* connect(m_subscription, SIGNAL(subscriptionError(QString)), this, SLOT(subscriptionError(QString))); } -AdBlockSubscription* AdBlockTreeWidget::subscription() const -{ +AdBlockSubscription *AdBlockTreeWidget::subscription() const { return m_subscription; } -void AdBlockTreeWidget::showRule(const AdBlockRule* rule) -{ - if (!m_topItem && rule) { +void AdBlockTreeWidget::showRule(const AdBlockRule *rule) { + if (m_topItem == NULL && rule) { + // Dialog is not loaded yet. Mark rule for late loading. m_ruleToBeSelected = rule->filter(); } else if (!m_ruleToBeSelected.isEmpty()) { QList items = findItems(m_ruleToBeSelected, Qt::MatchRecursive); + if (!items.isEmpty()) { - QTreeWidgetItem* item = items.at(0); + QTreeWidgetItem *item = items.at(0); setCurrentItem(item); scrollToItem(item, QAbstractItemView::PositionAtCenter); @@ -82,58 +64,56 @@ void AdBlockTreeWidget::showRule(const AdBlockRule* rule) } } -void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos) -{ +void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos) { if (!m_subscription->canEditRules()) { return; } - QTreeWidgetItem* item = itemAt(pos); - if (!item) { - return; + QTreeWidgetItem *item = itemAt(pos); + + if (item != NULL) { + QMenu menu; + menu.addAction(tr("Add Rule"), this, SLOT(addRule())); + menu.addSeparator(); + + QAction *delete_action = menu.addAction(tr("Remove Rule"), this, SLOT(removeRule())); + + if (item->parent() == NULL) { + delete_action->setDisabled(true); + } + + menu.exec(viewport()->mapToGlobal(pos)); } - - QMenu menu; - menu.addAction(tr("Add Rule"), this, SLOT(addRule())); - menu.addSeparator(); - QAction* deleteAction = menu.addAction(tr("Remove Rule"), this, SLOT(removeRule())); - - if (!item->parent()) { - deleteAction->setDisabled(true); - } - - menu.exec(viewport()->mapToGlobal(pos)); } -void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item) -{ +void AdBlockTreeWidget::itemChanged(QTreeWidgetItem *item) { m_refreshAllItemsNeeded = true; - if (!item || m_itemChangingBlock) { + if (item == NULL || m_itemChangingBlock) { return; } m_itemChangingBlock = true; int offset = item->data(0, Qt::UserRole + 10).toInt(); - const AdBlockRule* oldRule = m_subscription->rule(offset); + const AdBlockRule *old_rle = m_subscription->rule(offset); - if (item->checkState(0) == Qt::Unchecked && oldRule->isEnabled()) { - // Disable rule - const AdBlockRule* rule = m_subscription->disableRule(offset); + if (item->checkState(0) == Qt::Unchecked && old_rle->isEnabled()) { + // Disable rule. + const AdBlockRule *rule = m_subscription->disableRule(offset); adjustItemFeatures(item, rule); } - else if (item->checkState(0) == Qt::Checked && !oldRule->isEnabled()) { + else if (item->checkState(0) == Qt::Checked && !old_rle->isEnabled()) { // Enable rule - const AdBlockRule* rule = m_subscription->enableRule(offset); + const AdBlockRule *rule = m_subscription->enableRule(offset); adjustItemFeatures(item, rule); } else if (m_subscription->canEditRules()) { // Custom rule has been changed - AdBlockRule* newRule = new AdBlockRule(item->text(0), m_subscription); - const AdBlockRule* rule = m_subscription->replaceRule(newRule, offset); + AdBlockRule *new_rule = new AdBlockRule(item->text(0), m_subscription); + const AdBlockRule *rule = m_subscription->replaceRule(new_rule, offset); adjustItemFeatures(item, rule); } @@ -141,32 +121,29 @@ void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item) m_itemChangingBlock = false; } -void AdBlockTreeWidget::copyFilter() -{ - QTreeWidgetItem* item = currentItem(); - if (!item) { - return; - } +void AdBlockTreeWidget::copyFilter() { + QTreeWidgetItem *item = currentItem(); - QApplication::clipboard()->setText(item->text(0)); + if (item != NULL) { + QApplication::clipboard()->setText(item->text(0)); + } } -void AdBlockTreeWidget::addRule() -{ +void AdBlockTreeWidget::addRule() { if (!m_subscription->canEditRules()) { return; } - QString newRule = QInputDialog::getText(this, tr("Add Custom Rule"), tr("Please write your rule here:")); - if (newRule.isEmpty()) { + QString new_rule = QInputDialog::getText(this, tr("Add Rule"), tr("Please write your rule here")); + if (new_rule.isEmpty()) { return; } - AdBlockRule* rule = new AdBlockRule(newRule, m_subscription); + AdBlockRule *rule = new AdBlockRule(new_rule, m_subscription); int offset = m_subscription->addRule(rule); - QTreeWidgetItem* item = new QTreeWidgetItem(); - item->setText(0, newRule); + QTreeWidgetItem *item = new QTreeWidgetItem(); + item->setText(0, new_rule); item->setData(0, Qt::UserRole + 10, offset); item->setFlags(item->flags() | Qt::ItemIsEditable); @@ -177,21 +154,18 @@ void AdBlockTreeWidget::addRule() adjustItemFeatures(item, rule); } -void AdBlockTreeWidget::removeRule() -{ - QTreeWidgetItem* item = currentItem(); - if (!item || !m_subscription->canEditRules() || item == m_topItem) { +void AdBlockTreeWidget::removeRule() { + QTreeWidgetItem *item = currentItem(); + if (item == NULL || !m_subscription->canEditRules() || item == m_topItem) { return; } int offset = item->data(0, Qt::UserRole + 10).toInt(); - m_subscription->removeRule(offset); delete item; } -void AdBlockTreeWidget::subscriptionUpdated() -{ +void AdBlockTreeWidget::subscriptionUpdated() { refresh(); m_itemChangingBlock = true; @@ -199,17 +173,15 @@ void AdBlockTreeWidget::subscriptionUpdated() m_itemChangingBlock = false; } -void AdBlockTreeWidget::subscriptionError(const QString &message) -{ +void AdBlockTreeWidget::subscriptionError(const QString &message) { refresh(); m_itemChangingBlock = true; - m_topItem->setText(0, tr("%1 (Error: %2)").arg(m_subscription->title(), message)); + m_topItem->setText(0, tr("%1 (error: %2)").arg(m_subscription->title(), message)); m_itemChangingBlock = false; } -void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule) -{ +void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem *item, const AdBlockRule *rule) { if (!rule->isEnabled()) { QFont font; font.setItalic(true); @@ -241,8 +213,7 @@ void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockR } } -void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) -{ +void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) { copyFilter(); } @@ -254,8 +225,7 @@ void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) QTreeWidget::keyPressEvent(event); } -void AdBlockTreeWidget::refresh() -{ +void AdBlockTreeWidget::refresh() { m_itemChangingBlock = true; clear(); @@ -269,10 +239,10 @@ void AdBlockTreeWidget::refresh() addTopLevelItem(m_topItem); const QVector &allRules = m_subscription->allRules(); - int index = 0; - foreach (const AdBlockRule* rule, allRules) { - QTreeWidgetItem* item = new QTreeWidgetItem(m_topItem); + + foreach (const AdBlockRule *rule, allRules) { + QTreeWidgetItem *item = new QTreeWidgetItem(m_topItem); item->setText(0, rule->filter()); item->setData(0, Qt::UserRole + 10, index); @@ -281,31 +251,29 @@ void AdBlockTreeWidget::refresh() } adjustItemFeatures(item, rule); - ++index; + index++; } showRule(0); m_itemChangingBlock = false; } -void AdBlockTreeWidget::clear() -{ +void AdBlockTreeWidget::clear() { QTreeWidget::clear(); m_allTreeItems.clear(); } -void AdBlockTreeWidget::addTopLevelItem(QTreeWidgetItem* item) -{ +void AdBlockTreeWidget::addTopLevelItem(QTreeWidgetItem *item) { m_allTreeItems.append(item); QTreeWidget::addTopLevelItem(item); } -void AdBlockTreeWidget::iterateAllItems(QTreeWidgetItem* parent) +void AdBlockTreeWidget::iterateAllItems(QTreeWidgetItem *parent) { int count = parent ? parent->childCount() : topLevelItemCount(); for (int i = 0; i < count; i++) { - QTreeWidgetItem* item = parent ? parent->child(i) : topLevelItem(i); + QTreeWidgetItem *item = parent ? parent->child(i) : topLevelItem(i); if (item->childCount() == 0) { m_allTreeItems.append(item); @@ -315,8 +283,7 @@ void AdBlockTreeWidget::iterateAllItems(QTreeWidgetItem* parent) } } -QList AdBlockTreeWidget::allItems() -{ +QList AdBlockTreeWidget::allItems() { if (m_refreshAllItemsNeeded) { m_allTreeItems.clear(); iterateAllItems(0); @@ -326,16 +293,18 @@ QList AdBlockTreeWidget::allItems() return m_allTreeItems; } -void AdBlockTreeWidget::filterString(const QString &string) -{ - QList _allItems = allItems(); +void AdBlockTreeWidget::filterString(const QString &string) { + QList all_items = allItems(); QList parents; - bool stringIsEmpty = string.isEmpty(); - foreach (QTreeWidgetItem* item, _allItems) { - bool containsString = stringIsEmpty || item->text(0).contains(string, Qt::CaseInsensitive); - if (containsString) { + bool string_empty = string.isEmpty(); + + foreach (QTreeWidgetItem *item, all_items) { + bool contains_string = string_empty || item->text(0).contains(string, Qt::CaseInsensitive); + + if (contains_string) { item->setHidden(false); - if (item->parent()) { + + if (item->parent() != NULL) { if (!parents.contains(item->parent())) { parents << item->parent(); } @@ -343,18 +312,19 @@ void AdBlockTreeWidget::filterString(const QString &string) } else { item->setHidden(true); - if (item->parent()) { + + if (item->parent() != NULL) { item->parent()->setHidden(true); } } } - for (int i = 0; i < parents.size(); ++i) { - QTreeWidgetItem* parentItem = parents.at(i); + for (int i = 0; i < parents.size(); i++) { + QTreeWidgetItem *parentItem = parents.at(i); parentItem->setHidden(false); parentItem->setExpanded(true); - if (parentItem->parent() && !parents.contains(parentItem->parent())) { + if (parentItem->parent() != NULL && !parents.contains(parentItem->parent())) { parents << parentItem->parent(); } } diff --git a/src/network-web/adblock/adblocktreewidget.h b/src/network-web/adblock/adblocktreewidget.h index ea0c2e96e..3c927287b 100755 --- a/src/network-web/adblock/adblocktreewidget.h +++ b/src/network-web/adblock/adblocktreewidget.h @@ -1,86 +1,72 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + #ifndef ADBLOCKTREEWIDGET_H #define ADBLOCKTREEWIDGET_H #include + class AdBlockSubscription; class AdBlockRule; -class AdBlockTreeWidget : public QTreeWidget -{ - Q_OBJECT -public: - explicit AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent = 0); +class AdBlockTreeWidget : public QTreeWidget { + Q_OBJECT - AdBlockSubscription* subscription() const; + public: + explicit AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent = 0); - void showRule(const AdBlockRule* rule); - void refresh(); + AdBlockSubscription *subscription() const; -public slots: - void addRule(); - void removeRule(); - void filterString(const QString &string); - void clear(); + void showRule(const AdBlockRule *rule); + void refresh(); -private slots: - void contextMenuRequested(const QPoint &pos); - void itemChanged(QTreeWidgetItem* item); - void copyFilter(); + public slots: + void addRule(); + void removeRule(); + void filterString(const QString &string); + void clear(); - void subscriptionUpdated(); - void subscriptionError(const QString &message); + private slots: + void contextMenuRequested(const QPoint &pos); + void itemChanged(QTreeWidgetItem *item); + void copyFilter(); -private: - void adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule); - void keyPressEvent(QKeyEvent* event); - void addTopLevelItem(QTreeWidgetItem* item); - QList allItems(); - void iterateAllItems(QTreeWidgetItem* parent); + void subscriptionUpdated(); + void subscriptionError(const QString &message); - AdBlockSubscription* m_subscription; - QTreeWidgetItem* m_topItem; + protected: + void keyPressEvent(QKeyEvent *event); - QString m_ruleToBeSelected; - bool m_itemChangingBlock; + private: + void adjustItemFeatures(QTreeWidgetItem *item, const AdBlockRule *rule); + void addTopLevelItem(QTreeWidgetItem *item); + QList allItems(); + void iterateAllItems(QTreeWidgetItem *parent); - bool m_refreshAllItemsNeeded; - QList m_allTreeItems; + AdBlockSubscription *m_subscription; + QTreeWidgetItem *m_topItem; + QString m_ruleToBeSelected; + bool m_itemChangingBlock; + + bool m_refreshAllItemsNeeded; + QList m_allTreeItems; }; #endif // ADBLOCKTREEWIDGET_H diff --git a/src/network-web/adblock/followredirectreply.cpp b/src/network-web/adblock/followredirectreply.cpp index 7c41902b0..c46a8e352 100755 --- a/src/network-web/adblock/followredirectreply.cpp +++ b/src/network-web/adblock/followredirectreply.cpp @@ -1,96 +1,71 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 "followredirectreply.h" +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . + +#include "network-web/adblock/followredirectreply.h" #include -FollowRedirectReply::FollowRedirectReply(const QUrl &url, QNetworkAccessManager* manager) - : QObject() - , m_manager(manager) - , m_redirectCount(0) -{ + +FollowRedirectReply::FollowRedirectReply(const QUrl &url, QNetworkAccessManager *manager) + : QObject(), m_manager(manager), m_redirectCount(0) { m_reply = m_manager->get(QNetworkRequest(url)); connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished())); } -QNetworkReply* FollowRedirectReply::reply() const -{ +QNetworkReply *FollowRedirectReply::reply() const { return m_reply; } -QUrl FollowRedirectReply::originalUrl() const -{ +QUrl FollowRedirectReply::originalUrl() const { return m_reply->request().url(); } -QUrl FollowRedirectReply::url() const -{ +QUrl FollowRedirectReply::url() const { return m_reply->url(); } -QNetworkReply::NetworkError FollowRedirectReply::error() const -{ +QNetworkReply::NetworkError FollowRedirectReply::error() const { return m_reply->error(); } -QByteArray FollowRedirectReply::readAll() -{ +QByteArray FollowRedirectReply::readAll() { return m_reply->readAll(); } -void FollowRedirectReply::replyFinished() -{ - int replyStatus = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); +void FollowRedirectReply::replyFinished() { + int reply_status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if ((replyStatus != 301 && replyStatus != 302) || m_redirectCount == 5) { + if ((reply_status != 301 && reply_status != 302) || m_redirectCount == 5) { emit finished(); return; } m_redirectCount++; - QUrl redirectUrl = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); + QUrl redirect_url = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); m_reply->close(); m_reply->deleteLater(); - m_reply = m_manager->get(QNetworkRequest(redirectUrl)); + m_reply = m_manager->get(QNetworkRequest(redirect_url)); connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished())); } -FollowRedirectReply::~FollowRedirectReply() -{ +FollowRedirectReply::~FollowRedirectReply() { m_reply->close(); m_reply->deleteLater(); } diff --git a/src/network-web/adblock/followredirectreply.h b/src/network-web/adblock/followredirectreply.h index 636b272ea..f48c91799 100755 --- a/src/network-web/adblock/followredirectreply.h +++ b/src/network-web/adblock/followredirectreply.h @@ -1,73 +1,57 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* 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 . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* 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 . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// RSS Guard 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. +// +// RSS Guard 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 RSS Guard. If not, see . #ifndef FOLLOWREDIRECTREPLY_H #define FOLLOWREDIRECTREPLY_H #include + #include + class QNetworkAccessManager; class QNetworkReply; class QUrl; -class FollowRedirectReply : public QObject -{ - Q_OBJECT -public: - explicit FollowRedirectReply(const QUrl &url, QNetworkAccessManager* manager); - ~FollowRedirectReply(); +class FollowRedirectReply : public QObject { + Q_OBJECT - QNetworkReply* reply() const; - QUrl originalUrl() const; - QUrl url() const; + public: + explicit FollowRedirectReply(const QUrl &url, QNetworkAccessManager* manager); + virtual ~FollowRedirectReply(); - QNetworkReply::NetworkError error() const; - QByteArray readAll(); + QNetworkReply *reply() const; + QUrl originalUrl() const; + QUrl url() const; -signals: - void finished(); + QNetworkReply::NetworkError error() const; + QByteArray readAll(); -private slots: - void replyFinished(); + signals: + void finished(); -private: - QNetworkAccessManager* m_manager; - QNetworkReply* m_reply; - int m_redirectCount; + private slots: + void replyFinished(); + private: + QNetworkAccessManager *m_manager; + QNetworkReply *m_reply; + int m_redirectCount; }; #endif // FOLLOWREDIRECTREPLY_H diff --git a/src/network-web/downloader.cpp b/src/network-web/downloader.cpp index a36c9fa68..ec41afcf4 100755 --- a/src/network-web/downloader.cpp +++ b/src/network-web/downloader.cpp @@ -23,7 +23,7 @@ Downloader::Downloader(QObject *parent) - : QObject(parent), m_activeReply(NULL), m_downloadManager(SilentNetworkAccessManager::instance()), + : QObject(parent), m_activeReply(NULL), m_downloadManager(new SilentNetworkAccessManager(this)), m_timer(new QTimer(this)), m_customHeaders(QHash()), m_lastOutputData(QByteArray()), m_lastOutputError(QNetworkReply::NoError), m_lastContentType(QVariant()) { @@ -34,7 +34,7 @@ Downloader::Downloader(QObject *parent) } Downloader::~Downloader() { - //m_downloadManager->deleteLater(); + m_downloadManager->deleteLater(); } void Downloader::downloadFile(const QString &url, int timeout, bool protected_contents, const QString &username, const QString &password) { diff --git a/src/network-web/webfactory.cpp b/src/network-web/webfactory.cpp index 1d6ce6ae8..5830fe9df 100755 --- a/src/network-web/webfactory.cpp +++ b/src/network-web/webfactory.cpp @@ -135,6 +135,41 @@ QString WebFactory::deEscapeHtml(const QString &text) { return output; } +QString WebFactory::toSecondLevelDomain(const QUrl &url) { +#if QT_VERSION >= 0x040800 + const QString top_level_domain = url.topLevelDomain(); + const QString url_host = url.host(); + + if (top_level_domain.isEmpty() || url_host.isEmpty()) { + return QString(); + } + + QString domain = url_host.left(url_host.size() - top_level_domain.size()); + + if (domain.count(QL1C('.')) == 0) { + return url_host; + } + + while (domain.count(QL1C('.')) != 0) { + domain = domain.mid(domain.indexOf(QL1C('.')) + 1); + } + + return domain + top_level_domain; +#else + QString domain = url.host(); + + if (domain.count(QL1C('.')) == 0) { + return QString(); + } + + while (domain.count(QL1C('.')) != 1) { + domain = domain.mid(domain.indexOf(QL1C('.')) + 1); + } + + return domain; +#endif +} + void WebFactory::generetaEscapes() { m_escapes["<"] = '<'; m_escapes[">"] = '>'; diff --git a/src/network-web/webfactory.h b/src/network-web/webfactory.h old mode 100644 new mode 100755 index 85ff372c6..b7a24dda0 --- a/src/network-web/webfactory.h +++ b/src/network-web/webfactory.h @@ -30,6 +30,10 @@ class WebFactory : public QObject { QString escapeHtml(const QString &html); QString deEscapeHtml(const QString &text); + // BUG: Version for Qt < 4.8 has one issue, it will wrongly + // count .co.uk (and others) as second-level domain + QString toSecondLevelDomain(const QUrl &url); + // Switchers. bool javascriptEnabled() const; bool pluginsEnabled() const;