Big fixes for adblock - fixed adding of custom rules, formating of rules in adblock conf dialog.

This commit is contained in:
Martin Rotter 2021-01-07 13:48:30 +01:00
parent d019fccceb
commit cb72db0345
21 changed files with 210 additions and 215 deletions

View file

@ -19,7 +19,7 @@
#define ADBLOCK_ADBLOCKED_PAGE "adblockedpage" #define ADBLOCK_ADBLOCKED_PAGE "adblockedpage"
#define ADBLOCK_HOWTO_FILTERS "https://help.eyeo.com/en/adblockplus/how-to-write-filters" #define ADBLOCK_HOWTO_FILTERS "https://help.eyeo.com/en/adblockplus/how-to-write-filters"
#define ADBLOCK_UPDATE_DAYS_INTERVAL 5 #define ADBLOCK_UPDATE_DAYS_INTERVAL 14
#define ADBLOCK_ICON_ACTIVE "adblock" #define ADBLOCK_ICON_ACTIVE "adblock"
#define ADBLOCK_ICON_DISABLED "adblock-disabled" #define ADBLOCK_ICON_DISABLED "adblock-disabled"
#define IS_IN_ARRAY(offset, array) ((offset >= 0) && (offset < array.count())) #define IS_IN_ARRAY(offset, array) ((offset >= 0) && (offset < array.count()))

View file

@ -63,7 +63,7 @@ FormMain::FormMain(QWidget* parent, Qt::WindowFlags f)
setWindowTitle(APP_LONG_NAME); setWindowTitle(APP_LONG_NAME);
#if defined (USE_WEBENGINE) #if defined (USE_WEBENGINE)
m_ui->m_menuWebBrowserTabs->addAction(AdBlockManager::instance()->adBlockIcon()); m_ui->m_menuWebBrowserTabs->addAction(qApp->web()->adBlock()->adBlockIcon());
m_ui->m_menuWebBrowserTabs->addAction(qApp->web()->engineSettingsAction()); m_ui->m_menuWebBrowserTabs->addAction(qApp->web()->engineSettingsAction());
#endif #endif

View file

@ -166,7 +166,7 @@ void WebViewer::contextMenuEvent(QContextMenuEvent* event) {
}); });
} }
menu->addAction(AdBlockManager::instance()->adBlockIcon()); menu->addAction(qApp->web()->adBlock()->adBlockIcon());
menu->addAction(qApp->web()->engineSettingsAction()); menu->addAction(qApp->web()->engineSettingsAction());
const QPoint pos = event->globalPos(); const QPoint pos = event->globalPos();

View file

