summaryrefslogtreecommitdiff
path: root/src/adblock
diff options
context:
space:
mode:
authorPanagiotis Papadopoulos <pano_90@gmx.net>2010-04-21 13:34:20 +0200
committerPanagiotis Papadopoulos <pano_90@gmx.net>2010-04-21 13:34:20 +0200
commitf9153663cb8210ac4d6e7055838344839b4e19b6 (patch)
treeb70233b32eadfbebff062c0000f007ca2f99d732 /src/adblock
parentMerge branch 'master' of git://gitorious.org/rekonq/mainline into i18n (diff)
parentNebulon's patch. (diff)
downloadrekonq-f9153663cb8210ac4d6e7055838344839b4e19b6.tar.xz
Merge branch 'master' of git://gitorious.org/rekonq/mainline into i18n
Diffstat (limited to 'src/adblock')
-rw-r--r--src/adblock/adblockmanager.cpp259
-rw-r--r--src/adblock/adblockmanager.h26
-rw-r--r--src/adblock/adblocknetworkreply.cpp4
-rw-r--r--src/adblock/adblocknetworkreply.h7
-rw-r--r--src/adblock/adblockrule.cpp8
-rw-r--r--src/adblock/adblockrule.h7
6 files changed, 249 insertions, 62 deletions
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp
index c2a42f0b..0a139bdc 100644
--- a/src/adblock/adblockmanager.cpp
+++ b/src/adblock/adblockmanager.cpp
@@ -2,7 +2,7 @@
*
* This file is a part of the rekonq project
*
-* Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
+* Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
@@ -28,6 +28,9 @@
#include "adblockmanager.h"
#include "adblockmanager.moc"
+// Auto Includes
+#include "rekonq.h"
+
// Local Includes
#include "adblocknetworkreply.h"
#include "webpage.h"
@@ -36,6 +39,7 @@
#include <KSharedConfig>
#include <KConfigGroup>
#include <KDebug>
+#include <KIO/TransferJob>
// Qt Includes
#include <QUrl>
@@ -46,8 +50,8 @@ AdBlockManager::AdBlockManager(QObject *parent)
: QObject(parent)
, _isAdblockEnabled(false)
, _isHideAdsEnabled(false)
+ , _index(0)
{
- loadSettings();
}
@@ -56,60 +60,95 @@ AdBlockManager::~AdBlockManager()
}
-void AdBlockManager::loadSettings()
+void AdBlockManager::loadSettings(bool checkUpdateDate)
{
- KSharedConfig::Ptr config = KSharedConfig::openConfig("khtmlrc", KConfig::NoGlobals);
- KConfigGroup cg( config, "Filter Settings" );
+ _index = 0;
+ _buffer.clear();
+
+ _whiteList.clear();
+ _blackList.clear();
+ _hideList.clear();
+
+ _isAdblockEnabled = ReKonfig::adBlockEnabled();
+ kDebug() << "ADBLOCK ENABLED = " << _isAdblockEnabled;
+
+ // no need to load filters if adblock is not enabled :)
+ if(!_isAdblockEnabled)
+ return;
+
+ // just to be sure..
+ _isHideAdsEnabled = ReKonfig::hideAdsEnabled();
+
+ // local settings
+ KSharedConfig::Ptr config = KGlobal::config();
+ KConfigGroup rulesGroup( config, "rules" );
+ QStringList rules;
+ rules = rulesGroup.readEntry( "local-rules" , QStringList() );
+ loadRules(rules);
- if ( cg.exists() )
+ // ----------------------------------------------------------
+
+ QDateTime today = QDateTime::currentDateTime();
+ QDateTime lastUpdate = ReKonfig::lastUpdate(); // the day of the implementation.. :)
+ int days = ReKonfig::updateInterval();
+
+ if( !checkUpdateDate || today > lastUpdate.addDays( days ) )
{
- _isAdblockEnabled = cg.readEntry("Enabled", false);
- _isHideAdsEnabled = cg.readEntry("Shrink", false);
+ ReKonfig::setLastUpdate( today );
+
+ updateNextSubscription();
+ return;
+ }
- // no need to load filters if adblock is not enabled :)
- if(!_isAdblockEnabled)
- return;
+ // else
+ QStringList titles = ReKonfig::subscriptionTitles();
+ foreach(const QString &title, titles)
+ {
+ rules = rulesGroup.readEntry( title + "-rules" , QStringList() );
+ loadRules(rules);
+ }
+}
- _whiteList.clear();
- _blackList.clear();
- _hideList.clear();
+
+void AdBlockManager::loadRules(const QStringList &rules)
+{
+ foreach(const QString &stringRule, rules)
+ {
+ // ! rules are comments
+ if( stringRule.startsWith('!') )
+ continue;
+
+ // [ rules are ABP infos
+ if( stringRule.startsWith('[') )
+ continue;
- QMap<QString,QString> entryMap = cg.entryMap();
- QMap<QString,QString>::ConstIterator it;
- for( it = entryMap.constBegin(); it != entryMap.constEnd(); ++it )
+ // empty rules are just dangerous..
+ // (an empty rule in whitelist allows all, in blacklist blocks all..)
+ if( stringRule.isEmpty() )
+ continue;
+
+ // white rules
+ if( stringRule.startsWith( QLatin1String("@@") ) )
{
- QString name = it.key();
- QString url = it.value();
-
- if (name.startsWith(QLatin1String("Filter")))
- {
- if(!url.startsWith("!"))
- {
- // white rules
- if(url.startsWith("@@"))
- {
- AdBlockRule rule( url.mid(2) );
- _whiteList << rule;
- continue;
- }
-
- // hide (CSS) rules
- if(url.startsWith("##"))
- {
- _hideList << url.mid(2);
- continue;
- }
-
- AdBlockRule rule( url );
- _blackList << rule;
- }
- }
+ AdBlockRule rule( stringRule.mid(2) );
+ _whiteList << rule;
+ continue;
+ }
+
+ // hide (CSS) rules
+ if( stringRule.startsWith( QLatin1String("##") ) )
+ {
+ _hideList << stringRule.mid(2);
+ continue;
}
+
+ AdBlockRule rule( stringRule );
+ _blackList << rule;
}
}
-QNetworkReply *AdBlockManager::block(const QNetworkRequest &request)
+QNetworkReply *AdBlockManager::block(const QNetworkRequest &request, WebPage *page)
{
if (!_isAdblockEnabled)
return 0;
@@ -125,7 +164,9 @@ QNetworkReply *AdBlockManager::block(const QNetworkRequest &request)
{
if(filter.match(urlString))
{
- kDebug() << "****ADBLOCK: WHITE RULE (@@) Matched: ***********" << urlString;
+ kDebug() << "****ADBLOCK: WHITE RULE (@@) Matched: ***********";
+ kDebug() << "Filter exp: " << filter.pattern();
+ kDebug() << "UrlString: " << urlString;
return 0;
}
}
@@ -135,7 +176,23 @@ QNetworkReply *AdBlockManager::block(const QNetworkRequest &request)
{
if(filter.match(urlString))
{
- kDebug() << "****ADBLOCK: BLACK RULE Matched: ***********" << urlString;
+ kDebug() << "****ADBLOCK: BLACK RULE Matched: ***********";
+ kDebug() << "Filter exp: " << filter.pattern();
+ kDebug() << "UrlString: " << urlString;
+
+ QWebElement document = page->mainFrame()->documentElement();
+ QWebElementCollection elements = document.findAll("*");
+ foreach (QWebElement el, elements)
+ {
+ if(filter.match( el.attribute("src") ) )
+ {
+ kDebug() << "MATCHES ATTRIBUTE!!!!!";
+ el.setStyleProperty(QLatin1String("visibility"), QLatin1String("hidden"));
+ el.setStyleProperty(QLatin1String("width"), QLatin1String("0"));
+ el.setStyleProperty(QLatin1String("height"), QLatin1String("0"));
+ }
+ }
+
AdBlockNetworkReply *reply = new AdBlockNetworkReply(request, urlString, this);
return reply;
}
@@ -148,7 +205,7 @@ QNetworkReply *AdBlockManager::block(const QNetworkRequest &request)
void AdBlockManager::applyHidingRules(WebPage *page)
{
- if(!page || !page->mainFrame())
+ if(!page)
return;
if (!_isAdblockEnabled)
@@ -156,16 +213,116 @@ void AdBlockManager::applyHidingRules(WebPage *page)
if (!_isHideAdsEnabled)
return;
+
+ QWebElement document = page->mainFrame()->documentElement();
+ // HIDE RULES
foreach(const QString &filter, _hideList)
{
- QWebElement document = page->mainFrame()->documentElement();
QWebElementCollection elements = document.findAll(filter);
- foreach (QWebElement element, elements)
+ foreach (QWebElement el, elements)
{
- element.setStyleProperty(QLatin1String("visibility"), QLatin1String("hidden"));
- element.removeFromDocument();
+ if(el.isNull())
+ continue;
+ kDebug() << "Hide element: " << el.localName();
+ el.setStyleProperty(QLatin1String("visibility"), QLatin1String("hidden"));
+ el.removeFromDocument();
}
}
}
+
+
+void AdBlockManager::updateNextSubscription()
+{
+ QStringList locations = ReKonfig::subscriptionLocations();
+
+ if( _index < locations.size() )
+ {
+ QString urlString = locations.at(_index);
+ kDebug() << "DOWNLOADING FROM " << urlString;
+ KUrl subUrl = KUrl( urlString );
+
+ KIO::TransferJob* job = KIO::get( subUrl , KIO::Reload , KIO::HideProgressInfo );
+ connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)), this, SLOT(subscriptionData(KIO::Job*, const QByteArray&)));
+ connect(job, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
+
+ return;
+ }
+
+ _index = 0;
+ _buffer.clear();
+}
+
+
+void AdBlockManager::slotResult(KJob *job)
+{
+ kDebug() << "SLOTRESULT";
+ if(job->error())
+ return;
+
+ QList<QByteArray> list = _buffer.split('\n');
+ QStringList ruleList;
+ foreach(const QByteArray &ba, list)
+ {
+ kDebug() << ba;
+ ruleList << QString(ba);
+ }
+ loadRules(ruleList);
+ saveRules(ruleList);
+
+ _index++;
+
+ // last..
+ updateNextSubscription();
+}
+
+
+void AdBlockManager::subscriptionData(KIO::Job* job, const QByteArray& data)
+{
+ kDebug() << "subscriptionData";
+ 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() );
+}
+
+
+void AdBlockManager::saveRules(const QStringList &rules)
+{
+ QStringList cleanedRules;
+ foreach(const QString &r, rules)
+ {
+ if( !r.startsWith('!') && !r.startsWith('[') && !r.isEmpty() )
+ cleanedRules << r;
+ }
+
+ QStringList titles = ReKonfig::subscriptionTitles();
+ QString title = titles.at(_index) + "-rules";
+
+ KSharedConfig::Ptr config = KGlobal::config();
+ KConfigGroup cg( config , "rules" );
+ cg.writeEntry( title, cleanedRules );
+}
+
+
+void AdBlockManager::addSubscription(const QString &title, const QString &location)
+{
+ QStringList titles = ReKonfig::subscriptionTitles();
+ if( titles.contains(title) )
+ return;
+
+ QStringList locations = ReKonfig::subscriptionLocations();
+ if( locations.contains(location) )
+ return;
+
+ titles << title;
+ locations << location;
+
+ ReKonfig::setSubscriptionTitles(titles);
+ ReKonfig::setSubscriptionLocations(locations);
+}
diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h
index f01aaca0..9b1cc915 100644
--- a/src/adblock/adblockmanager.h
+++ b/src/adblock/adblockmanager.h
@@ -2,7 +2,7 @@
*
* This file is a part of the rekonq project
*
-* Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
+* Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
@@ -105,12 +105,15 @@
// Local Includes
+#include "rekonqprivate_export.h"
#include "adblockrule.h"
// Qt Includes
#include <QObject>
#include <QNetworkReply>
#include <QStringList>
+#include <QByteArray>
+#include <KIO/Job>
// Forward Includes
class QNetworkRequest;
@@ -120,7 +123,7 @@ class WebPage;
typedef QList<AdBlockRule> AdBlockRuleList;
-class AdBlockManager : public QObject
+class REKONQ_TESTS_EXPORT AdBlockManager : public QObject
{
Q_OBJECT
@@ -128,9 +131,21 @@ public:
AdBlockManager(QObject *parent = 0);
~AdBlockManager();
- void loadSettings();
- QNetworkReply *block(const QNetworkRequest &request);
+ QNetworkReply *block(const QNetworkRequest &request, WebPage *page);
void applyHidingRules(WebPage *page);
+ void addSubscription(const QString &title, const QString &location);
+
+public slots:
+ void loadSettings(bool checkUpdateDate = true);
+
+private:
+ void updateNextSubscription();
+ void saveRules(const QStringList &);
+ void loadRules(const QStringList &);
+
+private slots:
+ void slotResult(KJob *);
+ void subscriptionData(KIO::Job*, const QByteArray&);
private:
bool _isAdblockEnabled;
@@ -139,6 +154,9 @@ private:
AdBlockRuleList _blackList;
AdBlockRuleList _whiteList;
QStringList _hideList;
+
+ int _index;
+ QByteArray _buffer;
};
#endif
diff --git a/src/adblock/adblocknetworkreply.cpp b/src/adblock/adblocknetworkreply.cpp
index 1ccca96d..3b73b8a0 100644
--- a/src/adblock/adblocknetworkreply.cpp
+++ b/src/adblock/adblocknetworkreply.cpp
@@ -28,7 +28,7 @@
*
* This file is a part of the rekonq project
*
- * Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
+ * Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
@@ -68,7 +68,7 @@ AdBlockNetworkReply::AdBlockNetworkReply(const QNetworkRequest &request, const Q
setOperation(QNetworkAccessManager::GetOperation);
setRequest(request);
setUrl(request.url());
- setError(QNetworkReply::ContentAccessDenied, i18n("Blocked by AdBlockRule: %1").arg(urlString));
+ setError(QNetworkReply::ContentAccessDenied, i18n("Blocked by AdBlockRule: %1", urlString) );
QTimer::singleShot(0, this, SLOT(delayedFinished()));
}
diff --git a/src/adblock/adblocknetworkreply.h b/src/adblock/adblocknetworkreply.h
index b5bb8300..bbc3471e 100644
--- a/src/adblock/adblocknetworkreply.h
+++ b/src/adblock/adblocknetworkreply.h
@@ -28,7 +28,7 @@
*
* This file is a part of the rekonq project
*
- * Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
+ * Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
@@ -54,6 +54,9 @@
#define ADBLOCK_NETWORK_REPLY_H
+// Local Includes
+#include "rekonqprivate_export.h"
+
// Qt Includes
#include <QNetworkReply>
#include <QString>
@@ -62,7 +65,7 @@
class AdBlockRule;
-class AdBlockNetworkReply : public QNetworkReply
+class REKONQ_TESTS_EXPORT AdBlockNetworkReply : public QNetworkReply
{
Q_OBJECT
diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp
index 9f86ffee..c0c3fd5b 100644
--- a/src/adblock/adblockrule.cpp
+++ b/src/adblock/adblockrule.cpp
@@ -30,7 +30,7 @@
*
* This file is a part of the rekonq project
*
- * Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
+ * Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
@@ -181,3 +181,9 @@ QString AdBlockRule::convertPatternToRegExp(const QString &wildcardPattern)
// Finally, return...
return pattern;
}
+
+
+QString AdBlockRule::pattern() const
+{
+ return m_regExp.pattern();
+}
diff --git a/src/adblock/adblockrule.h b/src/adblock/adblockrule.h
index 35715051..ee4825d1 100644
--- a/src/adblock/adblockrule.h
+++ b/src/adblock/adblockrule.h
@@ -29,7 +29,7 @@
*
* This file is a part of the rekonq project
*
- * Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
+ * Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
@@ -54,6 +54,7 @@
#ifndef ADBLOCKRULE_H
#define ADBLOCKRULE_H
+
// Qt Includes
#include <QRegExp>
#include <QString>
@@ -68,7 +69,9 @@ public:
AdBlockRule(const QString &filter);
bool match(const QString &encodedUrl) const;
-
+
+ QString pattern() const;
+
private:
QString convertPatternToRegExp(const QString &wildcardPattern);