1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

AdBlock: Show correct subscription when opening dialog with rule

- from AdBlock context menu or icon
This commit is contained in:
nowrep 2012-07-01 18:13:49 +02:00
parent a37b29b275
commit fbc44d2889
13 changed files with 100 additions and 194 deletions

View File

@ -62,6 +62,24 @@ AdBlockDialog::AdBlockDialog(QWidget* parent)
buttonBox->setFocus();
}
void AdBlockDialog::showRule(const AdBlockRule* rule) const
{
AdBlockSubscription* subscription = rule->subscription();
if (!subscription) {
return;
}
for (int i = 0; i < tabWidget->count(); ++i) {
AdBlockTreeWidget* treeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(i));
if (subscription == treeWidget->subscription()) {
treeWidget->showRule(rule);
tabWidget->setCurrentIndex(i);
break;
}
}
}
void AdBlockDialog::addRule()
{
m_currentTreeWidget->addRule();

View File

@ -26,13 +26,16 @@
class AdBlockSubscription;
class AdBlockTreeWidget;
class AdBlockManager;
class AdBlockRule;
class QT_QUPZILLA_EXPORT AdBlockDialog : public QDialog, public Ui_AdBlockDialog
{
Q_OBJECT
public:
AdBlockDialog(QWidget* parent = 0);
explicit AdBlockDialog(QWidget* parent = 0);
void showRule(const AdBlockRule* rule) const;
private slots:
void addRule();

View File

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "adblockicon.h"
#include "adblockrule.h"
#include "adblockmanager.h"
#include "mainapplication.h"
#include "qupzilla.h"
@ -42,12 +43,22 @@ AdBlockIcon::AdBlockIcon(QupZilla* mainClass, QWidget* parent)
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
}
void AdBlockIcon::popupBlocked(const QString &rule, const QUrl &url)
void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url)
{
QPair<QString, QUrl> pair;
int index = ruleString.lastIndexOf(" (");
const QString &subscriptionName = ruleString.left(index);
const QString &filter = ruleString.mid(index + 2, ruleString.size() - index - 3);
AdBlockSubscription* subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName);
if (filter.isEmpty() || !subscription) {
return;
}
AdBlockRule rule(filter, subscription);
QPair<AdBlockRule, QUrl> pair;
pair.first = rule;
pair.second = url;
m_blockedPopups.append(pair);
mApp->desktopNotifications()->showNotifications(QPixmap(":html/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window."));
@ -97,9 +108,13 @@ void AdBlockIcon::createMenu(QMenu* menu)
if (!m_blockedPopups.isEmpty()) {
menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false);
for (int i = 0; i < m_blockedPopups.count(); i++) {
const QPair<QString, QUrl> &pair = m_blockedPopups.at(i);
const QPair<AdBlockRule, QUrl> &pair = m_blockedPopups.at(i);
QString address = pair.second.toString().right(55);
menu->addAction(tr("%1 with (%2)").arg(address, pair.first).replace("&", "&&"), manager, SLOT(showRule()))->setData(pair.first);
QString actionText = tr("%1 with (%2)").arg(address, pair.first.filter()).replace("&", "&&");
QAction* action = menu->addAction(actionText, manager, SLOT(showRule()));
action->setData(qVariantFromValue((void*)&pair.first));
}
}
@ -112,7 +127,10 @@ void AdBlockIcon::createMenu(QMenu* menu)
menu->addAction(tr("Blocked URL (AdBlock Rule) - click to edit rule"))->setEnabled(false);
foreach(const WebPage::AdBlockedEntry & entry, entries) {
QString address = entry.url.toString().right(55);
menu->addAction(tr("%1 with (%2)").arg(address, entry.rule).replace("&", "&&"), manager, SLOT(showRule()))->setData(entry.rule);
QString actionText = tr("%1 with (%2)").arg(address, entry.rule->filter()).replace("&", "&&");
QAction* action = menu->addAction(actionText, manager, SLOT(showRule()));
action->setData(qVariantFromValue((void*)entry.rule));
}
}
}

View File

@ -18,9 +18,9 @@
#ifndef ADBLOCKICON_H
#define ADBLOCKICON_H
#include "qz_namespace.h"
#include "clickablelabel.h"
#include "adblockrule.h"
class QMenu;
class QUrl;
@ -34,7 +34,7 @@ public:
explicit AdBlockIcon(QupZilla* mainClass, QWidget* parent = 0);
~AdBlockIcon();
void popupBlocked(const QString &rule, const QUrl &url);
void popupBlocked(const QString &ruleString, const QUrl &url);
QAction* menuAction();
signals:
@ -53,8 +53,9 @@ private:
QupZilla* p_QupZilla;
QAction* m_menuAction;
QList<QPair<QString, QUrl> > m_blockedPopups;
QList<QPair<AdBlockRule, QUrl> > m_blockedPopups;
QTimer* m_flashTimer;
int m_timerTicks;
bool m_enabled;
};

View File

