Helpviewer/HelpBrowserWV.py

changeset 676
6771da84fb0c
parent 675
2f626dcab902
child 747
48f13a14f754
equal deleted inserted replaced
675:2f626dcab902 676:6771da84fb0c
170 @param frame reference to the frame sending the request (QWebFrame) 170 @param frame reference to the frame sending the request (QWebFrame)
171 @param request reference to the request object (QNetworkRequest) 171 @param request reference to the request object (QNetworkRequest)
172 @param type_ type of the navigation request (QWebPage.NavigationType) 172 @param type_ type of the navigation request (QWebPage.NavigationType)
173 @return flag indicating acceptance (boolean) 173 @return flag indicating acceptance (boolean)
174 """ 174 """
175 # TODO: extend with more functionality
176 self.__lastRequest = request 175 self.__lastRequest = request
177 self.__lastRequestType = type_ 176 self.__lastRequestType = type_
178 177
179 if type_ == QWebPage.NavigationTypeFormResubmitted: 178 if type_ == QWebPage.NavigationTypeFormResubmitted:
180 res = E5MessageBox.yesNo(self.view(), 179 res = E5MessageBox.yesNo(self.view(),
1178 if reply.error() == QNetworkReply.NoError: 1177 if reply.error() == QNetworkReply.NoError:
1179 if reply.header(QNetworkRequest.ContentTypeHeader): 1178 if reply.header(QNetworkRequest.ContentTypeHeader):
1180 self.mw.downloadManager().handleUnsupportedContent( 1179 self.mw.downloadManager().handleUnsupportedContent(
1181 reply, webPage = self.page()) 1180 reply, webPage = self.page())
1182 return 1181 return
1183 else: 1182
1184 replyUrl = reply.url() 1183 replyUrl = reply.url()
1185 if replyUrl.isEmpty(): 1184 if replyUrl.isEmpty():
1185 return
1186
1187 notFoundFrame = self.page().mainFrame()
1188 if notFoundFrame is None:
1189 return
1190
1191 if reply.header(QNetworkRequest.ContentTypeHeader):
1192 data = reply.readAll()
1193 if contentSniff(data):
1194 notFoundFrame.setHtml(str(data, encoding = "utf-8"), replyUrl)
1186 return 1195 return
1187 1196
1188 html = notFoundPage_html 1197 html = notFoundPage_html
1189 urlString = bytes(replyUrl.toEncoded()).decode() 1198 urlString = bytes(replyUrl.toEncoded()).decode()
1190 title = self.trUtf8("Error loading page: {0}").format(urlString) 1199 title = self.trUtf8("Error loading page: {0}").format(urlString)
1191 pixmap = qApp.style()\ 1200 pixmap = qApp.style()\
1192 .standardIcon(QStyle.SP_MessageBoxWarning, None, self)\ 1201 .standardIcon(QStyle.SP_MessageBoxWarning, None, self)\
1193 .pixmap(32, 32) 1202 .pixmap(32, 32)
1194 imageBuffer = QBuffer() 1203 imageBuffer = QBuffer()
1195 imageBuffer.open(QIODevice.ReadWrite) 1204 imageBuffer.open(QIODevice.ReadWrite)
1196 if pixmap.save(imageBuffer, "PNG"): 1205 if pixmap.save(imageBuffer, "PNG"):
1197 html = html.replace("IMAGE_BINARY_DATA_HERE", 1206 html = html.replace("IMAGE_BINARY_DATA_HERE",
1198 str(imageBuffer.buffer().toBase64(), encoding="ascii")) 1207 str(imageBuffer.buffer().toBase64(), encoding="ascii"))
1199 html = html.format( 1208 html = html.format(
1200 title, 1209 title,
1201 reply.errorString(), 1210 reply.errorString(),
1202 self.trUtf8("When connecting to: {0}.").format(urlString), 1211 self.trUtf8("When connecting to: {0}.").format(urlString),
1203 self.trUtf8("Check the address for errors such as <b>ww</b>.example.org " 1212 self.trUtf8("Check the address for errors such as <b>ww</b>.example.org "
1204 "instead of <b>www</b>.example.org"), 1213 "instead of <b>www</b>.example.org"),
1205 self.trUtf8("If the address is correct, try checking the network " 1214 self.trUtf8("If the address is correct, try checking the network "
1206 "connection."), 1215 "connection."),
1207 self.trUtf8("If your computer or network is protected by a firewall or " 1216 self.trUtf8("If your computer or network is protected by a firewall or "
1208 "proxy, make sure that the browser is permitted to access " 1217 "proxy, make sure that the browser is permitted to access "
1209 "the network."), 1218 "the network."),
1210 self.trUtf8("If your cache policy is set to offline browsing," 1219 self.trUtf8("If your cache policy is set to offline browsing,"
1211 "only pages in the local cache are available.") 1220 "only pages in the local cache are available.")
1212 ) 1221 )
1213 self.setHtml(html, replyUrl) 1222 notFoundFrame.setHtml(html, replyUrl)
1214 self.mw.historyManager().removeHistoryEntry(replyUrl, self.title()) 1223 self.mw.historyManager().removeHistoryEntry(replyUrl, self.title())
1215 self.loadFinished.emit(False) 1224 self.loadFinished.emit(False)
1216 1225
1217 def __downloadRequested(self, request): 1226 def __downloadRequested(self, request):
1218 """ 1227 """
1219 Private slot to handle a download request. 1228 Private slot to handle a download request.
1220 1229
1289 def preferencesChanged(self): 1298 def preferencesChanged(self):
1290 """ 1299 """
1291 Public method to indicate a change of the settings. 1300 Public method to indicate a change of the settings.
1292 """ 1301 """
1293 self.reload() 1302 self.reload()
1303
1304 def contentSniff(data):
1305 """
1306 Module function to do some content sniffing to check, if the data is HTML.
1307
1308 @return flag indicating HTML content (boolean)
1309 """
1310 if data.contains("<!doctype") or \
1311 data.contains("<script") or \
1312 data.contains("<html") or \
1313 data.contains("<!--") or \
1314 data.contains("<head") or \
1315 data.contains("<iframe") or \
1316 data.contains("<h1") or \
1317 data.contains("<div") or \
1318 data.contains("<font") or \
1319 data.contains("<table") or \
1320 data.contains("<a") or \
1321 data.contains("<style") or \
1322 data.contains("<title") or \
1323 data.contains("<b") or \
1324 data.contains("<body") or \
1325 data.contains("<br") or \
1326 data.contains("<p"):
1327 return True
1328
1329 return False

eric ide

mercurial