copy + paste error
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / Timer.py
1 from enigma import *
2 from enigma import eServiceReference 
3 from enigma import eServiceCenter
4
5 from Components.Sources.Source import Source
6 from ServiceReference import ServiceReference
7 from RecordTimer import RecordTimerEntry, RecordTimer, AFTEREVENT,parseEvent
8
9 import time, string
10 # import sys, traceback
11
12 class Timer( Source):
13     LIST = 0
14     ADDBYID = 1
15     ADD = 2
16     DEL = 3
17     TVBROWSER = 4
18     
19     def __init__(self, session,func = LIST):
20         self.func = func
21         Source.__init__(self)        
22         self.session = session
23         self.recordtimer = session.nav.RecordTimer
24         self.epgcache = eEPGCache.getInstance()
25
26     def handleCommand(self,cmd):
27         if self.func is self.ADDBYID:
28             self.result = self.addTimerByEventID(cmd)
29         elif self.func is self.ADD:
30             self.result = self.addTimer(cmd)
31         elif self.func is self.TVBROWSER:
32             self.result = self.tvBrowser(cmd)
33         elif self.func is self.DEL:
34             self.result = self.delTimer(cmd)
35         else:
36             self.result = False,"unknown command"
37
38     def delTimer(self,param):
39         # is there an easier and better way? :\ 
40         print "delTimer",param
41         
42         if param['serviceref'] is None:
43             return False,"ServiceReference missing"
44         else: 
45             serviceref = ServiceReference(param['serviceref'])
46         
47         if param['begin'] is None:
48            return False,"begin missing"
49         else:
50             begin = float(param['begin'])
51         
52         if param['end'] is None:
53             return False,"end missing"
54         else:
55                 end = float(param['end'])
56              
57         toDelete = None
58         try:
59             print "timer_list ", self.recordtimer.timer_list
60             print "processed_timers", self.recordtimer.processed_timers
61             for x in self.recordtimer.timer_list + self.recordtimer.processed_timers:
62                 print "x.begin(%s), x.end(%s), x.service_ref(%s)" % (x.begin, x.end, x.service_ref)
63                 if str(x.service_ref) == str(serviceref) and float(x.begin) == begin and float(x.end) == end:
64                                   toDelete = x
65         except:
66             print "Fehler\n"
67             
68         if toDelete is not None:
69                 self.recordtimer.removeEntry(toDelete)
70                 return True,"Timer removed"
71         else:
72                 return False,"Timer not found"
73                 print "Timer not found"
74         
75         #self.session.nav.RecordTimer.saveTimer()
76     def tvBrowser(self,param):
77         print "addTimerByEventID",param
78         
79         syear = 0
80         if param['syear'] is None:
81            return False,"syear missing"
82         else:
83             syear = int(param['syear'])
84         
85         smonth = 0
86         if param['smonth'] is None:
87            return False,"smonth missing"
88         else:
89             smonth = int(param['smonth'])
90         
91         sday = 0
92         if param['sday'] is None:
93            return False,"sday missing"
94         else:
95             sday = int(param['sday'])
96         
97         shour = 0
98         if param['shour'] is None:
99            return False,"shour missing"
100         else:
101             shour = int(param['shour'])
102         
103         smin = 0
104         if param['smin'] is None:
105            return False,"smin missing"
106         else:
107             smin = int(param['smin'])
108             
109         eyear = 0
110         if param['eyear'] is None:
111            return False,"eyear missing"
112         else:
113             eyear = int(param['eyear'])
114         
115         emonth = 0
116         if param['emonth'] is None:
117            return False,"emonth missing"
118         else:
119             emonth = int(param['emonth'])
120         
121         eday = 0
122         if param['eday'] is None:
123            return False,"eday missing"
124         else:
125             eday = int(param['eday'])
126         
127         ehour = 0
128         if param['ehour'] is None:
129            return False,"ehour missing"
130         else:
131             ehour = int(param['ehour'])
132         
133         emin = 0
134         if param['emin'] is None:
135            return False,"emin missing"
136         else:
137             emin = int(param['emin'])
138         
139         # for compatibility reasons
140         if param['serviceref'] is None:
141             return False,"ServiceReference missing"
142         else:
143             takeApart = string.split(param['serviceref'], '|')
144             if len(takeApart) > 1:
145                 param['serviceref'] = takeApart[1]
146
147         param['begin'] = int( time.strftime("%s",  time.localtime(time.mktime( (syear, smonth, sday, shour, smin, 0, 0, 0, 0) ) ) ) )
148         param['end']   = int( time.strftime("%s",  time.localtime(time.mktime( (eyear, emonth, eday, ehour, emin, 0, 0, 0, 0) ) ) ) )
149         
150         if param['command'] == "add":
151             return self.addTimer(param)
152         elif param['command'] == "del":
153             return self.delTimer(param)
154         else:
155             return False,"command missing"
156         
157     def addTimer(self,param):
158         # is there an easier and better way? :\ 
159         print "addTimer",param
160         if param['serviceref'] is None:
161             return False,"ServiceReference missing"
162         else: 
163             serviceref = ServiceReference(param['serviceref'])
164         
165         if param['begin'] is None:
166            return False,"begin missing"
167         elif time.time() <= float(param['begin']):
168             begin = float(param['begin'])
169         else:
170             return False,"incorrect time begin"
171             
172         
173         if param['end'] is None:
174             return False,"end missing"
175         elif begin < float(param['end']):
176             end = float(param['end'])
177         else:
178              return False,"incorrect time end"
179                 
180         if param['name'] is None:
181             return False,"name is missing"
182         else:
183             name = param['name']
184             
185         if param['description'] is not None:
186             description = param['description']
187         else: 
188             description = ""
189             
190         if param['disabled'] =="0":
191             disabled = False
192         elif param['disabled'] =="1":
193             disabled = True
194         else:
195             return False,"disabled incorrect"
196         
197         if param['justplay'] == "0":
198             justplay = False
199         elif param['justplay'] == "1":
200             justplay = True
201         else:
202             return False,"justplay incorrect"
203             
204         if param['afterevent'] == "0":
205             afterevent = 0
206         elif param['afterevent'] == "1":
207             afterevent = 1
208         elif param['afterevent'] == "2":
209             afterevent = 2
210         else:
211             return False,"afterevent incorrect"
212             
213         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
214         self.recordtimer.record(newtimer)
215         #self.session.nav.RecordTimer.saveTimer()
216         return True,"Timer added"        
217
218     def addTimerByEventID(self,param):
219         print "addTimerByEventID",param['serviceref'],param['eventid']
220         if param['serviceref'] is None:
221             return False,"ServiceReference not set"
222         if param['eventid'] is None:
223             return False,"Eventid not set"
224         epgcache = eEPGCache.getInstance()
225         event = epgcache.lookupEventId(eServiceReference(param['serviceref']),int(param['eventid']))
226         if event is None:
227             return False,"Eventid not found"
228         (begin, end, name, description, eit) =parseEvent(event)
229         justplay = False
230         if param['justplay'] is None and param['justplay'] == "True":
231             justplay = True
232         
233         newtimer = RecordTimerEntry(ServiceReference(param['serviceref']), begin, end, name, description, eit, False, justplay, AFTEREVENT.NONE)
234         self.recordtimer.record(newtimer)
235         return True,"Timer added"    
236             
237     def getText(self):    
238         (result,text) = self.result
239         xml = "<?xml version=\"1.0\"?>\n"
240         xml  += "<e2timeraddresult>\n"
241         if result:
242             xml += "<e2state>True</e2state>\n"
243         else:
244             xml += "<e2state>False</e2state>\n"            
245         xml += "<e2statetext>%s</e2statetext>\n"%text
246         xml += "</e2timeraddresult>\n"
247         return xml
248     
249     text = property(getText)    
250     
251     ## part for listfiller requests
252     def command(self):
253         timerlist = []
254 #        print "len(self.recordtimer.timer_list) ", len(self.recordtimer.timer_list)
255  #       print "timer_list ", self.recordtimer.timer_list
256   #      print "processed_timers", self.recordtimer.processed_timers
257 #        try:
258         for item in self.recordtimer.timer_list + self.recordtimer.processed_timers:
259             timer = []
260             timer.append(item.service_ref)
261             timer.append(item.service_ref.getServiceName())
262             timer.append(item.eit)
263             timer.append(item.name)
264             timer.append(item.description)
265             timer.append(item.disabled)
266             timer.append(item.begin)
267             timer.append(item.end)
268             timer.append(item.end - item.begin)
269             timer.append(item.start_prepare)
270             timer.append(item.justplay)
271             timer.append(item.afterEvent)
272             timer.append(item.log_entries)
273             
274             try:
275                 timer.append(item.Filename)
276             except:
277                 timer.append("")
278             
279             timer.append(item.backoff)       
280             
281             try:
282                 timer.append(item.next_activation)
283             except:
284                 timer.append("")
285                 
286             timer.append(item.first_try_prepare)  
287             timer.append(item.state)
288             timer.append(item.repeated)
289             timer.append(item.dontSave)
290             timer.append(item.cancelled)
291             
292             if item.eit is not None:
293                 event = self.epgcache.lookupEvent(['E',("%s" % item.service_ref ,2,item.eit)])
294                 if event[0][0] is not None:
295                     timer.append(event[0][0])
296                 else:
297                     timer.append("N/A")
298             else:
299                 timer.append("N/A")
300                 
301             timerlist.append(timer)
302 #        except:
303 #            print sys.exc_info()[0]
304 #            print sys.exc_info()[1]
305 #            print traceback.extract_tb(sys.exc_info()[2])
306             
307         return timerlist
308     
309     list = property(command)
310     lut = {"ServiceReference":0
311            ,"ServiceName": 1
312            ,"EIT":2
313            ,"Name":3
314            ,"Description":4
315            ,"Disabled":5
316            ,"TimeBegin":6
317            ,"TimeEnd":7
318            ,"Duration":8
319            ,"startPrepare":9
320            ,"justPlay":10
321            ,"afterEvent":11
322            ,"LogEntries":12
323            ,"Filename":13
324            ,"Backoff":14
325            ,"nextActivation":15
326            ,"firstTryPrepare":16
327            ,"State":17
328            ,"Repeated":18
329            ,"dontSave":19
330            ,"Cancled":20
331            ,"DescriptionExtended":21
332            }
333