Added custom adblocked page.

This commit is contained in:
Martin Rotter 2017-07-27 08:18:25 +02:00
parent 281e903752
commit c1b23b6f6d
13 changed files with 266 additions and 116 deletions

View File

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<div class="container">
<div class="row">
<div class="alert alert-danger" role="alert">
<span class="sr-only">Error:</span>
%1
</div>
%2
</div>
</div>

View File

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<div class="container">
<div class="row">
<div class="alert alert-danger" role="alert">
<span class="sr-only">Error:</span>
%1
</div>
%2
</div>
</div>

View File

@ -498,16 +498,18 @@ equals(USE_WEBENGINE, true) {
src/gui/webbrowser.h \ src/gui/webbrowser.h \
src/gui/discoverfeedsbutton.h \ src/gui/discoverfeedsbutton.h \
src/network-web/googlesuggest.h \ src/network-web/googlesuggest.h \
src/network-web/webpage.h src/network-web/webpage.h \
src/network-web/rssguardschemehandler.h
SOURCES += src/gui/locationlineedit.cpp \ SOURCES += src/gui/locationlineedit.cpp \
src/gui/webviewer.cpp \ src/gui/webviewer.cpp \
src/gui/webbrowser.cpp \ src/gui/webbrowser.cpp \
src/gui/discoverfeedsbutton.cpp \ src/gui/discoverfeedsbutton.cpp \
src/network-web/googlesuggest.cpp \ src/network-web/googlesuggest.cpp \
src/network-web/webpage.cpp src/network-web/webpage.cpp \
src/network-web/rssguardschemehandler.cpp
# Add Ad-Block sources. # Add AdBlock sources.
HEADERS += src/network-web/adblock/adblockaddsubscriptiondialog.h \ HEADERS += src/network-web/adblock/adblockaddsubscriptiondialog.h \
src/network-web/adblock/adblockdialog.h \ src/network-web/adblock/adblockdialog.h \
src/network-web/adblock/adblockicon.h \ src/network-web/adblock/adblockicon.h \

View File

@ -26,6 +26,7 @@
#define ARGUMENTS_LIST_SEPARATOR "\n" #define ARGUMENTS_LIST_SEPARATOR "\n"
#define ADBLOCK_ADBLOCKED_PAGE "adblockedpage"
#define ADBLOCK_HOWTO_FILTERS "http://adblockplus.org/en/filters" #define ADBLOCK_HOWTO_FILTERS "http://adblockplus.org/en/filters"
#define ADBLOCK_UPDATE_DAYS_INTERVAL 5 #define ADBLOCK_UPDATE_DAYS_INTERVAL 5
#define ADBLOCK_ICON_ACTIVE "adblock" #define ADBLOCK_ICON_ACTIVE "adblock"

View File

@ -73,7 +73,6 @@ FormMain::FormMain(QWidget* parent, Qt::WindowFlags f)
prepareMenus(); prepareMenus();
// Prepare tabs. // Prepare tabs.
//m_ui->m_tabWidget->initializeTabs();
tabWidget()->feedMessageViewer()->feedsToolBar()->loadSavedActions(); tabWidget()->feedMessageViewer()->feedsToolBar()->loadSavedActions();
tabWidget()->feedMessageViewer()->messagesToolBar()->loadSavedActions(); tabWidget()->feedMessageViewer()->messagesToolBar()->loadSavedActions();

View File

@ -42,6 +42,7 @@
#include "network-web/networkurlinterceptor.h" #include "network-web/networkurlinterceptor.h"
#include "network-web/adblock/adblockicon.h" #include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockmanager.h" #include "network-web/adblock/adblockmanager.h"
#include "network-web/rssguardschemehandler.h"
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <QWebEngineDownloadItem> #include <QWebEngineDownloadItem>
@ -71,6 +72,10 @@ Application::Application(const QString& id, int& argc, char** argv)
// TODO: Call load settings when saving app settings from dialog. // TODO: Call load settings when saving app settings from dialog.
// Will need add that if I add more settings in the future. // Will need add that if I add more settings in the future.
m_urlInterceptor->loadSettings(); m_urlInterceptor->loadSettings();
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler(
QByteArray(APP_LOW_NAME),
new RssGuardSchemeHandler(QWebEngineProfile::defaultProfile()));
#endif #endif
} }

View File

