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>
|
#include <QLocale>
|
||||||
|
|
||||||
|
|
||||||
|
// AdBlock.
|
||||||
|
DKEY AdBlock::ID = "adblock";
|
||||||
|
|
||||||
|
DKEY AdBlock::AdBlockEnabled = "enabled";
|
||||||
|
DVALUE(bool) AdBlock::AdBlockEnabledDef = false;
|
||||||
|
|
||||||
// Feeds.
|
// Feeds.
|
||||||
DKEY Feeds::ID = "feeds";
|
DKEY Feeds::ID = "feeds";
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,13 @@
|
||||||
#define GROUP(x) x::ID
|
#define GROUP(x) x::ID
|
||||||
|
|
||||||
|
|
||||||
|
namespace AdBlock {
|
||||||
|
KEY ID;
|
||||||
|
|
||||||
|
KEY AdBlockEnabled;
|
||||||
|
VALUE(bool) AdBlockEnabledDef;
|
||||||
|
}
|
||||||
|
|
||||||
// Feeds.
|
// Feeds.
|
||||||
namespace Feeds {
|
namespace Feeds {
|
||||||
KEY ID;
|
KEY ID;
|
||||||
|
|
|
@ -16,11 +16,10 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include "adblockicon.h"
|
#include "network-web/adblock/adblockicon.h"
|
||||||
#include "adblockrule.h"
|
#include "network-web/adblock/adblockrule.h"
|
||||||
#include "adblockmanager.h"
|
#include "network-web/adblock/adblockmanager.h"
|
||||||
#include "adblocksubscription.h"
|
#include "network-web/adblock/adblocksubscription.h"
|
||||||
#include "tabwidget.h"
|
|
||||||
|
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
|
@ -16,18 +16,14 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include "adblockmanager.h"
|
#include "network-web/adblock/adblockmanager.h"
|
||||||
#include "adblockdialog.h"
|
#include "network-web/adblock/adblockdialog.h"
|
||||||
#include "adblockmatcher.h"
|
#include "network-web/adblock/adblockmatcher.h"
|
||||||
#include "adblocksubscription.h"
|
#include "network-web/adblock/adblocksubscription.h"
|
||||||
#include "adblockurlinterceptor.h"
|
#include "network-web/adblock/adblockurlinterceptor.h"
|
||||||
#include "datapaths.h"
|
|
||||||
#include "mainapplication.h"
|
#include "miscellaneous/application.h"
|
||||||
#include "webpage.h"
|
#include "miscellaneous/settings.h"
|
||||||
#include "qztools.h"
|
|
||||||
#include "browserwindow.h"
|
|
||||||
#include "settings.h"
|
|
||||||
#include "networkmanager.h"
|
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
@ -37,414 +33,391 @@
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
#include <QMutexLocker>
|
#include <QMutexLocker>
|
||||||
#include <QSaveFile>
|
#include <QSaveFile>
|
||||||
|
#include <QWebEngineUrlRequestInfo>
|
||||||
//#define ADBLOCK_DEBUG
|
|
||||||
|
|
||||||
#ifdef ADBLOCK_DEBUG
|
|
||||||
#include <QElapsedTimer>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Q_GLOBAL_STATIC(AdBlockManager, qz_adblock_manager)
|
Q_GLOBAL_STATIC(AdBlockManager, qz_adblock_manager)
|
||||||
|
|
||||||
AdBlockManager::AdBlockManager(QObject* parent)
|
|
||||||
: QObject(parent)
|
AdBlockManager::AdBlockManager(QObject *parent)
|
||||||
, m_loaded(false)
|
: QObject(parent), m_loaded(false), m_enabled(true), m_matcher(new AdBlockMatcher(this)), m_interceptor(new AdBlockUrlInterceptor(this)) {
|
||||||
, m_enabled(true)
|
load();
|
||||||
, m_matcher(new AdBlockMatcher(this))
|
|
||||||
, m_interceptor(new AdBlockUrlInterceptor(this))
|
|
||||||
{
|
|
||||||
load();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AdBlockManager::~AdBlockManager()
|
AdBlockManager::~AdBlockManager() {
|
||||||
{
|
qDeleteAll(m_subscriptions);
|
||||||
qDeleteAll(m_subscriptions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AdBlockManager* AdBlockManager::instance()
|
AdBlockManager *AdBlockManager::instance() {
|
||||||
{
|
return qz_adblock_manager();
|
||||||
return qz_adblock_manager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::setEnabled(bool enabled)
|
void AdBlockManager::setEnabled(bool enabled) {
|
||||||
{
|
if (m_enabled == enabled) {
|
||||||
if (m_enabled == enabled) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
m_enabled = enabled;
|
m_enabled = enabled;
|
||||||
emit enabledChanged(enabled);
|
emit enabledChanged(enabled);
|
||||||
|
|
||||||
Settings settings;
|
qApp->settings()->setValue(GROUP(Adblock), AdBlock::AdBlockEnabled, m_enabled);
|
||||||
settings.beginGroup("AdBlock");
|
load();
|
||||||
settings.setValue("enabled", m_enabled);
|
// TODO: Reload user stylesheet.
|
||||||
settings.endGroup();
|
//mApp->reloadUserStyleSheet();
|
||||||
|
|
||||||
load();
|
QMutexLocker locker(&m_mutex);
|
||||||
mApp->reloadUserStyleSheet();
|
|
||||||
|
|
||||||
QMutexLocker locker(&m_mutex);
|
|
||||||
|
|
||||||
if (m_enabled) {
|
|
||||||
m_matcher->update();
|
|
||||||
} else {
|
|
||||||
m_matcher->clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const
|
|
||||||
{
|
|
||||||
return m_subscriptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
if (!canRunOnScheme(urlScheme) || !canBeBlocked(request.firstPartyUrl())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool res = false;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
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
|
|
||||||
{
|
|
||||||
return m_disabledRules;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AdBlockManager::addDisabledRule(const QString &filter)
|
|
||||||
{
|
|
||||||
m_disabledRules.append(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AdBlockManager::removeDisabledRule(const QString &filter)
|
|
||||||
{
|
|
||||||
m_disabledRules.removeOne(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url)
|
|
||||||
{
|
|
||||||
const QList<QPair<QString, QString> > queryItems = QUrlQuery(url).queryItems(QUrl::FullyDecoded);
|
|
||||||
|
|
||||||
QString subscriptionTitle;
|
|
||||||
QString subscriptionUrl;
|
|
||||||
|
|
||||||
for (int i = 0; i < queryItems.count(); ++i) {
|
|
||||||
QPair<QString, QString> pair = queryItems.at(i);
|
|
||||||
if (pair.first == QL1S("location"))
|
|
||||||
subscriptionUrl = pair.second;
|
|
||||||
else if (pair.first == QL1S("title"))
|
|
||||||
subscriptionTitle = pair.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (subscriptionTitle.isEmpty() || subscriptionUrl.isEmpty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const QString message = AdBlockManager::tr("Do you want to add <b>%1</b> subscription?").arg(subscriptionTitle);
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const QString &url)
|
|
||||||
{
|
|
||||||
if (title.isEmpty() || url.isEmpty()) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString fileName = QzTools::filterCharsFromFilename(title.toLower()) + ".txt";
|
|
||||||
QString filePath = QzTools::ensureUniqueFilename(DataPaths::currentProfilePath() + "/adblock/" + fileName);
|
|
||||||
|
|
||||||
QByteArray data = QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1();
|
|
||||||
|
|
||||||
QSaveFile file(filePath);
|
|
||||||
if (!file.open(QFile::WriteOnly)) {
|
|
||||||
qWarning() << "AdBlockManager: Cannot write to file" << filePath;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
file.write(data);
|
|
||||||
file.commit();
|
|
||||||
|
|
||||||
AdBlockSubscription* subscription = new AdBlockSubscription(title, this);
|
|
||||||
subscription->setUrl(QUrl(url));
|
|
||||||
subscription->setFilePath(filePath);
|
|
||||||
subscription->loadSubscription(m_disabledRules);
|
|
||||||
|
|
||||||
m_subscriptions.insert(m_subscriptions.count() - 1, subscription);
|
|
||||||
connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
|
||||||
connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
|
|
||||||
|
|
||||||
return subscription;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription)
|
|
||||||
{
|
|
||||||
QMutexLocker locker(&m_mutex);
|
|
||||||
|
|
||||||
if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFile(subscription->filePath()).remove();
|
|
||||||
m_subscriptions.removeOne(subscription);
|
|
||||||
|
|
||||||
|
if (m_enabled) {
|
||||||
m_matcher->update();
|
m_matcher->update();
|
||||||
delete subscription;
|
}
|
||||||
|
else {
|
||||||
|
m_matcher->clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const {
|
||||||
|
return m_subscriptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) {
|
||||||
|
QMutexLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
if (!isEnabled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString urlString = request.requestUrl().toEncoded().toLower();
|
||||||
|
const QString urlDomain = request.requestUrl().host().toLower();
|
||||||
|
const QString urlScheme = request.requestUrl().scheme().toLower();
|
||||||
|
|
||||||
|
if (!canRunOnScheme(urlScheme) || !canBeBlocked(request.firstPartyUrl())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool res = false;
|
||||||
|
const AdBlockRule *blockedRule = m_matcher->match(request, urlDomain, urlString);
|
||||||
|
|
||||||
|
if (blockedRule) {
|
||||||
|
res = true;
|
||||||
|
|
||||||
|
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList AdBlockManager::disabledRules() const {
|
||||||
|
return m_disabledRules;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdBlockManager::addDisabledRule(const QString &filter) {
|
||||||
|
m_disabledRules.append(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdBlockManager::removeDisabledRule(const QString &filter) {
|
||||||
|
m_disabledRules.removeOne(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AdBlockManager::addSubscriptionFromUrl(const QUrl &url) {
|
||||||
|
const QList<QPair<QString, QString> > queryItems = QUrlQuery(url).queryItems(QUrl::FullyDecoded);
|
||||||
|
|
||||||
|
QString subscriptionTitle;
|
||||||
|
QString subscriptionUrl;
|
||||||
|
|
||||||
|
for (int i = 0; i < queryItems.count(); ++i) {
|
||||||
|
QPair<QString, QString> pair = queryItems.at(i);
|
||||||
|
|
||||||
|
if (pair.first == QL1S("location")) {
|
||||||
|
subscriptionUrl = pair.second;
|
||||||
|
}
|
||||||
|
else if (pair.first == QL1S("title")) {
|
||||||
|
subscriptionTitle = pair.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subscriptionTitle.isEmpty() || subscriptionUrl.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (result == QMessageBox::Yes) {
|
||||||
|
AdBlockManager::instance()->addSubscription(subscriptionTitle, subscriptionUrl);
|
||||||
|
AdBlockManager::instance()->showDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) {
|
||||||
|
if (title.isEmpty() || url.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString fileName = QzTools::filterCharsFromFilename(title.toLower()) + ".txt";
|
||||||
|
QString filePath = QzTools::ensureUniqueFilename(DataPaths::currentProfilePath() + "/adblock/" + fileName);
|
||||||
|
|
||||||
|
QByteArray data = QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1();
|
||||||
|
|
||||||
|
QSaveFile file(filePath);
|
||||||
|
if (!file.open(QFile::WriteOnly)) {
|
||||||
|
qWarning("Cannot save AdBlock subscription to file '%s'.", qPrintable(filePath));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.write(data);
|
||||||
|
file.commit();
|
||||||
|
|
||||||
|
AdBlockSubscription *subscription = new AdBlockSubscription(title, this);
|
||||||
|
subscription->setUrl(QUrl(url));
|
||||||
|
subscription->setFilePath(filePath);
|
||||||
|
subscription->loadSubscription(m_disabledRules);
|
||||||
|
|
||||||
|
m_subscriptions.insert(m_subscriptions.count() - 1, subscription);
|
||||||
|
connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
||||||
|
connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
|
||||||
|
|
||||||
|
return subscription;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) {
|
||||||
|
QMutexLocker locker(&m_mutex);
|
||||||
|
|
||||||
|
if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile(subscription->filePath()).remove();
|
||||||
|
m_subscriptions.removeOne(subscription);
|
||||||
|
|
||||||
|
m_matcher->update();
|
||||||
|
delete subscription;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdBlockCustomList* AdBlockManager::customList() const
|
AdBlockCustomList* AdBlockManager::customList() const
|
||||||
{
|
{
|
||||||
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
||||||
AdBlockCustomList* list = qobject_cast<AdBlockCustomList*>(subscription);
|
AdBlockCustomList* list = qobject_cast<AdBlockCustomList*>(subscription);
|
||||||
|
|
||||||
if (list) {
|
if (list) {
|
||||||
return list;
|
return list;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::load()
|
void AdBlockManager::load()
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
|
|
||||||
if (m_loaded) {
|
if (m_loaded) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ADBLOCK_DEBUG
|
#ifdef ADBLOCK_DEBUG
|
||||||
QElapsedTimer timer;
|
QElapsedTimer timer;
|
||||||
timer.start();
|
timer.start();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings.beginGroup("AdBlock");
|
settings.beginGroup("AdBlock");
|
||||||
m_enabled = settings.value("enabled", m_enabled).toBool();
|
m_enabled = settings.value("enabled", m_enabled).toBool();
|
||||||
m_disabledRules = settings.value("disabledRules", QStringList()).toStringList();
|
m_disabledRules = settings.value("disabledRules", QStringList()).toStringList();
|
||||||
QDateTime lastUpdate = settings.value("lastUpdate", QDateTime()).toDateTime();
|
QDateTime lastUpdate = settings.value("lastUpdate", QDateTime()).toDateTime();
|
||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
|
|
||||||
if (!m_enabled) {
|
if (!m_enabled) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDir adblockDir(DataPaths::currentProfilePath() + "/adblock");
|
||||||
|
// Create if neccessary
|
||||||
|
if (!adblockDir.exists()) {
|
||||||
|
QDir(DataPaths::currentProfilePath()).mkdir("adblock");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (const QString &fileName, adblockDir.entryList(QStringList("*.txt"), QDir::Files)) {
|
||||||
|
if (fileName == QLatin1String("customlist.txt")) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDir adblockDir(DataPaths::currentProfilePath() + "/adblock");
|
const QString absolutePath = adblockDir.absoluteFilePath(fileName);
|
||||||
// Create if neccessary
|
QFile file(absolutePath);
|
||||||
if (!adblockDir.exists()) {
|
if (!file.open(QFile::ReadOnly)) {
|
||||||
QDir(DataPaths::currentProfilePath()).mkdir("adblock");
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (const QString &fileName, adblockDir.entryList(QStringList("*.txt"), QDir::Files)) {
|
QTextStream textStream(&file);
|
||||||
if (fileName == QLatin1String("customlist.txt")) {
|
textStream.setCodec("UTF-8");
|
||||||
continue;
|
QString title = textStream.readLine(1024).remove(QLatin1String("Title: "));
|
||||||
}
|
QUrl url = QUrl(textStream.readLine(1024).remove(QLatin1String("Url: ")));
|
||||||
|
|
||||||
const QString absolutePath = adblockDir.absoluteFilePath(fileName);
|
if (title.isEmpty() || !url.isValid()) {
|
||||||
QFile file(absolutePath);
|
qWarning() << "AdBlockManager: Invalid subscription file" << absolutePath;
|
||||||
if (!file.open(QFile::ReadOnly)) {
|
continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QTextStream textStream(&file);
|
|
||||||
textStream.setCodec("UTF-8");
|
|
||||||
QString title = textStream.readLine(1024).remove(QLatin1String("Title: "));
|
|
||||||
QUrl url = QUrl(textStream.readLine(1024).remove(QLatin1String("Url: ")));
|
|
||||||
|
|
||||||
if (title.isEmpty() || !url.isValid()) {
|
|
||||||
qWarning() << "AdBlockManager: Invalid subscription file" << absolutePath;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
AdBlockSubscription* subscription = new AdBlockSubscription(title, this);
|
|
||||||
subscription->setUrl(url);
|
|
||||||
subscription->setFilePath(absolutePath);
|
|
||||||
|
|
||||||
m_subscriptions.append(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepend EasyList if subscriptions are empty
|
AdBlockSubscription* subscription = new AdBlockSubscription(title, this);
|
||||||
if (m_subscriptions.isEmpty()) {
|
subscription->setUrl(url);
|
||||||
AdBlockSubscription* easyList = new AdBlockSubscription(tr("EasyList"), this);
|
subscription->setFilePath(absolutePath);
|
||||||
easyList->setUrl(QUrl(ADBLOCK_EASYLIST_URL));
|
|
||||||
easyList->setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/easylist.txt"));
|
|
||||||
|
|
||||||
m_subscriptions.prepend(easyList);
|
m_subscriptions.append(subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append CustomList
|
// Prepend EasyList if subscriptions are empty
|
||||||
AdBlockCustomList* customList = new AdBlockCustomList(this);
|
if (m_subscriptions.isEmpty()) {
|
||||||
m_subscriptions.append(customList);
|
AdBlockSubscription* easyList = new AdBlockSubscription(tr("EasyList"), this);
|
||||||
|
easyList->setUrl(QUrl(ADBLOCK_EASYLIST_URL));
|
||||||
|
easyList->setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/easylist.txt"));
|
||||||
|
|
||||||
// Load all subscriptions
|
m_subscriptions.prepend(easyList);
|
||||||
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
}
|
||||||
subscription->loadSubscription(m_disabledRules);
|
|
||||||
|
|
||||||
connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
// Append CustomList
|
||||||
connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
|
AdBlockCustomList* customList = new AdBlockCustomList(this);
|
||||||
}
|
m_subscriptions.append(customList);
|
||||||
|
|
||||||
if (lastUpdate.addDays(5) < QDateTime::currentDateTime()) {
|
// Load all subscriptions
|
||||||
QTimer::singleShot(1000 * 60, this, SLOT(updateAllSubscriptions()));
|
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
||||||
}
|
subscription->loadSubscription(m_disabledRules);
|
||||||
|
|
||||||
|
connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
||||||
|
connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastUpdate.addDays(5) < QDateTime::currentDateTime()) {
|
||||||
|
QTimer::singleShot(1000 * 60, this, SLOT(updateAllSubscriptions()));
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef ADBLOCK_DEBUG
|
#ifdef ADBLOCK_DEBUG
|
||||||
qDebug() << "AdBlock loaded in" << timer.elapsed();
|
qDebug() << "AdBlock loaded in" << timer.elapsed();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_matcher->update();
|
m_matcher->update();
|
||||||
m_loaded = true;
|
m_loaded = true;
|
||||||
|
|
||||||
mApp->networkManager()->installUrlInterceptor(m_interceptor);
|
mApp->networkManager()->installUrlInterceptor(m_interceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::updateMatcher()
|
void AdBlockManager::updateMatcher()
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
|
|
||||||
m_matcher->update();
|
m_matcher->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::updateAllSubscriptions()
|
void AdBlockManager::updateAllSubscriptions()
|
||||||
{
|
{
|
||||||
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
||||||
subscription->updateSubscription();
|
subscription->updateSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings.beginGroup("AdBlock");
|
settings.beginGroup("AdBlock");
|
||||||
settings.setValue("lastUpdate", QDateTime::currentDateTime());
|
settings.setValue("lastUpdate", QDateTime::currentDateTime());
|
||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::save()
|
void AdBlockManager::save()
|
||||||
{
|
{
|
||||||
if (!m_loaded) {
|
if (!m_loaded) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
||||||
subscription->saveSubscription();
|
subscription->saveSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings.beginGroup("AdBlock");
|
settings.beginGroup("AdBlock");
|
||||||
settings.setValue("enabled", m_enabled);
|
settings.setValue("enabled", m_enabled);
|
||||||
settings.setValue("disabledRules", m_disabledRules);
|
settings.setValue("disabledRules", m_disabledRules);
|
||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AdBlockManager::isEnabled() const
|
bool AdBlockManager::isEnabled() const
|
||||||
{
|
{
|
||||||
return m_enabled;
|
return m_enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AdBlockManager::canRunOnScheme(const QString &scheme) const
|
bool AdBlockManager::canRunOnScheme(const QString &scheme) const
|
||||||
{
|
{
|
||||||
return !(scheme == QLatin1String("file") || scheme == QLatin1String("qrc")
|
return !(scheme == QLatin1String("file") || scheme == QLatin1String("qrc")
|
||||||
|| scheme == QLatin1String("qupzilla") || scheme == QLatin1String("data")
|
|| scheme == QLatin1String("qupzilla") || scheme == QLatin1String("data")
|
||||||
|| scheme == QLatin1String("abp"));
|
|| scheme == QLatin1String("abp"));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AdBlockManager::canBeBlocked(const QUrl &url) const
|
bool AdBlockManager::canBeBlocked(const QUrl &url) const
|
||||||
{
|
{
|
||||||
return !m_matcher->adBlockDisabledForUrl(url);
|
return !m_matcher->adBlockDisabledForUrl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AdBlockManager::elementHidingRules(const QUrl &url) const
|
QString AdBlockManager::elementHidingRules(const QUrl &url) const
|
||||||
{
|
{
|
||||||
if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url))
|
if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url))
|
||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
return m_matcher->elementHidingRules();
|
return m_matcher->elementHidingRules();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AdBlockManager::elementHidingRulesForDomain(const QUrl &url) const
|
QString AdBlockManager::elementHidingRulesForDomain(const QUrl &url) const
|
||||||
{
|
{
|
||||||
if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url))
|
if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url))
|
||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
return m_matcher->elementHidingRulesForDomain(url.host());
|
return m_matcher->elementHidingRulesForDomain(url.host());
|
||||||
}
|
}
|
||||||
|
|
||||||
AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) const
|
AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) const
|
||||||
{
|
{
|
||||||
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
foreach (AdBlockSubscription* subscription, m_subscriptions) {
|
||||||
if (subscription->title() == name) {
|
if (subscription->title() == name) {
|
||||||
return subscription;
|
return subscription;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdBlockDialog* AdBlockManager::showDialog()
|
AdBlockDialog* AdBlockManager::showDialog()
|
||||||
{
|
{
|
||||||
if (!m_adBlockDialog) {
|
if (!m_adBlockDialog) {
|
||||||
m_adBlockDialog = new AdBlockDialog;
|
m_adBlockDialog = new AdBlockDialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_adBlockDialog.data()->show();
|
m_adBlockDialog.data()->show();
|
||||||
m_adBlockDialog.data()->raise();
|
m_adBlockDialog.data()->raise();
|
||||||
m_adBlockDialog.data()->activateWindow();
|
m_adBlockDialog.data()->activateWindow();
|
||||||
|
|
||||||
return m_adBlockDialog.data();
|
return m_adBlockDialog.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::showRule()
|
void AdBlockManager::showRule()
|
||||||
{
|
{
|
||||||
if (QAction* action = qobject_cast<QAction*>(sender())) {
|
if (QAction* action = qobject_cast<QAction*>(sender())) {
|
||||||
const AdBlockRule* rule = static_cast<const AdBlockRule*>(action->data().value<void*>());
|
const AdBlockRule* rule = static_cast<const AdBlockRule*>(action->data().value<void*>());
|
||||||
|
|
||||||
if (rule) {
|
if (rule) {
|
||||||
showDialog()->showRule(rule);
|
showDialog()->showRule(rule);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
|
||||||
#include "qzcommon.h"
|
|
||||||
|
|
||||||
class QUrl;
|
class QUrl;
|
||||||
class QWebEngineUrlRequestInfo;
|
class QWebEngineUrlRequestInfo;
|
||||||
|
@ -36,13 +35,12 @@ class AdBlockCustomList;
|
||||||
class AdBlockSubscription;
|
class AdBlockSubscription;
|
||||||
class AdBlockUrlInterceptor;
|
class AdBlockUrlInterceptor;
|
||||||
|
|
||||||
class QUPZILLA_EXPORT AdBlockManager : public QObject
|
class AdBlockManager : public QObject {
|
||||||
{
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AdBlockManager(QObject* parent = 0);
|
explicit AdBlockManager(QObject *parent = 0);
|
||||||
~AdBlockManager();
|
virtual ~AdBlockManager();
|
||||||
|
|
||||||
void load();
|
void load();
|
||||||
void save();
|
void save();
|
||||||
|
@ -53,7 +51,7 @@ public:
|
||||||
QString elementHidingRules(const QUrl &url) const;
|
QString elementHidingRules(const QUrl &url) const;
|
||||||
QString elementHidingRulesForDomain(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;
|
QList<AdBlockSubscription*> subscriptions() const;
|
||||||
|
|
||||||
bool block(QWebEngineUrlRequestInfo &request);
|
bool block(QWebEngineUrlRequestInfo &request);
|
||||||
|
@ -64,33 +62,33 @@ public:
|
||||||
|
|
||||||
bool addSubscriptionFromUrl(const QUrl &url);
|
bool addSubscriptionFromUrl(const QUrl &url);
|
||||||
|
|
||||||
AdBlockSubscription* addSubscription(const QString &title, const QString &url);
|
AdBlockSubscription *addSubscription(const QString &title, const QString &url);
|
||||||
bool removeSubscription(AdBlockSubscription* subscription);
|
bool removeSubscription(AdBlockSubscription *subscription);
|
||||||
|
|
||||||
AdBlockCustomList* customList() const;
|
AdBlockCustomList *customList() const;
|
||||||
|
|
||||||
static AdBlockManager* instance();
|
static AdBlockManager *instance();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void enabledChanged(bool enabled);
|
void enabledChanged(bool enabled);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setEnabled(bool enabled);
|
void setEnabled(bool enabled);
|
||||||
void showRule();
|
void showRule();
|
||||||
|
|
||||||
void updateMatcher();
|
void updateMatcher();
|
||||||
void updateAllSubscriptions();
|
void updateAllSubscriptions();
|
||||||
|
|
||||||
AdBlockDialog* showDialog();
|
AdBlockDialog *showDialog();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline bool canBeBlocked(const QUrl &url) const;
|
inline bool canBeBlocked(const QUrl &url) const;
|
||||||
|
|
||||||
bool m_loaded;
|
bool m_loaded;
|
||||||
bool m_enabled;
|
bool m_enabled;
|
||||||
|
|
||||||
QList<AdBlockSubscription*> m_subscriptions;
|
QList<AdBlockSubscription*> m_subscriptions;
|
||||||
AdBlockMatcher* m_matcher;
|
AdBlockMatcher *m_matcher;
|
||||||
QStringList m_disabledRules;
|
QStringList m_disabledRules;
|
||||||
|
|
||||||
AdBlockUrlInterceptor *m_interceptor;
|
AdBlockUrlInterceptor *m_interceptor;
|
||||||
|
|
Loading…
Add table
Reference in a new issue