Changes for the function delTimer. Now, the new WebInterface can delete timers too.
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / Timer.py
1 from enigma import *
2 from enigma import eServiceReference 
3
4 from Components.Sources.Source import Source
5 from ServiceReference import ServiceReference
6 from RecordTimer import RecordTimerEntry, RecordTimer, AFTEREVENT,parseEvent
7
8 import time
9
10 class Timer( Source):
11     LIST = 0
12     ADDBYID = 1
13     ADD = 2
14     DEL = 3
15     
16     def __init__(self, session,func = LIST):
17         self.func = func
18         Source.__init__(self)        
19         self.session = session
20         self.recordtimer = session.nav.RecordTimer
21         
22     def handleCommand(self,cmd):
23         if self.func is self.ADDBYID:
24             self.result = self.addTimerByEventID(cmd)
25         elif self.func is self.ADD:
26             self.result = self.addTimer(cmd)
27         elif self.func is self.DEL:
28             self.result = self.delTimer(cmd)
29         else:
30             self.result = False,"unknown command"
31
32     def delTimer(self,param):
33         # is there an easier and better way? :\ 
34         print "delTimer",param
35         
36         if param['serviceref'] is None:
37             return False,"ServiceReference missing"
38         else: 
39             serviceref = ServiceReference(param['serviceref'])
40         
41         if param['begin'] is None:
42            return False,"begin missing"
43         else:
44             begin = float(param['begin'])
45         
46         if param['end'] is None:
47             return False,"end missing"
48         else:
49                 end = float(param['end'])
50              
51         toDelete = None
52         for x in self.session.nav.RecordTimer.timer_list+self.session.nav.RecordTimer.processed_timers:
53                 if str(x.service_ref) == str(serviceref) and float(x.begin) == begin and float(x.end) == end:
54                         toDelete = x
55             
56         if toDelete is not None:
57                 self.session.nav.RecordTimer.removeEntry(toDelete)
58                 return True,"Timer removed"
59         else:
60                 return False,"Timer not found"
61                 print "Timer not found"
62         
63         #self.session.nav.RecordTimer.saveTimer()
64     
65     def addTimer(self,param):
66         # is there an easier and better way? :\ 
67         print "addTimerByEventID",param
68         if param['serviceref'] is None:
69             return False,"ServiceReference missing"
70         else: 
71             serviceref = ServiceReference(param['serviceref'])
72         
73         if param['begin'] is None:
74            return False,"begin missing"
75         elif time.time() <= float(param['begin']):
76             begin = float(param['begin'])
77         else:
78             return False,"incorrect time begin"
79             
80         
81         if param['end'] is None:
82             return False,"end missing"
83         elif begin < float(param['end']):
84             end = float(param['end'])
85         else:
86              return False,"incorrect time end"
87                 
88         if param['name'] is None:
89             return False,"name is missing"
90         else:
91             name = param['name']
92             
93         if param['description'] is not None:
94             description = param['description']
95         else: 
96             description = ""
97             
98         if param['disabled'] =="0":
99             disabled = False
100         elif param['disabled'] =="1":
101             disabled = True
102         else:
103             return False,"disabled incorrect"
104         
105         if param['justplay'] == "0":
106             justplay = False
107         elif param['justplay'] == "1":
108             justplay = True
109         else:
110             return False,"justplay incorrect"
111             
112         if param['afterevent'] == "0":
113             afterevent = 0
114         elif param['afterevent'] == "1":
115             afterevent = 1
116         elif param['afterevent'] == "2":
117             afterevent = 2
118         else:
119             return False,"afterevent incorrect"
120             
121         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
122         self.session.nav.RecordTimer.record(newtimer)
123         #self.session.nav.RecordTimer.saveTimer()
124         return True,"Timer added"        
125
126     def addTimerByEventID(self,param):
127         print "addTimerByEventID",param['serviceref'],param['eventid']
128         if param['serviceref'] is None:
129             return False,"ServiceReference not set"
130         if param['eventid'] is None:
131             return False,"Eventid not set"
132         epgcache = eEPGCache.getInstance()
133         event = epgcache.lookupEventId(eServiceReference(param['serviceref']),int(param['eventid']))
134         if event is None:
135             return False,"Eventid not found"
136         (begin, end, name, description, eit) =parseEvent(event)
137         newtimer = RecordTimerEntry(ServiceReference(param['serviceref']), begin, end, name, description, eit, False, False, AFTEREVENT.NONE)
138         self.session.nav.RecordTimer.record(newtimer)
139         return True,"Timer added"    
140             
141     def getText(self):    
142         (result,text) = self.result
143         xml = "<?xml version=\"1.0\"?>\n"
144         xml  += "<e2timeraddresult>\n"
145         if result:
146             xml += "<e2state>True</e2state>\n"
147         else:
148             xml += "<e2state>False</e2state>\n"            
149         xml += "<e2statetext>%s</e2statetext>\n"%text
150         xml += "</e2timeraddresult>\n"
151         return xml
152     
153     text = property(getText)    
154     
155     ## part for listfiller requests
156     def command(self):
157         timerlist = []
158         for item in self.recordtimer.timer_list+self.recordtimer.processed_timers:
159             timer = []
160             timer.append(item.service_ref)
161             timer.append(item.service_ref.getServiceName())
162             timer.append(item.eit)
163             timer.append(item.name)
164             timer.append(item.description)
165             timer.append(item.disabled)
166             timer.append(item.begin)
167             timer.append(item.end)
168             timer.append(item.end-item.begin)
169             timer.append(item.start_prepare)
170             timer.append(item.justplay)
171             timer.append(item.afterEvent)
172             timer.append(item.log_entries)
173             try: 
174                 timer.append(item.Filename)
175             except AttributeError:
176                 timer.append("")
177             
178             timer.append(item.backoff)       
179             try:
180                 timer.append(item.next_activation)
181             except AttributeError:
182                 timer.append("")
183             timer.append(item.first_try_prepare)  
184             timer.append(item.state)  
185             timer.append(item.repeated)
186             timer.append(item.dontSave)
187             timer.append(item.cancelled)
188             timerlist.append(timer) 
189             
190         return timerlist
191     
192     list = property(command)
193     lut = {"ServiceReference": 0
194            ,"ServiceName": 1
195            ,"EIT":2
196            ,"Name":3
197            ,"Description":4
198            ,"Disabled":5
199            ,"TimeBegin":6
200            ,"TimeEnd":7
201            ,"Duration":8
202            ,"startPrepare":9
203            ,"justPlay":10
204            ,"afterEvent":11
205            ,"LogEntries":12
206            ,"Filename":13
207            ,"Backoff":14
208            ,"nextActivation":15
209            ,"firstTryPrepare":16
210            ,"State":17
211            ,"Repeated":18
212            ,"dontSave":19
213            ,"Cancled":20
214            }
215