From 5a7a48e488645bebd09eb51fd9236dc734f29fd4 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Tue, 6 Mar 2012 10:56:47 +0100 Subject: Clean up adblock manager code and fix unblock feature --- src/adblock/adblockmanager.cpp | 95 +++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 43 deletions(-) (limited to 'src/adblock/adblockmanager.cpp') diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index 9777bb94..39040767 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -101,8 +101,6 @@ void AdBlockManager::loadSettings() _blackList.clear(); _hideList.clear(); - clearElementsLists(); - KConfigGroup settingsGroup(_adblockConfig, "Settings"); _isAdblockEnabled = settingsGroup.readEntry("adBlockEnabled", false); kDebug() << "ADBLOCK ENABLED = " << _isAdblockEnabled; @@ -170,52 +168,57 @@ void AdBlockManager::loadRules(const QString &rulesFilePath) while (!in.atEnd()) { QString stringRule = in.readLine(); + loadRuleString(stringRule); + } +} - // ! rules are comments - if (stringRule.startsWith('!')) - continue; - // [ rules are ABP info - if (stringRule.startsWith('[')) - continue; +void AdBlockManager::loadRuleString(const QString &stringRule) +{ + // ! rules are comments + if (stringRule.startsWith('!')) + return; - // empty rules are just dangerous.. - // (an empty rule in whitelist allows all, in blacklist blocks all..) - if (stringRule.isEmpty()) - continue; + // [ rules are ABP info + if (stringRule.startsWith('[')) + return; - // white rules - if (stringRule.startsWith(QL1S("@@"))) - { - const QString filter = stringRule.mid(2); - if (_hostWhiteList.tryAddFilter(filter)) - continue; + // empty rules are just dangerous.. + // (an empty rule in whitelist allows all, in blacklist blocks all..) + if (stringRule.isEmpty()) + return; - AdBlockRule rule(filter); - _whiteList << rule; - continue; - } + // white rules + if (stringRule.startsWith(QL1S("@@"))) + { + const QString filter = stringRule.mid(2); + if (_hostWhiteList.tryAddFilter(filter)) + return; - // hide (CSS) rules - if (stringRule.startsWith(QL1S("##"))) - { - _hideList << stringRule.mid(2); - continue; - } + AdBlockRule rule(filter); + _whiteList << rule; + return; + } - // TODO implement domain-specific hiding - if (stringRule.contains(QL1S("##"))) - continue; + // hide (CSS) rules + if (stringRule.startsWith(QL1S("##"))) + { + _hideList << stringRule.mid(2); + return; + } - if (_hostBlackList.tryAddFilter(stringRule)) - continue; + // TODO implement domain-specific hiding + if (stringRule.contains(QL1S("##"))) + return; - AdBlockRule rule(stringRule); - _blackList << rule; - } -} + if (_hostBlackList.tryAddFilter(stringRule)) + return; + AdBlockRule rule(stringRule); + _blackList << rule; +} + QNetworkReply *AdBlockManager::block(const QNetworkRequest &request, WebPage *page) { if (!_isAdblockEnabled) @@ -383,13 +386,13 @@ void AdBlockManager::showSettings() } -void AdBlockManager::addCustomRule(const QString &stringRule) +void AdBlockManager::addCustomRule(const QString &stringRule, bool reloadPage) { // save rule in local filters QString localRulesFilePath = KStandardDirs::locateLocal("appdata" , QL1S("adblockrules_local")); QFile ruleFile(localRulesFilePath); - if (!ruleFile.open(QFile::WriteOnly | QFile::Text)) + if (!ruleFile.open(QFile::WriteOnly | QFile::Append)) { kDebug() << "Unable to open rule file" << localRulesFilePath; return; @@ -398,18 +401,21 @@ void AdBlockManager::addCustomRule(const QString &stringRule) QTextStream out(&ruleFile); out << stringRule << '\n'; + ruleFile.close(); + // load it - AdBlockRule rule(stringRule); - _blackList << rule; + loadRuleString(stringRule); - emit reloadCurrentPage(); + // eventually reload page + if (reloadPage) + emit reloadCurrentPage(); } void AdBlockManager::showBlockedItemDialog() { QPointer dialog = new KDialog(); - dialog->setCaption(i18nc("@title:window", "Blocked & hided elements")); + dialog->setCaption(i18nc("@title:window", "Blocked elements")); dialog->setButtons(KDialog::Ok); BlockedElementsWidget widget(this); @@ -419,6 +425,9 @@ void AdBlockManager::showBlockedItemDialog() dialog->setMainWidget(&widget); dialog->exec(); + if (widget.pageNeedsReload()) + emit reloadCurrentPage(); + dialog->deleteLater(); } -- cgit v1.2.1