|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the AdBlock manager. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import * |
|
13 |
|
14 import Helpviewer.HelpWindow |
|
15 |
|
16 from AdBlockNetwork import AdBlockNetwork |
|
17 from AdBlockPage import AdBlockPage |
|
18 from AdBlockSubscription import AdBlockSubscription |
|
19 from AdBlockDialog import AdBlockDialog |
|
20 |
|
21 from Utilities.AutoSaver import AutoSaver |
|
22 import Utilities |
|
23 import Preferences |
|
24 |
|
25 class AdBlockManager(QObject): |
|
26 """ |
|
27 Class implementing the AdBlock manager. |
|
28 |
|
29 @signal rulesChanged() emitted after some rule has changed |
|
30 """ |
|
31 def __init__(self, parent = None): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param parent reference to the parent object (QObject) |
|
36 """ |
|
37 QObject.__init__(self, parent) |
|
38 |
|
39 self.__loaded = False |
|
40 self.__enabled = False |
|
41 self.__adBlockDialog = None |
|
42 self.__adBlockNetwork = None |
|
43 self.__adBlockPage = None |
|
44 self.__subscriptions = [] |
|
45 self.__saveTimer = AutoSaver(self, self.save) |
|
46 |
|
47 self.connect(self, SIGNAL("rulesChanged()"), self.__saveTimer.changeOccurred) |
|
48 |
|
49 def close(self): |
|
50 """ |
|
51 Public method to close the open search engines manager. |
|
52 """ |
|
53 self.__saveTimer.saveIfNeccessary() |
|
54 |
|
55 def isEnabled(self): |
|
56 """ |
|
57 Public method to check, if blocking ads is enabled. |
|
58 |
|
59 @return flag indicating the enabled state (boolean) |
|
60 """ |
|
61 if not self.__loaded: |
|
62 self.load() |
|
63 |
|
64 return self.__enabled |
|
65 |
|
66 def setEnabled(self, enabled): |
|
67 """ |
|
68 Public slot to set the enabled state. |
|
69 |
|
70 @param enabled flag indicating the enabled state (boolean) |
|
71 """ |
|
72 if self.isEnabled() == enabled: |
|
73 return |
|
74 |
|
75 self.__enabled = enabled |
|
76 self.emit(SIGNAL("rulesChanged()")) |
|
77 |
|
78 def network(self): |
|
79 """ |
|
80 Public method to get a reference to the network block object. |
|
81 |
|
82 @return reference to the network block object (AdBlockNetwork) |
|
83 """ |
|
84 if self.__adBlockNetwork is None: |
|
85 self.__adBlockNetwork = AdBlockNetwork(self) |
|
86 return self.__adBlockNetwork |
|
87 |
|
88 def page(self): |
|
89 """ |
|
90 Public method to get a reference to the page block object. |
|
91 |
|
92 @return reference to the page block object (AdBlockPage) |
|
93 """ |
|
94 if self.__adBlockPage is None: |
|
95 self.__adBlockPage = AdBlockPage(self) |
|
96 return self.__adBlockPage |
|
97 |
|
98 def __customSubscriptionLocation(self): |
|
99 """ |
|
100 Private method to generate the path for custom subscriptions. |
|
101 |
|
102 @return URL for custom subscriptions (QUrl) |
|
103 """ |
|
104 dataDir = os.path.join(Utilities.getConfigDir(), "browser", "subscriptions") |
|
105 if not os.path.exists(dataDir): |
|
106 os.makedirs(dataDir) |
|
107 fileName = os.path.join(dataDir, "adblock_subscription_custom") |
|
108 return QUrl.fromLocalFile(fileName) |
|
109 |
|
110 def __customSubscriptionUrl(self): |
|
111 """ |
|
112 Private method to generate the URL for custom subscriptions. |
|
113 |
|
114 @return URL for custom subscriptions (QUrl) |
|
115 """ |
|
116 location = self.__customSubscriptionLocation() |
|
117 encodedUrl = unicode(location.toEncoded()) |
|
118 url = QUrl("abp:subscribe?location=%s&title=%s" % \ |
|
119 (encodedUrl, self.trUtf8("Custom Rules"))) |
|
120 return url |
|
121 |
|
122 def customRules(self): |
|
123 """ |
|
124 Public method to get a subscription for custom rules. |
|
125 |
|
126 @return subscription object for custom rules (AdBlockSubscription) |
|
127 """ |
|
128 location = self.__customSubscriptionLocation() |
|
129 for subscription in self.__subscriptions: |
|
130 if subscription.location() == location: |
|
131 return subscription |
|
132 |
|
133 url = self.__customSubscriptionUrl() |
|
134 customAdBlockSubscription = AdBlockSubscription(url, self) |
|
135 self.addSubscription(customAdBlockSubscription) |
|
136 return customAdBlockSubscription |
|
137 |
|
138 def subscriptions(self): |
|
139 """ |
|
140 Public method to get all subscriptions. |
|
141 |
|
142 @return list of subscriptions (list of AdBlockSubscription) |
|
143 """ |
|
144 if not self.__loaded: |
|
145 self.load() |
|
146 |
|
147 return self.__subscriptions[:] |
|
148 |
|
149 def removeSubscription(self, subscription): |
|
150 """ |
|
151 Public method to remove an AdBlock subscription. |
|
152 |
|
153 @param subscription AdBlock subscription to be removed (AdBlockSubscription) |
|
154 """ |
|
155 if subscription is None: |
|
156 return |
|
157 |
|
158 try: |
|
159 self.__subscriptions.remove(subscription) |
|
160 rulesFileName = subscription.rulesFileName() |
|
161 if subscription.parent() == self: |
|
162 subscription.deleteLater() |
|
163 QFile.remove(rulesFileName) |
|
164 self.emit(SIGNAL("rulesChanged()")) |
|
165 except ValueError: |
|
166 pass |
|
167 |
|
168 def addSubscription(self, subscription): |
|
169 """ |
|
170 Public method to add an AdBlock subscription. |
|
171 |
|
172 @param subscription AdBlock subscription to be added (AdBlockSubscription) |
|
173 """ |
|
174 if subscription is None: |
|
175 return |
|
176 |
|
177 self.__subscriptions.append(subscription) |
|
178 |
|
179 self.connect(subscription, SIGNAL("rulesChanged()"), |
|
180 self, SIGNAL("rulesChanged()")) |
|
181 self.connect(subscription, SIGNAL("changed()"), |
|
182 self, SIGNAL("rulesChanged()")) |
|
183 |
|
184 self.emit(SIGNAL("rulesChanged()")) |
|
185 |
|
186 def save(self): |
|
187 """ |
|
188 Public method to save the AdBlock subscriptions. |
|
189 """ |
|
190 if not self.__loaded: |
|
191 return |
|
192 |
|
193 Preferences.setHelp("AdBlockEnabled", int(self.__enabled)) |
|
194 subscriptions = [] |
|
195 for subscription in self.__subscriptions: |
|
196 if subscription is None: |
|
197 continue |
|
198 subscriptions.append(unicode(subscription.url().toEncoded())) |
|
199 subscription.saveRules() |
|
200 Preferences.setHelp("AdBlockSubscriptions", subscriptions) |
|
201 |
|
202 def load(self): |
|
203 """ |
|
204 Public method to load the AdBlock subscriptions. |
|
205 """ |
|
206 if self.__loaded: |
|
207 return |
|
208 |
|
209 self.__loaded = True |
|
210 |
|
211 self.__enabled = bool(Preferences.getHelp("AdBlockEnabled")) |
|
212 |
|
213 defaultSubscriptions = [] |
|
214 defaultSubscriptions.append( |
|
215 unicode(self.__customSubscriptionUrl().toEncoded())) |
|
216 defaultSubscriptions.append( |
|
217 "abp:subscribe?location=http://adblockplus.mozdev.org/easylist/easylist.txt&title=EasyList") |
|
218 |
|
219 subscriptions = Preferences.getHelp("AdBlockSubscriptions") |
|
220 if len(subscriptions) == 0: |
|
221 subscriptions = defaultSubscriptions |
|
222 for subscription in subscriptions: |
|
223 url = QUrl.fromEncoded(subscription) |
|
224 adBlockSubscription = AdBlockSubscription(url, self) |
|
225 self.connect(adBlockSubscription, SIGNAL("rulesChanged()"), |
|
226 self, SIGNAL("rulesChanged()")) |
|
227 self.connect(adBlockSubscription, SIGNAL("changed()"), |
|
228 self, SIGNAL("rulesChanged()")) |
|
229 self.__subscriptions.append(adBlockSubscription) |
|
230 |
|
231 def showDialog(self): |
|
232 """ |
|
233 Public slot to show the AdBlock subscription management dialog. |
|
234 """ |
|
235 if self.__adBlockDialog is None: |
|
236 self.__adBlockDialog = AdBlockDialog() |
|
237 |
|
238 self.__adBlockDialog.show() |
|
239 return self.__adBlockDialog |