src/eric7/WebBrowser/AdBlock/AdBlockDialog.py

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

eric ide

mercurial