From e3635f5394532637c8f18d8fbf3ebef547dfb8e3 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Wed, 19 Jul 2017 21:55:01 +0200 Subject: [PATCH] Save work. --- .../adblock/adblockaddsubscriptiondialog.cpp | 9 +- .../adblock/adblockaddsubscriptiondialog.h | 2 +- src/network-web/adblock/adblockdialog.cpp | 39 +- src/network-web/adblock/adblockdialog.h | 1 - src/network-web/adblock/adblockdialog.ui | 33 +- src/network-web/adblock/adblockicon.cpp | 8 +- src/network-web/adblock/adblocksearchtree.cpp | 173 ++++--- src/network-web/adblock/adblocksearchtree.h | 21 +- .../adblock/adblocksubscription.cpp | 422 +++++++++--------- src/network-web/adblock/adblocksubscription.h | 44 +- 10 files changed, 353 insertions(+), 399 deletions(-) diff --git a/src/network-web/adblock/adblockaddsubscriptiondialog.cpp b/src/network-web/adblock/adblockaddsubscriptiondialog.cpp index 930819b6b..7fd403a8b 100755 --- a/src/network-web/adblock/adblockaddsubscriptiondialog.cpp +++ b/src/network-web/adblock/adblockaddsubscriptiondialog.cpp @@ -20,6 +20,8 @@ #include "definitions/definitions.h" +#include + AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget* parent) : QDialog(parent), m_ui(new Ui::AdBlockAddSubscriptionDialog) { @@ -33,7 +35,7 @@ AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget* parent) << Subscription(QSL("IsraelList (Hebrew)"), QSL("http://secure.fanboy.co.nz/israelilist/IsraelList.txt")) << Subscription(QSL("NLBlock (Dutch)"), QSL("http://www.verzijlbergh.com/adblock/nlblock.txt")) << Subscription(QSL("Peter Lowe's list (English)"), QSL("http://pgl.yoyo.org/adservers/serverlist.php?hostformat=adblockplus&mimetype=plaintext")) - << Subscription(QSL("PLgeneral (Polish))", QSL("http://www.niecko.pl/adblock/adblock.txt")) + << Subscription(QSL("PLgeneral (Polish)"), QSL("http://www.niecko.pl/adblock/adblock.txt")) << Subscription(QSL("Schacks Adblock Plus liste (Danish)"), QSL("http://adblock.schack.dk/block.txt")) << Subscription(QSL("Xfiles (Italian)"), QSL("http://mozilla.gfsolone.com/filtri.txt")) << Subscription(QSL("EasyPrivacy (English)"), QSL("http://easylist-downloads.adblockplus.org/easyprivacy.txt")) @@ -47,7 +49,8 @@ AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget* parent) m_ui->comboBox->addItem(subscription.m_title); } - connect(m_ui->comboBox, &QComboBox::currentIndexChanged, this, &AdBlockAddSubscriptionDialog::indexChanged); + connect(m_ui->comboBox, static_cast(&QComboBox::currentIndexChanged), + this, &AdBlockAddSubscriptionDialog::indexChanged); indexChanged(0); } @@ -62,7 +65,7 @@ QString AdBlockAddSubscriptionDialog::url() const { void AdBlockAddSubscriptionDialog::indexChanged(int index) { const Subscription subscription = m_knownSubscriptions.at(index); - // "Other..." entry + // "Other..." entry. if (subscription.m_url.isEmpty()) { m_ui->title->clear(); m_ui->url->clear(); diff --git a/src/network-web/adblock/adblockaddsubscriptiondialog.h b/src/network-web/adblock/adblockaddsubscriptiondialog.h index 2bb14a719..eec53bfbe 100755 --- a/src/network-web/adblock/adblockaddsubscriptiondialog.h +++ b/src/network-web/adblock/adblockaddsubscriptiondialog.h @@ -43,7 +43,7 @@ class AdBlockAddSubscriptionDialog : public QDialog { void indexChanged(int index); private: - Ui::AdBlockAddSubscriptionDialog* m_ui; + Ui::AdBlockAddSubscriptionDialog *m_ui; struct Subscription { QString m_title; diff --git a/src/network-web/adblock/adblockdialog.cpp b/src/network-web/adblock/adblockdialog.cpp index 7e314439a..985075654 100755 --- a/src/network-web/adblock/adblockdialog.cpp +++ b/src/network-web/adblock/adblockdialog.cpp @@ -41,7 +41,7 @@ AdBlockDialog::AdBlockDialog(QWidget* parent) m_ui->adblockCheckBox->setChecked(m_manager->isEnabled()); - QMenu* menu = new QMenu(buttonOptions); + QMenu* menu = new QMenu(m_ui->buttonOptions); m_actionAddRule = menu->addAction(tr("Add Rule"), this, SLOT(addRule())); m_actionRemoveRule = menu->addAction(tr("Remove Rule"), this, SLOT(removeRule())); menu->addSeparator(); @@ -54,10 +54,9 @@ AdBlockDialog::AdBlockDialog(QWidget* parent) m_ui->buttonOptions->setMenu(menu); connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu())); - connect(adblockCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool))); - connect(search, SIGNAL(textChanged(QString)), this, SLOT(filterString(QString))); - connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int))); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(close())); + connect(m_ui->adblockCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool))); + connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int))); + connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(close())); load(); @@ -71,12 +70,12 @@ void AdBlockDialog::showRule(const AdBlockRule* rule) const { return; } - for (int i = 0; i < tabWidget->count(); ++i) { - AdBlockTreeWidget *treeWidget = qobject_cast(tabWidget->widget(i)); + for (int i = 0; i < m_ui->tabWidget->count(); ++i) { + AdBlockTreeWidget *treeWidget = qobject_cast(m_ui->tabWidget->widget(i)); if (subscription == treeWidget->subscription()) { treeWidget->showRule(rule); - tabWidget->setCurrentIndex(i); + m_ui->tabWidget->setCurrentIndex(i); break; } } @@ -101,10 +100,10 @@ void AdBlockDialog::addSubscription() { QString url = dialog.url(); if (AdBlockSubscription *subscription = m_manager->addSubscription(title, url)) { - AdBlockTreeWidget *tree = new AdBlockTreeWidget(subscription, tabWidget); - int index = tabWidget->insertTab(tabWidget->count() - 1, tree, subscription->title()); + AdBlockTreeWidget *tree = new AdBlockTreeWidget(subscription, m_ui->tabWidget); + int index = m_ui->tabWidget->insertTab(m_ui->tabWidget->count() - 1, tree, subscription->title()); - tabWidget->setCurrentIndex(index); + m_ui->tabWidget->setCurrentIndex(index); } } @@ -116,17 +115,11 @@ void AdBlockDialog::removeSubscription() { void AdBlockDialog::currentChanged(int index) { if (index != -1) { - m_currentTreeWidget = qobject_cast(tabWidget->widget(index)); + m_currentTreeWidget = qobject_cast(m_ui->tabWidget->widget(index)); m_currentSubscription = m_currentTreeWidget->subscription(); } } -void AdBlockDialog::filterString(const QString &string) { - if (m_currentTreeWidget && adblockCheckBox->isChecked()) { - m_currentTreeWidget->filterString(string); - } -} - void AdBlockDialog::enableAdBlock(bool state) { m_manager->setEnabled(state); @@ -149,20 +142,20 @@ void AdBlockDialog::learnAboutRules() { } void AdBlockDialog::loadSubscriptions() { - for (int i = 0; i < tabWidget->count(); ++i) { - AdBlockTreeWidget* treeWidget = qobject_cast(tabWidget->widget(i)); + for (int i = 0; i < m_ui->tabWidget->count(); ++i) { + AdBlockTreeWidget *treeWidget = qobject_cast(m_ui->tabWidget->widget(i)); treeWidget->refresh(); } } void AdBlockDialog::load() { - if (m_loaded || !adblockCheckBox->isChecked()) { + if (m_loaded || !m_ui->adblockCheckBox->isChecked()) { return; } foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) { - AdBlockTreeWidget* tree = new AdBlockTreeWidget(subscription, tabWidget); - tabWidget->addTab(tree, subscription->title()); + AdBlockTreeWidget *tree = new AdBlockTreeWidget(subscription, m_ui->tabWidget); + m_ui->tabWidget->addTab(tree, subscription->title()); } m_loaded = true; diff --git a/src/network-web/adblock/adblockdialog.h b/src/network-web/adblock/adblockdialog.h index 77bf0b75f..914fe3386 100755 --- a/src/network-web/adblock/adblockdialog.h +++ b/src/network-web/adblock/adblockdialog.h @@ -45,7 +45,6 @@ class AdBlockDialog : public QWidget { void removeSubscription(); void currentChanged(int index); - void filterString(const QString &string); void enableAdBlock(bool state); void aboutToShowMenu(); diff --git a/src/network-web/adblock/adblockdialog.ui b/src/network-web/adblock/adblockdialog.ui index 8435061b8..a856dcb9f 100755 --- a/src/network-web/adblock/adblockdialog.ui +++ b/src/network-web/adblock/adblockdialog.ui @@ -24,33 +24,6 @@ - - - - - - Search... - - - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 50 - 20 - - - - - - @@ -77,7 +50,7 @@ Options - + :/icons/other/adblock.png:/icons/other/adblock.png @@ -139,9 +112,7 @@ - - - + adblockCheckBox diff --git a/src/network-web/adblock/adblockicon.cpp b/src/network-web/adblock/adblockicon.cpp index 3ea3be9ca..b4295a8b0 100755 --- a/src/network-web/adblock/adblockicon.cpp +++ b/src/network-web/adblock/adblockicon.cpp @@ -20,10 +20,13 @@ #include "network-web/adblock/adblockrule.h" #include "network-web/adblock/adblockmanager.h" #include "network-web/adblock/adblocksubscription.h" +#include "miscellaneous/application.h" +#include "network-web/webpage.h" #include #include + AdBlockIcon::AdBlockIcon(BrowserWindow* window, QWidget* parent) : ClickableLabel(parent), m_window(window), m_menuAction(0), m_flashTimer(0), m_timerTicks(0), m_enabled(false) { setCursor(Qt::PointingHandCursor); @@ -55,7 +58,8 @@ void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) { pair.second = url; m_blockedPopups.append(pair); - mApp->desktopNotifications()->showNotification(QPixmap(":html/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window.")); + + qApp->showGuiMessage(tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window."), QSystemTrayIcon::Information); if (!m_flashTimer) { m_flashTimer = new QTimer(this); @@ -68,7 +72,7 @@ void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) { m_flashTimer->setInterval(500); m_flashTimer->start(); - connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); + connect(m_flashTimer, &QTimer::timeout, this, &AdBlockIcon::animateIcon); } QAction *AdBlockIcon::menuAction() { diff --git a/src/network-web/adblock/adblocksearchtree.cpp b/src/network-web/adblock/adblocksearchtree.cpp index 3184925fb..9c10ab806 100755 --- a/src/network-web/adblock/adblocksearchtree.cpp +++ b/src/network-web/adblock/adblocksearchtree.cpp @@ -16,123 +16,122 @@ // You should have received a copy of the GNU General Public License // along with RSS Guard. If not, see . -#include "adblocksearchtree.h" -#include "adblockrule.h" +#include "network-web/adblock/adblocksearchtree.h" +#include "network-web/adblock/adblockrule.h" #include -AdBlockSearchTree::AdBlockSearchTree() - : m_root(new Node) -{ + +AdBlockSearchTree::AdBlockSearchTree() : m_root(new Node) { } -AdBlockSearchTree::~AdBlockSearchTree() -{ - deleteNode(m_root); +AdBlockSearchTree::~AdBlockSearchTree() { + deleteNode(m_root); } -void AdBlockSearchTree::clear() -{ - deleteNode(m_root); - m_root = new Node; +void AdBlockSearchTree::clear() { + deleteNode(m_root); + m_root = new Node; } -bool AdBlockSearchTree::add(const AdBlockRule* rule) -{ - if (rule->m_type != AdBlockRule::StringContainsMatchRule) { - return false; +bool AdBlockSearchTree::add(const AdBlockRule *rule) { + if (rule->m_type != AdBlockRule::StringContainsMatchRule) { + return false; + } + + const QString filter = rule->m_matchString; + int len = filter.size(); + + if (len <= 0) { + qDebug("AdBlockSearchTree: 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); + Node *next = node->children.value(c); + + if (!next) { + next = new Node; + next->c = c; + node->children[c] = next; } - const QString filter = rule->m_matchString; - int len = filter.size(); + node = next; + } - if (len <= 0) { - qDebug() << "AdBlockSearchTree: Inserting rule with filter len <= 0!"; - return false; - } + node->rule = rule; - Node* node = m_root; - - for (int i = 0; i < len; ++i) { - const QChar c = filter.at(i); - Node *next = node->children.value(c); - if (!next) { - next = new Node; - next->c = c; - node->children[c] = next; - } - node = next; - } - - node->rule = rule; - - return true; + return true; } -const AdBlockRule* AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const -{ - int len = urlString.size(); - - if (len <= 0) { - return 0; - } - - const QChar* string = urlString.constData(); - - for (int i = 0; i < len; ++i) { - const AdBlockRule* rule = prefixSearch(request, domain, urlString, string++, len - i); - if (rule) { - return rule; - } - } +const AdBlockRule *AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const { + int len = urlString.size(); + if (len <= 0) { return 0; + } + + const QChar *string = urlString.constData(); + + for (int i = 0; i < len; ++i) { + const AdBlockRule *rule = prefixSearch(request, domain, urlString, string++, len - i); + + if (rule) { + return rule; + } + } + + return 0; } -const AdBlockRule* AdBlockSearchTree::prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString, const QChar* string, int len) const -{ - if (len <= 0) { - return 0; - } +const AdBlockRule *AdBlockSearchTree::prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString, const QChar *string, int len) const { + if (len <= 0) { + return 0; + } - QChar c = string[0]; + QChar c = string[0]; - Node* node = m_root->children.value(c); - if (!node) { - return nullptr; - } + Node *node = m_root->children.value(c); - for (int i = 1; i < len; ++i) { - const QChar c = (++string)[0]; + if (!node) { + return nullptr; + } - if (node->rule && node->rule->networkMatch(request, domain, urlString)) { - return node->rule; - } - - node = node->children.value(c); - if (!node) { - return nullptr; - } - } + for (int i = 1; i < len; ++i) { + const QChar c = (++string)[0]; if (node->rule && node->rule->networkMatch(request, domain, urlString)) { - return node->rule; + return node->rule; } - return nullptr; -} + node = node->children.value(c); -void AdBlockSearchTree::deleteNode(AdBlockSearchTree::Node* node) -{ if (!node) { - return; + return nullptr; } + } - QHashIterator i(node->children); - while (i.hasNext()) { - i.next(); - deleteNode(i.value()); - } + if (node->rule && node->rule->networkMatch(request, domain, urlString)) { + return node->rule; + } - delete node; + return nullptr; +} + +void AdBlockSearchTree::deleteNode(AdBlockSearchTree::Node* node) { + if (!node) { + return; + } + + QHashIterator i(node->children); + + while (i.hasNext()) { + i.next(); + deleteNode(i.value()); + } + + delete node; } diff --git a/src/network-web/adblock/adblocksearchtree.h b/src/network-web/adblock/adblocksearchtree.h index 8622694bd..3be09c289 100755 --- a/src/network-web/adblock/adblocksearchtree.h +++ b/src/network-web/adblock/adblocksearchtree.h @@ -22,24 +22,21 @@ #include #include -#include "qzcommon.h" class QWebEngineUrlRequestInfo; - class AdBlockRule; -class QUPZILLA_EXPORT AdBlockSearchTree -{ -public: +class AdBlockSearchTree { + public: explicit AdBlockSearchTree(); - ~AdBlockSearchTree(); + virtual ~AdBlockSearchTree(); void clear(); - bool add(const AdBlockRule* rule); - const AdBlockRule* find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const; + bool add(const AdBlockRule *rule); + const AdBlockRule *find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const; -private: + private: struct Node { QChar c; const AdBlockRule* rule; @@ -48,10 +45,10 @@ private: Node() : c(0) , rule(0) { } }; - const AdBlockRule* prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain, - const QString &urlString, const QChar* string, int len) const; + const AdBlockRule *prefixSearch(const QWebEngineUrlRequestInfo &request, const QString &domain, + const QString &urlString, const QChar *string, int len) const; - void deleteNode(Node* node); + void deleteNode(Node *node); Node* m_root; }; diff --git a/src/network-web/adblock/adblocksubscription.cpp b/src/network-web/adblock/adblocksubscription.cpp index eb6fddcb2..2466f67be 100755 --- a/src/network-web/adblock/adblocksubscription.cpp +++ b/src/network-web/adblock/adblocksubscription.cpp @@ -44,385 +44,373 @@ * SUCH DAMAGE. */ -#include "adblocksubscription.h" -#include "adblockmanager.h" -#include "adblocksearchtree.h" -#include "mainapplication.h" -#include "networkmanager.h" -#include "datapaths.h" -#include "qztools.h" +#include "network-web/adblock/adblocksubscription.h" + +#include "network-web/adblock/adblockmanager.h" +#include "network-web/adblock/adblocksearchtree.h" +#include "definitions/definitions.h" +#include "network-web/silentnetworkaccessmanager.h" #include #include #include #include + AdBlockSubscription::AdBlockSubscription(const QString &title, QObject* parent) - : QObject(parent) - , m_reply(0) - , m_title(title) - , m_updated(false) -{ + : QObject(parent), m_reply(0), m_title(title), m_updated(false) { } -QString AdBlockSubscription::title() const -{ - return m_title; +QString AdBlockSubscription::title() const { + return m_title; } -QString AdBlockSubscription::filePath() const -{ - return m_filePath; +QString AdBlockSubscription::filePath() const { + return m_filePath; } -void AdBlockSubscription::setFilePath(const QString &path) -{ - m_filePath = path; +void AdBlockSubscription::setFilePath(const QString &path) { + m_filePath = path; } -QUrl AdBlockSubscription::url() const -{ - return m_url; +QUrl AdBlockSubscription::url() const { + return m_url; } -void AdBlockSubscription::setUrl(const QUrl &url) -{ - m_url = url; +void AdBlockSubscription::setUrl(const QUrl &url) { + m_url = url; } -void AdBlockSubscription::loadSubscription(const QStringList &disabledRules) -{ - QFile file(m_filePath); +void AdBlockSubscription::loadSubscription(const QStringList &disabledRules) { + QFile file(m_filePath); - if (!file.exists()) { - QTimer::singleShot(0, this, SLOT(updateSubscription())); - return; + if (!file.exists()) { + QTimer::singleShot(0, this, SLOT(updateSubscription())); + return; + } + + if (!file.open(QFile::ReadOnly)) { + qWarning("Unable to open adblock file '%s' for reading.", qPrintable(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 AdBlock file '%s'.", qPrintable(m_filePath)); + QTimer::singleShot(0, this, SLOT(updateSubscription())); + return; + } + + m_rules.clear(); + + while (!textStream.atEnd()) { + AdBlockRule *rule = new AdBlockRule(textStream.readLine(), this); + + if (disabledRules.contains(rule->filter())) { + rule->setEnabled(false); } - if (!file.open(QFile::ReadOnly)) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for reading" << m_filePath; - QTimer::singleShot(0, this, SLOT(updateSubscription())); - return; - } + m_rules.append(rule); + } - 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(QLatin1String("[Adblock")) || m_title.isEmpty()) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "invalid format of adblock file" << m_filePath; - QTimer::singleShot(0, this, SLOT(updateSubscription())); - return; - } - - m_rules.clear(); - - while (!textStream.atEnd()) { - AdBlockRule* rule = new AdBlockRule(textStream.readLine(), this); - - if (disabledRules.contains(rule->filter())) { - rule->setEnabled(false); - } - - m_rules.append(rule); - } - - // Initial update - if (m_rules.isEmpty() && !m_updated) { - QTimer::singleShot(0, this, SLOT(updateSubscription())); - } + // Initial update. + if (m_rules.isEmpty() && !m_updated) { + QTimer::singleShot(0, this, SLOT(updateSubscription())); + } } -void AdBlockSubscription::saveSubscription() -{ +void AdBlockSubscription::saveSubscription() { } -void AdBlockSubscription::updateSubscription() -{ - if (m_reply || !m_url.isValid()) { - return; - } +void AdBlockSubscription::updateSubscription() { + if (m_reply || !m_url.isValid()) { + return; + } - m_reply = mApp->networkManager()->get(QNetworkRequest(m_url)); - connect(m_reply, &QNetworkReply::finished, this, &AdBlockSubscription::subscriptionDownloaded); + SilentNetworkAccessManager *mgs = new SilentNetworkAccessManager(this); + + m_reply = mgs->get(QNetworkRequest(m_url)); + connect(m_reply, &QNetworkReply::finished, this, &AdBlockSubscription::subscriptionDownloaded); } void AdBlockSubscription::subscriptionDownloaded() { - if (m_reply != qobject_cast(sender())) { - return; - } + if (m_reply != qobject_cast(sender())) { + return; + } - bool error = false; - const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); + bool error = false; + const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); - if (m_reply->error() != QNetworkReply::NoError || - !response.startsWith(QByteArray("[Adblock")) || - !saveDownloadedData(response) - ) { - error = true; - } + if (m_reply->error() != QNetworkReply::NoError || !response.startsWith(QByteArray("[Adblock")) || !saveDownloadedData(response)) { + error = true; + } - m_reply->deleteLater(); - m_reply = 0; + m_reply->manager()->deleteLater(); + m_reply->deleteLater(); + m_reply = 0; - if (error) { - emit subscriptionError(tr("Cannot load subscription!")); - return; - } + if (error) { + emit subscriptionError(tr("Cannot load subscription!")); + return; + } - loadSubscription(AdBlockManager::instance()->disabledRules()); + loadSubscription(AdBlockManager::instance()->disabledRules()); - emit subscriptionUpdated(); - emit subscriptionChanged(); + emit subscriptionUpdated(); + emit subscriptionChanged(); } bool AdBlockSubscription::saveDownloadedData(const QByteArray &data) { - QSaveFile file(m_filePath); + QSaveFile file(m_filePath); - if (!file.open(QFile::WriteOnly)) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath; - return false; - } + if (!file.open(QFile::WriteOnly)) { + qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << m_filePath; + return false; + } - // Write subscription header - file.write(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8()); - file.write(data); - file.commit(); - return true; + // Write subscription header + file.write(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8()); + file.write(data); + file.commit(); + return true; } const AdBlockRule* AdBlockSubscription::rule(int offset) const { - if (!QzTools::containsIndex(m_rules, offset)) { - return 0; - } + if (!QzTools::containsIndex(m_rules, offset)) { + return 0; + } - return m_rules[offset]; + return m_rules[offset]; } QVector AdBlockSubscription::allRules() const { - return m_rules; + return m_rules; } const AdBlockRule* AdBlockSubscription::enableRule(int offset) { - if (!QzTools::containsIndex(m_rules, offset)) { - return 0; - } + if (!QzTools::containsIndex(m_rules, offset)) { + return 0; + } - AdBlockRule* rule = m_rules[offset]; - rule->setEnabled(true); - AdBlockManager::instance()->removeDisabledRule(rule->filter()); + AdBlockRule* rule = m_rules[offset]; + rule->setEnabled(true); + AdBlockManager::instance()->removeDisabledRule(rule->filter()); - emit subscriptionChanged(); + emit subscriptionChanged(); - if (rule->isCssRule()) - mApp->reloadUserStyleSheet(); + if (rule->isCssRule()) + mApp->reloadUserStyleSheet(); - return rule; + return rule; } const AdBlockRule* AdBlockSubscription::disableRule(int offset) { - if (!QzTools::containsIndex(m_rules, offset)) { - return 0; - } + if (!QzTools::containsIndex(m_rules, offset)) { + return 0; + } - AdBlockRule* rule = m_rules[offset]; - rule->setEnabled(false); - AdBlockManager::instance()->addDisabledRule(rule->filter()); + AdBlockRule* rule = m_rules[offset]; + rule->setEnabled(false); + AdBlockManager::instance()->addDisabledRule(rule->filter()); - emit subscriptionChanged(); + emit subscriptionChanged(); - if (rule->isCssRule()) - mApp->reloadUserStyleSheet(); + if (rule->isCssRule()) + mApp->reloadUserStyleSheet(); - return rule; + return rule; } bool AdBlockSubscription::canEditRules() const { - return false; + return false; } bool AdBlockSubscription::canBeRemoved() const { - return true; + return true; } int AdBlockSubscription::addRule(AdBlockRule* rule) { - Q_UNUSED(rule) - return -1; + Q_UNUSED(rule) + return -1; } bool AdBlockSubscription::removeRule(int offset) { - Q_UNUSED(offset) - return false; + Q_UNUSED(offset) + return false; } const AdBlockRule* AdBlockSubscription::replaceRule(AdBlockRule* rule, int offset) { - Q_UNUSED(rule) - Q_UNUSED(offset) - return 0; + Q_UNUSED(rule) + Q_UNUSED(offset) + return 0; } AdBlockSubscription::~AdBlockSubscription() { - qDeleteAll(m_rules); + qDeleteAll(m_rules); } // AdBlockCustomList AdBlockCustomList::AdBlockCustomList(QObject* parent) - : AdBlockSubscription(tr("Custom Rules"), parent) + : AdBlockSubscription(tr("Custom Rules"), parent) { - setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/customlist.txt")); + setFilePath(DataPaths::currentProfilePath() + QLatin1String("/adblock/customlist.txt")); } void AdBlockCustomList::loadSubscription(const QStringList &disabledRules) { - // DuckDuckGo ad whitelist rules - // They cannot be removed, but can be disabled. - // Please consider not disabling them. Thanks! + // DuckDuckGo ad whitelist rules + // They cannot be removed, but can be disabled. + // Please consider not disabling them. Thanks! - const QString ddg1 = QSL("@@||duckduckgo.com^$document"); - const QString ddg2 = QSL("duckduckgo.com#@#.has-ad"); + const QString ddg1 = QSL("@@||duckduckgo.com^$document"); + const QString ddg2 = QSL("duckduckgo.com#@#.has-ad"); - const QString rules = QzTools::readAllFileContents(filePath()); + const QString rules = QzTools::readAllFileContents(filePath()); - QFile file(filePath()); - if (!file.exists()) { - saveSubscription(); - } + QFile file(filePath()); + if (!file.exists()) { + saveSubscription(); + } - if (file.open(QFile::WriteOnly | QFile::Append)) { - QTextStream stream(&file); - stream.setCodec("UTF-8"); + if (file.open(QFile::WriteOnly | QFile::Append)) { + QTextStream stream(&file); + stream.setCodec("UTF-8"); - if (!rules.contains(ddg1 + QL1S("\n"))) - stream << ddg1 << endl; + if (!rules.contains(ddg1 + QL1S("\n"))) + stream << ddg1 << endl; - if (!rules.contains(QL1S("\n") + ddg2)) - stream << ddg2 << endl; - } - file.close(); + if (!rules.contains(QL1S("\n") + ddg2)) + stream << ddg2 << endl; + } + file.close(); - AdBlockSubscription::loadSubscription(disabledRules); + AdBlockSubscription::loadSubscription(disabledRules); } void AdBlockCustomList::saveSubscription() { - QFile file(filePath()); + QFile file(filePath()); - if (!file.open(QFile::ReadWrite | QFile::Truncate)) { - qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << filePath(); - return; - } + if (!file.open(QFile::ReadWrite | QFile::Truncate)) { + qWarning() << "AdBlockSubscription::" << __FUNCTION__ << "Unable to open adblock file for writing:" << 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; + 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; - } + foreach (const AdBlockRule* rule, m_rules) { + textStream << rule->filter() << endl; + } - file.close(); + file.close(); } bool AdBlockCustomList::canEditRules() const { - return true; + return true; } bool AdBlockCustomList::canBeRemoved() const { - return false; + return false; } bool AdBlockCustomList::containsFilter(const QString &filter) const { - foreach (const AdBlockRule* rule, m_rules) { - if (rule->filter() == filter) { - return true; - } + foreach (const AdBlockRule* rule, m_rules) { + if (rule->filter() == filter) { + return true; } + } - return false; + return false; } bool AdBlockCustomList::removeFilter(const QString &filter) { - for (int i = 0; i < m_rules.count(); ++i) { - const AdBlockRule* rule = m_rules.at(i); + for (int i = 0; i < m_rules.count(); ++i) { + const AdBlockRule* rule = m_rules.at(i); - if (rule->filter() == filter) { - return removeRule(i); - } + if (rule->filter() == filter) { + return removeRule(i); } + } - return false; + return false; } int AdBlockCustomList::addRule(AdBlockRule* rule) { - m_rules.append(rule); + m_rules.append(rule); - emit subscriptionChanged(); + emit subscriptionChanged(); - if (rule->isCssRule()) - mApp->reloadUserStyleSheet(); + if (rule->isCssRule()) + mApp->reloadUserStyleSheet(); - return m_rules.count() - 1; + return m_rules.count() - 1; } bool AdBlockCustomList::removeRule(int offset) { - if (!QzTools::containsIndex(m_rules, offset)) { - return false; - } + if (!QzTools::containsIndex(m_rules, offset)) { + return false; + } - AdBlockRule* rule = m_rules.at(offset); - const QString filter = rule->filter(); + AdBlockRule* rule = m_rules.at(offset); + const QString filter = rule->filter(); - m_rules.remove(offset); + m_rules.remove(offset); - emit subscriptionChanged(); + emit subscriptionChanged(); - if (rule->isCssRule()) - mApp->reloadUserStyleSheet(); + if (rule->isCssRule()) + mApp->reloadUserStyleSheet(); - AdBlockManager::instance()->removeDisabledRule(filter); + AdBlockManager::instance()->removeDisabledRule(filter); - delete rule; - return true; + delete rule; + return true; } const AdBlockRule* AdBlockCustomList::replaceRule(AdBlockRule* rule, int offset) { - if (!QzTools::containsIndex(m_rules, offset)) { - return 0; - } + if (!QzTools::containsIndex(m_rules, offset)) { + return 0; + } - AdBlockRule* oldRule = m_rules.at(offset); - m_rules[offset] = rule; + AdBlockRule* oldRule = m_rules.at(offset); + m_rules[offset] = rule; - emit subscriptionChanged(); + emit subscriptionChanged(); - if (rule->isCssRule() || oldRule->isCssRule()) - mApp->reloadUserStyleSheet(); + if (rule->isCssRule() || oldRule->isCssRule()) + mApp->reloadUserStyleSheet(); - delete oldRule; - return m_rules[offset]; + delete oldRule; + return m_rules[offset]; } diff --git a/src/network-web/adblock/adblocksubscription.h b/src/network-web/adblock/adblocksubscription.h index 796d8b14d..75723a084 100755 --- a/src/network-web/adblock/adblocksubscription.h +++ b/src/network-web/adblock/adblocksubscription.h @@ -50,19 +50,19 @@ #include #include -#include "qzcommon.h" -#include "adblockrule.h" -#include "adblocksearchtree.h" +#include "network-web/adblock/adblockrule.h" +#include "network-web/adblock/adblocksearchtree.h" + class QUrl; class QNetworkReply; -class QUPZILLA_EXPORT AdBlockSubscription : public QObject -{ +class AdBlockSubscription : public QObject { Q_OBJECT -public: - explicit AdBlockSubscription(const QString &title, QObject* parent = 0); - ~AdBlockSubscription(); + + public: + explicit AdBlockSubscription(const QString &title, QObject *parent = 0); + virtual ~AdBlockSubscription(); QString title() const; @@ -75,11 +75,11 @@ public: virtual void loadSubscription(const QStringList &disabledRules); virtual void saveSubscription(); - const AdBlockRule* rule(int offset) const; + const AdBlockRule *rule(int offset) const; QVector allRules() const; - const AdBlockRule* enableRule(int offset); - const AdBlockRule* disableRule(int offset); + const AdBlockRule *enableRule(int offset); + const AdBlockRule *disableRule(int offset); virtual bool canEditRules() const; virtual bool canBeRemoved() const; @@ -88,24 +88,24 @@ public: virtual bool removeRule(int offset); virtual const AdBlockRule* replaceRule(AdBlockRule* rule, int offset); -public slots: + public slots: void updateSubscription(); -signals: + signals: void subscriptionChanged(); void subscriptionUpdated(); void subscriptionError(const QString &message); -protected slots: + protected slots: void subscriptionDownloaded(); -protected: + protected: virtual bool saveDownloadedData(const QByteArray &data); QNetworkReply *m_reply; QVector m_rules; -private: + private: QString m_title; QString m_filePath; @@ -113,11 +113,11 @@ private: bool m_updated; }; -class AdBlockCustomList : public AdBlockSubscription -{ +class AdBlockCustomList : public AdBlockSubscription { Q_OBJECT -public: - explicit AdBlockCustomList(QObject* parent = 0); + + public: + explicit AdBlockCustomList(QObject *parent = 0); void loadSubscription(const QStringList &disabledRules); void saveSubscription(); @@ -128,9 +128,9 @@ public: bool containsFilter(const QString &filter) const; bool removeFilter(const QString &filter); - int addRule(AdBlockRule* rule); + int addRule(AdBlockRule *rule); bool removeRule(int offset); - const AdBlockRule* replaceRule(AdBlockRule* rule, int offset); + const AdBlockRule *replaceRule(AdBlockRule *rule, int offset); }; #endif // ADBLOCKSUBSCRIPTION_H