@ -17,7 +17,6 @@
* ============================================================ */
#include "adblockmanager.h"
#include "adblockdialog.h"
#include "adblockpage.h"
#include "adblocksubscription.h"
#include "adblockblockednetworkreply.h"
#include "mainapplication.h"
@ -85,7 +84,7 @@ QNetworkReply* AdBlockManager::block(const QNetworkRequest &request)
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
WebPage* webPage = static_cast<WebPage*>(v.value<void*>());
if (WebPage::isPointerSafeToUse(webPage)) {
webPage->addAdBlockRule(blockedRule->filter(), request.url());
webPage->addAdBlockRule(blockedRule, request.url());
}
AdBlockBlockedNetworkReply* reply = new AdBlockBlockedNetworkReply(subscription, blockedRule, this);
@ -252,7 +251,7 @@ QString AdBlockManager::elementHidingRules() const
// Remove last ","
if (!rules.isEmpty()) {
rules = rules.mid(0, rules.size() - 1);
rules = rules.left(rules.size() - 1);
}
return rules;
@ -268,12 +267,23 @@ QString AdBlockManager::elementHidingRulesForDomain(const QString &domain) const
// Remove last ","
if (!rules.isEmpty()) {
rules = rules.mid(0, rules.size() - 1);
rules = rules.left(rules.size() - 1);
}
return rules;
}
AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) const
{
foreach(AdBlockSubscription * subscription, m_subscriptions) {
if (subscription->title() == name) {
return subscription;
}
}
return 0;
}
AdBlockDialog* AdBlockManager::showDialog()
{
if (!m_adBlockDialog) {
@ -287,6 +297,10 @@ AdBlockDialog* AdBlockManager::showDialog()
void AdBlockManager::showRule()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
showDialog()->search->setText(action->data().toString());
const AdBlockRule* rule = static_cast<const AdBlockRule*>(action->data().value<void*>());
if (rule) {
showDialog()->showRule(rule);
}
}
}

View File

@ -46,6 +46,7 @@ public:
QString elementHidingRules() const;
QString elementHidingRulesForDomain(const QString &domain) const;
AdBlockSubscription* subscriptionByName(const QString &name) const;
QList<AdBlockSubscription*> subscriptions() const;
QNetworkReply* block(const QNetworkRequest &request);

View File

@ -1,118 +0,0 @@
/**
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
*
* 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.
*/
#include "adblockpage.h"
#include "adblockmanager.h"
#include "adblocksubscription.h"
#include "adblockrule.h"
#include <QWebElement>
#include <QWebPage>
#include <QWebFrame>
// #define ADBLOCKPAGE_DEBUG
AdBlockPage::AdBlockPage(QObject* parent)
: QObject(parent)
{
}
void AdBlockPage::checkRule(const AdBlockRule* rule, QWebPage* page, const QString &host)
{
if (!rule->isEnabled()) {
return;
}
QString filter = rule->filter();
int offset = filter.indexOf(QLatin1String("##"));
if (offset == -1) {
return;
}
QString selectorQuery;
if (offset > 0) {
QString domainRules = filter.mid(0, offset);
selectorQuery = filter.mid(offset + 2);
QStringList domains = domainRules.split(QLatin1Char(','));
bool match = false;
foreach(const QString & domain, domains) {
bool reverse = (domain[0] == QLatin1Char('~'));
if (reverse) {
QString xdomain = domain.mid(1);
if (host.endsWith(xdomain)) {
return;
}
match = true;
}
if (host.endsWith(domain)) {
match = true;
}
}
if (!match) {
return;
}
}
if (offset == 0) {
selectorQuery = filter.mid(2);
}
Q_UNUSED(page);
QWebElement document = page->mainFrame()->documentElement();
QWebElementCollection elements = document.findAll(selectorQuery);
#if defined(ADBLOCKPAGE_DEBUG)
if (elements.count() != 0) {
qDebug() << "AdBlockPage::" << __FUNCTION__ << "blocking" << elements.count() << "items" << selectorQuery << elements.count() << "rule:" << rule->filter();
}
#endif
foreach(QWebElement element, elements) {
element.setStyleProperty(QLatin1String("visibility"), QLatin1String("hidden"));
element.removeFromDocument();
}
}
void AdBlockPage::applyRulesToPage(QWebPage* page)
{
if (!page || !page->mainFrame()) {
return;
}
AdBlockManager* manager = AdBlockManager::instance();
if (!manager->isEnabled()) {
return;
}
QString host = page->mainFrame()->url().host();
// AdBlockSubscription* subscription = manager->subscription();
// QList<const AdBlockRule*> rules = subscription->pageRules();
// foreach(const AdBlockRule * rule, rules) {
// checkRule(rule, page, host);
// }
}

View File

