From 57428510d454970bc369d9102df0ebe3a2df98c6 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Wed, 17 Jun 2015 11:11:12 +0200 Subject: [PATCH] Refactoring of Adblock component. --- .gitignore | 1 + CMakeLists.txt | 1 + src/definitions/definitions.h.in | 3 + src/gui/formsettings.ui | 25 +- src/gui/tabwidget.h | 4 + .../adblock/adblockaddsubscriptiondialog.cpp | 146 ++++++----- .../adblock/adblockaddsubscriptiondialog.h | 110 ++++----- .../adblock/adblockaddsubscriptiondialog.ui | 60 +++-- .../adblock/adblockblockednetworkreply.cpp | 82 +++---- .../adblock/adblockblockednetworkreply.h | 75 +++--- src/network-web/adblock/adblockdialog.cpp | 231 ++++++++---------- src/network-web/adblock/adblockdialog.h | 75 +++--- src/network-web/adblock/adblockdialog.ui | 96 +++----- src/network-web/adblock/adblockicon.cpp | 181 ++++++-------- src/network-web/adblock/adblockicon.h | 62 ++--- src/network-web/adblock/adblockmanager.cpp | 39 ++- src/network-web/adblock/adblockmanager.h | 7 +- src/network-web/adblock/adblockmatcher.cpp | 15 +- src/network-web/adblock/adblockrule.h | 2 +- 19 files changed, 562 insertions(+), 653 deletions(-) mode change 100644 => 100755 src/gui/formsettings.ui mode change 100644 => 100755 src/gui/tabwidget.h diff --git a/.gitignore b/.gitignore index ea81adde8..8d15259e0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ *.app # Other files. +*.autosave *.user* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f06d824b..8eb0aa6e5 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -703,6 +703,7 @@ include_directories ( ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src/gui ${CMAKE_SOURCE_DIR}/src/network-web + ${CMAKE_SOURCE_DIR}/src/network-web/adblock ${CMAKE_SOURCE_DIR}/src/dynamic-shortcuts ${CMAKE_BINARY_DIR}/src ) diff --git a/src/definitions/definitions.h.in b/src/definitions/definitions.h.in index 28336dfc6..c3e760fab 100755 --- a/src/definitions/definitions.h.in +++ b/src/definitions/definitions.h.in @@ -93,6 +93,9 @@ #define PLACEHOLDER_UNREAD_COUNTS "%unread" #define PLACEHOLDER_ALL_COUNTS "%all" +#define ADBLOCK_FILTERS_HELP "http://adblockplus.org/en/filters" +#define ADBLOCK_EASYLIST_URL "https://easylist-downloads.adblockplus.org/easylist.txt" + #define BACKUP_NAME_SETTINGS "config" #define BACKUP_SUFFIX_SETTINGS ".ini.backup" #define BACKUP_NAME_DATABASE "database" diff --git a/src/gui/formsettings.ui b/src/gui/formsettings.ui old mode 100644 new mode 100755 index 6ab30e721..a523793da --- a/src/gui/formsettings.ui +++ b/src/gui/formsettings.ui @@ -88,7 +88,7 @@ - 6 + 0 @@ -345,6 +345,11 @@ Authors of this application are NOT responsible for lost data. + + + true + + Note that speed of used MySQL server and latency of used connection medium HEAVILY influences the final performance of this application. Using slow database connections leads to bad performance when browsing feeds or messages. @@ -414,8 +419,8 @@ MySQL backend will automatically use database with name "rssguard". Do 0 0 - 100 - 30 + 740 + 451 @@ -492,8 +497,8 @@ MySQL backend will automatically use database with name "rssguard". Do 0 0 - 167 - 219 + 734 + 425 @@ -896,6 +901,11 @@ MySQL backend will automatically use database with name "rssguard". Do + + + true + + Mouse gestures work with middle mouse button. Possible gestures are: • previous web page (drag mouse left), @@ -1361,6 +1371,11 @@ MySQL backend will automatically use database with name "rssguard". Do + + + true + + Enter format for count of messages displayed next to each feed/category in feed list. Use "%all" and "%unread" strings which are placeholders for the actual count of all (or unread) messages. diff --git a/src/gui/tabwidget.h b/src/gui/tabwidget.h old mode 100644 new mode 100755 index a3fa7b90c..c39616720 --- a/src/gui/tabwidget.h +++ b/src/gui/tabwidget.h @@ -59,6 +59,10 @@ class TabWidget : public QTabWidget { return static_cast(QTabWidget::widget(index)); } + inline TabContent *currentWidget() const { + return static_cast(QTabWidget::currentWidget()); + } + // Initializes TabWidget with tabs, this includes initialization // of main "Feeds" widget. void initializeTabs(); diff --git a/src/network-web/adblock/adblockaddsubscriptiondialog.cpp b/src/network-web/adblock/adblockaddsubscriptiondialog.cpp index a011596b1..0f8207879 100755 --- a/src/network-web/adblock/adblockaddsubscriptiondialog.cpp +++ b/src/network-web/adblock/adblockaddsubscriptiondialog.cpp @@ -1,46 +1,38 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -#include "adblockaddsubscriptiondialog.h" -#include "adblockmanager.h" -#include "ui_adblockaddsubscriptiondialog.h" +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . -AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget* parent) - : QDialog(parent) - , ui(new Ui::AdBlockAddSubscriptionDialog) -{ - ui->setupUi(this); +#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 + + +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")); m_knownSubscriptions << Subscription("EasyList (English)", ADBLOCK_EASYLIST_URL) << Subscription("Fanboy's List (English)", "http://www.fanboy.co.nz/adblock/fanboy-adblock.txt") @@ -63,52 +55,72 @@ AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget* parent) << Subscription("RU AdList (Russian, Ukrainian)", "https://easylist-downloads.adblockplus.org/advblock.txt") << Subscription("ABPindo (Indonesian)", "https://indonesianadblockrules.googlecode.com/hg/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("Malware Domains list", "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt") << + Subscription(tr("Other list"), QString()); foreach (const Subscription &subscription, m_knownSubscriptions) { - ui->comboBox->addItem(subscription.title); + m_ui->m_cmbPresets->addItem(subscription.m_title); } - connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChanged(int))); + connect(m_ui->m_cmbPresets, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChanged(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())); indexChanged(0); } -QString AdBlockAddSubscriptionDialog::title() const -{ - return ui->title->text(); +QString AdBlockAddSubscriptionDialog::title() const { + return m_ui->m_txtTitle->lineEdit()->text(); } -QString AdBlockAddSubscriptionDialog::url() const -{ - return ui->url->text(); +QString AdBlockAddSubscriptionDialog::url() const { + return m_ui->m_txtUrl->lineEdit()->text(); } -void AdBlockAddSubscriptionDialog::indexChanged(int index) -{ +void AdBlockAddSubscriptionDialog::indexChanged(int index) { const Subscription subscription = m_knownSubscriptions.at(index); - // "Other..." entry - if (subscription.url.isEmpty()) { - ui->title->clear(); - ui->url->clear(); + // "Other" entry. + if (subscription.m_url.isEmpty()) { + m_ui->m_txtTitle->lineEdit()->clear(); + m_ui->m_txtUrl->lineEdit()->clear(); } else { - int pos = subscription.title.indexOf(QLatin1Char('(')); - QString title = subscription.title; + int pos = subscription.m_title.indexOf(QLatin1Char('(')); + QString title = subscription.m_title; if (pos > 0) { title = title.left(pos).trimmed(); } - ui->title->setText(title); - ui->title->setCursorPosition(0); - - ui->url->setText(subscription.url); - ui->url->setCursorPosition(0); + m_ui->m_txtTitle->lineEdit()->setText(title); + m_ui->m_txtTitle->lineEdit()->setCursorPosition(0); + m_ui->m_txtUrl->lineEdit()->setText(subscription.m_url); + m_ui->m_txtUrl->lineEdit()->setCursorPosition(0); } } -AdBlockAddSubscriptionDialog::~AdBlockAddSubscriptionDialog() -{ - delete ui; +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() { + delete m_ui; } diff --git a/src/network-web/adblock/adblockaddsubscriptiondialog.h b/src/network-web/adblock/adblockaddsubscriptiondialog.h index e3dc794d1..ccc7bbf1c 100755 --- a/src/network-web/adblock/adblockaddsubscriptiondialog.h +++ b/src/network-web/adblock/adblockaddsubscriptiondialog.h @@ -1,78 +1,68 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + #ifndef ADBLOCKADDSUBSCRIPTIONDIALOG_H #define ADBLOCKADDSUBSCRIPTIONDIALOG_H #include + +#include "ui_adblockaddsubscriptiondialog.h" + #include -namespace Ui -{ -class AdBlockAddSubscriptionDialog; + +namespace Ui { + class AdBlockAddSubscriptionDialog; } -class AdBlockAddSubscriptionDialog : public QDialog -{ - Q_OBJECT +class AdBlockAddSubscriptionDialog : public QDialog { + Q_OBJECT -public: - explicit AdBlockAddSubscriptionDialog(QWidget* parent = 0); - ~AdBlockAddSubscriptionDialog(); + public: + // Constructors. + explicit AdBlockAddSubscriptionDialog(QWidget *parent = 0); + virtual ~AdBlockAddSubscriptionDialog(); - QString title() const; - QString url() const; + QString title() const; + QString url() const; -private slots: - void indexChanged(int index); + private slots: + // Index of selected list changed. + void indexChanged(int index); + void checkInputs(); -private: - Ui::AdBlockAddSubscriptionDialog* ui; + private: + struct Subscription { + public: + QString m_title; + QString m_url; - struct Subscription { - QString title; - QString url; + explicit Subscription() { + } - Subscription() {} + explicit Subscription(const QString &title, const QString &url) { + m_title = title; + m_url = url; + } + }; - Subscription(const QString &t, const QString &u) { - title = t; - url = u; - } - }; - - QVector m_knownSubscriptions; + Ui::AdBlockAddSubscriptionDialog *m_ui; + QVector m_knownSubscriptions; }; #endif // ADBLOCKADDSUBSCRIPTIONDIALOG_H diff --git a/src/network-web/adblock/adblockaddsubscriptiondialog.ui b/src/network-web/adblock/adblockaddsubscriptiondialog.ui index 88b77db8a..9b26ff423 100755 --- a/src/network-web/adblock/adblockaddsubscriptiondialog.ui +++ b/src/network-web/adblock/adblockaddsubscriptiondialog.ui @@ -7,38 +7,32 @@ 0 0 557 - 162 + 142 - Add Subscription + Add subscription - - - + + + - - + + Title: - - - - - + + Address: - - - - + Qt::Horizontal @@ -47,19 +41,39 @@ - - - - Add new subscription to AdBlock: + + + + Qt::Vertical - + + + 20 + 40 + + + + + + + + + + + + LineEditWithStatus + QWidget +
lineeditwithstatus.h
+ 1 +
+
- buttonBox + m_buttonBox accepted() AdBlockAddSubscriptionDialog accept() @@ -75,7 +89,7 @@ - buttonBox + m_buttonBox rejected() AdBlockAddSubscriptionDialog reject() diff --git a/src/network-web/adblock/adblockblockednetworkreply.cpp b/src/network-web/adblock/adblockblockednetworkreply.cpp index 60fcbe705..8431f3396 100755 --- a/src/network-web/adblock/adblockblockednetworkreply.cpp +++ b/src/network-web/adblock/adblockblockednetworkreply.cpp @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + /** * Copyright (c) 2009, Benjamin C. Meyer * @@ -60,40 +44,38 @@ * SUCH DAMAGE. */ -#include "adblockblockednetworkreply.h" -#include "adblocksubscription.h" -#include "adblockrule.h" +#include "network-web/adblock/adblockblockednetworkreply.h" + +#include "network-web/adblock/adblocksubscription.h" +#include "network-web/adblock/adblockrule.h" #include #include -AdBlockBlockedNetworkReply::AdBlockBlockedNetworkReply(const AdBlockRule* rule, QObject* parent) - : QNetworkReply(parent) -{ + +AdBlockBlockedNetworkReply::AdBlockBlockedNetworkReply(const AdBlockRule *rule, QObject *parent) + : QNetworkReply(parent) { setOperation(QNetworkAccessManager::GetOperation); - setError(QNetworkReply::ContentAccessDenied, QString("AdBlock: %1 (%2)").arg(rule->subscription()->title(), rule->filter())); - + setError(QNetworkReply::ContentAccessDenied, QString("Adblock: %1 (%2)").arg(rule->subscription()->title(), rule->filter())); open(QIODevice::ReadOnly); - QTimer::singleShot(0, this, SLOT(delayedFinished())); } -void AdBlockBlockedNetworkReply::setRequest(const QNetworkRequest &request) -{ +void AdBlockBlockedNetworkReply::abort() { +} + +void AdBlockBlockedNetworkReply::setRequest(const QNetworkRequest &request) { QNetworkReply::setRequest(request); setUrl(request.url()); } -qint64 AdBlockBlockedNetworkReply::readData(char* data, qint64 maxSize) -{ +qint64 AdBlockBlockedNetworkReply::readData(char *data, qint64 maxSize) { Q_UNUSED(data); Q_UNUSED(maxSize); return -1; } -void AdBlockBlockedNetworkReply::delayedFinished() -{ +void AdBlockBlockedNetworkReply::delayedFinished() { emit error(QNetworkReply::ContentAccessDenied); emit finished(); } - diff --git a/src/network-web/adblock/adblockblockednetworkreply.h b/src/network-web/adblock/adblockblockednetworkreply.h index 6d32615f9..7ded70a29 100755 --- a/src/network-web/adblock/adblockblockednetworkreply.h +++ b/src/network-web/adblock/adblockblockednetworkreply.h @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + /** * Copyright (c) 2009, Benjamin C. Meyer * @@ -65,25 +49,24 @@ #include + class AdBlockRule; class AdBlockSubscription; -class AdBlockBlockedNetworkReply : public QNetworkReply -{ - Q_OBJECT +class AdBlockBlockedNetworkReply : public QNetworkReply{ + Q_OBJECT -public: - AdBlockBlockedNetworkReply(const AdBlockRule* rule, QObject* parent = 0); - void abort() {} + public: + explicit AdBlockBlockedNetworkReply(const AdBlockRule *rule, QObject *parent = 0); - void setRequest(const QNetworkRequest &request); + void abort(); + void setRequest(const QNetworkRequest &request); -protected: - qint64 readData(char* data, qint64 maxSize); - -private slots: - void delayedFinished(); + protected: + qint64 readData(char *data, qint64 maxSize); + private slots: + void delayedFinished(); }; #endif // ADBLOCKBLOCKEDNETWORKREPLY_H diff --git a/src/network-web/adblock/adblockdialog.cpp b/src/network-web/adblock/adblockdialog.cpp index f7dff329c..cf7dc4553 100755 --- a/src/network-web/adblock/adblockdialog.cpp +++ b/src/network-web/adblock/adblockdialog.cpp @@ -1,42 +1,31 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -#include "adblockdialog.h" -#include "adblockmanager.h" -#include "adblocksubscription.h" -#include "adblocktreewidget.h" -#include "adblockaddsubscriptiondialog.h" +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + +#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/formmain.h" #include #include @@ -44,98 +33,99 @@ #include #include -AdBlockDialog::AdBlockDialog(QWidget* parent) - : QWidget(parent) - , m_manager(AdBlockManager::instance()) - , m_currentTreeWidget(0) - , m_currentSubscription(0) - , m_loaded(false) - , m_useLimitedEasyList(false) -{ + +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); - setupUi(this); + setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint); + setWindowIcon(qApp->icons()->fromTheme("web-adblock")); - const QRect screen = QApplication::desktop()->screenGeometry(); - const QRect size = geometry(); - move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2); - tabWidget->setDocumentMode(false); -#ifdef Q_OS_MAC - tabWidget->setDocumentMode(false); -#endif - adblockCheckBox->setChecked(m_manager->isEnabled()); + 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() + " "); - buttonOptions->setText(buttonOptions->text() % " "); + // Setup the menu. + setupMenu(); - QMenu* menu = new QMenu(buttonOptions); - 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("Learn about writing rules..."), this, SLOT(learnAboutRules())); - - 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())); + // Initialize connections. + createConnections(); + // Load the contents. load(); - buttonBox->setFocus(); + m_ui->m_buttonBox->setFocus(); } AdBlockDialog::~AdBlockDialog() { qDebug("Destroying AdBlockDialog instance."); + delete m_ui; } -void AdBlockDialog::showRule(const AdBlockRule* rule) const -{ +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 { AdBlockSubscription* subscription = rule->subscription(); - if (!subscription) { + + if (subscription == NULL) { return; } - for (int i = 0; i < tabWidget->count(); ++i) { - AdBlockTreeWidget* treeWidget = qobject_cast(tabWidget->widget(i)); + for (int i = 0; i < m_ui->m_tabs->count(); i++) { + AdBlockTreeWidget* treeWidget = qobject_cast(m_ui->m_tabs->widget(i)); if (subscription == treeWidget->subscription()) { treeWidget->showRule(rule); - tabWidget->setCurrentIndex(i); + m_ui->m_tabs->setCurrentIndex(i); break; } } } -void AdBlockDialog::addRule() -{ +void AdBlockDialog::addRule() { m_currentTreeWidget->addRule(); } -void AdBlockDialog::removeRule() -{ +void AdBlockDialog::removeRule() { m_currentTreeWidget->removeRule(); } -void AdBlockDialog::addSubscription() -{ +void AdBlockDialog::addSubscription() { AdBlockAddSubscriptionDialog dialog(this); - if (dialog.exec() != QDialog::Accepted) { - return; - } - QString title = dialog.title(); - QString url = dialog.url(); + if (dialog.exec() == QDialog::Accepted) { + QString title = dialog.title(); + 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()); + if (AdBlockSubscription *subscription = m_manager->addSubscription(title, url)) { + AdBlockTreeWidget *tree = new AdBlockTreeWidget(subscription, m_ui->m_tabs); + int index = m_ui->m_tabs->insertTab(m_ui->m_tabs->count() - 1, tree, subscription->title()); - tabWidget->setCurrentIndex(index); + m_ui->m_tabs->setCurrentIndex(index); + } } } @@ -146,26 +136,24 @@ void AdBlockDialog::removeSubscription() } } -void AdBlockDialog::currentChanged(int index) -{ +void AdBlockDialog::currentChanged(int index) { if (index != -1) { - m_currentTreeWidget = qobject_cast(tabWidget->widget(index)); + m_currentTreeWidget = qobject_cast(m_ui->m_tabs->widget(index)); m_currentSubscription = m_currentTreeWidget->subscription(); - bool isEasyList = m_currentSubscription->url() == QUrl(ADBLOCK_EASYLIST_URL); - useLimitedEasyList->setVisible(isEasyList); + 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); } } -void AdBlockDialog::filterString(const QString &string) -{ - if (m_currentTreeWidget && adblockCheckBox->isChecked()) { +void AdBlockDialog::filterString(const QString &string) { + if (m_currentTreeWidget && m_ui->m_checkEnable->isChecked()) { m_currentTreeWidget->filterString(string); } } -void AdBlockDialog::enableAdBlock(bool state) -{ +void AdBlockDialog::enableAdBlock(bool state) { m_manager->setEnabled(state); if (state) { @@ -173,8 +161,7 @@ void AdBlockDialog::enableAdBlock(bool state) } } -void AdBlockDialog::aboutToShowMenu() -{ +void AdBlockDialog::aboutToShowMenu() { bool subscriptionEditable = m_currentSubscription && m_currentSubscription->canEditRules(); bool subscriptionRemovable = m_currentSubscription && m_currentSubscription->canBeRemoved(); @@ -183,34 +170,30 @@ void AdBlockDialog::aboutToShowMenu() m_actionRemoveSubscription->setEnabled(subscriptionRemovable); } -void AdBlockDialog::learnAboutRules() -{ - // TODO - //mainApp->mainWindow()->openNewsTab_ = NEW_TAB_FOREGROUND; - //mainApp->mainWindow()->createWebTab(QUrl("http://adblockplus.org/en/filters")); +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 < tabWidget->count(); ++i) { - AdBlockTreeWidget* treeWidget = qobject_cast(tabWidget->widget(i)); - treeWidget->refresh(); +void AdBlockDialog::loadSubscriptions() { + for (int i = 0; i < m_ui->m_tabs->count(); ++i) { + qobject_cast(m_ui->m_tabs->widget(i))->refresh(); } } void AdBlockDialog::load() { - if (m_loaded || !adblockCheckBox->isChecked()) { + if (m_loaded || !m_ui->m_checkEnable->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->m_tabs); + m_ui->m_tabs->addTab(tree, subscription->title()); } m_useLimitedEasyList = m_manager->useLimitedEasyList(); - useLimitedEasyList->setChecked(m_useLimitedEasyList); + m_ui->m_checkUseLimitedEasyList->setChecked(m_useLimitedEasyList); m_loaded = true; @@ -219,8 +202,8 @@ void AdBlockDialog::load() void AdBlockDialog::closeEvent(QCloseEvent* ev) { - if (useLimitedEasyList->isChecked() != m_useLimitedEasyList) { - m_manager->setUseLimitedEasyList(useLimitedEasyList->isChecked()); + if (m_ui->m_checkUseLimitedEasyList->isChecked() != m_useLimitedEasyList) { + m_manager->setUseLimitedEasyList(m_ui->m_checkUseLimitedEasyList->isChecked()); } QWidget::closeEvent(ev); diff --git a/src/network-web/adblock/adblockdialog.h b/src/network-web/adblock/adblockdialog.h index ca834ce93..ea6aac5d7 100755 --- a/src/network-web/adblock/adblockdialog.h +++ b/src/network-web/adblock/adblockdialog.h @@ -1,58 +1,47 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2011-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + #ifndef ADBLOCKDIALOG_H #define ADBLOCKDIALOG_H -#include +#include #include "ui_adblockdialog.h" + +namespace Ui { + class AdBlockDialog; +} + class AdBlockSubscription; class AdBlockTreeWidget; class AdBlockManager; class AdBlockRule; -class AdBlockDialog : public QWidget, public Ui_AdBlockDialog -{ +class AdBlockDialog : public QDialog { Q_OBJECT public: - explicit AdBlockDialog(QWidget* parent = 0); + // Constructors. + explicit AdBlockDialog(QWidget *parent = 0); virtual ~AdBlockDialog(); - void showRule(const AdBlockRule* rule) const; + void showRule(const AdBlockRule *rule) const; private slots: void addRule(); @@ -71,9 +60,14 @@ class AdBlockDialog : public QWidget, public Ui_AdBlockDialog void loadSubscriptions(); void load(); - private: + protected: void closeEvent(QCloseEvent* ev); + private: + void setupMenu(); + void createConnections(); + + Ui::AdBlockDialog *m_ui; AdBlockManager* m_manager; AdBlockTreeWidget* m_currentTreeWidget; AdBlockSubscription* m_currentSubscription; @@ -88,4 +82,3 @@ class AdBlockDialog : public QWidget, public Ui_AdBlockDialog }; #endif // ADBLOCKDIALOG_H - diff --git a/src/network-web/adblock/adblockdialog.ui b/src/network-web/adblock/adblockdialog.ui index 98eff9d73..fac98169c 100755 --- a/src/network-web/adblock/adblockdialog.ui +++ b/src/network-web/adblock/adblockdialog.ui @@ -1,23 +1,23 @@ AdBlockDialog - + 0 0 - 546 - 462 + 451 + 408 - AdBlock Configuration + Adblock settings - + - Enable AdBlock + Enable Adblock true @@ -25,34 +25,22 @@ - - - - - Search... - - - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 50 - 20 - - - - - + + + + true + + + + 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. + + + true + + - + true @@ -72,12 +60,19 @@ - + Options + + + + Filter rules + + + @@ -91,30 +86,10 @@ - - - - - 0 - 0 - - - - AdBlock - - - - - - -1 - - - true - - + @@ -122,14 +97,17 @@ - + + + false + Use only essential part of EasyList (for performance reasons) - + Qt::Horizontal @@ -145,9 +123,9 @@ - adblockCheckBox + m_checkEnable toggled(bool) - adblockWidget + m_adBlockWidget setEnabled(bool) @@ -161,7 +139,7 @@ - buttonBox + m_buttonBox clicked(QAbstractButton*) AdBlockDialog close() diff --git a/src/network-web/adblock/adblockicon.cpp b/src/network-web/adblock/adblockicon.cpp index fa61eebac..23deb36b7 100755 --- a/src/network-web/adblock/adblockicon.cpp +++ b/src/network-web/adblock/adblockicon.cpp @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + #include "network-web/adblock/adblockicon.h" #include "network-web/adblock/adblockrule.h" @@ -39,7 +23,6 @@ #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/formmain.h" @@ -52,20 +35,17 @@ AdBlockIcon::AdBlockIcon(QWidget *window, QWidget *parent) - : PlainToolButton(parent), m_window(window), m_menuAction(0), m_flashTimer(0), m_timerTicks(0), m_enabled(false) { - setToolTip(tr("Adblock lets you block unwanted content on web pages.")); - + : PlainToolButton(parent), m_window(window), m_menuAction(NULL), m_flashTimer(NULL), m_timerTicks(NULL), m_enabled(false) { connect(this, SIGNAL(clicked(QPoint)), this, SLOT(showMenu(QPoint))); connect(AdBlockManager::instance(), SIGNAL(enabledChanged(bool)), this, SLOT(setEnabled(bool))); } -void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) -{ - int index = ruleString.lastIndexOf(QLatin1String(" (")); +void AdBlockIcon::popupBlocked(const QString &rule_string, const QUrl &url) { + int index = rule_string.lastIndexOf(QLatin1String(" (")); + const QString subscription_ame = rule_string.left(index); + const QString filter = rule_string.mid(index + 2, rule_string.size() - index - 3); + AdBlockSubscription* subscription = AdBlockManager::instance()->subscriptionByName(subscription_ame); - const QString subscriptionName = ruleString.left(index); - const QString filter = ruleString.mid(index + 2, ruleString.size() - index - 3); - AdBlockSubscription* subscription = AdBlockManager::instance()->subscriptionByName(subscriptionName); if (filter.isEmpty() || !subscription) { return; } @@ -78,7 +58,7 @@ void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) //!** FIXME // mApp->desktopNotifications()->showNotification(QPixmap(":images/images/adblock_big.png"), tr("Blocked popup window"), tr("AdBlock blocked unwanted popup window.")); - if (!m_flashTimer) { + if (m_flashTimer == NULL) { m_flashTimer = new QTimer(this); } @@ -92,21 +72,20 @@ void AdBlockIcon::popupBlocked(const QString &ruleString, const QUrl &url) connect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); } -QAction* AdBlockIcon::menuAction() -{ - if (!m_menuAction) { - m_menuAction = new QAction(tr("AdBlock"), this); - m_menuAction->setMenu(new QMenu); +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(QIcon(m_enabled ? ":images/images/adblock.png" : ":images/images/adblock-disabled.png")); + m_menuAction->setIcon(m_enabled ? qApp->icons()->fromTheme("web-adblock") : qApp->icons()->fromTheme("web-adblock")); return m_menuAction; } -void AdBlockIcon::createMenu(QMenu *menu) -{ +void AdBlockIcon::createMenu(QMenu *menu) { if (menu == NULL) { menu = qobject_cast(sender()); @@ -118,43 +97,47 @@ void AdBlockIcon::createMenu(QMenu *menu) menu->clear(); AdBlockManager *manager = AdBlockManager::instance(); - AdBlockCustomList *customList = manager->customList(); + AdBlockCustomList *custom_list = manager->customList(); - WebPage* page = qApp->mainForm()->tabWidget()->widget(qApp->mainForm()->tabWidget()->currentIndex())->webBrowser()->view()->page(); - const QUrl pageUrl = page->mainFrame()->url(); + WebPage* page = qApp->mainForm()->tabWidget()->currentWidget()->webBrowser()->view()->page(); + const QUrl page_url = page->mainFrame()->url(); menu->addAction(tr("Show Adblock &settings"), manager, SLOT(showDialog())); menu->addSeparator(); - if (!pageUrl.host().isEmpty() && m_enabled && manager->canRunOnScheme(pageUrl.scheme())) { - const QString host = pageUrl.host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host(); - const QString hostFilter = QString("@@||%1^$document").arg(host); - const QString pageFilter = QString("@@|%1|$document").arg(pageUrl.toString()); + if (!page_url.host().isEmpty() && m_enabled && manager->canRunOnScheme(page_url.scheme())) { + const QString host = page_url.host().contains(QLatin1String("www.")) ? page_url.host().mid(4) : page_url.host(); + const QString host_filter = QString("@@||%1^$document").arg(host); + const QString page_filter = QString("@@|%1|$document").arg(page_url.toString()); - QAction* act = menu->addAction(tr("Disable on %1").arg(host)); + QAction *act; + + act = menu->addAction(tr("Disable on %1").arg(host)); act->setCheckable(true); - act->setChecked(customList->containsFilter(hostFilter)); - act->setData(hostFilter); + 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(customList->containsFilter(pageFilter)); - act->setData(pageFilter); + act->setChecked(custom_list->containsFilter(page_filter)); + act->setData(page_filter); connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter())); menu->addSeparator(); } if (!m_blockedPopups.isEmpty()) { - menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false); + menu->addAction(tr("Blocked popup windows"))->setEnabled(false); + for (int i = 0; i < m_blockedPopups.count(); i++) { - const QPair &pair = m_blockedPopups.at(i); + const QPair &pair = m_blockedPopups.at(i); QString address = pair.second.toString().right(55); - QString actionText = tr("%1 with (%2)").arg(address, pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&")); + QString actionText = tr("%1 with (%2)").arg(address, + pair.first->filter()).replace(QLatin1Char('&'), QLatin1String("&&")); - QAction* action = menu->addAction(actionText, manager, SLOT(showRule())); + QAction *action = menu->addAction(actionText, manager, SLOT(showRule())); action->setData(QVariant::fromValue((void*)pair.first)); } } @@ -162,98 +145,96 @@ void AdBlockIcon::createMenu(QMenu *menu) menu->addSeparator(); QVector entries = page->adBlockedEntries(); + if (entries.isEmpty()) { menu->addAction(tr("No content blocked"))->setEnabled(false); } else { - menu->addAction(tr("Blocked URL (AdBlock Rule) - click to edit rule"))->setEnabled(false); + menu->addAction(tr("Blocked some content - click to edit rule"))->setEnabled(false); + foreach (const WebPage::AdBlockedEntry &entry, entries) { QString address = entry.url.toString().right(55); - QString actionText = tr("%1 with (%2)").arg(address, entry.rule->filter()).replace(QLatin1Char('&'), QLatin1String("&&")); + QString action_text = tr("%1 with (%2)").arg(address, + entry.rule->filter()).replace(QLatin1Char('&'), QLatin1String("&&")); - QAction* action = menu->addAction(actionText, manager, SLOT(showRule())); + QAction *action = menu->addAction(action_text, manager, SLOT(showRule())); action->setData(QVariant::fromValue((void*)entry.rule)); } } } -void AdBlockIcon::showMenu(const QPoint &pos) -{ +void AdBlockIcon::showMenu(const QPoint &pos) { QMenu menu; createMenu(&menu); - menu.exec(pos); } -void AdBlockIcon::toggleCustomFilter() -{ - QAction* action = qobject_cast(sender()); - if (!action) { +void AdBlockIcon::toggleCustomFilter() { + QAction *action = qobject_cast(sender()); + + if (action == NULL) { return; } const QString filter = action->data().toString(); AdBlockManager* manager = AdBlockManager::instance(); - AdBlockCustomList* customList = manager->customList(); + AdBlockCustomList* custom_list = manager->customList(); - if (customList->containsFilter(filter)) { - customList->removeFilter(filter); + if (custom_list->containsFilter(filter)) { + custom_list->removeFilter(filter); } else { - AdBlockRule* rule = new AdBlockRule(filter, customList); - customList->addRule(rule); + AdBlockRule *rule = new AdBlockRule(filter, custom_list); + custom_list->addRule(rule); } } -void AdBlockIcon::animateIcon() -{ - ++m_timerTicks; +void AdBlockIcon::animateIcon() { + m_timerTicks++; + if (m_timerTicks > 10) { stopAnimation(); return; } if (icon().isNull()) { - setIcon(QIcon(":images/images/adblock.png")); + setIcon(qApp->icons()->fromTheme("web-adblock")); } else { setIcon(QIcon()); } } -void AdBlockIcon::stopAnimation() -{ +void AdBlockIcon::stopAnimation() { m_timerTicks = 0; m_flashTimer->stop(); - disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); + disconnect(m_flashTimer, SIGNAL(timeout()), this, SLOT(animateIcon())); setEnabled(m_enabled); } -void AdBlockIcon::setEnabled(bool enabled) -{ +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 && rect().contains(event->pos())) { - if (event->modifiers() != Qt::ControlModifier) { +void AdBlockIcon::mouseReleaseEvent(QMouseEvent *event) { + if (event->button() == Qt::LeftButton) { emit clicked(event->globalPos()); - } } else { QToolButton::mouseReleaseEvent(event); } } -AdBlockIcon::~AdBlockIcon() -{ +AdBlockIcon::~AdBlockIcon() { + qDebug("Destroying AdBlockIcon instance."); } diff --git a/src/network-web/adblock/adblockicon.h b/src/network-web/adblock/adblockicon.h index 259d8bec0..3adc4521f 100755 --- a/src/network-web/adblock/adblockicon.h +++ b/src/network-web/adblock/adblockicon.h @@ -1,37 +1,21 @@ -/* ============================================================ -* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader -* Copyright (C) 2011-2015 QuiteRSS Team -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program 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. -* -* This program 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 this program. If not, see . -* ============================================================ */ +// This file is part of RSS Guard. +// +// Copyright (C) 2014-2015 by Martin Rotter +// Copyright (C) 2010-2014 by David Rosca +// +// 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 . + #ifndef ADBLOCKICON_H #define ADBLOCKICON_H @@ -44,12 +28,13 @@ class AdBlockRule; class AdBlockIcon : public PlainToolButton { Q_OBJECT + public: // Constructors. explicit AdBlockIcon(QWidget *window, QWidget *parent = 0); virtual ~AdBlockIcon(); - void popupBlocked(const QString &ruleString, const QUrl &url); + void popupBlocked(const QString &rule_string, const QUrl &url); QAction *menuAction(); public slots: @@ -59,7 +44,6 @@ class AdBlockIcon : public PlainToolButton { private slots: void showMenu(const QPoint &pos); void toggleCustomFilter(); - void animateIcon(); void stopAnimation(); @@ -71,10 +55,10 @@ class AdBlockIcon : public PlainToolButton { private: QWidget *m_window; - QAction* m_menuAction; + QAction *m_menuAction; - QVector > m_blockedPopups; - QTimer* m_flashTimer; + QVector> m_blockedPopups; + QTimer *m_flashTimer; int m_timerTicks; bool m_enabled; diff --git a/src/network-web/adblock/adblockmanager.cpp b/src/network-web/adblock/adblockmanager.cpp index 0702d11a1..963191929 100755 --- a/src/network-web/adblock/adblockmanager.cpp +++ b/src/network-web/adblock/adblockmanager.cpp @@ -58,26 +58,20 @@ #include #endif -AdBlockManager* AdBlockManager::s_adBlockManager = 0; +AdBlockManager* AdBlockManager::s_adBlockManager = NULL; AdBlockManager::AdBlockManager(QObject* parent) - : QObject(parent) - , m_loaded(false) - , m_enabled(true) - , m_useLimitedEasyList(true) - , m_matcher(new AdBlockMatcher(this)), m_subscriptions(QList()) -{ + : QObject(parent), m_loaded(false), m_enabled(false), m_useLimitedEasyList(true), + m_matcher(new AdBlockMatcher(this)), m_subscriptions(QList()) { load(); } -AdBlockManager::~AdBlockManager() -{ +AdBlockManager::~AdBlockManager() { qDeleteAll(m_subscriptions); } -AdBlockManager* AdBlockManager::instance() -{ - if (!s_adBlockManager) { +AdBlockManager* AdBlockManager::instance() { + if (s_adBlockManager == NULL) { s_adBlockManager = new AdBlockManager(SilentNetworkAccessManager::instance()); } @@ -132,6 +126,7 @@ void AdBlockManager::setEnabled(bool enabled) } m_enabled = enabled; + emit enabledChanged(enabled); Settings *settings = qApp->settings(); @@ -139,7 +134,10 @@ void AdBlockManager::setEnabled(bool enabled) settings->setValue("enabled", m_enabled); settings->endGroup(); + // Load subscriptions and other data. load(); + + // Inform others (mainly ICON and MATCHER), that there are some changes. // TODO //mainApp->reloadUserStyleBrowser(); } @@ -207,10 +205,10 @@ void AdBlockManager::removeDisabledRule(const QString &filter) m_disabledRules.removeOne(filter); } -AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const QString &url) +AdBlockSubscription *AdBlockManager::addSubscription(const QString &title, const QString &url) { if (title.isEmpty() || url.isEmpty()) { - return 0; + return NULL; } QString fileName = filterCharsFromFilename(title.toLower()) + ".txt"; @@ -281,6 +279,7 @@ void AdBlockManager::load() QDateTime lastUpdate = settings->value("AdBlock","lastUpdate", QDateTime()).toDateTime(); if (!m_enabled) { + // We loaded settings, but Adblock should be disabled. Do not continue to save memory. return; } @@ -447,22 +446,12 @@ AdBlockSubscription* AdBlockManager::subscriptionByName(const QString &name) con } AdBlockDialog *AdBlockManager::showDialog() { - QPointer form_pointer = new AdBlockDialog(); + QPointer form_pointer = new AdBlockDialog(qApp->mainForm()); form_pointer.data()->show(); form_pointer.data()->raise(); form_pointer.data()->activateWindow(); form_pointer.data()->setAttribute(Qt::WA_DeleteOnClose, true); return form_pointer.data(); - /* - if (!m_adBlockDialog) { - m_adBlockDialog = new AdBlockDialog; - } - - m_adBlockDialog.data()->show(); - m_adBlockDialog.data()->raise(); - m_adBlockDialog.data()->activateWindow(); - - return m_adBlockDialog.data();*/ } void AdBlockManager::showRule() diff --git a/src/network-web/adblock/adblockmanager.h b/src/network-web/adblock/adblockmanager.h index 7b8451e81..87aa2adbb 100755 --- a/src/network-web/adblock/adblockmanager.h +++ b/src/network-web/adblock/adblockmanager.h @@ -35,8 +35,6 @@ #ifndef ADBLOCKMANAGER_H #define ADBLOCKMANAGER_H -#define ADBLOCK_EASYLIST_URL "https://easylist-downloads.adblockplus.org/easylist.txt" - #ifndef QSL #if QT_VERSION >= 0x050000 #define QSL(x) QStringLiteral(x) @@ -103,7 +101,7 @@ class AdBlockManager : public QObject void addDisabledRule(const QString &filter); void removeDisabledRule(const QString &filter); - AdBlockSubscription* addSubscription(const QString &title, const QString &url); + AdBlockSubscription *addSubscription(const QString &title, const QString &url); bool removeSubscription(AdBlockSubscription* subscription); AdBlockCustomList* customList() const; @@ -126,11 +124,10 @@ class AdBlockManager : public QObject bool m_useLimitedEasyList; QList m_subscriptions; - static AdBlockManager* s_adBlockManager; AdBlockMatcher* m_matcher; QStringList m_disabledRules; - QPointer m_adBlockDialog; + static AdBlockManager* s_adBlockManager; }; #endif // ADBLOCKMANAGER_H diff --git a/src/network-web/adblock/adblockmatcher.cpp b/src/network-web/adblock/adblockmatcher.cpp index a2bb29ede..0ae8c2817 100755 --- a/src/network-web/adblock/adblockmatcher.cpp +++ b/src/network-web/adblock/adblockmatcher.cpp @@ -37,10 +37,7 @@ #include "adblockrule.h" #include "adblocksubscription.h" -AdBlockMatcher::AdBlockMatcher(AdBlockManager* manager) - : QObject(manager) - , m_manager(manager) -{ +AdBlockMatcher::AdBlockMatcher(AdBlockManager* manager) : QObject(manager), m_manager(manager) { connect(manager, SIGNAL(enabledChanged(bool)), this, SLOT(enabledChanged(bool))); } @@ -117,7 +114,7 @@ QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const if (!rule->matchDomain(domain)) continue; - if (Q_UNLIKELY(addedRulesCount == 1000)) { + if (addedRulesCount == 1000) { rules.append(rule->cssSelector()); rules.append(QLatin1String("{display:none !important;}\n")); addedRulesCount = 0; @@ -205,7 +202,7 @@ void AdBlockMatcher::update() if (rule->isDomainRestricted()) { m_domainRestrictedCssRules.append(rule); } - else if (Q_UNLIKELY(hidingRulesCount == 1000)) { + else if (hidingRulesCount == 1000) { m_elementHidingRules.append(rule->cssSelector()); m_elementHidingRules.append(QLatin1String("{display:none !important;} ")); hidingRulesCount = 0; @@ -238,8 +235,10 @@ void AdBlockMatcher::clear() void AdBlockMatcher::enabledChanged(bool enabled) { - if (enabled) + if (enabled) { update(); - else + } + else { clear(); + } } diff --git a/src/network-web/adblock/adblockrule.h b/src/network-web/adblock/adblockrule.h index 0a5a53d0e..e5dc497a5 100755 --- a/src/network-web/adblock/adblockrule.h +++ b/src/network-web/adblock/adblockrule.h @@ -74,7 +74,7 @@ class AdBlockSubscription; class AdBlockRule { public: - AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = 0); + AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = NULL); ~AdBlockRule(); AdBlockRule* copy() const;