Fix external Web-Modules
[enigma2-plugins.git] / webinterface / src / WebChilds / Screenpage.py
1 from twisted.web import resource, http, server, static
2
3 from Plugins.Extensions.WebInterface import webif
4
5 import os
6
7 """
8         define all files in /web to send no XML-HTTP-Headers here
9         all files listed here will get an Content-Type: application/xhtml+xml charset: UTF-8
10 """
11 AppTextHeaderFiles = ['stream.m3u.xml', 'ts.m3u.xml', 'streamcurrent.m3u.xml', 'movielist.m3u.xml', 'services.m3u.xml', ]
12
13 """
14  Actualy, the TextHtmlHeaderFiles should contain the updates.html.xml, but the IE then
15  has problems with unicode-characters
16 """
17 TextHtmlHeaderFiles = ['wapremote.xml', 'stream.xml', ]
18
19 """
20         define all files in /web to send no XML-HTTP-Headers here
21         all files listed here will get an Content-Type: text/html charset: UTF-8
22 """
23 NoExplicitHeaderFiles = ['getpid.xml', 'tvbrowser.xml', ]
24
25 class ScreenPage(resource.Resource):
26         def __init__(self, session, path, addSlash = False):            
27                 resource.Resource.__init__(self)
28                                 
29                 self.session = session
30                 self.path = path
31                 self.addSlash = addSlash
32                 
33         def render(self, request):      
34                 if os.path.isfile(self.path):   
35                         
36 # Set the Header according to what's requested                                                          
37                         if self.path.split("/")[-1] in AppTextHeaderFiles:                              
38                                 request.setResponseCode(http.OK)
39                                 request.setHeader('Content-Type', 'application/text')
40                                 
41                         elif self.path.split("/")[-1] in TextHtmlHeaderFiles or (self.path.endswith(".html.xml") and self.path.split("/")[-1] != "updates.html.xml"):
42                                 request.setResponseCode(http.OK)
43                                 request.setHeader('Content-Type', 'text/html; charset=UTF-8')                           
44                                                                                                                                                                         
45                         elif self.path.split("/")[-1] in NoExplicitHeaderFiles:
46                                 request.setResponseCode(http.OK)                                
47                                 
48                         else:
49                                 request.setResponseCode(http.OK)                                
50                                 request.setHeader('Content-Type', 'application/xhtml+xml; charset=UTF-8')       
51
52                         # now go and write the Output
53                         # request.finish() is called inside webif.py (requestFinish() which is called via renderPage())                 
54                         webif.renderPage(request, self.path, self.session) # login?             
55                 
56                 elif os.path.isdir(self.path) and self.addSlash is True:                        
57                         return self.getChild("/", request).render(request)
58                         
59                 else:
60                         request.setResponseCode(http.NOT_FOUND)
61                         request.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n')
62                         request.write("<html><head><title>Enigma2 WebControl</title></head><body><h1>404 - Page not found</h1></body></html>")
63                         request.finish()
64                                 
65                 return server.NOT_DONE_YET
66                                 
67
68         def getChild(self, path, request):      
69                 path = "%s/%s" %(self.path, path)
70                 
71                 if path[-1:] == "/":
72                         path += "index.html"
73                         
74                 path += ".xml"
75                 return ScreenPage(self.session, path)
76