@ -30,24 +30,15 @@
#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/networkurlinterceptor.h" #include "network-web/networkurlinterceptor.h"
#include "network-web/rssguardschemehandler.h"
#include "network-web/urlinterceptor.h"
#include <QWebEngineDownloadItem> #include <QWebEngineDownloadItem>
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineUrlScheme>
#endif #endif
Application::Application(const QString& id, int& argc, char** argv) Application::Application(const QString& id, int& argc, char** argv)
: QtSingleApplication(id, argc, argv), m_updateFeedsLock(new Mutex()) { : QtSingleApplication(id, argc, argv), m_updateFeedsLock(new Mutex()) {
parseCmdArguments(); parseCmdArguments();
#if defined(USE_WEBENGINE)
m_urlInterceptor = new NetworkUrlInterceptor(this);
#endif
m_feedReader = nullptr; m_feedReader = nullptr;
m_quitLogicDone = false; m_quitLogicDone = false;
m_mainForm = nullptr; m_mainForm = nullptr;
@ -79,32 +70,12 @@ Application::Application(const QString& id, int& argc, char** argv)
connect(this, &Application::saveStateRequest, this, &Application::onSaveState); connect(this, &Application::saveStateRequest, this, &Application::onSaveState);
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
QWebEngineUrlScheme url_scheme(QByteArray(APP_LOW_NAME));
url_scheme.setDefaultPort(QWebEngineUrlScheme::SpecialPort::PortUnspecified);
url_scheme.setSyntax(QWebEngineUrlScheme::Syntax::Host);
url_scheme.setFlags(QWebEngineUrlScheme::Flag::LocalScheme |
QWebEngineUrlScheme::Flag::LocalAccessAllowed |
QWebEngineUrlScheme::Flag::ServiceWorkersAllowed |
QWebEngineUrlScheme::Flag::ContentSecurityPolicyIgnored);
QWebEngineUrlScheme::registerScheme(url_scheme);
connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &Application::downloadRequested); connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &Application::downloadRequested);
#if QT_VERSION >= 0x050D00 // Qt >= 5.13.0
QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor(m_urlInterceptor);
#else
QWebEngineProfile::defaultProfile()->setRequestInterceptor(m_urlInterceptor);
#endif
m_urlInterceptor->loadSettings();
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler(QByteArray(APP_LOW_NAME),
new RssGuardSchemeHandler(QWebEngineProfile::defaultProfile()));
#endif #endif
m_webFactory->updateProxy(); m_webFactory->updateProxy();
m_webFactory->urlIinterceptor()->load();
m_webFactory->adBlock()->load(true);
} }
Application::~Application() { Application::~Application() {
@ -189,7 +160,7 @@ QList<QAction*> Application::userActions() {
m_userActions = m_mainForm->allActions(); m_userActions = m_mainForm->allActions();
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
m_userActions.append(AdBlockManager::instance()->adBlockIcon()); m_userActions.append(m_webFactory->adBlock()->adBlockIcon());
#endif #endif
} }
@ -419,13 +390,6 @@ SystemTrayIcon* Application::trayIcon() {
return m_trayIcon; return m_trayIcon;
} }
#if defined(USE_WEBENGINE)
NetworkUrlInterceptor* Application::urlIinterceptor() {
return m_urlInterceptor;
}
#endif
QIcon Application::desktopAwareIcon() const { QIcon Application::desktopAwareIcon() const {
auto from_theme = m_icons->fromTheme(APP_LOW_NAME); auto from_theme = m_icons->fromTheme(APP_LOW_NAME);
@ -497,7 +461,7 @@ void Application::onAboutToQuit() {
m_quitLogicDone = true; m_quitLogicDone = true;
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
AdBlockManager::instance()->save(); m_webFactory->adBlock()->save();
#endif #endif
// Make sure that we obtain close lock BEFORE even trying to quit the application. // Make sure that we obtain close lock BEFORE even trying to quit the application.

View file

@ -36,10 +36,6 @@ class Mutex;
class QWebEngineDownloadItem; class QWebEngineDownloadItem;
class WebFactory; class WebFactory;
#if defined(USE_WEBENGINE)
class NetworkUrlInterceptor;
#endif
class RSSGUARD_DLLSPEC Application : public QtSingleApplication { class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
Q_OBJECT Q_OBJECT
@ -81,10 +77,6 @@ class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
QWidget* mainFormWidget(); QWidget* mainFormWidget();
SystemTrayIcon* trayIcon(); SystemTrayIcon* trayIcon();
#if defined(USE_WEBENGINE)
NetworkUrlInterceptor* urlIinterceptor();
#endif
QIcon desktopAwareIcon() const; QIcon desktopAwareIcon() const;
QString tempFolder() const; QString tempFolder() const;
@ -124,8 +116,6 @@ class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
// Custom debug/console log handler. // Custom debug/console log handler.
static void performLogging(QtMsgType type, const QMessageLogContext& context, const QString& msg); static void performLogging(QtMsgType type, const QMessageLogContext& context, const QString& msg);
//static QString s_customLogFile;
public slots: public slots:
// Restarts the application. // Restarts the application.
@ -153,11 +143,6 @@ class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
private: private:
QCommandLineParser m_cmdParser; QCommandLineParser m_cmdParser;
#if defined(USE_WEBENGINE)
NetworkUrlInterceptor* m_urlInterceptor;
#endif
FeedReader* m_feedReader; FeedReader* m_feedReader;
bool m_quitLogicDone; bool m_quitLogicDone;

View file

@ -104,3 +104,10 @@ void AdBlockAddSubscriptionDialog::presetsEnabledChanged(bool enabled) {
AdBlockAddSubscriptionDialog::~AdBlockAddSubscriptionDialog() { AdBlockAddSubscriptionDialog::~AdBlockAddSubscriptionDialog() {
delete m_ui; delete m_ui;
} }
AdBlockAddSubscriptionDialog::Subscription::Subscription() {}
AdBlockAddSubscriptionDialog::Subscription::Subscription(const QString& title, const QString& url) {
m_title = title;
m_url = url;
}

View file

@ -33,7 +33,7 @@ class AdBlockAddSubscriptionDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
explicit AdBlockAddSubscriptionDialog(QWidget* parent = 0); explicit AdBlockAddSubscriptionDialog(QWidget* parent = nullptr);
virtual ~AdBlockAddSubscriptionDialog(); virtual ~AdBlockAddSubscriptionDialog();
QString title() const; QString title() const;
@ -44,21 +44,15 @@ class AdBlockAddSubscriptionDialog : public QDialog {
void presetsEnabledChanged(bool enabled); void presetsEnabledChanged(bool enabled);
private: private:
Ui::AdBlockAddSubscriptionDialog* m_ui;
struct Subscription { struct Subscription {
QString m_title; QString m_title;
QString m_url; QString m_url;
Subscription() {} explicit Subscription();
explicit Subscription(const QString& title, const QString& url);
Subscription(const QString& t, const QString& u) {
m_title = t;
m_url = u;
}
}; };
Ui::AdBlockAddSubscriptionDialog* m_ui;
QVector<Subscription> m_knownSubscriptions; QVector<Subscription> m_knownSubscriptions;
}; };

View file

@ -25,6 +25,7 @@
#include "network-web/adblock/adblocktreewidget.h" #include "network-web/adblock/adblocktreewidget.h"
#include "definitions/definitions.h" #include "definitions/definitions.h"
#include "gui/guiutilities.h"
#include "miscellaneous/application.h" #include "miscellaneous/application.h"
#include "miscellaneous/iconfactory.h" #include "miscellaneous/iconfactory.h"
#include "network-web/webfactory.h" #include "network-web/webfactory.h"
@ -35,13 +36,15 @@
#include <QTimer> #include <QTimer>
AdBlockDialog::AdBlockDialog(QWidget* parent) AdBlockDialog::AdBlockDialog(QWidget* parent)
: QDialog(parent), m_manager(AdBlockManager::instance()), m_currentTreeWidget(nullptr), m_currentSubscription(nullptr), : QDialog(parent), m_manager(qApp->web()->adBlock()), m_currentTreeWidget(nullptr), m_currentSubscription(nullptr),
m_loaded(false), m_ui(new Ui::AdBlockDialog) { m_loaded(false), m_ui(new Ui::AdBlockDialog) {
m_ui->setupUi(this); m_ui->setupUi(this);
m_ui->m_cbEnable->setChecked(m_manager->isEnabled()); m_ui->m_cbEnable->setChecked(m_manager->isEnabled());
setAttribute(Qt::WA_DeleteOnClose);
setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint); GuiUtilities::applyDialogProperties(*this,
setWindowIcon(qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE)); qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE),
tr("AdBlock configuration"));
QPushButton* btn_options = m_ui->m_buttonBox->addButton(QDialogButtonBox::FirstButton); QPushButton* btn_options = m_ui->m_buttonBox->addButton(QDialogButtonBox::FirstButton);
btn_options->setText(tr("Options")); btn_options->setText(tr("Options"));
@ -55,11 +58,14 @@ AdBlockDialog::AdBlockDialog(QWidget* parent)
menu->addAction(tr("Update subscriptions"), m_manager, &AdBlockManager::updateAllSubscriptions); menu->addAction(tr("Update subscriptions"), m_manager, &AdBlockManager::updateAllSubscriptions);
menu->addSeparator(); menu->addSeparator();
menu->addAction(tr("Learn about writing rules..."), this, &AdBlockDialog::learnAboutRules); menu->addAction(tr("Learn about writing rules..."), this, &AdBlockDialog::learnAboutRules);
btn_options->setMenu(menu); btn_options->setMenu(menu);
connect(menu, &QMenu::aboutToShow, this, &AdBlockDialog::aboutToShowMenu); connect(menu, &QMenu::aboutToShow, this, &AdBlockDialog::aboutToShowMenu);
connect(m_ui->m_cbEnable, &QCheckBox::toggled, this, &AdBlockDialog::enableAdBlock); connect(m_ui->m_cbEnable, &QCheckBox::toggled, this, &AdBlockDialog::enableAdBlock);
connect(m_ui->m_tabSubscriptions, &QTabWidget::currentChanged, this, &AdBlockDialog::currentChanged); connect(m_ui->m_tabSubscriptions, &QTabWidget::currentChanged, this, &AdBlockDialog::currentChanged);
connect(m_ui->m_buttonBox, &QDialogButtonBox::rejected, this, &AdBlockDialog::close); connect(m_ui->m_buttonBox, &QDialogButtonBox::rejected, this, &AdBlockDialog::close);
load(); load();
m_ui->m_buttonBox->setFocus(); m_ui->m_buttonBox->setFocus();
} }
@ -69,10 +75,10 @@ void AdBlockDialog::showRule(const AdBlockRule* rule) const {
if (subscription != nullptr) { if (subscription != nullptr) {
for (int i = 0; i < m_ui->m_tabSubscriptions->count(); ++i) { for (int i = 0; i < m_ui->m_tabSubscriptions->count(); ++i) {
auto* treeWidget = qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabSubscriptions->widget(i)); auto* tree_widget = qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabSubscriptions->widget(i));
if (subscription == treeWidget->subscription()) { if (subscription == tree_widget->subscription()) {
treeWidget->showRule(rule); tree_widget->showRule(rule);
m_ui->m_tabSubscriptions->setCurrentIndex(i); m_ui->m_tabSubscriptions->setCurrentIndex(i);
break; break;
} }
@ -120,7 +126,7 @@ void AdBlockDialog::currentChanged(int index) {
} }
void AdBlockDialog::enableAdBlock(bool state) { void AdBlockDialog::enableAdBlock(bool state) {
m_manager->setEnabled(state); m_manager->load(false);
if (state) { if (state) {
load(); load();
@ -142,9 +148,9 @@ void AdBlockDialog::learnAboutRules() {
void AdBlockDialog::loadSubscriptions() { void AdBlockDialog::loadSubscriptions() {
for (int i = 0; i < m_ui->m_tabSubscriptions->count(); ++i) { for (int i = 0; i < m_ui->m_tabSubscriptions->count(); ++i) {
auto* treeWidget = qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabSubscriptions->widget(i)); auto* tree_widget = qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabSubscriptions->widget(i));
treeWidget->refresh(); tree_widget->refresh();
} }
} }

View file

@ -33,7 +33,7 @@ class AdBlockDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
explicit AdBlockDialog(QWidget* parent = 0); explicit AdBlockDialog(QWidget* parent = nullptr);
void showRule(const AdBlockRule* rule) const; void showRule(const AdBlockRule* rule) const;

View file

@ -13,9 +13,6 @@
<height>424</height> <height>424</height>
</rect> </rect>
</property> </property>
<property name="windowTitle">
<string>AdBlock configuration</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QCheckBox" name="m_cbEnable"> <widget class="QCheckBox" name="m_cbEnable">

View file

@ -33,17 +33,18 @@
#include <QTimer> #include <QTimer>
AdBlockIcon::AdBlockIcon(AdBlockManager* parent) AdBlockIcon::AdBlockIcon(AdBlockManager* parent)
: QAction(parent), m_manager(parent), m_flashTimer(nullptr), m_timerTicks(0), m_enabled(m_manager->isEnabled()) { : QAction(parent), m_manager(parent), m_flashTimer(nullptr), m_timerTicks(0) {
setToolTip(tr("AdBlock lets you block unwanted content on web pages")); setToolTip(tr("AdBlock lets you block unwanted content on web pages"));
setText(QSL("AdBlock")); setText(QSL("AdBlock"));
setMenu(new QMenu()); setMenu(new QMenu());
setIcon(m_enabled ? qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE) : qApp->icons()->miscIcon(ADBLOCK_ICON_DISABLED));
connect(m_manager, &AdBlockManager::enabledChanged, this, &AdBlockIcon::setEnabled); connect(m_manager, &AdBlockManager::enabledChanged, this, &AdBlockIcon::setEnabled);
connect(menu(), &QMenu::aboutToShow, this, [this]() { connect(menu(), &QMenu::aboutToShow, this, [this]() {
createMenu(); createMenu();
}); });
connect(this, &QAction::triggered, m_manager, &AdBlockManager::showDialog); connect(this, &QAction::triggered, m_manager, &AdBlockManager::showDialog);
setEnabled(m_manager->isEnabled());
} }
AdBlockIcon::~AdBlockIcon() { AdBlockIcon::~AdBlockIcon() {
@ -83,6 +84,7 @@ void AdBlockIcon::popupBlocked(const QString& ruleString, const QUrl& url) {
m_flashTimer->setInterval(500); m_flashTimer->setInterval(500);
m_flashTimer->start(); m_flashTimer->start();
connect(m_flashTimer, &QTimer::timeout, this, &AdBlockIcon::animateIcon); connect(m_flashTimer, &QTimer::timeout, this, &AdBlockIcon::animateIcon);
} }

View file

@ -43,7 +43,7 @@ class AdBlockIcon : public QAction {
public slots: public slots:
void setEnabled(bool enabled); void setEnabled(bool enabled);
void createMenu(QMenu* menu = 0); void createMenu(QMenu* menu = nullptr);
private slots: private slots:
void showMenu(const QPoint& pos); void showMenu(const QPoint& pos);

View file

@ -27,6 +27,7 @@
#include "network-web/adblock/adblocksubscription.h" #include "network-web/adblock/adblocksubscription.h"
#include "network-web/adblock/adblockurlinterceptor.h" #include "network-web/adblock/adblockurlinterceptor.h"
#include "network-web/networkurlinterceptor.h" #include "network-web/networkurlinterceptor.h"
#include "network-web/webfactory.h"
#include <QDateTime> #include <QDateTime>
#include <QDir> #include <QDir>
@ -39,11 +40,9 @@
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <QWebEngineUrlRequestInfo> #include <QWebEngineUrlRequestInfo>
Q_GLOBAL_STATIC(AdBlockManager, qz_adblock_manager)
AdBlockManager::AdBlockManager(QObject* parent) AdBlockManager::AdBlockManager(QObject* parent)
: QObject(parent), m_loaded(false), m_enabled(true), m_matcher(new AdBlockMatcher(this)), m_interceptor(new AdBlockUrlInterceptor(this)) { : QObject(parent), m_loaded(false), m_enabled(false), m_matcher(new AdBlockMatcher(this)),
load(); m_interceptor(new AdBlockUrlInterceptor(this)) {
m_adblockIcon = new AdBlockIcon(this); m_adblockIcon = new AdBlockIcon(this);
m_adblockIcon->setObjectName(QSL("m_adblockIconAction")); m_adblockIcon->setObjectName(QSL("m_adblockIconAction"));
} }
@ -52,31 +51,6 @@ AdBlockManager::~AdBlockManager() {
qDeleteAll(m_subscriptions); qDeleteAll(m_subscriptions);
} }
AdBlockManager* AdBlockManager::instance() {
return qz_adblock_manager();
}
void AdBlockManager::setEnabled(bool enabled) {
if (m_enabled == enabled) {
return;
}
m_enabled = enabled;
emit enabledChanged(enabled);
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, m_enabled);
load();
QMutexLocker locker(&m_mutex);
if (m_enabled) {
m_matcher->update();
}
else {
m_matcher->clear();
}
}
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const { QList<AdBlockSubscription*> AdBlockManager::subscriptions() const {
return m_subscriptions; return m_subscriptions;
} }
@ -155,9 +129,9 @@ bool AdBlockManager::addSubscriptionFromUrl(const QUrl& url) {
QMessageBox::StandardButton result = QMessageBox::question(nullptr, tr("Add AdBlock subscription"), message, QMessageBox::StandardButton result = QMessageBox::question(nullptr, tr("Add AdBlock subscription"), message,
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (result == QMessageBox::Yes) { if (result == QMessageBox::StandardButton::Yes) {
AdBlockManager::instance()->addSubscription(subscriptionTitle, subscriptionUrl); qApp->web()->adBlock()->addSubscription(subscriptionTitle, subscriptionUrl);
AdBlockManager::instance()->showDialog(); qApp->web()->adBlock()->showDialog();
} }
return true; return true;
@ -223,64 +197,72 @@ QString AdBlockManager::storedListsPath() {
return qApp->userDataFolder() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY; return qApp->userDataFolder() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY;
} }
void AdBlockManager::load() { void AdBlockManager::load(bool initial_load) {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
auto new_enabled = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)).toBool();
if (m_loaded) { if (!initial_load) {
new_enabled = !new_enabled;
}
if (new_enabled != m_enabled) {
emit enabledChanged(new_enabled);
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, new_enabled);
}
else if (!initial_load) {
return; return;
} }
m_enabled = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)).toBool(); m_enabled = new_enabled;
if (!m_loaded) {
m_disabledRules = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList(); m_disabledRules = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList();
QDateTime lastUpdate = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdatedOn)).toDateTime();
if (!m_enabled) { QDateTime last_update = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdatedOn)).toDateTime();
return; QDir adblock_dir(storedListsPath());
}
QDir adblockDir(storedListsPath());
// Create if neccessary // Create if neccessary
if (!adblockDir.exists()) { if (!adblock_dir.exists()) {
QDir().mkpath(storedListsPath()); QDir().mkpath(storedListsPath());
} }
for (const QString& fileName : adblockDir.entryList(QStringList("*.txt"), QDir::Files)) { for (const QString& subscription_file_name : adblock_dir.entryList(QStringList("*.txt"), QDir::Files)) {
if (fileName == ADBLOCK_CUSTOMLIST_NAME) { if (subscription_file_name == ADBLOCK_CUSTOMLIST_NAME) {
continue; continue;
} }
const QString absolutePath = adblockDir.absoluteFilePath(fileName); const QString absolute_path = adblock_dir.absoluteFilePath(subscription_file_name);
QFile file(absolutePath); QFile file(absolute_path);
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::OpenModeFlag::ReadOnly)) {
continue; continue;
} }
QTextStream textStream(&file); QTextStream subscription_stream(&file);
textStream.setCodec("UTF-8"); subscription_stream.setCodec("UTF-8");
QString title = textStream.readLine(1024).remove(QLatin1String("Title: ")); QString title = subscription_stream.readLine(1024).remove(QLatin1String("Title: "));
QUrl url = QUrl(textStream.readLine(1024).remove(QLatin1String("Url: "))); QUrl url = QUrl(subscription_stream.readLine(1024).remove(QLatin1String("Url: ")));
if (title.isEmpty() || !url.isValid()) { if (title.isEmpty() || !url.isValid()) {
qWarningNN << LOGSEC_ADBLOCK qWarningNN << LOGSEC_ADBLOCK
<< "Invalid AdBlock subscription file" << "Invalid AdBlock subscription file"
<< QUOTE_W_SPACE_DOT(absolutePath); << QUOTE_W_SPACE_DOT(absolute_path);
continue; continue;
} }
auto* subscription = new AdBlockSubscription(title, this); auto* subscription = new AdBlockSubscription(title, this);
subscription->setUrl(url); subscription->setUrl(url);
subscription->setFilePath(absolutePath); subscription->setFilePath(absolute_path);
m_subscriptions.append(subscription); m_subscriptions.append(subscription);
} }
// Append CustomList. // Append CustomList.
auto* customList = new AdBlockCustomList(this); auto* custom_list = new AdBlockCustomList(this);
m_subscriptions.append(customList); m_subscriptions.append(custom_list);
// Load all subscriptions. // Load all subscriptions.
for (AdBlockSubscription* subscription : m_subscriptions) { for (AdBlockSubscription* subscription : m_subscriptions) {
@ -288,13 +270,20 @@ void AdBlockManager::load() {
connect(subscription, &AdBlockSubscription::subscriptionChanged, this, &AdBlockManager::updateMatcher); connect(subscription, &AdBlockSubscription::subscriptionChanged, this, &AdBlockManager::updateMatcher);
} }
if (lastUpdate.addDays(ADBLOCK_UPDATE_DAYS_INTERVAL) < QDateTime::currentDateTime()) { if (last_update.addDays(ADBLOCK_UPDATE_DAYS_INTERVAL) < QDateTime::currentDateTime()) {
QTimer::singleShot(1000 * 60, this, &AdBlockManager::updateAllSubscriptions); QTimer::singleShot(1000 * 60, this, &AdBlockManager::updateAllSubscriptions);
} }
m_matcher->update(); qApp->web()->urlIinterceptor()->installUrlInterceptor(m_interceptor);
m_loaded = true; m_loaded = true;
qApp->urlIinterceptor()->installUrlInterceptor(m_interceptor); }
if (m_enabled) {
m_matcher->update();
}
else {
m_matcher->clear();
}
} }
void AdBlockManager::updateMatcher() { void AdBlockManager::updateMatcher() {
@ -329,7 +318,9 @@ bool AdBlockManager::isEnabled() const {
} }
bool AdBlockManager::canRunOnScheme(const QString& scheme) const { bool AdBlockManager::canRunOnScheme(const QString& scheme) const {
return !(scheme == QSL("file") || scheme == QSL("qrc") || scheme == QSL("data") || scheme == QSL("abp")); return !(scheme == QSL("file") || scheme == QSL("qrc") ||
scheme == QSL("data") || scheme == QSL("abp") ||
scheme == QSL(APP_LOW_NAME));
} }
bool AdBlockManager::canBeBlocked(const QUrl& url) const { bool AdBlockManager::canBeBlocked(const QUrl& url) const {
@ -365,9 +356,5 @@ AdBlockSubscription* AdBlockManager::subscriptionByName(const QString& name) con
} }
void AdBlockManager::showDialog() { void AdBlockManager::showDialog() {
if (m_adBlockDialog == nullptr) { AdBlockDialog(qApp->mainFormWidget()).exec();
m_adBlockDialog = new AdBlockDialog();
}
m_adBlockDialog.data()->exec();
} }

View file

@ -31,7 +31,6 @@ class AdBlockMatcher;
class AdBlockCustomList; class AdBlockCustomList;
class AdBlockSubscription; class AdBlockSubscription;
class AdBlockRule; class AdBlockRule;
class AdBlockDialog;
class AdBlockUrlInterceptor; class AdBlockUrlInterceptor;
class AdBlockIcon; class AdBlockIcon;
@ -39,12 +38,18 @@ class AdBlockManager : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit AdBlockManager(QObject* parent = 0); explicit AdBlockManager(QObject* parent = nullptr);
virtual ~AdBlockManager(); virtual ~AdBlockManager();
void load(); // If "initial_load" is true, then we want to explicitly turn off
// Adblock if it is running or turn on when not running.
// if "initial_load" is true, then we want to forcefully perform
// initial loading of Adblock.
void load(bool initial_load);
void save(); void save();
bool block(QWebEngineUrlRequestInfo& request);
bool isEnabled() const; bool isEnabled() const;
bool canRunOnScheme(const QString& scheme) const; bool canRunOnScheme(const QString& scheme) const;
@ -52,41 +57,33 @@ class AdBlockManager : public QObject {
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);
QStringList disabledRules() const; QStringList disabledRules() const;
void addDisabledRule(const QString& filter); void addDisabledRule(const QString& filter);
void removeDisabledRule(const QString& filter); void removeDisabledRule(const QString& filter);
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;
AdBlockIcon* adBlockIcon() const;
inline AdBlockIcon* adBlockIcon() const {
return m_adblockIcon;
}
static QString storedListsPath(); static QString storedListsPath();
static AdBlockManager* instance();
signals:
void enabledChanged(bool enabled);
public slots: public slots:
void setEnabled(bool enabled);
void updateMatcher(); void updateMatcher();
void updateAllSubscriptions(); void updateAllSubscriptions();
void showDialog(); void showDialog();
signals:
void enabledChanged(bool enabled);
private: private:
inline bool canBeBlocked(const QUrl& url) const; inline bool canBeBlocked(const QUrl& url) const;
private:
bool m_loaded; bool m_loaded;
bool m_enabled; bool m_enabled;
AdBlockIcon* m_adblockIcon; AdBlockIcon* m_adblockIcon;
@ -94,8 +91,11 @@ class AdBlockManager : public QObject {
AdBlockMatcher* m_matcher; AdBlockMatcher* m_matcher;
QStringList m_disabledRules; QStringList m_disabledRules;
AdBlockUrlInterceptor* m_interceptor; AdBlockUrlInterceptor* m_interceptor;
QPointer<AdBlockDialog> m_adBlockDialog;
QMutex m_mutex; QMutex m_mutex;
}; };
inline AdBlockIcon* AdBlockManager::adBlockIcon() const {
return m_adblockIcon;
}
#endif // ADBLOCKMANAGER_H #endif // ADBLOCKMANAGER_H

