summaryrefslogtreecommitdiff
path: root/src/adblock
diff options
context:
space:
mode:
Diffstat (limited to 'src/adblock')
-rw-r--r--src/adblock/adblockmanager.cpp198
-rw-r--r--src/adblock/adblockmanager.h18
-rw-r--r--src/adblock/adblockrule.cpp6
-rw-r--r--src/adblock/adblockrule.h4
4 files changed, 182 insertions, 44 deletions
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp
index 06f0d164..d2c2b013 100644
--- a/src/adblock/adblockmanager.cpp
+++ b/src/adblock/adblockmanager.cpp
@@ -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();
}
@@ -58,52 +62,78 @@ AdBlockManager::~AdBlockManager()
void AdBlockManager::loadSettings()
{
- 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( 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 names = ReKonfig::subscriptionNames();
+ foreach(const QString &name, names)
+ {
+ rules = rulesGroup.readEntry( name + "-rules" , QStringList() );
+ loadRules(rules);
+ }
+}
- _whiteList.clear();
- _blackList.clear();
- _hideList.clear();
-
- QMap<QString,QString> entryMap = cg.entryMap();
- QMap<QString,QString>::ConstIterator it;
- for( it = entryMap.constBegin(); it != entryMap.constEnd(); ++it )
- {
- QString name = it.key();
- QString url = it.value();
- if (name.startsWith(QLatin1String("Filter")))
+void AdBlockManager::loadRules(const QStringList &rules)
+{
+ foreach(const QString &stringRule, rules)
+ {
+ // ! rules are comments
+ if( !stringRule.startsWith('!') && !stringRule.startsWith('[') && !stringRule.isEmpty() )
+ {
+ // white rules
+ if( stringRule.startsWith( QLatin1String("@@") ) )
{
- if(!url.startsWith('!'))
- {
- // white rules
- if( url.startsWith( QLatin1String("@@") ) )
- {
- AdBlockRule rule( url.mid(2) );
- _whiteList << rule;
- continue;
- }
-
- // hide (CSS) rules
- if( url.startsWith( QLatin1String("##") ) )
- {
- _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;
}
}
}
@@ -125,7 +155,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 +167,9 @@ 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;
AdBlockNetworkReply *reply = new AdBlockNetworkReply(request, urlString, this);
return reply;
}
@@ -164,8 +198,88 @@ void AdBlockManager::applyHidingRules(WebPage *page)
foreach (QWebElement element, elements)
{
+ kDebug() << "Hide element: " << element.localName();
element.setStyleProperty(QLatin1String("visibility"), QLatin1String("hidden"));
element.removeFromDocument();
}
}
}
+
+
+void AdBlockManager::updateNextSubscription()
+{
+ kDebug() << "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO";
+
+ QStringList subUrlStrings = ReKonfig::subscriptionUrls();
+
+ if( _index < subUrlStrings.size() )
+ {
+ QString urlString = subUrlStrings.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 names = ReKonfig::subscriptionNames();
+ QString name = names.at(_index) + "-rules";
+
+ KSharedConfig::Ptr config = KGlobal::config();
+ KConfigGroup cg( config , "rules" );
+ cg.writeEntry( name, cleanedRules );
+}
diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h
index d1bbe7ba..aac78e3b 100644
--- a/src/adblock/adblockmanager.h
+++ b/src/adblock/adblockmanager.h
@@ -112,6 +112,8 @@
#include <QObject>
#include <QNetworkReply>
#include <QStringList>
+#include <QByteArray>
+#include <KIO/Job>
// Forward Includes
class QNetworkRequest;
@@ -129,9 +131,20 @@ public:
AdBlockManager(QObject *parent = 0);
~AdBlockManager();
- void loadSettings();
QNetworkReply *block(const QNetworkRequest &request);
void applyHidingRules(WebPage *page);
+
+public slots:
+ void loadSettings();
+
+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;
@@ -140,6 +153,9 @@ private:
AdBlockRuleList _blackList;
AdBlockRuleList _whiteList;
QStringList _hideList;
+
+ int _index;
+ QByteArray _buffer;
};
#endif
diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp
index 25ca3678..c0c3fd5b 100644
--- a/src/adblock/adblockrule.cpp
+++ b/src/adblock/adblockrule.cpp
@@ -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 8f79d16e..ee4825d1 100644
--- a/src/adblock/adblockrule.h
+++ b/src/adblock/adblockrule.h
@@ -69,7 +69,9 @@ public:
AdBlockRule(const QString &filter);
bool match(const QString &encodedUrl) const;
-
+
+ QString pattern() const;
+
private:
QString convertPatternToRegExp(const QString &wildcardPattern);