Fix compilation, add interceptor to Application class.

This commit is contained in:
Martin Rotter 2017-07-20 08:05:21 +02:00
parent 9fdacb26f9
commit 5cedad03ac
24 changed files with 623 additions and 347 deletions

@ -1 +1 @@
Subproject commit aea11879635d0d6b029a7f6d49d1f31dd95b3c05 Subproject commit 1bb36a305e8a23de1e4f3609d8fc4dfeb85ba6fe

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -518,7 +518,8 @@ equals(USE_WEBENGINE, true) {
src/network-web/urlinterceptor.h \ src/network-web/urlinterceptor.h \
src/network-web/networkurlinterceptor.h \ src/network-web/networkurlinterceptor.h \
src/gui/clickablelabel.h \ src/gui/clickablelabel.h \
src/miscellaneous/simpleregexp.h src/miscellaneous/simpleregexp.h \
src/gui/treewidget.h
SOURCES += src/network-web/adblock/adblockaddsubscriptiondialog.cpp \ SOURCES += src/network-web/adblock/adblockaddsubscriptiondialog.cpp \
src/network-web/adblock/adblockdialog.cpp \ src/network-web/adblock/adblockdialog.cpp \
@ -532,7 +533,8 @@ equals(USE_WEBENGINE, true) {
src/network-web/adblock/adblockurlinterceptor.cpp \ src/network-web/adblock/adblockurlinterceptor.cpp \
src/network-web/networkurlinterceptor.cpp \ src/network-web/networkurlinterceptor.cpp \
src/gui/clickablelabel.cpp \ src/gui/clickablelabel.cpp \
src/miscellaneous/simpleregexp.cpp src/miscellaneous/simpleregexp.cpp \
src/gui/treewidget.cpp
FORMS += src/network-web/adblock/adblockaddsubscriptiondialog.ui \ FORMS += src/network-web/adblock/adblockaddsubscriptiondialog.ui \
src/network-web/adblock/adblockdialog.ui src/network-web/adblock/adblockdialog.ui

View File

@ -26,6 +26,7 @@
#define ARGUMENTS_LIST_SEPARATOR "\n" #define ARGUMENTS_LIST_SEPARATOR "\n"
#define IS_IN_ARRAY(offset, array) ((offset >= 0) && (offset < array.count()))
#define ADBLOCK_CUSTOMLIST_NAME "customlist.txt" #define ADBLOCK_CUSTOMLIST_NAME "customlist.txt"
#define ADBLOCK_LISTS_SUBDIRECTORY "adblock" #define ADBLOCK_LISTS_SUBDIRECTORY "adblock"
#define ADBLOCK_EASYLIST_URL "https://easylist-downloads.adblockplus.org/easylist.txt" #define ADBLOCK_EASYLIST_URL "https://easylist-downloads.adblockplus.org/easylist.txt"

197
src/gui/treewidget.cpp Executable file
View 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
View 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

View File

@ -27,6 +27,8 @@
#include "gui/statusbar.h" #include "gui/statusbar.h"
#include "gui/dialogs/formmain.h" #include "gui/dialogs/formmain.h"
#include "exceptions/applicationexception.h" #include "exceptions/applicationexception.h"
#include "network-web/urlinterceptor.h"
#include "network-web/networkurlinterceptor.h"
#include "services/abstract/serviceroot.h" #include "services/abstract/serviceroot.h"
#include "services/standard/standardserviceroot.h" #include "services/standard/standardserviceroot.h"
@ -44,6 +46,7 @@
Application::Application(const QString &id, int &argc, char **argv) Application::Application(const QString &id, int &argc, char **argv)
: QtSingleApplication(id, argc, argv), : QtSingleApplication(id, argc, argv),
m_urlInterceptor(new NetworkUrlInterceptor(this)),
m_feedReader(nullptr), m_feedReader(nullptr),
m_updateFeedsLock(nullptr), m_userActions(QList<QAction*>()), m_mainForm(nullptr), m_updateFeedsLock(nullptr), m_userActions(QList<QAction*>()), m_mainForm(nullptr),
m_trayIcon(nullptr), m_settings(nullptr), m_system(nullptr), m_skins(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) #if defined(USE_WEBENGINE)
connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &Application::downloadRequested); connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &Application::downloadRequested);
QWebEngineProfile::setRequestInterceptor(m_urlInterceptor);
#endif #endif
} }