View file

@ -54,6 +54,7 @@
#include "network-web/adblock/adblockmanager.h" #include "network-web/adblock/adblockmanager.h"
#include "network-web/adblock/adblocksearchtree.h" #include "network-web/adblock/adblocksearchtree.h"
#include "network-web/silentnetworkaccessmanager.h" #include "network-web/silentnetworkaccessmanager.h"
#include "network-web/webfactory.h"
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
@ -172,7 +173,7 @@ void AdBlockSubscription::subscriptionDownloaded() {
return; return;
} }
loadSubscription(AdBlockManager::instance()->disabledRules()); loadSubscription(qApp->web()->adBlock()->disabledRules());
emit subscriptionUpdated(); emit subscriptionUpdated();
emit subscriptionChanged(); emit subscriptionChanged();
} }
@ -214,7 +215,7 @@ const AdBlockRule* AdBlockSubscription::enableRule(int offset) {
AdBlockRule* rule = m_rules[offset]; AdBlockRule* rule = m_rules[offset];
rule->setEnabled(true); rule->setEnabled(true);
AdBlockManager::instance()->removeDisabledRule(rule->filter()); qApp->web()->adBlock()->removeDisabledRule(rule->filter());
emit subscriptionChanged(); emit subscriptionChanged();
return rule; return rule;
@ -232,7 +233,7 @@ const AdBlockRule* AdBlockSubscription::disableRule(int offset) {
AdBlockRule* rule = m_rules[offset]; AdBlockRule* rule = m_rules[offset];
rule->setEnabled(false); rule->setEnabled(false);
AdBlockManager::instance()->addDisabledRule(rule->filter()); qApp->web()->adBlock()->addDisabledRule(rule->filter());
emit subscriptionChanged(); emit subscriptionChanged();
return rule; return rule;
@ -383,7 +384,7 @@ bool AdBlockCustomList::removeRule(int offset) {
m_rules.remove(offset); m_rules.remove(offset);
emit subscriptionChanged(); emit subscriptionChanged();
AdBlockManager::instance()->removeDisabledRule(filter); qApp->web()->adBlock()->removeDisabledRule(filter);
delete rule; delete rule;
return true; return true;
} }

View file

@ -37,7 +37,7 @@ AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget*
setIndentation(5); setIndentation(5);
connect(this, &QWidget::customContextMenuRequested, this, &AdBlockTreeWidget::contextMenuRequested); connect(this, &QWidget::customContextMenuRequested, this, &AdBlockTreeWidget::contextMenuRequested);
connect(this, &AdBlockTreeWidget::itemChanged, this, &AdBlockTreeWidget::itemChanged); connect(this, &QTreeWidget::itemChanged, this, &AdBlockTreeWidget::itemChanged);
connect(m_subscription, &AdBlockSubscription::subscriptionUpdated, this, &AdBlockTreeWidget::subscriptionUpdated); connect(m_subscription, &AdBlockSubscription::subscriptionUpdated, this, &AdBlockTreeWidget::subscriptionUpdated);
connect(m_subscription, &AdBlockSubscription::subscriptionError, this, &AdBlockTreeWidget::subscriptionError); connect(m_subscription, &AdBlockSubscription::subscriptionError, this, &AdBlockTreeWidget::subscriptionError);
} }
@ -193,10 +193,8 @@ void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockR
item->setCheckState(0, Qt::Unchecked); item->setCheckState(0, Qt::Unchecked);
item->setFont(0, font); item->setFont(0, font);
} }
return;
} }
else {
item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Checked); item->setCheckState(0, Qt::Checked);
@ -208,6 +206,11 @@ void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockR
item->setForeground(0, QColor(Qt::darkBlue)); item->setForeground(0, QColor(Qt::darkBlue));
item->setFont(0, QFont()); item->setFont(0, QFont());
} }
else {
item->setForeground(0, QColor(Qt::black));
item->setFont(0, QFont());
}
}
} }
void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) { void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) {

View file

@ -48,6 +48,6 @@ void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor* interceptor) {
m_interceptors.removeOne(interceptor); m_interceptors.removeOne(interceptor);
} }
void NetworkUrlInterceptor::loadSettings() { void NetworkUrlInterceptor::load() {
m_sendDNT = qApp->settings()->value(GROUP(Browser), SETTING(Browser::SendDNT)).toBool(); m_sendDNT = qApp->settings()->value(GROUP(Browser), SETTING(Browser::SendDNT)).toBool();
} }

