7 Module implementing a scheme handler for the eric: scheme. |
7 Module implementing a scheme handler for the eric: scheme. |
8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import QByteArray, QBuffer, QIODevice, QTextStream, \ |
12 from PyQt5.QtCore import pyqtSignal, QByteArray, QBuffer, QIODevice, \ |
13 QUrlQuery |
13 QTextStream, QUrlQuery |
14 from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler |
14 from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler |
15 |
15 |
16 from ..Tools.WebBrowserTools import readAllFileContents |
16 from ..Tools.WebBrowserTools import readAllFileContents |
17 |
17 |
18 class EricSchemeHandler(QWebEngineUrlSchemeHandler): |
18 class EricSchemeHandler(QWebEngineUrlSchemeHandler): |
29 |
29 |
30 @param parent reference to the parent object |
30 @param parent reference to the parent object |
31 @type QObject |
31 @type QObject |
32 """ |
32 """ |
33 super(EricSchemeHandler, self).__init__(parent) |
33 super(EricSchemeHandler, self).__init__(parent) |
|
34 |
|
35 self.__replies = [] |
34 |
36 |
35 def requestStarted(self, job): |
37 def requestStarted(self, job): |
36 """ |
38 """ |
37 Public method handling the URL request. |
39 Public method handling the URL request. |
38 |
40 |
39 @param job URL request job |
41 @param job URL request job |
40 @type QWebEngineUrlRequestJob |
42 @type QWebEngineUrlRequestJob |
41 """ |
43 """ |
42 if job.requestUrl().path() in self.SupportedPages: |
44 if job.requestUrl().path() in self.SupportedPages: |
43 job.reply(b"text/html", EricSchemeReply(job)) |
45 reply = EricSchemeReply(job) |
|
46 reply.closed.connect(self.__replyClosed) |
|
47 self.__replies.append(reply) |
|
48 job.reply(b"text/html", reply) |
44 else: |
49 else: |
45 job.reply(QByteArray(), QBuffer()) |
50 job.reply(QByteArray(), QBuffer()) |
46 # job.fail(QWebEngineUrlRequestJob.UrlNotFound) |
51 # job.fail(QWebEngineUrlRequestJob.UrlNotFound) |
|
52 |
|
53 def __replyClosed(self): |
|
54 """ |
|
55 Private slot handling the closed signal of a reply. |
|
56 """ |
|
57 object = self.sender() |
|
58 if object and object in self.__replies: |
|
59 self.__replies.remove(object) |
47 |
60 |
48 |
61 |
49 class EricSchemeReply(QIODevice): |
62 class EricSchemeReply(QIODevice): |
50 """ |
63 """ |
51 Class implementing a reply for a requested eric: page. |
64 Class implementing a reply for a requested eric: page. |
|
65 |
|
66 @signal closed emitted to signal that the web engine has read |
|
67 the data |
52 """ |
68 """ |
53 # TODO: SchemeHandler: debug this further |
69 closed = pyqtSignal() |
|
70 |
54 def __init__(self, job, parent=None): |
71 def __init__(self, job, parent=None): |
55 """ |
72 """ |
56 Constructor |
73 Constructor |
57 |
74 |
58 @param job reference to the URL request |
75 @param job reference to the URL request |
94 Public method to get the number of available bytes. |
111 Public method to get the number of available bytes. |
95 |
112 |
96 @return number of available bytes |
113 @return number of available bytes |
97 @rtype int |
114 @rtype int |
98 """ |
115 """ |
99 print("bytesAvailable", self.__buffer.bytesAvailable()) |
|
100 return self.__buffer.bytesAvailable() |
116 return self.__buffer.bytesAvailable() |
101 |
117 |
102 def readData(self, maxlen): |
118 def readData(self, maxlen): |
103 """ |
119 """ |
104 Public method to retrieve data from the reply object. |
120 Public method to retrieve data from the reply object. |
105 |
121 |
106 @param maxlen maximum number of bytes to read (integer) |
122 @param maxlen maximum number of bytes to read (integer) |
107 @return string containing the data (bytes) |
123 @return string containing the data (bytes) |
108 """ |
124 """ |
109 print("readData") |
|
110 self.__loadPage() |
|
111 return self.__buffer.read(maxlen) |
125 return self.__buffer.read(maxlen) |
112 |
126 |
113 def writeData(self, data, len): |
127 def close(self): |
114 return 0 |
128 """ |
|
129 Public method used to cloase the reply. |
|
130 """ |
|
131 super(EricSchemeReply, self).close() |
|
132 self.closed.emit() |
115 |
133 |
116 def __adBlockPage(self): |
134 def __adBlockPage(self): |
117 """ |
135 """ |
118 Private method to build the AdBlock page. |
136 Private method to build the AdBlock page. |
119 |
137 |