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