allow to add/remove/list services:
[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 # pretty basic resource which is just present to have a way to start a
7 # forced refresh through the webif
8 class EPGRefreshResource(resource.Resource):
9         def __init__(self):
10                 resource.Resource.__init__(self)
11
12         def render(self, req):
13                 do_add = req.args.has_key("add")
14                 do_del = req.args.has_key("del")
15                 state = False
16
17                 if req.args.has_key("refresh"):
18                         # forced refresh
19                         if epgrefresh.forceRefresh():
20                                 output = "initiated refresh"
21                                 state = True
22                         else:
23                                 output = "could not initiate refresh"
24                 elif do_add or do_del:
25                         # add/remove service/bouquet
26                         if do_add:
27                                 sref = req.args["add"][0]
28                                 # strip all after last : (custom name)
29                                 pos = sref.rfind(':')
30                                 if pos != -1:
31                                         sref = sref[:pos+1]
32                         else:
33                                 sref = req.args["del"][0]
34
35                         duration = req.args.get("duration", None)
36                         duration = duration and int(duration)
37                         epgservice = EPGRefreshService(sref, duration)
38
39                         if sref:
40                                 ref = eServiceReference(str(sref))
41                                 if not ref.valid():
42                                         output = "invalid argument"
43                                 elif (ref.flags & 7) == 7:
44                                         # bouquet
45                                         if epgservice in epgrefresh.services[1]:
46                                                 if do_add:
47                                                         output = "bouquet already in list"
48                                                 else:
49                                                         epgrefresh.services[1].remove(epgservice)
50                                                         output = "bouquet removed from list"
51                                                         state = True
52                                         else:
53                                                 if do_del:
54                                                         output = "bouquet not in list"
55                                                 else:
56                                                         epgrefresh.services[1].add(epgservice)
57                                                         output = "bouquet added to list"
58                                                         state = True
59                                 else:
60                                         # assume service
61                                         if epgservice in epgrefresh.services[0]:
62                                                 if do_add:
63                                                         output = "service already in list"
64                                                 else:
65                                                         epgrefresh.services[0].remove(epgservice)
66                                                         output = "service removed from list"
67                                                         state = True
68                                         else:
69                                                 if do_del:
70                                                         output = "service not in list"
71                                                 else:
72                                                         epgrefresh.services[0].add(epgservice)
73                                                         output = "service added to list"
74                                                         state = True
75
76                                 # save if list changed
77                                 if state:
78                                         epgrefresh.saveConfiguration()
79                         else:
80                                 output = "invalid argument"
81                 elif req.args.has_key("list"):
82                         # show xml
83                         req.setResponseCode(http.OK)
84                         req.setHeader('Content-type', 'application; xhtml+xml')
85                         req.setHeader('charset', 'UTF-8')
86                         return ''.join(epgrefresh.buildConfiguration())
87                 else:
88                         output = "unknown command"
89
90                 result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
91                         <e2simplexmlresult>
92                                 <e2state>%s</e2state>
93                                 <e2statetext>%s</e2statetext>
94                         </e2simplexmlresult>
95                         """ % ('true' if state else 'false', output)
96
97                 req.setResponseCode(http.OK)
98                 req.setHeader('Content-type', 'application; xhtml+xml')
99                 req.setHeader('charset', 'UTF-8')
100                 
101                 return result