@ -1,52 +0,0 @@
/**
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
*
* 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.
*/
#ifndef ADBLOCKPAGE_H
#define ADBLOCKPAGE_H
#include <QObject>
#include "qz_namespace.h"
class QWebPage;
class AdBlockRule;
class QT_QUPZILLA_EXPORT AdBlockPage : public QObject
{
public:
AdBlockPage(QObject* parent = 0);
void applyRulesToPage(QWebPage* page);
private:
void checkRule(const AdBlockRule* rule, QWebPage* page, const QString &host);
};
#endif // ADBLOCKPAGE_H

View File

@ -38,7 +38,7 @@ AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget*
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(itemChanged(QTreeWidgetItem*)));
connect(m_subscription, SIGNAL(subscriptionUpdated()), this, SLOT(subscriptionUpdated()));
QTimer::singleShot(0, this, SLOT(refresh()));
QTimer::singleShot(100, this, SLOT(refresh()));
}
AdBlockSubscription* AdBlockTreeWidget::subscription() const
@ -46,6 +46,24 @@ AdBlockSubscription* AdBlockTreeWidget::subscription() const
return m_subscription;
}
void AdBlockTreeWidget::showRule(const AdBlockRule* rule)
{
if (!m_topItem && rule) {
m_ruleToBeSelected = rule->filter();
}
else if (!m_ruleToBeSelected.isEmpty()) {
QList<QTreeWidgetItem*> items = findItems(m_ruleToBeSelected, Qt::MatchRecursive);
if (!items.isEmpty()) {
QTreeWidgetItem* item = items.at(0);
setCurrentItem(item);
scrollToItem(item, QAbstractItemView::PositionAtCenter);
}
m_ruleToBeSelected.clear();
}
}
void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos)
{
if (!m_subscription->canEditRules()) {
@ -115,7 +133,7 @@ void AdBlockTreeWidget::addRule()
return;
}
AdBlockRule rule(newRule);
AdBlockRule rule(newRule, m_subscription);
int offset = m_subscription->addRule(rule);
QTreeWidgetItem* item = new QTreeWidgetItem();
@ -217,5 +235,6 @@ void AdBlockTreeWidget::refresh()
}
expandAll();
showRule(0);
m_itemChangingBlock = false;
}

View File

@ -34,6 +34,8 @@ public:
AdBlockSubscription* subscription() const;
void showRule(const AdBlockRule* rule);
public slots:
void addRule();
void removeRule();
@ -52,6 +54,7 @@ private:
AdBlockSubscription* m_subscription;
QTreeWidgetItem* m_topItem;
QString m_ruleToBeSelected;
bool m_itemChangingBlock;
};

View File

@ -90,7 +90,6 @@ SOURCES += \
other/sourceviewersearch.cpp \
adblock/adblocksubscription.cpp \
adblock/adblockrule.cpp \
adblock/adblockpage.cpp \
adblock/adblockmanager.cpp \
adblock/adblockdialog.cpp \
adblock/adblockblockednetworkreply.cpp \
@ -240,7 +239,6 @@ HEADERS += \
other/sourceviewersearch.h \
adblock/adblocksubscription.h \
adblock/adblockrule.h \
adblock/adblockpage.h \
adblock/adblockmanager.h \
adblock/adblockdialog.h \
adblock/adblockblockednetworkreply.h \

View File

@ -462,10 +462,10 @@ QObject* WebPage::createPlugin(const QString &classid, const QUrl &url, const QS
return pluginFactory()->create(classid, url, paramNames, paramValues);
}
void WebPage::addAdBlockRule(const QString &filter, const QUrl &url)
void WebPage::addAdBlockRule(const AdBlockRule* rule, const QUrl &url)
{
AdBlockedEntry entry;
entry.rule = filter;
entry.rule = rule;
entry.url = url;
if (!m_adBlockedEntries.contains(entry)) {
@ -653,7 +653,7 @@ bool WebPage::extension(Extension extension, const ExtensionOption* option, Exte
}
else { //The whole page is blocked
QString rule = exOption->errorString;
rule.remove("AdBlock:");
rule.remove("AdBlock: ");
QFile file(":/html/adblockPage.html");
file.open(QFile::ReadOnly);

View File

@ -27,6 +27,7 @@ class QFileSystemWatcher;
class QEventLoop;
class QupZilla;
class AdBlockRule;
class TabbedWebView;
class SpeedDial;
class NetworkManagerProxy;
@ -36,7 +37,7 @@ class QT_QUPZILLA_EXPORT WebPage : public QWebPage
Q_OBJECT
public:
struct AdBlockedEntry {
QString rule;
const AdBlockRule* rule;
QUrl url;
bool operator==(const AdBlockedEntry &other) const {
@ -60,7 +61,7 @@ public:
bool javaScriptConfirm(QWebFrame* originatingFrame, const QString &msg);
void javaScriptAlert(QWebFrame* originatingFrame, const QString &msg);
void addAdBlockRule(const QString &filter, const QUrl &url);
void addAdBlockRule(const AdBlockRule* rule, const QUrl &url);
QList<AdBlockedEntry> adBlockedEntries() { return m_adBlockedEntries; }
void scheduleAdjustPage();