View File

@ -47,6 +47,7 @@ class QAction;
class Mutex; class Mutex;
class QWebEngineDownloadItem; class QWebEngineDownloadItem;
class FeedReader; class FeedReader;
class NetworkUrlInterceptor;
class Application : public QtSingleApplication { class Application : public QtSingleApplication {
Q_OBJECT Q_OBJECT
@ -156,6 +157,7 @@ class Application : public QtSingleApplication {
// action will be allowed to lock for reading. // action will be allowed to lock for reading.
QScopedPointer<Mutex> m_updateFeedsLock; QScopedPointer<Mutex> m_updateFeedsLock;
NetworkUrlInterceptor *m_urlInterceptor;
QList<QAction*> m_userActions; QList<QAction*> m_userActions;
FormMain *m_mainForm; FormMain *m_mainForm;
SystemTrayIcon *m_trayIcon; SystemTrayIcon *m_trayIcon;

View File

@ -258,6 +258,9 @@ DKEY Keyboard::ID = "keyboard";
// Web browser. // Web browser.
DKEY Browser::ID = "browser"; DKEY Browser::ID = "browser";
KEY Browser::SendDNT = "send_dnt";
VALUE(bool) Browser::SendDNTDef = false;
DKEY Browser::OpenLinksInExternalBrowserRightAway = "open_link_externally_wo_confirmation"; DKEY Browser::OpenLinksInExternalBrowserRightAway = "open_link_externally_wo_confirmation";
DVALUE(bool) Browser::OpenLinksInExternalBrowserRightAwayDef = false; DVALUE(bool) Browser::OpenLinksInExternalBrowserRightAwayDef = false;

View File

@ -291,6 +291,9 @@ namespace Keyboard {
namespace Browser { namespace Browser {
KEY ID; KEY ID;
KEY SendDNT;
VALUE(bool) SendDNTDef;
KEY OpenLinksInExternalBrowserRightAway; KEY OpenLinksInExternalBrowserRightAway;
VALUE(bool) OpenLinksInExternalBrowserRightAwayDef; VALUE(bool) OpenLinksInExternalBrowserRightAwayDef;

View File

@ -60,7 +60,7 @@ void AdBlockManager::setEnabled(bool enabled) {
m_enabled = enabled; m_enabled = enabled;
emit enabledChanged(enabled); emit enabledChanged(enabled);
qApp->settings()->setValue(GROUP(Adblock), AdBlock::AdBlockEnabled, m_enabled); qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, m_enabled);
load(); load();
// TODO: Reload user stylesheet. // TODO: Reload user stylesheet.
//mApp->reloadUserStyleSheet(); //mApp->reloadUserStyleSheet();
@ -102,7 +102,8 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) {
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
// We are blocking main URL frame, we can display "AdBlock error page" or redirect to somewhere. // 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"; // TODO request.redirect() přesměrovat na "chybovou stranku";
//QUrl url(QSL("rssguard:adblock")); //QUrl url(QSL("rssguard:adblock"));
@ -190,7 +191,9 @@ AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const
subscription->loadSubscription(m_disabledRules); subscription->loadSubscription(m_disabledRules);
m_subscriptions.insert(m_subscriptions.count() - 1, subscription); 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())); connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
return subscription; return subscription;
@ -297,7 +300,8 @@ void AdBlockManager::load() {
foreach (AdBlockSubscription *subscription, m_subscriptions) { foreach (AdBlockSubscription *subscription, m_subscriptions) {
subscription->loadSubscription(m_disabledRules); 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())); connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher()));
} }

View File

@ -178,7 +178,7 @@ void AdBlockMatcher::update() {
foreach (const AdBlockRule *rule, exceptionCssRules) { foreach (const AdBlockRule *rule, exceptionCssRules) {
const AdBlockRule *originalRule = cssRulesHash.value(rule->cssSelector()); 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) { if (!originalRule) {
continue; continue;
} }
@ -192,11 +192,11 @@ void AdBlockMatcher::update() {
} }
// Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes. // 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) // (In my testings, 4931 is the number that makes it crash).
// So let's split it by 1000 selectors... // So let's split it by 1000 selectors.
int hidingRulesCount = 0; int hidingRulesCount = 0;
QHashIterator<QString, const AdBlockRule*> it(cssRulesHash); QHashIterator<QString, const AdBlockRule*> it(cssRulesHash);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
const AdBlockRule *rule = it.value(); const AdBlockRule *rule = it.value();

View File

@ -20,10 +20,12 @@
#define ADBLOCKMATCHER_H #define ADBLOCKMATCHER_H
#include <QUrl> #include <QUrl>
#include <QObject>
#include "network-web/adblock/adblocksearchtree.h" #include "network-web/adblock/adblocksearchtree.h"
#include <QObject>
#include <QVector>
class QWebEngineUrlRequestInfo; class QWebEngineUrlRequestInfo;
class AdBlockManager; class AdBlockManager;

View File

@ -454,7 +454,7 @@ void AdBlockRule::parseFilter() {
parsedLine = parsedLine.left(optionsIndex); parsedLine = parsedLine.left(optionsIndex);
} }
.
// Rule is classic regexp. // Rule is classic regexp.
if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/'))) { if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/'))) {
parsedLine = parsedLine.mid(1); parsedLine = parsedLine.mid(1);
@ -659,7 +659,7 @@ bool AdBlockRule::stringMatch(const QString &domain, const QString &encodedUrl)
return false; return false;
} }
bool AdBlockRule::matchDomain(const QString &pattern, const QString &domain) { bool AdBlockRule::matchDomain(const QString &pattern, const QString &domain) const {
if (pattern == domain) { if (pattern == domain) {
return true; return true;
} }

View File

@ -50,11 +50,14 @@
#include "network-web/adblock/adblocksearchtree.h" #include "network-web/adblock/adblocksearchtree.h"
#include "definitions/definitions.h" #include "definitions/definitions.h"
#include "network-web/silentnetworkaccessmanager.h" #include "network-web/silentnetworkaccessmanager.h"
#include "miscellaneous/iofactory.h"
#include "miscellaneous/application.h"
#include <QFile> #include <QFile>
#include <QTimer> #include <QTimer>
#include <QNetworkReply> #include <QNetworkReply>
#include <QSaveFile> #include <QSaveFile>
#include <QDir>
AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent) AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent)
@ -97,7 +100,8 @@ void AdBlockSubscription::loadSubscription(const QStringList &disabledRules) {
QTextStream textStream(&file); QTextStream textStream(&file);
textStream.setCodec("UTF-8"); textStream.setCodec("UTF-8");
// Header is on 3rd line
// Header is on 3rd line.
textStream.readLine(1024); textStream.readLine(1024);
textStream.readLine(1024); textStream.readLine(1024);
QString header = 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 { 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]; return m_rules[offset];
} }
else { else {
@ -197,7 +201,7 @@ QVector<AdBlockRule*> AdBlockSubscription::allRules() const {
} }
const AdBlockRule *AdBlockSubscription::enableRule(int offset) { 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]; AdBlockRule *rule = m_rules[offset];
rule->setEnabled(true); rule->setEnabled(true);
AdBlockManager::instance()->removeDisabledRule(rule->filter()); AdBlockManager::instance()->removeDisabledRule(rule->filter());
@ -216,9 +220,8 @@ const AdBlockRule *AdBlockSubscription::enableRule(int offset) {
} }
} }
const AdBlockRule* AdBlockSubscription::disableRule(int offset) const AdBlockRule *AdBlockSubscription::disableRule(int offset) {
{ if (!IS_IN_ARRAY(offset, m_rules)) {
if (!QzTools::containsIndex(m_rules, offset)) {
return 0; return 0;
} }
@ -228,66 +231,60 @@ const AdBlockRule* AdBlockSubscription::disableRule(int offset)
emit subscriptionChanged(); emit subscriptionChanged();
if (rule->isCssRule()) if (rule->isCssRule()) {
mApp->reloadUserStyleSheet(); // TODO: opravdu?
//mApp->reloadUserStyleSheet();
}
return rule; return rule;
} }
bool AdBlockSubscription::canEditRules() const bool AdBlockSubscription::canEditRules() const {
{
return false; return false;
} }
bool AdBlockSubscription::canBeRemoved() const bool AdBlockSubscription::canBeRemoved() const {
{
return true; return true;
} }
int AdBlockSubscription::addRule(AdBlockRule* rule) int AdBlockSubscription::addRule(AdBlockRule* rule) {
{
Q_UNUSED(rule) Q_UNUSED(rule)
return -1; return -1;
} }
bool AdBlockSubscription::removeRule(int offset) bool AdBlockSubscription::removeRule(int offset) {
{
Q_UNUSED(offset) Q_UNUSED(offset)
return false; return false;
} }
const AdBlockRule* AdBlockSubscription::replaceRule(AdBlockRule* rule, int offset) const AdBlockRule *AdBlockSubscription::replaceRule(AdBlockRule *rule, int offset) {
{
Q_UNUSED(rule) Q_UNUSED(rule)
Q_UNUSED(offset) Q_UNUSED(offset)
return 0; return 0;
} }
AdBlockSubscription::~AdBlockSubscription() AdBlockSubscription::~AdBlockSubscription() {
{
qDeleteAll(m_rules); qDeleteAll(m_rules);
} }
// AdBlockCustomList // AdBlockCustomList
AdBlockCustomList::AdBlockCustomList(QObject *parent) AdBlockCustomList::AdBlockCustomList(QObject *parent)
: AdBlockSubscription(tr("Custom Rules"), parent) : AdBlockSubscription(tr("Custom rules"), parent) {
{ setFilePath(AdBlockManager::instance()->storedListsPath() + QDir::separator() + ADBLOCK_CUSTOMLIST_NAME);
setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/customlist.txt"));
} }
void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) {
{
// DuckDuckGo ad whitelist rules // DuckDuckGo ad whitelist rules
// They cannot be removed, but can be disabled. // They cannot be removed, but can be disabled.
// Please consider not disabling them. Thanks! // Please consider not disabling them. Thanks!
const QString ddg1 = QSL("@@||duckduckgo.com^$document"); const QString ddg1 = QSL("@@||duckduckgo.com^$document");
const QString ddg2 = QSL("duckduckgo.com#@#.has-ad"); const QString ddg2 = QSL("duckduckgo.com#@#.has-ad");
const QString rules = QString::fromUtf8(IOFactory::readTextFile(filePath()));
const QString rules = QzTools::readAllFileContents(filePath());
QFile file(filePath()); QFile file(filePath());
if (!file.exists()) { if (!file.exists()) {
saveSubscription(); saveSubscription();
} }
@ -302,21 +299,22 @@ void AdBlockCustomList::loadSubscription(const QStringList &disabledRules)
if (!rules.contains(QL1S("\n") + ddg2)) if (!rules.contains(QL1S("\n") + ddg2))
stream << ddg2 << endl; stream << ddg2 << endl;
} }
file.close(); file.close();
AdBlockSubscription::loadSubscription(disabledRules); AdBlockSubscription::loadSubscription(disabledRules);
} }
void AdBlockCustomList::saveSubscription() void AdBlockCustomList::saveSubscription() {
{
QFile file(filePath()); QFile file(filePath());
if (!file.open(QFile::ReadWrite | QFile::Truncate)) { 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; return;
} }
QTextStream textStream(&file); QTextStream textStream(&file);
textStream.setCodec("UTF-8"); textStream.setCodec("UTF-8");
textStream << "Title: " << title() << endl; textStream << "Title: " << title() << endl;
textStream << "Url: " << url().toString() << endl; textStream << "Url: " << url().toString() << endl;
@ -329,18 +327,15 @@ void AdBlockCustomList::saveSubscription()
file.close(); file.close();
} }
bool AdBlockCustomList::canEditRules() const bool AdBlockCustomList::canEditRules() const {
{
return true; return true;
} }
bool AdBlockCustomList::canBeRemoved() const bool AdBlockCustomList::canBeRemoved() const {
{
return false; return false;
} }
bool AdBlockCustomList::containsFilter(const QString &filter) const bool AdBlockCustomList::containsFilter(const QString &filter) const {
{
foreach (const AdBlockRule *rule, m_rules) { foreach (const AdBlockRule *rule, m_rules) {
if (rule->filter() == filter) { if (rule->filter() == filter) {
return true; return true;
@ -350,8 +345,7 @@ bool AdBlockCustomList::containsFilter(const QString &filter) const
return false; return false;
} }
bool AdBlockCustomList::removeFilter(const QString &filter) bool AdBlockCustomList::removeFilter(const QString &filter) {
{
for (int i = 0; i < m_rules.count(); ++i) { for (int i = 0; i < m_rules.count(); ++i) {
const AdBlockRule *rule = m_rules.at(i); const AdBlockRule *rule = m_rules.at(i);
@ -363,21 +357,21 @@ bool AdBlockCustomList::removeFilter(const QString &filter)
return false; return false;
} }
int AdBlockCustomList::addRule(AdBlockRule* rule) int AdBlockCustomList::addRule(AdBlockRule* rule) {
{
m_rules.append(rule); m_rules.append(rule);
emit subscriptionChanged(); emit subscriptionChanged();
if (rule->isCssRule()) if (rule->isCssRule()) {
mApp->reloadUserStyleSheet(); // TODO: opravdu
//mApp->reloadUserStyleSheet();
}
return m_rules.count() - 1; return m_rules.count() - 1;
} }
bool AdBlockCustomList::removeRule(int offset) bool AdBlockCustomList::removeRule(int offset) {
{ if (!IS_IN_ARRAY(offset, m_rules)) {
if (!QzTools::containsIndex(m_rules, offset)) {
return false; return false;
} }
@ -388,8 +382,10 @@ bool AdBlockCustomList::removeRule(int offset)
emit subscriptionChanged(); emit subscriptionChanged();
if (rule->isCssRule()) if (rule->isCssRule()) {
mApp->reloadUserStyleSheet(); // TODO: opravdu
//mApp->reloadUserStyleSheet();
}
AdBlockManager::instance()->removeDisabledRule(filter); AdBlockManager::instance()->removeDisabledRule(filter);
@ -397,9 +393,8 @@ bool AdBlockCustomList::removeRule(int offset)
return true; return true;
} }
const AdBlockRule* AdBlockCustomList::replaceRule(AdBlockRule* rule, int offset) const AdBlockRule *AdBlockCustomList::replaceRule(AdBlockRule *rule, int offset) {
{ if (!IS_IN_ARRAY(offset, m_rules)) {
if (!QzTools::containsIndex(m_rules, offset)) {
return 0; return 0;
} }
@ -408,8 +403,10 @@ const AdBlockRule* AdBlockCustomList::replaceRule(AdBlockRule* rule, int offset)
emit subscriptionChanged(); emit subscriptionChanged();
if (rule->isCssRule() || oldRule->isCssRule()) if (rule->isCssRule() || oldRule->isCssRule()) {
mApp->reloadUserStyleSheet(); // TODO: opravdu
//mApp->reloadUserStyleSheet();
}
delete oldRule; delete oldRule;
return m_rules[offset]; return m_rules[offset];

View File

@ -16,8 +16,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>. // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "adblocktreewidget.h" #include "network-web/adblock/adblocktreewidget.h"
#include "adblocksubscription.h"
#include "network-web/adblock/adblocksubscription.h"
#include <QMenu> #include <QMenu>
#include <QKeyEvent> #include <QKeyEvent>
@ -25,12 +26,9 @@
#include <QApplication> #include <QApplication>
#include <QInputDialog> #include <QInputDialog>
AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent) AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent)
: TreeWidget(parent) : TreeWidget(parent), m_subscription(subscription), m_topItem(0), m_itemChangingBlock(false) {
, m_subscription(subscription)
, m_topItem(0)
, m_itemChangingBlock(false)
{
setContextMenuPolicy(Qt::CustomContextMenu); setContextMenuPolicy(Qt::CustomContextMenu);
setDefaultItemShowMode(TreeWidget::ItemsExpanded); setDefaultItemShowMode(TreeWidget::ItemsExpanded);
setHeaderHidden(true); setHeaderHidden(true);
@ -43,18 +41,17 @@ AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget*
connect(m_subscription, SIGNAL(subscriptionError(QString)), this, SLOT(subscriptionError(QString))); connect(m_subscription, SIGNAL(subscriptionError(QString)), this, SLOT(subscriptionError(QString)));
} }
AdBlockSubscription* AdBlockTreeWidget::subscription() const AdBlockSubscription *AdBlockTreeWidget::subscription() const {
{
return m_subscription; return m_subscription;
} }
void AdBlockTreeWidget::showRule(const AdBlockRule* rule) void AdBlockTreeWidget::showRule(const AdBlockRule *rule) {
{
if (!m_topItem && rule) { if (!m_topItem && rule) {
m_ruleToBeSelected = rule->filter(); m_ruleToBeSelected = rule->filter();
} }
else if (!m_ruleToBeSelected.isEmpty()) { else if (!m_ruleToBeSelected.isEmpty()) {
QList<QTreeWidgetItem*> items = findItems(m_ruleToBeSelected, Qt::MatchRecursive); QList<QTreeWidgetItem*> items = findItems(m_ruleToBeSelected, Qt::MatchRecursive);
if (!items.isEmpty()) { if (!items.isEmpty()) {
QTreeWidgetItem *item = items.at(0); QTreeWidgetItem *item = items.at(0);
@ -66,21 +63,21 @@ void AdBlockTreeWidget::showRule(const AdBlockRule* rule)
} }
} }
void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos) void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos) {
{
if (!m_subscription->canEditRules()) { if (!m_subscription->canEditRules()) {
return; return;
} }
QTreeWidgetItem *item = itemAt(pos); QTreeWidgetItem *item = itemAt(pos);
if (!item) { if (!item) {
return; return;
} }
QMenu menu; QMenu menu;
menu.addAction(tr("Add Rule"), this, SLOT(addRule())); menu.addAction(tr("Add rule"), this, SLOT(addRule()));
menu.addSeparator(); menu.addSeparator();
QAction* deleteAction = menu.addAction(tr("Remove Rule"), this, SLOT(removeRule())); QAction* deleteAction = menu.addAction(tr("Remove rule"), this, SLOT(removeRule()));
if (!item->parent()) { if (!item->parent()) {
deleteAction->setDisabled(true); deleteAction->setDisabled(true);
@ -89,8 +86,7 @@ void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos)
menu.exec(viewport()->mapToGlobal(pos)); menu.exec(viewport()->mapToGlobal(pos));
} }
void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item) void AdBlockTreeWidget::itemChanged(QTreeWidgetItem *item) {
{
if (!item || m_itemChangingBlock) { if (!item || m_itemChangingBlock) {
return; return;
} }
@ -101,19 +97,19 @@ void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item)
const AdBlockRule *oldRule = m_subscription->rule(offset); const AdBlockRule *oldRule = m_subscription->rule(offset);
if (item->checkState(0) == Qt::Unchecked && oldRule->isEnabled()) { if (item->checkState(0) == Qt::Unchecked && oldRule->isEnabled()) {
// Disable rule // Disable rule.
const AdBlockRule *rule = m_subscription->disableRule(offset); const AdBlockRule *rule = m_subscription->disableRule(offset);
adjustItemFeatures(item, rule); adjustItemFeatures(item, rule);
} }
else if (item->checkState(0) == Qt::Checked && !oldRule->isEnabled()) { else if (item->checkState(0) == Qt::Checked && !oldRule->isEnabled()) {
// Enable rule // Enable rule.
const AdBlockRule *rule = m_subscription->enableRule(offset); const AdBlockRule *rule = m_subscription->enableRule(offset);
adjustItemFeatures(item, rule); adjustItemFeatures(item, rule);
} }
else if (m_subscription->canEditRules()) { else if (m_subscription->canEditRules()) {
// Custom rule has been changed // Custom rule has been changed.
AdBlockRule *newRule = new AdBlockRule(item->text(0), m_subscription); AdBlockRule *newRule = new AdBlockRule(item->text(0), m_subscription);
const AdBlockRule *rule = m_subscription->replaceRule(newRule, offset); const AdBlockRule *rule = m_subscription->replaceRule(newRule, offset);
@ -123,9 +119,9 @@ void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item)
m_itemChangingBlock = false; m_itemChangingBlock = false;
} }
void AdBlockTreeWidget::copyFilter() void AdBlockTreeWidget::copyFilter() {
{
QTreeWidgetItem *item = currentItem(); QTreeWidgetItem *item = currentItem();
if (!item) { if (!item) {
return; return;
} }
@ -133,13 +129,13 @@ void AdBlockTreeWidget::copyFilter()
QApplication::clipboard()->setText(item->text(0)); QApplication::clipboard()->setText(item->text(0));
} }
void AdBlockTreeWidget::addRule() void AdBlockTreeWidget::addRule() {
{
if (!m_subscription->canEditRules()) { if (!m_subscription->canEditRules()) {
return; return;
} }
QString newRule = QInputDialog::getText(this, tr("Add Custom Rule"), tr("Please write your rule here:")); QString newRule = QInputDialog::getText(this, tr("Add custom rule"), tr("Please write your rule here:"));
if (newRule.isEmpty()) { if (newRule.isEmpty()) {
return; return;
} }
@ -159,9 +155,9 @@ void AdBlockTreeWidget::addRule()
adjustItemFeatures(item, rule); adjustItemFeatures(item, rule);
} }
void AdBlockTreeWidget::removeRule() void AdBlockTreeWidget::removeRule() {
{
QTreeWidgetItem *item = currentItem(); QTreeWidgetItem *item = currentItem();
if (!item || !m_subscription->canEditRules() || item == m_topItem) { if (!item || !m_subscription->canEditRules() || item == m_topItem) {
return; return;
} }
@ -172,8 +168,7 @@ void AdBlockTreeWidget::removeRule()
deleteItem(item); deleteItem(item);
} }
void AdBlockTreeWidget::subscriptionUpdated() void AdBlockTreeWidget::subscriptionUpdated() {
{
refresh(); refresh();
m_itemChangingBlock = true; m_itemChangingBlock = true;
@ -181,8 +176,7 @@ void AdBlockTreeWidget::subscriptionUpdated()
m_itemChangingBlock = false; m_itemChangingBlock = false;
} }
void AdBlockTreeWidget::subscriptionError(const QString &message) void AdBlockTreeWidget::subscriptionError(const QString &message) {
{
refresh(); refresh();
m_itemChangingBlock = true; m_itemChangingBlock = true;
@ -190,8 +184,7 @@ void AdBlockTreeWidget::subscriptionError(const QString &message)
m_itemChangingBlock = false; m_itemChangingBlock = false;
} }
void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule) void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockRule* rule) {
{
if (!rule->isEnabled()) { if (!rule->isEnabled()) {
QFont font; QFont font;
font.setItalic(true); font.setItalic(true);
@ -219,8 +212,7 @@ void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockR
} }
} }
void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event) {
{
if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) { if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
copyFilter(); copyFilter();
} }
@ -232,8 +224,7 @@ void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event)
TreeWidget::keyPressEvent(event); TreeWidget::keyPressEvent(event);
} }
void AdBlockTreeWidget::refresh() void AdBlockTreeWidget::refresh() {
{
m_itemChangingBlock = true; m_itemChangingBlock = true;
clear(); clear();

View File

@ -19,15 +19,15 @@
#ifndef ADBLOCKTREEWIDGET_H #ifndef ADBLOCKTREEWIDGET_H
#define ADBLOCKTREEWIDGET_H #define ADBLOCKTREEWIDGET_H
#include <QTreeWidget> #include "gui/treewidget.h"
class AdBlockSubscription; class AdBlockSubscription;
class AdBlockRule; class AdBlockRule;
class AdBlockTreeWidget : public QTreeWidget class AdBlockTreeWidget : public TreeWidget {
{
Q_OBJECT Q_OBJECT
public: public:
explicit AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent = 0); explicit AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent = 0);

View File

@ -16,17 +16,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>. // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "adblockurlinterceptor.h" #include "network-web/adblock/adblockurlinterceptor.h"
#include "adblockmanager.h"
#include "network-web/adblock/adblockmanager.h"
AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager) AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager)
: UrlInterceptor(manager) : UrlInterceptor(manager), m_manager(manager) {
, m_manager(manager)
{
} }
void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
{ if (m_manager->block(info)) {
if (m_manager->block(info))
info.block(true); info.block(true);
} }
}

