From f51c660a207364b9b5ff400a29e8ff1dba721e61 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Fri, 19 Mar 2010 11:00:18 +0100 Subject: abp (Ad Block Plus) fake protocol support This will let rekonq to automatically add abp subscriptions from the adblockplus.org site (or wherever someone provides abp urls) Anyway, consider that the raccomended way to manage adblock (by me and by abp developers) is just adding the EasyList subscription. rekonq just does it by default!! --- src/adblock/adblockmanager.cpp | 42 ++++++++++++++------- src/adblock/adblockmanager.h | 5 ++- src/protocolhandler.cpp | 84 +++++++++++++++++++++++++++++++++++++----- src/protocolhandler.h | 3 +- src/rekonq.kcfg | 6 +-- src/settings/adblockwidget.cpp | 2 +- 6 files changed, 113 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index d2c2b013..66e11277 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -60,7 +60,7 @@ AdBlockManager::~AdBlockManager() } -void AdBlockManager::loadSettings() +void AdBlockManager::loadSettings(bool checkUpdateDate) { _index = 0; _buffer.clear(); @@ -92,7 +92,7 @@ void AdBlockManager::loadSettings() QDateTime lastUpdate = ReKonfig::lastUpdate(); // the day of the implementation.. :) int days = ReKonfig::updateInterval(); - if( today > lastUpdate.addDays( days ) ) + if( !checkUpdateDate || today > lastUpdate.addDays( days ) ) { ReKonfig::setLastUpdate( today ); @@ -101,10 +101,10 @@ void AdBlockManager::loadSettings() } // else - QStringList names = ReKonfig::subscriptionNames(); - foreach(const QString &name, names) + QStringList titles = ReKonfig::subscriptionTitles(); + foreach(const QString &title, titles) { - rules = rulesGroup.readEntry( name + "-rules" , QStringList() ); + rules = rulesGroup.readEntry( title + "-rules" , QStringList() ); loadRules(rules); } } @@ -208,13 +208,11 @@ void AdBlockManager::applyHidingRules(WebPage *page) void AdBlockManager::updateNextSubscription() { - kDebug() << "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"; - - QStringList subUrlStrings = ReKonfig::subscriptionUrls(); + QStringList locations = ReKonfig::subscriptionLocations(); - if( _index < subUrlStrings.size() ) + if( _index < locations.size() ) { - QString urlString = subUrlStrings.at(_index); + QString urlString = locations.at(_index); kDebug() << "DOWNLOADING FROM " << urlString; KUrl subUrl = KUrl( urlString ); @@ -276,10 +274,28 @@ void AdBlockManager::saveRules(const QStringList &rules) cleanedRules << r; } - QStringList names = ReKonfig::subscriptionNames(); - QString name = names.at(_index) + "-rules"; + QStringList titles = ReKonfig::subscriptionTitles(); + QString title = titles.at(_index) + "-rules"; KSharedConfig::Ptr config = KGlobal::config(); KConfigGroup cg( config , "rules" ); - cg.writeEntry( name, cleanedRules ); + 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 aac78e3b..c0bd2b70 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -133,9 +133,10 @@ public: QNetworkReply *block(const QNetworkRequest &request); void applyHidingRules(WebPage *page); - + void addSubscription(const QString &title, const QString &location); + public slots: - void loadSettings(); + void loadSettings(bool checkUpdateDate = true); private: void updateNextSubscription(); diff --git a/src/protocolhandler.cpp b/src/protocolhandler.cpp index 70d69107..9d1560ed 100644 --- a/src/protocolhandler.cpp +++ b/src/protocolhandler.cpp @@ -38,6 +38,7 @@ #include "urlbar.h" #include "webtab.h" #include "historymanager.h" +#include "adblockmanager.h" // KDE Includes #include @@ -52,6 +53,7 @@ #include #include #include +#include // Qt Includes #include @@ -61,6 +63,9 @@ #include #include +// Defines +#define QL1S(x) QLatin1String(x) + ProtocolHandler::ProtocolHandler(QObject *parent) : QObject(parent) @@ -87,11 +92,11 @@ bool ProtocolHandler::preHandling(const QNetworkRequest &request, QWebFrame *fra return false; // "http(s)" (fast) handling - if( _url.protocol() == QLatin1String("http") || _url.protocol() == QLatin1String("https") ) + if( _url.protocol() == QL1S("http") || _url.protocol() == QL1S("https") ) return false; // javascript handling - if( _url.protocol() == QLatin1String("javascript") ) + if( _url.protocol() == QL1S("javascript") ) { QString scriptSource = _url.authority(); QVariant result = frame->evaluateJavaScript(scriptSource); @@ -99,14 +104,21 @@ bool ProtocolHandler::preHandling(const QNetworkRequest &request, QWebFrame *fra } // "mailto" handling - if ( _url.protocol() == QLatin1String("mailto") ) + if ( _url.protocol() == QL1S("mailto") ) { KToolInvocation::invokeMailer(_url); return true; } + // "abp" handling + if ( _url.protocol() == QL1S("abp") ) + { + abpHandling(); + return true; + } + // "about" handling - if ( _url.protocol() == QLatin1String("about") ) + if ( _url.protocol() == QL1S("about") ) { // let webkit manage the about:blank url... if( _url == KUrl("about:blank") ) @@ -152,12 +164,12 @@ bool ProtocolHandler::postHandling(const QNetworkRequest &request, QWebFrame *fr kDebug() << "URL PROTOCOL: " << _url; // "http(s)" (fast) handling - if( _url.protocol() == QLatin1String("http") || _url.protocol() == QLatin1String("https") ) + if( _url.protocol() == QL1S("http") || _url.protocol() == QL1S("https") ) return false; // "mailto" handling: It needs to be handled both here(mail links clicked) // and in prehandling (mail url launched) - if ( _url.protocol() == QLatin1String("mailto") ) + if ( _url.protocol() == QL1S("mailto") ) { KToolInvocation::invokeMailer(_url); return true; @@ -168,7 +180,7 @@ bool ProtocolHandler::postHandling(const QNetworkRequest &request, QWebFrame *fr // My idea is: webkit cannot handle in any way ftp. So we have surely to return true here. // We start trying to guess what the url represent: it's a dir? show its contents (and download them). // it's a file? download it. It's another thing? beat me, but I don't know what to do... - if( _url.protocol() == QLatin1String("ftp") ) + if( _url.protocol() == QL1S("ftp") ) { KIO::StatJob *job = KIO::stat(_url); connect(job, SIGNAL(result(KJob*)), this, SLOT( slotMostLocalUrlResult(KJob*) )); @@ -176,7 +188,7 @@ bool ProtocolHandler::postHandling(const QNetworkRequest &request, QWebFrame *fr } // "file" handling. This is quite trivial :) - if( _url.protocol() == QLatin1String("file") ) + if( _url.protocol() == QL1S("file") ) { QFileInfo fileInfo( _url.path() ); if(fileInfo.isDir()) @@ -269,7 +281,7 @@ QString ProtocolHandler::dirHandling(const KFileItemList &list) msg += ""; - QString html = QString(QLatin1String(file.readAll())) + QString html = QString(QL1S(file.readAll())) .arg(title) .arg(msg) ; @@ -319,3 +331,57 @@ void ProtocolHandler::slotMostLocalUrlResult(KJob *job) emit downloadUrl(_url); } } + + +/** + * abp scheme (easy) explanation + * + */ +void ProtocolHandler::abpHandling() +{ + kDebug() << _url; + + QString path = _url.path(); + if( path != QL1S("subscribe") ) + return; + + QMap map = _url.queryItems( KUrl::CaseInsensitiveKeys ); + + QString location = map.value( QL1S("location") ); + kDebug() << location; + + QString title = map.value( QL1S("title") ); + kDebug() << title; + + QString requireslocation = map.value( QL1S("requireslocation") ); + kDebug() << requireslocation; + + QString requirestitle = map.value( QL1S("requirestitle") ); + kDebug() << requirestitle; + + QString info; + if( requirestitle.isEmpty() || requireslocation.isEmpty() ) + { + info = title; + } + else + { + info = i18n("\n %1,\n %2 (required by %3)\n", title, requirestitle, title); + } + + if ( KMessageBox::questionYesNo( 0, + i18n("Do you want to add the following subscriptions to your adblock settings?\n") + info, + i18n("Add automatic subscription to the adblock"), + KGuiItem(i18n("Add")), + KGuiItem(i18n("Discard")) + ) + ) + { + if( !requireslocation.isEmpty() && !requirestitle.isEmpty() ) + { + Application::adblockManager()->addSubscription( requirestitle, requireslocation ); + } + Application::adblockManager()->addSubscription( title, location ); + Application::adblockManager()->loadSettings(false); + } +} diff --git a/src/protocolhandler.h b/src/protocolhandler.h index 2a567015..99aec70a 100644 --- a/src/protocolhandler.h +++ b/src/protocolhandler.h @@ -74,7 +74,8 @@ private slots: private: QString dirHandling(const KFileItemList &list); - + void abpHandling(); + KDirLister *_lister; QWebFrame *_frame; KUrl _url; diff --git a/src/rekonq.kcfg b/src/rekonq.kcfg index 42ace3d3..bc41f5e1 100644 --- a/src/rekonq.kcfg +++ b/src/rekonq.kcfg @@ -161,10 +161,10 @@ true - - easylist + + EasyList - + https://easylist-downloads.adblockplus.org/easylist.txt diff --git a/src/settings/adblockwidget.cpp b/src/settings/adblockwidget.cpp index 8aac3e8c..471f57f0 100644 --- a/src/settings/adblockwidget.cpp +++ b/src/settings/adblockwidget.cpp @@ -115,7 +115,7 @@ void AdBlockWidget::load() int days = ReKonfig::updateInterval(); spinBox->setValue( days ); - QStringList subscriptions = ReKonfig::subscriptionNames(); + QStringList subscriptions = ReKonfig::subscriptionTitles(); // load automatic rules foreach(QString sub, subscriptions) -- cgit v1.2.1