@ -32,140 +32,150 @@ SkinFactory::~SkinFactory() {
} }
void SkinFactory::loadCurrentSkin() { void SkinFactory::loadCurrentSkin() {
QList<QString> skin_names_to_try; QList<QString> skin_names_to_try;
skin_names_to_try.append(selectedSkinName()); skin_names_to_try.append(selectedSkinName());
skin_names_to_try.append(APP_SKIN_DEFAULT); skin_names_to_try.append(APP_SKIN_DEFAULT);
bool skin_parsed; bool skin_parsed;
Skin skin_data; Skin skin_data;
QString skin_name; QString skin_name;
while (!skin_names_to_try.isEmpty()) { while (!skin_names_to_try.isEmpty()) {
skin_name = skin_names_to_try.takeFirst(); skin_name = skin_names_to_try.takeFirst();
skin_data = skinInfo(skin_name, &skin_parsed); skin_data = skinInfo(skin_name, &skin_parsed);
if (skin_parsed) { if (skin_parsed) {
loadSkinFromData(skin_data); loadSkinFromData(skin_data);
// Set this 'Skin' object as active one. // Set this 'Skin' object as active one.
m_currentSkin = skin_data; m_currentSkin = skin_data;
qDebug("Skin '%s' loaded.", qPrintable(skin_name)); qDebug("Skin '%s' loaded.", qPrintable(skin_name));
return; return;
} }
else { else {
qWarning("Failed to load skin '%s'.", qPrintable(skin_name)); qWarning("Failed to load skin '%s'.", qPrintable(skin_name));
} }
} }
qCritical("Failed to load selected or default skin. Quitting!"); qCritical("Failed to load selected or default skin. Quitting!");
} }
void SkinFactory::loadSkinFromData(const Skin& skin) { void SkinFactory::loadSkinFromData(const Skin& skin) {
if (!skin.m_rawData.isEmpty()) { if (!skin.m_rawData.isEmpty()) {
qApp->setStyleSheet(skin.m_rawData); qApp->setStyleSheet(skin.m_rawData);
} }
qApp->setStyle(qApp->settings()->value(GROUP(GUI), SETTING(GUI::Style)).toString()); qApp->setStyle(qApp->settings()->value(GROUP(GUI), SETTING(GUI::Style)).toString());
} }
void SkinFactory::setCurrentSkinName(const QString& skin_name) { void SkinFactory::setCurrentSkinName(const QString& skin_name) {
qApp->settings()->setValue(GROUP(GUI), GUI::Skin, skin_name); qApp->settings()->setValue(GROUP(GUI), GUI::Skin, skin_name);
} }
QString SkinFactory::getUserSkinBaseFolder() const { QString SkinFactory::getUserSkinBaseFolder() const {
return qApp->getUserDataPath() + QDir::separator() + APP_SKIN_USER_FOLDER; return qApp->getUserDataPath() + QDir::separator() + APP_SKIN_USER_FOLDER;
} }
QString SkinFactory::selectedSkinName() const { QString SkinFactory::selectedSkinName() const {
return qApp->settings()->value(GROUP(GUI), SETTING(GUI::Skin)).toString(); return qApp->settings()->value(GROUP(GUI), SETTING(GUI::Skin)).toString();
}
QString SkinFactory::adBlockedPage(const QString& subscription, const QString& rule) {
const QString& adblocked = currentSkin().m_adblocked.arg(tr("This page was blocked by AdBlock"),
tr("Blocked by set: \"%1\"<br/>Blocked by filter: \"%2\"")
.arg(subscription,
rule));
return currentSkin().m_layoutMarkupWrapper.arg(tr("This page was blocked by AdBlock"), adblocked);
} }
Skin SkinFactory::skinInfo(const QString& skin_name, bool* ok) const { Skin SkinFactory::skinInfo(const QString& skin_name, bool* ok) const {
Skin skin; Skin skin;
QStringList base_skin_folders; QStringList base_skin_folders;
base_skin_folders.append(APP_SKIN_PATH); base_skin_folders.append(APP_SKIN_PATH);
base_skin_folders.append(getUserSkinBaseFolder()); base_skin_folders.append(getUserSkinBaseFolder());
while (!base_skin_folders.isEmpty()) { while (!base_skin_folders.isEmpty()) {
const QString skin_folder = base_skin_folders.takeAt(0) + QDir::separator() + skin_name + QDir::separator(); const QString skin_folder = base_skin_folders.takeAt(0) + QDir::separator() + skin_name + QDir::separator();
const QString metadata_file = skin_folder + APP_SKIN_METADATA_FILE; const QString metadata_file = skin_folder + APP_SKIN_METADATA_FILE;
if (QFile::exists(metadata_file)) { if (QFile::exists(metadata_file)) {
QFile skin_file(metadata_file); QFile skin_file(metadata_file);
QDomDocument dokument; QDomDocument dokument;
if (!skin_file.open(QIODevice::Text | QIODevice::ReadOnly) || !dokument.setContent(&skin_file, true)) { if (!skin_file.open(QIODevice::Text | QIODevice::ReadOnly) || !dokument.setContent(&skin_file, true)) {
if (ok) { if (ok) {
*ok = false; *ok = false;
} }
return skin; return skin;
} }
const QDomNode skin_node = dokument.namedItem(QSL("skin")); const QDomNode skin_node = dokument.namedItem(QSL("skin"));
// Obtain visible skin name. // Obtain visible skin name.
skin.m_visibleName = skin_name; skin.m_visibleName = skin_name;
// Obtain author. // Obtain author.
skin.m_author = skin_node.namedItem(QSL("author")).namedItem(QSL("name")).toElement().text(); skin.m_author = skin_node.namedItem(QSL("author")).namedItem(QSL("name")).toElement().text();
// Obtain email. // Obtain email.
skin.m_email = skin_node.namedItem(QSL("author")).namedItem(QSL("email")).toElement().text(); skin.m_email = skin_node.namedItem(QSL("author")).namedItem(QSL("email")).toElement().text();
// Obtain version. // Obtain version.
skin.m_version = skin_node.attributes().namedItem(QSL("version")).toAttr().value(); skin.m_version = skin_node.attributes().namedItem(QSL("version")).toAttr().value();
// Obtain other information. // Obtain other information.
skin.m_baseName = skin_name; skin.m_baseName = skin_name;
// Free resources. // Free resources.
skin_file.close(); skin_file.close();
skin_file.deleteLater(); skin_file.deleteLater();
// Here we use "/" instead of QDir::separator() because CSS2.1 url field // Here we use "/" instead of QDir::separator() because CSS2.1 url field
// accepts '/' as path elements separator. // accepts '/' as path elements separator.
// //
// "##" is placeholder for the actual path to skin file. This is needed for using // "##" is placeholder for the actual path to skin file. This is needed for using
// images within the QSS file. // images within the QSS file.
// So if one uses "##/images/border.png" in QSS then it is // So if one uses "##/images/border.png" in QSS then it is
// replaced by fully absolute path and target file can // replaced by fully absolute path and target file can
// be safely loaded. // be safely loaded.
skin.m_layoutMarkupWrapper = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_wrapper.html"))); skin.m_layoutMarkupWrapper = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_wrapper.html")));
skin.m_layoutMarkupWrapper = skin.m_layoutMarkupWrapper.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name); skin.m_layoutMarkupWrapper = skin.m_layoutMarkupWrapper.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_enclosureImageMarkup = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_enclosure_image.html"))); skin.m_enclosureImageMarkup = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_enclosure_image.html")));
skin.m_enclosureImageMarkup = skin.m_enclosureImageMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name); skin.m_enclosureImageMarkup = skin.m_enclosureImageMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_layoutMarkup = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_single_message.html"))); skin.m_layoutMarkup = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_single_message.html")));
skin.m_layoutMarkup = skin.m_layoutMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name); skin.m_layoutMarkup = skin.m_layoutMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_enclosureMarkup = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_enclosure_every.html"))); skin.m_enclosureMarkup = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_enclosure_every.html")));
skin.m_enclosureMarkup = skin.m_enclosureMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name); skin.m_enclosureMarkup = skin.m_enclosureMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_rawData = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("theme.css"))); skin.m_rawData = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("theme.css")));
skin.m_rawData = skin.m_rawData.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name); skin.m_rawData = skin.m_rawData.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_adblocked = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_adblocked.html")));
if (ok != nullptr) { if (ok != nullptr) {
*ok = !skin.m_author.isEmpty() && !skin.m_version.isEmpty() && *ok = !skin.m_author.isEmpty() && !skin.m_version.isEmpty() &&
!skin.m_baseName.isEmpty() && !skin.m_email.isEmpty() && !skin.m_baseName.isEmpty() && !skin.m_email.isEmpty() &&
!skin.m_layoutMarkup.isEmpty(); !skin.m_layoutMarkup.isEmpty();
} }
break; break;
} }
} }
return skin; return skin;
} }
QList<Skin> SkinFactory::installedSkins() const { QList<Skin> SkinFactory::installedSkins() const {
QList<Skin> skins; QList<Skin> skins;
bool skin_load_ok; bool skin_load_ok;
QStringList skin_directories = QDir(APP_SKIN_PATH).entryList(QDir::Dirs | QStringList skin_directories = QDir(APP_SKIN_PATH).entryList(QDir::Dirs |
QDir::NoDotAndDotDot | QDir::NoDotAndDotDot |
QDir::NoSymLinks | QDir::NoSymLinks |
QDir::Readable); QDir::Readable);
skin_directories.append(QDir(getUserSkinBaseFolder()).entryList(QDir::Dirs | skin_directories.append(QDir(getUserSkinBaseFolder()).entryList(QDir::Dirs |
QDir::NoDotAndDotDot | QDir::NoDotAndDotDot |
QDir::NoSymLinks | QDir::NoSymLinks |
QDir::Readable)); QDir::Readable));
foreach (const QString& base_directory, skin_directories) { foreach (const QString& base_directory, skin_directories) {
const Skin skin_info = skinInfo(base_directory, &skin_load_ok); const Skin skin_info = skinInfo(base_directory, &skin_load_ok);
if (skin_load_ok) { if (skin_load_ok) {
skins.append(skin_info); skins.append(skin_info);
} }
} }
return skins; return skins;
} }

