diff --git a/src/adblock/adblockdialog.cpp b/src/adblock/adblockdialog.cpp index aa74968eb..1d4924b79 100644 --- a/src/adblock/adblockdialog.cpp +++ b/src/adblock/adblockdialog.cpp @@ -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 allRules = manager->subscription()->allRules(); + QList 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(); } diff --git a/src/adblock/adblockdialog.h b/src/adblock/adblockdialog.h index b9219716b..e565f1a33 100644 --- a/src/adblock/adblockdialog.h +++ b/src/adblock/adblockdialog.h @@ -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; }; diff --git a/src/adblock/adblockicon.cpp b/src/adblock/adblockicon.cpp new file mode 100644 index 000000000..1dd16d48c --- /dev/null +++ b/src/adblock/adblockicon.cpp @@ -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 . +* ============================================================ */ +#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 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() +{ + +} diff --git a/src/adblock/adblockicon.h b/src/adblock/adblockicon.h new file mode 100644 index 000000000..8256fb55c --- /dev/null +++ b/src/adblock/adblockicon.h @@ -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 . +* ============================================================ */ +#ifndef ADBLOCKICON_H +#define ADBLOCKICON_H + +#include +#include + +#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 diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index e08499eb7..715c411fe 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -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(sender())) { + AdBlockDialog* dialog = showDialog(); + dialog->search->setText(action->data().toString()); + } +} + AdBlockManager::~AdBlockManager() { } diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h index ebbaf0ee1..9f10c2927 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -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 m_adBlockDialog; - AdBlockNetwork *m_adBlockNetwork; - AdBlockPage *m_adBlockPage; + AdBlockNetwork* m_adBlockNetwork; + AdBlockPage* m_adBlockPage; AdBlockSubscription* m_subscription; }; diff --git a/src/adblock/adblocknetwork.cpp b/src/adblock/adblocknetwork.cpp index 9f0c4b1b9..196ee9b19 100644 --- a/src/adblock/adblocknetwork.cpp +++ b/src/adblock/adblocknetwork.cpp @@ -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()); + if (webPage) + webPage->addAdBlockRule(blockedRule->filter(), request.url()); + + AdBlockBlockedNetworkReply* reply = new AdBlockBlockedNetworkReply(request, blockedRule, this); return reply; } return 0; diff --git a/src/adblock/adblocksubscription.cpp b/src/adblock/adblocksubscription.cpp index dfcbd6769..564ab4bc0 100644 --- a/src/adblock/adblocksubscription.cpp +++ b/src/adblock/adblocksubscription.cpp @@ -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(sender()); + QNetworkReply* reply = qobject_cast(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; diff --git a/src/adblock/adblocksubscription.h b/src/adblock/adblocksubscription.h index 2528b443e..3477fc9eb 100644 --- a/src/adblock/adblocksubscription.h +++ b/src/adblock/adblocksubscription.h @@ -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 pageRules() const { return m_pageRules; } QList allRules() const; @@ -98,7 +98,7 @@ private: QString m_title; bool m_enabled; - QNetworkReply *m_downloading; + QNetworkReply* m_downloading; QList m_rules; // sorted list diff --git a/src/app/appui.cpp b/src/app/appui.cpp index 9371c546d..8f8a1bc03 100644 --- a/src/app/appui.cpp +++ b/src/app/appui.cpp @@ -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 actions = menuBar()->actions(); diff --git a/src/app/mainapplication.h b/src/app/mainapplication.h index cbc426b7b..c9a96a5c8 100644 --- a/src/app/mainapplication.h +++ b/src/app/mainapplication.h @@ -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); diff --git a/src/app/qupzilla.cpp b/src/app/qupzilla.cpp index 09c29ec3a..3c47411dd 100644 --- a/src/app/qupzilla.cpp +++ b/src/app/qupzilla.cpp @@ -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() diff --git a/src/network/networkmanager.cpp b/src/network/networkmanager.cpp index 4cf96da99..5e5edec4d 100644 --- a/src/network/networkmanager.cpp +++ b/src/network/networkmanager.cpp @@ -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; diff --git a/src/webview/webpage.cpp b/src/webview/webpage.cpp index 345312a14..001aece75 100644 --- a/src/webview/webpage.cpp +++ b/src/webview/webpage.cpp @@ -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) diff --git a/src/webview/webpage.h b/src/webview/webpage.h index f12df0bac..59369e3cb 100644 --- a/src/webview/webpage.h +++ b/src/webview/webpage.h @@ -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 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 m_adBlockedEntries; // bool m_isOpeningNextWindowAsNewTab; }; diff --git a/src/webview/webview.cpp b/src/webview/webview.cpp index 3f9e434b6..16c87e4ff 100644 --- a/src/webview/webview.cpp +++ b/src/webview/webview.cpp @@ -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)