2011-03-27 21:59:40 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-03-06 09:05:41 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
2011-03-27 21:59:40 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "adblockdialog.h"
|
|
|
|
#include "adblockmanager.h"
|
|
|
|
#include "adblocksubscription.h"
|
2012-06-24 23:46:32 +02:00
|
|
|
#include "adblocktreewidget.h"
|
|
|
|
#include "adblockaddsubscriptiondialog.h"
|
2011-03-29 20:30:05 +02:00
|
|
|
#include "mainapplication.h"
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
2012-07-11 18:30:00 +02:00
|
|
|
#include <QTimer>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
AdBlockDialog::AdBlockDialog(QWidget* parent)
|
2011-03-27 21:59:40 +02:00
|
|
|
: QDialog(parent)
|
2011-03-29 20:30:05 +02:00
|
|
|
, m_manager(AdBlockManager::instance())
|
2012-06-24 23:46:32 +02:00
|
|
|
, m_currentTreeWidget(0)
|
|
|
|
, m_currentSubscription(0)
|
2012-07-01 20:38:37 +02:00
|
|
|
, m_loaded(false)
|
2013-11-03 16:04:38 +01:00
|
|
|
, m_useLimitedEasyList(false)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2011-05-07 12:59:53 +02:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2011-03-27 21:59:40 +02:00
|
|
|
setupUi(this);
|
2013-02-20 21:50:07 +01:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
tabWidget->setDocumentMode(false);
|
|
|
|
#endif
|
2012-06-24 23:46:32 +02:00
|
|
|
adblockCheckBox->setChecked(m_manager->isEnabled());
|
2011-07-31 00:50:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
QMenu* menu = new QMenu(buttonMenu);
|
|
|
|
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()));
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
buttonMenu->setMenu(menu);
|
|
|
|
connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu()));
|
2011-03-29 21:51:27 +02:00
|
|
|
|
2012-07-01 20:38:37 +02:00
|
|
|
connect(adblockCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool)));
|
2012-06-24 23:46:32 +02:00
|
|
|
connect(search, SIGNAL(textChanged(QString)), this, SLOT(filterString(QString)));
|
|
|
|
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
|
2013-11-03 16:04:38 +01:00
|
|
|
connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
|
2011-03-29 21:51:27 +02:00
|
|
|
|
2012-07-01 20:38:37 +02:00
|
|
|
load();
|
2011-03-29 21:51:27 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
buttonBox->setFocus();
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-07-01 18:13:49 +02:00
|
|
|
void AdBlockDialog::showRule(const AdBlockRule* rule) const
|
|
|
|
{
|
|
|
|
AdBlockSubscription* subscription = rule->subscription();
|
|
|
|
if (!subscription) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tabWidget->count(); ++i) {
|
|
|
|
AdBlockTreeWidget* treeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(i));
|
|
|
|
|
|
|
|
if (subscription == treeWidget->subscription()) {
|
|
|
|
treeWidget->showRule(rule);
|
|
|
|
tabWidget->setCurrentIndex(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::addRule()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
m_currentTreeWidget->addRule();
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::removeRule()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
m_currentTreeWidget->removeRule();
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::addSubscription()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
AdBlockAddSubscriptionDialog dialog(this);
|
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
2011-03-27 21:59:40 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
QString title = dialog.title();
|
|
|
|
QString url = dialog.url();
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
if (AdBlockSubscription* subscription = m_manager->addSubscription(title, url)) {
|
|
|
|
AdBlockTreeWidget* tree = new AdBlockTreeWidget(subscription, tabWidget);
|
|
|
|
int index = tabWidget->insertTab(tabWidget->count() - 1, tree, subscription->title());
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
tabWidget->setCurrentIndex(index);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-06-24 23:46:32 +02:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::removeSubscription()
|
|
|
|
{
|
|
|
|
if (m_manager->removeSubscription(m_currentSubscription)) {
|
|
|
|
delete m_currentTreeWidget;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-06-24 23:46:32 +02:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::currentChanged(int index)
|
|
|
|
{
|
|
|
|
if (index != -1) {
|
|
|
|
m_currentTreeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(index));
|
|
|
|
m_currentSubscription = m_currentTreeWidget->subscription();
|
2013-11-03 16:04:38 +01:00
|
|
|
|
|
|
|
bool isEasyList = m_currentSubscription->url() == QUrl(ADBLOCK_EASYLIST_URL);
|
|
|
|
useLimitedEasyList->setVisible(isEasyList);
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
2012-06-24 23:46:32 +02:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::filterString(const QString &string)
|
|
|
|
{
|
2012-07-01 20:38:37 +02:00
|
|
|
if (m_currentTreeWidget && adblockCheckBox->isChecked()) {
|
|
|
|
m_currentTreeWidget->filterString(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockDialog::enableAdBlock(bool state)
|
|
|
|
{
|
|
|
|
m_manager->setEnabled(state);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
load();
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::aboutToShowMenu()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-07-01 20:38:37 +02:00
|
|
|
bool subscriptionEditable = m_currentSubscription && m_currentSubscription->canEditRules();
|
|
|
|
bool subscriptionRemovable = m_currentSubscription && m_currentSubscription->canBeRemoved();
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
m_actionAddRule->setEnabled(subscriptionEditable);
|
|
|
|
m_actionRemoveRule->setEnabled(subscriptionEditable);
|
|
|
|
m_actionRemoveSubscription->setEnabled(subscriptionRemovable);
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 23:46:32 +02:00
|
|
|
void AdBlockDialog::learnAboutRules()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
mApp->addNewTab(QUrl("http://adblockplus.org/en/filters"));
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
2012-07-01 20:38:37 +02:00
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
void AdBlockDialog::loadSubscriptions()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < tabWidget->count(); ++i) {
|
|
|
|
AdBlockTreeWidget* treeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(i));
|
|
|
|
treeWidget->refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:38:37 +02:00
|
|
|
void AdBlockDialog::load()
|
|
|
|
{
|
|
|
|
if (m_loaded || !adblockCheckBox->isChecked()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) {
|
2012-07-01 20:38:37 +02:00
|
|
|
AdBlockTreeWidget* tree = new AdBlockTreeWidget(subscription, tabWidget);
|
|
|
|
tabWidget->addTab(tree, subscription->title());
|
|
|
|
}
|
|
|
|
|
2013-11-03 16:04:38 +01:00
|
|
|
m_useLimitedEasyList = m_manager->useLimitedEasyList();
|
|
|
|
useLimitedEasyList->setChecked(m_useLimitedEasyList);
|
|
|
|
|
2012-07-01 20:38:37 +02:00
|
|
|
m_loaded = true;
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
QTimer::singleShot(50, this, SLOT(loadSubscriptions()));
|
2012-07-01 20:38:37 +02:00
|
|
|
}
|
2013-11-03 16:04:38 +01:00
|
|
|
|
|
|
|
void AdBlockDialog::closeEvent(QCloseEvent* ev)
|
|
|
|
{
|
|
|
|
if (useLimitedEasyList->isChecked() != m_useLimitedEasyList) {
|
|
|
|
m_manager->setUseLimitedEasyList(useLimitedEasyList->isChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog::closeEvent(ev);
|
|
|
|
}
|