View File

@ -31,6 +31,7 @@ struct Skin {
QString m_email; QString m_email;
QString m_version; QString m_version;
QString m_rawData; QString m_rawData;
QString m_adblocked;
QString m_layoutMarkupWrapper; QString m_layoutMarkupWrapper;
QString m_enclosureImageMarkup; QString m_enclosureImageMarkup;
QString m_layoutMarkup; QString m_layoutMarkup;
@ -60,6 +61,8 @@ class SkinFactory : public QObject {
// after application restart. // after application restart.
QString selectedSkinName() const; QString selectedSkinName() const;
QString adBlockedPage(const QString& subscription, const QString& rule);
// Gets skin about a particular skin. // Gets skin about a particular skin.
Skin skinInfo(const QString& skin_name, bool* ok = nullptr) const; Skin skinInfo(const QString& skin_name, bool* ok = nullptr) const;

View File

@ -103,16 +103,20 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo& request) {
const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString); const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString);
if (blockedRule) { if (blockedRule) {
res = true;
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
// NOTE: We are blocking main URL frame, we can display "AdBlock error page" or QUrlQuery query;
// redirect to somewhere. QUrl url(QSL("rssguard:adblockedpage"));
request.block(true);
query.addQueryItem(QSL("rule"), blockedRule->filter());
query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title());
url.setQuery(query);
request.redirect(url);
} }
else { else {
request.block(true); res = true;
request.block(true);
} }
} }

