From 908a1dbfb08c30ae2a92ce7f6e36a9e88e8b5d09 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Mon, 13 Feb 2012 22:48:51 +0100 Subject: Let rekonq block sparse images (this idea has been copied from Arora) --- src/adblock/adblockmanager.cpp | 29 +++++++++++++++++++++++++++++ src/adblock/adblockmanager.h | 4 ++++ src/webview.cpp | 19 +++++++++++++++++++ src/webview.h | 3 +++ 4 files changed, 55 insertions(+) (limited to 'src') diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index b435d437..27fb4020 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -62,6 +62,12 @@ AdBlockManager::~AdBlockManager() } +bool AdBlockManager::isEnabled() +{ + return _isAdblockEnabled; +} + + void AdBlockManager::loadSettings() { // first, check this... @@ -347,3 +353,26 @@ void AdBlockManager::showSettings() dialog->deleteLater(); } + + +void AdBlockManager::addCustomRule(const QString &stringRule) +{ + // save rule in local filters + QString localRulesFilePath = KStandardDirs::locateLocal("appdata" , QL1S("adblockrules_local")); + + QFile ruleFile(localRulesFilePath); + if (!ruleFile.open(QFile::WriteOnly | QFile::Text)) + { + kDebug() << "Unable to open rule file" << localRulesFilePath; + return; + } + + QTextStream out(&ruleFile); + out << stringRule << '\n'; + + // load it + AdBlockRule rule(stringRule); + _blackList << rule; + + // TODO: update page? +} diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h index f853a8ed..2e7f7836 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -154,9 +154,13 @@ public: AdBlockManager(QObject *parent = 0); ~AdBlockManager(); + bool isEnabled(); + QNetworkReply *block(const QNetworkRequest &request, WebPage *page); void applyHidingRules(WebPage *page); + void addCustomRule(const QString &); + public Q_SLOTS: void loadSettings(); void showSettings(); diff --git a/src/webview.cpp b/src/webview.cpp index 80f021fc..3d4d7dcf 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -34,6 +34,7 @@ // Local Includes #include "application.h" +#include "adblockmanager.h" #include "bookmarkmanager.h" #include "iconmanager.h" #include "mainview.h" @@ -285,6 +286,14 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) a->setData(result.imageUrl()); connect(a, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(slotCopyImageLocation())); menu.addAction(a); + + if (rApp->adblockManager()->isEnabled()) + { + a = new KAction(KIcon("preferences-web-browser-adblock"), i18n("Block image"), this); + a->setData(result.imageUrl()); + connect(a, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(blockImage())); + menu.addAction(a); + } } // ACTIONS FOR TEXT SELECTION ----------------------------------------------------- @@ -1213,3 +1222,13 @@ void WebView::sendByMail() KToolInvocation::invokeMailer("", "", "", "", url); } + +void WebView::blockImage() +{ + QAction *action = qobject_cast(sender()); + if (!action) + return; + + QString imageUrl = action->data().toString(); + rApp->adblockManager()->addCustomRule(imageUrl); +} diff --git a/src/webview.h b/src/webview.h index 18977798..3ae9b7e9 100644 --- a/src/webview.h +++ b/src/webview.h @@ -108,6 +108,9 @@ private Q_SLOTS: void loadStarted(); + void blockImage(); + + Q_SIGNALS: void loadUrl(const KUrl &, const Rekonq::OpenType &); void zoomChanged(int); -- cgit v1.2.1