Helpviewer/Network/QtHelpAccessHandler.py

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

eric ide

mercurial