eric7/WebBrowser/AdBlock/AdBlockDialog.py

branch
eric7
changeset 8312
800c432b34c8
parent 8260
2161475d9639
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the AdBlock configuration dialog.
8 """
9
10 from PyQt5.QtCore import pyqtSlot, Qt, QTimer, QCoreApplication
11 from PyQt5.QtWidgets import QDialog, QMenu, QToolButton
12
13 from E5Gui import E5MessageBox
14
15 from .Ui_AdBlockDialog import Ui_AdBlockDialog
16
17 import UI.PixmapCache
18 import Preferences
19
20
21 class AdBlockDialog(QDialog, Ui_AdBlockDialog):
22 """
23 Class implementing the AdBlock configuration dialog.
24 """
25 def __init__(self, manager, parent=None):
26 """
27 Constructor
28
29 @param manager reference to the AdBlock manager
30 @type AdBlockManager
31 @param parent reference to the parent object
32 @type QWidget
33 """
34 super().__init__(parent)
35 self.setupUi(self)
36 self.setWindowFlags(Qt.WindowType.Window)
37
38 self.__manager = manager
39
40 self.iconLabel.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus48"))
41
42 self.updateSpinBox.setValue(
43 Preferences.getWebBrowser("AdBlockUpdatePeriod"))
44
45 self.useLimitedEasyListCheckBox.setChecked(Preferences.getWebBrowser(
46 "AdBlockUseLimitedEasyList"))
47
48 self.searchEdit.setInactiveText(self.tr("Search..."))
49
50 self.adBlockGroup.setChecked(self.__manager.isEnabled())
51 self.__manager.requiredSubscriptionLoaded.connect(self.addSubscription)
52 self.__manager.enabledChanged.connect(self.__managerEnabledChanged)
53
54 self.__currentTreeWidget = None
55 self.__currentSubscription = None
56 self.__loaded = False
57
58 menu = QMenu(self)
59 menu.aboutToShow.connect(self.__aboutToShowActionMenu)
60 self.actionButton.setMenu(menu)
61 self.actionButton.setIcon(UI.PixmapCache.getIcon("adBlockAction"))
62 self.actionButton.setPopupMode(
63 QToolButton.ToolButtonPopupMode.InstantPopup)
64
65 self.__load()
66
67 self.buttonBox.setFocus()
68
69 def __loadSubscriptions(self):
70 """
71 Private slot to load the AdBlock subscription rules.
72 """
73 for index in range(self.subscriptionsTabWidget.count()):
74 tree = self.subscriptionsTabWidget.widget(index)
75 tree.refresh()
76
77 def __load(self):
78 """
79 Private slot to populate the tab widget with subscriptions.
80 """
81 if self.__loaded or not self.adBlockGroup.isChecked():
82 return
83
84 from .AdBlockTreeWidget import AdBlockTreeWidget
85 for subscription in self.__manager.subscriptions():
86 tree = AdBlockTreeWidget(subscription, self.subscriptionsTabWidget)
87 icon = (
88 UI.PixmapCache.getIcon("adBlockPlus")
89 if subscription.isEnabled() else
90 UI.PixmapCache.getIcon("adBlockPlusDisabled")
91 )
92 self.subscriptionsTabWidget.addTab(
93 tree, icon, subscription.title())
94
95 self.__loaded = True
96 QCoreApplication.processEvents()
97
98 QTimer.singleShot(50, self.__loadSubscriptions)
99
100 def addSubscription(self, subscription, refresh=True):
101 """
102 Public slot adding a subscription to the list.
103
104 @param subscription reference to the subscription to be
105 added
106 @type AdBlockSubscription
107 @param refresh flag indicating to refresh the tree
108 @type bool
109 """
110 from .AdBlockTreeWidget import AdBlockTreeWidget
111 tree = AdBlockTreeWidget(subscription, self.subscriptionsTabWidget)
112 index = self.subscriptionsTabWidget.insertTab(
113 self.subscriptionsTabWidget.count() - 1, tree,
114 subscription.title())
115 self.subscriptionsTabWidget.setCurrentIndex(index)
116 QCoreApplication.processEvents()
117 if refresh:
118 tree.refresh()
119 self.__setSubscriptionEnabled(subscription, True)
120
121 def __aboutToShowActionMenu(self):
122 """
123 Private slot to show the actions menu.
124 """
125 subscriptionEditable = (
126 self.__currentSubscription and
127 self.__currentSubscription.canEditRules()
128 )
129 subscriptionRemovable = (
130 self.__currentSubscription and
131 self.__currentSubscription.canBeRemoved()
132 )
133 subscriptionEnabled = (
134 self.__currentSubscription and
135 self.__currentSubscription.isEnabled()
136 )
137
138 menu = self.actionButton.menu()
139 menu.clear()
140
141 menu.addAction(
142 self.tr("Add Rule"), self.__addCustomRule
143 ).setEnabled(subscriptionEditable)
144 menu.addAction(
145 self.tr("Remove Rule"), self.__removeCustomRule
146 ).setEnabled(subscriptionEditable)
147 menu.addSeparator()
148 menu.addAction(
149 self.tr("Browse Subscriptions..."), self.__browseSubscriptions)
150 menu.addAction(
151 self.tr("Remove Subscription"), self.__removeSubscription
152 ).setEnabled(subscriptionRemovable)
153 if self.__currentSubscription:
154 menu.addSeparator()
155 if subscriptionEnabled:
156 txt = self.tr("Disable Subscription")
157 else:
158 txt = self.tr("Enable Subscription")
159 menu.addAction(txt, self.__switchSubscriptionEnabled)
160 menu.addSeparator()
161 menu.addAction(
162 self.tr("Update Subscription"), self.__updateSubscription
163 ).setEnabled(not subscriptionEditable)
164 menu.addAction(
165 self.tr("Update All Subscriptions"),
166 self.__updateAllSubscriptions)
167 menu.addSeparator()
168 menu.addAction(self.tr("Learn more about writing rules..."),
169 self.__learnAboutWritingFilters)
170
171 def addCustomRule(self, filterRule):
172 """
173 Public slot to add a custom AdBlock rule.
174
175 @param filterRule filter to be added
176 @type string
177 """
178 self.subscriptionsTabWidget.setCurrentIndex(
179 self.subscriptionsTabWidget.count() - 1)
180 self.__currentTreeWidget.addRule(filterRule)
181
182 def __addCustomRule(self):
183 """
184 Private slot to add a custom AdBlock rule.
185 """
186 self.__currentTreeWidget.addRule()
187
188 def __removeCustomRule(self):
189 """
190 Private slot to remove a custom AdBlock rule.
191 """
192 self.__currentTreeWidget.removeRule()
193
194 def __updateSubscription(self):
195 """
196 Private slot to update the selected subscription.
197 """
198 self.__currentSubscription.updateNow()
199
200 def __updateAllSubscriptions(self):
201 """
202 Private slot to update all subscriptions.
203 """
204 self.__manager.updateAllSubscriptions()
205
206 def __browseSubscriptions(self):
207 """
208 Private slot to browse the list of available AdBlock subscriptions.
209 """
210 from WebBrowser.WebBrowserWindow import WebBrowserWindow
211 mw = WebBrowserWindow.mainWindow()
212 mw.newTab("http://adblockplus.org/en/subscriptions")
213 mw.raise_()
214
215 def __learnAboutWritingFilters(self):
216 """
217 Private slot to show the web page about how to write filters.
218 """
219 from WebBrowser.WebBrowserWindow import WebBrowserWindow
220 mw = WebBrowserWindow.mainWindow()
221 mw.newTab("http://adblockplus.org/en/filters")
222 mw.raise_()
223
224 def __removeSubscription(self):
225 """
226 Private slot to remove the selected subscription.
227 """
228 requiresTitles = []
229 requiresSubscriptions = (
230 self.__manager.getRequiresSubscriptions(self.__currentSubscription)
231 )
232 for subscription in requiresSubscriptions:
233 requiresTitles.append(subscription.title())
234 message = (
235 self.tr(
236 "<p>Do you really want to remove subscription"
237 " <b>{0}</b> and all subscriptions requiring it?</p>"
238 "<ul><li>{1}</li></ul>").format(
239 self.__currentSubscription.title(),
240 "</li><li>".join(requiresTitles))
241 if requiresTitles else
242 self.tr(
243 "<p>Do you really want to remove subscription"
244 " <b>{0}</b>?</p>").format(self.__currentSubscription.title())
245 )
246 res = E5MessageBox.yesNo(
247 self,
248 self.tr("Remove Subscription"),
249 message)
250
251 if res:
252 removeSubscription = self.__currentSubscription
253 removeTrees = [self.__currentTreeWidget]
254 for index in range(self.subscriptionsTabWidget.count()):
255 tree = self.subscriptionsTabWidget.widget(index)
256 if tree.subscription() in requiresSubscriptions:
257 removeTrees.append(tree)
258 for tree in removeTrees:
259 self.subscriptionsTabWidget.removeTab(
260 self.subscriptionsTabWidget.indexOf(tree))
261 self.__manager.removeSubscription(removeSubscription)
262
263 def __switchSubscriptionEnabled(self):
264 """
265 Private slot to switch the enabled state of the selected subscription.
266 """
267 newState = not self.__currentSubscription.isEnabled()
268 self.__setSubscriptionEnabled(self.__currentSubscription, newState)
269
270 def __setSubscriptionEnabled(self, subscription, enable):
271 """
272 Private slot to set the enabled state of a subscription.
273
274 @param subscription subscription to set the state for
275 @type AdBlockSubscription
276 @param enable state to set to
277 @type bool
278 """
279 if enable:
280 # enable required one as well
281 sub = self.__manager.subscription(subscription.requiresLocation())
282 requiresSubscriptions = [] if sub is None else [sub]
283 icon = UI.PixmapCache.getIcon("adBlockPlus")
284 else:
285 # disable dependent ones as well
286 requiresSubscriptions = (
287 self.__manager.getRequiresSubscriptions(subscription)
288 )
289 icon = UI.PixmapCache.getIcon("adBlockPlusDisabled")
290 requiresSubscriptions.append(subscription)
291 for sub in requiresSubscriptions:
292 sub.setEnabled(enable)
293
294 for index in range(self.subscriptionsTabWidget.count()):
295 tree = self.subscriptionsTabWidget.widget(index)
296 if tree.subscription() in requiresSubscriptions:
297 self.subscriptionsTabWidget.setTabIcon(
298 self.subscriptionsTabWidget.indexOf(tree), icon)
299
300 @pyqtSlot(int)
301 def on_updateSpinBox_valueChanged(self, value):
302 """
303 Private slot to handle changes of the update period.
304
305 @param value update period
306 @type int
307 """
308 if value != Preferences.getWebBrowser("AdBlockUpdatePeriod"):
309 Preferences.setWebBrowser("AdBlockUpdatePeriod", value)
310
311 from WebBrowser.WebBrowserWindow import WebBrowserWindow
312 manager = WebBrowserWindow.adBlockManager()
313 for subscription in manager.subscriptions():
314 subscription.checkForUpdate()
315
316 @pyqtSlot(int)
317 def on_subscriptionsTabWidget_currentChanged(self, index):
318 """
319 Private slot handling the selection of another tab.
320
321 @param index index of the new current tab
322 @type int
323 """
324 if index != -1:
325 self.__currentTreeWidget = (
326 self.subscriptionsTabWidget.widget(index)
327 )
328 self.__currentSubscription = (
329 self.__currentTreeWidget.subscription()
330 )
331
332 isEasyList = (
333 self.__currentSubscription.url().toString().startswith(
334 self.__manager.getDefaultSubscriptionUrl())
335 )
336 self.useLimitedEasyListCheckBox.setVisible(isEasyList)
337
338 @pyqtSlot(str)
339 def on_searchEdit_textChanged(self, filterRule):
340 """
341 Private slot to set a new filter on the current widget.
342
343 @param filterRule filter to be set
344 @type str
345 """
346 if self.__currentTreeWidget and self.adBlockGroup.isChecked():
347 self.__currentTreeWidget.filterString(filterRule)
348
349 @pyqtSlot(bool)
350 def on_adBlockGroup_toggled(self, state):
351 """
352 Private slot handling the enabling/disabling of AdBlock.
353
354 @param state state of the toggle
355 @type bool
356 """
357 self.__manager.setEnabled(state)
358
359 if state:
360 self.__load()
361
362 @pyqtSlot(bool)
363 def on_useLimitedEasyListCheckBox_clicked(self, checked):
364 """
365 Private slot handling the selection of the limited EasyList.
366
367 @param checked flag indicating the state of the check box
368 @type bool
369 """
370 self.__manager.setUseLimitedEasyList(
371 self.useLimitedEasyListCheckBox.isChecked())
372
373 @pyqtSlot(bool)
374 def __managerEnabledChanged(self, enabled):
375 """
376 Private slot handling a change of the AdBlock manager enabled state.
377
378 @param enabled flag indicating the enabled state
379 @type bool
380 """
381 self.adBlockGroup.setChecked(enabled)

eric ide

mercurial