allow to change settings via webif:
[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 from Components.config import config
6
7 class EPGRefreshStartRefreshResource(resource.Resource):
8         def render(self, req):
9                 state = False
10
11                 if epgrefresh.forceRefresh():
12                         output = "initiated refresh"
13                         state = True
14                 else:
15                         output = "could not initiate refresh"
16
17                 req.setResponseCode(http.OK)
18                 req.setHeader('Content-type', 'application; xhtml+xml')
19                 req.setHeader('charset', 'UTF-8')
20
21                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
22                         <e2simplexmlresult>
23                                 <e2state>%s</e2state>
24                                 <e2statetext>%s</e2statetext>
25                         </e2simplexmlresult>
26                         """ % ('true' if state else 'false', output)
27
28 class EPGRefreshAddRemoveServiceResource(resource.Resource):
29         TYPE_ADD = 0
30         TYPE_DEL = 1
31
32         def __init__(self, type):
33                 assert(type in (self.TYPE_ADD, self.TYPE_DEL))
34                 self.type = type
35
36         def render(self, req):
37                 do_add = self.type == self.TYPE_ADD
38                 state = False
39
40                 if 'sref' in req.args:
41                         sref = req.args["sref"][0]
42                         if do_add:
43                                 # strip all after last : (custom name)
44                                 pos = sref.rfind(':')
45                                 if pos != -1:
46                                         sref = sref[:pos+1]
47
48                         duration = req.args.get("duration", None)
49                         try:
50                                 duration = duration and int(duration)
51                         except ValueError, ve:
52                                 output = 'invalid value for "duration": ' + str(duration)
53                         else:
54                                 epgservice = EPGRefreshService(sref, duration)
55
56                                 if sref:
57                                         ref = eServiceReference(str(sref))
58                                         if not ref.valid():
59                                                 output = 'invalid value for "sref": ' + str(sref)
60                                         elif (ref.flags & 7) == 7:
61                                                 # bouquet
62                                                 if epgservice in epgrefresh.services[1]:
63                                                         if do_add:
64                                                                 output = "bouquet already in list"
65                                                         else:
66                                                                 epgrefresh.services[1].remove(epgservice)
67                                                                 output = "bouquet removed from list"
68                                                                 state = True
69                                                 else:
70                                                         if do_add:
71                                                                 epgrefresh.services[1].add(epgservice)
72                                                                 output = "bouquet added to list"
73                                                                 state = True
74                                                         else:
75                                                                 output = "bouquet not in list"
76                                         else:
77                                                 # assume service
78                                                 if epgservice in epgrefresh.services[0]:
79                                                         if do_add:
80                                                                 output = "service already in list"
81                                                         else:
82                                                                 epgrefresh.services[0].remove(epgservice)
83                                                                 output = "service removed from list"
84                                                                 state = True
85                                                 else:
86                                                         if do_add:
87                                                                 epgrefresh.services[0].add(epgservice)
88                                                                 output = "service added to list"
89                                                                 state = True
90                                                         else:
91                                                                 output = "service not in list"
92
93                                         # save if list changed
94                                         if state:
95                                                 epgrefresh.saveConfiguration()
96                                 else:
97                                         output = 'invalid value for "sref": ' + str(sref)
98                 else:
99                         output = 'missing argument "sref"'
100
101                 req.setResponseCode(http.OK)
102                 req.setHeader('Content-type', 'application; xhtml+xml')
103                 req.setHeader('charset', 'UTF-8')
104                 
105                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
106                         <e2simplexmlresult>
107                                 <e2state>%s</e2state>
108                                 <e2statetext>%s</e2statetext>
109                         </e2simplexmlresult>
110                         """ % ('true' if state else 'false', output)
111
112 class EPGRefreshListServicesResource(resource.Resource):
113         def render(self, req):
114                 # show xml
115                 req.setResponseCode(http.OK)
116                 req.setHeader('Content-type', 'application; xhtml+xml')
117                 req.setHeader('charset', 'UTF-8')
118                 return ''.join(epgrefresh.buildConfiguration(webif = True))
119
120 class EPGRefreshSettingsResource(resource.Resource):
121         def render(self, req):
122                 for key, value in req.args.iteritems():
123                         value = value[0]
124                         if key == "enabled":
125                                 config.plugins.epgrefresh.enabled.value = True if value == "true" else False
126                         elif key == "begin":
127                                 config.plugins.epgrefresh.begin.value = int(value)
128                         elif key == "end":
129                                 config.plugins.epgrefresh.end.value = int(value)
130                         elif key == "interval":
131                                 config.plugins.epgrefresh.interval.value = int(value)
132                         elif key == "delay_standby":
133                                 config.plugins.epgrefresh.delay_standby.value = int(value)
134                         elif key == "inherit_autotimer":
135                                 config.plugins.epgrefresh.inherit_autotimer.value = True if value == "true" else False
136                         elif key == "afterevent":
137                                 config.plugins.epgrefresh.afterevent.value = True if value == "true" else False
138                         elif key == "force":
139                                 config.plugins.epgrefresh.force.value = True if value == "true" else False
140                         elif key == "wakeup":
141                                 config.plugins.epgrefresh.wakeup.value = True if value == "true" else False
142                         elif key == "pase_autotimer":
143                                 config.plugins.epgrefresh.parse_autotimer.value = True if value == "true" else False
144
145                 config.plugins.epgrefresh.save()
146
147                 if config.plugins.epgrefresh.enabled.value:
148                         epgrefresh.start()
149                 else:
150                         epgrefresh.stop()
151
152                 req.setResponseCode(http.OK)
153                 req.setHeader('Content-type', 'application; xhtml+xml')
154                 req.setHeader('charset', 'UTF-8')
155
156                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
157                         <e2simplexmlresult>
158                                 <e2state>true</e2state>
159                                 <e2statetext>config changed.</e2statetext>
160                         </e2simplexmlresult>
161                         """
162