summaryrefslogtreecommitdiff
path: root/src/adblock/adblockmanager.cpp
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2012-03-06 10:56:47 +0100
committerAndrea Diamantini <adjam7@gmail.com>2012-03-13 10:24:42 +0100
commit5a7a48e488645bebd09eb51fd9236dc734f29fd4 (patch)
tree3e8c34c3bb22779554c4972a3eee08595bd8ae4f /src/adblock/adblockmanager.cpp
parentupdate adblockrc (diff)
downloadrekonq-5a7a48e488645bebd09eb51fd9236dc734f29fd4.tar.xz
Clean up adblock manager code and fix unblock feature
Diffstat (limited to 'src/adblock/adblockmanager.cpp')
-rw-r--r--src/adblock/adblockmanager.cpp95
1 files changed, 52 insertions, 43 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();
}