View File

@ -19,13 +19,14 @@
#ifndef ADBLOCKURLINTERCEPTOR_H #ifndef ADBLOCKURLINTERCEPTOR_H
#define ADBLOCKURLINTERCEPTOR_H #define ADBLOCKURLINTERCEPTOR_H
#include "urlinterceptor.h" #include "network-web/urlinterceptor.h"
#include "qzcommon.h"
class AdBlockManager; class AdBlockManager;
class QUPZILLA_EXPORT AdBlockUrlInterceptor : public UrlInterceptor class AdBlockUrlInterceptor : public UrlInterceptor {
{ Q_OBJECT
public: public:
explicit AdBlockUrlInterceptor(AdBlockManager *manager); explicit AdBlockUrlInterceptor(AdBlockManager *manager);

View File

@ -16,45 +16,41 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>. // along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "networkurlinterceptor.h" #include "network-web/networkurlinterceptor.h"
#include "urlinterceptor.h"
#include "settings.h" #include "network-web/urlinterceptor.h"
#include "mainapplication.h" #include "miscellaneous/application.h"
#include "useragentmanager.h" #include "miscellaneous/settings.h"
NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent) NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent)
: QWebEngineUrlRequestInterceptor(parent) : QWebEngineUrlRequestInterceptor(parent), m_sendDNT(false) {
, m_sendDNT(false)
{
} }
void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
{ if (m_sendDNT) {
if (m_sendDNT)
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1")); 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) { foreach (UrlInterceptor *interceptor, m_interceptors) {
interceptor->interceptRequest(info); interceptor->interceptRequest(info);
} }
} }
void NetworkUrlInterceptor::installUrlInterceptor(UrlInterceptor *interceptor) void NetworkUrlInterceptor::installUrlInterceptor(UrlInterceptor *interceptor) {
{ if (!m_interceptors.contains(interceptor)) {
if (!m_interceptors.contains(interceptor))
m_interceptors.append(interceptor); m_interceptors.append(interceptor);
} }
}
void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor) void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor) {
{
m_interceptors.removeOne(interceptor); m_interceptors.removeOne(interceptor);
} }
void NetworkUrlInterceptor::loadSettings() void NetworkUrlInterceptor::loadSettings() {
{ m_sendDNT = qApp->settings()->value(GROUP(Browser), SETTING(Browser::SendDNT)).toBool();
Settings settings;
settings.beginGroup("Web-Browser-Settings");
m_sendDNT = settings.value("DoNotTrack", false).toBool();
settings.endGroup();
} }

View File

@ -21,14 +21,14 @@
#include <QWebEngineUrlRequestInterceptor> #include <QWebEngineUrlRequestInterceptor>
#include "qzcommon.h"
class UrlInterceptor; class UrlInterceptor;
class QUPZILLA_EXPORT NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor class NetworkUrlInterceptor : public QWebEngineUrlRequestInterceptor {
{ Q_OBJECT
public: public:
explicit NetworkUrlInterceptor(QObject* parent = Q_NULLPTR); explicit NetworkUrlInterceptor(QObject *parent = nullptr);
void interceptRequest(QWebEngineUrlRequestInfo &info) Q_DECL_OVERRIDE; void interceptRequest(QWebEngineUrlRequestInfo &info) Q_DECL_OVERRIDE;

View File

@ -23,8 +23,10 @@
#include <QObject> #include <QObject>
#include <QWebEngineUrlRequestInfo> #include <QWebEngineUrlRequestInfo>
class UrlInterceptor : public QObject
{ class UrlInterceptor : public QObject {
Q_OBJECT
public: public:
explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { } explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { }
virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0; virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0;