diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index d0172d335..5b3e02ed4 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -60,6 +60,7 @@ set(SRCS ${SRCS} adblock/adblocksearchtree.cpp adblock/adblocksubscription.cpp adblock/adblocktreewidget.cpp + adblock/adblockplugin.cpp app/autosaver.cpp app/browserwindow.cpp app/commandlineoptions.cpp @@ -290,6 +291,7 @@ qt5_add_resources(SRCS data/html.qrc data/icons.qrc data/breeze-fallback.qrc + adblock/adblock.qrc ) add_library(FalkonPrivate SHARED ${SRCS}) diff --git a/src/lib/adblock/adblock.qrc b/src/lib/adblock/adblock.qrc new file mode 100644 index 000000000..903bcb64a --- /dev/null +++ b/src/lib/adblock/adblock.qrc @@ -0,0 +1,7 @@ + + + data/adblock.png + data/adblock_big.png + data/adblock.html + + diff --git a/src/lib/adblock/adblockicon.cpp b/src/lib/adblock/adblockicon.cpp index 5d0a755d5..dd430b170 100644 --- a/src/lib/adblock/adblockicon.cpp +++ b/src/lib/adblock/adblockicon.cpp @@ -33,7 +33,7 @@ AdBlockIcon::AdBlockIcon(QObject *parent) : AbstractButtonInterface(parent) { setTitle(tr("AdBlock")); - setIcon(QIcon(QSL(":icons/other/adblock.png"))); + setIcon(QIcon(QSL(":adblock/data/adblock.png"))); updateState(); diff --git a/src/lib/adblock/adblockplugin.cpp b/src/lib/adblock/adblockplugin.cpp new file mode 100644 index 000000000..5b138a4e2 --- /dev/null +++ b/src/lib/adblock/adblockplugin.cpp @@ -0,0 +1,61 @@ +/* ============================================================ +* QupZilla - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 "adblockplugin.h" +#include "adblockmanager.h" +#include "adblockicon.h" + +#include "scripts.h" +#include "webpage.h" +#include "pluginproxy.h" +#include "browserwindow.h" +#include "navigationbar.h" +#include "mainapplication.h" + +AdBlockPlugin::AdBlockPlugin(QObject *parent) + : QObject(parent) +{ + connect(mApp, &MainApplication::aboutToQuit, AdBlockManager::instance(), &AdBlockManager::save); + connect(mApp->plugins(), &PluginProxy::webPageCreated, this, &AdBlockPlugin::webPageCreated); + connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &AdBlockPlugin::mainWindowCreated); +} + +void AdBlockPlugin::webPageCreated(WebPage *page) +{ + connect(page, &WebPage::loadFinished, this, [=]() { + AdBlockManager *manager = AdBlockManager::instance(); + if (!manager->isEnabled()) { + return; + } + // Apply global element hiding rules + const QString elementHiding = manager->elementHidingRules(page->url()); + if (!elementHiding.isEmpty()) { + page->runJavaScript(Scripts::setCss(elementHiding), WebPage::SafeJsWorld); + } + // Apply domain-specific element hiding rules + const QString siteElementHiding = manager->elementHidingRulesForDomain(page->url()); + if (!siteElementHiding.isEmpty()) { + page->runJavaScript(Scripts::setCss(siteElementHiding), WebPage::SafeJsWorld); + } + }); +} + +void AdBlockPlugin::mainWindowCreated(BrowserWindow *window) +{ + window->navigationBar()->addToolButton(new AdBlockIcon(window)); +} diff --git a/src/lib/adblock/adblockplugin.h b/src/lib/adblock/adblockplugin.h new file mode 100644 index 000000000..64372ef61 --- /dev/null +++ b/src/lib/adblock/adblockplugin.h @@ -0,0 +1,33 @@ +/* ============================================================ +* QupZilla - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#pragma once + +#include + +class WebPage; +class BrowserWindow; + +class AdBlockPlugin : public QObject +{ +public: + explicit AdBlockPlugin(QObject *parent = nullptr); + +private: + void webPageCreated(WebPage *page); + void mainWindowCreated(BrowserWindow *window); +}; diff --git a/src/lib/data/html/adblock.html b/src/lib/adblock/data/adblock.html similarity index 100% rename from src/lib/data/html/adblock.html rename to src/lib/adblock/data/adblock.html diff --git a/src/lib/data/icons/other/adblock.png b/src/lib/adblock/data/adblock.png similarity index 100% rename from src/lib/data/icons/other/adblock.png rename to src/lib/adblock/data/adblock.png diff --git a/src/lib/data/html/adblock_big.png b/src/lib/adblock/data/adblock_big.png similarity index 100% rename from src/lib/data/html/adblock_big.png rename to src/lib/adblock/data/adblock_big.png diff --git a/src/lib/app/browserwindow.cpp b/src/lib/app/browserwindow.cpp index 05135c2c9..e3916639b 100644 --- a/src/lib/app/browserwindow.cpp +++ b/src/lib/app/browserwindow.cpp @@ -38,7 +38,6 @@ #include "docktitlebarwidget.h" #include "iconprovider.h" #include "progressbar.h" -#include "adblockicon.h" #include "closedwindowsmanager.h" #include "statusbarmessage.h" #include "browsinglibrary.h" @@ -389,8 +388,6 @@ void BrowserWindow::setupUi() statusBar()->addPermanentWidget(m_progressBar); statusBar()->addPermanentWidget(m_ipLabel); - m_navigationToolbar->addToolButton(new AdBlockIcon(this)); - QDesktopWidget* desktop = mApp->desktop(); int windowWidth = desktop->availableGeometry().width() / 1.3; int windowHeight = desktop->availableGeometry().height() / 1.3; diff --git a/src/lib/app/browserwindow.h b/src/lib/app/browserwindow.h index 15b6df961..f30c99cd4 100644 --- a/src/lib/app/browserwindow.h +++ b/src/lib/app/browserwindow.h @@ -43,7 +43,6 @@ class AutoFill; class MainApplication; class WebView; class WebPage; -class AdBlockIcon; class SideBar; class SideBarManager; class ProgressBar; diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index a02614d6a..fd4c65f2f 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -35,7 +35,6 @@ #include "checkboxdialog.h" #include "networkmanager.h" #include "profilemanager.h" -#include "adblockmanager.h" #include "restoremanager.h" #include "browsinglibrary.h" #include "downloadmanager.h" @@ -48,6 +47,7 @@ #include "scripts.h" #include "sessionmanager.h" #include "closedwindowsmanager.h" +#include "adblock/adblockplugin.h" #include #include @@ -310,6 +310,7 @@ MainApplication::MainApplication(int &argc, char** argv) m_plugins = new PluginProxy; m_autoFill = new AutoFill(this); + new AdBlockPlugin(this); if (!noAddons) m_plugins->loadPlugins(); @@ -795,7 +796,6 @@ void MainApplication::saveSettings() m_networkManager->shutdown(); qzSettings->saveSettings(); - AdBlockManager::instance()->save(); QFile::remove(DataPaths::currentProfilePath() + QLatin1String("/WebpageIcons.db")); sessionManager()->saveSettings(); diff --git a/src/lib/data/html.qrc b/src/lib/data/html.qrc index 5b88ed5e2..78e215373 100644 --- a/src/lib/data/html.qrc +++ b/src/lib/data/html.qrc @@ -1,7 +1,5 @@ - html/adblock_big.png - html/adblock.html html/about.html html/copyright html/reportbug.html diff --git a/src/lib/data/icons.qrc b/src/lib/data/icons.qrc index 04b265c86..316068927 100644 --- a/src/lib/data/icons.qrc +++ b/src/lib/data/icons.qrc @@ -18,11 +18,9 @@ icons/other/login.png icons/preferences/applications-graphics.png icons/preferences/document-properties.png - icons/other/adblock.png icons/other/download.svg icons/other/bighistory.svg icons/other/bighistory-selected.svg - icons/other/adblock-disabled.png icons/menu/search-icon.svg icons/browsers/firefox.png icons/browsers/firefox@2x.png diff --git a/src/lib/data/icons/other/adblock-disabled.png b/src/lib/data/icons/other/adblock-disabled.png deleted file mode 100644 index 83503b38e..000000000 Binary files a/src/lib/data/icons/other/adblock-disabled.png and /dev/null differ diff --git a/src/lib/network/schemehandlers/falkonschemehandler.cpp b/src/lib/network/schemehandlers/falkonschemehandler.cpp index 70faf4d7a..4163a07b8 100644 --- a/src/lib/network/schemehandlers/falkonschemehandler.cpp +++ b/src/lib/network/schemehandlers/falkonschemehandler.cpp @@ -476,9 +476,9 @@ QString FalkonSchemeReply::adblockPage() static QString aPage; if (aPage.isEmpty()) { - aPage.append(QzTools::readAllFileContents(":html/adblock.html")); - aPage.replace(QLatin1String("%FAVICON%"), QLatin1String("qrc:html/adblock_big.png")); - aPage.replace(QLatin1String("%IMAGE%"), QLatin1String("qrc:html/adblock_big.png")); + aPage.append(QzTools::readAllFileContents(":adblock/data/adblock.html")); + aPage.replace(QLatin1String("%FAVICON%"), QLatin1String("qrc:adblock/data/adblock_big.png")); + aPage.replace(QLatin1String("%IMAGE%"), QLatin1String("qrc:adblock/data/adblock_big.png")); aPage.replace(QLatin1String("%TITLE%"), tr("Blocked content")); aPage = QzTools::applyDirectionToPage(aPage); } diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 5bbd1379e..6c8a560f1 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -28,7 +28,6 @@ #include "autofill.h" #include "popupwebview.h" #include "popupwindow.h" -#include "adblockmanager.h" #include "iconprovider.h" #include "qzsettings.h" #include "useragentmanager.h" @@ -37,9 +36,9 @@ #include "html5permissions/html5permissionsmanager.h" #include "javascript/externaljsobject.h" #include "tabwidget.h" -#include "scripts.h" #include "networkmanager.h" #include "webhittestresult.h" +#include "adblock/adblockmanager.h" #ifdef NONBLOCK_JS_DIALOGS #include "ui_jsconfirm.h" @@ -232,9 +231,6 @@ void WebPage::finished() m_fileWatcher->removePaths(m_fileWatcher->files()); } - // AdBlock - cleanBlockedObjects(); - // AutoFill m_passwordEntries = mApp->autoFill()->completePage(this, url()); } @@ -422,24 +418,6 @@ QVector WebPage::autoFillData() const return m_passwordEntries; } -void WebPage::cleanBlockedObjects() -{ - AdBlockManager* manager = AdBlockManager::instance(); - if (!manager->isEnabled()) { - return; - } - - // Apply global element hiding rules - const QString elementHiding = manager->elementHidingRules(url()); - if (!elementHiding.isEmpty()) - runJavaScript(Scripts::setCss(elementHiding), WebPage::SafeJsWorld); - - // Apply domain-specific element hiding rules - const QString siteElementHiding = manager->elementHidingRulesForDomain(url()); - if (!siteElementHiding.isEmpty()) - runJavaScript(Scripts::setCss(siteElementHiding), WebPage::SafeJsWorld); -} - bool WebPage::javaScriptPrompt(const QUrl &securityOrigin, const QString &msg, const QString &defaultValue, QString* result) { Q_UNUSED(securityOrigin) diff --git a/src/lib/webengine/webpage.h b/src/lib/webengine/webpage.h index ac05a6b71..7702f7539 100644 --- a/src/lib/webengine/webpage.h +++ b/src/lib/webengine/webpage.h @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2017 David Rosca +* Copyright (C) 2010-2018 David Rosca * * 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 @@ -77,7 +77,6 @@ protected slots: void finished(); private slots: - void cleanBlockedObjects(); void urlChanged(const QUrl &url); void watchedFileChanged(const QString &file); void windowCloseRequested();