diff options
author | Andrea Diamantini <adjam7@gmail.com> | 2012-03-06 10:56:47 +0100 |
---|---|---|
committer | Andrea Diamantini <adjam7@gmail.com> | 2012-03-13 10:24:42 +0100 |
commit | 5a7a48e488645bebd09eb51fd9236dc734f29fd4 (patch) | |
tree | 3e8c34c3bb22779554c4972a3eee08595bd8ae4f /src/adblock | |
parent | update adblockrc (diff) | |
download | rekonq-5a7a48e488645bebd09eb51fd9236dc734f29fd4.tar.xz |
Clean up adblock manager code and fix unblock feature
Diffstat (limited to 'src/adblock')
-rw-r--r-- | src/adblock/adblockmanager.cpp | 95 | ||||
-rw-r--r-- | src/adblock/adblockmanager.h | 8 | ||||
-rw-r--r-- | src/adblock/blockedelementswidget.cpp | 5 | ||||
-rw-r--r-- | src/adblock/blockedelementswidget.h | 3 |
4 files changed, 65 insertions, 46 deletions
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<KDialog> 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(); } diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h index 469f87cb..9b125737 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -160,14 +160,18 @@ public: QNetworkReply *block(const QNetworkRequest &request, WebPage *page); void applyHidingRules(WebPage *page); - void addCustomRule(const QString &); + void addCustomRule(const QString &, bool reloadPage = true); void clearElementsLists(); private: void updateSubscription(int); bool subscriptionFileExists(int); - void loadRules(const QString &); + // load a file rule, given a path + void loadRules(const QString &rulesFilePath); + + // load a single rule + void loadRuleString(const QString &stringRule); private Q_SLOTS: void loadSettings(); diff --git a/src/adblock/blockedelementswidget.cpp b/src/adblock/blockedelementswidget.cpp index cdb40c27..58b96299 100644 --- a/src/adblock/blockedelementswidget.cpp +++ b/src/adblock/blockedelementswidget.cpp @@ -40,6 +40,7 @@ BlockedElementsWidget::BlockedElementsWidget(QObject *manager, QWidget *parent) : QWidget(parent) , _manager(manager) + , _reloadPage(false) { setupUi(this); } @@ -102,5 +103,7 @@ void BlockedElementsWidget::unblockElement() buttonClicked->setIcon(KIcon("dialog-ok")); AdBlockManager *m = qobject_cast<AdBlockManager *>(_manager); - m->addCustomRule("@@" + urlString); + m->addCustomRule(QL1S("@@") + urlString, false); + + _reloadPage = true; } diff --git a/src/adblock/blockedelementswidget.h b/src/adblock/blockedelementswidget.h index 6f5ceaf8..d1b0b387 100644 --- a/src/adblock/blockedelementswidget.h +++ b/src/adblock/blockedelementswidget.h @@ -48,11 +48,14 @@ public: void setBlockedElements(const QStringList &); void setHidedElements(int); + bool pageNeedsReload() { return _reloadPage; }; + private Q_SLOTS: void unblockElement(); private: QObject *_manager; + bool _reloadPage; }; #endif // BLOCKED_ELEMENTS_WIDGET_H |