summaryrefslogtreecommitdiff
path: root/src/adblock
diff options
context:
space:
mode:
Diffstat (limited to 'src/adblock')
-rw-r--r--src/adblock/adblockmanager.cpp298
-rw-r--r--src/adblock/adblockmanager.h98
-rw-r--r--src/adblock/adblocknetworkreply.cpp16
-rw-r--r--src/adblock/adblocknetworkreply.h13
-rw-r--r--src/adblock/adblockrule.cpp86
-rw-r--r--src/adblock/adblockrule.h22
6 files changed, 361 insertions, 172 deletions
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp
index c2a42f0b..e195c705 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
@@ -10,9 +10,9 @@
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
-* by the membership of KDE e.V.), which shall act as a proxy
+* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
-*
+*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -28,6 +28,9 @@
#include "adblockmanager.h"
#include "adblockmanager.moc"
+// Auto Includes
+#include "rekonq.h"
+
// Local Includes
#include "adblocknetworkreply.h"
#include "webpage.h"
@@ -35,7 +38,7 @@
// KDE Includes
#include <KSharedConfig>
#include <KConfigGroup>
-#include <KDebug>
+#include <KIO/TransferJob>
// Qt Includes
#include <QUrl>
@@ -43,11 +46,11 @@
AdBlockManager::AdBlockManager(QObject *parent)
- : QObject(parent)
- , _isAdblockEnabled(false)
- , _isHideAdsEnabled(false)
+ : QObject(parent)
+ , _isAdblockEnabled(false)
+ , _isHideAdsEnabled(false)
+ , _index(0)
{
- loadSettings();
}
@@ -56,91 +59,144 @@ 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);
-
- // no need to load filters if adblock is not enabled :)
- if(!_isAdblockEnabled)
- return;
-
- _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 )
+ ReKonfig::setLastUpdate(today);
+
+ updateNextSubscription();
+ return;
+ }
+
+ // else
+ QStringList titles = ReKonfig::subscriptionTitles();
+ foreach(const QString &title, titles)
+ {
+ rules = rulesGroup.readEntry(title + "-rules" , QStringList());
+ loadRules(rules);
+ }
+}
+
+
+void AdBlockManager::loadRules(const QStringList &rules)
+{
+ foreach(const QString &stringRule, rules)
+ {
+ // ! rules are comments
+ if (stringRule.startsWith('!'))
+ continue;
+
+ // [ rules are ABP info
+ if (stringRule.startsWith('['))
+ continue;
+
+ // 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(QL1S("@@")))
{
- QString name = it.key();
- QString url = it.value();
+ AdBlockRule rule(stringRule.mid(2));
+ _whiteList << rule;
+ continue;
+ }
- 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;
- }
- }
+ // hide (CSS) rules
+ if (stringRule.startsWith(QL1S("##")))
+ {
+ _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;
-
+
// we (ad)block just http traffic
- if(request.url().scheme() != QLatin1String("http"))
+ if (request.url().scheme() != QL1S("http"))
return 0;
-
+
QString urlString = request.url().toString();
// check white rules before :)
foreach(const AdBlockRule &filter, _whiteList)
{
- if(filter.match(urlString))
+ if (filter.match(urlString))
{
- kDebug() << "****ADBLOCK: WHITE RULE (@@) Matched: ***********" << urlString;
- return 0;
+ kDebug() << "****ADBLOCK: WHITE RULE (@@) Matched: ***********";
+ kDebug() << "Filter exp: " << filter.pattern();
+ kDebug() << "UrlString: " << urlString;
+ return 0;
}
}
-
+
// then check the black ones :(
foreach(const AdBlockRule &filter, _blackList)
{
- if(filter.match(urlString))
+ 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(QL1S("visibility"), QL1S("hidden"));
+ el.setStyleProperty(QL1S("width"), QL1S("0"));
+ el.setStyleProperty(QL1S("height"), QL1S("0"));
+ }
+ }
+
AdBlockNetworkReply *reply = new AdBlockNetworkReply(request, urlString, this);
- return reply;
+ return reply;
}
}
-
+
// no match
return 0;
}
@@ -148,24 +204,124 @@ QNetworkReply *AdBlockManager::block(const QNetworkRequest &request)
void AdBlockManager::applyHidingRules(WebPage *page)
{
- if(!page || !page->mainFrame())
+ if (!page)
return;
-
+
if (!_isAdblockEnabled)
return;
-
+
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(QL1S("visibility"), QL1S("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..eae761e0 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
@@ -10,9 +10,9 @@
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
-* by the membership of KDE e.V.), which shall act as a proxy
+* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
-*
+*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -30,42 +30,42 @@
// NOTE: AdBlockPlus Filters (fast) summary
-//
+//
// ### Basic Filter rules
-//
-// RULE = http://example.com/ads/*
+//
+// RULE = http://example.com/ads/*
// this should block every link containing all things from that link
-//
+//
// ### Exception rules (@@)
-//
+//
// RULE = @@advice*
-//
+//
// this will save every site, also that matched by other rules, cointaining words
// that starts with "advice". Wildcards && regular expression allowed here.
-//
+//
// ### Beginning/end matching rules (||)
-//
+//
// RULE=||http://badsite.com
-//
+//
// will stop all links starting with http://badsite.com
-//
+//
// RULE=*swf||
-//
+//
// will stop all links to direct flash contents
-//
+//
// ### Comments (!)
-//
+//
// RULE=!azz..
-//
+//
// Every rule starting with a ! is commented out and should not be checked
-//
+//
// ### Filter Options
-//
-// You can also specify a number of options to modify the behavior of a filter.
+//
+// You can also specify a number of options to modify the behavior of a filter.
// You list these options separated with commas after a dollar sign ($) at the end of the filter
-//
+//
// RULE=*/ads/*$element,match-case
-//
+//
// where $element can be one of the following:
// $script external scripts loaded via HTML script tag
// $image regular images, typically loaded via HTML img tag
@@ -80,37 +80,44 @@
// $subdocument embedded pages, usually included via HTML frames
// $document the page itself (only exception rules can be applied to the page)
// $other types of requests not covered in the list above
-//
+//
// Inverse type options are allowed through the ~ sign, for example:
-//
+//
// RULE=*/ads/*~$script,match-case
-//
+//
// ### Regular expressions
-//
+//
// They usually allow to check for (a lot of) sites, using just one rule, but be careful:
// BASIC FILTERS ARE PROCESSED FASTER THAN REGULAR EXPRESSIONS (In ADP! In rekonq, I don't know...)
-//
-//
+//
+//
// ### ELEMENT HIDING (##)
-//
-// This is quite different from usual adblock (but, for me, more powerful!). Sometimes you will find advertisements
+//
+// This is quite different from usual adblock (but, for me, more powerful!). Sometimes you will find advertisements
// that can’t be blocked because they are embedded as text in the web page itself.
// All you can do there is HIDE the element :)
-//
+//
// RULE=##div.advise
-//
+//
// The previous rule will hide every div whose class is named "advise". Usual CSS selectors apply here :)
//
// END NOTE ----------------------------------------------------------------------------------------------------------
+// Rekonq Includes
+#include "rekonq_defines.h"
+
// Local Includes
#include "adblockrule.h"
+// KDE Includes
+#include <KIO/Job>
+
// Qt Includes
#include <QObject>
#include <QNetworkReply>
#include <QStringList>
+#include <QByteArray>
// Forward Includes
class QNetworkRequest;
@@ -120,18 +127,30 @@ class WebPage;
typedef QList<AdBlockRule> AdBlockRuleList;
-class AdBlockManager : public QObject
+class REKONQ_TESTS_EXPORT AdBlockManager : public QObject
{
-Q_OBJECT
-
+ Q_OBJECT
+
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;
bool _isHideAdsEnabled;
@@ -139,6 +158,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..13677daa 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
@@ -36,9 +36,9 @@
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
- * by the membership of KDE e.V.), which shall act as a proxy
+ * by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -58,17 +58,19 @@
#include <klocalizedstring.h>
// Qt Includes
-#include <QNetworkRequest>
-#include <QTimer>
+#include <QtCore/QTimer>
+#include <QtCore/QString>
+
+#include <QtNetwork/QNetworkRequest>
AdBlockNetworkReply::AdBlockNetworkReply(const QNetworkRequest &request, const QString &urlString, QObject *parent)
- : QNetworkReply(parent)
+ : QNetworkReply(parent)
{
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..14a0672c 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
@@ -36,9 +36,9 @@
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
- * by the membership of KDE e.V.), which shall act as a proxy
+ * by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -54,15 +54,18 @@
#define ADBLOCK_NETWORK_REPLY_H
+// Rekonq Includes
+#include "rekonq_defines.h"
+
// Qt Includes
#include <QNetworkReply>
-#include <QString>
// Forward Declarations
class AdBlockRule;
+class QString;
-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..7c91a692 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
@@ -38,9 +38,9 @@
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
- * by the membership of KDE e.V.), which shall act as a proxy
+ * by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -57,44 +57,38 @@
// Qt Includes
#include <QStringList>
-#include <QDebug>
-#include <QRegExp>
#include <QUrl>
-// Defines
-#define QL1S(x) QLatin1String(x)
-#define QL1C(x) QLatin1Char(x)
-
AdBlockRule::AdBlockRule(const QString &filter)
- : m_optionMatchRule(false)
+ : m_optionMatchRule(false)
{
bool isRegExpRule = false;
QString parsedLine = filter;
-
- if ( parsedLine.startsWith( QL1C('/') ) && parsedLine.endsWith( QL1C('/') ) )
+
+ if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/')))
{
parsedLine = parsedLine.mid(1);
parsedLine = parsedLine.left(parsedLine.size() - 1);
isRegExpRule = true;
}
-
- int optionsNumber = parsedLine.indexOf( QL1C('$'), 0);
+
+ int optionsNumber = parsedLine.indexOf(QL1C('$'), 0);
QStringList options;
-
- if (optionsNumber >= 0)
+
+ if (optionsNumber >= 0)
{
options = parsedLine.mid(optionsNumber + 1).split(QL1C(','));
parsedLine = parsedLine.left(optionsNumber);
}
- if(!isRegExpRule)
+ if (!isRegExpRule)
parsedLine = convertPatternToRegExp(parsedLine);
-
+
m_regExp = QRegExp(parsedLine, Qt::CaseInsensitive, QRegExp::RegExp2);
- if ( options.contains( QL1S("match-case") ))
+ if (options.contains(QL1S("match-case")))
{
m_regExp.setCaseSensitivity(Qt::CaseSensitive);
m_optionMatchRule = true;
@@ -111,19 +105,19 @@ bool AdBlockRule::match(const QString &encodedUrl) const
// TODO: Reimplement this in rekonq 0.5 :)
//
-// if (matched && !m_options.isEmpty())
+// if (matched && !m_options.isEmpty())
// {
// // we only support domain right now
// if (m_options.count() == 1)
// {
-// foreach (const QString &option, m_options)
+// foreach (const QString &option, m_options)
// {
-// if (option.startsWith( QL1S("domain=") ))
+// if (option.startsWith( QL1S("domain=") ))
// {
// QUrl url = QUrl::fromEncoded(encodedUrl.toUtf8());
// QString host = url.host();
// QStringList domainOptions = option.mid(7).split( QL1C('|') );
-// foreach (QString domainOption, domainOptions)
+// foreach (QString domainOption, domainOptions)
// {
// bool negate = domainOption.at(0) == QL1C('~');
// if (negate)
@@ -147,37 +141,43 @@ bool AdBlockRule::match(const QString &encodedUrl) const
QString AdBlockRule::convertPatternToRegExp(const QString &wildcardPattern)
{
QString pattern = wildcardPattern;
-
+
// remove multiple wildcards
- pattern.replace(QRegExp( QL1S("\\*+") ), QL1S("*") );
-
+ pattern.replace(QRegExp(QL1S("\\*+")), QL1S("*"));
+
// remove anchors following separator placeholder
- pattern.replace(QRegExp( QL1S("\\^\\|$") ), QL1S("^") );
-
+ pattern.replace(QRegExp(QL1S("\\^\\|$")), QL1S("^"));
+
// remove leading wildcards
- pattern.replace(QRegExp( QL1S("^(\\*)") ), QL1S("") );
-
+ pattern.replace(QRegExp(QL1S("^(\\*)")), QL1S(""));
+
// remove trailing wildcards
- pattern.replace(QRegExp( QL1S("(\\*)$") ), QL1S("") );
-
+ pattern.replace(QRegExp(QL1S("(\\*)$")), QL1S(""));
+
// escape special symbols
- pattern.replace(QRegExp( QL1S("(\\W)") ), QL1S("\\\\1") );
-
+ pattern.replace(QRegExp(QL1S("(\\W)")), QL1S("\\\\1"));
+
// process extended anchor at expression start
- pattern.replace(QRegExp( QL1S("^\\\\\\|\\\\\\|") ), QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?") );
-
+ pattern.replace(QRegExp(QL1S("^\\\\\\|\\\\\\|")), QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?"));
+
// process separator placeholders
- pattern.replace(QRegExp( QL1S("\\\\\\^") ), QL1S("(?:[^\\w\\d\\-.%]|$)") );
-
+ pattern.replace(QRegExp(QL1S("\\\\\\^")), QL1S("(?:[^\\w\\d\\-.%]|$)"));
+
// process anchor at expression start
- pattern.replace(QRegExp( QL1S("^\\\\\\|") ), QL1S("^") );
-
+ pattern.replace(QRegExp(QL1S("^\\\\\\|")), QL1S("^"));
+
// process anchor at expression end
- pattern.replace(QRegExp( QL1S("\\\\\\|$") ), QL1S("$") );
-
+ pattern.replace(QRegExp(QL1S("\\\\\\|$")), QL1S("$"));
+
// replace wildcards by .*
- pattern.replace(QRegExp( QL1S("\\\\\\*") ), QL1S(".*") );
+ pattern.replace(QRegExp(QL1S("\\\\\\*")), QL1S(".*"));
// 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..6f042fe2 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
@@ -37,9 +37,9 @@
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
- * by the membership of KDE e.V.), which shall act as a proxy
+ * by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -54,9 +54,13 @@
#ifndef ADBLOCKRULE_H
#define ADBLOCKRULE_H
+
+// Rekonq Includes
+#include "rekonq_defines.h"
+
// Qt Includes
-#include <QRegExp>
-#include <QString>
+#include <QtCore/QRegExp>
+#include <QtCore/QString>
// Forward Includes
class QUrl;
@@ -69,11 +73,13 @@ public:
bool match(const QString &encodedUrl) const;
-private:
+ QString pattern() const;
+
+private:
QString convertPatternToRegExp(const QString &wildcardPattern);
-
+
QRegExp m_regExp;
-
+
// Rule Options
bool m_optionMatchRule;
};