summaryrefslogtreecommitdiff
path: root/src/adblock
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2012-11-05 18:51:31 +0100
committerAndrea Diamantini <adjam7@gmail.com>2012-12-10 02:48:06 +0100
commitfb57031a8d81a19e426765b1ceaa325ce51411b4 (patch)
tree465eaf0c619e9c84ac15c4002cd668c977e9f752 /src/adblock
parentFix tools menu position (diff)
downloadrekonq-fb57031a8d81a19e426765b1ceaa325ce51411b4.tar.xz
adblock work
readded an icon in the urlbar when adblock is active, BUT with different features: you can now disable adblock "per-site", in a similar way chromium does. cleaned up adblock manager code, removing some old no more used code fragments
Diffstat (limited to 'src/adblock')
-rw-r--r--src/adblock/adblockhostmatcher.cpp37
-rw-r--r--src/adblock/adblockhostmatcher.h4
-rw-r--r--src/adblock/adblockmanager.cpp64
-rw-r--r--src/adblock/adblockmanager.h8
-rw-r--r--src/adblock/adblocksettingwidget.cpp (renamed from src/adblock/adblockwidget.cpp)20
-rw-r--r--src/adblock/adblocksettingwidget.h (renamed from src/adblock/adblockwidget.h)10
-rw-r--r--src/adblock/blockedelementswidget.cpp116
-rw-r--r--src/adblock/blockedelementswidget.h71
8 files changed, 97 insertions, 233 deletions
diff --git a/src/adblock/adblockhostmatcher.cpp b/src/adblock/adblockhostmatcher.cpp
index 021fe12d..5033c6f7 100644
--- a/src/adblock/adblockhostmatcher.cpp
+++ b/src/adblock/adblockhostmatcher.cpp
@@ -29,11 +29,11 @@
// Rekonq Includes
#include "rekonq_defines.h"
+
bool AdBlockHostMatcher::tryAddFilter(const QString &filter)
{
if (filter.startsWith(QL1S("||")))
{
-
QString domain = filter.mid(2);
if (!domain.endsWith(QL1C('^')))
@@ -49,8 +49,41 @@ bool AdBlockHostMatcher::tryAddFilter(const QString &filter)
domain = domain.toLower();
m_hostList.insert(domain);
- m_hostList.insert(QL1S("www.") + domain);
return true;
}
+
+ if (filter.startsWith(QL1S("@@")))
+ {
+ QString domain = filter.mid(2);
+
+ if (domain.contains(QL1C('^')))
+ return false;
+
+ if (domain.contains(QL1C('$')))
+ return false;
+
+ if (domain.contains(QL1C('*')))
+ return false;
+
+ if (domain.contains(QL1C('|')))
+ return false;
+
+ if (domain.contains(QL1C('/')))
+ {
+ if (!domain.endsWith(QL1C('/')))
+ return false;
+ }
+ domain = domain.toLower();
+ m_hostList.insert(domain);
+ return true;
+ }
+
return false;
}
+
+
+void AdBlockHostMatcher::remove(const QString &hostRule)
+{
+ bool on = m_hostList.remove(hostRule);
+ kDebug() << "REMOVED? " << on;
+}
diff --git a/src/adblock/adblockhostmatcher.h b/src/adblock/adblockhostmatcher.h
index bdad883c..3ce6e284 100644
--- a/src/adblock/adblockhostmatcher.h
+++ b/src/adblock/adblockhostmatcher.h
@@ -29,6 +29,8 @@
#include <QSet>
#include <QString>
+#include <KDebug>
+
class AdBlockHostMatcher
{
public:
@@ -47,6 +49,8 @@ public:
m_hostList.clear();
}
+ void remove(const QString &hostRule);
+
private:
QSet<QString> m_hostList;
};
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp
index feee243e..49dfa4f4 100644
--- a/src/adblock/adblockmanager.cpp
+++ b/src/adblock/adblockmanager.cpp
@@ -32,10 +32,7 @@
#include "rekonq.h"
// Local Includes
-#include "adblockwidget.h"
-#include "blockedelementswidget.h"
-
-#include "webpage.h"
+#include "adblocksettingwidget.h"
// KDE Includes
#include <KIO/FileCopyJob>
@@ -207,10 +204,10 @@ void AdBlockManager::loadRuleString(const QString &stringRule)
// white rules
if (stringRule.startsWith(QL1S("@@")))
{
- const QString filter = stringRule.mid(2);
- if (_hostWhiteList.tryAddFilter(filter))
+ if (_hostWhiteList.tryAddFilter(stringRule))
return;
+ const QString filter = stringRule.mid(2);
AdBlockRule rule(filter);
_whiteList << rule;
return;
@@ -335,7 +332,7 @@ void AdBlockManager::showSettings()
dialog->setCaption(i18nc("@title:window", "Ad Block Settings"));
dialog->setButtons(KDialog::Ok | KDialog::Cancel);
- AdBlockWidget widget(_adblockConfig);
+ AdBlockSettingWidget widget(_adblockConfig);
dialog->setMainWidget(&widget);
connect(dialog, SIGNAL(okClicked()), &widget, SLOT(save()));
connect(dialog, SIGNAL(okClicked()), this, SLOT(loadSettings()));
@@ -371,33 +368,52 @@ void AdBlockManager::addCustomRule(const QString &stringRule, bool reloadPage)
}
-void AdBlockManager::showBlockedItemDialog()
+void AdBlockManager::removeCustomHostRule(const QString &stringRule, bool reloadPage)
{
- QPointer<KDialog> dialog = new KDialog();
- dialog->setCaption(i18nc("@title:window", "Blocked elements"));
- dialog->setButtons(KDialog::Ok);
+ // save rule in local filters
+ QString localRulesFilePath = KStandardDirs::locateLocal("appdata" , QL1S("adblockrules_local"));
- BlockedElementsWidget widget(this);
- widget.setBlockedElements(_blockedElements);
- widget.setHidedElements(_hidedElements);
+ QFile ruleFile(localRulesFilePath);
+ if (!ruleFile.open(QFile::ReadOnly))
+ {
+ kDebug() << "Unable to open rule file" << localRulesFilePath;
+ return;
+ }
- dialog->setMainWidget(&widget);
- dialog->exec();
+ QTextStream in(&ruleFile);
+ QStringList localRules;
+ QString r;
+ do
+ {
+ r = in.readLine();
+ if (r != stringRule)
+ localRules << r;
+ }
+ while (!r.isNull());
+ ruleFile.close();
- Q_FOREACH(const QString & r, widget.rulesToAdd())
+ if (!ruleFile.open(QFile::WriteOnly))
{
- addCustomRule(r);
+ kDebug() << "Unable to open rule file" << localRulesFilePath;
+ return;
}
- if (widget.pageNeedsReload())
- emit reloadCurrentPage();
+ QTextStream out(&ruleFile);
+ Q_FOREACH(const QString &r, localRules)
+ {
+ out << r << '\n';
+ }
+
+ // (un)load it
+ _hostWhiteList.remove(stringRule);
- dialog->deleteLater();
+ // eventually reload page
+ if (reloadPage)
+ emit reloadCurrentPage();
}
-void AdBlockManager::clearElementsLists()
+bool AdBlockManager::isAdblockEnabledForHost(const QString &host)
{
- _blockedElements.clear();
- _hidedElements = 0;
+ return ! _hostWhiteList.match(host);
}
diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h
index 1946d4ad..7e93a379 100644
--- a/src/adblock/adblockmanager.h
+++ b/src/adblock/adblockmanager.h
@@ -165,8 +165,10 @@ public:
bool blockRequest(const QNetworkRequest &request);
void addCustomRule(const QString &, bool reloadPage = true);
- void clearElementsLists();
+ void removeCustomHostRule(const QString &, bool reloadPage = true);
+ bool isAdblockEnabledForHost(const QString &host);
+
private:
AdBlockManager(QObject *parent = 0);
@@ -182,7 +184,6 @@ private:
private Q_SLOTS:
void loadSettings();
void showSettings();
- void showBlockedItemDialog();
void slotFinished(KJob *);
@@ -199,9 +200,6 @@ private:
AdBlockRuleList _whiteList;
QStringList _hideList;
- QStringList _blockedElements;
- int _hidedElements;
-
KSharedConfig::Ptr _adblockConfig;
static QWeakPointer<AdBlockManager> s_adBlockManager;
diff --git a/src/adblock/adblockwidget.cpp b/src/adblock/adblocksettingwidget.cpp
index 5ada506e..55b48046 100644
--- a/src/adblock/adblockwidget.cpp
+++ b/src/adblock/adblocksettingwidget.cpp
@@ -25,8 +25,8 @@
// Self Includes
-#include "adblockwidget.h"
-#include "adblockwidget.moc"
+#include "adblocksettingwidget.h"
+#include "adblocksettingwidget.moc"
// Auto Includes
#include "rekonq.h"
@@ -42,7 +42,7 @@
#include <QListWidgetItem>
-AdBlockWidget::AdBlockWidget(KSharedConfig::Ptr config, QWidget *parent)
+AdBlockSettingWidget::AdBlockSettingWidget(KSharedConfig::Ptr config, QWidget *parent)
: QWidget(parent)
, _changed(false)
, _adblockConfig(config)
@@ -78,7 +78,7 @@ AdBlockWidget::AdBlockWidget(KSharedConfig::Ptr config, QWidget *parent)
}
-void AdBlockWidget::slotInfoLinkActivated(const QString &url)
+void AdBlockSettingWidget::slotInfoLinkActivated(const QString &url)
{
Q_UNUSED(url)
@@ -94,7 +94,7 @@ void AdBlockWidget::slotInfoLinkActivated(const QString &url)
}
-void AdBlockWidget::insertRule()
+void AdBlockSettingWidget::insertRule()
{
QString rule = addFilterLineEdit->text();
if (rule.isEmpty())
@@ -105,13 +105,13 @@ void AdBlockWidget::insertRule()
}
-void AdBlockWidget::removeRule()
+void AdBlockSettingWidget::removeRule()
{
manualFiltersListWidget->takeItem(manualFiltersListWidget->currentRow());
}
-void AdBlockWidget::load()
+void AdBlockSettingWidget::load()
{
// General settings
KConfigGroup settingsGroup(_adblockConfig, "Settings");
@@ -177,7 +177,7 @@ void AdBlockWidget::load()
}
-void AdBlockWidget::save()
+void AdBlockSettingWidget::save()
{
if (!_changed)
return;
@@ -226,7 +226,7 @@ void AdBlockWidget::save()
}
-void AdBlockWidget::hasChanged()
+void AdBlockSettingWidget::hasChanged()
{
// update enabled status
checkHideAds->setEnabled(checkEnableAdblock->isChecked());
@@ -236,7 +236,7 @@ void AdBlockWidget::hasChanged()
}
-bool AdBlockWidget::changed()
+bool AdBlockSettingWidget::changed()
{
return _changed;
}
diff --git a/src/adblock/adblockwidget.h b/src/adblock/adblocksettingwidget.h
index 37f29f93..c630d795 100644
--- a/src/adblock/adblockwidget.h
+++ b/src/adblock/adblocksettingwidget.h
@@ -24,8 +24,8 @@
* ============================================================ */
-#ifndef ADBLOCK_WIDGET_H
-#define ADBLOCK_WIDGET_H
+#ifndef ADBLOCK_SETTINGS_WIDGET_H
+#define ADBLOCK_SETTINGS_WIDGET_H
// Rekonq Includes
@@ -41,12 +41,12 @@
#include <QWidget>
-class AdBlockWidget : public QWidget, private Ui::adblock
+class AdBlockSettingWidget : public QWidget, private Ui::adblock
{
Q_OBJECT
public:
- explicit AdBlockWidget(KSharedConfig::Ptr config, QWidget *parent = 0);
+ explicit AdBlockSettingWidget(KSharedConfig::Ptr config, QWidget *parent = 0);
bool changed();
@@ -70,4 +70,4 @@ private:
KSharedConfig::Ptr _adblockConfig;
};
-#endif // ADBLOCK_WIDGET_H
+#endif // ADBLOCK_SETTINGS_WIDGET_H
diff --git a/src/adblock/blockedelementswidget.cpp b/src/adblock/blockedelementswidget.cpp
deleted file mode 100644
index 141dd203..00000000
--- a/src/adblock/blockedelementswidget.cpp
+++ /dev/null
@@ -1,116 +0,0 @@
-/* ============================================================
-*
-* This file is a part of the rekonq project
-*
-* Copyright (C) 2012 by Andrea Diamantini <adjam7 at gmail dot com>
-*
-*
-* 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 <http://www.gnu.org/licenses/>.
-*
-* ============================================================ */
-
-
-// Self Includes
-#include "blockedelementswidget.h"
-#include "blockedelementswidget.moc"
-
-// Local Includes
-#include "adblockmanager.h"
-
-// Qt Includes
-#include <QHBoxLayout>
-#include <QVBoxLayout>
-#include <QPushButton>
-
-
-BlockedElementsWidget::BlockedElementsWidget(QObject *manager, QWidget *parent)
- : QWidget(parent)
- , _manager(manager)
- , _reloadPage(false)
-{
- setupUi(this);
-}
-
-
-void BlockedElementsWidget::setBlockedElements(const QStringList &list)
-{
- QVBoxLayout *frameLayout = new QVBoxLayout(frame);
-
- Q_FOREACH(const QString & block, list)
- {
- QString truncatedUrl = block;
- const int maxTextSize = 73;
- if (truncatedUrl.length() > maxTextSize)
- {
- const int truncateSize = 70;
- truncatedUrl.truncate(truncateSize);
- truncatedUrl += QL1S("...");
- }
- QWidget *w = new QWidget(this);
- QHBoxLayout *l = new QHBoxLayout(w);
- l->addWidget(new QLabel(truncatedUrl, this));
-
- QPushButton *button = new QPushButton(KIcon("dialog-ok-apply"), i18n("Unblock"), this);
- button->setProperty("URLTOUNBLOCK", block);
- button->setFixedWidth(100);
- connect(button, SIGNAL(clicked()), this, SLOT(unblockElement()));
- l->addWidget(button);
-
- w->setMinimumWidth(500);
- frameLayout->addWidget(w);
- }
-}
-
-
-void BlockedElementsWidget::setHidedElements(int n)
-{
- AdBlockManager *m = qobject_cast<AdBlockManager *>(_manager);
- if (m->isHidingElements())
- label->setText(i18np("There is %1 hidden element in this page.", "There are %1 hidden elements in this page.", QString::number(n)));
- else
- label->setText(i18n("Hiding elements is disabled."));
-}
-
-
-void BlockedElementsWidget::unblockElement()
-{
- QPushButton *buttonClicked = qobject_cast<QPushButton *>(sender());
- if (!buttonClicked)
- return;
-
- QString urlString = QL1S("@@") + buttonClicked->property("URLTOUNBLOCK").toString();
- kDebug() << "urlString: " << urlString;
-
- QString newText = i18n("Unblocked");
- QString buttonText = buttonClicked->text().remove('&');
- if (buttonText == newText)
- {
- buttonClicked->setText(i18n("Unblock"));
- buttonClicked->setIcon(KIcon("dialog-ok-apply"));
-
- _rulesToAdd.removeOne(urlString);
- }
- else
- {
- buttonClicked->setText(newText);
- buttonClicked->setIcon(KIcon("dialog-ok"));
-
- _rulesToAdd << urlString;
- }
-
- _reloadPage = true;
-}
diff --git a/src/adblock/blockedelementswidget.h b/src/adblock/blockedelementswidget.h
deleted file mode 100644
index d26ee849..00000000
--- a/src/adblock/blockedelementswidget.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/* ============================================================
-*
-* This file is a part of the rekonq project
-*
-* Copyright (C) 2012 by Andrea Diamantini <adjam7 at gmail dot com>
-*
-*
-* 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 <http://www.gnu.org/licenses/>.
-*
-* ============================================================ */
-
-
-#ifndef BLOCKED_ELEMENTS_WIDGET_H
-#define BLOCKED_ELEMENTS_WIDGET_H
-
-
-// Rekonq Includes
-#include "rekonq_defines.h"
-
-// Ui Includes
-#include "ui_blocked_elements.h"
-
-// Qt Includes
-#include <QWidget>
-
-
-class BlockedElementsWidget : public QWidget, private Ui::BlockedElements
-{
- Q_OBJECT
-
-public:
- explicit BlockedElementsWidget(QObject *manager, QWidget *parent = 0);
-
- void setBlockedElements(const QStringList &);
- void setHidedElements(int);
-
- bool pageNeedsReload()
- {
- return _reloadPage;
- };
-
- QStringList rulesToAdd()
- {
- return _rulesToAdd;
- };
-
-private Q_SLOTS:
- void unblockElement();
-
-private:
- QObject *_manager;
-
- bool _reloadPage;
- QStringList _rulesToAdd;
-};
-
-#endif // BLOCKED_ELEMENTS_WIDGET_H