1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

Added AdBlock icon at the right corner of status bar which shows actual

status of AdBlock as well as shows blocked content on page
This commit is contained in:
nowrep 2011-03-29 20:30:05 +02:00
parent 77b31b8f85
commit d7f7f7843f
16 changed files with 198 additions and 70 deletions

View File

@ -46,27 +46,23 @@
#include "adblockmanager.h"
#include "adblocksubscription.h"
#include "ui_adblockdialog.h"
#include "mainapplication.h"
AdBlockDialog::AdBlockDialog(QWidget *parent)
: QDialog(parent)
, m_itemChangingBlock(false)
, m_manager(AdBlockManager::instance())
{
setupUi(this);
// m_adBlockModel = new AdBlockModel(this);
// m_proxyModel = new TreeSortFilterProxyModel(this);
// m_proxyModel->setSourceModel(m_adBlockModel);
// treeWidget->setModel(m_proxyModel);
// connect(search, SIGNAL(textChanged(QString)), m_proxyModel, SLOT(setFilterFixedString(QString)));
AdBlockManager* manager = AdBlockManager::instance();
adblockCheckBox->setChecked(manager->isEnabled());
connect(adblockCheckBox, SIGNAL(toggled(bool)), manager, SLOT(setEnabled(bool)));
adblockCheckBox->setChecked(m_manager->isEnabled());
connect(adblockCheckBox, SIGNAL(toggled(bool)), m_manager, SLOT(setEnabled(bool)));
connect(addButton, SIGNAL(clicked()), this, SLOT(addCustomRule()));
connect(reloadButton, SIGNAL(clicked()), this, SLOT(updateSubscription()));
connect(search, SIGNAL(textChanged(QString)), treeWidget, SLOT(filterStringWithoutTopItems(QString)));
connect(manager->subscription(), SIGNAL(changed()), this, SLOT(refreshAfterUpdate()));
connect(m_manager->subscription(), SIGNAL(changed()), this, SLOT(refreshAfterUpdate()));
QTimer::singleShot(0, this, SLOT(firstRefresh()));
// QTimer::singleShot(0, this, SLOT(firstRefresh()));
firstRefresh();
}
void AdBlockDialog::firstRefresh()
@ -86,7 +82,6 @@ void AdBlockDialog::refresh()
m_itemChangingBlock = true;
treeWidget->setUpdatesEnabled(false);
treeWidget->clear();
AdBlockManager *manager = AdBlockManager::instance();
QFont boldFont;
boldFont.setBold(true);
@ -104,7 +99,7 @@ void AdBlockDialog::refresh()
treeWidget->addTopLevelItem(m_easyListItem);
bool customRulesStarted = false;
QList<AdBlockRule> allRules = manager->subscription()->allRules();
QList<AdBlockRule> allRules = m_manager->subscription()->allRules();
int index = 0;
foreach (const AdBlockRule rule, allRules) {
@ -143,7 +138,7 @@ void AdBlockDialog::itemChanged(QTreeWidgetItem *item)
item->setText(0, item->text(0).prepend("!"));
AdBlockRule rul(item->text(0));
AdBlockManager::instance()->subscription()->replaceRule(rul, offset);
m_manager->subscription()->replaceRule(rul, offset);
} else if (item->checkState(0) == Qt::Checked && item->text(0).startsWith("!")) { //Enable rule
int offset = item->whatsThis(0).toInt();
@ -152,13 +147,13 @@ void AdBlockDialog::itemChanged(QTreeWidgetItem *item)
item->setText(0, newText);
AdBlockRule rul(newText);
AdBlockManager::instance()->subscription()->replaceRule(rul, offset);
m_manager->subscription()->replaceRule(rul, offset);
} else { //Custom rule has been changed
int offset = item->whatsThis(0).toInt();
AdBlockRule rul(item->text(0));
AdBlockManager::instance()->subscription()->replaceRule(rul, offset);
m_manager->subscription()->replaceRule(rul, offset);
}
@ -172,8 +167,7 @@ void AdBlockDialog::addCustomRule()
if (newRule.isEmpty())
return;
AdBlockManager *manager = AdBlockManager::instance();
AdBlockSubscription *subscription = manager->subscription();
AdBlockSubscription *subscription = m_manager->subscription();
subscription->addRule(AdBlockRule(newRule));
m_itemChangingBlock = true;
QTreeWidgetItem* item = new QTreeWidgetItem(m_customListItem);
@ -186,6 +180,6 @@ void AdBlockDialog::addCustomRule()
void AdBlockDialog::updateSubscription()
{
AdBlockSubscription *subscription = AdBlockManager::instance()->subscription();
AdBlockSubscription *subscription = m_manager->subscription();
subscription->updateNow();
}