View File

@ -26,7 +26,5 @@ AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager* manager)
} }
void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) { void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) {
if (m_manager->block(info)) { info.block(m_manager->block(info));
info.block(true);
}
} }

View File

@ -0,0 +1,65 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard 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.
//
// RSS Guard 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 RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "network-web/rssguardschemehandler.h"
#include "definitions/definitions.h"
#include "miscellaneous/application.h"
#include "miscellaneous/skinfactory.h"
#include <QBuffer>
#include <QUrlQuery>
#include <QWebEngineUrlRequestJob>
RssGuardSchemeHandler::RssGuardSchemeHandler(QObject* parent) : QWebEngineUrlSchemeHandler(parent) {
}
RssGuardSchemeHandler::~RssGuardSchemeHandler() {
}
void RssGuardSchemeHandler::requestStarted(QWebEngineUrlRequestJob* job) {
// Decide which data we want.
QByteArray data = targetData(job->requestUrl());
if (data.isEmpty()) {
job->fail(QWebEngineUrlRequestJob::UrlNotFound);
}
else {
QBuffer* buf = new QBuffer(job);
buf->setData(data);
job->reply(QByteArray("text/html"), buf);
}
}
QByteArray RssGuardSchemeHandler::targetData(const QUrl& url) {
const QString& url_string = url.toString();
if (url_string.contains(QSL(ADBLOCK_ADBLOCKED_PAGE))) {
QUrlQuery query(url);
const QString& subscription = query.queryItemValue(QSL("subscription"));
const QString& rule = query.queryItemValue(QSL("rule"));
return qApp->skins()->adBlockedPage(subscription, rule).toUtf8();
}
else {
return QByteArray();
}
}

View File

@ -0,0 +1,42 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard 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.
//
// RSS Guard 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 RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef RSSGUARDSCHEMEHANDLER_H
#define RSSGUARDSCHEMEHANDLER_H
#include <QIODevice>
#include <QWebEngineUrlSchemeHandler>
class QWebEngineUrlRequestJob;
class QBuffer;
class RssGuardSchemeHandler : public QWebEngineUrlSchemeHandler {
Q_OBJECT
public:
explicit RssGuardSchemeHandler(QObject* parent = nullptr);
virtual ~RssGuardSchemeHandler();
void requestStarted(QWebEngineUrlRequestJob* job);
private:
QByteArray targetData(const QUrl& url);
};
#endif // RSSGUARDSCHEMEHANDLER_H

View File

@ -70,7 +70,6 @@ bool WebPage::acceptNavigationRequest(const QUrl& url, NavigationType type, bool
setHtml(view()->messageContents(), QUrl(INTERNAL_URL_MESSAGE)); setHtml(view()->messageContents(), QUrl(INTERNAL_URL_MESSAGE));
return true; return true;
} }
else { else {
return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame); return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame);
} }