16 pyqtSignal, Qt, QObject, QByteArray, QDateTime, QUrl, QUrlQuery, |
16 pyqtSignal, Qt, QObject, QByteArray, QDateTime, QUrl, QUrlQuery, |
17 QCryptographicHash, QFile, QIODevice, QTextStream, QDate, QTime |
17 QCryptographicHash, QFile, QIODevice, QTextStream, QDate, QTime |
18 ) |
18 ) |
19 from PyQt6.QtNetwork import QNetworkReply, QNetworkRequest |
19 from PyQt6.QtNetwork import QNetworkReply, QNetworkRequest |
20 |
20 |
21 from E5Gui import E5MessageBox |
21 from E5Gui import EricMessageBox |
22 |
22 |
23 import Utilities |
23 import Utilities |
24 import Preferences |
24 import Preferences |
25 |
25 |
26 |
26 |
279 """ |
279 """ |
280 fileName = self.rulesFileName() |
280 fileName = self.rulesFileName() |
281 f = QFile(fileName) |
281 f = QFile(fileName) |
282 if f.exists(): |
282 if f.exists(): |
283 if not f.open(QIODevice.OpenModeFlag.ReadOnly): |
283 if not f.open(QIODevice.OpenModeFlag.ReadOnly): |
284 E5MessageBox.warning( |
284 EricMessageBox.warning( |
285 None, |
285 None, |
286 self.tr("Load subscription rules"), |
286 self.tr("Load subscription rules"), |
287 self.tr( |
287 self.tr( |
288 """Unable to open AdBlock file '{0}' for reading.""") |
288 """Unable to open AdBlock file '{0}' for reading.""") |
289 .format(fileName)) |
289 .format(fileName)) |
290 else: |
290 else: |
291 textStream = QTextStream(f) |
291 textStream = QTextStream(f) |
292 header = textStream.readLine(1024) |
292 header = textStream.readLine(1024) |
293 if not header.startswith("[Adblock"): |
293 if not header.startswith("[Adblock"): |
294 E5MessageBox.warning( |
294 EricMessageBox.warning( |
295 None, |
295 None, |
296 self.tr("Load subscription rules"), |
296 self.tr("Load subscription rules"), |
297 self.tr("""AdBlock file '{0}' does not start""" |
297 self.tr("""AdBlock file '{0}' does not start""" |
298 """ with [Adblock.""") |
298 """ with [Adblock.""") |
299 .format(fileName)) |
299 .format(fileName)) |
394 self.__downloading = None |
394 self.__downloading = None |
395 |
395 |
396 if reply.error() != QNetworkReply.NetworkError.NoError: |
396 if reply.error() != QNetworkReply.NetworkError.NoError: |
397 if not self.__defaultSubscription: |
397 if not self.__defaultSubscription: |
398 # don't show error if we try to load the default |
398 # don't show error if we try to load the default |
399 E5MessageBox.warning( |
399 EricMessageBox.warning( |
400 None, |
400 None, |
401 self.tr("Downloading subscription rules"), |
401 self.tr("Downloading subscription rules"), |
402 self.tr( |
402 self.tr( |
403 """<p>Subscription rules could not be""" |
403 """<p>Subscription rules could not be""" |
404 """ downloaded.</p><p>Error: {0}</p>""") |
404 """ downloaded.</p><p>Error: {0}</p>""") |
407 # reset after first download attempt |
407 # reset after first download attempt |
408 self.__defaultSubscription = False |
408 self.__defaultSubscription = False |
409 return |
409 return |
410 |
410 |
411 if response.isEmpty(): |
411 if response.isEmpty(): |
412 E5MessageBox.warning( |
412 EricMessageBox.warning( |
413 None, |
413 None, |
414 self.tr("Downloading subscription rules"), |
414 self.tr("Downloading subscription rules"), |
415 self.tr("""Got empty subscription rules.""")) |
415 self.tr("""Got empty subscription rules.""")) |
416 return |
416 return |
417 |
417 |
418 fileName = self.rulesFileName() |
418 fileName = self.rulesFileName() |
419 QFile.remove(fileName) |
419 QFile.remove(fileName) |
420 f = QFile(fileName) |
420 f = QFile(fileName) |
421 if not f.open(QIODevice.OpenModeFlag.ReadWrite): |
421 if not f.open(QIODevice.OpenModeFlag.ReadWrite): |
422 E5MessageBox.warning( |
422 EricMessageBox.warning( |
423 None, |
423 None, |
424 self.tr("Downloading subscription rules"), |
424 self.tr("Downloading subscription rules"), |
425 self.tr( |
425 self.tr( |
426 """Unable to open AdBlock file '{0}' for writing.""") |
426 """Unable to open AdBlock file '{0}' for writing.""") |
427 .file(fileName)) |
427 .file(fileName)) |
497 base64.b64encode(md5.digest()).decode().rstrip("=") |
497 base64.b64encode(md5.digest()).decode().rstrip("=") |
498 ) |
498 ) |
499 if calculatedChecksum == expectedChecksum: |
499 if calculatedChecksum == expectedChecksum: |
500 return True |
500 return True |
501 else: |
501 else: |
502 res = E5MessageBox.yesNo( |
502 res = EricMessageBox.yesNo( |
503 None, |
503 None, |
504 self.tr("Downloading subscription rules"), |
504 self.tr("Downloading subscription rules"), |
505 self.tr( |
505 self.tr( |
506 """<p>AdBlock subscription <b>{0}</b> has a wrong""" |
506 """<p>AdBlock subscription <b>{0}</b> has a wrong""" |
507 """ checksum.<br/>""" |
507 """ checksum.<br/>""" |
521 return |
521 return |
522 |
522 |
523 f = QFile(fileName) |
523 f = QFile(fileName) |
524 if not f.open(QIODevice.OpenModeFlag.ReadWrite | |
524 if not f.open(QIODevice.OpenModeFlag.ReadWrite | |
525 QIODevice.OpenModeFlag.Truncate): |
525 QIODevice.OpenModeFlag.Truncate): |
526 E5MessageBox.warning( |
526 EricMessageBox.warning( |
527 None, |
527 None, |
528 self.tr("Saving subscription rules"), |
528 self.tr("Saving subscription rules"), |
529 self.tr( |
529 self.tr( |
530 """Unable to open AdBlock file '{0}' for writing.""") |
530 """Unable to open AdBlock file '{0}' for writing.""") |
531 .format(fileName)) |
531 .format(fileName)) |