change xml tags in webif hook
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshResource.py
1 from twisted.web import http, resource
2 from EPGRefresh import epgrefresh
3 from EPGRefreshService import EPGRefreshService
4 from enigma import eServiceReference
5
6 class EPGRefreshStartRefreshResource(resource.Resource):
7         def render(self, req):
8                 state = False
9
10                 if epgrefresh.forceRefresh():
11                         output = "initiated refresh"
12                         state = True
13                 else:
14                         output = "could not initiate refresh"
15
16                 req.setResponseCode(http.OK)
17                 req.setHeader('Content-type', 'application; xhtml+xml')
18                 req.setHeader('charset', 'UTF-8')
19
20                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
21                         <e2simplexmlresult>
22                                 <e2state>%s</e2state>
23                                 <e2statetext>%s</e2statetext>
24                         </e2simplexmlresult>
25                         """ % ('true' if state else 'false', output)
26
27 class EPGRefreshAddRemoveServiceResource(resource.Resource):
28         TYPE_ADD = 0
29         TYPE_DEL = 1
30
31         def __init__(self, type):
32                 assert(type in (self.TYPE_ADD, self.TYPE_DEL))
33                 self.type = type
34
35         def render(self, req):
36                 do_add = self.type == self.TYPE_ADD
37                 state = False
38
39                 if 'sref' in req.args:
40                         sref = req.args["sref"][0]
41                         if do_add:
42                                 # strip all after last : (custom name)
43                                 pos = sref.rfind(':')
44                                 if pos != -1:
45                                         sref = sref[:pos+1]
46
47                         duration = req.args.get("duration", None)
48                         try:
49                                 duration = duration and int(duration)
50                         except ValueError, ve:
51                                 output = 'invalid value for "duration": ' + str(duration)
52                         else:
53                                 epgservice = EPGRefreshService(sref, duration)
54
55                                 if sref:
56                                         ref = eServiceReference(str(sref))
57                                         if not ref.valid():
58                                                 output = 'invalid value for "sref": ' + str(sref)
59                                         elif (ref.flags & 7) == 7:
60                                                 # bouquet
61                                                 if epgservice in epgrefresh.services[1]:
62                                                         if do_add:
63                                                                 output = "bouquet already in list"
64                                                         else:
65                                                                 epgrefresh.services[1].remove(epgservice)
66                                                                 output = "bouquet removed from list"
67                                                                 state = True
68                                                 else:
69                                                         if do_add:
70                                                                 epgrefresh.services[1].add(epgservice)
71                                                                 output = "bouquet added to list"
72                                                                 state = True
73                                                         else:
74                                                                 output = "bouquet not in list"
75                                         else:
76                                                 # assume service
77                                                 if epgservice in epgrefresh.services[0]:
78                                                         if do_add:
79                                                                 output = "service already in list"
80                                                         else:
81                                                                 epgrefresh.services[0].remove(epgservice)
82                                                                 output = "service removed from list"
83                                                                 state = True
84                                                 else:
85                                                         if do_add:
86                                                                 epgrefresh.services[0].add(epgservice)
87                                                                 output = "service added to list"
88                                                                 state = True
89                                                         else:
90                                                                 output = "service not in list"
91
92                                         # save if list changed
93                                         if state:
94                                                 epgrefresh.saveConfiguration()
95                                 else:
96                                         output = 'invalid value for "sref": ' + str(sref)
97                 else:
98                         output = 'missing argument "sref"'
99
100                 req.setResponseCode(http.OK)
101                 req.setHeader('Content-type', 'application; xhtml+xml')
102                 req.setHeader('charset', 'UTF-8')
103                 
104                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
105                         <e2simplexmlresult>
106                                 <e2state>%s</e2state>
107                                 <e2statetext>%s</e2statetext>
108                         </e2simplexmlresult>
109                         """ % ('true' if state else 'false', output)
110
111 class EPGRefreshListServicesResource(resource.Resource):
112         def render(self, req):
113                 # show xml
114                 req.setResponseCode(http.OK)
115                 req.setHeader('Content-type', 'application; xhtml+xml')
116                 req.setHeader('charset', 'UTF-8')
117                 return ''.join(epgrefresh.buildConfiguration(webif = True))
118