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