mirror of
https://github.com/martinrotter/rssguard.git
synced 2025-02-03 10:47:47 +01:00
Fix compilation, add interceptor to Application class.
This commit is contained in:
parent
9fdacb26f9
commit
5cedad03ac
@ -1 +1 @@
|
||||
Subproject commit aea11879635d0d6b029a7f6d49d1f31dd95b3c05
|
||||
Subproject commit 1bb36a305e8a23de1e4f3609d8fc4dfeb85ba6fe
|
BIN
resources/graphics/misc/adblock-disabled.png
Executable file
BIN
resources/graphics/misc/adblock-disabled.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 3.5 KiB |
BIN
resources/graphics/misc/adblock.png
Executable file
BIN
resources/graphics/misc/adblock.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
@ -518,7 +518,8 @@ equals(USE_WEBENGINE, true) {
|
||||
src/network-web/urlinterceptor.h \
|
||||
src/network-web/networkurlinterceptor.h \
|
||||
src/gui/clickablelabel.h \
|
||||
src/miscellaneous/simpleregexp.h
|
||||
src/miscellaneous/simpleregexp.h \
|
||||
src/gui/treewidget.h
|
||||
|
||||
SOURCES += src/network-web/adblock/adblockaddsubscriptiondialog.cpp \
|
||||
src/network-web/adblock/adblockdialog.cpp \
|
||||
@ -532,7 +533,8 @@ equals(USE_WEBENGINE, true) {
|
||||
src/network-web/adblock/adblockurlinterceptor.cpp \
|
||||
src/network-web/networkurlinterceptor.cpp \
|
||||
src/gui/clickablelabel.cpp \
|
||||
src/miscellaneous/simpleregexp.cpp
|
||||
src/miscellaneous/simpleregexp.cpp \
|
||||
src/gui/treewidget.cpp
|
||||
|
||||
FORMS += src/network-web/adblock/adblockaddsubscriptiondialog.ui \
|
||||
src/network-web/adblock/adblockdialog.ui
|
||||
|
@ -26,6 +26,7 @@
|
||||
|
||||
#define ARGUMENTS_LIST_SEPARATOR "\n"
|
||||
|
||||
#define IS_IN_ARRAY(offset, array) ((offset >= 0) && (offset < array.count()))
|
||||
#define ADBLOCK_CUSTOMLIST_NAME "customlist.txt"
|
||||
#define ADBLOCK_LISTS_SUBDIRECTORY "adblock"
|
||||
#define ADBLOCK_EASYLIST_URL "https://easylist-downloads.adblockplus.org/easylist.txt"
|
||||
|
197
src/gui/treewidget.cpp
Executable file
197
src/gui/treewidget.cpp
Executable file
@ -0,0 +1,197 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
|
||||
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
|
||||
//
|
||||
// RSS Guard is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// RSS Guard is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "gui/treewidget.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
|
||||
|
||||
TreeWidget::TreeWidget(QWidget *parent)
|
||||
: QTreeWidget(parent), m_refreshAllItemsNeeded(true), m_showMode(ItemsCollapsed) {
|
||||
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(sheduleRefresh()));
|
||||
}
|
||||
|
||||
void TreeWidget::clear() {
|
||||
QTreeWidget::clear();
|
||||
m_allTreeItems.clear();
|
||||
}
|
||||
|
||||
void TreeWidget::sheduleRefresh() {
|
||||
m_refreshAllItemsNeeded = true;
|
||||
}
|
||||
|
||||
void TreeWidget::addTopLevelItem(QTreeWidgetItem *item) {
|
||||
m_allTreeItems.append(item);
|
||||
QTreeWidget::addTopLevelItem(item);
|
||||
}
|
||||
|
||||
void TreeWidget::addTopLevelItems(const QList<QTreeWidgetItem*> &items) {
|
||||
m_allTreeItems.append(items);
|
||||
QTreeWidget::addTopLevelItems(items);
|
||||
}
|
||||
|
||||
void TreeWidget::insertTopLevelItem(int index, QTreeWidgetItem *item) {
|
||||
m_allTreeItems.append(item);
|
||||
QTreeWidget::insertTopLevelItem(index, item);
|
||||
}
|
||||
|
||||
void TreeWidget::insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items) {
|
||||
m_allTreeItems.append(items);
|
||||
QTreeWidget::insertTopLevelItems(index, items);
|
||||
}
|
||||
|
||||
void TreeWidget::mousePressEvent(QMouseEvent *event) {
|
||||
if (event->modifiers() == Qt::ControlModifier) {
|
||||
emit itemControlClicked(itemAt(event->pos()));
|
||||
}
|
||||
|
||||
if (event->buttons() == Qt::MiddleButton) {
|
||||
emit itemMiddleButtonClicked(itemAt(event->pos()));
|
||||
}
|
||||
|
||||
QTreeWidget::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void TreeWidget::iterateAllItems(QTreeWidgetItem *parent) {
|
||||
int count = parent ? parent->childCount() : topLevelItemCount();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
QTreeWidgetItem *item = parent ? parent->child(i) : topLevelItem(i);
|
||||
|
||||
if (item->childCount() == 0) {
|
||||
m_allTreeItems.append(item);
|
||||
}
|
||||
|
||||
iterateAllItems(item);
|
||||
}
|
||||
}
|
||||
|
||||
QList<QTreeWidgetItem*> TreeWidget::allItems() {
|
||||
if (m_refreshAllItemsNeeded) {
|
||||
m_allTreeItems.clear();
|
||||
iterateAllItems(0);
|
||||
m_refreshAllItemsNeeded = false;
|
||||
}
|
||||
|
||||
return m_allTreeItems;
|
||||
}
|
||||
|
||||
void TreeWidget::filterString(const QString &string) {
|
||||
QList<QTreeWidgetItem*> _allItems = allItems();
|
||||
QList<QTreeWidgetItem*> parents;
|
||||
bool stringIsEmpty = string.isEmpty();
|
||||
foreach (QTreeWidgetItem *item, _allItems) {
|
||||
bool containsString = stringIsEmpty || item->text(0).contains(string, Qt::CaseInsensitive);
|
||||
|
||||
if (containsString) {
|
||||
item->setHidden(false);
|
||||
if (item->parent()) {
|
||||
if (!parents.contains(item->parent())) {
|
||||
parents << item->parent();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
item->setHidden(true);
|
||||
if (item->parent()) {
|
||||
item->parent()->setHidden(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < parents.size(); ++i) {
|
||||
QTreeWidgetItem *parentItem = parents.at(i);
|
||||
parentItem->setHidden(false);
|
||||
|
||||
if (stringIsEmpty) {
|
||||
parentItem->setExpanded(m_showMode == ItemsExpanded);
|
||||
}
|
||||
else {
|
||||
parentItem->setExpanded(true);
|
||||
}
|
||||
|
||||
if (parentItem->parent() && !parents.contains(parentItem->parent())) {
|
||||
parents << parentItem->parent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TreeWidget::appendToParentItem(const QString &parentText, QTreeWidgetItem *item) {
|
||||
QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
|
||||
|
||||
if (list.count() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QTreeWidgetItem *parentItem = list.at(0);
|
||||
|
||||
if (!parentItem) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_allTreeItems.append(item);
|
||||
parentItem->addChild(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TreeWidget::appendToParentItem(QTreeWidgetItem *parent, QTreeWidgetItem *item) {
|
||||
if (!parent || parent->treeWidget() != this) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_allTreeItems.append(item);
|
||||
parent->addChild(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TreeWidget::prependToParentItem(const QString &parentText, QTreeWidgetItem *item) {
|
||||
QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
|
||||
if (list.count() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QTreeWidgetItem *parentItem = list.at(0);
|
||||
|
||||
if (!parentItem) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_allTreeItems.append(item);
|
||||
parentItem->insertChild(0, item);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TreeWidget::prependToParentItem(QTreeWidgetItem *parent, QTreeWidgetItem *item) {
|
||||
if (!parent || parent->treeWidget() != this) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_allTreeItems.append(item);
|
||||
parent->insertChild(0, item);
|
||||
return true;
|
||||
}
|
||||
|
||||
void TreeWidget::deleteItem(QTreeWidgetItem* item) {
|
||||
m_refreshAllItemsNeeded = true;
|
||||
delete item;
|
||||
}
|
||||
|
||||
void TreeWidget::deleteItems(const QList<QTreeWidgetItem*> &items) {
|
||||
m_refreshAllItemsNeeded = true;
|
||||
qDeleteAll(items);
|
||||
}
|
70
src/gui/treewidget.h
Executable file
70
src/gui/treewidget.h
Executable file
@ -0,0 +1,70 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
|
||||
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
|
||||
//
|
||||
// RSS Guard is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// RSS Guard is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef BOOKMARKSTREEWIDGET_H
|
||||
#define BOOKMARKSTREEWIDGET_H
|
||||
|
||||
#include <QTreeWidget>
|
||||
|
||||
|
||||
class TreeWidget : public QTreeWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TreeWidget(QWidget *parent = 0);
|
||||
|
||||
enum ItemShowMode { ItemsCollapsed = 0, ItemsExpanded = 1 };
|
||||
|
||||
ItemShowMode defaultItemShowMode() { return m_showMode; }
|
||||
void setDefaultItemShowMode(ItemShowMode mode) { m_showMode = mode; }
|
||||
QList<QTreeWidgetItem*> allItems();
|
||||
|
||||
bool appendToParentItem(const QString &parentText, QTreeWidgetItem *item);
|
||||
bool appendToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem *item);
|
||||
bool prependToParentItem(const QString &parentText, QTreeWidgetItem *item);
|
||||
bool prependToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem *item);
|
||||
|
||||
void addTopLevelItem(QTreeWidgetItem *item);
|
||||
void addTopLevelItems(const QList<QTreeWidgetItem*> &items);
|
||||
void insertTopLevelItem(int index, QTreeWidgetItem *item);
|
||||
void insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items);
|
||||
|
||||
void deleteItem(QTreeWidgetItem *item);
|
||||
void deleteItems(const QList<QTreeWidgetItem*> &items);
|
||||
|
||||
signals:
|
||||
void itemControlClicked(QTreeWidgetItem *item);
|
||||
void itemMiddleButtonClicked(QTreeWidgetItem *item);
|
||||
|
||||
public slots:
|
||||
void filterString(const QString &string);
|
||||
void clear();
|
||||
|
||||
private slots:
|
||||
void sheduleRefresh();
|
||||
|
||||
private:
|
||||
void mousePressEvent(QMouseEvent* event);
|
||||
void iterateAllItems(QTreeWidgetItem* parent);
|
||||
|
||||
bool m_refreshAllItemsNeeded;
|
||||
QList<QTreeWidgetItem*> m_allTreeItems;
|
||||
ItemShowMode m_showMode;
|
||||
};
|
||||
|
||||
#endif // BOOKMARKSTREEWIDGET_H
|
@ -27,6 +27,8 @@
|
||||
#include "gui/statusbar.h"
|
||||
#include "gui/dialogs/formmain.h"
|
||||
#include "exceptions/applicationexception.h"
|
||||
#include "network-web/urlinterceptor.h"
|
||||
#include "network-web/networkurlinterceptor.h"
|
||||
|
||||
#include "services/abstract/serviceroot.h"
|
||||
#include "services/standard/standardserviceroot.h"
|
||||
@ -44,6 +46,7 @@
|
||||
|
||||
Application::Application(const QString &id, int &argc, char **argv)
|
||||
: QtSingleApplication(id, argc, argv),
|
||||
m_urlInterceptor(new NetworkUrlInterceptor(this)),
|
||||
m_feedReader(nullptr),
|
||||
m_updateFeedsLock(nullptr), m_userActions(QList<QAction*>()), m_mainForm(nullptr),
|
||||
m_trayIcon(nullptr), m_settings(nullptr), m_system(nullptr), m_skins(nullptr),
|
||||
@ -54,6 +57,8 @@ Application::Application(const QString &id, int &argc, char **argv)
|
||||
|
||||
#if defined(USE_WEBENGINE)
|
||||
connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &Application::downloadRequested);
|
||||
|
||||
QWebEngineProfile::setRequestInterceptor(m_urlInterceptor);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -47,6 +47,7 @@ class QAction;
|
||||
class Mutex;
|
||||
class QWebEngineDownloadItem;
|
||||
class FeedReader;
|
||||
class NetworkUrlInterceptor;
|
||||
|
||||
class Application : public QtSingleApplication {
|
||||
Q_OBJECT
|
||||
@ -156,6 +157,7 @@ class Application : public QtSingleApplication {
|
||||
// action will be allowed to lock for reading.
|
||||
QScopedPointer<Mutex> m_updateFeedsLock;
|
||||
|
||||
NetworkUrlInterceptor *m_urlInterceptor;
|
||||
QList<QAction*> m_userActions;
|
||||
FormMain *m_mainForm;
|
||||
SystemTrayIcon *m_trayIcon;
|
||||
|
@ -258,6 +258,9 @@ DKEY Keyboard::ID = "keyboard";
|
||||
// Web browser.
|
||||
DKEY Browser::ID = "browser";
|
||||
|
||||
KEY Browser::SendDNT = "send_dnt";
|
||||
VALUE(bool) Browser::SendDNTDef = false;
|
||||
|
||||
DKEY Browser::OpenLinksInExternalBrowserRightAway = "open_link_externally_wo_confirmation";
|
||||
DVALUE(bool) Browser::OpenLinksInExternalBrowserRightAwayDef = false;
|
||||
|
||||
|
@ -291,6 +291,9 @@ namespace Keyboard {
|
||||
namespace Browser {
|
||||
KEY ID;
|
||||
|
||||
KEY SendDNT;
|
||||
VALUE(bool) SendDNTDef;
|
||||
|
||||
KEY OpenLinksInExternalBrowserRightAway;
|
||||
VALUE(bool) OpenLinksInExternalBrowserRightAwayDef;
|
||||
|
||||
|
@ -60,7 +60,7 @@ void AdBlockManager::setEnabled(bool enabled) {
|
||||
m_enabled = enabled;
|
||||
emit enabledChanged(enabled);
|
||||
|
||||
qApp->settings()->setValue(GROUP(Adblock), AdBlock::AdBlockEnabled, m_enabled);
|
||||
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, m_enabled);
|
||||
load();
|
||||
// TODO: Reload user stylesheet.
|
||||
//mApp->reloadUserStyleSheet();
|
||||
@ -102,7 +102,8 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) {
|
||||
|
||||
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: dodělat lepší
|
||||
QMessageBox::warning(nullptr, "blocked website", "blocket");
|
||||
|
||||
// TODO request.redirect() přesměrovat na "chybovou stranku";
|
||||
//QUrl url(QSL("rssguard:adblock"));
|
||||
@ -190,7 +191,9 @@ AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
|
||||
m_subscriptions.insert(m_subscriptions.count() - 1, subscription);
|
||||
connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
||||
|
||||
// TODO: po změně subskripce přehrat user css?
|
||||
//connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
||||
connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
|
||||
|
||||
return subscription;
|
||||
@ -290,14 +293,15 @@ void AdBlockManager::load() {
|
||||
}
|
||||
|
||||
// Append CustomList.
|
||||
AdBlockCustomList* customList = new AdBlockCustomList(this);
|
||||
AdBlockCustomList *customList = new AdBlockCustomList(this);
|
||||
m_subscriptions.append(customList);
|
||||
|
||||
// Load all subscriptions
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
|
||||
connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
||||
// TODO: po zmene subskripce prehrat user css?
|
||||
//connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet()));
|
||||
connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ AdBlockMatcher::~AdBlockMatcher() {
|
||||
clear();
|
||||
}
|
||||
|
||||
const AdBlockRule* AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const {
|
||||
const AdBlockRule *AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const {
|
||||
// Exception rules.
|
||||
if (m_networkExceptionTree.find(request, urlDomain, urlString)) {
|
||||
return 0;
|
||||
@ -41,7 +41,7 @@ const AdBlockRule* AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request
|
||||
int count = m_networkExceptionRules.count();
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const AdBlockRule* rule = m_networkExceptionRules.at(i);
|
||||
const AdBlockRule *rule = m_networkExceptionRules.at(i);
|
||||
|
||||
if (rule->networkMatch(request, urlDomain, urlString)) {
|
||||
return 0;
|
||||
@ -56,7 +56,7 @@ const AdBlockRule* AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request
|
||||
count = m_networkBlockRules.count();
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const AdBlockRule* rule = m_networkBlockRules.at(i);
|
||||
const AdBlockRule *rule = m_networkBlockRules.at(i);
|
||||
|
||||
if (rule->networkMatch(request, urlDomain, urlString)) {
|
||||
return rule;
|
||||
@ -104,7 +104,7 @@ QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
|
||||
int count = m_domainRestrictedCssRules.count();
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
|
||||
const AdBlockRule *rule = m_domainRestrictedCssRules.at(i);
|
||||
|
||||
if (!rule->matchDomain(domain)) {
|
||||
continue;
|
||||
@ -135,8 +135,8 @@ void AdBlockMatcher::update() {
|
||||
QHash<QString, const AdBlockRule*> cssRulesHash;
|
||||
QVector<const AdBlockRule*> exceptionCssRules;
|
||||
|
||||
foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) {
|
||||
foreach (const AdBlockRule* rule, subscription->allRules()) {
|
||||
foreach (AdBlockSubscription *subscription, m_manager->subscriptions()) {
|
||||
foreach (const AdBlockRule *rule, subscription->allRules()) {
|
||||
// Don't add internally disabled rules to cache.
|
||||
if (rule->isInternalDisabled()) {
|
||||
continue;
|
||||
@ -178,7 +178,7 @@ void AdBlockMatcher::update() {
|
||||
foreach (const AdBlockRule *rule, exceptionCssRules) {
|
||||
const AdBlockRule *originalRule = cssRulesHash.value(rule->cssSelector());
|
||||
|
||||
// If we don't have this selector, the exception does nothing
|
||||
// If we don't have this selector, the exception does nothing.
|
||||
if (!originalRule) {
|
||||
continue;
|
||||
}
|
||||
@ -192,11 +192,11 @@ void AdBlockMatcher::update() {
|
||||
}
|
||||
|
||||
// Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes.
|
||||
// (In my testings, 4931 is the number that makes it crash)
|
||||
// So let's split it by 1000 selectors...
|
||||
// (In my testings, 4931 is the number that makes it crash).
|
||||
// So let's split it by 1000 selectors.
|
||||
int hidingRulesCount = 0;
|
||||
|
||||
QHashIterator<QString, const AdBlockRule*> it(cssRulesHash);
|
||||
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
const AdBlockRule *rule = it.value();
|
||||
|
@ -20,10 +20,12 @@
|
||||
#define ADBLOCKMATCHER_H
|
||||
|
||||
#include <QUrl>
|
||||
#include <QObject>
|
||||
|
||||
#include "network-web/adblock/adblocksearchtree.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QVector>
|
||||
|
||||
|
||||
class QWebEngineUrlRequestInfo;
|
||||
class AdBlockManager;
|
||||
|
@ -454,7 +454,7 @@ void AdBlockRule::parseFilter() {
|
||||
|
||||
parsedLine = parsedLine.left(optionsIndex);
|
||||
}
|
||||
.
|
||||
|
||||
// Rule is classic regexp.
|
||||
if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/'))) {
|
||||
parsedLine = parsedLine.mid(1);
|
||||
@ -659,7 +659,7 @@ bool AdBlockRule::stringMatch(const QString &domain, const QString &encodedUrl)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockRule::matchDomain(const QString &pattern, const QString &domain) {
|
||||
bool AdBlockRule::matchDomain(const QString &pattern, const QString &domain) const {
|
||||
if (pattern == domain) {
|
||||
return true;
|
||||
}
|
||||
|
@ -50,11 +50,14 @@
|
||||
#include "network-web/adblock/adblocksearchtree.h"
|
||||
#include "definitions/definitions.h"
|
||||
#include "network-web/silentnetworkaccessmanager.h"
|
||||
#include "miscellaneous/iofactory.h"
|
||||
#include "miscellaneous/application.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QTimer>
|
||||
#include <QNetworkReply>
|
||||
#include <QSaveFile>
|
||||
#include <QDir>
|
||||
|
||||
|
||||
AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent)
|
||||
@ -97,7 +100,8 @@ void AdBlockSubscription::loadSubscription(const QStringList &disabledRules) {
|
||||
|
||||
QTextStream textStream(&file);
|
||||
textStream.setCodec("UTF-8");
|
||||
// Header is on 3rd line
|
||||
|
||||
// Header is on 3rd line.
|
||||
textStream.readLine(1024);
|
||||
textStream.readLine(1024);
|
||||
QString header = textStream.readLine(1024);
|
||||
@ -184,7 +188,7 @@ bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) {
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSubscription::rule(int offset) const {
|
||||
if (offset >= 0 && offset < m_rules.size()) {
|
||||
if (IS_IN_ARRAY(offset, m_rules)) {
|
||||
return m_rules[offset];
|
||||
}
|
||||
else {
|
||||
@ -197,7 +201,7 @@ QVector<AdBlockRule*> AdBlockSubscription::allRules() const {
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSubscription::enableRule(int offset) {
|
||||
if (offset >= 0 && offset < m_rules.size()) {
|
||||
if (IS_IN_ARRAY(offset, m_rules)) {
|
||||
AdBlockRule *rule = m_rules[offset];
|
||||
rule->setEnabled(true);
|
||||
AdBlockManager::instance()->removeDisabledRule(rule->filter());
|
||||
@ -216,9 +220,8 @@ const AdBlockRule *AdBlockSubscription::enableRule(int offset) {
|
||||
}
|
||||
}
|
||||
|
||||
const AdBlockRule* AdBlockSubscription::disableRule(int offset)
|
||||
{
|
||||
if (!QzTools::containsIndex(m_rules, offset)) {
|
||||
const AdBlockRule *AdBlockSubscription::disableRule(int offset) {
|
||||
if (!IS_IN_ARRAY(offset, m_rules)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -228,66 +231,60 @@ const AdBlockRule* AdBlockSubscription::disableRule(int offset)
|
||||
|
||||
emit subscriptionChanged();
|
||||
|
||||
if (rule->isCssRule())
|
||||
mApp->reloadUserStyleSheet();
|
||||
if (rule->isCssRule()) {
|
||||
// TODO: opravdu?
|
||||
//mApp->reloadUserStyleSheet();
|
||||
}
|
||||
|
||||
return rule;
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::canEditRules() const
|
||||
{
|
||||
bool AdBlockSubscription::canEditRules() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::canBeRemoved() const
|
||||
{
|
||||
bool AdBlockSubscription::canBeRemoved() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
int AdBlockSubscription::addRule(AdBlockRule* rule)
|
||||
{
|
||||
int AdBlockSubscription::addRule(AdBlockRule* rule) {
|
||||
Q_UNUSED(rule)
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::removeRule(int offset)
|
||||
{
|
||||
bool AdBlockSubscription::removeRule(int offset) {
|
||||
Q_UNUSED(offset)
|
||||
return false;
|
||||
}
|
||||
|
||||
const AdBlockRule* AdBlockSubscription::replaceRule(AdBlockRule* rule, int offset)
|
||||
{
|
||||
const AdBlockRule *AdBlockSubscription::replaceRule(AdBlockRule *rule, int offset) {
|
||||
Q_UNUSED(rule)
|
||||
Q_UNUSED(offset)
|
||||
return 0;
|
||||
}
|
||||
|
||||
AdBlockSubscription::~AdBlockSubscription()
|
||||
{
|
||||
AdBlockSubscription::~AdBlockSubscription() {
|
||||
qDeleteAll(m_rules);
|
||||
}
|
||||
|
||||
// AdBlockCustomList
|
||||
|
||||
AdBlockCustomList::AdBlockCustomList(QObject* parent)
|
||||
: AdBlockSubscription(tr("Custom Rules"), parent)
|
||||
{
|
||||
setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/customlist.txt"));
|
||||
AdBlockCustomList::AdBlockCustomList(QObject *parent)
|
||||
: AdBlockSubscription(tr("Custom rules"), parent) {
|
||||
setFilePath(AdBlockManager::instance()->storedListsPath() + QDir::separator() + ADBLOCK_CUSTOMLIST_NAME);
|
||||
}
|
||||
|
||||
void AdBlockCustomList::loadSubscription(const QStringList &disabledRules)
|
||||
{
|
||||
void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) {
|
||||
// DuckDuckGo ad whitelist rules
|
||||
// They cannot be removed, but can be disabled.
|
||||
// Please consider not disabling them. Thanks!
|
||||
|
||||
const QString ddg1 = QSL("@@||duckduckgo.com^$document");
|
||||
const QString ddg2 = QSL("duckduckgo.com#@#.has-ad");
|
||||
|
||||
const QString rules = QzTools::readAllFileContents(filePath());
|
||||
const QString rules = QString::fromUtf8(IOFactory::readTextFile(filePath()));
|
||||
|
||||
QFile file(filePath());
|
||||
|
||||
if (!file.exists()) {
|
||||
saveSubscription();
|
||||
}
|
||||
@ -302,46 +299,44 @@ void AdBlockCustomList::loadSubscription(const QStringList &disabledRules)
|
||||
if (!rules.contains(QL1S("\n") + ddg2))
|
||||
stream << ddg2 << endl;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
AdBlockSubscription::loadSubscription(disabledRules);
|
||||
}
|
||||
|
||||
void AdBlockCustomList::saveSubscription()
|
||||
{
|
||||
void AdBlockCustomList::saveSubscription() {
|
||||
QFile file(filePath());
|
||||
|
||||
if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
|
||||
qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << filePath();
|
||||
qWarning("Unable to open AdBlock file '%s' for writing.", qPrintable(filePath()));
|
||||
return;
|
||||
}
|
||||
|
||||
QTextStream textStream(&file);
|
||||
|
||||
textStream.setCodec("UTF-8");
|
||||
textStream << "Title: " << title() << endl;
|
||||
textStream << "Url: " << url().toString() << endl;
|
||||
textStream << "[Adblock Plus 1.1.1]" << endl;
|
||||
|
||||
foreach (const AdBlockRule* rule, m_rules) {
|
||||
foreach (const AdBlockRule *rule, m_rules) {
|
||||
textStream << rule->filter() << endl;
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::canEditRules() const
|
||||
{
|
||||
bool AdBlockCustomList::canEditRules() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::canBeRemoved() const
|
||||
{
|
||||
bool AdBlockCustomList::canBeRemoved() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::containsFilter(const QString &filter) const
|
||||
{
|
||||
foreach (const AdBlockRule* rule, m_rules) {
|
||||
bool AdBlockCustomList::containsFilter(const QString &filter) const {
|
||||
foreach (const AdBlockRule *rule, m_rules) {
|
||||
if (rule->filter() == filter) {
|
||||
return true;
|
||||
}
|
||||
@ -350,10 +345,9 @@ bool AdBlockCustomList::containsFilter(const QString &filter) const
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::removeFilter(const QString &filter)
|
||||
{
|
||||
bool AdBlockCustomList::removeFilter(const QString &filter) {
|
||||
for (int i = 0; i < m_rules.count(); ++i) {
|
||||
const AdBlockRule* rule = m_rules.at(i);
|
||||
const AdBlockRule *rule = m_rules.at(i);
|
||||
|
||||
if (rule->filter() == filter) {
|
||||
return removeRule(i);
|
||||
@ -363,33 +357,35 @@ bool AdBlockCustomList::removeFilter(const QString &filter)
|
||||
return false;
|
||||
}
|
||||
|
||||
int AdBlockCustomList::addRule(AdBlockRule* rule)
|
||||
{
|
||||
int AdBlockCustomList::addRule(AdBlockRule* rule) {
|
||||
m_rules.append(rule);
|
||||
|
||||
emit subscriptionChanged();
|
||||
|
||||
if (rule->isCssRule())
|
||||
mApp->reloadUserStyleSheet();
|
||||
if (rule->isCssRule()) {
|
||||
// TODO: opravdu
|
||||
//mApp->reloadUserStyleSheet();
|
||||
}
|
||||
|
||||
return m_rules.count() - 1;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::removeRule(int offset)
|
||||
{
|
||||
if (!QzTools::containsIndex(m_rules, offset)) {
|
||||
bool AdBlockCustomList::removeRule(int offset) {
|
||||
if (!IS_IN_ARRAY(offset, m_rules)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AdBlockRule* rule = m_rules.at(offset);
|
||||
AdBlockRule *rule = m_rules.at(offset);
|
||||
const QString filter = rule->filter();
|
||||
|
||||
m_rules.remove(offset);
|
||||
|
||||
emit subscriptionChanged();
|
||||
|
||||
if (rule->isCssRule())
|
||||
mApp->reloadUserStyleSheet();
|
||||
if (rule->isCssRule()) {
|
||||
// TODO: opravdu
|
||||
//mApp->reloadUserStyleSheet();
|
||||
}
|
||||
|
||||
AdBlockManager::instance()->removeDisabledRule(filter);
|
||||
|
||||
@ -397,19 +393,20 @@ bool AdBlockCustomList::removeRule(int offset)
|
||||
return true;
|
||||
}
|
||||
|
||||
const AdBlockRule* AdBlockCustomList::replaceRule(AdBlockRule* rule, int offset)
|
||||
{
|
||||
if (!QzTools::containsIndex(m_rules, offset)) {
|
||||
const AdBlockRule *AdBlockCustomList::replaceRule(AdBlockRule *rule, int offset) {
|
||||
if (!IS_IN_ARRAY(offset, m_rules)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
AdBlockRule* oldRule = m_rules.at(offset);
|
||||
AdBlockRule *oldRule = m_rules.at(offset);
|
||||
m_rules[offset] = rule;
|
||||
|
||||
emit subscriptionChanged();
|
||||
|
||||
if (rule->isCssRule() || oldRule->isCssRule())
|
||||
mApp->reloadUserStyleSheet();
|
||||
if (rule->isCssRule() || oldRule->isCssRule()) {
|
||||
// TODO: opravdu
|
||||
//mApp->reloadUserStyleSheet();
|
||||
}
|
||||
|
||||
delete oldRule;
|
||||
return m_rules[offset];
|
||||
|
@ -86,7 +86,7 @@ class AdBlockSubscription : public QObject {
|
||||
|
||||
virtual int addRule(AdBlockRule* rule);
|
||||
virtual bool removeRule(int offset);
|
||||
virtual const AdBlockRule* replaceRule(AdBlockRule* rule, int offset);
|
||||
virtual const AdBlockRule *replaceRule(AdBlockRule *rule, int offset);
|
||||
|
||||
public slots:
|
||||
void updateSubscription();
|
||||
|
@ -16,8 +16,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "adblocktreewidget.h"
|
||||
#include "adblocksubscription.h"
|
||||
#include "network-web/adblock/adblocktreewidget.h"
|
||||
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QKeyEvent>
|
||||
@ -25,243 +26,233 @@
|
||||
#include <QApplication>
|
||||
#include <QInputDialog>
|
||||
|
||||
AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent)
|
||||
: TreeWidget(parent)
|
||||
, m_subscription(subscription)
|
||||
, m_topItem(0)
|
||||
, m_itemChangingBlock(false)
|
||||
{
|
||||
setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
setDefaultItemShowMode(TreeWidget::ItemsExpanded);
|
||||
setHeaderHidden(true);
|
||||
setAlternatingRowColors(true);
|
||||
setLayoutDirection(Qt::LeftToRight);
|
||||
|
||||
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequested(QPoint)));
|
||||
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(itemChanged(QTreeWidgetItem*)));
|
||||
connect(m_subscription, SIGNAL(subscriptionUpdated()), this, SLOT(subscriptionUpdated()));
|
||||
connect(m_subscription, SIGNAL(subscriptionError(QString)), this, SLOT(subscriptionError(QString)));
|
||||
AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent)
|
||||
: TreeWidget(parent), m_subscription(subscription), m_topItem(0), m_itemChangingBlock(false) {
|
||||
setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
setDefaultItemShowMode(TreeWidget::ItemsExpanded);
|
||||
setHeaderHidden(true);
|
||||
setAlternatingRowColors(true);
|
||||
setLayoutDirection(Qt::LeftToRight);
|
||||
|
||||
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequested(QPoint)));
|
||||
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(itemChanged(QTreeWidgetItem*)));
|
||||
connect(m_subscription, SIGNAL(subscriptionUpdated()), this, SLOT(subscriptionUpdated()));
|
||||
connect(m_subscription, SIGNAL(subscriptionError(QString)), this, SLOT(subscriptionError(QString)));
|
||||
}
|
||||
|
||||
AdBlockSubscription* AdBlockTreeWidget::subscription() const
|
||||
{
|
||||
return m_subscription;
|
||||
AdBlockSubscription *AdBlockTreeWidget::subscription() const {
|
||||
return m_subscription;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::showRule(const AdBlockRule* rule)
|
||||
{
|
||||
if (!m_topItem && rule) {
|
||||
m_ruleToBeSelected = rule->filter();
|
||||
}
|
||||
else if (!m_ruleToBeSelected.isEmpty()) {
|
||||
QList<QTreeWidgetItem*> items = findItems(m_ruleToBeSelected, Qt::MatchRecursive);
|
||||
if (!items.isEmpty()) {
|
||||
QTreeWidgetItem* item = items.at(0);
|
||||
void AdBlockTreeWidget::showRule(const AdBlockRule *rule) {
|
||||
if (!m_topItem && rule) {
|
||||
m_ruleToBeSelected = rule->filter();
|
||||
}
|
||||
else if (!m_ruleToBeSelected.isEmpty()) {
|
||||
QList<QTreeWidgetItem*> items = findItems(m_ruleToBeSelected, Qt::MatchRecursive);
|
||||
|
||||
setCurrentItem(item);
|
||||
scrollToItem(item, QAbstractItemView::PositionAtCenter);
|
||||
}
|
||||
if (!items.isEmpty()) {
|
||||
QTreeWidgetItem *item = items.at(0);
|
||||
|
||||
m_ruleToBeSelected.clear();
|
||||
setCurrentItem(item);
|
||||
scrollToItem(item, QAbstractItemView::PositionAtCenter);
|
||||
}
|
||||
|
||||
m_ruleToBeSelected.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos)
|
||||
{
|
||||
if (!m_subscription->canEditRules()) {
|
||||
return;
|
||||
}
|
||||
void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos) {
|
||||
if (!m_subscription->canEditRules()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTreeWidgetItem* item = itemAt(pos);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
QTreeWidgetItem *item = itemAt(pos);
|
||||
|
||||
QMenu menu;
|
||||
menu.addAction(tr("Add Rule"), this, SLOT(addRule()));
|
||||
menu.addSeparator();
|
||||
QAction* deleteAction = menu.addAction(tr("Remove Rule"), this, SLOT(removeRule()));
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!item->parent()) {
|
||||
deleteAction->setDisabled(true);
|
||||
}
|
||||
QMenu menu;
|
||||
menu.addAction(tr("Add rule"), this, SLOT(addRule()));
|
||||
menu.addSeparator();
|
||||
QAction* deleteAction = menu.addAction(tr("Remove rule"), this, SLOT(removeRule()));
|
||||
|
||||
menu.exec(viewport()->mapToGlobal(pos));
|
||||
if (!item->parent()) {
|
||||
deleteAction->setDisabled(true);
|
||||
}
|
||||
|
||||
menu.exec(viewport()->mapToGlobal(pos));
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item)
|
||||
{
|
||||
if (!item || m_itemChangingBlock) {
|
||||
return;
|
||||
}
|
||||
void AdBlockTreeWidget::itemChanged(QTreeWidgetItem *item) {
|
||||
if (!item || m_itemChangingBlock) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_itemChangingBlock = true;
|
||||
|
||||
int offset = item->data(0, Qt::UserRole + 10).toInt();
|
||||
const AdBlockRule* oldRule = m_subscription->rule(offset);
|
||||
int offset = item->data(0, Qt::UserRole + 10).toInt();
|
||||
const AdBlockRule *oldRule = m_subscription->rule(offset);
|
||||
|
||||
if (item->checkState(0) == Qt::Unchecked && oldRule->isEnabled()) {
|
||||
// Disable rule
|
||||
const AdBlockRule* rule = m_subscription->disableRule(offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
else if (item->checkState(0) == Qt::Checked && !oldRule->isEnabled()) {
|
||||
// Enable rule
|
||||
const AdBlockRule* rule = m_subscription->enableRule(offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
else if (m_subscription->canEditRules()) {
|
||||
// Custom rule has been changed
|
||||
AdBlockRule* newRule = new AdBlockRule(item->text(0), m_subscription);
|
||||
const AdBlockRule* rule = m_subscription->replaceRule(newRule, offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::copyFilter()
|
||||
{
|
||||
QTreeWidgetItem* item = currentItem();
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
QApplication::clipboard()->setText(item->text(0));
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::addRule()
|
||||
{
|
||||
if (!m_subscription->canEditRules()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString newRule = QInputDialog::getText(this, tr("Add Custom Rule"), tr("Please write your rule here:"));
|
||||
if (newRule.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AdBlockRule* rule = new AdBlockRule(newRule, m_subscription);
|
||||
int offset = m_subscription->addRule(rule);
|
||||
|
||||
QTreeWidgetItem* item = new QTreeWidgetItem();
|
||||
item->setText(0, newRule);
|
||||
item->setData(0, Qt::UserRole + 10, offset);
|
||||
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->addChild(item);
|
||||
m_itemChangingBlock = false;
|
||||
if (item->checkState(0) == Qt::Unchecked && oldRule->isEnabled()) {
|
||||
// Disable rule.
|
||||
const AdBlockRule *rule = m_subscription->disableRule(offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
else if (item->checkState(0) == Qt::Checked && !oldRule->isEnabled()) {
|
||||
// Enable rule.
|
||||
const AdBlockRule *rule = m_subscription->enableRule(offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
else if (m_subscription->canEditRules()) {
|
||||
// Custom rule has been changed.
|
||||
AdBlockRule *newRule = new AdBlockRule(item->text(0), m_subscription);
|
||||
const AdBlockRule *rule = m_subscription->replaceRule(newRule, offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::removeRule()
|
||||
{
|
||||
QTreeWidgetItem* item = currentItem();
|
||||
if (!item || !m_subscription->canEditRules() || item == m_topItem) {
|
||||
return;
|
||||
}
|
||||
void AdBlockTreeWidget::copyFilter() {
|
||||
QTreeWidgetItem *item = currentItem();
|
||||
|
||||
int offset = item->data(0, Qt::UserRole + 10).toInt();
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_subscription->removeRule(offset);
|
||||
deleteItem(item);
|
||||
QApplication::clipboard()->setText(item->text(0));
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::subscriptionUpdated()
|
||||
{
|
||||
refresh();
|
||||
void AdBlockTreeWidget::addRule() {
|
||||
if (!m_subscription->canEditRules()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->setText(0, tr("%1 (recently updated)").arg(m_subscription->title()));
|
||||
m_itemChangingBlock = false;
|
||||
QString newRule = QInputDialog::getText(this, tr("Add custom rule"), tr("Please write your rule here:"));
|
||||
|
||||
if (newRule.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AdBlockRule *rule = new AdBlockRule(newRule, m_subscription);
|
||||
int offset = m_subscription->addRule(rule);
|
||||
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem();
|
||||
item->setText(0, newRule);
|
||||
item->setData(0, Qt::UserRole + 10, offset);
|
||||
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->addChild(item);
|
||||
m_itemChangingBlock = false;
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::subscriptionError(const QString &message)
|
||||
{
|
||||
refresh();
|
||||
void AdBlockTreeWidget::removeRule() {
|
||||
QTreeWidgetItem *item = currentItem();
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->setText(0, tr("%1 (Error: %2)").arg(m_subscription->title(), message));
|
||||
m_itemChangingBlock = false;
|
||||
if (!item || !m_subscription->canEditRules() || item == m_topItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
int offset = item->data(0, Qt::UserRole + 10).toInt();
|
||||
|
||||
m_subscription->removeRule(offset);
|
||||
deleteItem(item);
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule)
|
||||
{
|
||||
if (!rule->isEnabled()) {
|
||||
QFont font;
|
||||
font.setItalic(true);
|
||||
item->setForeground(0, QColor(Qt::gray));
|
||||
void AdBlockTreeWidget::subscriptionUpdated() {
|
||||
refresh();
|
||||
|
||||
if (!rule->isComment()) {
|
||||
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
||||
item->setCheckState(0, Qt::Unchecked);
|
||||
item->setFont(0, font);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->setText(0, tr("%1 (recently updated)").arg(m_subscription->title()));
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event)
|
||||
{
|
||||
if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
|
||||
copyFilter();
|
||||
}
|
||||
void AdBlockTreeWidget::subscriptionError(const QString &message) {
|
||||
refresh();
|
||||
|
||||
if (event->key() == Qt::Key_Delete) {
|
||||
removeRule();
|
||||
}
|
||||
|
||||
TreeWidget::keyPressEvent(event);
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->setText(0, tr("%1 (Error: %2)").arg(m_subscription->title(), message));
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::refresh()
|
||||
{
|
||||
m_itemChangingBlock = true;
|
||||
clear();
|
||||
void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule) {
|
||||
if (!rule->isEnabled()) {
|
||||
QFont font;
|
||||
font.setItalic(true);
|
||||
item->setForeground(0, QColor(Qt::gray));
|
||||
|
||||
QFont boldFont;
|
||||
boldFont.setBold(true);
|
||||
|
||||
m_topItem = new QTreeWidgetItem(this);
|
||||
m_topItem->setText(0, m_subscription->title());
|
||||
m_topItem->setFont(0, boldFont);
|
||||
m_topItem->setExpanded(true);
|
||||
addTopLevelItem(m_topItem);
|
||||
|
||||
const QVector<AdBlockRule*> &allRules = m_subscription->allRules();
|
||||
|
||||
int index = 0;
|
||||
foreach (const AdBlockRule* rule, allRules) {
|
||||
QTreeWidgetItem* item = new QTreeWidgetItem(m_topItem);
|
||||
item->setText(0, rule->filter());
|
||||
item->setData(0, Qt::UserRole + 10, index);
|
||||
|
||||
if (m_subscription->canEditRules()) {
|
||||
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
}
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
++index;
|
||||
if (!rule->isComment()) {
|
||||
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
||||
item->setCheckState(0, Qt::Unchecked);
|
||||
item->setFont(0, font);
|
||||
}
|
||||
|
||||
showRule(0);
|
||||
m_itemChangingBlock = false;
|
||||
return;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) {
|
||||
if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
|
||||
copyFilter();
|
||||
}
|
||||
|
||||
if (event->key() == Qt::Key_Delete) {
|
||||
removeRule();
|
||||
}
|
||||
|
||||
TreeWidget::keyPressEvent(event);
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::refresh() {
|
||||
m_itemChangingBlock = true;
|
||||
clear();
|
||||
|
||||
QFont boldFont;
|
||||
boldFont.setBold(true);
|
||||
|
||||
m_topItem = new QTreeWidgetItem(this);
|
||||
m_topItem->setText(0, m_subscription->title());
|
||||
m_topItem->setFont(0, boldFont);
|
||||
m_topItem->setExpanded(true);
|
||||
addTopLevelItem(m_topItem);
|
||||
|
||||
const QVector<AdBlockRule*> &allRules = m_subscription->allRules();
|
||||
|
||||
int index = 0;
|
||||
foreach (const AdBlockRule *rule, allRules) {
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem(m_topItem);
|
||||
item->setText(0, rule->filter());
|
||||
item->setData(0, Qt::UserRole + 10, index);
|
||||
|
||||
if (m_subscription->canEditRules()) {
|
||||
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
}
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
++index;
|
||||
}
|
||||
|
||||
showRule(0);
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
@ -19,41 +19,41 @@
|
||||
#ifndef ADBLOCKTREEWIDGET_H
|
||||
#define ADBLOCKTREEWIDGET_H
|
||||
|
||||
#include <QTreeWidget>
|
||||
#include "gui/treewidget.h"
|
||||
|
||||
|
||||
class AdBlockSubscription;
|
||||
class AdBlockRule;
|
||||
|
||||
class AdBlockTreeWidget : public QTreeWidget
|
||||
{
|
||||
class AdBlockTreeWidget : public TreeWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent = 0);
|
||||
|
||||
AdBlockSubscription* subscription() const;
|
||||
public:
|
||||
explicit AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent = 0);
|
||||
|
||||
void showRule(const AdBlockRule* rule);
|
||||
AdBlockSubscription *subscription() const;
|
||||
|
||||
void showRule(const AdBlockRule *rule);
|
||||
void refresh();
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
void addRule();
|
||||
void removeRule();
|
||||
|
||||
private slots:
|
||||
private slots:
|
||||
void contextMenuRequested(const QPoint &pos);
|
||||
void itemChanged(QTreeWidgetItem* item);
|
||||
void itemChanged(QTreeWidgetItem *item);
|
||||
void copyFilter();
|
||||
|
||||
void subscriptionUpdated();
|
||||
void subscriptionError(const QString &message);
|
||||
|
||||
private:
|
||||
void adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule);
|
||||
private:
|
||||
void adjustItemFeatures(QTreeWidgetItem *item, const AdBlockRule *rule);
|
||||
void keyPressEvent(QKeyEvent* event);
|
||||
|
||||
AdBlockSubscription* m_subscription;
|
||||
QTreeWidgetItem* m_topItem;
|
||||
AdBlockSubscription *m_subscription;
|
||||
QTreeWidgetItem *m_topItem;
|
||||
|
||||
QString m_ruleToBeSelected;
|
||||
bool m_itemChangingBlock;
|
||||
|
@ -16,17 +16,17 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "adblockurlinterceptor.h"
|
||||
#include "adblockmanager.h"
|
||||
#include "network-web/adblock/adblockurlinterceptor.h"
|
||||
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
|
||||
|
||||
AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager)
|
||||
: UrlInterceptor(manager)
|
||||
, m_manager(manager)
|
||||
{
|
||||
: UrlInterceptor(manager), m_manager(manager) {
|
||||
}
|
||||
|
||||
void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
|
||||
{
|
||||
if (m_manager->block(info))
|
||||
info.block(true);
|
||||
void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
|
||||
if (m_manager->block(info)) {
|
||||
info.block(true);
|
||||
}
|
||||
}
|
||||
|
@ -19,19 +19,20 @@
|
||||
#ifndef ADBLOCKURLINTERCEPTOR_H
|
||||
#define ADBLOCKURLINTERCEPTOR_H
|
||||
|
||||
#include "urlinterceptor.h"
|
||||
#include "qzcommon.h"
|
||||
#include "network-web/urlinterceptor.h"
|
||||
|
||||
|
||||
class AdBlockManager;
|
||||
|
||||
class QUPZILLA_EXPORT AdBlockUrlInterceptor : public UrlInterceptor
|
||||
{
|
||||
public:
|
||||
explicit AdBlockUrlInterceptor(AdBlockManager* manager);
|
||||
class AdBlockUrlInterceptor : public UrlInterceptor {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockUrlInterceptor(AdBlockManager *manager);
|
||||
|
||||
void interceptRequest(QWebEngineUrlRequestInfo &info);
|
||||
|
||||
private:
|
||||
private:
|
||||
AdBlockManager *m_manager;
|
||||
};
|
||||
|
||||
|
@ -16,45 +16,41 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "networkurlinterceptor.h"
|
||||
#include "urlinterceptor.h"
|
||||
#include "settings.h"
|
||||
#include "mainapplication.h"
|
||||
#include "useragentmanager.h"
|
||||
#include "network-web/networkurlinterceptor.h"
|
||||
|
||||
#include "network-web/urlinterceptor.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/settings.h"
|
||||
|
||||
|
||||
NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent)
|
||||
: QWebEngineUrlRequestInterceptor(parent)
|
||||
, m_sendDNT(false)
|
||||
{
|
||||
: QWebEngineUrlRequestInterceptor(parent), m_sendDNT(false) {
|
||||
}
|
||||
|
||||
void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
|
||||
{
|
||||
if (m_sendDNT)
|
||||
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
|
||||
void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
|
||||
if (m_sendDNT) {
|
||||
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
|
||||
}
|
||||
|
||||
info.setHttpHeader(QByteArrayLiteral("User-Agent"), mApp->userAgentManager()->userAgentForUrl(info.firstPartyUrl()).toUtf8());
|
||||
// TODO: mužeme zde nastavovat custom věci pro každej webengine sitovej pozadavek
|
||||
// treba user agenta
|
||||
//info.setHttpHeader(QByteArrayLiteral("User-Agent"), mApp->userAgentManager()->userAgentForUrl(info.firstPartyUrl()).toUtf8());
|
||||
|
||||
foreach (UrlInterceptor *interceptor, m_interceptors) {
|
||||
interceptor->interceptRequest(info);
|
||||
}
|
||||
foreach (UrlInterceptor *interceptor, m_interceptors) {
|
||||
interceptor->interceptRequest(info);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkUrlInterceptor::installUrlInterceptor(UrlInterceptor *interceptor)
|
||||
{
|
||||
if (!m_interceptors.contains(interceptor))
|
||||
m_interceptors.append(interceptor);
|
||||
void NetworkUrlInterceptor::installUrlInterceptor(UrlInterceptor *interceptor) {
|
||||
if (!m_interceptors.contains(interceptor)) {
|
||||
m_interceptors.append(interceptor);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor)
|
||||
{
|
||||
m_interceptors.removeOne(interceptor);
|
||||
void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor) {
|
||||
m_interceptors.removeOne(interceptor);
|
||||
}
|
||||
|
||||
void NetworkUrlInterceptor::loadSettings()
|
||||
{
|
||||
Settings settings;
|
||||
settings.beginGroup("Web-Browser-Settings");
|
||||
m_sendDNT = settings.value("DoNotTrack", false).toBool();
|
||||
settings.endGroup();
|
||||
void NetworkUrlInterceptor::loadSettings() {
|
||||
m_sendDNT = qApp->settings()->value(GROUP(Browser), SETTING(Browser::SendDNT)).toBool();
|
||||
}
|
||||
|
@ -21,14 +21,14 @@
|
||||
|
||||
#include <QWebEngineUrlRequestInterceptor>
|
||||
|
||||
#include "qzcommon.h"
|
||||
|
||||
class UrlInterceptor;
|
||||
|
||||
class QUPZILLA_EXPORT NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor
|
||||
{
|
||||
public:
|
||||
explicit NetworkUrlInterceptor(QObject* parent = Q_NULLPTR);
|
||||
class NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit NetworkUrlInterceptor(QObject *parent = nullptr);
|
||||
|
||||
void interceptRequest(QWebEngineUrlRequestInfo &info) Q_DECL_OVERRIDE;
|
||||
|
||||
@ -37,7 +37,7 @@ public:
|
||||
|
||||
void loadSettings();
|
||||
|
||||
private:
|
||||
private:
|
||||
QList<UrlInterceptor*> m_interceptors;
|
||||
bool m_sendDNT;
|
||||
};
|
||||
|
@ -23,9 +23,11 @@
|
||||
#include <QObject>
|
||||
#include <QWebEngineUrlRequestInfo>
|
||||
|
||||
class UrlInterceptor : public QObject
|
||||
{
|
||||
public:
|
||||
|
||||
class UrlInterceptor : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { }
|
||||
virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user