Helpviewer/AdBlock/AdBlockSubscription.py

changeset 483
ca7d8599a575
parent 464
a2b1d1770ef0
child 539
87f9bce38a44
equal deleted inserted replaced
482:4650a72c307a 483:ca7d8599a575
24 Class implementing the AdBlock subscription. 24 Class implementing the AdBlock subscription.
25 25
26 @signal changed() emitted after the subscription has changed 26 @signal changed() emitted after the subscription has changed
27 @signal rulesChanged() emitted after the subscription's rules have changed 27 @signal rulesChanged() emitted after the subscription's rules have changed
28 """ 28 """
29 changed = pyqtSignal()
30 rulesChanged = pyqtSignal()
31
29 def __init__(self, url, parent = None): 32 def __init__(self, url, parent = None):
30 """ 33 """
31 Constructor 34 Constructor
32 35
33 @param url AdBlock URL for the subscription (QUrl) 36 @param url AdBlock URL for the subscription (QUrl)
114 if self.__enabled == enabled: 117 if self.__enabled == enabled:
115 return 118 return
116 119
117 self.__enabled = enabled 120 self.__enabled = enabled
118 self.__populateCache() 121 self.__populateCache()
119 self.emit(SIGNAL("changed()")) 122 self.changed.emit()
120 123
121 def title(self): 124 def title(self):
122 """ 125 """
123 Public method to get the subscription title. 126 Public method to get the subscription title.
124 127
134 """ 137 """
135 if self.__title == title: 138 if self.__title == title:
136 return 139 return
137 140
138 self.__title = title 141 self.__title = title
139 self.emit(SIGNAL("changed()")) 142 self.changed.emit()
140 143
141 def location(self): 144 def location(self):
142 """ 145 """
143 Public method to get the subscription location. 146 Public method to get the subscription location.
144 147
155 if url == self.location(): 158 if url == self.location():
156 return 159 return
157 160
158 self.__location = url.toEncoded() 161 self.__location = url.toEncoded()
159 self.__lastUpdate = QDateTime() 162 self.__lastUpdate = QDateTime()
160 self.emit(SIGNAL("changed()")) 163 self.changed.emit()
161 164
162 def lastUpdate(self): 165 def lastUpdate(self):
163 """ 166 """
164 Public method to get the date and time of the last update. 167 Public method to get the date and time of the last update.
165 168
216 self.__rules = [] 219 self.__rules = []
217 while not textStream.atEnd(): 220 while not textStream.atEnd():
218 line = textStream.readLine() 221 line = textStream.readLine()
219 self.__rules.append(AdBlockRule(line)) 222 self.__rules.append(AdBlockRule(line))
220 self.__populateCache() 223 self.__populateCache()
221 self.emit(SIGNAL("changed()")) 224 self.changed.emit()
222 225
223 if not self.__lastUpdate.isValid() or \ 226 if not self.__lastUpdate.isValid() or \
224 self.__lastUpdate.addDays(7) < QDateTime.currentDateTime(): 227 self.__lastUpdate.addDays(7) < QDateTime.currentDateTime():
225 self.updateNow() 228 self.updateNow()
226 229
235 return 238 return
236 239
237 if self.location().scheme() == "file": 240 if self.location().scheme() == "file":
238 self.__lastUpdate = QDateTime.currentDateTime() 241 self.__lastUpdate = QDateTime.currentDateTime()
239 self.__loadRules() 242 self.__loadRules()
240 self.emit(SIGNAL("changed()")) 243 self.changed.emit()
241 return 244 return
242 245
243 request = QNetworkRequest(self.location()) 246 request = QNetworkRequest(self.location())
244 self.__downloading = \ 247 self.__downloading = \
245 Helpviewer.HelpWindow.HelpWindow.networkAccessManager().get(request) 248 Helpviewer.HelpWindow.HelpWindow.networkAccessManager().get(request)
285 .file(fileName)) 288 .file(fileName))
286 return 289 return
287 f.write(response) 290 f.write(response)
288 self.__lastUpdate = QDateTime.currentDateTime() 291 self.__lastUpdate = QDateTime.currentDateTime()
289 self.__loadRules() 292 self.__loadRules()
290 self.emit(SIGNAL("changed()")) 293 self.changed.emit()
291 self.__downloading = None 294 self.__downloading = None
292 295
293 def saveRules(self): 296 def saveRules(self):
294 """ 297 """
295 Public method to save the subscription rules. 298 Public method to save the subscription rules.
357 360
358 @param rule reference to the rule to add (AdBlockRule) 361 @param rule reference to the rule to add (AdBlockRule)
359 """ 362 """
360 self.__rules.append(rule) 363 self.__rules.append(rule)
361 self.__populateCache() 364 self.__populateCache()
362 self.emit(SIGNAL("rulesChanged()")) 365 self.rulesChanged.emit()
363 366
364 def removeRule(self, offset): 367 def removeRule(self, offset):
365 """ 368 """
366 Public method to remove a rule given the offset. 369 Public method to remove a rule given the offset.
367 370
370 if offset < 0 or offset > len(self.__rules): 373 if offset < 0 or offset > len(self.__rules):
371 return 374 return
372 375
373 del self.__rules[offset] 376 del self.__rules[offset]
374 self.__populateCache() 377 self.__populateCache()
375 self.emit(SIGNAL("rulesChanged()")) 378 self.rulesChanged.emit()
376 379
377 def replaceRule(self, rule, offset): 380 def replaceRule(self, rule, offset):
378 """ 381 """
379 Public method to replace a rule given the offset. 382 Public method to replace a rule given the offset.
380 383
381 @param rule reference to the rule to set (AdBlockRule) 384 @param rule reference to the rule to set (AdBlockRule)
382 @param offset offset of the rule to remove (integer) 385 @param offset offset of the rule to remove (integer)
383 """ 386 """
384 self.__rules[offset] = rule 387 self.__rules[offset] = rule
385 self.__populateCache() 388 self.__populateCache()
386 self.emit(SIGNAL("rulesChanged()")) 389 self.rulesChanged.emit()
387 390
388 def __populateCache(self): 391 def __populateCache(self):
389 """ 392 """
390 Private method to populate the various rule caches. 393 Private method to populate the various rule caches.
391 """ 394 """

eric ide

mercurial