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