View file

@ -35,7 +35,7 @@ class NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor {
void installUrlInterceptor(UrlInterceptor* interceptor); void installUrlInterceptor(UrlInterceptor* interceptor);
void removeUrlInterceptor(UrlInterceptor* interceptor); void removeUrlInterceptor(UrlInterceptor* interceptor);
void loadSettings(); void load();
private: private:
QList<UrlInterceptor*> m_interceptors; QList<UrlInterceptor*> m_interceptors;

View file

@ -36,7 +36,7 @@ void RssGuardSchemeHandler::requestStarted(QWebEngineUrlRequestJob* job) {
QByteArray data = targetData(job->requestUrl()); QByteArray data = targetData(job->requestUrl());
if (data.isEmpty()) { if (data.isEmpty()) {
job->fail(QWebEngineUrlRequestJob::UrlNotFound); job->fail(QWebEngineUrlRequestJob::Error::UrlNotFound);
} }
else { else {
auto* buf = new QBuffer(job); auto* buf = new QBuffer(job);

View file

@ -10,13 +10,47 @@
#include <QUrl> #include <QUrl>
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
#include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockmanager.h"
#include "network-web/networkurlinterceptor.h"
#include "network-web/rssguardschemehandler.h"
#include "network-web/urlinterceptor.h"
#include <QWebEngineDownloadItem>
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineUrlScheme>
#endif #endif
WebFactory::WebFactory(QObject* parent) WebFactory::WebFactory(QObject* parent)
: QObject(parent) { : QObject(parent) {
#if defined (USE_WEBENGINE) #if defined (USE_WEBENGINE)
m_engineSettings = nullptr; m_engineSettings = nullptr;
m_adBlock = new AdBlockManager(this);
m_urlInterceptor = new NetworkUrlInterceptor(this);
#endif
#if defined(USE_WEBENGINE)
QWebEngineUrlScheme url_scheme(QByteArray(APP_LOW_NAME));
url_scheme.setDefaultPort(QWebEngineUrlScheme::SpecialPort::PortUnspecified);
url_scheme.setSyntax(QWebEngineUrlScheme::Syntax::Host);
url_scheme.setFlags(QWebEngineUrlScheme::Flag::LocalScheme |
QWebEngineUrlScheme::Flag::LocalAccessAllowed |
QWebEngineUrlScheme::Flag::ServiceWorkersAllowed |
QWebEngineUrlScheme::Flag::ContentSecurityPolicyIgnored);
QWebEngineUrlScheme::registerScheme(url_scheme);
#if QT_VERSION >= 0x050D00 // Qt >= 5.13.0
QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor(m_urlInterceptor);
#else
QWebEngineProfile::defaultProfile()->setRequestInterceptor(m_urlInterceptor);
#endif
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler(QByteArray(APP_LOW_NAME),
new RssGuardSchemeHandler(QWebEngineProfile::defaultProfile()));
#endif #endif
} }
@ -187,6 +221,14 @@ void WebFactory::updateProxy() {
} }
#if defined (USE_WEBENGINE) #if defined (USE_WEBENGINE)
AdBlockManager* WebFactory::adBlock() {
return m_adBlock;
}
NetworkUrlInterceptor* WebFactory::urlIinterceptor() {
return m_urlInterceptor;
}
QAction* WebFactory::engineSettingsAction() { QAction* WebFactory::engineSettingsAction() {
if (m_engineSettings == nullptr) { if (m_engineSettings == nullptr) {
m_engineSettings = new QAction(qApp->icons()->fromTheme(QSL("applications-internet")), tr("Web engine settings"), this); m_engineSettings = new QAction(qApp->icons()->fromTheme(QSL("applications-internet")), tr("Web engine settings"), this);

View file

@ -15,6 +15,8 @@
#if defined (USE_WEBENGINE) #if defined (USE_WEBENGINE)
class QMenu; class QMenu;
class AdBlockManager;
class NetworkUrlInterceptor;
#endif #endif
class WebFactory : public QObject { class WebFactory : public QObject {
@ -35,6 +37,8 @@ class WebFactory : public QObject {
#if defined (USE_WEBENGINE) #if defined (USE_WEBENGINE)
QAction* engineSettingsAction(); QAction* engineSettingsAction();
AdBlockManager* adBlock();
NetworkUrlInterceptor* urlIinterceptor();
#endif #endif
public slots: public slots:
@ -54,11 +58,14 @@ class WebFactory : public QObject {
private: private:
void generateUnescapes(); void generateUnescapes();
QMap<QString, char16_t> m_htmlNamedEntities; private:
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
AdBlockManager * m_adBlock;
NetworkUrlInterceptor* m_urlInterceptor;
QAction* m_engineSettings; QAction* m_engineSettings;
#endif #endif
QMap<QString, char16_t> m_htmlNamedEntities;
}; };
#endif // WEBFACTORY_H #endif // WEBFACTORY_H