src/eric7/WebBrowser/AdBlock/AdBlockDialog.py

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

eric ide

mercurial