Big fixes for adblock - fixed adding of custom rules, formating of rules in adblock conf dialog.
This commit is contained in:
parent
d019fccceb
commit
cb72db0345
21 changed files with 210 additions and 215 deletions
|
@ -19,7 +19,7 @@
|
|||
|
||||
#define ADBLOCK_ADBLOCKED_PAGE "adblockedpage"
|
||||
#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_DISABLED "adblock-disabled"
|
||||
#define IS_IN_ARRAY(offset, array) ((offset >= 0) && (offset < array.count()))
|
||||
|
|
|
@ -63,7 +63,7 @@ FormMain::FormMain(QWidget* parent, Qt::WindowFlags f)
|
|||
setWindowTitle(APP_LONG_NAME);
|
||||
|
||||
#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());
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
||||
const QPoint pos = event->globalPos();
|
||||
|
|
|
@ -30,24 +30,15 @@
|
|||
#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 <QWebEngineScript>
|
||||
#include <QWebEngineScriptCollection>
|
||||
#include <QWebEngineUrlScheme>
|
||||
#endif
|
||||
|
||||
Application::Application(const QString& id, int& argc, char** argv)
|
||||
: QtSingleApplication(id, argc, argv), m_updateFeedsLock(new Mutex()) {
|
||||
parseCmdArguments();
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
m_urlInterceptor = new NetworkUrlInterceptor(this);
|
||||
#endif
|
||||
|
||||
m_feedReader = nullptr;
|
||||
m_quitLogicDone = false;
|
||||
m_mainForm = nullptr;
|
||||
|
@ -79,32 +70,12 @@ Application::Application(const QString& id, int& argc, char** argv)
|
|||
connect(this, &Application::saveStateRequest, this, &Application::onSaveState);
|
||||
|
||||
#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);
|
||||
|
||||
#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
|
||||
|
||||
m_webFactory->updateProxy();
|
||||
m_webFactory->urlIinterceptor()->load();
|
||||
m_webFactory->adBlock()->load(true);
|
||||
}
|
||||
|
||||
Application::~Application() {
|
||||
|
@ -189,7 +160,7 @@ QList<QAction*> Application::userActions() {
|
|||
m_userActions = m_mainForm->allActions();
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
m_userActions.append(AdBlockManager::instance()->adBlockIcon());
|
||||
m_userActions.append(m_webFactory->adBlock()->adBlockIcon());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -419,13 +390,6 @@ SystemTrayIcon* Application::trayIcon() {
|
|||
return m_trayIcon;
|
||||
}
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
NetworkUrlInterceptor* Application::urlIinterceptor() {
|
||||
return m_urlInterceptor;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
QIcon Application::desktopAwareIcon() const {
|
||||
auto from_theme = m_icons->fromTheme(APP_LOW_NAME);
|
||||
|
||||
|
@ -497,7 +461,7 @@ void Application::onAboutToQuit() {
|
|||
m_quitLogicDone = true;
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
AdBlockManager::instance()->save();
|
||||
m_webFactory->adBlock()->save();
|
||||
#endif
|
||||
|
||||
// Make sure that we obtain close lock BEFORE even trying to quit the application.
|
||||
|
|
|
@ -36,10 +36,6 @@ class Mutex;
|
|||
class QWebEngineDownloadItem;
|
||||
class WebFactory;
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
class NetworkUrlInterceptor;
|
||||
#endif
|
||||
|
||||
class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
|
||||
Q_OBJECT
|
||||
|
||||
|
@ -81,10 +77,6 @@ class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
|
|||
QWidget* mainFormWidget();
|
||||
SystemTrayIcon* trayIcon();
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
NetworkUrlInterceptor* urlIinterceptor();
|
||||
#endif
|
||||
|
||||
QIcon desktopAwareIcon() const;
|
||||
|
||||
QString tempFolder() const;
|
||||
|
@ -124,8 +116,6 @@ class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
|
|||
// Custom debug/console log handler.
|
||||
static void performLogging(QtMsgType type, const QMessageLogContext& context, const QString& msg);
|
||||
|
||||
//static QString s_customLogFile;
|
||||
|
||||
public slots:
|
||||
|
||||
// Restarts the application.
|
||||
|
@ -153,11 +143,6 @@ class RSSGUARD_DLLSPEC Application : public QtSingleApplication {
|
|||
|
||||
private:
|
||||
QCommandLineParser m_cmdParser;
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
NetworkUrlInterceptor* m_urlInterceptor;
|
||||
#endif
|
||||
|
||||
FeedReader* m_feedReader;
|
||||
|
||||
bool m_quitLogicDone;
|
||||
|
|
|
@ -104,3 +104,10 @@ void AdBlockAddSubscriptionDialog::presetsEnabledChanged(bool enabled) {
|
|||
AdBlockAddSubscriptionDialog::~AdBlockAddSubscriptionDialog() {
|
||||
delete m_ui;
|
||||
}
|
||||
|
||||
AdBlockAddSubscriptionDialog::Subscription::Subscription() {}
|
||||
|
||||
AdBlockAddSubscriptionDialog::Subscription::Subscription(const QString& title, const QString& url) {
|
||||
m_title = title;
|
||||
m_url = url;
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ class AdBlockAddSubscriptionDialog : public QDialog {
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockAddSubscriptionDialog(QWidget* parent = 0);
|
||||
explicit AdBlockAddSubscriptionDialog(QWidget* parent = nullptr);
|
||||
virtual ~AdBlockAddSubscriptionDialog();
|
||||
|
||||
QString title() const;
|
||||
|
@ -44,21 +44,15 @@ class AdBlockAddSubscriptionDialog : public QDialog {
|
|||
void presetsEnabledChanged(bool enabled);
|
||||
|
||||
private:
|
||||
Ui::AdBlockAddSubscriptionDialog* m_ui;
|
||||
|
||||
struct Subscription {
|
||||
QString m_title;
|
||||
QString m_url;
|
||||
|
||||
Subscription() {}
|
||||
|
||||
Subscription(const QString& t, const QString& u) {
|
||||
m_title = t;
|
||||
m_url = u;
|
||||
}
|
||||
|
||||
explicit Subscription();
|
||||
explicit Subscription(const QString& title, const QString& url);
|
||||
};
|
||||
|
||||
Ui::AdBlockAddSubscriptionDialog* m_ui;
|
||||
QVector<Subscription> m_knownSubscriptions;
|
||||
};
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "network-web/adblock/adblocktreewidget.h"
|
||||
|
||||
#include "definitions/definitions.h"
|
||||
#include "gui/guiutilities.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/iconfactory.h"
|
||||
#include "network-web/webfactory.h"
|
||||
|
@ -35,13 +36,15 @@
|
|||
#include <QTimer>
|
||||
|
||||
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_ui->setupUi(this);
|
||||
m_ui->m_cbEnable->setChecked(m_manager->isEnabled());
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint);
|
||||
setWindowIcon(qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE));
|
||||
|
||||
GuiUtilities::applyDialogProperties(*this,
|
||||
qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE),
|
||||
tr("AdBlock configuration"));
|
||||
|
||||
QPushButton* btn_options = m_ui->m_buttonBox->addButton(QDialogButtonBox::FirstButton);
|
||||
|
||||
btn_options->setText(tr("Options"));
|
||||
|
@ -55,11 +58,14 @@ AdBlockDialog::AdBlockDialog(QWidget* parent)
|
|||
menu->addAction(tr("Update subscriptions"), m_manager, &AdBlockManager::updateAllSubscriptions);
|
||||
menu->addSeparator();
|
||||
menu->addAction(tr("Learn about writing rules..."), this, &AdBlockDialog::learnAboutRules);
|
||||
|
||||
btn_options->setMenu(menu);
|
||||
|
||||
connect(menu, &QMenu::aboutToShow, this, &AdBlockDialog::aboutToShowMenu);
|
||||
connect(m_ui->m_cbEnable, &QCheckBox::toggled, this, &AdBlockDialog::enableAdBlock);
|
||||
connect(m_ui->m_tabSubscriptions, &QTabWidget::currentChanged, this, &AdBlockDialog::currentChanged);
|
||||
connect(m_ui->m_buttonBox, &QDialogButtonBox::rejected, this, &AdBlockDialog::close);
|
||||
|
||||
load();
|
||||
m_ui->m_buttonBox->setFocus();
|
||||
}
|
||||
|
@ -69,10 +75,10 @@ void AdBlockDialog::showRule(const AdBlockRule* rule) const {
|
|||
|
||||
if (subscription != nullptr) {
|
||||
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()) {
|
||||
treeWidget->showRule(rule);
|
||||
if (subscription == tree_widget->subscription()) {
|
||||
tree_widget->showRule(rule);
|
||||
m_ui->m_tabSubscriptions->setCurrentIndex(i);
|
||||
break;
|
||||
}
|
||||
|
@ -120,7 +126,7 @@ void AdBlockDialog::currentChanged(int index) {
|
|||
}
|
||||
|
||||
void AdBlockDialog::enableAdBlock(bool state) {
|
||||
m_manager->setEnabled(state);
|
||||
m_manager->load(false);
|
||||
|
||||
if (state) {
|
||||
load();
|
||||
|
@ -142,9 +148,9 @@ void AdBlockDialog::learnAboutRules() {
|
|||
|
||||
void AdBlockDialog::loadSubscriptions() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ class AdBlockDialog : public QDialog {
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockDialog(QWidget* parent = 0);
|
||||
explicit AdBlockDialog(QWidget* parent = nullptr);
|
||||
|
||||
void showRule(const AdBlockRule* rule) const;
|
||||
|
||||
|
|
|
@ -13,9 +13,6 @@
|
|||
<height>424</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>AdBlock configuration</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="m_cbEnable">
|
||||
|
|
|
@ -33,17 +33,18 @@
|
|||
#include <QTimer>
|
||||
|
||||
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"));
|
||||
setText(QSL("AdBlock"));
|
||||
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(menu(), &QMenu::aboutToShow, this, [this]() {
|
||||
createMenu();
|
||||
});
|
||||
connect(this, &QAction::triggered, m_manager, &AdBlockManager::showDialog);
|
||||
|
||||
setEnabled(m_manager->isEnabled());
|
||||
}
|
||||
|
||||
AdBlockIcon::~AdBlockIcon() {
|
||||
|
@ -83,6 +84,7 @@ void AdBlockIcon::popupBlocked(const QString& ruleString, const QUrl& url) {
|
|||
|
||||
m_flashTimer->setInterval(500);
|
||||
m_flashTimer->start();
|
||||
|
||||
connect(m_flashTimer, &QTimer::timeout, this, &AdBlockIcon::animateIcon);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ class AdBlockIcon : public QAction {
|
|||
|
||||
public slots:
|
||||
void setEnabled(bool enabled);
|
||||
void createMenu(QMenu* menu = 0);
|
||||
void createMenu(QMenu* menu = nullptr);
|
||||
|
||||
private slots:
|
||||
void showMenu(const QPoint& pos);
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "network-web/adblock/adblockurlinterceptor.h"
|
||||
#include "network-web/networkurlinterceptor.h"
|
||||
#include "network-web/webfactory.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
|
@ -39,11 +40,9 @@
|
|||
#include <QWebEngineProfile>
|
||||
#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)) {
|
||||
load();
|
||||
: QObject(parent), m_loaded(false), m_enabled(false), m_matcher(new AdBlockMatcher(this)),
|
||||
m_interceptor(new AdBlockUrlInterceptor(this)) {
|
||||
m_adblockIcon = new AdBlockIcon(this);
|
||||
m_adblockIcon->setObjectName(QSL("m_adblockIconAction"));
|
||||
}
|
||||
|
@ -52,31 +51,6 @@ AdBlockManager::~AdBlockManager() {
|
|||
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 {
|
||||
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::Yes | QMessageBox::No, QMessageBox::Yes);
|
||||
|
||||
if (result == QMessageBox::Yes) {
|
||||
AdBlockManager::instance()->addSubscription(subscriptionTitle, subscriptionUrl);
|
||||
AdBlockManager::instance()->showDialog();
|
||||
if (result == QMessageBox::StandardButton::Yes) {
|
||||
qApp->web()->adBlock()->addSubscription(subscriptionTitle, subscriptionUrl);
|
||||
qApp->web()->adBlock()->showDialog();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -223,78 +197,93 @@ QString AdBlockManager::storedListsPath() {
|
|||
return qApp->userDataFolder() + QDir::separator() + ADBLOCK_LISTS_SUBDIRECTORY;
|
||||
}
|
||||
|
||||
void AdBlockManager::load() {
|
||||
void AdBlockManager::load(bool initial_load) {
|
||||
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;
|
||||
}
|
||||
|
||||
m_enabled = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)).toBool();
|
||||
m_disabledRules = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList();
|
||||
QDateTime lastUpdate = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdatedOn)).toDateTime();
|
||||
m_enabled = new_enabled;
|
||||
|
||||
if (!m_enabled) {
|
||||
return;
|
||||
}
|
||||
if (!m_loaded) {
|
||||
m_disabledRules = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList();
|
||||
|
||||
QDir adblockDir(storedListsPath());
|
||||
QDateTime last_update = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::LastUpdatedOn)).toDateTime();
|
||||
QDir adblock_dir(storedListsPath());
|
||||
|
||||
// Create if neccessary
|
||||
if (!adblockDir.exists()) {
|
||||
QDir().mkpath(storedListsPath());
|
||||
}
|
||||
|
||||
for (const QString& fileName : adblockDir.entryList(QStringList("*.txt"), QDir::Files)) {
|
||||
if (fileName == ADBLOCK_CUSTOMLIST_NAME) {
|
||||
continue;
|
||||
// Create if neccessary
|
||||
if (!adblock_dir.exists()) {
|
||||
QDir().mkpath(storedListsPath());
|
||||
}
|
||||
|
||||
const QString absolutePath = adblockDir.absoluteFilePath(fileName);
|
||||
QFile file(absolutePath);
|
||||
for (const QString& subscription_file_name : adblock_dir.entryList(QStringList("*.txt"), QDir::Files)) {
|
||||
if (subscription_file_name == ADBLOCK_CUSTOMLIST_NAME) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!file.open(QFile::ReadOnly)) {
|
||||
continue;
|
||||
const QString absolute_path = adblock_dir.absoluteFilePath(subscription_file_name);
|
||||
QFile file(absolute_path);
|
||||
|
||||
if (!file.open(QFile::OpenModeFlag::ReadOnly)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QTextStream subscription_stream(&file);
|
||||
|
||||
subscription_stream.setCodec("UTF-8");
|
||||
QString title = subscription_stream.readLine(1024).remove(QLatin1String("Title: "));
|
||||
QUrl url = QUrl(subscription_stream.readLine(1024).remove(QLatin1String("Url: ")));
|
||||
|
||||
if (title.isEmpty() || !url.isValid()) {
|
||||
qWarningNN << LOGSEC_ADBLOCK
|
||||
<< "Invalid AdBlock subscription file"
|
||||
<< QUOTE_W_SPACE_DOT(absolute_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto* subscription = new AdBlockSubscription(title, this);
|
||||
|
||||
subscription->setUrl(url);
|
||||
subscription->setFilePath(absolute_path);
|
||||
m_subscriptions.append(subscription);
|
||||
}
|
||||
|
||||
QTextStream textStream(&file);
|
||||
// Append CustomList.
|
||||
auto* custom_list = new AdBlockCustomList(this);
|
||||
|
||||
textStream.setCodec("UTF-8");
|
||||
QString title = textStream.readLine(1024).remove(QLatin1String("Title: "));
|
||||
QUrl url = QUrl(textStream.readLine(1024).remove(QLatin1String("Url: ")));
|
||||
m_subscriptions.append(custom_list);
|
||||
|
||||
if (title.isEmpty() || !url.isValid()) {
|
||||
qWarningNN << LOGSEC_ADBLOCK
|
||||
<< "Invalid AdBlock subscription file"
|
||||
<< QUOTE_W_SPACE_DOT(absolutePath);
|
||||
continue;
|
||||
// Load all subscriptions.
|
||||
for (AdBlockSubscription* subscription : m_subscriptions) {
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
connect(subscription, &AdBlockSubscription::subscriptionChanged, this, &AdBlockManager::updateMatcher);
|
||||
}
|
||||
|
||||
auto* subscription = new AdBlockSubscription(title, this);
|
||||
if (last_update.addDays(ADBLOCK_UPDATE_DAYS_INTERVAL) < QDateTime::currentDateTime()) {
|
||||
QTimer::singleShot(1000 * 60, this, &AdBlockManager::updateAllSubscriptions);
|
||||
}
|
||||
|
||||
subscription->setUrl(url);
|
||||
subscription->setFilePath(absolutePath);
|
||||
m_subscriptions.append(subscription);
|
||||
qApp->web()->urlIinterceptor()->installUrlInterceptor(m_interceptor);
|
||||
m_loaded = true;
|
||||
}
|
||||
|
||||
// Append CustomList.
|
||||
auto* customList = new AdBlockCustomList(this);
|
||||
|
||||
m_subscriptions.append(customList);
|
||||
|
||||
// Load all subscriptions.
|
||||
for (AdBlockSubscription* subscription : m_subscriptions) {
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
connect(subscription, &AdBlockSubscription::subscriptionChanged, this, &AdBlockManager::updateMatcher);
|
||||
if (m_enabled) {
|
||||
m_matcher->update();
|
||||
}
|
||||
|
||||
if (lastUpdate.addDays(ADBLOCK_UPDATE_DAYS_INTERVAL) < QDateTime::currentDateTime()) {
|
||||
QTimer::singleShot(1000 * 60, this, &AdBlockManager::updateAllSubscriptions);
|
||||
else {
|
||||
m_matcher->clear();
|
||||
}
|
||||
|
||||
m_matcher->update();
|
||||
m_loaded = true;
|
||||
qApp->urlIinterceptor()->installUrlInterceptor(m_interceptor);
|
||||
}
|
||||
|
||||
void AdBlockManager::updateMatcher() {
|
||||
|
@ -329,7 +318,9 @@ bool AdBlockManager::isEnabled() 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 {
|
||||
|
@ -365,9 +356,5 @@ AdBlockSubscription* AdBlockManager::subscriptionByName(const QString& name) con
|
|||
}
|
||||
|
||||
void AdBlockManager::showDialog() {
|
||||
if (m_adBlockDialog == nullptr) {
|
||||
m_adBlockDialog = new AdBlockDialog();
|
||||
}
|
||||
|
||||
m_adBlockDialog.data()->exec();
|
||||
AdBlockDialog(qApp->mainFormWidget()).exec();
|
||||
}
|
||||
|
|
|
@ -31,7 +31,6 @@ class AdBlockMatcher;
|
|||
class AdBlockCustomList;
|
||||
class AdBlockSubscription;
|
||||
class AdBlockRule;
|
||||
class AdBlockDialog;
|
||||
class AdBlockUrlInterceptor;
|
||||
class AdBlockIcon;
|
||||
|
||||
|
@ -39,12 +38,18 @@ class AdBlockManager : public QObject {
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockManager(QObject* parent = 0);
|
||||
explicit AdBlockManager(QObject* parent = nullptr);
|
||||
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();
|
||||
|
||||
bool block(QWebEngineUrlRequestInfo& request);
|
||||
|
||||
bool isEnabled() const;
|
||||
bool canRunOnScheme(const QString& scheme) const;
|
||||
|
||||
|
@ -52,41 +57,33 @@ class AdBlockManager : public QObject {
|
|||
QString elementHidingRulesForDomain(const QUrl& url) const;
|
||||
|
||||
AdBlockSubscription* subscriptionByName(const QString& name) const;
|
||||
|
||||
QList<AdBlockSubscription*> subscriptions() const;
|
||||
|
||||
bool block(QWebEngineUrlRequestInfo& request);
|
||||
|
||||
QStringList disabledRules() const;
|
||||
void addDisabledRule(const QString& filter);
|
||||
void removeDisabledRule(const QString& filter);
|
||||
|
||||
bool addSubscriptionFromUrl(const QUrl& url);
|
||||
|
||||
AdBlockSubscription* addSubscription(const QString& title, const QString& url);
|
||||
|
||||
bool removeSubscription(AdBlockSubscription* subscription);
|
||||
|
||||
AdBlockCustomList* customList() const;
|
||||
|
||||
inline AdBlockIcon* adBlockIcon() const {
|
||||
return m_adblockIcon;
|
||||
}
|
||||
AdBlockIcon* adBlockIcon() const;
|
||||
|
||||
static QString storedListsPath();
|
||||
static AdBlockManager* instance();
|
||||
|
||||
signals:
|
||||
void enabledChanged(bool enabled);
|
||||
|
||||
public slots:
|
||||
void setEnabled(bool enabled);
|
||||
void updateMatcher();
|
||||
void updateAllSubscriptions();
|
||||
void showDialog();
|
||||
|
||||
signals:
|
||||
void enabledChanged(bool enabled);
|
||||
|
||||
private:
|
||||
inline bool canBeBlocked(const QUrl& url) const;
|
||||
|
||||
private:
|
||||
bool m_loaded;
|
||||
bool m_enabled;
|
||||
AdBlockIcon* m_adblockIcon;
|
||||
|
@ -94,8 +91,11 @@ class AdBlockManager : public QObject {
|
|||
AdBlockMatcher* m_matcher;
|
||||
QStringList m_disabledRules;
|
||||
AdBlockUrlInterceptor* m_interceptor;
|
||||
QPointer<AdBlockDialog> m_adBlockDialog;
|
||||
QMutex m_mutex;
|
||||
};
|
||||
|
||||
inline AdBlockIcon* AdBlockManager::adBlockIcon() const {
|
||||
return m_adblockIcon;
|
||||
}
|
||||
|
||||
#endif // ADBLOCKMANAGER_H
|
||||
|
|
|
@ -54,6 +54,7 @@
|
|||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblocksearchtree.h"
|
||||
#include "network-web/silentnetworkaccessmanager.h"
|
||||
#include "network-web/webfactory.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
@ -172,7 +173,7 @@ void AdBlockSubscription::subscriptionDownloaded() {
|
|||
return;
|
||||
}
|
||||
|
||||
loadSubscription(AdBlockManager::instance()->disabledRules());
|
||||
loadSubscription(qApp->web()->adBlock()->disabledRules());
|
||||
emit subscriptionUpdated();
|
||||
emit subscriptionChanged();
|
||||
}
|
||||
|
@ -214,7 +215,7 @@ const AdBlockRule* AdBlockSubscription::enableRule(int offset) {
|
|||
AdBlockRule* rule = m_rules[offset];
|
||||
|
||||
rule->setEnabled(true);
|
||||
AdBlockManager::instance()->removeDisabledRule(rule->filter());
|
||||
qApp->web()->adBlock()->removeDisabledRule(rule->filter());
|
||||
emit subscriptionChanged();
|
||||
|
||||
return rule;
|
||||
|
@ -232,7 +233,7 @@ const AdBlockRule* AdBlockSubscription::disableRule(int offset) {
|
|||
AdBlockRule* rule = m_rules[offset];
|
||||
|
||||
rule->setEnabled(false);
|
||||
AdBlockManager::instance()->addDisabledRule(rule->filter());
|
||||
qApp->web()->adBlock()->addDisabledRule(rule->filter());
|
||||
emit subscriptionChanged();
|
||||
|
||||
return rule;
|
||||
|
@ -383,7 +384,7 @@ bool AdBlockCustomList::removeRule(int offset) {
|
|||
m_rules.remove(offset);
|
||||
emit subscriptionChanged();
|
||||
|
||||
AdBlockManager::instance()->removeDisabledRule(filter);
|
||||
qApp->web()->adBlock()->removeDisabledRule(filter);
|
||||
delete rule;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget*
|
|||
setIndentation(5);
|
||||
|
||||
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::subscriptionError, this, &AdBlockTreeWidget::subscriptionError);
|
||||
}
|
||||
|
@ -193,20 +193,23 @@ void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockR
|
|||
item->setCheckState(0, Qt::Unchecked);
|
||||
item->setFont(0, font);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
else {
|
||||
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
||||
item->setCheckState(0, Qt::Checked);
|
||||
|
||||
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
||||
item->setCheckState(0, Qt::Checked);
|
||||
|
||||
if (rule->isException()) {
|
||||
item->setForeground(0, QColor(Qt::darkGreen));
|
||||
item->setFont(0, QFont());
|
||||
}
|
||||
else if (rule->isCssRule()) {
|
||||
item->setForeground(0, QColor(Qt::darkBlue));
|
||||
item->setFont(0, QFont());
|
||||
if (rule->isException()) {
|
||||
item->setForeground(0, QColor(Qt::darkGreen));
|
||||
item->setFont(0, QFont());
|
||||
}
|
||||
else if (rule->isCssRule()) {
|
||||
item->setForeground(0, QColor(Qt::darkBlue));
|
||||
item->setFont(0, QFont());
|
||||
}
|
||||
else {
|
||||
item->setForeground(0, QColor(Qt::black));
|
||||
item->setFont(0, QFont());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,6 @@ void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor* interceptor) {
|
|||
m_interceptors.removeOne(interceptor);
|
||||
}
|
||||
|
||||
void NetworkUrlInterceptor::loadSettings() {
|
||||
void NetworkUrlInterceptor::load() {
|
||||
m_sendDNT = qApp->settings()->value(GROUP(Browser), SETTING(Browser::SendDNT)).toBool();
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ class NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor {
|
|||
void installUrlInterceptor(UrlInterceptor* interceptor);
|
||||
void removeUrlInterceptor(UrlInterceptor* interceptor);
|
||||
|
||||
void loadSettings();
|
||||
void load();
|
||||
|
||||
private:
|
||||
QList<UrlInterceptor*> m_interceptors;
|
||||
|
|
|
@ -36,7 +36,7 @@ void RssGuardSchemeHandler::requestStarted(QWebEngineUrlRequestJob* job) {
|
|||
QByteArray data = targetData(job->requestUrl());
|
||||
|
||||
if (data.isEmpty()) {
|
||||
job->fail(QWebEngineUrlRequestJob::UrlNotFound);
|
||||
job->fail(QWebEngineUrlRequestJob::Error::UrlNotFound);
|
||||
}
|
||||
else {
|
||||
auto* buf = new QBuffer(job);
|
||||
|
|
|
@ -9,14 +9,48 @@
|
|||
#include <QProcess>
|
||||
#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 <QWebEngineScript>
|
||||
#include <QWebEngineScriptCollection>
|
||||
#include <QWebEngineUrlScheme>
|
||||
#endif
|
||||
|
||||
WebFactory::WebFactory(QObject* parent)
|
||||
: QObject(parent) {
|
||||
#if defined (USE_WEBENGINE)
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -187,6 +221,14 @@ void WebFactory::updateProxy() {
|
|||
}
|
||||
|
||||
#if defined (USE_WEBENGINE)
|
||||
AdBlockManager* WebFactory::adBlock() {
|
||||
return m_adBlock;
|
||||
}
|
||||
|
||||
NetworkUrlInterceptor* WebFactory::urlIinterceptor() {
|
||||
return m_urlInterceptor;
|
||||
}
|
||||
|
||||
QAction* WebFactory::engineSettingsAction() {
|
||||
if (m_engineSettings == nullptr) {
|
||||
m_engineSettings = new QAction(qApp->icons()->fromTheme(QSL("applications-internet")), tr("Web engine settings"), this);
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#if defined (USE_WEBENGINE)
|
||||
class QMenu;
|
||||
class AdBlockManager;
|
||||
class NetworkUrlInterceptor;
|
||||
#endif
|
||||
|
||||
class WebFactory : public QObject {
|
||||
|
@ -35,6 +37,8 @@ class WebFactory : public QObject {
|
|||
|
||||
#if defined (USE_WEBENGINE)
|
||||
QAction* engineSettingsAction();
|
||||
AdBlockManager* adBlock();
|
||||
NetworkUrlInterceptor* urlIinterceptor();
|
||||
#endif
|
||||
|
||||
public slots:
|
||||
|
@ -54,11 +58,14 @@ class WebFactory : public QObject {
|
|||
private:
|
||||
void generateUnescapes();
|
||||
|
||||
QMap<QString, char16_t> m_htmlNamedEntities;
|
||||
|
||||
#if defined (USE_WEBENGINE)
|
||||
private:
|
||||
#if defined(USE_WEBENGINE)
|
||||
AdBlockManager * m_adBlock;
|
||||
NetworkUrlInterceptor* m_urlInterceptor;
|
||||
QAction* m_engineSettings;
|
||||
#endif
|
||||
|
||||
QMap<QString, char16_t> m_htmlNamedEntities;
|
||||
};
|
||||
|
||||
#endif // WEBFACTORY_H
|
||||
|
|
Loading…
Add table
Reference in a new issue