Work on adblock manager.
This commit is contained in:
parent
b6d7e8be2e
commit
566b03df76
5 changed files with 321 additions and 338 deletions
|
@ -25,6 +25,12 @@
|
|||
#include <QLocale>
|
||||
|
||||
|
||||
// AdBlock.
|
||||
DKEY AdBlock::ID = "adblock";
|
||||
|
||||
DKEY AdBlock::AdBlockEnabled = "enabled";
|
||||
DVALUE(bool) AdBlock::AdBlockEnabledDef = false;
|
||||
|
||||
// Feeds.
|
||||
DKEY Feeds::ID = "feeds";
|
||||
|
||||
|
|
|
@ -40,6 +40,13 @@
|
|||
#define GROUP(x) x::ID
|
||||
|
||||
|
||||
namespace AdBlock {
|
||||
KEY ID;
|
||||
|
||||
KEY AdBlockEnabled;
|
||||
VALUE(bool) AdBlockEnabledDef;
|
||||
}
|
||||
|
||||
// Feeds.
|
||||
namespace Feeds {
|
||||
KEY ID;
|
||||
|
|
|
@ -16,11 +16,10 @@
|
|||
// 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 "adblockicon.h"
|
||||
#include "adblockrule.h"
|
||||
#include "adblockmanager.h"
|
||||
#include "adblocksubscription.h"
|
||||
#include "tabwidget.h"
|
||||
#include "network-web/adblock/adblockicon.h"
|
||||
#include "network-web/adblock/adblockrule.h"
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QTimer>
|
||||
|
|
|
@ -16,18 +16,14 @@
|
|||
// 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 "adblockmanager.h"
|
||||
#include "adblockdialog.h"
|
||||
#include "adblockmatcher.h"
|
||||
#include "adblocksubscription.h"
|
||||
#include "adblockurlinterceptor.h"
|
||||
#include "datapaths.h"
|
||||
#include "mainapplication.h"
|
||||
#include "webpage.h"
|
||||
#include "qztools.h"
|
||||
#include "browserwindow.h"
|
||||
#include "settings.h"
|
||||
#include "networkmanager.h"
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblockdialog.h"
|
||||
#include "network-web/adblock/adblockmatcher.h"
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "network-web/adblock/adblockurlinterceptor.h"
|
||||
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/settings.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QTextStream>
|
||||
|
@ -37,37 +33,25 @@
|
|||
#include <QUrlQuery>
|
||||
#include <QMutexLocker>
|
||||
#include <QSaveFile>
|
||||
|
||||
//#define ADBLOCK_DEBUG
|
||||
|
||||
#ifdef ADBLOCK_DEBUG
|
||||
#include <QElapsedTimer>
|
||||
#endif
|
||||
#include <QWebEngineUrlRequestInfo>
|
||||
|
||||
Q_GLOBAL_STATIC(AdBlockManager, qz_adblock_manager)
|
||||
|
||||
AdBlockManager::AdBlockManager(QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_loaded(false)
|
||||
, m_enabled(true)
|
||||
, m_matcher(new AdBlockMatcher(this))
|
||||
, m_interceptor(new AdBlockUrlInterceptor(this))
|
||||
{
|
||||
|
||||
AdBlockManager::AdBlockManager(QObject *parent)
|
||||
: QObject(parent), m_loaded(false), m_enabled(true), m_matcher(new AdBlockMatcher(this)), m_interceptor(new AdBlockUrlInterceptor(this)) {
|
||||
load();
|
||||
}
|
||||
|
||||
AdBlockManager::~AdBlockManager()
|
||||
{
|
||||
AdBlockManager::~AdBlockManager() {
|
||||
qDeleteAll(m_subscriptions);
|
||||
}
|
||||
|
||||
AdBlockManager* AdBlockManager::instance()
|
||||
{
|
||||
AdBlockManager *AdBlockManager::instance() {
|
||||
return qz_adblock_manager();
|
||||
}
|
||||
|
||||
void AdBlockManager::setEnabled(bool enabled)
|
||||
{
|
||||
void AdBlockManager::setEnabled(bool enabled) {
|
||||
if (m_enabled == enabled) {
|
||||
return;
|
||||
}
|
||||
|
@ -75,40 +59,32 @@ void AdBlockManager::setEnabled(bool enabled)
|
|||
m_enabled = enabled;
|
||||
emit enabledChanged(enabled);
|
||||
|
||||
Settings settings;
|
||||
settings.beginGroup("AdBlock");
|
||||
settings.setValue("enabled", m_enabled);
|
||||
settings.endGroup();
|
||||
|
||||
qApp->settings()->setValue(GROUP(Adblock), AdBlock::AdBlockEnabled, m_enabled);
|
||||
load();
|
||||
mApp->reloadUserStyleSheet();
|
||||
// TODO: Reload user stylesheet.
|
||||
//mApp->reloadUserStyleSheet();
|
||||
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
if (m_enabled) {
|
||||
m_matcher->update();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
m_matcher->clear();
|
||||
}
|
||||
}
|
||||
|
||||
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const
|
||||
{
|
||||
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const {
|
||||
return m_subscriptions;
|
||||
}
|
||||
|
||||
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request)
|
||||
{
|
||||
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) {
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
if (!isEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ADBLOCK_DEBUG
|
||||
QElapsedTimer timer;
|
||||
timer.start();
|
||||
#endif
|
||||
const QString urlString = request.requestUrl().toEncoded().toLower();
|
||||
const QString urlDomain = request.requestUrl().host().toLower();
|
||||
const QString urlScheme = request.requestUrl().scheme().toLower();
|
||||
|
@ -118,52 +94,45 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo &request)
|
|||
}
|
||||
|
||||
bool res = false;
|
||||
const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString);
|
||||
const AdBlockRule *blockedRule = m_matcher->match(request, urlDomain, urlString);
|
||||
|
||||
if (blockedRule) {
|
||||
res = true;
|
||||
|
||||
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
|
||||
QUrl url(QSL("qupzilla:adblock"));
|
||||
QUrlQuery query;
|
||||
query.addQueryItem(QSL("rule"), blockedRule->filter());
|
||||
query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title());
|
||||
url.setQuery(query);
|
||||
request.redirect(url);
|
||||
// We are blocking main URL frame, we can display "AdBlock error page" or redirect to somewhere.
|
||||
QMessageBox::warning(nullptr, "blocked website");
|
||||
|
||||
// TODO request.redirect() přesměrovat na "chybovou stranku";
|
||||
//QUrl url(QSL("rssguard:adblock"));
|
||||
//QUrlQuery query;
|
||||
//query.addQueryItem(QSL("rule"), blockedRule->filter());
|
||||
//query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title());
|
||||
//url.setQuery(query);
|
||||
//request.redirect(url);
|
||||
|
||||
request.block(true);
|
||||
}
|
||||
else {
|
||||
request.block(true);
|
||||
}
|
||||
|
||||
#ifdef ADBLOCK_DEBUG
|
||||
qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl();
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ADBLOCK_DEBUG
|
||||
qDebug() << timer.elapsed() << request.requestUrl();
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
QStringList AdBlockManager::disabledRules() const
|
||||
{
|
||||
QStringList AdBlockManager::disabledRules() const {
|
||||
return m_disabledRules;
|
||||
}
|
||||
|
||||
void AdBlockManager::addDisabledRule(const QString &filter)
|
||||
{
|
||||
void AdBlockManager::addDisabledRule(const QString &filter) {
|
||||
m_disabledRules.append(filter);
|
||||
}
|
||||
|
||||
void AdBlockManager::removeDisabledRule(const QString &filter)
|
||||
{
|
||||
void AdBlockManager::removeDisabledRule(const QString &filter) {
|
||||
m_disabledRules.removeOne(filter);
|
||||
}
|
||||
|
||||
bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url)
|
||||
{
|
||||
bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url) {
|
||||
const QList<QPair<QString, QString> > queryItems = QUrlQuery(url).queryItems(QUrl::FullyDecoded);
|
||||
|
||||
QString subscriptionTitle;
|
||||
|
@ -171,18 +140,23 @@ bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url)
|
|||
|
||||
for (int i = 0; i < queryItems.count(); ++i) {
|
||||
QPair<QString, QString> pair = queryItems.at(i);
|
||||
if (pair.first == QL1S("location"))
|
||||
|
||||
if (pair.first == QL1S("location")) {
|
||||
subscriptionUrl = pair.second;
|
||||
else if (pair.first == QL1S("title"))
|
||||
}
|
||||
else if (pair.first == QL1S("title")) {
|
||||
subscriptionTitle = pair.second;
|
||||
}
|
||||
}
|
||||
|
||||
if (subscriptionTitle.isEmpty() || subscriptionUrl.isEmpty())
|
||||
if (subscriptionTitle.isEmpty() || subscriptionUrl.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const QString message = AdBlockManager::tr("Do you want to add <b>%1</b> subscription?").arg(subscriptionTitle);
|
||||
const QString message = tr("Do you want to add <b>%1</b> subscription?").arg(subscriptionTitle);
|
||||
|
||||
QMessageBox::StandardButton result = QMessageBox::question(nullptr, tr("Add AdBlock subscription"), message, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
|
||||
|
||||
QMessageBox::StandardButton result = QMessageBox::question(0, AdBlockManager::tr("AdBlock Subscription"), message, QMessageBox::Yes | QMessageBox::No);
|
||||
if (result == QMessageBox::Yes) {
|
||||
AdBlockManager::instance()->addSubscription(subscriptionTitle, subscriptionUrl);
|
||||
AdBlockManager::instance()->showDialog();
|
||||
|
@ -191,8 +165,7 @@ bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url)
|
|||
return true;
|
||||
}
|
||||
|
||||
AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const QString &url)
|
||||
{
|
||||
AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) {
|
||||
if (title.isEmpty() || url.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -204,13 +177,14 @@ AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const
|
|||
|
||||
QSaveFile file(filePath);
|
||||
if (!file.open(QFile::WriteOnly)) {
|
||||
qWarning() << "AdBlockManager: Cannot write to file" << filePath;
|
||||
qWarning("Cannot save AdBlock subscription to file '%s'.", qPrintable(filePath));
|
||||
return 0;
|
||||
}
|
||||
|
||||
file.write(data);
|
||||
file.commit();
|
||||
|
||||
AdBlockSubscription* subscription = new AdBlockSubscription(title, this);
|
||||
AdBlockSubscription *subscription = new AdBlockSubscription(title, this);
|
||||
subscription->setUrl(QUrl(url));
|
||||
subscription->setFilePath(filePath);
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
|
@ -222,8 +196,7 @@ AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const
|
|||
return subscription;
|
||||
}
|
||||
|
||||
bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription)
|
||||
{
|
||||
bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) {
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) {
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
#include <QPointer>
|
||||
#include <QMutex>
|
||||
|
||||
#include "qzcommon.h"
|
||||
|
||||
class QUrl;
|
||||
class QWebEngineUrlRequestInfo;
|
||||
|
@ -36,13 +35,12 @@ class AdBlockCustomList;
|
|||
class AdBlockSubscription;
|
||||
class AdBlockUrlInterceptor;
|
||||
|
||||
class QUPZILLA_EXPORT AdBlockManager : public QObject
|
||||
{
|
||||
class AdBlockManager : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AdBlockManager(QObject* parent = 0);
|
||||
~AdBlockManager();
|
||||
public:
|
||||
explicit AdBlockManager(QObject *parent = 0);
|
||||
virtual ~AdBlockManager();
|
||||
|
||||
void load();
|
||||
void save();
|
||||
|
@ -53,7 +51,7 @@ public:
|
|||
QString elementHidingRules(const QUrl &url) const;
|
||||
QString elementHidingRulesForDomain(const QUrl &url) const;
|
||||
|
||||
AdBlockSubscription* subscriptionByName(const QString &name) const;
|
||||
AdBlockSubscription *subscriptionByName(const QString &name) const;
|
||||
QList<AdBlockSubscription*> subscriptions() const;
|
||||
|
||||
bool block(QWebEngineUrlRequestInfo &request);
|
||||
|
@ -64,33 +62,33 @@ public:
|
|||
|
||||
bool addSubscriptionFromUrl(const QUrl &url);
|
||||
|
||||
AdBlockSubscription* addSubscription(const QString &title, const QString &url);
|
||||
bool removeSubscription(AdBlockSubscription* subscription);
|
||||
AdBlockSubscription *addSubscription(const QString &title, const QString &url);
|
||||
bool removeSubscription(AdBlockSubscription *subscription);
|
||||
|
||||
AdBlockCustomList* customList() const;
|
||||
AdBlockCustomList *customList() const;
|
||||
|
||||
static AdBlockManager* instance();
|
||||
static AdBlockManager *instance();
|
||||
|
||||
signals:
|
||||
signals:
|
||||
void enabledChanged(bool enabled);
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
void setEnabled(bool enabled);
|
||||
void showRule();
|
||||
|
||||
void updateMatcher();
|
||||
void updateAllSubscriptions();
|
||||
|
||||
AdBlockDialog* showDialog();
|
||||
AdBlockDialog *showDialog();
|
||||
|
||||
private:
|
||||
private:
|
||||
inline bool canBeBlocked(const QUrl &url) const;
|
||||
|
||||
bool m_loaded;
|
||||
bool m_enabled;
|
||||
|
||||
QList<AdBlockSubscription*> m_subscriptions;
|
||||
AdBlockMatcher* m_matcher;
|
||||
AdBlockMatcher *m_matcher;
|
||||
QStringList m_disabledRules;
|
||||
|
||||
AdBlockUrlInterceptor *m_interceptor;
|
||||
|
|
Loading…
Add table
Reference in a new issue