discription for the tvBrowser function
[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     
77     def tvBrowser(self,param):
78         print "tvbrowser",param
79         
80         """ Therefor the URL's for the tvBrowser-Capture-Driver are:
81         
82             http://dreambox/web/tvbrowser? +
83             
84         To add something:
85             &command=add&action=0&syear={start_year}&smonth={start_month}&sday={start_day}&shour={start_hour}&smin={start_minute}&eyear={end_year}&emonth={end_month}&eday={end_day}&ehour={end_hour}&emin={end_minute}&serviceref={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(title, "utf8")}&afterevent=0&eit=&disabled=0&justplay=0
86         to delete something:
87             &command=del&action=0&syear={start_year}&smonth={start_month}&sday={start_day}&shour={start_hour}&smin={start_minute}&eyear={end_year}&emonth={end_month}&eday={end_day}&ehour={end_hour}&emin={end_minute}&serviceref={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(title, "utf8")}&afterevent=0&eit=&disabled=0&justplay=0
88         """
89         
90         syear = 0
91         if param['syear'] is None:
92            return False,"syear missing"
93         else:
94             syear = int(param['syear'])
95         
96         smonth = 0
97         if param['smonth'] is None:
98            return False,"smonth missing"
99         else:
100             smonth = int(param['smonth'])
101         
102         sday = 0
103         if param['sday'] is None:
104            return False,"sday missing"
105         else:
106             sday = int(param['sday'])
107         
108         shour = 0
109         if param['shour'] is None:
110            return False,"shour missing"
111         else:
112             shour = int(param['shour'])
113         
114         smin = 0
115         if param['smin'] is None:
116            return False,"smin missing"
117         else:
118             smin = int(param['smin'])
119             
120         eyear = 0
121         if param['eyear'] is None:
122            return False,"eyear missing"
123         else:
124             eyear = int(param['eyear'])
125         
126         emonth = 0
127         if param['emonth'] is None:
128            return False,"emonth missing"
129         else:
130             emonth = int(param['emonth'])
131         
132         eday = 0
133         if param['eday'] is None:
134            return False,"eday missing"
135         else:
136             eday = int(param['eday'])
137         
138         ehour = 0
139         if param['ehour'] is None:
140            return False,"ehour missing"
141         else:
142             ehour = int(param['ehour'])
143         
144         emin = 0
145         if param['emin'] is None:
146            return False,"emin missing"
147         else:
148             emin = int(param['emin'])
149         
150         # for compatibility reasons
151         if param['serviceref'] is None:
152             return False,"ServiceReference missing"
153         else:
154             takeApart = string.split(param['serviceref'], '|')
155             if len(takeApart) > 1:
156                 param['serviceref'] = takeApart[1]
157
158         param['begin'] = int( time.strftime("%s",  time.localtime(time.mktime( (syear, smonth, sday, shour, smin, 0, 0, 0, 0) ) ) ) )
159         param['end']   = int( time.strftime("%s",  time.localtime(time.mktime( (eyear, emonth, eday, ehour, emin, 0, 0, 0, 0) ) ) ) )
160         
161         if param['command'] == "add":
162             return self.addTimer(param)
163         elif param['command'] == "del":
164             return self.delTimer(param)
165         else:
166             return False,"command missing"
167         
168     def addTimer(self,param):
169         # is there an easier and better way? :\ 
170         print "addTimer",param
171         if param['serviceref'] is None:
172             return False,"ServiceReference missing"
173         else: 
174             serviceref = ServiceReference(param['serviceref'])
175         
176         if param['begin'] is None:
177            return False,"begin missing"
178         elif time.time() <= float(param['begin']):
179             begin = float(param['begin'])
180         else:
181             return False,"incorrect time begin"
182             
183         
184         if param['end'] is None:
185             return False,"end missing"
186         elif begin < float(param['end']):
187             end = float(param['end'])
188         else:
189              return False,"incorrect time end"
190                 
191         if param['name'] is None:
192             return False,"name is missing"
193         else:
194             name = param['name']
195             
196         if param['description'] is not None:
197             description = param['description']
198         else: 
199             description = ""
200             
201         if param['disabled'] =="0":
202             disabled = False
203         elif param['disabled'] =="1":
204             disabled = True
205         else:
206             return False,"disabled incorrect"
207         
208         if param['justplay'] == "0":
209             justplay = False
210         elif param['justplay'] == "1":
211             justplay = True
212         else:
213             return False,"justplay incorrect"
214             
215         if param['afterevent'] == "0":
216             afterevent = 0
217         elif param['afterevent'] == "1":
218             afterevent = 1
219         elif param['afterevent'] == "2":
220             afterevent = 2
221         else:
222             return False,"afterevent incorrect"
223             
224         newtimer = RecordTimerEntry(serviceref, begin, end, name, description, 0, disabled, justplay, afterevent)
225         self.recordtimer.record(newtimer)
226         #self.session.nav.RecordTimer.saveTimer()
227         return True,"Timer added"        
228
229     def addTimerByEventID(self,param):
230         print "addTimerByEventID",param['serviceref'],param['eventid']
231         if param['serviceref'] is None:
232             return False,"ServiceReference not set"
233         if param['eventid'] is None:
234             return False,"Eventid not set"
235         epgcache = eEPGCache.getInstance()
236         event = epgcache.lookupEventId(eServiceReference(param['serviceref']),int(param['eventid']))
237         if event is None:
238             return False,"Eventid not found"
239         (begin, end, name, description, eit) =parseEvent(event)
240         justplay = False
241         if param['justplay'] is None and param['justplay'] == "True":
242             justplay = True
243         
244         newtimer = RecordTimerEntry(ServiceReference(param['serviceref']), begin, end, name, description, eit, False, justplay, AFTEREVENT.NONE)
245         self.recordtimer.record(newtimer)
246         return True,"Timer added"    
247             
248     def getText(self):    
249         (result,text) = self.result
250         xml = "<?xml version=\"1.0\"?>\n"
251         xml  += "<e2timeraddresult>\n"
252         if result:
253             xml += "<e2state>True</e2state>\n"
254         else:
255             xml += "<e2state>False</e2state>\n"            
256         xml += "<e2statetext>%s</e2statetext>\n"%text
257         xml += "</e2timeraddresult>\n"
258         return xml
259     
260     text = property(getText)    
261     
262     ## part for listfiller requests
263     def command(self):
264         timerlist = []
265 #        print "len(self.recordtimer.timer_list) ", len(self.recordtimer.timer_list)
266  #       print "timer_list ", self.recordtimer.timer_list
267   #      print "processed_timers", self.recordtimer.processed_timers
268 #        try:
269         for item in self.recordtimer.timer_list + self.recordtimer.processed_timers:
270             timer = []
271             timer.append(item.service_ref)
272             timer.append(item.service_ref.getServiceName())
273             timer.append(item.eit)
274             timer.append(item.name)
275             timer.append(item.description)
276             timer.append(item.disabled)
277             timer.append(item.begin)
278             timer.append(item.end)
279             timer.append(item.end - item.begin)
280             timer.append(item.start_prepare)
281             timer.append(item.justplay)
282             timer.append(item.afterEvent)
283             timer.append(item.log_entries)
284             
285             try:
286                 timer.append(item.Filename)
287             except:
288                 timer.append("")
289             
290             timer.append(item.backoff)       
291             
292             try:
293                 timer.append(item.next_activation)
294             except:
295                 timer.append("")
296                 
297             timer.append(item.first_try_prepare)  
298             timer.append(item.state)
299             timer.append(item.repeated)
300             timer.append(item.dontSave)
301             timer.append(item.cancelled)
302             
303             if item.eit is not None:
304                 event = self.epgcache.lookupEvent(['E',("%s" % item.service_ref ,2,item.eit)])
305                 if event[0][0] is not None:
306                     timer.append(event[0][0])
307                 else:
308                     timer.append("N/A")
309             else:
310                 timer.append("N/A")
311                 
312             timerlist.append(timer)
313 #        except:
314 #            print sys.exc_info()[0]
315 #            print sys.exc_info()[1]
316 #            print traceback.extract_tb(sys.exc_info()[2])
317             
318         return timerlist
319     
320     list = property(command)
321     lut = {"ServiceReference":0
322            ,"ServiceName": 1
323            ,"EIT":2
324            ,"Name":3
325            ,"Description":4
326            ,"Disabled":5
327            ,"TimeBegin":6
328            ,"TimeEnd":7
329            ,"Duration":8
330            ,"startPrepare":9
331            ,"justPlay":10
332            ,"afterEvent":11
333            ,"LogEntries":12
334            ,"Filename":13
335            ,"Backoff":14
336            ,"nextActivation":15
337            ,"firstTryPrepare":16
338            ,"State":17
339            ,"Repeated":18
340            ,"dontSave":19
341            ,"Cancled":20
342            ,"DescriptionExtended":21
343            }
344