Remove adblock completely.
This commit is contained in:
parent
c7b127343e
commit
1759d31a8b
@ -291,7 +291,6 @@ file( GLOB APP_SOURCES_GL
|
||||
"src/services/owncloud/network/*.cpp"
|
||||
"src/services/owncloud/gui/*.cpp"
|
||||
"src/network-web/*.cpp"
|
||||
"src/network-web/adblock/*.cpp"
|
||||
"src/*.cpp")
|
||||
|
||||
set(APP_SOURCES
|
||||
@ -322,8 +321,7 @@ file( GLOB APP_HEADERS
|
||||
"src/services/owncloud/*.h"
|
||||
"src/services/owncloud/network/*.h"
|
||||
"src/services/owncloud/gui/*.h"
|
||||
"src/network-web/*.h"
|
||||
"src/network-web/adblock/*.h")
|
||||
"src/network-web/*.h")
|
||||
|
||||
# APP forms.
|
||||
file( GLOB APP_FORMS
|
||||
|
@ -20,7 +20,6 @@
|
||||
#include "gui/dialogs/formmain.h"
|
||||
#include "gui/tabwidget.h"
|
||||
#include "gui/plaintoolbutton.h"
|
||||
#include "network-web/adblock/adblockicon.h"
|
||||
#include "miscellaneous/iconfactory.h"
|
||||
|
||||
#include <QToolButton>
|
||||
@ -33,13 +32,6 @@ StatusBar::StatusBar(QWidget *parent) : QStatusBar(parent) {
|
||||
setSizeGripEnabled(false);
|
||||
setContentsMargins(2, 2, 2, 2);
|
||||
|
||||
m_adBlockIcon = new AdBlockIcon(this);
|
||||
m_adBlockIcon->activate();
|
||||
m_adBlockIcon->setObjectName(QSL("m_adBlockIcon"));
|
||||
|
||||
m_adBlockIconAction = new QAction(qApp->icons()->fromTheme("web-adblock"), tr("Adblock"), this);
|
||||
m_adBlockIconAction->setObjectName(QSL("m_adBlockIconAction"));
|
||||
|
||||
m_barProgressFeeds = new QProgressBar(this);
|
||||
m_barProgressFeeds->setTextVisible(false);
|
||||
m_barProgressFeeds->setFixedWidth(100);
|
||||
@ -88,7 +80,7 @@ QList<QAction*> StatusBar::availableActions() const {
|
||||
QList<QAction*> actions = qApp->userActions();
|
||||
|
||||
// Now, add placeholder actions for custom stuff.
|
||||
actions << m_adBlockIconAction << m_barProgressDownloadAction << m_barProgressFeedsAction <<
|
||||
actions << m_barProgressDownloadAction << m_barProgressFeedsAction <<
|
||||
m_lblProgressDownloadAction << m_lblProgressFeedsAction;
|
||||
|
||||
return actions;
|
||||
@ -121,13 +113,7 @@ void StatusBar::loadChangeableActions(const QStringList &action_names) {
|
||||
QAction *action_to_add;
|
||||
QWidget *widget_to_add;
|
||||
|
||||
if (matching_action == m_adBlockIconAction) {
|
||||
widget_to_add = m_adBlockIcon;
|
||||
action_to_add = m_adBlockIconAction;
|
||||
|
||||
widget_to_add->setVisible(true);
|
||||
}
|
||||
else if (matching_action == m_barProgressDownloadAction) {
|
||||
if (matching_action == m_barProgressDownloadAction) {
|
||||
widget_to_add = m_barProgressDownload;
|
||||
action_to_add = m_barProgressDownloadAction;
|
||||
|
||||
|
@ -66,9 +66,6 @@ class StatusBar : public QStatusBar, public BaseBar {
|
||||
|
||||
QLabel *m_lblProgressDownload;
|
||||
QAction *m_lblProgressDownloadAction;
|
||||
|
||||
AdBlockIcon* m_adBlockIcon;
|
||||
QAction *m_adBlockIconAction;
|
||||
};
|
||||
|
||||
#endif // STATUSBAR_H
|
||||
|
@ -26,7 +26,6 @@
|
||||
#include "gui/statusbar.h"
|
||||
#include "gui/dialogs/formmain.h"
|
||||
#include "exceptions/applicationexception.h"
|
||||
#include "adblock/adblockmanager.h"
|
||||
|
||||
#include "services/abstract/serviceroot.h"
|
||||
#include "services/standard/standardserviceroot.h"
|
||||
@ -279,7 +278,6 @@ void Application::onAboutToQuit() {
|
||||
mainForm()->tabWidget()->feedMessageViewer()->quit();
|
||||
database()->saveDatabase();
|
||||
mainForm()->saveSize();
|
||||
AdBlockManager::instance()->save();
|
||||
|
||||
if (locked_safely) {
|
||||
// Application obtained permission to close in a safe way.
|
||||
|
@ -19,8 +19,6 @@
|
||||
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/iofactory.h"
|
||||
#include "network-web/adblock/requestinterceptor.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QPointer>
|
||||
@ -359,9 +357,6 @@ Settings *Settings::setupSettings(QObject *parent) {
|
||||
// Portable settings are available, use them.
|
||||
new_settings = new Settings(properties.m_absoluteSettingsFileName, QSettings::IniFormat, properties.m_type, parent);
|
||||
|
||||
// Set Blick communication interceptor for simple Adblocking.
|
||||
QWebEngineProfile::defaultProfile()->setRequestInterceptor(new RequestInterceptor(new_settings));
|
||||
|
||||
// Check if portable settings are available.
|
||||
if (properties.m_type == SettingsProperties::Portable) {
|
||||
qDebug("Initializing settings in '%s' (portable way).", qPrintable(QDir::toNativeSeparators(properties.m_absoluteSettingsFileName)));
|
||||
|
@ -1,129 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 "network-web/adblock/adblockaddsubscriptiondialog.h"
|
||||
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "definitions/definitions.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/iconfactory.h"
|
||||
#include "gui/lineeditwithstatus.h"
|
||||
|
||||
#include <QPushButton>
|
||||
|
||||
|
||||
AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget *parent)
|
||||
: QDialog(parent), m_ui(new Ui::AdBlockAddSubscriptionDialog) {
|
||||
m_ui->setupUi(this);
|
||||
|
||||
setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint);
|
||||
setWindowIcon(qApp->icons()->fromTheme("web-adblock"));
|
||||
|
||||
setTabOrder(m_ui->m_cmbPresets, m_ui->m_txtTitle->lineEdit());
|
||||
setTabOrder(m_ui->m_txtTitle->lineEdit(), m_ui->m_txtUrl->lineEdit());
|
||||
setTabOrder(m_ui->m_txtUrl->lineEdit(), m_ui->m_buttonBox);
|
||||
|
||||
loadPresets();
|
||||
|
||||
connect(m_ui->m_cmbPresets, SIGNAL(currentIndexChanged(int)), this, SLOT(onSubscriptionPresetChanged(int)));
|
||||
connect(m_ui->m_txtTitle->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(checkInputs()));
|
||||
connect(m_ui->m_txtUrl->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(checkInputs()));
|
||||
|
||||
onSubscriptionPresetChanged(0);
|
||||
}
|
||||
|
||||
void AdBlockAddSubscriptionDialog::loadPresets() {
|
||||
m_knownSubscriptions << Subscription("EasyList (English)", ADBLOCK_EASYLIST_URL)
|
||||
<< Subscription("Fanboy's List (English)", "http://www.fanboy.co.nz/adblock/fanboy-adblock.txt")
|
||||
<< Subscription("Adversity (English)", "https://raw.githubusercontent.com/Hubird-au/Adversity/master/Adversity.txt")
|
||||
<< Subscription("BSI Lista Polska (Polish)", "http://www.bsi.info.pl/filtrABP.txt")
|
||||
<< Subscription("Easylist Czech and Slovak (Czech)", "https://raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt")
|
||||
<< Subscription("dutchblock (Dutch)", "http://groenewoudt.net/dutchblock/list.txt")
|
||||
<< Subscription("Filtros Nauscopicos (Spanish)", "http://abp.mozilla-hispano.org/nauscopio/filtros.txt")
|
||||
<< Subscription("hufilter (Hungarian)", "http://www.hufilter.hu/hufilter.txt")
|
||||
<< Subscription("IsraelList (Hebrew)", "http://secure.fanboy.co.nz/israelilist/IsraelList.txt")
|
||||
<< Subscription("Lista Basa (Polish)", "http://plok.studentlive.pl/abp.txt")
|
||||
<< Subscription("NLBlock (Dutch)", "http://www.verzijlbergh.com/adblock/nlblock.txt")
|
||||
<< Subscription("Peter Lowe's list (English)", "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=adblockplus&mimetype=plaintext")
|
||||
<< Subscription("PLgeneral (Polish)", "http://www.niecko.pl/adblock/adblock.txt")
|
||||
<< Subscription("Schacks Adblock Plus liste (Danish)", "http://adblock.schack.dk/block.txt")
|
||||
<< Subscription("Xfiles (Italian)", "http://mozilla.gfsolone.com/filtri.txt")
|
||||
<< Subscription("Adblock Warning Removal List", "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt")
|
||||
<< Subscription("EasyPrivacy (English)", "http://easylist-downloads.adblockplus.org/easyprivacy.txt")
|
||||
<< Subscription("Antisocial (English)", "https://raw.githubusercontent.com/Hubird-au/Adversity/master/Antisocial.txt")
|
||||
<< Subscription("RuAdList+EasyList (Russian, Ukrainian)", "https://easylist-downloads.adblockplus.org/ruadlist+easylist.txt")
|
||||
<< Subscription("RU AdList (Russian, Ukrainian)", "https://easylist-downloads.adblockplus.org/advblock.txt")
|
||||
<< Subscription("ABPindo (Indonesian)", "https://raw.githubusercontent.com/heradhis/indonesianadblockrules/master/subscriptions/abpindo.txt")
|
||||
<< Subscription("ChinaList (Chinese)", "http://adblock-chinalist.googlecode.com/svn/trunk/adblock.txt")
|
||||
<< Subscription("Malware Domains list", "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt") <<
|
||||
Subscription(tr("Another subscription"), QString());
|
||||
|
||||
foreach (const Subscription &subscription, m_knownSubscriptions) {
|
||||
m_ui->m_cmbPresets->addItem(subscription.m_title);
|
||||
}
|
||||
}
|
||||
|
||||
QString AdBlockAddSubscriptionDialog::title() const {
|
||||
return m_ui->m_txtTitle->lineEdit()->text();
|
||||
}
|
||||
|
||||
QString AdBlockAddSubscriptionDialog::url() const {
|
||||
return m_ui->m_txtUrl->lineEdit()->text();
|
||||
}
|
||||
|
||||
void AdBlockAddSubscriptionDialog::onSubscriptionPresetChanged(int index) {
|
||||
const Subscription subscription = m_knownSubscriptions.at(index);
|
||||
|
||||
// "Other" entry.
|
||||
if (subscription.m_url.isEmpty()) {
|
||||
m_ui->m_txtTitle->lineEdit()->clear();
|
||||
m_ui->m_txtUrl->lineEdit()->clear();
|
||||
}
|
||||
else {
|
||||
m_ui->m_txtTitle->lineEdit()->setText( subscription.m_title);
|
||||
m_ui->m_txtTitle->lineEdit()->setCursorPosition(0);
|
||||
m_ui->m_txtUrl->lineEdit()->setText(subscription.m_url);
|
||||
m_ui->m_txtUrl->lineEdit()->setCursorPosition(0);
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockAddSubscriptionDialog::checkInputs() {
|
||||
bool is_ok = true;
|
||||
|
||||
if (!m_ui->m_txtTitle->lineEdit()->text().simplified().isEmpty()) {
|
||||
m_ui->m_txtTitle->setStatus(WidgetWithStatus::Ok, tr("Entered title is okay."));
|
||||
}
|
||||
else {
|
||||
m_ui->m_txtTitle->setStatus(WidgetWithStatus::Error, tr("Entered title is empty."));
|
||||
is_ok = false;
|
||||
}
|
||||
|
||||
if (!m_ui->m_txtUrl->lineEdit()->text().simplified().isEmpty()) {
|
||||
m_ui->m_txtUrl->setStatus(WidgetWithStatus::Ok, tr("Entered url is okay."));
|
||||
}
|
||||
else {
|
||||
m_ui->m_txtUrl->setStatus(WidgetWithStatus::Error, tr("Entered url is empty."));
|
||||
is_ok = false;
|
||||
}
|
||||
|
||||
m_ui->m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(is_ok);
|
||||
}
|
||||
|
||||
AdBlockAddSubscriptionDialog::~AdBlockAddSubscriptionDialog() {
|
||||
qDebug("Destroying AdBlockAddSubscriptionDialog instance.");
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 ADBLOCKADDSUBSCRIPTIONDIALOG_H
|
||||
#define ADBLOCKADDSUBSCRIPTIONDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
#include "ui_adblockaddsubscriptiondialog.h"
|
||||
|
||||
#include <QVector>
|
||||
|
||||
|
||||
namespace Ui {
|
||||
class AdBlockAddSubscriptionDialog;
|
||||
}
|
||||
|
||||
class AdBlockAddSubscriptionDialog : public QDialog {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
// Constructors.
|
||||
explicit AdBlockAddSubscriptionDialog(QWidget *parent = 0);
|
||||
virtual ~AdBlockAddSubscriptionDialog();
|
||||
|
||||
QString title() const;
|
||||
QString url() const;
|
||||
|
||||
private slots:
|
||||
// Index of selected list changed.
|
||||
void onSubscriptionPresetChanged(int index);
|
||||
void checkInputs();
|
||||
|
||||
private:
|
||||
void loadPresets();
|
||||
|
||||
struct Subscription {
|
||||
public:
|
||||
QString m_title;
|
||||
QString m_url;
|
||||
|
||||
explicit Subscription() {
|
||||
}
|
||||
|
||||
explicit Subscription(const QString &title, const QString &url) {
|
||||
m_title = title;
|
||||
m_url = url;
|
||||
}
|
||||
};
|
||||
|
||||
QScopedPointer<Ui::AdBlockAddSubscriptionDialog> m_ui;
|
||||
QVector<Subscription> m_knownSubscriptions;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKADDSUBSCRIPTIONDIALOG_H
|
@ -1,114 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>AdBlockAddSubscriptionDialog</class>
|
||||
<widget class="QDialog" name="AdBlockAddSubscriptionDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>557</width>
|
||||
<height>142</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Add subscription</string>
|
||||
</property>
|
||||
<layout class="QFormLayout">
|
||||
<item row="0" column="0" colspan="2">
|
||||
<widget class="QComboBox" name="m_cmbPresets"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="m_lblTitle">
|
||||
<property name="text">
|
||||
<string>Title</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>m_txtTitle</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="m_lblUrl">
|
||||
<property name="text">
|
||||
<string>Address</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>m_txtUrl</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QDialogButtonBox" name="m_buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<spacer name="m_vertSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="LineEditWithStatus" name="m_txtTitle" native="true"/>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="LineEditWithStatus" name="m_txtUrl" native="true"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>LineEditWithStatus</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>lineeditwithstatus.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>m_buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>AdBlockAddSubscriptionDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>m_buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>AdBlockAddSubscriptionDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
@ -1,213 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 "network-web/adblock/adblockdialog.h"
|
||||
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "network-web/adblock/adblocktreewidget.h"
|
||||
#include "network-web/adblock/adblockaddsubscriptiondialog.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/iconfactory.h"
|
||||
#include "gui/tabwidget.h"
|
||||
#include "gui/dialogs/formmain.h"
|
||||
|
||||
#include <QDesktopWidget>
|
||||
#include <QMenu>
|
||||
#include <QTimer>
|
||||
#include <QMessageBox>
|
||||
#include <QInputDialog>
|
||||
|
||||
|
||||
AdBlockDialog::AdBlockDialog(QWidget* parent) : QDialog(parent), m_ui(new Ui::AdBlockDialog),
|
||||
m_manager(AdBlockManager::instance()), m_currentTreeWidget(NULL),
|
||||
m_currentSubscription(NULL), m_loaded(false), m_useLimitedEasyList(false) {
|
||||
m_ui->setupUi(this);
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint);
|
||||
setWindowIcon(qApp->icons()->fromTheme("web-adblock"));
|
||||
|
||||
m_ui->m_checkEnable->setChecked(m_manager->isEnabled());
|
||||
m_ui->m_checkUseLimitedEasyList->setVisible(false);
|
||||
m_ui->m_btnOptions->setIcon(qApp->icons()->fromTheme("web-adblock"));
|
||||
m_ui->m_btnOptions->setText(m_ui->m_btnOptions->text() + " ");
|
||||
|
||||
// Setup the menu.
|
||||
setupMenu();
|
||||
|
||||
// Initialize connections.
|
||||
createConnections();
|
||||
|
||||
// Load the contents.
|
||||
load();
|
||||
}
|
||||
|
||||
AdBlockDialog::~AdBlockDialog() {
|
||||
qDebug("Destroying AdBlockDialog instance.");
|
||||
}
|
||||
|
||||
void AdBlockDialog::setupMenu() {
|
||||
QMenu *menu = new QMenu(m_ui->m_btnOptions);
|
||||
|
||||
m_actionAddRule = menu->addAction(tr("Add rule"), this, SLOT(addRule()));
|
||||
m_actionRemoveRule = menu->addAction(tr("Remove rule"), this, SLOT(removeRule()));
|
||||
menu->addSeparator();
|
||||
m_actionAddSubscription = menu->addAction(tr("Add subscription"), this, SLOT(addSubscription()));
|
||||
m_actionRemoveSubscription = menu->addAction(tr("Remove subscription"), this, SLOT(removeSubscription()));
|
||||
menu->addAction(tr("Update subscriptions"), m_manager, SLOT(updateAllSubscriptions()));
|
||||
menu->addSeparator();
|
||||
menu->addAction(tr("Rules writing guide"), this, SLOT(learnAboutRules()));
|
||||
|
||||
m_ui->m_btnOptions->setMenu(menu);
|
||||
}
|
||||
|
||||
void AdBlockDialog::createConnections() {
|
||||
connect(m_ui->m_btnOptions->menu(), SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu()));
|
||||
connect(m_ui->m_checkEnable, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool)));
|
||||
connect(m_ui->m_txtFilter, SIGNAL(textChanged(QString)), this, SLOT(filterString(QString)));
|
||||
connect(m_ui->m_tabs, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
|
||||
connect(m_ui->m_buttonBox, SIGNAL(accepted()), this, SLOT(close()));
|
||||
}
|
||||
|
||||
void AdBlockDialog::showRule(const AdBlockRule *rule) const {
|
||||
const AdBlockSubscription *subscription = rule->subscription();
|
||||
|
||||
if (subscription == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_ui->m_tabs->count(); i++) {
|
||||
AdBlockTreeWidget *tree_widget = qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabs->widget(i));
|
||||
|
||||
if (subscription == tree_widget->subscription()) {
|
||||
tree_widget->showRule(rule);
|
||||
m_ui->m_tabs->setCurrentIndex(i);
|
||||
tree_widget->setFocus();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockDialog::addRule() {
|
||||
m_currentTreeWidget->addRule();
|
||||
}
|
||||
|
||||
void AdBlockDialog::removeRule() {
|
||||
m_currentTreeWidget->removeRule();
|
||||
}
|
||||
|
||||
void AdBlockDialog::addSubscription() {
|
||||
QScopedPointer<AdBlockAddSubscriptionDialog> dialog(new AdBlockAddSubscriptionDialog(this));
|
||||
|
||||
if (dialog.data()->exec() == QDialog::Accepted) {
|
||||
const QString title = dialog.data()->title();
|
||||
const QString url = dialog.data()->url();
|
||||
|
||||
if (AdBlockSubscription *subscription = m_manager->addSubscription(title, url)) {
|
||||
AdBlockTreeWidget *tree = new AdBlockTreeWidget(subscription, this);
|
||||
connect(tree, SIGNAL(refreshStatusChanged(bool)), this, SLOT(setDisabled(bool)));
|
||||
|
||||
const int index = m_ui->m_tabs->insertTab(m_ui->m_tabs->count() - 1, tree, subscription->title());
|
||||
m_ui->m_tabs->setCurrentIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
//delete dialog.data();
|
||||
}
|
||||
|
||||
void AdBlockDialog::removeSubscription() {
|
||||
if (m_manager->removeSubscription(m_currentSubscription)) {
|
||||
delete m_currentTreeWidget;
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockDialog::currentChanged(int index) {
|
||||
if (index != -1) {
|
||||
m_currentTreeWidget = qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabs->widget(index));
|
||||
m_currentSubscription = m_currentTreeWidget->subscription();
|
||||
|
||||
const bool is_easylist = m_currentSubscription->url() == QUrl(ADBLOCK_EASYLIST_URL);
|
||||
m_ui->m_checkUseLimitedEasyList->setEnabled(is_easylist && m_ui->m_checkEnable->isChecked());
|
||||
m_ui->m_checkUseLimitedEasyList->setVisible(is_easylist);
|
||||
|
||||
m_ui->m_txtFilter->blockSignals(true);
|
||||
m_ui->m_txtFilter->clear();
|
||||
m_ui->m_txtFilter->blockSignals(false);
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockDialog::filterString(const QString &string) {
|
||||
if (m_currentTreeWidget && m_ui->m_checkEnable->isChecked()) {
|
||||
m_currentTreeWidget->filterString(string);
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockDialog::enableAdBlock(bool state) {
|
||||
m_manager->setEnabled(state);
|
||||
|
||||
if (state) {
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockDialog::aboutToShowMenu() {
|
||||
const bool subscription_editable = m_currentSubscription && m_currentSubscription->canEditRules();
|
||||
const bool subscription_removable = m_currentSubscription && m_currentSubscription->canBeRemoved();
|
||||
|
||||
m_actionAddRule->setEnabled(subscription_editable);
|
||||
m_actionRemoveRule->setEnabled(subscription_editable);
|
||||
m_actionRemoveSubscription->setEnabled(subscription_removable);
|
||||
}
|
||||
|
||||
void AdBlockDialog::learnAboutRules() {
|
||||
qApp->mainForm()->tabWidget()->addBrowser(true, true, QUrl(ADBLOCK_FILTERS_HELP));
|
||||
QTimer::singleShot(100, this, SLOT(close()));
|
||||
}
|
||||
|
||||
void AdBlockDialog::loadSubscriptions() {
|
||||
for (int i = 0; i < m_ui->m_tabs->count(); ++i) {
|
||||
qobject_cast<AdBlockTreeWidget*>(m_ui->m_tabs->widget(i))->refresh();
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockDialog::load() {
|
||||
if (m_loaded || !m_ui->m_checkEnable->isChecked()) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (AdBlockSubscription *subscription, m_manager->subscriptions()) {
|
||||
AdBlockTreeWidget *tree = new AdBlockTreeWidget(subscription, this);
|
||||
|
||||
connect(tree, SIGNAL(refreshStatusChanged(bool)), this, SLOT(setDisabled(bool)));
|
||||
m_ui->m_tabs->addTab(tree, subscription->title());
|
||||
}
|
||||
|
||||
m_useLimitedEasyList = m_manager->useLimitedEasyList();
|
||||
m_ui->m_checkUseLimitedEasyList->setChecked(m_useLimitedEasyList);
|
||||
m_loaded = true;
|
||||
|
||||
QTimer::singleShot(100, this, SLOT(loadSubscriptions()));
|
||||
}
|
||||
|
||||
void AdBlockDialog::closeEvent(QCloseEvent *event) {
|
||||
if (m_ui->m_checkUseLimitedEasyList->isChecked() != m_useLimitedEasyList) {
|
||||
m_manager->setUseLimitedEasyList(m_ui->m_checkUseLimitedEasyList->isChecked());
|
||||
}
|
||||
|
||||
QWidget::closeEvent(event);
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2011-2016 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 ADBLOCKDIALOG_H
|
||||
#define ADBLOCKDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
#include "ui_adblockdialog.h"
|
||||
|
||||
|
||||
namespace Ui {
|
||||
class AdBlockDialog;
|
||||
}
|
||||
|
||||
class AdBlockSubscription;
|
||||
class AdBlockTreeWidget;
|
||||
class AdBlockManager;
|
||||
class AdBlockRule;
|
||||
|
||||
class AdBlockDialog : public QDialog {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
// Constructors.
|
||||
explicit AdBlockDialog(QWidget *parent = 0);
|
||||
virtual ~AdBlockDialog();
|
||||
|
||||
void showRule(const AdBlockRule *rule) const;
|
||||
|
||||
private slots:
|
||||
void addRule();
|
||||
void removeRule();
|
||||
|
||||
void addSubscription();
|
||||
void removeSubscription();
|
||||
|
||||
void currentChanged(int index);
|
||||
void filterString(const QString &string);
|
||||
void enableAdBlock(bool state);
|
||||
|
||||
void aboutToShowMenu();
|
||||
void learnAboutRules();
|
||||
|
||||
void loadSubscriptions();
|
||||
void load();
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *event);
|
||||
|
||||
private:
|
||||
void setupMenu();
|
||||
void createConnections();
|
||||
|
||||
QScopedPointer<Ui::AdBlockDialog> m_ui;
|
||||
AdBlockManager *m_manager;
|
||||
AdBlockTreeWidget *m_currentTreeWidget;
|
||||
AdBlockSubscription *m_currentSubscription;
|
||||
|
||||
QAction *m_actionAddRule;
|
||||
QAction *m_actionRemoveRule;
|
||||
QAction *m_actionAddSubscription;
|
||||
QAction *m_actionRemoveSubscription;
|
||||
|
||||
bool m_loaded;
|
||||
bool m_useLimitedEasyList;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKDIALOG_H
|
@ -1,154 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>AdBlockDialog</class>
|
||||
<widget class="QDialog" name="AdBlockDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>535</width>
|
||||
<height>466</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Adblock settings</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="m_checkEnable">
|
||||
<property name="text">
|
||||
<string>Enable Adblock</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="m_lblInfo">
|
||||
<property name="font">
|
||||
<font>
|
||||
<italic>true</italic>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Note that Adblock may significantly slow this application down once you activate huge subscriptions. Too many rules is not good for performance. Also, make sure you restart application after you disable Adblock if you wish to have low memory footprint. Adblock is known to use much system memory.
|
||||
|
||||
Also note that some resources are cached by internal web browser. Thus, after changing some rules or subscriptions they will fully apply only for new application instances. Make sure you restart RSS Guard for best Adblock experience.</string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QWidget" name="m_adBlockWidget" native="true">
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="4" column="0" colspan="2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="m_btnOptions">
|
||||
<property name="text">
|
||||
<string>Options</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="m_txtFilter">
|
||||
<property name="placeholderText">
|
||||
<string>Filter rules</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QTabWidget" name="m_tabs"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="m_checkUseLimitedEasyList">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Use only essential part of EasyList (for performance reasons)</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="m_buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Close</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
<tabstop>m_checkEnable</tabstop>
|
||||
<tabstop>m_tabs</tabstop>
|
||||
<tabstop>m_btnOptions</tabstop>
|
||||
<tabstop>m_txtFilter</tabstop>
|
||||
<tabstop>m_checkUseLimitedEasyList</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>m_checkEnable</sender>
|
||||
<signal>toggled(bool)</signal>
|
||||
<receiver>m_adBlockWidget</receiver>
|
||||
<slot>setEnabled(bool)</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>106</x>
|
||||
<y>39</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>349</x>
|
||||
<y>74</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>m_buttonBox</sender>
|
||||
<signal>clicked(QAbstractButton*)</signal>
|
||||
<receiver>AdBlockDialog</receiver>
|
||||
<slot>close()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>434</x>
|
||||
<y>440</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>272</x>
|
||||
<y>230</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
@ -1,151 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 "network-web/adblock/adblockicon.h"
|
||||
|
||||
#include "network-web/adblock/adblockrule.h"
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "network-web/webpage.h"
|
||||
#include "network-web/webbrowser.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "gui/plaintoolbutton.h"
|
||||
#include "gui/tabwidget.h"
|
||||
#include "gui/dialogs/formmain.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QMouseEvent>
|
||||
|
||||
|
||||
AdBlockIcon::AdBlockIcon(QWidget *window, QWidget *parent)
|
||||
: PlainToolButton(parent), m_window(window), m_menuAction(NULL), m_enabled(false) {
|
||||
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint)));
|
||||
connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool)));
|
||||
}
|
||||
|
||||
QAction *AdBlockIcon::menuAction() {
|
||||
if (m_menuAction == NULL) {
|
||||
m_menuAction = new QAction(tr("Adblock"), this);
|
||||
m_menuAction->setMenu(new QMenu(this));
|
||||
|
||||
connect(m_menuAction->menu(), SIGNAL(aboutToShow()), this, SLOT(createMenu()));
|
||||
}
|
||||
|
||||
m_menuAction->setIcon(m_enabled ? qApp->icons()->fromTheme("web-adblock") : qApp->icons()->fromTheme("web-adblock"));
|
||||
|
||||
return m_menuAction;
|
||||
}
|
||||
|
||||
void AdBlockIcon::activate() {
|
||||
setEnabled(AdBlockManager::instance()->shouldBeEnabled());
|
||||
}
|
||||
|
||||
void AdBlockIcon::createMenu(QMenu *menu) {
|
||||
if (menu == NULL) {
|
||||
menu = qobject_cast<QMenu*>(sender());
|
||||
|
||||
if (menu == NULL) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
menu->clear();
|
||||
|
||||
AdBlockManager *manager = AdBlockManager::instance();
|
||||
AdBlockCustomList *custom_list = manager->customList();
|
||||
|
||||
WebPage *page = qApp->mainForm()->tabWidget()->currentWidget()->webBrowser()->view()->page();
|
||||
const QUrl page_url = page->url();
|
||||
|
||||
menu->addAction(tr("Show Adblock &settings"), manager, SLOT(showDialog()));
|
||||
menu->addSeparator();
|
||||
|
||||
if (!page_url.host().isEmpty() && m_enabled && manager->canRunOnScheme(page_url.scheme())) {
|
||||
const QString host = page_url.host().contains(QSL("www.")) ? page_url.host().mid(4) : page_url.host();
|
||||
const QString host_filter = QString(QSL("@@||%1^$document")).arg(host);
|
||||
const QString page_filter = QString(QSL("@@|%1|$document")).arg(page_url.toString());
|
||||
|
||||
QAction *act;
|
||||
|
||||
act = menu->addAction(tr("Disable on %1").arg(host));
|
||||
act->setCheckable(true);
|
||||
act->setChecked(custom_list->containsFilter(host_filter));
|
||||
act->setData(host_filter);
|
||||
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
|
||||
|
||||
act = menu->addAction(tr("Disable only on this page"));
|
||||
act->setCheckable(true);
|
||||
act->setChecked(custom_list->containsFilter(page_filter));
|
||||
act->setData(page_filter);
|
||||
connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter()));
|
||||
|
||||
menu->addSeparator();
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockIcon::showMenu(const QPoint &pos) {
|
||||
QMenu menu;
|
||||
createMenu(&menu);
|
||||
menu.exec(pos);
|
||||
}
|
||||
|
||||
void AdBlockIcon::toggleCustomFilter() {
|
||||
const QAction *action = qobject_cast<QAction*>(sender());
|
||||
|
||||
if (action == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
const QString filter = action->data().toString();
|
||||
AdBlockManager *manager = AdBlockManager::instance();
|
||||
AdBlockCustomList *custom_list = manager->customList();
|
||||
|
||||
if (custom_list->containsFilter(filter)) {
|
||||
custom_list->removeFilter(filter);
|
||||
}
|
||||
else {
|
||||
AdBlockRule *rule = new AdBlockRule(filter, custom_list);
|
||||
custom_list->addRule(rule);
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockIcon::setEnabled(bool enabled) {
|
||||
if (enabled) {
|
||||
setToolTip(tr("Adblock - up and running"));
|
||||
setIcon(qApp->icons()->fromTheme("web-adblock"));
|
||||
}
|
||||
else {
|
||||
setToolTip(tr("Adblock - not running"));
|
||||
setIcon(qApp->icons()->fromTheme("web-adblock-disabled"));
|
||||
}
|
||||
|
||||
m_enabled = enabled;
|
||||
}
|
||||
|
||||
void AdBlockIcon::mouseReleaseEvent(QMouseEvent *event) {
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
emit clicked(event->globalPos());
|
||||
}
|
||||
else {
|
||||
QToolButton::mouseReleaseEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
AdBlockIcon::~AdBlockIcon() {
|
||||
qDebug("Destroying AdBlockIcon instance.");
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 ADBLOCKICON_H
|
||||
#define ADBLOCKICON_H
|
||||
|
||||
#include "gui/plaintoolbutton.h"
|
||||
|
||||
|
||||
class QMenu;
|
||||
class QUrl;
|
||||
class AdBlockRule;
|
||||
|
||||
class AdBlockIcon : public PlainToolButton {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
// Constructors.
|
||||
explicit AdBlockIcon(QWidget *window, QWidget *parent = 0);
|
||||
virtual ~AdBlockIcon();
|
||||
|
||||
QAction *menuAction();
|
||||
|
||||
public slots:
|
||||
void activate();
|
||||
void setEnabled(bool enabled);
|
||||
void createMenu(QMenu *menu = NULL);
|
||||
|
||||
private slots:
|
||||
void showMenu(const QPoint &pos);
|
||||
void toggleCustomFilter();
|
||||
|
||||
protected:
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
|
||||
signals:
|
||||
void clicked(QPoint);
|
||||
|
||||
private:
|
||||
QWidget *m_window;
|
||||
QAction *m_menuAction;
|
||||
QVector<QPair<AdBlockRule*,QUrl> > m_blockedPopups;
|
||||
bool m_enabled;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKICON_H
|
@ -1,328 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 "network-web/adblock/adblockmanager.h"
|
||||
|
||||
#include "network-web/adblock/adblockdialog.h"
|
||||
#include "network-web/adblock/adblockmatcher.h"
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "network-web/adblock/adblockicon.h"
|
||||
#include "network-web/webpage.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "miscellaneous/settings.h"
|
||||
#include "definitions/definitions.h"
|
||||
#include "gui/dialogs/formmain.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QTextStream>
|
||||
#include <QDir>
|
||||
#include <QTimer>
|
||||
|
||||
|
||||
AdBlockManager *AdBlockManager::s_adBlockManager = NULL;
|
||||
|
||||
AdBlockManager::AdBlockManager(QObject* parent)
|
||||
: QObject(parent), m_loaded(false), m_enabled(false), m_useLimitedEasyList(true),
|
||||
m_subscriptions(QList<AdBlockSubscription*>()), m_matcher(new AdBlockMatcher(this)) {
|
||||
load();
|
||||
}
|
||||
|
||||
AdBlockManager::~AdBlockManager() {
|
||||
qDeleteAll(m_subscriptions);
|
||||
}
|
||||
|
||||
AdBlockManager *AdBlockManager::instance() {
|
||||
if (s_adBlockManager == NULL) {
|
||||
s_adBlockManager = new AdBlockManager(qApp);
|
||||
}
|
||||
|
||||
return s_adBlockManager;
|
||||
}
|
||||
|
||||
void AdBlockManager::setEnabled(bool enabled) {
|
||||
if (m_enabled == enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_enabled = enabled;
|
||||
emit enabledChanged(enabled);
|
||||
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::Enabled, m_enabled);
|
||||
|
||||
// Load subscriptions and other data.
|
||||
load();
|
||||
}
|
||||
|
||||
QList<AdBlockSubscription*> AdBlockManager::subscriptions() const {
|
||||
return m_subscriptions;
|
||||
}
|
||||
|
||||
bool AdBlockManager::shouldBlock(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) {
|
||||
const QString url_string = url.toEncoded().toLower();
|
||||
const QString url_domain = url.host().toLower();
|
||||
const QString url_scheme = url.scheme().toLower();
|
||||
|
||||
if (!isEnabled() || !canRunOnScheme(url_scheme)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const AdBlockRule *blocked_rule = m_matcher->match(url, url_domain, url_string, referer, resource_type);
|
||||
|
||||
if (blocked_rule != NULL) {
|
||||
if (!canBeBlocked(url, referer, resource_type)) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QStringList AdBlockManager::disabledRules() const {
|
||||
return m_disabledRules;
|
||||
}
|
||||
|
||||
void AdBlockManager::addDisabledRule(const QString &filter) {
|
||||
m_disabledRules.append(filter);
|
||||
}
|
||||
|
||||
void AdBlockManager::removeDisabledRule(const QString &filter) {
|
||||
m_disabledRules.removeOne(filter);
|
||||
}
|
||||
|
||||
AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) {
|
||||
if (title.isEmpty() || url.isEmpty()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const QString file_name = IOFactory::filterBadCharsFromFilename(title.toLower()) + QL1S(".txt");
|
||||
const QString file_path = IOFactory::ensureUniqueFilename(baseSubscriptionDirectory() + QDir::separator() + file_name);
|
||||
QFile file(file_path);
|
||||
|
||||
if (!file.open(QFile::WriteOnly | QFile::Truncate | QFile::Unbuffered)) {
|
||||
qWarning("Cannot write to file '%s'.",qPrintable(file_path));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file.write(QString("Title: %1\nUrl: %2\n[Adblock Plus 1.1.1]").arg(title, url).toLatin1());
|
||||
file.close();
|
||||
|
||||
AdBlockSubscription *subscription = new AdBlockSubscription(title, this);
|
||||
subscription->setUrl(QUrl(url));
|
||||
subscription->setFilePath(file_path);
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
|
||||
// This expects that there is at least "Custom rules" subscription available in
|
||||
// active subscriptions.
|
||||
m_subscriptions.insert(m_subscriptions.size() - 1, subscription);
|
||||
m_matcher->update();
|
||||
|
||||
return subscription;
|
||||
}
|
||||
|
||||
bool AdBlockManager::removeSubscription(AdBlockSubscription *subscription) {
|
||||
if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
QFile::remove(subscription->filePath());
|
||||
m_subscriptions.removeOne(subscription);
|
||||
delete subscription;
|
||||
m_matcher->update();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
AdBlockCustomList *AdBlockManager::customList() const {
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
AdBlockCustomList *list = qobject_cast<AdBlockCustomList*>(subscription);
|
||||
|
||||
if (list != NULL) {
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QString AdBlockManager::baseSubscriptionDirectory() {
|
||||
QString directory;
|
||||
|
||||
if (qApp->settings()->type() == SettingsProperties::Portable) {
|
||||
directory = qApp->applicationDirPath();
|
||||
}
|
||||
else {
|
||||
directory = qApp->homeFolderPath() + QDir::separator() + QString(APP_LOW_H_NAME);
|
||||
}
|
||||
|
||||
directory += QString(QDir::separator()) + ADBLOCK_BASE_DIRECTORY_NAME;
|
||||
return QDir::toNativeSeparators(directory);
|
||||
}
|
||||
|
||||
bool AdBlockManager::shouldBeEnabled() const {
|
||||
return qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::Enabled)).toBool();
|
||||
}
|
||||
|
||||
void AdBlockManager::load() {
|
||||
if (m_loaded) {
|
||||
// It is already loaded: subscriptions are loaded from files into objects,
|
||||
// enabled status is determined, disabled rules are also determined.
|
||||
return;
|
||||
}
|
||||
|
||||
const Settings *settings = qApp->settings();
|
||||
m_enabled = settings->value(GROUP(AdBlock), SETTING(AdBlock::Enabled)).toBool();
|
||||
m_useLimitedEasyList = settings->value(GROUP(AdBlock), SETTING(AdBlock::UseLimitedEasyList)).toBool();
|
||||
m_disabledRules = settings->value(GROUP(AdBlock), SETTING(AdBlock::DisabledRules)).toStringList();
|
||||
|
||||
if (!m_enabled) {
|
||||
// We loaded settings, but Adblock should be disabled. Do not continue to save memory.
|
||||
return;
|
||||
}
|
||||
|
||||
QDir adblock_dir(baseSubscriptionDirectory());
|
||||
|
||||
if (!adblock_dir.exists()) {
|
||||
adblock_dir.mkpath(adblock_dir.absolutePath());
|
||||
}
|
||||
|
||||
foreach (const QString &file_name, adblock_dir.entryList(QStringList(QL1S("*.txt")), QDir::Files)) {
|
||||
if (file_name == QSL(ADBLOCK_CUSTOM_LIST_FILENAME)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QString absolute_path = adblock_dir.absoluteFilePath(file_name);
|
||||
QFile file(absolute_path);
|
||||
|
||||
if (!file.open(QFile::ReadOnly)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QTextStream stream(&file);
|
||||
stream.setCodec("UTF-8");
|
||||
const QString title = stream.readLine(1024).remove(QSL("Title: "));
|
||||
const QUrl url = QUrl(stream.readLine(1024).remove(QSL("Url: ")));
|
||||
|
||||
// Close the file.
|
||||
file.close();
|
||||
|
||||
if (title.isEmpty() || !url.isValid()) {
|
||||
qWarning("Invalid subscription file '%s'.", qPrintable(absolute_path));
|
||||
continue;
|
||||
}
|
||||
|
||||
AdBlockSubscription *subscription = new AdBlockSubscription(title, this);
|
||||
subscription->setUrl(url);
|
||||
subscription->setFilePath(absolute_path);
|
||||
|
||||
m_subscriptions.append(subscription);
|
||||
}
|
||||
|
||||
// Append list for "Custom rules".
|
||||
m_subscriptions.append(new AdBlockCustomList(this));
|
||||
|
||||
// Load all subscriptions, including obligatory "Custom rules" list.
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
subscription->loadSubscription(m_disabledRules);
|
||||
|
||||
connect(subscription, SIGNAL(subscriptionChanged()), m_matcher, SLOT(update()));
|
||||
}
|
||||
|
||||
// Notify matcher about loaded subscriptions
|
||||
// and mark all this shit as loaded.
|
||||
m_matcher->update();
|
||||
m_loaded = true;
|
||||
}
|
||||
|
||||
void AdBlockManager::updateAllSubscriptions() {
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
subscription->updateSubscription();
|
||||
}
|
||||
|
||||
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::LastUpdated, QDateTime::currentDateTime());
|
||||
}
|
||||
|
||||
void AdBlockManager::save() {
|
||||
if (m_loaded) {
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
subscription->saveSubscription();
|
||||
}
|
||||
|
||||
Settings *settings = qApp->settings();
|
||||
settings->setValue(GROUP(AdBlock), AdBlock::Enabled, m_enabled);
|
||||
settings->setValue(GROUP(AdBlock), AdBlock::UseLimitedEasyList, m_useLimitedEasyList);
|
||||
settings->setValue(GROUP(AdBlock), AdBlock::DisabledRules, m_disabledRules);
|
||||
}
|
||||
}
|
||||
|
||||
bool AdBlockManager::isEnabled() const {
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
bool AdBlockManager::canRunOnScheme(const QString &scheme) const {
|
||||
return !(scheme == QL1S("file") || scheme == QL1S("qrc") || scheme == QL1S("data") || scheme == QL1S("abp"));
|
||||
}
|
||||
|
||||
bool AdBlockManager::useLimitedEasyList() const {
|
||||
return m_useLimitedEasyList;
|
||||
}
|
||||
|
||||
void AdBlockManager::setUseLimitedEasyList(bool use_limited) {
|
||||
m_useLimitedEasyList = use_limited;
|
||||
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
if (subscription->url() == QUrl(ADBLOCK_EASYLIST_URL)) {
|
||||
// User really has EasyList activated, update it.
|
||||
subscription->updateSubscription();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AdBlockManager::canBeBlocked(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const {
|
||||
return !m_matcher->adBlockDisabledForUrl(url, referer, resource_type);
|
||||
}
|
||||
|
||||
AdBlockSubscription *AdBlockManager::subscriptionByName(const QString &name) const {
|
||||
foreach (AdBlockSubscription *subscription, m_subscriptions) {
|
||||
if (subscription->title() == name) {
|
||||
return subscription;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AdBlockDialog *AdBlockManager::showDialog() {
|
||||
QPointer<AdBlockDialog> form_pointer = new AdBlockDialog(qApp->mainForm());
|
||||
form_pointer.data()->setModal(true);
|
||||
form_pointer.data()->show();
|
||||
form_pointer.data()->raise();
|
||||
form_pointer.data()->activateWindow();
|
||||
return form_pointer.data();
|
||||
}
|
||||
|
||||
void AdBlockManager::showRule() {
|
||||
if (QAction *action = qobject_cast<QAction*>(sender())) {
|
||||
const AdBlockRule* rule = static_cast<const AdBlockRule*>(action->data().value<void*>());
|
||||
|
||||
if (rule != NULL) {
|
||||
showDialog()->showRule(rule);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 ADBLOCKMANAGER_H
|
||||
#define ADBLOCKMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <QStringList>
|
||||
#include <QPointer>
|
||||
#include <QWebEngineUrlRequestInfo>
|
||||
|
||||
|
||||
class QUrl;
|
||||
class AdBlockRule;
|
||||
class AdBlockDialog;
|
||||
class AdBlockMatcher;
|
||||
class AdBlockCustomList;
|
||||
class AdBlockSubscription;
|
||||
|
||||
class AdBlockManager : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
// Constructors.
|
||||
explicit AdBlockManager(QObject* parent = 0);
|
||||
virtual ~AdBlockManager();
|
||||
|
||||
void load();
|
||||
void save();
|
||||
|
||||
bool isEnabled() const;
|
||||
bool canRunOnScheme(const QString &scheme) const;
|
||||
|
||||
bool useLimitedEasyList() const;
|
||||
void setUseLimitedEasyList(bool use_limited);
|
||||
|
||||
AdBlockSubscription *subscriptionByName(const QString &name) const;
|
||||
QList<AdBlockSubscription*> subscriptions() const;
|
||||
|
||||
bool shouldBlock(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type);
|
||||
|
||||
QStringList disabledRules() const;
|
||||
void addDisabledRule(const QString &filter);
|
||||
void removeDisabledRule(const QString &filter);
|
||||
|
||||
AdBlockSubscription *addSubscription(const QString &title, const QString &url);
|
||||
bool removeSubscription(AdBlockSubscription* subscription);
|
||||
|
||||
AdBlockCustomList *customList() const;
|
||||
|
||||
bool shouldBeEnabled() const;
|
||||
|
||||
static QString baseSubscriptionDirectory();
|
||||
static AdBlockManager *instance();
|
||||
|
||||
public slots:
|
||||
void setEnabled(bool enabled);
|
||||
void showRule();
|
||||
void updateAllSubscriptions();
|
||||
|
||||
AdBlockDialog *showDialog();
|
||||
|
||||
signals:
|
||||
void enabledChanged(bool enabled);
|
||||
|
||||
private:
|
||||
bool canBeBlocked(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const;
|
||||
|
||||
bool m_loaded;
|
||||
bool m_enabled;
|
||||
bool m_useLimitedEasyList;
|
||||
|
||||
QList<AdBlockSubscription*> m_subscriptions;
|
||||
AdBlockMatcher* m_matcher;
|
||||
QStringList m_disabledRules;
|
||||
|
||||
static AdBlockManager* s_adBlockManager;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKMANAGER_H
|
||||
|
@ -1,123 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
|
||||
// Copyright (C) 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 "network-web/adblock/adblockmatcher.h"
|
||||
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblockrule.h"
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "definitions/definitions.h"
|
||||
|
||||
|
||||
AdBlockMatcher::AdBlockMatcher(AdBlockManager *manager) : QObject(manager), m_manager(manager) {
|
||||
connect(manager, SIGNAL(enabledChanged(bool)), this, SLOT(enabledChanged(bool)));
|
||||
}
|
||||
|
||||
AdBlockMatcher::~AdBlockMatcher() {
|
||||
clear();
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockMatcher::match(const QUrl &url, const QString &url_domain,
|
||||
const QString &url_string, const QString &referer,
|
||||
QWebEngineUrlRequestInfo::ResourceType resource_type) const {
|
||||
// Exception rules.
|
||||
if (m_networkExceptionTree.find(url, url_domain, url_string, referer, resource_type)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0, count = m_networkExceptionRules.size(); i < count; i++) {
|
||||
const AdBlockRule *rule = m_networkExceptionRules.at(i);
|
||||
|
||||
if (rule->networkMatch(url, url_domain, url_string, referer, resource_type)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Block rules.
|
||||
if (const AdBlockRule* rule = m_networkBlockTree.find(url, url_domain, url_string, referer, resource_type)) {
|
||||
return rule;
|
||||
}
|
||||
|
||||
for (int i = 0, count = m_networkBlockRules.size(); i < count; i++) {
|
||||
const AdBlockRule *rule = m_networkBlockRules.at(i);
|
||||
|
||||
if (rule->networkMatch(url, url_domain, url_string, referer, resource_type)) {
|
||||
return rule;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url, const QString &referer,
|
||||
QWebEngineUrlRequestInfo::ResourceType resource_type) const {
|
||||
for (int i = 0, count = m_documentRules.size(); i < count; i++) {
|
||||
if (m_documentRules.at(i)->urlMatch(url, referer, resource_type)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void AdBlockMatcher::update() {
|
||||
clear();
|
||||
|
||||
foreach (const AdBlockSubscription *subscription, m_manager->subscriptions()) {
|
||||
foreach (const AdBlockRule *rule, subscription->allRules()) {
|
||||
// Don't add internally disabled rules to cache
|
||||
if (rule->isInternalDisabled()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rule->isDocument()) {
|
||||
m_documentRules.append(rule);
|
||||
}
|
||||
else if (rule->isException()) {
|
||||
if (!m_networkExceptionTree.add(rule)) {
|
||||
m_networkExceptionRules.append(rule);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!m_networkBlockTree.add(rule)) {
|
||||
m_networkBlockRules.append(rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockMatcher::clear() {
|
||||
m_networkExceptionTree.clear();
|
||||
m_networkExceptionRules.clear();
|
||||
|
||||
m_networkBlockTree.clear();
|
||||
m_networkBlockRules.clear();
|
||||
|
||||
m_domainRestrictedCssRules.clear();
|
||||
m_documentRules.clear();
|
||||
}
|
||||
|
||||
void AdBlockMatcher::enabledChanged(bool enabled) {
|
||||
if (enabled) {
|
||||
update();
|
||||
}
|
||||
else {
|
||||
clear();
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 by Martin Rotter <rotter.martinos@gmail.com>
|
||||
// Copyright (C) 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 ADBLOCKMATCHER_H
|
||||
#define ADBLOCKMATCHER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <QUrl>
|
||||
#include <QVector>
|
||||
|
||||
#include "network-web/adblock/adblocksearchtree.h"
|
||||
|
||||
|
||||
class AdBlockManager;
|
||||
class AdBlockRule;
|
||||
|
||||
class AdBlockMatcher : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockMatcher(AdBlockManager *manager);
|
||||
virtual ~AdBlockMatcher();
|
||||
|
||||
const AdBlockRule *match(const QUrl &url, const QString &url_domain, const QString &url_string,
|
||||
const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const;
|
||||
|
||||
bool adBlockDisabledForUrl(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const;
|
||||
|
||||
public slots:
|
||||
void update();
|
||||
void clear();
|
||||
|
||||
private slots:
|
||||
void enabledChanged(bool enabled);
|
||||
|
||||
private:
|
||||
AdBlockManager *m_manager;
|
||||
|
||||
QVector<const AdBlockRule*> m_networkExceptionRules;
|
||||
QVector<const AdBlockRule*> m_networkBlockRules;
|
||||
QVector<const AdBlockRule*> m_domainRestrictedCssRules;
|
||||
QVector<const AdBlockRule*> m_documentRules;
|
||||
|
||||
AdBlockSearchTree m_networkBlockTree;
|
||||
AdBlockSearchTree m_networkExceptionTree;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKMATCHER_H
|
@ -1,638 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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/>.
|
||||
|
||||
/**
|
||||
* Copyright (c) 2009, Zsombor Gegesy <gzsombor@gmail.com>
|
||||
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "network-web/adblock/adblockrule.h"
|
||||
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/webfactory.h"
|
||||
#include "definitions/definitions.h"
|
||||
|
||||
#include <QUrl>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QNetworkRequest>
|
||||
#include <QWebEnginePage>
|
||||
|
||||
|
||||
AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscription)
|
||||
: m_subscription(subscription), m_type(StringContainsMatchRule), m_caseSensitivity(Qt::CaseInsensitive), m_isEnabled(true),
|
||||
m_isException(false), m_isInternalDisabled(false), m_regExp(NULL) {
|
||||
setFilter(filter);
|
||||
}
|
||||
|
||||
AdBlockRule::~AdBlockRule() {
|
||||
delete m_regExp;
|
||||
}
|
||||
|
||||
AdBlockRule *AdBlockRule::copy() const {
|
||||
AdBlockRule* rule = new AdBlockRule();
|
||||
|
||||
rule->m_subscription = m_subscription;
|
||||
rule->m_type = m_type;
|
||||
rule->m_options = m_options;
|
||||
rule->m_exceptions = m_exceptions;
|
||||
rule->m_filter = m_filter;
|
||||
rule->m_matchString = m_matchString;
|
||||
rule->m_caseSensitivity = m_caseSensitivity;
|
||||
rule->m_isEnabled = m_isEnabled;
|
||||
rule->m_isException = m_isException;
|
||||
rule->m_isInternalDisabled = m_isInternalDisabled;
|
||||
rule->m_allowedDomains = m_allowedDomains;
|
||||
rule->m_blockedDomains = m_blockedDomains;
|
||||
|
||||
if (m_regExp != NULL) {
|
||||
rule->m_regExp = new RegExp();
|
||||
rule->m_regExp->regExp = m_regExp->regExp;
|
||||
rule->m_regExp->matchers = m_regExp->matchers;
|
||||
}
|
||||
|
||||
return rule;
|
||||
}
|
||||
|
||||
AdBlockSubscription *AdBlockRule::subscription() const {
|
||||
return m_subscription;
|
||||
}
|
||||
|
||||
void AdBlockRule::setSubscription(AdBlockSubscription *subscription) {
|
||||
m_subscription = subscription;
|
||||
}
|
||||
|
||||
QString AdBlockRule::filter() const {
|
||||
return m_filter;
|
||||
}
|
||||
|
||||
void AdBlockRule::setFilter(const QString &filter) {
|
||||
m_filter = filter;
|
||||
parseFilter();
|
||||
}
|
||||
|
||||
bool AdBlockRule::isDocument() const {
|
||||
return hasOption(DocumentOption);
|
||||
}
|
||||
|
||||
bool AdBlockRule::isElemhide() const {
|
||||
return hasOption(ElementHideOption);
|
||||
}
|
||||
|
||||
bool AdBlockRule::isDomainRestricted() const {
|
||||
return hasOption(DomainRestrictedOption);
|
||||
}
|
||||
|
||||
bool AdBlockRule::isException() const {
|
||||
return m_isException;
|
||||
}
|
||||
|
||||
bool AdBlockRule::isComment() const {
|
||||
return m_filter.startsWith(QL1C('!'));
|
||||
}
|
||||
|
||||
bool AdBlockRule::isEnabled() const {
|
||||
return m_isEnabled;
|
||||
}
|
||||
|
||||
void AdBlockRule::setEnabled(bool enabled) {
|
||||
m_isEnabled = enabled;
|
||||
}
|
||||
|
||||
bool AdBlockRule::isSlow() const {
|
||||
return m_regExp != NULL;
|
||||
}
|
||||
|
||||
bool AdBlockRule::isInternalDisabled() const {
|
||||
return m_isInternalDisabled;
|
||||
}
|
||||
|
||||
bool AdBlockRule::urlMatch(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const {
|
||||
if (!hasOption(DocumentOption) && !hasOption(ElementHideOption)) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return networkMatch(url, url.host(), url.toEncoded(), referer, resource_type);
|
||||
}
|
||||
}
|
||||
|
||||
bool AdBlockRule::networkMatch(const QUrl &url, const QString &domain,
|
||||
const QString &encoded_url, const QString &referer,
|
||||
QWebEngineUrlRequestInfo::ResourceType resource_type) const {
|
||||
if (!m_isEnabled || m_isInternalDisabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool matched = false;
|
||||
|
||||
if (m_type == StringContainsMatchRule) {
|
||||
matched = encoded_url.contains(m_matchString, m_caseSensitivity);
|
||||
}
|
||||
else if (m_type == DomainMatchRule) {
|
||||
matched = isMatchingDomain(domain, m_matchString);
|
||||
}
|
||||
else if (m_type == StringEndsMatchRule) {
|
||||
matched = encoded_url.endsWith(m_matchString, m_caseSensitivity);
|
||||
}
|
||||
else if (m_type == RegExpMatchRule) {
|
||||
if (!isMatchingRegExpStrings(encoded_url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
matched = (m_regExp->regExp.indexIn(encoded_url) != -1);
|
||||
}
|
||||
|
||||
if (matched) {
|
||||
// Check domain restrictions.
|
||||
if (hasOption(DomainRestrictedOption) && !matchDomain(domain)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check third-party restriction.
|
||||
if (hasOption(ThirdPartyOption) && !matchThirdParty(referer, url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check object restrictions.
|
||||
if (hasOption(ObjectOption) && !matchObject(resource_type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check subdocument restriction.
|
||||
if (hasOption(SubdocumentOption)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check xmlhttprequest restriction
|
||||
if (hasOption(XMLHttpRequestOption)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check image restriction
|
||||
if (hasOption(ImageOption) && !matchImage(encoded_url)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return matched;
|
||||
}
|
||||
|
||||
bool AdBlockRule::matchDomain(const QString &domain) const {
|
||||
if (!m_isEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hasOption(DomainRestrictedOption)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_blockedDomains.isEmpty()) {
|
||||
foreach (const QString &d, m_allowedDomains) {
|
||||
if (isMatchingDomain(domain, d)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (m_allowedDomains.isEmpty()) {
|
||||
foreach (const QString &d, m_blockedDomains) {
|
||||
if (isMatchingDomain(domain, d)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
foreach (const QString &d, m_blockedDomains) {
|
||||
if (isMatchingDomain(domain, d)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (const QString &d, m_allowedDomains) {
|
||||
if (isMatchingDomain(domain, d)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockRule::matchThirdParty(const QString &referer, const QUrl &url) const {
|
||||
if (referer.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Third-party matching should be performed on second-level domains.
|
||||
const QString refererHost = WebFactory::instance()->toSecondLevelDomain(QUrl(referer));
|
||||
const QString host = WebFactory::instance()->toSecondLevelDomain(url);
|
||||
|
||||
bool match = refererHost != host;
|
||||
|
||||
return hasException(ThirdPartyOption) ? !match : match;
|
||||
}
|
||||
|
||||
bool AdBlockRule::matchObject(QWebEngineUrlRequestInfo::ResourceType type) const {
|
||||
bool match = type == QWebEngineUrlRequestInfo::ResourceTypeObject;
|
||||
|
||||
return hasException(ObjectOption) ? !match : match;
|
||||
}
|
||||
|
||||
bool AdBlockRule::matchImage(const QString &encoded_url) const {
|
||||
bool match = encoded_url.endsWith(QL1S(".png")) ||
|
||||
encoded_url.endsWith(QL1S(".jpg")) ||
|
||||
encoded_url.endsWith(QL1S(".gif")) ||
|
||||
encoded_url.endsWith(QL1S(".jpeg"));
|
||||
|
||||
return hasException(ImageOption) ? !match : match;
|
||||
}
|
||||
|
||||
void AdBlockRule::parseFilter() {
|
||||
QString parsed_line = m_filter;
|
||||
|
||||
// Empty rule or just comment.
|
||||
if (m_filter.trimmed().isEmpty() || m_filter.startsWith(QL1C('!'))) {
|
||||
// We want to differentiate rule disabled by user and rule disabled in subscription file
|
||||
// m_isInternalDisabled is also used when rule is disabled due to all options not being supported.
|
||||
m_isEnabled = false;
|
||||
m_isInternalDisabled = true;
|
||||
m_type = Invalid;
|
||||
return;
|
||||
}
|
||||
|
||||
// CSS Element hiding rule.
|
||||
if (parsed_line.contains(QL1S("##")) || parsed_line.contains(QL1S("#@#"))) {
|
||||
// Do not parse CSS rules.
|
||||
return;
|
||||
}
|
||||
|
||||
// Exception always starts with @@
|
||||
if (parsed_line.startsWith(QL1S("@@"))) {
|
||||
m_isException = true;
|
||||
parsed_line = parsed_line.mid(2);
|
||||
}
|
||||
|
||||
// Parse all options following $ char.
|
||||
int optionsIndex = parsed_line.indexOf(QL1C('$'));
|
||||
if (optionsIndex >= 0) {
|
||||
const QStringList options = parsed_line.mid(optionsIndex + 1).split(QL1C(','), QString::SkipEmptyParts);
|
||||
|
||||
int handledOptions = 0;
|
||||
foreach (const QString &option, options) {
|
||||
if (option.startsWith(QL1S("domain="))) {
|
||||
parseDomains(option.mid(7), QL1C('|'));
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option == QL1S("match-case")) {
|
||||
m_caseSensitivity = Qt::CaseSensitive;
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option.endsWith(QL1S("third-party"))) {
|
||||
setOption(ThirdPartyOption);
|
||||
setException(ThirdPartyOption, option.startsWith(QL1C('~')));
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option.endsWith(QL1S("object"))) {
|
||||
setOption(ObjectOption);
|
||||
setException(ObjectOption, option.startsWith(QL1C('~')));
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option.endsWith(QL1S("subdocument"))) {
|
||||
setOption(SubdocumentOption);
|
||||
setException(SubdocumentOption, option.startsWith(QL1C('~')));
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option.endsWith(QL1S("xmlhttprequest"))) {
|
||||
setOption(XMLHttpRequestOption);
|
||||
setException(XMLHttpRequestOption, option.startsWith(QL1C('~')));
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option.endsWith(QL1S("image"))) {
|
||||
setOption(ImageOption);
|
||||
setException(ImageOption, option.startsWith(QL1C('~')));
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option == QL1S("document") && m_isException) {
|
||||
setOption(DocumentOption);
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option == QL1S("elemhide") && m_isException) {
|
||||
setOption(ElementHideOption);
|
||||
++handledOptions;
|
||||
}
|
||||
else if (option == QL1S("collapse")) {
|
||||
// Hiding placeholders of blocked elements is enabled by default.
|
||||
++handledOptions;
|
||||
}
|
||||
}
|
||||
|
||||
// If we don't handle all options, it's safer to just disable this rule.
|
||||
if (handledOptions != options.count()) {
|
||||
m_isInternalDisabled = true;
|
||||
m_type = Invalid;
|
||||
return;
|
||||
}
|
||||
|
||||
parsed_line = parsed_line.left(optionsIndex);
|
||||
}
|
||||
|
||||
// Rule is classic regexp.
|
||||
if (parsed_line.startsWith(QL1C('/')) && parsed_line.endsWith(QL1C('/'))) {
|
||||
parsed_line = parsed_line.mid(1);
|
||||
parsed_line = parsed_line.left(parsed_line.size() - 1);
|
||||
|
||||
m_type = RegExpMatchRule;
|
||||
m_regExp = new RegExp();
|
||||
m_regExp->regExp = QRegExp(parsed_line, m_caseSensitivity);
|
||||
m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsed_line));
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove starting and ending wildcards (*).
|
||||
if (parsed_line.startsWith(QL1C('*'))) {
|
||||
parsed_line = parsed_line.mid(1);
|
||||
}
|
||||
|
||||
if (parsed_line.endsWith(QL1C('*'))) {
|
||||
parsed_line = parsed_line.left(parsed_line.size() - 1);
|
||||
}
|
||||
|
||||
// We can use fast string matching for domain here.
|
||||
if (filterIsOnlyDomain(parsed_line)) {
|
||||
parsed_line = parsed_line.mid(2);
|
||||
parsed_line = parsed_line.left(parsed_line.size() - 1);
|
||||
|
||||
m_type = DomainMatchRule;
|
||||
m_matchString = parsed_line;
|
||||
return;
|
||||
}
|
||||
|
||||
// If rule contains only | at end, we can also use string matching.
|
||||
if (filterIsOnlyEndsMatch(parsed_line)) {
|
||||
parsed_line = parsed_line.left(parsed_line.size() - 1);
|
||||
|
||||
m_type = StringEndsMatchRule;
|
||||
m_matchString = parsed_line;
|
||||
return;
|
||||
}
|
||||
|
||||
// If we still find a wildcard (*) or separator (^) or (|)
|
||||
// we must modify parsedLine to comply with QzRegExp.
|
||||
if (parsed_line.contains(QL1C('*')) ||
|
||||
parsed_line.contains(QL1C('^')) ||
|
||||
parsed_line.contains(QL1C('|'))
|
||||
) {
|
||||
m_type = RegExpMatchRule;
|
||||
m_regExp = new RegExp;
|
||||
m_regExp->regExp = QRegExp(createRegExpFromFilter(parsed_line), m_caseSensitivity);
|
||||
m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsed_line));
|
||||
return;
|
||||
}
|
||||
|
||||
// We haven't found anything that needs use of regexp, yay!
|
||||
m_type = StringContainsMatchRule;
|
||||
m_matchString = parsed_line;
|
||||
}
|
||||
|
||||
void AdBlockRule::parseDomains(const QString &domains, const QChar &separator) {
|
||||
QStringList domains_list = domains.split(separator, QString::SkipEmptyParts);
|
||||
|
||||
foreach (const QString domain, domains_list) {
|
||||
if (!domain.isEmpty()) {
|
||||
if (domain.startsWith(QL1C('~'))) {
|
||||
m_blockedDomains.append(domain.mid(1));
|
||||
}
|
||||
else {
|
||||
m_allowedDomains.append(domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_blockedDomains.isEmpty() || !m_allowedDomains.isEmpty()) {
|
||||
setOption(DomainRestrictedOption);
|
||||
}
|
||||
}
|
||||
|
||||
bool AdBlockRule::filterIsOnlyDomain(const QString &filter) const {
|
||||
if (!filter.endsWith(QL1C('^')) || !filter.startsWith(QL1S("||"))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < filter.size(); i++) {
|
||||
switch (filter.at(i).toLatin1()) {
|
||||
case '/':
|
||||
case ':':
|
||||
case '?':
|
||||
case '=':
|
||||
case '&':
|
||||
case '*':
|
||||
return false;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AdBlockRule::filterIsOnlyEndsMatch(const QString &filter) const {
|
||||
for (int i = 0; i < filter.size(); ++i) {
|
||||
switch (filter.at(i).toLatin1()) {
|
||||
case '^':
|
||||
case '*':
|
||||
return false;
|
||||
|
||||
case '|':
|
||||
return i == filter.size() - 1;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool wordCharacter(const QChar &c) {
|
||||
return c.isLetterOrNumber() || c.isMark() || c == QL1C('_');
|
||||
}
|
||||
|
||||
QString AdBlockRule::createRegExpFromFilter(const QString &filter) const {
|
||||
QString parsed;
|
||||
parsed.reserve(filter.size());
|
||||
|
||||
bool hadWildcard = false; // Filter multiple wildcards.
|
||||
|
||||
for (int i = 0; i < filter.size(); i++) {
|
||||
const QChar c = filter.at(i);
|
||||
|
||||
switch (c.toLatin1()) {
|
||||
case '^':
|
||||
parsed.append(QL1S("(?:[^\\w\\d\\-.%]|$)"));
|
||||
break;
|
||||
|
||||
case '*':
|
||||
if (!hadWildcard)
|
||||
parsed.append(QL1S(".*"));
|
||||
break;
|
||||
|
||||
case '|':
|
||||
if (i == 0) {
|
||||
if (filter.size() > 1 && filter.at(1) == QL1C('|')) {
|
||||
parsed.append(QL1S("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?"));
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
parsed.append('^');
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (i == filter.size() - 1) {
|
||||
parsed.append(QL1C('$'));
|
||||
break;
|
||||
}
|
||||
// Fall through.
|
||||
|
||||
default:
|
||||
if (!wordCharacter(c)) {
|
||||
parsed.append(QL1C('\\') + c);
|
||||
}
|
||||
else {
|
||||
parsed.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
hadWildcard = c == QL1C('*');
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
QList<QStringMatcher> AdBlockRule::createStringMatchers(const QStringList &filters) const {
|
||||
QList<QStringMatcher> matchers;
|
||||
matchers.reserve(filters.size());
|
||||
|
||||
foreach (const QString &filter, filters) {
|
||||
matchers.append(QStringMatcher(filter, m_caseSensitivity));
|
||||
}
|
||||
|
||||
return matchers;
|
||||
}
|
||||
|
||||
bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &pattern) const {
|
||||
if (pattern == domain) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!domain.endsWith(pattern)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = domain.indexOf(pattern);
|
||||
|
||||
return index > 0 && domain[index - 1] == QLatin1Char('.');
|
||||
}
|
||||
|
||||
bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const {
|
||||
Q_ASSERT(m_regExp);
|
||||
|
||||
foreach (const QStringMatcher &matcher, m_regExp->matchers) {
|
||||
if (matcher.indexIn(url) == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Split regexp filter into strings that can be used with QString::contains.
|
||||
// Don't use parts that contains only 1 char and duplicated parts.
|
||||
QStringList AdBlockRule::parseRegExpFilter(const QString &filter) const {
|
||||
QStringList list;
|
||||
int start_pos = -1;
|
||||
|
||||
for (int i = 0; i < filter.size(); i++) {
|
||||
const QChar c = filter.at(i);
|
||||
|
||||
// Meta characters in AdBlock rules are "| * ^".
|
||||
if (c == QL1C('|') || c == QL1C('*') || c == QL1C('^')) {
|
||||
const QString sub = filter.mid(start_pos, i - start_pos);
|
||||
|
||||
if (sub.size() > 1) {
|
||||
list.append(sub);
|
||||
}
|
||||
|
||||
start_pos = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
const QString sub = filter.mid(start_pos);
|
||||
|
||||
if (sub.size() > 1) {
|
||||
list.append(sub);
|
||||
}
|
||||
|
||||
list.removeDuplicates();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
bool AdBlockRule::hasOption(const AdBlockRule::RuleOption &opt) const {
|
||||
return (m_options & opt);
|
||||
}
|
||||
|
||||
bool AdBlockRule::hasException(const AdBlockRule::RuleOption &opt) const {
|
||||
return (m_exceptions & opt);
|
||||
}
|
||||
|
||||
void AdBlockRule::setOption(const AdBlockRule::RuleOption &opt) {
|
||||
m_options |= opt;
|
||||
}
|
||||
|
||||
void AdBlockRule::setException(const AdBlockRule::RuleOption &opt, bool on) {
|
||||
if (on) {
|
||||
m_exceptions |= opt;
|
||||
}
|
||||
}
|
@ -1,176 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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/>.
|
||||
|
||||
/**
|
||||
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef ADBLOCKRULE_H
|
||||
#define ADBLOCKRULE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
#include <QWebEngineUrlRequestInfo>
|
||||
|
||||
class QNetworkRequest;
|
||||
class QUrl;
|
||||
|
||||
class AdBlockSubscription;
|
||||
|
||||
class AdBlockRule {
|
||||
friend class AdBlockMatcher;
|
||||
friend class AdBlockSearchTree;
|
||||
friend class AdBlockSubscription;
|
||||
|
||||
public:
|
||||
explicit AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = NULL);
|
||||
virtual ~AdBlockRule();
|
||||
|
||||
AdBlockRule *copy() const;
|
||||
|
||||
AdBlockSubscription *subscription() const;
|
||||
void setSubscription(AdBlockSubscription *subscription);
|
||||
|
||||
QString filter() const;
|
||||
void setFilter(const QString &filter);
|
||||
|
||||
bool isDocument() const;
|
||||
bool isElemhide() const;
|
||||
|
||||
bool isDomainRestricted() const;
|
||||
bool isException() const;
|
||||
|
||||
bool isComment() const;
|
||||
bool isEnabled() const;
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
bool isSlow() const;
|
||||
bool isInternalDisabled() const;
|
||||
|
||||
bool urlMatch(const QUrl &url, const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const;
|
||||
bool networkMatch(const QUrl &url, const QString &domain, const QString &encoded_url,
|
||||
const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const;
|
||||
|
||||
bool matchDomain(const QString &domain) const;
|
||||
bool matchThirdParty(const QString &referer, const QUrl &url) const;
|
||||
bool matchObject(QWebEngineUrlRequestInfo::ResourceType type) const;
|
||||
bool matchImage(const QString &encoded_url) const;
|
||||
|
||||
protected:
|
||||
bool isMatchingDomain(const QString &domain, const QString &pattern) const;
|
||||
bool isMatchingRegExpStrings(const QString &url) const;
|
||||
QStringList parseRegExpFilter(const QString &filter) const;
|
||||
|
||||
private:
|
||||
enum RuleType {
|
||||
DomainMatchRule = 1,
|
||||
RegExpMatchRule = 2,
|
||||
StringEndsMatchRule = 3,
|
||||
StringContainsMatchRule = 4,
|
||||
Invalid = 5
|
||||
};
|
||||
|
||||
enum RuleOption {
|
||||
DomainRestrictedOption = 1,
|
||||
ThirdPartyOption = 2,
|
||||
ObjectOption = 4,
|
||||
SubdocumentOption = 8,
|
||||
XMLHttpRequestOption = 16,
|
||||
ImageOption = 32,
|
||||
|
||||
// Exception only options.
|
||||
DocumentOption = 64,
|
||||
ElementHideOption = 128
|
||||
};
|
||||
|
||||
Q_DECLARE_FLAGS(RuleOptions, RuleOption)
|
||||
|
||||
inline bool hasOption(const RuleOption &opt) const;
|
||||
inline bool hasException(const RuleOption &opt) const;
|
||||
|
||||
inline void setOption(const RuleOption &opt);
|
||||
inline void setException(const RuleOption &opt, bool on);
|
||||
|
||||
void parseFilter();
|
||||
void parseDomains(const QString &domains, const QChar &separator);
|
||||
bool filterIsOnlyDomain(const QString &filter) const;
|
||||
bool filterIsOnlyEndsMatch(const QString &filter) const;
|
||||
QString createRegExpFromFilter(const QString &filter) const;
|
||||
QList<QStringMatcher> createStringMatchers(const QStringList &filters) const;
|
||||
|
||||
AdBlockSubscription *m_subscription;
|
||||
|
||||
RuleType m_type;
|
||||
RuleOptions m_options;
|
||||
RuleOptions m_exceptions;
|
||||
|
||||
// Original rule filter.
|
||||
QString m_filter;
|
||||
|
||||
// Parsed rule for string matching (CSS Selector for CSS rules).
|
||||
QString m_matchString;
|
||||
|
||||
// Case sensitivity for string matching.
|
||||
Qt::CaseSensitivity m_caseSensitivity;
|
||||
|
||||
bool m_isEnabled;
|
||||
bool m_isException;
|
||||
bool m_isInternalDisabled;
|
||||
|
||||
QStringList m_allowedDomains;
|
||||
QStringList m_blockedDomains;
|
||||
|
||||
struct RegExp {
|
||||
public:
|
||||
explicit RegExp() {
|
||||
}
|
||||
|
||||
QRegExp regExp;
|
||||
QList<QStringMatcher> matchers;
|
||||
};
|
||||
|
||||
// Use dynamic allocation to save memory
|
||||
RegExp *m_regExp;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKRULE_H
|
||||
|
@ -1,140 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 "network-web/adblock/adblocksearchtree.h"
|
||||
#include "network-web/adblock/adblockrule.h"
|
||||
|
||||
|
||||
AdBlockSearchTree::AdBlockSearchTree() : m_root(new Node()) {
|
||||
}
|
||||
|
||||
AdBlockSearchTree::~AdBlockSearchTree() {
|
||||
deleteNode(m_root);
|
||||
}
|
||||
|
||||
void AdBlockSearchTree::clear() {
|
||||
deleteNode(m_root);
|
||||
m_root = new Node();
|
||||
}
|
||||
|
||||
bool AdBlockSearchTree::add(const AdBlockRule *rule) {
|
||||
if (rule->m_type != AdBlockRule::StringContainsMatchRule) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const QString filter = rule->m_matchString;
|
||||
const int len = filter.size();
|
||||
|
||||
if (len <= 0) {
|
||||
qWarning("Inserting rule with filter len <= 0!");
|
||||
return false;
|
||||
}
|
||||
|
||||
Node *node = m_root;
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
const QChar c = filter.at(i);
|
||||
|
||||
if (!node->children.contains(c)) {
|
||||
Node *n = new Node();
|
||||
n->c = c;
|
||||
|
||||
node->children[c] = n;
|
||||
}
|
||||
|
||||
node = node->children[c];
|
||||
}
|
||||
|
||||
node->rule = rule;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSearchTree::find(const QUrl &url, const QString &domain,
|
||||
const QString &url_string, const QString &referer,
|
||||
QWebEngineUrlRequestInfo::ResourceType resource_type) const {
|
||||
const int len = url_string.size();
|
||||
|
||||
if (len <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const QChar *string = url_string.constData();
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
const AdBlockRule *rule = prefixSearch(url, domain, url_string, string++, referer, resource_type, len - i);
|
||||
|
||||
if (rule != NULL) {
|
||||
return rule;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSearchTree::prefixSearch(const QUrl &url, const QString &domain,
|
||||
const QString &url_string, const QChar* string,
|
||||
const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type,
|
||||
int len) const {
|
||||
if (len <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QChar c = string[0];
|
||||
|
||||
if (!m_root->children.contains(c)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Node *node = m_root->children[c];
|
||||
|
||||
for (int i = 1; i < len; i++) {
|
||||
const QChar c = (++string)[0];
|
||||
|
||||
if (node->rule && node->rule->networkMatch(url, domain, url_string, referer, resource_type)) {
|
||||
return node->rule;
|
||||
}
|
||||
|
||||
if (!node->children.contains(c)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node = node->children[c];
|
||||
}
|
||||
|
||||
if (node->rule && node->rule->networkMatch(url, domain, url_string, referer, resource_type)) {
|
||||
return node->rule;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void AdBlockSearchTree::deleteNode(Node *node) {
|
||||
if (node == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
QHashIterator<QChar,Node*> i(node->children);
|
||||
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
deleteNode(i.value());
|
||||
}
|
||||
|
||||
delete node;
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 ADBLOCKSEARCHTREE_H
|
||||
#define ADBLOCKSEARCHTREE_H
|
||||
|
||||
#include <QChar>
|
||||
#include <QHash>
|
||||
#include <QUrl>
|
||||
#include <QWebEngineUrlRequestInfo>
|
||||
|
||||
|
||||
class QNetworkRequest;
|
||||
class AdBlockRule;
|
||||
|
||||
class AdBlockSearchTree {
|
||||
public:
|
||||
explicit AdBlockSearchTree();
|
||||
virtual ~AdBlockSearchTree();
|
||||
|
||||
void clear();
|
||||
|
||||
bool add(const AdBlockRule *rule);
|
||||
const AdBlockRule *find(const QUrl &url, const QString &domain, const QString &url_string,
|
||||
const QString &referer, QWebEngineUrlRequestInfo::ResourceType resource_type) const;
|
||||
|
||||
private:
|
||||
struct Node {
|
||||
public:
|
||||
QChar c;
|
||||
const AdBlockRule *rule;
|
||||
QHash<QChar,Node*> children;
|
||||
|
||||
Node() : c(0) , rule(0) {
|
||||
}
|
||||
};
|
||||
|
||||
void deleteNode(Node *node);
|
||||
const AdBlockRule *prefixSearch(const QUrl &url, const QString &domain,
|
||||
const QString &url_string, const QChar* string, const QString &referer,
|
||||
QWebEngineUrlRequestInfo::ResourceType resource_type, int len) const;
|
||||
|
||||
Node *m_root;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKSEARCHTREE_H
|
@ -1,393 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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/>.
|
||||
|
||||
/**
|
||||
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "network-web/adblock/adblocksearchtree.h"
|
||||
#include "network-web/downloader.h"
|
||||
#include "miscellaneous/iofactory.h"
|
||||
#include "miscellaneous/application.h"
|
||||
#include "exceptions/applicationexception.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QTimer>
|
||||
#include <QNetworkReply>
|
||||
#include <QDir>
|
||||
#include <QWebEnginePage>
|
||||
|
||||
|
||||
AdBlockSubscription::AdBlockSubscription(const QString &title, QObject *parent)
|
||||
: QObject(parent), m_title(title), m_downloadingSubscription(false), m_updated(false) {
|
||||
}
|
||||
|
||||
QString AdBlockSubscription::title() const {
|
||||
return m_title;
|
||||
}
|
||||
|
||||
void AdBlockSubscription::setTitle(const QString &title) {
|
||||
m_title = title;
|
||||
}
|
||||
|
||||
QString AdBlockSubscription::filePath() const {
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
void AdBlockSubscription::setFilePath(const QString &path) {
|
||||
m_filePath = path;
|
||||
}
|
||||
|
||||
QUrl AdBlockSubscription::url() const {
|
||||
return m_url;
|
||||
}
|
||||
|
||||
void AdBlockSubscription::setUrl(const QUrl &url) {
|
||||
m_url = url;
|
||||
}
|
||||
|
||||
void AdBlockSubscription::loadSubscription(const QStringList &disabled_rules) {
|
||||
QFile file(m_filePath);
|
||||
|
||||
if (!file.exists()) {
|
||||
qWarning("Cannot load subscription '%s'. Requesting its update.", qPrintable(title()));
|
||||
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.open(QFile::ReadOnly)) {
|
||||
qWarning("Unable to open subscription file '%s' for reading.", qPrintable(QDir::toNativeSeparators(m_filePath)));
|
||||
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
||||
return;
|
||||
}
|
||||
|
||||
QTextStream textStream(&file);
|
||||
textStream.setCodec("UTF-8");
|
||||
|
||||
// Header is on 3rd line.
|
||||
textStream.readLine(1024);
|
||||
textStream.readLine(1024);
|
||||
QString header = textStream.readLine(1024);
|
||||
|
||||
if (!header.startsWith(QL1S("[Adblock")) || m_title.isEmpty()) {
|
||||
qWarning("Invalid format of subscription file '%s'.", qPrintable(QDir::toNativeSeparators(m_filePath)));
|
||||
file.close();
|
||||
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
||||
return;
|
||||
}
|
||||
|
||||
m_rules.clear();
|
||||
|
||||
while (!textStream.atEnd()) {
|
||||
AdBlockRule *rule = new AdBlockRule(textStream.readLine(), this);
|
||||
|
||||
if (disabled_rules.contains(rule->filter())) {
|
||||
rule->setEnabled(false);
|
||||
}
|
||||
|
||||
m_rules.append(rule);
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
// Initial update.
|
||||
if (m_rules.isEmpty() && !m_updated) {
|
||||
QTimer::singleShot(0, this, SLOT(updateSubscription()));
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockSubscription::saveSubscription() {
|
||||
}
|
||||
|
||||
void AdBlockSubscription::updateSubscription() {
|
||||
if (m_downloadingSubscription || !m_url.isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_downloadingSubscription = true;
|
||||
|
||||
Downloader *downloader = new Downloader();
|
||||
|
||||
connect(downloader, SIGNAL(completed(QNetworkReply::NetworkError,QByteArray)), this, SLOT(subscriptionDownloaded()));
|
||||
downloader->downloadFile(m_url.toString());
|
||||
}
|
||||
|
||||
void AdBlockSubscription::subscriptionDownloaded() {
|
||||
Downloader *downloader = qobject_cast<Downloader*>(sender());
|
||||
|
||||
if (downloader == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool error = false;
|
||||
QByteArray response = QString::fromUtf8(downloader->lastOutputData()).toUtf8();
|
||||
|
||||
if (response.startsWith(' ')) {
|
||||
// Deal with " [Adblock".
|
||||
response = response.remove(0, 1);
|
||||
}
|
||||
|
||||
if (downloader->lastOutputError() != QNetworkReply::NoError ||
|
||||
!response.startsWith(QByteArray("[Adblock")) ||
|
||||
!saveDownloadedData(response)) {
|
||||
error = true;
|
||||
}
|
||||
|
||||
downloader->deleteLater();
|
||||
|
||||
if (error) {
|
||||
emit subscriptionError(tr("Cannot load subscription!"));
|
||||
}
|
||||
else {
|
||||
loadSubscription(AdBlockManager::instance()->disabledRules());
|
||||
|
||||
emit subscriptionUpdated();
|
||||
emit subscriptionChanged();
|
||||
}
|
||||
|
||||
m_downloadingSubscription = false;
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) {
|
||||
QFile file(m_filePath);
|
||||
|
||||
if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
|
||||
qWarning("Unable to open subscription file '%s' for writting.", qPrintable(QDir::toNativeSeparators(m_filePath)));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write subscription header
|
||||
file.write(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8());
|
||||
|
||||
if (AdBlockManager::instance()->useLimitedEasyList() && m_url == QUrl(ADBLOCK_EASYLIST_URL)) {
|
||||
// Third-party advertisers rules are with start domain (||) placeholder which needs regexps
|
||||
// So we are ignoring it for keeping good performance
|
||||
// But we will use whitelist rules at the end of list
|
||||
|
||||
QByteArray part1 = data.left(data.indexOf(QL1S("!-----------------------------Third-party adverts-----------------------------!")));
|
||||
QByteArray part2 = data.mid(data.indexOf(QL1S("!---------------------------------Whitelists----------------------------------!")));
|
||||
|
||||
file.write(part1);
|
||||
file.write(part2);
|
||||
}
|
||||
else {
|
||||
file.write(data);
|
||||
}
|
||||
|
||||
file.flush();
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSubscription::rule(int offset) const{
|
||||
if (offset >= 0 && m_rules.size() > offset) {
|
||||
return m_rules[offset];
|
||||
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
QVector<AdBlockRule*> AdBlockSubscription::allRules() const {
|
||||
return m_rules;
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSubscription::enableRule(int offset) {
|
||||
if (offset >= 0 && m_rules.size() > offset) {
|
||||
AdBlockRule *rule = m_rules[offset];
|
||||
rule->setEnabled(true);
|
||||
AdBlockManager::instance()->removeDisabledRule(rule->filter());
|
||||
|
||||
emit subscriptionChanged();
|
||||
|
||||
return rule;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSubscription::disableRule(int offset) {
|
||||
if (offset >= 0 && m_rules.size() > offset) {
|
||||
AdBlockRule *rule = m_rules[offset];
|
||||
rule->setEnabled(false);
|
||||
AdBlockManager::instance()->addDisabledRule(rule->filter());
|
||||
|
||||
emit subscriptionChanged();
|
||||
|
||||
return rule;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::canEditRules() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::canBeRemoved() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
int AdBlockSubscription::addRule(AdBlockRule *rule) {
|
||||
Q_UNUSED(rule)
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool AdBlockSubscription::removeRule(int offset) {
|
||||
Q_UNUSED(offset)
|
||||
return false;
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockSubscription::replaceRule(AdBlockRule *rule, int offset) {
|
||||
Q_UNUSED(rule)
|
||||
Q_UNUSED(offset)
|
||||
return 0;
|
||||
}
|
||||
|
||||
AdBlockSubscription::~AdBlockSubscription() {
|
||||
qDeleteAll(m_rules);
|
||||
}
|
||||
|
||||
AdBlockCustomList::AdBlockCustomList(QObject *parent) : AdBlockSubscription(tr("Custom rules"), parent) {
|
||||
setTitle(tr("Custom rules"));
|
||||
setFilePath(AdBlockManager::baseSubscriptionDirectory() + QDir::separator() + ADBLOCK_CUSTOM_LIST_FILENAME);
|
||||
}
|
||||
|
||||
AdBlockCustomList::~AdBlockCustomList() {
|
||||
}
|
||||
|
||||
void AdBlockCustomList::saveSubscription() {
|
||||
QFile file(m_filePath);
|
||||
|
||||
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
||||
qWarning("Unable to open custom subscription file '%s' for writting.", qPrintable(QDir::toNativeSeparators(m_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) {
|
||||
textStream << rule->filter() << endl;
|
||||
}
|
||||
|
||||
file.flush();
|
||||
file.close();
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::canEditRules() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::canBeRemoved() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::containsFilter(const QString &filter) const {
|
||||
foreach (const AdBlockRule *rule, m_rules) {
|
||||
if (rule->filter() == filter) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::removeFilter(const QString &filter) {
|
||||
for (int i = 0; i < m_rules.size(); i++) {
|
||||
const AdBlockRule *rule = m_rules.at(i);
|
||||
|
||||
if (rule->filter() == filter) {
|
||||
return removeRule(i);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int AdBlockCustomList::addRule(AdBlockRule *rule) {
|
||||
m_rules.append(rule);
|
||||
emit subscriptionChanged();
|
||||
|
||||
return m_rules.size() - 1;
|
||||
}
|
||||
|
||||
bool AdBlockCustomList::removeRule(int offset) {
|
||||
if (offset >= 0 && m_rules.size() > offset) {
|
||||
AdBlockRule *rule = m_rules.at(offset);
|
||||
const QString filter = rule->filter();
|
||||
|
||||
m_rules.remove(offset);
|
||||
emit subscriptionChanged();
|
||||
|
||||
AdBlockManager::instance()->removeDisabledRule(filter);
|
||||
delete rule;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const AdBlockRule *AdBlockCustomList::replaceRule(AdBlockRule *rule, int offset) {
|
||||
if (offset >= 0 && m_rules.size() > offset) {
|
||||
AdBlockRule *oldRule = m_rules.at(offset);
|
||||
m_rules[offset] = rule;
|
||||
emit subscriptionChanged();
|
||||
|
||||
delete oldRule;
|
||||
return m_rules[offset];
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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/>.
|
||||
|
||||
/**
|
||||
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef ADBLOCKSUBSCRIPTION_H
|
||||
#define ADBLOCKSUBSCRIPTION_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <QVector>
|
||||
#include <QUrl>
|
||||
|
||||
#include "network-web/adblock/adblockrule.h"
|
||||
#include "network-web/adblock/adblocksearchtree.h"
|
||||
|
||||
|
||||
class QNetworkRequest;
|
||||
class QNetworkReply;
|
||||
class QUrl;
|
||||
class FollowRedirectReply;
|
||||
|
||||
class AdBlockSubscription : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockSubscription(const QString &title, QObject *parent = 0);
|
||||
virtual ~AdBlockSubscription();
|
||||
|
||||
QString title() const;
|
||||
void setTitle(const QString &title);
|
||||
|
||||
QString filePath() const;
|
||||
void setFilePath(const QString &path);
|
||||
|
||||
QUrl url() const;
|
||||
void setUrl(const QUrl &url);
|
||||
|
||||
virtual void loadSubscription(const QStringList &disabled_rules);
|
||||
virtual void saveSubscription();
|
||||
|
||||
const AdBlockRule *rule(int offset) const;
|
||||
QVector<AdBlockRule*> allRules() const;
|
||||
|
||||
const AdBlockRule *enableRule(int offset);
|
||||
const AdBlockRule *disableRule(int offset);
|
||||
|
||||
virtual bool canEditRules() const;
|
||||
virtual bool canBeRemoved() const;
|
||||
|
||||
virtual int addRule(AdBlockRule *rule);
|
||||
virtual bool removeRule(int offset);
|
||||
virtual const AdBlockRule *replaceRule(AdBlockRule *rule, int offset);
|
||||
|
||||
public slots:
|
||||
void updateSubscription();
|
||||
|
||||
signals:
|
||||
void subscriptionChanged();
|
||||
void subscriptionUpdated();
|
||||
void subscriptionError(const QString &message);
|
||||
|
||||
protected slots:
|
||||
void subscriptionDownloaded();
|
||||
|
||||
protected:
|
||||
virtual bool saveDownloadedData(const QByteArray &data);
|
||||
|
||||
QVector<AdBlockRule*> m_rules;
|
||||
QString m_title;
|
||||
QString m_filePath;
|
||||
|
||||
private:
|
||||
bool m_downloadingSubscription;
|
||||
QUrl m_url;
|
||||
bool m_updated;
|
||||
};
|
||||
|
||||
class AdBlockCustomList : public AdBlockSubscription {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockCustomList(QObject* parent = 0);
|
||||
virtual ~AdBlockCustomList();
|
||||
|
||||
void saveSubscription();
|
||||
|
||||
bool canEditRules() const;
|
||||
bool canBeRemoved() const;
|
||||
|
||||
bool containsFilter(const QString &filter) const;
|
||||
bool removeFilter(const QString &filter);
|
||||
|
||||
int addRule(AdBlockRule *rule);
|
||||
bool removeRule(int offset);
|
||||
const AdBlockRule *replaceRule(AdBlockRule *rule, int offset);
|
||||
};
|
||||
|
||||
#endif // ADBLOCKSUBSCRIPTION_H
|
||||
|
@ -1,337 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 "network-web/adblock/adblocktreewidget.h"
|
||||
|
||||
#include "network-web/adblock/adblocksubscription.h"
|
||||
#include "miscellaneous/application.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QKeyEvent>
|
||||
#include <QClipboard>
|
||||
#include <QInputDialog>
|
||||
|
||||
|
||||
AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription *subscription, QWidget *parent)
|
||||
: QTreeWidget(parent), m_subscription(subscription), m_topItem(NULL),
|
||||
m_itemChangingBlock(false), m_refreshAllItemsNeeded(true) {
|
||||
setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
setHeaderHidden(true);
|
||||
setAlternatingRowColors(true);
|
||||
setFrameStyle(QFrame::NoFrame);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::showRule(const AdBlockRule *rule) {
|
||||
if (m_topItem == NULL && rule) {
|
||||
// Dialog is not loaded yet. Mark rule for late loading.
|
||||
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);
|
||||
|
||||
setCurrentItem(item);
|
||||
scrollToItem(item, QAbstractItemView::PositionAtCenter);
|
||||
}
|
||||
|
||||
m_ruleToBeSelected.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::contextMenuRequested(const QPoint &pos) {
|
||||
if (!m_subscription->canEditRules()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTreeWidgetItem *item = itemAt(pos);
|
||||
|
||||
if (item != NULL) {
|
||||
QMenu menu;
|
||||
menu.addAction(tr("Add rule"), this, SLOT(addRule()));
|
||||
menu.addSeparator();
|
||||
|
||||
QAction *delete_action = menu.addAction(tr("Remove rule"), this, SLOT(removeRule()));
|
||||
|
||||
if (item->parent() == NULL) {
|
||||
delete_action->setDisabled(true);
|
||||
}
|
||||
|
||||
menu.exec(viewport()->mapToGlobal(pos));
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::itemChanged(QTreeWidgetItem *item) {
|
||||
m_refreshAllItemsNeeded = true;
|
||||
|
||||
if (item == NULL || m_itemChangingBlock) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
|
||||
int offset = item->data(0, Qt::UserRole + 10).toInt();
|
||||
const AdBlockRule *old_rle = m_subscription->rule(offset);
|
||||
|
||||
if (item->checkState(0) == Qt::Unchecked && old_rle->isEnabled()) {
|
||||
// Disable rule.
|
||||
const AdBlockRule *rule = m_subscription->disableRule(offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
else if (item->checkState(0) == Qt::Checked && !old_rle->isEnabled()) {
|
||||
// Enable rule
|
||||
const AdBlockRule *rule = m_subscription->enableRule(offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
else if (m_subscription->canEditRules()) {
|
||||
// Custom rule has been changed
|
||||
AdBlockRule *new_rule = new AdBlockRule(item->text(0), m_subscription);
|
||||
const AdBlockRule *rule = m_subscription->replaceRule(new_rule, offset);
|
||||
|
||||
adjustItemFeatures(item, rule);
|
||||
}
|
||||
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::copyFilter() {
|
||||
QTreeWidgetItem *item = currentItem();
|
||||
|
||||
if (item != NULL) {
|
||||
Application::clipboard()->setText(item->text(0));
|
||||
}
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::addRule() {
|
||||
if (!m_subscription->canEditRules()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString new_rule = QInputDialog::getText(this, tr("Add rule"), tr("Please write your rule here"));
|
||||
if (new_rule.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AdBlockRule *rule = new AdBlockRule(new_rule, m_subscription);
|
||||
int offset = m_subscription->addRule(rule);
|
||||
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem();
|
||||
item->setText(0, new_rule);
|
||||
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::removeRule() {
|
||||
QTreeWidgetItem *item = currentItem();
|
||||
if (item == NULL || !m_subscription->canEditRules() || item == m_topItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
int offset = item->data(0, Qt::UserRole + 10).toInt();
|
||||
m_subscription->removeRule(offset);
|
||||
delete item;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::subscriptionUpdated() {
|
||||
refresh();
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->setText(0, tr("%1 (recently updated)").arg(m_subscription->title()));
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::subscriptionError(const QString &message) {
|
||||
refresh();
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
m_topItem->setText(0, tr("%1 (error: %2)").arg(m_subscription->title(), message));
|
||||
m_itemChangingBlock = false;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem *item, const AdBlockRule *rule) {
|
||||
if (!rule->isEnabled()) {
|
||||
QFont font;
|
||||
font.setItalic(true);
|
||||
item->setForeground(0, QColor(Qt::gray));
|
||||
|
||||
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 {
|
||||
item->setForeground(0, QColor());
|
||||
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();
|
||||
}
|
||||
|
||||
QTreeWidget::keyPressEvent(event);
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::refresh() {
|
||||
// Disable GUI editing for parent.
|
||||
emit refreshStatusChanged(true);
|
||||
setUpdatesEnabled(false);
|
||||
|
||||
m_itemChangingBlock = true;
|
||||
clear();
|
||||
|
||||
QFont bold_font;
|
||||
bold_font.setBold(true);
|
||||
|
||||
m_topItem = new QTreeWidgetItem(this);
|
||||
m_topItem->setText(0, m_subscription->title());
|
||||
m_topItem->setFont(0, bold_font);
|
||||
m_topItem->setExpanded(true);
|
||||
addTopLevelItem(m_topItem);
|
||||
|
||||
const QVector<AdBlockRule*> &all_rules = m_subscription->allRules();
|
||||
int index = 0;
|
||||
|
||||
foreach (const AdBlockRule *rule, all_rules) {
|
||||
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 (index % 100 == 0) {
|
||||
qApp->processEvents();
|
||||
}
|
||||
}
|
||||
|
||||
showRule(0);
|
||||
m_itemChangingBlock = false;
|
||||
setUpdatesEnabled(true);
|
||||
emit refreshStatusChanged(false);
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::clear() {
|
||||
QTreeWidget::clear();
|
||||
m_allTreeItems.clear();
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::addTopLevelItem(QTreeWidgetItem *item) {
|
||||
m_allTreeItems.append(item);
|
||||
QTreeWidget::addTopLevelItem(item);
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::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*> AdBlockTreeWidget::allItems() {
|
||||
if (m_refreshAllItemsNeeded) {
|
||||
m_allTreeItems.clear();
|
||||
iterateAllItems(0);
|
||||
m_refreshAllItemsNeeded = false;
|
||||
}
|
||||
|
||||
return m_allTreeItems;
|
||||
}
|
||||
|
||||
void AdBlockTreeWidget::filterString(const QString &string) {
|
||||
QList<QTreeWidgetItem*> all_items = allItems();
|
||||
QList<QTreeWidgetItem*> parents;
|
||||
bool string_empty = string.isEmpty();
|
||||
|
||||
foreach (QTreeWidgetItem *item, all_items) {
|
||||
bool contains_string = string_empty || item->text(0).contains(string, Qt::CaseInsensitive);
|
||||
|
||||
if (contains_string) {
|
||||
item->setHidden(false);
|
||||
|
||||
if (item->parent() != NULL) {
|
||||
if (!parents.contains(item->parent())) {
|
||||
parents << item->parent();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
item->setHidden(true);
|
||||
|
||||
if (item->parent() != NULL) {
|
||||
item->parent()->setHidden(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < parents.size(); i++) {
|
||||
QTreeWidgetItem *parentItem = parents.at(i);
|
||||
parentItem->setHidden(false);
|
||||
parentItem->setExpanded(true);
|
||||
|
||||
if (parentItem->parent() != NULL && !parents.contains(parentItem->parent())) {
|
||||
parents << parentItem->parent();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2014-2015 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 ADBLOCKTREEWIDGET_H
|
||||
#define ADBLOCKTREEWIDGET_H
|
||||
|
||||
#include <QTreeWidget>
|
||||
|
||||
|
||||
class AdBlockSubscription;
|
||||
class AdBlockRule;
|
||||
|
||||
class AdBlockTreeWidget : public QTreeWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent = 0);
|
||||
|
||||
AdBlockSubscription *subscription() const;
|
||||
|
||||
void showRule(const AdBlockRule *rule);
|
||||
void refresh();
|
||||
|
||||
public slots:
|
||||
void addRule();
|
||||
void removeRule();
|
||||
void filterString(const QString &string);
|
||||
void clear();
|
||||
|
||||
private slots:
|
||||
void contextMenuRequested(const QPoint &pos);
|
||||
void itemChanged(QTreeWidgetItem *item);
|
||||
void copyFilter();
|
||||
|
||||
void subscriptionUpdated();
|
||||
void subscriptionError(const QString &message);
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event);
|
||||
|
||||
signals:
|
||||
void refreshStatusChanged(bool started);
|
||||
|
||||
private:
|
||||
void adjustItemFeatures(QTreeWidgetItem *item, const AdBlockRule *rule);
|
||||
void addTopLevelItem(QTreeWidgetItem *item);
|
||||
QList<QTreeWidgetItem*> allItems();
|
||||
void iterateAllItems(QTreeWidgetItem *parent);
|
||||
|
||||
AdBlockSubscription *m_subscription;
|
||||
QTreeWidgetItem *m_topItem;
|
||||
|
||||
QString m_ruleToBeSelected;
|
||||
bool m_itemChangingBlock;
|
||||
|
||||
bool m_refreshAllItemsNeeded;
|
||||
QList<QTreeWidgetItem*> m_allTreeItems;
|
||||
};
|
||||
|
||||
#endif // ADBLOCKTREEWIDGET_H
|
@ -1,41 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2011-2016 by Martin Rotter <rotter.martinos@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 "network-web/adblock/requestinterceptor.h"
|
||||
|
||||
#include "network-web/adblock/adblockmanager.h"
|
||||
#include "miscellaneous/application.h"
|
||||
|
||||
|
||||
RequestInterceptor::RequestInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) {
|
||||
}
|
||||
|
||||
RequestInterceptor::~RequestInterceptor() {
|
||||
}
|
||||
|
||||
void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
|
||||
if (AdBlockManager::instance()->shouldBlock(info.requestUrl(), info.firstPartyUrl().toString(), info.resourceType())) {
|
||||
info.block(true);
|
||||
qWarning("%s just blocked network resource:\n URL: '%s'.", APP_NAME, qPrintable(info.requestUrl().toString()));
|
||||
|
||||
/*
|
||||
qApp->showGuiMessage(tr("Network resource blocked"),
|
||||
tr("%1 just blocked network resource:\n URL: %2").arg(APP_NAME, info.requestUrl().toString()),
|
||||
QSystemTrayIcon::Information);
|
||||
*/
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
// This file is part of RSS Guard.
|
||||
//
|
||||
// Copyright (C) 2011-2016 by Martin Rotter <rotter.martinos@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 REQUESTINTERCEPTOR_H
|
||||
#define REQUESTINTERCEPTOR_H
|
||||
|
||||
#include <QWebEngineUrlRequestInterceptor>
|
||||
|
||||
|
||||
|
||||
class RequestInterceptor : public QWebEngineUrlRequestInterceptor {
|
||||
public:
|
||||
explicit RequestInterceptor(QObject *parent = 0);
|
||||
virtual ~RequestInterceptor();
|
||||
|
||||
void interceptRequest(QWebEngineUrlRequestInfo &info);
|
||||
};
|
||||
|
||||
#endif // REQUESTINTERCEPTOR_H
|
Loading…
x
Reference in New Issue
Block a user