summaryrefslogtreecommitdiff
path: root/src/adblock
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2012-02-13 12:03:23 +0100
committerAndrea Diamantini <adjam7@gmail.com>2012-03-13 10:24:41 +0100
commitfdb6bf971a3f3e0edea840cd92d18374b494aacb (patch)
tree5a106ef755c56b2dd8bd696d4f63552d135b7790 /src/adblock
parentRemove ability to manage abp schemes and add subscriptions on click (diff)
downloadrekonq-fdb6bf971a3f3e0edea840cd92d18374b494aacb.tar.xz
Move adblock manager to use multiple text files for rules and
separate adblock settings from rekonq ones. We now have 60 adblock rules list sites stored to be enabled "on click"
Diffstat (limited to 'src/adblock')
-rw-r--r--src/adblock/adblockmanager.cpp164
-rw-r--r--src/adblock/adblockmanager.h13
-rw-r--r--src/adblock/adblockwidget.cpp66
3 files changed, 108 insertions, 135 deletions
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp
index e48cc753..ff3aad6a 100644
--- a/src/adblock/adblockmanager.cpp
+++ b/src/adblock/adblockmanager.cpp
@@ -37,9 +37,8 @@
#include "webpage.h"
// KDE Includes
-#include <KSharedConfig>
-#include <KConfigGroup>
-#include <KIO/TransferJob>
+#include <KIO/FileCopyJob>
+#include <KStandardDirs>
// Qt Includes
#include <QUrl>
@@ -50,7 +49,6 @@ AdBlockManager::AdBlockManager(QObject *parent)
: QObject(parent)
, _isAdblockEnabled(false)
, _isHideAdsEnabled(false)
- , _index(0)
{
loadSettings();
}
@@ -66,8 +64,21 @@ AdBlockManager::~AdBlockManager()
void AdBlockManager::loadSettings()
{
- _index = 0;
- _buffer.clear();
+ // first, check this...
+ QString adblockFilePath = KStandardDirs::locateLocal("appdata" , QL1S("adblockrc"));
+ if (!QFile::exists(adblockFilePath))
+ {
+ QString generalAdblockFilePath = KStandardDirs::locate("appdata" , QL1S("adblockrc"));
+ QFile adblockFile(generalAdblockFilePath);
+ bool copied = adblockFile.copy(adblockFilePath);
+ if (!copied)
+ {
+ kDebug() << "oh oh... Problems copying default adblock file";
+ return;
+ }
+ }
+ _adblockConfig = KSharedConfig::openConfig("adblockrc", KConfig::SimpleConfig, "appdata");
+ // ----------------
_hostWhiteList.clear();
_hostBlackList.clear();
@@ -75,7 +86,8 @@ void AdBlockManager::loadSettings()
_blackList.clear();
_hideList.clear();
- _isAdblockEnabled = ReKonfig::adBlockEnabled();
+ KConfigGroup settingsGroup(_adblockConfig, "Settings");
+ _isAdblockEnabled = settingsGroup.readEntry("adBlockEnabled", false);
kDebug() << "ADBLOCK ENABLED = " << _isAdblockEnabled;
// no need to load filters if adblock is not enabled :)
@@ -83,43 +95,51 @@ void AdBlockManager::loadSettings()
return;
// just to be sure..
- _isHideAdsEnabled = ReKonfig::hideAdsEnabled();
-
- // read settings
- KSharedConfig::Ptr config = KSharedConfig::openConfig("adblock", KConfig::SimpleConfig, "appdata");
- KConfigGroup rulesGroup(config, "rules");
- QStringList rules;
- rules = rulesGroup.readEntry("local-rules" , QStringList());
- loadRules(rules);
+ _isHideAdsEnabled = settingsGroup.readEntry("hideAdsEnabled", false);
// ----------------------------------------------------------
QDateTime today = QDateTime::currentDateTime();
- QDateTime lastUpdate = ReKonfig::lastUpdate(); // the day of the implementation.. :)
- int days = ReKonfig::updateInterval();
+ QDateTime lastUpdate = QDateTime::fromString(settingsGroup.readEntry("lastUpdate", QString()));
+ int days = settingsGroup.readEntry("updateInterval", 7);
if (today > lastUpdate.addDays(days))
{
- ReKonfig::setLastUpdate(today);
+ settingsGroup.writeEntry("lastUpdate", today.toString());
- updateNextSubscription();
+ updateSubscriptions();
return;
}
// else
- QStringList titles = ReKonfig::subscriptionTitles();
- Q_FOREACH(const QString & title, titles)
+ KConfigGroup filtersGroup(_adblockConfig, "FiltersList");
+ for (int i = 1; i <= 60; ++i)
{
- rules = rulesGroup.readEntry(title + "-rules" , QStringList());
- loadRules(rules);
+ QString n = QString::number(i);
+ bool filterActive = filtersGroup.readEntry("FilterEnabled-" + n, false);
+ if (filterActive)
+ {
+ QString rulesFilePath = KStandardDirs::locateLocal("appdata" , QL1S("adblockrules_") + n);
+ loadRules(rulesFilePath);
+ }
}
}
-void AdBlockManager::loadRules(const QStringList &rules)
+void AdBlockManager::loadRules(const QString &rulesFilePath)
{
- Q_FOREACH(const QString & stringRule, rules)
+ QFile ruleFile(rulesFilePath);
+ if (!ruleFile.open(QFile::ReadOnly | QFile::Text))
+ {
+ kDebug() << "Unable to open rule file" << rulesFilePath;
+ return;
+ }
+
+ QTextStream in(&ruleFile);
+ while (!in.atEnd())
{
+ QString stringRule = in.readLine();
+
// ! rules are comments
if (stringRule.startsWith('!'))
continue;
@@ -267,82 +287,38 @@ void AdBlockManager::applyHidingRules(WebPage *page)
}
-void AdBlockManager::updateNextSubscription()
+void AdBlockManager::updateSubscriptions()
{
- QStringList locations = ReKonfig::subscriptionLocations();
-
- if (_index < locations.size())
- {
- QString urlString = locations.at(_index);
- KUrl subUrl = KUrl(urlString);
-
- KIO::TransferJob* job = KIO::get(subUrl , KIO::Reload , KIO::HideProgressInfo);
- job->metaData().insert("ssl_no_client_cert", "TRUE");
- job->metaData().insert("ssl_no_ui", "TRUE");
- job->metaData().insert("UseCache", "false");
- job->metaData().insert("cookies", "none");
- job->metaData().insert("no-auth", "true");
-
- connect(job, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(subscriptionData(KIO::Job*,QByteArray)));
- connect(job, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
-
- return;
- }
-
- _index = 0;
- _buffer.clear();
-}
+ KConfigGroup filtersGroup(_adblockConfig, "FiltersList");
+ int i = 1;
+ QString n = QString::number(i);
-void AdBlockManager::slotResult(KJob *job)
-{
- if (job->error())
- return;
-
- QList<QByteArray> list = _buffer.split('\n');
- QStringList ruleList;
- Q_FOREACH(const QByteArray & ba, list)
+ while (filtersGroup.hasKey("FilterName-" + n))
{
- ruleList << QString(ba);
- }
- loadRules(ruleList);
- saveRules(ruleList);
-
- _index++;
-
- // last..
- updateNextSubscription();
-}
-
-
-void AdBlockManager::subscriptionData(KIO::Job* job, const QByteArray& data)
-{
- Q_UNUSED(job)
-
- if (data.isEmpty())
- return;
-
- int oldSize = _buffer.size();
- _buffer.resize(_buffer.size() + data.size());
- memcpy(_buffer.data() + oldSize, data.data(), data.size());
-}
-
+ bool isFilterEnabled = filtersGroup.readEntry("FilterEnabled-" + n, false);
+ if (isFilterEnabled)
+ {
+ QString fUrl = filtersGroup.readEntry("FilterURL-" + n, QString());
+ KUrl subUrl = KUrl(fUrl);
+
+ kDebug() << "Filter ENABLED: " << fUrl;
+ QString rulesFilePath = KStandardDirs::locateLocal("appdata" , QL1S("adblockrules_") + n);
+ KUrl destUrl = KUrl(rulesFilePath);
+
+ KIO::FileCopyJob* job = KIO::file_copy(subUrl , destUrl, -1, KIO::HideProgressInfo);
+ job->metaData().insert("ssl_no_client_cert", "TRUE");
+ job->metaData().insert("ssl_no_ui", "TRUE");
+ job->metaData().insert("UseCache", "false");
+ job->metaData().insert("cookies", "none");
+ job->metaData().insert("no-auth", "true");
+ }
-void AdBlockManager::saveRules(const QStringList &rules)
-{
- QStringList cleanedRules;
- Q_FOREACH(const QString & r, rules)
- {
- if (!r.startsWith('!') && !r.startsWith('[') && !r.isEmpty())
- cleanedRules << r;
+ i++;
+ n = QString::number(i);
}
- QStringList titles = ReKonfig::subscriptionTitles();
- QString title = titles.at(_index) + "-rules";
-
- KSharedConfig::Ptr config = KSharedConfig::openConfig("adblock", KConfig::SimpleConfig, "appdata");
- KConfigGroup cg(config , "rules");
- cg.writeEntry(title, cleanedRules);
+ loadSettings();
}
diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h
index cce00877..6b7c225e 100644
--- a/src/adblock/adblockmanager.h
+++ b/src/adblock/adblockmanager.h
@@ -130,6 +130,7 @@
// KDE Includes
#include <KIO/Job>
+#include <KSharedConfig>
// Qt Includes
#include <QObject>
@@ -161,13 +162,10 @@ public Q_SLOTS:
void showSettings();
private:
- void updateNextSubscription();
- void saveRules(const QStringList &);
- void loadRules(const QStringList &);
+ void updateSubscriptions();
+ void loadRules(const QString &);
-private Q_SLOTS:
- void slotResult(KJob *);
- void subscriptionData(KIO::Job*, const QByteArray&);
+ KSharedConfig::Ptr config();
private:
bool _isAdblockEnabled;
@@ -179,8 +177,7 @@ private:
AdBlockRuleList _whiteList;
QStringList _hideList;
- int _index;
- QByteArray _buffer;
+ KSharedConfig::Ptr _adblockConfig;
};
#endif
diff --git a/src/adblock/adblockwidget.cpp b/src/adblock/adblockwidget.cpp
index c26e31f4..8e6a81ae 100644
--- a/src/adblock/adblockwidget.cpp
+++ b/src/adblock/adblockwidget.cpp
@@ -108,36 +108,36 @@ void AdBlockWidget::removeRule()
void AdBlockWidget::load()
{
- const bool isAdBlockEnabled = ReKonfig::adBlockEnabled();
- checkEnableAdblock->setChecked(isAdBlockEnabled);
- // update enabled status
- checkHideAds->setEnabled(checkEnableAdblock->isChecked());
- tabWidget->setEnabled(checkEnableAdblock->isChecked());
-
- const bool areImageFiltered = ReKonfig::hideAdsEnabled();
- checkHideAds->setChecked(areImageFiltered);
-
- const int days = ReKonfig::updateInterval();
- spinBox->setValue(days);
-
- const QStringList subscriptions = ReKonfig::subscriptionTitles();
-
- // load automatic rules
- Q_FOREACH(const QString & sub, subscriptions)
- {
- QTreeWidgetItem *subItem = new QTreeWidgetItem(treeWidget);
- subItem->setText(0, sub);
- loadRules(subItem);
- }
-
- // load local rules
- KSharedConfig::Ptr config = KSharedConfig::openConfig("adblock", KConfig::SimpleConfig, "appdata");
- KConfigGroup localGroup(config, "rules");
- const QStringList rules = localGroup.readEntry("local-rules" , QStringList());
- Q_FOREACH(const QString & rule, rules)
- {
- listWidget->addItem(rule);
- }
+// const bool isAdBlockEnabled = ReKonfig::adBlockEnabled();
+// checkEnableAdblock->setChecked(isAdBlockEnabled);
+// // update enabled status
+// checkHideAds->setEnabled(checkEnableAdblock->isChecked());
+// tabWidget->setEnabled(checkEnableAdblock->isChecked());
+//
+// const bool areImageFiltered = ReKonfig::hideAdsEnabled();
+// checkHideAds->setChecked(areImageFiltered);
+//
+// const int days = ReKonfig::updateInterval();
+// spinBox->setValue(days);
+//
+// const QStringList subscriptions = ReKonfig::subscriptionTitles();
+//
+// // load automatic rules
+// Q_FOREACH(const QString & sub, subscriptions)
+// {
+// QTreeWidgetItem *subItem = new QTreeWidgetItem(treeWidget);
+// subItem->setText(0, sub);
+// loadRules(subItem);
+// }
+//
+// // load local rules
+// KSharedConfig::Ptr config = KSharedConfig::openConfig("adblock", KConfig::SimpleConfig, "appdata");
+// KConfigGroup localGroup(config, "rules");
+// const QStringList rules = localGroup.readEntry("local-rules" , QStringList());
+// Q_FOREACH(const QString & rule, rules)
+// {
+// listWidget->addItem(rule);
+// }
}
@@ -176,9 +176,9 @@ void AdBlockWidget::save()
}
localGroup.writeEntry("local-rules" , localRules);
- ReKonfig::setAdBlockEnabled(checkEnableAdblock->isChecked());
- ReKonfig::setHideAdsEnabled(checkHideAds->isChecked());
- ReKonfig::setUpdateInterval(spinBox->value());
+// ReKonfig::setAdBlockEnabled(checkEnableAdblock->isChecked());
+// ReKonfig::setHideAdsEnabled(checkHideAds->isChecked());
+// ReKonfig::setUpdateInterval(spinBox->value());
_changed = false;
emit changed(false);