View File

@ -61,6 +61,7 @@
#include "ui_adblockdialog.h"
class AdBlockModel;
class AdBlockManager;
class TreeSortFilterProxyModel;
class AdBlockDialog : public QDialog, public Ui_AdBlockDialog
{
@ -82,6 +83,7 @@ private:
bool m_itemChangingBlock;
QTreeWidgetItem* m_customListItem;
QTreeWidgetItem* m_easyListItem;
AdBlockManager* m_manager;
};

View File

@ -0,0 +1,66 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2011 nowrep
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
* ============================================================ */
#include "adblockicon.h"
#include "adblockmanager.h"
#include "qupzilla.h"
#include "webpage.h"
AdBlockIcon::AdBlockIcon(QupZilla *mainClass, QWidget *parent)
:ClickableLabel(parent)
,p_QupZilla(mainClass)
{
setPixmap(QPixmap(":/icons/other/adblock.png"));
setMaximumHeight(16);
setCursor(Qt::PointingHandCursor);
setToolTip(tr("AdBlock let you block any unwanted content on pages"));
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
}
void AdBlockIcon::showMenu(const QPoint &pos)
{
AdBlockManager* manager = AdBlockManager::instance();
QMenu menu;
menu.addAction(tr("Show AdBlock Settings"), manager, SLOT(showDialog()));
menu.addSeparator();
QList<WebPage::AdBlockedEntry> entries = p_QupZilla->weView()->webPage()->adBlockedEntries();
if (entries.isEmpty())
menu.addAction(tr("No content blocked"))->setEnabled(false);
else {
menu.addAction(tr("Blocked URL (AdBlock Rule) - click to edit rule"))->setEnabled(false);
foreach (WebPage::AdBlockedEntry entry, entries) {
QString address = entry.url.toString().right(55);
menu.addAction(tr("%1 with (%2)").arg(address, entry.rule), manager, SLOT(showRule()))->setData(entry.rule);
}
}
menu.addSeparator();
menu.addAction(tr("Learn About Writing Rules"), this, SLOT(learnAboutRules()));
menu.exec(pos);
}
void AdBlockIcon::learnAboutRules()
{
p_QupZilla->tabWidget()->addView(QUrl("http://adblockplus.org/en/filters"), tr("New tab"), TabWidget::NewSelectedTab);
}
AdBlockIcon::~AdBlockIcon()
{
}

47
src/adblock/adblockicon.h Normal file
View File

@ -0,0 +1,47 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2011 nowrep
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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 ADBLOCKICON_H
#define ADBLOCKICON_H
#include <QObject>
#include <QMenu>
#include "clickablelabel.h"
class QupZilla;
class AdBlockIcon : public ClickableLabel
{
Q_OBJECT
public:
explicit AdBlockIcon(QupZilla* mainClass, QWidget* parent = 0);
~AdBlockIcon();
signals:
public slots:
private slots:
void showMenu(const QPoint &pos);
void learnAboutRules();
private:
QupZilla* p_QupZilla;
};
#endif // ADBLOCKICON_H

View File

