|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a scheme access handler for QtHelp. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import mimetypes |
|
13 import os |
|
14 |
|
15 from PyQt5.QtCore import QByteArray |
|
16 |
|
17 from .SchemeAccessHandler import SchemeAccessHandler |
|
18 |
|
19 from .NetworkReply import NetworkReply |
|
20 |
|
21 QtDocPath = "qthelp://com.trolltech." |
|
22 |
|
23 ExtensionMap = { |
|
24 ".bmp": "image/bmp", |
|
25 ".css": "text/css", |
|
26 ".gif": "image/gif", |
|
27 ".html": "text/html", |
|
28 ".htm": "text/html", |
|
29 ".ico": "image/x-icon", |
|
30 ".jpeg": "image/jpeg", |
|
31 ".jpg": "image/jpeg", |
|
32 ".js": "application/x-javascript", |
|
33 ".mng": "video/x-mng", |
|
34 ".pbm": "image/x-portable-bitmap", |
|
35 ".pgm": "image/x-portable-graymap", |
|
36 ".pdf": "application/pdf", |
|
37 ".png": "image/png", |
|
38 ".ppm": "image/x-portable-pixmap", |
|
39 ".rss": "application/rss+xml", |
|
40 ".svg": "image/svg+xml", |
|
41 ".svgz": "image/svg+xml", |
|
42 ".text": "text/plain", |
|
43 ".tif": "image/tiff", |
|
44 ".tiff": "image/tiff", |
|
45 ".txt": "text/plain", |
|
46 ".xbm": "image/x-xbitmap", |
|
47 ".xml": "text/xml", |
|
48 ".xpm": "image/x-xpm", |
|
49 ".xsl": "text/xsl", |
|
50 ".xhtml": "application/xhtml+xml", |
|
51 ".wml": "text/vnd.wap.wml", |
|
52 ".wmlc": "application/vnd.wap.wmlc", |
|
53 } |
|
54 |
|
55 |
|
56 class QtHelpAccessHandler(SchemeAccessHandler): |
|
57 """ |
|
58 Class implementing a scheme access handler for QtHelp. |
|
59 """ |
|
60 def __init__(self, engine, parent=None): |
|
61 """ |
|
62 Constructor |
|
63 |
|
64 @param engine reference to the help engine (QHelpEngine) |
|
65 @param parent reference to the parent object (QObject) |
|
66 """ |
|
67 SchemeAccessHandler.__init__(self, parent) |
|
68 |
|
69 self.__engine = engine |
|
70 |
|
71 def __mimeFromUrl(self, url): |
|
72 """ |
|
73 Private method to guess the mime type given an URL. |
|
74 |
|
75 @param url URL to guess the mime type from (QUrl) |
|
76 @return mime type for the given URL (string) |
|
77 """ |
|
78 path = url.path() |
|
79 ext = os.path.splitext(path)[1].lower() |
|
80 if ext in ExtensionMap: |
|
81 return ExtensionMap[ext] |
|
82 else: |
|
83 return "application/octet-stream" |
|
84 |
|
85 def createRequest(self, op, request, outgoingData=None): |
|
86 """ |
|
87 Public method to create a request. |
|
88 |
|
89 @param op the operation to be performed |
|
90 (QNetworkAccessManager.Operation) |
|
91 @param request reference to the request object (QNetworkRequest) |
|
92 @param outgoingData reference to an IODevice containing data to be sent |
|
93 (QIODevice) |
|
94 @return reference to the created reply object (QNetworkReply) |
|
95 """ |
|
96 url = request.url() |
|
97 strUrl = url.toString() |
|
98 |
|
99 # For some reason the url to load is already wrong (passed from webkit) |
|
100 # though the css file and the references inside should work that way. |
|
101 # One possible problem might be that the css is loaded at the same |
|
102 # level as the html, thus a path inside the css like |
|
103 # (../images/foo.png) might cd out of the virtual folder |
|
104 if not self.__engine.findFile(url).isValid(): |
|
105 if strUrl.startswith(QtDocPath): |
|
106 newUrl = request.url() |
|
107 if not newUrl.path().startswith("/qdoc/"): |
|
108 newUrl.setPath("qdoc" + newUrl.path()) |
|
109 url = newUrl |
|
110 strUrl = url.toString() |
|
111 |
|
112 mimeType = mimetypes.guess_type(strUrl)[0] |
|
113 if mimeType is None: |
|
114 # do our own (limited) guessing |
|
115 mimeType = self.__mimeFromUrl(url) |
|
116 |
|
117 if self.__engine.findFile(url).isValid(): |
|
118 data = self.__engine.fileData(url) |
|
119 else: |
|
120 data = QByteArray(self.tr( |
|
121 """<title>Error 404...</title>""" |
|
122 """<div align="center"><br><br>""" |
|
123 """<h1>The page could not be found</h1><br>""" |
|
124 """<h3>'{0}'</h3></div>""").format(strUrl).encode("utf-8")) |
|
125 return NetworkReply(request, data, mimeType, self.parent()) |