1 /* ============================================================
2 * QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
3 * Copyright (C) 2011-2020 QuiteRSS Team <quiterssteam@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 * ============================================================ */
18 /* ============================================================
19 * QupZilla - WebKit based browser
20 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
21 *
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
34 * ============================================================ */
35 #include "adblockdialog.h"
36 #include "adblockmanager.h"
37 #include "adblocksubscription.h"
38 #include "adblocktreewidget.h"
39 #include "adblockaddsubscriptiondialog.h"
40 #include "mainapplication.h"
41 #include "mainwindow.h"
42 #include "common.h"
43 
44 #include <QMenu>
45 #include <QTimer>
46 #include <QMessageBox>
47 #include <QInputDialog>
48 
AdBlockDialog(QWidget * parent)49 AdBlockDialog::AdBlockDialog(QWidget* parent)
50   : QDialog(parent)
51   , m_manager(AdBlockManager::instance())
52   , m_currentTreeWidget(0)
53   , m_currentSubscription(0)
54   , m_loaded(false)
55   , m_useLimitedEasyList(false)
56 {
57   setModal(true);
58   setWindowFlags (windowFlags() & ~Qt::WindowContextHelpButtonHint);
59   setAttribute(Qt::WA_DeleteOnClose);
60   setupUi(this);
61 
62   const QRect screen = QApplication::desktop()->screenGeometry();
63   const QRect size = geometry();
64   move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2);
65   tabWidget->setDocumentMode(false);
66 #ifdef Q_OS_MAC
67   tabWidget->setDocumentMode(false);
68 #endif
69   adblockCheckBox->setChecked(m_manager->isEnabled());
70 
71   buttonOptions->setText(buttonOptions->text() % "   ");
72 
73   QMenu* menu = new QMenu(buttonOptions);
74   m_actionAddRule = menu->addAction(tr("Add Rule"), this, SLOT(addRule()));
75   m_actionRemoveRule = menu->addAction(tr("Remove Rule"), this, SLOT(removeRule()));
76   menu->addSeparator();
77   m_actionAddSubscription = menu->addAction(tr("Add Subscription"), this, SLOT(addSubscription()));
78   m_actionRemoveSubscription = menu->addAction(tr("Remove Subscription"), this, SLOT(removeSubscription()));
79   menu->addAction(tr("Update Subscriptions"), m_manager, SLOT(updateAllSubscriptions()));
80   menu->addSeparator();
81   menu->addAction(tr("Learn about writing rules..."), this, SLOT(learnAboutRules()));
82 
83   buttonOptions->setMenu(menu);
84   connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu()));
85 
86   connect(adblockCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableAdBlock(bool)));
87   connect(search, SIGNAL(textChanged(QString)), this, SLOT(filterString(QString)));
88   connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
89   connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
90 
91   load();
92 
93   buttonBox->setFocus();
94 }
95 
showRule(const AdBlockRule * rule) const96 void AdBlockDialog::showRule(const AdBlockRule* rule) const
97 {
98   AdBlockSubscription* subscription = rule->subscription();
99   if (!subscription) {
100     return;
101   }
102 
103   for (int i = 0; i < tabWidget->count(); ++i) {
104     AdBlockTreeWidget* treeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(i));
105 
106     if (subscription == treeWidget->subscription()) {
107       treeWidget->showRule(rule);
108       tabWidget->setCurrentIndex(i);
109       break;
110     }
111   }
112 }
113 
addRule()114 void AdBlockDialog::addRule()
115 {
116   m_currentTreeWidget->addRule();
117 }
118 
removeRule()119 void AdBlockDialog::removeRule()
120 {
121   m_currentTreeWidget->removeRule();
122 }
123 
addSubscription()124 void AdBlockDialog::addSubscription()
125 {
126   AdBlockAddSubscriptionDialog dialog(this);
127   if (dialog.exec() != QDialog::Accepted) {
128     return;
129   }
130 
131   QString title = dialog.title();
132   QString url = dialog.url();
133 
134   if (AdBlockSubscription* subscription = m_manager->addSubscription(title, url)) {
135     AdBlockTreeWidget* tree = new AdBlockTreeWidget(subscription, tabWidget);
136     int index = tabWidget->insertTab(tabWidget->count() - 1, tree, subscription->title());
137 
138     tabWidget->setCurrentIndex(index);
139   }
140 }
141 
removeSubscription()142 void AdBlockDialog::removeSubscription()
143 {
144   if (m_manager->removeSubscription(m_currentSubscription)) {
145     delete m_currentTreeWidget;
146   }
147 }
148 
currentChanged(int index)149 void AdBlockDialog::currentChanged(int index)
150 {
151   if (index != -1) {
152     m_currentTreeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(index));
153     m_currentSubscription = m_currentTreeWidget->subscription();
154 
155     bool isEasyList = m_currentSubscription->url() == QUrl(ADBLOCK_EASYLIST_URL);
156     useLimitedEasyList->setVisible(isEasyList);
157   }
158 }
159 
filterString(const QString & string)160 void AdBlockDialog::filterString(const QString &string)
161 {
162   if (m_currentTreeWidget && adblockCheckBox->isChecked()) {
163     m_currentTreeWidget->filterString(string);
164   }
165 }
166 
enableAdBlock(bool state)167 void AdBlockDialog::enableAdBlock(bool state)
168 {
169   m_manager->setEnabled(state);
170 
171   if (state) {
172     load();
173   }
174 }
175 
aboutToShowMenu()176 void AdBlockDialog::aboutToShowMenu()
177 {
178   bool subscriptionEditable = m_currentSubscription && m_currentSubscription->canEditRules();
179   bool subscriptionRemovable = m_currentSubscription && m_currentSubscription->canBeRemoved();
180 
181   m_actionAddRule->setEnabled(subscriptionEditable);
182   m_actionRemoveRule->setEnabled(subscriptionEditable);
183   m_actionRemoveSubscription->setEnabled(subscriptionRemovable);
184 }
185 
learnAboutRules()186 void AdBlockDialog::learnAboutRules()
187 {
188   mainApp->mainWindow()->openNewsTab_ = NEW_TAB_FOREGROUND;
189   mainApp->mainWindow()->createWebTab(QUrl("https://adblockplus.org/en/filters"));
190 }
191 
loadSubscriptions()192 void AdBlockDialog::loadSubscriptions()
193 {
194   for (int i = 0; i < tabWidget->count(); ++i) {
195     AdBlockTreeWidget* treeWidget = qobject_cast<AdBlockTreeWidget*>(tabWidget->widget(i));
196     treeWidget->refresh();
197   }
198 }
199 
load()200 void AdBlockDialog::load()
201 {
202   if (m_loaded || !adblockCheckBox->isChecked()) {
203     return;
204   }
205 
206   foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) {
207     AdBlockTreeWidget* tree = new AdBlockTreeWidget(subscription, tabWidget);
208     tabWidget->addTab(tree, subscription->title());
209   }
210 
211   m_useLimitedEasyList = m_manager->useLimitedEasyList();
212   useLimitedEasyList->setChecked(m_useLimitedEasyList);
213 
214   m_loaded = true;
215 
216   QTimer::singleShot(50, this, SLOT(loadSubscriptions()));
217 }
218 
closeEvent(QCloseEvent * ev)219 void AdBlockDialog::closeEvent(QCloseEvent* ev)
220 {
221   if (useLimitedEasyList->isChecked() != m_useLimitedEasyList) {
222     m_manager->setUseLimitedEasyList(useLimitedEasyList->isChecked());
223   }
224 
225   QWidget::closeEvent(ev);
226 }
227