@ -65,10 +65,9 @@ AdBlockManager::AdBlockManager(QObject* parent)
AdBlockManager* AdBlockManager::instance()
{
if (!s_adBlockManager) {
qDebug() << "creating adblock manager";
if (!s_adBlockManager)
s_adBlockManager = new AdBlockManager(mApp->networkManager());
}
return s_adBlockManager;
}
@ -78,6 +77,7 @@ void AdBlockManager::setEnabled(bool enabled)
return;
m_enabled = enabled;
emit rulesChanged();
mApp->sendMessages(MainApplication::SetAdBlockIconEnabled, enabled);
}
AdBlockNetwork* AdBlockManager::network()
@ -132,6 +132,14 @@ AdBlockDialog* AdBlockManager::showDialog()
return m_adBlockDialog;
}
void AdBlockManager::showRule()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
AdBlockDialog* dialog = showDialog();
dialog->search->setText(action->data().toString());
}
}
AdBlockManager::~AdBlockManager()
{
}

View File

@ -78,7 +78,8 @@ public:
public slots:
void setEnabled(bool enabled);
AdBlockDialog *showDialog();
AdBlockDialog* showDialog();
void showRule();
private:
static AdBlockManager* s_adBlockManager;
@ -86,8 +87,8 @@ private:
bool m_loaded;
bool m_enabled;
QPointer<AdBlockDialog> m_adBlockDialog;
AdBlockNetwork *m_adBlockNetwork;
AdBlockPage *m_adBlockPage;
AdBlockNetwork* m_adBlockNetwork;
AdBlockPage* m_adBlockPage;
AdBlockSubscription* m_subscription;
};

View File

