From 85900021a041818ecf2415f5925fcc2a5aa5e7a4 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Fri, 20 Nov 2009 02:27:17 +0100 Subject: AdBlock. First blocks :D --- src/adblock/adblockmanager.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ src/adblock/adblockmanager.h | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/adblock/adblockmanager.cpp create mode 100644 src/adblock/adblockmanager.h (limited to 'src/adblock') diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp new file mode 100644 index 00000000..2d3feff7 --- /dev/null +++ b/src/adblock/adblockmanager.cpp @@ -0,0 +1,47 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* 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 +* 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 +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +#include "adblockmanager.h" +#include "adblockmanager.moc" + +#include + + +AdBlockManager::AdBlockManager(QObject *parent) + : QObject(parent) +{ +} + + +AdBlockManager::~AdBlockManager() +{ +} + + +bool AdBlockManager::isUrlAllowed(const QUrl &url) +{ + return true; +} \ No newline at end of file diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h new file mode 100644 index 00000000..12e897c1 --- /dev/null +++ b/src/adblock/adblockmanager.h @@ -0,0 +1,48 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* 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 +* 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 +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + + +#ifndef ADBLOCK_MANAGER_H +#define ADBLOCK_MANAGER_H + + +#include + +class QUrl; + + +class AdBlockManager : public QObject +{ +Q_OBJECT + +public: + AdBlockManager(QObject *parent = 0); + ~AdBlockManager(); + + bool isUrlAllowed(const QUrl &url); +}; + +#endif -- cgit v1.2.1 From 770f38f4aced1656fdee3c6d0dcf8ddfb9493c2b Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Fri, 20 Nov 2009 14:59:55 +0100 Subject: AdBlock, first file (and first UI. Probably not last :) --- src/adblock/CMakeLists.txt | 18 +++++++ src/adblock/kcmwebkitadblock.cpp | 67 +++++++++++++++++++++++++ src/adblock/kcmwebkitadblock.h | 52 +++++++++++++++++++ src/adblock/webkitAdblock.desktop | 14 ++++++ src/adblock/webkitadblock.ui | 102 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 253 insertions(+) create mode 100644 src/adblock/CMakeLists.txt create mode 100644 src/adblock/kcmwebkitadblock.cpp create mode 100644 src/adblock/kcmwebkitadblock.h create mode 100644 src/adblock/webkitAdblock.desktop create mode 100644 src/adblock/webkitadblock.ui (limited to 'src/adblock') diff --git a/src/adblock/CMakeLists.txt b/src/adblock/CMakeLists.txt new file mode 100644 index 00000000..8777bc50 --- /dev/null +++ b/src/adblock/CMakeLists.txt @@ -0,0 +1,18 @@ + +ADD_DEFINITIONS ( ${KDE4_DEFINITIONS} ) + +SET(kcm_webkitadblock_SRCS + kcmwebkitadblock.cpp +) + +kde4_add_ui_files(kcm_webkitadblock_SRCS + webkitadblock.ui +) + +kde4_add_plugin(kcm_webkitadblock ${kcm_webkitadblock_SRCS}) + +target_link_libraries(kcm_webkitadblock ${KDE4_KDEUI_LIBS} ) + +install(TARGETS kcm_webkitadblock DESTINATION ${PLUGIN_INSTALL_DIR} ) + +install( FILES webkitAdblock.desktop DESTINATION ${SERVICES_INSTALL_DIR} ) diff --git a/src/adblock/kcmwebkitadblock.cpp b/src/adblock/kcmwebkitadblock.cpp new file mode 100644 index 00000000..28487b74 --- /dev/null +++ b/src/adblock/kcmwebkitadblock.cpp @@ -0,0 +1,67 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* 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 +* 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 +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +// Self Includes +#include "kcmwebkitadblock.h" +#include "kcmwebkitadblock.moc" + +// KDE Includes +#include +#include + + +K_PLUGIN_FACTORY(RekonqPluginFactory, + registerPlugin("webkitAdblock"); + ) + +K_EXPORT_PLUGIN(RekonqPluginFactory("kcmrekonqfactory")) + + +KCMWebkitAdblock::KCMWebkitAdblock(QWidget *parent, const QVariantList &args) + : KCModule(KGlobal::mainComponent(), parent, args) +{ + setupUi(this); +} + + +KCMWebkitAdblock::~KCMWebkitAdblock() +{ +} + + +void KCMWebkitAdblock::defaults() +{ +} + + +void KCMWebkitAdblock::load() +{ +} + + +void KCMWebkitAdblock::save() +{ +} diff --git a/src/adblock/kcmwebkitadblock.h b/src/adblock/kcmwebkitadblock.h new file mode 100644 index 00000000..01f0e653 --- /dev/null +++ b/src/adblock/kcmwebkitadblock.h @@ -0,0 +1,52 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* 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 +* 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 +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +#ifndef KCM_WEBKIT_ADBLOCK_H +#define KCM_WEBKIT_ADBLOCK_H + + +// UI Includes +#include "ui_webkitadblock.h" + +// KDE Includes +#include + + +class KCMWebkitAdblock : public KCModule, private Ui::WebkitAdblock +{ +Q_OBJECT + +public: + KCMWebkitAdblock(QWidget *parent, const QVariantList &args); + + ~KCMWebkitAdblock(); + + void defaults(); + void load(); + void save(); +}; + +#endif diff --git a/src/adblock/webkitAdblock.desktop b/src/adblock/webkitAdblock.desktop new file mode 100644 index 00000000..22195b82 --- /dev/null +++ b/src/adblock/webkitAdblock.desktop @@ -0,0 +1,14 @@ +[Desktop Entry] +Type=Service +X-KDE-ServiceTypes=KCModule + +Icon=preferences-web-browser-adblock +Exec=kcmshell4 webkitAdblock + +X-KDE-Library=kcm_webkitadblock +X-KDE-PluginKeyword=webkitAdblock +X-KDE-ParentApp=kcontrol + +Name=Webkit AdBlock Settings + +Categories=Qt;KDE;X-KDE-settings-webbrowsing; diff --git a/src/adblock/webkitadblock.ui b/src/adblock/webkitadblock.ui new file mode 100644 index 00000000..8d7ac25f --- /dev/null +++ b/src/adblock/webkitadblock.ui @@ -0,0 +1,102 @@ + + + WebkitAdblock + + + + 0 + 0 + 543 + 491 + + + + Form + + + + + + Enable adblock + + + true + + + + + + + adblock settings + + + + + + + + + <qt>Filter expression (e.g. <tt>http://www.example.com/*</tt>, <a href="filterhelp">more info</a>): + + + + + + + + + + + + Add + + + + + + + Remove + + + + + + + Search + + + + + + + Import + + + + + + + + + + + + + KListWidget + QListWidget +
klistwidget.h
+
+ + KLineEdit + QLineEdit +
klineedit.h
+
+ + KListWidgetSearchLine + KLineEdit +
klistwidgetsearchline.h
+
+
+ + +
-- cgit v1.2.1 From 20a3df6c7d64d845009acc94ead1ddfbec4c413d Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sat, 21 Nov 2009 01:58:23 +0100 Subject: A new kcmshell named "webkit adblock" to set... (guess what?!) First bits work, seems enough for this evening --- src/adblock/CMakeLists.txt | 5 +- src/adblock/kcmwebkitadblock.cpp | 132 ++++++++++++++++++++++++++++++++++++++ src/adblock/kcmwebkitadblock.h | 17 +++++ src/adblock/webkitAdblock.desktop | 2 +- src/adblock/webkitadblock.ui | 59 ++++++++++------- 5 files changed, 189 insertions(+), 26 deletions(-) (limited to 'src/adblock') diff --git a/src/adblock/CMakeLists.txt b/src/adblock/CMakeLists.txt index 8777bc50..f2785811 100644 --- a/src/adblock/CMakeLists.txt +++ b/src/adblock/CMakeLists.txt @@ -11,7 +11,10 @@ kde4_add_ui_files(kcm_webkitadblock_SRCS kde4_add_plugin(kcm_webkitadblock ${kcm_webkitadblock_SRCS}) -target_link_libraries(kcm_webkitadblock ${KDE4_KDEUI_LIBS} ) +target_link_libraries(kcm_webkitadblock + ${KDE4_KDEUI_LIBS} + ${KDE4_KIO_LIBS} +) install(TARGETS kcm_webkitadblock DESTINATION ${PLUGIN_INSTALL_DIR} ) diff --git a/src/adblock/kcmwebkitadblock.cpp b/src/adblock/kcmwebkitadblock.cpp index 28487b74..d114b30a 100644 --- a/src/adblock/kcmwebkitadblock.cpp +++ b/src/adblock/kcmwebkitadblock.cpp @@ -31,6 +31,15 @@ // KDE Includes #include #include +#include +#include +#include +#include + +// Qt Includes +#include +#include +#include K_PLUGIN_FACTORY(RekonqPluginFactory, @@ -42,8 +51,27 @@ K_EXPORT_PLUGIN(RekonqPluginFactory("kcmrekonqfactory")) KCMWebkitAdblock::KCMWebkitAdblock(QWidget *parent, const QVariantList &args) : KCModule(KGlobal::mainComponent(), parent, args) + , _isAdblockEnabled(false) + , _group("adblock") { + KAboutData *about = new KAboutData( I18N_NOOP("kcmrekonqfactory"), 0, + ki18n( "rekonq Browsing Control Module" ), 0, + KLocalizedString(), KAboutData::License_GPL, + ki18n( "(c) 2009 Andrea Diamantini" ) ); + + about->addAuthor( ki18n("Andrea Diamantini"), KLocalizedString(), "adjam7@gmail.com" ); + setAboutData( about ); + setupUi(this); + connect(label, SIGNAL(linkActivated(const QString &)), SLOT(infoLinkActivated(const QString &)) ); + connect(groupBox,SIGNAL(clicked(bool)), this, SLOT(stateChanged(bool))); + searchLine->setListWidget(listWidget); + + connect(addButton,SIGNAL(clicked()),this,SLOT(addExpr())); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removeSelected())); + connect(importButton, SIGNAL(clicked()), this, SLOT(importExpr())); + + _config = KSharedConfig::openConfig("webkitrc", KConfig::NoGlobals); } @@ -54,14 +82,118 @@ KCMWebkitAdblock::~KCMWebkitAdblock() void KCMWebkitAdblock::defaults() { + searchLine->clear(); + lineEdit->clear(); + listWidget->clear(); + groupBox->setChecked(false); // set also _isAdblockEnabled } void KCMWebkitAdblock::load() { + KConfigGroup cg(_config, _group); + groupBox->setChecked( cg.readEntry("Enabled", false) ); + + int num = cg.readEntry("Count", 0); + for (int i = 0; i < num; ++i) + { + QString key = "Filter-" + QString::number(i); + QString filter = cg.readEntry( key, QString() ); + listWidget->addItem(filter); + } +// updateButton(); } void KCMWebkitAdblock::save() { + KConfigGroup cg(_config, _group); + cg.deleteGroup(); + cg = KConfigGroup(_config, _group); + + cg.writeEntry("Enabled", groupBox->isChecked()); + + for(int i = 0; i < listWidget->count(); ++i ) + { + QString key = "Filter-" + QString::number(i); + cg.writeEntry(key, listWidget->item(i)->text()); + } + cg.writeEntry("Count", listWidget->count()); + cg.sync(); +} + + +void KCMWebkitAdblock::infoLinkActivated(const QString &url) +{ + QString helpString = i18n("

Enter an expression to filter. Filters can be defined as either:" + "

  • a shell-style wildcard, e.g. http://www.example.com/ads*, the wildcards *?[] may be used
  • " + "
  • a full regular expression by surrounding the string with '/', e.g. /\\/(ad|banner)\\./
" + "

Any filter string can be preceded by '@@' to whitelist (allow) any matching URL, " + "which takes priority over any blacklist (blocking) filter."); + + + if ( url == "filterhelp" ) + QWhatsThis::showText( QCursor::pos(), helpString ); +} + + +void KCMWebkitAdblock::stateChanged(bool state) +{ + _isAdblockEnabled = state; +} + + +bool KCMWebkitAdblock::isAdblockEnabled() +{ + return _isAdblockEnabled; +} + + +void KCMWebkitAdblock::addExpr() +{ + listWidget->addItem( lineEdit->text() ); + lineEdit->clear(); +} + + +void KCMWebkitAdblock::removeSelected() +{ + listWidget->takeItem(listWidget->currentRow()); + searchLine->clear(); +} + + +void KCMWebkitAdblock::importExpr() +{ + + QString target; + KUrl url("http://adblockplus.mozdev.org/easylist/easylist.txt"); + + kDebug() << "downloading list.."; + + bool success = KIO::NetAccess::download(url, target, 0); + if(!success) + { + kDebug() << "not success"; + return; + } + + QFile temp(target); + if (!temp.open(QIODevice::ReadOnly | QIODevice::Text)) + { + kDebug() << "File not open"; + return; + } + + QTextStream stream(&temp); + QString line; + do + { + line = stream.readLine(); + if(!line.startsWith('!') && !line.startsWith('[')) + listWidget->addItem(line); + } + while (!line.isNull()); + + KIO::NetAccess::removeTempFile(target); } diff --git a/src/adblock/kcmwebkitadblock.h b/src/adblock/kcmwebkitadblock.h index 01f0e653..15442ac7 100644 --- a/src/adblock/kcmwebkitadblock.h +++ b/src/adblock/kcmwebkitadblock.h @@ -33,6 +33,7 @@ // KDE Includes #include +#include class KCMWebkitAdblock : public KCModule, private Ui::WebkitAdblock @@ -47,6 +48,22 @@ public: void defaults(); void load(); void save(); + + bool isAdblockEnabled(); + +private slots: + void infoLinkActivated(const QString &url); + void stateChanged(bool state); + + void addExpr(); + void removeSelected(); + void importExpr(); + +private: + bool _isAdblockEnabled; + + KSharedConfig::Ptr _config; + QString _group; }; #endif diff --git a/src/adblock/webkitAdblock.desktop b/src/adblock/webkitAdblock.desktop index 22195b82..61fa99c0 100644 --- a/src/adblock/webkitAdblock.desktop +++ b/src/adblock/webkitAdblock.desktop @@ -9,6 +9,6 @@ X-KDE-Library=kcm_webkitadblock X-KDE-PluginKeyword=webkitAdblock X-KDE-ParentApp=kcontrol -Name=Webkit AdBlock Settings +Name=Webkit AdBlock Categories=Qt;KDE;X-KDE-settings-webbrowsing; diff --git a/src/adblock/webkitadblock.ui b/src/adblock/webkitadblock.ui index 8d7ac25f..c75c0f0f 100644 --- a/src/adblock/webkitadblock.ui +++ b/src/adblock/webkitadblock.ui @@ -15,23 +15,37 @@ - - - Enable adblock + + + enable adblock - + true - - - - - - adblock settings + + true - + + + + + Search: + + + + + + + + + + + + + + @@ -41,10 +55,10 @@ - - - - + + + + @@ -52,24 +66,21 @@ + + + + - Remove - - - - - - - Search + Remove Expr - Import + Import from... -- cgit v1.2.1 From 4f5ac89ad5afdc6aa57e655b33ebed8ef87c1f23 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sat, 21 Nov 2009 02:12:25 +0100 Subject: Ok, same other bits on the adblock manager. Now it's really time to go to bed :) --- src/adblock/adblockmanager.cpp | 25 ++++++++++++++++++++++++- src/adblock/adblockmanager.h | 10 +++++++++- src/adblock/kcmwebkitadblock.cpp | 16 +--------------- src/adblock/kcmwebkitadblock.h | 6 ------ 4 files changed, 34 insertions(+), 23 deletions(-) (limited to 'src/adblock') diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index 2d3feff7..6b2f9616 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -32,7 +32,9 @@ AdBlockManager::AdBlockManager(QObject *parent) : QObject(parent) + , _isAdblockEnabled(false) { + loadSettings(); } @@ -41,7 +43,28 @@ AdBlockManager::~AdBlockManager() } +void AdBlockManager::loadSettings() +{ +// KConfigGroup cg(_config, _group); +// groupBox->setChecked( cg.readEntry("Enabled", false) ); +// +// int num = cg.readEntry("Count", 0); +// for (int i = 0; i < num; ++i) +// { +// QString key = "Filter-" + QString::number(i); +// QString filter = cg.readEntry( key, QString() ); +// listWidget->addItem(filter); +// } +} + + bool AdBlockManager::isUrlAllowed(const QUrl &url) { + if (url.scheme() == QLatin1String("data")) + return false; + + if (!_isAdblockEnabled) + return false; + return true; -} \ No newline at end of file +} diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h index 12e897c1..c782ee0d 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -29,8 +29,11 @@ #define ADBLOCK_MANAGER_H +// Qt Includes #include +#include +// Forward Includes class QUrl; @@ -41,8 +44,13 @@ Q_OBJECT public: AdBlockManager(QObject *parent = 0); ~AdBlockManager(); - + + void loadSettings(); bool isUrlAllowed(const QUrl &url); + +private: + bool _isAdblockEnabled; + QStringList _blocks; }; #endif diff --git a/src/adblock/kcmwebkitadblock.cpp b/src/adblock/kcmwebkitadblock.cpp index d114b30a..f6383788 100644 --- a/src/adblock/kcmwebkitadblock.cpp +++ b/src/adblock/kcmwebkitadblock.cpp @@ -51,7 +51,6 @@ K_EXPORT_PLUGIN(RekonqPluginFactory("kcmrekonqfactory")) KCMWebkitAdblock::KCMWebkitAdblock(QWidget *parent, const QVariantList &args) : KCModule(KGlobal::mainComponent(), parent, args) - , _isAdblockEnabled(false) , _group("adblock") { KAboutData *about = new KAboutData( I18N_NOOP("kcmrekonqfactory"), 0, @@ -64,7 +63,6 @@ KCMWebkitAdblock::KCMWebkitAdblock(QWidget *parent, const QVariantList &args) setupUi(this); connect(label, SIGNAL(linkActivated(const QString &)), SLOT(infoLinkActivated(const QString &)) ); - connect(groupBox,SIGNAL(clicked(bool)), this, SLOT(stateChanged(bool))); searchLine->setListWidget(listWidget); connect(addButton,SIGNAL(clicked()),this,SLOT(addExpr())); @@ -85,7 +83,7 @@ void KCMWebkitAdblock::defaults() searchLine->clear(); lineEdit->clear(); listWidget->clear(); - groupBox->setChecked(false); // set also _isAdblockEnabled + groupBox->setChecked(false); } @@ -137,18 +135,6 @@ void KCMWebkitAdblock::infoLinkActivated(const QString &url) } -void KCMWebkitAdblock::stateChanged(bool state) -{ - _isAdblockEnabled = state; -} - - -bool KCMWebkitAdblock::isAdblockEnabled() -{ - return _isAdblockEnabled; -} - - void KCMWebkitAdblock::addExpr() { listWidget->addItem( lineEdit->text() ); diff --git a/src/adblock/kcmwebkitadblock.h b/src/adblock/kcmwebkitadblock.h index 15442ac7..37130613 100644 --- a/src/adblock/kcmwebkitadblock.h +++ b/src/adblock/kcmwebkitadblock.h @@ -42,26 +42,20 @@ Q_OBJECT public: KCMWebkitAdblock(QWidget *parent, const QVariantList &args); - ~KCMWebkitAdblock(); void defaults(); void load(); void save(); - bool isAdblockEnabled(); - private slots: void infoLinkActivated(const QString &url); - void stateChanged(bool state); void addExpr(); void removeSelected(); void importExpr(); private: - bool _isAdblockEnabled; - KSharedConfig::Ptr _config; QString _group; }; -- cgit v1.2.1 From ae723ad13075808d687ad1b14f46f4a8cae87849 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Mon, 23 Nov 2009 01:25:17 +0100 Subject: First adblock implementation. now rekonq seems... slower :( But at least it's ads free! --- src/adblock/adblockmanager.cpp | 65 +++++++++++++++++++++++++++++++++--------- src/adblock/adblockmanager.h | 3 +- 2 files changed, 53 insertions(+), 15 deletions(-) (limited to 'src/adblock') diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index 6b2f9616..70960b44 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -27,6 +27,16 @@ #include "adblockmanager.h" #include "adblockmanager.moc" + +// Local Includes +#include "adblockrule.h" + +// KDE Includes +#include +#include +#include + +// Qt Includes #include @@ -45,26 +55,53 @@ AdBlockManager::~AdBlockManager() void AdBlockManager::loadSettings() { -// KConfigGroup cg(_config, _group); -// groupBox->setChecked( cg.readEntry("Enabled", false) ); -// -// int num = cg.readEntry("Count", 0); -// for (int i = 0; i < num; ++i) -// { -// QString key = "Filter-" + QString::number(i); -// QString filter = cg.readEntry( key, QString() ); -// listWidget->addItem(filter); -// } + _blockList.clear(); + + KSharedConfig::Ptr config = KSharedConfig::openConfig("webkitrc", KConfig::NoGlobals); + KConfigGroup cg(config, "adblock"); + + _isAdblockEnabled = cg.readEntry("Enabled", false); + + int num = cg.readEntry("Count", 0); + for (int i = 0; i < num; ++i) + { + QString key = "Filter-" + QString::number(i); + QString filter = cg.readEntry( key, QString() ); + if(!filter.isEmpty()) + { + if (filter.startsWith(QLatin1String("@@"))) + _whiteList << filter; + else + _blockList << filter; + } + } } bool AdBlockManager::isUrlAllowed(const QUrl &url) { - if (url.scheme() == QLatin1String("data")) - return false; - + kDebug() << "matching rule..."; if (!_isAdblockEnabled) - return false; + return true; + QString urlString = QString::fromUtf8(url.toEncoded()); + + foreach(const QString &str, _whiteList) + { + AdBlockRule rule(str); + kDebug() << "checking white list rule..."; + if(rule.match(urlString)) + return true; + } + + foreach(const QString &str, _blockList) + { + AdBlockRule rule(str); + kDebug() << "checking block list rule..."; + if(rule.match(urlString)) + return false; + } + + kDebug() << "done"; return true; } diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h index c782ee0d..a175fa2f 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -50,7 +50,8 @@ public: private: bool _isAdblockEnabled; - QStringList _blocks; + QStringList _blockList; + QStringList _whiteList; }; #endif -- cgit v1.2.1 From fd4886d7500e6b56b7e9a5f9128e535d0520c66a Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Mon, 23 Nov 2009 01:41:31 +0100 Subject: Forgot some files :) --- src/adblock/adblockrule.cpp | 173 +++++++++++++++++++++++++++++++++++++++ src/adblock/adblockrule.h | 82 +++++++++++++++++++ src/adblock/kcmwebkitadblock.cpp | 4 +- 3 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 src/adblock/adblockrule.cpp create mode 100644 src/adblock/adblockrule.h (limited to 'src/adblock') diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp new file mode 100644 index 00000000..870ad825 --- /dev/null +++ b/src/adblock/adblockrule.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2009, Zsombor Gegesy + * Copyright (c) 2009, Benjamin C. Meyer + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Benjamin Meyer nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * ============================================================ + * + * This file is a part of the rekonq project + * + * Copyright (C) 2009 by Andrea Diamantini + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * 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 + * 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * ============================================================ */ + + +#include "adblockrule.h" + +#include +#include +#include + + +AdBlockRule::AdBlockRule(const QString &filter) + : m_cssRule(false) + , m_exceptionRule(false) + , m_enabledRule(true) +{ + bool isRegExpRule = false; + + if (filter.startsWith(QLatin1String("!")) || filter.trimmed().isEmpty()) + m_enabledRule = false; + + if (filter.contains(QLatin1String("##"))) + m_cssRule = true; + + QString parsedLine = filter; + if (parsedLine.startsWith(QLatin1String("@@"))) + { + m_exceptionRule = true; + parsedLine = parsedLine.mid(2); + } + + if (parsedLine.startsWith(QLatin1Char('/'))) + { + if (parsedLine.endsWith(QLatin1Char('/'))) + { + parsedLine = parsedLine.mid(1); + parsedLine = parsedLine.left(parsedLine.size() - 1); + isRegExpRule = true; + } + } + + int options = parsedLine.indexOf(QLatin1String("$"), 0); + if (options >= 0) + { + m_options = parsedLine.mid(options + 1).split(QLatin1Char(',')); + parsedLine = parsedLine.left(options); + } + + if(!isRegExpRule) + parsedLine = convertPatternToRegExp(parsedLine); + m_regExp = QRegExp(parsedLine, Qt::CaseInsensitive, QRegExp::RegExp2); + + if (m_options.contains(QLatin1String("match-case"))) + { + m_regExp.setCaseSensitivity(Qt::CaseSensitive); + m_options.removeOne(QLatin1String("match-case")); + } +} + + +// here return false means that rule doesn't match, +// so that url is allowed +// return true means "matched rule", so stop url! +bool AdBlockRule::match(const QString &encodedUrl) const +{ + if (m_cssRule) + return false; + + if (!m_enabledRule) + return false; + + bool matched = m_regExp.indexIn(encodedUrl) != -1; + + if (matched && !m_options.isEmpty()) + { + // we only support domain right now + if (m_options.count() == 1) + { + foreach (const QString &option, m_options) + { + if (option.startsWith(QLatin1String("domain="))) + { + QUrl url = QUrl::fromEncoded(encodedUrl.toUtf8()); + QString host = url.host(); + QStringList domainOptions = option.mid(7).split(QLatin1Char('|')); + foreach (QString domainOption, domainOptions) + { + bool negate = domainOption.at(0) == QLatin1Char('~'); + if (negate) + domainOption = domainOption.mid(1); + bool hostMatched = domainOption == host; + if (hostMatched && !negate) + return true; + if (!hostMatched && negate) + return true; + } + } + } + } + return false; + } + + return matched; +} + + +QString AdBlockRule::convertPatternToRegExp(const QString &wildcardPattern) +{ + QString pattern = wildcardPattern; + return pattern.replace(QRegExp(QLatin1String("\\*+")), QLatin1String("*")) // remove multiple wildcards + .replace(QRegExp(QLatin1String("\\^\\|$")), QLatin1String("^")) // remove anchors following separator placeholder + .replace(QRegExp(QLatin1String("^(\\*)")), QLatin1String("")) // remove leading wildcards + .replace(QRegExp(QLatin1String("(\\*)$")), QLatin1String("")) // remove trailing wildcards + .replace(QRegExp(QLatin1String("(\\W)")), QLatin1String("\\\\1")) // escape special symbols + .replace(QRegExp(QLatin1String("^\\\\\\|\\\\\\|")), + QLatin1String("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?")) // process extended anchor at expression start + .replace(QRegExp(QLatin1String("\\\\\\^")), + QLatin1String("(?:[^\\w\\d\\-.%]|$)")) // process separator placeholders + .replace(QRegExp(QLatin1String("^\\\\\\|")), QLatin1String("^")) // process anchor at expression start + .replace(QRegExp(QLatin1String("\\\\\\|$")), QLatin1String("$")) // process anchor at expression end + .replace(QRegExp(QLatin1String("\\\\\\*")), QLatin1String(".*")) // replace wildcards by .* + ; +} diff --git a/src/adblock/adblockrule.h b/src/adblock/adblockrule.h new file mode 100644 index 00000000..3f1bd8bf --- /dev/null +++ b/src/adblock/adblockrule.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2009, Benjamin C. Meyer + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Benjamin Meyer nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * ============================================================ + * + * This file is a part of the rekonq project + * + * Copyright (C) 2009 by Andrea Diamantini + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * 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 + * 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * ============================================================ */ + + +#ifndef ADBLOCKRULE_H +#define ADBLOCKRULE_H + +// Qt Includes +#include + +// Forward Includes +class QUrl; +class QRegExp; + + +class AdBlockRule +{ +public: + AdBlockRule(const QString &filter); + + bool match(const QString &encodedUrl) const; + +private: + QString convertPatternToRegExp(const QString &wildcardPattern); + + bool m_cssRule; + bool m_exceptionRule; + bool m_enabledRule; + QRegExp m_regExp; + QStringList m_options; +}; + +#endif // ADBLOCKRULE_H diff --git a/src/adblock/kcmwebkitadblock.cpp b/src/adblock/kcmwebkitadblock.cpp index f6383788..bbe44c0e 100644 --- a/src/adblock/kcmwebkitadblock.cpp +++ b/src/adblock/kcmwebkitadblock.cpp @@ -99,7 +99,6 @@ void KCMWebkitAdblock::load() QString filter = cg.readEntry( key, QString() ); listWidget->addItem(filter); } -// updateButton(); } @@ -139,6 +138,7 @@ void KCMWebkitAdblock::addExpr() { listWidget->addItem( lineEdit->text() ); lineEdit->clear(); + emit changed(true); } @@ -146,6 +146,7 @@ void KCMWebkitAdblock::removeSelected() { listWidget->takeItem(listWidget->currentRow()); searchLine->clear(); + emit changed(true); } @@ -182,4 +183,5 @@ void KCMWebkitAdblock::importExpr() while (!line.isNull()); KIO::NetAccess::removeTempFile(target); + emit changed(true); } -- cgit v1.2.1