|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a scheme handler for the eric: scheme. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import QByteArray, QBuffer, QIODevice, QTextStream, \ |
|
13 QUrlQuery |
|
14 from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler |
|
15 |
|
16 from ..Tools.WebBrowserTools import readAllFileContents |
|
17 |
|
18 class EricSchemeHandler(QWebEngineUrlSchemeHandler): |
|
19 """ |
|
20 Class implementing a scheme handler for the eric: scheme. |
|
21 """ |
|
22 SupportedPages = [ |
|
23 "adblock", # error page for URLs blocked by AdBlock |
|
24 ] |
|
25 |
|
26 def __init__(self, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param parent reference to the parent object |
|
31 @type QObject |
|
32 """ |
|
33 super(EricSchemeHandler, self).__init__(parent) |
|
34 |
|
35 def requestStarted(self, job): |
|
36 """ |
|
37 Public method handling the URL request. |
|
38 |
|
39 @param job URL request job |
|
40 @type QWebEngineUrlRequestJob |
|
41 """ |
|
42 if job.requestUrl().path() in self.SupportedPages: |
|
43 job.reply(b"text/html", EricSchemeReply(job)) |
|
44 else: |
|
45 job.reply(QByteArray(), QBuffer()) |
|
46 # job.fail(QWebEngineUrlRequestJob.UrlNotFound) |
|
47 |
|
48 |
|
49 class EricSchemeReply(QIODevice): |
|
50 """ |
|
51 Class implementing a reply for a requested eric: page. |
|
52 """ |
|
53 # TODO: SchemeHandler: debug this further |
|
54 def __init__(self, job, parent=None): |
|
55 """ |
|
56 Constructor |
|
57 |
|
58 @param job reference to the URL request |
|
59 @type QWebEngineUrlRequestJob |
|
60 @param parent reference to the parent object |
|
61 @type QObject |
|
62 """ |
|
63 super(EricSchemeReply, self).__init__(parent) |
|
64 |
|
65 self.__loaded = False |
|
66 self.__job = job |
|
67 |
|
68 self.__pageName = self.__job.requestUrl().path() |
|
69 self.__buffer = QBuffer() |
|
70 |
|
71 self.open(QIODevice.ReadOnly) |
|
72 self.__buffer.open(QIODevice.ReadWrite) |
|
73 self.__loadPage() |
|
74 |
|
75 def __loadPage(self): |
|
76 """ |
|
77 Private method to load the requested page. |
|
78 """ |
|
79 if self.__loaded: |
|
80 return |
|
81 |
|
82 stream = QTextStream(self.__buffer) |
|
83 stream.setCodec("utf-8") |
|
84 |
|
85 if self.__pageName == "adblock": |
|
86 stream << self.__adBlockPage() |
|
87 |
|
88 stream.flush() |
|
89 self.__buffer.reset() |
|
90 self.__loaded = True |
|
91 |
|
92 def bytesAvailable(self): |
|
93 """ |
|
94 Public method to get the number of available bytes. |
|
95 |
|
96 @return number of available bytes |
|
97 @rtype int |
|
98 """ |
|
99 print("bytesAvailable", self.__buffer.bytesAvailable()) |
|
100 return self.__buffer.bytesAvailable() |
|
101 |
|
102 def readData(self, maxlen): |
|
103 """ |
|
104 Public method to retrieve data from the reply object. |
|
105 |
|
106 @param maxlen maximum number of bytes to read (integer) |
|
107 @return string containing the data (bytes) |
|
108 """ |
|
109 print("readData") |
|
110 self.__loadPage() |
|
111 return self.__buffer.read(maxlen) |
|
112 |
|
113 def writeData(self, data, len): |
|
114 return 0 |
|
115 |
|
116 def __adBlockPage(self): |
|
117 """ |
|
118 Private method to build the AdBlock page. |
|
119 |
|
120 @return built AdBlock page |
|
121 @rtype str |
|
122 """ |
|
123 query = QUrlQuery(self.__job.requestUrl()) |
|
124 rule = query.queryItemValue("rule") |
|
125 subscription = query.queryItemValue("subscription") |
|
126 title = self.tr("Content blocked by AdBlock Plus") |
|
127 message = self.tr( |
|
128 "Blocked by rule: <i>{0} ({1})</i>").format(rule, subscription) |
|
129 |
|
130 page = readAllFileContents(":/html/adblockPage.html") |
|
131 page = page.replace( |
|
132 "@FAVICON@", "qrc:icons/adBlockPlus16.png") |
|
133 page = page.replace( |
|
134 "@IMAGE@", "qrc:icons/adBlockPlus64.png") |
|
135 page = page.replace("@TITLE@", title) |
|
136 page = page.replace("@MESSAGE@", message) |
|
137 |
|
138 return page |