@ -48,19 +48,20 @@
#include "adblockmanager.h"
#include "adblocksubscription.h"
#include "mainapplication.h"
#include "webpage.h"
AdBlockNetwork::AdBlockNetwork(QObject *parent)
: QObject(parent)
{
}
QNetworkReply *AdBlockNetwork::block(const QNetworkRequest &request)
QNetworkReply* AdBlockNetwork::block(const QNetworkRequest &request)
{
QUrl url = request.url();
if (url.scheme() == "data")
return 0;
AdBlockManager *manager = AdBlockManager::instance();
AdBlockManager* manager = AdBlockManager::instance();
if (!manager->isEnabled())
return 0;
@ -71,11 +72,16 @@ QNetworkReply *AdBlockNetwork::block(const QNetworkRequest &request)
if (subscription->allow(urlString))
return 0;
if (const AdBlockRule *rule = subscription->block(urlString))
if (const AdBlockRule* rule = subscription->block(urlString))
blockedRule = rule;
if (blockedRule) {
AdBlockBlockedNetworkReply *reply = new AdBlockBlockedNetworkReply(request, blockedRule, this);
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
WebPage* webPage = (WebPage*)(v.value<void*>());
if (webPage)
webPage->addAdBlockRule(blockedRule->filter(), request.url());
AdBlockBlockedNetworkReply* reply = new AdBlockBlockedNetworkReply(request, blockedRule, this);
return reply;
}
return 0;

View File

@ -47,7 +47,7 @@
#include "networkmanager.h"
// #define ADBLOCKSUBSCRIPTION_DEBUG
AdBlockSubscription::AdBlockSubscription(QObject *parent)
AdBlockSubscription::AdBlockSubscription(QObject* parent)
: QObject(parent)
, m_downloading(0)
{
@ -88,14 +88,14 @@ void AdBlockSubscription::updateNow()
return;
QNetworkRequest request(QUrl("https://easylist-downloads.adblockplus.org/easylist.txt"));
QNetworkReply *reply = mApp->networkManager()->get(request);
QNetworkReply* reply = mApp->networkManager()->get(request);
m_downloading = reply;
connect(reply, SIGNAL(finished()), this, SLOT(rulesDownloaded()));
}
void AdBlockSubscription::rulesDownloaded()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply)
return;
@ -111,7 +111,7 @@ void AdBlockSubscription::rulesDownloaded()
QString fileName = mApp->getActiveProfil()+"adblocklist.txt";
QFile file(fileName);
if (!file.open(QFile::ReadWrite)) {
if (!file.open(QFile::WriteOnly)) {
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << fileName;
return;
}
@ -155,7 +155,7 @@ void AdBlockSubscription::saveRules()
const AdBlockRule* AdBlockSubscription::allow(const QString &urlString) const
{
foreach (const AdBlockRule *rule, m_networkExceptionRules) {
foreach (const AdBlockRule* rule, m_networkExceptionRules) {
if (rule->networkMatch(urlString))
return rule;
}
@ -208,7 +208,7 @@ void AdBlockSubscription::populateCache()
m_pageRules.clear();
for (int i = 0; i < m_rules.count(); ++i) {
const AdBlockRule *rule = &m_rules.at(i);
const AdBlockRule* rule = &m_rules.at(i);
if (!rule->isEnabled())
continue;

View File

@ -77,8 +77,8 @@ public:
void saveRules();
const AdBlockRule *allow(const QString &urlString) const;
const AdBlockRule *block(const QString &urlString) const;
const AdBlockRule* allow(const QString &urlString) const;
const AdBlockRule* block(const QString &urlString) const;
QList<const AdBlockRule*> pageRules() const { return m_pageRules; }
QList<AdBlockRule> allRules() const;
@ -98,7 +98,7 @@ private:
QString m_title;
bool m_enabled;
QNetworkReply *m_downloading;
QNetworkReply* m_downloading;
QList<AdBlockRule> m_rules;
// sorted list

View File

@ -20,7 +20,7 @@
#include "bookmarkstoolbar.h"
#include "locationbar.h"
#include "clickablelabel.h"
#include "adblockmanager.h"
#include "adblockicon.h"
void QupZilla::postLaunch()
{
@ -149,12 +149,7 @@ void QupZilla::setupUi()
m_privateBrowsing->setPixmap(QPixmap(":/icons/locationbar/privatebrowsing.png"));
m_privateBrowsing->setVisible(false);
m_privateBrowsing->setToolTip(tr("Private Browsing Enabled"));
m_adblockIcon = new ClickableLabel(this);
m_adblockIcon->setPixmap(QPixmap(":/icons/other/adblock.png"));
m_adblockIcon->setMaximumHeight(16);
m_adblockIcon->setVisible(false);
m_adblockIcon->setCursor(Qt::PointingHandCursor);
m_adblockIcon->setToolTip(tr("Click to show AdBlock options"));
m_adblockIcon = new AdBlockIcon(this);
m_ipLabel = new QLabel(this);
m_ipLabel->setStyleSheet("padding-right: 5px;");
m_ipLabel->setToolTip(tr("IP Address of current page"));
@ -288,7 +283,6 @@ void QupZilla::setupMenu()
connect(m_buttonReload, SIGNAL(triggered()), this, SLOT(reload()));
connect(m_buttonHome, SIGNAL(triggered()), this, SLOT(goHome()));
connect(m_actionExitFullscreen, SIGNAL(triggered(bool)), this, SLOT(fullScreen(bool)));
connect(m_adblockIcon, SIGNAL(clicked(QPoint)), AdBlockManager::instance(), SLOT(showDialog()));
//Make shortcuts available even in fullscreen (menu hidden)
QList<QAction*> actions = menuBar()->actions();

View File

@ -50,7 +50,7 @@ public:
QString DATADIR;
explicit MainApplication(int &argc, char **argv);
enum MessageType{ ShowFlashIcon, CheckPrivateBrowsing };
enum MessageType{ SetAdBlockIconEnabled, CheckPrivateBrowsing };
void loadSettings();
bool restoreStateSlot(QupZilla* window);

View File

@ -91,11 +91,6 @@ void QupZilla::loadSettings()
QWebSettings* websettings=mApp->webSettings();
websettings->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
//Web browsing settings
settings.beginGroup("Web-Browser-Settings");
bool allowFlash = settings.value("allowFlash",true).toBool();
settings.endGroup();
m_adblockIcon->setVisible(allowFlash);
//Browser Window settings
settings.beginGroup("Browser-View-Settings");
@ -111,6 +106,9 @@ void QupZilla::loadSettings()
bool showMenuBar = settings.value("showMenubar",true).toBool();
bool makeTransparent = settings.value("useTransparentBackground",false).toBool();
settings.endGroup();
bool adBlockEnabled = settings.value("AdBlock/enabled", true).toBool();
m_adblockIcon->setEnabled(adBlockEnabled);
statusBar()->setVisible(showStatusBar);
m_actionShowStatusbar->setChecked(showStatusBar);
@ -157,8 +155,8 @@ void QupZilla::loadSettings()
void QupZilla::receiveMessage(MainApplication::MessageType mes, bool state)
{
switch (mes) {
case MainApplication::ShowFlashIcon:
m_adblockIcon->setVisible(state);
case MainApplication::SetAdBlockIconEnabled:
m_adblockIcon->setEnabled(state);
break;
case MainApplication::CheckPrivateBrowsing:
@ -526,12 +524,8 @@ void QupZilla::showDownloadManager()
void QupZilla::showPreferences()
{
bool flashIconVisibility = m_adblockIcon->isVisible();
Preferences prefs(this, this);
prefs.exec();
if (flashIconVisibility != m_adblockIcon->isVisible())
emit message(MainApplication::ShowFlashIcon, m_adblockIcon->isVisible());
}
void QupZilla::showSource()

View File

@ -186,17 +186,14 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op
QNetworkRequest req = request;
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
// if (QNetworkReply* repl = mApp->plugins()->createNetworkRequest(op, request, outgoingData))
// return repl;
// Adblock
if (op == QNetworkAccessManager::GetOperation) {
if (!m_adblockNetwork)
m_adblockNetwork = AdBlockManager::instance()->network();
QNetworkReply* reply = m_adblockNetwork->block(req);
if (reply)
return reply;
}
if (op == QNetworkAccessManager::GetOperation) {
if (!m_adblockNetwork)
m_adblockNetwork = AdBlockManager::instance()->network();
QNetworkReply* reply = m_adblockNetwork->block(req);
if (reply)
return reply;
}
QNetworkReply* reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
return reply;

View File

@ -123,6 +123,15 @@ QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
return p_QupZilla->weView(index)->page();
}
void WebPage::addAdBlockRule(const QString &filter, const QUrl &url)
{
AdBlockedEntry entry;
entry.rule = filter;
entry.url = url;
m_adBlockedEntries.append(entry);
}
bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
{
if (extension == ChooseMultipleFilesExtension)

View File

@ -36,6 +36,11 @@ class WebPage : public QWebPage
{
Q_OBJECT
public:
struct AdBlockedEntry {
QString rule;
QUrl url;
};
WebPage(WebView* parent, QupZilla* mainClass);
void populateNetworkRequest(QNetworkRequest &request);
~WebPage();
@ -46,10 +51,13 @@ public:
bool supportsExtension(Extension extension) const { return (extension == ErrorPageExtension); }
bool extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output);
void addAdBlockRule(const QString &filter, const QUrl &url);
QList<AdBlockedEntry> adBlockedEntries() { return m_adBlockedEntries; }
protected slots:
QWebPage* createWindow(QWebPage::WebWindowType type);
void handleUnsupportedContent(QNetworkReply* url);
void clearSSLCert() { m_SslCert = 0; }
void clearSSLCert() { m_SslCert = 0; m_adBlockedEntries.clear(); }
protected:
bool acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type);
@ -59,6 +67,7 @@ protected:
QWebPage::NavigationType m_lastRequestType;
WebView* m_view;
QSslCertificate m_SslCert;
QList<AdBlockedEntry> m_adBlockedEntries;
// bool m_isOpeningNextWindowAsNewTab;
};

View File

@ -95,7 +95,8 @@ void WebView::urlChanged(const QUrl &url)
emit showUrl(url);
p_QupZilla->refreshHistory();
}
emit changed();
if (m_lastUrl != url)
emit changed();
}
void WebView::linkClicked(const QUrl &url)